]> Dogcows Code - chaz/tint2/blob - src/server.c
1c7b4cda18e821d3ad9c4657d539567ffb10b253
[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._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);
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
88
89 void cleanup_server()
90 {
91 if (name_trayer) free(name_trayer);
92 }
93
94
95 void send_event32 (Window win, Atom at, long data1, long data2, long data3)
96 {
97 XEvent event;
98
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;
105
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;
112
113 XSendEvent(server.dsp, server.root_win, False, SubstructureRedirectMask|SubstructureNotifyMask, &event);
114 }
115
116
117 int get_property32 (Window win, Atom at, Atom type)
118 {
119 Atom type_ret;
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;
124 int result;
125
126 if (!win) return 0;
127
128 result = XGetWindowProperty(server.dsp, win, at, 0, 0x7fffffff, False, type, &type_ret, &format_ret, &nitems_ret, &bafter_ret, &prop_value);
129
130 if (result == Success && prop_value) {
131 data = ((gulong*)prop_value)[0];
132 XFree (prop_value);
133 }
134 return data;
135 }
136
137
138 void *server_get_property (Window win, Atom at, Atom type, int *num_results)
139 {
140 Atom type_ret;
141 int format_ret = 0;
142 unsigned long nitems_ret = 0;
143 unsigned long bafter_ret = 0;
144 unsigned char *prop_value;
145 int result;
146
147 if (!win) return 0;
148
149 result = XGetWindowProperty(server.dsp, win, at, 0, 0x7fffffff, False, type, &type_ret, &format_ret, &nitems_ret, &bafter_ret, &prop_value);
150
151 /* Send back resultcount */
152 if (num_results) *num_results = nitems_ret;
153
154 if (result == Success && prop_value) return prop_value;
155 else return 0;
156 }
157
158
159 void get_root_pixmap()
160 {
161 Pixmap ret = None;
162
163 unsigned long *res;
164 int c = 2;
165
166 do {
167 res = server_get_property (server.root_win, server.atom._XROOTPMAP_ID, XA_PIXMAP, 0);
168 if (res) {
169 ret = *((Pixmap*)res);
170 XFree(res);
171 break;
172 }
173 } while (--c > 0);
174 server.root_pmap = ret;
175
176 if (server.root_pmap == None)
177 fprintf(stderr, "tint2 : pixmap background detection failed\n");
178 else {
179 XGCValues gcv;
180 gcv.ts_x_origin = 0;
181 gcv.ts_y_origin = 0;
182 gcv.fill_style = FillTiled;
183 uint mask = GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle | GCTile;
184
185 gcv.tile = server.root_pmap;
186 XChangeGC(server.dsp, server.gc, mask, &gcv);
187 }
188 }
189
190
191 int compareMonitor(const void *monitor1, const void *monitor2)
192 {
193 Monitor *m1 = (Monitor*)monitor1;
194 Monitor *m2 = (Monitor*)monitor2;
195
196 if (m1->x < m2->x) {
197 return -1;
198 }
199 else
200 if (m1->x > m2->x) {
201 return 1;
202 }
203 else
204 if (m1->width < m2->width) {
205 return 1;
206 }
207 else
208 if (m1->width > m2->width) {
209 return -1;
210 }
211 else {
212 return 0;
213 }
214 }
215
216
217 void get_monitors_and_desktops()
218 {
219 int i;
220
221 if (server.monitor) free(server.monitor);
222 server.nb_monitor = 0;
223 server.monitor = 0;
224
225 int nb_monitor;
226 if (XineramaIsActive(server.dsp)) {
227 XineramaScreenInfo *info = XineramaQueryScreens(server.dsp, &nb_monitor);
228
229 if (info) {
230 int nb=0, j;
231
232 i = 0;
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
239 break;
240 }
241 else {
242 // doesn't count monitor 'i' because it's included into another one
243 //fprintf(stderr, "monitor %d included into another one\n", i);
244 goto next;
245 }
246 }
247 }
248
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;
253 nb++;
254 next:
255 i++;
256 }
257 XFree(info);
258 server.nb_monitor = nb;
259
260 // ordered monitor according to coordinate
261 qsort(server.monitor, server.nb_monitor, sizeof(Monitor), compareMonitor);
262 }
263 }
264
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);
271 }
272
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;
278 sleep(1);
279 }
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");
283 }
284 fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", server.nb_monitor, server.nb_desktop);
285 }
286
287
This page took 0.052538 seconds and 4 git commands to generate.