]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
*fix* execute an external command by calling fork/execl and do not ignore SIGCHLD...
[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 <stdlib.h>
26
27 #include "window.h"
28 #include "server.h"
29 #include "area.h"
30 #include "panel.h"
31 #include "taskbar.h"
32 #include "clock.h"
33 #include "timer.h"
34 #include "common.h"
35
36
37 char *time1_format;
38 char *time1_timezone;
39 char *time2_format;
40 char *time2_timezone;
41 char *time_tooltip_format;
42 char *time_tooltip_timezone;
43 char *clock_lclick_command;
44 char *clock_rclick_command;
45 struct timeval time_clock;
46 PangoFontDescription *time1_font_desc;
47 PangoFontDescription *time2_font_desc;
48 static char buf_time[40];
49 static char buf_date[40];
50 static char buf_tooltip[40];
51 int clock_enabled;
52 static timeout* clock_timeout;
53
54
55 void default_clock()
56 {
57 clock_enabled = 0;
58 clock_timeout = 0;
59 time1_format = 0;
60 time1_timezone = 0;
61 time2_format = 0;
62 time2_timezone = 0;
63 time_tooltip_format = 0;
64 time_tooltip_timezone = 0;
65 clock_lclick_command = 0;
66 clock_rclick_command = 0;
67 time1_font_desc = 0;
68 time2_font_desc = 0;
69 }
70
71 void cleanup_clock()
72 {
73 if (time1_font_desc) pango_font_description_free(time1_font_desc);
74 if (time2_font_desc) pango_font_description_free(time2_font_desc);
75 if (time1_format) g_free(time1_format);
76 if (time2_format) g_free(time2_format);
77 if (time_tooltip_format) g_free(time_tooltip_format);
78 if (time1_timezone) g_free(time1_timezone);
79 if (time2_timezone) g_free(time2_timezone);
80 if (time_tooltip_timezone) g_free(time_tooltip_timezone);
81 if (clock_lclick_command) g_free(clock_lclick_command);
82 if (clock_rclick_command) g_free(clock_rclick_command);
83 }
84
85
86 void update_clocks_sec(void* arg)
87 {
88 gettimeofday(&time_clock, 0);
89 int i;
90 if (time1_format) {
91 for (i=0 ; i < nb_panel ; i++)
92 panel1[i].clock.area.resize = 1;
93 }
94 panel_refresh = 1;
95 }
96
97 void update_clocks_min(void* arg)
98 {
99 // remember old_sec because after suspend/hibernate the clock should be updated directly, and not
100 // on next minute change
101 time_t old_sec = time_clock.tv_sec;
102 gettimeofday(&time_clock, 0);
103 if (time_clock.tv_sec % 60 == 0 || time_clock.tv_sec - old_sec > 60) {
104 int i;
105 if (time1_format) {
106 for (i=0 ; i < nb_panel ; i++)
107 panel1[i].clock.area.resize = 1;
108 }
109 panel_refresh = 1;
110 }
111 }
112
113 struct tm* clock_gettime_for_tz(const char* timezone) {
114 if (timezone) {
115 const char* old_tz = getenv("TZ");
116 setenv("TZ", timezone, 1);
117 struct tm* result = localtime(&time_clock.tv_sec);
118 if (old_tz) setenv("TZ", old_tz, 1);
119 else unsetenv("TZ");
120 return result;
121 }
122 else return localtime(&time_clock.tv_sec);
123 }
124
125 const char* clock_get_tooltip(void* obj)
126 {
127 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
128 return buf_tooltip;
129 }
130
131
132 void init_clock()
133 {
134 if(time1_format && clock_timeout==0) {
135 if (strchr(time1_format, 'S') || strchr(time1_format, 'T') || strchr(time1_format, 'r'))
136 clock_timeout = add_timeout(10, 1000, update_clocks_sec, 0);
137 else
138 clock_timeout = add_timeout(10, 1000, update_clocks_min, 0);
139 }
140 }
141
142
143 void init_clock_panel(void *p)
144 {
145 Panel *panel =(Panel*)p;
146 Clock *clock = &panel->clock;
147 int time_height, time_height_ink, date_height, date_height_ink;
148
149 clock->area.parent = p;
150 clock->area.panel = p;
151 clock->area._draw_foreground = draw_clock;
152 clock->area._resize = resize_clock;
153 clock->area.resize = 1;
154 clock->area.redraw = 1;
155 clock->area.on_screen = 1;
156
157 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
158 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
159 if (time2_format) {
160 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
161 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
162 }
163
164 if (panel_horizontal) {
165 // panel horizonal => fixed height and posy
166 clock->area.posy = panel->area.bg->border.width + panel->area.paddingy;
167 clock->area.height = panel->area.height - (2 * clock->area.posy);
168 }
169 else {
170 // panel vertical => fixed width, height, posy and posx
171 clock->area.posy = panel->area.bg->border.width + panel->area.paddingxlr;
172 clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
173 clock->area.posx = panel->area.bg->border.width + panel->area.paddingy;
174 clock->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
175 }
176
177 clock->time1_posy = (clock->area.height - time_height) / 2;
178 if (time2_format) {
179 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
180 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
181
182 clock->time1_posy -= ((date_height_ink + 2) / 2);
183 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
184 }
185
186 if (time_tooltip_format) {
187 clock->area._get_tooltip_text = clock_get_tooltip;
188 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
189 }
190 }
191
192
193 void draw_clock (void *obj, cairo_t *c)
194 {
195 Clock *clock = obj;
196 PangoLayout *layout;
197
198 layout = pango_cairo_create_layout (c);
199
200 // draw layout
201 pango_layout_set_font_description (layout, time1_font_desc);
202 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
203 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
204 pango_layout_set_text (layout, buf_time, strlen(buf_time));
205
206 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
207
208 pango_cairo_update_layout (c, layout);
209 cairo_move_to (c, 0, clock->time1_posy);
210 pango_cairo_show_layout (c, layout);
211
212 if (time2_format) {
213 pango_layout_set_font_description (layout, time2_font_desc);
214 pango_layout_set_indent(layout, 0);
215 pango_layout_set_text (layout, buf_date, strlen(buf_date));
216 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
217
218 pango_cairo_update_layout (c, layout);
219 cairo_move_to (c, 0, clock->time2_posy);
220 pango_cairo_show_layout (c, layout);
221 }
222
223 g_object_unref (layout);
224 }
225
226
227 void resize_clock (void *obj)
228 {
229 Clock *clock = obj;
230 PangoLayout *layout;
231 int time_width, date_width, new_width;
232
233 clock->area.redraw = 1;
234 time_width = date_width = 0;
235 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
236 if (time2_format)
237 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
238
239 // vertical panel doen't adjust width
240 if (!panel_horizontal) return;
241
242 //printf(" resize_clock\n");
243 cairo_surface_t *cs;
244 cairo_t *c;
245 Pixmap pmap;
246 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
247
248 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
249 c = cairo_create (cs);
250 layout = pango_cairo_create_layout (c);
251
252 // check width
253 pango_layout_set_font_description (layout, time1_font_desc);
254 pango_layout_set_indent(layout, 0);
255 pango_layout_set_text (layout, buf_time, strlen(buf_time));
256 pango_layout_get_pixel_size (layout, &time_width, NULL);
257 if (time2_format) {
258 pango_layout_set_font_description (layout, time2_font_desc);
259 pango_layout_set_indent(layout, 0);
260 pango_layout_set_text (layout, buf_date, strlen(buf_date));
261 pango_layout_get_pixel_size (layout, &date_width, NULL);
262 }
263
264 if (time_width > date_width) new_width = time_width;
265 else new_width = date_width;
266 new_width += (2*clock->area.paddingxlr) + (2*clock->area.bg->border.width);
267
268 Panel *panel = ((Area*)obj)->panel;
269 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
270 // resize clock
271 // we try to limit the number of resize
272 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
273 clock->area.width = new_width + 1;
274
275 // resize other objects on panel
276 panel->area.resize = 1;
277 #ifdef ENABLE_BATTERY
278 panel->battery.area.resize = 1;
279 #endif
280 systray.area.resize = 1;
281 panel_refresh = 1;
282 }
283 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.bg->border.width;
284
285
286 g_object_unref (layout);
287 cairo_destroy (c);
288 cairo_surface_destroy (cs);
289 XFreePixmap (server.dsp, pmap);
290 }
291
292
293 void clock_action(int button)
294 {
295 char *command = 0;
296 switch (button) {
297 case 1:
298 command = clock_lclick_command;
299 break;
300 case 3:
301 command = clock_rclick_command;
302 break;
303 }
304 tint_exec(command);
305 }
306
This page took 0.05598 seconds and 5 git commands to generate.