File: ShowDateModule.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 (270 lines) | stat: -rw-r--r-- 7,244 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/shape.h>

#include <fvwm/fvwmlib.h>

#include "Goodies.h"
#include "GoodyLoadable.h"
#include "Mallocs.h"
#include "FvwmTaskBar.h"

#include <X11/xpm.h>

struct DateInfo {
    char *id;
    char *clockfmt;
    char *command;
    char *tip;
    char *hourlycommand;
    Time lastclick;
    int clock_width;
    int fontheight;
    int last_date;
    int my_offset;
};

/* let's ensure unique symbols */
#define dateLoad      ShowDateModuleLoad
#define dateSymbol    ShowDateModuleSymbol
#define dateInit      ShowDateModuleInit
#define dateDraw      ShowDateModuleDraw
#define dateInMouse   ShowDateModuleSeeMouse
#define dateClick     ShowDateModuleHandleIconClick
#define dateTipWindow ShowDateModuleCreateTipWindow
#define dateParse     ShowDateModuleParseResource

#define SetIconTip     ShowDateModuleSetIconTip
#define SetIconCommand ShowDateModuleSetIconCommand

void *dateInit(char *id, int k);
int dateParse(struct DateInfo *v,char *tline, char *Module,int Clength) ;
void dateLoad(struct DateInfo *v,Display *dpy,Drawable win) ;
void dateDraw(struct DateInfo *v,Display *dpy,Drawable win) ;
int dateInMouse(struct DateInfo *v,int x, int y) ;
void dateClick(struct DateInfo *mif,XEvent event); 
void dateTipWindow(struct DateInfo *v);


struct GoodyLoadable dateSymbol = {
  (LoadableInit_f)            &dateInit,
  (LoadableParseResource_f)   &dateParse,
  (LoadableLoad_f)            &dateLoad,
  (LoadableDraw_f)            &dateDraw,
  (LoadableSeeMouse_f)        &dateInMouse,
  (LoadableCreateTipWindow_f) &dateTipWindow,
  (HandleIconClick_f)         &dateClick
};


extern int RowHeight;

extern Display *dpy;
extern Window Root, win;
extern int win_width, win_height, win_y, win_border, d_depth,
       ScreenWidth, ScreenHeight, RowHeight; 
extern Pixel back, fore;
extern GC blackgc, hilite, shadow, checkered;
extern  XFontStruct *StatusFont;
char *StatusFont_string = "fixed";

extern GC dategc;
extern GC statusgc;

Pixmap pmask, pclip;


#define gray_width  8
#define gray_height 8
extern unsigned char gray_bits[];



void *dateInit(char *id, int k)
{
  struct DateInfo *p;

  p = (struct DateInfo*)calloc(1, sizeof(struct DateInfo));
  if (p == NULL) {
    perror("FvwmTaskBar.ShowDateModule.dateInit()");
    return NULL;
  }
  p->id = id;
  p->clockfmt = NULL;
  p->command = NULL;
  p->last_date = -1;
  p->tip = NULL;
  p->hourlycommand = NULL;
  return p;
}


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

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


/* Parse 'goodies' specific resources */
int dateParse(struct DateInfo *v, char *tline, char *Module, int Clength) 
{
  char *s;

  if(strncasecmp(tline,CatString3(Module, "ShowDateModuleClockFormat",v->id),
			  Clength+25+strlen(v->id))==0) {
    UpdateString(&(v->clockfmt), &tline[Clength+26+strlen(v->id)]);
    if(v->clockfmt[strlen(v->clockfmt)-1] == '\n')
      v->clockfmt[strlen(v->clockfmt)-1] = 0;
    return 1;
  } else if(strncasecmp(tline, CatString3(Module, "ShowDateModuleStatusFont",v->id),
                          Clength+24+strlen(v->id))==0) {
    CopyString(&(StatusFont_string),&tline[Clength+25+strlen(v->id)]);
    return 1;
  } else if(strncasecmp(tline, CatString3(Module, "ShowDateModuleCommand",v->id),
                               Clength+21+strlen(v->id))==0) {                                   
    CopyString(&s, &tline[Clength+22+strlen(v->id)]);
    SetIconCommand(v,s);
    return(1);
  } else if (strncasecmp(tline, CatString3(Module, "ShowDateModuleHourlyCommand",v->id),
			 Clength+27+strlen(v->id))==0) {                                   
    CopyString(&(v->hourlycommand), &tline[Clength+28+strlen(v->id)]);
    return(1);
  } else return 0;  
}


