File: GoodyLoadable.c

package info (click to toggle)
fvwm95 2.0.43ba-15
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,356 kB
  • ctags: 4,759
  • sloc: ansic: 46,398; makefile: 1,586; sh: 782; perl: 328
file content (417 lines) | stat: -rw-r--r-- 9,482 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <stdio.h>
#include <dlfcn.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

#include <fvwm/fvwmlib.h>
#include "GoodyLoadable.h"
#include "FvwmTaskBar.h"
#include "Goodies.h"

extern int win_width;
extern int stwin_width;
extern GC statusgc;
extern int icons_offset;

void RestoreGC(void);

int Report = 1;

#define PathSize 1000


struct GoodyLoadableInfo {
	char *file;
	char *symbol;
	char *id;
	void *handle;
	void *data;	
};
	
struct GoodyLoadableInfo  **GLI = NULL;
struct GoodyLoadable	  **GL  = NULL;
int GLfree = -1,
    GLsize = -1,
    GLtip  = -1;

#define GLS 10


/* Why two checks ?
        because when there is an error (say not enough memory)
        the routines don't exit but simply do nothing.
        This allows FvwmTaskBar keep running even if you misspelled
        something in your .fvwmrc

   Why no ShrinkGL ?
   	because we don't need it :)
   	
   Why GLS ? Why such a bad list ?
   	first you don't need more then 10 icons there..definitely not more
   	then 30 !
   	secondly, we do this only on startup.
   	thirdly it's not that bad - we keep only pointers. the extra space
   	used is measured in bytes..
   	
   Why NULL ?
   	Hmm it seems like a sane idea. If it so happens that your whatever
   	platform you are running this in give NULL pointers when you call calloc
   	do this : 
   	  1.replace everywhere in the file calloc with mycalloc
   	  2.define a mycalloc at the beginning of this file:
   	  void *mycalloc(size_t a, sizeo_t b)
   	  {
   	    void *aa, *bb;
   	    aa = calloc(a, b);
   	    if (aa != NULL) return aa;
   	    bb = calloc(a, b);
     	    free(aa); / * some error checking needed here * /
   	    return(bb);
   	  }
   	  
   	If your calloc can utilize same address for storing two pieces of data
   	then I'm sorry ! do something else
   	
*/   	

/* looks like we will have to introduce plugins directory */
#ifndef PLUGINS
#define PLUGINS "/usr/local/lib/X11/fvwm95"
#endif
char *plugins = PLUGINS;

void **Handles = NULL;
int HandlesFree = -1;
int HandlesSize = -1;    	   	        
        

void InitHandles(void)
{
  Handles = (void**)calloc(GLS, sizeof(void *));
  if (Handles == NULL) {
    perror("FvwmTaskBar.GoodyLoadable.InitHandles()");
    return;
  }
  HandlesFree = 0;
  HandlesSize = GLS;
  GLtip = -1;
}

void ExpandHandles(void)
{
  void **h;
  int k;

  if ((HandlesFree < 0) || (HandlesSize < 0) || (Handles == NULL)) {
    InitHandles();
    return;
  }

  h = (void**)calloc(GLS+HandlesSize, sizeof(void *));
  if (h == NULL) {
    perror("FvwmTaskBar.GoodyLoadable.ExpandHandles()");
    return;
  }

  for (k=0; k<HandlesFree; k++) h[k] = Handles[k];
  free(Handles);
  Handles = h;
  HandlesSize += GLS;
}

int isSO(char *name)
{
  int k;

  if (name == NULL) return 0;
  k = strlen(name);
  if (k < 3) return 0;
  return((name[k-1] == 'o') && (name[k-2] == 's') && (name[k-3] == '.'));
}

void AddHandle(char *plugin)
{
  if (!isSO(plugin)) return;
  if ((Handles == NULL) || (HandlesFree < 0 ) || (HandlesSize < 0))
    InitHandles();
  if (HandlesFree+1 > HandlesSize) ExpandHandles();
  if (HandlesFree+1 > HandlesSize) return;
  Handles[HandlesFree] = NULL;
#if defined(NO_DL)
  Handles[HandlesFree] = dlopen(0, RTLD_NOW);
#else
  Handles[HandlesFree] = dlopen(plugin, RTLD_NOW);
#endif
  if(Handles[HandlesFree] == NULL) {
    if (Report) {
      printf("FvwmTaskBar.GoodyLoadable.AddHandle(\"%s\"): error\n%s\n",
             plugin, dlerror());
    }
    return;
  }
  HandlesFree++;
  if (Report) {
    printf("FvwmTaskBar.GoodyLoadable.AddHandle(\"%s\"): plugin \"%s\" loaded.\n",
           plugin, plugin);
    fflush(stdout);
  }
}

