File: functions.c

package info (click to toggle)
sawfish 1:1.5.3-2.4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,160 kB
  • ctags: 1,428
  • sloc: lisp: 23,542; ansic: 15,824; sh: 10,268; makefile: 615; perl: 19
file content (1410 lines) | stat: -rw-r--r-- 37,203 bytes parent folder | download | duplicates (2)
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
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
/* functions.c -- useful window manager Lisp functions
   $Id$

   Copyright (C) 1999 John Harper <john@dcs.warwick.ac.uk>

   This file is part of sawfish.

   sawfish 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.

   sawfish 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 sawfish; see the file COPYING.   If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

/* Define this to fake two side-by-side Xinerama style heads */
#undef TEST_XINERAMA

/* AIX requires this to be the first thing in the file.  */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef __GNUC__
# if HAVE_ALLOCA_H
#  include <alloca.h>
# else
#  ifdef _AIX
 #pragma alloca
#  else
#   ifndef alloca /* predefined by HP cc +Olibcalls */
   char *alloca ();
#   endif
#  endif
# endif
#endif
   
#include "sawfish.h"
#include <string.h>
#include <X11/Xatom.h>

/* Number of outstanding server grabs made; only when this is zero is
   the server ungrabbed. */
static int server_grabs;

static int xinerama_heads;

#ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
# include <X11/extensions/Xinerama.h>
  static XineramaScreenInfo *xinerama_head_info;
# ifdef TEST_XINERAMA
   static XineramaScreenInfo debug_heads[2] = {
     { 0, 0, 0, 512, 768 },
     { 0, 512, 0, 512, 768 }
   };
   static int debug_nheads = 2;
# else
   static int xinerama_event_base, xinerama_error_base;
# endif
#endif

DEFSYM(root, "root");
DEFSYM(after_restacking_hook, "after-restacking-hook");
DEFSYM(position, "position");
DEFSYM(spacing, "spacing");
DEFSYM(window, "window");
DEFSYM(head, "head");

DEFUN("restack-windows", Frestack_windows, Srestack_windows,
      (repv list), rep_Subr1) /*
::doc:sawfish.wm.windows.subrs#restack-windows::
restack-windows LIST

Restack all windows in the list of windows LIST in the order they occur
in the list (from top to bottom). The stacking order of any unspecified
windows isn't affected.
::end:: */
{
    repv ptr;
    Lisp_Window *pred;

    rep_DECLARE1(list, rep_LISTP);
    for (ptr = list; rep_CONSP (ptr); ptr = rep_CDR (ptr))
    {
	if (!WINDOWP (rep_CAR (ptr)))
	    return rep_signal_arg_error (list, 1);
    }

    if (list == Qnil)
	return Qt;

    ptr = list;
    pred = 0;

    while (rep_CONSP (ptr))
    {
	Lisp_Window *this = VWIN (rep_CAR (ptr));

	if (!WINDOW_IS_GONE_P (this))
	{
	    if (pred != 0 && !WINDOW_IS_GONE_P (pred))
	    {
		remove_from_stacking_list (this);
		insert_in_stacking_list_below (this, pred);

		/* This works because it tries to stack relative to
		   the window above THIS first; which we just set */
		restack_window (this);
	    }
	    pred = this;
	}

	ptr = rep_CDR (ptr);

	rep_TEST_INT;
	if (rep_INTERRUPTP)
	    return rep_NULL;
    }

    Fcall_hook (Qafter_restacking_hook, Qnil, Qnil);
    return Qt;
}

DEFUN("x-raise-window", Fx_raise_window, Sx_raise_window,
      (repv win, repv above), rep_Subr2) /*
::doc:sawfish.wm.windows.subrs#x-raise-window::
x-raise-window WINDOW [ABOVE]

Raise WINDOW so that it is above window ABOVE. If ABOVE is undefined,
raise WINDOW to the top of the stacking order.
::end:: */
{
    rep_DECLARE1 (win, WINDOWP);

    if (!WINDOW_IS_GONE_P (VWIN (win)))
    {
	if (WINDOWP (above))
	{
	    if (!WINDOW_IS_GONE_P (VWIN (above)))
	    {
		remove_from_stacking_list (VWIN (win));
		insert_in_stacking_list_above (VWIN (win), VWIN (above));
	    }
	}
	else
	{
	    remove_from_stacking_list (VWIN (win));
	    insert_in_stacking_list_above_all (VWIN (win));
	}
	restack_window (VWIN (win));
	Fcall_hook (Qafter_restacking_hook, Qnil, Qnil);
    }
    return win;
}

DEFUN("x-lower-window", Fx_lower_window, Sx_lower_window,
      (repv win, repv below), rep_Subr2) /*
::doc:sawfish.wm.windows.subrs#x-lower-window::
x-lower-window WINDOW [BELOW]

Lower WINDOW so that it is below window BELOW. If BELOW is undefined,
lower WINDOW to the bottom of the stacking order.
::end:: */
{
    rep_DECLARE1 (win, WINDOWP);

    if (!WINDOW_IS_GONE_P (VWIN (win)))
    {
	if (WINDOWP (below))
	{
	    if (!WINDOW_IS_GONE_P (VWIN (below)))
	    {
		remove_from_stacking_list (VWIN (win));
		insert_in_stacking_list_below (VWIN (win), VWIN (below));
	    }
	}
	else
	{
	    remove_from_stacking_list (VWIN (win));
	    insert_in_stacking_list_below_all (VWIN (win));
	}
	restack_window (VWIN (win));
	Fcall_hook (Qafter_restacking_hook, Qnil, Qnil);
    }
    return win;
}

DEFUN("x-kill-client", Fx_kill_client, Sx_kill_client,
      (repv win), rep_Subr1)
{
    Window w = x_win_from_arg (win);
    if (w == 0)
	return WINDOWP(win) ? Qnil : rep_signal_arg_error (win, 1);
    XKillClient (dpy, w);
    return win;
}

