]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
SIGUSR1 does now a full restat of tint2
[chaz/tint2] / src / clock / clock.c
1 /**************************************************************************
2 *
3 * Tint2 : clock
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <cairo.h>
23 #include <cairo-xlib.h>
24 #include <pango/pangocairo.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdlib.h>
28
29 #include "window.h"
30 #include "server.h"
31 #include "area.h"
32 #include "panel.h"
33 #include "taskbar.h"
34 #include "clock.h"
35 #include "timer.h"
36
37
38 char *time1_format;
39 char *time1_timezone;
40 char *time2_format;
41 char *time2_timezone;
42 char *time_tooltip_format;
43 char *time_tooltip_timezone;
44 char *clock_lclick_command;
45 char *clock_rclick_command;
46 struct timeval time_clock;
47 PangoFontDescription *time1_font_desc;
48 PangoFontDescription *time2_font_desc;
49 static char buf_time[40];
50 static char buf_date[40];
51 static char buf_tooltip[40];
52 int clock_enabled;
53 static timeout* clock_timeout;
54
55
56 void default_clock()
57 {
58 clock_enabled = 0;
59 clock_timeout = 0;
60 time1_format = 0;
61 time1_timezone = 0;
62 time2_format = 0;
63 time2_timezone = 0;
64 time_tooltip_format = 0;
65 time_tooltip_timezone = 0;
66 clock_lclick_command = 0;
67 clock_rclick_command = 0;
68 time1_font_desc = 0;
69 time2_font_desc = 0;
70 }
71
72 void cleanup_clock()
73 {
74 printf("*** cleanup_clock()\n");
75 if (time1_font_desc) pango_font_description_free(time1_font_desc);
76 if (time2_font_desc) pango_font_description_free(time2_font_desc);
77 if (time1_format) g_free(time1_format);
78 if (time2_format) g_free(time2_format);
79 if (time_tooltip_format) g_free(time_tooltip_format);
80 if (time1_timezone) g_free(time1_timezone);
81 if (time2_timezone) g_free(time2_timezone);
82 if (time_tooltip_timezone) g_free(time_tooltip_timezone);
83 if (clock_lclick_command) g_free(clock_lclick_command);
84 if (clock_rclick_command) g_free(clock_rclick_command);
85 }
86
87
88 void update_clocks_sec(void* arg)
89 {
90 gettimeofday(&time_clock, 0);
91 int i;
92 if (time1_format) {
93 for (i=0 ; i < nb_panel ; i++)
94 panel1[i].clock.area.resize = 1;
95 }
96 panel_refresh = 1;
97 }
98
99 void update_clocks_min(void* arg)
100 {
101 // remember old_sec because after suspend/hibernate the clock should be updated directly, and not
102 // on next minute change
103 time_t old_sec = time_clock.tv_sec;
104 gettimeofday(&time_clock, 0);
105 if (time_clock.tv_sec % 60 == 0 || time_clock.tv_sec - old_sec > 60) {
106 int i;
107 if (time1_format) {
108 for (i=0 ; i < nb_panel ; i++)
109 panel1[i].clock.area.resize = 1;
110 }
111 panel_refresh = 1;
112 }
113 }
114
115 struct tm* clock_gettime_for_tz(const char* timezone) {
116 if (timezone) {
117 const char* old_tz = getenv("TZ");
118 setenv("TZ", timezone, 1);
119 struct tm* result = localtime(&time_clock.tv_sec);
120 if (old_tz) setenv("TZ", old_tz, 1);
121 else unsetenv("TZ");
122 return result;
123 }
124 else return localtime(&time_clock.tv_sec);
125 }
126
127 const char* clock_get_tooltip(void* obj)
128 {
129 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
130 return buf_tooltip;
131 }
132
133
134 void init_clock()
135 {
136 if(time1_format && clock_timeout==0) {
137 if (strchr(time1_format, 'S') || strchr(time1_format, 'T') || strchr(time1_format, 'r'))
138 clock_timeout = add_timeout(10, 1000, update_clocks_sec, 0);
139 else
140 clock_timeout = add_timeout(10, 1000, update_clocks_min, 0);
141 }
142 }
143
144
145 void init_clock_panel(void *p)
146 {
147 Panel *panel =(Panel*)p;
148 Clock *clock = &panel->clock;
149 int time_height, time_height_ink, date_height, date_height_ink;
150
151 clock->area.parent = p;
152 clock->area.panel = p;
153 clock->area._draw_foreground = draw_clock;
154 clock->area._resize = resize_clock;
155 clock->area.resize = 1;
156 clock->area.redraw = 1;
157 clock->area.on_screen = 1;
158
159 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
160 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
161 if (time2_format) {
162 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
163 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
164 }
165
166 if (panel_horizontal) {
167 // panel horizonal => fixed height and posy
168 clock->area.posy = panel->area.bg->border.width + panel->area.paddingy;
169 clock->area.height = panel->area.height - (2 * clock->area.posy);
170 }
171 else {
172 // panel vertical => fixed width, height, posy and posx
173 clock->area.posy = panel->area.bg->border.width + panel->area.paddingxlr;
174 clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
175 clock->area.posx = panel->area.bg->border.width + panel->area.paddingy;
176 clock->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
177 }
178
179 clock->time1_posy = (clock->area.height - time_height) / 2;
180 if (time2_format) {
181 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
182 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
183
184 clock->time1_posy -= ((date_height_ink + 2) / 2);
185 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
186 }
187
188 if (time_tooltip_format) {
189 clock->area._get_tooltip_text = clock_get_tooltip;
190 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
191 }
192 }
193
194
195 void draw_clock (void *obj, cairo_t *c)
196 {
197 Clock *clock = obj;
198 PangoLayout *layout;
199
200 layout = pango_cairo_create_layout (c);
201
202 // draw layout
203 pango_layout_set_font_description (layout, time1_font_desc);
204 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
205 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
206 pango_layout_set_text (layout, buf_time, strlen(buf_time));
207
208 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
209
210 pango_cairo_update_layout (c, layout);
211 cairo_move_to (c, 0, clock->time1_posy);
212 pango_cairo_show_layout (c, layout);
213
214 if (time2_format) {
215 pango_layout_set_font_description (layout, time2_font_desc);
216 pango_layout_set_indent(layout, 0);
217 pango_layout_set_text (layout, buf_date, strlen(buf_date));
218 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
219
220 pango_cairo_update_layout (c, layout);
221 cairo_move_to (c, 0, clock->time2_posy);
222 pango_cairo_show_layout (c, layout);
223 }
224
225 g_object_unref (layout);
226 }
227
228
229 void resize_clock (void *obj)
230 {
231 Clock *clock = obj;
232 PangoLayout *layout;
233 int time_width, date_width, new_width;
234
235 clock->area.redraw = 1;
236 time_width = date_width = 0;
237 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
238 if (time2_format)
239 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
240
241 // vertical panel doen't adjust width
242 if (!panel_horizontal) return;
243
244 //printf(" resize_clock\n");
245 cairo_surface_t *cs;
246 cairo_t *c;
247 Pixmap pmap;
248 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
249
250 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
251 c = cairo_create (cs);
252 layout = pango_cairo_create_layout (c);
253
254 // check width
255 pango_layout_set_font_description (layout, time1_font_desc);
256 pango_layout_set_indent(layout, 0);
257 pango_layout_set_text (layout, buf_time, strlen(buf_time));
258 pango_layout_get_pixel_size (layout, &time_width, NULL);
259 if (time2_format) {
260 pango_layout_set_font_description (layout, time2_font_desc);
261 pango_layout_set_indent(layout, 0);
262 pango_layout_set_text (layout, buf_date, strlen(buf_date));
263 pango_layout_get_pixel_size (layout, &date_width, NULL);
264 }
265
266 if (time_width > date_width) new_width = time_width;
267 else new_width = date_width;
268 new_width += (2*clock->area.paddingxlr) + (2*clock->area.bg->border.width);
269
270 Panel *panel = ((Area*)obj)->panel;
271 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
272 // resize clock
273 // we try to limit the number of resize
274 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
275 clock->area.width = new_width + 1;
276
277 // resize other objects on panel
278 panel->area.resize = 1;
279 #ifdef ENABLE_BATTERY
280 panel->battery.area.resize = 1;
281 #endif
282 systray.area.resize = 1;
283 panel_refresh = 1;
284 }
285 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.bg->border.width;
286
287
288 g_object_unref (layout);
289 cairo_destroy (c);
290 cairo_surface_destroy (cs);
291 XFreePixmap (server.dsp, pmap);
292 }
293
294
295 void clock_action(int button)
296 {
297 char *command = 0;
298 switch (button) {
299 case 1:
300 command = clock_lclick_command;
301 break;
302 case 3:
303 command = clock_rclick_command;
304 break;
305 }
306 if (command) {
307 pid_t pid;
308 pid = fork();
309 if (pid == 0) {
310 // change for the fork the signal mask
311 // sigset_t sigset;
312 // sigprocmask(SIG_SETMASK, &sigset, 0);
313 // sigprocmask(SIG_UNBLOCK, &sigset, 0);
314 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
315 _exit(0);
316 }
317 }
318 }
319
This page took 0.052992 seconds and 5 git commands to generate.