summaryrefslogtreecommitdiff
path: root/src/manage.c
blob: 073235fa01800fecaf0cc8d5ca9557479b34365b (plain)
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/* Manage windows, such as Mapping them and making sure the proper key
 * Grabs have been put in place.
 *
 * Copyright (C) 2000, 2001, 2002, 2003, 2004 Shawn Betts <sabetts@vcn.bc.ca>
 *
 * This file is part of ratpoison.
 *
 * ratpoison 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, or (at your option)
 * any later version.
 *
 * ratpoison 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 */

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

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

#include "ratpoison.h"

static char **unmanaged_window_list = NULL;
static int num_unmanaged_windows = 0;

void
clear_unmanaged_list (void)
{
  if (unmanaged_window_list)
    {
      int i;

      for (i = 0; i < num_unmanaged_windows; i++)
        free(unmanaged_window_list[i]);

      free(unmanaged_window_list);

      unmanaged_window_list = NULL;
    }
  num_unmanaged_windows = 0;
}

char *
list_unmanaged_windows (void)
{
  char *tmp = NULL;

  if (unmanaged_window_list)
    {
      struct sbuf *buf;
      int i;

      buf = sbuf_new (0);

      for (i = 0; i < num_unmanaged_windows; i++)
        {
          sbuf_concat (buf, unmanaged_window_list[i]);
          sbuf_concat (buf, "\n");
        }
      sbuf_chop (buf);
      tmp = sbuf_free_struct (buf);
    }
  return tmp;
}

void
add_unmanaged_window (char *name)
{
  char **tmp;

  if (!name) return;

  tmp = xmalloc((num_unmanaged_windows + 1) * sizeof(char *));

  if (unmanaged_window_list)
    {
      memcpy(tmp, unmanaged_window_list, num_unmanaged_windows * sizeof(char *));
      free(unmanaged_window_list);
    }

  tmp[num_unmanaged_windows] = xstrdup(name);
  num_unmanaged_windows++;

  unmanaged_window_list = tmp;
}

void
grab_top_level_keys (Window w)
{
#ifdef HIDE_MOUSE
  XGrabKey(dpy, AnyKey, AnyModifier, w, True,
           GrabModeAsync, GrabModeAsync);
#else
  rp_keymap *map = find_keymap (defaults.top_kmap);
  int i;

  if (map == NULL)
    {
      PRINT_ERROR (("Unable to find %s level keymap\n", defaults.top_kmap));
      return;
    }

  PRINT_DEBUG(("grabbing top level key\n"));
  for (i=0; i<map->actions_last; i++)
    {
      PRINT_DEBUG(("%d\n", i));
      grab_key (map->actions[i].key, map->actions[i].state, w);
    }
#endif
}

void
ungrab_top_level_keys (Window w)
{
  XUngrabKey(dpy, AnyKey, AnyModifier, w);
}

void
ungrab_keys_all_wins (void)
{
  rp_window *cur;

  /* Remove the grab on the current prefix key */
  list_for_each_entry (cur, &rp_mapped_window, node)
    {
      ungrab_top_level_keys (cur->w);
    }
}

void
grab_keys_all_wins (void)
{
  rp_window *cur;

  /* Remove the grab on the current prefix key */
  list_for_each_entry (cur, &rp_mapped_window, node)
    {
      grab_top_level_keys (cur->w);
    }
}

void
update_normal_hints (rp_window *win)
{
  long supplied;

  XGetWMNormalHints (dpy, win->w, win->hints, &supplied);

  /* Print debugging output for window hints. */
#ifdef DEBUG
  if (win->hints->flags & PMinSize)
    PRINT_DEBUG (("minx: %d miny: %d\n", win->hints->min_width, win->hints->min_height));

  if (win->hints->flags & PMaxSize)
    PRINT_DEBUG (("maxx: %d maxy: %d\n", win->hints->max_width, win->hints->max_height));

  if (win->hints->flags & PResizeInc)
    PRINT_DEBUG (("incx: %d incy: %d\n", win->hints->width_inc, win->hints->height_inc));

#endif
}


