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