wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / src / wmspec.c
blobe71265b07bf1d183a90bceaf50d987272809bdea
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"
53 /* Global variables */
54 extern Atom _XA_WM_DELETE_WINDOW;
55 extern Time LastTimestamp;
57 /* Root Window Properties */
58 static Atom net_supported;
59 static Atom net_client_list;
60 static Atom net_client_list_stacking;
61 static Atom net_number_of_desktops;
62 static Atom net_desktop_geometry;
63 static Atom net_desktop_viewport;
64 static Atom net_current_desktop;
65 static Atom net_desktop_names;
66 static Atom net_active_window;
67 static Atom net_supporting_wm_check;
68 static Atom net_virtual_roots; /* N/A */
69 static Atom net_desktop_layout; /* XXX */
70 static Atom net_showing_desktop;
72 /* Other Root Window Messages */
73 static Atom net_close_window;
74 static Atom net_moveresize_window; /* TODO */
75 static Atom net_wm_moveresize; /* TODO */
77 /* Application Window Properties */
78 static Atom net_wm_name;
79 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
80 static Atom net_wm_icon_name;
81 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
82 static Atom net_wm_desktop;
83 static Atom net_wm_window_type;
84 static Atom net_wm_window_type_desktop;
85 static Atom net_wm_window_type_dock;
86 static Atom net_wm_window_type_toolbar;
87 static Atom net_wm_window_type_menu;
88 static Atom net_wm_window_type_utility;
89 static Atom net_wm_window_type_splash;
90 static Atom net_wm_window_type_dialog;
91 static Atom net_wm_window_type_normal;
92 static Atom net_wm_state;
93 static Atom net_wm_state_modal; /* XXX: what is this?!? */
94 static Atom net_wm_state_sticky;
95 static Atom net_wm_state_maximized_vert;
96 static Atom net_wm_state_maximized_horz;
97 static Atom net_wm_state_shaded;
98 static Atom net_wm_state_skip_taskbar;
99 static Atom net_wm_state_skip_pager;
100 static Atom net_wm_state_hidden;
101 static Atom net_wm_state_fullscreen;
102 static Atom net_wm_state_above;
103 static Atom net_wm_state_below;
104 static Atom net_wm_allowed_actions;
105 static Atom net_wm_action_move;
106 static Atom net_wm_action_resize;
107 static Atom net_wm_action_minimize;
108 static Atom net_wm_action_shade;
109 static Atom net_wm_action_stick;
110 static Atom net_wm_action_maximize_horz;
111 static Atom net_wm_action_maximize_vert;
112 static Atom net_wm_action_fullscreen;
113 static Atom net_wm_action_change_desktop;
114 static Atom net_wm_action_close;
115 static Atom net_wm_strut; /* XXX: see net_workarea */
116 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
117 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. */
118 static Atom net_wm_icon;
119 static Atom net_wm_pid; /* TODO */
120 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
122 static Atom net_frame_extents;
124 /* Window Manager Protocols */
125 static Atom net_wm_ping; /* TODO */
127 static Atom utf8_string;
129 typedef struct {
130 char *name;
131 Atom *atom;
132 } atomitem_t;
134 static atomitem_t atomNames[] = {
135 {"_NET_SUPPORTED", &net_supported},
136 {"_NET_CLIENT_LIST", &net_client_list},
137 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
138 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
139 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
140 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
141 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
142 {"_NET_DESKTOP_NAMES", &net_desktop_names},
143 {"_NET_ACTIVE_WINDOW", &net_active_window},
144 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
145 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
146 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
147 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
149 {"_NET_CLOSE_WINDOW", &net_close_window},
150 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
151 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
153 {"_NET_WM_NAME", &net_wm_name},
154 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
155 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
156 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
157 {"_NET_WM_DESKTOP", &net_wm_desktop},
158 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
159 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
160 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
161 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
162 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
163 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
164 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
165 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
166 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
167 {"_NET_WM_STATE", &net_wm_state},
168 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
169 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
170 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
171 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
172 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
173 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
174 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
175 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
176 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
177 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
178 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
179 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
180 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
181 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
182 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
183 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
184 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
185 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
186 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
187 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
188 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
189 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
190 {"_NET_WM_STRUT", &net_wm_strut},
191 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
192 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
193 {"_NET_WM_ICON", &net_wm_icon},
194 {"_NET_WM_PID", &net_wm_pid},
195 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
197 {"_NET_FRAME_EXTENTS", &net_frame_extents},
199 {"_NET_WM_PING", &net_wm_ping},
201 {"UTF8_STRING", &utf8_string},
204 #define atomNr (sizeof(atomNames)/sizeof(atomNames[0]))
206 #define _NET_WM_STATE_REMOVE 0
207 #define _NET_WM_STATE_ADD 1
208 #define _NET_WM_STATE_TOGGLE 2
210 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
211 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
212 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
213 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
214 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
215 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
216 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
217 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
218 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
219 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
220 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
222 static void observer(void *self, WMNotification *notif);
223 static void wsobserver(void *self, WMNotification *notif);
225 static void updateClientList(WScreen *scr);
226 static void updateClientListStacking(WScreen *scr, WWindow *);
228 static void updateWorkspaceNames(WScreen *scr);
229 static void updateCurrentWorkspace(WScreen *scr);
230 static void updateWorkspaceCount(WScreen *scr);
231 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
233 typedef struct NetData {
234 WScreen *scr;
235 WReservedArea *strut;
236 WWindow **show_desktop;
237 } NetData;
239 static void setSupportedHints(WScreen *scr)
241 Atom atom[atomNr];
242 int i = 0;
244 /* set supported hints list */
245 /* XXX: extend this !!! */
247 atom[i++] = net_client_list;
248 atom[i++] = net_client_list_stacking;
249 atom[i++] = net_number_of_desktops;
250 atom[i++] = net_desktop_geometry;
251 atom[i++] = net_desktop_viewport;
252 atom[i++] = net_current_desktop;
253 atom[i++] = net_desktop_names;
254 atom[i++] = net_active_window;
255 atom[i++] = net_supporting_wm_check;
256 atom[i++] = net_showing_desktop;
257 #if 0
258 atom[i++] = net_wm_moveresize;
259 #endif
260 atom[i++] = net_wm_desktop;
261 atom[i++] = net_wm_window_type;
262 atom[i++] = net_wm_window_type_desktop;
263 atom[i++] = net_wm_window_type_dock;
264 atom[i++] = net_wm_window_type_toolbar;
265 atom[i++] = net_wm_window_type_menu;
266 atom[i++] = net_wm_window_type_utility;
267 atom[i++] = net_wm_window_type_splash;
268 atom[i++] = net_wm_window_type_dialog;
269 atom[i++] = net_wm_window_type_normal;
271 atom[i++] = net_wm_state;
272 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
273 atom[i++] = net_wm_state_sticky;
274 atom[i++] = net_wm_state_shaded;
275 atom[i++] = net_wm_state_maximized_horz;
276 atom[i++] = net_wm_state_maximized_vert;
277 atom[i++] = net_wm_state_skip_taskbar;
278 atom[i++] = net_wm_state_skip_pager;
279 atom[i++] = net_wm_state_hidden;
280 atom[i++] = net_wm_state_fullscreen;
281 atom[i++] = net_wm_state_above;
282 atom[i++] = net_wm_state_below;
284 atom[i++] = net_wm_allowed_actions;
285 atom[i++] = net_wm_action_move;
286 atom[i++] = net_wm_action_resize;
287 atom[i++] = net_wm_action_minimize;
288 atom[i++] = net_wm_action_shade;
289 atom[i++] = net_wm_action_stick;
290 atom[i++] = net_wm_action_maximize_horz;
291 atom[i++] = net_wm_action_maximize_vert;
292 atom[i++] = net_wm_action_fullscreen;
293 atom[i++] = net_wm_action_change_desktop;
294 atom[i++] = net_wm_action_close;
296 atom[i++] = net_wm_strut;
297 atom[i++] = net_wm_icon_geometry;
298 atom[i++] = net_wm_icon;
299 atom[i++] = net_wm_handled_icons;
301 atom[i++] = net_frame_extents;
303 atom[i++] = net_wm_name;
304 atom[i++] = net_wm_icon_name;
306 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
308 /* set supporting wm hint */
309 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
310 PropModeReplace, (unsigned char *)&scr->info_window, 1);
312 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
313 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
316 void wNETWMUpdateDesktop(WScreen *scr)
318 long *views, sizes[2];
319 int count, i;
321 if (scr->workspace_count == 0)
322 return;
324 count = scr->workspace_count * 2;
325 views = wmalloc(sizeof(long) * count);
326 /*memset(views, 0, sizeof(long) * count); */
327 sizes[0] = scr->scr_width;
328 sizes[1] = scr->scr_height;
330 for (i = 0; i < scr->workspace_count; i++) {
331 views[2 * i + 0] = 0;
332 views[2 * i + 1] = 0;
335 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
336 PropModeReplace, (unsigned char *)sizes, 2);
338 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
339 PropModeReplace, (unsigned char *)views, count);
341 wfree(views);
344 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
346 int count;
347 unsigned char *prop;
349 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
350 if (prop) {
351 int desktop = *(long *)prop;
352 XFree(prop);
353 return desktop;
355 return -1;
359 * Find the best icon to be used by Window Maker for appicon/miniwindows.
360 * Currently the algorithm is to take the image with the size closest
361 * to icon_size x icon_size, but never bigger than that.
363 * This algorithm is very poorly implemented and needs to be redone (it can
364 * easily select images with very large widths and very small heights over
365 * square images, if the area of the former is closer to the desired one).
367 * The logic can also be changed to accept bigger images and scale them down.
369 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
371 int size, wanted, d;
372 unsigned long i, distance;
373 unsigned long *icon;
375 /* better use only 75% of icon_size. For 64x64 this means 48x48
376 * This leaves room around the icon for the miniwindow title and
377 * results in better overall aesthetics -Dan */
378 wanted = wPreferences.icon_size * wPreferences.icon_size;
380 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
381 size = data[i] * data[i + 1];
382 if (size == 0)
383 break;
384 d = wanted - size;
385 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
386 distance = d;
387 icon = &data[i];
389 i += size + 2;
392 return icon;
395 static RImage *makeRImageFromARGBData(unsigned long *data)
397 int size, width, height, i;
398 RImage *image;
399 unsigned char *imgdata;
400 unsigned long pixel;
402 width = data[0];
403 height = data[1];
404 size = width * height;
406 if (size == 0)
407 return NULL;
409 image = RCreateImage(width, height, True);
411 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
412 pixel = data[i];
413 imgdata[3] = (pixel >> 24) & 0xff; /* A */
414 imgdata[0] = (pixel >> 16) & 0xff; /* R */
415 imgdata[1] = (pixel >> 8) & 0xff; /* G */
416 imgdata[2] = (pixel >> 0) & 0xff; /* B */
419 return image;
422 RImage *get_window_image_from_x11(Window window)
424 RImage *image;
425 Atom type;
426 int format;
427 unsigned long items, rest;
428 unsigned long *property, *data;
430 /* Get the icon from X11 Window */
431 if (XGetWindowProperty(dpy, window, net_wm_icon, 0L, LONG_MAX,
432 False, XA_CARDINAL, &type, &format, &items, &rest,
433 (unsigned char **)&property) != Success || !property)
434 return NULL;
436 if (type != XA_CARDINAL || format != 32 || items < 2) {
437 XFree(property);
438 return NULL;
441 /* Find the best icon */
442 data = findBestIcon(property, items);
443 if (!data) {
444 XFree(property);
445 return NULL;
448 /* Save the best icon in the X11 icon */
449 image = makeRImageFromARGBData(data);
451 XFree(property);
453 /* Resize the image to the correct value */
454 image = wIconValidateIconSize(image, wPreferences.icon_size);
456 return image;
459 static void updateIconImage(WWindow *wwin)
461 /* Remove the icon image from X11 */
462 if (wwin->net_icon_image)
463 RReleaseImage(wwin->net_icon_image);
465 /* Save the icon in the X11 icon */
466 wwin->net_icon_image = get_window_image_from_x11(wwin->client_win);
468 /* Refresh the Window Icon */
469 if (wwin->icon)
470 wIconUpdate(wwin->icon);
472 /* Refresh the application icon */
473 WApplication *app = wApplicationOf(wwin->main_window);
474 if (app && app->app_icon) {
475 wIconUpdate(app->app_icon->icon);
476 wAppIconPaint(app->app_icon);
480 static void updateShowDesktop(WScreen *scr, Bool show)
482 long foo;
484 foo = (show == True);
485 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
486 PropModeReplace, (unsigned char *)&foo, 1);
489 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
491 if (show && scr->netdata->show_desktop == NULL) {
492 WWindow *tmp, **wins;
493 int i = 0;
495 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
497 tmp = scr->focused_window;
498 while (tmp) {
499 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
501 wins[i++] = tmp;
502 tmp->flags.skip_next_animation = 1;
503 tmp->flags.net_show_desktop = 1;
504 wIconifyWindow(tmp);
507 tmp = tmp->prev;
509 wins[i++] = NULL;
511 scr->netdata->show_desktop = wins;
512 updateShowDesktop(scr, True);
513 } else if (scr->netdata->show_desktop != NULL) {
514 /* FIXME: get rid of workspace flashing ! */
515 int ws = scr->current_workspace;
516 WWindow **tmp;
517 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
518 wDeiconifyWindow(*tmp);
519 (*tmp)->flags.net_show_desktop = 0;
521 if (ws != scr->current_workspace)
522 wWorkspaceChange(scr, ws);
523 wfree(scr->netdata->show_desktop);
524 scr->netdata->show_desktop = NULL;
525 updateShowDesktop(scr, False);
529 void wNETWMInitStuff(WScreen *scr)
531 NetData *data;
532 int i;
534 #ifdef DEBUG_WMSPEC
535 wmessage("wNETWMInitStuff");
536 #endif
538 #ifdef HAVE_XINTERNATOMS
540 Atom atoms[atomNr];
541 char *names[atomNr];
543 for (i = 0; i < atomNr; ++i)
544 names[i] = atomNames[i].name;
546 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
547 for (i = 0; i < atomNr; ++i)
548 *atomNames[i].atom = atoms[i];
551 #else
552 for (i = 0; i < atomNr; i++)
553 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
554 #endif
556 data = wmalloc(sizeof(NetData));
557 data->scr = scr;
558 data->strut = NULL;
559 data->show_desktop = NULL;
561 scr->netdata = data;
563 setSupportedHints(scr);
565 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
566 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
567 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
568 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
569 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
570 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
571 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
573 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
574 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
575 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
576 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
578 updateClientList(scr);
579 updateClientListStacking(scr, NULL);
580 updateWorkspaceCount(scr);
581 updateWorkspaceNames(scr);
582 updateShowDesktop(scr, False);
584 wScreenUpdateUsableArea(scr);
587 void wNETWMCleanup(WScreen *scr)
589 int i;
591 for (i = 0; i < atomNr; i++)
592 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
595 void wNETWMUpdateActions(WWindow *wwin, Bool del)
597 Atom action[10]; /* nr of actions atoms defined */
598 int i = 0;
600 if (del) {
601 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
602 return;
605 if (IS_MOVABLE(wwin))
606 action[i++] = net_wm_action_move;
608 if (IS_RESIZABLE(wwin))
609 action[i++] = net_wm_action_resize;
611 if (!WFLAGP(wwin, no_miniaturizable))
612 action[i++] = net_wm_action_minimize;
614 if (!WFLAGP(wwin, no_shadeable))
615 action[i++] = net_wm_action_shade;
617 /* if (!WFLAGP(wwin, no_stickable)) */
618 action[i++] = net_wm_action_stick;
620 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
621 if (IS_RESIZABLE(wwin))
622 action[i++] = net_wm_action_maximize_horz;
624 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
625 if (IS_RESIZABLE(wwin))
626 action[i++] = net_wm_action_maximize_vert;
628 /* if (!WFLAGP(wwin, no_fullscreen)) */
629 action[i++] = net_wm_action_fullscreen;
631 /* if (!WFLAGP(wwin, no_change_desktop)) */
632 action[i++] = net_wm_action_change_desktop;
634 if (!WFLAGP(wwin, no_closable))
635 action[i++] = net_wm_action_close;
637 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
638 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
641 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
643 WReservedArea *cur;
644 WMRect rect;
646 if (!scr->netdata || !scr->netdata->strut)
647 return False;
649 area->x1 = area->y1 = area->x2 = area->y2 = 0;
651 for (cur = scr->netdata->strut; cur; cur = cur->next) {
652 WWindow *wwin = wWindowFor(cur->window);
653 if (wWindowTouchesHead(wwin, head)) {
654 if (cur->area.x1 > area->x1)
655 area->x1 = cur->area.x1;
656 if (cur->area.y1 > area->y1)
657 area->y1 = cur->area.y1;
658 if (cur->area.x2 > area->x2)
659 area->x2 = cur->area.x2;
660 if (cur->area.y2 > area->y2)
661 area->y2 = cur->area.y2;
665 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
666 return False;
668 rect = wGetRectForHead(scr, head);
670 area->x1 = rect.pos.x + area->x1;
671 area->x2 = rect.pos.x + rect.size.width - area->x2;
672 area->y1 = rect.pos.y + area->y1;
673 area->y2 = rect.pos.y + rect.size.height - area->y2;
675 return True;
678 static void updateClientList(WScreen *scr)
680 WWindow *wwin;
681 Window *windows;
682 int count;
684 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
686 count = 0;
687 wwin = scr->focused_window;
688 while (wwin) {
689 windows[count++] = wwin->client_win;
690 wwin = wwin->prev;
692 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
693 PropModeReplace, (unsigned char *)windows, count);
695 wfree(windows);
696 XFlush(dpy);
699 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
701 WWindow *wwin;
702 Window *client_list, *client_list_reverse;
703 int client_count, i;
704 WCoreWindow *tmp;
705 WMBagIterator iter;
707 /* update client list */
708 i = scr->window_count + 1;
709 client_list = (Window *) wmalloc(sizeof(Window) * i);
710 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
712 client_count = 0;
713 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
714 while (tmp) {
715 wwin = wWindowFor(tmp->window);
716 /* wwin_excl is a window to exclude from the list
717 (e.g. it's now unmanaged) */
718 if (wwin && (wwin != wwin_excl))
719 client_list[client_count++] = wwin->client_win;
720 tmp = tmp->stacking->under;
724 for (i = 0; i < client_count; i++) {
725 Window w = client_list[client_count - i - 1];
726 client_list_reverse[i] = w;
729 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
730 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
732 wfree(client_list);
733 wfree(client_list_reverse);
735 XFlush(dpy);
738 static void updateWorkspaceCount(WScreen *scr)
739 { /* changeable */
740 long count;
742 count = scr->workspace_count;
744 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
745 32, PropModeReplace, (unsigned char *)&count, 1);
748 static void updateCurrentWorkspace(WScreen *scr)
749 { /* changeable */
750 long count;
752 count = scr->current_workspace;
754 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
755 PropModeReplace, (unsigned char *)&count, 1);
758 static void updateWorkspaceNames(WScreen *scr)
760 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
761 unsigned int i, len, curr_size;
763 pos = buf;
764 len = 0;
765 for (i = 0; i < scr->workspace_count; i++) {
766 curr_size = strlen(scr->workspaces[i]->name);
767 strcpy(pos, scr->workspaces[i]->name);
768 pos += (curr_size + 1);
769 len += (curr_size + 1);
772 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
773 PropModeReplace, (unsigned char *)buf, len);
776 static void updateFocusHint(WScreen *scr, WWindow *wwin)
777 { /* changeable */
778 Window window;
780 if (!scr->focused_window || !scr->focused_window->flags.focused)
781 window = None;
782 else
783 window = scr->focused_window->client_win;
785 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
786 PropModeReplace, (unsigned char *)&window, 1);
789 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
791 long l;
793 if (del) {
794 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
795 } else {
796 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
797 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
798 32, PropModeReplace, (unsigned char *)&l, 1);
802 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
803 { /* changeable */
804 if (del) {
805 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
806 } else {
807 Atom state[15]; /* nr of defined state atoms */
808 int i = 0;
810 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
811 updateWorkspaceHint(wwin, False, False);
813 if (IS_OMNIPRESENT(wwin))
814 state[i++] = net_wm_state_sticky;
815 if (wwin->flags.shaded)
816 state[i++] = net_wm_state_shaded;
817 if (wwin->flags.maximized & MAX_HORIZONTAL)
818 state[i++] = net_wm_state_maximized_horz;
819 if (wwin->flags.maximized & MAX_VERTICAL)
820 state[i++] = net_wm_state_maximized_vert;
821 if (WFLAGP(wwin, skip_window_list))
822 state[i++] = net_wm_state_skip_taskbar;
823 if (wwin->flags.net_skip_pager)
824 state[i++] = net_wm_state_skip_pager;
825 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
826 state[i++] = net_wm_state_hidden;
827 state[i++] = net_wm_state_skip_pager;
829 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
830 if (!IS_OMNIPRESENT(wwin))
831 updateWorkspaceHint(wwin, True, False);
832 state[i++] = net_wm_state_sticky;
835 if (WFLAGP(wwin, sunken))
836 state[i++] = net_wm_state_below;
837 if (WFLAGP(wwin, floating))
838 state[i++] = net_wm_state_above;
839 if (wwin->flags.fullscreen)
840 state[i++] = net_wm_state_fullscreen;
842 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
843 PropModeReplace, (unsigned char *)state, i);
847 static Bool updateStrut(WScreen *scr, Window w, Bool adding)
849 WReservedArea *area;
850 Bool hasState = False;
852 if (adding) {
853 Atom type_ret;
854 int fmt_ret;
855 unsigned long nitems_ret, bytes_after_ret;
856 long *data = NULL;
858 if ((XGetWindowProperty(dpy, w, net_wm_strut, 0, 4, False,
859 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
860 &bytes_after_ret, (unsigned char **)&data) == Success && data) ||
861 ((XGetWindowProperty(dpy, w, net_wm_strut_partial, 0, 12, False,
862 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
863 &bytes_after_ret, (unsigned char **)&data) == Success && data))) {
865 /* XXX: This is strictly incorrect in the case of net_wm_strut_partial...
866 * Discard the start and end properties from the partial strut and treat it as
867 * a (deprecated) strut.
868 * This means we are marking the whole width or height of the screen as
869 * reserved, which is not necessarily what the strut defines. However for the
870 * purposes of determining placement or maximization it's probably good enough.
872 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
873 area->area.x1 = data[0];
874 area->area.x2 = data[1];
875 area->area.y1 = data[2];
876 area->area.y2 = data[3];
878 area->window = w;
880 area->next = scr->netdata->strut;
881 scr->netdata->strut = area;
883 XFree(data);
884 hasState = True;
886 } else {
887 /* deleting */
888 area = scr->netdata->strut;
890 if (area) {
891 if (area->window == w) {
892 scr->netdata->strut = area->next;
893 wfree(area);
894 hasState = True;
895 } else {
896 while (area->next && area->next->window != w)
897 area = area->next;
899 if (area->next) {
900 WReservedArea *next;
902 next = area->next->next;
903 wfree(area->next);
904 area->next = next;
906 hasState = True;
912 return hasState;
915 static int getWindowLayer(WWindow *wwin)
917 int layer = WMNormalLevel;
919 if (wwin->type == net_wm_window_type_desktop) {
920 layer = WMDesktopLevel;
921 } else if (wwin->type == net_wm_window_type_dock) {
922 layer = WMDockLevel;
923 } else if (wwin->type == net_wm_window_type_toolbar) {
924 layer = WMMainMenuLevel;
925 } else if (wwin->type == net_wm_window_type_menu) {
926 layer = WMSubmenuLevel;
927 } else if (wwin->type == net_wm_window_type_utility) {
928 } else if (wwin->type == net_wm_window_type_splash) {
929 } else if (wwin->type == net_wm_window_type_dialog) {
930 if (wwin->transient_for) {
931 WWindow *parent = wWindowFor(wwin->transient_for);
932 if (parent && parent->flags.fullscreen)
933 layer = WMFullscreenLevel;
935 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
936 } else if (wwin->type == net_wm_window_type_normal) {
939 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
940 layer = WMSunkenLevel;
941 if (wwin->client_flags.floating && WMFloatingLevel > layer)
942 layer = WMFloatingLevel;
944 return layer;
947 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
949 if (state == net_wm_state_sticky) {
950 if (set == _NET_WM_STATE_TOGGLE)
951 set = !IS_OMNIPRESENT(wwin);
953 if (set != wwin->flags.omnipresent)
954 wWindowSetOmnipresent(wwin, set);
956 } else if (state == net_wm_state_shaded) {
957 if (set == _NET_WM_STATE_TOGGLE)
958 set = !wwin->flags.shaded;
960 if (init) {
961 wwin->flags.shaded = set;
962 } else {
963 if (set)
964 wShadeWindow(wwin);
965 else
966 wUnshadeWindow(wwin);
968 } else if (state == net_wm_state_skip_taskbar) {
969 if (set == _NET_WM_STATE_TOGGLE)
970 set = !wwin->client_flags.skip_window_list;
972 wwin->client_flags.skip_window_list = set;
973 } else if (state == net_wm_state_skip_pager) {
974 if (set == _NET_WM_STATE_TOGGLE)
975 set = !wwin->flags.net_skip_pager;
977 wwin->flags.net_skip_pager = set;
978 } else if (state == net_wm_state_maximized_vert) {
979 if (set == _NET_WM_STATE_TOGGLE)
980 set = !(wwin->flags.maximized & MAX_VERTICAL);
982 if (init) {
983 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
984 } else {
985 if (set)
986 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
987 else
988 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
990 } else if (state == net_wm_state_maximized_horz) {
991 if (set == _NET_WM_STATE_TOGGLE)
992 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
994 if (init) {
995 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
996 } else {
997 if (set)
998 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
999 else
1000 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1002 } else if (state == net_wm_state_hidden) {
1003 if (set == _NET_WM_STATE_TOGGLE)
1004 set = !(wwin->flags.miniaturized);
1006 if (init) {
1007 wwin->flags.miniaturized = set;
1008 } else {
1009 if (set)
1010 wIconifyWindow(wwin);
1011 else
1012 wDeiconifyWindow(wwin);
1014 } else if (state == net_wm_state_fullscreen) {
1015 if (set == _NET_WM_STATE_TOGGLE)
1016 set = !(wwin->flags.fullscreen);
1018 if (init) {
1019 wwin->flags.fullscreen = set;
1020 } else {
1021 if (set)
1022 wFullscreenWindow(wwin);
1023 else
1024 wUnfullscreenWindow(wwin);
1026 } else if (state == net_wm_state_above) {
1027 if (set == _NET_WM_STATE_TOGGLE)
1028 set = !(wwin->client_flags.floating);
1030 if (init) {
1031 wwin->client_flags.floating = set;
1032 } else {
1033 wwin->client_flags.floating = set;
1034 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1037 } else if (state == net_wm_state_below) {
1038 if (set == _NET_WM_STATE_TOGGLE)
1039 set = !(wwin->client_flags.sunken);
1041 if (init) {
1042 wwin->client_flags.sunken = set;
1043 } else {
1044 wwin->client_flags.sunken = set;
1045 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1048 } else {
1049 #ifdef DEBUG_WMSPEC
1050 wmessage("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1051 #endif
1055 static void removeIcon(WWindow *wwin)
1057 if (wwin->icon == NULL)
1058 return;
1059 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1060 XUnmapWindow(dpy, wwin->icon->core->window);
1061 RemoveFromStackList(wwin->icon->core);
1062 wIconDestroy(wwin->icon);
1063 wwin->icon = NULL;
1067 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1069 Bool ret = True;
1071 if (type == net_wm_window_type_desktop) {
1072 wwin->client_flags.no_titlebar = 1;
1073 wwin->client_flags.no_resizable = 1;
1074 wwin->client_flags.no_miniaturizable = 1;
1075 wwin->client_flags.no_border = 1;
1076 wwin->client_flags.no_resizebar = 1;
1077 wwin->client_flags.no_shadeable = 1;
1078 wwin->client_flags.no_movable = 1;
1079 wwin->client_flags.omnipresent = 1;
1080 wwin->client_flags.skip_window_list = 1;
1081 wwin->client_flags.skip_switchpanel = 1;
1082 wwin->client_flags.dont_move_off = 1;
1083 wwin->client_flags.no_appicon = 1;
1084 wwin->flags.net_skip_pager = 1;
1085 wwin->frame_x = 0;
1086 wwin->frame_y = 0;
1087 } else if (type == net_wm_window_type_dock) {
1088 wwin->client_flags.no_titlebar = 1;
1089 wwin->client_flags.no_resizable = 1;
1090 wwin->client_flags.no_miniaturizable = 1;
1091 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1092 wwin->client_flags.no_resizebar = 1;
1093 wwin->client_flags.no_shadeable = 1;
1094 wwin->client_flags.no_movable = 1;
1095 wwin->client_flags.omnipresent = 1;
1096 wwin->client_flags.skip_window_list = 1;
1097 wwin->client_flags.skip_switchpanel = 1;
1098 wwin->client_flags.dont_move_off = 1;
1099 wwin->flags.net_skip_pager = 1;
1100 } else if (type == net_wm_window_type_toolbar) {
1101 wwin->client_flags.no_titlebar = 1;
1102 wwin->client_flags.no_resizable = 1;
1103 wwin->client_flags.no_miniaturizable = 1;
1104 wwin->client_flags.no_resizebar = 1;
1105 wwin->client_flags.no_shadeable = 1;
1106 wwin->client_flags.skip_window_list = 1;
1107 wwin->client_flags.skip_switchpanel = 1;
1108 wwin->client_flags.dont_move_off = 1;
1109 wwin->client_flags.no_appicon = 1;
1110 } else if (type == net_wm_window_type_menu) {
1111 wwin->client_flags.no_titlebar = 1;
1112 wwin->client_flags.no_resizable = 1;
1113 wwin->client_flags.no_miniaturizable = 1;
1114 wwin->client_flags.no_resizebar = 1;
1115 wwin->client_flags.no_shadeable = 1;
1116 wwin->client_flags.skip_window_list = 1;
1117 wwin->client_flags.skip_switchpanel = 1;
1118 wwin->client_flags.dont_move_off = 1;
1119 wwin->client_flags.no_appicon = 1;
1120 } else if (type == net_wm_window_type_utility) {
1121 wwin->client_flags.no_appicon = 1;
1122 } else if (type == net_wm_window_type_splash) {
1123 wwin->client_flags.no_titlebar = 1;
1124 wwin->client_flags.no_resizable = 1;
1125 wwin->client_flags.no_miniaturizable = 1;
1126 wwin->client_flags.no_resizebar = 1;
1127 wwin->client_flags.no_shadeable = 1;
1128 wwin->client_flags.no_movable = 1;
1129 wwin->client_flags.skip_window_list = 1;
1130 wwin->client_flags.skip_switchpanel = 1;
1131 wwin->client_flags.dont_move_off = 1;
1132 wwin->client_flags.no_appicon = 1;
1133 wwin->flags.net_skip_pager = 1;
1134 } else if (type == net_wm_window_type_dialog) {
1135 /* These also seem a bad idea in our context -Dan
1136 // wwin->client_flags.skip_window_list = 1;
1137 // wwin->client_flags.no_appicon = 1;
1139 } else if (type == net_wm_window_type_normal) {
1140 } else {
1141 ret = False;
1144 wwin->type = type;
1145 *layer = getWindowLayer(wwin);
1147 return ret;
1150 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1152 if (wwin->type == net_wm_window_type_splash) {
1153 WScreen *scr = wwin->screen_ptr;
1154 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1155 *x = rect.pos.x + (rect.size.width - width) / 2;
1156 *y = rect.pos.y + (rect.size.height - height) / 2;
1160 static void updateWindowType(WWindow *wwin)
1162 Atom type_ret;
1163 int fmt_ret, layer;
1164 unsigned long nitems_ret, bytes_after_ret;
1165 long *data = NULL;
1167 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1168 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1169 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1171 int i;
1172 Atom *type = (Atom *) data;
1173 for (i = 0; i < nitems_ret; ++i) {
1174 if (handleWindowType(wwin, type[i], &layer))
1175 break;
1177 XFree(data);
1180 if (wwin->frame != NULL) {
1181 ChangeStackingLevel(wwin->frame->core, layer);
1182 wwin->frame->flags.need_texture_change = 1;
1183 wWindowConfigureBorders(wwin);
1184 wFrameWindowPaint(wwin->frame);
1185 wNETWMUpdateActions(wwin, False);
1189 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1191 Atom type_ret;
1192 int fmt_ret, i;
1193 unsigned long nitems_ret, bytes_after_ret;
1194 long *data = NULL;
1196 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1197 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1198 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1200 long desktop = *data;
1201 XFree(data);
1203 if (desktop == -1)
1204 wwin->client_flags.omnipresent = 1;
1205 else
1206 *workspace = desktop;
1209 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1210 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1211 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1213 Atom *state = (Atom *) data;
1214 for (i = 0; i < nitems_ret; ++i)
1215 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1217 XFree(data);
1220 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1221 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1222 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1224 Atom *type = (Atom *) data;
1225 for (i = 0; i < nitems_ret; ++i) {
1226 if (handleWindowType(wwin, type[i], layer))
1227 break;
1229 XFree(data);
1232 wNETWMUpdateActions(wwin, False);
1233 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1234 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1236 wScreenUpdateUsableArea(wwin->screen_ptr);
1239 static Bool updateNetIconInfo(WWindow *wwin)
1241 Atom type_ret;
1242 int fmt_ret;
1243 unsigned long nitems_ret, bytes_after_ret;
1244 long *data = NULL;
1245 Bool hasState = False;
1246 Bool old_state = wwin->flags.net_handle_icon;
1248 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1249 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1250 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1251 long handled = *data;
1252 wwin->flags.net_handle_icon = (handled != 0);
1253 XFree(data);
1254 hasState = True;
1256 } else {
1257 wwin->flags.net_handle_icon = False;
1260 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1261 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1262 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1264 #ifdef NETWM_PROPER
1265 if (wwin->flags.net_handle_icon)
1266 #else
1267 wwin->flags.net_handle_icon = True;
1268 #endif
1270 wwin->icon_x = data[0];
1271 wwin->icon_y = data[1];
1272 wwin->icon_w = data[2];
1273 wwin->icon_h = data[3];
1276 XFree(data);
1277 hasState = True;
1279 } else {
1280 wwin->flags.net_handle_icon = False;
1283 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1284 if (wwin->flags.net_handle_icon) {
1285 removeIcon(wwin);
1286 } else {
1287 wwin->flags.miniaturized = False;
1288 wwin->flags.skip_next_animation = True;
1289 wIconifyWindow(wwin);
1293 return hasState;
1296 void wNETWMCheckInitialClientState(WWindow *wwin)
1298 #ifdef DEBUG_WMSPEC
1299 wmessage("wNETWMCheckInitialClientState");
1300 #endif
1302 wNETWMShowingDesktop(wwin->screen_ptr, False);
1304 updateNetIconInfo(wwin);
1305 updateIconImage(wwin);
1308 static void handleDesktopNames(XClientMessageEvent *event, WScreen *scr)
1310 unsigned long nitems_ret, bytes_after_ret;
1311 char *data, *names[32];
1312 int fmt_ret, i, n;
1313 Atom type_ret;
1315 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1316 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1317 &bytes_after_ret, (unsigned char **)&data) != Success)
1318 return;
1320 if (data == NULL)
1321 return;
1323 if (type_ret != utf8_string || fmt_ret != 8)
1324 return;
1326 n = 0;
1327 names[n] = data;
1328 for (i = 0; i < nitems_ret; i++) {
1329 if (data[i] == 0) {
1330 n++;
1331 names[n] = &data[i];
1332 } else if (*names[n] == 0) {
1333 names[n] = &data[i];
1334 wWorkspaceRename(scr, n, names[n]);
1339 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1341 WScreen *scr;
1342 WWindow *wwin;
1343 Bool done = True;
1345 #ifdef DEBUG_WMSPEC
1346 wmessage("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1347 #endif
1349 scr = wScreenForWindow(event->window);
1350 if (scr) {
1351 /* generic client messages */
1352 if (event->message_type == net_current_desktop) {
1353 wWorkspaceChange(scr, event->data.l[0]);
1354 } else if (event->message_type == net_number_of_desktops) {
1355 long value;
1357 value = event->data.l[0];
1358 if (value > scr->workspace_count) {
1359 wWorkspaceMake(scr, value - scr->workspace_count);
1360 } else if (value < scr->workspace_count) {
1361 int i;
1362 Bool rebuild = False;
1364 for (i = scr->workspace_count - 1; i >= value; i--) {
1365 if (!wWorkspaceDelete(scr, i)) {
1366 rebuild = True;
1367 break;
1371 if (rebuild)
1372 updateWorkspaceCount(scr);
1374 } else if (event->message_type == net_showing_desktop) {
1375 wNETWMShowingDesktop(scr, event->data.l[0]);
1376 } else if (event->message_type == net_desktop_names) {
1377 handleDesktopNames(event, scr);
1378 } else {
1379 done = False;
1382 if (done)
1383 return True;
1386 /* window specific client messages */
1388 wwin = wWindowFor(event->window);
1389 if (!wwin)
1390 return False;
1392 if (event->message_type == net_active_window) {
1394 * Satisfy a client's focus request only if
1395 * - request comes from a pager, or
1396 * - it's explicitly allowed in Advanced Options, or
1397 * - giving the client the focus does not cause a change in
1398 * the active workspace (XXX: or the active head if Xinerama)
1400 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1401 || event->data.l[0] == 2 /* Requested by pager */
1402 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1403 wNETWMShowingDesktop(scr, False);
1404 wMakeWindowVisible(wwin);
1406 } else if (event->message_type == net_close_window) {
1407 if (!WFLAGP(wwin, no_closable)) {
1408 if (wwin->protocols.DELETE_WINDOW)
1409 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1411 } else if (event->message_type == net_wm_state) {
1412 int maximized = wwin->flags.maximized;
1413 long set = event->data.l[0];
1415 #ifdef DEBUG_WMSPEC
1416 wmessage("net_wm_state set %ld a1 %s a2 %s\n", set,
1417 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1418 #endif
1420 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1421 if (event->data.l[2])
1422 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1424 if (wwin->flags.maximized != maximized) {
1425 if (!wwin->flags.maximized) {
1426 wwin->flags.maximized = maximized;
1427 wUnmaximizeWindow(wwin);
1428 } else {
1429 wMaximizeWindow(wwin, wwin->flags.maximized);
1432 updateStateHint(wwin, False, False);
1433 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1434 updateNetIconInfo(wwin);
1435 } else if (event->message_type == net_wm_desktop) {
1436 long desktop = event->data.l[0];
1437 if (desktop == -1) {
1438 wWindowSetOmnipresent(wwin, True);
1439 } else {
1440 if (IS_OMNIPRESENT(wwin))
1441 wWindowSetOmnipresent(wwin, False);
1442 wWindowChangeWorkspace(wwin, desktop);
1444 } else {
1445 done = False;
1448 return done;
1451 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1453 #ifdef DEBUG_WMSPEC
1454 wmessage("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1455 #endif
1457 if (event->atom == net_wm_strut || event->atom == net_wm_strut_partial) {
1458 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1459 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1460 wScreenUpdateUsableArea(wwin->screen_ptr);
1461 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1462 updateNetIconInfo(wwin);
1463 } else if (event->atom == net_wm_window_type) {
1464 updateWindowType(wwin);
1465 } else if (event->atom == net_wm_name) {
1466 char *name = wNETWMGetWindowName(wwin->client_win);
1467 wWindowUpdateName(wwin, name);
1468 if (name)
1469 wfree(name);
1470 } else if (event->atom == net_wm_icon_name) {
1471 if (wwin->icon) {
1472 wIconChangeTitle(wwin->icon, wwin);
1473 wIconPaint(wwin->icon);
1475 } else if (event->atom == net_wm_icon) {
1476 updateIconImage(wwin);
1480 int wNETWMGetPidForWindow(Window window)
1482 Atom type_ret;
1483 int fmt_ret;
1484 unsigned long nitems_ret, bytes_after_ret;
1485 long *data = NULL;
1486 int pid;
1488 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1489 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1490 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1491 pid = *data;
1492 XFree(data);
1493 } else {
1494 pid = 0;
1497 return pid;
1500 char *wNETWMGetWindowName(Window window)
1502 char *name;
1503 char *ret;
1504 int size;
1506 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1507 if (name) {
1508 ret = wstrndup(name, size);
1509 XFree(name);
1510 } else {
1511 ret = NULL;
1514 return ret;
1517 char *wNETWMGetIconName(Window window)
1519 char *name;
1520 char *ret;
1521 int size;
1523 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1524 if (name) {
1525 ret = wstrndup(name, size);
1526 XFree(name);
1527 } else {
1528 ret = NULL;
1531 return ret;
1534 static void observer(void *self, WMNotification *notif)
1536 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1537 const char *name = WMGetNotificationName(notif);
1538 void *data = WMGetNotificationClientData(notif);
1539 NetData *ndata = (NetData *) self;
1541 if (strcmp(name, WMNManaged) == 0 && wwin) {
1542 updateClientList(wwin->screen_ptr);
1543 updateClientListStacking(wwin->screen_ptr, NULL);
1544 updateStateHint(wwin, True, False);
1546 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1547 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1548 wScreenUpdateUsableArea(wwin->screen_ptr);
1549 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1550 updateClientList(wwin->screen_ptr);
1551 updateClientListStacking(wwin->screen_ptr, wwin);
1552 updateWorkspaceHint(wwin, False, True);
1553 updateStateHint(wwin, False, True);
1554 wNETWMUpdateActions(wwin, True);
1556 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1557 wScreenUpdateUsableArea(wwin->screen_ptr);
1558 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1559 updateClientListStacking(wwin->screen_ptr, NULL);
1560 updateStateHint(wwin, False, False);
1561 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1562 updateClientListStacking(wwin->screen_ptr, NULL);
1563 updateStateHint(wwin, False, False);
1564 } else if (strcmp(name, WMNChangedFocus) == 0) {
1565 updateFocusHint(ndata->scr, wwin);
1566 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1567 updateWorkspaceHint(wwin, False, False);
1568 updateStateHint(wwin, True, False);
1569 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1570 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1574 static void wsobserver(void *self, WMNotification *notif)
1576 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1577 const char *name = WMGetNotificationName(notif);
1579 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1580 updateWorkspaceCount(scr);
1581 updateWorkspaceNames(scr);
1582 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1583 updateWorkspaceCount(scr);
1584 updateWorkspaceNames(scr);
1585 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1586 updateCurrentWorkspace(scr);
1587 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1588 updateWorkspaceNames(scr);
1592 void wNETFrameExtents(WWindow *wwin)
1594 long extents[4] = { 0, 0, 0, 0 };
1596 /* The extents array describes dimensions which are not
1597 * part of the client window. In our case that means
1598 * widths of the border and heights of the titlebar and resizebar.
1600 * Index 0 = left
1601 * 1 = right
1602 * 2 = top
1603 * 3 = bottom
1605 if (wwin->frame->titlebar)
1606 extents[2] = wwin->frame->titlebar->height;
1607 if (wwin->frame->resizebar)
1608 extents[3] = wwin->frame->resizebar->height;
1609 if (HAS_BORDER(wwin)) {
1610 extents[0] += wwin->screen_ptr->frame_border_width;
1611 extents[1] += wwin->screen_ptr->frame_border_width;
1612 extents[2] += wwin->screen_ptr->frame_border_width;
1613 extents[3] += wwin->screen_ptr->frame_border_width;
1616 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1619 void wNETCleanupFrameExtents(WWindow *wwin)
1621 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);