DEFUN_INT("destroy-window", Fdestroy_window, Sdestroy_window, (repv win), rep_Subr1, "%W") /*
::doc:sawfish.wm.windows.subrs#destroy-window::
destroy-window WINDOW

Destroy WINDOW with out giving the owning application any warning.

WINDOW may be a window object or a numeric window id.
::end:: */
{
    if (WINDOWP(win))
	XDestroyWindow (dpy, VWIN(win)->id);
    else if (rep_INTEGERP(win))
	XDestroyWindow (dpy, rep_get_long_uint (win));
    else
	return rep_signal_arg_error (win, 1);
    return win;
}

DEFUN("warp-cursor", Fwarp_cursor, Swarp_cursor, (repv x, repv y), rep_Subr2) /*
::doc:sawfish.wm.misc#warp-cursor::
warp-cursor X Y

Move the mouse pointer to position (X, Y) relative to the origin of the
root window.
::end:: */
{
    rep_DECLARE1(x, rep_INTP);
    rep_DECLARE2(y, rep_INTP);
    if (rep_INT(x) >= 0 && rep_INT(x) < screen_width
	&& rep_INT(y) >= 0 && rep_INT(y) < screen_height)
    {
	XWarpPointer (dpy, None, root_window,
		      0, 0, 0, 0, rep_INT(x), rep_INT(y));
	invalidate_cached_mouse_position ();
	return Qt;
    }
    else
	return Qnil;
}

DEFUN("move-window-to", Fmove_window_to, Smove_window_to,
      (repv win, repv x, repv y), rep_Subr3) /*
::doc:sawfish.wm.windows.subrs#move-window-to::
move-window-to WINDOW X Y

Move the top-left corner of window object WINDOW to (X, Y).
::end:: */
{
    rep_DECLARE1(win, WINDOWP);
    rep_DECLARE2(x, rep_INTP);
    rep_DECLARE3(y, rep_INTP);
    if (VWIN(win)->attr.x != rep_INT(x) || VWIN(win)->attr.y != rep_INT(y))
    {
	VWIN(win)->attr.x = rep_INT(x);
	VWIN(win)->attr.y = rep_INT(y);
	XMoveWindow (dpy,
		     VWIN(win)->reparented ? VWIN(win)->frame : VWIN(win)->id,
		     VWIN(win)->attr.x, VWIN(win)->attr.y);
	send_synthetic_configure (VWIN(win));
	Fcall_window_hook (Qwindow_moved_hook, win, Qnil, Qnil);
    }
    return win;
}

DEFUN("resize-window-to", Fresize_window_to, Sresize_window_to,
      (repv win, repv width, repv height), rep_Subr3) /*
::doc:sawfish.wm.windows.subrs#resize-window-to::
resize-window-to WINDOW WIDTH HEIGHT

Set the dimensions of window object WINDOW to (WIDTH, HEIGHT).
::end:: */
{
    rep_DECLARE1(win, WINDOWP);
    rep_DECLARE2(width, rep_INTP);
    rep_DECLARE3(height, rep_INTP);
    VWIN(win)->attr.width = rep_INT(width);
    VWIN(win)->attr.height = rep_INT(height);
    fix_window_size (VWIN(win));
    VWIN (win)->pending_configure = 0;
    Fcall_window_hook (Qwindow_resized_hook, win, Qnil, Qnil);
    return win;
}

DEFUN("move-resize-window-to", Fmove_resize_window_to, Smove_resize_window_to,
      (repv win, repv x, repv y, repv width, repv height), rep_Subr5) /*
::doc:sawfish.wm.windows.subrs#move-resize-window-to::
move-resize-window-to WINDOW X Y WIDTH HEIGHT

Reconfigure the geometry of window object WINDOW as specified.
::end:: */
{
    bool resized, moved;
    rep_DECLARE1(win, WINDOWP);
    rep_DECLARE2(x, rep_INTP);
    rep_DECLARE3(y, rep_INTP);
    rep_DECLARE4(width, rep_INTP);
    rep_DECLARE5(height, rep_INTP);
    moved = (VWIN(win)->attr.x != rep_INT(x)
	     || VWIN(win)->attr.y != rep_INT(y));
    resized = (VWIN(win)->attr.width != rep_INT(width)
	       || VWIN(win)->attr.height != rep_INT(height));
    VWIN(win)->attr.x = rep_INT(x);
    VWIN(win)->attr.y = rep_INT(y);
    VWIN(win)->attr.width = rep_INT(width);
    VWIN(win)->attr.height = rep_INT(height);
    if (resized)
    {
	fix_window_size (VWIN(win));
	VWIN (win)->pending_configure = 0;
    }
    if (moved && !resized)
    {
	XMoveWindow (dpy, VWIN(win)->reparented ? VWIN(win)->frame
		     : VWIN(win)->id, VWIN(win)->attr.x, VWIN(win)->attr.y);
	send_synthetic_configure (VWIN(win));
    }
    if (moved)
	Fcall_window_hook (Qwindow_moved_hook, win, Qnil, Qnil);
    if (resized)
	Fcall_window_hook (Qwindow_resized_hook, win, Qnil, Qnil);
    return win;
}

DEFUN("grab-server", Fgrab_server, Sgrab_server, (void), rep_Subr0) /*
::doc:sawfish.wm.misc#grab-server::
grab-server

Prevent any other clients from accessing the X server. See `ungrab-server'.
::end:: */
{
    if (server_grabs++ == 0)
    {
	XGrabServer (dpy);
	XSync (dpy, False);
	rep_mark_input_pending (ConnectionNumber (dpy));
    }
    return Qt;
}