static char *
get_wmname (Window w)
{
  char *name = NULL;
  XTextProperty text_prop;
  int ret = None, n;
  char** cl;

  /* If current encoding is UTF-8, try to use the window's _NET_WM_NAME ewmh
     property */
  if (defaults.utf8_locale)
    {
      Atom type = None;
      unsigned long nitems, bytes_after;
      int format;
      unsigned char *val = NULL;

      ret = XGetWindowProperty (dpy, w, _net_wm_name, 0, 40, False,
				xa_utf8_string, &type, &format, &nitems,
				&bytes_after, &val);
      /* We have a valid UTF-8 string */
      if (ret == Success && type == xa_utf8_string
	  && format == 8 && nitems > 0)
	{
	  name = xstrdup ((char *)val);
	  XFree (val);
          PRINT_DEBUG (("Fetching window name using _NET_WM_NAME succeeded\n"));
	  PRINT_DEBUG (("WM_NAME: %s\n", name));
	  return name;
	}
      /* Something went wrong for whatever reason */
      if (ret == Success && val)
	XFree (val);
      PRINT_DEBUG (("Could not fetch window name using _NET_WM_NAME\n"));
    }

  if (XGetWMName (dpy, w, &text_prop) == 0)
    {
      PRINT_DEBUG (("XGetWMName failed\n"));
      return NULL;
    }

  PRINT_DEBUG (("WM_NAME encoding: "));
  if (text_prop.encoding == xa_string)
    PRINT_DEBUG  (("STRING\n"));
  else if (text_prop.encoding == xa_compound_text)
    PRINT_DEBUG (("COMPOUND_TEXT\n"));
  else if (text_prop.encoding == xa_utf8_string)
    PRINT_DEBUG (("UTF8_STRING\n"));
  else
    PRINT_DEBUG (("unknown (%d)\n", (int) text_prop.encoding));

#ifdef X_HAVE_UTF8_STRING
  /* It seems that most applications supporting UTF8_STRING and
     _NET_WM_NAME don't bother making their WM_NAME available as
     UTF8_STRING (but only as either STRING or COMPOUND_TEXT).
     Let's try anyway.  */
  if (defaults.utf8_locale && text_prop.encoding == xa_utf8_string)
    {
      ret = Xutf8TextPropertyToTextList (dpy, &text_prop, &cl, &n);
      PRINT_DEBUG (("Xutf8TextPropertyToTextList: %s\n",
		    ret == Success ? "success" : "error"));
    }
  else
#endif
    {
      /* XmbTextPropertyToTextList should be fine for all cases,
	 even UTF8_STRING encoded WM_NAME */
      ret = XmbTextPropertyToTextList (dpy, &text_prop, &cl, &n);
      PRINT_DEBUG (("XmbTextPropertyToTextList: %s\n",
		    ret == Success ? "success" : "error"));
    }

  if (ret == Success && cl && n > 0)
    {
      name = xstrdup (cl[0]);
      XFreeStringList (cl);
    }
  else if (text_prop.value)
    {
      /* Convertion failed, try to get the raw string */
      name = xstrdup ((char *) text_prop.value);
      XFree (text_prop.value);
    }

  if (name == NULL) {
    PRINT_DEBUG (("I can't get the WMName.\n"));
  } else {
    PRINT_DEBUG (("WM_NAME: '%s'\n", name));
  }

  return name;
}

static XClassHint *
get_class_hints (Window w)
{
  XClassHint *class;

  class = XAllocClassHint();

  if (class == NULL)
    {
      PRINT_ERROR (("Not enough memory for WM_CLASS structure.\n"));
      exit (EXIT_FAILURE);
    }

  XGetClassHint (dpy, w, class);

  return class;
}

/* Reget the WM_NAME property for the window and update its
   name. Return 1 if the name changed. */
int
update_window_name (rp_window *win)
{
  char *newstr;
  int changed = 0;
  XClassHint *class;

  newstr = get_wmname (win->w);
  if (newstr != NULL)
    {
      changed = changed || win->wm_name == NULL || strcmp (newstr, win->wm_name);
      free (win->wm_name);
      win->wm_name = newstr;
    }

  class = get_class_hints (win->w);

  if (class->res_class != NULL
      && (win->res_class == NULL || strcmp (class->res_class, win->res_class)))
    {
      changed = 1;
      free (win->res_class);
      win->res_class = xstrdup(class->res_class);
    }

  if (class->res_name != NULL
      && (win->res_name == NULL || strcmp (class->res_name, win->res_name)))
    {
      changed = 1;
      free (win->res_name);
      win->res_name = xstrdup(class->res_name);
    }

  XFree (class->res_name);
  XFree (class->res_class);
  XFree (class);
  return changed;
}

