]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
fixed config reload SIGUSR1. added systray = 1 parameter to enable systray
[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
27 #include "window.h"
28 #include "server.h"
29 #include "area.h"
30 #include "panel.h"
31 #include "taskbar.h"
32 #include "clock.h"
33
34
35 char *time1_format=0;
36 char *time2_format=0;
37 char *clock_lclick_command=0;
38 char *clock_rclick_command=0;
39 struct timeval time_clock;
40 int time_precision;
41 PangoFontDescription *time1_font_desc=0;
42 PangoFontDescription *time2_font_desc=0;
43 static char buf_time[40];
44 static char buf_date[40];
45 int clock_enabled;
46
47
48 void init_precision()
49 {
50 if (!time1_format) time_precision = 60;
51 else if (strchr(time1_format, 'S')) time_precision = 1;
52 else if (strchr(time1_format, 'T')) time_precision = 1;
53 else if (strchr(time1_format, 'r')) time_precision = 1;
54 else time_precision = 60;
55 }
56
57
58 void init_clock()
59 {
60 init_precision();
61
62 // update clock to force update (-time_precision)
63 struct timeval stv;
64 gettimeofday(&stv, 0);
65 time_clock.tv_sec = stv.tv_sec - time_precision;
66 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
67 }
68
69
70 void init_clock_panel(void *p)
71 {
72 Panel *panel =(Panel*)p;
73 Clock *clock = &panel->clock;
74 int time_height, time_height_ink, date_height, date_height_ink;
75
76 clock->area.parent = p;
77 clock->area.panel = p;
78 clock->area._draw_foreground = draw_clock;
79 clock->area._resize = resize_clock;
80 clock->area.resize = 1;
81 clock->area.redraw = 1;
82 clock->area.on_screen = 1;
83
84 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
85 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
86 if (time2_format) {
87 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
88 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
89 }
90
91 if (panel_horizontal) {
92 // panel horizonal => fixed height and posy
93 clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
94 clock->area.height = panel->area.height - (2 * clock->area.posy);
95 }
96 else {
97 // panel vertical => fixed width, height, posy and posx
98 clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
99 clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
100 clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
101 clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
102 }
103
104 clock->time1_posy = (clock->area.height - time_height) / 2;
105 if (time2_format) {
106 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
107 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
108
109 clock->time1_posy -= ((date_height_ink + 2) / 2);
110 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
111 }
112 }
113
114
115 void cleanup_clock()
116 {
117 clock_enabled = 0;
118 if (time1_font_desc)
119 pango_font_description_free(time1_font_desc);
120 if (time2_font_desc)
121 pango_font_description_free(time2_font_desc);
122 if (time1_format)
123 g_free(time1_format);
124 if (time2_format)
125 g_free(time2_format);
126 if (clock_lclick_command)
127 g_free(clock_lclick_command);
128 if (clock_rclick_command)
129 g_free(clock_rclick_command);
130 time1_font_desc = time2_font_desc = 0;
131 time1_format = time2_format = 0;
132 clock_lclick_command = clock_rclick_command = 0;
133 }
134
135
136 void draw_clock (void *obj, cairo_t *c, int active)
137 {
138 Clock *clock = obj;
139 PangoLayout *layout;
140
141 layout = pango_cairo_create_layout (c);
142
143 // draw layout
144 pango_layout_set_font_description (layout, time1_font_desc);
145 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
146 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
147 pango_layout_set_text (layout, buf_time, strlen(buf_time));
148
149 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
150
151 pango_cairo_update_layout (c, layout);
152 cairo_move_to (c, 0, clock->time1_posy);
153 pango_cairo_show_layout (c, layout);
154
155 if (time2_format) {
156 pango_layout_set_font_description (layout, time2_font_desc);
157 pango_layout_set_indent(layout, 0);
158 pango_layout_set_text (layout, buf_date, strlen(buf_date));
159 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
160
161 pango_cairo_update_layout (c, layout);
162 cairo_move_to (c, 0, clock->time2_posy);
163 pango_cairo_show_layout (c, layout);
164 }
165
166 g_object_unref (layout);
167 }
168
169
170 void resize_clock (void *obj)
171 {
172 Clock *clock = obj;
173 PangoLayout *layout;
174 int time_width, date_width, new_width;
175
176 clock->area.redraw = 1;
177 time_width = date_width = 0;
178 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
179 if (time2_format)
180 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
181
182 // vertical panel doen't adjust width
183 if (!panel_horizontal) return;
184
185 //printf(" resize_clock\n");
186 cairo_surface_t *cs;
187 cairo_t *c;
188 Pixmap pmap;
189 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
190
191 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
192 c = cairo_create (cs);
193 layout = pango_cairo_create_layout (c);
194
195 // check width
196 pango_layout_set_font_description (layout, time1_font_desc);
197 pango_layout_set_indent(layout, 0);
198 pango_layout_set_text (layout, buf_time, strlen(buf_time));
199 pango_layout_get_pixel_size (layout, &time_width, NULL);
200 if (time2_format) {
201 pango_layout_set_font_description (layout, time2_font_desc);
202 pango_layout_set_indent(layout, 0);
203 pango_layout_set_text (layout, buf_date, strlen(buf_date));
204 pango_layout_get_pixel_size (layout, &date_width, NULL);
205 }
206
207 if (time_width > date_width) new_width = time_width;
208 else new_width = date_width;
209 new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
210
211 Panel *panel = ((Area*)obj)->panel;
212 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
213
214 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
215 // resize clock
216 // we try to limit the number of resize
217 // printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
218 clock->area.width = new_width + 1;
219
220 // resize other objects on panel
221 panel->area.resize = 1;
222 #ifdef ENABLE_BATTERY
223 panel->battery.area.resize = 1;
224 #endif
225 systray.area.resize = 1;
226 panel_refresh = 1;
227 }
228
229 g_object_unref (layout);
230 cairo_destroy (c);
231 cairo_surface_destroy (cs);
232 XFreePixmap (server.dsp, pmap);
233 }
234
235
236 void clock_action(int button)
237 {
238 char *command = 0;
239 switch (button) {
240 case 1:
241 command = clock_lclick_command;
242 break;
243 case 3:
244 command = clock_rclick_command;
245 break;
246 }
247 if (command) {
248 pid_t pid;
249 pid = fork();
250 if (pid == 0) {
251 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
252 _exit(0);
253 }
254 }
255 }
256
This page took 0.044938 seconds and 5 git commands to generate.