]>
Dogcows Code - chaz/tint2/blob - src/tint.c
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
);
55 signal(SIGHUP
, signal_handler
);
56 signal(SIGCLD
, SIG_IGN
); // don't have to wait() after fork()
59 memset(&server
, 0, sizeof(Server_global
));
60 memset(&systray
, 0, sizeof(Systraybar
));
62 server
.dsp
= XOpenDisplay (NULL
);
64 fprintf(stderr
, "tint2 exit : could not open display.\n");
68 server
.screen
= DefaultScreen (server
.dsp
);
69 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
70 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
71 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
72 server
.desktop
= server_get_current_desktop ();
74 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
) ;
76 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
78 imlib_context_set_display (server
.dsp
);
79 imlib_context_set_visual (server
.visual
);
80 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
83 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
85 setlocale (LC_ALL
, "");
93 if (time1_font_desc
) pango_font_description_free(time1_font_desc
);
94 if (time2_font_desc
) pango_font_description_free(time2_font_desc
);
95 if (time1_format
) g_free(time1_format
);
96 if (time2_format
) g_free(time2_format
);
98 if (bat1_font_desc
) pango_font_description_free(bat1_font_desc
);
99 if (bat2_font_desc
) pango_font_description_free(bat2_font_desc
);
100 if (battery_low_cmd
) g_free(battery_low_cmd
);
101 if (path_energy_now
) g_free(path_energy_now
);
102 if (path_energy_full
) g_free(path_energy_full
);
103 if (path_current_now
) g_free(path_current_now
);
104 if (path_status
) g_free(path_status
);
106 if (clock_lclick_command
) g_free(clock_lclick_command
);
107 if (clock_rclick_command
) g_free(clock_rclick_command
);
109 if (server
.monitor
) free(server
.monitor
);
110 XFreeGC(server
.dsp
, server
.gc
);
111 XCloseDisplay(server
.dsp
);
115 void window_action (Task
*tsk
, int action
)
119 set_close (tsk
->win
);
122 set_active(tsk
->win
);
125 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
128 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
129 else set_active (tsk
->win
);
132 window_toggle_shade (tsk
->win
);
138 void event_button_press (XEvent
*e
)
140 Panel
*panel
= get_panel(e
->xany
.window
);
144 if ((panel_horizontal
&& (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
)) || (!panel_horizontal
&& (e
->xbutton
.y
< panel
->area
.paddingxlr
|| e
->xbutton
.y
> panel
->area
.height
-panel
->area
.paddingxlr
|| e
->xbutton
.x
< panel
->area
.paddingy
|| e
->xbutton
.x
> panel
->area
.paddingy
+panel
->g_taskbar
.width
))) {
145 // forward the click to the desktop window (thanks conky)
146 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
147 e
->xbutton
.window
= server
.root_win
;
148 // icewm doesn't open under the mouse.
149 // and xfce doesn't open at all.
150 //e->xbutton.x = e->xbutton.x_root;
151 //e->xbutton.y = e->xbutton.y_root;
152 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
153 XSetInputFocus(server
.dsp
, e
->xbutton
.window
, RevertToParent
, e
->xbutton
.time
);
154 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
159 if (e
->xbutton
.button
!= 1) return;
161 if (panel_mode
!= MULTI_DESKTOP
) {
162 // drag and drop disabled
163 XLowerWindow (server
.dsp
, panel
->main_win
);
169 if (panel_horizontal
) {
170 int x
= e
->xbutton
.x
;
171 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
173 if (!tskbar
->area
.on_screen
) continue;
174 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
179 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
181 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
189 int y
= e
->xbutton
.y
;
190 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
192 if (!tskbar
->area
.on_screen
) continue;
193 if (y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
198 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
200 if (y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
208 XLowerWindow (server
.dsp
, panel
->main_win
);
212 void event_button_release (XEvent
*e
)
214 Panel
*panel
= get_panel(e
->xany
.window
);
218 if ((panel_horizontal
&& (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
)) || (!panel_horizontal
&& (e
->xbutton
.y
< panel
->area
.paddingxlr
|| e
->xbutton
.y
> panel
->area
.height
-panel
->area
.paddingxlr
|| e
->xbutton
.x
< panel
->area
.paddingy
|| e
->xbutton
.x
> panel
->area
.paddingy
+panel
->g_taskbar
.width
))) {
219 // forward the click to the desktop window (thanks conky)
220 e
->xbutton
.window
= server
.root_win
;
221 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonReleaseMask
, e
);
226 int action
= TOGGLE_ICONIFY
;
227 int x
= e
->xbutton
.x
;
228 int y
= e
->xbutton
.y
;
229 switch (e
->xbutton
.button
) {
231 action
= mouse_middle
;
234 action
= mouse_right
;
237 action
= mouse_scroll_up
;
240 action
= mouse_scroll_down
;
247 Clock clk
= panel
->clock
;
248 if (panel_horizontal
) {
249 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
250 clock_action(e
->xbutton
.button
);
252 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
254 if (!tskbar
->area
.on_screen
) continue;
255 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
261 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
262 clock_action(e
->xbutton
.button
);
264 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
266 if (!tskbar
->area
.on_screen
) continue;
267 if (y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
273 // TODO: check better solution to keep window below
274 XLowerWindow (server
.dsp
, panel
->main_win
);
279 // drag and drop task
281 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
282 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
283 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
284 if (tskbar
->desktop
== server
.desktop
)
285 set_active(task_drag
->win
);
294 if (panel_mode
== MULTI_DESKTOP
) {
295 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
)
296 set_desktop (tskbar
->desktop
);
302 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
304 if (panel_horizontal
) {
305 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
306 window_action (tsk
, action
);
311 if (y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
312 window_action (tsk
, action
);
318 // to keep window below
319 XLowerWindow (server
.dsp
, panel
->main_win
);
323 void event_property_notify (XEvent
*e
)
327 Window win
= e
->xproperty
.window
;
328 Atom at
= e
->xproperty
.atom
;
330 if (win
== server
.root_win
) {
331 if (!server
.got_root_win
) {
332 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
333 server
.got_root_win
= 1;
336 // Change number of desktops
337 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
338 server
.nb_desktop
= server_get_number_of_desktop ();
342 for (i
=0 ; i
< nb_panel
; i
++) {
343 panel1
[i
].area
.resize
= 1;
345 task_refresh_tasklist();
349 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
350 server
.desktop
= server_get_current_desktop ();
351 if (panel_mode
!= MULTI_DESKTOP
) {
356 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
357 task_refresh_tasklist();
361 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
364 for (i
=0 ; i
< nb_panel
; i
++) {
365 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
366 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
368 tsk
->area
.is_active
= 0;
374 Window w1
= window_get_active ();
375 Task
*t
= task_get_task(w1
);
378 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
379 if (w2
) t
= task_get_task(w2
);
381 if (task_urgent
== t
) {
386 for (i
=0 ; i
< nb_panel
; i
++) {
387 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
388 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
390 if (tsk
->win
== t
->win
) {
391 tsk
->area
.is_active
= 1;
392 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
401 else if (at
== server
.atom
._XROOTPMAP_ID
) {
403 for (i
=0 ; i
< nb_panel
; i
++) {
404 set_panel_background(&panel1
[i
]);
410 tsk
= task_get_task (win
);
412 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
414 // Window title changed
415 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
419 // changed other tsk->title
420 for (i
=0 ; i
< nb_panel
; i
++) {
421 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
422 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
424 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
425 tsk2
->title
= tsk
->title
;
426 tsk2
->area
.redraw
= 1;
434 else if (at
== server
.atom
._NET_WM_STATE
) {
435 if (window_is_urgent (win
)) {
441 else if (at
== server
.atom
.WM_STATE
) {
443 // TODO : try to delete following code
444 if (window_is_iconified (win
)) {
446 if (task_active
->win
== tsk
->win
) {
449 for (i
=0 ; i
< nb_panel
; i
++) {
450 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
451 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
453 tsk2
->area
.is_active
= 0;
462 // Window icon changed
463 else if (at
== server
.atom
._NET_WM_ICON
) {
467 for (i
=0 ; i
< nb_panel
; i
++) {
468 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
469 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
471 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
472 tsk2
->icon_width
= tsk
->icon_width
;
473 tsk2
->icon_height
= tsk
->icon_height
;
474 tsk2
->icon_data
= tsk
->icon_data
;
475 tsk2
->area
.redraw
= 1;
482 // Window desktop changed
483 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
484 int desktop
= window_get_desktop (win
);
485 int active
= tsk
->area
.is_active
;
486 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
487 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
488 if (desktop
!= tsk
->desktop
) {
490 tsk
= add_task (win
);
492 tsk
->area
.is_active
= 1;
499 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
504 void event_expose (XEvent
*e
)
508 panel
= get_panel(e
->xany
.window
);
510 // TODO : one panel_refresh per panel ?
515 void event_configure_notify (Window win
)
517 // check 'win' move in systray
520 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
521 traywin
= (TrayWindow
*)l
->data
;
522 if (traywin
->id
== win
) {
523 //printf("move tray %d\n", traywin->x);
524 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
530 // check 'win' move in another monitor
531 if (nb_panel
== 1) return;
532 if (server
.nb_monitor
== 1) return;
533 Task
*tsk
= task_get_task (win
);
536 Panel
*p
= tsk
->area
.panel
;
537 if (p
->monitor
!= window_get_monitor (win
)) {
540 if (win
== window_get_active ()) {
541 Task
*tsk
= task_get_task (win
);
542 tsk
->area
.is_active
= 1;
555 if (gettimeofday(&stv
, 0)) return;
557 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
558 time_clock
.tv_sec
= stv
.tv_sec
;
559 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
563 if (tick_urgent
< max_tick_urgent
) {
564 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
565 task_urgent
->area
.redraw
= 1;
571 #ifdef ENABLE_BATTERY
572 if (panel1
[0].battery
.area
.on_screen
) {
574 for (i
=0 ; i
< nb_panel
; i
++)
575 panel1
[i
].battery
.area
.resize
= 1;
581 for (i
=0 ; i
< nb_panel
; i
++)
582 panel1
[i
].clock
.area
.resize
= 1;
588 int main (int argc
, char *argv
[])
601 c
= getopt (argc
, argv
, "c:");
604 i
= config_read_file (optarg
);
605 c
= getopt (argc
, argv
, "j:");
607 // usage: tint2 [-c] <config_file> -j <file> for internal use
608 printf("file %s\n", optarg
);
616 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
622 x11_fd
= ConnectionNumber(server
.dsp
);
623 XSync(server
.dsp
, False
);
626 // thanks to AngryLlama for the timer
627 // Create a File Description Set containing x11_fd
629 FD_SET (x11_fd
, &fd
);
634 // Wait for X Event or a Timer
635 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
636 while (XPending (server
.dsp
)) {
637 XNextEvent(server
.dsp
, &e
);
641 event_button_press (&e
);
645 event_button_release(&e
);
653 event_property_notify(&e
);
656 case ConfigureNotify
:
657 if (e
.xconfigure
.window
== server
.root_win
)
660 event_configure_notify (e
.xconfigure
.window
);
664 if (!systray
.area
.on_screen
)
666 panel
= (Panel
*)systray
.area
.panel
;
667 if (e
.xany
.window
== panel
->main_win
) // reparented to us
669 // FIXME: 'reparent to us' badly detected => disabled
673 if (!systray
.area
.on_screen
)
675 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
676 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
677 remove_icon((TrayWindow
*)it
->data
);
684 if (!systray
.area
.on_screen
) break;
685 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
686 net_message(&e
.xclient
);
694 switch (signal_pending
) {
708 if (refresh_systray
) {
709 panel
= (Panel
*)systray
.area
.panel
;
710 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
712 for (i
=0 ; i
< nb_panel
; i
++) {
715 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
716 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
718 refresh(&panel
->area
);
719 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
723 if (refresh_systray
) {
725 panel
= (Panel
*)systray
.area
.panel
;
726 // tint2 doen't draw systray icons. it just redraw background.
727 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
728 // force icon's refresh
729 refresh_systray_icon();
This page took 0.066438 seconds and 4 git commands to generate.