]>
Dogcows Code - chaz/tint2/blob - src/tint.c
718668f160e43d33604edbfd57074ba9eeba7715
1 /**************************************************************************
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
27 #include <X11/Xutil.h>
28 #include <X11/Xatom.h>
29 #include <X11/Xlocale.h>
38 #include "systraybar.h"
42 void signal_handler(int sig
)
44 // signal handler is light as it should be
52 signal(SIGUSR1
, signal_handler
);
53 signal(SIGINT
, signal_handler
);
54 signal(SIGTERM
, signal_handler
);
57 memset(&server
, 0, sizeof(Server_global
));
58 memset(&systray
, 0, sizeof(Systraybar
));
60 server
.dsp
= XOpenDisplay (NULL
);
62 fprintf(stderr
, "tint2 exit : could not open display.\n");
66 server
.screen
= DefaultScreen (server
.dsp
);
67 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
68 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
69 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
70 server
.desktop
= server_get_current_desktop ();
72 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
) ;
74 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
76 imlib_context_set_display (server
.dsp
);
77 imlib_context_set_visual (server
.visual
);
78 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
81 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
83 setlocale (LC_ALL
, "");
91 if (time1_font_desc
) pango_font_description_free(time1_font_desc
);
92 if (time2_font_desc
) pango_font_description_free(time2_font_desc
);
93 if (time1_format
) g_free(time1_format
);
94 if (time2_format
) g_free(time2_format
);
95 if (bat1_font_desc
) pango_font_description_free(bat1_font_desc
);
96 if (bat2_font_desc
) pango_font_description_free(bat2_font_desc
);
97 if (battery_low_cmd
) g_free(battery_low_cmd
);
98 if (path_energy_now
) g_free(path_energy_now
);
99 if (path_energy_full
) g_free(path_energy_full
);
100 if (path_current_now
) g_free(path_current_now
);
101 if (path_status
) g_free(path_status
);
102 if (clock_lclick_command
) g_free(clock_lclick_command
);
103 if (clock_rclick_command
) g_free(clock_rclick_command
);
105 if (server
.monitor
) free(server
.monitor
);
106 XFreeGC(server
.dsp
, server
.gc
);
107 XCloseDisplay(server
.dsp
);
111 void window_action (Task
*tsk
, int action
)
115 set_close (tsk
->win
);
118 set_active(tsk
->win
);
121 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
124 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
125 else set_active (tsk
->win
);
128 window_toggle_shade (tsk
->win
);
134 void event_button_press (XEvent
*e
)
136 Panel
*panel
= get_panel(e
->xany
.window
);
140 if ((e
->xbutton
.x
< panel
->area
.paddingxlr
) || (e
->xbutton
.x
> panel
->area
.width
-panel
->area
.paddingxlr
) || (e
->xbutton
.y
< panel
->area
.paddingy
) || (e
->xbutton
.y
> panel
->area
.paddingy
+panel
->g_taskbar
.height
)) {
141 // forward the click to the desktop window (thanks conky)
142 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
143 e
->xbutton
.window
= server
.root_win
;
144 // icewm doesn't open under the mouse.
145 // and xfce doesn't open at all.
146 //e->xbutton.x = e->xbutton.x_root;
147 //e->xbutton.y = e->xbutton.y_root;
148 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
149 XSetInputFocus(server
.dsp
, e
->xbutton
.window
, RevertToParent
, e
->xbutton
.time
);
150 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
155 if (e
->xbutton
.button
!= 1) return;
157 if (panel_mode
!= MULTI_DESKTOP
) {
158 // drag and drop disabled
159 XLowerWindow (server
.dsp
, panel
->main_win
);
165 int x
= e
->xbutton
.x
;
166 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
168 if (!tskbar
->area
.on_screen
) continue;
169 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
175 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
177 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
184 XLowerWindow (server
.dsp
, panel
->main_win
);
188 void event_button_release (XEvent
*e
)
190 Panel
*panel
= get_panel(e
->xany
.window
);
194 if ((e
->xbutton
.x
< panel
->area
.paddingxlr
) || (e
->xbutton
.x
> panel
->area
.width
-panel
->area
.paddingxlr
) || (e
->xbutton
.y
< panel
->area
.paddingy
) || (e
->xbutton
.y
> panel
->area
.paddingy
+panel
->g_taskbar
.height
)) {
195 // forward the click to the desktop window (thanks conky)
196 e
->xbutton
.window
= server
.root_win
;
197 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonReleaseMask
, e
);
202 int action
= TOGGLE_ICONIFY
;
203 int x
= e
->xbutton
.x
;
204 //int y = e->xbutton.y; // unused
205 switch (e
->xbutton
.button
) {
207 action
= mouse_middle
;
210 action
= mouse_right
;
213 action
= mouse_scroll_up
;
216 action
= mouse_scroll_down
;
223 Clock clk
= panel
->clock
;
224 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
225 clock_action(e
->xbutton
.button
);
227 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
229 if (!tskbar
->area
.on_screen
) continue;
230 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
235 // TODO: check better solution to keep window below
236 XLowerWindow (server
.dsp
, panel
->main_win
);
241 // drag and drop task
243 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
244 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
245 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
246 if (tskbar
->desktop
== server
.desktop
)
247 set_active(task_drag
->win
);
256 if (panel_mode
== MULTI_DESKTOP
)
257 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
)
258 set_desktop (tskbar
->desktop
);
263 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
265 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
266 window_action (tsk
, action
);
271 // to keep window below
272 XLowerWindow (server
.dsp
, panel
->main_win
);
276 void event_property_notify (XEvent
*e
)
280 Window win
= e
->xproperty
.window
;
281 Atom at
= e
->xproperty
.atom
;
283 if (win
== server
.root_win
) {
284 if (!server
.got_root_win
) {
285 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
286 server
.got_root_win
= 1;
289 // Change number of desktops
290 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
291 server
.nb_desktop
= server_get_number_of_desktop ();
295 for (i
=0 ; i
< nb_panel
; i
++) {
296 panel1
[i
].area
.resize
= 1;
298 task_refresh_tasklist();
302 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
303 server
.desktop
= server_get_current_desktop ();
304 if (panel_mode
!= MULTI_DESKTOP
) {
309 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
310 task_refresh_tasklist();
314 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
317 for (i
=0 ; i
< nb_panel
; i
++) {
318 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
319 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
321 tsk
->area
.is_active
= 0;
327 Window w1
= window_get_active ();
328 Task
*t
= task_get_task(w1
);
331 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
332 if (w2
) t
= task_get_task(w2
);
334 if (task_urgent
== t
) {
339 for (i
=0 ; i
< nb_panel
; i
++) {
340 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
341 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
343 if (tsk
->win
== t
->win
) {
344 tsk
->area
.is_active
= 1;
345 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
355 else if (at
== server
.atom
._XROOTPMAP_ID
) {
356 for (i
=0 ; i
< nb_panel
; i
++) {
357 set_panel_background(&panel1
[i
]);
363 tsk
= task_get_task (win
);
365 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
367 // Window title changed
368 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
372 // changed other tsk->title
373 for (i
=0 ; i
< nb_panel
; i
++) {
374 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
375 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
377 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
378 tsk2
->title
= tsk
->title
;
379 tsk2
->area
.redraw
= 1;
387 else if (at
== server
.atom
._NET_WM_STATE
) {
388 if (window_is_urgent (win
)) {
393 else if (at
== server
.atom
.WM_STATE
) {
395 // TODO : try to delete following code
396 if (window_is_iconified (win
)) {
398 if (task_active
->win
== tsk
->win
) {
401 for (i
=0 ; i
< nb_panel
; i
++) {
402 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
403 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
405 tsk2
->area
.is_active
= 0;
414 // Window icon changed
415 else if (at
== server
.atom
._NET_WM_ICON
) {
419 for (i
=0 ; i
< nb_panel
; i
++) {
420 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
421 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
423 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
424 tsk2
->icon_width
= tsk
->icon_width
;
425 tsk2
->icon_height
= tsk
->icon_height
;
426 tsk2
->icon_data
= tsk
->icon_data
;
427 tsk2
->area
.redraw
= 1;
434 // Window desktop changed
435 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
436 int desktop
= window_get_desktop (win
);
437 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
438 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
439 if (desktop
!= tsk
->desktop
) {
446 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
451 void event_expose (XEvent
*e
)
455 panel
= get_panel(e
->xany
.window
);
458 if (systray.area.on_screen) {
459 // force trayer refresh
460 //XClearWindow(tray_data.dpy, ti->mid_parent);
461 //x11_send_visibility(tray_data.dpy, dst, VisibilityFullyObscured);
462 //x11_send_visibility(tray_data.dpy, dst, VisibilityUnobscured);
466 for (l = systray.list_icons; l ; l = l->next) {
467 traywin = (TrayWindow*)l->data;
469 XClearArea(server.dsp, traywin->id, 0, 0, systray.area.width, systray.area.height, True);
470 //printf("expose %lx\n", traywin->id);
473 //x11_refresh_window(tray_data.dpy, ti->wid, ti->l.wnd_sz.x, ti->l.wnd_sz.y, True);
477 //XCopyArea (server.dsp, panel->temp_pmap, panel->main_win, server.gc, 0, 0, panel->area.width, panel->area.height, 0, 0);
482 void event_configure_notify (Window win
)
484 if (nb_panel
== 1) return;
485 if (server
.nb_monitor
== 1) return;
487 Task
*tsk
= task_get_task (win
);
490 Panel
*p
= tsk
->area
.panel
;
491 if (p
->monitor
!= window_get_monitor (win
)) {
492 // task on another monitor
495 if (win
== window_get_active ()) {
496 Task
*tsk
= task_get_task (win
);
497 tsk
->area
.is_active
= 1;
510 if (gettimeofday(&stv
, 0)) return;
512 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
513 time_clock
.tv_sec
= stv
.tv_sec
;
514 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
518 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
519 task_urgent
->area
.redraw
= 1;
523 if (panel1
[0].battery
.area
.on_screen
) {
524 update_battery(&battery_state
);
525 for (i
=0 ; i
< nb_panel
; i
++)
526 panel1
[i
].battery
.area
.resize
= 1;
531 for (i
=0 ; i
< nb_panel
; i
++)
532 panel1
[i
].clock
.area
.resize
= 1;
538 int main (int argc
, char *argv
[])
547 c
= getopt (argc
, argv
, "c:");
554 i
= config_read_file (optarg
);
558 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
564 x11_fd
= ConnectionNumber(server
.dsp
);
565 XSync(server
.dsp
, False
);
568 // thanks to AngryLlama for the timer
569 // Create a File Description Set containing x11_fd
571 FD_SET (x11_fd
, &fd
);
576 // Wait for X Event or a Timer
577 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
578 while (XPending (server
.dsp
)) {
579 XNextEvent(server
.dsp
, &e
);
583 event_button_press (&e
);
587 event_button_release(&e
);
595 event_property_notify(&e
);
598 case ConfigureNotify
:
599 if (e
.xconfigure
.window
== server
.root_win
)
602 event_configure_notify (e
.xconfigure
.window
);
607 if (!systray
.area
.on_screen
) break;
608 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
609 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
610 remove_icon((TrayWindow
*)it
->data
);
617 if (!systray
.area
.on_screen
) break;
618 //printf("ClientMessage\n");
619 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
620 net_message(&e
.xclient
);
628 switch (signal_pending
) {
640 for (i
=0 ; i
< nb_panel
; i
++) {
643 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
644 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
646 refresh(&panel
->area
);
647 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
This page took 0.066013 seconds and 4 git commands to generate.