all repos — tint2 @ 8c7f4cc825fb78ed30f9bbe8aa26eafca8cdb869

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/clock/clock.c (raw)

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
/**************************************************************************
*
* Tint2 : clock
*
* Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

#include <string.h>
#include <stdio.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <pango/pangocairo.h>
#include <stdlib.h>

#include "window.h"
#include "server.h"
#include "panel.h"
#include "clock.h"
#include "timer.h"
#include "common.h"

char *time1_format;
char *time1_timezone;
char *time2_format;
char *time2_timezone;
char *time_tooltip_format;
char *time_tooltip_timezone;
char *clock_lclick_command;
char *clock_mclick_command;
char *clock_rclick_command;
char *clock_uwheel_command;
char *clock_dwheel_command;
struct timeval time_clock;
gboolean time1_has_font;
PangoFontDescription *time1_font_desc;
gboolean time2_has_font;
PangoFontDescription *time2_font_desc;
static char buf_time[256];
static char buf_date[256];
static char buf_tooltip[512];
int clock_enabled;
static timeout *clock_timeout;

void clock_init_fonts();
char *clock_get_tooltip(void *obj);
void clock_dump_geometry(void *obj, int indent);

void default_clock()
{
	clock_enabled = 0;
	clock_timeout = NULL;
	time1_format = NULL;
	time1_timezone = NULL;
	time2_format = NULL;
	time2_timezone = NULL;
	time_tooltip_format = NULL;
	time_tooltip_timezone = NULL;
	clock_lclick_command = NULL;
	clock_mclick_command = NULL;
	clock_rclick_command = NULL;
	clock_uwheel_command = NULL;
	clock_dwheel_command = NULL;
	time1_has_font = FALSE;
	time1_font_desc = NULL;
	time2_has_font = FALSE;
	time2_font_desc = NULL;
}

void cleanup_clock()
{
	pango_font_description_free(time1_font_desc);
	time1_font_desc = NULL;
	pango_font_description_free(time2_font_desc);
	time2_font_desc = NULL;
	free(time1_format);
	time1_format = NULL;
	free(time2_format);
	time2_format = NULL;
	free(time_tooltip_format);
	time_tooltip_format = NULL;
	free(time1_timezone);
	time1_timezone = NULL;
	free(time2_timezone);
	time2_timezone = NULL;
	free(time_tooltip_timezone);
	time_tooltip_timezone = NULL;
	free(clock_lclick_command);
	clock_lclick_command = NULL;
	free(clock_mclick_command);
	clock_mclick_command = NULL;
	free(clock_rclick_command);
	clock_rclick_command = NULL;
	free(clock_uwheel_command);
	clock_uwheel_command = NULL;
	free(clock_dwheel_command);
	clock_dwheel_command = NULL;
	stop_timeout(clock_timeout);
	clock_timeout = NULL;
}

void update_clocks_sec(void *arg)
{
	gettimeofday(&time_clock, 0);
	if (time1_format) {
		for (int i = 0; i < num_panels; i++)
			panels[i].clock.area.resize_needed = 1;
	}
	panel_refresh = TRUE;
}

void update_clocks_min(void *arg)
{
	// remember old_sec because after suspend/hibernate the clock should be updated directly, and not
	// on next minute change
	time_t old_sec = time_clock.tv_sec;
	gettimeofday(&time_clock, 0);
	if (time_clock.tv_sec % 60 == 0 || time_clock.tv_sec - old_sec > 60) {
		if (time1_format) {
			for (int i = 0; i < num_panels; i++)
				panels[i].clock.area.resize_needed = 1;
		}
		panel_refresh = TRUE;
	}
}

struct tm *clock_gettime_for_tz(const char *timezone)
{
	if (timezone) {
		const char *old_tz = getenv("TZ");
		setenv("TZ", timezone, 1);
		struct tm *result = localtime(&time_clock.tv_sec);
		if (old_tz)
			setenv("TZ", old_tz, 1);
		else
			unsetenv("TZ");
		return result;
	} else {
		return localtime(&time_clock.tv_sec);
	}
}

gboolean time_format_needs_sec_ticks(char *time_format)
{
	if (!time_format)
		return FALSE;
	if (strchr(time_format, 'S') || strchr(time_format, 'T') || strchr(time_format, 'r'))
		return TRUE;
	return FALSE;
}

void init_clock()
{
	if (!clock_timeout) {
		if (time_format_needs_sec_ticks(time1_format) || time_format_needs_sec_ticks(time2_format)) {
			clock_timeout = add_timeout(10, 1000, update_clocks_sec, 0, &clock_timeout);
		} else {
			clock_timeout = add_timeout(10, 1000, update_clocks_min, 0, &clock_timeout);
		}
	}
}

void init_clock_panel(void *p)
{
	Panel *panel = (Panel *)p;
	Clock *clock = &panel->clock;

	if (!clock->area.bg)
		clock->area.bg = &g_array_index(backgrounds, Background, 0);
	clock_init_fonts();
	clock->area.parent = p;
	clock->area.panel = p;
	snprintf(clock->area.name, sizeof(clock->area.name), "Clock");
	clock->area._is_under_mouse = full_width_area_is_under_mouse;
	clock->area.has_mouse_press_effect = clock->area.has_mouse_over_effect =
		panel_config.mouse_effects && (clock_lclick_command || clock_mclick_command || clock_rclick_command ||
									   clock_uwheel_command || clock_dwheel_command);
	clock->area._draw_foreground = draw_clock;
	clock->area.size_mode = LAYOUT_FIXED;
	clock->area._resize = resize_clock;
	clock->area._dump_geometry = clock_dump_geometry;
	// check consistency
	if (!time1_format)
		return;

	clock->area.resize_needed = 1;
	clock->area.on_screen = TRUE;
	instantiate_area_gradients(&clock->area);

	if (time_tooltip_format) {
		clock->area._get_tooltip_text = clock_get_tooltip;
		strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
	}
}

void clock_init_fonts()
{
	if (!time1_font_desc) {
		time1_font_desc = pango_font_description_from_string(get_default_font());
		pango_font_description_set_weight(time1_font_desc, PANGO_WEIGHT_BOLD);
		pango_font_description_set_size(time1_font_desc, pango_font_description_get_size(time1_font_desc));
	}
	if (!time2_font_desc) {
		time2_font_desc = pango_font_description_from_string(get_default_font());
		pango_font_description_set_size(time2_font_desc,
		                                pango_font_description_get_size(time2_font_desc) - PANGO_SCALE);
	}
}

void clock_default_font_changed()
{
	if (!clock_enabled)
		return;
	if (time1_has_font && time2_has_font)
		return;
	if (!time1_has_font) {
		pango_font_description_free(time1_font_desc);
		time1_font_desc = NULL;
	}
	if (!time2_has_font) {
		pango_font_description_free(time2_font_desc);
		time2_font_desc = NULL;
	}
	clock_init_fonts();
	for (int i = 0; i < num_panels; i++) {
		panels[i].clock.area.resize_needed = TRUE;
		schedule_redraw(&panels[i].clock.area);
	}
	panel_refresh = TRUE;
}

gboolean resize_clock(void *obj)
{
	Clock *clock = obj;
	Panel *panel = clock->area.panel;
	int time_height_ink, time_height, time_width, date_height_ink, date_height, date_width;
	gboolean result = FALSE;

	schedule_redraw(&clock->area);

	date_height = date_width = 0;
	strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
	get_text_size2(time1_font_desc,
				   &time_height_ink,
				   &time_height,
				   &time_width,
				   panel->area.height,
				   panel->area.width,
				   buf_time,
				   strlen(buf_time),
				   PANGO_WRAP_WORD_CHAR,
				   PANGO_ELLIPSIZE_NONE,
				   FALSE);
	if (time2_format) {
		strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
		get_text_size2(time2_font_desc,
					   &date_height_ink,
					   &date_height,
					   &date_width,
					   panel->area.height,
					   panel->area.width,
					   buf_date,
					   strlen(buf_date),
					   PANGO_WRAP_WORD_CHAR,
					   PANGO_ELLIPSIZE_NONE,
					   FALSE);
	}

	if (panel_horizontal) {
		int new_size = (time_width > date_width) ? time_width : date_width;
		new_size += 2 * clock->area.paddingxlr + left_right_border_width(&clock->area);
		if (new_size > clock->area.width || new_size < (clock->area.width - 6)) {
			// we try to limit the number of resizes
			clock->area.width = new_size + 1;
			clock->time1_posy = (clock->area.height - time_height) / 2;
			if (time2_format) {
				clock->time1_posy -= (date_height) / 2;
				clock->time2_posy = clock->time1_posy + time_height;
			}
			result = TRUE;
		}
	} else {
		int new_size = time_height + date_height + 2 * clock->area.paddingxlr + top_bottom_border_width(&clock->area);
		if (new_size != clock->area.height) {
			// we try to limit the number of resizes
			clock->area.height = new_size;
			clock->time1_posy = (clock->area.height - time_height) / 2;
			if (time2_format) {
				clock->time1_posy -= (date_height) / 2;
				clock->time2_posy = clock->time1_posy + time_height;
			}
			result = TRUE;
		}
	}

	return result;
}

void draw_clock(void *obj, cairo_t *c)
{
	Clock *clock = obj;
	PangoLayout *layout = pango_cairo_create_layout(c);

	pango_layout_set_font_description(layout, time1_font_desc);
	pango_layout_set_width(layout, clock->area.width * PANGO_SCALE);
	pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
	pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
	pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE);
	pango_layout_set_text(layout, buf_time, strlen(buf_time));

	cairo_set_source_rgba(c, clock->font.rgb[0], clock->font.rgb[1], clock->font.rgb[2], clock->font.alpha);

	pango_cairo_update_layout(c, layout);
	draw_text(layout, c, 0, clock->time1_posy, &clock->font, ((Panel *)clock->area.panel)->font_shadow);

	if (time2_format) {
		pango_layout_set_font_description(layout, time2_font_desc);
		pango_layout_set_indent(layout, 0);
		pango_layout_set_text(layout, buf_date, strlen(buf_date));
		pango_layout_set_width(layout, clock->area.width * PANGO_SCALE);

		pango_cairo_update_layout(c, layout);
		draw_text(layout, c, 0, clock->time2_posy, &clock->font, ((Panel *)clock->area.panel)->font_shadow);
	}

	g_object_unref(layout);
}

void clock_dump_geometry(void *obj, int indent)
{
	Clock *clock = (Clock *)obj;
	fprintf(stderr,
			"%*sText 1: y = %d, text = %s\n",
			indent,
			"",
			clock->time1_posy,
			buf_time);
	if (time2_format) {
		fprintf(stderr,
			"%*sText 2: y = %d, text = %s\n",
			indent,
			"",
			clock->time2_posy,
			buf_date);
	}

}

char *clock_get_tooltip(void *obj)
{
	strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
	return strdup(buf_tooltip);
}

void clock_action(int button)
{
	char *command = NULL;
	switch (button) {
	case 1:
		command = clock_lclick_command;
		break;
	case 2:
		command = clock_mclick_command;
		break;
	case 3:
		command = clock_rclick_command;
		break;
	case 4:
		command = clock_uwheel_command;
		break;
	case 5:
		command = clock_dwheel_command;
		break;
	}
	tint_exec(command);
}