]>
Dogcows Code - chaz/tint2/blob - src/tint.c
3696911387bbff9813b4dbb796905f8c70c47cf5
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 while ((c
= getopt(argc
, argv
, "c:j:v")) != -1) {
58 config_path
= strdup (optarg
);
61 thumbnail_path
= strdup (optarg
);
64 printf("tint2 version 0.7-svn\n");
71 signal(SIGUSR1
, signal_handler
);
72 signal(SIGINT
, signal_handler
);
73 signal(SIGTERM
, signal_handler
);
74 signal(SIGHUP
, signal_handler
);
75 signal(SIGCHLD
, SIG_IGN
); // don't have to wait() after fork()
76 signal(SIGALRM
, tooltip_sighandler
);
79 memset(&server
, 0, sizeof(Server_global
));
80 memset(&systray
, 0, sizeof(Systraybar
));
82 server
.dsp
= XOpenDisplay (NULL
);
84 fprintf(stderr
, "tint2 exit : could not open display.\n");
88 server
.screen
= DefaultScreen (server
.dsp
);
89 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
90 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
91 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
92 server
.desktop
= server_get_current_desktop ();
94 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
) ;
96 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
98 imlib_context_set_display (server
.dsp
);
99 imlib_context_set_visual (server
.visual
);
100 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
103 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
105 setlocale (LC_ALL
, "");
110 const gchar
* const *data_dirs
;
111 data_dirs
= g_get_system_data_dirs ();
112 for (i
= 0; data_dirs
[i
] != NULL
; i
++) {
113 path
= g_build_filename(data_dirs
[i
], "tint2", "default_icon.png", NULL
);
114 if (g_file_test (path
, G_FILE_TEST_EXISTS
))
115 default_icon
= imlib_load_image(path
);
119 // get monitor and desktop config
132 #ifdef ENABLE_BATTERY
137 imlib_context_set_image(default_icon
);
140 if (config_path
) g_free(config_path
);
141 if (thumbnail_path
) g_free(thumbnail_path
);
143 if (server
.monitor
) free(server
.monitor
);
144 XFreeGC(server
.dsp
, server
.gc
);
145 XCloseDisplay(server
.dsp
);
149 Taskbar
*click_taskbar (Panel
*panel
, int x
, int y
)
154 if (panel_horizontal
) {
155 for (i
=0; i
< panel
->nb_desktop
; i
++) {
156 tskbar
= &panel
->taskbar
[i
];
157 if (tskbar
->area
.on_screen
&& x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
162 for (i
=0; i
< panel
->nb_desktop
; i
++) {
163 tskbar
= &panel
->taskbar
[i
];
164 if (tskbar
->area
.on_screen
&& y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
172 Task
*click_task (Panel
*panel
, int x
, int y
)
177 if ( (tskbar
= click_taskbar(panel
, x
, y
)) ) {
178 if (panel_horizontal
) {
180 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
182 if (tsk
->area
.on_screen
&& x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
189 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
191 if (tsk
->area
.on_screen
&& y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
201 int click_padding(Panel
*panel
, int x
, int y
)
203 if (panel_horizontal
) {
204 if (x
< panel
->area
.paddingxlr
|| x
> panel
->area
.width
-panel
->area
.paddingxlr
)
208 if (y
< panel
->area
.paddingxlr
|| y
> panel
->area
.height
-panel
->area
.paddingxlr
)
215 int click_clock(Panel
*panel
, int x
, int y
)
217 Clock clk
= panel
->clock
;
218 if (panel_horizontal
) {
219 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
222 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
229 void window_action (Task
*tsk
, int action
)
235 set_close (tsk
->win
);
238 set_active(tsk
->win
);
241 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
244 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
245 else set_active (tsk
->win
);
248 window_toggle_shade (tsk
->win
);
250 case MAXIMIZE_RESTORE
:
251 window_maximize_restore (tsk
->win
);
254 window_maximize_restore (tsk
->win
);
257 window_maximize_restore (tsk
->win
);
260 if ( tsk
->desktop
== 0 ) break;
261 desk
= tsk
->desktop
- 1;
262 windows_set_desktop(tsk
->win
, desk
);
263 if (desk
== server
.desktop
)
264 set_active(tsk
->win
);
267 if (tsk
->desktop
== server
.nb_desktop
) break;
268 desk
= tsk
->desktop
+ 1;
269 windows_set_desktop(tsk
->win
, desk
);
270 if (desk
== server
.desktop
)
271 set_active(tsk
->win
);
276 void event_button_press (XEvent
*e
)
278 Panel
*panel
= get_panel(e
->xany
.window
);
281 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
283 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
) && (e
->xbutton
.button
!= 1) ) {
284 // forward the click to the desktop window (thanks conky)
285 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
286 e
->xbutton
.window
= server
.root_win
;
287 // icewm doesn't open under the mouse.
288 // and xfce doesn't open at all.
289 e
->xbutton
.x
= e
->xbutton
.x_root
;
290 e
->xbutton
.y
= e
->xbutton
.y_root
;
291 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
292 //XSetInputFocus(server.dsp, e->xbutton.window, RevertToParent, e->xbutton.time);
293 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
297 XLowerWindow (server
.dsp
, panel
->main_win
);
301 void event_button_release (XEvent
*e
)
303 Panel
*panel
= get_panel(e
->xany
.window
);
306 int action
= TOGGLE_ICONIFY
;
307 switch (e
->xbutton
.button
) {
309 action
= mouse_middle
;
312 action
= mouse_right
;
315 action
= mouse_scroll_up
;
318 action
= mouse_scroll_down
;
321 action
= mouse_tilt_left
;
324 action
= mouse_tilt_right
;
328 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
329 clock_action(e
->xbutton
.button
);
330 XLowerWindow (server
.dsp
, panel
->main_win
);
336 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
337 // TODO: check better solution to keep window below
338 XLowerWindow (server
.dsp
, panel
->main_win
);
343 // drag and drop task
345 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
346 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
347 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
348 if (tskbar
->desktop
== server
.desktop
)
349 set_active(task_drag
->win
);
358 if (panel_mode
== MULTI_DESKTOP
) {
359 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
360 set_desktop (tskbar
->desktop
);
364 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
366 // to keep window below
367 XLowerWindow (server
.dsp
, panel
->main_win
);
371 void event_property_notify (XEvent
*e
)
375 Window win
= e
->xproperty
.window
;
376 Atom at
= e
->xproperty
.atom
;
378 if (win
== server
.root_win
) {
379 if (!server
.got_root_win
) {
380 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
381 server
.got_root_win
= 1;
384 // Change number of desktops
385 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
386 server
.nb_desktop
= server_get_number_of_desktop ();
390 for (i
=0 ; i
< nb_panel
; i
++) {
391 panel1
[i
].area
.resize
= 1;
393 task_refresh_tasklist();
397 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
398 int old_desktop
= server
.desktop
;
399 server
.desktop
= server_get_current_desktop ();
400 for (i
=0 ; i
< nb_panel
; i
++) {
401 Panel
*panel
= &panel1
[i
];
402 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
403 // redraw both taskbar
404 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
405 panel
->taskbar
[old_desktop
].area
.resize
= 1;
406 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
407 panel
->taskbar
[server
.desktop
].area
.resize
= 1;
410 // check ALLDESKTOP task => resize taskbar
414 tskbar
= &panel
->taskbar
[old_desktop
];
415 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
417 if (tsk
->desktop
== ALLDESKTOP
) {
418 tsk
->area
.on_screen
= 0;
419 tskbar
->area
.resize
= 1;
423 tskbar
= &panel
->taskbar
[server
.desktop
];
424 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
426 if (tsk
->desktop
== ALLDESKTOP
) {
427 tsk
->area
.on_screen
= 1;
428 tskbar
->area
.resize
= 1;
432 if (panel_mode
!= MULTI_DESKTOP
) {
437 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
438 task_refresh_tasklist();
442 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
446 else if (at
== server
.atom
._XROOTPMAP_ID
) {
448 for (i
=0 ; i
< nb_panel
; i
++) {
449 set_panel_background(&panel1
[i
]);
455 tsk
= task_get_task (win
);
457 if ( at
!= server
.atom
._NET_WM_STATE
)
459 else if ( !(tsk
= add_task(win
)) )
462 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
464 // Window title changed
465 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
469 // changed other tsk->title
470 for (i
=0 ; i
< nb_panel
; i
++) {
471 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
472 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
474 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
475 tsk2
->title
= tsk
->title
;
476 tsk2
->area
.redraw
= 1;
484 else if (at
== server
.atom
._NET_WM_STATE
) {
485 if (window_is_urgent (win
)) {
490 if (window_is_skip_taskbar(win
)) {
495 // We do not check for the iconified state, since it only unsets our active window
496 // but in openbox a shaded window is considered iconified. So we would loose the active window
497 // property on unshading it again (commented 01.10.2009)
498 // else if (at == server.atom.WM_STATE) {
500 // // TODO : try to delete following code
501 // if (window_is_iconified (win)) {
502 // if (task_active) {
503 // if (task_active->win == tsk->win) {
506 // for (i=0 ; i < nb_panel ; i++) {
507 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
508 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
510 // tsk2->area.is_active = 0;
519 // Window icon changed
520 else if (at
== server
.atom
._NET_WM_ICON
) {
524 for (i
=0 ; i
< nb_panel
; i
++) {
525 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
526 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
528 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
529 tsk2
->icon_width
= tsk
->icon_width
;
530 tsk2
->icon_height
= tsk
->icon_height
;
531 tsk2
->icon
= tsk
->icon
;
532 tsk2
->icon_active
= tsk
->icon_active
;
533 tsk2
->area
.redraw
= 1;
540 // Window desktop changed
541 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
542 int desktop
= window_get_desktop (win
);
543 int active
= tsk
->area
.is_active
;
544 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
545 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
546 if (desktop
!= tsk
->desktop
) {
548 tsk
= add_task (win
);
550 tsk
->area
.is_active
= 1;
556 else if (at
== server
.atom
.WM_HINTS
) {
557 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
558 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
566 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
571 void event_expose (XEvent
*e
)
573 if (e
->xany
.window
== g_tooltip
.window
)
577 panel
= get_panel(e
->xany
.window
);
579 // TODO : one panel_refresh per panel ?
585 void event_configure_notify (Window win
)
587 // change in root window (xrandr)
588 if (win
== server
.root_win
) {
591 config_read_file (config_path
);
597 // 'win' is a trayer icon
600 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
601 traywin
= (TrayWindow
*)l
->data
;
602 if (traywin
->id
== win
) {
603 //printf("move tray %d\n", traywin->x);
604 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
610 // 'win' move in another monitor
611 if (nb_panel
== 1) return;
612 Task
*tsk
= task_get_task (win
);
615 Panel
*p
= tsk
->area
.panel
;
616 if (p
->monitor
!= window_get_monitor (win
)) {
619 if (win
== window_get_active ()) {
620 Task
*tsk
= task_get_task (win
);
621 tsk
->area
.is_active
= 1;
634 if (gettimeofday(&stv
, 0)) return;
636 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
637 time_clock
.tv_sec
= stv
.tv_sec
;
638 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
642 if (tick_urgent
< max_tick_urgent
) {
643 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
644 task_urgent
->area
.redraw
= 1;
650 #ifdef ENABLE_BATTERY
651 if (battery_enabled
) {
653 for (i
=0 ; i
< nb_panel
; i
++)
654 panel1
[i
].battery
.area
.resize
= 1;
660 for (i
=0 ; i
< nb_panel
; i
++)
661 panel1
[i
].clock
.area
.resize
= 1;
667 void dnd_message(XClientMessageEvent
*e
)
669 Panel
*panel
= get_panel(e
->window
);
670 int x
, y
, mapX
, mapY
;
672 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
673 y
= e
->data
.l
[2] & 0xFFFF;
674 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
675 Task
* task
= click_task(panel
, mapX
, mapY
);
677 if (task
->desktop
!= server
.desktop
)
678 set_desktop (task
->desktop
);
679 window_action(task
, TOGGLE
);
682 // send XdndStatus event to get more XdndPosition events
683 XClientMessageEvent se
;
684 se
.type
= ClientMessage
;
685 se
.window
= e
->data
.l
[0];
686 se
.message_type
= server
.atom
.XdndStatus
;
688 se
.data
.l
[0] = e
->window
; // XID of the target window
689 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
690 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
691 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
692 se
.data
.l
[4] = None
; // None = drop will not be accepted
693 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
697 int main (int argc
, char *argv
[])
711 i
= config_read_file (config_path
);
715 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
721 if (thumbnail_path
) {
722 // usage: tint2 -j <file> for internal use
723 printf("file %s\n", thumbnail_path
);
728 x11_fd
= ConnectionNumber(server
.dsp
);
729 XSync(server
.dsp
, False
);
732 // thanks to AngryLlama for the timer
733 // Create a File Description Set containing x11_fd
735 FD_SET (x11_fd
, &fd
);
740 // Wait for X Event or a Timer
741 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
742 while (XPending (server
.dsp
)) {
743 XNextEvent(server
.dsp
, &e
);
748 event_button_press (&e
);
752 event_button_release(&e
);
756 if (!g_tooltip
.enabled
) break;
757 Panel
* panel
= get_panel(e
.xmotion
.window
);
758 Task
* task
= click_task(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
760 tooltip_trigger_show(task
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
762 tooltip_trigger_hide();
767 tooltip_trigger_hide();
775 event_property_notify(&e
);
778 case ConfigureNotify
:
779 event_configure_notify (e
.xconfigure
.window
);
783 if (!systray_enabled
)
785 panel
= (Panel
*)systray
.area
.panel
;
786 if (e
.xany
.window
== panel
->main_win
) // reparented to us
788 // FIXME: 'reparent to us' badly detected => disabled
792 if (!systray
.area
.on_screen
)
794 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
795 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
796 remove_icon((TrayWindow
*)it
->data
);
803 if (!systray
.area
.on_screen
) break;
804 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
805 net_message(&e
.xclient
);
807 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
808 dnd_message(&e
.xclient
);
816 switch (signal_pending
) {
817 case SIGUSR1
: // reload config file
820 config_read_file (config_path
);
834 if (refresh_systray
) {
835 panel
= (Panel
*)systray
.area
.panel
;
836 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
838 for (i
=0 ; i
< nb_panel
; i
++) {
841 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
842 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
844 refresh(&panel
->area
);
845 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
849 if (refresh_systray
) {
851 panel
= (Panel
*)systray
.area
.panel
;
852 // tint2 doen't draw systray icons. it just redraw background.
853 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
854 // force icon's refresh
855 refresh_systray_icon();
This page took 0.071639 seconds and 4 git commands to generate.