DEFUN("ungrab-server", Fungrab_server, Sungrab_server, (void), rep_Subr0) /*
::doc:sawfish.wm.misc#ungrab-server::
ungrab-server

After a call to `grab-server' this will allow other clients to access
the X server again.

Note that calls to `grab-server' and `ungrab-server' _nest_.
::end:: */
{
    if (--server_grabs == 0)
    {
	XUngrabServer (dpy);
	XFlush (dpy);
    }
    return Qt;
}

DEFUN("server-grabbed-p", Fserver_grabbed_p,
      Sserver_grabbed_p, (void), rep_Subr0) /*
::doc:sawfish.wm.misc#server-grabbed-p::
server-grabbed-p

Return t if the server is currently grabbed.
::end:: */
{
    return (server_grabs > 0) ? Qt : Qnil;
}

/* Call this when the server may have been ungrabbed prematurely (imlib
   lossage) */
void
regrab_server (void)
{
    if (server_grabs > 0)
    {
	server_grabs--;
	Fgrab_server ();
    }
}

DEFUN("grab-pointer", Fgrab_pointer, Sgrab_pointer,
      (repv win, repv cursor, repv ptr_sync, repv kbd_sync, repv confine_to),
      rep_Subr5) /*
::doc:sawfish.wm.events#grab-pointer::
grab-pointer [WINDOW] [CURSOR] [PTR-SYNC] [KBD-SYNC] [CONFINE-TO]

Grab the pointer and direct all pointer events to window object
WINDOW. If CURSOR is defined and a cursor object, display this whilst
the pointer is grabbed.

If PTR-SYNC or KBD-SYNC is non-nil the pointer or the keyboard will be
frozen, i.e., the device will not produce events until either the grab
is released or events are re-enabled using allow-events.

CONFINE-TO, if non-nil, is a visible window to confine the pointer to.

If WINDOW is a window object corresponding to a visible window the
grab will be made on its frame.  If WINDOW is an integer, it specifies the
window id of the grab window.  Otherwise the grab will be made on the root
window.  CONFINE-TO is interpreted similarly except that the default
is not to confine the pointer.  If the window id of a non-viewable window
was specified for either WINDOW of CONFINE-TO the grab will be made on the
root window without confining the pointer.

Returns non-nil if the grab succeeded.
::end:: */
{
    Window g_win, c_win;
    int ret;

    if (WINDOWP(win) && VWIN(win)->visible)
	g_win = VWIN(win)->frame;
    else if (rep_INTP(win))
	g_win = rep_INT(win);
    else
	g_win = root_window;

    if (WINDOWP(confine_to) && VWIN(confine_to)->visible)
        c_win = VWIN(confine_to)->frame;
    else if (rep_INTP(confine_to))
	c_win = rep_INT(confine_to);
    else
        c_win = None;

    if (cursor != Qnil && !CURSORP(cursor))
    {
	cursor = Fget_cursor (cursor);
	if (!cursor)
	    cursor = Qnil;
    }

again:
    ret = XGrabPointer (dpy, g_win, False, POINTER_GRAB_EVENTS,
			rep_NILP( ptr_sync) ? GrabModeAsync : GrabModeSync,
			rep_NILP( kbd_sync) ? GrabModeAsync : GrabModeSync,
			c_win,
			CURSORP(cursor) ? VCURSOR(cursor)->cursor : None,
			last_event_time);
    if (ret == GrabNotViewable && (g_win != root_window || c_win != None))
    {
	/* fall back to the root window. */
	g_win = root_window;
	c_win = None;
	goto again;
    }

    if (ret == GrabSuccess)
	mark_pointer_grabbed ();

    DB(("grab-pointer: time=%lu ret=%d\n", last_event_time, ret));
    return (ret == GrabSuccess) ? Qt : Qnil;
}

DEFUN("ungrab-pointer", Fungrab_pointer, Sungrab_pointer, (void), rep_Subr0) /*
::doc:sawfish.wm.events#ungrab-pointer::
ungrab-pointer

Release the grab on the mouse pointer.
::end:: */
{
    ungrab_pointer ();
    synthesize_button_release ();
    DB(("ungrab-pointer: time=%lu\n", last_event_time));
    return Qt;
}

DEFUN("grab-keyboard", Fgrab_keyboard, Sgrab_keyboard,
      (repv win, repv ptr_sync, repv kbd_sync), rep_Subr3) /*
::doc:sawfish.wm.events#grab-keyboard::
grab-keyboard [WINDOW] [PTR-SYNC] [KBD-SYNC]

Grab the keyboard and direct all keyboard events to window object
WINDOW.  If WINDOW is a window object corresponding to a visible
window the grab will be made on its frame.  If WINDOW is an integer it
specifies the window id of the grab window.  Otherwise the grab will
be made on the root window.  If the window id of a non-viewable window
was specified the grab is made on the root window instead.

If PTR-SYNC or KBD-SYNC is non-nil the pointer or the keyboard will be
frozen, i.e., the device will not produce events until either the grab
is released or events are re-enabled using allow-events.

Returns non-nil if the grab succeeded.
::end:: */
{
    Window g_win;
    int ret;

    if (WINDOWP(win) && VWIN(win)->visible)
	g_win = VWIN(win)->frame;
    else if (rep_INTP(win))
	g_win = rep_INT(win);
    else
	g_win = root_window;

again:
    ret = XGrabKeyboard (dpy, g_win, False,
			 rep_NILP( ptr_sync) ? GrabModeAsync : GrabModeSync,
			 rep_NILP( kbd_sync) ? GrabModeAsync : GrabModeSync,
			 last_event_time);
    if (ret == GrabNotViewable && g_win != root_window)
    {
	/* fall back to the root window. */
	g_win = root_window;
	goto again;
    }

    DB(("grab-keyboard: time=%lu ret=%d\n", last_event_time, ret));
    return (ret == GrabSuccess) ? Qt : Qnil;
}
    