/* please don't reuse me !*/
char path[PathSize];

void LoadPlugins(void)
{
  DIR *dir;
  struct dirent *dp;

  dir = NULL;
  dir = opendir(plugins);
  if (dir == NULL) {
    perror("FvwmTaskBar.GoodyLoadable.LoadPlugins()");
    return;
  }

  while ((dp = readdir(dir)) != NULL) {
    path[0] = 0;
    strcat(strcat(path, plugins), "/");
    AddHandle(strcat(path, dp->d_name));
  }
  (void) closedir(dir);
}
	     
    
void InitGL(void)
{
  GL = (struct GoodyLoadable**)calloc(GLS, sizeof(struct GoodyLoadable *));
  GLI = (struct GoodyLoadableInfo**)calloc(GLS, sizeof(struct GoodyLoadableInfo *));
  if ((GL == NULL) || (GLI == NULL)) {
    perror("FvwmTaskBar.GoodyLoadable.InitGL()");
    return;
  }
  GLfree = 0;
  GLsize = GLS;
}


void ExpandGL(void)
{
  struct GoodyLoadable **_GL;
  struct GoodyLoadableInfo **_GLI;
  int k;

  _GL = (struct GoodyLoadable**)calloc(GLsize+GLS, sizeof(struct GoodyLoadable *));
  _GLI = (struct GoodyLoadableInfo**)calloc(GLsize+GLS, sizeof(struct GoodyLoadableInfo *));
  if ((GL == NULL) || (GLI == NULL)) {
    perror("FvwmTaskBar.GoodyLoadable.ExpandGL()");
    return;
  }
  for (k=0; k<GLfree; k++) {
    _GL[k] = GL[k];
    _GLI[k] = GLI[k];
  }
  free(GL); GL = _GL;
  free(GLI); GLI = _GLI;
  GLsize += GLS;
}


void AddGLSymbol(char *symbol)
{
#ifdef __DEBUG__
  printf("FvwmTaskBar.GoodyLoadable.AddGLSymbol(\"%s\")\n", symbol);
  fflush(stdout);
#endif

  if ((GLfree < 0) || (GLsize < 0) || (GL == NULL) || (GLI == NULL)) 
    InitGL();

  if ((GLfree < 0) || (GLsize < 0) || (GL == NULL) || (GLI == NULL))
    return;

  if (GLfree+1 > GLsize) ExpandGL();
  if (GLfree+1 > GLsize) return;

  GL[GLfree] = NULL;
  GLI[GLfree] = NULL;

  GLI[GLfree] = (struct GoodyLoadableInfo*)calloc(1, sizeof(struct GoodyLoadableInfo));
  if (GLI[GLfree] == NULL) {
    perror("FvwmTaskBar.GoodyLoadable.AddGLinfo()");fflush(stderr);
    return;
  }
  GLI[GLfree]->symbol = symbol;
  GLI[GLfree]->id = (char*)calloc(20, sizeof(char));
  if (GLI[GLfree]->id == NULL) {
    perror("FvwmTaskBar.GoodyLoadable.AddGLSymbol()");
    fflush(stderr);
    free(GLI[GLfree]);
    return;
  }         
  sprintf(GLI[GLfree]->id, "%d", GLfree+1);
  GLI[GLfree]->data = NULL;
  GLfree++;           
}

void GLsetId(char *id)
{
  if (GLfree <= 0) return;
  if (GLI[GLfree-1] == NULL) return;

  free(GLI[GLfree-1]->id);
  GLI[GLfree-1]->id = id;
}


