]>
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 // block all signals, such that no race conditions occur before pselect in our main loop
85 sigaddset(&block_mask
, SIGINT
);
86 sigaddset(&block_mask
, SIGTERM
);
87 sigaddset(&block_mask
, SIGHUP
);
88 sigaddset(&block_mask
, SIGUSR1
);
89 sigprocmask(SIG_BLOCK
, &block_mask
, 0);
93 memset(&server
, 0, sizeof(Server_global
));
94 memset(&systray
, 0, sizeof(Systraybar
));
96 server
.dsp
= XOpenDisplay (NULL
);
98 fprintf(stderr
, "tint2 exit : could not open display.\n");
101 server_init_atoms ();
102 server
.screen
= DefaultScreen (server
.dsp
);
103 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
104 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
105 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
106 server
.desktop
= server_get_current_desktop ();
108 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
);
110 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
112 imlib_context_set_display (server
.dsp
);
113 imlib_context_set_visual (server
.visual
);
114 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
117 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
119 setlocale (LC_ALL
, "");
123 const gchar
* const *data_dirs
;
124 data_dirs
= g_get_system_data_dirs ();
125 for (i
= 0; data_dirs
[i
] != NULL
; i
++) {
126 path
= g_build_filename(data_dirs
[i
], "tint2", "default_icon.png", NULL
);
127 if (g_file_test (path
, G_FILE_TEST_EXISTS
))
128 default_icon
= imlib_load_image(path
);
132 // get monitor and desktop config
145 #ifdef ENABLE_BATTERY
150 imlib_context_set_image(default_icon
);
153 if (config_path
) g_free(config_path
);
154 if (snapshot_path
) g_free(snapshot_path
);
156 if (server
.monitor
) free(server
.monitor
);
157 XFreeGC(server
.dsp
, server
.gc
);
158 XCloseDisplay(server
.dsp
);
162 void get_snapshot(const char *path
)
164 Panel
*panel
= &panel1
[0];
166 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
167 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
169 refresh(&panel
->area
);
171 Imlib_Image img
= NULL
;
172 imlib_context_set_drawable(panel
->temp_pmap
);
173 img
= imlib_create_image_from_drawable(0, 0, 0, panel
->area
.width
, panel
->area
.height
, 0);
175 imlib_context_set_image(img
);
176 imlib_save_image(path
);
181 Taskbar
*click_taskbar (Panel
*panel
, int x
, int y
)
186 if (panel_horizontal
) {
187 for (i
=0; i
< panel
->nb_desktop
; i
++) {
188 tskbar
= &panel
->taskbar
[i
];
189 if (tskbar
->area
.on_screen
&& x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
194 for (i
=0; i
< panel
->nb_desktop
; i
++) {
195 tskbar
= &panel
->taskbar
[i
];
196 if (tskbar
->area
.on_screen
&& y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
204 Task
*click_task (Panel
*panel
, int x
, int y
)
209 if ( (tskbar
= click_taskbar(panel
, x
, y
)) ) {
210 if (panel_horizontal
) {
212 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
214 if (tsk
->area
.on_screen
&& x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
221 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
223 if (tsk
->area
.on_screen
&& y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
233 int click_padding(Panel
*panel
, int x
, int y
)
235 if (panel_horizontal
) {
236 if (x
< panel
->area
.paddingxlr
|| x
> panel
->area
.width
-panel
->area
.paddingxlr
)
240 if (y
< panel
->area
.paddingxlr
|| y
> panel
->area
.height
-panel
->area
.paddingxlr
)
247 int click_clock(Panel
*panel
, int x
, int y
)
249 Clock clk
= panel
->clock
;
250 if (panel_horizontal
) {
251 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
254 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
261 void window_action (Task
*tsk
, int action
)
267 set_close (tsk
->win
);
270 set_active(tsk
->win
);
273 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
276 if (task_active
&& tsk
->win
== task_active
->win
)
277 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
279 set_active (tsk
->win
);
282 window_toggle_shade (tsk
->win
);
284 case MAXIMIZE_RESTORE
:
285 window_maximize_restore (tsk
->win
);
288 window_maximize_restore (tsk
->win
);
291 window_maximize_restore (tsk
->win
);
294 if ( tsk
->desktop
== 0 ) break;
295 desk
= tsk
->desktop
- 1;
296 windows_set_desktop(tsk
->win
, desk
);
297 if (desk
== server
.desktop
)
298 set_active(tsk
->win
);
301 if (tsk
->desktop
== server
.nb_desktop
) break;
302 desk
= tsk
->desktop
+ 1;
303 windows_set_desktop(tsk
->win
, desk
);
304 if (desk
== server
.desktop
)
305 set_active(tsk
->win
);
310 void event_button_press (XEvent
*e
)
312 Panel
*panel
= get_panel(e
->xany
.window
);
315 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
317 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
) && (e
->xbutton
.button
!= 1) ) {
318 // forward the click to the desktop window (thanks conky)
319 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
320 e
->xbutton
.window
= server
.root_win
;
321 // icewm doesn't open under the mouse.
322 // and xfce doesn't open at all.
323 e
->xbutton
.x
= e
->xbutton
.x_root
;
324 e
->xbutton
.y
= e
->xbutton
.y_root
;
325 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
326 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
327 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
331 XLowerWindow (server
.dsp
, panel
->main_win
);
335 void event_button_release (XEvent
*e
)
337 Panel
*panel
= get_panel(e
->xany
.window
);
340 int action
= TOGGLE_ICONIFY
;
341 switch (e
->xbutton
.button
) {
343 action
= mouse_middle
;
346 action
= mouse_right
;
349 action
= mouse_scroll_up
;
352 action
= mouse_scroll_down
;
355 action
= mouse_tilt_left
;
358 action
= mouse_tilt_right
;
362 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
363 clock_action(e
->xbutton
.button
);
364 XLowerWindow (server
.dsp
, panel
->main_win
);
370 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
371 // TODO: check better solution to keep window below
372 XLowerWindow (server
.dsp
, panel
->main_win
);
377 // drag and drop task
379 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
380 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
381 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
382 if (tskbar
->desktop
== server
.desktop
)
383 set_active(task_drag
->win
);
392 if (panel_mode
== MULTI_DESKTOP
) {
393 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
394 set_desktop (tskbar
->desktop
);
398 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
400 // to keep window below
401 XLowerWindow (server
.dsp
, panel
->main_win
);
405 void event_property_notify (XEvent
*e
)
409 Window win
= e
->xproperty
.window
;
410 Atom at
= e
->xproperty
.atom
;
412 if (win
== server
.root_win
) {
413 if (!server
.got_root_win
) {
414 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
415 server
.got_root_win
= 1;
418 // Change number of desktops
419 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
420 server
.nb_desktop
= server_get_number_of_desktop ();
424 for (i
=0 ; i
< nb_panel
; i
++) {
425 panel1
[i
].area
.resize
= 1;
427 task_refresh_tasklist();
431 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
432 int old_desktop
= server
.desktop
;
433 server
.desktop
= server_get_current_desktop ();
434 for (i
=0 ; i
< nb_panel
; i
++) {
435 Panel
*panel
= &panel1
[i
];
436 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
437 // redraw both taskbar
438 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
439 panel
->taskbar
[old_desktop
].area
.resize
= 1;
440 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
441 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
444 // check ALLDESKTOP task => resize taskbar
448 tskbar
= &panel
->taskbar
[old_desktop
];
449 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
451 if (tsk
->desktop
== ALLDESKTOP
) {
452 tsk
->area
.on_screen
= 0;
453 tskbar
->area
.resize
= 1;
457 tskbar
= &panel
->taskbar
[server
.desktop
];
458 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
460 if (tsk
->desktop
== ALLDESKTOP
) {
461 tsk
->area
.on_screen
= 1;
462 tskbar
->area
.resize
= 1;
466 if (panel_mode
!= MULTI_DESKTOP
) {
471 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
472 task_refresh_tasklist();
476 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
480 else if (at
== server
.atom
._XROOTPMAP_ID
) {
482 for (i
=0 ; i
< nb_panel
; i
++) {
483 set_panel_background(&panel1
[i
]);
489 tsk
= task_get_task (win
);
491 if (at
!= server
.atom
._NET_WM_STATE
)
494 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
495 // if it is mapped and not set as skip_taskbar, we must add it to our task list
496 XWindowAttributes wa
;
497 XGetWindowAttributes(server
.dsp
, win
, &wa
);
498 if (wa
.map_state
== IsViewable
&& !window_is_skip_taskbar(win
)) {
499 if ( (tsk
= add_task(win
)) )
506 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
508 // Window title changed
509 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
513 // changed other tsk->title
514 for (i
=0 ; i
< nb_panel
; i
++) {
515 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
516 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
518 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
519 tsk2
->title
= tsk
->title
;
520 tsk2
->area
.redraw
= 1;
528 else if (at
== server
.atom
._NET_WM_STATE
) {
529 if (window_is_urgent (win
)) {
532 if (window_is_skip_taskbar(win
)) {
537 // We do not check for the iconified state, since it only unsets our active window
538 // but in openbox a shaded window is considered iconified. So we would loose the active window
539 // property on unshading it again (commented 01.10.2009)
540 // else if (at == server.atom.WM_STATE) {
542 // // TODO : try to delete following code
543 // if (window_is_iconified (win)) {
544 // if (task_active) {
545 // if (task_active->win == tsk->win) {
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 // tsk2->area.is_active = 0;
561 // Window icon changed
562 else if (at
== server
.atom
._NET_WM_ICON
) {
566 for (i
=0 ; i
< nb_panel
; i
++) {
567 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
568 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
570 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
571 tsk2
->icon_width
= tsk
->icon_width
;
572 tsk2
->icon_height
= tsk
->icon_height
;
573 tsk2
->icon
= tsk
->icon
;
574 tsk2
->icon_active
= tsk
->icon_active
;
575 tsk2
->area
.redraw
= 1;
582 // Window desktop changed
583 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
584 int desktop
= window_get_desktop (win
);
585 int active
= tsk
->area
.is_active
;
586 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
587 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
588 if (desktop
!= tsk
->desktop
) {
590 tsk
= add_task (win
);
592 tsk
->area
.is_active
= 1;
598 else if (at
== server
.atom
.WM_HINTS
) {
599 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
600 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
606 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
611 void event_expose (XEvent
*e
)
613 if (e
->xany
.window
== g_tooltip
.window
)
617 panel
= get_panel(e
->xany
.window
);
619 // TODO : one panel_refresh per panel ?
625 void event_configure_notify (Window win
)
627 // change in root window (xrandr)
628 if (win
== server
.root_win
) {
631 config_read_file (config_path
);
637 // 'win' is a trayer icon
640 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
641 traywin
= (TrayWindow
*)l
->data
;
642 if (traywin
->id
== win
) {
643 //printf("move tray %d\n", traywin->x);
644 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
650 // 'win' move in another monitor
651 if (nb_panel
== 1) return;
652 Task
*tsk
= task_get_task (win
);
655 Panel
*p
= tsk
->area
.panel
;
656 if (p
->monitor
!= window_get_monitor (win
)) {
659 if (win
== window_get_active ()) {
660 Task
*tsk
= task_get_task (win
);
661 tsk
->area
.is_active
= 1;
674 void dnd_message(XClientMessageEvent
*e
)
676 Panel
*panel
= get_panel(e
->window
);
677 int x
, y
, mapX
, mapY
;
679 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
680 y
= e
->data
.l
[2] & 0xFFFF;
681 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
682 Task
* task
= click_task(panel
, mapX
, mapY
);
684 if (task
->desktop
!= server
.desktop
)
685 set_desktop (task
->desktop
);
686 window_action(task
, TOGGLE
);
689 // send XdndStatus event to get more XdndPosition events
690 XClientMessageEvent se
;
691 se
.type
= ClientMessage
;
692 se
.window
= e
->data
.l
[0];
693 se
.message_type
= server
.atom
.XdndStatus
;
695 se
.data
.l
[0] = e
->window
; // XID of the target window
696 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
697 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
698 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
699 se
.data
.l
[4] = None
; // None = drop will not be accepted
700 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
704 int main (int argc
, char *argv
[])
719 i
= config_read_file (config_path
);
723 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
730 get_snapshot(snapshot_path
);
735 x11_fd
= ConnectionNumber(server
.dsp
);
736 XSync(server
.dsp
, False
);
739 sigemptyset(&empty_mask
);
742 // thanks to AngryLlama for the timer
743 // Create a File Description Set containing x11_fd, and every timer_fd
745 FD_SET (x11_fd
, &fd_set
);
747 timer_iter
= timer_list
;
749 timer
= timer_iter
->data
;
750 max_fd
= timer
->id
> max_fd
? timer
->id
: max_fd
;
751 FD_SET(timer
->id
, &fd_set
);
752 timer_iter
= timer_iter
->next
;
755 // Wait for X Event or a Timer
756 if (pselect(max_fd
+1, &fd_set
, 0, 0, 0, &empty_mask
) > 0) {
757 while (XPending (server
.dsp
)) {
758 XNextEvent(server
.dsp
, &e
);
763 event_button_press (&e
);
767 event_button_release(&e
);
771 if (!g_tooltip
.enabled
) break;
772 Panel
* panel
= get_panel(e
.xmotion
.window
);
773 Task
* task
= click_task(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
775 tooltip_trigger_show(task
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
777 tooltip_trigger_hide();
782 tooltip_trigger_hide();
790 event_property_notify(&e
);
793 case ConfigureNotify
:
794 event_configure_notify (e
.xconfigure
.window
);
798 if (!systray_enabled
)
800 panel
= (Panel
*)systray
.area
.panel
;
801 if (e
.xany
.window
== panel
->main_win
) // reparented to us
803 // FIXME: 'reparent to us' badly detected => disabled
807 if (!systray
.area
.on_screen
)
809 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
810 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
811 remove_icon((TrayWindow
*)it
->data
);
818 if (!systray
.area
.on_screen
) break;
819 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
820 net_message(&e
.xclient
);
822 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
823 dnd_message(&e
.xclient
);
829 // we need to iterate over the whole timer list, since fd_set can only be checked with the
830 // brute force method FD_ISSET for every possible timer
831 timer_iter
= timer_list
;
833 timer
= timer_iter
->data
;
834 if (FD_ISSET(timer
->id
, &fd_set
)) {
836 read(timer
->id
, &dummy
, sizeof(uint64_t));
839 timer_iter
= timer_iter
->next
;
843 switch (signal_pending
) {
844 case SIGUSR1
: // reload config file
847 config_read_file (config_path
);
861 if (refresh_systray
) {
862 panel
= (Panel
*)systray
.area
.panel
;
863 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
865 for (i
=0 ; i
< nb_panel
; i
++) {
868 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
869 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
871 refresh(&panel
->area
);
872 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
876 if (refresh_systray
) {
878 panel
= (Panel
*)systray
.area
.panel
;
879 // tint2 doen't draw systray icons. it just redraw background.
880 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
881 // force icon's refresh
882 refresh_systray_icon();
This page took 0.072343 seconds and 5 git commands to generate.