DEFUN("ungrab-keyboard", Fungrab_keyboard,
      Sungrab_keyboard, (void), rep_Subr0) /*
::doc:sawfish.wm.events#ungrab-keyboard::
ungrab-keyboard

Release the grab on the keyboard.
::end:: */
{
    DB(("ungrab-keyboard: time=%lu\n", last_event_time));
    XUngrabKeyboard (dpy, last_event_time);
    return Qt;
}

DEFUN("screen-width", Fscreen_width, Sscreen_width, (void), rep_Subr0) /*
::doc:sawfish.wm.misc#screen-width::
screen-width

Return the width of the root window (in pixels).
::end:: */
{
    return rep_MAKE_INT(screen_width);
}

DEFUN("screen-height", Fscreen_height, Sscreen_height, (void), rep_Subr0) /*
::doc:sawfish.wm.misc#screen-height::
screen-height

Return the height of the root window (in pixels).
::end:: */
{
    return rep_MAKE_INT(screen_height);
}

DEFUN("sync-server", Fsync_server, Ssync_server, (repv wait), rep_Subr0) /*
::doc:sawfish.wm.misc#sync-server::
sync-server [WAIT]

Flush all pending X requests, don't wait for them to finish, unless WAIT
is true.
::end:: */
{
    if (wait == Qnil)
	XFlush (dpy);
    else
	XSync (dpy, False);
    return Qt;
}

DEFUN("delete-x-property", Fdelete_x_property, Sdelete_x_property,
      (repv win, repv atom), rep_Subr2) /*
::doc:sawfish.wm.misc#delete-x-property::
delete-x-property WINDOW ATOM

Delete the X property ATOM (a symbol) of WINDOW.

WINDOW may be the symbol `root', a window object or a numeric window id.
::end:: */
{
    Window w = x_win_from_arg (win);
    rep_DECLARE2(atom, rep_SYMBOLP);
    if (w == 0)
	return WINDOWP(win) ? atom : rep_signal_arg_error (win, 1);
    XDeleteProperty (dpy, w,
		     XInternAtom (dpy, rep_STR(rep_SYM(atom)->name), False));
    if (WINDOWP (win))
	property_cache_invalidate (win, atom);
    return atom;
}

DEFUN("list-x-properties", Flist_x_properties, Slist_x_properties,
      (repv win), rep_Subr1) /*
::doc:sawfish.wm.misc#list-x-properties::
list-x-properties WINDOW

List all X properties (symbols) of WINDOW.

WINDOW may be the symbol `root', a window object or a numeric window id.
::end:: */
{
    Window w;
    Atom *atoms;
    int count;
    repv ret = Qnil;

    w = x_win_from_arg (win);
    if (w == 0)
	return WINDOWP(win) ? Qnil : rep_signal_arg_error (win, 1);
    atoms = XListProperties (dpy, w, &count);
    if (atoms != 0)
    {
	char **names = alloca (sizeof (char *) * count);
	if (XGetAtomNames (dpy, atoms, count, names) != 0)
	{
	    int i;
	    for (i = 0; i < count; i++)
	    {
		ret = Fcons (Fintern (rep_string_dup (names[i]),
				      rep_obarray), ret);
		XFree (names[i]);
	    }
	}
	XFree (atoms);
    }
    return Fnreverse (ret);
}

DEFUN("get-x-property", Fget_x_property, Sget_x_property,
      (repv win, repv prop), rep_Subr2) /*
::doc:sawfish.wm.misc#get-x-property::
get-x-property WINDOW PROPERTY

Return (TYPE FORMAT DATA) representing the X property PROPERTY (a
symbol) of WINDOW. If no such property exists, return nil.

WINDOW may be the symbol `root', a window object or a numeric window
id.

TYPE is a symbol representing the atom defining the type of the
property. FORMAT is an integer, either 8, 16 or 32, defining the width
of the data items read. DATA is an array, either a string for an 8-bit
format, or a vector of integers.

If TYPE is `ATOM' and FORMAT is 32, then DATA will be a vector of
symbols, representing the atoms read.
::end:: */
{
    Window w;
    Atom a_prop;
    Atom type;
    int format;
    unsigned long nitems;
    unsigned char *data = 0;
    repv type_sym, ret_data = Qnil;

    w = x_win_from_arg (win);
    rep_DECLARE2(prop, rep_SYMBOLP);
    if (w == 0)
	return WINDOWP(win) ? Qnil : rep_signal_arg_error (win, 1);

    if (WINDOWP (win))
    {
	ret_data = property_cache_ref (win, prop);
	if (ret_data != rep_NULL)
	    return ret_data;
    }

    a_prop = XInternAtom (dpy, rep_STR(rep_SYM(prop)->name), False);

    /* First read the data.. */
    {
	long long_length = 32;
	unsigned long bytes_after;
	while (1)
	{
	    ret_data = Qnil;
	    if (data != 0)
		XFree (data);
	    if (XGetWindowProperty (dpy, w, a_prop, 0, long_length, False,
				    AnyPropertyType, &type, &format,
				    &nitems, &bytes_after, &data) != Success)
		goto out;
	    if (type == None)
		goto out;
	    if (bytes_after == 0)
		break;
	    long_length += (bytes_after / sizeof(unsigned long)) + 1;
	}
    }

    /* Convert the type to a symbol */
    type_sym = x_atom_symbol (type);
    
    /* Then convert the contents to a vector or string */
    switch (format)
    {
	unsigned short *s_data;
	unsigned long *l_data;
	int i;

    case 8:
	ret_data = rep_string_dupn ((char *) data, nitems);
	break;

    case 16:
	ret_data = Fmake_vector (rep_MAKE_INT(nitems), Qnil);
	s_data = (unsigned short *)data;
	for (i = 0; i < nitems; i++)
	    rep_VECTI(ret_data, i) = rep_MAKE_INT(s_data[i]);
	break;

    case 32:
	ret_data = Fmake_vector (rep_MAKE_INT(nitems), Qnil);
	l_data = (unsigned long *)data;
	for (i = 0; i < nitems; i++)
	{
	    repv name;
	    if (type == XA_ATOM && (name = x_atom_symbol (l_data[i])) != Qnil)
		rep_VECTI(ret_data, i) = name;
	    else if (type == XA_INTEGER)
		rep_VECTI(ret_data, i) = rep_make_long_int((long) l_data[i]);
	    else
		rep_VECTI(ret_data, i) = rep_make_long_uint(l_data[i] & 0xffffffffUL);
	}
	break;
    }

    XFree (data);
    ret_data = rep_list_3 (type_sym, rep_MAKE_INT(format), ret_data);

out:
    if (WINDOWP (win))
	property_cache_set (win, prop, ret_data, 0);

    return ret_data;
}

