Use wfree instead of free.
[wmaker-crm.git] / src / client.c
blob819c68b1e1f8ce137a293958316f9b5b02e5e10e
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"
46 #ifdef KWM_HINTS
47 #include "kwm.h"
48 #endif
50 /****** Global Variables ******/
52 /* contexts */
53 extern XContext wWinContext;
55 extern Atom _XA_WM_STATE;
56 extern Atom _XA_WM_PROTOCOLS;
57 extern Atom _XA_WM_COLORMAP_WINDOWS;
59 extern Atom _XA_WINDOWMAKER_MENU;
61 extern Atom _XA_GNUSTEP_WM_ATTR;
62 extern Atom _XA_GNUSTEP_WM_RESIZEBAR;
64 #ifdef SHAPE
65 extern Bool wShapeSupported;
66 #endif
70 *--------------------------------------------------------------------
71 * wClientRestore--
72 * Reparent the window back to the root window.
74 *--------------------------------------------------------------------
76 void
77 wClientRestore(WWindow *wwin)
79 #if 0
80 int gx, gy;
82 wClientGetGravityOffsets(wwin, &gx, &gy);
83 /* set the positio of the frame on screen */
84 wwin->frame_x -= gx * FRAME_BORDER_WIDTH;
85 wwin->frame_y -= gy * FRAME_BORDER_WIDTH;
86 /* if gravity is to the south, account for the border sizes */
87 if (gy > 0)
88 wwin->frame_y += (wwin->frame->top_width + wwin->frame->bottom_width);
89 #endif
90 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
91 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
92 wwin->frame_x, wwin->frame_y);
94 /* don't let the window get iconified after restart */
96 if (wwin->flags.shaded)
97 wClientSetState(wwin, NormalState, None);
103 *----------------------------------------------------------------------
104 * wClientSetState--
105 * Set the state of the client window to one of the window
106 * states defined in ICCCM (Iconic, Withdrawn, Normal)
108 * Side effects:
109 * The WM_STATE property of the window is updated as well as the
110 * WWindow.state variable.
111 *----------------------------------------------------------------------
113 void
114 wClientSetState(WWindow *wwin, int state, Window icon_win)
116 CARD32 data[2];
118 wwin->state = state;
120 data[0] = (unsigned long) state;
121 data[1] = (unsigned long) icon_win;
123 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
124 PropModeReplace, (unsigned char *) data, 2);
128 void
129 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
131 switch (wwin->normal_hints->win_gravity) {
132 case ForgetGravity:
133 case CenterGravity:
134 case StaticGravity:
135 *ofs_x = 0;
136 *ofs_y = 0;
137 break;
138 case NorthWestGravity:
139 *ofs_x = -1;
140 *ofs_y = -1;
141 break;
142 case NorthGravity:
143 *ofs_x = 0;
144 *ofs_y = -1;
145 break;
146 case NorthEastGravity:
147 *ofs_x = 1;
148 *ofs_y = -1;
149 break;
150 case WestGravity:
151 *ofs_x = -1;
152 *ofs_y = 0;
153 break;
154 case EastGravity:
155 *ofs_x = 1;
156 *ofs_y = 0;
157 break;
158 case SouthWestGravity:
159 *ofs_x = -1;
160 *ofs_y = 1;
161 break;
162 case SouthGravity:
163 *ofs_x = 0;
164 *ofs_y = 1;
165 break;
166 case SouthEastGravity:
167 *ofs_x = 1;
168 *ofs_y = 1;
169 break;
175 void
176 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
178 XWindowChanges xwc;
179 int nx, ny, nwidth, nheight;
180 int ofs_x, ofs_y;
182 if (wwin==NULL) {
184 * configure a window that was not mapped by us
186 xwc.x = xcre->x;
187 xwc.y = xcre->y;
188 xwc.width = xcre->width;
189 xwc.height = xcre->height;
190 xwc.border_width = xcre->border_width;
191 xwc.stack_mode = xcre->detail;
192 xwc.sibling = xcre->above;
193 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
194 return;
196 #ifdef SHAPE
197 if (wShapeSupported) {
198 int junk;
199 unsigned int ujunk;
200 int b_shaped;
202 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
203 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
204 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
205 &ujunk);
206 wwin->flags.shaped = b_shaped;
208 #endif
209 if (xcre->value_mask & CWStackMode) {
210 WObjDescriptor *desc;
211 WWindow *sibling;
213 if ((xcre->value_mask & CWSibling) &&
214 (XFindContext(dpy, xcre->above, wWinContext,
215 (XPointer *)&desc) == XCSUCCESS)
216 && (desc->parent_type==WCLASS_WINDOW)) {
217 sibling=desc->parent;
218 xwc.sibling = sibling->frame->core->window;
219 } else {
220 xwc.sibling = xcre->above;
222 xwc.stack_mode = xcre->detail;
223 XConfigureWindow(dpy, wwin->frame->core->window,
224 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
225 /* fix stacking order */
226 RemakeStackList(wwin->screen_ptr);
229 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
231 if (xcre->value_mask & CWBorderWidth) {
232 wwin->old_border_width = xcre->border_width;
235 if (!wwin->flags.shaded) {
236 /* If the window is shaded, wrong height will be set for the window */
237 if (xcre->value_mask & CWX) {
238 nx = xcre->x;
239 if (!WFLAGP(wwin, no_border))
240 nx -= FRAME_BORDER_WIDTH;
242 else
243 nx = wwin->frame_x;
245 if (xcre->value_mask & CWY) {
246 ny = xcre->y - ((ofs_y < 0) ? 0 : wwin->frame->top_width);
247 if (!WFLAGP(wwin, no_border))
248 ny -= FRAME_BORDER_WIDTH;
250 else
251 ny = wwin->frame_y;
253 if (xcre->value_mask & CWWidth)
254 nwidth = xcre->width;
255 else
256 nwidth = wwin->frame->core->width;
258 if (xcre->value_mask & CWHeight)
259 nheight = xcre->height;
260 else
261 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
263 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
264 wwin->old_geometry.x = nx;
265 wwin->old_geometry.y = ny;
266 wwin->old_geometry.width = nwidth;
267 wwin->old_geometry.height = nheight;
272 void
273 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
275 XEvent event;
277 event.xclient.type = ClientMessage;
278 event.xclient.message_type = _XA_WM_PROTOCOLS;
279 event.xclient.format = 32;
280 event.xclient.display = dpy;
281 event.xclient.window = wwin->client_win;
282 event.xclient.data.l[0] = protocol;
283 event.xclient.data.l[1] = time;
284 event.xclient.data.l[2] = 0;
285 event.xclient.data.l[3] = 0;
286 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
287 XSync(dpy, False);
292 void
293 wClientKill(WWindow *wwin)
295 XKillClient(dpy, wwin->client_win);
297 XFlush(dpy);
301 *----------------------------------------------------------------------
302 * wClientCheckProperty--
303 * Handles PropertyNotify'es, verifying which property was
304 * changed and updating internal state according to that, like redrawing
305 * the icon title when it is changed.
307 * Side effects:
308 * Depends on the changed property.
310 * TODO: _GNUSTEP_WM_ATTR
311 *----------------------------------------------------------------------
313 void
314 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
316 XWindowAttributes attribs;
317 XWMHints *new_hints;
318 int i, g1, g2;
319 char *tmp;
321 switch (event->atom) {
322 case XA_WM_NAME:
323 /* window title was changed */
324 wwin->flags.wm_name_changed = 1;
325 if (wwin->frame) {
326 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
327 /* the hint was removed */
328 tmp = wstrdup(DEF_WINDOW_TITLE);
329 #ifdef KWM_HINTS
330 wKWMSendEventMessage(wwin, WKWMChangedClient);
331 #endif
333 if (wFrameWindowChangeTitle(wwin->frame, tmp)) {
334 /* only update the menu if the title has actually changed */
335 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
336 #ifdef KWM_HINTS
337 wKWMSendEventMessage(wwin, WKWMChangedClient);
338 #endif
340 if (tmp)
341 wfree(tmp);
343 break;
345 case XA_WM_ICON_NAME:
346 #ifdef KWM_HINTS
347 wKWMSendEventMessage(wwin, WKWMChangedClient);
348 #endif
349 if (!wwin->icon)
350 break;
351 else {
352 char *new_title;
354 /* icon title was changed */
355 wGetIconName(dpy, wwin->client_win, &new_title);
356 wIconChangeTitle(wwin->icon, new_title);
358 break;
360 case XA_WM_COMMAND:
361 if (wwin->main_window!=None) {
362 WApplication *wapp = wApplicationOf(wwin->main_window);
363 char **argv;
364 int argc;
366 if (!wapp || !wapp->app_icon)
367 break;
369 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
370 if (argc > 0 && argv != NULL) {
371 if (wapp->app_icon->command)
372 wfree(wapp->app_icon->command);
373 wapp->app_icon->command = wtokenjoin(argv, argc);
375 if (argv) {
376 XFreeStringList(argv);
380 break;
382 case XA_WM_HINTS:
383 /* WM_HINTS */
385 new_hints = XGetWMHints(dpy, wwin->client_win);
387 /* group leader update
389 * This means that the window is setting the leader after
390 * it was mapped, changing leaders or removing the leader.
392 * Valid state transitions are:
394 * _1 __2
395 * / \ / \
396 * v | v |
397 * (GC) (GC')
398 * / ^ / ^
399 * 3| |4 5| |6
400 * | | | |
401 * v / v /
402 * (G'C) (G'C')
404 * Where G is the window_group hint, C is CLIENT_LEADER property
405 * and ' indicates the hint is unset.
407 * 1,2 - change group leader to new value of window_group
408 * 3 - change leader to value of CLIENT_LEADER
409 * 4 - change leader to value of window_group
410 * 5 - destroy application
411 * 6 - create application
413 if (new_hints && (new_hints->flags & WindowGroupHint)
414 && new_hints->window_group!=None) {
415 g2 = 1;
416 } else {
417 g2 = 0;
419 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
420 && wwin->wm_hints->window_group!=None) {
421 g1 = 1;
422 } else {
423 g1 = 0;
426 if (wwin->client_leader) {
427 if (g1 && g2
428 && wwin->wm_hints->window_group!=new_hints->window_group) {
429 i = 1;
430 } else if (g1 && !g2) {
431 i = 3;
432 } else if (!g1 && g2) {
433 i = 4;
434 } else {
435 i = 0;
437 } else {
438 if (g1 && g2
439 && wwin->wm_hints->window_group!=new_hints->window_group) {
440 i = 2;
441 } else if (g1 && !g2) {
442 i = 5;
443 } else if (!g1 && g2) {
444 i = 6;
445 } else {
446 i = 0;
450 if (wwin->wm_hints)
451 XFree(wwin->wm_hints);
453 wwin->wm_hints = new_hints;
455 /* do action according to state transition */
456 switch (i) {
457 /* 3 - change leader to value of CLIENT_LEADER */
458 case 3:
459 wApplicationDestroy(wApplicationOf(wwin->main_window));
460 wwin->main_window = wwin->client_leader;
461 wwin->group_id = None;
462 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
463 break;
465 /* 1,2,4 - change leader to new value of window_group */
466 case 1:
467 case 2:
468 case 4:
469 wApplicationDestroy(wApplicationOf(wwin->main_window));
470 wwin->main_window = new_hints->window_group;
471 wwin->group_id = wwin->main_window;
472 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
473 break;
475 /* 5 - destroy application */
476 case 5:
477 wApplicationDestroy(wApplicationOf(wwin->main_window));
478 wwin->main_window = None;
479 wwin->group_id = None;
480 break;
482 /* 6 - create application */
483 case 6:
484 wwin->main_window = new_hints->window_group;
485 wwin->group_id = wwin->main_window;
486 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
487 break;
489 #ifdef DEBUG
490 if (i) {
491 printf("window leader update caused state transition %i\n",i);
493 #endif
495 if (wwin->wm_hints) {
496 /* update icon */
497 if ((wwin->wm_hints->flags & IconPixmapHint)
498 || (wwin->wm_hints->flags & IconWindowHint)) {
499 WApplication *wapp;
501 if (wwin->flags.miniaturized && wwin->icon) {
502 wIconUpdate(wwin->icon);
504 wapp = wApplicationOf(wwin->main_window);
505 if (wapp && wapp->app_icon) {
506 wIconUpdate(wapp->app_icon->icon);
508 #ifdef KWM_HINTS
509 wKWMSendEventMessage(wwin, WKWMIconChange);
510 #endif
513 if (wwin->wm_hints->flags & UrgencyHint)
514 wwin->flags.urgent = 1;
515 else
516 wwin->flags.urgent = 0;
517 } else {
518 wwin->group_id = None;
520 break;
522 case XA_WM_NORMAL_HINTS:
523 /* normal (geometry) hints */
525 int foo;
526 unsigned bar;
528 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
529 wClientGetNormalHints(wwin, &attribs, False, &foo, &foo,
530 &bar, &bar);
531 /* TODO: should we check for consistency of the current
532 * size against the new geometry hints? */
534 break;
536 case XA_WM_TRANSIENT_FOR:
538 Window new_owner;
539 WWindow *owner;
541 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
542 new_owner = None;
543 } else {
544 if (new_owner==0 || new_owner == wwin->client_win) {
545 new_owner = wwin->screen_ptr->root_win;
548 if (new_owner!=wwin->transient_for) {
549 owner = wWindowFor(wwin->transient_for);
550 if (owner) {
551 if (owner->flags.semi_focused) {
552 owner->flags.semi_focused = 0;
553 if ((owner->flags.mapped || owner->flags.shaded)
554 && owner->frame)
555 wFrameWindowPaint(owner->frame);
558 owner = wWindowFor(new_owner);
559 if (owner) {
560 if (!owner->flags.semi_focused) {
561 owner->flags.semi_focused = 1;
562 if ((owner->flags.mapped || owner->flags.shaded)
563 && owner->frame)
564 wFrameWindowPaint(owner->frame);
567 wwin->transient_for = new_owner;
568 if (new_owner==None) {
569 if (WFLAGP(wwin, no_miniaturizable)) {
570 WSETUFLAG(wwin, no_miniaturizable, 0);
571 WSETUFLAG(wwin, no_miniaturize_button, 0);
572 if (wwin->frame)
573 wWindowConfigureBorders(wwin);
575 } else if (!WFLAGP(wwin, no_miniaturizable)) {
576 WSETUFLAG(wwin, no_miniaturizable, 1);
577 WSETUFLAG(wwin, no_miniaturize_button, 1);
578 if (wwin->frame)
579 wWindowConfigureBorders(wwin);
583 break;
585 default:
586 if (event->atom==_XA_WM_PROTOCOLS) {
588 PropGetProtocols(wwin->client_win, &wwin->protocols);
590 WSETUFLAG(wwin, kill_close, !wwin->protocols.DELETE_WINDOW);
592 if (wwin->frame)
593 wWindowUpdateButtonImages(wwin);
595 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
597 GetColormapWindows(wwin);
598 wColormapInstallForWindow(wwin->screen_ptr, wwin);
600 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
601 WApplication *wapp;
603 wapp = wApplicationOf(wwin->main_window);
604 if (wapp) {
605 if (wapp->menu) {
606 /* update menu */
607 /* TODO: remake appmenu update */
608 wAppMenuDestroy(wapp->menu);
610 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
611 /* make the appmenu be mapped */
612 wSetFocusTo(wwin->screen_ptr, NULL);
613 wSetFocusTo(wwin->screen_ptr,
614 wwin->screen_ptr->focused_window);
616 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
617 GNUstepWMAttributes *attr;
619 PropGetGNUstepWMAttr(wwin->client_win, &attr);
621 wWindowUpdateGNUstepAttr(wwin, attr);
623 XFree(attr);
624 } else {
625 #ifdef KWM_HINTS
626 Bool done;
628 done = wKWMCheckClientHintChange(wwin, event);
629 #endif /* KWM_HINTS */
636 *----------------------------------------------------------------------
637 * wClientGetNormalHints--
638 * Get size (normal) hints and a default geometry for the client
639 * window. The hints are also checked for inconsistency. If geometry is
640 * True, the returned data will account for client specified initial
641 * geometry.
643 * Side effects:
644 * normal_hints is filled with valid data.
645 *----------------------------------------------------------------------
647 void
648 wClientGetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, Bool geometry,
649 int *x, int *y, unsigned *width, unsigned *height)
651 int pre_icccm = 0; /* not used */
653 /* find a position for the window */
654 if (!wwin->normal_hints)
655 wwin->normal_hints = XAllocSizeHints();
657 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
658 wwin->normal_hints->flags = 0;
660 *x = wattribs->x;
661 *y = wattribs->y;
663 *width = wattribs->width;
664 *height = wattribs->height;
666 if (!(wwin->normal_hints->flags & PWinGravity)) {
667 wwin->normal_hints->win_gravity = NorthWestGravity;
669 if (!(wwin->normal_hints->flags & PMinSize)) {
670 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
671 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
673 if (!(wwin->normal_hints->flags & PBaseSize)) {
674 wwin->normal_hints->base_width = 0;
675 wwin->normal_hints->base_height = 0;
677 if (!(wwin->normal_hints->flags & PMaxSize)) {
678 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
679 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
682 /* some buggy apps set weird hints.. */
683 if (wwin->normal_hints->min_width <= 0)
684 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
686 if (wwin->normal_hints->min_height <= 0)
687 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
690 if (wwin->normal_hints->max_width < wwin->normal_hints->min_width)
691 wwin->normal_hints->max_width = wwin->normal_hints->min_width;
693 if (wwin->normal_hints->max_height < wwin->normal_hints->min_height)
694 wwin->normal_hints->max_height = wwin->normal_hints->min_height;
696 if (!(wwin->normal_hints->flags & PResizeInc)) {
697 wwin->normal_hints->width_inc = 1;
698 wwin->normal_hints->height_inc = 1;
699 } else {
700 if (wwin->normal_hints->width_inc <= 0)
701 wwin->normal_hints->width_inc = 1;
702 if (wwin->normal_hints->height_inc <= 0)
703 wwin->normal_hints->height_inc = 1;
706 if (wwin->normal_hints->flags & PAspect) {
707 if (wwin->normal_hints->min_aspect.x < 1)
708 wwin->normal_hints->min_aspect.x = 1;
709 if (wwin->normal_hints->min_aspect.y < 1)
710 wwin->normal_hints->min_aspect.y = 1;
712 if (wwin->normal_hints->max_aspect.x < 1)
713 wwin->normal_hints->max_aspect.x = 1;
714 if (wwin->normal_hints->max_aspect.y < 1)
715 wwin->normal_hints->max_aspect.y = 1;
718 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
719 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
721 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
722 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
725 #ifdef IGNORE_PPOSITION
726 wwin->normal_hints->flags &= ~PPosition;
727 #endif
729 if (pre_icccm && !wwin->screen_ptr->flags.startup && geometry) {
730 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
731 *x = wwin->normal_hints->x;
732 *y = wwin->normal_hints->y;
734 if (wwin->normal_hints->flags & (USSize|PSize)) {
735 *width = wwin->normal_hints->width;
736 *height = wwin->normal_hints->height;
742 void
743 GetColormapWindows(WWindow *wwin)
745 #ifndef NO_CRASHES
746 if (wwin->cmap_windows) {
747 XFree(wwin->cmap_windows);
750 wwin->cmap_windows = NULL;
751 wwin->cmap_window_no = 0;
753 if (!XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
754 &(wwin->cmap_window_no))
755 || !wwin->cmap_windows) {
756 wwin->cmap_window_no = 0;
757 wwin->cmap_windows = NULL;
760 #endif