]>
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
=0;
40 char *clock_lclick_command
=0;
41 char *clock_rclick_command
=0;
42 struct timeval time_clock
;
43 PangoFontDescription
*time1_font_desc
=0;
44 PangoFontDescription
*time2_font_desc
=0;
45 static char buf_time
[40];
46 static char buf_date
[40];
47 static char buf_tooltip
[40];
49 static const struct timeout
* clock_timeout
=0;
54 gettimeofday(&time_clock
, 0);
57 for (i
=0 ; i
< nb_panel
; i
++)
58 panel1
[i
].clock
.area
.resize
= 1;
64 const char* clock_get_tooltip(void* obj
)
66 strftime(buf_tooltip
, sizeof(buf_tooltip
), time_tooltip_format
, localtime(&time_clock
.tv_sec
));
73 if(time1_format
&& clock_timeout
==0) {
74 if (strchr(time1_format
, 'S') || strchr(time1_format
, 'T') || strchr(time1_format
, 'r'))
75 clock_timeout
= add_timeout(10, 1000, update_clocks
);
77 clock_timeout
= add_timeout(10, 60000, update_clocks
);
82 void init_clock_panel(void *p
)
84 Panel
*panel
=(Panel
*)p
;
85 Clock
*clock
= &panel
->clock
;
86 int time_height
, time_height_ink
, date_height
, date_height_ink
;
88 clock
->area
.parent
= p
;
89 clock
->area
.panel
= p
;
90 clock
->area
._draw_foreground
= draw_clock
;
91 clock
->area
._resize
= resize_clock
;
92 clock
->area
.resize
= 1;
93 clock
->area
.redraw
= 1;
94 clock
->area
.on_screen
= 1;
96 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
97 get_text_size(time1_font_desc
, &time_height_ink
, &time_height
, panel
->area
.height
, buf_time
, strlen(buf_time
));
99 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
100 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
103 if (panel_horizontal
) {
104 // panel horizonal => fixed height and posy
105 clock
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
106 clock
->area
.height
= panel
->area
.height
- (2 * clock
->area
.posy
);
109 // panel vertical => fixed width, height, posy and posx
110 clock
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingxlr
;
111 clock
->area
.height
= (2 * clock
->area
.paddingxlr
) + (time_height
+ date_height
);
112 clock
->area
.posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
113 clock
->area
.width
= panel
->area
.width
- (2 * panel
->area
.pix
.border
.width
) - (2 * panel
->area
.paddingy
);
116 clock
->time1_posy
= (clock
->area
.height
- time_height
) / 2;
118 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
119 get_text_size(time2_font_desc
, &date_height_ink
, &date_height
, panel
->area
.height
, buf_date
, strlen(buf_date
));
121 clock
->time1_posy
-= ((date_height_ink
+ 2) / 2);
122 clock
->time2_posy
= clock
->time1_posy
+ time_height
+ 2 - (time_height
- time_height_ink
)/2 - (date_height
- date_height_ink
)/2;
125 if (time_tooltip_format
) {
126 clock
->area
._get_tooltip_text
= clock_get_tooltip
;
127 strftime(buf_tooltip
, sizeof(buf_tooltip
), time_tooltip_format
, localtime(&time_clock
.tv_sec
));
136 pango_font_description_free(time1_font_desc
);
138 pango_font_description_free(time2_font_desc
);
140 g_free(time1_format
);
142 g_free(time2_format
);
143 if (time_tooltip_format
)
144 g_free(time_tooltip_format
);
145 if (clock_lclick_command
)
146 g_free(clock_lclick_command
);
147 if (clock_rclick_command
)
148 g_free(clock_rclick_command
);
149 time1_font_desc
= time2_font_desc
= 0;
150 time1_format
= time2_format
= 0;
151 clock_lclick_command
= clock_rclick_command
= 0;
155 void draw_clock (void *obj
, cairo_t
*c
, int active
)
160 layout
= pango_cairo_create_layout (c
);
163 pango_layout_set_font_description (layout
, time1_font_desc
);
164 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
165 pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
166 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
168 cairo_set_source_rgba (c
, clock
->font
.color
[0], clock
->font
.color
[1], clock
->font
.color
[2], clock
->font
.alpha
);
170 pango_cairo_update_layout (c
, layout
);
171 cairo_move_to (c
, 0, clock
->time1_posy
);
172 pango_cairo_show_layout (c
, layout
);
175 pango_layout_set_font_description (layout
, time2_font_desc
);
176 pango_layout_set_indent(layout
, 0);
177 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
178 pango_layout_set_width (layout
, clock
->area
.width
* PANGO_SCALE
);
180 pango_cairo_update_layout (c
, layout
);
181 cairo_move_to (c
, 0, clock
->time2_posy
);
182 pango_cairo_show_layout (c
, layout
);
185 g_object_unref (layout
);
189 void resize_clock (void *obj
)
193 int time_width
, date_width
, new_width
;
195 clock
->area
.redraw
= 1;
196 time_width
= date_width
= 0;
197 strftime(buf_time
, sizeof(buf_time
), time1_format
, localtime(&time_clock
.tv_sec
));
199 strftime(buf_date
, sizeof(buf_date
), time2_format
, localtime(&time_clock
.tv_sec
));
201 // vertical panel doen't adjust width
202 if (!panel_horizontal
) return;
204 //printf(" resize_clock\n");
208 pmap
= XCreatePixmap (server
.dsp
, server
.root_win
, clock
->area
.width
, clock
->area
.height
, server
.depth
);
210 cs
= cairo_xlib_surface_create (server
.dsp
, pmap
, server
.visual
, clock
->area
.width
, clock
->area
.height
);
211 c
= cairo_create (cs
);
212 layout
= pango_cairo_create_layout (c
);
215 pango_layout_set_font_description (layout
, time1_font_desc
);
216 pango_layout_set_indent(layout
, 0);
217 pango_layout_set_text (layout
, buf_time
, strlen(buf_time
));
218 pango_layout_get_pixel_size (layout
, &time_width
, NULL
);
220 pango_layout_set_font_description (layout
, time2_font_desc
);
221 pango_layout_set_indent(layout
, 0);
222 pango_layout_set_text (layout
, buf_date
, strlen(buf_date
));
223 pango_layout_get_pixel_size (layout
, &date_width
, NULL
);
226 if (time_width
> date_width
) new_width
= time_width
;
227 else new_width
= date_width
;
228 new_width
+= (2*clock
->area
.paddingxlr
) + (2*clock
->area
.pix
.border
.width
);
230 Panel
*panel
= ((Area
*)obj
)->panel
;
231 clock
->area
.posx
= panel
->area
.width
- clock
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.pix
.border
.width
;
233 if (new_width
> clock
->area
.width
|| new_width
< (clock
->area
.width
-6)) {
235 // we try to limit the number of resize
236 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
237 clock
->area
.width
= new_width
+ 1;
239 // resize other objects on panel
240 panel
->area
.resize
= 1;
241 #ifdef ENABLE_BATTERY
242 panel
->battery
.area
.resize
= 1;
244 systray
.area
.resize
= 1;
248 g_object_unref (layout
);
250 cairo_surface_destroy (cs
);
251 XFreePixmap (server
.dsp
, pmap
);
255 void clock_action(int button
)
260 command
= clock_lclick_command
;
263 command
= clock_rclick_command
;
270 // change for the fork the signal mask
272 sigprocmask(SIG_SETMASK
, &sigset
, 0);
273 sigprocmask(SIG_UNBLOCK
, &sigset
, 0);
274 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
This page took 0.04447 seconds and 4 git commands to generate.