DEFUN("set-x-property", Fset_x_property, Sset_x_property,
      (repv win, repv prop, repv data, repv type, repv format), rep_Subr5) /*
::doc:sawfish.wm.misc#set-x-property::
set-x-property WINDOW PROPERTY DATA TYPE FORMAT

Set the X property PROPERTY (a symbol) of WINDOW to the array DATA.

WINDOW may be the symbol `root', a window object or a numeric window
id.

TYPE is a symbol representing the atom defining the type of the
property, FORMAT is either 8, 16 or 32 defining the width of the data
values. DATA is either a string or a vector of integers.

If TYPE is `ATOM' and FORMAT is 32, then any symbols in DATA will be
converted to their numeric X atoms.
::end:: */
{
    Window w;
    Atom a_prop, a_type;
    unsigned long nitems;
    unsigned char *c_data = 0;

    w = x_win_from_arg (win);
    rep_DECLARE2(prop, rep_SYMBOLP);
    a_prop = XInternAtom (dpy, rep_STR(rep_SYM(prop)->name), False);
    rep_DECLARE(3, data, rep_VECTORP(data) || rep_STRINGP(data));
    rep_DECLARE4(type, rep_SYMBOLP);
    a_type = XInternAtom (dpy, rep_STR(rep_SYM(type)->name), False);
    rep_DECLARE5(format, rep_INTP);
    if (w == 0)
	return WINDOWP(win) ? prop : rep_signal_arg_error (win, 1);

    if (WINDOWP (win))
	property_cache_set (win, prop, rep_list_3 (type, format, data), 1);

    /* Convert to data array */

    if (rep_STRINGP(data))
	nitems = rep_STRING_LEN(data);
    else
	nitems = rep_VECT_LEN(data);

    switch (rep_INT(format))
    {
	int i;
	unsigned short *s_data;
	unsigned long *l_data;

    case 8:
	if (rep_STRINGP(data))
	    c_data = (unsigned char *) rep_STR (data);
	else
	{
	    c_data = alloca (nitems);
	    for (i = 0; i < nitems; i++)
		c_data[i] = rep_STR(data)[i];
	}
	break;

    case 16:
	if (rep_STRINGP(data))
	    return rep_signal_arg_error (data, 3);
	c_data = alloca (nitems * sizeof (unsigned short));
	s_data = (unsigned short *)c_data;
	for (i = 0; i < nitems; i++)
	    s_data[i] = rep_INT(rep_VECTI(data, i));
	break;

    case 32:
	if (rep_STRINGP(data))
	    return rep_signal_arg_error (data, 3);
	c_data = alloca (nitems * sizeof (unsigned long));
	l_data = (unsigned long *)c_data;
	for (i = 0; i < nitems; i++)
	{
	    if (a_type == XA_ATOM && rep_SYMBOLP(rep_VECTI(data, i)))
		l_data[i] = XInternAtom (dpy, rep_STR(rep_SYM(rep_VECTI(data, i))->name), False);
	    else
		l_data[i] = rep_get_long_uint (rep_VECTI(data, i));
	}
	break;
    }

    /* Overwrite property */
    XChangeProperty (dpy, w, a_prop, a_type, rep_INT(format),
		     PropModeReplace, c_data, nitems);
    return prop;
}

DEFUN("send-client-message", Fsend_client_message, Ssend_client_message,
      (repv win, repv type, repv data, repv format), rep_Subr4) /*
::doc:sawfish.wm.events#send-client-message::
send-client-message WINDOW TYPE DATA FORMAT

Send an X ClientMessage event to WINDOW (a window object or the symbol
`root'). It will be of the type TYPE (a symbol), contain the array of
integers DATA (i.e. a vector or a string), and it will be transferred as
FORMAT sized quantities (8, 16 or 32).
::end:: */
{
    XClientMessageEvent ev;
    Window w = x_win_from_arg (win);

    rep_DECLARE2(type, rep_SYMBOLP);
    rep_DECLARE(3, data, rep_STRINGP(data) || rep_VECTORP(data));
    rep_DECLARE4(format, rep_INTP);

    if (w == 0)
	return WINDOWP(win) ? Qnil : rep_signal_arg_error (win, 1);

    ev.type = ClientMessage;
    ev.window = w;
    ev.message_type = XInternAtom (dpy, rep_STR(rep_SYM(type)->name), False);
    ev.format = rep_INT(format);

    switch (rep_INT(format))
    {
	int i;

    case 8:
	if (rep_STRINGP(data))
	    memcpy (rep_STR(data), ev.data.b, MAX(rep_STRING_LEN(data), 20));
	else
	{
	    for (i = 0; i < rep_VECT_LEN(data) && i < 20; i++)
		ev.data.b[i] = rep_INT(rep_VECTI(data, i));
	}
	break;

    case 16:
	if (rep_STRINGP(data))
	    return rep_signal_arg_error (data, 3);
	for (i = 0; i < rep_VECT_LEN(data) && i < 10; i++)
	    ev.data.s[i] = rep_INT(rep_VECTI(data, i));
	break;

    case 32:
	if (rep_STRINGP(data))
	    return rep_signal_arg_error (data, 3);
	for (i = 0; i < rep_VECT_LEN(data) && i < 5; i++)
	    ev.data.l[i] = rep_get_long_uint (rep_VECTI(data, i));
	break;
    }

    XSendEvent (dpy, w, False, 0L, (XEvent *) &ev);
    return win;
}

