File: main.c

package info (click to toggle)
rxvt 1:2.7.10-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,068 kB
  • ctags: 4,415
  • sloc: ansic: 23,997; sh: 6,606; makefile: 649; perl: 33
file content (1559 lines) | stat: -rw-r--r-- 43,187 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
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
/*--------------------------------*-C-*---------------------------------*
 * File:	main.c
 *----------------------------------------------------------------------*
 * $Id: main.c,v 1.172 2002/10/24 01:50:58 gcw Exp $
 *
 * All portions of code are copyright by their respective author/s.
 * Copyright (c) 1992      John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
 *				- original version
 * Copyright (c) 1994      Robert Nation <nation@rocket.sanders.lockheed.com>
 *				- extensive modifications
 * Copyright (c) 1995      Garrett D'Amore <garrett@netcom.com>
 * Copyright (c) 1997      mj olesen <olesen@me.QueensU.CA>
 *				- extensive modifications
 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
 *				- extensive modifications
 *
 * 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 "../config.h"		/* NECESSARY */
#include "rxvt.h"		/* NECESSARY */
#include "main.intpro"		/* PROTOS for internal routines */

#include <signal.h>

#ifdef TTY_GID_SUPPORT
# include <grp.h>
#endif

#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif

/*----------------------------------------------------------------------*/
/* rxvt_init() */
/* LIBPROTO */
rxvt_t         *
rxvt_init(int argc, const char *const *argv)
{
    const char    **cmd_argv;
    rxvt_t         *r;

    r = (rxvt_t *)rxvt_calloc(1, sizeof(rxvt_t));
    rxvt_set_r(r);		/* only assignment to _rxvt_vars */
    if (rxvt_init_vars(r) < 0) {
	free(r);
	return NULL;
    }

/*
 * Save and then give up any super-user privileges
 * If we need privileges in any area then we must specifically request it.
 * We should only need to be root in these cases:
 *  1.  write utmp entries on some systems
 *  2.  chown tty on some systems
 */
    rxvt_privileges(r, SAVE);
    rxvt_privileges(r, IGNORE);

    rxvt_init_secondary(r);

    cmd_argv = rxvt_init_resources(r, argc, argv);

#if (MENUBAR_MAX)
    rxvt_menubar_read(r, r->h->rs[Rs_menu]);
#endif
#ifdef HAVE_SCROLLBARS
    if (r->Options & Opt_scrollBar)
      scrollbar_setIdle();	/* set existence for size calculations */
#endif

    rxvt_Create_Windows(r, argc, argv);

    rxvt_init_xlocale(r);

    rxvt_scr_reset(r);		/* initialize screen */
    rxvt_Gr_reset(r);		/* reset graphics */

#ifdef DEBUG_X
    XSynchronize(r->Xdisplay, True);
    XSetErrorHandler((XErrorHandler) abort);
#else
    XSetErrorHandler((XErrorHandler) rxvt_xerror_handler);
#endif

#ifdef HAVE_SCROLLBARS
    if (r->Options & Opt_scrollBar)
	rxvt_Resize_scrollBar(r);	/* create and map scrollbar */
#endif
#if (MENUBAR_MAX)
    if (menubar_visible(r))
	XMapWindow(r->Xdisplay, r->menuBar.win);
#endif
#ifdef TRANSPARENT
    if (r->Options & Opt_transparent) {
	XSelectInput(r->Xdisplay, Xroot, PropertyChangeMask);
	rxvt_check_our_parents(r);
    }
#endif
    XMapWindow(r->Xdisplay, r->TermWin.vt);
    XMapWindow(r->Xdisplay, r->TermWin.parent[0]);

    rxvt_init_env(r);
    rxvt_init_command(r, cmd_argv);
    return r;
}

/* ------------------------------------------------------------------------- *
 *                       SIGNAL HANDLING & EXIT HANDLER                      *
 * ------------------------------------------------------------------------- */
/*
 * Catch a SIGCHLD signal and exit if the direct child has died
 */
/* ARGSUSED */
/* EXTPROTO */
RETSIGTYPE
rxvt_Child_signal(int sig __attribute__((unused)))
{
    int             pid, save_errno = errno;
    rxvt_t         *r;

    do {
	errno = 0;
    } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR);

    r = rxvt_get_r();
    if (pid == r->h->cmd_pid)
	exit(EXIT_SUCCESS);

    errno = save_errno;
    signal(SIGCHLD, rxvt_Child_signal);
}

/*
 * Catch a fatal signal and tidy up before quitting
 */
/* EXTPROTO */
RETSIGTYPE
rxvt_Exit_signal(int sig)
{
    signal(sig, SIG_DFL);
#ifdef DEBUG_CMD
    rxvt_print_error("signal %d", sig);
#endif
    rxvt_clean_exit();
    kill(getpid(), sig);
}

/* ARGSUSED */
/* INTPROTO */
int
rxvt_xerror_handler(const Display *display __attribute__((unused)), const XErrorEvent *event)
{
    rxvt_t         *r = rxvt_get_r();

    if (r->h->allowedxerror == -1) {
	r->h->allowedxerror = event->error_code;
	return 0;		/* ignored anyway */
    }
    rxvt_print_error("XError: Request: %d . %d, Error: %d",
		     event->request_code, event->minor_code,
		     event->error_code);
    /* XXX: probably should call rxvt_clean_exit() bypassing X routines */
    exit(EXIT_FAILURE);
    /* NOTREACHED */
}

/*----------------------------------------------------------------------*/
/*
 * Exit gracefully, clearing the utmp entry and restoring tty attributes
 * TODO: if debugging, this should free up any known resources if we can
 */
/* EXTPROTO */
void
rxvt_clean_exit(void)
{
    rxvt_t         *r = rxvt_get_r();

#ifdef DEBUG_SCREEN
    rxvt_scr_release(r);
#endif
    rxvt_privileged_ttydev(r, RESTORE);
    rxvt_privileged_utmp(r, RESTORE);
#ifdef USE_XIM
    if (r->h->Input_Context != NULL) {
	XDestroyIC(r->h->Input_Context);
	r->h->Input_Context = NULL;
    }
#endif
}

/* ------------------------------------------------------------------------- *
 *                         MEMORY ALLOCATION WRAPPERS                        *
 * ------------------------------------------------------------------------- */
/* EXTPROTO */
void           *
rxvt_malloc(size_t size)
{
     void           *p;

     p = malloc(size);
     if (p)
	return p;

     fprintf(stderr, APL_NAME ": memory allocation failure.  Aborting");
     rxvt_clean_exit();
     exit(EXIT_FAILURE);
     /* NOTREACHED */
}

