]>
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>
30 #include <X11/extensions/Xdamage.h>
39 #include "systraybar.h"
44 void signal_handler(int sig
)
46 // signal handler is light as it should be
51 void init (int argc
, char *argv
[])
56 for (i
= 1; i
< argc
; ++i
) {
57 if (!strcmp(argv
[i
], "-h") || !strcmp(argv
[i
], "--help")) {
58 printf("Usage: tint2 [-c] <config_file>\n");
61 if (!strcmp(argv
[i
], "-v") || !strcmp(argv
[i
], "--version")) {
62 printf("tint2 version 0.8\n");
65 if (!strcmp(argv
[i
], "-c")) {
68 config_path
= strdup(argv
[i
]);
70 if (!strcmp(argv
[i
], "-s")) {
73 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);
92 memset(&server
, 0, sizeof(Server_global
));
93 memset(&systray
, 0, sizeof(Systraybar
));
98 server
.dsp
= XOpenDisplay (NULL
);
100 fprintf(stderr
, "tint2 exit : could not open display.\n");
103 server_init_atoms ();
104 server
.screen
= DefaultScreen (server
.dsp
);
105 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
106 server
.desktop
= server_get_current_desktop ();
107 server_init_visual();
108 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
110 imlib_context_set_display (server
.dsp
);
111 imlib_context_set_visual (server
.visual
);
112 imlib_context_set_colormap (server
.colormap
);
115 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
117 setlocale (LC_ALL
, "");
121 const gchar
* const *data_dirs
;
122 data_dirs
= g_get_system_data_dirs ();
124 for (i
= 0; data_dirs
[i
] != NULL
; i
++) {
125 path
= g_build_filename(data_dirs
[i
], "tint2", "default_icon.png", NULL
);
126 if (g_file_test (path
, G_FILE_TEST_EXISTS
))
127 default_icon
= imlib_load_image(path
);
131 // get monitor and desktop config
144 #ifdef ENABLE_BATTERY
149 imlib_context_set_image(default_icon
);
152 if (config_path
) g_free(config_path
);
153 if (snapshot_path
) g_free(snapshot_path
);
156 XCloseDisplay(server
.dsp
);
160 void get_snapshot(const char *path
)
162 Panel
*panel
= &panel1
[0];
164 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
165 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
167 refresh(&panel
->area
);
169 Imlib_Image img
= NULL
;
170 imlib_context_set_drawable(panel
->temp_pmap
);
171 img
= imlib_create_image_from_drawable(0, 0, 0, panel
->area
.width
, panel
->area
.height
, 0);
173 imlib_context_set_image(img
);
174 imlib_save_image(path
);
179 void window_action (Task
*tsk
, int action
)
185 set_close (tsk
->win
);
188 set_active(tsk
->win
);
191 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
194 if (task_active
&& tsk
->win
== task_active
->win
)
195 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
197 set_active (tsk
->win
);
200 window_toggle_shade (tsk
->win
);
202 case MAXIMIZE_RESTORE
:
203 window_maximize_restore (tsk
->win
);
206 window_maximize_restore (tsk
->win
);
209 window_maximize_restore (tsk
->win
);
212 if ( tsk
->desktop
== 0 ) break;
213 desk
= tsk
->desktop
- 1;
214 windows_set_desktop(tsk
->win
, desk
);
215 if (desk
== server
.desktop
)
216 set_active(tsk
->win
);
219 if (tsk
->desktop
== server
.nb_desktop
) break;
220 desk
= tsk
->desktop
+ 1;
221 windows_set_desktop(tsk
->win
, desk
);
222 if (desk
== server
.desktop
)
223 set_active(tsk
->win
);
228 tsk1
= next_task(task_active
);
229 set_active(tsk1
->win
);
235 tsk1
= prev_task(task_active
);
236 set_active(tsk1
->win
);
242 int tint2_handles_click(Panel
* panel
, XButtonEvent
* e
)
244 Task
* task
= click_task(panel
, e
->x
, e
->y
);
247 || (e
->button
== 2 && mouse_middle
!= 0)
248 || (e
->button
== 3 && mouse_right
!= 0)
249 || (e
->button
== 4 && mouse_scroll_up
!= 0)
250 || (e
->button
== 5 && mouse_scroll_down
!=0) )
257 // no task clicked --> check if taskbar clicked
258 Taskbar
*tskbar
= click_taskbar(panel
, e
->x
, e
->y
);
259 if (tskbar
&& e
->button
== 1 && panel_mode
== MULTI_DESKTOP
)
261 if (click_clock(panel
, e
->x
, e
->y
)) {
262 if ( (e
->button
== 1 && clock_lclick_command
) || (e
->button
== 2 && clock_rclick_command
) )
271 void forward_click(XEvent
* e
)
273 // forward the click to the desktop window (thanks conky)
274 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
275 e
->xbutton
.window
= server
.root_win
;
276 // icewm doesn't open under the mouse.
277 // and xfce doesn't open at all.
278 e
->xbutton
.x
= e
->xbutton
.x_root
;
279 e
->xbutton
.y
= e
->xbutton
.y_root
;
280 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
281 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
282 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
286 void event_button_press (XEvent
*e
)
288 Panel
*panel
= get_panel(e
->xany
.window
);
292 if (wm_menu
&& !tint2_handles_click(panel
, &e
->xbutton
) ) {
296 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
298 XLowerWindow (server
.dsp
, panel
->main_win
);
302 void event_button_release (XEvent
*e
)
304 Panel
*panel
= get_panel(e
->xany
.window
);
307 if (wm_menu
&& !tint2_handles_click(panel
, &e
->xbutton
)) {
313 int action
= TOGGLE_ICONIFY
;
314 switch (e
->xbutton
.button
) {
316 action
= mouse_middle
;
319 action
= mouse_right
;
322 action
= mouse_scroll_up
;
325 action
= mouse_scroll_down
;
328 action
= mouse_tilt_left
;
331 action
= mouse_tilt_right
;
335 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
336 clock_action(e
->xbutton
.button
);
337 XLowerWindow (server
.dsp
, panel
->main_win
);
343 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
344 // TODO: check better solution to keep window below
345 XLowerWindow (server
.dsp
, panel
->main_win
);
350 // drag and drop task
352 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
353 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
354 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
355 if (tskbar
->desktop
== server
.desktop
)
356 set_active(task_drag
->win
);
365 if (panel_mode
== MULTI_DESKTOP
) {
366 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
367 set_desktop (tskbar
->desktop
);
371 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
373 // to keep window below
374 XLowerWindow (server
.dsp
, panel
->main_win
);
378 void event_property_notify (XEvent
*e
)
382 Window win
= e
->xproperty
.window
;
383 Atom at
= e
->xproperty
.atom
;
385 if (win
== server
.root_win
) {
386 if (!server
.got_root_win
) {
387 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
388 server
.got_root_win
= 1;
391 // Change number of desktops
392 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
393 server
.nb_desktop
= server_get_number_of_desktop ();
397 for (i
=0 ; i
< nb_panel
; i
++) {
398 panel1
[i
].area
.resize
= 1;
400 task_refresh_tasklist();
405 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
406 int old_desktop
= server
.desktop
;
407 server
.desktop
= server_get_current_desktop ();
408 for (i
=0 ; i
< nb_panel
; i
++) {
409 Panel
*panel
= &panel1
[i
];
410 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
411 // redraw both taskbar
412 if (server
.nb_desktop
> old_desktop
) {
413 // can happen if last desktop is deleted and we've been on the last desktop
414 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
415 panel
->taskbar
[old_desktop
].area
.resize
= 1;
417 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
418 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
421 // check ALLDESKTOP task => resize taskbar
425 if (server
.nb_desktop
> old_desktop
) {
426 tskbar
= &panel
->taskbar
[old_desktop
];
427 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
429 if (tsk
->desktop
== ALLDESKTOP
) {
430 tsk
->area
.on_screen
= 0;
431 tskbar
->area
.resize
= 1;
436 tskbar
= &panel
->taskbar
[server
.desktop
];
437 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
439 if (tsk
->desktop
== ALLDESKTOP
) {
440 tsk
->area
.on_screen
= 1;
441 tskbar
->area
.resize
= 1;
445 if (panel_mode
!= MULTI_DESKTOP
) {
450 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
451 task_refresh_tasklist();
455 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
459 else if (at
== server
.atom
._XROOTPMAP_ID
) {
461 for (i
=0 ; i
< nb_panel
; i
++) {
462 set_panel_background(&panel1
[i
]);
468 tsk
= task_get_task (win
);
470 if (at
!= server
.atom
._NET_WM_STATE
)
473 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
474 // if it is mapped and not set as skip_taskbar, we must add it to our task list
475 XWindowAttributes wa
;
476 XGetWindowAttributes(server
.dsp
, win
, &wa
);
477 if (wa
.map_state
== IsViewable
&& !window_is_skip_taskbar(win
)) {
478 if ( (tsk
= add_task(win
)) )
487 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
489 // Window title changed
490 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
494 // changed other tsk->title
495 for (i
=0 ; i
< nb_panel
; i
++) {
496 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
497 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
499 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
500 tsk2
->title
= tsk
->title
;
501 tsk2
->area
.redraw
= 1;
509 else if (at
== server
.atom
._NET_WM_STATE
) {
510 if (window_is_urgent (win
)) {
513 if (window_is_skip_taskbar(win
)) {
518 // We do not check for the iconified state, since it only unsets our active window
519 // but in openbox a shaded window is considered iconified. So we would loose the active window
520 // property on unshading it again (commented 01.10.2009)
521 // else if (at == server.atom.WM_STATE) {
523 // // TODO : try to delete following code
524 // if (window_is_iconified (win)) {
525 // if (task_active) {
526 // if (task_active->win == tsk->win) {
529 // for (i=0 ; i < nb_panel ; i++) {
530 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
531 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
533 // tsk2->area.is_active = 0;
542 // Window icon changed
543 else if (at
== server
.atom
._NET_WM_ICON
) {
547 for (i
=0 ; i
< nb_panel
; i
++) {
548 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
549 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
551 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
552 tsk2
->icon_width
= tsk
->icon_width
;
553 tsk2
->icon_height
= tsk
->icon_height
;
554 tsk2
->icon
= tsk
->icon
;
555 tsk2
->icon_active
= tsk
->icon_active
;
556 tsk2
->area
.redraw
= 1;
563 // Window desktop changed
564 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
565 int desktop
= window_get_desktop (win
);
566 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
567 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
568 if (desktop
!= tsk
->desktop
) {
570 tsk
= add_task (win
);
575 else if (at
== server
.atom
.WM_HINTS
) {
576 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
577 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
583 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
588 void event_expose (XEvent
*e
)
591 panel
= get_panel(e
->xany
.window
);
593 // TODO : one panel_refresh per panel ?
598 void event_configure_notify (Window win
)
600 // change in root window (xrandr)
601 if (win
== server
.root_win
) {
604 config_read_file (config_path
);
610 // 'win' is a trayer icon
613 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
614 traywin
= (TrayWindow
*)l
->data
;
615 if (traywin
->tray_id
== win
) {
616 //printf("move tray %d\n", traywin->x);
617 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
618 XResizeWindow(server
.dsp
, traywin
->tray_id
, traywin
->width
, traywin
->height
);
624 // 'win' move in another monitor
625 if (nb_panel
== 1) return;
626 Task
*tsk
= task_get_task (win
);
629 Panel
*p
= tsk
->area
.panel
;
630 if (p
->monitor
!= window_get_monitor (win
)) {
633 if (win
== window_get_active ()) {
634 GSList
* task_list
= task_get_tasks(win
);
636 Task
*tsk
= task_list
->data
;
637 tsk
->area
.is_active
= 1;
639 task_list
= task_list
->next
;
647 void dnd_message(XClientMessageEvent
*e
)
649 Panel
*panel
= get_panel(e
->window
);
650 int x
, y
, mapX
, mapY
;
652 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
653 y
= e
->data
.l
[2] & 0xFFFF;
654 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
655 Task
* task
= click_task(panel
, mapX
, mapY
);
657 if (task
->desktop
!= server
.desktop
)
658 set_desktop (task
->desktop
);
659 window_action(task
, TOGGLE
);
662 // send XdndStatus event to get more XdndPosition events
663 XClientMessageEvent se
;
664 se
.type
= ClientMessage
;
665 se
.window
= e
->data
.l
[0];
666 se
.message_type
= server
.atom
.XdndStatus
;
668 se
.data
.l
[0] = e
->window
; // XID of the target window
669 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
670 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
671 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
672 se
.data
.l
[4] = None
; // None = drop will not be accepted
673 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
677 int main (int argc
, char *argv
[])
684 const struct timespec
* timeout
;
690 i
= config_read_file (config_path
);
694 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
703 get_snapshot(snapshot_path
);
708 int damage_event
, damage_error
;
709 XDamageQueryExtension(server
.dsp
, &damage_event
, &damage_error
);
710 x11_fd
= ConnectionNumber(server
.dsp
);
711 XSync(server
.dsp
, False
);
714 sigemptyset(&empty_mask
);
720 // QUESTION: do we need this first refresh_systray, because we check refresh_systray once again later...
721 if (refresh_systray
) {
722 panel
= (Panel
*)systray
.area
.panel
;
723 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
725 for (i
=0 ; i
< nb_panel
; i
++) {
728 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
729 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
731 refresh(&panel
->area
);
732 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
736 if (refresh_systray
) {
738 panel
= (Panel
*)systray
.area
.panel
;
739 // tint2 doen't draw systray icons. it just redraw background.
740 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
741 // force icon's refresh
742 refresh_systray_icon();
746 // thanks to AngryLlama for the timer
747 // Create a File Description Set containing x11_fd
749 FD_SET (x11_fd
, &fdset
);
750 update_next_timeout();
751 if (next_timeout
.tv_sec
>= 0 && next_timeout
.tv_nsec
>= 0)
752 timeout
= &next_timeout
;
756 // Wait for X Event or a Timer
757 if (pselect(x11_fd
+1, &fdset
, 0, 0, timeout
, &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 Area
* area
= click_area(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
775 if (area
->_get_tooltip_text
)
776 tooltip_trigger_show(area
, panel
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
778 tooltip_trigger_hide();
783 tooltip_trigger_hide();
791 if (e
.xany
.window
== g_tooltip
.window
)
796 event_property_notify(&e
);
799 case ConfigureNotify
:
800 event_configure_notify (e
.xconfigure
.window
);
804 if (!systray_enabled
)
806 panel
= (Panel
*)systray
.area
.panel
;
807 if (e
.xany
.window
== panel
->main_win
) // reparented to us
809 // FIXME: 'reparent to us' badly detected => disabled
813 if (e
.xany
.window
== g_tooltip
.window
|| !systray
.area
.on_screen
)
815 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
816 if (((TrayWindow
*)it
->data
)->tray_id
== e
.xany
.window
) {
817 remove_icon((TrayWindow
*)it
->data
);
824 if (!systray
.area
.on_screen
) break;
825 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
826 net_message(&e
.xclient
);
828 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
829 dnd_message(&e
.xclient
);
834 if (e
.type
== XDamageNotify
+damage_event
)
835 // TODO: update only the damaged icon, not all of them
836 systray
.area
.redraw
= 1;
841 callback_timeout_expired();
843 switch (signal_pending
) {
844 case SIGUSR1
: // reload config file
847 config_read_file (config_path
);
This page took 0.07085 seconds and 4 git commands to generate.