DEFUN("create-window", Fcreate_window, Screate_window,
      (repv parent, repv x, repv y, repv width, repv height), rep_Subr5) /*
::doc:sawfish.wm.misc#create-window::
create-window PARENT-WINDOW X Y WIDTH HEIGHT

Create an unmapped window that is a child of PARENT-WINDOW (a window object,
an integer window id, or the symbol `root'), with the specified dimensions.

Returns the window id of the new window.
::end:: */
{
    Window parent_w = x_win_from_arg (parent);
    Window id;
    rep_DECLARE2(x, rep_INTP);
    rep_DECLARE3(y, rep_INTP);
    rep_DECLARE4(width, rep_INTP);
    rep_DECLARE5(height, rep_INTP);
    if (parent_w == 0)
	return WINDOWP(parent) ? Qnil : rep_signal_arg_error (parent, 1);
    id = XCreateSimpleWindow (dpy, parent_w, rep_INT(x), rep_INT(y),
			      rep_INT(width), rep_INT(height),
			      0, BlackPixel (dpy, screen_num),
			      WhitePixel (dpy, screen_num));
    return id ? rep_MAKE_INT(id) : Qnil;
}

DEFUN("x-atom", Fx_atom, Sx_atom, (repv symbol), rep_Subr1) /*
::doc:sawfish.wm.misc#x-atom::
x-atom SYMBOL

Return the integer identifying the X atom with the same name as SYMBOL.
::end:: */
{
    rep_DECLARE1(symbol, rep_SYMBOLP);
    return rep_MAKE_INT (XInternAtom (dpy, rep_STR(rep_SYM(symbol)->name),
				      False));
}

DEFUN("x-atom-name", Fx_atom_name, Sx_atom_name, (repv atom), rep_Subr1) /*
::doc:sawfish.wm.misc#x-atom-name::
x-atom-name ATOM

Return the symbol with the same name as the X atom identified by the
integer ATOM.
::end:: */
{
    rep_DECLARE1(atom, rep_INTP);
    return x_atom_symbol (rep_INT(atom));
}

DEFUN("root-window-id", Froot_window_id, Sroot_window_id, (void), rep_Subr0) /*
::doc:sawfish.wm.misc#root-window-id::
root-window-id

Returns the numeric id of the root window of the managed screen.
::end:: */
{
    return rep_MAKE_INT (root_window);
}

/* xinerama support */

void
update_xinerama_info (void)
{
#ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
# ifndef TEST_XINERAMA
    if (dpy != 0)
    {
	if (XineramaQueryExtension (dpy, &xinerama_event_base,
				    &xinerama_error_base))
	{
	    xinerama_head_info = XineramaQueryScreens (dpy, &xinerama_heads);
	}
    }
# else
    xinerama_head_info = debug_heads;
    xinerama_heads = debug_nheads;
# endif
#endif
}

DEFUN ("head-count", Fhead_count, Shead_count, (void), rep_Subr0)
{
    return rep_MAKE_INT (MAX (1, xinerama_heads));
}

DEFUN ("find-head", Ffind_head, Sfind_head, (repv x, repv y), rep_Subr2)
{
    if (rep_CONSP (x) && y == Qnil)
    {
	y = rep_CDR (x);
	x = rep_CAR (x);
    }
    rep_DECLARE (1, x, rep_INTP (x));
    rep_DECLARE (2, y, rep_INTP (y));

#ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
    {
	int i;
	for (i = 0; i < xinerama_heads; i++)
	{
	    if ((xinerama_head_info[i].x_org <= rep_INT (x))
		&& (xinerama_head_info[i].y_org <= rep_INT (y))
		&& (xinerama_head_info[i].x_org
		    + xinerama_head_info[i].width > rep_INT (x))
		&& (xinerama_head_info[i].y_org
		    + xinerama_head_info[i].height > rep_INT (y)))
	    {
		return rep_MAKE_INT (i);
	    }
	}
    }
#endif
    if (xinerama_heads == 0
	&& rep_INT (x) >= 0 && rep_INT (x) < screen_width
	&& rep_INT (y) >= 0 && rep_INT (y) < screen_height)
    {
	return rep_MAKE_INT (0);
    }
    return Qnil;
}

DEFUN ("head-dimensions", Fhead_dimensions,
       Shead_dimensions, (repv id), rep_Subr1)
{
    rep_DECLARE (1, id, rep_INTP (id));

    if (xinerama_heads == 0 && rep_INT (id) == 0)
    {
	return Fcons (rep_MAKE_INT (screen_width),
		      rep_MAKE_INT (screen_height));
    }
    else
    {
	rep_DECLARE (1, id, rep_INT (id) >= 0
		     && rep_INT (id) < xinerama_heads);
#ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
	return Fcons (rep_MAKE_INT (xinerama_head_info[rep_INT(id)].width),
		      rep_MAKE_INT (xinerama_head_info[rep_INT(id)].height));
#else
	abort ();
#endif
    }
}

DEFUN ("head-offset", Fhead_offset, Shead_offset, (repv id), rep_Subr1)
{
    rep_DECLARE (1, id, rep_INTP (id));

    if (xinerama_heads == 0 && rep_INT (id) == 0)
    {
	return Fcons (rep_MAKE_INT (0), rep_MAKE_INT (0));
    }
    else
    {
	rep_DECLARE (1, id, rep_INT (id) >= 0
		     && rep_INT (id) < xinerama_heads);
#ifdef HAVE_X11_EXTENSIONS_XINERAMA_H
	return Fcons (rep_MAKE_INT (xinerama_head_info[rep_INT(id)].x_org),
		      rep_MAKE_INT (xinerama_head_info[rep_INT(id)].y_org));
#else
	abort ();
#endif
    }
}