/* EXTPROTO */
void           *
rxvt_calloc(size_t number, size_t size)
{
     void           *p;

     p = calloc(number, size);
     if (p)
	return p;

     fprintf(stderr, APL_NAME ": memory allocation failure.  Aborting");
     rxvt_clean_exit();
     exit(EXIT_FAILURE);
     /* NOTREACHED */
}

/* EXTPROTO */
void           *
rxvt_realloc(void *ptr, size_t size)
{
     void           *p;

     if (ptr)
	p = realloc(ptr, size);
     else
	p = malloc(size);
     if (p)
	return p;

     fprintf(stderr, APL_NAME ": memory allocation failure.  Aborting");
     rxvt_clean_exit();
     exit(EXIT_FAILURE);
     /* NOTREACHED */
}
/* ------------------------------------------------------------------------- *
 *                            PRIVILEGED OPERATIONS                          *
 * ------------------------------------------------------------------------- */
/* take care of suid/sgid super-user (root) privileges */
/* INTPROTO */
void
rxvt_privileges(rxvt_t *r, int mode)
{
#if ! defined(__CYGWIN32__)
# if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
/* setreuid() is the poor man's setuid(), seteuid() */
#  define seteuid(a)	setreuid(-1, (a))
#  define setegid(a)	setregid(-1, (a))
#  define HAVE_SETEUID
# endif
# ifdef HAVE_SETEUID
    switch (mode) {
    case IGNORE:
	/*
	 * change effective uid/gid - not real uid/gid - so we can switch
	 * back to root later, as required
	 */
	seteuid(getuid());
	setegid(getgid());
	break;
    case SAVE:
	r->h->euid = geteuid();
	r->h->egid = getegid();
	break;
    case RESTORE:
	seteuid(r->h->euid);
	setegid(r->h->egid);
	break;
    }
# else
    switch (mode) {
    case IGNORE:
	setuid(getuid());
	setgid(getgid());
    /* FALLTHROUGH */
    case SAVE:
    /* FALLTHROUGH */
    case RESTORE:
	break;
    }
# endif
#endif
}

#ifdef UTMP_SUPPORT
/* EXTPROTO */
void
rxvt_privileged_utmp(rxvt_t *r, char action)
{
    struct rxvt_hidden *h = r->h;

    D_MAIN((stderr, "rxvt_privileged_utmp(%c); waiting for: %c (pid: %d)", action, h->next_utmp_action, getpid()));
    if (h->next_utmp_action != action
	|| (action != SAVE && action != RESTORE)
	|| (r->Options & Opt_utmpInhibit)
	|| h->ttydev == NULL
	|| *h->ttydev == '\0')
	return;

    rxvt_privileges(r, RESTORE);
    if (action == SAVE) {
	h->next_utmp_action = RESTORE;
	rxvt_makeutent(r, h->ttydev, h->rs[Rs_display_name]);
    } else {		/* action == RESTORE */
	h->next_utmp_action = IGNORE;
	rxvt_cleanutent(r);
    }
    rxvt_privileges(r, IGNORE);
}
#endif