/* Send an artificial configure event to the window. */
void
send_configure (Window w, int x, int y, int width, int height, int border)
{
  XConfigureEvent ce;

  ce.type = ConfigureNotify;
  ce.event = w;
  ce.window = w;
  ce.x = x;
  ce.y = y;
  ce.width = width;
  ce.height = height;
  ce.border_width = border;
  ce.above = None;
  ce.override_redirect = 0;

  XSendEvent (dpy, w, False, StructureNotifyMask, (XEvent*)&ce);
}

/* This function is used to determine if the window should be treated
   as a transient. */
int
window_is_transient (rp_window *win)
{
  return win->transient
#ifdef ASPECT_WINDOWS_ARE_TRANSIENTS
 || win->hints->flags & PAspect
#endif
#ifdef MAXSIZE_WINDOWS_ARE_TRANSIENTS
|| (win->hints->flags & PMaxSize
    && (win->hints->max_width < win->scr->width
        || win->hints->max_height < win->scr->height))
#endif
    ;
}

static Atom
get_net_wm_window_type (rp_window *win)
{
  Atom type, window_type = None;
  int format;
  unsigned long nitems;
  unsigned long bytes_left;
  unsigned char *data;

  if (win == NULL)
    return None;

  if (XGetWindowProperty (dpy, win->w, _net_wm_window_type, 0, 1L,
                          False, XA_ATOM, &type, &format,
                          &nitems, &bytes_left,
                          &data) == Success && nitems > 0)
    {
      window_type = *(Atom *)data;
      XFree (data);
      PRINT_DEBUG(("hey ya %ld %ld\n", window_type, _net_wm_window_type_dialog));
    }

  return window_type;
}


void
update_window_information (rp_window *win)
{
  XWindowAttributes attr;

  update_window_name (win);

  /* Get the WM Hints */
  update_normal_hints (win);

  /* Get the colormap */
  XGetWindowAttributes (dpy, win->w, &attr);
  win->colormap = attr.colormap;
  win->x = attr.x;
  win->y = attr.y;
  win->width = attr.width;
  win->height = attr.height;
  win->border = attr.border_width;

  /* Transient status */
  win->transient = XGetTransientForHint (dpy, win->w, &win->transient_for);

  if (get_net_wm_window_type(win) == _net_wm_window_type_dialog)
    win->transient = 1;

  update_window_gravity (win);
}

void
unmanage (rp_window *w)
{
  list_del (&w->node);
  groups_del_window (w);

  free_window (w);

#ifdef AUTO_CLOSE
  if (rp_mapped_window.next == &rp_mapped_window
      && rp_mapped_window.prev == &rp_mapped_window)
    {
      /* If the mapped window list is empty then we have run out of
         managed windows, so kill ratpoison. */

      /* FIXME: The unmapped window list may also have to be checked
         in the case that the only mapped window in unmapped and
         shortly after another window is mapped most likely by the
         same app. */

      kill_signalled = 1;
    }
#endif
}

/* When starting up scan existing windows and start managing them. */
void
scanwins (void)
{
  rp_window *win;
  XWindowAttributes attr;
  unsigned int i, nwins;
  Window dw1, dw2, *wins;

  XQueryTree(dpy, rp_glob_screen.root, &dw1, &dw2, &wins, &nwins);
  PRINT_DEBUG (("windows: %d\n", nwins));

  for (i = 0; i < nwins; i++)
    {
      rp_screen *screen;

      XGetWindowAttributes(dpy, wins[i], &attr);
      if (is_rp_window (wins[i])
          || attr.override_redirect == True
          || unmanaged_window (wins[i])) continue;


      screen = find_screen_by_attr (attr);
      if (!screen)
          list_first (screen, &rp_screens, node);

      win = add_to_window_list (screen, wins[i]);

      PRINT_DEBUG (("map_state: %s\n",
                    attr.map_state == IsViewable ? "IsViewable":
                    attr.map_state == IsUnviewable ? "IsUnviewable" : "IsUnmapped"));
      PRINT_DEBUG (("state: %s\n",
                    get_state(win) == IconicState ? "Iconic":
                    get_state(win) == NormalState ? "Normal" : "Other"));

      /* Collect mapped and iconized windows. */
      if (attr.map_state == IsViewable
          || (attr.map_state == IsUnmapped
              && get_state (win) == IconicState))
        map_window (win);
    }

  XFree(wins);
}