void dateLoad(struct DateInfo *v,Display *dpy,Drawable win) 
{
  if ((StatusFont = XLoadQueryFont(dpy, StatusFont_string)) == NULL) {
    if ((StatusFont = XLoadQueryFont(dpy, "fixed")) == NULL) {
      ConsoleMessage("FvwmTaskBar.ShowDateModule.dateLoad():Couldn't load fixed font.\n");
      StatusFont=NULL;
      return;
    }
  }

  v->fontheight = StatusFont->ascent + StatusFont->descent;
  
  /*statusgc = XCreateGC(dpy, Root, gcmask, &gcval);*/
  
  if (v->clockfmt) {
    struct tm *tms;
    static time_t timer;
    static char str[24];
    time(&timer);
    tms = localtime(&timer);
    strftime(str, 24,v->clockfmt, tms);
    v->clock_width = XTextWidth(StatusFont, str, strlen(str)) + 4;
  }
  else v->clock_width = XTextWidth(StatusFont, "XX:XX", 5) + 4;
  v->my_offset=icons_offset;
  icons_offset += v->clock_width;
 }

void Draw3dBox(Window wn, int x, int y, int w, int h);


void dateDraw(struct DateInfo *v,Display *dpy,Drawable win) {
  struct tm *tms;
  static char str[40];
  static time_t timer;
  static int last_hour = -1;
  unsigned long gcmask;
  XGCValues gcval;

  time(&timer);
  tms = localtime(&timer);
  if (v->clockfmt) {
    strftime(str, 24, v->clockfmt, tms);
    if (str[0] == '0') str[0]=' ';
  } else {
    strftime(str, 15, "%R", tms);
  }
  XClearArea(dpy, win, win_width-stwin_width+icons_offset, 1, v->clock_width, RowHeight-2, False);
  gcmask = GCForeground | GCBackground | GCFont | GCGraphicsExposures | GCClipMask;
  gcval.foreground = fore;
  gcval.background = back;
  gcval.font = StatusFont->fid;
  gcval.graphics_exposures = False;
  gcval.clip_mask=0;

  XChangeGC(dpy, statusgc, gcmask, &gcval);

  XDrawString(dpy,win,statusgc,
	      win_width - stwin_width +icons_offset+4,
	      ((RowHeight - v->fontheight) >> 1) +StatusFont->ascent,
	      str, strlen(str));
  v->my_offset=icons_offset;
  icons_offset+=v->clock_width;

  if (v->hourlycommand) {
    if (tms->tm_min == 0 && tms->tm_hour != last_hour) {
      last_hour = tms->tm_hour;
      SendFvwmPipe(v->hourlycommand, 0);
    }
  }

/*
  if (Tip.open) {
    if (Tip.type == DATE_TIP)
      if (tms->tm_mday != v->last_date) / * reflect date change * /
        dateTipWindow(v); / * This automatically deletes any old window * /
    v->last_date = tms->tm_mday;
  }
 */ 


}

int dateInMouse(struct DateInfo *v,int x, int y) {
  int clockl = win_width - stwin_width+v->my_offset;
  int clockr = win_width - stwin_width +v->my_offset+ v->clock_width;
  return (x>=clockl && x<clockr && y>1 && y<RowHeight-2);
}


void dateTipWindow(struct DateInfo *v) {
  struct tm *tms;
  static time_t timer;
  static char str[40];

  time(&timer);
  tms = localtime(&timer);
  strftime(str, 40, "%A, %B %d, %Y", tms);
  v->last_date = tms->tm_mday;

  PopupTipWindow(win_width, 0, str);
}


void dateClick(struct DateInfo *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;
}