File: CheckLockModule.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 (315 lines) | stat: -rw-r--r-- 8,296 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <sys/stat.h>

#include <X11/X.h>

#include <fvwm/fvwmlib.h>

#include "GoodyLoadable.h"
#include "FvwmTaskBar.h"

#include <X11/xpm.h>

/*
 * Be careful to avoid name collisions.
 * It's best to name all symbols used for external reference as
 * ModuleNameSymbol, where ModuleName is the name of the module
 * and Symbol is the name you would rather use.
 * More then one module may want to use a proc called Load !
 */

#include <unistd.h>
extern char *IconPath, *PixmapPath;

struct MyInfo {
    char *id;
    /* other stuff */
    char *command;
    char *tip;
    char *icon;
    Pixmap icon_pix;
    Pixmap icon_mask;
    XpmAttributes icon_attr;
    int offset;
    int visible;
    Time lastclick;
    int show; /* whether to show the icon */
    time_t lastchecked;
    char *lock;
};
 
void *CheckLockModuleInit(char *id, int k);
int CheckLockModuleParseResource(struct MyInfo *mif,char *tline,char *Module,int Clength);
void CheckLockModuleLoad(struct MyInfo *mif,Display *dpy,Drawable win);
void CheckLockModuleDraw(struct MyInfo *mif,Display *dpy,Window win);
int CheckLockModuleSeeMouse(struct MyInfo *mif,int x,int y);
void CheckLockModuleCreateIconTipWindow_(struct MyInfo *mif);
void CheckLockModuleIconClick(struct MyInfo *mif,XEvent event);

struct GoodyLoadable CheckLockModuleSymbol = {
  (LoadableInit_f)            &CheckLockModuleInit,
  (LoadableParseResource_f)   &CheckLockModuleParseResource,
  (LoadableLoad_f)            &CheckLockModuleLoad,
  (LoadableDraw_f)            &CheckLockModuleDraw,
  (LoadableSeeMouse_f)        &CheckLockModuleSeeMouse,
  (LoadableCreateTipWindow_f) &CheckLockModuleCreateIconTipWindow_,
  (HandleIconClick_f)         &CheckLockModuleIconClick
};

extern int win_width, stwin_width;
extern int RowHeight;

        

void *CheckLockModuleInit(char *id, int k)
{
  struct MyInfo *mif;

#ifdef __DEBUG__
  printf("FvwmTaskBar.CheckLockModule.Init(\"%s\")\n", id); fflush(stdout);
#endif

  mif = (struct MyInfo*)calloc(1, sizeof(struct MyInfo));
  if(mif == NULL) {
    perror("FvwmTaskBar.CheckLockModule.Init()");
    return NULL;
  }
  mif->id = id;
  mif->command = NULL;
  mif->icon = NULL;
  mif->tip = NULL;
  mif->lastclick = 0;
  mif->show = 0; /* don't show up by default */
  mif->lastchecked = 0;
  mif->lock = NULL;

  return mif;
}


void CheckLockModuleSetIcon(struct MyInfo *mif, char *i)
{
  char *path;
#ifdef __DEBUG__
  fprintf(stderr, "FvwmTaskBar.CheckLockModule.AddIcon(*,\"%s\")\n", i);
#endif
  if ((path=findIconFile(i,PixmapPath,R_OK)) ||
      (path=findIconFile(i,IconPath,R_OK))) {
    free(i);
    i = path;
  }

  if (mif == NULL) return;
  if (mif->icon != NULL) free(mif->icon);
  mif->icon = i;
}

#define SetIcon CheckLockModuleSetIcon


void CheckLockModuleSetIconCommand(struct MyInfo *mif, char *c)
{
  if (mif == NULL) return;
  if (mif->command != NULL) free(mif->command);
  mif->command = c;
}

#define SetIconCommand CheckLockModuleSetIconCommand


void CheckLockModuleSetIconTip(struct MyInfo *mif, char *c)
{
  if (mif == NULL) return;
  if (mif->tip != NULL) free(mif->tip);
  mif->tip = c;
}

#define SetIconTip CheckLockModuleSetIconTip


void CheckLockModuleSetLock(struct MyInfo *mif, char *c)
{
  if (mif == NULL) return;
  if (mif->lock != NULL) free(mif->lock);
  mif->lock = EnvExpand(c);
  free(c);
}

#define SetLock CheckLockModuleSetLock


