-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
6178 lines (5593 loc) · 143 KB
/
misc.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
/* $XTermId: misc.c,v 1.743 2016/10/07 00:41:14 tom Exp $ */
/*
* Copyright 1999-2015,2016 by Thomas E. Dickey
*
* All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name(s) of the above copyright
* holders shall not be used in advertising or otherwise to promote the
* sale, use or other dealings in this Software without prior written
* authorization.
*
*
* Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital Equipment
* Corporation not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
*
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <version.h>
#include <main.h>
#include <xterm.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include <ctype.h>
#include <pwd.h>
#include <sys/wait.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/Xlocale.h>
#include <X11/Xmu/Error.h>
#include <X11/Xmu/SysUtil.h>
#include <X11/Xmu/WinUtil.h>
#include <X11/Xmu/Xmu.h>
#if HAVE_X11_SUNKEYSYM_H
#include <X11/Sunkeysym.h>
#endif
#ifdef HAVE_LIBXPM
#include <X11/xpm.h>
#endif
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
#include <xutf8.h>
#include <data.h>
#include <error.h>
#include <menu.h>
#include <fontutils.h>
#include <xstrings.h>
#include <xtermcap.h>
#include <VTparse.h>
#include <graphics.h>
#include <graphics_regis.h>
#include <graphics_sixel.h>
#include <assert.h>
#if (XtSpecificationRelease < 6)
#ifndef X_GETTIMEOFDAY
#define X_GETTIMEOFDAY(t) gettimeofday(t,(struct timezone *)0)
#endif
#endif
#ifdef VMS
#define XTERM_VMS_LOGFILE "SYS$SCRATCH:XTERM_LOG.TXT"
#ifdef ALLOWLOGFILEEXEC
#undef ALLOWLOGFILEEXEC
#endif
#endif /* VMS */
#if OPT_TEK4014
#define OUR_EVENT(event,Type) \
(event.type == Type && \
(event.xcrossing.window == XtWindow(XtParent(xw)) || \
(tekWidget && \
event.xcrossing.window == XtWindow(XtParent(tekWidget)))))
#else
#define OUR_EVENT(event,Type) \
(event.type == Type && \
(event.xcrossing.window == XtWindow(XtParent(xw))))
#endif
static Boolean xtermAllocColor(XtermWidget, XColor *, const char *);
static Cursor make_hidden_cursor(XtermWidget);
static char emptyString[] = "";
#if OPT_EXEC_XTERM
/* Like readlink(2), but returns a malloc()ed buffer, or NULL on
error; adapted from libc docs */
static char *
Readlink(const char *filename)
{
char *buf = NULL;
size_t size = 100;
for (;;) {
int n;
char *tmp = TypeRealloc(char, size, buf);
if (tmp == NULL) {
free(buf);
return NULL;
}
buf = tmp;
memset(buf, 0, size);
n = (int) readlink(filename, buf, size);
if (n < 0) {
free(buf);
return NULL;
}
if ((unsigned) n < size) {
return buf;
}
size *= 2;
}
}
#endif /* OPT_EXEC_XTERM */
static void
Sleep(int msec)
{
static struct timeval select_timeout;
select_timeout.tv_sec = 0;
select_timeout.tv_usec = msec * 1000;
select(0, 0, 0, 0, &select_timeout);
}
static void
selectwindow(XtermWidget xw, int flag)
{
TScreen *screen = TScreenOf(xw);
TRACE(("selectwindow(%d) flag=%d\n", screen->select, flag));
#if OPT_TEK4014
if (TEK4014_ACTIVE(xw)) {
if (!Ttoggled)
TCursorToggle(tekWidget, TOGGLE);
screen->select |= flag;
if (!Ttoggled)
TCursorToggle(tekWidget, TOGGLE);
} else
#endif
{
#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
TInput *input = lookupTInput(xw, (Widget) xw);
if (input && input->xic)
XSetICFocus(input->xic);
#endif
if (screen->cursor_state && CursorMoved(screen))
HideCursor();
screen->select |= flag;
if (screen->cursor_state)
ShowCursor();
}
GetScrollLock(screen);
}
static void
unselectwindow(XtermWidget xw, int flag)
{
TScreen *screen = TScreenOf(xw);
TRACE(("unselectwindow(%d) flag=%d\n", screen->select, flag));
if (screen->hide_pointer && screen->pointer_mode < pFocused) {
screen->hide_pointer = False;
xtermDisplayCursor(xw);
}
if (!screen->always_highlight) {
#if OPT_TEK4014
if (TEK4014_ACTIVE(xw)) {
if (!Ttoggled)
TCursorToggle(tekWidget, TOGGLE);
screen->select &= ~flag;
if (!Ttoggled)
TCursorToggle(tekWidget, TOGGLE);
} else
#endif
{
#if OPT_I18N_SUPPORT && OPT_INPUT_METHOD
TInput *input = lookupTInput(xw, (Widget) xw);
if (input && input->xic)
XUnsetICFocus(input->xic);
#endif
screen->select &= ~flag;
if (screen->cursor_state && CursorMoved(screen))
HideCursor();
if (screen->cursor_state)
ShowCursor();
}
}
}
static void
DoSpecialEnterNotify(XtermWidget xw, XEnterWindowEvent *ev)
{
TScreen *screen = TScreenOf(xw);
TRACE(("DoSpecialEnterNotify(%d)\n", screen->select));
TRACE_FOCUS(xw, ev);
if (((ev->detail) != NotifyInferior) &&
ev->focus &&
!(screen->select & FOCUS))
selectwindow(xw, INWINDOW);
}
static void
DoSpecialLeaveNotify(XtermWidget xw, XEnterWindowEvent *ev)
{
TScreen *screen = TScreenOf(xw);
TRACE(("DoSpecialLeaveNotify(%d)\n", screen->select));
TRACE_FOCUS(xw, ev);
if (((ev->detail) != NotifyInferior) &&
ev->focus &&
!(screen->select & FOCUS))
unselectwindow(xw, INWINDOW);
}
#ifndef XUrgencyHint
#define XUrgencyHint (1L << 8) /* X11R5 does not define */
#endif
static void
setXUrgency(XtermWidget xw, Bool enable)
{
TScreen *screen = TScreenOf(xw);
if (screen->bellIsUrgent) {
XWMHints *h = XGetWMHints(screen->display, VShellWindow(xw));
if (h != 0) {
if (enable && !(screen->select & FOCUS)) {
h->flags |= XUrgencyHint;
} else {
h->flags &= ~XUrgencyHint;
}
XSetWMHints(screen->display, VShellWindow(xw), h);
}
}
}
void
do_xevents(void)
{
TScreen *screen = TScreenOf(term);
if (xtermAppPending()
||
#if defined(VMS) || defined(__VMS)
screen->display->qlen > 0
#else
GetBytesAvailable(ConnectionNumber(screen->display)) > 0
#endif
)
xevents();
}
void
xtermDisplayCursor(XtermWidget xw)
{
TScreen *screen = TScreenOf(xw);
if (screen->Vshow) {
if (screen->hide_pointer) {
TRACE(("Display hidden_cursor\n"));
XDefineCursor(screen->display, VWindow(screen), screen->hidden_cursor);
} else {
TRACE(("Display pointer_cursor\n"));
recolor_cursor(screen,
screen->pointer_cursor,
T_COLOR(screen, MOUSE_FG),
T_COLOR(screen, MOUSE_BG));
XDefineCursor(screen->display, VWindow(screen), screen->pointer_cursor);
}
}
}
void
xtermShowPointer(XtermWidget xw, Bool enable)
{
static int tried = -1;
TScreen *screen = TScreenOf(xw);
#if OPT_TEK4014
if (TEK4014_SHOWN(xw))
enable = True;
#endif
/*
* Whether we actually hide the pointer depends on the pointer-mode and
* the mouse-mode:
*/
if (!enable) {
switch (screen->pointer_mode) {
case pNever:
enable = True;
break;
case pNoMouse:
if (screen->send_mouse_pos != MOUSE_OFF)
enable = True;
break;
case pAlways:
case pFocused:
break;
}
}
if (enable) {
if (screen->hide_pointer) {
screen->hide_pointer = False;
xtermDisplayCursor(xw);
switch (screen->send_mouse_pos) {
case ANY_EVENT_MOUSE:
break;
default:
MotionOff(screen, xw);
break;
}
}
} else if (!(screen->hide_pointer) && (tried <= 0)) {
if (screen->hidden_cursor == 0) {
screen->hidden_cursor = make_hidden_cursor(xw);
}
if (screen->hidden_cursor == 0) {
tried = 1;
} else {
tried = 0;
screen->hide_pointer = True;
xtermDisplayCursor(xw);
MotionOn(screen, xw);
}
}
}
#if OPT_TRACE
static void
TraceExposeEvent(XEvent *arg)
{
XExposeEvent *event = (XExposeEvent *) arg;
TRACE(("pending Expose %ld %d: %d,%d %dx%d %#lx\n",
event->serial,
event->count,
event->y,
event->x,
event->height,
event->width,
event->window));
}
#else
#define TraceExposeEvent(event) /* nothing */
#endif
/* true if p contains q */
#define ExposeContains(p,q) \
((p)->y <= (q)->y \
&& (p)->x <= (q)->x \
&& ((p)->y + (p)->height) >= ((q)->y + (q)->height) \
&& ((p)->x + (p)->width) >= ((q)->x + (q)->width))
static XtInputMask
mergeExposeEvents(XEvent *target)
{
XEvent next_event;
XExposeEvent *p;
TRACE(("pending Expose...?\n"));
TraceExposeEvent(target);
XtAppNextEvent(app_con, target);
p = (XExposeEvent *) target;
while (XtAppPending(app_con)
&& XtAppPeekEvent(app_con, &next_event)
&& next_event.type == Expose) {
Boolean merge_this = False;
XExposeEvent *q;
TraceExposeEvent(&next_event);
q = (XExposeEvent *) (&next_event);
XtAppNextEvent(app_con, &next_event);
/*
* If either window is contained within the other, merge the events.
* The traces show that there are also cases where a full repaint of
* a window is broken into 3 or more rectangles, which do not arrive
* in the same instant. We could merge those if xterm were modified
* to skim several events ahead.
*/
if (p->window == q->window) {
if (ExposeContains(p, q)) {
TRACE(("pending Expose...merged forward\n"));
merge_this = True;
next_event = *target;
} else if (ExposeContains(q, p)) {
TRACE(("pending Expose...merged backward\n"));
merge_this = True;
}
}
if (!merge_this) {
XtDispatchEvent(target);
}
*target = next_event;
}
XtDispatchEvent(target);
return XtAppPending(app_con);
}
#if OPT_TRACE
static void
TraceConfigureEvent(XEvent *arg)
{
XConfigureEvent *event = (XConfigureEvent *) arg;
TRACE(("pending Configure %ld %d,%d %dx%d %#lx\n",
event->serial,
event->y,
event->x,
event->height,
event->width,
event->window));
}
#else
#define TraceConfigureEvent(event) /* nothing */
#endif
/*
* On entry, we have peeked at the event queue and see a configure-notify
* event. Remove that from the queue so we can look further.
*
* Then, as long as there is a configure-notify event in the queue, remove
* that. If the adjacent events are for different windows, process the older
* event and update the event used for comparing windows. If they are for the
* same window, only the newer event is of interest.
*
* Finally, process the (remaining) configure-notify event.
*/
static XtInputMask
mergeConfigureEvents(XEvent *target)
{
XEvent next_event;
XConfigureEvent *p;
XtAppNextEvent(app_con, target);
p = (XConfigureEvent *) target;
TRACE(("pending Configure...?%s\n", XtAppPending(app_con) ? "yes" : "no"));
TraceConfigureEvent(target);
if (XtAppPending(app_con)
&& XtAppPeekEvent(app_con, &next_event)
&& next_event.type == ConfigureNotify) {
Boolean merge_this = False;
XConfigureEvent *q;
TraceConfigureEvent(&next_event);
XtAppNextEvent(app_con, &next_event);
q = (XConfigureEvent *) (&next_event);
if (p->window == q->window) {
TRACE(("pending Configure...merged\n"));
merge_this = True;
}
if (!merge_this) {
TRACE(("pending Configure...skipped\n"));
XtDispatchEvent(target);
}
*target = next_event;
}
XtDispatchEvent(target);
return XtAppPending(app_con);
}
/*
* Filter redundant Expose- and ConfigureNotify-events. This is limited to
* adjacent events because there could be other event-loop processing. Absent
* that limitation, it might be possible to scan ahead to find when the screen
* would be completely updated, skipping unnecessary re-repainting before that
* point.
*
* Note: all cases should allow doing XtAppNextEvent if result is true.
*/
XtInputMask
xtermAppPending(void)
{
XtInputMask result = XtAppPending(app_con);
XEvent this_event;
Boolean found = False;
while (result && XtAppPeekEvent(app_con, &this_event)) {
found = True;
if (this_event.type == Expose) {
result = mergeExposeEvents(&this_event);
TRACE(("got merged expose events\n"));
} else if (this_event.type == ConfigureNotify) {
result = mergeConfigureEvents(&this_event);
TRACE(("got merged configure notify events\n"));
} else {
TRACE(("pending %s\n", visibleEventType(this_event.type)));
break;
}
}
/*
* With NetBSD, closing a shell results in closing the X input event
* stream, which interferes with the "-hold" option. Wait a short time in
* this case, to avoid max'ing the CPU.
*/
if (hold_screen && caught_intr && !found) {
Sleep(10);
}
return result;
}
void
xevents(void)
{
XtermWidget xw = term;
TScreen *screen = TScreenOf(xw);
XEvent event;
XtInputMask input_mask;
if (need_cleanup)
NormalExit();
if (screen->scroll_amt)
FlushScroll(xw);
/*
* process timeouts, relying on the fact that XtAppProcessEvent
* will process the timeout and return without blockng on the
* XEvent queue. Other sources i.e., the pty are handled elsewhere
* with select().
*/
while ((input_mask = xtermAppPending()) != 0) {
if (input_mask & XtIMTimer)
XtAppProcessEvent(app_con, (XtInputMask) XtIMTimer);
#if OPT_SESSION_MGT
/*
* Session management events are alternative input events. Deal with
* them in the same way.
*/
else if (input_mask & XtIMAlternateInput)
XtAppProcessEvent(app_con, (XtInputMask) XtIMAlternateInput);
#endif
else
break;
}
/*
* If there's no XEvents, don't wait around...
*/
if ((input_mask & XtIMXEvent) != XtIMXEvent)
return;
do {
/*
* This check makes xterm hang when in mouse hilite tracking mode.
* We simply ignore all events except for those not passed down to
* this function, e.g., those handled in in_put().
*/
if (screen->waitingForTrackInfo) {
Sleep(10);
return;
}
XtAppNextEvent(app_con, &event);
/*
* Hack to get around problems with the toolkit throwing away
* eventing during the exclusive grab of the menu popup. By
* looking at the event ourselves we make sure that we can
* do the right thing.
*/
if (OUR_EVENT(event, EnterNotify)) {
DoSpecialEnterNotify(xw, &event.xcrossing);
} else if (OUR_EVENT(event, LeaveNotify)) {
DoSpecialLeaveNotify(xw, &event.xcrossing);
} else if ((screen->send_mouse_pos == ANY_EVENT_MOUSE
#if OPT_DEC_LOCATOR
|| screen->send_mouse_pos == DEC_LOCATOR
#endif /* OPT_DEC_LOCATOR */
)
&& event.xany.type == MotionNotify
&& event.xcrossing.window == XtWindow(xw)) {
SendMousePosition(xw, &event);
xtermShowPointer(xw, True);
continue;
}
/*
* If the event is interesting (and not a keyboard event), turn the
* mouse pointer back on.
*/
if (screen->hide_pointer) {
if (screen->pointer_mode >= pFocused) {
switch (event.xany.type) {
case MotionNotify:
xtermShowPointer(xw, True);
break;
}
} else {
switch (event.xany.type) {
case KeyPress:
case KeyRelease:
case ButtonPress:
case ButtonRelease:
/* also these... */
case Expose:
case GraphicsExpose:
case NoExpose:
case PropertyNotify:
case ClientMessage:
break;
default:
xtermShowPointer(xw, True);
break;
}
}
}
if (!event.xany.send_event ||
screen->allowSendEvents ||
((event.xany.type != KeyPress) &&
(event.xany.type != KeyRelease) &&
(event.xany.type != ButtonPress) &&
(event.xany.type != ButtonRelease))) {
XtDispatchEvent(&event);
}
} while (xtermAppPending() & XtIMXEvent);
}
static Cursor
make_hidden_cursor(XtermWidget xw)
{
TScreen *screen = TScreenOf(xw);
Cursor c;
Display *dpy = screen->display;
XFontStruct *fn;
static XColor dummy;
/*
* Prefer nil2 (which is normally available) to "fixed" (which is supposed
* to be "always" available), since it's a smaller glyph in case the
* server insists on drawing _something_.
*/
TRACE(("Ask for nil2 font\n"));
if ((fn = XLoadQueryFont(dpy, "nil2")) == 0) {
TRACE(("...Ask for fixed font\n"));
fn = XLoadQueryFont(dpy, DEFFONT);
}
if (fn != 0) {
/* a space character seems to work as a cursor (dots are not needed) */
c = XCreateGlyphCursor(dpy, fn->fid, fn->fid, 'X', ' ', &dummy, &dummy);
XFreeFont(dpy, fn);
} else {
c = 0;
}
TRACE(("XCreateGlyphCursor ->%#lx\n", c));
return (c);
}
/*
* Xlib uses Xcursor to customize cursor coloring, which interferes with
* xterm's pointerColor resource. Work around this by providing our own
* default theme. Testing seems to show that we only have to provide this
* until the window is initialized.
*/
void
init_colored_cursor(Display *dpy)
{
#ifdef HAVE_LIB_XCURSOR
static const char theme[] = "index.theme";
static const char pattern[] = "xtermXXXXXX";
char *env = getenv("XCURSOR_THEME");
xterm_cursor_theme = 0;
/*
* The environment variable overrides a (possible) resource Xcursor.theme
*/
if (IsEmpty(env)) {
env = XGetDefault(dpy, "Xcursor", "theme");
}
/*
* If neither found, provide our own default theme.
*/
if (IsEmpty(env)) {
const char *tmp_dir;
char *filename;
size_t needed;
if ((tmp_dir = getenv("TMPDIR")) == 0) {
tmp_dir = P_tmpdir;
}
needed = strlen(tmp_dir) + 4 + strlen(theme) + strlen(pattern);
if ((filename = malloc(needed)) != 0) {
sprintf(filename, "%s/%s", tmp_dir, pattern);
#ifdef HAVE_MKDTEMP
xterm_cursor_theme = mkdtemp(filename);
#else
if (mktemp(filename) != 0
&& mkdir(filename, 0700) == 0) {
xterm_cursor_theme = filename;
}
#endif
/*
* Actually, Xcursor does what _we_ want just by steering its
* search path away from home. We are setting up the complete
* theme just in case the library ever acquires a maintainer.
*/
if (xterm_cursor_theme != 0) {
char *leaf = xterm_cursor_theme + strlen(xterm_cursor_theme);
FILE *fp;
strcat(leaf, "/");
strcat(leaf, theme);
if ((fp = fopen(xterm_cursor_theme, "w")) != 0) {
fprintf(fp, "[Icon Theme]\n");
fclose(fp);
*leaf = '\0';
xtermSetenv("XCURSOR_PATH", xterm_cursor_theme);
*leaf = '/';
}
atexit(cleanup_colored_cursor);
}
}
}
#else
(void) dpy;
#endif /* HAVE_LIB_XCURSOR */
}
/*
* Once done, discard the file and directory holding it.
*/
void
cleanup_colored_cursor(void)
{
#ifdef HAVE_LIB_XCURSOR
if (xterm_cursor_theme != 0) {
char *my_path = getenv("XCURSOR_PATH");
struct stat sb;
if (!IsEmpty(my_path)
&& stat(my_path, &sb) == 0
&& (sb.st_mode & S_IFMT) == S_IFDIR) {
unlink(xterm_cursor_theme);
rmdir(my_path);
free(xterm_cursor_theme);
xterm_cursor_theme = 0;
}
}
#endif /* HAVE_LIB_XCURSOR */
}
Cursor
make_colored_cursor(unsigned cursorindex, /* index into font */
unsigned long fg, /* pixel value */
unsigned long bg) /* pixel value */
{
TScreen *screen = TScreenOf(term);
Cursor c;
Display *dpy = screen->display;
c = XCreateFontCursor(dpy, cursorindex);
if (c != None) {
recolor_cursor(screen, c, fg, bg);
}
return (c);
}
/* ARGSUSED */
void
HandleKeyPressed(Widget w GCC_UNUSED,
XEvent *event,
String *params GCC_UNUSED,
Cardinal *nparams GCC_UNUSED)
{
TRACE(("Handle insert-seven-bit for %p\n", (void *) w));
Input(term, &event->xkey, False);
}
/* ARGSUSED */
void
HandleEightBitKeyPressed(Widget w GCC_UNUSED,
XEvent *event,
String *params GCC_UNUSED,
Cardinal *nparams GCC_UNUSED)
{
TRACE(("Handle insert-eight-bit for %p\n", (void *) w));
Input(term, &event->xkey, True);
}
/* ARGSUSED */
void
HandleStringEvent(Widget w GCC_UNUSED,
XEvent *event GCC_UNUSED,
String *params,
Cardinal *nparams)
{
if (*nparams != 1)
return;
if ((*params)[0] == '0' && (*params)[1] == 'x' && (*params)[2] != '\0') {
const char *abcdef = "ABCDEF";
const char *xxxxxx;
Char c;
UString p;
unsigned value = 0;
for (p = (UString) (*params + 2); (c = CharOf(x_toupper(*p))) !=
'\0'; p++) {
value *= 16;
if (c >= '0' && c <= '9')
value += (unsigned) (c - '0');
else if ((xxxxxx = (strchr) (abcdef, c)) != 0)
value += (unsigned) (xxxxxx - abcdef) + 10;
else
break;
}
if (c == '\0') {
Char hexval[2];
hexval[0] = (Char) value;
hexval[1] = 0;
StringInput(term, hexval, (size_t) 1);
}
} else {
StringInput(term, (const Char *) *params, strlen(*params));
}
}
#if OPT_EXEC_XTERM
#ifndef PROCFS_ROOT
#define PROCFS_ROOT "/proc"
#endif
/*
* Determine the current working directory of the child so that we can
* spawn a new terminal in the same directory.
*
* If we cannot get the CWD of the child, just use our own.
*/
char *
ProcGetCWD(pid_t pid)
{
char *child_cwd = NULL;
if (pid) {
char child_cwd_link[sizeof(PROCFS_ROOT) + 80];
sprintf(child_cwd_link, PROCFS_ROOT "/%lu/cwd", (unsigned long) pid);
child_cwd = Readlink(child_cwd_link);
}
return child_cwd;
}
/* ARGSUSED */
void
HandleSpawnTerminal(Widget w GCC_UNUSED,
XEvent *event GCC_UNUSED,
String *params,
Cardinal *nparams)
{
TScreen *screen = TScreenOf(term);
char *child_cwd = NULL;
char *child_exe;
pid_t pid;
/*
* Try to find the actual program which is running in the child process.
* This works for Linux. If we cannot find the program, fall back to the
* xterm program (which is usually adequate). Give up if we are given only
* a relative path to xterm, since that would not always match $PATH.
*/
child_exe = Readlink(PROCFS_ROOT "/self/exe");
if (!child_exe) {
if (strncmp(ProgramName, "./", (size_t) 2)
&& strncmp(ProgramName, "../", (size_t) 3)) {
child_exe = xtermFindShell(ProgramName, True);
} else {
xtermWarning("Cannot exec-xterm given \"%s\"\n", ProgramName);
}
if (child_exe == 0)
return;
}
child_cwd = ProcGetCWD(screen->pid);
/* The reaper will take care of cleaning up the child */
pid = fork();
if (pid == -1) {
xtermWarning("Could not fork: %s\n", SysErrorMsg(errno));
} else if (!pid) {
/* We are the child */
if (child_cwd) {
IGNORE_RC(chdir(child_cwd)); /* We don't care if this fails */
}
if (setuid(screen->uid) == -1
|| setgid(screen->gid) == -1) {
xtermWarning("Cannot reset uid/gid\n");
} else {
unsigned myargc = *nparams + 1;
char **myargv = TypeMallocN(char *, myargc + 1);
if (myargv != 0) {
unsigned n = 0;
myargv[n++] = child_exe;
while (n < myargc) {
myargv[n++] = (char *) *params++;
}
myargv[n] = 0;
execv(child_exe, myargv);
}
/* If we get here, we've failed */
xtermWarning("exec of '%s': %s\n", child_exe, SysErrorMsg(errno));
}
_exit(0);
}
/* We are the parent; clean up */
if (child_cwd)
free(child_cwd);
free(child_exe);
}
#endif /* OPT_EXEC_XTERM */
/*
* Rather than sending characters to the host, put them directly into our
* input queue. That lets a user have access to any of the control sequences
* for a key binding. This is the equivalent of local function key support.
*
* NOTE: This code does not support the hexadecimal kludge used in
* HandleStringEvent because it prevents us from sending an arbitrary string
* (but it appears in a lot of examples - so we are stuck with it). The
* standard string converter does recognize "\" for newline ("\n") and for
* octal constants (e.g., "\007" for BEL). So we assume the user can make do
* without a specialized converter. (Don't try to use \000, though).
*/
/* ARGSUSED */
void
HandleInterpret(Widget w GCC_UNUSED,