/* Displaying a `message' window */

static Window message_win;

static struct {
    GC gc;
    repv text, fg, bg, font, justify;
    int width, spacing;
} message;

#define MSG_PAD_X 20
#define MSG_PAD_Y 10

static void
refresh_message_window ()
{
    if (message_win != 0)
    {
	XGCValues values;
	unsigned long mask;
	char *ptr;
	int row = 0;

	values.background = VCOLOR(message.bg)->pixel;
	values.graphics_exposures = False;
	mask = GCBackground | GCGraphicsExposures;

	if (message.gc == 0)
	    message.gc = XCreateGC (dpy, message_win, mask, &values);
	else
	    XChangeGC (dpy, message.gc, mask, &values);

	XClearWindow (dpy, message_win);

	ptr = rep_STR(message.text);
	while (*ptr != 0)
	{
	    char *end = strchr (ptr, '\n');
	    int offset;
	    if (end == 0)
		end = ptr + strlen (ptr);
	    if (message.justify == Qleft)
		offset = MSG_PAD_X;
	    else
	    {
		int width = x_text_width (message.font, ptr, end - ptr);
		if (message.justify == Qright)
		    offset = message.width - (width + MSG_PAD_X);
		else
		    offset = (message.width - width) / 2;
	    }
	    x_draw_string (message_win, message.font,
			   message.gc, VCOLOR(message.fg), offset,
			   MSG_PAD_Y
			   + row * (VFONT(message.font)->ascent
				    + VFONT(message.font)->descent
				    + message.spacing)
			   + VFONT(message.font)->ascent, ptr, end - ptr);
	    row++;
	    ptr = end;
	    if (*ptr == '\n')
		ptr++;
	}
    }
}

static void
message_event_handler (XEvent *ev)
{
    if (ev->type == Expose && ev->xexpose.count == 0)
	refresh_message_window ();
    else if (ev->type == ButtonPress)
	Fdisplay_message (Qnil, Qnil);
}

DEFSTRING(white, "white");
DEFSTRING(black, "black");

DEFUN("display-message", Fdisplay_message, Sdisplay_message,
      (repv text, repv attrs), rep_Subr2)
{
    if (text == Qnil)
    {
	if (message_win != 0)
	{
	    deregister_event_handler (message_win);
	    XDestroyWindow (dpy, message_win);
	    message_win = 0;
	}
	if (message.gc != 0)
	{
	    XFreeGC (dpy, message.gc);
	    message.gc = 0;
	}
	message.font = message.fg = message.bg = message.text = Qnil;
	return Qnil;
    }
    else
    {
	int height, x, y;
	repv tem;

	rep_DECLARE1(text, rep_STRINGP);
	rep_DECLARE2(attrs, rep_LISTP);

	message.text = text;
	message.font = message.fg = message.bg = Qnil;
	message.justify = Qleft;
	message.spacing = 0;

	tem = Fassq (Qfont, attrs);
	if (tem && rep_CONSP(tem))
	{
	    message.font = rep_CDR(tem);
	    if (!FONTP(message.font))
	    {
		message.font = Fget_font (message.font);
		if (!message.font)
		    return rep_NULL;
	    }
	}
	if (!FONTP(message.font))
	    message.font = global_symbol_value (Qdefault_font);
	if (!FONTP(message.font))
	    return rep_signal_arg_error (Qfont, 1);

	tem = Fassq (Qforeground, attrs);
	if (tem && rep_CONSP(tem))
	{
	    message.fg = rep_CDR(tem);
	    if (!COLORP(message.fg))
	    {
		message.fg = Fget_color (message.fg, Qnil);
		if (!message.fg)
		    return rep_NULL;
	    }
	}
	if (!COLORP(message.fg))
	    message.fg = Fget_color (rep_VAL(&black), Qnil);
	if (!COLORP(message.fg))
	    return rep_signal_arg_error (Qforeground, 1);

	tem = Fassq (Qbackground, attrs);
	if (tem && rep_CONSP(tem))
	{
	    message.bg = rep_CDR(tem);
	    if (!COLORP(message.bg))
	    {
		message.bg = Fget_color (message.bg, Qnil);
		if (!message.bg)
		    return rep_NULL;
	    }
	}
	if (!COLORP(message.bg))
	    message.bg = Fget_color (rep_VAL(&white), Qnil);
	if (!COLORP(message.bg))
	    return rep_signal_arg_error (Qbackground, 1);

	tem = Fassq (Qx_justify, attrs);
	if (tem && rep_CONSP(tem))
	    message.justify = rep_CDR(tem);

	tem = Fassq (Qspacing, attrs);
	if (tem && rep_CONSP(tem) && rep_INTP(rep_CDR(tem)))
	    message.spacing = rep_INT(rep_CDR(tem));

	{
	    char *ptr = rep_STR(text);
	    int max_width = 0, rows = 0;
	    repv head = Qnil;
	    int head_width, head_height;
	    int head_xoff, head_yoff;
	    while (*ptr != 0)
	    {
		int text_width;
		char *end = strchr (ptr, '\n');
		if (end == 0)
		  end = ptr + strlen (ptr);
		text_width = x_text_width (message.font, ptr, end - ptr);
		max_width = MAX(max_width, text_width);
		rows++;
		ptr = end;
		if (*ptr == '\n')
		  ptr++;
	    }
	    message.width = max_width + MSG_PAD_X * 2;
	    height = ((rep_INT(Ffont_height (message.font)) + message.spacing)
		      * rows + MSG_PAD_Y * 2);

	    /* Find the head to put the message on. */
	    tem = Fassq (Qhead, attrs);
	    if (tem && rep_CONSP (tem) && rep_INTP (rep_CDR (tem)))
		head = rep_CDR (tem);
	    if (!head || head == Qnil)
		head = Ffind_head (Fquery_pointer (Qnil), Qnil);

	    if (head && head != Qnil)
	    {
		/* We have a head to centre on. */

		tem = Fhead_dimensions (head);
		if (!tem)
		    goto no_head;
		head_width = rep_INT (rep_CAR (tem));
		head_height = rep_INT (rep_CDR (tem));

		tem = Fhead_offset (head);
		if (!tem)
		    goto no_head;
		head_xoff = rep_INT (rep_CAR (tem));
		head_yoff = rep_INT (rep_CDR (tem));
	    }
	    else
	    {
		/* No head, just centre on the screen. */
	    no_head:
		head_xoff = head_yoff = 0;
		head_width = screen_width;
		head_height = screen_height;
	    }

	    x = head_xoff + ((head_width - message.width) / 2);
	    y = head_yoff + ((head_height - height) / 2);
	}

	tem = Fassq (Qposition, attrs);
	if (tem && rep_CONSP(tem))
	{
	    tem = rep_CDR(tem);
	    if (rep_CONSP(tem))
	    {
		if (rep_INTP(rep_CAR(tem)))
		{
		    x = rep_INT(rep_CAR(tem));
		    if (x < 0)
			x += screen_width - message.width;
		}
		if (rep_INTP(rep_CDR(tem)))
		{
		    y = rep_INT(rep_CDR(tem));
		    if (y < 0)
			y += screen_height - height;
		}
	    }
	}

	if (x + message.width > screen_width)
	    x = MAX (0, screen_width - message.width - 4);
	else if (x < 4)
	    x = 4;
	if (y + height > screen_height)
	    y = MAX (0, screen_height - height - 4);
	else if (y < 4)
	    y = 4;

	if (message_win == 0)
	{
	    /* I tried setting save_under in here, but it just slows
	       down opaque window moves.. */
	    XSetWindowAttributes attr;
	    attr.override_redirect = True;
	    attr.background_pixel = VCOLOR(message.bg)->pixel;
	    attr.border_pixel = BlackPixel(dpy, screen_num);
	    attr.event_mask = ExposureMask | ButtonPressMask;
	    attr.colormap = image_cmap;
	    message_win = XCreateWindow (dpy, root_window, x, y,
					 message.width, height, 1,
					 image_depth, InputOutput,
					 image_visual,
					 CWBackPixel | CWBorderPixel
					 | CWOverrideRedirect | CWEventMask
					 | CWColormap, &attr);
	    if (message_win == 0)
		return Qnil;
	    register_event_handler (message_win, message_event_handler);
	    XMapRaised (dpy, message_win);
	}
	else
	{
	    XWindowChanges attr;
	    attr.x = x;
	    attr.y = y;
	    attr.width = message.width;
	    attr.height = height;
	    attr.stack_mode = TopIf;
	    XConfigureWindow (dpy, message_win,
			      CWX | CWY | CWWidth | CWHeight | CWStackMode,
			      &attr);
	    XSetWindowBackground (dpy, message_win, VCOLOR(message.bg)->pixel);
	    refresh_message_window ();
	}
	return rep_MAKE_INT(message_win);
    }
}

