wmaker: removed unused constant SCROLL_STEPS in the switchpanel code
[wmaker-crm.git] / src / wmspec.c
blobd6bb61c6ed47f4a0717f1f61ee00b1bcebd35961
1 /* wmspec.c-- support for the wm-spec Hints
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * TODO
24 * ----
26 * This file needs to be checked for all calls to XGetWindowProperty() and
27 * proper checks need to be made on the returned values. Only checking for
28 * return to be Success is not enough. -Dan
31 #include "wconfig.h"
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <string.h>
37 #include <WINGs/WUtil.h>
38 #include "WindowMaker.h"
39 #include "window.h"
40 #include "screen.h"
41 #include "workspace.h"
42 #include "framewin.h"
43 #include "actions.h"
44 #include "client.h"
45 #include "appicon.h"
46 #include "wmspec.h"
47 #include "icon.h"
48 #include "stacking.h"
49 #include "xinerama.h"
50 #include "properties.h"
53 /* Root Window Properties */
54 static Atom net_supported;
55 static Atom net_client_list;
56 static Atom net_client_list_stacking;
57 static Atom net_number_of_desktops;
58 static Atom net_desktop_geometry;
59 static Atom net_desktop_viewport;
60 static Atom net_current_desktop;
61 static Atom net_desktop_names;
62 static Atom net_active_window;
63 static Atom net_workarea;
64 static Atom net_supporting_wm_check;
65 static Atom net_virtual_roots; /* N/A */
66 static Atom net_desktop_layout; /* XXX */
67 static Atom net_showing_desktop;
69 /* Other Root Window Messages */
70 static Atom net_close_window;
71 static Atom net_moveresize_window; /* TODO */
72 static Atom net_wm_moveresize; /* TODO */
74 /* Application Window Properties */
75 static Atom net_wm_name;
76 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
77 static Atom net_wm_icon_name;
78 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
79 static Atom net_wm_desktop;
80 static Atom net_wm_window_type;
81 static Atom net_wm_window_type_desktop;
82 static Atom net_wm_window_type_dock;
83 static Atom net_wm_window_type_toolbar;
84 static Atom net_wm_window_type_menu;
85 static Atom net_wm_window_type_utility;
86 static Atom net_wm_window_type_splash;
87 static Atom net_wm_window_type_dialog;
88 static Atom net_wm_window_type_dropdown_menu;
89 static Atom net_wm_window_type_popup_menu;
90 static Atom net_wm_window_type_tooltip;
91 static Atom net_wm_window_type_notification;
92 static Atom net_wm_window_type_combo;
93 static Atom net_wm_window_type_dnd;
94 static Atom net_wm_window_type_normal;
95 static Atom net_wm_state;
96 static Atom net_wm_state_modal; /* XXX: what is this?!? */
97 static Atom net_wm_state_sticky;
98 static Atom net_wm_state_maximized_vert;
99 static Atom net_wm_state_maximized_horz;
100 static Atom net_wm_state_shaded;
101 static Atom net_wm_state_skip_taskbar;
102 static Atom net_wm_state_skip_pager;
103 static Atom net_wm_state_hidden;
104 static Atom net_wm_state_fullscreen;
105 static Atom net_wm_state_above;
106 static Atom net_wm_state_below;
107 static Atom net_wm_allowed_actions;
108 static Atom net_wm_action_move;
109 static Atom net_wm_action_resize;
110 static Atom net_wm_action_minimize;
111 static Atom net_wm_action_shade;
112 static Atom net_wm_action_stick;
113 static Atom net_wm_action_maximize_horz;
114 static Atom net_wm_action_maximize_vert;
115 static Atom net_wm_action_fullscreen;
116 static Atom net_wm_action_change_desktop;
117 static Atom net_wm_action_close;
118 static Atom net_wm_strut;
119 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
120 static Atom net_wm_icon_geometry; /* FIXME: should work together with net_wm_handled_icons, gnome-panel-2.2.0.1 doesn't use _NET_WM_HANDLED_ICONS, thus present situation. */
121 static Atom net_wm_icon;
122 static Atom net_wm_pid; /* TODO */
123 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
124 static Atom net_wm_window_opacity;
126 static Atom net_frame_extents;
128 /* Window Manager Protocols */
129 static Atom net_wm_ping; /* TODO */
131 static Atom utf8_string;
133 typedef struct {
134 char *name;
135 Atom *atom;
136 } atomitem_t;
138 static atomitem_t atomNames[] = {
139 {"_NET_SUPPORTED", &net_supported},
140 {"_NET_CLIENT_LIST", &net_client_list},
141 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
142 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
143 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
144 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
145 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
146 {"_NET_DESKTOP_NAMES", &net_desktop_names},
147 {"_NET_ACTIVE_WINDOW", &net_active_window},
148 {"_NET_WORKAREA", &net_workarea},
149 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
150 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
151 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
152 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
154 {"_NET_CLOSE_WINDOW", &net_close_window},
155 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
156 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
158 {"_NET_WM_NAME", &net_wm_name},
159 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
160 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
161 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
162 {"_NET_WM_DESKTOP", &net_wm_desktop},
163 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
164 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
165 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
166 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
167 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
168 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
169 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
170 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
171 {"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", &net_wm_window_type_dropdown_menu},
172 {"_NET_WM_WINDOW_TYPE_POPUP_MENU", &net_wm_window_type_popup_menu},
173 {"_NET_WM_WINDOW_TYPE_TOOLTIP", &net_wm_window_type_tooltip},
174 {"_NET_WM_WINDOW_TYPE_NOTIFICATION", &net_wm_window_type_notification},
175 {"_NET_WM_WINDOW_TYPE_COMBO", &net_wm_window_type_combo},
176 {"_NET_WM_WINDOW_TYPE_DND", &net_wm_window_type_dnd},
177 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
178 {"_NET_WM_STATE", &net_wm_state},
179 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
180 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
181 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
182 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
183 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
184 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
185 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
186 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
187 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
188 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
189 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
190 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
191 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
192 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
193 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
194 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
195 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
196 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
197 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
198 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
199 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
200 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
201 {"_NET_WM_STRUT", &net_wm_strut},
202 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
203 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
204 {"_NET_WM_ICON", &net_wm_icon},
205 {"_NET_WM_PID", &net_wm_pid},
206 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
207 {"_NET_WM_WINDOW_OPACITY", &net_wm_window_opacity},
209 {"_NET_FRAME_EXTENTS", &net_frame_extents},
211 {"_NET_WM_PING", &net_wm_ping},
213 {"UTF8_STRING", &utf8_string},
216 #define _NET_WM_STATE_REMOVE 0
217 #define _NET_WM_STATE_ADD 1
218 #define _NET_WM_STATE_TOGGLE 2
220 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
221 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
222 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
223 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
224 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
225 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
226 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
227 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
228 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
229 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
230 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
232 static void observer(void *self, WMNotification *notif);
233 static void wsobserver(void *self, WMNotification *notif);
235 static void updateClientList(WScreen *scr);
236 static void updateClientListStacking(WScreen *scr, WWindow *);
238 static void updateWorkspaceNames(WScreen *scr);
239 static void updateCurrentWorkspace(WScreen *scr);
240 static void updateWorkspaceCount(WScreen *scr);
241 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
243 typedef struct NetData {
244 WScreen *scr;
245 WReservedArea *strut;
246 WWindow **show_desktop;
247 } NetData;
249 static void setSupportedHints(WScreen *scr)
251 Atom atom[wlengthof(atomNames)];
252 int i = 0;
254 /* set supported hints list */
255 /* XXX: extend this !!! */
257 atom[i++] = net_client_list;
258 atom[i++] = net_client_list_stacking;
259 atom[i++] = net_number_of_desktops;
260 atom[i++] = net_desktop_geometry;
261 atom[i++] = net_desktop_viewport;
262 atom[i++] = net_current_desktop;
263 atom[i++] = net_desktop_names;
264 atom[i++] = net_active_window;
265 atom[i++] = net_workarea;
266 atom[i++] = net_supporting_wm_check;
267 atom[i++] = net_showing_desktop;
268 #if 0
269 atom[i++] = net_wm_moveresize;
270 #endif
271 atom[i++] = net_wm_desktop;
272 atom[i++] = net_wm_window_type;
273 atom[i++] = net_wm_window_type_desktop;
274 atom[i++] = net_wm_window_type_dock;
275 atom[i++] = net_wm_window_type_toolbar;
276 atom[i++] = net_wm_window_type_menu;
277 atom[i++] = net_wm_window_type_utility;
278 atom[i++] = net_wm_window_type_splash;
279 atom[i++] = net_wm_window_type_dialog;
280 atom[i++] = net_wm_window_type_dropdown_menu;
281 atom[i++] = net_wm_window_type_popup_menu;
282 atom[i++] = net_wm_window_type_tooltip;
283 atom[i++] = net_wm_window_type_notification;
284 atom[i++] = net_wm_window_type_combo;
285 atom[i++] = net_wm_window_type_dnd;
286 atom[i++] = net_wm_window_type_normal;
288 atom[i++] = net_wm_state;
289 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
290 atom[i++] = net_wm_state_sticky;
291 atom[i++] = net_wm_state_shaded;
292 atom[i++] = net_wm_state_maximized_horz;
293 atom[i++] = net_wm_state_maximized_vert;
294 atom[i++] = net_wm_state_skip_taskbar;
295 atom[i++] = net_wm_state_skip_pager;
296 atom[i++] = net_wm_state_hidden;
297 atom[i++] = net_wm_state_fullscreen;
298 atom[i++] = net_wm_state_above;
299 atom[i++] = net_wm_state_below;
301 atom[i++] = net_wm_allowed_actions;
302 atom[i++] = net_wm_action_move;
303 atom[i++] = net_wm_action_resize;
304 atom[i++] = net_wm_action_minimize;
305 atom[i++] = net_wm_action_shade;
306 atom[i++] = net_wm_action_stick;
307 atom[i++] = net_wm_action_maximize_horz;
308 atom[i++] = net_wm_action_maximize_vert;
309 atom[i++] = net_wm_action_fullscreen;
310 atom[i++] = net_wm_action_change_desktop;
311 atom[i++] = net_wm_action_close;
313 atom[i++] = net_wm_strut;
314 atom[i++] = net_wm_icon_geometry;
315 atom[i++] = net_wm_icon;
316 atom[i++] = net_wm_handled_icons;
317 atom[i++] = net_wm_window_opacity;
319 atom[i++] = net_frame_extents;
321 atom[i++] = net_wm_name;
322 atom[i++] = net_wm_icon_name;
324 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
326 /* set supporting wm hint */
327 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
328 PropModeReplace, (unsigned char *)&scr->info_window, 1);
330 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
331 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
334 void wNETWMUpdateDesktop(WScreen *scr)
336 long *views, sizes[2];
337 int count, i;
339 if (scr->workspace_count == 0)
340 return;
342 count = scr->workspace_count * 2;
343 views = wmalloc(sizeof(long) * count);
344 sizes[0] = scr->scr_width;
345 sizes[1] = scr->scr_height;
347 for (i = 0; i < scr->workspace_count; i++) {
348 views[2 * i + 0] = 0;
349 views[2 * i + 1] = 0;
352 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
353 PropModeReplace, (unsigned char *)sizes, 2);
355 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
356 PropModeReplace, (unsigned char *)views, count);
358 wfree(views);
361 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
363 int count;
364 unsigned char *prop;
366 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
367 if (prop) {
368 int desktop = *(long *)prop;
369 XFree(prop);
370 return desktop;
372 return -1;
376 * Find the best icon to be used by Window Maker for appicon/miniwindows.
377 * Currently the algorithm is to take the image with the size closest
378 * to icon_size x icon_size, but never bigger than that.
380 * This algorithm is very poorly implemented and needs to be redone (it can
381 * easily select images with very large widths and very small heights over
382 * square images, if the area of the former is closer to the desired one).
384 * The logic can also be changed to accept bigger images and scale them down.
386 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
388 int size, wanted, d;
389 unsigned long i, distance;
390 unsigned long *icon;
392 /* better use only 75% of icon_size. For 64x64 this means 48x48
393 * This leaves room around the icon for the miniwindow title and
394 * results in better overall aesthetics -Dan */
395 wanted = wPreferences.icon_size * wPreferences.icon_size;
397 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
398 size = data[i] * data[i + 1];
399 if (size == 0)
400 break;
401 d = wanted - size;
402 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
403 distance = d;
404 icon = &data[i];
406 i += size + 2;
409 return icon;
412 static RImage *makeRImageFromARGBData(unsigned long *data)
414 int size, width, height, i;
415 RImage *image;
416 unsigned char *imgdata;
417 unsigned long pixel;
419 width = data[0];
420 height = data[1];
421 size = width * height;
423 if (size == 0)
424 return NULL;
426 image = RCreateImage(width, height, True);
428 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
429 pixel = data[i];
430 imgdata[3] = (pixel >> 24) & 0xff; /* A */
431 imgdata[0] = (pixel >> 16) & 0xff; /* R */
432 imgdata[1] = (pixel >> 8) & 0xff; /* G */
433 imgdata[2] = (pixel >> 0) & 0xff; /* B */
436 return image;
439 RImage *get_window_image_from_x11(Window window)
441 RImage *image;
442 Atom type;
443 int format;
444 unsigned long items, rest;
445 unsigned long *property, *data;
447 /* Get the icon from X11 Window */
448 if (XGetWindowProperty(dpy, window, net_wm_icon, 0L, LONG_MAX,
449 False, XA_CARDINAL, &type, &format, &items, &rest,
450 (unsigned char **)&property) != Success || !property)
451 return NULL;
453 if (type != XA_CARDINAL || format != 32 || items < 2) {
454 XFree(property);
455 return NULL;
458 /* Find the best icon */
459 data = findBestIcon(property, items);
460 if (!data) {
461 XFree(property);
462 return NULL;
465 /* Save the best icon in the X11 icon */
466 image = makeRImageFromARGBData(data);
468 XFree(property);
470 /* Resize the image to the correct value */
471 image = wIconValidateIconSize(image, wPreferences.icon_size);
473 return image;
476 static void updateIconImage(WWindow *wwin)
478 /* Remove the icon image from X11 */
479 if (wwin->net_icon_image)
480 RReleaseImage(wwin->net_icon_image);
482 /* Save the icon in the X11 icon */
483 wwin->net_icon_image = get_window_image_from_x11(wwin->client_win);
485 /* Refresh the Window Icon */
486 if (wwin->icon)
487 wIconUpdate(wwin->icon);
489 /* Refresh the application icon */
490 WApplication *app = wApplicationOf(wwin->main_window);
491 if (app && app->app_icon) {
492 wIconUpdate(app->app_icon->icon);
493 wAppIconPaint(app->app_icon);
497 static void updateWindowOpacity(WWindow *wwin)
499 Atom type;
500 int format;
501 unsigned long items, rest;
502 unsigned long *property;
504 if (!wwin->frame)
505 return;
507 /* We don't care about this ourselves, but other programs need us to copy
508 * this to the frame window. */
509 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_opacity, 0L, 1L,
510 False, XA_CARDINAL, &type, &format, &items, &rest,
511 (unsigned char **)&property) != Success)
512 return;
514 if (type == None) {
515 XDeleteProperty(dpy, wwin->frame->core->window, net_wm_window_opacity);
516 } else if (type == XA_CARDINAL && format == 32 && items == 1 && property) {
517 XChangeProperty(dpy, wwin->frame->core->window, net_wm_window_opacity,
518 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)property, 1L);
521 if (property)
522 XFree(property);
525 static void updateShowDesktop(WScreen *scr, Bool show)
527 long foo;
529 foo = (show == True);
530 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
531 PropModeReplace, (unsigned char *)&foo, 1);
534 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
536 if (show && scr->netdata->show_desktop == NULL) {
537 WWindow *tmp, **wins;
538 int i = 0;
540 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
542 tmp = scr->focused_window;
543 while (tmp) {
544 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
546 wins[i++] = tmp;
547 tmp->flags.skip_next_animation = 1;
548 tmp->flags.net_show_desktop = 1;
549 wIconifyWindow(tmp);
552 tmp = tmp->prev;
554 wins[i++] = NULL;
556 scr->netdata->show_desktop = wins;
557 updateShowDesktop(scr, True);
558 } else if (scr->netdata->show_desktop != NULL) {
559 /* FIXME: get rid of workspace flashing ! */
560 int ws = scr->current_workspace;
561 WWindow **tmp;
562 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
563 wDeiconifyWindow(*tmp);
564 (*tmp)->flags.net_show_desktop = 0;
566 if (ws != scr->current_workspace)
567 wWorkspaceChange(scr, ws);
568 wfree(scr->netdata->show_desktop);
569 scr->netdata->show_desktop = NULL;
570 updateShowDesktop(scr, False);
574 void wNETWMInitStuff(WScreen *scr)
576 NetData *data;
577 int i;
579 #ifdef DEBUG_WMSPEC
580 wmessage("wNETWMInitStuff");
581 #endif
583 #ifdef HAVE_XINTERNATOMS
585 Atom atoms[wlengthof(atomNames)];
586 char *names[wlengthof(atomNames)];
588 for (i = 0; i < wlengthof(atomNames); ++i)
589 names[i] = atomNames[i].name;
591 XInternAtoms(dpy, &names[0], wlengthof(atomNames), False, atoms);
592 for (i = 0; i < wlengthof(atomNames); ++i)
593 *atomNames[i].atom = atoms[i];
596 #else
597 for (i = 0; i < wlengthof(atomNames); i++)
598 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
599 #endif
601 data = wmalloc(sizeof(NetData));
602 data->scr = scr;
603 data->strut = NULL;
604 data->show_desktop = NULL;
606 scr->netdata = data;
608 setSupportedHints(scr);
610 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
611 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
612 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
613 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
614 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
615 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
616 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
618 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
619 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
620 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
621 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
623 updateClientList(scr);
624 updateClientListStacking(scr, NULL);
625 updateWorkspaceCount(scr);
626 updateWorkspaceNames(scr);
627 updateShowDesktop(scr, False);
629 wScreenUpdateUsableArea(scr);
632 void wNETWMCleanup(WScreen *scr)
634 int i;
636 for (i = 0; i < wlengthof(atomNames); i++)
637 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
640 void wNETWMUpdateActions(WWindow *wwin, Bool del)
642 Atom action[10]; /* nr of actions atoms defined */
643 int i = 0;
645 if (del) {
646 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
647 return;
650 if (IS_MOVABLE(wwin))
651 action[i++] = net_wm_action_move;
653 if (IS_RESIZABLE(wwin))
654 action[i++] = net_wm_action_resize;
656 if (!WFLAGP(wwin, no_miniaturizable))
657 action[i++] = net_wm_action_minimize;
659 if (!WFLAGP(wwin, no_shadeable))
660 action[i++] = net_wm_action_shade;
662 /* if (!WFLAGP(wwin, no_stickable)) */
663 action[i++] = net_wm_action_stick;
665 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
666 if (IS_RESIZABLE(wwin))
667 action[i++] = net_wm_action_maximize_horz;
669 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
670 if (IS_RESIZABLE(wwin))
671 action[i++] = net_wm_action_maximize_vert;
673 /* if (!WFLAGP(wwin, no_fullscreen)) */
674 action[i++] = net_wm_action_fullscreen;
676 /* if (!WFLAGP(wwin, no_change_desktop)) */
677 action[i++] = net_wm_action_change_desktop;
679 if (!WFLAGP(wwin, no_closable))
680 action[i++] = net_wm_action_close;
682 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
683 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
686 void wNETWMUpdateWorkarea(WScreen *scr)
688 long *area;
689 int count, i;
691 if (!scr->netdata || scr->workspace_count == 0 || !scr->usableArea)
692 return;
694 count = scr->workspace_count * 4;
695 area = wmalloc(sizeof(long) * count);
697 for (i = 0; i < scr->workspace_count; i++) {
698 area[4 * i + 0] = scr->usableArea[0].x1;
699 area[4 * i + 1] = scr->usableArea[0].y1;
700 area[4 * i + 2] = scr->usableArea[0].x2 - scr->usableArea[0].x1;
701 area[4 * i + 3] = scr->usableArea[0].y2 - scr->usableArea[0].y1;
704 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
705 PropModeReplace, (unsigned char *)area, count);
706 wfree(area);
709 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
711 WReservedArea *cur;
712 WMRect rect;
714 if (!scr->netdata || !scr->netdata->strut)
715 return False;
717 area->x1 = area->y1 = area->x2 = area->y2 = 0;
719 for (cur = scr->netdata->strut; cur; cur = cur->next) {
720 WWindow *wwin = wWindowFor(cur->window);
721 if (wWindowTouchesHead(wwin, head)) {
722 if (cur->area.x1 > area->x1)
723 area->x1 = cur->area.x1;
724 if (cur->area.y1 > area->y1)
725 area->y1 = cur->area.y1;
726 if (cur->area.x2 > area->x2)
727 area->x2 = cur->area.x2;
728 if (cur->area.y2 > area->y2)
729 area->y2 = cur->area.y2;
733 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
734 return False;
736 rect = wGetRectForHead(scr, head);
738 area->x1 = rect.pos.x + area->x1;
739 area->x2 = rect.pos.x + rect.size.width - area->x2;
740 area->y1 = rect.pos.y + area->y1;
741 area->y2 = rect.pos.y + rect.size.height - area->y2;
743 return True;
746 static void updateClientList(WScreen *scr)
748 WWindow *wwin;
749 Window *windows;
750 int count;
752 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
754 count = 0;
755 wwin = scr->focused_window;
756 while (wwin) {
757 windows[count++] = wwin->client_win;
758 wwin = wwin->prev;
760 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
761 PropModeReplace, (unsigned char *)windows, count);
763 wfree(windows);
764 XFlush(dpy);
767 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
769 WWindow *wwin;
770 Window *client_list, *client_list_reverse;
771 int client_count, i;
772 WCoreWindow *tmp;
773 WMBagIterator iter;
775 /* update client list */
776 i = scr->window_count + 1;
777 client_list = (Window *) wmalloc(sizeof(Window) * i);
778 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
780 client_count = 0;
781 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
782 while (tmp) {
783 wwin = wWindowFor(tmp->window);
784 /* wwin_excl is a window to exclude from the list
785 (e.g. it's now unmanaged) */
786 if (wwin && (wwin != wwin_excl))
787 client_list[client_count++] = wwin->client_win;
788 tmp = tmp->stacking->under;
792 for (i = 0; i < client_count; i++) {
793 Window w = client_list[client_count - i - 1];
794 client_list_reverse[i] = w;
797 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
798 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
800 wfree(client_list);
801 wfree(client_list_reverse);
803 XFlush(dpy);
806 static void updateWorkspaceCount(WScreen *scr)
807 { /* changeable */
808 long count;
810 count = scr->workspace_count;
812 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
813 32, PropModeReplace, (unsigned char *)&count, 1);
816 static void updateCurrentWorkspace(WScreen *scr)
817 { /* changeable */
818 long count;
820 count = scr->current_workspace;
822 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
823 PropModeReplace, (unsigned char *)&count, 1);
826 static void updateWorkspaceNames(WScreen *scr)
828 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
829 unsigned int i, len, curr_size;
831 pos = buf;
832 len = 0;
833 for (i = 0; i < scr->workspace_count; i++) {
834 curr_size = strlen(scr->workspaces[i]->name);
835 strcpy(pos, scr->workspaces[i]->name);
836 pos += (curr_size + 1);
837 len += (curr_size + 1);
840 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
841 PropModeReplace, (unsigned char *)buf, len);
844 static void updateFocusHint(WScreen *scr)
845 { /* changeable */
846 Window window;
848 if (!scr->focused_window || !scr->focused_window->flags.focused)
849 window = None;
850 else
851 window = scr->focused_window->client_win;
853 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
854 PropModeReplace, (unsigned char *)&window, 1);
857 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
859 long l;
861 if (del) {
862 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
863 } else {
864 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
865 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
866 32, PropModeReplace, (unsigned char *)&l, 1);
870 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
871 { /* changeable */
872 if (del) {
873 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
874 } else {
875 Atom state[15]; /* nr of defined state atoms */
876 int i = 0;
878 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
879 updateWorkspaceHint(wwin, False, False);
881 if (IS_OMNIPRESENT(wwin))
882 state[i++] = net_wm_state_sticky;
883 if (wwin->flags.shaded)
884 state[i++] = net_wm_state_shaded;
885 if (wwin->flags.maximized & MAX_HORIZONTAL)
886 state[i++] = net_wm_state_maximized_horz;
887 if (wwin->flags.maximized & MAX_VERTICAL)
888 state[i++] = net_wm_state_maximized_vert;
889 if (WFLAGP(wwin, skip_window_list))
890 state[i++] = net_wm_state_skip_taskbar;
891 if (wwin->flags.net_skip_pager)
892 state[i++] = net_wm_state_skip_pager;
893 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
894 state[i++] = net_wm_state_hidden;
895 state[i++] = net_wm_state_skip_pager;
897 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
898 if (!IS_OMNIPRESENT(wwin))
899 updateWorkspaceHint(wwin, True, False);
900 state[i++] = net_wm_state_sticky;
903 if (WFLAGP(wwin, sunken))
904 state[i++] = net_wm_state_below;
905 if (WFLAGP(wwin, floating))
906 state[i++] = net_wm_state_above;
907 if (wwin->flags.fullscreen)
908 state[i++] = net_wm_state_fullscreen;
910 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
911 PropModeReplace, (unsigned char *)state, i);
915 static Bool updateStrut(WScreen *scr, Window w, Bool adding)
917 WReservedArea *area;
918 Bool hasState = False;
920 if (adding) {
921 Atom type_ret;
922 int fmt_ret;
923 unsigned long nitems_ret, bytes_after_ret;
924 long *data = NULL;
926 if ((XGetWindowProperty(dpy, w, net_wm_strut, 0, 4, False,
927 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
928 &bytes_after_ret, (unsigned char **)&data) == Success && data) ||
929 ((XGetWindowProperty(dpy, w, net_wm_strut_partial, 0, 12, False,
930 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
931 &bytes_after_ret, (unsigned char **)&data) == Success && data))) {
933 /* XXX: This is strictly incorrect in the case of net_wm_strut_partial...
934 * Discard the start and end properties from the partial strut and treat it as
935 * a (deprecated) strut.
936 * This means we are marking the whole width or height of the screen as
937 * reserved, which is not necessarily what the strut defines. However for the
938 * purposes of determining placement or maximization it's probably good enough.
940 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
941 area->area.x1 = data[0];
942 area->area.x2 = data[1];
943 area->area.y1 = data[2];
944 area->area.y2 = data[3];
946 area->window = w;
948 area->next = scr->netdata->strut;
949 scr->netdata->strut = area;
951 XFree(data);
952 hasState = True;
954 } else {
955 /* deleting */
956 area = scr->netdata->strut;
958 if (area) {
959 if (area->window == w) {
960 scr->netdata->strut = area->next;
961 wfree(area);
962 hasState = True;
963 } else {
964 while (area->next && area->next->window != w)
965 area = area->next;
967 if (area->next) {
968 WReservedArea *next;
970 next = area->next->next;
971 wfree(area->next);
972 area->next = next;
974 hasState = True;
980 return hasState;
983 static int getWindowLayer(WWindow *wwin)
985 int layer = WMNormalLevel;
987 if (wwin->type == net_wm_window_type_desktop) {
988 layer = WMDesktopLevel;
989 } else if (wwin->type == net_wm_window_type_dock) {
990 layer = WMDockLevel;
991 } else if (wwin->type == net_wm_window_type_toolbar) {
992 layer = WMMainMenuLevel;
993 } else if (wwin->type == net_wm_window_type_menu) {
994 layer = WMSubmenuLevel;
995 } else if (wwin->type == net_wm_window_type_utility) {
996 } else if (wwin->type == net_wm_window_type_splash) {
997 } else if (wwin->type == net_wm_window_type_dialog) {
998 if (wwin->transient_for) {
999 WWindow *parent = wWindowFor(wwin->transient_for);
1000 if (parent && parent->flags.fullscreen)
1001 layer = WMNormalLevel;
1003 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
1004 } else if (wwin->type == net_wm_window_type_dropdown_menu) {
1005 layer = WMSubmenuLevel;
1006 } else if (wwin->type == net_wm_window_type_popup_menu) {
1007 layer = WMSubmenuLevel;
1008 } else if (wwin->type == net_wm_window_type_tooltip) {
1009 } else if (wwin->type == net_wm_window_type_notification) {
1010 layer = WMPopUpLevel;
1011 } else if (wwin->type == net_wm_window_type_combo) {
1012 layer = WMSubmenuLevel;
1013 } else if (wwin->type == net_wm_window_type_dnd) {
1014 } else if (wwin->type == net_wm_window_type_normal) {
1017 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
1018 layer = WMSunkenLevel;
1019 if (wwin->client_flags.floating && WMFloatingLevel > layer)
1020 layer = WMFloatingLevel;
1022 return layer;
1025 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
1027 if (state == net_wm_state_sticky) {
1028 if (set == _NET_WM_STATE_TOGGLE)
1029 set = !IS_OMNIPRESENT(wwin);
1031 if (set != wwin->flags.omnipresent)
1032 wWindowSetOmnipresent(wwin, set);
1034 } else if (state == net_wm_state_shaded) {
1035 if (set == _NET_WM_STATE_TOGGLE)
1036 set = !wwin->flags.shaded;
1038 if (init) {
1039 wwin->flags.shaded = set;
1040 } else {
1041 if (set)
1042 wShadeWindow(wwin);
1043 else
1044 wUnshadeWindow(wwin);
1046 } else if (state == net_wm_state_skip_taskbar) {
1047 if (set == _NET_WM_STATE_TOGGLE)
1048 set = !wwin->client_flags.skip_window_list;
1050 wwin->client_flags.skip_window_list = set;
1051 } else if (state == net_wm_state_skip_pager) {
1052 if (set == _NET_WM_STATE_TOGGLE)
1053 set = !wwin->flags.net_skip_pager;
1055 wwin->flags.net_skip_pager = set;
1056 } else if (state == net_wm_state_maximized_vert) {
1057 if (set == _NET_WM_STATE_TOGGLE)
1058 set = !(wwin->flags.maximized & MAX_VERTICAL);
1060 if (init) {
1061 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1062 } else {
1063 if (set)
1064 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1065 else
1066 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1068 } else if (state == net_wm_state_maximized_horz) {
1069 if (set == _NET_WM_STATE_TOGGLE)
1070 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1072 if (init) {
1073 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1074 } else {
1075 if (set)
1076 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1077 else
1078 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1080 } else if (state == net_wm_state_hidden) {
1081 if (set == _NET_WM_STATE_TOGGLE)
1082 set = !(wwin->flags.miniaturized);
1084 if (init) {
1085 wwin->flags.miniaturized = set;
1086 } else {
1087 if (set)
1088 wIconifyWindow(wwin);
1089 else
1090 wDeiconifyWindow(wwin);
1092 } else if (state == net_wm_state_fullscreen) {
1093 if (set == _NET_WM_STATE_TOGGLE)
1094 set = !(wwin->flags.fullscreen);
1096 if (init) {
1097 wwin->flags.fullscreen = set;
1098 } else {
1099 if (set)
1100 wFullscreenWindow(wwin);
1101 else
1102 wUnfullscreenWindow(wwin);
1104 } else if (state == net_wm_state_above) {
1105 if (set == _NET_WM_STATE_TOGGLE)
1106 set = !(wwin->client_flags.floating);
1108 if (init) {
1109 wwin->client_flags.floating = set;
1110 } else {
1111 wwin->client_flags.floating = set;
1112 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1115 } else if (state == net_wm_state_below) {
1116 if (set == _NET_WM_STATE_TOGGLE)
1117 set = !(wwin->client_flags.sunken);
1119 if (init) {
1120 wwin->client_flags.sunken = set;
1121 } else {
1122 wwin->client_flags.sunken = set;
1123 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1126 } else {
1127 #ifdef DEBUG_WMSPEC
1128 wmessage("doStateAtom unknown atom %s set %d", XGetAtomName(dpy, state), set);
1129 #endif
1133 static void removeIcon(WWindow *wwin)
1135 if (wwin->icon == NULL)
1136 return;
1137 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1138 XUnmapWindow(dpy, wwin->icon->core->window);
1139 RemoveFromStackList(wwin->icon->core);
1140 wIconDestroy(wwin->icon);
1141 wwin->icon = NULL;
1145 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1147 Bool ret = True;
1149 if (type == net_wm_window_type_desktop) {
1150 wwin->client_flags.no_titlebar = 1;
1151 wwin->client_flags.no_resizable = 1;
1152 wwin->client_flags.no_miniaturizable = 1;
1153 wwin->client_flags.no_border = 1;
1154 wwin->client_flags.no_resizebar = 1;
1155 wwin->client_flags.no_shadeable = 1;
1156 wwin->client_flags.no_movable = 1;
1157 wwin->client_flags.omnipresent = 1;
1158 wwin->client_flags.skip_window_list = 1;
1159 wwin->client_flags.skip_switchpanel = 1;
1160 wwin->client_flags.dont_move_off = 1;
1161 wwin->client_flags.no_appicon = 1;
1162 wwin->flags.net_skip_pager = 1;
1163 wwin->frame_x = 0;
1164 wwin->frame_y = 0;
1165 } else if (type == net_wm_window_type_dock) {
1166 wwin->client_flags.no_titlebar = 1;
1167 wwin->client_flags.no_resizable = 1;
1168 wwin->client_flags.no_miniaturizable = 1;
1169 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1170 wwin->client_flags.no_resizebar = 1;
1171 wwin->client_flags.no_shadeable = 1;
1172 wwin->client_flags.no_movable = 1;
1173 wwin->client_flags.omnipresent = 1;
1174 wwin->client_flags.skip_window_list = 1;
1175 wwin->client_flags.skip_switchpanel = 1;
1176 wwin->client_flags.dont_move_off = 1;
1177 wwin->flags.net_skip_pager = 1;
1178 } else if (type == net_wm_window_type_toolbar ||
1179 type == net_wm_window_type_menu) {
1180 wwin->client_flags.no_resizable = 1;
1181 wwin->client_flags.no_miniaturizable = 1;
1182 wwin->client_flags.no_resizebar = 1;
1183 wwin->client_flags.no_shadeable = 1;
1184 wwin->client_flags.skip_window_list = 1;
1185 wwin->client_flags.skip_switchpanel = 1;
1186 wwin->client_flags.dont_move_off = 1;
1187 wwin->client_flags.no_appicon = 1;
1188 } else if (type == net_wm_window_type_dropdown_menu ||
1189 type == net_wm_window_type_popup_menu ||
1190 type == net_wm_window_type_combo) {
1191 wwin->client_flags.no_titlebar = 1;
1192 wwin->client_flags.no_resizable = 1;
1193 wwin->client_flags.no_miniaturizable = 1;
1194 wwin->client_flags.no_resizebar = 1;
1195 wwin->client_flags.no_shadeable = 1;
1196 wwin->client_flags.skip_window_list = 1;
1197 wwin->client_flags.skip_switchpanel = 1;
1198 wwin->client_flags.dont_move_off = 1;
1199 wwin->client_flags.no_appicon = 1;
1200 } else if (type == net_wm_window_type_utility) {
1201 wwin->client_flags.no_appicon = 1;
1202 } else if (type == net_wm_window_type_splash) {
1203 wwin->client_flags.no_titlebar = 1;
1204 wwin->client_flags.no_resizable = 1;
1205 wwin->client_flags.no_miniaturizable = 1;
1206 wwin->client_flags.no_resizebar = 1;
1207 wwin->client_flags.no_shadeable = 1;
1208 wwin->client_flags.no_movable = 1;
1209 wwin->client_flags.skip_window_list = 1;
1210 wwin->client_flags.skip_switchpanel = 1;
1211 wwin->client_flags.dont_move_off = 1;
1212 wwin->client_flags.no_appicon = 1;
1213 wwin->flags.net_skip_pager = 1;
1214 } else if (type == net_wm_window_type_dialog) {
1215 /* These also seem a bad idea in our context -Dan
1216 // wwin->client_flags.skip_window_list = 1;
1217 // wwin->client_flags.no_appicon = 1;
1219 } else if (wwin->type == net_wm_window_type_tooltip) {
1220 wwin->client_flags.no_titlebar = 1;
1221 wwin->client_flags.no_resizable = 1;
1222 wwin->client_flags.no_miniaturizable = 1;
1223 wwin->client_flags.no_resizebar = 1;
1224 wwin->client_flags.no_shadeable = 1;
1225 wwin->client_flags.no_movable = 1;
1226 wwin->client_flags.skip_window_list = 1;
1227 wwin->client_flags.skip_switchpanel = 1;
1228 wwin->client_flags.dont_move_off = 1;
1229 wwin->client_flags.no_appicon = 1;
1230 wwin->client_flags.no_focusable = 1;
1231 wwin->flags.net_skip_pager = 1;
1232 } else if (wwin->type == net_wm_window_type_notification) {
1233 wwin->client_flags.no_titlebar = 1;
1234 wwin->client_flags.no_resizable = 1;
1235 wwin->client_flags.no_miniaturizable = 1;
1236 wwin->client_flags.no_border = 1;
1237 wwin->client_flags.no_resizebar = 1;
1238 wwin->client_flags.no_shadeable = 1;
1239 wwin->client_flags.no_movable = 1;
1240 wwin->client_flags.omnipresent = 1;
1241 wwin->client_flags.skip_window_list = 1;
1242 wwin->client_flags.skip_switchpanel = 1;
1243 wwin->client_flags.dont_move_off = 1;
1244 wwin->client_flags.no_hide_others= 1;
1245 wwin->client_flags.no_appicon = 1;
1246 wwin->client_flags.no_focusable = 1;
1247 wwin->flags.net_skip_pager = 1;
1248 } else if (wwin->type == net_wm_window_type_dnd) {
1249 wwin->client_flags.no_titlebar = 1;
1250 wwin->client_flags.no_resizable = 1;
1251 wwin->client_flags.no_miniaturizable = 1;
1252 wwin->client_flags.no_border = 1;
1253 wwin->client_flags.no_resizebar = 1;
1254 wwin->client_flags.no_shadeable = 1;
1255 wwin->client_flags.no_movable = 1;
1256 wwin->client_flags.skip_window_list = 1;
1257 wwin->client_flags.skip_switchpanel = 1;
1258 wwin->client_flags.dont_move_off = 1;
1259 wwin->client_flags.no_appicon = 1;
1260 wwin->flags.net_skip_pager = 1;
1261 } else if (type == net_wm_window_type_normal) {
1262 } else {
1263 ret = False;
1266 wwin->type = type;
1267 *layer = getWindowLayer(wwin);
1269 return ret;
1272 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1274 if (wwin->type == net_wm_window_type_splash) {
1275 WScreen *scr = wwin->screen_ptr;
1276 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1277 *x = rect.pos.x + (rect.size.width - width) / 2;
1278 *y = rect.pos.y + (rect.size.height - height) / 2;
1282 static void updateWindowType(WWindow *wwin)
1284 Atom type_ret;
1285 int fmt_ret, layer;
1286 unsigned long nitems_ret, bytes_after_ret;
1287 long *data = NULL;
1289 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1290 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1291 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1293 int i;
1294 Atom *type = (Atom *) data;
1295 for (i = 0; i < nitems_ret; ++i) {
1296 if (handleWindowType(wwin, type[i], &layer))
1297 break;
1299 XFree(data);
1302 if (wwin->frame != NULL) {
1303 ChangeStackingLevel(wwin->frame->core, layer);
1304 wwin->frame->flags.need_texture_change = 1;
1305 wWindowConfigureBorders(wwin);
1306 wFrameWindowPaint(wwin->frame);
1307 wNETWMUpdateActions(wwin, False);
1311 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1313 Atom type_ret;
1314 int fmt_ret, i;
1315 unsigned long nitems_ret, bytes_after_ret;
1316 long *data = NULL;
1318 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1319 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1320 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1322 long desktop = *data;
1323 XFree(data);
1325 if (desktop == -1)
1326 wwin->client_flags.omnipresent = 1;
1327 else
1328 *workspace = desktop;
1331 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1332 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1333 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1335 Atom *state = (Atom *) data;
1336 for (i = 0; i < nitems_ret; ++i)
1337 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1339 XFree(data);
1342 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1343 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1344 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1346 Atom *type = (Atom *) data;
1347 for (i = 0; i < nitems_ret; ++i) {
1348 if (handleWindowType(wwin, type[i], layer))
1349 break;
1351 XFree(data);
1354 wNETWMUpdateActions(wwin, False);
1355 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1356 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1358 wScreenUpdateUsableArea(wwin->screen_ptr);
1361 static Bool updateNetIconInfo(WWindow *wwin)
1363 Atom type_ret;
1364 int fmt_ret;
1365 unsigned long nitems_ret, bytes_after_ret;
1366 long *data = NULL;
1367 Bool hasState = False;
1368 Bool old_state = wwin->flags.net_handle_icon;
1370 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1371 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1372 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1373 long handled = *data;
1374 wwin->flags.net_handle_icon = (handled != 0);
1375 XFree(data);
1376 hasState = True;
1378 } else {
1379 wwin->flags.net_handle_icon = False;
1382 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1383 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1384 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1386 #ifdef NETWM_PROPER
1387 if (wwin->flags.net_handle_icon)
1388 #else
1389 wwin->flags.net_handle_icon = True;
1390 #endif
1392 wwin->icon_x = data[0];
1393 wwin->icon_y = data[1];
1394 wwin->icon_w = data[2];
1395 wwin->icon_h = data[3];
1398 XFree(data);
1399 hasState = True;
1401 } else {
1402 wwin->flags.net_handle_icon = False;
1405 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1406 if (wwin->flags.net_handle_icon) {
1407 removeIcon(wwin);
1408 } else {
1409 wwin->flags.miniaturized = False;
1410 wwin->flags.skip_next_animation = True;
1411 wIconifyWindow(wwin);
1415 return hasState;
1418 void wNETWMCheckInitialClientState(WWindow *wwin)
1420 #ifdef DEBUG_WMSPEC
1421 wmessage("wNETWMCheckInitialClientState");
1422 #endif
1424 wNETWMShowingDesktop(wwin->screen_ptr, False);
1426 updateWindowType(wwin);
1427 updateNetIconInfo(wwin);
1428 updateIconImage(wwin);
1431 void wNETWMCheckInitialFrameState(WWindow *wwin)
1433 #ifdef DEBUG_WMSPEC
1434 wmessage("wNETWMCheckInitialFrameState");
1435 #endif
1437 updateWindowOpacity(wwin);
1440 static void handleDesktopNames(WScreen *scr)
1442 unsigned long nitems_ret, bytes_after_ret;
1443 char *data, *names[32];
1444 int fmt_ret, i, n;
1445 Atom type_ret;
1447 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1448 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1449 &bytes_after_ret, (unsigned char **)&data) != Success)
1450 return;
1452 if (data == NULL)
1453 return;
1455 if (type_ret != utf8_string || fmt_ret != 8)
1456 return;
1458 n = 0;
1459 names[n] = data;
1460 for (i = 0; i < nitems_ret; i++) {
1461 if (data[i] == 0) {
1462 n++;
1463 names[n] = &data[i];
1464 } else if (*names[n] == 0) {
1465 names[n] = &data[i];
1466 wWorkspaceRename(scr, n, names[n]);
1471 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1473 WScreen *scr;
1474 WWindow *wwin;
1476 #ifdef DEBUG_WMSPEC
1477 wmessage("processClientMessage type %s", XGetAtomName(dpy, event->message_type));
1478 #endif
1480 scr = wScreenForWindow(event->window);
1481 if (scr) {
1482 /* generic client messages */
1483 if (event->message_type == net_current_desktop) {
1484 wWorkspaceChange(scr, event->data.l[0]);
1485 return True;
1487 } else if (event->message_type == net_number_of_desktops) {
1488 long value;
1490 value = event->data.l[0];
1491 if (value > scr->workspace_count) {
1492 wWorkspaceMake(scr, value - scr->workspace_count);
1493 } else if (value < scr->workspace_count) {
1494 int i;
1495 Bool rebuild = False;
1497 for (i = scr->workspace_count - 1; i >= value; i--) {
1498 if (!wWorkspaceDelete(scr, i)) {
1499 rebuild = True;
1500 break;
1504 if (rebuild)
1505 updateWorkspaceCount(scr);
1507 return True;
1509 } else if (event->message_type == net_showing_desktop) {
1510 wNETWMShowingDesktop(scr, event->data.l[0]);
1511 return True;
1513 } else if (event->message_type == net_desktop_names) {
1514 handleDesktopNames(scr);
1515 return True;
1519 /* window specific client messages */
1521 wwin = wWindowFor(event->window);
1522 if (!wwin)
1523 return False;
1525 if (event->message_type == net_active_window) {
1527 * Satisfy a client's focus request only if
1528 * - request comes from a pager, or
1529 * - it's explicitly allowed in Advanced Options, or
1530 * - giving the client the focus does not cause a change in
1531 * the active workspace (XXX: or the active head if Xinerama)
1533 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1534 || event->data.l[0] == 2 /* Requested by pager */
1535 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1536 wNETWMShowingDesktop(scr, False);
1537 wMakeWindowVisible(wwin);
1539 return True;
1541 } else if (event->message_type == net_close_window) {
1542 if (!WFLAGP(wwin, no_closable)) {
1543 if (wwin->protocols.DELETE_WINDOW)
1544 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
1545 w_global.timestamp.last_event);
1547 return True;
1549 } else if (event->message_type == net_wm_state) {
1550 int maximized = wwin->flags.maximized;
1551 long set = event->data.l[0];
1553 #ifdef DEBUG_WMSPEC
1554 wmessage("net_wm_state set %ld a1 %s a2 %s", set,
1555 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1556 #endif
1558 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1559 if (event->data.l[2])
1560 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1562 if (wwin->flags.maximized != maximized) {
1563 if (!wwin->flags.maximized) {
1564 wwin->flags.maximized = maximized;
1565 wUnmaximizeWindow(wwin);
1566 } else {
1567 wMaximizeWindow(wwin, wwin->flags.maximized);
1570 updateStateHint(wwin, False, False);
1571 return True;
1573 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1574 updateNetIconInfo(wwin);
1575 return True;
1577 } else if (event->message_type == net_wm_desktop) {
1578 long desktop = event->data.l[0];
1580 if (desktop == -1) {
1581 wWindowSetOmnipresent(wwin, True);
1582 } else {
1583 if (IS_OMNIPRESENT(wwin))
1584 wWindowSetOmnipresent(wwin, False);
1585 wWindowChangeWorkspace(wwin, desktop);
1587 return True;
1590 return False;
1593 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1595 #ifdef DEBUG_WMSPEC
1596 wmessage("clientHintChange type %s", XGetAtomName(dpy, event->atom));
1597 #endif
1599 if (event->atom == net_wm_strut || event->atom == net_wm_strut_partial) {
1600 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1601 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1602 wScreenUpdateUsableArea(wwin->screen_ptr);
1603 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1604 updateNetIconInfo(wwin);
1605 } else if (event->atom == net_wm_window_type) {
1606 updateWindowType(wwin);
1607 } else if (event->atom == net_wm_name) {
1608 char *name = wNETWMGetWindowName(wwin->client_win);
1609 wWindowUpdateName(wwin, name);
1610 if (name)
1611 wfree(name);
1612 } else if (event->atom == net_wm_icon_name) {
1613 if (wwin->icon) {
1614 wIconChangeTitle(wwin->icon, wwin);
1615 wIconPaint(wwin->icon);
1617 } else if (event->atom == net_wm_icon) {
1618 updateIconImage(wwin);
1619 } else if (event->atom == net_wm_window_opacity) {
1620 updateWindowOpacity(wwin);
1624 int wNETWMGetPidForWindow(Window window)
1626 Atom type_ret;
1627 int fmt_ret;
1628 unsigned long nitems_ret, bytes_after_ret;
1629 long *data = NULL;
1630 int pid;
1632 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1633 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1634 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1635 pid = *data;
1636 XFree(data);
1637 } else {
1638 pid = 0;
1641 return pid;
1644 char *wNETWMGetWindowName(Window window)
1646 char *name;
1647 char *ret;
1648 int size;
1650 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1651 if (name) {
1652 ret = wstrndup(name, size);
1653 XFree(name);
1654 } else {
1655 ret = NULL;
1658 return ret;
1661 char *wNETWMGetIconName(Window window)
1663 char *name;
1664 char *ret;
1665 int size;
1667 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1668 if (name) {
1669 ret = wstrndup(name, size);
1670 XFree(name);
1671 } else {
1672 ret = NULL;
1675 return ret;
1678 static void observer(void *self, WMNotification *notif)
1680 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1681 const char *name = WMGetNotificationName(notif);
1682 void *data = WMGetNotificationClientData(notif);
1683 NetData *ndata = (NetData *) self;
1685 if (strcmp(name, WMNManaged) == 0 && wwin) {
1686 updateClientList(wwin->screen_ptr);
1687 updateClientListStacking(wwin->screen_ptr, NULL);
1688 updateStateHint(wwin, True, False);
1690 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1691 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1692 wScreenUpdateUsableArea(wwin->screen_ptr);
1693 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1694 updateClientList(wwin->screen_ptr);
1695 updateClientListStacking(wwin->screen_ptr, wwin);
1696 updateWorkspaceHint(wwin, False, True);
1697 updateStateHint(wwin, False, True);
1698 wNETWMUpdateActions(wwin, True);
1700 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1701 wScreenUpdateUsableArea(wwin->screen_ptr);
1702 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1703 updateClientListStacking(wwin->screen_ptr, NULL);
1704 updateStateHint(wwin, False, False);
1705 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1706 updateClientListStacking(wwin->screen_ptr, NULL);
1707 updateStateHint(wwin, False, False);
1708 } else if (strcmp(name, WMNChangedFocus) == 0) {
1709 updateFocusHint(ndata->scr);
1710 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1711 updateWorkspaceHint(wwin, False, False);
1712 updateStateHint(wwin, True, False);
1713 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1714 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1718 static void wsobserver(void *self, WMNotification *notif)
1720 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1721 const char *name = WMGetNotificationName(notif);
1723 /* Parameter not used, but tell the compiler that it is ok */
1724 (void) self;
1726 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1727 updateWorkspaceCount(scr);
1728 updateWorkspaceNames(scr);
1729 wNETWMUpdateWorkarea(scr);
1730 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1731 updateWorkspaceCount(scr);
1732 updateWorkspaceNames(scr);
1733 wNETWMUpdateWorkarea(scr);
1734 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1735 updateCurrentWorkspace(scr);
1736 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1737 updateWorkspaceNames(scr);
1741 void wNETFrameExtents(WWindow *wwin)
1743 long extents[4] = { 0, 0, 0, 0 };
1745 /* The extents array describes dimensions which are not
1746 * part of the client window. In our case that means
1747 * widths of the border and heights of the titlebar and resizebar.
1749 * Index 0 = left
1750 * 1 = right
1751 * 2 = top
1752 * 3 = bottom
1754 if (wwin->frame->titlebar)
1755 extents[2] = wwin->frame->titlebar->height;
1756 if (wwin->frame->resizebar)
1757 extents[3] = wwin->frame->resizebar->height;
1758 if (HAS_BORDER(wwin)) {
1759 extents[0] += wwin->screen_ptr->frame_border_width;
1760 extents[1] += wwin->screen_ptr->frame_border_width;
1761 extents[2] += wwin->screen_ptr->frame_border_width;
1762 extents[3] += wwin->screen_ptr->frame_border_width;
1765 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1768 void wNETCleanupFrameExtents(WWindow *wwin)
1770 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);