int
unmanaged_window (Window w)
{
  char *wname;
  int i;

  if (!unmanaged_window_list)
    return 0;

  wname = get_wmname (w);
  if (!wname)
    return 0;

  for (i = 0; i < num_unmanaged_windows; i++)
    {
      if (!strcmp (unmanaged_window_list[i], wname))
        {
          free (wname);
          return 1;
        }
    }

  free (wname);
  return 0;
}

/* Set the state of the window. */
void
set_state (rp_window *win, int state)
{
  long data[2];

  win->state = state;

  data[0] = (long)win->state;
  data[1] = (long)None;

  XChangeProperty (dpy, win->w, wm_state, wm_state, 32,
                   PropModeReplace, (unsigned char *)data, 2);
}

/* Get the WM state of the window. */
long
get_state (rp_window *win)
{
  long state = WithdrawnState;
  Atom type;
  int format;
  unsigned long nitems;
  unsigned long bytes_left;
  unsigned char *data;

  if (win == NULL)
    return state;

  if (XGetWindowProperty (dpy, win->w, wm_state, 0L, 2L,
                          False, wm_state, &type, &format,
                          &nitems, &bytes_left,
                          &data) == Success && nitems > 0)
    {
      state = *(long *)data;
      XFree (data);
    }

  return state;
}

static void
move_window (rp_window *win)
{
  rp_frame *frame;

  if (win->frame_number == EMPTY)
    return;

  frame = win_get_frame (win);

  /* X coord. */
  switch (win->gravity)
    {
    case NorthWestGravity:
    case WestGravity:
    case SouthWestGravity:
      win->x = frame->x;
      break;
    case NorthGravity:
    case CenterGravity:
    case SouthGravity:
      win->x = frame->x + (frame->width - win->border * 2) / 2 - win->width / 2;
      break;
    case NorthEastGravity:
    case EastGravity:
    case SouthEastGravity:
      win->x = frame->x + frame->width - win->width - win->border;
      break;
    }

  /* Y coord. */
  switch (win->gravity)
    {
    case NorthEastGravity:
    case NorthGravity:
    case NorthWestGravity:
      win->y = frame->y;
      break;
    case EastGravity:
    case CenterGravity:
    case WestGravity:
      win->y = frame->y + (frame->height - win->border * 2) / 2 - win->height / 2;
      break;
    case SouthEastGravity:
    case SouthGravity:
    case SouthWestGravity:
      win->y = frame->y + frame->height - win->height - win->border;
      break;
    }
}

/* Set a transient window's x,y,width,height fields to maximize the
   window. */
static void
maximize_transient (rp_window *win)
{
  rp_frame *frame;
  int maxx, maxy;

  frame = win_get_frame (win);

  /* We can't maximize a window if it has no frame. */
  if (frame == NULL)
    return;

  /* Set the window's border */
  win->border = defaults.window_border_width;

  /* Always use the window's current width and height for
     transients. */
  maxx = win->width;
  maxy = win->height;

  /* Fit the window inside its frame (if it has one) */
  if (frame)
    {
      PRINT_DEBUG (("frame width=%d height=%d\n",
                   frame->width, frame->height));

      if (maxx + win->border * 2 > frame->width) maxx = frame->width - win->border * 2;
      if (maxy + win->border * 2 > frame->height) maxy = frame->height - win->border * 2;
    }

  /* Make sure we maximize to the nearest Resize Increment specified
     by the window */
  if (win->hints->flags & PResizeInc)
    {
      int amount;
      int delta;

      /* Avoid a divide by zero if width/height_inc is 0. */
      if (win->hints->width_inc)
        {
          amount = maxx - win->width;
          delta = amount % win->hints->width_inc;
          amount -= delta;
          if (amount < 0 && delta) amount -= win->hints->width_inc;
          maxx = amount + win->width;
        }

      if (win->hints->height_inc)
        {
          amount = maxy - win->height;
          delta = amount % win->hints->height_inc;
          amount -= delta;
          if (amount < 0 && delta) amount -= win->hints->height_inc;
          maxy = amount + win->height;
        }
    }

  PRINT_DEBUG (("maxsize: %d %d\n", maxx, maxy));

  win->width = maxx;
  win->height = maxy;
}

/* set a good standard window's x,y,width,height fields to maximize
   the window. */
