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