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 **************************************************************************/
21 #include <X11/extensions/Xrender.h>
22 #include <X11/extensions/Xrandr.h>
34 void server_catch_error (Display
*d
, XErrorEvent
*ev
){}
36 void server_init_atoms ()
38 server
.atom
._XROOTPMAP_ID
= XInternAtom (server
.dsp
, "_XROOTPMAP_ID", False
);
39 server
.atom
._NET_CURRENT_DESKTOP
= XInternAtom (server
.dsp
, "_NET_CURRENT_DESKTOP", False
);
40 server
.atom
._NET_NUMBER_OF_DESKTOPS
= XInternAtom (server
.dsp
, "_NET_NUMBER_OF_DESKTOPS", False
);
41 server
.atom
._NET_DESKTOP_GEOMETRY
= XInternAtom (server
.dsp
, "_NET_DESKTOP_GEOMETRY", False
);
42 server
.atom
._NET_DESKTOP_VIEWPORT
= XInternAtom (server
.dsp
, "_NET_DESKTOP_VIEWPORT", False
);
43 server
.atom
._NET_ACTIVE_WINDOW
= XInternAtom (server
.dsp
, "_NET_ACTIVE_WINDOW", False
);
44 server
.atom
._NET_WM_WINDOW_TYPE
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE", False
);
45 server
.atom
._NET_WM_STATE_SKIP_PAGER
= XInternAtom (server
.dsp
, "_NET_WM_STATE_SKIP_PAGER", False
);
46 server
.atom
._NET_WM_STATE_SKIP_TASKBAR
= XInternAtom (server
.dsp
, "_NET_WM_STATE_SKIP_TASKBAR", False
);
47 server
.atom
._NET_WM_STATE_STICKY
= XInternAtom (server
.dsp
, "_NET_WM_STATE_STICKY", False
);
48 server
.atom
._NET_WM_STATE_DEMANDS_ATTENTION
= XInternAtom (server
.dsp
, "_NET_WM_STATE_DEMANDS_ATTENTION", False
);
49 server
.atom
._NET_WM_WINDOW_TYPE_DOCK
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_DOCK", False
);
50 server
.atom
._NET_WM_WINDOW_TYPE_DESKTOP
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_DESKTOP", False
);
51 server
.atom
._NET_WM_WINDOW_TYPE_TOOLBAR
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_TOOLBAR", False
);
52 server
.atom
._NET_WM_WINDOW_TYPE_MENU
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_MENU", False
);
53 server
.atom
._NET_WM_WINDOW_TYPE_SPLASH
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_SPLASH", False
);
54 server
.atom
._NET_WM_WINDOW_TYPE_DIALOG
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
55 server
.atom
._NET_WM_WINDOW_TYPE_NORMAL
= XInternAtom (server
.dsp
, "_NET_WM_WINDOW_TYPE_NORMAL", False
);
56 server
.atom
._NET_WM_DESKTOP
= XInternAtom (server
.dsp
, "_NET_WM_DESKTOP", False
);
57 server
.atom
.WM_STATE
= XInternAtom (server
.dsp
, "WM_STATE", False
);
58 server
.atom
._NET_WM_STATE
= XInternAtom (server
.dsp
, "_NET_WM_STATE", False
);
59 server
.atom
._NET_WM_STATE_MAXIMIZED_VERT
= XInternAtom (server
.dsp
, "_NET_WM_STATE_MAXIMIZED_VERT", False
);
60 server
.atom
._NET_WM_STATE_MAXIMIZED_HORZ
= XInternAtom (server
.dsp
, "_NET_WM_STATE_MAXIMIZED_HORZ", False
);
61 server
.atom
._NET_WM_STATE_SHADED
= XInternAtom (server
.dsp
, "_NET_WM_STATE_SHADED", False
);
62 server
.atom
._NET_WM_STATE_HIDDEN
= XInternAtom (server
.dsp
, "_NET_WM_STATE_HIDDEN", False
);
63 server
.atom
._NET_WM_STATE_BELOW
= XInternAtom (server
.dsp
, "_NET_WM_STATE_BELOW", False
);
64 server
.atom
._NET_WM_STATE_ABOVE
= XInternAtom (server
.dsp
, "_NET_WM_STATE_ABOVE", False
);
65 server
.atom
._NET_WM_STATE_MODAL
= XInternAtom (server
.dsp
, "_NET_WM_STATE_MODAL", False
);
66 server
.atom
._NET_CLIENT_LIST
= XInternAtom (server
.dsp
, "_NET_CLIENT_LIST", False
);
67 server
.atom
._NET_WM_VISIBLE_NAME
= XInternAtom (server
.dsp
, "_NET_WM_VISIBLE_NAME", False
);
68 server
.atom
._NET_WM_NAME
= XInternAtom (server
.dsp
, "_NET_WM_NAME", False
);
69 server
.atom
._NET_WM_STRUT
= XInternAtom (server
.dsp
, "_NET_WM_STRUT", False
);
70 server
.atom
._NET_WM_ICON
= XInternAtom (server
.dsp
, "_NET_WM_ICON", False
);
71 server
.atom
._NET_WM_ICON_GEOMETRY
= XInternAtom(server
.dsp
, "_NET_WM_ICON_GEOMETRY", False
);
72 server
.atom
._NET_CLOSE_WINDOW
= XInternAtom (server
.dsp
, "_NET_CLOSE_WINDOW", False
);
73 server
.atom
.UTF8_STRING
= XInternAtom (server
.dsp
, "UTF8_STRING", False
);
74 server
.atom
._NET_SUPPORTING_WM_CHECK
= XInternAtom (server
.dsp
, "_NET_SUPPORTING_WM_CHECK", False
);
75 server
.atom
._NET_WM_CM_S0
= XInternAtom (server
.dsp
, "_NET_WM_CM_S0", False
);
76 server
.atom
._NET_SUPPORTING_WM_CHECK
= XInternAtom (server
.dsp
, "_NET_WM_NAME", False
);
77 server
.atom
._NET_WM_STRUT_PARTIAL
= XInternAtom (server
.dsp
, "_NET_WM_STRUT_PARTIAL", False
);
78 server
.atom
.WM_NAME
= XInternAtom(server
.dsp
, "WM_NAME", False
);
79 server
.atom
.__SWM_VROOT
= XInternAtom(server
.dsp
, "__SWM_VROOT", False
);
80 server
.atom
._MOTIF_WM_HINTS
= XInternAtom(server
.dsp
, "_MOTIF_WM_HINTS", False
);
81 server
.atom
.WM_HINTS
= XInternAtom(server
.dsp
, "WM_HINTS", False
);
84 char *name_trayer
= g_strdup_printf("_NET_SYSTEM_TRAY_S%d", DefaultScreen(server
.dsp
));
85 server
.atom
._NET_SYSTEM_TRAY_SCREEN
= XInternAtom(server
.dsp
, name_trayer
, False
);
86 server
.atom
._NET_SYSTEM_TRAY_OPCODE
= XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_OPCODE", False
);
87 server
.atom
.MANAGER
= XInternAtom(server
.dsp
, "MANAGER", False
);
88 server
.atom
._NET_SYSTEM_TRAY_MESSAGE_DATA
= XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_MESSAGE_DATA", False
);
89 server
.atom
._NET_SYSTEM_TRAY_ORIENTATION
= XInternAtom(server
.dsp
, "_NET_SYSTEM_TRAY_ORIENTATION", False
);
90 server
.atom
._XEMBED
= XInternAtom(server
.dsp
, "_XEMBED", False
);
91 server
.atom
._XEMBED_INFO
= XInternAtom(server
.dsp
, "_XEMBED_INFO", False
);
95 server
.atom
.XdndAware
= XInternAtom(server
.dsp
, "XdndAware", False
);
96 server
.atom
.XdndPosition
= XInternAtom(server
.dsp
, "XdndPosition", False
);
97 server
.atom
.XdndStatus
= XInternAtom(server
.dsp
, "XdndStatus", False
);
98 server
.atom
.XdndLeave
= XInternAtom(server
.dsp
, "XdndLeave", False
);
102 void cleanup_server()
104 if (server
.colormap
) XFreeColormap(server
.dsp
, server
.colormap
);
105 if (server
.colormap32
) XFreeColormap(server
.dsp
, server
.colormap32
);
106 if (server
.monitor
) {
108 for (i
=0; i
<server
.nb_monitor
; ++i
)
109 if (server
.monitor
[i
].names
)
110 g_strfreev(server
.monitor
[i
].names
);
111 free(server
.monitor
);
113 if (server
.gc
) XFreeGC(server
.dsp
, server
.gc
);
117 void send_event32 (Window win
, Atom at
, long data1
, long data2
, long data3
)
121 event
.xclient
.type
= ClientMessage
;
122 event
.xclient
.serial
= 0;
123 event
.xclient
.send_event
= True
;
124 event
.xclient
.display
= server
.dsp
;
125 event
.xclient
.window
= win
;
126 event
.xclient
.message_type
= at
;
128 event
.xclient
.format
= 32;
129 event
.xclient
.data
.l
[0] = data1
;
130 event
.xclient
.data
.l
[1] = data2
;
131 event
.xclient
.data
.l
[2] = data3
;
132 event
.xclient
.data
.l
[3] = 0;
133 event
.xclient
.data
.l
[4] = 0;
135 XSendEvent(server
.dsp
, server
.root_win
, False
, SubstructureRedirectMask
|SubstructureNotifyMask
, &event
);
139 int get_property32 (Window win
, Atom at
, Atom type
)
142 int format_ret
= 0, data
= 0;
143 unsigned long nitems_ret
= 0;
144 unsigned long bafter_ret
= 0;
145 unsigned char *prop_value
= 0;
150 result
= XGetWindowProperty(server
.dsp
, win
, at
, 0, 0x7fffffff, False
, type
, &type_ret
, &format_ret
, &nitems_ret
, &bafter_ret
, &prop_value
);
152 if (result
== Success
&& prop_value
) {
153 data
= ((gulong
*)prop_value
)[0];
160 void *server_get_property (Window win
, Atom at
, Atom type
, int *num_results
)
164 unsigned long nitems_ret
= 0;
165 unsigned long bafter_ret
= 0;
166 unsigned char *prop_value
;
171 result
= XGetWindowProperty(server
.dsp
, win
, at
, 0, 0x7fffffff, False
, type
, &type_ret
, &format_ret
, &nitems_ret
, &bafter_ret
, &prop_value
);
173 /* Send back resultcount */
174 if (num_results
) *num_results
= nitems_ret
;
176 if (result
== Success
&& prop_value
) return prop_value
;
181 void get_root_pixmap()
189 res
= server_get_property (server
.root_win
, server
.atom
._XROOTPMAP_ID
, XA_PIXMAP
, 0);
191 ret
= *((Pixmap
*)res
);
196 server
.root_pmap
= ret
;
198 if (server
.root_pmap
== None
)
199 fprintf(stderr
, "tint2 : pixmap background detection failed\n");
204 gcv
.fill_style
= FillTiled
;
205 uint mask
= GCTileStipXOrigin
| GCTileStipYOrigin
| GCFillStyle
| GCTile
;
207 gcv
.tile
= server
.root_pmap
;
208 XChangeGC(server
.dsp
, server
.gc
, mask
, &gcv
);
213 int compareMonitorPos(const void *monitor1
, const void *monitor2
)
215 Monitor
*m1
= (Monitor
*)monitor1
;
216 Monitor
*m2
= (Monitor
*)monitor2
;
221 else if (m1
->x
> m2
->x
) {
224 else if (m1
->y
< m2
->y
) {
227 else if (m1
->y
> m2
->y
) {
236 int compareMonitorIncluded(const void *monitor1
, const void *monitor2
)
238 Monitor
*m1
= (Monitor
*)monitor1
;
239 Monitor
*m2
= (Monitor
*)monitor2
;
241 if (m1
->x
>= m2
->x
&& m1
->y
>= m2
->y
&& (m1
->x
+m1
->width
) <= (m2
->x
+m2
->width
) && (m1
->y
+m1
->height
) <= (m2
->y
+m2
->height
)) {
242 // m1 included inside m2
254 if (XineramaIsActive(server
.dsp
)) {
255 XineramaScreenInfo
*info
= XineramaQueryScreens(server
.dsp
, &nbmonitor
);
256 XRRScreenResources
*res
= XRRGetScreenResourcesCurrent(server
.dsp
, server
.root_win
);
258 if (res
&& res
->ncrtc
>= nbmonitor
) {
259 // use xrandr to identify monitors (does not work with proprietery nvidia drivers)
260 printf("xRandr: Found crtc's: %d\n", res
->ncrtc
);
261 server
.monitor
= malloc(res
->ncrtc
* sizeof(Monitor
));
262 for (i
=0; i
<res
->ncrtc
; ++i
) {
263 XRRCrtcInfo
* crtc_info
= XRRGetCrtcInfo(server
.dsp
, res
, res
->crtcs
[i
]);
264 server
.monitor
[i
].x
= crtc_info
->x
;
265 server
.monitor
[i
].y
= crtc_info
->y
;
266 server
.monitor
[i
].width
= crtc_info
->width
;
267 server
.monitor
[i
].height
= crtc_info
->height
;
268 server
.monitor
[i
].names
= malloc((crtc_info
->noutput
+1) * sizeof(char*));
269 for (j
=0; j
<crtc_info
->noutput
; ++j
) {
270 XRROutputInfo
* output_info
= XRRGetOutputInfo(server
.dsp
, res
, crtc_info
->outputs
[j
]);
271 printf("xRandr: Linking output %s with crtc %d\n", output_info
->name
, i
);
272 server
.monitor
[i
].names
[j
] = g_strdup(output_info
->name
);
273 XRRFreeOutputInfo(output_info
);
275 server
.monitor
[i
].names
[j
] = 0;
276 XRRFreeCrtcInfo(crtc_info
);
278 nbmonitor
= res
->ncrtc
;
280 else if (info
&& nbmonitor
> 0) {
281 server
.monitor
= malloc(nbmonitor
* sizeof(Monitor
));
282 for (i
=0 ; i
< nbmonitor
; i
++) {
283 server
.monitor
[i
].x
= info
[i
].x_org
;
284 server
.monitor
[i
].y
= info
[i
].y_org
;
285 server
.monitor
[i
].width
= info
[i
].width
;
286 server
.monitor
[i
].height
= info
[i
].height
;
287 server
.monitor
[i
].names
= 0;
292 qsort(server
.monitor
, nbmonitor
, sizeof(Monitor
), compareMonitorIncluded
);
294 // remove monitor included into another one
296 while (i
< nbmonitor
) {
297 for (j
=0; j
< i
; j
++) {
298 if (compareMonitorIncluded(&server
.monitor
[i
], &server
.monitor
[j
]) > 0) {
305 for (j
=i
; j
<server
.nb_monitor
; ++j
)
306 if (server
.monitor
[j
].names
)
307 g_strfreev(server
.monitor
[j
].names
);
308 server
.nb_monitor
= i
;
309 server
.monitor
= realloc(server
.monitor
, server
.nb_monitor
* sizeof(Monitor
));
310 qsort(server
.monitor
, server
.nb_monitor
, sizeof(Monitor
), compareMonitorPos
);
313 XRRFreeScreenResources(res
);
317 if (!server
.nb_monitor
) {
318 server
.nb_monitor
= 1;
319 server
.monitor
= malloc(sizeof(Monitor
));
320 server
.monitor
[0].x
= server
.monitor
[0].y
= 0;
321 server
.monitor
[0].width
= DisplayWidth (server
.dsp
, server
.screen
);
322 server
.monitor
[0].height
= DisplayHeight (server
.dsp
, server
.screen
);
323 server
.monitor
[0].names
= 0;
332 // detect number of desktops
333 // wait 15s to leave some time for window manager startup
334 for (i
=0 ; i
< 15 ; i
++) {
335 server
.nb_desktop
= server_get_number_of_desktop ();
336 if (server
.nb_desktop
> 0) break;
339 if (server
.nb_desktop
== 0) {
340 server
.nb_desktop
= 1;
341 fprintf(stderr
, "warning : WM doesn't respect NETWM specs. tint2 default to 1 desktop.\n");
346 void server_init_visual()
348 // inspired by freedesktops fdclock ;)
350 XVisualInfo templ
= { .screen
=server
.screen
, .depth
=32, .class=TrueColor
};
352 xvi
= XGetVisualInfo(server
.dsp
, VisualScreenMask
|VisualDepthMask
|VisualClassMask
, &templ
, &nvi
);
357 XRenderPictFormat
*format
;
358 for (i
= 0; i
< nvi
; i
++) {
359 format
= XRenderFindVisualFormat(server
.dsp
, xvi
[i
].visual
);
360 if (format
->type
== PictTypeDirect
&& format
->direct
.alphaMask
) {
361 visual
= xvi
[i
].visual
;
368 // check composite manager
369 server
.composite_manager
= XGetSelectionOwner(server
.dsp
, server
.atom
._NET_WM_CM_S0
);
371 XFreeColormap(server
.dsp
, server
.colormap
);
372 if (server
.colormap32
)
373 XFreeColormap(server
.dsp
, server
.colormap32
);
376 server
.visual32
= visual
;
377 server
.colormap32
= XCreateColormap(server
.dsp
, server
.root_win
, visual
, AllocNone
);
380 if (visual
&& server
.composite_manager
!= None
&& snapshot_path
== 0) {
381 XSetWindowAttributes attrs
;
382 attrs
.event_mask
= StructureNotifyMask
;
383 XChangeWindowAttributes (server
.dsp
, server
.composite_manager
, CWEventMask
, &attrs
);
385 server
.real_transparency
= 1;
387 printf("real transparency on... depth: %d\n", server
.depth
);
388 server
.colormap
= XCreateColormap(server
.dsp
, server
.root_win
, visual
, AllocNone
);
389 server
.visual
= visual
;
392 // no composite manager or snapshot mode => fake transparency
393 server
.real_transparency
= 0;
394 server
.depth
= DefaultDepth(server
.dsp
, server
.screen
);
395 printf("real transparency off.... depth: %d\n", server
.depth
);
396 server
.colormap
= DefaultColormap(server
.dsp
, server
.screen
);
397 server
.visual
= DefaultVisual(server
.dsp
, server
.screen
);