]>
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 for (i
=0 ; i
< nb_panel
; i
++) {
392 Panel
*panel
= &panel1
[i
];
393 if (panel_mode
== MULTI_DESKTOP
&& panel
->g_taskbar
.use_active
) {
399 for (j
=0 ; j
< panel
->nb_desktop
; j
++) {
400 tskbar
= &panel
->taskbar
[j
];
401 if (tskbar
->area
.is_active
) {
402 tskbar
->area
.is_active
= 0;
403 tskbar
->area
.redraw
= 1;
404 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
406 tsk
->area
.redraw
= 1;
409 if (j
== server
.desktop
) {
410 tskbar
->area
.is_active
= 1;
411 tskbar
->area
.redraw
= 1;
412 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
414 tsk
->area
.redraw
= 1;
420 if (panel_mode
!= MULTI_DESKTOP
) {
425 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
426 task_refresh_tasklist();
430 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
433 for (i
=0 ; i
< nb_panel
; i
++) {
434 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
435 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
437 tsk
->area
.is_active
= 0;
443 Window w1
= window_get_active ();
444 Task
*t
= task_get_task(w1
);
447 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
448 if (w2
) t
= task_get_task(w2
);
450 if (task_urgent
== t
) {
454 // put active state on all task (multi_desktop)
456 for (i
=0 ; i
< nb_panel
; i
++) {
457 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
458 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
460 if (tsk
->win
== t
->win
) {
461 tsk
->area
.is_active
= 1;
462 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
471 else if (at
== server
.atom
._XROOTPMAP_ID
) {
473 for (i
=0 ; i
< nb_panel
; i
++) {
474 set_panel_background(&panel1
[i
]);
480 tsk
= task_get_task (win
);
482 if ( at
!= server
.atom
._NET_WM_STATE
)
484 else if ( !(tsk
= add_task(win
)) )
487 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
489 // Window title changed
490 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
494 // changed other tsk->title
495 for (i
=0 ; i
< nb_panel
; i
++) {
496 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
497 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
499 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
500 tsk2
->title
= tsk
->title
;
501 tsk2
->area
.redraw
= 1;
509 else if (at
== server
.atom
._NET_WM_STATE
) {
510 if (window_is_urgent (win
)) {
515 if (window_is_skip_taskbar(win
)) {
520 else if (at
== server
.atom
.WM_STATE
) {
522 // TODO : try to delete following code
523 if (window_is_iconified (win
)) {
525 if (task_active
->win
== tsk
->win
) {
528 for (i
=0 ; i
< nb_panel
; i
++) {
529 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
530 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
532 tsk2
->area
.is_active
= 0;
541 // Window icon changed
542 else if (at
== server
.atom
._NET_WM_ICON
) {
546 for (i
=0 ; i
< nb_panel
; i
++) {
547 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
548 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
550 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
551 tsk2
->icon_width
= tsk
->icon_width
;
552 tsk2
->icon_height
= tsk
->icon_height
;
553 tsk2
->icon_data
= tsk
->icon_data
;
554 tsk2
->icon_data_active
= tsk
->icon_data_active
;
555 tsk2
->area
.redraw
= 1;
562 // Window desktop changed
563 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
564 int desktop
= window_get_desktop (win
);
565 int active
= tsk
->area
.is_active
;
566 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
567 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
568 if (desktop
!= tsk
->desktop
) {
570 tsk
= add_task (win
);
572 tsk
->area
.is_active
= 1;
579 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
584 void event_expose (XEvent
*e
)
588 panel
= get_panel(e
->xany
.window
);
590 // TODO : one panel_refresh per panel ?
595 void event_configure_notify (Window win
)
597 // check 'win' move in systray
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 // check 'win' move in another monitor
611 if (nb_panel
== 1) return;
612 if (server
.nb_monitor
== 1) return;
613 Task
*tsk
= task_get_task (win
);
616 Panel
*p
= tsk
->area
.panel
;
617 if (p
->monitor
!= window_get_monitor (win
)) {
620 if (win
== window_get_active ()) {
621 Task
*tsk
= task_get_task (win
);
622 tsk
->area
.is_active
= 1;
635 if (gettimeofday(&stv
, 0)) return;
637 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
638 time_clock
.tv_sec
= stv
.tv_sec
;
639 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
643 if (tick_urgent
< max_tick_urgent
) {
644 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
645 task_urgent
->area
.redraw
= 1;
651 #ifdef ENABLE_BATTERY
652 if (panel1
[0].battery
.area
.on_screen
) {
654 for (i
=0 ; i
< nb_panel
; i
++)
655 panel1
[i
].battery
.area
.resize
= 1;
661 for (i
=0 ; i
< nb_panel
; i
++)
662 panel1
[i
].clock
.area
.resize
= 1;
668 int main (int argc
, char *argv
[])
683 i
= config_read_file (config_path
);
687 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
692 if (thumbnail_path
) {
693 // usage: tint2 -j <file> for internal use
694 printf("file %s\n", thumbnail_path
);
699 x11_fd
= ConnectionNumber(server
.dsp
);
700 XSync(server
.dsp
, False
);
703 // thanks to AngryLlama for the timer
704 // Create a File Description Set containing x11_fd
706 FD_SET (x11_fd
, &fd
);
711 // Wait for X Event or a Timer
712 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
713 while (XPending (server
.dsp
)) {
714 XNextEvent(server
.dsp
, &e
);
718 event_button_press (&e
);
722 event_button_release(&e
);
730 event_property_notify(&e
);
733 case ConfigureNotify
:
734 if (e
.xconfigure
.window
== server
.root_win
)
737 event_configure_notify (e
.xconfigure
.window
);
741 if (!systray
.area
.on_screen
)
743 panel
= (Panel
*)systray
.area
.panel
;
744 if (e
.xany
.window
== panel
->main_win
) // reparented to us
746 // FIXME: 'reparent to us' badly detected => disabled
750 if (!systray
.area
.on_screen
)
752 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
753 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
754 remove_icon((TrayWindow
*)it
->data
);
761 if (!systray
.area
.on_screen
) break;
762 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
763 net_message(&e
.xclient
);
771 switch (signal_pending
) {
785 if (refresh_systray
) {
786 panel
= (Panel
*)systray
.area
.panel
;
787 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
789 for (i
=0 ; i
< nb_panel
; i
++) {
792 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
793 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
795 refresh(&panel
->area
);
796 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
800 if (refresh_systray
) {
802 panel
= (Panel
*)systray
.area
.panel
;
803 // tint2 doen't draw systray icons. it just redraw background.
804 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
805 // force icon's refresh
806 refresh_systray_icon();
This page took 0.069334 seconds and 4 git commands to generate.