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
._NET_WM_STRUT_PARTIAL
= XInternAtom (server
.dsp
, "_NET_WM_STRUT_PARTIAL", False
);
72 server
.atom
.WM_NAME
= XInternAtom(server
.dsp
, "WM_NAME", False
);
73 server
.atom
.__SWM_VROOT
= XInternAtom(server
.dsp
, "__SWM_VROOT", False
);
74 server
.atom
._MOTIF_WM_HINTS
= XInternAtom(server
.dsp
, "_MOTIF_WM_HINTS", False
);
75 server
.atom
.WM_HINTS
= XInternAtom(server
.dsp
, "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
);
88 server
.atom
.XdndAware
= XInternAtom(server
.dsp
, "XdndAware", False
);
89 server
.atom
.XdndPosition
= XInternAtom(server
.dsp
, "XdndPosition", False
);
90 server
.atom
.XdndStatus
= XInternAtom(server
.dsp
, "XdndStatus", False
);
96 if (name_trayer
) free(name_trayer
);
100 void send_event32 (Window win
, Atom at
, long data1
, long data2
, long data3
)
104 event
.xclient
.type
= ClientMessage
;
105 event
.xclient
.serial
= 0;
106 event
.xclient
.send_event
= True
;
107 event
.xclient
.display
= server
.dsp
;
108 event
.xclient
.window
= win
;
109 event
.xclient
.message_type
= at
;
111 event
.xclient
.format
= 32;
112 event
.xclient
.data
.l
[0] = data1
;
113 event
.xclient
.data
.l
[1] = data2
;
114 event
.xclient
.data
.l
[2] = data3
;
115 event
.xclient
.data
.l
[3] = 0;
116 event
.xclient
.data
.l
[4] = 0;
118 XSendEvent(server
.dsp
, server
.root_win
, False
, SubstructureRedirectMask
|SubstructureNotifyMask
, &event
);
122 int get_property32 (Window win
, Atom at
, Atom type
)
125 int format_ret
= 0, data
= 0;
126 unsigned long nitems_ret
= 0;
127 unsigned long bafter_ret
= 0;
128 unsigned char *prop_value
= 0;
133 result
= XGetWindowProperty(server
.dsp
, win
, at
, 0, 0x7fffffff, False
, type
, &type_ret
, &format_ret
, &nitems_ret
, &bafter_ret
, &prop_value
);
135 if (result
== Success
&& prop_value
) {
136 data
= ((gulong
*)prop_value
)[0];
143 void *server_get_property (Window win
, Atom at
, Atom type
, int *num_results
)
147 unsigned long nitems_ret
= 0;
148 unsigned long bafter_ret
= 0;
149 unsigned char *prop_value
;
154 result
= XGetWindowProperty(server
.dsp
, win
, at
, 0, 0x7fffffff, False
, type
, &type_ret
, &format_ret
, &nitems_ret
, &bafter_ret
, &prop_value
);
156 /* Send back resultcount */
157 if (num_results
) *num_results
= nitems_ret
;
159 if (result
== Success
&& prop_value
) return prop_value
;
164 void get_root_pixmap()
172 res
= server_get_property (server
.root_win
, server
.atom
._XROOTPMAP_ID
, XA_PIXMAP
, 0);
174 ret
= *((Pixmap
*)res
);
179 server
.root_pmap
= ret
;
181 if (server
.root_pmap
== None
)
182 fprintf(stderr
, "tint2 : pixmap background detection failed\n");
187 gcv
.fill_style
= FillTiled
;
188 uint mask
= GCTileStipXOrigin
| GCTileStipYOrigin
| GCFillStyle
| GCTile
;
190 gcv
.tile
= server
.root_pmap
;
191 XChangeGC(server
.dsp
, server
.gc
, mask
, &gcv
);
196 int compareMonitor(const void *monitor1
, const void *monitor2
)
198 Monitor
*m1
= (Monitor
*)monitor1
;
199 Monitor
*m2
= (Monitor
*)monitor2
;
209 if (m1
->width
< m2
->width
) {
213 if (m1
->width
> m2
->width
) {
224 if (server
.monitor
) free(server
.monitor
);
225 server
.nb_monitor
= 0;
229 if (XineramaIsActive(server
.dsp
)) {
230 XineramaScreenInfo
*info
= XineramaQueryScreens(server
.dsp
, &nb_monitor
);
236 server
.monitor
= calloc(nb_monitor
, sizeof(Monitor
));
237 while (i
< nb_monitor
) {
238 for (j
= 0; j
< i
; j
++) {
239 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
)) {
240 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) {
241 // add the first monitor
245 // doesn't count monitor 'i' because it's included into another one
246 //fprintf(stderr, "monitor %d included into another one\n", i);
252 server
.monitor
[nb
].x
= info
[i
].x_org
;
253 server
.monitor
[nb
].y
= info
[i
].y_org
;
254 server
.monitor
[nb
].width
= info
[i
].width
;
255 server
.monitor
[nb
].height
= info
[i
].height
;
261 server
.nb_monitor
= nb
;
263 // ordered monitor according to coordinate
264 qsort(server
.monitor
, server
.nb_monitor
, sizeof(Monitor
), compareMonitor
);
268 if (!server
.nb_monitor
) {
269 server
.nb_monitor
= 1;
270 server
.monitor
= calloc(server
.nb_monitor
, sizeof(Monitor
));
271 server
.monitor
[0].x
= server
.monitor
[0].y
= 0;
272 server
.monitor
[0].width
= DisplayWidth (server
.dsp
, server
.screen
);
273 server
.monitor
[0].height
= DisplayHeight (server
.dsp
, server
.screen
);
282 // detect number of desktops
283 // wait 15s to leave some time for window manager startup
284 for (i
=0 ; i
< 15 ; i
++) {
285 server
.nb_desktop
= server_get_number_of_desktop ();
286 if (server
.nb_desktop
> 0) break;
289 if (server
.nb_desktop
== 0) {
290 server
.nb_desktop
= 1;
291 fprintf(stderr
, "warning : WM doesn't respect NETWM specs. tint2 default to 1 desktop.\n");
293 fprintf(stderr
, "tint2 : nb monitor %d, nb desktop %d\n", server
.nb_monitor
, server
.nb_desktop
);