]>
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(SIGCHLD
, 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 Taskbar
*click_taskbar (Panel
*panel
, XEvent
*e
)
118 Taskbar
*tskbar
= NULL
;
119 if (panel_horizontal
) {
120 int x
= e
->xbutton
.x
;
121 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
123 if (!tskbar
->area
.on_screen
) continue;
124 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
129 int y
= e
->xbutton
.y
;
130 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
132 if (!tskbar
->area
.on_screen
) continue;
133 if (y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
141 Task
*click_task (Panel
*panel
, XEvent
*e
)
146 if ( (tskbar
= click_taskbar(panel
, e
)) ) {
147 if (panel_horizontal
) {
148 int x
= e
->xbutton
.x
;
150 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
152 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
158 int y
= e
->xbutton
.y
;
160 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
162 if (y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
172 int click_padding(Panel
*panel
, XEvent
*e
)
174 if (panel_horizontal
) {
175 if (e
->xbutton
.x
< panel
->area
.paddingxlr
|| e
->xbutton
.x
> panel
->area
.width
-panel
->area
.paddingxlr
)
179 if (e
->xbutton
.y
< panel
->area
.paddingxlr
|| e
->xbutton
.y
> panel
->area
.height
-panel
->area
.paddingxlr
)
186 int click_clock(Panel
*panel
, XEvent
*e
)
188 Clock clk
= panel
->clock
;
189 if (panel_horizontal
) {
190 if (clk
.area
.on_screen
&& e
->xbutton
.x
>= clk
.area
.posx
&& e
->xbutton
.x
<= (clk
.area
.posx
+ clk
.area
.width
))
193 if (clk
.area
.on_screen
&& e
->xbutton
.y
>= clk
.area
.posy
&& e
->xbutton
.y
<= (clk
.area
.posy
+ clk
.area
.height
))
200 void window_action (Task
*tsk
, int action
)
205 set_close (tsk
->win
);
208 set_active(tsk
->win
);
211 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
214 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
215 else set_active (tsk
->win
);
218 window_toggle_shade (tsk
->win
);
220 case MAXIMIZE_RESTORE
:
221 window_maximize_restore (tsk
->win
);
224 window_maximize_restore (tsk
->win
);
227 window_maximize_restore (tsk
->win
);
233 void event_button_press (XEvent
*e
)
235 Panel
*panel
= get_panel(e
->xany
.window
);
239 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))) {
240 // forward the click to the desktop window (thanks conky)
241 XUngrabPointer(server.dsp, e->xbutton.time);
242 e->xbutton.window = server.root_win;
243 // icewm doesn't open under the mouse.
244 // and xfce doesn't open at all.
245 //e->xbutton.x = e->xbutton.x_root;
246 //e->xbutton.y = e->xbutton.y_root;
247 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
248 XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
249 XSendEvent(server.dsp, e->xbutton.window, False, ButtonPressMask, e);
254 if (panel_mode
== MULTI_DESKTOP
)
255 task_drag
= click_task(panel
, e
);
257 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
) && (e
->xbutton
.button
!= 1) ) {
258 // forward the click to the desktop window (thanks conky)
259 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
260 e
->xbutton
.window
= server
.root_win
;
261 // icewm doesn't open under the mouse.
262 // and xfce doesn't open at all.
263 //e->xbutton.x = e->xbutton.x_root;
264 //e->xbutton.y = e->xbutton.y_root;
265 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
266 XSetInputFocus(server
.dsp
, e
->xbutton
.window
, RevertToParent
, e
->xbutton
.time
);
267 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
271 if (e->xbutton.button != 1) return;
274 if (panel_horizontal) {
275 int x = e->xbutton.x;
276 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
278 if (!tskbar->area.on_screen) continue;
279 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
284 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
286 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
294 int y = e->xbutton.y;
295 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
297 if (!tskbar->area.on_screen) continue;
298 if (y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
303 for (l0 = tskbar->area.list; l0 ; l0 = l0->next) {
305 if (y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
313 XLowerWindow (server
.dsp
, panel
->main_win
);
317 void event_button_release (XEvent
*e
)
319 Panel
*panel
= get_panel(e
->xany
.window
);
322 if (wm_menu
&& click_padding(panel
, e
)) {
323 // forward the click to the desktop window (thanks conky)
324 e
->xbutton
.window
= server
.root_win
;
325 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonReleaseMask
, e
);
330 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))) {
331 // forward the click to the desktop window (thanks conky)
332 e->xbutton.window = server.root_win;
333 XSendEvent(server.dsp, e->xbutton.window, False, ButtonReleaseMask, e);
339 int action
= TOGGLE_ICONIFY
;
340 switch (e
->xbutton
.button
) {
342 action
= mouse_middle
;
345 action
= mouse_right
;
348 action
= mouse_scroll_up
;
351 action
= mouse_scroll_down
;
354 action
= mouse_tilt_left
;
357 action
= mouse_tilt_right
;
361 if ( click_clock(panel
, e
)) {
362 clock_action(e
->xbutton
.button
);
363 XLowerWindow (server
.dsp
, panel
->main_win
);
369 if ( !(tskbar
= click_taskbar(panel
, e
)) ) {
370 // TODO: check better solution to keep window below
371 XLowerWindow (server
.dsp
, panel
->main_win
);
376 // drag and drop task
378 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
379 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
380 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
381 if (tskbar
->desktop
== server
.desktop
)
382 set_active(task_drag
->win
);
391 if (panel_mode
== MULTI_DESKTOP
&& action
!= mouse_tilt_left
&& action
!= mouse_tilt_right
) {
392 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
)
393 set_desktop (tskbar
->desktop
);
397 window_action( click_task(panel
, e
), action
);
399 // to keep window below
400 XLowerWindow (server
.dsp
, panel
->main_win
);
402 int x = e->xbutton.x;
403 int y = e->xbutton.y;
407 Clock clk = panel->clock;
408 if (panel_horizontal) {
409 if (clk.area.on_screen && x >= clk.area.posx && x <= (clk.area.posx + clk.area.width))
410 clock_action(e->xbutton.button);
412 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
414 if (!tskbar->area.on_screen) continue;
415 if (x >= tskbar->area.posx && x <= (tskbar->area.posx + tskbar->area.width))
421 if (clk.area.on_screen && y >= clk.area.posy && y <= (clk.area.posy + clk.area.height))
422 clock_action(e->xbutton.button);
424 for (l0 = panel->area.list; l0 ; l0 = l0->next) {
426 if (!tskbar->area.on_screen) continue;
427 if (y >= tskbar->area.posy && y <= (tskbar->area.posy + tskbar->area.height))
433 // TODO: check better solution to keep window below
434 XLowerWindow (server.dsp, panel->main_win);
439 // drag and drop task
441 if (tskbar != task_drag->area.parent && action == TOGGLE_ICONIFY) {
442 if (task_drag->desktop != ALLDESKTOP && panel_mode == MULTI_DESKTOP) {
443 windows_set_desktop(task_drag->win, tskbar->desktop);
444 if (tskbar->desktop == server.desktop)
445 set_active(task_drag->win);
454 if (panel_mode == MULTI_DESKTOP) {
455 if (tskbar->desktop != server.desktop && action != CLOSE)
456 set_desktop (tskbar->desktop);
462 for (l = tskbar->area.list ; l ; l = l->next) {
464 if (panel_horizontal) {
465 if (x >= tsk->area.posx && x <= (tsk->area.posx + tsk->area.width)) {
466 window_action (tsk, action);
471 if (y >= tsk->area.posy && y <= (tsk->area.posy + tsk->area.height)) {
472 window_action (tsk, action);
478 // to keep window below
479 XLowerWindow (server.dsp, panel->main_win);
484 void event_property_notify (XEvent
*e
)
488 Window win
= e
->xproperty
.window
;
489 Atom at
= e
->xproperty
.atom
;
491 if (win
== server
.root_win
) {
492 if (!server
.got_root_win
) {
493 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
494 server
.got_root_win
= 1;
497 // Change number of desktops
498 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
499 server
.nb_desktop
= server_get_number_of_desktop ();
503 for (i
=0 ; i
< nb_panel
; i
++) {
504 panel1
[i
].area
.resize
= 1;
506 task_refresh_tasklist();
510 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
511 server
.desktop
= server_get_current_desktop ();
512 if (panel_mode
!= MULTI_DESKTOP
) {
517 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
518 task_refresh_tasklist();
522 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
525 for (i
=0 ; i
< nb_panel
; i
++) {
526 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
527 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
529 tsk
->area
.is_active
= 0;
535 Window w1
= window_get_active ();
536 Task
*t
= task_get_task(w1
);
539 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
540 if (w2
) t
= task_get_task(w2
);
542 if (task_urgent
== t
) {
546 // put active state on all task (multi_desktop)
548 for (i
=0 ; i
< nb_panel
; i
++) {
549 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
550 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
552 if (tsk
->win
== t
->win
) {
553 tsk
->area
.is_active
= 1;
554 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
563 else if (at
== server
.atom
._XROOTPMAP_ID
) {
565 for (i
=0 ; i
< nb_panel
; i
++) {
566 set_panel_background(&panel1
[i
]);
572 tsk
= task_get_task (win
);
574 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
576 // Window title changed
577 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
581 // changed other tsk->title
582 for (i
=0 ; i
< nb_panel
; i
++) {
583 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
584 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
586 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
587 tsk2
->title
= tsk
->title
;
588 tsk2
->area
.redraw
= 1;
596 else if (at
== server
.atom
._NET_WM_STATE
) {
597 if (window_is_urgent (win
)) {
603 else if (at
== server
.atom
.WM_STATE
) {
605 // TODO : try to delete following code
606 if (window_is_iconified (win
)) {
608 if (task_active
->win
== tsk
->win
) {
611 for (i
=0 ; i
< nb_panel
; i
++) {
612 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
613 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
615 tsk2
->area
.is_active
= 0;
624 // Window icon changed
625 else if (at
== server
.atom
._NET_WM_ICON
) {
629 for (i
=0 ; i
< nb_panel
; i
++) {
630 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
631 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
633 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
634 tsk2
->icon_width
= tsk
->icon_width
;
635 tsk2
->icon_height
= tsk
->icon_height
;
636 tsk2
->icon_data
= tsk
->icon_data
;
637 tsk2
->icon_data_active
= tsk
->icon_data_active
;
638 tsk2
->area
.redraw
= 1;
645 // Window desktop changed
646 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
647 int desktop
= window_get_desktop (win
);
648 int active
= tsk
->area
.is_active
;
649 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
650 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
651 if (desktop
!= tsk
->desktop
) {
653 tsk
= add_task (win
);
655 tsk
->area
.is_active
= 1;
662 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
667 void event_expose (XEvent
*e
)
671 panel
= get_panel(e
->xany
.window
);
673 // TODO : one panel_refresh per panel ?
678 void event_configure_notify (Window win
)
680 // check 'win' move in systray
683 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
684 traywin
= (TrayWindow
*)l
->data
;
685 if (traywin
->id
== win
) {
686 //printf("move tray %d\n", traywin->x);
687 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
693 // check 'win' move in another monitor
694 if (nb_panel
== 1) return;
695 if (server
.nb_monitor
== 1) return;
696 Task
*tsk
= task_get_task (win
);
699 Panel
*p
= tsk
->area
.panel
;
700 if (p
->monitor
!= window_get_monitor (win
)) {
703 if (win
== window_get_active ()) {
704 Task
*tsk
= task_get_task (win
);
705 tsk
->area
.is_active
= 1;
718 if (gettimeofday(&stv
, 0)) return;
720 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
721 time_clock
.tv_sec
= stv
.tv_sec
;
722 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
726 if (tick_urgent
< max_tick_urgent
) {
727 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
728 task_urgent
->area
.redraw
= 1;
734 #ifdef ENABLE_BATTERY
735 if (panel1
[0].battery
.area
.on_screen
) {
737 for (i
=0 ; i
< nb_panel
; i
++)
738 panel1
[i
].battery
.area
.resize
= 1;
744 for (i
=0 ; i
< nb_panel
; i
++)
745 panel1
[i
].clock
.area
.resize
= 1;
751 int main (int argc
, char *argv
[])
764 c
= getopt (argc
, argv
, "c:");
767 i
= config_read_file (optarg
);
768 c
= getopt (argc
, argv
, "j:");
770 // usage: tint2 [-c] <config_file> -j <file> for internal use
771 printf("file %s\n", optarg
);
779 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
785 x11_fd
= ConnectionNumber(server
.dsp
);
786 XSync(server
.dsp
, False
);
789 // thanks to AngryLlama for the timer
790 // Create a File Description Set containing x11_fd
792 FD_SET (x11_fd
, &fd
);
797 // Wait for X Event or a Timer
798 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
799 while (XPending (server
.dsp
)) {
800 XNextEvent(server
.dsp
, &e
);
804 event_button_press (&e
);
808 event_button_release(&e
);
816 event_property_notify(&e
);
819 case ConfigureNotify
:
820 if (e
.xconfigure
.window
== server
.root_win
)
823 event_configure_notify (e
.xconfigure
.window
);
827 if (!systray
.area
.on_screen
)
829 panel
= (Panel
*)systray
.area
.panel
;
830 if (e
.xany
.window
== panel
->main_win
) // reparented to us
832 // FIXME: 'reparent to us' badly detected => disabled
836 if (!systray
.area
.on_screen
)
838 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
839 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
840 remove_icon((TrayWindow
*)it
->data
);
847 if (!systray
.area
.on_screen
) break;
848 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
849 net_message(&e
.xclient
);
857 switch (signal_pending
) {
871 if (refresh_systray
) {
872 panel
= (Panel
*)systray
.area
.panel
;
873 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
875 for (i
=0 ; i
< nb_panel
; i
++) {
878 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
879 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
881 refresh(&panel
->area
);
882 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
886 if (refresh_systray
) {
888 panel
= (Panel
*)systray
.area
.panel
;
889 // tint2 doen't draw systray icons. it just redraw background.
890 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
891 // force icon's refresh
892 refresh_systray_icon();
This page took 0.078967 seconds and 5 git commands to generate.