#ifndef NO_SETOWNER_TTYDEV
/* EXTPROTO */
void
rxvt_privileged_ttydev(rxvt_t *r, char action)
{
    struct rxvt_hidden *h = r->h;

    D_MAIN((stderr, "rxvt_privileged_ttydev(r, %c); waiting for: %c (pid: %d)", action, h->next_tty_action, getpid()));
    if (h->next_tty_action != action
	|| (action != SAVE && action != RESTORE)
	|| h->ttydev == NULL
	|| *h->ttydev == '\0')
	return;

    rxvt_privileges(r, RESTORE);

    if (action == SAVE) {
	h->next_tty_action = RESTORE;
# ifndef RESET_TTY_TO_COMMON_DEFAULTS
/* store original tty status for restoration rxvt_clean_exit() -- rgg 04/12/95 */
	if (lstat(h->ttydev, &h->ttyfd_stat) < 0)	/* you lose out */
	    h->next_tty_action = IGNORE;
	else
# endif
	{
	    chown(h->ttydev, getuid(), h->ttygid); /* fail silently */
	    chmod(h->ttydev, h->ttymode);
# ifdef HAVE_REVOKE
	    revoke(h->ttydev);
# endif
	}
    } else {			/* action == RESTORE */
	h->next_tty_action = IGNORE;
# ifndef RESET_TTY_TO_COMMON_DEFAULTS
	chmod(h->ttydev, h->ttyfd_stat.st_mode);
	chown(h->ttydev, h->ttyfd_stat.st_uid, h->ttyfd_stat.st_gid);
# else
	chmod(h->ttydev,
	      (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
	chown(h->ttydev, 0, 0);
# endif
    }

    rxvt_privileges(r, IGNORE);

# ifndef RESET_TTY_TO_COMMON_DEFAULTS
    D_MAIN((stderr, "%s \"%s\": mode %03o, uid %d, gid %d", action == RESTORE ? "Restoring" : (action == SAVE ? "Saving" : "UNKNOWN ERROR for"), h->ttydev, h->ttyfd_stat.st_mode, h->ttyfd_stat.st_uid, h->ttyfd_stat.st_gid));
# endif
}
#endif

/*----------------------------------------------------------------------*/
/*
 * window size/position calculcations for XSizeHint and other storage.
 * if width/height are non-zero then override calculated width/height
 */
/* EXTPROTO */
void
rxvt_window_calc(rxvt_t *r, unsigned int width, unsigned int height)
{
    short           recalc_x, recalc_y;
    int             x, y, sb_w, mb_h, flags;
    unsigned int    w, h;
    unsigned int    max_width, max_height;

    r->szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity;
    r->szHint.win_gravity = NorthWestGravity;
    /* r->szHint.min_aspect.x = r->szHint.min_aspect.y = 1; */

    recalc_x = recalc_y = 0;
    flags = 0;
    if (!r->h->parsed_geometry) {
	r->h->parsed_geometry = 1;
	if (r->h->rs[Rs_geometry])
	    flags = XParseGeometry(r->h->rs[Rs_geometry], &x, &y, &w, &h);
	if (flags & WidthValue) {
	    r->TermWin.ncol = BOUND_POSITIVE_INT16(w);
	    r->szHint.flags |= USSize;
	}
	if (flags & HeightValue) {
	    r->TermWin.nrow = BOUND_POSITIVE_INT16(h);
	    r->szHint.flags |= USSize;
	}
	if (flags & XValue) {
	    r->szHint.x = x;
	    r->szHint.flags |= USPosition;
	    if (flags & XNegative) {
		recalc_x = 1;
		r->szHint.win_gravity = NorthEastGravity;
	    }
	}
	if (flags & YValue) {
	    r->szHint.y = y;
	    r->szHint.flags |= USPosition;
	    if (flags & YNegative) {
		recalc_y = 1;
		if (r->szHint.win_gravity == NorthEastGravity)
		    r->szHint.win_gravity = SouthEastGravity;
		else
		    r->szHint.win_gravity = SouthWestGravity;
	    }
	}
    }
/* TODO: BOUNDS */
    r->TermWin.width = r->TermWin.ncol * r->TermWin.fwidth;
    r->TermWin.height = r->TermWin.nrow * r->TermWin.fheight;
    max_width = MAX_COLS * r->TermWin.fwidth;
    max_height = MAX_ROWS * r->TermWin.fheight;

    r->szHint.base_width = r->szHint.base_height = 2 * r->TermWin.int_bwidth;

    sb_w = mb_h = 0;
    r->h->window_vt_x = r->h->window_vt_y = 0;
    if (scrollbar_visible(r)) {
	sb_w = scrollbar_TotalWidth();
	r->szHint.base_width += sb_w;
	if (!(r->Options & Opt_scrollBar_right))
	    r->h->window_vt_x = sb_w;
    }
    if (menubar_visible(r)) {
	mb_h = menuBar_TotalHeight();
	r->szHint.base_height += mb_h;
	r->h->window_vt_y = mb_h;
    }
    r->szHint.width_inc = r->TermWin.fwidth;
    r->szHint.height_inc = r->TermWin.fheight;
    r->szHint.min_width = r->szHint.base_width + r->szHint.width_inc;
    r->szHint.min_height = r->szHint.base_height + r->szHint.height_inc;

    if (width && width - r->szHint.base_width < max_width) {
	r->szHint.width = width;
	r->TermWin.width = width - r->szHint.base_width;
    } else {
	MIN_IT(r->TermWin.width, max_width);
	r->szHint.width = r->szHint.base_width + r->TermWin.width;
    }
    if (height && height - r->szHint.base_height < max_height) {
	r->szHint.height = height;
	r->TermWin.height = height - r->szHint.base_height;
    } else {
	MIN_IT(r->TermWin.height, max_height);
	r->szHint.height = r->szHint.base_height + r->TermWin.height;
    }
    if (scrollbar_visible(r) && (r->Options & Opt_scrollBar_right))
	r->h->window_sb_x = r->szHint.width - sb_w;

    if (recalc_x)
	r->szHint.x += (DisplayWidth(r->Xdisplay, Xscreen)
			- r->szHint.width - 2 * r->TermWin.ext_bwidth);
    if (recalc_y)
	r->szHint.y += (DisplayHeight(r->Xdisplay, Xscreen)
			- r->szHint.height - 2 * r->TermWin.ext_bwidth);

    r->TermWin.ncol = r->TermWin.width / r->TermWin.fwidth;
    r->TermWin.nrow = r->TermWin.height / r->TermWin.fheight;
    return;
}

/*----------------------------------------------------------------------*/
/*
 * Tell the teletype handler what size the window is.
 * Called after a window size change.
 */
/* EXTPROTO */
void
rxvt_tt_winsize(int fd, unsigned short col, unsigned short row)
{
    struct winsize  ws;

    if (fd < 0)
	return;
    ws.ws_col = col;
    ws.ws_row = row;
    ws.ws_xpixel = ws.ws_ypixel = 0;
    ioctl(fd, TIOCSWINSZ, &ws);
}

/*----------------------------------------------------------------------*/
/* rxvt_change_font() - Switch to a new font */
/*
 * init = 1   - initialize
 *
 * fontname == FONT_UP  - switch to bigger font
 * fontname == FONT_DN  - switch to smaller font
 */
/* EXTPROTO */
void
rxvt_change_font(rxvt_t *r, int init, const char *fontname)
{
    const char     *msg = "can't load font \"%s\"";
    int             fh, fw, recheckfonts;
    int             pf;
    int             idx = 0;	/* index into r->h->rs[Rs_font] */
    XFontStruct    *xfont;
#ifdef MULTICHAR_SET
    int             i;
    char           *c, *enc;
    char            tmpbuf[64];
#endif

#define IDX2FNUM(i)	((i) == 0 ? FONT0_IDX : ((i) <= FONT0_IDX ? ((i)-1) : (i)))
#define FNUM2IDX(f)	((f) == FONT0_IDX ? 0 : ((f) < FONT0_IDX ? ((f)+1) : (f)))

    if (!init) {
	pf = r->h->fnum;
	switch (fontname[0]) {
	case '\0':
	    r->h->fnum = FONT0_IDX;
	    fontname = NULL;
	    break;

	    /* special (internal) prefix for font commands */
	case FONT_CMD:
	    idx = atoi(fontname + 1);
	    switch (fontname[1]) {
	    case '+':		/* corresponds to FONT_UP */
		rxvt_font_up_down(r, (idx ? idx : 1), 1);
		break;

	    case '-':		/* corresponds to FONT_DN */
		rxvt_font_up_down(r, (idx ? idx : 1), -1);
		break;

	    default:
		if (fontname[1] != '\0' && !isdigit(fontname[1]))
		    return;
		if (idx < 0 || idx >= MAX_NFONTS)
		    return;
		r->h->fnum = IDX2FNUM(idx);
		break;
	    }
	    fontname = NULL;
	    break;

	default:
	    if (fontname == NULL)
		return;
	    else
	    	/* search for existing fontname */
		for (idx = 0; idx < MAX_NFONTS; idx++)
		    if (r->h->rs[Rs_font + idx] == NULL) continue;
		    if (!STRCMP(r->h->rs[Rs_font + idx], fontname)) {
			r->h->fnum = IDX2FNUM(idx);
			fontname = NULL;
			break;
		    }
	    break;
	}
	/* re-position around the normal font */
	idx = FNUM2IDX(r->h->fnum);

	if (pf == r->h->fnum)
	    return;		/* no change */

	if (fontname != NULL) {
	    char           *name;

	    xfont = XLoadQueryFont(r->Xdisplay, fontname);
	    if (!xfont)
		return;

	    name = rxvt_malloc(STRLEN(fontname + 1) * sizeof(char));

	    if (name == NULL) {
		XFreeFont(r->Xdisplay, xfont);
		return;
	    }
	    STRCPY(name, fontname);
	    if (r->h->newfont[idx] != NULL)
		free(r->h->newfont[idx]);
	    r->h->newfont[idx] = name;
	    r->h->rs[Rs_font + idx] = r->h->newfont[idx];
	}
    }
    if (r->TermWin.font)
	XFreeFont(r->Xdisplay, r->TermWin.font);

/* load font or substitute */
    xfont = XLoadQueryFont(r->Xdisplay, r->h->rs[Rs_font + idx]);
    if (!xfont) {
	rxvt_print_error(msg, r->h->rs[Rs_font + idx]);
	r->h->rs[Rs_font + idx] = "fixed";
	xfont = XLoadQueryFont(r->Xdisplay, "fixed");
	if (!xfont) {
	    rxvt_print_error(msg, "fixed");
	    goto Abort;
	}
    }
    r->TermWin.font = xfont;

#ifndef NO_BOLDFONT
/* fail silently */
    if (init && r->h->rs[Rs_boldFont] != NULL)
	r->TermWin.boldFont_loaded = XLoadQueryFont(r->Xdisplay,
						    r->h->rs[Rs_boldFont]);
#endif

/* alter existing GC */
    if (!init)
	XSetFont(r->Xdisplay, r->TermWin.gc, r->TermWin.font->fid);

/* set the sizes */
    fw = rxvt_get_fontwidest(r->TermWin.font);
    fh = r->TermWin.font->ascent + r->TermWin.font->descent
	 + r->TermWin.lineSpace;
    if (fw == r->TermWin.font->min_bounds.width)
	r->TermWin.propfont &= ~PROPFONT_NORMAL;/* Mono-spaced font */
    else
	r->TermWin.propfont |= PROPFONT_NORMAL;	/* Proportional font */
    recheckfonts = !(fw == r->TermWin.fwidth && fh == r->TermWin.fheight);
    r->TermWin.fwidth = fw;
    r->TermWin.fheight = fh;
    r->TermWin.fascent = r->TermWin.font->ascent;
    r->TermWin.fdescent = r->TermWin.font->descent;
    r->TermWin.fontdiff = 0;

/* check that size of boldFont is okay */
#ifndef NO_BOLDFONT
    if (recheckfonts) {
	r->TermWin.boldFont = NULL;
	if (r->TermWin.boldFont_loaded != NULL) {
	    fw = rxvt_get_fontwidest(r->TermWin.boldFont_loaded);
	    fh = r->TermWin.boldFont_loaded->ascent
		 + r->TermWin.boldFont_loaded->descent;
	    if (fw <= r->TermWin.fwidth && fh <= r->TermWin.fheight)
		r->TermWin.boldFont = r->TermWin.boldFont_loaded;
	    if (fw == r->TermWin.fwidth /* && fh == r->TermWin.fheight */ )
		r->TermWin.propfont &= ~PROPFONT_BOLD;
	    else
		r->TermWin.propfont |= PROPFONT_BOLD;
	}
    }
#endif				/* NO_BOLDFONT */

#ifdef MULTICHAR_SET
    if (r->TermWin.mfont)
	XFreeFont(r->Xdisplay, r->TermWin.mfont);

    xfont = NULL;
/* load font or substitute */
    if (r->h->rs[Rs_mfont + idx] == NULL
	|| (xfont = XLoadQueryFont(r->Xdisplay,
				   r->h->rs[Rs_mfont + idx])) == NULL) {
	/* TODO: this is now mainly handled in rxvt_set_defaultfont() */
	i = 0;
	c = enc = "";
	switch (r->encoding_method) {
	case GB:
	    c = "-*-r-*-%.2d-*-gb2312.1980-0";
	    enc = "GB";
	    break;
	case BIG5:
	    c = "-*-r-*-%.2d-*-big5-0";
	    enc = "BIG5";
	    break;
	case EUCJ:
	case SJIS:
	    c = "-*-%.2d-*-jisx0208*-*";
	    enc = "EUCJ/SJIS";
	    break;
	case EUCKR:
	    c = "-*-%.2d-*-ksc5601*-*";
	    enc = "KR";
	    break;
	default:
	    i = fh;		/* jump past next two sections */
	    break;
	}
	for (; i < fh / 2; i++) {
	    sprintf(tmpbuf, c, fh - i);
	    xfont = XLoadQueryFont(r->Xdisplay, tmpbuf);
	    if (xfont) {
		r->h->rs[Rs_mfont + idx] = rxvt_malloc(STRLEN(tmpbuf) + 1);
		STRCPY(r->h->rs[Rs_mfont + idx], tmpbuf);
		break;
	    }
	}
	if (xfont == NULL && i != fh)
	    rxvt_print_error("no similar multichar font: encoding %s; size %d",
			     enc, fh);
    }
    r->TermWin.mfont = xfont;

    if (recheckfonts)
	if (r->TermWin.mfont != NULL) {
	    fw = rxvt_get_fontwidest(r->TermWin.mfont);
	    if (r->TermWin.mfont->ascent > r->TermWin.fascent) {
		r->TermWin.fascent = r->TermWin.mfont->ascent;
		r->TermWin.fontdiff = 1;
	    }
	    if (r->TermWin.mfont->descent > r->TermWin.fdescent) {
		r->TermWin.fdescent = r->TermWin.mfont->descent;
		r->TermWin.fontdiff = 1;
	    }
	    if (r->TermWin.fontdiff)
		r->TermWin.fheight = r->TermWin.fascent + r->TermWin.fdescent;
	    if (fw == (r->TermWin.fwidth * 2) /* && fh==r->TermWin.fheight */ )
		r->TermWin.propfont &= ~PROPFONT_MULTI;
	    else
		r->TermWin.propfont |= PROPFONT_MULTI;
	}
#endif				/* MULTICHAR_SET */

    /* Fontset setting is only valid when xlocale initialized. Since
     * rxvt_init_xlocale() is called after here, so we don't set fontset when
     * initialization, but let it set by rxvt_init_xlocale() */
    if (init)
	rxvt_setTermFontSet(r, -1);
    else
	rxvt_setTermFontSet(r, idx);

    rxvt_set_colorfgbg(r);

    if (!init) {
	rxvt_resize_all_windows(r, 0, 0, 0);
	rxvt_scr_touch(r, True);
    }
    return;
  Abort:
    rxvt_print_error("aborting");	/* fatal problem */
    exit(EXIT_FAILURE);
}

/* INTPROTO */
void
rxvt_font_up_down(rxvt_t *r, int n, int direction)
{
    const char     *p;
    int             initial, j;

    for (j = 0; j < n; j++) {
	initial = r->h->fnum;
	for (;;) {
	    r->h->fnum += direction;
	    if (r->h->fnum == MAX_NFONTS || r->h->fnum == -1) {
		r->h->fnum = initial;
		return;
	    }
	    p = r->h->rs[Rs_font + FNUM2IDX(r->h->fnum)];
	    if (p != NULL && STRLEN(p) > 1)
		break;
	}
    }
}
#undef IDX2FNUM
#undef FNUM2IDX

#ifdef STRICT_FONT_CHECKING
/* INTPROTO */
int
rxvt_get_fontwidest(XFontStruct *f)
{
    int             i, cw, fw = 0;

    if (f->min_bounds.width == f->max_bounds.width)
	return f->min_bounds.width;
    if (f->per_char == NULL)
	return f->max_bounds.width;
    for (i = f->max_char_or_byte2 - f->min_char_or_byte2; --i >= 0;) {
	cw = f->per_char[i].width;
	MAX_IT(fw, cw);
    }
    return fw;
}
#endif

/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* xterm sequences - title, iconName, color (exptl) */
/* EXTPROTO */
void
rxvt_set_title(rxvt_t *r, const char *str)
{
#ifndef SMART_WINDOW_TITLE
    XStoreName(r->Xdisplay, r->TermWin.parent[0], str);
#else
    char           *name;

    if (XFetchName(r->Xdisplay, r->TermWin.parent[0], &name) == 0)
	name = NULL;
    if (name == NULL || STRCMP(name, str))
	XStoreName(r->Xdisplay, r->TermWin.parent[0], str);
    if (name)
	XFree(name);
#endif
}

/* EXTPROTO */
void
rxvt_set_iconName(rxvt_t *r, const char *str)
{
#ifndef SMART_WINDOW_TITLE
    XSetIconName(r->Xdisplay, r->TermWin.parent[0], str);
#else
    char           *name;

    if (XGetIconName(r->Xdisplay, r->TermWin.parent[0], &name))
	name = NULL;
    if (name == NULL || STRCMP(name, str))
	XSetIconName(r->Xdisplay, r->TermWin.parent[0], str);
    if (name)
	XFree(name);
#endif
}

#ifdef XTERM_COLOR_CHANGE
/* EXTPROTO */
void
rxvt_set_window_color(rxvt_t *r, int idx, const char *color)
{
    XColor          xcol;
    int             i;

    if (color == NULL || *color == '\0')
	return;

/* handle color aliases */
    if (isdigit(*color)) {
	i = atoi(color);
	if (i >= 8 && i <= 15) {	/* bright colors */
	    i -= 8;
# ifndef NO_BRIGHTCOLOR
	    r->PixColors[idx] = r->PixColors[minBrightCOLOR + i];
	    SET_PIXCOLOR(r->h, idx);
	    goto Done;
# endif
	}
	if (i >= 0 && i <= 7) {	/* normal colors */
	    r->PixColors[idx] = r->PixColors[minCOLOR + i];
	    SET_PIXCOLOR(r->h, idx);
	    goto Done;
	}
    }
    if (!rxvt_rXParseAllocColor(r, &xcol, color))
	return;
/* XStoreColor (r->Xdisplay, XCMAP, XColor*); */

/*
 * FIXME: should free colors here, but no idea how to do it so instead,
 * so just keep gobbling up the colormap
 */
# if 0
    for (i = Color_Black; i <= Color_White; i++)
	if (r->PixColors[idx] == r->PixColors[i])
	    break;
    if (i > Color_White) {
	/* fprintf (stderr, "XFreeColors: r->PixColors [%d] = %lu\n", idx, r->PixColors [idx]); */
	XFreeColors(r->Xdisplay, XCMAP, (r->PixColors + idx), 1,
		    DisplayPlanes(r->Xdisplay, Xscreen));
    }
# endif

    r->PixColors[idx] = xcol.pixel;
    SET_PIXCOLOR(r->h, idx);

/* XSetWindowAttributes attr; */
/* Cursor cursor; */
  Done:
    if (idx == Color_bg && !(r->Options & Opt_transparent))
	XSetWindowBackground(r->Xdisplay, r->TermWin.vt,
			     r->PixColors[Color_bg]);

/* handle Color_BD, scrollbar background, etc. */

    rxvt_set_colorfgbg(r);
    rxvt_recolour_cursor(r);
/* the only reasonable way to enforce a clean update */
    rxvt_scr_touch(r, False);
}

#else
# define rxvt_set_window_color(r, idx,color)	((void)0)
#endif				/* XTERM_COLOR_CHANGE */

/* EXTPROTO */
void
rxvt_recolour_cursor(rxvt_t *r)
{
    XColor          xcol[2];

    xcol[0].pixel = r->PixColors[Color_pointer];
    xcol[1].pixel = r->PixColors[Color_bg];
    XQueryColors(r->Xdisplay, XCMAP, xcol, 2);
    XRecolorCursor(r->Xdisplay, r->TermWin_cursor, &(xcol[0]), &(xcol[1]));
}

/*----------------------------------------------------------------------*/
/*
 * find if fg/bg matches any of the normal (low-intensity) colors
 */
/* INTPROTO */
void
rxvt_set_colorfgbg(rxvt_t *r)
{
    unsigned int    i;
    const char     *xpmb = "\0";
    char            fstr[sizeof("default") + 1], bstr[sizeof("default") + 1];

    r->h->env_colorfgbg = rxvt_malloc(sizeof("COLORFGBG=default;default;bg")
				      + 1);
    STRCPY(fstr, "default");
    STRCPY(bstr, "default");
    for (i = Color_Black; i <= Color_White; i++)
	if (r->PixColors[Color_fg] == r->PixColors[i]) {
	    sprintf(fstr, "%d", (i - Color_Black));
	    break;
	}
    for (i = Color_Black; i <= Color_White; i++)
	if (r->PixColors[Color_bg] == r->PixColors[i]) {
	    sprintf(bstr, "%d", (i - Color_Black));
#ifdef XPM_BACKGROUND
	    xpmb = "default;";
#endif
	    break;
	}
    sprintf(r->h->env_colorfgbg, "COLORFGBG=%s;%s%s", fstr, xpmb, bstr);
    putenv(r->h->env_colorfgbg);

#ifndef NO_BRIGHTCOLOR
    r->h->colorfgbg = DEFAULT_RSTYLE;
    for (i = minCOLOR; i <= maxCOLOR; i++) {
	if (r->PixColors[Color_fg] == r->PixColors[i]
# ifndef NO_BOLD_UNDERLINE_REVERSE
	    && r->PixColors[Color_fg] == r->PixColors[Color_BD]
# endif				/* ! NO_BOLD_UNDERLINE_REVERSE */
	    /* if we wanted boldFont to have precedence */
# if 0				/* ifndef NO_BOLDFONT */
	    && r->TermWin.boldFont == NULL
# endif				/* NO_BOLDFONT */
	    )
	    r->h->colorfgbg = SET_FGCOLOR(r->h->colorfgbg, i);
	if (r->PixColors[Color_bg] == r->PixColors[i])
	    r->h->colorfgbg = SET_BGCOLOR(r->h->colorfgbg, i);
    }
#endif
}

/*----------------------------------------------------------------------*/
/*
 * Colour determination for low colour displays, routine from
 *     Hans de Goede <hans@highrise.nl>
 */

/* EXTPROTO */
int
rxvt_rXParseAllocColor(rxvt_t *r, XColor *screen_in_out, const char *colour)
{
    int             res = 0;

    if (!XParseColor(r->Xdisplay, XCMAP, colour, screen_in_out))
	rxvt_print_error("can't determine colour: %s", colour);
    else
	res = rxvt_rXAllocColor(r, screen_in_out, colour);
    return res;
}

/* EXTPROTO */
int
rxvt_rXAllocColor(rxvt_t *r, XColor *screen_in_out, const char *colour)
{
    int             res;

    if ((res = XAllocColor(r->Xdisplay, XCMAP, screen_in_out)))
	return res;

    /* try again with closest match */
    if (XDEPTH >= 4 && XDEPTH <= 8) {
	int             i, numcol;
	int             best_pixel = 0;
	unsigned long   best_diff, diff;
	XColor         *colors;

#define rSQR(x)		((x)*(x))

	best_diff = 0;
	numcol = 0x01 << XDEPTH;
	if ((colors = rxvt_malloc(numcol * sizeof(XColor)))) {
	    for (i = 0; i < numcol; i++)
		colors[i].pixel = i;

	    XQueryColors(r->Xdisplay, XCMAP, colors, numcol);
	    for (i = 0; i < numcol; i++) {
		diff = rSQR(screen_in_out->red - colors[i].red)
		       + rSQR(screen_in_out->green - colors[i].green)
		       + rSQR(screen_in_out->blue - colors[i].blue);
		if (i == 0 || diff < best_diff) {
		    best_pixel = colors[i].pixel;
		    best_diff = diff;
		}
	    }
	    *screen_in_out = colors[best_pixel];
	    free(colors);
	    res = XAllocColor(r->Xdisplay, XCMAP, screen_in_out);
	}
    }
    if (res == 0)
	rxvt_print_error("can't allocate colour: %s", colour);
    return res;
}

/* -------------------------------------------------------------------- *
 * -                         WINDOW RESIZING                          - *
 * -------------------------------------------------------------------- */
/* EXTPROTO */
void
rxvt_resize_all_windows(rxvt_t *r, unsigned int width, unsigned int height, int ignoreparent)
{
    int             fix_screen;
#ifdef SMART_RESIZE	
    int             old_width = r->szHint.width,
		    old_height = r->szHint.height;
#endif

    rxvt_window_calc(r, width, height);
    XSetWMNormalHints(r->Xdisplay, r->TermWin.parent[0], &r->szHint);
    if (!ignoreparent) {
#ifdef SMART_RESIZE	
/*
 * resize by Marius Gedminas <marius.gedminas@uosis.mif.vu.lt>
 * reposition window on resize depending on placement on screen
 */
	int             x, y, x1, y1;
	int             dx, dy;
	unsigned int    unused_w1, unused_h1, unused_b1, unused_d1;
	Window          unused_cr;

	XTranslateCoordinates(r->Xdisplay, r->TermWin.parent[0], Xroot,
			      0, 0, &x, &y, &unused_cr);
	XGetGeometry(r->Xdisplay, r->TermWin.parent[0], &unused_cr, &x1, &y1,
		     &unused_w1, &unused_h1, &unused_b1, &unused_d1);
	/*
	 * if Xroot isn't the parent window, a WM will probably have offset
	 * our position for handles and decorations.  Counter it
	 */
	if (x1 != x || y1 != y) {
	    x -= x1;
	    y -= y1;
	}

	x1 = (DisplayWidth(r->Xdisplay, Xscreen) - old_width) / 2;
	y1 = (DisplayHeight(r->Xdisplay, Xscreen) - old_height) / 2;
	dx = old_width - r->szHint.width;
	dy = old_height - r->szHint.height;

	/* Check position of the center of the window */
	if (x < x1)		/* left half */
	    dx = 0;
	else if (x == x1)	/* exact center */
	    dx /= 2;
	if (y < y1)		/* top half */
	    dy = 0;
	else if (y == y1)	/* exact center */
	    dy /= 2;

	XMoveResizeWindow(r->Xdisplay, r->TermWin.parent[0], x + dx, y + dy,
			  r->szHint.width, r->szHint.height);
#else
	XResizeWindow(r->Xdisplay, r->TermWin.parent[0], r->szHint.width,
		      r->szHint.height);
#endif
    }

    fix_screen = (r->TermWin.ncol != r->h->prev_ncol
		  || r->TermWin.nrow != r->h->prev_nrow);
    if (fix_screen || width != r->h->old_width || height != r->h->old_height) {
	if (scrollbar_visible(r)) {
	    XMoveResizeWindow(r->Xdisplay, r->scrollBar.win, r->h->window_sb_x,
			      0, scrollbar_TotalWidth(), r->szHint.height);
	    rxvt_Resize_scrollBar(r);
	}
	if (menubar_visible(r))
	    XMoveResizeWindow(r->Xdisplay, r->menuBar.win, r->h->window_vt_x,
			      0, TermWin_TotalWidth(), menuBar_TotalHeight());
	XMoveResizeWindow(r->Xdisplay, r->TermWin.vt, r->h->window_vt_x,
			  r->h->window_vt_y, TermWin_TotalWidth(),
			  TermWin_TotalHeight());
#ifdef RXVT_GRAPHICS
	if (r->h->old_height)
	    rxvt_Gr_Resize(r, r->h->old_width - r->szHint.base_width,
			   r->h->old_height - r->szHint.base_height);
#endif
	rxvt_scr_clear(r);
	rxvt_resize_pixmap(r);
    }

    if (fix_screen || r->h->old_height == 0) {
	int             curr_screen = -1;
	u_int16_t       old_ncol = r->h->prev_ncol;

	/* scr_reset only works on the primary screen */
	if (r->h->old_height) 	/* this is not the first time through */
	    curr_screen = rxvt_scr_change_screen(r, PRIMARY);
	rxvt_scr_reset(r);
	if (curr_screen >= 0) {	/* this is not the first time through */
	    rxvt_scr_change_screen(r, curr_screen);
	    rxvt_selection_check(r, (old_ncol != r->TermWin.ncol ? 4 : 0));
	}
    }

    r->h->old_width = r->szHint.width;
    r->h->old_height = r->szHint.height;

#ifdef USE_XIM
    rxvt_IMSetStatusPosition(r);
#endif
}

/*
 * Set the width/height of the vt window in characters.  Units are pixels.
 * good for toggling 80/132 columns
 */
/* EXTPROTO */
void
rxvt_set_widthheight(rxvt_t *r, unsigned int width, unsigned int height)
{
    XWindowAttributes wattr;

    if (width == 0 || height == 0) {
	XGetWindowAttributes(r->Xdisplay, Xroot, &wattr);
	if (width == 0)
	    width = wattr.width - r->szHint.base_width;
	if (height == 0)
	    height = wattr.height - r->szHint.base_height;
    }
    if (width != r->TermWin.width || height != r->TermWin.height) {
	width += r->szHint.base_width;
	height += r->szHint.base_height;
	rxvt_resize_all_windows(r, width, height, 0);
    }
}

/* -------------------------------------------------------------------- *
 * -                      X INPUT METHOD ROUTINES                     - *
 * -------------------------------------------------------------------- */
#ifdef USE_XIM
/* INTPROTO */
void
rxvt_setSize(rxvt_t *r, XRectangle *size)
{
    size->x = r->TermWin.int_bwidth;
    size->y = r->TermWin.int_bwidth;
    size->width = Width2Pixel(r->TermWin.ncol);
    size->height = Height2Pixel(r->TermWin.nrow);
}

/* INTPROTO */
void
rxvt_setColor(rxvt_t *r, unsigned long *fg, unsigned long *bg)
{
    *fg = r->PixColors[Color_fg];
    *bg = r->PixColors[Color_bg];
}

/* Checking whether input method is running. */
/* INTPROTO */
Bool
rxvt_IMisRunning(rxvt_t *r)
{
    char           *p;
    Atom            atom;
    Window          win;
    char            server[IMBUFSIZ];

    /* get current locale modifier */
    if ((p = XSetLocaleModifiers(NULL)) != NULL) {
	STRCPY(server, "@server=");
	STRNCAT(server, &(p[4]), IMBUFSIZ - 9);	/* skip "@im=" */
	if ((p = STRCHR(server + 1, '@')) != NULL)	/* first one only */
	    *p = '\0';

	atom = XInternAtom(r->Xdisplay, server, False);
	win = XGetSelectionOwner(r->Xdisplay, atom);
	if (win != None)
	    return True;
    }
    return False;
}

/* EXTPROTO */
void
rxvt_IMSendSpot(rxvt_t *r)
{
    XPoint          spot;
    XVaNestedList   preedit_attr;

    if (r->h->Input_Context == NULL
	|| !r->TermWin.focus
	|| !(r->h->input_style & XIMPreeditPosition)
	|| !(r->h->event_type == KeyPress
	     || r->h->event_type == Expose
	     || r->h->event_type == NoExpose
	     || r->h->event_type == SelectionNotify
	     || r->h->event_type == ButtonRelease
	     || r->h->event_type == FocusIn)
	|| !rxvt_IMisRunning(r))
	return;

    rxvt_setPosition(r, &spot);

    preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL);
    XSetICValues(r->h->Input_Context, XNPreeditAttributes, preedit_attr, NULL);
    XFree(preedit_attr);
}

/* EXTPROTO */
void
rxvt_setTermFontSet(rxvt_t *r, int idx)
{
    char           *string;
    long            length;
    XFontSet        prev_fontset;
    int             success = 0;

    if (idx < 0 || idx >= MAX_NFONTS)
	return;
    D_MAIN((stderr, "rxvt_setTermFontSet()"));
    prev_fontset = r->TermWin.fontset;
    r->TermWin.fontset = NULL;

    length = 0;
    if (r->h->rs[Rs_font + idx])
	length += STRLEN(r->h->rs[Rs_font + idx]) + 1;
# ifdef MULTICHAR_SET
    if (r->h->rs[Rs_mfont + idx])
	length += STRLEN(r->h->rs[Rs_mfont + idx]) + 1;
# endif
    if (length == 0 || (string = rxvt_malloc(length + 1)) == NULL)
	r->TermWin.fontset = NULL;
    else {
	int             missing_charsetcount;
	char          **missing_charsetlist, *def_string;

	string[0] = '\0';
	if (r->h->rs[Rs_font + idx]) {
	    STRCAT(string, r->h->rs[Rs_font + idx]);
	    STRCAT(string, ",");
	}
# ifdef MULTICHAR_SET
	if (r->h->rs[Rs_mfont + idx]) {
	    STRCAT(string, r->h->rs[Rs_mfont + idx]);
	    STRCAT(string, ",");
	}
# endif
	string[STRLEN(string) - 1] = '\0';
	r->TermWin.fontset = XCreateFontSet(r->Xdisplay, string,
					    &missing_charsetlist,
					    &missing_charsetcount,
					    &def_string);
	free(string);
	if (r->TermWin.fontset != NULL)
	    success = 1;
    }

    if (success) {
	if (prev_fontset != NULL)
	    XFreeFontSet(r->Xdisplay, prev_fontset);
    } else
	r->TermWin.fontset = prev_fontset;
}

/* INTPROTO */
void
rxvt_setPreeditArea(rxvt_t *r, XRectangle *preedit_rect, XRectangle *status_rect, XRectangle *needed_rect)
{
    int             mbh, vtx = 0;

    if (scrollbar_visible(r) && !(r->Options & Opt_scrollBar_right))
	vtx = scrollbar_TotalWidth();
    mbh = menubar_visible(r) ? menuBar_TotalHeight() : 0;
    mbh -= r->TermWin.lineSpace;

    preedit_rect->x = needed_rect->width + vtx;
    preedit_rect->y = Height2Pixel(r->TermWin.nrow - 1) + mbh;

    preedit_rect->width = Width2Pixel(r->TermWin.ncol + 1) - needed_rect->width
	    		  + vtx;
    preedit_rect->height = Height2Pixel(1);

    status_rect->x = vtx;
    status_rect->y = Height2Pixel(r->TermWin.nrow - 1) + mbh;

    status_rect->width = needed_rect->width ? needed_rect->width
					    : Width2Pixel(r->TermWin.ncol + 1);
    status_rect->height = Height2Pixel(1);
}

/* ARGSUSED */
/* INTPROTO */
void
rxvt_IMDestroyCallback(XIM xim __attribute__((unused)), XPointer client_data __attribute__((unused)), XPointer call_data __attribute__((unused)))
{
    rxvt_t         *r = rxvt_get_r();

    r->h->Input_Context = NULL;
    /* To avoid Segmentation Fault in C locale: Solaris only? */
    if (STRCMP(r->h->locale, "C"))
	XRegisterIMInstantiateCallback(r->Xdisplay, NULL, NULL, NULL,
				       rxvt_IMInstantiateCallback, NULL);
}

/*
 * X manual pages and include files don't match on some systems:
 * some think this is an XIDProc and others an XIMProc so we can't
 * use the first argument - need to update this to be nice for
 * both types via some sort of configure detection
 */
/* ARGSUSED */
/* EXTPROTO */
void
rxvt_IMInstantiateCallback(Display *unused __attribute__((unused)), XPointer client_data __attribute__((unused)), XPointer call_data __attribute__((unused)))
{
    int             i, found, had_im;
    const char     *p;
    char          **s;
    rxvt_t         *r = rxvt_get_r();
    char            buf[IMBUFSIZ];

    D_MAIN((stderr, "rxvt_IMInstantiateCallback()"));
    if (r->h->Input_Context)
	return;

    found = had_im = 0;
    p = r->h->rs[Rs_inputMethod];
    if (p && *p) {
	had_im = 1;
	s = rxvt_splitcommastring(p);
	for (i = 0; s[i]; i++) {
	    if (*s[i]) {
		STRCPY(buf, "@im=");
		STRNCAT(buf, s[i], IMBUFSIZ - 5);
		if ((p = XSetLocaleModifiers(buf)) != NULL && *p
		    && (rxvt_IM_get_IC(r) == True)) {
		    found = 1;
		    break;
		}
	    }
	}
	for (i = 0; s[i]; i++)
	    free(s[i]);
	free(s);
    }
    if (found)
	return;

/* try with XMODIFIERS env. var. */
    if ((p = XSetLocaleModifiers("")) != NULL && *p) {
	rxvt_IM_get_IC(r);
	return;
    }

/* try with no modifiers base IF the user didn't specify an IM */
    if (!had_im && (p = XSetLocaleModifiers("@im=none")) != NULL && *p
	&& rxvt_IM_get_IC(r) == True)
	return;
}

/*
 * Try to open a XIM with the current modifiers, then see if we can
 * open a suitable preedit type
 */
/* INTPROTO */
Bool
rxvt_IM_get_IC(rxvt_t *r)
{
    int             i, j, found;
    XIM             xim;
    XPoint          spot;
    XRectangle      rect, status_rect, needed_rect;
    unsigned long   fg, bg;
    const char     *p;
    char          **s;
    XIMStyles      *xim_styles;
    XVaNestedList   preedit_attr, status_attr;
    XIMCallback     ximcallback;
    struct rxvt_hidden *h = r->h;

    D_MAIN((stderr, "rxvt_IM_get_IC()"));
    xim = XOpenIM(r->Xdisplay, NULL, NULL, NULL);
    if (xim == NULL)
	return False;

    xim_styles = NULL;
    if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL)
	|| !xim_styles || !xim_styles->count_styles) {
	XCloseIM(xim);
	return False;
    }

    p = h->rs[Rs_preeditType] ? h->rs[Rs_preeditType]
			      : "OverTheSpot,OffTheSpot,Root";
    s = rxvt_splitcommastring(p);
    for (i = found = 0; !found && s[i]; i++) {
	if (!STRCMP(s[i], "OverTheSpot"))
	    h->input_style = (XIMPreeditPosition | XIMStatusNothing);
	else if (!STRCMP(s[i], "OffTheSpot"))
	    h->input_style = (XIMPreeditArea | XIMStatusArea);
	else if (!STRCMP(s[i], "Root"))
	    h->input_style = (XIMPreeditNothing | XIMStatusNothing);

	for (j = 0; j < xim_styles->count_styles; j++)
	    if (h->input_style == xim_styles->supported_styles[j]) {
		found = 1;
		break;
	    }
    }
    for (i = 0; s[i]; i++)
	free(s[i]);
    free(s);
    XFree(xim_styles);

    if (!found) {
	XCloseIM(xim);
	return False;
    }

    ximcallback.callback = rxvt_IMDestroyCallback;

    /* XXX: not sure why we need this (as well as IC one below) */
    XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);

    preedit_attr = status_attr = NULL;

    if (h->input_style & XIMPreeditPosition) {
	rxvt_setSize(r, &rect);
	rxvt_setPosition(r, &spot);
	rxvt_setColor(r, &fg, &bg);

	preedit_attr = XVaCreateNestedList(0, XNArea, &rect,
					   XNSpotLocation, &spot,
					   XNForeground, fg,
					   XNBackground, bg,
					   XNFontSet, r->TermWin.fontset,
					   NULL);
    } else if (h->input_style & XIMPreeditArea) {
	rxvt_setColor(r, &fg, &bg);

	/*
	 * The necessary width of preedit area is unknown
	 * until create input context.
	 */
	needed_rect.width = 0;

	rxvt_setPreeditArea(r, &rect, &status_rect, &needed_rect);

	preedit_attr = XVaCreateNestedList(0, XNArea, &rect,
					   XNForeground, fg,
					   XNBackground, bg,
					   XNFontSet, r->TermWin.fontset,
					   NULL);
	status_attr = XVaCreateNestedList(0, XNArea, &status_rect,
					  XNForeground, fg,
					  XNBackground, bg,
					  XNFontSet, r->TermWin.fontset, NULL);
    }
    h->Input_Context = XCreateIC(xim, XNInputStyle, h->input_style,
				 XNClientWindow, r->TermWin.parent[0],
				 XNFocusWindow, r->TermWin.parent[0],
				 XNDestroyCallback, &ximcallback,
				 preedit_attr ? XNPreeditAttributes : NULL,
				 preedit_attr,
				 status_attr ? XNStatusAttributes : NULL,
				 status_attr, NULL);
    if (preedit_attr)
	XFree(preedit_attr);
    if (status_attr)
	XFree(status_attr);
    if (h->Input_Context == NULL) {
	rxvt_print_error("failed to create input context");
	XCloseIM(xim);
	return False;
    }
    if (h->input_style & XIMPreeditArea)
	rxvt_IMSetStatusPosition(r);
    D_MAIN((stderr, "rxvt_IM_get_IC() - successful connection"));
    return True;
}

