WINGs: Added 'const' attribute to function 'WMCreateHashTable'
[wmaker-crm.git] / src / wmspec.c
blobd6851d79e8dd3b5b719b3307efcd548c883fffee
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 <X11/Xarch.h>
36 #include <string.h>
38 #include <WINGs/WUtil.h>
39 #include "WindowMaker.h"
40 #include "window.h"
41 #include "screen.h"
42 #include "workspace.h"
43 #include "framewin.h"
44 #include "actions.h"
45 #include "client.h"
46 #include "appicon.h"
47 #include "wmspec.h"
48 #include "icon.h"
49 #include "stacking.h"
50 #include "xinerama.h"
51 #include "properties.h"
54 /* Root Window Properties */
55 static Atom net_supported;
56 static Atom net_client_list;
57 static Atom net_client_list_stacking;
58 static Atom net_number_of_desktops;
59 static Atom net_desktop_geometry;
60 static Atom net_desktop_viewport;
61 static Atom net_current_desktop;
62 static Atom net_desktop_names;
63 static Atom net_active_window;
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_normal;
89 static Atom net_wm_state;
90 static Atom net_wm_state_modal; /* XXX: what is this?!? */
91 static Atom net_wm_state_sticky;
92 static Atom net_wm_state_maximized_vert;
93 static Atom net_wm_state_maximized_horz;
94 static Atom net_wm_state_shaded;
95 static Atom net_wm_state_skip_taskbar;
96 static Atom net_wm_state_skip_pager;
97 static Atom net_wm_state_hidden;
98 static Atom net_wm_state_fullscreen;
99 static Atom net_wm_state_above;
100 static Atom net_wm_state_below;
101 static Atom net_wm_allowed_actions;
102 static Atom net_wm_action_move;
103 static Atom net_wm_action_resize;
104 static Atom net_wm_action_minimize;
105 static Atom net_wm_action_shade;
106 static Atom net_wm_action_stick;
107 static Atom net_wm_action_maximize_horz;
108 static Atom net_wm_action_maximize_vert;
109 static Atom net_wm_action_fullscreen;
110 static Atom net_wm_action_change_desktop;
111 static Atom net_wm_action_close;
112 static Atom net_wm_strut; /* XXX: see net_workarea */
113 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
114 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. */
115 static Atom net_wm_icon;
116 static Atom net_wm_pid; /* TODO */
117 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
119 static Atom net_frame_extents;
121 /* Window Manager Protocols */
122 static Atom net_wm_ping; /* TODO */
124 static Atom utf8_string;
126 typedef struct {
127 char *name;
128 Atom *atom;
129 } atomitem_t;
131 static atomitem_t atomNames[] = {
132 {"_NET_SUPPORTED", &net_supported},
133 {"_NET_CLIENT_LIST", &net_client_list},
134 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
135 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
136 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
137 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
138 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
139 {"_NET_DESKTOP_NAMES", &net_desktop_names},
140 {"_NET_ACTIVE_WINDOW", &net_active_window},
141 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
142 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
143 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
144 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
146 {"_NET_CLOSE_WINDOW", &net_close_window},
147 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
148 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
150 {"_NET_WM_NAME", &net_wm_name},
151 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
152 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
153 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
154 {"_NET_WM_DESKTOP", &net_wm_desktop},
155 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
156 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
157 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
158 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
159 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
160 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
161 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
162 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
163 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
164 {"_NET_WM_STATE", &net_wm_state},
165 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
166 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
167 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
168 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
169 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
170 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
171 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
172 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
173 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
174 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
175 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
176 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
177 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
178 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
179 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
180 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
181 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
182 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
183 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
184 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
185 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
186 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
187 {"_NET_WM_STRUT", &net_wm_strut},
188 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
189 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
190 {"_NET_WM_ICON", &net_wm_icon},
191 {"_NET_WM_PID", &net_wm_pid},
192 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
194 {"_NET_FRAME_EXTENTS", &net_frame_extents},
196 {"_NET_WM_PING", &net_wm_ping},
198 {"UTF8_STRING", &utf8_string},
201 #define _NET_WM_STATE_REMOVE 0
202 #define _NET_WM_STATE_ADD 1
203 #define _NET_WM_STATE_TOGGLE 2
205 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
206 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
207 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
208 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
209 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
210 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
211 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
212 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
213 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
214 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
215 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
217 static void observer(void *self, WMNotification *notif);
218 static void wsobserver(void *self, WMNotification *notif);
220 static void updateClientList(WScreen *scr);
221 static void updateClientListStacking(WScreen *scr, WWindow *);
223 static void updateWorkspaceNames(WScreen *scr);
224 static void updateCurrentWorkspace(WScreen *scr);
225 static void updateWorkspaceCount(WScreen *scr);
226 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
228 typedef struct NetData {
229 WScreen *scr;
230 WReservedArea *strut;
231 WWindow **show_desktop;
232 } NetData;
234 static void setSupportedHints(WScreen *scr)
236 Atom atom[wlengthof(atomNames)];
237 int i = 0;
239 /* set supported hints list */
240 /* XXX: extend this !!! */
242 atom[i++] = net_client_list;
243 atom[i++] = net_client_list_stacking;
244 atom[i++] = net_number_of_desktops;
245 atom[i++] = net_desktop_geometry;
246 atom[i++] = net_desktop_viewport;
247 atom[i++] = net_current_desktop;
248 atom[i++] = net_desktop_names;
249 atom[i++] = net_active_window;
250 atom[i++] = net_supporting_wm_check;
251 atom[i++] = net_showing_desktop;
252 #if 0
253 atom[i++] = net_wm_moveresize;
254 #endif
255 atom[i++] = net_wm_desktop;
256 atom[i++] = net_wm_window_type;
257 atom[i++] = net_wm_window_type_desktop;
258 atom[i++] = net_wm_window_type_dock;
259 atom[i++] = net_wm_window_type_toolbar;
260 atom[i++] = net_wm_window_type_menu;
261 atom[i++] = net_wm_window_type_utility;
262 atom[i++] = net_wm_window_type_splash;
263 atom[i++] = net_wm_window_type_dialog;
264 atom[i++] = net_wm_window_type_normal;
266 atom[i++] = net_wm_state;
267 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
268 atom[i++] = net_wm_state_sticky;
269 atom[i++] = net_wm_state_shaded;
270 atom[i++] = net_wm_state_maximized_horz;
271 atom[i++] = net_wm_state_maximized_vert;
272 atom[i++] = net_wm_state_skip_taskbar;
273 atom[i++] = net_wm_state_skip_pager;
274 atom[i++] = net_wm_state_hidden;
275 atom[i++] = net_wm_state_fullscreen;
276 atom[i++] = net_wm_state_above;
277 atom[i++] = net_wm_state_below;
279 atom[i++] = net_wm_allowed_actions;
280 atom[i++] = net_wm_action_move;
281 atom[i++] = net_wm_action_resize;
282 atom[i++] = net_wm_action_minimize;
283 atom[i++] = net_wm_action_shade;
284 atom[i++] = net_wm_action_stick;
285 atom[i++] = net_wm_action_maximize_horz;
286 atom[i++] = net_wm_action_maximize_vert;
287 atom[i++] = net_wm_action_fullscreen;
288 atom[i++] = net_wm_action_change_desktop;
289 atom[i++] = net_wm_action_close;
291 atom[i++] = net_wm_strut;
292 atom[i++] = net_wm_icon_geometry;
293 atom[i++] = net_wm_icon;
294 atom[i++] = net_wm_handled_icons;
296 atom[i++] = net_frame_extents;
298 atom[i++] = net_wm_name;
299 atom[i++] = net_wm_icon_name;
301 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
303 /* set supporting wm hint */
304 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
305 PropModeReplace, (unsigned char *)&scr->info_window, 1);
307 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
308 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
311 void wNETWMUpdateDesktop(WScreen *scr)
313 long *views, sizes[2];
314 int count, i;
316 if (w_global.workspace.count == 0)
317 return;
319 count = w_global.workspace.count * 2;
320 views = wmalloc(sizeof(long) * count);
321 /*memset(views, 0, sizeof(long) * count); */
322 sizes[0] = scr->scr_width;
323 sizes[1] = scr->scr_height;
325 for (i = 0; i < w_global.workspace.count; i++) {
326 views[2 * i + 0] = 0;
327 views[2 * i + 1] = 0;
330 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
331 PropModeReplace, (unsigned char *)sizes, 2);
333 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
334 PropModeReplace, (unsigned char *)views, count);
336 wfree(views);
339 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
341 int count;
342 unsigned char *prop;
344 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
345 if (prop) {
346 int desktop = *(long *)prop;
347 XFree(prop);
348 return desktop;
350 return -1;
354 * Find the best icon to be used by Window Maker for appicon/miniwindows.
355 * Currently the algorithm is to take the image with the size closest
356 * to icon_size x icon_size, but never bigger than that.
358 * This algorithm is very poorly implemented and needs to be redone (it can
359 * easily select images with very large widths and very small heights over
360 * square images, if the area of the former is closer to the desired one).
362 * The logic can also be changed to accept bigger images and scale them down.
364 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
366 int size, wanted, d;
367 unsigned long i, distance;
368 unsigned long *icon;
370 /* better use only 75% of icon_size. For 64x64 this means 48x48
371 * This leaves room around the icon for the miniwindow title and
372 * results in better overall aesthetics -Dan */
373 wanted = wPreferences.icon_size * wPreferences.icon_size;
375 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
376 size = data[i] * data[i + 1];
377 if (size == 0)
378 break;
379 d = wanted - size;
380 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
381 distance = d;
382 icon = &data[i];
384 i += size + 2;
387 return icon;
390 static RImage *makeRImageFromARGBData(unsigned long *data)
392 int size, width, height, i;
393 RImage *image;
394 unsigned char *imgdata;
395 unsigned long pixel;
397 width = data[0];
398 height = data[1];
399 size = width * height;
401 if (size == 0)
402 return NULL;
404 image = RCreateImage(width, height, True);
406 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
407 pixel = data[i];
408 imgdata[3] = (pixel >> 24) & 0xff; /* A */
409 imgdata[0] = (pixel >> 16) & 0xff; /* R */
410 imgdata[1] = (pixel >> 8) & 0xff; /* G */
411 imgdata[2] = (pixel >> 0) & 0xff; /* B */
414 return image;
417 RImage *get_window_image_from_x11(Window window)
419 RImage *image;
420 Atom type;
421 int format;
422 unsigned long items, rest;
423 unsigned long *property, *data;
425 /* Get the icon from X11 Window */
426 if (XGetWindowProperty(dpy, window, net_wm_icon, 0L, LONG_MAX,
427 False, XA_CARDINAL, &type, &format, &items, &rest,
428 (unsigned char **)&property) != Success || !property)
429 return NULL;
431 if (type != XA_CARDINAL || format != 32 || items < 2) {
432 XFree(property);
433 return NULL;
436 /* Find the best icon */
437 data = findBestIcon(property, items);
438 if (!data) {
439 XFree(property);
440 return NULL;
443 /* Save the best icon in the X11 icon */
444 image = makeRImageFromARGBData(data);
446 XFree(property);
448 /* Resize the image to the correct value */
449 image = wIconValidateIconSize(image, wPreferences.icon_size);
451 return image;
454 static void updateIconImage(WWindow *wwin)
456 /* Remove the icon image from X11 */
457 if (wwin->net_icon_image)
458 RReleaseImage(wwin->net_icon_image);
460 /* Save the icon in the X11 icon */
461 wwin->net_icon_image = get_window_image_from_x11(wwin->client_win);
463 /* Refresh the Window Icon */
464 if (wwin->icon)
465 wIconUpdate(wwin->icon);
467 /* Refresh the application icon */
468 WApplication *app = wApplicationOf(wwin->main_window);
469 if (app && app->app_icon) {
470 wIconUpdate(app->app_icon->icon);
471 wAppIconPaint(app->app_icon);
475 static void updateShowDesktop(WScreen *scr, Bool show)
477 long foo;
479 foo = (show == True);
480 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
481 PropModeReplace, (unsigned char *)&foo, 1);
484 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
486 if (show && scr->netdata->show_desktop == NULL) {
487 WWindow *tmp, **wins;
488 int i = 0;
490 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
492 tmp = scr->focused_window;
493 while (tmp) {
494 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
496 wins[i++] = tmp;
497 tmp->flags.skip_next_animation = 1;
498 tmp->flags.net_show_desktop = 1;
499 wIconifyWindow(tmp);
502 tmp = tmp->prev;
504 wins[i++] = NULL;
506 scr->netdata->show_desktop = wins;
507 updateShowDesktop(scr, True);
508 } else if (scr->netdata->show_desktop != NULL) {
509 /* FIXME: get rid of workspace flashing ! */
510 int ws = w_global.workspace.current;
511 WWindow **tmp;
512 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
513 wDeiconifyWindow(*tmp);
514 (*tmp)->flags.net_show_desktop = 0;
516 if (ws != w_global.workspace.current)
517 wWorkspaceChange(scr, ws);
518 wfree(scr->netdata->show_desktop);
519 scr->netdata->show_desktop = NULL;
520 updateShowDesktop(scr, False);
524 void wNETWMInitStuff(WScreen *scr)
526 NetData *data;
527 int i;
529 #ifdef DEBUG_WMSPEC
530 wmessage("wNETWMInitStuff");
531 #endif
533 #ifdef HAVE_XINTERNATOMS
535 Atom atoms[wlengthof(atomNames)];
536 char *names[wlengthof(atomNames)];
538 for (i = 0; i < wlengthof(atomNames); ++i)
539 names[i] = atomNames[i].name;
541 XInternAtoms(dpy, &names[0], wlengthof(atomNames), False, atoms);
542 for (i = 0; i < wlengthof(atomNames); ++i)
543 *atomNames[i].atom = atoms[i];
546 #else
547 for (i = 0; i < wlengthof(atomNames); i++)
548 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
549 #endif
551 data = wmalloc(sizeof(NetData));
552 data->scr = scr;
553 data->strut = NULL;
554 data->show_desktop = NULL;
556 scr->netdata = data;
558 setSupportedHints(scr);
560 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
561 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
562 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
563 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
564 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
565 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
566 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
568 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
569 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
570 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
571 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
573 updateClientList(scr);
574 updateClientListStacking(scr, NULL);
575 updateWorkspaceCount(scr);
576 updateWorkspaceNames(scr);
577 updateShowDesktop(scr, False);
579 wScreenUpdateUsableArea(scr);
582 void wNETWMCleanup(WScreen *scr)
584 int i;
586 for (i = 0; i < wlengthof(atomNames); i++)
587 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
590 void wNETWMUpdateActions(WWindow *wwin, Bool del)
592 Atom action[10]; /* nr of actions atoms defined */
593 int i = 0;
595 if (del) {
596 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
597 return;
600 if (IS_MOVABLE(wwin))
601 action[i++] = net_wm_action_move;
603 if (IS_RESIZABLE(wwin))
604 action[i++] = net_wm_action_resize;
606 if (!WFLAGP(wwin, no_miniaturizable))
607 action[i++] = net_wm_action_minimize;
609 if (!WFLAGP(wwin, no_shadeable))
610 action[i++] = net_wm_action_shade;
612 /* if (!WFLAGP(wwin, no_stickable)) */
613 action[i++] = net_wm_action_stick;
615 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
616 if (IS_RESIZABLE(wwin))
617 action[i++] = net_wm_action_maximize_horz;
619 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
620 if (IS_RESIZABLE(wwin))
621 action[i++] = net_wm_action_maximize_vert;
623 /* if (!WFLAGP(wwin, no_fullscreen)) */
624 action[i++] = net_wm_action_fullscreen;
626 /* if (!WFLAGP(wwin, no_change_desktop)) */
627 action[i++] = net_wm_action_change_desktop;
629 if (!WFLAGP(wwin, no_closable))
630 action[i++] = net_wm_action_close;
632 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
633 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
636 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
638 WReservedArea *cur;
639 WMRect rect;
641 if (!scr->netdata || !scr->netdata->strut)
642 return False;
644 area->x1 = area->y1 = area->x2 = area->y2 = 0;
646 for (cur = scr->netdata->strut; cur; cur = cur->next) {
647 WWindow *wwin = wWindowFor(cur->window);
648 if (wWindowTouchesHead(wwin, head)) {
649 if (cur->area.x1 > area->x1)
650 area->x1 = cur->area.x1;
651 if (cur->area.y1 > area->y1)
652 area->y1 = cur->area.y1;
653 if (cur->area.x2 > area->x2)
654 area->x2 = cur->area.x2;
655 if (cur->area.y2 > area->y2)
656 area->y2 = cur->area.y2;
660 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
661 return False;
663 rect = wGetRectForHead(scr, head);
665 area->x1 = rect.pos.x + area->x1;
666 area->x2 = rect.pos.x + rect.size.width - area->x2;
667 area->y1 = rect.pos.y + area->y1;
668 area->y2 = rect.pos.y + rect.size.height - area->y2;
670 return True;
673 static void updateClientList(WScreen *scr)
675 WWindow *wwin;
676 Window *windows;
677 int count;
679 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
681 count = 0;
682 wwin = scr->focused_window;
683 while (wwin) {
684 windows[count++] = wwin->client_win;
685 wwin = wwin->prev;
687 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
688 PropModeReplace, (unsigned char *)windows, count);
690 wfree(windows);
691 XFlush(dpy);
694 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
696 WWindow *wwin;
697 Window *client_list, *client_list_reverse;
698 int client_count, i;
699 WCoreWindow *tmp;
700 WMBagIterator iter;
702 /* update client list */
703 i = scr->window_count + 1;
704 client_list = (Window *) wmalloc(sizeof(Window) * i);
705 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
707 client_count = 0;
708 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
709 while (tmp) {
710 wwin = wWindowFor(tmp->window);
711 /* wwin_excl is a window to exclude from the list
712 (e.g. it's now unmanaged) */
713 if (wwin && (wwin != wwin_excl))
714 client_list[client_count++] = wwin->client_win;
715 tmp = tmp->stacking->under;
719 for (i = 0; i < client_count; i++) {
720 Window w = client_list[client_count - i - 1];
721 client_list_reverse[i] = w;
724 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
725 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
727 wfree(client_list);
728 wfree(client_list_reverse);
730 XFlush(dpy);
733 static void updateWorkspaceCount(WScreen *scr)
734 { /* changeable */
735 long count;
737 count = w_global.workspace.count;
739 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
740 32, PropModeReplace, (unsigned char *)&count, 1);
743 static void updateCurrentWorkspace(WScreen *scr)
744 { /* changeable */
745 long count;
747 count = w_global.workspace.current;
749 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
750 PropModeReplace, (unsigned char *)&count, 1);
753 static void updateWorkspaceNames(WScreen *scr)
755 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
756 unsigned int i, len, curr_size;
758 pos = buf;
759 len = 0;
760 for (i = 0; i < w_global.workspace.count; i++) {
761 curr_size = strlen(w_global.workspace.array[i]->name);
762 strcpy(pos, w_global.workspace.array[i]->name);
763 pos += (curr_size + 1);
764 len += (curr_size + 1);
767 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
768 PropModeReplace, (unsigned char *)buf, len);
771 static void updateFocusHint(WScreen *scr)
772 { /* changeable */
773 Window window;
775 if (!scr->focused_window || !scr->focused_window->flags.focused)
776 window = None;
777 else
778 window = scr->focused_window->client_win;
780 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
781 PropModeReplace, (unsigned char *)&window, 1);
784 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
786 long l;
788 if (del) {
789 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
790 } else {
791 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
792 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
793 32, PropModeReplace, (unsigned char *)&l, 1);
797 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
798 { /* changeable */
799 if (del) {
800 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
801 } else {
802 Atom state[15]; /* nr of defined state atoms */
803 int i = 0;
805 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
806 updateWorkspaceHint(wwin, False, False);
808 if (IS_OMNIPRESENT(wwin))
809 state[i++] = net_wm_state_sticky;
810 if (wwin->flags.shaded)
811 state[i++] = net_wm_state_shaded;
812 if (wwin->flags.maximized & MAX_HORIZONTAL)
813 state[i++] = net_wm_state_maximized_horz;
814 if (wwin->flags.maximized & MAX_VERTICAL)
815 state[i++] = net_wm_state_maximized_vert;
816 if (WFLAGP(wwin, skip_window_list))
817 state[i++] = net_wm_state_skip_taskbar;
818 if (wwin->flags.net_skip_pager)
819 state[i++] = net_wm_state_skip_pager;
820 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
821 state[i++] = net_wm_state_hidden;
822 state[i++] = net_wm_state_skip_pager;
824 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
825 if (!IS_OMNIPRESENT(wwin))
826 updateWorkspaceHint(wwin, True, False);
827 state[i++] = net_wm_state_sticky;
830 if (WFLAGP(wwin, sunken))
831 state[i++] = net_wm_state_below;
832 if (WFLAGP(wwin, floating))
833 state[i++] = net_wm_state_above;
834 if (wwin->flags.fullscreen)
835 state[i++] = net_wm_state_fullscreen;
837 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
838 PropModeReplace, (unsigned char *)state, i);
842 static Bool updateStrut(WScreen *scr, Window w, Bool adding)
844 WReservedArea *area;
845 Bool hasState = False;
847 if (adding) {
848 Atom type_ret;
849 int fmt_ret;
850 unsigned long nitems_ret, bytes_after_ret;
851 long *data = NULL;
853 if ((XGetWindowProperty(dpy, w, net_wm_strut, 0, 4, False,
854 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
855 &bytes_after_ret, (unsigned char **)&data) == Success && data) ||
856 ((XGetWindowProperty(dpy, w, net_wm_strut_partial, 0, 12, False,
857 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
858 &bytes_after_ret, (unsigned char **)&data) == Success && data))) {
860 /* XXX: This is strictly incorrect in the case of net_wm_strut_partial...
861 * Discard the start and end properties from the partial strut and treat it as
862 * a (deprecated) strut.
863 * This means we are marking the whole width or height of the screen as
864 * reserved, which is not necessarily what the strut defines. However for the
865 * purposes of determining placement or maximization it's probably good enough.
867 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
868 area->area.x1 = data[0];
869 area->area.x2 = data[1];
870 area->area.y1 = data[2];
871 area->area.y2 = data[3];
873 area->window = w;
875 area->next = scr->netdata->strut;
876 scr->netdata->strut = area;
878 XFree(data);
879 hasState = True;
881 } else {
882 /* deleting */
883 area = scr->netdata->strut;
885 if (area) {
886 if (area->window == w) {
887 scr->netdata->strut = area->next;
888 wfree(area);
889 hasState = True;
890 } else {
891 while (area->next && area->next->window != w)
892 area = area->next;
894 if (area->next) {
895 WReservedArea *next;
897 next = area->next->next;
898 wfree(area->next);
899 area->next = next;
901 hasState = True;
907 return hasState;
910 static int getWindowLayer(WWindow *wwin)
912 int layer = WMNormalLevel;
914 if (wwin->type == net_wm_window_type_desktop) {
915 layer = WMDesktopLevel;
916 } else if (wwin->type == net_wm_window_type_dock) {
917 layer = WMDockLevel;
918 } else if (wwin->type == net_wm_window_type_toolbar) {
919 layer = WMMainMenuLevel;
920 } else if (wwin->type == net_wm_window_type_menu) {
921 layer = WMSubmenuLevel;
922 } else if (wwin->type == net_wm_window_type_utility) {
923 } else if (wwin->type == net_wm_window_type_splash) {
924 } else if (wwin->type == net_wm_window_type_dialog) {
925 if (wwin->transient_for) {
926 WWindow *parent = wWindowFor(wwin->transient_for);
927 if (parent && parent->flags.fullscreen)
928 layer = WMNormalLevel;
930 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
931 } else if (wwin->type == net_wm_window_type_normal) {
934 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
935 layer = WMSunkenLevel;
936 if (wwin->client_flags.floating && WMFloatingLevel > layer)
937 layer = WMFloatingLevel;
939 return layer;
942 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
944 if (state == net_wm_state_sticky) {
945 if (set == _NET_WM_STATE_TOGGLE)
946 set = !IS_OMNIPRESENT(wwin);
948 if (set != wwin->flags.omnipresent)
949 wWindowSetOmnipresent(wwin, set);
951 } else if (state == net_wm_state_shaded) {
952 if (set == _NET_WM_STATE_TOGGLE)
953 set = !wwin->flags.shaded;
955 if (init) {
956 wwin->flags.shaded = set;
957 } else {
958 if (set)
959 wShadeWindow(wwin);
960 else
961 wUnshadeWindow(wwin);
963 } else if (state == net_wm_state_skip_taskbar) {
964 if (set == _NET_WM_STATE_TOGGLE)
965 set = !wwin->client_flags.skip_window_list;
967 wwin->client_flags.skip_window_list = set;
968 } else if (state == net_wm_state_skip_pager) {
969 if (set == _NET_WM_STATE_TOGGLE)
970 set = !wwin->flags.net_skip_pager;
972 wwin->flags.net_skip_pager = set;
973 } else if (state == net_wm_state_maximized_vert) {
974 if (set == _NET_WM_STATE_TOGGLE)
975 set = !(wwin->flags.maximized & MAX_VERTICAL);
977 if (init) {
978 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
979 } else {
980 if (set)
981 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
982 else
983 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
985 } else if (state == net_wm_state_maximized_horz) {
986 if (set == _NET_WM_STATE_TOGGLE)
987 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
989 if (init) {
990 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
991 } else {
992 if (set)
993 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
994 else
995 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
997 } else if (state == net_wm_state_hidden) {
998 if (set == _NET_WM_STATE_TOGGLE)
999 set = !(wwin->flags.miniaturized);
1001 if (init) {
1002 wwin->flags.miniaturized = set;
1003 } else {
1004 if (set)
1005 wIconifyWindow(wwin);
1006 else
1007 wDeiconifyWindow(wwin);
1009 } else if (state == net_wm_state_fullscreen) {
1010 if (set == _NET_WM_STATE_TOGGLE)
1011 set = !(wwin->flags.fullscreen);
1013 if (init) {
1014 wwin->flags.fullscreen = set;
1015 } else {
1016 if (set)
1017 wFullscreenWindow(wwin);
1018 else
1019 wUnfullscreenWindow(wwin);
1021 } else if (state == net_wm_state_above) {
1022 if (set == _NET_WM_STATE_TOGGLE)
1023 set = !(wwin->client_flags.floating);
1025 if (init) {
1026 wwin->client_flags.floating = set;
1027 } else {
1028 wwin->client_flags.floating = set;
1029 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1032 } else if (state == net_wm_state_below) {
1033 if (set == _NET_WM_STATE_TOGGLE)
1034 set = !(wwin->client_flags.sunken);
1036 if (init) {
1037 wwin->client_flags.sunken = set;
1038 } else {
1039 wwin->client_flags.sunken = set;
1040 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1043 } else {
1044 #ifdef DEBUG_WMSPEC
1045 wmessage("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1046 #endif
1050 static void removeIcon(WWindow *wwin)
1052 if (wwin->icon == NULL)
1053 return;
1054 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1055 XUnmapWindow(dpy, wwin->icon->core->window);
1056 RemoveFromStackList(wwin->icon->core);
1057 wIconDestroy(wwin->icon);
1058 wwin->icon = NULL;
1062 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1064 Bool ret = True;
1066 if (type == net_wm_window_type_desktop) {
1067 wwin->client_flags.no_titlebar = 1;
1068 wwin->client_flags.no_resizable = 1;
1069 wwin->client_flags.no_miniaturizable = 1;
1070 wwin->client_flags.no_border = 1;
1071 wwin->client_flags.no_resizebar = 1;
1072 wwin->client_flags.no_shadeable = 1;
1073 wwin->client_flags.no_movable = 1;
1074 wwin->client_flags.omnipresent = 1;
1075 wwin->client_flags.skip_window_list = 1;
1076 wwin->client_flags.skip_switchpanel = 1;
1077 wwin->client_flags.dont_move_off = 1;
1078 wwin->client_flags.no_appicon = 1;
1079 wwin->flags.net_skip_pager = 1;
1080 wwin->frame_x = 0;
1081 wwin->frame_y = 0;
1082 } else if (type == net_wm_window_type_dock) {
1083 wwin->client_flags.no_titlebar = 1;
1084 wwin->client_flags.no_resizable = 1;
1085 wwin->client_flags.no_miniaturizable = 1;
1086 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1087 wwin->client_flags.no_resizebar = 1;
1088 wwin->client_flags.no_shadeable = 1;
1089 wwin->client_flags.no_movable = 1;
1090 wwin->client_flags.omnipresent = 1;
1091 wwin->client_flags.skip_window_list = 1;
1092 wwin->client_flags.skip_switchpanel = 1;
1093 wwin->client_flags.dont_move_off = 1;
1094 wwin->flags.net_skip_pager = 1;
1095 } else if (type == net_wm_window_type_toolbar) {
1096 wwin->client_flags.no_titlebar = 1;
1097 wwin->client_flags.no_resizable = 1;
1098 wwin->client_flags.no_miniaturizable = 1;
1099 wwin->client_flags.no_resizebar = 1;
1100 wwin->client_flags.no_shadeable = 1;
1101 wwin->client_flags.skip_window_list = 1;
1102 wwin->client_flags.skip_switchpanel = 1;
1103 wwin->client_flags.dont_move_off = 1;
1104 wwin->client_flags.no_appicon = 1;
1105 } else if (type == net_wm_window_type_menu) {
1106 wwin->client_flags.no_titlebar = 1;
1107 wwin->client_flags.no_resizable = 1;
1108 wwin->client_flags.no_miniaturizable = 1;
1109 wwin->client_flags.no_resizebar = 1;
1110 wwin->client_flags.no_shadeable = 1;
1111 wwin->client_flags.skip_window_list = 1;
1112 wwin->client_flags.skip_switchpanel = 1;
1113 wwin->client_flags.dont_move_off = 1;
1114 wwin->client_flags.no_appicon = 1;
1115 } else if (type == net_wm_window_type_utility) {
1116 wwin->client_flags.no_appicon = 1;
1117 } else if (type == net_wm_window_type_splash) {
1118 wwin->client_flags.no_titlebar = 1;
1119 wwin->client_flags.no_resizable = 1;
1120 wwin->client_flags.no_miniaturizable = 1;
1121 wwin->client_flags.no_resizebar = 1;
1122 wwin->client_flags.no_shadeable = 1;
1123 wwin->client_flags.no_movable = 1;
1124 wwin->client_flags.skip_window_list = 1;
1125 wwin->client_flags.skip_switchpanel = 1;
1126 wwin->client_flags.dont_move_off = 1;
1127 wwin->client_flags.no_appicon = 1;
1128 wwin->flags.net_skip_pager = 1;
1129 } else if (type == net_wm_window_type_dialog) {
1130 /* These also seem a bad idea in our context -Dan
1131 // wwin->client_flags.skip_window_list = 1;
1132 // wwin->client_flags.no_appicon = 1;
1134 } else if (type == net_wm_window_type_normal) {
1135 } else {
1136 ret = False;
1139 wwin->type = type;
1140 *layer = getWindowLayer(wwin);
1142 return ret;
1145 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1147 if (wwin->type == net_wm_window_type_splash) {
1148 WScreen *scr = wwin->screen_ptr;
1149 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1150 *x = rect.pos.x + (rect.size.width - width) / 2;
1151 *y = rect.pos.y + (rect.size.height - height) / 2;
1155 static void updateWindowType(WWindow *wwin)
1157 Atom type_ret;
1158 int fmt_ret, layer;
1159 unsigned long nitems_ret, bytes_after_ret;
1160 long *data = NULL;
1162 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1163 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1164 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1166 int i;
1167 Atom *type = (Atom *) data;
1168 for (i = 0; i < nitems_ret; ++i) {
1169 if (handleWindowType(wwin, type[i], &layer))
1170 break;
1172 XFree(data);
1175 if (wwin->frame != NULL) {
1176 ChangeStackingLevel(wwin->frame->core, layer);
1177 wwin->frame->flags.need_texture_change = 1;
1178 wWindowConfigureBorders(wwin);
1179 wFrameWindowPaint(wwin->frame);
1180 wNETWMUpdateActions(wwin, False);
1184 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1186 Atom type_ret;
1187 int fmt_ret, i;
1188 unsigned long nitems_ret, bytes_after_ret;
1189 long *data = NULL;
1191 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1192 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1193 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1195 long desktop = *data;
1196 XFree(data);
1198 if (desktop == -1)
1199 wwin->client_flags.omnipresent = 1;
1200 else
1201 *workspace = desktop;
1204 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1205 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1206 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1208 Atom *state = (Atom *) data;
1209 for (i = 0; i < nitems_ret; ++i)
1210 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1212 XFree(data);
1215 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1216 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1217 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1219 Atom *type = (Atom *) data;
1220 for (i = 0; i < nitems_ret; ++i) {
1221 if (handleWindowType(wwin, type[i], layer))
1222 break;
1224 XFree(data);
1227 wNETWMUpdateActions(wwin, False);
1228 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1229 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1231 wScreenUpdateUsableArea(wwin->screen_ptr);
1234 static Bool updateNetIconInfo(WWindow *wwin)
1236 Atom type_ret;
1237 int fmt_ret;
1238 unsigned long nitems_ret, bytes_after_ret;
1239 long *data = NULL;
1240 Bool hasState = False;
1241 Bool old_state = wwin->flags.net_handle_icon;
1243 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1244 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1245 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1246 long handled = *data;
1247 wwin->flags.net_handle_icon = (handled != 0);
1248 XFree(data);
1249 hasState = True;
1251 } else {
1252 wwin->flags.net_handle_icon = False;
1255 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1256 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1257 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1259 #ifdef NETWM_PROPER
1260 if (wwin->flags.net_handle_icon)
1261 #else
1262 wwin->flags.net_handle_icon = True;
1263 #endif
1265 wwin->icon_x = data[0];
1266 wwin->icon_y = data[1];
1267 wwin->icon_w = data[2];
1268 wwin->icon_h = data[3];
1271 XFree(data);
1272 hasState = True;
1274 } else {
1275 wwin->flags.net_handle_icon = False;
1278 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1279 if (wwin->flags.net_handle_icon) {
1280 removeIcon(wwin);
1281 } else {
1282 wwin->flags.miniaturized = False;
1283 wwin->flags.skip_next_animation = True;
1284 wIconifyWindow(wwin);
1288 return hasState;
1291 void wNETWMCheckInitialClientState(WWindow *wwin)
1293 #ifdef DEBUG_WMSPEC
1294 wmessage("wNETWMCheckInitialClientState");
1295 #endif
1297 wNETWMShowingDesktop(wwin->screen_ptr, False);
1299 updateNetIconInfo(wwin);
1300 updateIconImage(wwin);
1303 static void handleDesktopNames(WScreen *scr)
1305 unsigned long nitems_ret, bytes_after_ret;
1306 char *data, *names[32];
1307 int fmt_ret, i, n;
1308 Atom type_ret;
1310 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1311 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1312 &bytes_after_ret, (unsigned char **)&data) != Success)
1313 return;
1315 if (data == NULL)
1316 return;
1318 if (type_ret != utf8_string || fmt_ret != 8)
1319 return;
1321 n = 0;
1322 names[n] = data;
1323 for (i = 0; i < nitems_ret; i++) {
1324 if (data[i] == 0) {
1325 n++;
1326 names[n] = &data[i];
1327 } else if (*names[n] == 0) {
1328 names[n] = &data[i];
1329 wWorkspaceRename(scr, n, names[n]);
1334 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1336 WScreen *scr;
1337 WWindow *wwin;
1338 Bool done = True;
1340 #ifdef DEBUG_WMSPEC
1341 wmessage("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1342 #endif
1344 scr = wScreenForWindow(event->window);
1345 if (scr) {
1346 /* generic client messages */
1347 if (event->message_type == net_current_desktop) {
1348 wWorkspaceChange(scr, event->data.l[0]);
1349 } else if (event->message_type == net_number_of_desktops) {
1350 long value;
1352 value = event->data.l[0];
1353 if (value > w_global.workspace.count) {
1354 wWorkspaceMake(scr, value - w_global.workspace.count);
1355 } else if (value < w_global.workspace.count) {
1356 int i;
1357 Bool rebuild = False;
1359 for (i = w_global.workspace.count - 1; i >= value; i--) {
1360 if (!wWorkspaceDelete(scr, i)) {
1361 rebuild = True;
1362 break;
1366 if (rebuild)
1367 updateWorkspaceCount(scr);
1369 } else if (event->message_type == net_showing_desktop) {
1370 wNETWMShowingDesktop(scr, event->data.l[0]);
1371 } else if (event->message_type == net_desktop_names) {
1372 handleDesktopNames(scr);
1373 } else {
1374 done = False;
1377 if (done)
1378 return True;
1381 /* window specific client messages */
1383 wwin = wWindowFor(event->window);
1384 if (!wwin)
1385 return False;
1387 if (event->message_type == net_active_window) {
1389 * Satisfy a client's focus request only if
1390 * - request comes from a pager, or
1391 * - it's explicitly allowed in Advanced Options, or
1392 * - giving the client the focus does not cause a change in
1393 * the active workspace (XXX: or the active head if Xinerama)
1395 if (wwin->frame->workspace == w_global.workspace.current /* No workspace change */
1396 || event->data.l[0] == 2 /* Requested by pager */
1397 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1398 wNETWMShowingDesktop(scr, False);
1399 wMakeWindowVisible(wwin);
1401 } else if (event->message_type == net_close_window) {
1402 if (!WFLAGP(wwin, no_closable)) {
1403 if (wwin->protocols.DELETE_WINDOW)
1404 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
1405 w_global.timestamp.last_event);
1407 } else if (event->message_type == net_wm_state) {
1408 int maximized = wwin->flags.maximized;
1409 long set = event->data.l[0];
1411 #ifdef DEBUG_WMSPEC
1412 wmessage("net_wm_state set %ld a1 %s a2 %s\n", set,
1413 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1414 #endif
1416 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1417 if (event->data.l[2])
1418 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1420 if (wwin->flags.maximized != maximized) {
1421 if (!wwin->flags.maximized) {
1422 wwin->flags.maximized = maximized;
1423 wUnmaximizeWindow(wwin);
1424 } else {
1425 wMaximizeWindow(wwin, wwin->flags.maximized);
1428 updateStateHint(wwin, False, False);
1429 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1430 updateNetIconInfo(wwin);
1431 } else if (event->message_type == net_wm_desktop) {
1432 long desktop = event->data.l[0];
1433 if (desktop == -1) {
1434 wWindowSetOmnipresent(wwin, True);
1435 } else {
1436 if (IS_OMNIPRESENT(wwin))
1437 wWindowSetOmnipresent(wwin, False);
1438 wWindowChangeWorkspace(wwin, desktop);
1440 } else {
1441 done = False;
1444 return done;
1447 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1449 #ifdef DEBUG_WMSPEC
1450 wmessage("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1451 #endif
1453 if (event->atom == net_wm_strut || event->atom == net_wm_strut_partial) {
1454 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1455 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1456 wScreenUpdateUsableArea(wwin->screen_ptr);
1457 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1458 updateNetIconInfo(wwin);
1459 } else if (event->atom == net_wm_window_type) {
1460 updateWindowType(wwin);
1461 } else if (event->atom == net_wm_name) {
1462 char *name = wNETWMGetWindowName(wwin->client_win);
1463 wWindowUpdateName(wwin, name);
1464 if (name)
1465 wfree(name);
1466 } else if (event->atom == net_wm_icon_name) {
1467 if (wwin->icon) {
1468 wIconChangeTitle(wwin->icon, wwin);
1469 wIconPaint(wwin->icon);
1471 } else if (event->atom == net_wm_icon) {
1472 updateIconImage(wwin);
1476 int wNETWMGetPidForWindow(Window window)
1478 Atom type_ret;
1479 int fmt_ret;
1480 unsigned long nitems_ret, bytes_after_ret;
1481 long *data = NULL;
1482 int pid;
1484 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1485 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1486 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1487 pid = *data;
1488 XFree(data);
1489 } else {
1490 pid = 0;
1493 return pid;
1496 char *wNETWMGetWindowName(Window window)
1498 char *name;
1499 char *ret;
1500 int size;
1502 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1503 if (name) {
1504 ret = wstrndup(name, size);
1505 XFree(name);
1506 } else {
1507 ret = NULL;
1510 return ret;
1513 char *wNETWMGetIconName(Window window)
1515 char *name;
1516 char *ret;
1517 int size;
1519 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1520 if (name) {
1521 ret = wstrndup(name, size);
1522 XFree(name);
1523 } else {
1524 ret = NULL;
1527 return ret;
1530 static void observer(void *self, WMNotification *notif)
1532 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1533 const char *name = WMGetNotificationName(notif);
1534 void *data = WMGetNotificationClientData(notif);
1535 NetData *ndata = (NetData *) self;
1537 if (strcmp(name, WMNManaged) == 0 && wwin) {
1538 updateClientList(wwin->screen_ptr);
1539 updateClientListStacking(wwin->screen_ptr, NULL);
1540 updateStateHint(wwin, True, False);
1542 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1543 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1544 wScreenUpdateUsableArea(wwin->screen_ptr);
1545 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1546 updateClientList(wwin->screen_ptr);
1547 updateClientListStacking(wwin->screen_ptr, wwin);
1548 updateWorkspaceHint(wwin, False, True);
1549 updateStateHint(wwin, False, True);
1550 wNETWMUpdateActions(wwin, True);
1552 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1553 wScreenUpdateUsableArea(wwin->screen_ptr);
1554 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1555 updateClientListStacking(wwin->screen_ptr, NULL);
1556 updateStateHint(wwin, False, False);
1557 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1558 updateClientListStacking(wwin->screen_ptr, NULL);
1559 updateStateHint(wwin, False, False);
1560 } else if (strcmp(name, WMNChangedFocus) == 0) {
1561 updateFocusHint(ndata->scr);
1562 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1563 updateWorkspaceHint(wwin, False, False);
1564 updateStateHint(wwin, True, False);
1565 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1566 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1570 static void wsobserver(void *self, WMNotification *notif)
1572 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1573 const char *name = WMGetNotificationName(notif);
1575 /* Parameter not used, but tell the compiler that it is ok */
1576 (void) self;
1578 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1579 updateWorkspaceCount(scr);
1580 updateWorkspaceNames(scr);
1581 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1582 updateWorkspaceCount(scr);
1583 updateWorkspaceNames(scr);
1584 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1585 updateCurrentWorkspace(scr);
1586 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1587 updateWorkspaceNames(scr);
1591 void wNETFrameExtents(WWindow *wwin)
1593 long extents[4] = { 0, 0, 0, 0 };
1595 /* The extents array describes dimensions which are not
1596 * part of the client window. In our case that means
1597 * widths of the border and heights of the titlebar and resizebar.
1599 * Index 0 = left
1600 * 1 = right
1601 * 2 = top
1602 * 3 = bottom
1604 if (wwin->frame->titlebar)
1605 extents[2] = wwin->frame->titlebar->height;
1606 if (wwin->frame->resizebar)
1607 extents[3] = wwin->frame->resizebar->height;
1608 if (HAS_BORDER(wwin)) {
1609 extents[0] += wwin->screen_ptr->frame_border_width;
1610 extents[1] += wwin->screen_ptr->frame_border_width;
1611 extents[2] += wwin->screen_ptr->frame_border_width;
1612 extents[3] += wwin->screen_ptr->frame_border_width;
1615 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1618 void wNETCleanupFrameExtents(WWindow *wwin)
1620 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);