]>
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"
43 void signal_handler(int sig
)
45 // signal handler is light as it should be
50 void init (int argc
, char *argv
[])
55 for (i
= 1; i
< argc
; ++i
) {
56 if (!strcmp(argv
[i
], "-h") || !strcmp(argv
[i
], "--help")) {
57 printf("Usage: tint2 [-c] <config_file>\n");
60 if (!strcmp(argv
[i
], "-v") || !strcmp(argv
[i
], "--version")) {
61 printf("tint2 version 0.7.svn\n");
64 if (!strcmp(argv
[i
], "-c")) {
67 config_path
= strdup(argv
[i
]);
69 if (!strcmp(argv
[i
], "-s")) {
72 snapshot_path
= strdup(argv
[i
]);
77 struct sigaction sa
= { .sa_handler
= signal_handler
};
78 sigaction(SIGUSR1
, &sa
, 0);
79 sigaction(SIGINT
, &sa
, 0);
80 sigaction(SIGTERM
, &sa
, 0);
81 sigaction(SIGHUP
, &sa
, 0);
82 signal(SIGCHLD
, SIG_IGN
); // don't have to wait() after fork()
83 signal(SIGALRM
, tooltip_sighandler
);
84 // block all signals, such that no race conditions occur before pselect in our main loop
86 sigaddset(&block_mask
, SIGINT
);
87 sigaddset(&block_mask
, SIGTERM
);
88 sigaddset(&block_mask
, SIGHUP
);
89 sigaddset(&block_mask
, SIGUSR1
);
90 sigprocmask(SIG_BLOCK
, &block_mask
, 0);
94 memset(&server
, 0, sizeof(Server_global
));
95 memset(&systray
, 0, sizeof(Systraybar
));
97 server
.dsp
= XOpenDisplay (NULL
);
99 fprintf(stderr
, "tint2 exit : could not open display.\n");
102 server_init_atoms ();
103 server
.screen
= DefaultScreen (server
.dsp
);
104 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
105 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
106 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
107 server
.desktop
= server_get_current_desktop ();
109 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
);
111 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
113 imlib_context_set_display (server
.dsp
);
114 imlib_context_set_visual (server
.visual
);
115 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
118 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
120 setlocale (LC_ALL
, "");
124 const gchar
* const *data_dirs
;
125 data_dirs
= g_get_system_data_dirs ();
126 for (i
= 0; data_dirs
[i
] != NULL
; i
++) {
127 path
= g_build_filename(data_dirs
[i
], "tint2", "default_icon.png", NULL
);
128 if (g_file_test (path
, G_FILE_TEST_EXISTS
))
129 default_icon
= imlib_load_image(path
);
133 // get monitor and desktop config
146 #ifdef ENABLE_BATTERY
151 imlib_context_set_image(default_icon
);
154 if (config_path
) g_free(config_path
);
155 if (snapshot_path
) g_free(snapshot_path
);
157 if (server
.monitor
) free(server
.monitor
);
158 XFreeGC(server
.dsp
, server
.gc
);
159 XCloseDisplay(server
.dsp
);
163 void get_snapshot(const char *path
)
165 Panel
*panel
= &panel1
[0];
167 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
168 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
170 refresh(&panel
->area
);
172 Imlib_Image img
= NULL
;
173 imlib_context_set_drawable(panel
->temp_pmap
);
174 img
= imlib_create_image_from_drawable(0, 0, 0, panel
->area
.width
, panel
->area
.height
, 0);
176 imlib_context_set_image(img
);
177 imlib_save_image(path
);
182 Taskbar
*click_taskbar (Panel
*panel
, int x
, int y
)
187 if (panel_horizontal
) {
188 for (i
=0; i
< panel
->nb_desktop
; i
++) {
189 tskbar
= &panel
->taskbar
[i
];
190 if (tskbar
->area
.on_screen
&& x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
195 for (i
=0; i
< panel
->nb_desktop
; i
++) {
196 tskbar
= &panel
->taskbar
[i
];
197 if (tskbar
->area
.on_screen
&& y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
205 Task
*click_task (Panel
*panel
, int x
, int y
)
210 if ( (tskbar
= click_taskbar(panel
, x
, y
)) ) {
211 if (panel_horizontal
) {
213 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
215 if (tsk
->area
.on_screen
&& x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
222 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
224 if (tsk
->area
.on_screen
&& y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
234 int click_padding(Panel
*panel
, int x
, int y
)
236 if (panel_horizontal
) {
237 if (x
< panel
->area
.paddingxlr
|| x
> panel
->area
.width
-panel
->area
.paddingxlr
)
241 if (y
< panel
->area
.paddingxlr
|| y
> panel
->area
.height
-panel
->area
.paddingxlr
)
248 int click_clock(Panel
*panel
, int x
, int y
)
250 Clock clk
= panel
->clock
;
251 if (panel_horizontal
) {
252 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
255 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
262 void window_action (Task
*tsk
, int action
)
268 set_close (tsk
->win
);
271 set_active(tsk
->win
);
274 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
277 if (task_active
&& tsk
->win
== task_active
->win
)
278 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
280 set_active (tsk
->win
);
283 window_toggle_shade (tsk
->win
);
285 case MAXIMIZE_RESTORE
:
286 window_maximize_restore (tsk
->win
);
289 window_maximize_restore (tsk
->win
);
292 window_maximize_restore (tsk
->win
);
295 if ( tsk
->desktop
== 0 ) break;
296 desk
= tsk
->desktop
- 1;
297 windows_set_desktop(tsk
->win
, desk
);
298 if (desk
== server
.desktop
)
299 set_active(tsk
->win
);
302 if (tsk
->desktop
== server
.nb_desktop
) break;
303 desk
= tsk
->desktop
+ 1;
304 windows_set_desktop(tsk
->win
, desk
);
305 if (desk
== server
.desktop
)
306 set_active(tsk
->win
);
311 void event_button_press (XEvent
*e
)
313 Panel
*panel
= get_panel(e
->xany
.window
);
316 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
318 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
) && (e
->xbutton
.button
!= 1) ) {
319 // forward the click to the desktop window (thanks conky)
320 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
321 e
->xbutton
.window
= server
.root_win
;
322 // icewm doesn't open under the mouse.
323 // and xfce doesn't open at all.
324 e
->xbutton
.x
= e
->xbutton
.x_root
;
325 e
->xbutton
.y
= e
->xbutton
.y_root
;
326 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
327 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
328 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
332 XLowerWindow (server
.dsp
, panel
->main_win
);
336 void event_button_release (XEvent
*e
)
338 Panel
*panel
= get_panel(e
->xany
.window
);
341 int action
= TOGGLE_ICONIFY
;
342 switch (e
->xbutton
.button
) {
344 action
= mouse_middle
;
347 action
= mouse_right
;
350 action
= mouse_scroll_up
;
353 action
= mouse_scroll_down
;
356 action
= mouse_tilt_left
;
359 action
= mouse_tilt_right
;
363 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
364 clock_action(e
->xbutton
.button
);
365 XLowerWindow (server
.dsp
, panel
->main_win
);
371 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
372 // TODO: check better solution to keep window below
373 XLowerWindow (server
.dsp
, panel
->main_win
);
378 // drag and drop task
380 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
381 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
382 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
383 if (tskbar
->desktop
== server
.desktop
)
384 set_active(task_drag
->win
);
393 if (panel_mode
== MULTI_DESKTOP
) {
394 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
395 set_desktop (tskbar
->desktop
);
399 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
401 // to keep window below
402 XLowerWindow (server
.dsp
, panel
->main_win
);
406 void event_property_notify (XEvent
*e
)
410 Window win
= e
->xproperty
.window
;
411 Atom at
= e
->xproperty
.atom
;
413 if (win
== server
.root_win
) {
414 if (!server
.got_root_win
) {
415 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
416 server
.got_root_win
= 1;
419 // Change number of desktops
420 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
421 server
.nb_desktop
= server_get_number_of_desktop ();
425 for (i
=0 ; i
< nb_panel
; i
++) {
426 panel1
[i
].area
.resize
= 1;
428 task_refresh_tasklist();
432 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
433 int old_desktop
= server
.desktop
;
434 server
.desktop
= server_get_current_desktop ();
435 for (i
=0 ; i
< nb_panel
; i
++) {
436 Panel
*panel
= &panel1
[i
];
437 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
438 // redraw both taskbar
439 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
440 panel
->taskbar
[old_desktop
].area
.resize
= 1;
441 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
442 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
445 // check ALLDESKTOP task => resize taskbar
449 tskbar
= &panel
->taskbar
[old_desktop
];
450 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
452 if (tsk
->desktop
== ALLDESKTOP
) {
453 tsk
->area
.on_screen
= 0;
454 tskbar
->area
.resize
= 1;
458 tskbar
= &panel
->taskbar
[server
.desktop
];
459 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
461 if (tsk
->desktop
== ALLDESKTOP
) {
462 tsk
->area
.on_screen
= 1;
463 tskbar
->area
.resize
= 1;
467 if (panel_mode
!= MULTI_DESKTOP
) {
472 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
473 task_refresh_tasklist();
477 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
481 else if (at
== server
.atom
._XROOTPMAP_ID
) {
483 for (i
=0 ; i
< nb_panel
; i
++) {
484 set_panel_background(&panel1
[i
]);
490 tsk
= task_get_task (win
);
492 if (at
!= server
.atom
._NET_WM_STATE
)
495 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
496 // if it is mapped and not set as skip_taskbar, we must add it to our task list
497 XWindowAttributes wa
;
498 XGetWindowAttributes(server
.dsp
, win
, &wa
);
499 if (wa
.map_state
== IsViewable
&& !window_is_skip_taskbar(win
)) {
500 if ( (tsk
= add_task(win
)) )
507 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
509 // Window title changed
510 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
514 // changed other tsk->title
515 for (i
=0 ; i
< nb_panel
; i
++) {
516 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
517 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
519 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
520 tsk2
->title
= tsk
->title
;
521 tsk2
->area
.redraw
= 1;
529 else if (at
== server
.atom
._NET_WM_STATE
) {
530 if (window_is_urgent (win
)) {
533 if (window_is_skip_taskbar(win
)) {
538 // We do not check for the iconified state, since it only unsets our active window
539 // but in openbox a shaded window is considered iconified. So we would loose the active window
540 // property on unshading it again (commented 01.10.2009)
541 // else if (at == server.atom.WM_STATE) {
543 // // TODO : try to delete following code
544 // if (window_is_iconified (win)) {
545 // if (task_active) {
546 // if (task_active->win == tsk->win) {
549 // for (i=0 ; i < nb_panel ; i++) {
550 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
551 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
553 // tsk2->area.is_active = 0;
562 // Window icon changed
563 else if (at
== server
.atom
._NET_WM_ICON
) {
567 for (i
=0 ; i
< nb_panel
; i
++) {
568 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
569 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
571 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
572 tsk2
->icon_width
= tsk
->icon_width
;
573 tsk2
->icon_height
= tsk
->icon_height
;
574 tsk2
->icon
= tsk
->icon
;
575 tsk2
->icon_active
= tsk
->icon_active
;
576 tsk2
->area
.redraw
= 1;
583 // Window desktop changed
584 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
585 int desktop
= window_get_desktop (win
);
586 int active
= tsk
->area
.is_active
;
587 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
588 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
589 if (desktop
!= tsk
->desktop
) {
591 tsk
= add_task (win
);
593 tsk
->area
.is_active
= 1;
599 else if (at
== server
.atom
.WM_HINTS
) {
600 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
601 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
607 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
612 void event_expose (XEvent
*e
)
614 if (e
->xany
.window
== g_tooltip
.window
)
618 panel
= get_panel(e
->xany
.window
);
620 // TODO : one panel_refresh per panel ?
626 void event_configure_notify (Window win
)
628 // change in root window (xrandr)
629 if (win
== server
.root_win
) {
632 config_read_file (config_path
);
638 // 'win' is a trayer icon
641 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
642 traywin
= (TrayWindow
*)l
->data
;
643 if (traywin
->id
== win
) {
644 //printf("move tray %d\n", traywin->x);
645 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
651 // 'win' move in another monitor
652 if (nb_panel
== 1) return;
653 Task
*tsk
= task_get_task (win
);
656 Panel
*p
= tsk
->area
.panel
;
657 if (p
->monitor
!= window_get_monitor (win
)) {
660 if (win
== window_get_active ()) {
661 Task
*tsk
= task_get_task (win
);
662 tsk
->area
.is_active
= 1;
675 void dnd_message(XClientMessageEvent
*e
)
677 Panel
*panel
= get_panel(e
->window
);
678 int x
, y
, mapX
, mapY
;
680 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
681 y
= e
->data
.l
[2] & 0xFFFF;
682 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
683 Task
* task
= click_task(panel
, mapX
, mapY
);
685 if (task
->desktop
!= server
.desktop
)
686 set_desktop (task
->desktop
);
687 window_action(task
, TOGGLE
);
690 // send XdndStatus event to get more XdndPosition events
691 XClientMessageEvent se
;
692 se
.type
= ClientMessage
;
693 se
.window
= e
->data
.l
[0];
694 se
.message_type
= server
.atom
.XdndStatus
;
696 se
.data
.l
[0] = e
->window
; // XID of the target window
697 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
698 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
699 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
700 se
.data
.l
[4] = None
; // None = drop will not be accepted
701 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
705 int main (int argc
, char *argv
[])
720 i
= config_read_file (config_path
);
724 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
731 get_snapshot(snapshot_path
);
736 x11_fd
= ConnectionNumber(server
.dsp
);
737 XSync(server
.dsp
, False
);
740 sigemptyset(&empty_mask
);
743 // thanks to AngryLlama for the timer
744 // Create a File Description Set containing x11_fd, and every timer_fd
746 FD_SET (x11_fd
, &fd_set
);
748 timer_iter
= timer_list
;
750 timer
= timer_iter
->data
;
751 max_fd
= timer
->id
> max_fd
? timer
->id
: max_fd
;
752 FD_SET(timer
->id
, &fd_set
);
753 timer_iter
= timer_iter
->next
;
756 // Wait for X Event or a Timer
757 if (pselect(max_fd
+1, &fd_set
, 0, 0, 0, &empty_mask
) > 0) {
758 while (XPending (server
.dsp
)) {
759 XNextEvent(server
.dsp
, &e
);
764 event_button_press (&e
);
768 event_button_release(&e
);
772 if (!g_tooltip
.enabled
) break;
773 Panel
* panel
= get_panel(e
.xmotion
.window
);
774 Task
* task
= click_task(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
776 tooltip_trigger_show(task
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
778 tooltip_trigger_hide();
783 tooltip_trigger_hide();
791 event_property_notify(&e
);
794 case ConfigureNotify
:
795 event_configure_notify (e
.xconfigure
.window
);
799 if (!systray_enabled
)
801 panel
= (Panel
*)systray
.area
.panel
;
802 if (e
.xany
.window
== panel
->main_win
) // reparented to us
804 // FIXME: 'reparent to us' badly detected => disabled
808 if (!systray
.area
.on_screen
)
810 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
811 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
812 remove_icon((TrayWindow
*)it
->data
);
819 if (!systray
.area
.on_screen
) break;
820 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
821 net_message(&e
.xclient
);
823 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
824 dnd_message(&e
.xclient
);
830 timer_iter
= timer_list
;
832 timer
= timer_iter
->data
;
833 if (FD_ISSET(timer
->id
, &fd_set
)) {
835 read(timer
->id
, &dummy
, sizeof(uint64_t));
838 timer_iter
= timer_iter
->next
;
842 switch (signal_pending
) {
843 case SIGUSR1
: // reload config file
846 config_read_file (config_path
);
860 if (refresh_systray
) {
861 panel
= (Panel
*)systray
.area
.panel
;
862 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
864 for (i
=0 ; i
< nb_panel
; i
++) {
867 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
868 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
870 refresh(&panel
->area
);
871 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
875 if (refresh_systray
) {
877 panel
= (Panel
*)systray
.area
.panel
;
878 // tint2 doen't draw systray icons. it just redraw background.
879 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
880 // force icon's refresh
881 refresh_systray_icon();
This page took 0.071685 seconds and 5 git commands to generate.