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 **************************************************************************/
29 void server_catch_error (Display
*d
, XErrorEvent
*ev
){}
31 static char *name_trayer
= 0;
34 void server_init_atoms ()
36 server
.atom
._XROOTPMAP_ID
= XInternAtom (server
.dsp
, "_XROOTPMAP_ID", False
);
37 server
.atom
._NET_CURRENT_DESKTOP
= XInternAtom (server
.dsp
, "_NET_CURRENT_DESKTOP", False
);
38 server
.atom
._NET_NUMBER_OF_DESKTOPS
= XInternAtom (server
.dsp
, "_NET_NUMBER_OF_DESKTOPS", False
);
39 server
.atom
._NET_DESKTOP_GEOMETRY
= XInternAtom (server
.dsp
, "_NET_DESKTOP_GEOMETRY", False
);
40 server
.atom
._NET_DESKTOP_VIEWPORT
= XInternAtom (server
.dsp
, "_NET_DESKTOP_VIEWPORT", False
);
41 server
.atom
._NET_ACTIVE_WINDOW
= XInternAtom (server
.dsp
, "_NET_ACTIVE_WINDOW", False
);
42 server
.atom
._NET_WM_WINDOW_TYPE
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE", False
);
43 server
.atom
._NET_WM_STATE_SKIP_PAGER
= XInternAtom (server
.dsp
, "_NET_WM_STATE_SKIP_PAGER", False
);
44 server
.atom
._NET_WM_STATE_SKIP_TASKBAR
= XInternAtom (server
.dsp
, "_NET_WM_STATE_SKIP_TASKBAR", False
);
45 server
.atom
._NET_WM_STATE_STICKY
= XInternAtom (server
.dsp
, "_NET_WM_STATE_STICKY", False
);
46 server
.atom
._NET_WM_STATE_DEMANDS_ATTENTION
= XInternAtom (server
.dsp
, "_NET_WM_STATE_DEMANDS_ATTENTION", False
);
47 server
.atom
._NET_WM_WINDOW_TYPE_DOCK
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_DOCK", False
);
48 server
.atom
._NET_WM_WINDOW_TYPE_DESKTOP
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_DESKTOP", False
);
49 server
.atom
._NET_WM_WINDOW_TYPE_TOOLBAR
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_TOOLBAR", False
);
50 server
.atom
._NET_WM_WINDOW_TYPE_MENU
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_MENU", False
);
51 server
.atom
._NET_WM_WINDOW_TYPE_SPLASH
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_SPLASH", False
);
52 server
.atom
._NET_WM_WINDOW_TYPE_DIALOG
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
53 server
.atom
._NET_WM_WINDOW_TYPE_NORMAL
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_NORMAL", False
);
54 server
.atom
._NET_WM_DESKTOP
= XInternAtom (server
.dsp
, "_NET_WM_DESKTOP", False
);
55 server
.atom
.WM_STATE
= XInternAtom (server
.dsp
, "WM_STATE", False
);
56 server
.atom
._NET_WM_STATE
= XInternAtom (server
.dsp
, "_NET_WM_STATE", False
);
57 server
.atom
._NET_WM_STATE_MAXIMIZED_VERT
= XInternAtom (server
.dsp
, "_NET_WM_STATE_MAXIMIZED_VERT", False
);
58 server
.atom
._NET_WM_STATE_MAXIMIZED_HORZ
= XInternAtom (server
.dsp
, "_NET_WM_STATE_MAXIMIZED_HORZ", False
);
59 server
.atom
._NET_WM_STATE_SHADED
= XInternAtom (server
.dsp
, "_NET_WM_STATE_SHADED", False
);
60 server
.atom
._NET_WM_STATE_BELOW
= XInternAtom (server
.dsp
, "_NET_WM_STATE_BELOW", False
);
61 server
.atom
._NET_WM_STATE_MODAL
= XInternAtom (server
.dsp
, "_NET_WM_STATE_MODAL", False
);
62 server
.atom
._NET_CLIENT_LIST
= XInternAtom (server
.dsp
, "_NET_CLIENT_LIST", False
);
63 server
.atom
._NET_WM_VISIBLE_NAME
= XInternAtom (server
.dsp
, "_NET_WM_VISIBLE_NAME", False
);
64 server
.atom
._NET_WM_NAME
= XInternAtom (server
.dsp
, "_NET_WM_NAME", False
);
65 server
.atom
._NET_WM_STRUT
= XInternAtom (server
.dsp
, "_NET_WM_STRUT", False
);
66 server
.atom
._NET_WM_ICON
= XInternAtom (server
.dsp
, "_NET_WM_ICON", False
);
67 server
.atom
._NET_CLOSE_WINDOW
= XInternAtom (server
.dsp
, "_NET_CLOSE_WINDOW", False
);
68 server
.atom
.UTF8_STRING
= XInternAtom (server
.dsp
, "UTF8_STRING", False
);
69 server
.atom
._NET_SUPPORTING_WM_CHECK
= XInternAtom (server
.dsp
, "_NET_SUPPORTING_WM_CHECK", False
);
70 server
.atom
._NET_SUPPORTING_WM_CHECK
= XInternAtom (server
.dsp
, "_NET_WM_NAME", False
);
71 server
.atom
._WIN_LAYER
= XInternAtom (server
.dsp
, "_WIN_LAYER", False
);
72 server
.atom
._NET_WM_STRUT_PARTIAL
= XInternAtom (server
.dsp
, "_NET_WM_STRUT_PARTIAL", False
);
73 server
.atom
.WM_NAME
= XInternAtom(server
.dsp
, "WM_NAME", False
);
74 server
.atom
.__SWM_VROOT
= XInternAtom(server
.dsp
, "__SWM_VROOT", False
);
75 server
.atom
._MOTIF_WM_HINTS
= XInternAtom(server
.dsp
, "_MOTIF_WM_HINTS", False
);
78 name_trayer
= g_strdup_printf("_NET_SYSTEM_TRAY_S%d", DefaultScreen(server
.dsp
));
79 server
.atom
._NET_SYSTEM_TRAY_SCREEN
= XInternAtom(server
.dsp
, name_trayer
, False
);
80 server
.atom
._NET_SYSTEM_TRAY_OPCODE
= XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_OPCODE", False
);
81 server
.atom
.MANAGER
= XInternAtom(server
.dsp
, "MANAGER", False
);
82 server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
= XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_MESSAGE_DATA", False
);
83 server
.atom
._NET_SYSTEM_TRAY_ORIENTATION
= XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_ORIENTATION", False
);
84 server
.atom
._XEMBED
= XInternAtom(server
.dsp
, "_XEMBED", False
);
85 server
.atom
._XEMBED_INFO
= XInternAtom(server
.dsp
, "_XEMBED_INFO", False
);
91 if (name_trayer
) free(name_trayer
);
95 void send_event32 (Window win
, Atom at
, long data1
, long data2
, long data3
)
99 event
.xclient
.type
= ClientMessage
;
100 event
.xclient
.serial
= 0;
101 event
.xclient
.send_event
= True
;
102 event
.xclient
.display
= server
.dsp
;
103 event
.xclient
.window
= win
;
104 event
.xclient
.message_type
= at
;
106 event
.xclient
.format
= 32;
107 event
.xclient
.data
.l
[0] = data1
;
108 event
.xclient
.data
.l
[1] = data2
;
109 event
.xclient
.data
.l
[2] = data3
;
110 event
.xclient
.data
.l
[3] = 0;
111 event
.xclient
.data
.l
[4] = 0;
113 XSendEvent(server
.dsp
, server
.root_win
, False
, SubstructureRedirectMask
|SubstructureNotifyMask
, &event
);
117 int get_property32 (Window win
, Atom at
, Atom type
)
120 int format_ret
= 0, data
= 0;
121 unsigned long nitems_ret
= 0;
122 unsigned long bafter_ret
= 0;
123 unsigned char *prop_value
= 0;
128 result
= XGetWindowProperty(server
.dsp
, win
, at
, 0, 0x7fffffff, False
, type
, &type_ret
, &format_ret
, &nitems_ret
, &bafter_ret
, &prop_value
);
130 if (result
== Success
&& prop_value
) {
131 data
= ((gulong
*)prop_value
)[0];
138 void *server_get_property (Window win
, Atom at
, Atom type
, int *num_results
)
142 unsigned long nitems_ret
= 0;
143 unsigned long bafter_ret
= 0;
144 unsigned char *prop_value
;
149 result
= XGetWindowProperty(server
.dsp
, win
, at
, 0, 0x7fffffff, False
, type
, &type_ret
, &format_ret
, &nitems_ret
, &bafter_ret
, &prop_value
);
151 /* Send back resultcount */
152 if (num_results
) *num_results
= nitems_ret
;
154 if (result
== Success
&& prop_value
) return prop_value
;
159 void get_root_pixmap()
167 res
= server_get_property (server
.root_win
, server
.atom
._XROOTPMAP_ID
, XA_PIXMAP
, 0);
169 ret
= *((Pixmap
*)res
);
174 server
.root_pmap
= ret
;
176 if (server
.root_pmap
== None
)
177 fprintf(stderr
, "tint2 : pixmap background detection failed\n");
182 gcv
.fill_style
= FillTiled
;
183 uint mask
= GCTileStipXOrigin
| GCTileStipYOrigin
| GCFillStyle
| GCTile
;
185 gcv
.tile
= server
.root_pmap
;
186 XChangeGC(server
.dsp
, server
.gc
, mask
, &gcv
);
191 int compareMonitor(const void *monitor1
, const void *monitor2
)
193 Monitor
*m1
= (Monitor
*)monitor1
;
194 Monitor
*m2
= (Monitor
*)monitor2
;
204 if (m1
->width
< m2
->width
) {
208 if (m1
->width
> m2
->width
) {
217 void get_monitors_and_desktops()
221 if (server
.monitor
) free(server
.monitor
);
222 server
.nb_monitor
= 0;
226 if (XineramaIsActive(server
.dsp
)) {
227 XineramaScreenInfo
*info
= XineramaQueryScreens(server
.dsp
, &nb_monitor
);
233 server
.monitor
= calloc(nb_monitor
, sizeof(Monitor
));
234 while (i
< nb_monitor
) {
235 for (j
= 0; j
< i
; j
++) {
236 if (info
[i
].x_org
>= info
[j
].x_org
&& info
[i
].y_org
>= info
[j
].y_org
&& (info
[i
].x_org
+info
[i
].width
) <= (info
[j
].x_org
+info
[j
].width
) && (info
[i
].y_org
+info
[i
].height
) <= (info
[j
].y_org
+info
[j
].height
)) {
237 if (info
[i
].x_org
== info
[j
].x_org
&& info
[i
].y_org
== info
[j
].y_org
&& info
[i
].width
== info
[j
].width
&& info
[i
].height
== info
[j
].height
&& nb
== 0) {
238 // add the first monitor
242 // doesn't count monitor 'i' because it's included into another one
243 //fprintf(stderr, "monitor %d included into another one\n", i);
249 server
.monitor
[nb
].x
= info
[i
].x_org
;
250 server
.monitor
[nb
].y
= info
[i
].y_org
;
251 server
.monitor
[nb
].width
= info
[i
].width
;
252 server
.monitor
[nb
].height
= info
[i
].height
;
258 server
.nb_monitor
= nb
;
260 // ordered monitor according to coordinate
261 qsort(server
.monitor
, server
.nb_monitor
, sizeof(Monitor
), compareMonitor
);
265 if (!server
.nb_monitor
) {
266 server
.nb_monitor
= 1;
267 server
.monitor
= calloc(server
.nb_monitor
, sizeof(Monitor
));
268 server
.monitor
[0].x
= server
.monitor
[0].y
= 0;
269 server
.monitor
[0].width
= DisplayWidth (server
.dsp
, server
.screen
);
270 server
.monitor
[0].height
= DisplayHeight (server
.dsp
, server
.screen
);
273 // detect number of desktops
274 // wait 15s to leave some time for window manager startup
275 for (i
=0 ; i
< 15 ; i
++) {
276 server
.nb_desktop
= server_get_number_of_desktop ();
277 if (server
.nb_desktop
> 0) break;
280 if (server
.nb_desktop
== 0) {
281 server
.nb_desktop
= 1;
282 fprintf(stderr
, "warning : WM doesn't respect NETWM specs. tint2 default to 1 desktop.\n");
284 fprintf(stderr
, "tint2 : nb monitor %d, nb desktop %d\n", server
.nb_monitor
, server
.nb_desktop
);