]> Dogcows Code - chaz/tint2/blob - src/server.c
fixed Issue 282
[chaz/tint2] / src / server.c
1 /**************************************************************************
2 *
3 * Tint2 panel
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
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.
11 *
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 **************************************************************************/
20
21 #include <X11/extensions/Xrender.h>
22 #include <X11/extensions/Xrandr.h>
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #include "server.h"
30 #include "config.h"
31 #include "task.h"
32 #include "window.h"
33
34 void server_catch_error (Display *d, XErrorEvent *ev){}
35
36 void server_init_atoms ()
37 {
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);
82
83 // systray protocol
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);
92 g_free(name_trayer);
93
94 // drag 'n' drop
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);
99 }
100
101
102 void cleanup_server()
103 {
104 if (server.colormap) XFreeColormap(server.dsp, server.colormap);
105 if (server.colormap32) XFreeColormap(server.dsp, server.colormap32);
106 if (server.monitor) {
107 int i;
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);
112 }
113 if (server.gc) XFreeGC(server.dsp, server.gc);
114 }
115
116
117 void send_event32 (Window win, Atom at, long data1, long data2, long data3)
118 {
119 XEvent event;
120
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;
127
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;
134
135 XSendEvent(server.dsp, server.root_win, False, SubstructureRedirectMask|SubstructureNotifyMask, &event);
136 }
137
138
139 int get_property32 (Window win, Atom at, Atom type)
140 {
141 Atom type_ret;
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;
146 int result;
147
148 if (!win) return 0;
149
150 result = XGetWindowProperty(server.dsp, win, at, 0, 0x7fffffff, False, type, &type_ret, &format_ret, &nitems_ret, &bafter_ret, &prop_value);
151
152 if (result == Success && prop_value) {
153 data = ((gulong*)prop_value)[0];
154 XFree (prop_value);
155 }
156 return data;
157 }
158
159
160 void *server_get_property (Window win, Atom at, Atom type, int *num_results)
161 {
162 Atom type_ret;
163 int format_ret = 0;
164 unsigned long nitems_ret = 0;
165 unsigned long bafter_ret = 0;
166 unsigned char *prop_value;
167 int result;
168
169 if (!win) return 0;
170
171 result = XGetWindowProperty(server.dsp, win, at, 0, 0x7fffffff, False, type, &type_ret, &format_ret, &nitems_ret, &bafter_ret, &prop_value);
172
173 /* Send back resultcount */
174 if (num_results) *num_results = nitems_ret;
175
176 if (result == Success && prop_value) return prop_value;
177 else return 0;
178 }
179
180
181 void get_root_pixmap()
182 {
183 Pixmap ret = None;
184
185 unsigned long *res;
186 int c = 2;
187
188 do {
189 res = server_get_property (server.root_win, server.atom._XROOTPMAP_ID, XA_PIXMAP, 0);
190 if (res) {
191 ret = *((Pixmap*)res);
192 XFree(res);
193 break;
194 }
195 } while (--c > 0);
196 server.root_pmap = ret;
197
198 if (server.root_pmap == None)
199 fprintf(stderr, "tint2 : pixmap background detection failed\n");
200 else {
201 XGCValues gcv;
202 gcv.ts_x_origin = 0;
203 gcv.ts_y_origin = 0;
204 gcv.fill_style = FillTiled;
205 uint mask = GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle | GCTile;
206
207 gcv.tile = server.root_pmap;
208 XChangeGC(server.dsp, server.gc, mask, &gcv);
209 }
210 }
211
212
213 int compareMonitorPos(const void *monitor1, const void *monitor2)
214 {
215 Monitor *m1 = (Monitor*)monitor1;
216 Monitor *m2 = (Monitor*)monitor2;
217
218 if (m1->x < m2->x) {
219 return -1;
220 }
221 else if (m1->x > m2->x) {
222 return 1;
223 }
224 else if (m1->y < m2->y) {
225 return -1;
226 }
227 else if (m1->y > m2->y) {
228 return 1;
229 }
230 else {
231 return 0;
232 }
233 }
234
235
236 int compareMonitorIncluded(const void *monitor1, const void *monitor2)
237 {
238 Monitor *m1 = (Monitor*)monitor1;
239 Monitor *m2 = (Monitor*)monitor2;
240
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
243 return 1;
244 }
245 else {
246 return -1;
247 }
248 }
249
250
251 void get_monitors()
252 {
253 int i, j, nbmonitor;
254 if (XineramaIsActive(server.dsp)) {
255 XineramaScreenInfo *info = XineramaQueryScreens(server.dsp, &nbmonitor);
256 XRRScreenResources *res = XRRGetScreenResourcesCurrent(server.dsp, server.root_win);
257
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);
274 }
275 server.monitor[i].names[j] = 0;
276 XRRFreeCrtcInfo(crtc_info);
277 }
278 nbmonitor = res->ncrtc;
279 }
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;
288 }
289 }
290
291 // ordered monitor
292 qsort(server.monitor, nbmonitor, sizeof(Monitor), compareMonitorIncluded);
293
294 // remove monitor included into another one
295 i = 0;
296 while (i < nbmonitor) {
297 for (j=0; j < i ; j++) {
298 if (compareMonitorIncluded(&server.monitor[i], &server.monitor[j]) > 0) {
299 goto next;
300 }
301 }
302 i++;
303 }
304 next:
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);
311
312 if (res)
313 XRRFreeScreenResources(res);
314 XFree(info);
315 }
316
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;
324 }
325 }
326
327
328 void get_desktops()
329 {
330 int i;
331
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;
337 sleep(1);
338 }
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");
342 }
343 }
344
345
346 void server_init_visual()
347 {
348 // inspired by freedesktops fdclock ;)
349 XVisualInfo *xvi;
350 XVisualInfo templ = { .screen=server.screen, .depth=32, .class=TrueColor };
351 int nvi;
352 xvi = XGetVisualInfo(server.dsp, VisualScreenMask|VisualDepthMask|VisualClassMask, &templ, &nvi);
353
354 Visual *visual = 0;
355 if (xvi) {
356 int i;
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;
362 break;
363 }
364 }
365 }
366 XFree (xvi);
367
368 // check composite manager
369 server.composite_manager = XGetSelectionOwner(server.dsp, server.atom._NET_WM_CM_S0);
370 if (server.colormap)
371 XFreeColormap(server.dsp, server.colormap);
372 if (server.colormap32)
373 XFreeColormap(server.dsp, server.colormap32);
374
375 if (visual) {
376 server.visual32 = visual;
377 server.colormap32 = XCreateColormap(server.dsp, server.root_win, visual, AllocNone);
378 }
379
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);
384
385 server.real_transparency = 1;
386 server.depth = 32;
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;
390 }
391 else {
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);
398 }
399 }
This page took 0.056717 seconds and 4 git commands to generate.