/* code searching loadables in this proc.. */
void LoadGL(int k)
{
  char *error;
  int i;

#ifdef __DEBUG__
  printf("FvwmTaskBar.GoodyLoadable.LoadGL(%d)\n", k);
  fflush(stdout);
#endif

  if ((k < 0) || (k >= GLfree)) return;
/*
  GLI[k]->handle = dlopen(GLI[k]->file, RTLD_NOW);
  if (!GLI[k]->handle) {
    fprintf(stderr,"FvwmTaskBar.GoodyLoadable.LoadGL():file %s:%s\n",GLI[k]->file,dlerror());
    fflush(stderr);
    return;
  }
*/

  if ((Handles == NULL) || (HandlesFree < 0) || (HandlesSize < 0))
    LoadPlugins();

  if ((Handles == NULL) || (HandlesFree < 0) || (HandlesSize < 0))
    return;

  if (GLI[k]->symbol == NULL) return;       

  if (HandlesFree == 0) {
    fprintf(stderr,"FvwmTaskBar.GoodyLoadable.LoadGL(): no plugins loaded\n");
    fflush(stderr);
    return;
  }

  i = 0;
  do {
    GL[k] = dlsym(Handles[i], GLI[k]->symbol);
    error = dlerror();
    i++;
  } while ((error != NULL) && (i < HandlesFree));

  if (error == NULL)
    GLI[k]->data = (*(GL[k]->LoadableInit))(GLI[k]->id, k);
  else
    GL[k] = NULL;
}

int ParseGLinfo(char *tline, char *Module, int Clength)
{
  int k;
  int go = 1;
  char *s;

  s = (char*)calloc(100, sizeof(char));
  if (s == NULL) {
    perror("FvwmTaskBar.GoodyLoadable.ParseGLResource()");
    return 0;
  }

  if(strncasecmp(tline,CatString3(Module, "GoodyLoadablePlugins",""),
                               Clength+20)==0) {
    CopyString(&s, &tline[Clength+21]);
    plugins = s;
    LoadPlugins();
    return(1);
  } else if(strncasecmp(tline,CatString3(Module, "GoodyLoadableQuiet",""),
                               Clength+18)==0) {
    Report = 0;
    return(1);
  } else if(strncasecmp(tline,CatString3(Module, "GoodyLoadableID",""),
                               Clength+15)==0) {
    CopyString(&s, &tline[Clength+16]);
    GLsetId(s);
    return(1);
  } else if(strncasecmp(tline,CatString3(Module, "GoodyLoadableSymbol",""),
                               Clength+19)==0) {
    CopyString(&s, &tline[Clength+19]);
    AddGLSymbol(s);
    return(1);
  } else {
    for (k=0; go && (k<GLfree); k++) {
      if (GL[k] == NULL) LoadGL(k);
      if (GL[k] != NULL) go = !(*(GL[k]->LoadableParseResource))(GLI[k]->data,tline,Module,Clength);
    }
    return go;
  }
}
  
void LoadableLoad(Display *dpy, Drawable win)
{
  int k;

  for (k=0; k<GLfree; k++) {
    if (GL[k] != NULL)
      (*(GL[k]->LoadableLoad))(GLI[k]->data, dpy, win);
  }
}

void LoadableDraw(Display *dpy,Drawable win)
{
  int k;

  for (k=0; k<GLfree; k++) {
    if (GL[k] != NULL)
      (*(GL[k]->LoadableDraw))(GLI[k]->data, dpy, win);
  }
  /* no need anymore - only modules */ /*RestoreGC(); */
}

int LoadableSeeMouse(int x, int y)
{
  int k;
  int go;

  go = 1;
  for (k=0; k<GLfree; k++)
  if (GL[k] != NULL) {
    if((*(GL[k]->LoadableSeeMouse))(GLI[k]->data, x, y)) return k+1;
  }

  return 0;
}	

int CreateLoadableTipWindow(int k)
{
  k--;
  if ((k < 0) || (k >= GLfree) || (GL[k] == NULL)) return 0;
  if (k != GLtip) {
    GLtip = k;
    (*(GL[k]->LoadableCreateTipWindow))(GLI[k]->data);
  }
  return GLOADABLE_TIP - k;
}

int IsLoadableTip(int type) {
  int k = GLOADABLE_TIP - type;
  return ((k >= 0) && (k < GLfree));
}

void HandleLoadableClick(XEvent event, int k)
{
  k--;
  if ((k < 0) || (k >= GLfree) || (GL[k] == NULL)) return;
  (*(GL[k]->HandleIconClick))(GLI[k]->data, event);
}

char *EnvExpand(char *str)
{
  return envDupExpand(str, 0);
}