]>
Dogcows Code - chaz/tint2/blob - src/tint.c
1b6c4e8988ecfb53d5227eaab397d630c8055ce6
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
, "");
113 if (time1_font_desc
) pango_font_description_free(time1_font_desc
);
114 if (time2_font_desc
) pango_font_description_free(time2_font_desc
);
115 if (time1_format
) g_free(time1_format
);
116 if (time2_format
) g_free(time2_format
);
117 #ifdef ENABLE_BATTERY
118 if (bat1_font_desc
) pango_font_description_free(bat1_font_desc
);
119 if (bat2_font_desc
) pango_font_description_free(bat2_font_desc
);
120 if (battery_low_cmd
) g_free(battery_low_cmd
);
121 if (path_energy_now
) g_free(path_energy_now
);
122 if (path_energy_full
) g_free(path_energy_full
);
123 if (path_current_now
) g_free(path_current_now
);
124 if (path_status
) g_free(path_status
);
126 if (clock_lclick_command
) g_free(clock_lclick_command
);
127 if (clock_rclick_command
) g_free(clock_rclick_command
);
128 if (config_path
) g_free(config_path
);
129 if (thumbnail_path
) g_free(thumbnail_path
);
131 if (server
.monitor
) free(server
.monitor
);
132 XFreeGC(server
.dsp
, server
.gc
);
133 XCloseDisplay(server
.dsp
);
137 Taskbar
*click_taskbar (Panel
*panel
, int x
, int y
)
142 if (panel_horizontal
) {
143 for (i
=0; i
< panel
->nb_desktop
; i
++) {
144 tskbar
= &panel
->taskbar
[i
];
145 if (!tskbar
->area
.on_screen
) continue;
146 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
151 for (i
=0; i
< panel
->nb_desktop
; i
++) {
152 tskbar
= &panel
->taskbar
[i
];
153 if (!tskbar
->area
.on_screen
) continue;
154 if (y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
162 Task
*click_task (Panel
*panel
, int x
, int y
)
167 if ( (tskbar
= click_taskbar(panel
, x
, y
)) ) {
168 if (panel_horizontal
) {
170 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
172 if (tsk
->area
.on_screen
&& x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
179 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
181 if (tsk
->area
.on_screen
&& y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
191 int click_padding(Panel
*panel
, int x
, int y
)
193 if (panel_horizontal
) {
194 if (x
< panel
->area
.paddingxlr
|| x
> panel
->area
.width
-panel
->area
.paddingxlr
)
198 if (y
< panel
->area
.paddingxlr
|| y
> panel
->area
.height
-panel
->area
.paddingxlr
)
205 int click_clock(Panel
*panel
, int x
, int y
)
207 Clock clk
= panel
->clock
;
208 if (panel_horizontal
) {
209 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
212 if (clk
.area
.on_screen
&& y
>= clk
.area
.posy
&& y
<= (clk
.area
.posy
+ clk
.area
.height
))
219 void window_action (Task
*tsk
, int action
)
225 set_close (tsk
->win
);
228 set_active(tsk
->win
);
231 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
234 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
235 else set_active (tsk
->win
);
238 window_toggle_shade (tsk
->win
);
240 case MAXIMIZE_RESTORE
:
241 window_maximize_restore (tsk
->win
);
244 window_maximize_restore (tsk
->win
);
247 window_maximize_restore (tsk
->win
);
250 if ( tsk
->desktop
== 0 ) break;
251 desk
= tsk
->desktop
- 1;
252 windows_set_desktop(tsk
->win
, desk
);
253 if (desk
== server
.desktop
)
254 set_active(tsk
->win
);
257 if (tsk
->desktop
== server
.nb_desktop
) break;
258 desk
= tsk
->desktop
+ 1;
259 windows_set_desktop(tsk
->win
, desk
);
260 if (desk
== server
.desktop
)
261 set_active(tsk
->win
);
266 void event_button_press (XEvent
*e
)
268 Panel
*panel
= get_panel(e
->xany
.window
);
271 task_drag
= click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
);
273 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
) && (e
->xbutton
.button
!= 1) ) {
274 // forward the click to the desktop window (thanks conky)
275 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
276 e
->xbutton
.window
= server
.root_win
;
277 // icewm doesn't open under the mouse.
278 // and xfce doesn't open at all.
279 //e->xbutton.x = e->xbutton.x_root;
280 //e->xbutton.y = e->xbutton.y_root;
281 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
282 XSetInputFocus(server
.dsp
, e
->xbutton
.window
, RevertToParent
, e
->xbutton
.time
);
283 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
287 XLowerWindow (server
.dsp
, panel
->main_win
);
291 void event_button_release (XEvent
*e
)
293 Panel
*panel
= get_panel(e
->xany
.window
);
296 if (wm_menu
&& click_padding(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
297 // forward the click to the desktop window (thanks conky)
298 e
->xbutton
.window
= server
.root_win
;
299 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonReleaseMask
, e
);
303 int action
= TOGGLE_ICONIFY
;
304 switch (e
->xbutton
.button
) {
306 action
= mouse_middle
;
309 action
= mouse_right
;
312 action
= mouse_scroll_up
;
315 action
= mouse_scroll_down
;
318 action
= mouse_tilt_left
;
321 action
= mouse_tilt_right
;
325 if ( click_clock(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) {
326 clock_action(e
->xbutton
.button
);
327 XLowerWindow (server
.dsp
, panel
->main_win
);
333 if ( !(tskbar
= click_taskbar(panel
, e
->xbutton
.x
, e
->xbutton
.y
)) ) {
334 // TODO: check better solution to keep window below
335 XLowerWindow (server
.dsp
, panel
->main_win
);
340 // drag and drop task
342 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
343 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
344 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
345 if (tskbar
->desktop
== server
.desktop
)
346 set_active(task_drag
->win
);
355 if (panel_mode
== MULTI_DESKTOP
) {
356 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
357 set_desktop (tskbar
->desktop
);
361 window_action( click_task(panel
, e
->xbutton
.x
, e
->xbutton
.y
), action
);
363 // to keep window below
364 XLowerWindow (server
.dsp
, panel
->main_win
);
368 void event_property_notify (XEvent
*e
)
372 Window win
= e
->xproperty
.window
;
373 Atom at
= e
->xproperty
.atom
;
375 if (win
== server
.root_win
) {
376 if (!server
.got_root_win
) {
377 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
378 server
.got_root_win
= 1;
381 // Change number of desktops
382 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
383 server
.nb_desktop
= server_get_number_of_desktop ();
387 for (i
=0 ; i
< nb_panel
; i
++) {
388 panel1
[i
].area
.resize
= 1;
390 task_refresh_tasklist();
394 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
395 int old_desktop
= server
.desktop
;
396 server
.desktop
= server_get_current_desktop ();
397 for (i
=0 ; i
< nb_panel
; i
++) {
398 Panel
*panel
= &panel1
[i
];
399 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
400 // redraw both taskbar
401 panel
->taskbar
[old_desktop
].area
.is_active
= 0;
402 panel
->taskbar
[old_desktop
].area
.redraw
= 1;
403 panel
->taskbar
[server
.desktop
].area
.is_active
= 1;
404 panel
->taskbar
[server
.desktop
].area
.redraw
= 1;
407 // check ALLDESKTOP task => resize taskbar
411 tskbar
= &panel
->taskbar
[old_desktop
];
412 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
414 if (tsk
->desktop
== ALLDESKTOP
) {
415 tsk
->area
.on_screen
= 0;
416 tskbar
->area
.resize
= 1;
420 tskbar
= &panel
->taskbar
[server
.desktop
];
421 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
423 if (tsk
->desktop
== ALLDESKTOP
) {
424 tsk
->area
.on_screen
= 1;
425 tskbar
->area
.resize
= 1;
429 if (panel_mode
!= MULTI_DESKTOP
) {
434 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
435 task_refresh_tasklist();
439 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
442 for (i
=0 ; i
< nb_panel
; i
++) {
443 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
444 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
446 tsk
->area
.is_active
= 0;
452 Window w1
= window_get_active ();
453 Task
*t
= task_get_task(w1
);
456 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
457 if (w2
) t
= task_get_task(w2
);
459 if (task_urgent
== t
) {
463 // put active state on all task (multi_desktop)
465 for (i
=0 ; i
< nb_panel
; i
++) {
466 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
467 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
469 if (tsk
->win
== t
->win
) {
470 tsk
->area
.is_active
= 1;
471 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
480 else if (at
== server
.atom
._XROOTPMAP_ID
) {
482 for (i
=0 ; i
< nb_panel
; i
++) {
483 set_panel_background(&panel1
[i
]);
489 tsk
= task_get_task (win
);
491 if ( at
!= server
.atom
._NET_WM_STATE
)
493 else if ( !(tsk
= add_task(win
)) )
496 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
498 // Window title changed
499 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
503 // changed other tsk->title
504 for (i
=0 ; i
< nb_panel
; i
++) {
505 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
506 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
508 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
509 tsk2
->title
= tsk
->title
;
510 tsk2
->area
.redraw
= 1;
518 else if (at
== server
.atom
._NET_WM_STATE
) {
519 if (window_is_urgent (win
)) {
524 if (window_is_skip_taskbar(win
)) {
529 // else if (at == server.atom.WM_STATE) {
531 // // TODO : try to delete following code
532 // if (window_is_iconified (win)) {
533 // if (task_active) {
534 // if (task_active->win == tsk->win) {
537 // for (i=0 ; i < nb_panel ; i++) {
538 // for (j=0 ; j < panel1[i].nb_desktop ; j++) {
539 // for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
541 // tsk2->area.is_active = 0;
550 // Window icon changed
551 else if (at
== server
.atom
._NET_WM_ICON
) {
555 for (i
=0 ; i
< nb_panel
; i
++) {
556 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
557 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
559 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
560 tsk2
->icon_width
= tsk
->icon_width
;
561 tsk2
->icon_height
= tsk
->icon_height
;
562 tsk2
->icon
= tsk
->icon
;
563 tsk2
->icon_active
= tsk
->icon_active
;
564 tsk2
->area
.redraw
= 1;
571 // Window desktop changed
572 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
573 int desktop
= window_get_desktop (win
);
574 int active
= tsk
->area
.is_active
;
575 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
576 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
577 if (desktop
!= tsk
->desktop
) {
579 tsk
= add_task (win
);
581 tsk
->area
.is_active
= 1;
587 else if (at
== server
.atom
.WM_HINTS
) {
588 XWMHints
* wmhints
= XGetWMHints(server
.dsp
, win
);
589 if (wmhints
&& wmhints
->flags
& XUrgencyHint
) {
597 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
602 void event_expose (XEvent
*e
)
604 if (e
->xany
.window
== g_tooltip
.window
)
608 panel
= get_panel(e
->xany
.window
);
610 // TODO : one panel_refresh per panel ?
616 void event_configure_notify (Window win
)
618 // check 'win' move in systray
621 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
622 traywin
= (TrayWindow
*)l
->data
;
623 if (traywin
->id
== win
) {
624 //printf("move tray %d\n", traywin->x);
625 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
631 // check 'win' move in another monitor
632 if (nb_panel
== 1) return;
633 if (server
.nb_monitor
== 1) return;
634 Task
*tsk
= task_get_task (win
);
637 Panel
*p
= tsk
->area
.panel
;
638 if (p
->monitor
!= window_get_monitor (win
)) {
641 if (win
== window_get_active ()) {
642 Task
*tsk
= task_get_task (win
);
643 tsk
->area
.is_active
= 1;
656 if (gettimeofday(&stv
, 0)) return;
658 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
659 time_clock
.tv_sec
= stv
.tv_sec
;
660 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
664 if (tick_urgent
< max_tick_urgent
) {
665 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
666 task_urgent
->area
.redraw
= 1;
672 #ifdef ENABLE_BATTERY
673 if (panel1
[0].battery
.area
.on_screen
) {
675 for (i
=0 ; i
< nb_panel
; i
++)
676 panel1
[i
].battery
.area
.resize
= 1;
682 for (i
=0 ; i
< nb_panel
; i
++)
683 panel1
[i
].clock
.area
.resize
= 1;
689 void dnd_message(XClientMessageEvent
*e
)
691 Panel
*panel
= get_panel(e
->window
);
692 int x
, y
, mapX
, mapY
;
694 x
= (e
->data
.l
[2] >> 16) & 0xFFFF;
695 y
= e
->data
.l
[2] & 0xFFFF;
696 XTranslateCoordinates(server
.dsp
, server
.root_win
, e
->window
, x
, y
, &mapX
, &mapY
, &child
);
697 Task
* task
= click_task(panel
, mapX
, mapY
);
699 if (task
->desktop
!= server
.desktop
)
700 set_desktop (task
->desktop
);
701 window_action(task
, TOGGLE
);
704 // send XdndStatus event to get more XdndPosition events
705 XClientMessageEvent se
;
706 se
.type
= ClientMessage
;
707 se
.window
= e
->data
.l
[0];
708 se
.message_type
= server
.atom
.XdndStatus
;
710 se
.data
.l
[0] = e
->window
; // XID of the target window
711 se
.data
.l
[1] = 0; // bit 0: accept drop bit 1: send XdndPosition events if inside rectangle
712 se
.data
.l
[2] = 0; // Rectangle x,y for which no more XdndPosition events
713 se
.data
.l
[3] = (1 << 16) | 1; // Rectangle w,h for which no more XdndPosition events
714 se
.data
.l
[4] = None
; // None = drop will not be accepted
715 XSendEvent(server
.dsp
, e
->data
.l
[0], False
, NoEventMask
, (XEvent
*)&se
);
719 int main (int argc
, char *argv
[])
733 i
= config_read_file (config_path
);
737 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
742 if (thumbnail_path
) {
743 // usage: tint2 -j <file> for internal use
744 printf("file %s\n", thumbnail_path
);
749 x11_fd
= ConnectionNumber(server
.dsp
);
750 XSync(server
.dsp
, False
);
753 // thanks to AngryLlama for the timer
754 // Create a File Description Set containing x11_fd
756 FD_SET (x11_fd
, &fd
);
761 // Wait for X Event or a Timer
762 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
763 while (XPending (server
.dsp
)) {
764 XNextEvent(server
.dsp
, &e
);
769 event_button_press (&e
);
773 event_button_release(&e
);
777 Panel
* panel
= get_panel(e
.xmotion
.window
);
778 Task
* task
= click_task(panel
, e
.xmotion
.x
, e
.xmotion
.y
);
780 tooltip_trigger_show(task
, e
.xmotion
.x_root
, e
.xmotion
.y_root
);
782 tooltip_trigger_hide();
787 tooltip_trigger_hide();
795 event_property_notify(&e
);
798 case ConfigureNotify
:
799 //XMoveWindow(dpy, fen, pos_x, pos_y);
800 //XResizeWindow(dpy, fen, largeur, hauteur);
801 if (e
.xconfigure
.window
== server
.root_win
)
804 event_configure_notify (e
.xconfigure
.window
);
808 if (!systray
.area
.on_screen
)
810 panel
= (Panel
*)systray
.area
.panel
;
811 if (e
.xany
.window
== panel
->main_win
) // reparented to us
813 // FIXME: 'reparent to us' badly detected => disabled
817 if (!systray
.area
.on_screen
)
819 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
820 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
821 remove_icon((TrayWindow
*)it
->data
);
828 if (!systray
.area
.on_screen
) break;
829 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
830 net_message(&e
.xclient
);
832 else if (e
.xclient
.message_type
== server
.atom
.XdndPosition
) {
833 dnd_message(&e
.xclient
);
841 switch (signal_pending
) {
855 if (refresh_systray
) {
856 panel
= (Panel
*)systray
.area
.panel
;
857 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
859 for (i
=0 ; i
< nb_panel
; i
++) {
862 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
863 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
865 refresh(&panel
->area
);
866 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
870 if (refresh_systray
) {
872 panel
= (Panel
*)systray
.area
.panel
;
873 // tint2 doen't draw systray icons. it just redraw background.
874 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
875 // force icon's refresh
876 refresh_systray_icon();
This page took 0.071415 seconds and 4 git commands to generate.