]>
Dogcows Code - chaz/tint2/blob - src/clock/clock.c
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 *time_tooltip_format
;
40 char *time_tooltip_timezone
;
41 char *clock_lclick_command
;
42 char *clock_rclick_command
;
43 struct timeval time_clock
;
44 PangoFontDescription
*time1_font_desc
;
45 PangoFontDescription
*time2_font_desc
;
46 static char buf_time
[40];
47 static char buf_date
[40];
48 static char buf_tooltip
[40];
50 static timeout
* clock_timeout
;
61 time_tooltip_format
= 0;
62 time_tooltip_timezone
= 0;
63 clock_lclick_command
= 0;
64 clock_rclick_command
= 0;
71 if (time1_font_desc
) pango_font_description_free(time1_font_desc
);
72 if (time2_font_desc
) pango_font_description_free(time2_font_desc
);
73 if (time1_format
) g_free(time1_format
);
74 if (time2_format
) g_free(time2_format
);
75 if (time_tooltip_format
) g_free(time_tooltip_format
);
76 if (time1_timezone
) g_free(time1_timezone
);
77 if (time2_timezone
) g_free(time2_timezone
);
78 if (time_tooltip_timezone
) g_free(time_tooltip_timezone
);
79 if (clock_lclick_command
) g_free(clock_lclick_command
);
80 if (clock_rclick_command
) g_free(clock_rclick_command
);
84 void update_clocks_sec(void* arg
)
86 gettimeofday(&time_clock
, 0);
89 for (i
=0 ; i
< nb_panel
; i
++)
90 panel1
[i
].clock
.area
.resize
= 1;
95 void update_clocks_min(void* arg
)
97 // remember old_sec because after suspend/hibernate the clock should be updated directly, and not
98 // on next minute change
99 time_t old_sec
= time_clock
.tv_sec
;
100 gettimeofday(&time_clock
, 0);
101 if (time_clock
.tv_sec
% 60 == 0 || time_clock
.tv_sec
- old_sec
> 60) {
104 for (i
=0 ; i
< nb_panel
; i
++)
105 panel1
[i
].clock
.area
.resize
= 1;
111 struct tm
* clock_gettime_for_tz(const char* timezone
) {
113 const char* old_tz
= getenv("TZ");
114 setenv("TZ", timezone
, 1);
115 struct tm
* result
= localtime(&time_clock
.tv_sec
);
116 if (old_tz
) setenv("TZ", old_tz
, 1);
120 else return localtime(&time_clock
.tv_sec
);
123 const char* clock_get_tooltip(void* obj
)
125 strftime(buf_tooltip
, sizeof(buf_tooltip
), time_tooltip_format
, clock_gettime_for_tz(time_tooltip_timezone
));
132 if(time1_format
&& clock_timeout
==0) {
133 if (strchr(time1_format
, 'S') || strchr(time1_format
, 'T') || strchr(time1_format
, 'r'))
134 clock_timeout
= add_timeout(10, 1000, update_clocks_sec
, 0);
136 clock_timeout
= add_timeout(10, 1000, update_clocks_min
, 0);
141 void init_clock_panel(void *p
)
143 Panel
*panel
=(Panel
*)p
;
144 Clock
*clock
= &panel
->clock
;
146 clock
->area
.parent
= p
;
147 clock
->area
.panel
= p
;
148 clock
->area
._draw_foreground
= draw_clock
;
149 clock
->area
.size_mode
= SIZE_BY_CONTENT
;
150 clock
->area
._resize
= resize_clock
;
152 if (time1_format
== 0)
155 clock
->area
.resize
= 1;
156 clock
->area
.on_screen
= 1;
158 if (time_tooltip_format
) {
159 clock
->area
._get_tooltip_text
= clock_get_tooltip
;
160 strftime(buf_tooltip
, sizeof(buf_tooltip
), time_tooltip_format
, clock_gettime_for_tz(time_tooltip_timezone
));
165 void draw_clock (void *obj
, cairo_t
*c
)
170 layout
= pango_cairo_create_layout (c
);
173 pango_layout_set_font_description (layout
, time1_font_desc
);
174 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
175 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
176 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
178 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
180 pango_cairo_update_layout (c
, layout
);
181 cairo_move_to (c
, 0, clock
->time1_posy
);
182 pango_cairo_show_layout (c
, layout
);
185 pango_layout_set_font_description (layout
, time2_font_desc
);
186 pango_layout_set_indent(layout
, 0);
187 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
188 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
190 pango_cairo_update_layout (c
, layout
);
191 cairo_move_to (c
, 0, clock
->time2_posy
);
192 pango_cairo_show_layout (c
, layout
);
195 g_object_unref (layout
);
199 int resize_clock (void *obj
)
202 Panel
*panel
= clock
->area
.panel
;
203 int time_height_ink
, time_height
, time_width
, date_height_ink
, date_height
, date_width
, ret
= 0;
205 clock
->area
.redraw
= 1;
207 date_height
= date_width
= 0;
208 strftime(buf_time
, sizeof(buf_time
), time1_format
, clock_gettime_for_tz(time1_timezone
));
209 get_text_size2(time1_font_desc
, &time_height_ink
, &time_height
, &time_width
, panel
->area
.height
, panel
->area
.width
, buf_time
, strlen(buf_time
));
211 strftime(buf_date
, sizeof(buf_date
), time2_format
, clock_gettime_for_tz(time2_timezone
));
212 get_text_size2(time2_font_desc
, &date_height_ink
, &date_height
, &date_width
, panel
->area
.height
, panel
->area
.width
, buf_date
, strlen(buf_date
));
215 if (panel_horizontal
) {
216 int new_size
= (time_width
> date_width
) ? time_width
: date_width
;
217 new_size
+= (2*clock
->area
.paddingxlr
) + (2*clock
->area
.bg
->border
.width
);
218 if (new_size
> clock
->area
.width
|| new_size
< (clock
->area
.width
-6)) {
219 // we try to limit the number of resize
220 clock
->area
.width
= new_size
+ 1;
221 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
223 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
224 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
230 int new_size
= time_height
+ date_height
+ (2 * (clock
->area
.paddingxlr
+ clock
->area
.bg
->border
.width
));
231 if (new_size
!= clock
->area
.height
) {
232 // we try to limit the number of resize
233 clock
->area
.height
= new_size
;
234 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
236 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
237 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
247 void clock_action(int button
)
252 command
= clock_lclick_command
;
255 command
= clock_rclick_command
;
This page took 0.045466 seconds and 5 git commands to generate.