Updating to version 0.20.2
[wmaker-crm.git] / src / client.c
blobd49bfd6675b45e758571976700ccf6132d575c81
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "framewin.h"
37 #include "window.h"
38 #include "properties.h"
39 #include "actions.h"
40 #include "icon.h"
41 #include "client.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "appicon.h"
45 #include "appmenu.h"
47 /****** Global Variables ******/
49 /* contexts */
50 extern XContext wWinContext;
52 extern Atom _XA_WM_STATE;
53 extern Atom _XA_WM_PROTOCOLS;
54 extern Atom _XA_WM_COLORMAP_WINDOWS;
56 extern Atom _XA_WINDOWMAKER_MENU;
58 extern Atom _XA_GNUSTEP_WM_ATTR;
59 extern Atom _XA_GNUSTEP_WM_RESIZEBAR;
61 #ifdef SHAPE
62 extern Bool wShapeSupported;
63 #endif
67 *--------------------------------------------------------------------
68 * wClientRestore--
69 * Reparent the window back to the root window.
71 *--------------------------------------------------------------------
73 void
74 wClientRestore(WWindow *wwin)
76 int gx, gy;
78 wClientGetGravityOffsets(wwin, &gx, &gy);
79 /* set the positio of the frame on screen */
80 wwin->frame_x -= gx * FRAME_BORDER_WIDTH;
81 wwin->frame_y -= gy * FRAME_BORDER_WIDTH;
82 /* if gravity is to the south, account for the border sizes */
83 if (gy > 0)
84 wwin->frame_y += (wwin->frame->top_width + wwin->frame->bottom_width);
86 XUnmapWindow(dpy, wwin->client_win);
87 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
88 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
89 wwin->frame_x, wwin->frame_y);
90 if (wwin->flags.mapped)
91 XMapWindow(dpy, wwin->client_win);
93 /* don't let the window get iconified after restart */
94 if (wwin->flags.shaded)
95 wClientSetState(wwin, NormalState, None);
100 *----------------------------------------------------------------------
101 * wClientSetState--
102 * Set the state of the client window to one of the window
103 * states defined in ICCCM (Iconic, Withdrawn, Normal)
105 * Side effects:
106 * The WM_STATE property of the window is updated as well as the
107 * WWindow.state variable.
108 *----------------------------------------------------------------------
110 void
111 wClientSetState(WWindow *wwin, int state, Window icon_win)
113 CARD32 data[2];
115 wwin->state = state;
117 data[0] = (unsigned long) state;
118 data[1] = (unsigned long) icon_win;
120 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
121 PropModeReplace, (unsigned char *) data, 2);
125 void
126 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
128 switch (wwin->normal_hints->win_gravity) {
129 case ForgetGravity:
130 case CenterGravity:
131 case StaticGravity:
132 *ofs_x = 0;
133 *ofs_y = 0;
134 break;
135 case NorthWestGravity:
136 *ofs_x = -1;
137 *ofs_y = -1;
138 break;
139 case NorthGravity:
140 *ofs_x = 0;
141 *ofs_y = -1;
142 break;
143 case NorthEastGravity:
144 *ofs_x = 1;
145 *ofs_y = -1;
146 break;
147 case WestGravity:
148 *ofs_x = -1;
149 *ofs_y = 0;
150 break;
151 case EastGravity:
152 *ofs_x = 1;
153 *ofs_y = 0;
154 break;
155 case SouthWestGravity:
156 *ofs_x = -1;
157 *ofs_y = 1;
158 break;
159 case SouthGravity:
160 *ofs_x = 0;
161 *ofs_y = 1;
162 break;
163 case SouthEastGravity:
164 *ofs_x = 1;
165 *ofs_y = 1;
166 break;
172 void
173 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
175 XWindowChanges xwc;
176 int nx, ny, nwidth, nheight;
177 int ofs_x, ofs_y;
179 if (wwin==NULL) {
181 * configure a window that was not mapped by us
183 xwc.x = xcre->x;
184 xwc.y = xcre->y;
185 xwc.width = xcre->width;
186 xwc.height = xcre->height;
187 xwc.border_width = xcre->border_width;
188 xwc.stack_mode = xcre->detail;
189 xwc.sibling = xcre->above;
190 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
191 return;
193 #ifdef SHAPE
194 if (wShapeSupported) {
195 int junk;
196 unsigned int ujunk;
197 int b_shaped;
199 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
200 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
201 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
202 &ujunk);
203 wwin->flags.shaped = b_shaped;
205 #endif
206 if (xcre->value_mask & CWStackMode) {
207 WObjDescriptor *desc;
208 WWindow *sibling;
210 if ((xcre->value_mask & CWSibling) &&
211 (XFindContext(dpy, xcre->above, wWinContext,
212 (XPointer *)&desc) == XCSUCCESS)
213 && (desc->parent_type==WCLASS_WINDOW)) {
214 sibling=desc->parent;
215 xwc.sibling = sibling->frame->core->window;
216 } else {
217 xwc.sibling = xcre->above;
219 xwc.stack_mode = xcre->detail;
220 XConfigureWindow(dpy, wwin->frame->core->window,
221 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
222 /* fix stacking order */
223 RemakeStackList(wwin->screen_ptr);
226 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
228 if (xcre->value_mask & CWBorderWidth) {
229 wwin->old_border_width = xcre->border_width;
232 if (!wwin->flags.shaded) {
233 /* If the window is shaded, wrong height will be set for the window */
234 if (xcre->value_mask & CWX)
235 nx = xcre->x - FRAME_BORDER_WIDTH;
236 else
237 nx = wwin->frame_x;
239 if (xcre->value_mask & CWY)
240 ny = xcre->y - FRAME_BORDER_WIDTH - ((ofs_y < 0) ? 0 : wwin->frame->top_width);
241 else
242 ny = wwin->frame_y;
244 if (xcre->value_mask & CWWidth)
245 nwidth = xcre->width;
246 else
247 nwidth = wwin->frame->core->width;
249 if (xcre->value_mask & CWHeight)
250 nheight = xcre->height;
251 else
252 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
254 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
255 wwin->old_geometry.x = nx;
256 wwin->old_geometry.y = ny;
257 wwin->old_geometry.width = nwidth;
258 wwin->old_geometry.height = nheight;
263 void
264 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
266 XEvent event;
268 event.xclient.type = ClientMessage;
269 event.xclient.message_type = _XA_WM_PROTOCOLS;
270 event.xclient.format = 32;
271 event.xclient.display = dpy;
272 event.xclient.window = wwin->client_win;
273 event.xclient.data.l[0] = protocol;
274 event.xclient.data.l[1] = time;
275 event.xclient.data.l[2] = 0;
276 event.xclient.data.l[3] = 0;
277 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
278 XSync(dpy, False);
283 void
284 wClientKill(WWindow *wwin)
286 XKillClient(dpy, wwin->client_win);
287 XFlush(dpy);
291 *----------------------------------------------------------------------
292 * wClientCheckProperty--
293 * Handles PropertyNotify'es, verifying which property was
294 * changed and updating internal state according to that, like redrawing
295 * the icon title when it is changed.
297 * Side effects:
298 * Depends on the changed property.
300 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
301 *----------------------------------------------------------------------
303 void
304 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
306 XWindowAttributes attribs;
307 XWMHints *new_hints;
308 int i, g1, g2;
309 char *tmp;
311 switch (event->atom) {
312 case XA_WM_NAME:
313 /* window title was changed */
314 if (wwin->frame) {
315 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
316 /* the hint was removed */
317 tmp = wstrdup(DEF_WINDOW_TITLE);
319 if (wFrameWindowChangeTitle(wwin->frame, tmp)) {
320 /* only update the menu if the title has actually changed */
321 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
323 if (tmp)
324 free(tmp);
326 break;
328 case XA_WM_ICON_NAME:
329 if (!wwin->icon)
330 break;
331 else {
332 char *new_title;
334 /* icon title was changed */
335 wGetIconName(dpy, wwin->client_win, &new_title);
336 wIconChangeTitle(wwin->icon, new_title);
338 break;
340 case XA_WM_COMMAND:
341 if (wwin->main_window!=None) {
342 WApplication *wapp = wApplicationOf(wwin->main_window);
343 char **argv;
344 int argc;
346 if (wapp && wapp->app_icon) {
347 if (wapp->app_icon->command!=NULL)
348 free(wapp->app_icon->command);
350 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
351 if (argc > 0 && argv != NULL)
352 wapp->app_icon->command = FlattenStringList(argv,argc);
353 if (argv) {
354 XFreeStringList(argv);
359 break;
361 case XA_WM_HINTS:
362 /* WM_HINTS */
364 new_hints = XGetWMHints(dpy, wwin->client_win);
366 /* group leader update
368 * This means that the window is setting the leader after
369 * it was mapped, changing leaders or removing the leader.
371 * Valid state transitions are:
373 * _1 __2
374 * / \ / \
375 * v | v |
376 * (GC) (GC')
377 * / ^ / ^
378 * 3| |4 5| |6
379 * | | | |
380 * v / v /
381 * (G'C) (G'C')
383 * Where G is the window_group hint, C is CLIENT_LEADER property
384 * and ' indicates the hint is unset.
386 * 1,2 - change group leader to new value of window_group
387 * 3 - change leader to value of CLIENT_LEADER
388 * 4 - change leader to value of window_group
389 * 5 - destroy application
390 * 6 - create application
392 if (new_hints && (new_hints->flags & WindowGroupHint)
393 && new_hints->window_group!=None) {
394 g2 = 1;
395 } else {
396 g2 = 0;
398 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
399 && wwin->wm_hints->window_group!=None) {
400 g1 = 1;
401 } else {
402 g1 = 0;
405 if (wwin->client_leader) {
406 if (g1 && g2
407 && wwin->wm_hints->window_group!=new_hints->window_group) {
408 i = 1;
409 } else if (g1 && !g2) {
410 i = 3;
411 } else if (!g1 && g2) {
412 i = 4;
413 } else {
414 i = 0;
416 } else {
417 if (g1 && g2
418 && wwin->wm_hints->window_group!=new_hints->window_group) {
419 i = 2;
420 } else if (g1 && !g2) {
421 i = 5;
422 } else if (!g1 && g2) {
423 i = 6;
424 } else {
425 i = 0;
429 if (wwin->wm_hints)
430 XFree(wwin->wm_hints);
432 wwin->wm_hints = new_hints;
434 /* do action according to state transition */
435 switch (i) {
436 /* 3 - change leader to value of CLIENT_LEADER */
437 case 3:
438 wApplicationDestroy(wApplicationOf(wwin->main_window));
439 wwin->main_window = wwin->client_leader;
440 wwin->group_id = None;
441 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
442 break;
444 /* 1,2,4 - change leader to new value of window_group */
445 case 1:
446 case 2:
447 case 4:
448 wApplicationDestroy(wApplicationOf(wwin->main_window));
449 wwin->main_window = new_hints->window_group;
450 wwin->group_id = wwin->main_window;
451 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
452 break;
454 /* 5 - destroy application */
455 case 5:
456 wApplicationDestroy(wApplicationOf(wwin->main_window));
457 wwin->main_window = None;
458 wwin->group_id = None;
459 break;
461 /* 6 - create application */
462 case 6:
463 wwin->main_window = new_hints->window_group;
464 wwin->group_id = wwin->main_window;
465 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
466 break;
468 #ifdef DEBUG
469 if (i) {
470 printf("window leader update caused state transition %i\n",i);
472 #endif
474 if (wwin->wm_hints) {
475 /* update icon */
476 if ((wwin->wm_hints->flags & IconPixmapHint)
477 || (wwin->wm_hints->flags & IconWindowHint)) {
478 WApplication *wapp;
480 if (wwin->flags.miniaturized) {
481 wIconUpdate(wwin->icon);
483 wapp = wApplicationOf(wwin->main_window);
484 if (wapp && wapp->app_icon) {
485 wIconUpdate(wapp->app_icon->icon);
489 if (wwin->wm_hints->flags & UrgencyHint)
490 wwin->flags.urgent = 1;
491 else
492 wwin->flags.urgent = 0;
493 } else {
494 wwin->group_id = None;
496 break;
498 case XA_WM_NORMAL_HINTS:
499 /* normal (geometry) hints */
501 int foo;
502 unsigned bar;
504 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
505 wClientGetNormalHints(wwin, &attribs, False, &foo, &foo,
506 &bar, &bar);
507 /* TODO: should we check for consistency of the current
508 * size against the new geometry hints? */
510 break;
512 case XA_WM_TRANSIENT_FOR:
514 Window new_owner;
515 WWindow *owner;
517 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
518 new_owner = None;
519 } else {
520 if (new_owner==0 || new_owner == wwin->client_win) {
521 new_owner = wwin->screen_ptr->root_win;
524 if (new_owner!=wwin->transient_for) {
525 owner = wWindowFor(wwin->transient_for);
526 if (owner) {
527 if (owner->flags.semi_focused) {
528 owner->flags.semi_focused = 0;
529 if ((owner->flags.mapped || owner->flags.shaded)
530 && owner->frame)
531 wFrameWindowPaint(owner->frame);
534 owner = wWindowFor(new_owner);
535 if (owner) {
536 if (!owner->flags.semi_focused) {
537 owner->flags.semi_focused = 1;
538 if ((owner->flags.mapped || owner->flags.shaded)
539 && owner->frame)
540 wFrameWindowPaint(owner->frame);
543 wwin->transient_for = new_owner;
544 if (new_owner==None) {
545 if (wwin->window_flags.no_miniaturizable) {
546 wwin->window_flags.no_miniaturizable = 0;
547 wwin->window_flags.no_miniaturize_button = 0;
548 if (wwin->frame)
549 wWindowConfigureBorders(wwin);
551 } else if (!wwin->window_flags.no_miniaturizable) {
552 wwin->window_flags.no_miniaturizable = 1;
553 wwin->window_flags.no_miniaturize_button = 1;
554 if (wwin->frame)
555 wWindowConfigureBorders(wwin);
559 break;
561 default:
562 if (event->atom==_XA_WM_PROTOCOLS) {
563 PropGetProtocols(wwin->client_win, &wwin->protocols);
564 if (wwin->protocols.DELETE_WINDOW) {
565 wwin->window_flags.kill_close = 0;
566 } else {
567 wwin->window_flags.kill_close = 1;
569 if (wwin->frame)
570 wWindowUpdateButtonImages(wwin);
572 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
574 GetColormapWindows(wwin);
575 wColormapInstallForWindow(wwin->screen_ptr, wwin);
577 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
578 WApplication *wapp;
580 wapp = wApplicationOf(wwin->main_window);
581 if (wapp) {
582 if (wapp->menu) {
583 /* update menu */
584 /* TODO: remake appmenu update */
585 wAppMenuDestroy(wapp->menu);
587 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
588 /* make the appmenu be mapped */
589 wSetFocusTo(wwin->screen_ptr, NULL);
590 wSetFocusTo(wwin->screen_ptr,
591 wwin->screen_ptr->focused_window);
593 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
594 GNUstepWMAttributes *attr;
596 PropGetGNUstepWMAttr(wwin->client_win, &attr);
598 wWindowUpdateGNUstepAttr(wwin, attr);
600 XFree(attr);
607 *----------------------------------------------------------------------
608 * wClientGetNormalHints--
609 * Get size (normal) hints and a default geometry for the client
610 * window. The hints are also checked for inconsistency. If geometry is
611 * True, the returned data will account for client specified initial
612 * geometry.
614 * Side effects:
615 * normal_hints is filled with valid data.
616 *----------------------------------------------------------------------
618 void
619 wClientGetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, Bool geometry,
620 int *x, int *y, unsigned *width, unsigned *height)
622 int pre_icccm=0;
624 /* find a position for the window */
625 if (!wwin->normal_hints)
626 wwin->normal_hints = XAllocSizeHints();
628 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
629 wwin->normal_hints->flags = 0;
631 *x = wattribs->x;
632 *y = wattribs->y;
634 *width = wattribs->width;
635 *height = wattribs->height;
637 if (!(wwin->normal_hints->flags & PWinGravity)) {
638 wwin->normal_hints->win_gravity = NorthWestGravity;
640 if (!(wwin->normal_hints->flags & PMinSize)) {
641 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
642 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
644 if (!(wwin->normal_hints->flags & PBaseSize)) {
645 wwin->normal_hints->base_width = 0;
646 wwin->normal_hints->base_height = 0;
648 if (!(wwin->normal_hints->flags & PMaxSize)) {
649 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
650 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
653 if (!(wwin->normal_hints->flags & PResizeInc)) {
654 wwin->normal_hints->width_inc = 1;
655 wwin->normal_hints->height_inc = 1;
656 } else {
657 if (wwin->normal_hints->width_inc <= 0)
658 wwin->normal_hints->width_inc = 1;
659 if (wwin->normal_hints->height_inc <= 0)
660 wwin->normal_hints->height_inc = 1;
663 if (wwin->normal_hints->flags & PAspect) {
664 if (wwin->normal_hints->min_aspect.x < 1)
665 wwin->normal_hints->min_aspect.x = 1;
666 if (wwin->normal_hints->min_aspect.y < 1)
667 wwin->normal_hints->min_aspect.y = 1;
669 if (wwin->normal_hints->max_aspect.x < 1)
670 wwin->normal_hints->max_aspect.x = 1;
671 if (wwin->normal_hints->max_aspect.y < 1)
672 wwin->normal_hints->max_aspect.y = 1;
675 if (wwin->normal_hints->min_width <= 0)
676 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
678 if (wwin->normal_hints->min_height <= 0)
679 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
681 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
682 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
684 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
685 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
688 /* pre ICCCM (old) client */
689 if (pre_icccm && !wwin->screen_ptr->flags.startup && geometry) {
690 #ifdef DEBUG
691 printf("PRE ICCCM\n");
692 #endif
693 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
694 *x = wwin->normal_hints->x;
695 *y = wwin->normal_hints->y;
697 if (wwin->normal_hints->flags & (USSize|PSize)) {
698 *width = wwin->normal_hints->width;
699 *height = wwin->normal_hints->height;
705 void
706 GetColormapWindows(WWindow *wwin)
708 if (wwin->cmap_windows) {
709 XFree(wwin->cmap_windows);
711 if (!XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
712 &(wwin->cmap_window_no))) {
713 wwin->cmap_window_no = 0;
714 wwin->cmap_windows = NULL;