]> Dogcows Code - chaz/tint2/blob - src/tint.c
fixed issue and test svn
[chaz/tint2] / src / tint.c
1 /**************************************************************************
2 *
3 * Tint2 panel
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29 #include <X11/Xlocale.h>
30 #include <Imlib2.h>
31 #include <signal.h>
32
33 #include "server.h"
34 #include "window.h"
35 #include "config.h"
36 #include "task.h"
37 #include "taskbar.h"
38 #include "panel.h"
39 #include "docker.h"
40 #include "net.h"
41 #include "kde.h"
42
43
44 void signal_handler(int sig)
45 {
46 // signal handler is light as it should be
47 panel.signal_pending = sig;
48 }
49
50
51 void init ()
52 {
53 // Set signal handler
54 signal(SIGUSR1, signal_handler);
55 signal(SIGINT, signal_handler);
56 signal(SIGTERM, signal_handler);
57
58 // set global data
59 memset(&panel, 0, sizeof(Panel));
60 memset(&server, 0, sizeof(Server_global));
61 memset(&g_task, 0, sizeof(Global_task));
62 memset(&g_taskbar, 0, sizeof(Area));
63 panel.clock.area.draw_foreground = draw_foreground_clock;
64 g_task.area.draw_foreground = draw_foreground_task;
65 window.main_win = 0;
66
67 // append full transparency background
68 //Area *back = calloc(1, sizeof(Area));
69 list_back = g_slist_append(0, calloc(1, sizeof(Area)));
70
71 server.dsp = XOpenDisplay (NULL);
72 if (!server.dsp) {
73 fprintf(stderr, "Could not open display.\n");
74 exit(0);
75 }
76 server_init_atoms ();
77 server.screen = DefaultScreen (server.dsp);
78 server.root_win = RootWindow(server.dsp, server.screen);
79 server.depth = DefaultDepth (server.dsp, server.screen);
80 server.visual = DefaultVisual (server.dsp, server.screen);
81 server.desktop = server_get_current_desktop ();
82
83 XSetErrorHandler ((XErrorHandler) server_catch_error);
84
85 // init systray
86 display = server.dsp;
87 root = RootWindow(display, DefaultScreen(display));
88 //create_main_window();
89 //kde_init();
90 //net_init();
91 //printf("ici 4\n");
92
93 imlib_context_set_display (server.dsp);
94 imlib_context_set_visual (server.visual);
95 imlib_context_set_colormap (DefaultColormap (server.dsp, server.screen));
96
97 /* Catch events */
98 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
99
100 setlocale(LC_ALL, "");
101 }
102
103
104 void window_action (Task *tsk, int action)
105 {
106 switch (action) {
107 case CLOSE:
108 set_close (tsk->win);
109 break;
110 case TOGGLE:
111 set_active(tsk->win);
112 break;
113 case ICONIFY:
114 XIconifyWindow (server.dsp, tsk->win, server.screen);
115 break;
116 case TOGGLE_ICONIFY:
117 if (tsk == panel.task_active) XIconifyWindow (server.dsp, tsk->win, server.screen);
118 else set_active (tsk->win);
119 break;
120 case SHADE:
121 window_toggle_shade (tsk->win);
122 break;
123 }
124 }
125
126
127 void event_button_press (int x, int y)
128 {
129 if (panel.mode == SINGLE_DESKTOP) {
130 // drag and drop disabled
131 XLowerWindow (server.dsp, window.main_win);
132 return;
133 }
134
135 Taskbar *tskbar;
136 GSList *l0;
137 for (l0 = panel.area.list; l0 ; l0 = l0->next) {
138 tskbar = l0->data;
139 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
140 break;
141 }
142
143 if (l0) {
144 Task *tsk;
145 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
146 tsk = l0->data;
147 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
148 panel.task_drag = tsk;
149 break;
150 }
151 }
152 }
153
154 XLowerWindow (server.dsp, window.main_win);
155 }
156
157
158 void event_button_release (int button, int x, int y)
159 {
160 int action = TOGGLE_ICONIFY;
161
162 switch (button) {
163 case 2:
164 action = panel.mouse_middle;
165 break;
166 case 3:
167 action = panel.mouse_right;
168 break;
169 case 4:
170 action = panel.mouse_scroll_up;
171 break;
172 case 5:
173 action = panel.mouse_scroll_down;
174 break;
175 }
176
177 // TODO: ne pas afficher les taskbar invisibles
178 //if (panel.mode != MULTI_DESKTOP && desktop != server.desktop) continue;
179
180 // search taskbar
181 Taskbar *tskbar;
182 GSList *l0;
183 for (l0 = panel.area.list; l0 ; l0 = l0->next) {
184 tskbar = l0->data;
185 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
186 goto suite;
187 }
188
189 // TODO: check better solution to keep window below
190 XLowerWindow (server.dsp, window.main_win);
191 panel.task_drag = 0;
192 return;
193
194 suite:
195 // drag and drop task
196 if (panel.task_drag) {
197 if (tskbar != panel.task_drag->area.parent && action == TOGGLE_ICONIFY) {
198 windows_set_desktop(panel.task_drag->win, tskbar->desktop);
199 if (tskbar->desktop == server.desktop)
200 set_active(panel.task_drag->win);
201 panel.task_drag = 0;
202 return;
203 }
204 else panel.task_drag = 0;
205 }
206
207 // switch desktop
208 if (panel.mode == MULTI_DESKTOP)
209 if (tskbar->desktop != server.desktop && action != CLOSE)
210 set_desktop (tskbar->desktop);
211
212 // action on task
213 Task *tsk;
214 GSList *l;
215 for (l = tskbar->area.list ; l ; l = l->next) {
216 tsk = l->data;
217 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
218 window_action (tsk, action);
219 break;
220 }
221 }
222
223 // to keep window below
224 XLowerWindow (server.dsp, window.main_win);
225 }
226
227
228 void event_property_notify (Window win, Atom at)
229 {
230
231 if (win == server.root_win) {
232 if (!server.got_root_win) {
233 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
234 server.got_root_win = 1;
235 }
236
237 /* Change number of desktops */
238 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
239 config_taskbar();
240 redraw(&panel.area);
241 panel.refresh = 1;
242 }
243 /* Change desktop */
244 else if (at == server.atom._NET_CURRENT_DESKTOP) {
245 server.desktop = server_get_current_desktop ();
246 if (panel.mode != MULTI_DESKTOP) panel.refresh = 1;
247 }
248 /* Window list */
249 else if (at == server.atom._NET_CLIENT_LIST) {
250 task_refresh_tasklist ();
251 panel.refresh = 1;
252 }
253 /* Active */
254 else if (at == server.atom._NET_ACTIVE_WINDOW) {
255 Window w1 = window_get_active ();
256 Task *t = task_get_task(w1);
257 if (t) panel.task_active = t;
258 else {
259 Window w2;
260 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
261 if (w2) panel.task_active = task_get_task(w2);
262 }
263 panel.refresh = 1;
264 }
265 /* Wallpaper changed */
266 else if (at == server.atom._XROOTPMAP_ID) {
267 XFreePixmap (server.dsp, server.root_pmap);
268 server.root_pmap = 0;
269 redraw(&panel.area);
270 panel.clock.area.redraw = 1;
271 panel.refresh = 1;
272 }
273 }
274 else {
275 Task *tsk;
276 tsk = task_get_task (win);
277 if (!tsk) return;
278 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
279
280 /* Window title changed */
281 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
282 get_title(tsk);
283 tsk->area.redraw = 1;
284 panel.refresh = 1;
285 }
286 /* Iconic state */
287 else if (at == server.atom.WM_STATE) {
288 if (window_is_iconified (win))
289 if (panel.task_active == tsk) panel.task_active = 0;
290 }
291 /* Window icon changed */
292 else if (at == server.atom._NET_WM_ICON) {
293 if (tsk->icon_data != 0) XFree (tsk->icon_data);
294 tsk->area.redraw = 1;
295 tsk->icon_data = 0;
296 panel.refresh = 1;
297 }
298 /* Window desktop changed */
299 else if (at == server.atom._NET_WM_DESKTOP) {
300 add_task (tsk->win);
301 remove_task (tsk);
302 panel.refresh = 1;
303 }
304
305 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
306 }
307 }
308
309
310 void event_configure_notify (Window win)
311 {
312 Task *tsk;
313
314 tsk = task_get_task (win);
315 if (!tsk) return;
316
317 /* TODO ??? voir ancien code !!
318 Taskbar *tskbar;
319 tskbar = tsk->area.parent;
320 int new_monitor = window_get_monitor (win);
321 int desktop = tskbar->desktop;
322
323 // task on the same monitor
324 if (tsk->id_taskbar == index(desktop, new_monitor)) return;
325
326 add_task (tsk->win);
327 remove_task (tsk);
328 panel.refresh = 1;
329 */
330 }
331
332
333 void event_timer()
334 {
335 struct timeval stv;
336
337 if (!panel.clock.time1_format) return;
338
339 if (gettimeofday(&stv, 0)) return;
340
341 if (abs(stv.tv_sec - panel.clock.clock.tv_sec) < panel.clock.time_precision) return;
342
343 // update clock
344 panel.clock.clock.tv_sec = stv.tv_sec;
345 panel.clock.clock.tv_sec -= panel.clock.clock.tv_sec % panel.clock.time_precision;
346 panel.clock.area.redraw = 1;
347 panel.refresh = 1;
348 }
349
350
351 int main (int argc, char *argv[])
352 {
353 XEvent e;
354 fd_set fd;
355 int x11_fd, i, c;
356 struct timeval tv;
357
358 c = getopt (argc, argv, "c:");
359 init ();
360
361 load_config:
362 if (server.root_pmap) XFreePixmap (server.dsp, server.root_pmap);
363 server.root_pmap = 0;
364 // read tint2rc config
365 i = 0;
366 if (c != -1)
367 i = config_read_file (optarg);
368 if (!i)
369 i = config_read ();
370 if (!i) {
371 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
372 cleanup();
373 exit(1);
374 }
375 config_finish ();
376
377 window_draw_panel ();
378
379 x11_fd = ConnectionNumber (server.dsp);
380 XSync (server.dsp, False);
381
382 while (1) {
383 // thanks to AngryLlama for the timer
384 // Create a File Description Set containing x11_fd
385 FD_ZERO (&fd);
386 FD_SET (x11_fd, &fd);
387
388 tv.tv_usec = 500000;
389 tv.tv_sec = 0;
390
391 // Wait for X Event or a Timer
392 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
393 while (XPending (server.dsp)) {
394 XNextEvent(server.dsp, &e);
395
396 switch (e.type) {
397 case ButtonPress:
398 if (e.xbutton.button == 1) event_button_press (e.xbutton.x, e.xbutton.y);
399 break;
400
401 case ButtonRelease:
402 event_button_release (e.xbutton.button, e.xbutton.x, e.xbutton.y);
403 break;
404
405 case Expose:
406 XCopyArea (server.dsp, server.pmap, window.main_win, server.gc, 0, 0, panel.area.width, panel.area.height, 0, 0);
407 break;
408
409 case PropertyNotify:
410 //printf("PropertyNotify\n");
411 event_property_notify (e.xproperty.window, e.xproperty.atom);
412 break;
413
414 case ConfigureNotify:
415 if (e.xconfigure.window == server.root_win)
416 goto load_config;
417 else
418 if (panel.mode == MULTI_MONITOR)
419 event_configure_notify (e.xconfigure.window);
420 break;
421 }
422 }
423 }
424 else event_timer();
425
426 switch (panel.signal_pending) {
427 case SIGUSR1:
428 goto load_config;
429 case SIGINT:
430 case SIGTERM:
431 cleanup ();
432 return 0;
433 }
434
435 if (panel.refresh && !panel.sleep_mode) {
436 visual_refresh ();
437 //printf(" *** visual_refresh\n");
438 }
439 }
440 }
441
442
This page took 0.052541 seconds and 4 git commands to generate.