]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
*fix* use another timer implementation, coz *BSD does not support timerfd :(
[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
28 #include "window.h"
29 #include "server.h"
30 #include "area.h"
31 #include "panel.h"
32 #include "taskbar.h"
33 #include "clock.h"
34 #include "timer.h"
35
36
37 char *time1_format=0;
38 char *time2_format=0;
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];
48 int clock_enabled;
49 static const struct timeout* clock_timeout=0;
50
51
52 void update_clocks()
53 {
54 gettimeofday(&time_clock, 0);
55 int i;
56 if (time1_format) {
57 for (i=0 ; i < nb_panel ; i++)
58 panel1[i].clock.area.resize = 1;
59 }
60 panel_refresh = 1;
61 }
62
63
64 const char* clock_get_tooltip(void* obj)
65 {
66 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, localtime(&time_clock.tv_sec));
67 return buf_tooltip;
68 }
69
70
71 void init_clock()
72 {
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);
76 else
77 clock_timeout = add_timeout(10, 60000, update_clocks);
78 }
79 }
80
81
82 void init_clock_panel(void *p)
83 {
84 Panel *panel =(Panel*)p;
85 Clock *clock = &panel->clock;
86 int time_height, time_height_ink, date_height, date_height_ink;
87
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;
95
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));
98 if (time2_format) {
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));
101 }
102
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);
107 }
108 else {
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);
114 }
115
116 clock->time1_posy = (clock->area.height - time_height) / 2;
117 if (time2_format) {
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));
120
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;
123 }
124
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));
128 }
129 }
130
131
132 void cleanup_clock()
133 {
134 clock_enabled = 0;
135 if (time1_font_desc)
136 pango_font_description_free(time1_font_desc);
137 if (time2_font_desc)
138 pango_font_description_free(time2_font_desc);
139 if (time1_format)
140 g_free(time1_format);
141 if (time2_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;
152 }
153
154
155 void draw_clock (void *obj, cairo_t *c, int active)
156 {
157 Clock *clock = obj;
158 PangoLayout *layout;
159
160 layout = pango_cairo_create_layout (c);
161
162 // draw layout
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));
167
168 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
169
170 pango_cairo_update_layout (c, layout);
171 cairo_move_to (c, 0, clock->time1_posy);
172 pango_cairo_show_layout (c, layout);
173
174 if (time2_format) {
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);
179
180 pango_cairo_update_layout (c, layout);
181 cairo_move_to (c, 0, clock->time2_posy);
182 pango_cairo_show_layout (c, layout);
183 }
184
185 g_object_unref (layout);
186 }
187
188
189 void resize_clock (void *obj)
190 {
191 Clock *clock = obj;
192 PangoLayout *layout;
193 int time_width, date_width, new_width;
194
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));
198 if (time2_format)
199 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
200
201 // vertical panel doen't adjust width
202 if (!panel_horizontal) return;
203
204 //printf(" resize_clock\n");
205 cairo_surface_t *cs;
206 cairo_t *c;
207 Pixmap pmap;
208 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
209
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);
213
214 // check width
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);
219 if (time2_format) {
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);
224 }
225
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);
229
230 Panel *panel = ((Area*)obj)->panel;
231 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
232
233 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
234 // resize clock
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;
238
239 // resize other objects on panel
240 panel->area.resize = 1;
241 #ifdef ENABLE_BATTERY
242 panel->battery.area.resize = 1;
243 #endif
244 systray.area.resize = 1;
245 panel_refresh = 1;
246 }
247
248 g_object_unref (layout);
249 cairo_destroy (c);
250 cairo_surface_destroy (cs);
251 XFreePixmap (server.dsp, pmap);
252 }
253
254
255 void clock_action(int button)
256 {
257 char *command = 0;
258 switch (button) {
259 case 1:
260 command = clock_lclick_command;
261 break;
262 case 3:
263 command = clock_rclick_command;
264 break;
265 }
266 if (command) {
267 pid_t pid;
268 pid = fork();
269 if (pid == 0) {
270 // change for the fork the signal mask
271 sigset_t sigset;
272 sigprocmask(SIG_SETMASK, &sigset, 0);
273 sigprocmask(SIG_UNBLOCK, &sigset, 0);
274 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
275 _exit(0);
276 }
277 }
278 }
279
This page took 0.049278 seconds and 5 git commands to generate.