]> Dogcows Code - chaz/tint2/blob - src/server.c
9b09010506e2bc53323303ecb0df5eaae8d20a44
[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 <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "server.h"
26 #include "task.h"
27 #include "window.h"
28
29 void server_catch_error (Display *d, XErrorEvent *ev){}
30
31 static char *name_trayer = 0;
32
33
34 void server_init_atoms ()
35 {
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);
76
77 // systray protocol
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);
86
87 // drag 'n' drop
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);
91 }
92
93
94 void cleanup_server()
95 {
96 if (name_trayer) free(name_trayer);
97 }
98
99
100 void send_event32 (Window win, Atom at, long data1, long data2, long data3)
101 {
102 XEvent event;
103
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;
110
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;
117
118 XSendEvent(server.dsp, server.root_win, False, SubstructureRedirectMask|SubstructureNotifyMask, &event);
119 }
120
121
122 int get_property32 (Window win, Atom at, Atom type)
123 {
124 Atom type_ret;
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;
129 int result;
130
131 if (!win) return 0;
132
133 result = XGetWindowProperty(server.dsp, win, at, 0, 0x7fffffff, False, type, &type_ret, &format_ret, &nitems_ret, &bafter_ret, &prop_value);
134
135 if (result == Success && prop_value) {
136 data = ((gulong*)prop_value)[0];
137 XFree (prop_value);
138 }
139 return data;
140 }
141
142
143 void *server_get_property (Window win, Atom at, Atom type, int *num_results)
144 {
145 Atom type_ret;
146 int format_ret = 0;
147 unsigned long nitems_ret = 0;
148 unsigned long bafter_ret = 0;
149 unsigned char *prop_value;
150 int result;
151
152 if (!win) return 0;
153
154 result = XGetWindowProperty(server.dsp, win, at, 0, 0x7fffffff, False, type, &type_ret, &format_ret, &nitems_ret, &bafter_ret, &prop_value);
155
156 /* Send back resultcount */
157 if (num_results) *num_results = nitems_ret;
158
159 if (result == Success && prop_value) return prop_value;
160 else return 0;
161 }
162
163
164 void get_root_pixmap()
165 {
166 Pixmap ret = None;
167
168 unsigned long *res;
169 int c = 2;
170
171 do {
172 res = server_get_property (server.root_win, server.atom._XROOTPMAP_ID, XA_PIXMAP, 0);
173 if (res) {
174 ret = *((Pixmap*)res);
175 XFree(res);
176 break;
177 }
178 } while (--c > 0);
179 server.root_pmap = ret;
180
181 if (server.root_pmap == None)
182 fprintf(stderr, "tint2 : pixmap background detection failed\n");
183 else {
184 XGCValues gcv;
185 gcv.ts_x_origin = 0;
186 gcv.ts_y_origin = 0;
187 gcv.fill_style = FillTiled;
188 uint mask = GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle | GCTile;
189
190 gcv.tile = server.root_pmap;
191 XChangeGC(server.dsp, server.gc, mask, &gcv);
192 }
193 }
194
195
196 int compareMonitor(const void *monitor1, const void *monitor2)
197 {
198 Monitor *m1 = (Monitor*)monitor1;
199 Monitor *m2 = (Monitor*)monitor2;
200
201 if (m1->x < m2->x) {
202 return -1;
203 }
204 else
205 if (m1->x > m2->x) {
206 return 1;
207 }
208 else
209 if (m1->width < m2->width) {
210 return 1;
211 }
212 else
213 if (m1->width > m2->width) {
214 return -1;
215 }
216 else {
217 return 0;
218 }
219 }
220
221
222 void get_monitors()
223 {
224 if (server.monitor) free(server.monitor);
225 server.nb_monitor = 0;
226 server.monitor = 0;
227
228 int i, nb_monitor;
229 if (XineramaIsActive(server.dsp)) {
230 XineramaScreenInfo *info = XineramaQueryScreens(server.dsp, &nb_monitor);
231
232 if (info) {
233 int nb=0, j;
234
235 i = 0;
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
242 break;
243 }
244 else {
245 // doesn't count monitor 'i' because it's included into another one
246 //fprintf(stderr, "monitor %d included into another one\n", i);
247 goto next;
248 }
249 }
250 }
251
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;
256 nb++;
257 next:
258 i++;
259 }
260 XFree(info);
261 server.nb_monitor = nb;
262
263 // ordered monitor according to coordinate
264 qsort(server.monitor, server.nb_monitor, sizeof(Monitor), compareMonitor);
265 }
266 }
267
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);
274 }
275 }
276
277
278 void get_desktops()
279 {
280 int i;
281
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;
287 sleep(1);
288 }
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");
292 }
293 fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", server.nb_monitor, server.nb_desktop);
294 }
295
296
This page took 0.048595 seconds and 4 git commands to generate.