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