]>
Dogcows Code - chaz/tint2/blob - src/clock/clock.c
d020c5269fb33109494cd6a412d311d3fe238720
1 /**************************************************************************
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
23 #include <cairo-xlib.h>
24 #include <pango/pangocairo.h>
39 char *time1_timezone
=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];
53 static timeout
* clock_timeout
=0;
56 void update_clocks_sec(void* arg
)
58 gettimeofday(&time_clock
, 0);
61 for (i
=0 ; i
< nb_panel
; i
++)
62 panel1
[i
].clock
.area
.resize
= 1;
67 void update_clocks_min(void* arg
)
69 // remember old_sec because after suspend/hibernate the clock should be updated directly, and not
70 // on next minute change
71 time_t old_sec
= time_clock
.tv_sec
;
72 gettimeofday(&time_clock
, 0);
73 if (time_clock
.tv_sec
% 60 == 0 || time_clock
.tv_sec
- old_sec
> 60) {
76 for (i
=0 ; i
< nb_panel
; i
++)
77 panel1
[i
].clock
.area
.resize
= 1;
83 struct tm
* clock_gettime_for_tz(const char* timezone
) {
85 const char* old_tz
= getenv("TZ");
86 setenv("TZ", timezone
, 1);
87 struct tm
* result
= localtime(&time_clock
.tv_sec
);
88 if (old_tz
) setenv("TZ", old_tz
, 1);
92 else return localtime(&time_clock
.tv_sec
);
95 const char* clock_get_tooltip(void* obj
)
97 strftime(buf_tooltip
, sizeof(buf_tooltip
), time_tooltip_format
, clock_gettime_for_tz(time_tooltip_timezone
));
104 if(time1_format
&& clock_timeout
==0) {
105 if (strchr(time1_format
, 'S') || strchr(time1_format
, 'T') || strchr(time1_format
, 'r'))
106 clock_timeout
= add_timeout(10, 1000, update_clocks_sec
, 0);
108 clock_timeout
= add_timeout(10, 1000, update_clocks_min
, 0);
113 void init_clock_panel(void *p
)
115 Panel
*panel
=(Panel
*)p
;
116 Clock
*clock
= &panel
->clock
;
117 int time_height
, time_height_ink
, date_height
, date_height_ink
;
119 clock
->area
.parent
= p
;
120 clock
->area
.panel
= p
;
121 clock
->area
._draw_foreground
= draw_clock
;
122 clock
->area
._resize
= resize_clock
;
123 clock
->area
.resize
= 1;
124 clock
->area
.redraw
= 1;
125 clock
->area
.on_screen
= 1;
127 strftime(buf_time
, sizeof(buf_time
), time1_format
, clock_gettime_for_tz(time1_timezone
));
128 get_text_size(time1_font_desc
, &time_height_ink
, &time_height
, panel
->area
.height
, buf_time
, strlen(buf_time
));
130 strftime(buf_date
, sizeof(buf_date
), time2_format
, clock_gettime_for_tz(time2_timezone
));
131 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
134 if (panel_horizontal
) {
135 // panel horizonal => fixed height and posy
136 clock
->area
.posy
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
137 clock
->area
.height
= panel
->area
.height
- (2 * clock
->area
.posy
);
140 // panel vertical => fixed width, height, posy and posx
141 clock
->area
.posy
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingxlr
;
142 clock
->area
.height
= (2 * clock
->area
.paddingxlr
) + (time_height
+ date_height
);
143 clock
->area
.posx
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
144 clock
->area
.width
= panel
->area
.width
- (2 * panel
->area
.bg
->border
.width
) - (2 * panel
->area
.paddingy
);
147 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
149 strftime(buf_date
, sizeof(buf_date
), time2_format
, clock_gettime_for_tz(time2_timezone
));
150 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
152 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
153 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
156 if (time_tooltip_format
) {
157 clock
->area
._get_tooltip_text
= clock_get_tooltip
;
158 strftime(buf_tooltip
, sizeof(buf_tooltip
), time_tooltip_format
, clock_gettime_for_tz(time_tooltip_timezone
));
166 if (time1_font_desc
) {
167 pango_font_description_free(time1_font_desc
);
170 if (time2_font_desc
) {
171 pango_font_description_free(time2_font_desc
);
175 g_free(time1_format
);
179 g_free(time2_format
);
182 if (time_tooltip_format
) {
183 g_free(time_tooltip_format
);
184 time_tooltip_format
= 0;
186 if (time1_timezone
) {
187 g_free(time1_timezone
);
190 if (time2_timezone
) {
191 g_free(time2_timezone
);
194 if (time_tooltip_timezone
) {
195 g_free(time_tooltip_timezone
);
196 time_tooltip_timezone
= 0;
198 if (clock_lclick_command
) {
199 g_free(clock_lclick_command
);
200 clock_lclick_command
= 0;
202 if (clock_rclick_command
) {
203 g_free(clock_rclick_command
);
204 clock_rclick_command
= 0;
209 void draw_clock (void *obj
, cairo_t
*c
)
214 layout
= pango_cairo_create_layout (c
);
217 pango_layout_set_font_description (layout
, time1_font_desc
);
218 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
219 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
220 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
222 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
224 pango_cairo_update_layout (c
, layout
);
225 cairo_move_to (c
, 0, clock
->time1_posy
);
226 pango_cairo_show_layout (c
, layout
);
229 pango_layout_set_font_description (layout
, time2_font_desc
);
230 pango_layout_set_indent(layout
, 0);
231 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
232 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
234 pango_cairo_update_layout (c
, layout
);
235 cairo_move_to (c
, 0, clock
->time2_posy
);
236 pango_cairo_show_layout (c
, layout
);
239 g_object_unref (layout
);
243 void resize_clock (void *obj
)
247 int time_width
, date_width
, new_width
;
249 clock
->area
.redraw
= 1;
250 time_width
= date_width
= 0;
251 strftime(buf_time
, sizeof(buf_time
), time1_format
, clock_gettime_for_tz(time1_timezone
));
253 strftime(buf_date
, sizeof(buf_date
), time2_format
, clock_gettime_for_tz(time2_timezone
));
255 // vertical panel doen't adjust width
256 if (!panel_horizontal
) return;
258 //printf(" resize_clock\n");
262 pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, clock
->area
.width
, clock
->area
.height
, server
.depth
);
264 cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, clock
->area
.width
, clock
->area
.height
);
265 c
= cairo_create (cs
);
266 layout
= pango_cairo_create_layout (c
);
269 pango_layout_set_font_description (layout
, time1_font_desc
);
270 pango_layout_set_indent(layout
, 0);
271 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
272 pango_layout_get_pixel_size (layout
, &time_width
, NULL
);
274 pango_layout_set_font_description (layout
, time2_font_desc
);
275 pango_layout_set_indent(layout
, 0);
276 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
277 pango_layout_get_pixel_size (layout
, &date_width
, NULL
);
280 if (time_width
> date_width
) new_width
= time_width
;
281 else new_width
= date_width
;
282 new_width
+= (2*clock
->area
.paddingxlr
) + (2*clock
->area
.bg
->border
.width
);
284 Panel
*panel
= ((Area
*)obj
)->panel
;
285 if (new_width
> clock
->area
.width
|| new_width
< (clock
->area
.width
-6)) {
287 // we try to limit the number of resize
288 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
289 clock
->area
.width
= new_width
+ 1;
291 // resize other objects on panel
292 panel
->area
.resize
= 1;
293 #ifdef ENABLE_BATTERY
294 panel
->battery
.area
.resize
= 1;
296 systray
.area
.resize
= 1;
299 clock
->area
.posx
= panel
->area
.width
- clock
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.bg
->border
.width
;
302 g_object_unref (layout
);
304 cairo_surface_destroy (cs
);
305 XFreePixmap (server
.dsp
, pmap
);
309 void clock_action(int button
)
314 command
= clock_lclick_command
;
317 command
= clock_rclick_command
;
324 // change for the fork the signal mask
326 // sigprocmask(SIG_SETMASK, &sigset, 0);
327 // sigprocmask(SIG_UNBLOCK, &sigset, 0);
328 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
This page took 0.046609 seconds and 4 git commands to generate.