File: manage.cc

package info (click to toggle)
miwm 1.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,296 kB
  • ctags: 910
  • sloc: cpp: 8,179; sh: 231; makefile: 148
file content (517 lines) | stat: -rw-r--r-- 13,686 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//-*-c++-*-
// -------------------------------------------
// RCS data:
// $Date: 2003/07/03 13:25:29 $
// $Revision: 1.2 $
// $Source: /cvsroot/miwm/miwm/miwm/manage.cc,v $
// $Id: manage.cc,v 1.2 2003/07/03 13:25:29 sgbeal Exp $
// $RCSfile: manage.cc,v $
// -------------------------------------------
// Copyright by Ben Paul Wise.
// -------------------------------------------
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
// -------------------------------------------

#include	<stdio.h>
#include	<stdlib.h>

#include	<X11/X.h>
#include	<X11/Xlib.h>
#include	<X11/Xutil.h>
#include	<X11/Xatom.h>

#include "miwm.h"

void setshape(Client *);
static int getprop(Window, Atom, Atom, long, unsigned char **);
static int getstate(Window, int *);
static void apply_gravity(Client *);

//-------------------------------------------------

static void
apply_gravity(Client *c)
{
  if (c->size.flags & PWinGravity) {
    switch (c->size.win_gravity) {
    case NorthEastGravity:
      c->size.x -= 2 * border;
      break;
    case SouthWestGravity:
      c->size.y -= 2 * border;
      break;
    case SouthEastGravity:
      c->size.x -= 2 * border;
      c->size.y -= 2 * border;
      break;
    }
  }
}

void
getTransientFor(Client *c) {
  Window	trans = None;
  XGetTransientForHint(dpy, c->window, &trans);
  c->trans = trans;
}

void
withdraw(Client *c) {
  if (c->parent != root) {
    XUnmapWindow(dpy, c->parent);
    c->isMapped = 0; // is this right?
    XReparentWindow(dpy, c->parent, root, c->size.x, c->size.y);
  }

  XRemoveFromSaveSet(dpy, c->window);
  setClientState(c, WithdrawnState);


//  Flush and ignore any errors. X11 sends us an UnmapNotify before it
//  sends us a DestroyNotify. That means we can get here without knowing
//  whether the relevant window still exists.

  ignore_badwindow = 1;
  XSync(dpy, False);
  ignore_badwindow = 0;
}

static void
installcmap(Colormap cmap) {
  if (cmap == None)
    cmap = DefaultColormap(dpy, DefaultScreen(dpy));
  XInstallColormap(dpy, cmap);
}

void
cmapfocus(Client *c) {
  int	i;
  int	found;
  Client	*cc;

  if (c == 0)
    installcmap(None);
  else if (c->ncmapwins != 0) {
    found = 0;
    for (i = c->ncmapwins - 1; i >= 0; i--) {
      installcmap(c->wmcmaps[i]);
      if (c->cmapwins[i] == c->window)
	found++;
    }
    if (!found)
      installcmap(c->cmap);
  } else if (c->trans != None && (cc = getClient(c->trans)) != 0 &&
	     cc->ncmapwins != 0)
    cmapfocus(cc);
  else
    installcmap(c->cmap);
}

void
getColormaps(Client *c) {
  int	n;
  int	i;
  Window	*cw;
  XWindowAttributes	attr;

  if (c == 0)
    return;

  n = getprop(c->window, wm_colormaps, XA_WINDOW, 100L, (unsigned char **) &cw);
  if (c->ncmapwins != 0) {
    XFree(c->cmapwins);
    free(c->wmcmaps);
  }
  if (n <= 0) {
    c->ncmapwins = 0;
    return;
  }
  c->ncmapwins = n;
  c->cmapwins = cw;

  c->wmcmaps = (Colormap *) malloc(n * sizeof(Colormap));
  for (i = 0; i < n; i++) {
    if (cw[i] == c->window)
      c->wmcmaps[i] = c->cmap;
    else {
      XSelectInput(dpy, cw[i], ColormapChangeMask);
      XGetWindowAttributes(dpy, cw[i], &attr);
      c->wmcmaps[i] = attr.colormap;
    }
  }
}

void
// Terminate(int signal)
Terminate(int) { // parameter signal is unused
  cout << endl;
  cout << "Terminating MIWM ... ";

  delete theWM;
  theWM = NULL;


  // Give up the input focus and the colormap. 
  XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
  installcmap(None);

  XCloseDisplay(dpy);

  StopMemoryLeakCheck();

  cout << "Terminated MIWM" << endl << flush;

  exit(EXIT_FAILURE);

  return;
}


static int
getprop(Window w, Atom a, Atom type, long len, unsigned char **p) {
  Atom	real_type;
  int	format;
  unsigned long	n;
  unsigned long	extra;
  int	status;

  // len is in 32-bit multiples.
  status = XGetWindowProperty(dpy, w, a, 0L, len, False, type, &real_type, &format, &n, &extra, p);
  if (status != Success || *p == 0)
    return -1;
  if (n == 0)
    XFree(*p);

  // could check real_type, format, extra here...

  return n;
}