/* EXTPROTO */
void
rxvt_IMSetStatusPosition(rxvt_t *r)
{
    XRectangle      preedit_rect, status_rect, *needed_rect;
    XVaNestedList   preedit_attr, status_attr;

    if (r->h->Input_Context == NULL
	|| !r->TermWin.focus
	|| !(r->h->input_style & XIMPreeditArea)
	|| !rxvt_IMisRunning(r))
	return;

    /* Getting the necessary width of preedit area */
    status_attr = XVaCreateNestedList(0, XNAreaNeeded, &needed_rect, NULL);
    XGetICValues(r->h->Input_Context, XNStatusAttributes, status_attr, NULL);
    XFree(status_attr);

    rxvt_setPreeditArea(r, &preedit_rect, &status_rect, needed_rect);

    preedit_attr = XVaCreateNestedList(0, XNArea, &preedit_rect, NULL);
    status_attr = XVaCreateNestedList(0, XNArea, &status_rect, NULL);

    XSetICValues(r->h->Input_Context,
		 XNPreeditAttributes, preedit_attr,
		 XNStatusAttributes, status_attr, NULL);

    XFree(preedit_attr);
    XFree(status_attr);
}
#endif				/* USE_XIM */

/*----------------------------------------------------------------------*/
static rxvt_t  *_rxvt_vars = NULL;

/* EXTPROTO */
rxvt_t         *
rxvt_get_r(void)
{
    return _rxvt_vars;
}
/* INTPROTO */
void
rxvt_set_r(rxvt_t *r)
{
    _rxvt_vars = r;
}

/*----------------------- end-of-file (C source) -----------------------*/