aboutsummaryrefslogtreecommitdiff
path: root/src/clock.c
blob: 147c1f694aedb25ba3b57c3e31332c0468daad85 (plain)
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
/**
 * @file clock.c
 * @author Joe Wingbermuehle
 * @date 2005-2006
 *
 * @brief Clock tray component.
 *
 */

#include "jwm.h"
#include "clock.h"
#include "tray.h"
#include "color.h"
#include "font.h"
#include "timing.h"
#include "main.h"
#include "root.h"
#include "cursor.h"
#include "popup.h"
#include "misc.h"

/** Structure to respresent a clock tray component. */
typedef struct ClockType {

	TrayComponentType *cp;   /**< Common component data. */

	char *format;            /**< The time format to use. */
	char *command;           /**< A command to run when clicked. */
	char shortTime[80];      /**< Currently displayed time. */

	/* The following are used to control popups. */
	int mousex;              /**< Last mouse x-coordinate. */
	int mousey;              /**< Last mouse y-coordinate. */
	TimeType mouseTime;      /**< Time of the last mouse motion. */

	int userWidth;           /**< User-specified clock width (or 0). */

	struct ClockType *next;  /**< Next clock in the list. */

} ClockType;

/** The default time format to use. */
static const char *DEFAULT_FORMAT = "%I:%M %p";

static ClockType *clocks;
static TimeType lastUpdate = ZERO_TIME;

static void Create(TrayComponentType *cp);
static void Resize(TrayComponentType *cp);
static void Destroy(TrayComponentType *cp);
static void ProcessClockButtonEvent(TrayComponentType *cp,
	int x, int y, int mask);
static void ProcessClockMotionEvent(TrayComponentType *cp,
	int x, int y, int mask);

static void DrawClock(ClockType *clk, const TimeType *now, int x, int y);

/** Initialize clocks. */
void InitializeClock() {
	clocks = NULL;
}

/** Start clock(s). */
void StartupClock() {

	ClockType *clk;

	for(clk = clocks; clk; clk = clk->next) {
		if(clk->cp->requestedWidth == 0) {
			clk->cp->requestedWidth = GetStringWidth(FONT_CLOCK, clk->format) + 4;
		}
		if(clk->cp->requestedHeight == 0) {
			clk->cp->requestedHeight = GetStringHeight(FONT_CLOCK) + 4;
		}
	}

}

/** Stop clock(s). */
void ShutdownClock() {
}

/** Destroy clock(s). */
void DestroyClock() {

	ClockType *cp;

	while(clocks) {
		cp = clocks->next;

		if(clocks->format) {
			Release(clocks->format);
		}
		if(clocks->command) {
			Release(clocks->command);
		}

		Release(clocks);
		clocks = cp;
	}

}

/** Create a clock tray component. */
TrayComponentType *CreateClock(const char *format, const char *command,
	int width, int height) {

	TrayComponentType *cp;
	ClockType *clk;

	clk = Allocate(sizeof(ClockType));
	clk->next = clocks;
	clocks = clk;

	clk->mousex = 0;
	clk->mousey = 0;
	clk->mouseTime.seconds = 0;
	clk->mouseTime.ms = 0;
	clk->userWidth = 0;

	if(!format) {
		format = DEFAULT_FORMAT;
	}
	clk->format = CopyString(format);

	clk->command = CopyString(command);

	clk->shortTime[0] = 0;

	cp = CreateTrayComponent();
	cp->object = clk;
	clk->cp = cp;
	if(width > 0) {
		cp->requestedWidth = width;
		clk->userWidth = 1;
	} else {
		cp->requestedWidth = 0;
		clk->userWidth = 0;
	}
	cp->requestedHeight = height;

	cp->Create = Create;
	cp->Resize = Resize;
	cp->Destroy = Destroy;
	cp->ProcessButtonEvent = ProcessClockButtonEvent;
	cp->ProcessMotionEvent = ProcessClockMotionEvent;

	return cp;

}

/** Initialize a clock tray component. */
void Create(TrayComponentType *cp) {

	ClockType *clk;

	Assert(cp);

	clk = (ClockType*)cp->object;

	Assert(clk);

	cp->pixmap = JXCreatePixmap(display, rootWindow, cp->width, cp->height,
		rootDepth);

	JXSetForeground(display, rootGC, colors[COLOR_CLOCK_BG]);
	JXFillRectangle(display, cp->pixmap, rootGC, 0, 0, cp->width, cp->height);

}

