]>
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
52 signal(SIGUSR1
, signal_handler
);
53 signal(SIGINT
, signal_handler
);
54 signal(SIGTERM
, signal_handler
);
55 signal(SIGHUP
, signal_handler
);
58 memset(&server
, 0, sizeof(Server_global
));
59 memset(&systray
, 0, sizeof(Systraybar
));
61 server
.dsp
= XOpenDisplay (NULL
);
63 fprintf(stderr
, "tint2 exit : could not open display.\n");
67 server
.screen
= DefaultScreen (server
.dsp
);
68 server
.root_win
= RootWindow(server
.dsp
, server
.screen
);
69 server
.depth
= DefaultDepth (server
.dsp
, server
.screen
);
70 server
.visual
= DefaultVisual (server
.dsp
, server
.screen
);
71 server
.desktop
= server_get_current_desktop ();
73 server
.gc
= XCreateGC (server
.dsp
, server
.root_win
, (unsigned long)0, &gcv
) ;
75 XSetErrorHandler ((XErrorHandler
) server_catch_error
);
77 imlib_context_set_display (server
.dsp
);
78 imlib_context_set_visual (server
.visual
);
79 imlib_context_set_colormap (DefaultColormap (server
.dsp
, server
.screen
));
82 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
84 setlocale (LC_ALL
, "");
92 if (time1_font_desc
) pango_font_description_free(time1_font_desc
);
93 if (time2_font_desc
) pango_font_description_free(time2_font_desc
);
94 if (time1_format
) g_free(time1_format
);
95 if (time2_format
) g_free(time2_format
);
97 if (bat1_font_desc
) pango_font_description_free(bat1_font_desc
);
98 if (bat2_font_desc
) pango_font_description_free(bat2_font_desc
);
99 if (battery_low_cmd
) g_free(battery_low_cmd
);
100 if (path_energy_now
) g_free(path_energy_now
);
101 if (path_energy_full
) g_free(path_energy_full
);
102 if (path_current_now
) g_free(path_current_now
);
103 if (path_status
) g_free(path_status
);
105 if (clock_lclick_command
) g_free(clock_lclick_command
);
106 if (clock_rclick_command
) g_free(clock_rclick_command
);
108 if (server
.monitor
) free(server
.monitor
);
109 XFreeGC(server
.dsp
, server
.gc
);
110 XCloseDisplay(server
.dsp
);
114 void window_action (Task
*tsk
, int action
)
118 set_close (tsk
->win
);
121 set_active(tsk
->win
);
124 XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
127 if (tsk
== task_active
) XIconifyWindow (server
.dsp
, tsk
->win
, server
.screen
);
128 else set_active (tsk
->win
);
131 window_toggle_shade (tsk
->win
);
137 void event_button_press (XEvent
*e
)
139 Panel
*panel
= get_panel(e
->xany
.window
);
143 if ((e
->xbutton
.x
< panel
->area
.paddingxlr
) || (e
->xbutton
.x
> panel
->area
.width
-panel
->area
.paddingxlr
) || (e
->xbutton
.y
< panel
->area
.paddingy
) || (e
->xbutton
.y
> panel
->area
.paddingy
+panel
->g_taskbar
.height
)) {
144 // forward the click to the desktop window (thanks conky)
145 XUngrabPointer(server
.dsp
, e
->xbutton
.time
);
146 e
->xbutton
.window
= server
.root_win
;
147 // icewm doesn't open under the mouse.
148 // and xfce doesn't open at all.
149 //e->xbutton.x = e->xbutton.x_root;
150 //e->xbutton.y = e->xbutton.y_root;
151 //printf("**** %d, %d\n", e->xbutton.x, e->xbutton.y);
152 XSetInputFocus(server
.dsp
, e
->xbutton
.window
, RevertToParent
, e
->xbutton
.time
);
153 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonPressMask
, e
);
158 if (e
->xbutton
.button
!= 1) return;
160 if (panel_mode
!= MULTI_DESKTOP
) {
161 // drag and drop disabled
162 XLowerWindow (server
.dsp
, panel
->main_win
);
168 int x
= e
->xbutton
.x
;
169 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
171 if (!tskbar
->area
.on_screen
) continue;
172 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
178 for (l0
= tskbar
->area
.list
; l0
; l0
= l0
->next
) {
180 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
187 XLowerWindow (server
.dsp
, panel
->main_win
);
191 void event_button_release (XEvent
*e
)
193 Panel
*panel
= get_panel(e
->xany
.window
);
197 if ((e
->xbutton
.x
< panel
->area
.paddingxlr
) || (e
->xbutton
.x
> panel
->area
.width
-panel
->area
.paddingxlr
) || (e
->xbutton
.y
< panel
->area
.paddingy
) || (e
->xbutton
.y
> panel
->area
.paddingy
+panel
->g_taskbar
.height
)) {
198 // forward the click to the desktop window (thanks conky)
199 e
->xbutton
.window
= server
.root_win
;
200 XSendEvent(server
.dsp
, e
->xbutton
.window
, False
, ButtonReleaseMask
, e
);
205 int action
= TOGGLE_ICONIFY
;
206 int x
= e
->xbutton
.x
;
207 //int y = e->xbutton.y; // unused
208 switch (e
->xbutton
.button
) {
210 action
= mouse_middle
;
213 action
= mouse_right
;
216 action
= mouse_scroll_up
;
219 action
= mouse_scroll_down
;
226 Clock clk
= panel
->clock
;
227 if (clk
.area
.on_screen
&& x
>= clk
.area
.posx
&& x
<= (clk
.area
.posx
+ clk
.area
.width
))
228 clock_action(e
->xbutton
.button
);
230 for (l0
= panel
->area
.list
; l0
; l0
= l0
->next
) {
232 if (!tskbar
->area
.on_screen
) continue;
233 if (x
>= tskbar
->area
.posx
&& x
<= (tskbar
->area
.posx
+ tskbar
->area
.width
))
238 // TODO: check better solution to keep window below
239 XLowerWindow (server
.dsp
, panel
->main_win
);
244 // drag and drop task
246 if (tskbar
!= task_drag
->area
.parent
&& action
== TOGGLE_ICONIFY
) {
247 if (task_drag
->desktop
!= ALLDESKTOP
&& panel_mode
== MULTI_DESKTOP
) {
248 windows_set_desktop(task_drag
->win
, tskbar
->desktop
);
249 if (tskbar
->desktop
== server
.desktop
)
250 set_active(task_drag
->win
);
259 if (panel_mode
== MULTI_DESKTOP
)
260 if (tskbar
->desktop
!= server
.desktop
&& action
!= CLOSE
)
261 set_desktop (tskbar
->desktop
);
266 for (l
= tskbar
->area
.list
; l
; l
= l
->next
) {
268 if (x
>= tsk
->area
.posx
&& x
<= (tsk
->area
.posx
+ tsk
->area
.width
)) {
269 window_action (tsk
, action
);
274 // to keep window below
275 XLowerWindow (server
.dsp
, panel
->main_win
);
279 void event_property_notify (XEvent
*e
)
283 Window win
= e
->xproperty
.window
;
284 Atom at
= e
->xproperty
.atom
;
286 if (win
== server
.root_win
) {
287 if (!server
.got_root_win
) {
288 XSelectInput (server
.dsp
, server
.root_win
, PropertyChangeMask
|StructureNotifyMask
);
289 server
.got_root_win
= 1;
292 // Change number of desktops
293 else if (at
== server
.atom
._NET_NUMBER_OF_DESKTOPS
) {
294 server
.nb_desktop
= server_get_number_of_desktop ();
298 for (i
=0 ; i
< nb_panel
; i
++) {
299 panel1
[i
].area
.resize
= 1;
301 task_refresh_tasklist();
305 else if (at
== server
.atom
._NET_CURRENT_DESKTOP
) {
306 server
.desktop
= server_get_current_desktop ();
307 if (panel_mode
!= MULTI_DESKTOP
) {
312 else if (at
== server
.atom
._NET_CLIENT_LIST
) {
313 task_refresh_tasklist();
317 else if (at
== server
.atom
._NET_ACTIVE_WINDOW
) {
320 for (i
=0 ; i
< nb_panel
; i
++) {
321 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
322 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
324 tsk
->area
.is_active
= 0;
330 Window w1
= window_get_active ();
331 Task
*t
= task_get_task(w1
);
334 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
335 if (w2
) t
= task_get_task(w2
);
337 if (task_urgent
== t
) {
342 for (i
=0 ; i
< nb_panel
; i
++) {
343 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
344 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
346 if (tsk
->win
== t
->win
) {
347 tsk
->area
.is_active
= 1;
348 //printf("active monitor %d, task %s\n", panel1[i].monitor, tsk->title);
357 else if (at
== server
.atom
._XROOTPMAP_ID
) {
359 for (i
=0 ; i
< nb_panel
; i
++) {
360 set_panel_background(&panel1
[i
]);
366 tsk
= task_get_task (win
);
368 //printf("atom root_win = %s, %s\n", XGetAtomName(server.dsp, at), tsk->title);
370 // Window title changed
371 if (at
== server
.atom
._NET_WM_VISIBLE_NAME
|| at
== server
.atom
._NET_WM_NAME
|| at
== server
.atom
.WM_NAME
) {
375 // changed other tsk->title
376 for (i
=0 ; i
< nb_panel
; i
++) {
377 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
378 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
380 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
381 tsk2
->title
= tsk
->title
;
382 tsk2
->area
.redraw
= 1;
390 else if (at
== server
.atom
._NET_WM_STATE
) {
391 if (window_is_urgent (win
)) {
397 else if (at
== server
.atom
.WM_STATE
) {
399 // TODO : try to delete following code
400 if (window_is_iconified (win
)) {
402 if (task_active
->win
== tsk
->win
) {
405 for (i
=0 ; i
< nb_panel
; i
++) {
406 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
407 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
409 tsk2
->area
.is_active
= 0;
418 // Window icon changed
419 else if (at
== server
.atom
._NET_WM_ICON
) {
423 for (i
=0 ; i
< nb_panel
; i
++) {
424 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
425 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
427 if (tsk
->win
== tsk2
->win
&& tsk
!= tsk2
) {
428 tsk2
->icon_width
= tsk
->icon_width
;
429 tsk2
->icon_height
= tsk
->icon_height
;
430 tsk2
->icon_data
= tsk
->icon_data
;
431 tsk2
->area
.redraw
= 1;
438 // Window desktop changed
439 else if (at
== server
.atom
._NET_WM_DESKTOP
) {
440 int desktop
= window_get_desktop (win
);
441 //printf(" Window desktop changed %d, %d\n", tsk->desktop, desktop);
442 // bug in windowmaker : send unecessary 'desktop changed' when focus changed
443 if (desktop
!= tsk
->desktop
) {
450 if (!server
.got_root_win
) server
.root_win
= RootWindow (server
.dsp
, server
.screen
);
455 void event_expose (XEvent
*e
)
459 panel
= get_panel(e
->xany
.window
);
461 // TODO : one panel_refresh per panel ?
466 void event_configure_notify (Window win
)
468 // check 'win' move in systray
471 for (l
= systray
.list_icons
; l
; l
= l
->next
) {
472 traywin
= (TrayWindow
*)l
->data
;
473 if (traywin
->id
== win
) {
474 //printf("move tray %d\n", traywin->x);
475 XMoveResizeWindow(server
.dsp
, traywin
->id
, traywin
->x
, traywin
->y
, traywin
->width
, traywin
->height
);
481 // check 'win' move in another monitor
482 if (nb_panel
== 1) return;
483 if (server
.nb_monitor
== 1) return;
484 Task
*tsk
= task_get_task (win
);
487 Panel
*p
= tsk
->area
.panel
;
488 if (p
->monitor
!= window_get_monitor (win
)) {
491 if (win
== window_get_active ()) {
492 Task
*tsk
= task_get_task (win
);
493 tsk
->area
.is_active
= 1;
506 if (gettimeofday(&stv
, 0)) return;
508 if (abs(stv
.tv_sec
- time_clock
.tv_sec
) < time_precision
) return;
509 time_clock
.tv_sec
= stv
.tv_sec
;
510 time_clock
.tv_sec
-= time_clock
.tv_sec
% time_precision
;
514 if (tick_urgent
< max_tick_urgent
) {
515 task_urgent
->area
.is_active
= !task_urgent
->area
.is_active
;
516 task_urgent
->area
.redraw
= 1;
522 #ifdef ENABLE_BATTERY
523 if (panel1
[0].battery
.area
.on_screen
) {
525 for (i
=0 ; i
< nb_panel
; i
++)
526 panel1
[i
].battery
.area
.resize
= 1;
532 for (i
=0 ; i
< nb_panel
; i
++)
533 panel1
[i
].clock
.area
.resize
= 1;
539 int main (int argc
, char *argv
[])
548 c
= getopt (argc
, argv
, "c:");
555 i
= config_read_file (optarg
);
556 c
= getopt (argc
, argv
, "j:");
558 // usage: tint2 [-c] <config_file> -j <jpeg_file> for internal use
559 printf("jpeg file %s\n", optarg
);
567 fprintf(stderr
, "usage: tint2 [-c] <config_file>\n");
573 x11_fd
= ConnectionNumber(server
.dsp
);
574 XSync(server
.dsp
, False
);
577 // thanks to AngryLlama for the timer
578 // Create a File Description Set containing x11_fd
580 FD_SET (x11_fd
, &fd
);
585 // Wait for X Event or a Timer
586 if (select(x11_fd
+1, &fd
, 0, 0, &tv
)) {
587 while (XPending (server
.dsp
)) {
588 XNextEvent(server
.dsp
, &e
);
592 event_button_press (&e
);
596 event_button_release(&e
);
604 event_property_notify(&e
);
607 case ConfigureNotify
:
608 if (e
.xconfigure
.window
== server
.root_win
)
611 event_configure_notify (e
.xconfigure
.window
);
615 if (e
.xany
.window
== server
.root_win
) // reparented to us
619 if (!systray
.area
.on_screen
)
621 for (it
= systray
.list_icons
; it
; it
= g_slist_next(it
)) {
622 if (((TrayWindow
*)it
->data
)->id
== e
.xany
.window
) {
623 remove_icon((TrayWindow
*)it
->data
);
630 if (!systray
.area
.on_screen
) break;
631 if (e
.xclient
.message_type
== server
.atom
._NET_SYSTEM_TRAY_OPCODE
&& e
.xclient
.format
== 32 && e
.xclient
.window
== net_sel_win
) {
632 net_message(&e
.xclient
);
640 switch (signal_pending
) {
653 for (i
=0 ; i
< nb_panel
; i
++) {
656 if (panel
== systray
.area
.panel
)
657 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, None
);
659 if (panel
->temp_pmap
) XFreePixmap(server
.dsp
, panel
->temp_pmap
);
660 panel
->temp_pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, panel
->area
.width
, panel
->area
.height
, server
.depth
);
662 refresh(&panel
->area
);
663 XCopyArea(server
.dsp
, panel
->temp_pmap
, panel
->main_win
, server
.gc
, 0, 0, panel
->area
.width
, panel
->area
.height
, 0, 0);
665 if (panel
== systray
.area
.panel
) {
666 // tint2 doen't draw systray icons. it just redraw background.
667 XSetWindowBackgroundPixmap (server
.dsp
, panel
->main_win
, panel
->temp_pmap
);
668 // force icon's refresh
This page took 0.075814 seconds and 5 git commands to generate.