static void
maximize_normal (rp_window *win)
{
  rp_frame *frame;
  int maxx, maxy;

  frame = win_get_frame (win);

  /* We can't maximize a window if it has no frame. */
  if (frame == NULL)
    return;

  /* Set the window's border */
  if (defaults.only_border == 0 && num_frames(win->scr) <= 1){
    win->border = 0;
  } else {
    win->border = defaults.window_border_width;
  }


  /* Honour the window's maximum size */
  if (win->hints->flags & PMaxSize)
    {
      maxx = win->hints->max_width;
      maxy = win->hints->max_height;
    }
  else
    {
      maxx = frame->width - win->border * 2;
      maxy = frame->height - win->border * 2;
    }

  /* Honour the window's aspect ratio. */
  PRINT_DEBUG (("aspect: %ld\n", win->hints->flags & PAspect));
  if (win->hints->flags & PAspect)
    {
      float ratio = (float)maxx / maxy;
      float min_ratio = (float)win->hints->min_aspect.x / win->hints->min_aspect.y;
      float max_ratio = (float)win->hints->max_aspect.x / win->hints->max_aspect.y;
      PRINT_DEBUG (("ratio=%f min_ratio=%f max_ratio=%f\n",
                    ratio,min_ratio,max_ratio));
      if (ratio < min_ratio)
        {
          maxy = (int) (maxx / min_ratio);
        }
      else if (ratio > max_ratio)
        {
          maxx = (int) (maxy * max_ratio);
        }
    }

  /* Fit the window inside its frame (if it has one) */
  if (frame)
    {
      PRINT_DEBUG (("frame width=%d height=%d\n",
                   frame->width, frame->height));

      if (maxx > frame->width) maxx = frame->width - win->border * 2;
      if (maxy > frame->height) maxy = frame->height - win->border * 2;
    }

  /* Make sure we maximize to the nearest Resize Increment specified
     by the window */
  if (win->hints->flags & PResizeInc)
    {
      int amount;
      int delta;

      if (win->hints->width_inc)
        {
          amount = maxx - win->width;
          delta = amount % win->hints->width_inc;
          if (amount < 0 && delta) amount -= win->hints->width_inc;
          amount -= delta;
          maxx = amount + win->width;
        }

      if (win->hints->height_inc)
        {
          amount = maxy - win->height;
          delta = amount % win->hints->height_inc;
          if (amount < 0 && delta) amount -= win->hints->height_inc;
          amount -= delta;
          maxy = amount + win->height;
        }
    }

  PRINT_DEBUG (("maxsize: %d %d\n", maxx, maxy));

  win->width = maxx;
  win->height = maxy;
}

/* Maximize the current window if data = 0, otherwise assume it is a
   pointer to a window that should be maximized */
void
maximize (rp_window *win)
{
  if (!win) win = current_window();
  if (!win) return;

  /* Handle maximizing transient windows differently. */
  if (win->transient)
    maximize_transient (win);
  else
    maximize_normal (win);

  /* Reposition the window. */
  move_window (win);

  PRINT_DEBUG (("Resizing window '%s' to x:%d y:%d w:%d h:%d\n", window_name (win),
               win->x, win->y, win->width, win->height));


  /* Actually do the maximizing. */
  XMoveResizeWindow (dpy, win->w, win->scr->left + win->x, win->scr->top + win->y, win->width, win->height);
  XSetWindowBorderWidth (dpy, win->w, win->border);

  XSync (dpy, False);
}

/* Maximize the current window but don't treat transient windows
   differently. */
void
force_maximize (rp_window *win)
{
  if (!win) win = current_window();
  if (!win) return;

  maximize_normal(win);

  /* Reposition the window. */
  move_window (win);

  /* This little dance is to force a maximize event. If the window is
     already "maximized" X11 will optimize away the event since to
     geometry changes were made. This initial resize solves the
     problem. */
  if (win->hints->flags & PResizeInc)
    {
      XMoveResizeWindow (dpy, win->w, win->scr->left + win->x, win->scr->top + win->y,
                         win->width + win->hints->width_inc,
                         win->height + win->hints->height_inc);
    }
  else
    {
      XResizeWindow (dpy, win->w, win->width + 1, win->height + 1);
    }

  XSync (dpy, False);

  /* Resize the window to its proper maximum size. */
  XMoveResizeWindow (dpy, win->w, win->scr->left + win->x, win->scr->top + win->y, win->width, win->height);
  XSetWindowBorderWidth (dpy, win->w, win->border);

  XSync (dpy, False);
}