/* initialisation */

void
functions_init (void)
{
    repv tem;

    tem = rep_push_structure ("sawfish.wm.windows.subrs");
    rep_ADD_SUBR(Srestack_windows);
    rep_ADD_SUBR(Sx_raise_window);
    rep_ADD_SUBR(Sx_lower_window);
    rep_ADD_SUBR_INT(Sdestroy_window);
    rep_ADD_SUBR(Smove_window_to);
    rep_ADD_SUBR(Sresize_window_to);
    rep_ADD_SUBR(Smove_resize_window_to);
    rep_pop_structure (tem);

    tem = rep_push_structure ("sawfish.wm.misc");
    rep_ADD_SUBR(Sx_kill_client);
    rep_ADD_SUBR(Swarp_cursor);
    rep_ADD_SUBR(Sgrab_server);
    rep_ADD_SUBR(Sungrab_server);
    rep_ADD_SUBR(Sserver_grabbed_p);
    rep_ADD_SUBR(Sscreen_width);
    rep_ADD_SUBR(Sscreen_height);
    rep_ADD_SUBR(Ssync_server);
    rep_ADD_SUBR(Sdelete_x_property);
    rep_ADD_SUBR(Slist_x_properties);
    rep_ADD_SUBR(Sget_x_property);
    rep_ADD_SUBR(Sset_x_property);
    rep_ADD_SUBR(Screate_window);
    rep_ADD_SUBR(Sx_atom);
    rep_ADD_SUBR(Sx_atom_name);
    rep_ADD_SUBR(Sroot_window_id);
    rep_ADD_SUBR(Shead_count);
    rep_ADD_SUBR(Sfind_head);
    rep_ADD_SUBR(Shead_dimensions);
    rep_ADD_SUBR(Shead_offset);
    rep_ADD_SUBR(Sdisplay_message);
    rep_pop_structure (tem);

    tem = rep_push_structure ("sawfish.wm.events");
    rep_ADD_SUBR(Sgrab_pointer);
    rep_ADD_SUBR(Sungrab_pointer);
    rep_ADD_SUBR(Sgrab_keyboard);
    rep_ADD_SUBR(Sungrab_keyboard);
    rep_ADD_SUBR(Ssend_client_message);
    rep_pop_structure (tem);

    rep_INTERN(root);
    rep_INTERN_SPECIAL(after_restacking_hook);
    rep_INTERN(position);
    rep_INTERN(spacing);
    rep_INTERN(head);

    rep_mark_static (&message.text);
    rep_mark_static (&message.fg);
    rep_mark_static (&message.bg);
    rep_mark_static (&message.font);
    rep_mark_static (&message.justify);

    update_xinerama_info ();
}

void
functions_kill (void)
{
    if (message_win != 0)
	XDestroyWindow (dpy, message_win);
}