]>
Dogcows Code - chaz/tint2/blob - tint.c
05d50f67c14e085d3c4ec6117c8ab29b77045d07
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 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 void event_button_press (XEvent
*e
)
232 Panel
*panel
= get_panel(e
->xany
.window
);
235 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
237 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
) && (e
->xbutton
.button
!= 1) ) {
238 // forward the click to the desktop window (thanks conky)
239 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
240 e
->xbutton
.window
= server
.root_win
;
241 // icewm doesn't open under the mouse.
242 // and xfce doesn't open at all.
243 e
->xbutton
.x
= e
->xbutton
.x_root
;
244 e
->xbutton
.y
= e
->xbutton
.y_root
;
245 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
246 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
247 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
251 XLowerWindow (server
.dsp
, panel
->main_win
);
255 void event_button_release (XEvent
*e
)
257 Panel
*panel
= get_panel(e
->xany
.window
);
260 int action
= TOGGLE_ICONIFY
;
261 switch (e
->xbutton
.button
) {
263 action
= mouse_middle
;
266 action
= mouse_right
;
269 action
= mouse_scroll_up
;
272 action
= mouse_scroll_down
;
275 action
= mouse_tilt_left
;
278 action
= mouse_tilt_right
;
282 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
283 clock_action(e
->xbutton
.button
);
284 XLowerWindow (server
.dsp
, panel
->main_win
);
290 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
291 // TODO: check better solution to keep window below
292 XLowerWindow (server
.dsp
, panel
->main_win
);
297 // drag and drop task
299 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
300 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
301 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
302 if (tskbar
->desktop
== server
.desktop
)
303 set_active(task_drag
->win
);
312 if (panel_mode
== MULTI_DESKTOP
) {
313 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
314 set_desktop (tskbar
->desktop
);
318 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
320 // to keep window below
321 XLowerWindow (server
.dsp
, panel
->main_win
);
325 void event_property_notify (XEvent
*e
)
329 Window win
= e
->xproperty
.window
;
330 Atom at
= e
->xproperty
.atom
;
332 if (win
== server
.root_win
) {
333 if (!server
.got_root_win
) {
334 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
335 server
.got_root_win
= 1;
338 // Change number of desktops
339 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
340 server
.nb_desktop
= server_get_number_of_desktop ();
344 for (i
=0 ; i
< nb_panel
; i
++) {
345 panel1
[i
].area
.resize
= 1;
347 task_refresh_tasklist();
351 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
352 int old_desktop
= server
.desktop
;
353 server
.desktop
= server_get_current_desktop ();
354 for (i
=0 ; i
< nb_panel
; i
++) {
355 Panel
*panel
= &panel1
[i
];
356 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
357 // redraw both taskbar
358 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
359 panel
->taskbar
[old_desktop
].area
.resize
= 1;
360 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
361 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
364 // check ALLDESKTOP task => resize taskbar
368 tskbar
= &panel
->taskbar
[old_desktop
];
369 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
371 if (tsk
->desktop
== ALLDESKTOP
) {
372 tsk
->area
.on_screen
= 0;
373 tskbar
->area
.resize
= 1;
377 tskbar
= &panel
->taskbar
[server
.desktop
];
378 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
380 if (tsk
->desktop
== ALLDESKTOP
) {
381 tsk
->area
.on_screen
= 1;
382 tskbar
->area
.resize
= 1;
386 if (panel_mode
!= MULTI_DESKTOP
) {
391 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
392 task_refresh_tasklist();
396 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
400 else if (at
== server
.atom
._XROOTPMAP_ID
) {
402 for (i
=0 ; i
< nb_panel
; i
++) {
403 set_panel_background(&panel1
[i
]);
409 tsk
= task_get_task (win
);
411 if (at
!= server
.atom
._NET_WM_STATE
)
414 // xfce4 sends _NET_WM_STATE after minimized to tray, so we need to check if window is mapped
415 // if it is mapped and not set as skip_taskbar, we must add it to our task list
416 XWindowAttributes wa
;
417 XGetWindowAttributes(server
.dsp
, win
, &wa
);
418 if (wa
.map_state
== IsViewable
&& !window_is_skip_taskbar(win
)) {
419 if ( (tsk
= add_task(win
)) )
426 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
428 // Window title changed
429 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
433 // changed other tsk->title
434 for (i
=0 ; i
< nb_panel
; i
++) {
435 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
436 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
438 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
439 tsk2
->title
= tsk
->title
;
440 tsk2
->area
.redraw
= 1;
448 else if (at
== server
.atom
._NET_WM_STATE
) {
449 if (window_is_urgent (win
)) {
452 if (window_is_skip_taskbar(win
)) {
457 // We do not check for the iconified state, since it only unsets our active window
458 // but in openbox a shaded window is considered iconified. So we would loose the active window
459 // property on unshading it again (commented 01.10.2009)
460 // else if (at == server.atom.WM_STATE) {
462 // // TODO : try to delete following code
463 // if (window_is_iconified (win)) {
464 // if (task_active) {
465 // if (task_active->win == tsk->win) {
468 // for (i=0 ; i < nb_panel ; i++) {
469 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
470 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
472 // tsk2->area.is_active = 0;
481 // Window icon changed
482 else if (at
== server
.atom
._NET_WM_ICON
) {
486 for (i
=0 ; i
< nb_panel
; i
++) {
487 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
488 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
490 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
491 tsk2
->icon_width
= tsk
->icon_width
;
492 tsk2
->icon_height
= tsk
->icon_height
;
493 tsk2
->icon
= tsk
->icon
;
494 tsk2
->icon_active
= tsk
->icon_active
;
495 tsk2
->area
.redraw
= 1;
502 // Window desktop changed
503 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
504 int desktop
= window_get_desktop (win
);
505 int active
= tsk
->area
.is_active
;
506 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
507 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
508 if (desktop
!= tsk
->desktop
) {
510 tsk
= add_task (win
);
512 tsk
->area
.is_active
= 1;
518 else if (at
== server
.atom
.WM_HINTS
) {
519 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
520 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
526 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
531 void event_expose (XEvent
*e
)
534 panel
= get_panel(e
->xany
.window
);
536 // TODO : one panel_refresh per panel ?
541 void event_configure_notify (Window win
)
543 // change in root window (xrandr)
544 if (win
== server
.root_win
) {
547 config_read_file (config_path
);
553 // 'win' is a trayer icon
556 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
557 traywin
= (TrayWindow
*)l
->data
;
558 if (traywin
->id
== win
) {
559 //printf("move tray %d\n", traywin->x);
560 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
566 // 'win' move in another monitor
567 if (nb_panel
== 1) return;
568 Task
*tsk
= task_get_task (win
);
571 Panel
*p
= tsk
->area
.panel
;
572 if (p
->monitor
!= window_get_monitor (win
)) {
575 if (win
== window_get_active ()) {
576 Task
*tsk
= task_get_task (win
);
577 tsk
->area
.is_active
= 1;
585 void dnd_message(XClientMessageEvent
*e
)
587 Panel
*panel
= get_panel(e
->window
);
588 int x
, y
, mapX
, mapY
;
590 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
591 y
= e
->data
.l
[2] & 0xFFFF;
592 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
593 Task
* task
= click_task(panel
, mapX
, mapY
);
595 if (task
->desktop
!= server
.desktop
)
596 set_desktop (task
->desktop
);
597 window_action(task
, TOGGLE
);
600 // send XdndStatus event to get more XdndPosition events
601 XClientMessageEvent se
;
602 se
.type
= ClientMessage
;
603 se
.window
= e
->data
.l
[0];
604 se
.message_type
= server
.atom
.XdndStatus
;
606 se
.data
.l
[0] = e
->window
; // XID of the target window
607 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
608 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
609 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
610 se
.data
.l
[4] = None
; // None = drop will not be accepted
611 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
615 int main (int argc
, char *argv
[])
630 i
= config_read_file (config_path
);
634 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
641 get_snapshot(snapshot_path
);
646 x11_fd
= ConnectionNumber(server
.dsp
);
647 XSync(server
.dsp
, False
);
650 sigemptyset(&empty_mask
);
656 if (refresh_systray
) {
657 panel
= (Panel
*)systray
.area
.panel
;
658 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
660 for (i
=0 ; i
< nb_panel
; i
++) {
663 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
664 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
666 refresh(&panel
->area
);
667 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
671 if (refresh_systray
) {
673 panel
= (Panel
*)systray
.area
.panel
;
674 // tint2 doen't draw systray icons. it just redraw background.
675 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
676 // force icon's refresh
677 refresh_systray_icon();
681 // thanks to AngryLlama for the timer
682 // Create a File Description Set containing x11_fd, and every timer_fd
684 FD_SET (x11_fd
, &fdset
);
686 timer_iter
= timer_list
;
688 timer
= timer_iter
->data
;
689 max_fd
= timer
->id
> max_fd
? timer
->id
: max_fd
;
690 FD_SET(timer
->id
, &fdset
);
691 timer_iter
= timer_iter
->next
;
694 // Wait for X Event or a Timer
695 if (pselect(max_fd
+1, &fdset
, 0, 0, 0, &empty_mask
) > 0) {
696 while (XPending (server
.dsp
)) {
697 XNextEvent(server
.dsp
, &e
);
702 event_button_press (&e
);
706 event_button_release(&e
);
710 if (!g_tooltip
.enabled
) break;
711 Panel
* panel
= get_panel(e
.xmotion
.window
);
712 Area
* area
= click_area(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
713 if (area
->_get_tooltip_text
) {
714 tooltip_trigger_show(area
, panel
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
717 tooltip_trigger_hide();
722 tooltip_trigger_hide();
730 if (e
.xany
.window
== g_tooltip
.window
)
735 event_property_notify(&e
);
738 case ConfigureNotify
:
739 event_configure_notify (e
.xconfigure
.window
);
743 if (!systray_enabled
)
745 panel
= (Panel
*)systray
.area
.panel
;
746 if (e
.xany
.window
== panel
->main_win
) // reparented to us
748 // FIXME: 'reparent to us' badly detected => disabled
752 if (e
.xany
.window
== g_tooltip
.window
|| !systray
.area
.on_screen
)
754 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
755 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
756 remove_icon((TrayWindow
*)it
->data
);
763 if (!systray
.area
.on_screen
) break;
764 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
765 net_message(&e
.xclient
);
767 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
768 dnd_message(&e
.xclient
);
774 // we need to iterate over the whole timer list, since fd_set can only be checked with the
775 // brute force method FD_ISSET for every possible timer
776 timer_iter
= timer_list
;
778 timer
= timer_iter
->data
;
779 if (FD_ISSET(timer
->id
, &fdset
)) {
781 read(timer
->id
, &dummy
, sizeof(uint64_t));
784 timer_iter
= timer_iter
->next
;
788 switch (signal_pending
) {
789 case SIGUSR1
: // reload config file
792 config_read_file (config_path
);
This page took 0.074264 seconds and 3 git commands to generate.