]>
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()
84 // BSD is too stupid to support pselect(), therefore we have to use select and hope that we do not
85 // end up in a race condition there
86 // block all signals, such that no race conditions occur before pselect in our main loop
87 // sigset_t block_mask;
88 // sigaddset(&block_mask, SIGINT);
89 // sigaddset(&block_mask, SIGTERM);
90 // sigaddset(&block_mask, SIGHUP);
91 // sigaddset(&block_mask, SIGUSR1);
92 // sigprocmask(SIG_BLOCK, &block_mask, 0);
95 memset(&server
, 0, sizeof(Server_global
));
96 memset(&systray
, 0, sizeof(Systraybar
));
101 server
.dsp
= XOpenDisplay (NULL
);
103 fprintf(stderr
, "tint2 exit : could not open display.\n");
106 server_init_atoms ();
107 server
.screen
= DefaultScreen (server
.dsp
);
108 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
109 server
.desktop
= server_get_current_desktop ();
110 server_init_visual();
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 (server
.colormap
);
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 ();
127 for (i
= 0; data_dirs
[i
] != NULL
; i
++) {
128 path
= g_build_filename(data_dirs
[i
], "tint2", "default_icon.png", NULL
);
129 if (g_file_test (path
, G_FILE_TEST_EXISTS
))
130 default_icon
= imlib_load_image(path
);
134 // get monitor and desktop config
148 #ifdef ENABLE_BATTERY
153 imlib_context_set_image(default_icon
);
156 if (config_path
) g_free(config_path
);
157 if (snapshot_path
) g_free(snapshot_path
);
160 if (server
.dsp
) XCloseDisplay(server
.dsp
);
164 void get_snapshot(const char *path
)
166 Panel
*panel
= &panel1
[0];
168 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
169 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
171 refresh(&panel
->area
);
173 Imlib_Image img
= NULL
;
174 imlib_context_set_drawable(panel
->temp_pmap
);
175 img
= imlib_create_image_from_drawable(0, 0, 0, panel
->area
.width
, panel
->area
.height
, 0);
177 imlib_context_set_image(img
);
178 imlib_save_image(path
);
183 void window_action (Task
*tsk
, int action
)
189 set_close (tsk
->win
);
192 set_active(tsk
->win
);
195 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
198 if (task_active
&& tsk
->win
== task_active
->win
)
199 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
201 set_active (tsk
->win
);
204 window_toggle_shade (tsk
->win
);
206 case MAXIMIZE_RESTORE
:
207 window_maximize_restore (tsk
->win
);
210 window_maximize_restore (tsk
->win
);
213 window_maximize_restore (tsk
->win
);
216 if ( tsk
->desktop
== 0 ) break;
217 desk
= tsk
->desktop
- 1;
218 windows_set_desktop(tsk
->win
, desk
);
219 if (desk
== server
.desktop
)
220 set_active(tsk
->win
);
223 if (tsk
->desktop
== server
.nb_desktop
) break;
224 desk
= tsk
->desktop
+ 1;
225 windows_set_desktop(tsk
->win
, desk
);
226 if (desk
== server
.desktop
)
227 set_active(tsk
->win
);
232 tsk1
= next_task(task_active
);
233 set_active(tsk1
->win
);
239 tsk1
= prev_task(task_active
);
240 set_active(tsk1
->win
);
246 int tint2_handles_click(Panel
* panel
, XButtonEvent
* e
)
248 Task
* task
= click_task(panel
, e
->x
, e
->y
);
251 || (e
->button
== 2 && mouse_middle
!= 0)
252 || (e
->button
== 3 && mouse_right
!= 0)
253 || (e
->button
== 4 && mouse_scroll_up
!= 0)
254 || (e
->button
== 5 && mouse_scroll_down
!=0) )
261 // no task clicked --> check if taskbar clicked
262 Taskbar
*tskbar
= click_taskbar(panel
, e
->x
, e
->y
);
263 if (tskbar
&& e
->button
== 1 && panel_mode
== MULTI_DESKTOP
)
265 if (click_clock(panel
, e
->x
, e
->y
)) {
266 printf("ici 1 : %d\n", e
->button
);
267 if ( (e
->button
== 1 && clock_lclick_command
) || (e
->button
== 3 && clock_rclick_command
) )
276 void forward_click(XEvent
* e
)
278 // forward the click to the desktop window (thanks conky)
279 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
280 e
->xbutton
.window
= server
.root_win
;
281 // icewm doesn't open under the mouse.
282 // and xfce doesn't open at all.
283 e
->xbutton
.x
= e
->xbutton
.x_root
;
284 e
->xbutton
.y
= e
->xbutton
.y_root
;
285 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
286 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
287 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
291 void event_button_press (XEvent
*e
)
293 Panel
*panel
= get_panel(e
->xany
.window
);
297 if (wm_menu
&& !tint2_handles_click(panel
, &e
->xbutton
) ) {
301 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
303 XLowerWindow (server
.dsp
, panel
->main_win
);
307 void event_button_release (XEvent
*e
)
309 Panel
*panel
= get_panel(e
->xany
.window
);
312 if (wm_menu
&& !tint2_handles_click(panel
, &e
->xbutton
)) {
314 XLowerWindow (server
.dsp
, panel
->main_win
);
319 int action
= TOGGLE_ICONIFY
;
320 switch (e
->xbutton
.button
) {
322 action
= mouse_middle
;
325 action
= mouse_right
;
328 action
= mouse_scroll_up
;
331 action
= mouse_scroll_down
;
334 action
= mouse_tilt_left
;
337 action
= mouse_tilt_right
;
341 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
342 clock_action(e
->xbutton
.button
);
343 XLowerWindow (server
.dsp
, panel
->main_win
);
349 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
350 // TODO: check better solution to keep window below
351 XLowerWindow (server
.dsp
, panel
->main_win
);
356 // drag and drop task
358 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
359 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
360 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
361 if (tskbar
->desktop
== server
.desktop
)
362 set_active(task_drag
->win
);
371 if (panel_mode
== MULTI_DESKTOP
) {
372 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
373 set_desktop (tskbar
->desktop
);
377 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
379 // to keep window below
380 XLowerWindow (server
.dsp
, panel
->main_win
);
384 void event_property_notify (XEvent
*e
)
388 Window win
= e
->xproperty
.window
;
389 Atom at
= e
->xproperty
.atom
;
391 if (win
== server
.root_win
) {
392 if (!server
.got_root_win
) {
393 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
394 server
.got_root_win
= 1;
397 // Change number of desktops
398 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
399 server
.nb_desktop
= server_get_number_of_desktop ();
403 for (i
=0 ; i
< nb_panel
; i
++) {
404 panel1
[i
].area
.resize
= 1;
406 task_refresh_tasklist();
411 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
412 int old_desktop
= server
.desktop
;
413 server
.desktop
= server_get_current_desktop ();
414 for (i
=0 ; i
< nb_panel
; i
++) {
415 Panel
*panel
= &panel1
[i
];
416 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
417 // redraw both taskbar
418 if (server
.nb_desktop
> old_desktop
) {
419 // can happen if last desktop is deleted and we've been on the last desktop
420 panel
->taskbar
[old_desktop
].area
.bg
= panel
->g_taskbar
.bg
;
421 panel
->taskbar
[old_desktop
].area
.resize
= 1;
423 panel
->taskbar
[server
.desktop
].area
.bg
= panel
->g_taskbar
.bg_active
;
424 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
427 // check ALLDESKTOP task => resize taskbar
431 if (server
.nb_desktop
> old_desktop
) {
432 tskbar
= &panel
->taskbar
[old_desktop
];
433 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
435 if (tsk
->desktop
== ALLDESKTOP
) {
436 tsk
->area
.on_screen
= 0;
437 tskbar
->area
.resize
= 1;
442 tskbar
= &panel
->taskbar
[server
.desktop
];
443 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
445 if (tsk
->desktop
== ALLDESKTOP
) {
446 tsk
->area
.on_screen
= 1;
447 tskbar
->area
.resize
= 1;
451 if (panel_mode
!= MULTI_DESKTOP
) {
456 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
457 task_refresh_tasklist();
461 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
465 else if (at
== server
.atom
._XROOTPMAP_ID
) {
467 for (i
=0 ; i
< nb_panel
; i
++) {
468 set_panel_background(&panel1
[i
]);
474 tsk
= task_get_task (win
);
476 if (at
!= server
.atom
._NET_WM_STATE
)
479 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
480 // if it is mapped and not set as skip_taskbar, we must add it to our task list
481 XWindowAttributes wa
;
482 XGetWindowAttributes(server
.dsp
, win
, &wa
);
483 if (wa
.map_state
== IsViewable
&& !window_is_skip_taskbar(win
)) {
484 if ( (tsk
= add_task(win
)) )
493 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
495 // Window title changed
496 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
501 else if (at
== server
.atom
._NET_WM_STATE
) {
502 if (window_is_urgent (win
)) {
505 if (window_is_skip_taskbar(win
)) {
510 else if (at
== server
.atom
.WM_STATE
) {
512 int state
= (task_active
&& tsk
->win
== task_active
->win
? TASK_ACTIVE
: TASK_NORMAL
);
513 if (window_is_iconified(win
))
514 state
= TASK_ICONIFIED
;
515 set_task_state(tsk
, state
);
518 // Window icon changed
519 else if (at
== server
.atom
._NET_WM_ICON
) {
523 // Window desktop changed
524 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
525 int desktop
= window_get_desktop (win
);
526 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
527 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
528 if (desktop
!= tsk
->desktop
) {
530 tsk
= add_task (win
);
535 else if (at
== server
.atom
.WM_HINTS
) {
536 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
537 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
543 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
548 void event_expose (XEvent
*e
)
551 panel
= get_panel(e
->xany
.window
);
553 // TODO : one panel_refresh per panel ?
558 void event_configure_notify (Window win
)
560 // change in root window (xrandr)
561 if (win
== server
.root_win
) {
564 config_read_file (config_path
);
570 // 'win' is a trayer icon
573 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
574 traywin
= (TrayWindow
*)l
->data
;
575 if (traywin
->tray_id
== win
) {
576 //printf("move tray %d\n", traywin->x);
577 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
578 XResizeWindow(server
.dsp
, traywin
->tray_id
, traywin
->width
, traywin
->height
);
584 // 'win' move in another monitor
585 if (nb_panel
== 1) return;
586 Task
*tsk
= task_get_task (win
);
589 Panel
*p
= tsk
->area
.panel
;
590 if (p
->monitor
!= window_get_monitor (win
)) {
592 tsk
= add_task (win
);
593 if (win
== window_get_active ()) {
594 set_task_state(tsk
, TASK_ACTIVE
);
602 void dnd_message(XClientMessageEvent
*e
)
604 Panel
*panel
= get_panel(e
->window
);
605 int x
, y
, mapX
, mapY
;
607 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
608 y
= e
->data
.l
[2] & 0xFFFF;
609 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
610 Task
* task
= click_task(panel
, mapX
, mapY
);
612 if (task
->desktop
!= server
.desktop
)
613 set_desktop (task
->desktop
);
614 window_action(task
, TOGGLE
);
617 // send XdndStatus event to get more XdndPosition events
618 XClientMessageEvent se
;
619 se
.type
= ClientMessage
;
620 se
.window
= e
->data
.l
[0];
621 se
.message_type
= server
.atom
.XdndStatus
;
623 se
.data
.l
[0] = e
->window
; // XID of the target window
624 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
625 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
626 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
627 se
.data
.l
[4] = None
; // None = drop will not be accepted
628 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
632 int main (int argc
, char *argv
[])
639 struct timeval
* timeout
;
645 i
= config_read_file (config_path
);
649 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
658 get_snapshot(snapshot_path
);
663 int damage_event
, damage_error
;
664 XDamageQueryExtension(server
.dsp
, &damage_event
, &damage_error
);
665 x11_fd
= ConnectionNumber(server
.dsp
);
666 XSync(server
.dsp
, False
);
668 // sigset_t empty_mask;
669 // sigemptyset(&empty_mask);
675 // QUESTION: do we need this first refresh_systray, because we check refresh_systray once again later...
676 if (refresh_systray
) {
677 panel
= (Panel
*)systray
.area
.panel
;
678 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
680 for (i
=0 ; i
< nb_panel
; i
++) {
683 if (panel
->is_hidden
) {
684 XCopyArea(server
.dsp
, panel
->hidden_pixmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->hidden_width
, panel
->hidden_height
, 0, 0);
685 XSetWindowBackgroundPixmap(server
.dsp
, panel
->main_win
, panel
->hidden_pixmap
);
688 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
689 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
690 refresh(&panel
->area
);
691 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
696 panel
= (Panel
*)systray
.area
.panel
;
697 if (refresh_systray
&& !panel
->is_hidden
) {
699 panel
= (Panel
*)systray
.area
.panel
;
700 // tint2 doen't draw systray icons. it just redraw background.
701 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
702 // force icon's refresh
703 refresh_systray_icon();
707 // thanks to AngryLlama for the timer
708 // Create a File Description Set containing x11_fd
710 FD_SET (x11_fd
, &fdset
);
711 update_next_timeout();
712 if (next_timeout
.tv_sec
>= 0 && next_timeout
.tv_usec
>= 0)
713 timeout
= &next_timeout
;
717 // Wait for X Event or a Timer
718 if (select(x11_fd
+1, &fdset
, 0, 0, timeout
) > 0) {
719 while (XPending (server
.dsp
)) {
720 XNextEvent(server
.dsp
, &e
);
722 panel
= get_panel(e
.xany
.window
);
723 if (panel
&& panel_autohide
) {
724 if (e
.type
== EnterNotify
)
725 autohide_trigger_show(panel
);
726 else if (e
.type
== LeaveNotify
)
727 autohide_trigger_hide(panel
);
728 if (panel
->is_hidden
)
729 continue; // discard further processing of this event because the panel is not visible yet
735 event_button_press (&e
);
739 event_button_release(&e
);
743 if (!g_tooltip
.enabled
) break;
744 Panel
* panel
= get_panel(e
.xmotion
.window
);
745 Area
* area
= click_area(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
746 if (area
->_get_tooltip_text
)
747 tooltip_trigger_show(area
, panel
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
749 tooltip_trigger_hide();
754 if (g_tooltip
.enabled
)
755 tooltip_trigger_hide();
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
)->tray_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
);
801 if (e
.type
== XDamageNotify
+damage_event
) {
802 // union needed to avoid strict-aliasing warnings by gcc
803 union { XEvent e
; XDamageNotifyEvent de
; } event_union
= {.e
=e
};
806 XDamageNotifyEvent
* de
= &event_union
.de
;
807 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
808 traywin
= (TrayWindow
*)l
->data
;
809 if ( traywin
->id
== de
->drawable
&& !de
->more
) {
810 systray_render_icon(traywin
);
819 callback_timeout_expired();
821 switch (signal_pending
) {
822 case SIGUSR1
: // reload config file
825 config_read_file (config_path
);
This page took 0.072733 seconds and 5 git commands to generate.