void
getWindowName(Client *c) {
  char * name;
  Atom actual_type;
  int format;
  unsigned long n;
  unsigned long extra;
  XClassHint * class_hints;
  int was_nameless;

  if (c == 0)
    return;

  was_nameless = (c->name == 0);

//  This hack is necessary because xwsh uses
//  COMPOUND_TEXT rather than STRING for its WM_NAME property,
//  and anonymous xwsh windows are annoying.
   
  if (XGetWindowProperty(dpy, c->window, XA_WM_NAME, 0L, 100L,
			 False, AnyPropertyType, &actual_type, &format, &n,
			 &extra, (unsigned char **) &name) == Success &&
      name && *name != '\0' && n != 0) {
    if (c->name != 0)
      XFree(c->name);
    c->name = name;
    // this pair of lines mysteriously locks up the screen,
    // keyboard, and mouse...
    //    c->name = stringCopy(name);
    //    XFree(name);
    if (!was_nameless)
      theWM->drawClientBorder(c, c == current);
  }
	
  if (c->name == 0)
    return;

  class_hints = XAllocClassHint();
  if (class_hints != 0) {
    XGetClassHint(dpy, c->window, class_hints);
    if (class_hints->res_name != 0 && !strcmp(class_hints->res_name, c->name)) {
      XFree(c->name);
      c->name = 0;
      XFree(c->fallback_name);
      c->fallback_name = stringCopy(class_hints->res_name);
    }
    XFree(class_hints->res_name);
    XFree(class_hints->res_class);
    XFree(class_hints);
  }
  return;
}

void
getNormalHints(Client *c) {
  int x, y, w, h;
  long msize;
  float innerMinimumFactor = 1.0;

// We have to be careful. The ICCCM says that the x, y
// and width, height components are not used. But we use them, so
// we need to save and restore them whenever we fill the size struct.
  x = c->size.x;
  y = c->size.y;
  w = c->size.width;
  h = c->size.height;

//  Do the get.
  if (XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0)
    c->size.flags = 0;


// Correct the minimum allowable size of this client to take account of
// the window border.

  if (c->size.flags & PMinSize) {
    c->size.min_width  += 2 * border;
    c->size.min_height += 2 * border + titleHeight();
  } else {
    c->size.flags |= PMinSize;
    c->size.min_width  = ((int) ((2 * border) + (innerMinimumFactor * border)));
    if (c->accepts_focus)
      c->size.min_height = ((int) ((2 * border + titleHeight()) + (innerMinimumFactor * border)));
    else
      c->size.min_height = ((int) ((2 * border) + (innerMinimumFactor * border)));
  }

// Correct the maximum allowable size of this client to take account of
//  the window border.
  if (c->size.flags & PMaxSize) {
    c->size.max_width  += 2 * border;
    c->size.max_height += 2 * border + titleHeight();
  }

//  Ensure that the base width & height and the width & height increments
//  are set correctly so that we do not have problems resizing
  if (!(c->size.flags & PBaseSize))
    c->size.base_width = c->size.base_height = 0;

  if (!(c->size.flags & PResizeInc))
    c->size.width_inc = c->size.height_inc = 1;

//  If the client gives identical minimum and maximum sizes, we do not
//  want the user to resize in that direction.
  if (c->size.min_width == c->size.max_width)
    c->size.width_inc = 0;

  if (c->size.min_height == c->size.max_height)
    c->size.height_inc = 0;

//  Restore the window-manager bits.
  c->size.x = x;
  c->size.y = y;
  c->size.width = w;
  c->size.height = h;
}

static int
getstate(Window w, int *state) {
  long	*p = 0;

  if (getprop(w, wm_state, wm_state, 2L, (unsigned char **) &p) <= 0)
    return 0;

  *state = (int) *p;
  XFree(p);
  return 1;
}


//-------------------------------------------
// class related stuff
//-------------------------------------------

