]> Dogcows Code - chaz/tint2/blob - src/tint.c
fixed bugs with new design (first step)
[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 // TODO: convert event_button_press(int x, int y) to area->event_button_press()
162 // if systray is ok
163
164 switch (button) {
165 case 2:
166 action = panel.mouse_middle;
167 break;
168 case 3:
169 action = panel.mouse_right;
170 break;
171 case 4:
172 action = panel.mouse_scroll_up;
173 break;
174 case 5:
175 action = panel.mouse_scroll_down;
176 break;
177 }
178
179 // search taskbar
180 Taskbar *tskbar;
181 GSList *l0;
182 for (l0 = panel.area.list; l0 ; l0 = l0->next) {
183 tskbar = l0->data;
184 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
185 goto suite;
186 }
187
188 // TODO: check better solution to keep window below
189 XLowerWindow (server.dsp, window.main_win);
190 panel.task_drag = 0;
191 return;
192
193 suite:
194 // drag and drop task
195 if (panel.task_drag) {
196 if (tskbar != panel.task_drag->area.parent && action == TOGGLE_ICONIFY) {
197 windows_set_desktop(panel.task_drag->win, tskbar->desktop);
198 if (tskbar->desktop == server.desktop)
199 set_active(panel.task_drag->win);
200 panel.task_drag = 0;
201 return;
202 }
203 else panel.task_drag = 0;
204 }
205
206 // switch desktop
207 if (panel.mode == MULTI_DESKTOP)
208 if (tskbar->desktop != server.desktop && action != CLOSE)
209 set_desktop (tskbar->desktop);
210
211 // action on task
212 Task *tsk;
213 GSList *l;
214 for (l = tskbar->area.list ; l ; l = l->next) {
215 tsk = l->data;
216 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
217 window_action (tsk, action);
218 break;
219 }
220 }
221
222 // to keep window below
223 XLowerWindow (server.dsp, window.main_win);
224 }
225
226
227 void event_property_notify (Window win, Atom at)
228 {
229
230 if (win == server.root_win) {
231 if (!server.got_root_win) {
232 XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask);
233 server.got_root_win = 1;
234 }
235
236 /* Change number of desktops */
237 else if (at == server.atom._NET_NUMBER_OF_DESKTOPS) {
238 config_taskbar();
239 visible_object();
240 redraw(&panel.area);
241 }
242 /* Change desktop */
243 else if (at == server.atom._NET_CURRENT_DESKTOP) {
244 server.desktop = server_get_current_desktop ();
245 if (panel.mode != MULTI_DESKTOP) {
246 visible_object();
247 }
248 }
249 /* Window list */
250 else if (at == server.atom._NET_CLIENT_LIST) {
251 task_refresh_tasklist ();
252 panel.refresh = 1;
253 }
254 /* Active */
255 else if (at == server.atom._NET_ACTIVE_WINDOW) {
256 Window w1 = window_get_active ();
257 Task *t = task_get_task(w1);
258 if (t) panel.task_active = t;
259 else {
260 Window w2;
261 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
262 if (w2) panel.task_active = task_get_task(w2);
263 }
264 panel.refresh = 1;
265 }
266 /* Wallpaper changed */
267 else if (at == server.atom._XROOTPMAP_ID) {
268 XFreePixmap (server.dsp, server.root_pmap);
269 server.root_pmap = 0;
270 redraw(&panel.area);
271 panel.clock.area.redraw = 1;
272 panel.refresh = 1;
273 }
274 }
275 else {
276 Task *tsk;
277 tsk = task_get_task (win);
278 if (!tsk) return;
279 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
280
281 /* Window title changed */
282 if (at == server.atom._NET_WM_VISIBLE_NAME || at == server.atom._NET_WM_NAME || at == server.atom.WM_NAME) {
283 get_title(tsk);
284 tsk->area.redraw = 1;
285 panel.refresh = 1;
286 }
287 /* Iconic state */
288 else if (at == server.atom.WM_STATE) {
289 if (window_is_iconified (win))
290 if (panel.task_active == tsk) panel.task_active = 0;
291 }
292 /* Window icon changed */
293 else if (at == server.atom._NET_WM_ICON) {
294 if (tsk->icon_data != 0) XFree (tsk->icon_data);
295 tsk->area.redraw = 1;
296 tsk->icon_data = 0;
297 panel.refresh = 1;
298 }
299 /* Window desktop changed */
300 else if (at == server.atom._NET_WM_DESKTOP) {
301 add_task (tsk->win);
302 remove_task (tsk);
303 panel.refresh = 1;
304 }
305
306 if (!server.got_root_win) server.root_win = RootWindow (server.dsp, server.screen);
307 }
308 }
309
310
311 void event_configure_notify (Window win)
312 {
313 Task *tsk;
314
315 tsk = task_get_task (win);
316 if (!tsk) return;
317
318 Taskbar *tskbar = tsk->area.parent;
319 if (tskbar->monitor != window_get_monitor (win)) {
320 // task on another monitor
321 add_task (tsk->win);
322 remove_task (tsk);
323 panel.refresh = 1;
324 }
325 }
326
327
328 void event_timer()
329 {
330 struct timeval stv;
331
332 if (!panel.clock.time1_format) return;
333
334 if (gettimeofday(&stv, 0)) return;
335
336 if (abs(stv.tv_sec - panel.clock.clock.tv_sec) < panel.clock.time_precision) return;
337
338 // update clock
339 panel.clock.clock.tv_sec = stv.tv_sec;
340 panel.clock.clock.tv_sec -= panel.clock.clock.tv_sec % panel.clock.time_precision;
341 panel.clock.area.redraw = 1;
342 panel.refresh = 1;
343 }
344
345
346 int main (int argc, char *argv[])
347 {
348 XEvent e;
349 fd_set fd;
350 int x11_fd, i, c;
351 struct timeval tv;
352
353 c = getopt (argc, argv, "c:");
354 init ();
355
356 load_config:
357 if (server.root_pmap) XFreePixmap (server.dsp, server.root_pmap);
358 server.root_pmap = 0;
359 // read tint2rc config
360 i = 0;
361 if (c != -1)
362 i = config_read_file (optarg);
363 if (!i)
364 i = config_read ();
365 if (!i) {
366 fprintf(stderr, "usage: tint2 [-c] <config_file>\n");
367 cleanup();
368 exit(1);
369 }
370 config_finish ();
371
372 window_draw_panel ();
373
374 // BUG: draw(clock) is needed here, but 'on the paper' it's not necessary.
375 draw(&panel.clock.area);
376
377 x11_fd = ConnectionNumber (server.dsp);
378 XSync (server.dsp, False);
379
380 while (1) {
381 // thanks to AngryLlama for the timer
382 // Create a File Description Set containing x11_fd
383 FD_ZERO (&fd);
384 FD_SET (x11_fd, &fd);
385
386 tv.tv_usec = 500000;
387 tv.tv_sec = 0;
388
389 // Wait for X Event or a Timer
390 if (select(x11_fd+1, &fd, 0, 0, &tv)) {
391 while (XPending (server.dsp)) {
392 XNextEvent(server.dsp, &e);
393
394 switch (e.type) {
395 case ButtonPress:
396 if (e.xbutton.button == 1) event_button_press (e.xbutton.x, e.xbutton.y);
397 break;
398
399 case ButtonRelease:
400 event_button_release (e.xbutton.button, e.xbutton.x, e.xbutton.y);
401 break;
402
403 case Expose:
404 XCopyArea (server.dsp, server.pmap, window.main_win, server.gc, 0, 0, panel.area.width, panel.area.height, 0, 0);
405 break;
406
407 case PropertyNotify:
408 //printf("PropertyNotify\n");
409 event_property_notify (e.xproperty.window, e.xproperty.atom);
410 break;
411
412 case ConfigureNotify:
413 if (e.xconfigure.window == server.root_win)
414 goto load_config;
415 else
416 if (panel.mode == MULTI_MONITOR)
417 event_configure_notify (e.xconfigure.window);
418 break;
419 }
420 }
421 }
422 else event_timer();
423
424 switch (panel.signal_pending) {
425 case SIGUSR1:
426 goto load_config;
427 case SIGINT:
428 case SIGTERM:
429 cleanup ();
430 return 0;
431 }
432
433 if (panel.refresh && !panel.sleep_mode) {
434 visual_refresh ();
435 //printf(" *** visual_refresh\n");
436 }
437 }
438 }
439
440
This page took 0.057187 seconds and 4 git commands to generate.