-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.C
1682 lines (1390 loc) · 42.1 KB
/
main.C
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
/*----------------------------------------------------------------------*
* File: main.C
*----------------------------------------------------------------------*
*
* 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
* Copyright (c) 2003-2010 Marc Lehmann <schmorp@schmorp.de>
*
* 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 "init.h"
#include "keyboard.h"
#include "rxvtperl.h"
#include <limits>
#include <cassert>
#include <csignal>
#include <cstring>
#include <termios.h>
#ifdef HAVE_XSETLOCALE
# define X_LOCALE
# include <X11/Xlocale.h>
#else
# ifdef HAVE_SETLOCALE
# include <clocale>
# endif
#endif
struct termios rxvt_term::def_tio;
vector<rxvt_term *> rxvt_term::termlist;
// used to tell global functions which terminal instance is "active"
rxvt_t rxvt_current_term;
static char curlocale[128], savelocale[128];
bool
rxvt_set_locale (const char *locale) NOTHROW
{
int size = strlen (locale) + 1;
if (size > sizeof (curlocale))
rxvt_fatal ("locale string too long, aborting.\n");
if (!locale || !memcmp (locale, curlocale, size))
return false;
memcpy (curlocale, locale, size);
setlocale (LC_CTYPE, curlocale);
return true;
}
void
rxvt_push_locale (const char *locale) NOTHROW
{
strcpy (savelocale, curlocale);
rxvt_set_locale (locale);
}
void
rxvt_pop_locale () NOTHROW
{
rxvt_set_locale (savelocale);
}
#if ENABLE_COMBINING
class rxvt_composite_vec rxvt_composite;
text_t rxvt_composite_vec::compose (unicode_t c1, unicode_t c2)
{
compose_char *cc;
// break compose chains, as stupid readline really likes to duplicate
// composing characters for some reason, near the end of a line.
cc = (*this)[c1];
while (cc)
{
if (cc->c2 == c2) return c1;
cc = (*this)[cc->c1];
}
// check to see whether this combination already exists otherwise
for (cc = v.end (); cc-- > v.begin (); )
if (cc->c1 == c1 && cc->c2 == c2)
return COMPOSE_LO + (cc - v.begin ());
// allocate a new combination
if (v.size () == COMPOSE_HI - COMPOSE_LO + 1)
{
static int seen;
if (!seen++)
rxvt_warn ("too many unrepresentable composite characters, try --enable-unicode3\n");
return REPLACEMENT_CHAR;
}
v.push_back (compose_char (c1, c2));
return v.size () - 1 + COMPOSE_LO;
}
int rxvt_composite_vec::expand (unicode_t c, wchar_t *r)
{
compose_char *cc = (*this)[c];
if (!cc)
{
if (r) *r = c;
return 1;
}
int len = expand (cc->c1, r);
if (r) r += len;
if (cc->c2 != NOCHAR)
{
len++;
if (r) *r++ = cc->c2;
}
return len;
}
#endif
rxvt_term::rxvt_term ()
{
#if HAVE_BG_PIXMAP
update_background_ev.set<rxvt_term, &rxvt_term::update_background_cb> (this);
#endif
#ifdef CURSOR_BLINK
cursor_blink_ev.set <rxvt_term, &rxvt_term::cursor_blink_cb> (this); cursor_blink_ev.set (0., CURSOR_BLINK_INTERVAL);
#endif
#ifdef TEXT_BLINK
text_blink_ev.set <rxvt_term, &rxvt_term::text_blink_cb> (this); text_blink_ev.set (0., TEXT_BLINK_INTERVAL);
#endif
#ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
cont_scroll_ev.set <rxvt_term, &rxvt_term::cont_scroll_cb> (this);
#endif
#ifdef SELECTION_SCROLLING
sel_scroll_ev.set <rxvt_term, &rxvt_term::sel_scroll_cb> (this);
#endif
#if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
slip_wheel_ev.set <rxvt_term, &rxvt_term::slip_wheel_cb> (this);
#endif
#if ENABLE_TRANSPARENCY || ENABLE_PERL
rootwin_ev.set <rxvt_term, &rxvt_term::rootwin_cb> (this),
#endif
scrollbar_ev.set <rxvt_term, &rxvt_term::x_cb> (this),
#ifdef USE_XIM
im_ev.set <rxvt_term, &rxvt_term::im_cb> (this),
#endif
#ifdef POINTER_BLANK
pointer_ev.set <rxvt_term, &rxvt_term::pointer_cb> (this);
#endif
#ifndef NO_BELL
bell_ev.set <rxvt_term, &rxvt_term::bell_cb> (this);
#endif
child_ev.set <rxvt_term, &rxvt_term::child_cb> (this);
flush_ev.set <rxvt_term, &rxvt_term::flush_cb> (this);
destroy_ev.set <rxvt_term, &rxvt_term::destroy_cb> (this);
pty_ev.set <rxvt_term, &rxvt_term::pty_cb> (this);
termwin_ev.set <rxvt_term, &rxvt_term::x_cb> (this);
vt_ev.set <rxvt_term, &rxvt_term::x_cb> (this);
cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
termlist.push_back (this);
#ifdef KEYSYM_RESOURCE
keyboard = new keyboard_manager;
#endif
}
// clean up the most important stuff, do *not* call x or free mem etc.
// for use before an emergency exit
void
rxvt_term::emergency_cleanup ()
{
if (cmd_pid)
kill (-cmd_pid, SIGHUP);
pty_ev.stop ();
delete pty; pty = 0;
}
rxvt_term::~rxvt_term ()
{
termlist.erase (find (termlist.begin (), termlist.end(), this));
emergency_cleanup ();
#if ENABLE_STYLES
for (int i = RS_styleCount; --i; )
if (fontset[i] != fontset[0])
delete fontset[i];
#endif
delete fontset[0];
#ifdef HAVE_BG_PIXMAP
bg_destroy ();
#endif
if (display)
{
selection_clear ();
selection_clear (true);
#ifdef USE_XIM
im_destroy ();
#endif
scrollBar.destroy ();
if (gc) XFreeGC (dpy, gc);
delete drawable;
// destroy all windows
if (parent)
XDestroyWindow (dpy, parent);
for (int i = 0; i < TOTAL_COLORS; i++)
if (ISSET_PIXCOLOR (i))
{
pix_colors_focused [i].free (this);
#if OFF_FOCUS_FADING
pix_colors_unfocused [i].free (this);
#endif
}
clear ();
display->flush (); /* ideally .put should do this */
displays.put (display);
}
scr_release ();
/* clear all resources */
for (int i = 0; i < allocated.size (); i++)
free (allocated [i]);
free (selection.text);
free (selection.clip_text);
// TODO: manage env vars in child only(!)
free (env_display);
free (env_term);
free (locale);
free (v_buffer);
delete selection_req;
delete envv;
delete argv;
#ifdef KEYSYM_RESOURCE
delete keyboard;
#endif
#ifndef NO_RESOURCES
XrmDestroyDatabase (option_db);
#endif
}
// child has exited, usually destroys
void
rxvt_term::child_cb (ev::child &w, int status)
{
HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END));
cmd_pid = 0;
if (!option (Opt_hold))
destroy ();
}
void
rxvt_term::destroy ()
{
if (destroy_ev.is_active ())
return;
HOOK_INVOKE ((this, HOOK_DESTROY, DT_END));
#if ENABLE_OVERLAY
scr_overlay_off ();
#endif
if (display)
{
#if USE_XIM
im_ev.stop (display);
#endif
scrollbar_ev.stop (display);
#if ENABLE_TRANSPARENCY || ENABLE_PERL
rootwin_ev.stop (display);
#endif
termwin_ev.stop (display);
vt_ev.stop (display);
}
flush_ev.stop ();
pty_ev.stop ();
#ifdef CURSOR_BLINK
cursor_blink_ev.stop ();
#endif
#ifdef TEXT_BLINK
text_blink_ev.stop ();
#endif
#ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
cont_scroll_ev.stop ();
#endif
#ifdef SELECTION_SCROLLING
sel_scroll_ev.stop ();
#endif
#ifdef POINTER_BLANK
pointer_ev.stop ();
#endif
destroy_ev.start ();
}
void
rxvt_term::destroy_cb (ev::idle &w, int revents)
{
make_current ();
delete this;
}
void
rxvt_term::set_option (uint8_t opt, bool set)
{
if (!opt)
return;
uint8_t mask = 1 << (opt & 7);
uint8_t &val = options [opt >> 3];
val = val & ~mask | (set ? mask : 0);
}
/*----------------------------------------------------------------------*/
/*
* Exit gracefully, clearing the utmp entry and restoring tty attributes
* TODO: if debugging, this should free up any known resources if we can
*/
static XErrorHandler old_xerror_handler;
static void
rxvt_emergency_cleanup ()
{
for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
(*t)->emergency_cleanup ();
}
#if !ENABLE_MINIMAL
static void
print_x_error (Display *dpy, XErrorEvent *event)
{
char buffer[BUFSIZ];
char mesg[BUFSIZ];
char number[32];
const char mtype[] = "XlibMessage";
XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
rxvt_warn ("An X Error occurred, trying to continue after report.\n");
rxvt_warn ("%s: %s\n", mesg, buffer);
XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d", mesg, BUFSIZ);
rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->request_code);
sprintf(number, "%d", event->request_code);
XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
rxvt_warn ("(which is %s)\n", buffer);
if (event->request_code >= 128) {
XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
mesg, BUFSIZ);
rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->minor_code);
}
if ((event->error_code == BadWindow) ||
(event->error_code == BadPixmap) ||
(event->error_code == BadCursor) ||
(event->error_code == BadFont) ||
(event->error_code == BadDrawable) ||
(event->error_code == BadColor) ||
(event->error_code == BadGC) ||
(event->error_code == BadIDChoice) ||
(event->error_code == BadValue) ||
(event->error_code == BadAtom)) {
if (event->error_code == BadValue)
XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
mesg, BUFSIZ);
else if (event->error_code == BadAtom)
XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
mesg, BUFSIZ);
else
XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
mesg, BUFSIZ);
rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->resourceid);
}
XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
mesg, BUFSIZ);
rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->serial);
}
#endif
int
rxvt_xerror_handler (Display *display, XErrorEvent *event)
{
if (GET_R->allowedxerror == -1)
GET_R->allowedxerror = event->error_code;
else
{
// GET_R is most likely not the terminal which caused the error,
// so just output the error and continue
#if ENABLE_MINIMAL
old_xerror_handler (display, event);
#else
print_x_error (display, event);
#endif
}
return 0;
}
int
rxvt_xioerror_handler (Display *display)
{
rxvt_warn ("X connection to '%s' broken, unable to recover, exiting.\n",
DisplayString (display));
rxvt_emergency_cleanup ();
_exit (EXIT_FAILURE);
}
static struct sig_handlers
{
ev::sig sw_term, sw_int;
/*
* Catch a fatal signal and tidy up before quitting
*/
void sig_term (ev::sig &w, int revents);
sig_handlers ()
{
sw_term.set<sig_handlers, &sig_handlers::sig_term> (this);
sw_int .set<sig_handlers, &sig_handlers::sig_term> (this);
}
} sig_handlers;
void
sig_handlers::sig_term (ev::sig &w, int revents)
{
rxvt_emergency_cleanup ();
w.stop ();
kill (getpid (), w.signum);
}
static void
rxvt_get_ttymode (struct termios *tio)
{
if (tcgetattr (STDIN_FILENO, tio) < 0)
memset (tio, 0, sizeof (struct termios));
for (int i = 0; i < NCCS; i++)
tio->c_cc[i] = VDISABLE;
tio->c_cc[VINTR] = CINTR;
tio->c_cc[VQUIT] = CQUIT;
tio->c_cc[VERASE] = CERASE;
#ifdef VERASE2
tio->c_cc[VERASE2] = CERASE2;
#endif
tio->c_cc[VKILL] = CKILL;
tio->c_cc[VEOF] = CEOF;
tio->c_cc[VSTART] = CSTART;
tio->c_cc[VSTOP] = CSTOP;
tio->c_cc[VSUSP] = CSUSP;
# ifdef VDSUSP
tio->c_cc[VDSUSP] = CDSUSP;
# endif
# ifdef VREPRINT
tio->c_cc[VREPRINT] = CRPRNT;
# endif
# ifdef VDISCRD
tio->c_cc[VDISCRD] = CFLUSH;
# endif
# ifdef VWERSE
tio->c_cc[VWERSE] = CWERASE;
# endif
# ifdef VLNEXT
tio->c_cc[VLNEXT] = CLNEXT;
# endif
# ifdef VSTATUS
tio->c_cc[VSTATUS] = CSTATUS;
# endif
# if VMIN != VEOF
tio->c_cc[VMIN] = 1;
# endif
# if VTIME != VEOL
tio->c_cc[VTIME] = 0;
# endif
/* input modes */
tio->c_iflag = (BRKINT | IGNPAR | ICRNL
# ifdef IMAXBEL
| IMAXBEL
# endif
| IXON);
/* output modes */
tio->c_oflag = (OPOST | ONLCR);
/* control modes */
tio->c_cflag = (CS8 | CREAD);
/* local modes */
tio->c_lflag = (ISIG | ICANON | IEXTEN | ECHO
# if defined (ECHOCTL) && defined (ECHOKE)
| ECHOCTL | ECHOKE
# endif
| ECHOE | ECHOK);
}
char **rxvt_environ; // startup environment
void
rxvt_init ()
{
assert (("fontMask must not overlap other RS masks",
0 == (RS_fontMask & (RS_Sel | RS_baseattrMask | RS_customMask | RS_bgMask | RS_fgMask))));
rxvt_get_ttymode (&rxvt_term::def_tio);
// get rid of stdin/stdout as we don't need them, to free resources
dup2 (STDERR_FILENO, STDIN_FILENO);
dup2 (STDERR_FILENO, STDOUT_FILENO);
if (!ev_default_loop ())
rxvt_fatal ("cannot initialise libev (bad value for LIBEV_METHODS?)\n");
rxvt_environ = environ;
signal (SIGHUP, SIG_IGN);
signal (SIGPIPE, SIG_IGN);
sig_handlers.sw_term.start (SIGTERM); ev_unref ();
sig_handlers.sw_int.start (SIGINT); ev_unref ();
old_xerror_handler = XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler);
// TODO: handle this with exceptions and tolerate the memory loss
XSetIOErrorHandler (rxvt_xioerror_handler);
XrmInitialize ();
}
/*----------------------------------------------------------------------*/
/*
* window size/position calculations for XSizeHint and other storage.
* if width/height are non-zero then override calculated width/height
*/
void
rxvt_term::window_calc (unsigned int newwidth, unsigned int newheight)
{
short recalc_x, recalc_y;
int x, y, flags;
unsigned int w, h;
unsigned int max_width, max_height;
szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity;
szHint.win_gravity = NorthWestGravity;
/* szHint.min_aspect.x = szHint.min_aspect.y = 1; */
recalc_x = recalc_y = 0;
flags = 0;
if (!parsed_geometry)
{
parsed_geometry = 1;
if (rs[Rs_geometry])
flags = XParseGeometry (rs[Rs_geometry], &x, &y, &w, &h);
if (flags & WidthValue)
{
if (!w)
rxvt_fatal ("illegal window geometry (width and height must be non-zero), aborting.\n");
ncol = clamp (w, 1, std::numeric_limits<int16_t>::max ());
szHint.flags |= USSize;
}
if (flags & HeightValue)
{
if (!h)
rxvt_fatal ("illegal window geometry (width and height must be non-zero), aborting.\n");
nrow = clamp (h, 1, std::numeric_limits<int16_t>::max ());
szHint.flags |= USSize;
}
if (flags & XValue)
{
szHint.x = x;
szHint.flags |= USPosition;
if (flags & XNegative)
{
recalc_x = 1;
szHint.win_gravity = NorthEastGravity;
}
}
if (flags & YValue)
{
szHint.y = y;
szHint.flags |= USPosition;
if (flags & YNegative)
{
recalc_y = 1;
if (szHint.win_gravity == NorthEastGravity)
szHint.win_gravity = SouthEastGravity;
else
szHint.win_gravity = SouthWestGravity;
}
}
}
/* TODO: BOUNDS */
width = ncol * fwidth;
height = nrow * fheight;
max_width = MAX_COLS * fwidth;
max_height = MAX_ROWS * fheight;
szHint.base_width = szHint.base_height = 2 * int_bwidth;
window_vt_x = window_vt_y = int_bwidth;
if (scrollBar.state)
{
int sb_w = scrollBar.total_width ();
szHint.base_width += sb_w;
if (!option (Opt_scrollBar_right))
window_vt_x += sb_w;
}
szHint.width_inc = fwidth;
szHint.height_inc = fheight;
szHint.min_width = szHint.base_width + szHint.width_inc;
szHint.min_height = szHint.base_height + szHint.height_inc;
if (newwidth && newwidth - szHint.base_width < max_width)
{
szHint.width = newwidth;
width = newwidth - szHint.base_width;
}
else
{
min_it (width, max_width);
szHint.width = szHint.base_width + width;
}
if (newheight && newheight - szHint.base_height < max_height)
{
szHint.height = newheight;
height = newheight - szHint.base_height;
}
else
{
min_it (height, max_height);
szHint.height = szHint.base_height + height;
}
if (recalc_x)
szHint.x += DisplayWidth (dpy, display->screen) - szHint.width - 2 * ext_bwidth;
if (recalc_y)
szHint.y += DisplayHeight (dpy, display->screen) - szHint.height - 2 * ext_bwidth;
ncol = width / fwidth;
nrow = height / fheight;
}
/*----------------------------------------------------------------------*/
/*
* Tell the teletype handler what size the window is.
* Called after a window size change.
*/
void
rxvt_term::tt_winch ()
{
if (pty->pty < 0)
return;
struct winsize ws;
ws.ws_col = ncol;
ws.ws_row = nrow;
ws.ws_xpixel = width;
ws.ws_ypixel = height;
ioctl (pty->pty, TIOCSWINSZ, &ws);
#if 0
// TIOCSWINSZ is supposed to do this automatically and correctly
if (cmd_pid) /* force through to the command */
kill (-cmd_pid, SIGWINCH);
#endif
}
/*----------------------------------------------------------------------*/
/* set_fonts () - load and set the various fonts
*
* init = 1 - initialize
*
* fontname == FONT_UP - switch to bigger font
* fontname == FONT_DN - switch to smaller font
*/
bool
rxvt_term::set_fonts ()
{
rxvt_fontset *fs = new rxvt_fontset (this);
rxvt_fontprop prop;
if (!fs
|| !fs->populate (rs[Rs_font] ? rs[Rs_font] : "fixed")
|| !fs->realize_font (1))
{
delete fs;
return false;
}
#if ENABLE_STYLES
for (int i = RS_styleCount; --i; )
if (fontset[i] != fontset[0])
delete fontset[i];
#endif
delete fontset[0];
fontset[0] = fs;
prop = (*fs)[rxvt_fontset::firstFont]->properties ();
prop.height += lineSpace;
prop.width += letterSpace;
fs->set_prop (prop, false);
fwidth = prop.width;
fheight = prop.height;
fbase = prop.ascent;
for (int style = 1; style < 4; style++)
{
#if ENABLE_STYLES
const char *res = rs[Rs_font + style];
if (res && !*res)
fontset[style] = fontset[0];
else
{
fontset[style] = fs = new rxvt_fontset (this);
rxvt_fontprop prop2 = prop;
if (res)
{
fs->populate (res);
fs->set_prop (prop2, false);
}
else
{
fs->populate (fontset[0]->fontdesc);
if (SET_STYLE (0, style) & RS_Bold) prop2.weight = rxvt_fontprop::bold;
if (SET_STYLE (0, style) & RS_Italic) prop2.slant = rxvt_fontprop::italic;
fs->set_prop (prop2, true);
}
}
#else
fontset[style] = fontset[0];
#endif
}
if (parent)
{
resize_all_windows (0, 0, 0);
scr_remap_chars ();
scr_touch (true);
}
return true;
}
void
rxvt_term::set_string_property (Atom prop, const char *str, int len)
{
XChangeProperty (dpy, parent,
prop, XA_STRING, 8, PropModeReplace,
(const unsigned char *)str, len >= 0 ? len : strlen (str));
}
void
rxvt_term::set_mbstring_property (Atom prop, const char *str, int len)
{
XTextProperty ct;
if (XmbTextListToTextProperty (dpy, (char **)&str, 1, XStdICCTextStyle, &ct) >= 0)
{
XSetTextProperty (dpy, parent, &ct, prop);
XFree (ct.value);
}
}
void
rxvt_term::set_utf8_property (Atom prop, const char *str, int len)
{
wchar_t *ws = rxvt_mbstowcs (str, len);
char *s = rxvt_wcstoutf8 (ws);
XChangeProperty (dpy, parent,
prop, xa[XA_UTF8_STRING], 8, PropModeReplace,
(const unsigned char *)s, strlen (s));
free (s);
free (ws);
}
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* xterm sequences - title, iconName, color (exptl) */
void
rxvt_term::set_title (const char *str)
{
set_mbstring_property (XA_WM_NAME, str);
#if ENABLE_EWMH
set_utf8_property (xa[XA_NET_WM_NAME], str);
#endif
}
void
rxvt_term::set_icon_name (const char *str)
{
set_mbstring_property (XA_WM_ICON_NAME, str);
#if ENABLE_EWMH
set_utf8_property (xa[XA_NET_WM_ICON_NAME], str);
#endif
}
void
rxvt_term::set_window_color (int idx, const char *color)
{
#ifdef XTERM_COLOR_CHANGE
rxvt_color xcol;
if (color == NULL || *color == '\0')
return;
color = strdup (color);
allocated.push_back ((void *)color);
rs[Rs_color + idx] = color;
/* handle color aliases */
if (isdigit (*color))
{
int i = atoi (color);
if (i >= 8 && i <= 15)
{
/* bright colors */
pix_colors_focused[idx] = pix_colors_focused[minBrightCOLOR + i - 8];
goto done;
}
if (i >= 0 && i <= 7)
{
/* normal colors */
pix_colors_focused[idx] = pix_colors_focused[minCOLOR + i];
goto done;
}
}
set_color (xcol, color);
/*
* FIXME: should free colors here, but no idea how to do it so instead,
* so just keep gobbling up the colormap
*/
pix_colors_focused[idx] = xcol;
done:
/*TODO: handle Color_BD, scrollbar background, etc. */
update_fade_color (idx);
recolour_cursor ();
scr_recolour ();
#endif /* XTERM_COLOR_CHANGE */
}
void
rxvt_term::recolour_cursor ()
{
XColor fg, bg;
(ISSET_PIXCOLOR (Color_pointer_fg)
? pix_colors_focused[Color_pointer_fg]
: pix_colors_focused[Color_fg]).get (fg);
(ISSET_PIXCOLOR (Color_pointer_bg)
? pix_colors_focused[Color_pointer_bg]
: pix_colors_focused[Color_bg]).get (bg);
XRecolorCursor (dpy, TermWin_cursor, &fg, &bg);
}
/*----------------------------------------------------------------------*/
/*
* find if fg/bg matches any of the normal (low-intensity) colors
*/
void
rxvt_term::set_colorfgbg ()
{
unsigned int i;
const char *xpmb = "";
char fstr[] = "default";
char bstr[] = "default";
for (i = Color_Black; i <= Color_White; i++)
if (pix_colors[Color_fg] == pix_colors[i])
{
sprintf (fstr, "%d", i - Color_Black);
break;
}
for (i = Color_Black; i <= Color_White; i++)
if (pix_colors[Color_bg] == pix_colors[i])
{
sprintf (bstr, "%d", i - Color_Black);
#ifdef BG_IMAGE_FROM_FILE
xpmb = "default;";
#endif
break;
}
sprintf (env_colorfgbg, "COLORFGBG=%s;%s%s", fstr, xpmb, bstr);
}
/*----------------------------------------------------------------------*/
bool
rxvt_term::set_color (rxvt_color &color, const char *name)
{
if (color.set (this, name))
return true;
rxvt_warn ("can't get colour '%s', continuing without.\n", name);
return false;
}
void
rxvt_term::alias_color (int dst, int src)
{
pix_colors[dst].set (this, rs[Rs_color + dst] = rs[Rs_color + src]);
}
/* -------------------------------------------------------------------- *
* - WINDOW RESIZING - *
* -------------------------------------------------------------------- */
void
rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, int ignoreparent)
{