]>
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>
38 #include "systraybar.h"
42 void signal_handler(int sig
)
44 // signal handler is light as it should be
49 void init (int argc
, char *argv
[])
54 c
= getopt (argc
, argv
, "c:");
56 config_path
= strdup (optarg
);
57 c
= getopt (argc
, argv
, "j:");
59 thumbnail_path
= strdup (optarg
);
63 signal(SIGUSR1
, signal_handler
);
64 signal(SIGINT
, signal_handler
);
65 signal(SIGTERM
, signal_handler
);
66 signal(SIGHUP
, signal_handler
);
67 signal(SIGCHLD
, SIG_IGN
); // don't have to wait() after fork()
70 memset(&server
, 0, sizeof(Server_global
));
71 memset(&systray
, 0, sizeof(Systraybar
));
73 server
.dsp
= XOpenDisplay (NULL
);
75 fprintf(stderr
, "tint2 exit : could not open display.\n");
79 server
.screen
= DefaultScreen (server
.dsp
);
80 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
81 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
82 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
83 server
.desktop
= server_get_current_desktop ();
85 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
) ;
87 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
89 imlib_context_set_display (server
.dsp
);
90 imlib_context_set_visual (server
.visual
);
91 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
94 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
96 setlocale (LC_ALL
, "");
104 if (time1_font_desc
) pango_font_description_free(time1_font_desc
);
105 if (time2_font_desc
) pango_font_description_free(time2_font_desc
);
106 if (time1_format
) g_free(time1_format
);
107 if (time2_format
) g_free(time2_format
);
108 #ifdef ENABLE_BATTERY
109 if (bat1_font_desc
) pango_font_description_free(bat1_font_desc
);
110 if (bat2_font_desc
) pango_font_description_free(bat2_font_desc
);
111 if (battery_low_cmd
) g_free(battery_low_cmd
);
112 if (path_energy_now
) g_free(path_energy_now
);
113 if (path_energy_full
) g_free(path_energy_full
);
114 if (path_current_now
) g_free(path_current_now
);
115 if (path_status
) g_free(path_status
);
117 if (clock_lclick_command
) g_free(clock_lclick_command
);
118 if (clock_rclick_command
) g_free(clock_rclick_command
);
119 if (config_path
) g_free(config_path
);
120 if (thumbnail_path
) g_free(thumbnail_path
);
122 if (server
.monitor
) free(server
.monitor
);
123 XFreeGC(server
.dsp
, server
.gc
);
124 XCloseDisplay(server
.dsp
);
128 Taskbar
*click_taskbar (Panel
*panel
, XEvent
*e
)
131 Taskbar
*tskbar
= NULL
;
132 if (panel_horizontal
) {
133 int x
= e
->xbutton
.x
;
134 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
136 if (!tskbar
->area
.on_screen
) continue;
137 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
142 int y
= e
->xbutton
.y
;
143 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
145 if (!tskbar
->area
.on_screen
) continue;
146 if (y
>= tskbar
->area
.posy
&& y
<= (tskbar
->area
.posy
+ tskbar
->area
.height
))
154 Task
*click_task (Panel
*panel
, XEvent
*e
)
159 if ( (tskbar
= click_taskbar(panel
, e
)) ) {
160 if (panel_horizontal
) {
161 int x
= e
->xbutton
.x
;
163 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
165 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
171 int y
= e
->xbutton
.y
;
173 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
175 if (y
>= tsk
->area
.posy
&& y
<= (tsk
->area
.posy
+ tsk
->area
.height
)) {
185 int click_padding(Panel
*panel
, XEvent
*e
)
187 if (panel_horizontal
) {
188 if (e
->xbutton
.x
< panel
->area
.paddingxlr
|| e
->xbutton
.x
> panel
->area
.width
-panel
->area
.paddingxlr
)
192 if (e
->xbutton
.y
< panel
->area
.paddingxlr
|| e
->xbutton
.y
> panel
->area
.height
-panel
->area
.paddingxlr
)
199 int click_clock(Panel
*panel
, XEvent
*e
)
201 Clock clk
= panel
->clock
;
202 if (panel_horizontal
) {
203 if (clk
.area
.on_screen
&& e
->xbutton
.x
>= clk
.area
.posx
&& e
->xbutton
.x
<= (clk
.area
.posx
+ clk
.area
.width
))
206 if (clk
.area
.on_screen
&& e
->xbutton
.y
>= clk
.area
.posy
&& e
->xbutton
.y
<= (clk
.area
.posy
+ clk
.area
.height
))
213 void window_action (Task
*tsk
, int action
)
219 set_close (tsk
->win
);
222 set_active(tsk
->win
);
225 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
228 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
229 else set_active (tsk
->win
);
232 window_toggle_shade (tsk
->win
);
234 case MAXIMIZE_RESTORE
:
235 window_maximize_restore (tsk
->win
);
238 window_maximize_restore (tsk
->win
);
241 window_maximize_restore (tsk
->win
);
244 if ( tsk
->desktop
== 0 ) break;
245 desk
= tsk
->desktop
- 1;
246 windows_set_desktop(tsk
->win
, desk
);
247 if (desk
== server
.desktop
)
248 set_active(tsk
->win
);
251 if (tsk
->desktop
== server
.nb_desktop
) break;
252 desk
= tsk
->desktop
+ 1;
253 windows_set_desktop(tsk
->win
, desk
);
254 if (desk
== server
.desktop
)
255 set_active(tsk
->win
);
260 void event_button_press (XEvent
*e
)
262 Panel
*panel
= get_panel(e
->xany
.window
);
265 if (panel_mode
== MULTI_DESKTOP
)
266 task_drag
= click_task(panel
, e
);
268 if (wm_menu
&& !task_drag
&& !click_clock(panel
, e
) && (e
->xbutton
.button
!= 1) ) {
269 // forward the click to the desktop window (thanks conky)
270 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
271 e
->xbutton
.window
= server
.root_win
;
272 // icewm doesn't open under the mouse.
273 // and xfce doesn't open at all.
274 //e->xbutton.x = e->xbutton.x_root;
275 //e->xbutton.y = e->xbutton.y_root;
276 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
277 XSetInputFocus(server
.dsp
, e
->xbutton
.window
, RevertToParent
, e
->xbutton
.time
);
278 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
282 XLowerWindow (server
.dsp
, panel
->main_win
);
286 void event_button_release (XEvent
*e
)
288 Panel
*panel
= get_panel(e
->xany
.window
);
291 if (wm_menu
&& click_padding(panel
, e
)) {
292 // forward the click to the desktop window (thanks conky)
293 e
->xbutton
.window
= server
.root_win
;
294 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonReleaseMask
, e
);
298 int action
= TOGGLE_ICONIFY
;
299 switch (e
->xbutton
.button
) {
301 action
= mouse_middle
;
304 action
= mouse_right
;
307 action
= mouse_scroll_up
;
310 action
= mouse_scroll_down
;
313 action
= mouse_tilt_left
;
316 action
= mouse_tilt_right
;
320 if ( click_clock(panel
, e
)) {
321 clock_action(e
->xbutton
.button
);
322 XLowerWindow (server
.dsp
, panel
->main_win
);
328 if ( !(tskbar
= click_taskbar(panel
, e
)) ) {
329 // TODO: check better solution to keep window below
330 XLowerWindow (server
.dsp
, panel
->main_win
);
335 // drag and drop task
337 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
338 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
339 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
340 if (tskbar
->desktop
== server
.desktop
)
341 set_active(task_drag
->win
);
350 if (panel_mode
== MULTI_DESKTOP
) {
351 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
&& action
!= DESKTOP_LEFT
&& action
!= DESKTOP_RIGHT
)
352 set_desktop (tskbar
->desktop
);
356 window_action( click_task(panel
, e
), action
);
358 // to keep window below
359 XLowerWindow (server
.dsp
, panel
->main_win
);
363 void event_property_notify (XEvent
*e
)
367 Window win
= e
->xproperty
.window
;
368 Atom at
= e
->xproperty
.atom
;
370 if (win
== server
.root_win
) {
371 if (!server
.got_root_win
) {
372 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
373 server
.got_root_win
= 1;
376 // Change number of desktops
377 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
378 server
.nb_desktop
= server_get_number_of_desktop ();
382 for (i
=0 ; i
< nb_panel
; i
++) {
383 panel1
[i
].area
.resize
= 1;
385 task_refresh_tasklist();
389 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
390 server
.desktop
= server_get_current_desktop ();
391 if (panel_mode
!= MULTI_DESKTOP
) {
396 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
397 task_refresh_tasklist();
401 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
404 for (i
=0 ; i
< nb_panel
; i
++) {
405 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
406 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
408 tsk
->area
.is_active
= 0;
414 Window w1
= window_get_active ();
415 Task
*t
= task_get_task(w1
);
418 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
419 if (w2
) t
= task_get_task(w2
);
421 if (task_urgent
== t
) {
425 // put active state on all task (multi_desktop)
427 for (i
=0 ; i
< nb_panel
; i
++) {
428 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
429 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
431 if (tsk
->win
== t
->win
) {
432 tsk
->area
.is_active
= 1;
433 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
442 else if (at
== server
.atom
._XROOTPMAP_ID
) {
444 for (i
=0 ; i
< nb_panel
; i
++) {
445 set_panel_background(&panel1
[i
]);
451 tsk
= task_get_task (win
);
453 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
455 // Window title changed
456 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
460 // changed other tsk->title
461 for (i
=0 ; i
< nb_panel
; i
++) {
462 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
463 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
465 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
466 tsk2
->title
= tsk
->title
;
467 tsk2
->area
.redraw
= 1;
475 else if (at
== server
.atom
._NET_WM_STATE
) {
476 if (window_is_urgent (win
)) {
482 else if (at
== server
.atom
.WM_STATE
) {
484 // TODO : try to delete following code
485 if (window_is_iconified (win
)) {
487 if (task_active
->win
== tsk
->win
) {
490 for (i
=0 ; i
< nb_panel
; i
++) {
491 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
492 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
494 tsk2
->area
.is_active
= 0;
503 // Window icon changed
504 else if (at
== server
.atom
._NET_WM_ICON
) {
508 for (i
=0 ; i
< nb_panel
; i
++) {
509 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
510 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
512 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
513 tsk2
->icon_width
= tsk
->icon_width
;
514 tsk2
->icon_height
= tsk
->icon_height
;
515 tsk2
->icon_data
= tsk
->icon_data
;
516 tsk2
->icon_data_active
= tsk
->icon_data_active
;
517 tsk2
->area
.redraw
= 1;
524 // Window desktop changed
525 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
526 int desktop
= window_get_desktop (win
);
527 int active
= tsk
->area
.is_active
;
528 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
529 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
530 if (desktop
!= tsk
->desktop
) {
532 tsk
= add_task (win
);
534 tsk
->area
.is_active
= 1;
541 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
546 void event_expose (XEvent
*e
)
550 panel
= get_panel(e
->xany
.window
);
552 // TODO : one panel_refresh per panel ?
557 void event_configure_notify (Window win
)
559 // check 'win' move in systray
562 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
563 traywin
= (TrayWindow
*)l
->data
;
564 if (traywin
->id
== win
) {
565 //printf("move tray %d\n", traywin->x);
566 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
572 // check 'win' move in another monitor
573 if (nb_panel
== 1) return;
574 if (server
.nb_monitor
== 1) return;
575 Task
*tsk
= task_get_task (win
);
578 Panel
*p
= tsk
->area
.panel
;
579 if (p
->monitor
!= window_get_monitor (win
)) {
582 if (win
== window_get_active ()) {
583 Task
*tsk
= task_get_task (win
);
584 tsk
->area
.is_active
= 1;
597 if (gettimeofday(&stv
, 0)) return;
599 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
600 time_clock
.tv_sec
= stv
.tv_sec
;
601 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
605 if (tick_urgent
< max_tick_urgent
) {
606 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
607 task_urgent
->area
.redraw
= 1;
613 #ifdef ENABLE_BATTERY
614 if (panel1
[0].battery
.area
.on_screen
) {
616 for (i
=0 ; i
< nb_panel
; i
++)
617 panel1
[i
].battery
.area
.resize
= 1;
623 for (i
=0 ; i
< nb_panel
; i
++)
624 panel1
[i
].clock
.area
.resize
= 1;
630 int main (int argc
, char *argv
[])
645 i
= config_read_file (config_path
);
649 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
654 if (thumbnail_path
) {
655 // usage: tint2 -j <file> for internal use
656 printf("file %s\n", thumbnail_path
);
661 x11_fd
= ConnectionNumber(server
.dsp
);
662 XSync(server
.dsp
, False
);
665 // thanks to AngryLlama for the timer
666 // Create a File Description Set containing x11_fd
668 FD_SET (x11_fd
, &fd
);
673 // Wait for X Event or a Timer
674 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
675 while (XPending (server
.dsp
)) {
676 XNextEvent(server
.dsp
, &e
);
680 event_button_press (&e
);
684 event_button_release(&e
);
692 event_property_notify(&e
);
695 case ConfigureNotify
:
696 if (e
.xconfigure
.window
== server
.root_win
)
699 event_configure_notify (e
.xconfigure
.window
);
703 if (!systray
.area
.on_screen
)
705 panel
= (Panel
*)systray
.area
.panel
;
706 if (e
.xany
.window
== panel
->main_win
) // reparented to us
708 // FIXME: 'reparent to us' badly detected => disabled
712 if (!systray
.area
.on_screen
)
714 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
715 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
716 remove_icon((TrayWindow
*)it
->data
);
723 if (!systray
.area
.on_screen
) break;
724 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
725 net_message(&e
.xclient
);
733 switch (signal_pending
) {
747 if (refresh_systray
) {
748 panel
= (Panel
*)systray
.area
.panel
;
749 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
751 for (i
=0 ; i
< nb_panel
; i
++) {
754 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
755 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
757 refresh(&panel
->area
);
758 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
762 if (refresh_systray
) {
764 panel
= (Panel
*)systray
.area
.panel
;
765 // tint2 doen't draw systray icons. it just redraw background.
766 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
767 // force icon's refresh
768 refresh_systray_icon();
This page took 0.06711 seconds and 4 git commands to generate.