]>
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.8\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 void window_action (Task
*tsk
, int action
)
187 set_close (tsk
->win
);
190 set_active(tsk
->win
);
193 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
196 if (task_active
&& tsk
->win
== task_active
->win
)
197 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
199 set_active (tsk
->win
);
202 window_toggle_shade (tsk
->win
);
204 case MAXIMIZE_RESTORE
:
205 window_maximize_restore (tsk
->win
);
208 window_maximize_restore (tsk
->win
);
211 window_maximize_restore (tsk
->win
);
214 if ( tsk
->desktop
== 0 ) break;
215 desk
= tsk
->desktop
- 1;
216 windows_set_desktop(tsk
->win
, desk
);
217 if (desk
== server
.desktop
)
218 set_active(tsk
->win
);
221 if (tsk
->desktop
== server
.nb_desktop
) break;
222 desk
= tsk
->desktop
+ 1;
223 windows_set_desktop(tsk
->win
, desk
);
224 if (desk
== server
.desktop
)
225 set_active(tsk
->win
);
230 tsk1
= next_task(task_active
);
231 set_active(tsk1
->win
);
237 tsk1
= prev_task(task_active
);
238 set_active(tsk1
->win
);
244 void event_button_press (XEvent
*e
)
246 Panel
*panel
= get_panel(e
->xany
.window
);
249 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
251 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
) && (e
->xbutton
.button
!= 1) ) {
252 // forward the click to the desktop window (thanks conky)
253 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
254 e
->xbutton
.window
= server
.root_win
;
255 // icewm doesn't open under the mouse.
256 // and xfce doesn't open at all.
257 e
->xbutton
.x
= e
->xbutton
.x_root
;
258 e
->xbutton
.y
= e
->xbutton
.y_root
;
259 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
260 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
261 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
265 XLowerWindow (server
.dsp
, panel
->main_win
);
269 void event_button_release (XEvent
*e
)
271 Panel
*panel
= get_panel(e
->xany
.window
);
274 int action
= TOGGLE_ICONIFY
;
275 switch (e
->xbutton
.button
) {
277 action
= mouse_middle
;
280 action
= mouse_right
;
283 action
= mouse_scroll_up
;
286 action
= mouse_scroll_down
;
289 action
= mouse_tilt_left
;
292 action
= mouse_tilt_right
;
296 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
297 clock_action(e
->xbutton
.button
);
298 XLowerWindow (server
.dsp
, panel
->main_win
);
304 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
305 // TODO: check better solution to keep window below
306 XLowerWindow (server
.dsp
, panel
->main_win
);
311 // drag and drop task
313 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
314 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
315 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
316 if (tskbar
->desktop
== server
.desktop
)
317 set_active(task_drag
->win
);
326 if (panel_mode
== MULTI_DESKTOP
) {
327 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
328 set_desktop (tskbar
->desktop
);
332 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
334 // to keep window below
335 XLowerWindow (server
.dsp
, panel
->main_win
);
339 void event_property_notify (XEvent
*e
)
343 Window win
= e
->xproperty
.window
;
344 Atom at
= e
->xproperty
.atom
;
346 if (win
== server
.root_win
) {
347 if (!server
.got_root_win
) {
348 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
349 server
.got_root_win
= 1;
352 // Change number of desktops
353 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
354 server
.nb_desktop
= server_get_number_of_desktop ();
358 for (i
=0 ; i
< nb_panel
; i
++) {
359 panel1
[i
].area
.resize
= 1;
361 task_refresh_tasklist();
365 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
366 int old_desktop
= server
.desktop
;
367 server
.desktop
= server_get_current_desktop ();
368 for (i
=0 ; i
< nb_panel
; i
++) {
369 Panel
*panel
= &panel1
[i
];
370 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
371 // redraw both taskbar
372 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
373 panel
->taskbar
[old_desktop
].area
.resize
= 1;
374 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
375 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
378 // check ALLDESKTOP task => resize taskbar
382 tskbar
= &panel
->taskbar
[old_desktop
];
383 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
385 if (tsk
->desktop
== ALLDESKTOP
) {
386 tsk
->area
.on_screen
= 0;
387 tskbar
->area
.resize
= 1;
391 tskbar
= &panel
->taskbar
[server
.desktop
];
392 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
394 if (tsk
->desktop
== ALLDESKTOP
) {
395 tsk
->area
.on_screen
= 1;
396 tskbar
->area
.resize
= 1;
400 if (panel_mode
!= MULTI_DESKTOP
) {
405 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
406 task_refresh_tasklist();
410 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
414 else if (at
== server
.atom
._XROOTPMAP_ID
) {
416 for (i
=0 ; i
< nb_panel
; i
++) {
417 set_panel_background(&panel1
[i
]);
423 tsk
= task_get_task (win
);
425 if (at
!= server
.atom
._NET_WM_STATE
)
428 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
429 // if it is mapped and not set as skip_taskbar, we must add it to our task list
430 XWindowAttributes wa
;
431 XGetWindowAttributes(server
.dsp
, win
, &wa
);
432 if (wa
.map_state
== IsViewable
&& !window_is_skip_taskbar(win
)) {
433 if ( (tsk
= add_task(win
)) )
442 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
444 // Window title changed
445 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
449 // changed other tsk->title
450 for (i
=0 ; i
< nb_panel
; i
++) {
451 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
452 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
454 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
455 tsk2
->title
= tsk
->title
;
456 tsk2
->area
.redraw
= 1;
464 else if (at
== server
.atom
._NET_WM_STATE
) {
465 if (window_is_urgent (win
)) {
468 if (window_is_skip_taskbar(win
)) {
473 // We do not check for the iconified state, since it only unsets our active window
474 // but in openbox a shaded window is considered iconified. So we would loose the active window
475 // property on unshading it again (commented 01.10.2009)
476 // else if (at == server.atom.WM_STATE) {
478 // // TODO : try to delete following code
479 // if (window_is_iconified (win)) {
480 // if (task_active) {
481 // if (task_active->win == tsk->win) {
484 // for (i=0 ; i < nb_panel ; i++) {
485 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
486 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
488 // tsk2->area.is_active = 0;
497 // Window icon changed
498 else if (at
== server
.atom
._NET_WM_ICON
) {
502 for (i
=0 ; i
< nb_panel
; i
++) {
503 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
504 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
506 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
507 tsk2
->icon_width
= tsk
->icon_width
;
508 tsk2
->icon_height
= tsk
->icon_height
;
509 tsk2
->icon
= tsk
->icon
;
510 tsk2
->icon_active
= tsk
->icon_active
;
511 tsk2
->area
.redraw
= 1;
518 // Window desktop changed
519 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
520 int desktop
= window_get_desktop (win
);
521 int active
= tsk
->area
.is_active
;
522 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
523 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
524 if (desktop
!= tsk
->desktop
) {
526 tsk
= add_task (win
);
528 tsk
->area
.is_active
= 1;
534 else if (at
== server
.atom
.WM_HINTS
) {
535 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
536 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
542 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
547 void event_expose (XEvent
*e
)
550 panel
= get_panel(e
->xany
.window
);
552 // TODO : one panel_refresh per panel ?
557 void event_configure_notify (Window win
)
559 // change in root window (xrandr)
560 if (win
== server
.root_win
) {
563 config_read_file (config_path
);
569 // 'win' is a trayer icon
572 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
573 traywin
= (TrayWindow
*)l
->data
;
574 if (traywin
->id
== win
) {
575 //printf("move tray %d\n", traywin->x);
576 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
582 // 'win' move in another monitor
583 if (nb_panel
== 1) return;
584 Task
*tsk
= task_get_task (win
);
587 Panel
*p
= tsk
->area
.panel
;
588 if (p
->monitor
!= window_get_monitor (win
)) {
591 if (win
== window_get_active ()) {
592 Task
*tsk
= task_get_task (win
);
593 tsk
->area
.is_active
= 1;
601 void dnd_message(XClientMessageEvent
*e
)
603 Panel
*panel
= get_panel(e
->window
);
604 int x
, y
, mapX
, mapY
;
606 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
607 y
= e
->data
.l
[2] & 0xFFFF;
608 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
609 Task
* task
= click_task(panel
, mapX
, mapY
);
611 if (task
->desktop
!= server
.desktop
)
612 set_desktop (task
->desktop
);
613 window_action(task
, TOGGLE
);
616 // send XdndStatus event to get more XdndPosition events
617 XClientMessageEvent se
;
618 se
.type
= ClientMessage
;
619 se
.window
= e
->data
.l
[0];
620 se
.message_type
= server
.atom
.XdndStatus
;
622 se
.data
.l
[0] = e
->window
; // XID of the target window
623 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
624 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
625 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
626 se
.data
.l
[4] = None
; // None = drop will not be accepted
627 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
631 int main (int argc
, char *argv
[])
646 i
= config_read_file (config_path
);
650 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
657 get_snapshot(snapshot_path
);
662 x11_fd
= ConnectionNumber(server
.dsp
);
663 XSync(server
.dsp
, False
);
666 sigemptyset(&empty_mask
);
672 if (refresh_systray
) {
673 panel
= (Panel
*)systray
.area
.panel
;
674 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
676 for (i
=0 ; i
< nb_panel
; i
++) {
679 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
680 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
682 refresh(&panel
->area
);
683 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
687 if (refresh_systray
) {
689 panel
= (Panel
*)systray
.area
.panel
;
690 // tint2 doen't draw systray icons. it just redraw background.
691 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
692 // force icon's refresh
693 refresh_systray_icon();
697 // thanks to AngryLlama for the timer
698 // Create a File Description Set containing x11_fd, and every timer_fd
700 FD_SET (x11_fd
, &fdset
);
702 timer_iter
= timer_list
;
704 timer
= timer_iter
->data
;
705 max_fd
= timer
->id
> max_fd
? timer
->id
: max_fd
;
706 FD_SET(timer
->id
, &fdset
);
707 timer_iter
= timer_iter
->next
;
710 // Wait for X Event or a Timer
711 if (pselect(max_fd
+1, &fdset
, 0, 0, 0, &empty_mask
) > 0) {
712 // we need to iterate over the whole timer list, since fd_set can only be checked with the
713 // brute force method FD_ISSET for every possible timer
714 timer_iter
= timer_list
;
716 timer
= timer_iter
->data
;
717 if (FD_ISSET(timer
->id
, &fdset
)) {
719 if ( -1 != read(timer
->id
, &dummy
, sizeof(uint64_t)) )
722 timer_iter
= timer_iter
->next
;
725 while (XPending (server
.dsp
)) {
726 XNextEvent(server
.dsp
, &e
);
731 event_button_press (&e
);
735 event_button_release(&e
);
739 if (!g_tooltip
.enabled
) break;
740 Panel
* panel
= get_panel(e
.xmotion
.window
);
741 Area
* area
= click_area(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
742 if (area
->_get_tooltip_text
)
743 tooltip_trigger_show(area
, panel
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
745 tooltip_trigger_hide();
750 tooltip_trigger_hide();
758 if (e
.xany
.window
== g_tooltip
.window
)
763 event_property_notify(&e
);
766 case ConfigureNotify
:
767 event_configure_notify (e
.xconfigure
.window
);
771 if (!systray_enabled
)
773 panel
= (Panel
*)systray
.area
.panel
;
774 if (e
.xany
.window
== panel
->main_win
) // reparented to us
776 // FIXME: 'reparent to us' badly detected => disabled
780 if (e
.xany
.window
== g_tooltip
.window
|| !systray
.area
.on_screen
)
782 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
783 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
784 remove_icon((TrayWindow
*)it
->data
);
791 if (!systray
.area
.on_screen
) break;
792 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
793 net_message(&e
.xclient
);
795 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
796 dnd_message(&e
.xclient
);
803 switch (signal_pending
) {
804 case SIGUSR1
: // reload config file
807 config_read_file (config_path
);
This page took 0.07184 seconds and 4 git commands to generate.