// manage takes a new client (see scanWindowTree)
void
WindowManager::manageClient(Client * c, bool preexisting )  {
  int dohide;
  int state;
  int th = titleHeightc(c);
  XWMHints * hints;
  XWindowAttributes current_attr;
  XSetWindowAttributes attr;

  // For WM_PROTOCOLS handling. 
  Atom	*protocols;
  int	n;
  int	p;

  // Get the hints, window name, and normal hints (see ICCCM
  // section 4.1.2.3).
  hints = XGetWMHints(dpy, c->window);

  getWindowName(c);
  getNormalHints(c);

//  Get the colormaps associated with this window. Get the window
//  attribute colormap first, then look to see if the
//  WM_COLORMAP_WINDOWS property has been used to specify windows
//  needing colormaps that differ from the top-level colormap.
//  (See ICCCM section 4.1.8.)
  XGetWindowAttributes(dpy, c->window, &current_attr);
  c->cmap = current_attr.colormap;

  getColormaps(c);


//  Scan the list of atoms on WM_PROTOCOLS to see which of the
//  protocols that we understand the client is prepared to
//  participate in. (See ICCCM section 4.1.2.7.)
  if (XGetWMProtocols(dpy, c->window, &protocols, &n) != 0) {
    for (p = 0; p < n; p++) {
      if (protocols[p] == wm_delete)
	c->proto |= Pdelete;
      else if (protocols[p] == wm_take_focus)
	c->proto |= Ptakefocus;
    }

    XFree(protocols);
  }

  // Get the WM_TRANSIENT_FOR property (see ICCCM section 4.1.2.6). 
  getTransientFor(c);

//  Work out details for the Client structure from the hints. 
  if (hints && (hints->flags & InputHint))
    c->accepts_focus = hints->input;

  if (!getstate(c->window, &state))
    state = hints ? hints->initial_state : NormalState;
  dohide = (state == IconicState);


// Sort out window position.
  {
    Window root_window;
    int x, y;
    unsigned int w, h;
    unsigned int border_width, depth;

    XGetGeometry(dpy, c->window, &root_window, &x, &y, &w, &h,
		 &border_width, &depth);

//  Do the size first. Note that
//  "The size specifiers refer to the width and height of the
//  client excluding borders" -- ICCCM 4.1.2.3.
    c->size.width  = w + 2 * border;
    c->size.height = h + 2 * border;

//  OpenGL programs often appear smaller than their
//  minimum sizes, which they do not handle well.
    if (c->size.width < c->size.min_width)
      c->size.width = c->size.min_width;
    if (c->size.height < c->size.min_height)
      c->size.height = c->size.min_height;


    //  Do the position next. 
    // assign to the current workspace
    c->workSpace = theWM->workSpace;


//  If we have a user-specified position for a top-level window,
//  or a program-specified position for a dialogue box, we'll
//  take it.

    if (c->trans != None && c->size.flags & PPosition) {
      // It is  a "dialogue box". Trust it. 
      c->size.x = x;
      c->size.y = y;
    } else if (c->size.flags & USPosition) {
      // Use the specified window position.
      c->size.x = x;
      c->size.y = y;


//  We need to be careful of the right-hand edge and
//  bottom. We can use the window gravity (if specified)
//  to handle this. (See section 4.1.2.3 of the ICCCM.)
      apply_gravity(c);
    } else 
      { // No position was specified: use the auto-placement heuristics.
	if( ! preexisting ) theWM->autoPlaceCoords(c);
      } 
  }

  // absolutely ensure the top-left corner is on the screen.
  c->size.x = imax(0 , c->size.x);
  c->size.x = imin(c->size.x, display_width - 2 * border);
  c->size.y = imax(th , c->size.y);
  c->size.y = imin(c->size.y, display_height - 2 * border);

  if (hints)
    XFree(hints);

  // all the reparenting stuff.

  // I could ignore the shaped windows, but that makes
  // it impossible to move/resize/hide/kill them. So
  // I go ahead and do it, but it is hard to get hold of
  // shaped clients like xeyes or oclock.

//   if (!isshaped(c->window)) 
  if (1)
    {
    c->parent = XCreateSimpleWindow(dpy, root, c->size.x, c->size.y,
				    c->size.width, c->size.height, 1, 
				    theWM->parentWindowColor.pixel, theWM->inactiveBorderColor.pixel);

    attr.event_mask = ExposureMask | EnterWindowMask | ButtonMask |
      SubstructureRedirectMask | SubstructureNotifyMask;
    attr.cursor = frame_cursor;
    XChangeWindowAttributes(dpy, c->parent, CWCursor | CWEventMask,
			    &attr);

    XResizeWindow(dpy, c->window, c->size.width - 2 * border,
		  c->size.height - 2 * border);
  }

  //  X11 doe no't let us change border width. It's a window
  // attribute, but it is somehow different.
  XSetWindowBorderWidth(dpy, c->window, 0);

  attr.event_mask = ColormapChangeMask | EnterWindowMask | //LeaveWindowMask |
    PropertyChangeMask;
  attr.win_gravity = StaticGravity;
  attr.do_not_propagate_mask =  ButtonMask;
  XChangeWindowAttributes(dpy, c->window, CWEventMask | CWWinGravity |
			  CWDontPropagate, &attr);

  XReparentWindow(dpy, c->window, c->parent, border, border);

  setshape(c);

  XAddToSaveSet(dpy, c->window);
  if (dohide)
    hide(c);
  else {
    // Map the new window in the relevant state.
    c->hidden = False;
    XMapWindow(dpy, c->parent);
    XMapWindow(dpy, c->window);
    c->isMapped = 1;
    setactive(c, 0, 0L);
    setClientState(c, NormalState);
  }

  if (current != c)
    cmapfocus(current);

  return;
}

// -------------------------------------------
// end of manage.cc
// -------------------------------------------