/** Resize a clock tray component. */
void Resize(TrayComponentType *cp) {

	ClockType *clk;
	TimeType now;
	int x, y;

	Assert(cp);

	clk = (ClockType*)cp->object;

	Assert(clk);

	if(cp->pixmap != None) {
		JXFreePixmap(display, cp->pixmap);
	}

	cp->pixmap = JXCreatePixmap(display, rootWindow, cp->width, cp->height,
		rootDepth);

	clk->shortTime[0] = 0;

	GetCurrentTime(&now);
	GetMousePosition(&x, &y);
	DrawClock(clk, &now, x, y);

}

/** Destroy a clock tray component. */
void Destroy(TrayComponentType *cp) {

	ClockType *clk;

	Assert(cp);

	clk = (ClockType*)cp->object;

	Assert(clk);

	if(cp->pixmap != None) {
		JXFreePixmap(display, cp->pixmap);
	}
}

/** Process a click event on a clock tray component. */
void ProcessClockButtonEvent(TrayComponentType *cp, int x, int y, int mask) {

	ClockType *clk;

	Assert(cp);

	clk = (ClockType*)cp->object;

	Assert(clk);

	if(clk->command) {
		RunCommand(clk->command);
	}

}

/** Process a motion event on a clock tray component. */
void ProcessClockMotionEvent(TrayComponentType *cp,
	int x, int y, int mask) {

	Assert(cp);

	ClockType *clk = (ClockType*)cp->object;
	clk->mousex = cp->screenx + x;
	clk->mousey = cp->screeny + y;
	GetCurrentTime(&clk->mouseTime);

}

/** Update a clock tray component. */
void SignalClock(const TimeType *now, int x, int y) {

	ClockType *cp;
	int shouldDraw;
	char *longTime;
	time_t t;

	Assert(now);

	/* Determine if we should update the clock(s). */
	if(GetTimeDifference(&lastUpdate, now) > 900) {
		shouldDraw = 1;
		lastUpdate = *now;
	} else {
		shouldDraw = 0;
	}

	/* Update each clock. */
	for(cp = clocks; cp; cp = cp->next) {

		if(shouldDraw) {
			DrawClock(cp, now, x, y);
		}

		if(abs(cp->mousex - x) < POPUP_DELTA
			&& abs(cp->mousey - y) < POPUP_DELTA) {
			if(GetTimeDifference(now, &cp->mouseTime) >= popupDelay) {
				time(&t);
				longTime = asctime(localtime(&t));
				Trim(longTime);
				ShowPopup(x, y, longTime);
			}
		}

	}

}

/** Draw a clock tray component. */
void DrawClock(ClockType *clk, const TimeType *now, int x, int y) {

	TrayComponentType *cp;
	const char *shortTime;
	int width;
	int rwidth;

	Assert(clk);
	Assert(now);

	/* Only draw if the label changed. */
	shortTime = GetTimeString(clk->format);
	if(!strcmp(clk->shortTime, shortTime)) {
		return;
	}
	strcpy(clk->shortTime, shortTime);

	cp = clk->cp;

	/* Clear the area. */
	JXSetForeground(display, rootGC, colors[COLOR_CLOCK_BG]);
	JXFillRectangle(display, cp->pixmap, rootGC, 0, 0,
		cp->width, cp->height);

	/* Determine if the clock is the right size. */
	width = GetStringWidth(FONT_CLOCK, shortTime);
	rwidth = width + 4;
	if(rwidth == clk->cp->requestedWidth || clk->userWidth) {

		/* Draw the clock. */
		RenderString(cp->pixmap, FONT_CLOCK, COLOR_CLOCK_FG,
			cp->width / 2 - width / 2,
			cp->height / 2 - GetStringHeight(FONT_CLOCK) / 2,
			cp->width, NULL, shortTime);

		UpdateSpecificTray(clk->cp->tray, clk->cp);

	} else {

		/* Wrong size. Resize. */
		clk->cp->requestedWidth = rwidth;
		ResizeTray(clk->cp->tray);

	}

}