/* map the unmapped window win */
void
map_window (rp_window *win)
{
  PRINT_DEBUG (("Mapping the unmapped window %s\n", window_name (win)));

  /* Fill in the necessary data about the window */
  update_window_information (win);
  win->number = numset_request (rp_window_numset);
  grab_top_level_keys (win->w);

  /* Put win in the mapped window list */
  list_del (&win->node);
  insert_into_list (win, &rp_mapped_window);

  /* Update all groups. */
  groups_map_window (win);

  /* The window has never been accessed since it was brought back from
     the Withdrawn state. */
  win->last_access = 0;

  /* It is now considered iconic and set_active_window can handle the rest. */
  set_state (win, IconicState);

  /* Depending on the rudeness level, actually map the window. */
  if ((rp_honour_transient_map && win->transient)
      || (rp_honour_normal_map && !win->transient))
    set_active_window (win);
  else
    show_rudeness_msg (win, 0);

  hook_run (&rp_new_window_hook);
}

void
hide_window (rp_window *win)
{
  if (win == NULL) return;

  /* An unmapped window is not inside a frame. */
  win->frame_number = EMPTY;

  /* Ignore the unmap_notify event. */
  XSelectInput(dpy, win->w, WIN_EVENTS&~(StructureNotifyMask));
  XUnmapWindow (dpy, win->w);
  XSelectInput (dpy, win->w, WIN_EVENTS);
  /* Ensure that the window doesn't have the focused border
     color. This is needed by remove_frame and possibly others. */
  XSetWindowBorder (dpy, win->w, rp_glob_screen.bw_color);
  set_state (win, IconicState);
}

void
unhide_window (rp_window *win)
{
  if (win == NULL) return;

  /* Always raise the window. */
  XRaiseWindow (dpy, win->w);

  if (win->state != IconicState) return;

  XMapWindow (dpy, win->w);
  set_state (win, NormalState);
}

void
unhide_all_windows (void)
{
  struct list_head *tmp, *iter;
  rp_window *win;

  list_for_each_safe_entry (win, iter, tmp, &rp_mapped_window, node)
    unhide_window (win);
}

/* same as unhide_window except that it makes sure the window is mapped
   on the bottom of the window stack. */
void
unhide_window_below (rp_window *win)
{
  if (win == NULL) return;

  /* Always lower the window, but if its not iconic we don't need to
     map it since it already is mapped. */
  XLowerWindow (dpy, win->w);

  if (win->state != IconicState) return;

  XMapWindow (dpy, win->w);
  set_state (win, NormalState);
}

void
withdraw_window (rp_window *win)
{
  if (win == NULL) return;

  PRINT_DEBUG (("withdraw_window on '%s'\n", window_name (win)));

  /* Give back the window number. the window will get another one,
     if it is remapped. */
  if (win->number == -1)
    PRINT_ERROR(("Attempting to withdraw '%s' with number -1!\n", window_name(win)));

  numset_release (rp_window_numset, win->number);
  win->number = -1;

  list_move_tail(&win->node, &rp_unmapped_window);

  /* Update the groups. */
  groups_unmap_window (win);

  ignore_badwindow++;

  XRemoveFromSaveSet (dpy, win->w);
  set_state (win, WithdrawnState);
  XSync (dpy, False);

  ignore_badwindow--;

  /* Call our hook */
  hook_run (&rp_delete_window_hook);
}

/* Hide all other mapped windows except for win in win's frame. */
void
hide_others (rp_window *win)
{
  rp_frame *frame;
  rp_window *cur;

  if (win == NULL) return;
  frame = find_windows_frame (win);
  if (frame == NULL) return;

  list_for_each_entry (cur, &rp_mapped_window, node)
    {
      if (find_windows_frame (cur)
          || cur->state != NormalState
          || cur->frame_number != frame->number)
        continue;

      hide_window (cur);
    }
}

/* Hide any window displayed on the given screen */
void
hide_screen_windows (rp_screen *s)
{
    rp_frame *cur_frame;
    rp_window *cur_win;

  list_for_each_entry (cur_frame, &s->frames, node)
    {
      cur_win = find_window_number (cur_frame->win_number);
      hide_window (cur_win);
    }
}