int CheckLockModuleParseResource(struct MyInfo *mif, char *tline,
                                 char *Module, int Clength)
{
  char *s;

#ifdef __DEBUG__
  printf("FvwmTaskBar.CheckLockModule.ParseResource(\"%s\",\"%s\",*)\n",
         mif->id, tline);
  fflush(stdout);
#endif

  if (mif == NULL) return 0;

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

  if(strncasecmp(tline,CatString3(Module, "CheckLockModuleIcon",mif->id),
                               Clength+19+strlen(mif->id))==0) {
    CopyString(&s, &tline[Clength+20+strlen(mif->id)]);
    SetIcon(mif,s);
    return(1);
  } else if(strncasecmp(tline,CatString3(Module, "CheckLockModuleCommand",mif->id),
                               Clength+22+strlen(mif->id))==0) {
    CopyString(&s, &tline[Clength+23+strlen(mif->id)]);
    SetIconCommand(mif,s);
    return(1);
  } else if(strncasecmp(tline,CatString3(Module, "CheckLockModuleLockFile",mif->id),
                               Clength+23+strlen(mif->id))==0) {
    CopyString(&s, &tline[Clength+24+strlen(mif->id)]);
    SetLock(mif,s);
    return(1);
  } else if(strncasecmp(tline,CatString3(Module, "CheckLockModuleTip",mif->id),
                               Clength+18+strlen(mif->id))==0) {
    CopyString(&s, &tline[Clength+19+strlen(mif->id)]);
    SetIconTip(mif,s);
    return(1);
  } else return 0;
}


void CheckLockModuleLoad(struct MyInfo *mif, Display *dpy, Drawable win)
{
#ifdef __DEBUG__
  fprintf(stderr, "FvwmTaskBar.CheckLockModule.LoadModule()\n");
#endif

  if (XpmReadFileToPixmap(dpy, win, mif->icon,
                          &(mif->icon_pix), &(mif->icon_mask),
                          &(mif->icon_attr)) == XpmSuccess) {
    mif->visible = True;
    if ((mif->icon_attr.valuemask & XpmSize) == 0) {
      mif->icon_attr.width = 16;
      mif->icon_attr.height = 16;
    }

#if 0  /* does not appear to be needed */
    goodies_width += mif->icon_attr.width+2;
#endif
    mif->offset = icons_offset;
    icons_offset += mif->icon_attr.width+2;

#ifdef __DEBUG__
  fprintf(stderr, "    Loaded %s width=%d height=%d\n",
          mif->icon, mif->icon_attr.width, mif->icon_attr.height);
#endif   

  } else {
    mif->visible = False;
    fprintf(stderr, "FvwmTaskBar.CheckLockModule.LoadModule(): error loading %s\n"
                    "(FvwmTaskBarCheckLockModuleIcon%s)\n",
                    mif->icon, mif->id);
  }
}

void CheckLockModule_check_lock(struct MyInfo *mif)
{
  struct stat buf;
  int lock_status;

  if (mif == NULL) return;
  if (mif->lock == NULL) return;
  if (stat(mif->lock, &buf) < 0)
    lock_status = 0;
  else
    lock_status = 1;

  if (lock_status != mif->show) {
    mif->show = lock_status;
    RenewGoodies = 1;
  }
}


#define check_lock CheckLockModule_check_lock
#define start      (win_width-stwin_width)

extern GC statusgc;

void CheckLockModuleDraw(struct MyInfo *mif, Display *dpy, Window win)
{
  XGCValues gcv;
  time_t now;

  unsigned long CheckLockModulegcm = GCClipMask |
                                     GCClipXOrigin | GCClipYOrigin;
#define gcm CheckLockModulegcm

  if (mif == NULL) return;

  now = time(NULL);
  if (now-mif->lastchecked > 2) {
    mif->lastchecked = now;
    check_lock(mif);
  }
                       
  if (mif->visible && mif->show) {
    XClearArea(dpy, win, win_width-stwin_width+icons_offset, 1,
                         mif->icon_attr.width, RowHeight-2, False);
    gcv.clip_mask = mif->icon_mask;
    gcv.clip_x_origin = start + icons_offset+3;
    gcv.clip_y_origin = ((RowHeight - mif->icon_attr.height) >> 1);

    XChangeGC(dpy, statusgc, gcm, &gcv);      
    XCopyArea(dpy, mif->icon_pix, win, statusgc, 0, 0,
	      mif->icon_attr.width, mif->icon_attr.height,
	      gcv.clip_x_origin,
	      gcv.clip_y_origin);

    mif->offset = icons_offset;
    icons_offset += mif->icon_attr.width+2;
  }

}
         
         
int CheckLockModuleSeeMouse(struct MyInfo *mif, int x, int y)
{
  int xl, xr;

  if (mif == NULL) return 0;
  if (mif->show == 0) return 0;
  xl = win_width - stwin_width + mif->offset;
  xr = win_width - stwin_width + mif->offset + mif->icon_attr.width;
  return (x>=xl && x<xr && y>1 && y<RowHeight-2);
}


void CheckLockModuleCreateIconTipWindow_(struct MyInfo *mif)
{
  if (mif == NULL) return;
  if (mif->tip == NULL) return;
  PopupTipWindow(win_width - stwin_width + mif->offset, 0, mif->tip);
}

#undef __DEBUG__

void CheckLockModuleIconClick(struct MyInfo *mif, XEvent event)
{
  if (mif == NULL) return;
  if (mif->command == NULL) return;
  if (event.xbutton.time - mif->lastclick < 250) {
    SendFvwmPipe(mif->command, 0);
    #ifdef __DEBUG__
    printf("\"%s\"\n",mif->command); fflush(stdout);
    #endif
  }
  mif->lastclick = event.xbutton.time;
}