ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/main.C
(Generate patch)

Comparing rxvt-unicode/src/main.C (file contents):
Revision 1.5 by pcg, Tue Nov 25 15:44:38 2003 UTC vs.
Revision 1.15 by pcg, Thu Dec 18 05:45:11 2003 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: main.c 2 * File: main.c
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: main.C,v 1.5 2003/11/25 15:44:38 pcg Exp $ 4 * $Id: main.C,v 1.15 2003/12/18 05:45:11 pcg Exp $
5 * 5 *
6 * All portions of code are copyright by their respective author/s. 6 * All portions of code are copyright by their respective author/s.
7 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> 7 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
8 * - original version 8 * - original version
9 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> 9 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
42 42
43#ifdef HAVE_TERMIOS_H 43#ifdef HAVE_TERMIOS_H
44# include <termios.h> 44# include <termios.h>
45#endif 45#endif
46 46
47#include <cstring>
48
49static char curlocale[128];
50
51void
52rxvt_set_locale (const char *locale)
53{
54 if (locale && strncmp (locale, curlocale, 128))
55 {
56 strncpy (curlocale, locale, 128);
57 setlocale (LC_CTYPE, curlocale);
58 }
59}
60
47void * 61void *
48rxvt_term::operator new (size_t s) 62rxvt_term::operator new (size_t s)
49{ 63{
50 void *p = malloc (s); 64 void *p = malloc (s);
51 65
58{ 72{
59 free (p); 73 free (p);
60} 74}
61 75
62rxvt_term::rxvt_term () 76rxvt_term::rxvt_term ()
63: pty_ev (this, &rxvt_term::pty_cb), 77: pty_ev (this, &rxvt_term::pty_cb),
64#ifdef CURSOR_BLINK 78#ifdef CURSOR_BLINK
65 blink_ev (this, &rxvt_term::blink_cb), 79 blink_ev (this, &rxvt_term::blink_cb),
66#endif 80#endif
81#ifdef POINTER_BLANK
82 pointer_ev (this, &rxvt_term::pointer_cb),
83#endif
67 x_ev (this, &rxvt_term::x_cb) 84 x_ev (this, &rxvt_term::x_cb),
85 destroy_ev (this, &rxvt_term::destroy_cb),
86 check_ev (this, &rxvt_term::check_cb),
87 incr_ev (this, &rxvt_term::incr_cb)
68{ 88{
69 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; 89 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
70} 90}
71 91
72rxvt_term::~rxvt_term () 92rxvt_term::~rxvt_term ()
73{ 93{
94 scr_release ();
95
96 free (locale);
97 free (codeset);
98
99#ifndef NO_SETOWNER_TTYDEV
100 rxvt_privileged_ttydev (this, RESTORE);
101#endif
102#ifdef UTMP_SUPPORT
103 rxvt_privileged_utmp (this, RESTORE);
104#endif
105#ifdef USE_XIM
106 if (Input_Context != NULL)
107 {
108 XDestroyIC (Input_Context);
109 Input_Context = NULL;
110 }
111#endif
112
113 if (cmd_fd >= 0)
114 close (cmd_fd);
115
116 if (Xfd >= 0)
117 XCloseDisplay (Xdisplay);
118
74 delete PixColors; 119 delete PixColors;
120}
121
122void
123rxvt_term::destroy ()
124{
125 check_ev.stop ();
126 pty_ev.stop ();
127 x_ev.stop ();
128#ifdef CURSOR_BLINK
129 blink_ev.stop ();
130#endif
131#ifdef POINTER_BLANK
132 pointer_ev.stop ();
133#endif
134
135 destroy_ev.start (0);
136}
137
138void
139rxvt_term::destroy_cb (time_watcher &w)
140{
141 SET_R (this);
142
143 delete this;
75} 144}
76 145
77/*----------------------------------------------------------------------*/ 146/*----------------------------------------------------------------------*/
78/* rxvt_init() */ 147/* rxvt_init() */
79/* LIBPROTO */ 148/* LIBPROTO */
80rxvt_t 149rxvt_t
81rxvt_init(int argc, const char *const *argv) 150rxvt_init (int argc, const char *const *argv)
82{ 151{
83 SET_R(new rxvt_term); 152 SET_R (new rxvt_term);
84 dR; 153 dR;
85 154
86 if (!R->init_vars () || !R->init (argc, argv)) 155 if (!R->init_vars () || !R->init (argc, argv))
87 { 156 {
88 delete R; 157 delete R;
90 } 159 }
91 160
92 return R; 161 return R;
93} 162}
94 163
164/* EXTPROTO */
165void
166rxvt_init_signals ()
167{
168/* install exit handler for cleanup */
169#if 0
170#ifdef HAVE_ATEXIT
171 atexit(rxvt_clean_exit);
172#else
173#endif
174#endif
175
176 struct sigaction sa;
177
178 sigfillset (&sa.sa_mask);
179 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
180 sa.sa_handler = SIG_IGN; sigaction (SIGHUP , &sa, 0);
181 sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0);
182 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0);
183 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0);
184 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0);
185 sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0);
186
187/* need to trap SIGURG for SVR4 (Unixware) rlogin */
188/* signal (SIGURG, SIG_DFL); */
189
190 XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler);
191 //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler);
192}
193
95bool 194bool
96rxvt_term::init (int argc, const char *const *argv) 195rxvt_term::init (int argc, const char *const *argv)
97{ 196{
98 dR;//TODO (scrollbar, setidle)
99
100 /* 197 /*
101 * Save and then give up any super-user privileges 198 * Save and then give up any super-user privileges
102 * If we need privileges in any area then we must specifically request it. 199 * If we need privileges in any area then we must specifically request it.
103 * We should only need to be root in these cases: 200 * We should only need to be root in these cases:
104 * 1. write utmp entries on some systems 201 * 1. write utmp entries on some systems
105 * 2. chown tty on some systems 202 * 2. chown tty on some systems
106 */ 203 */
107 rxvt_privileges (this, SAVE); 204 rxvt_privileges (this, SAVE);
108 rxvt_privileges (this, IGNORE); 205 rxvt_privileges (this, IGNORE);
109 206
207#if HAVE_XSETLOCALE || HAVE_SETLOCALE
208 locale = strdup (setlocale (LC_CTYPE, ""));
209#endif
210#if HAVE_NL_LANGINFO
211 codeset = strdup (nl_langinfo (CODESET));
212#endif
213
110 rxvt_init_secondary (this); 214 init_secondary ();
111 215
112 const char **cmd_argv = rxvt_init_resources (this, argc, argv); 216 const char **cmd_argv = init_resources (argc, argv);
113 217
114#if (MENUBAR_MAX) 218#if (MENUBAR_MAX)
115 rxvt_menubar_read (this, rs[Rs_menu]); 219 rxvt_menubar_read (this, rs[Rs_menu]);
116#endif 220#endif
117#ifdef HAVE_SCROLLBARS 221#ifdef HAVE_SCROLLBARS
118 if (Options & Opt_scrollBar) 222 if (Options & Opt_scrollBar)
119 scrollbar_setIdle (); /* set existence for size calculations */ 223 scrollBar.setIdle (); /* set existence for size calculations */
120#endif 224#endif
121 225
122 rxvt_Create_Windows (this, argc, argv); 226 rxvt_Create_Windows (this, argc, argv);
123 227
124 rxvt_init_xlocale (this); 228 init_xlocale ();
125 229
126 rxvt_scr_reset (this); /* initialize screen */ 230 scr_reset (); /* initialize screen */
127#ifdef RXVT_GRAPHICS 231#ifdef RXVT_GRAPHICS
128 rxvt_Gr_reset (this); /* reset graphics */ 232 rxvt_Gr_reset (this); /* reset graphics */
129#endif 233#endif
130 234
131#if 0 235#if 0
132#ifdef DEBUG_X 236#ifdef DEBUG_X
133 XSynchronize(Xdisplay, True); 237 XSynchronize(Xdisplay, True);
134 XSetErrorHandler((XErrorHandler) abort);
135#else
136 XSetErrorHandler((XErrorHandler) rxvt_xerror_handler);
137#endif 238#endif
138#endif 239#endif
139 240
140#ifdef HAVE_SCROLLBARS 241#ifdef HAVE_SCROLLBARS
141 if (Options & Opt_scrollBar) 242 if (Options & Opt_scrollBar)
153 } 254 }
154#endif 255#endif
155 XMapWindow (Xdisplay, TermWin.vt); 256 XMapWindow (Xdisplay, TermWin.vt);
156 XMapWindow (Xdisplay, TermWin.parent[0]); 257 XMapWindow (Xdisplay, TermWin.parent[0]);
157 258
158 rxvt_init_env (this); 259 init_env ();
159 rxvt_init_command (this, cmd_argv); 260 init_command (cmd_argv);
160 261
161 x_ev.start (Xfd, EVENT_READ); 262 x_ev.start (Xfd, EVENT_READ);
162 pty_ev.start (cmd_fd, EVENT_READ); 263 pty_ev.start (cmd_fd, EVENT_READ);
264
265 check_ev.start ();
266
267 process_x_events ();
163 268
164 return true; 269 return true;
165} 270}
166 271
167/* ------------------------------------------------------------------------- * 272/* ------------------------------------------------------------------------- *
173/* ARGSUSED */ 278/* ARGSUSED */
174/* EXTPROTO */ 279/* EXTPROTO */
175RETSIGTYPE 280RETSIGTYPE
176rxvt_Child_signal(int sig __attribute__ ((unused))) 281rxvt_Child_signal(int sig __attribute__ ((unused)))
177{ 282{
178 dR;
179 int pid, save_errno = errno; 283 int pid, save_errno = errno;
180
181 do {
182 errno = 0;
183 } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR); 284 while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR)
285 ;
286 errno = save_errno;
184 287
288#if 0
185 if (pid == R->cmd_pid) 289 if (pid == R->cmd_pid)
186 exit(EXIT_SUCCESS); 290 exit (EXIT_SUCCESS);
187 291#endif
188 errno = save_errno;
189 signal(SIGCHLD, rxvt_Child_signal);
190} 292}
191 293
192/* 294/*
193 * Catch a fatal signal and tidy up before quitting 295 * Catch a fatal signal and tidy up before quitting
194 */ 296 */
208/* INTPROTO */ 310/* INTPROTO */
209int 311int
210rxvt_xerror_handler(const Display * display 312rxvt_xerror_handler(const Display * display
211 __attribute__ ((unused)), const XErrorEvent * event) 313 __attribute__ ((unused)), const XErrorEvent * event)
212{ 314{
213 dR; 315 dR;
214 316
215 if (R->allowedxerror == -1) { 317 if (R->allowedxerror == -1)
216 R->allowedxerror = event->error_code; 318 R->allowedxerror = event->error_code;
217 return 0; /* ignored anyway */ 319 else
218 } 320 {
219 rxvt_print_error("XError: Request: %d . %d, Error: %d", 321 rxvt_print_error("XError: Request: %d . %d, Error: %d",
220 event->request_code, event->minor_code, 322 event->request_code, event->minor_code,
221 event->error_code); 323 event->error_code);
222/* XXX: probably should call rxvt_clean_exit() bypassing X routines */ 324
223 exit(EXIT_FAILURE); 325 R->destroy ();
224/* NOTREACHED */ 326 }
327
328 return 0;
225} 329}
226 330
227/*----------------------------------------------------------------------*/ 331/*----------------------------------------------------------------------*/
228/* 332/*
229 * Exit gracefully, clearing the utmp entry and restoring tty attributes 333 * Exit gracefully, clearing the utmp entry and restoring tty attributes
230 * TODO: if debugging, this should free up any known resources if we can 334 * TODO: if debugging, this should free up any known resources if we can
231 */ 335 */
232/* EXTPROTO */ 336/* EXTPROTO */
233void 337void
234rxvt_clean_exit(void) 338rxvt_clean_exit ()
235{ 339{
236 dR; 340 dR;
237 341
238#ifdef DEBUG_SCREEN 342 R->destroy ();
239 rxvt_scr_release(aR);
240#endif
241#ifndef NO_SETOWNER_TTYDEV
242 rxvt_privileged_ttydev(aR_ RESTORE);
243#endif
244#ifdef UTMP_SUPPORT
245 rxvt_privileged_utmp(aR_ RESTORE);
246#endif
247#ifdef USE_XIM
248 if (R->Input_Context != NULL) {
249 XDestroyIC(R->Input_Context);
250 R->Input_Context = NULL;
251 }
252#endif
253} 343}
254 344
255/* ------------------------------------------------------------------------- * 345/* ------------------------------------------------------------------------- *
256 * MEMORY ALLOCATION WRAPPERS * 346 * MEMORY ALLOCATION WRAPPERS *
257 * ------------------------------------------------------------------------- */ 347 * ------------------------------------------------------------------------- */
549/*----------------------------------------------------------------------*/ 639/*----------------------------------------------------------------------*/
550/* 640/*
551 * Tell the teletype handler what size the window is. 641 * Tell the teletype handler what size the window is.
552 * Called after a window size change. 642 * Called after a window size change.
553 */ 643 */
554/* EXTPROTO */
555void 644void
556rxvt_tt_winsize(int fd, unsigned short col, unsigned short row, int pid) 645rxvt_term::tt_winch ()
557{ 646{
558 struct winsize ws; 647 struct winsize ws;
559 648
560 if (fd < 0) 649 if (cmd_fd < 0)
561 return; 650 return;
651
562 ws.ws_col = col; 652 ws.ws_col = TermWin.ncol;
563 ws.ws_row = row; 653 ws.ws_row = TermWin.nrow;
564 ws.ws_xpixel = ws.ws_ypixel = 0; 654 ws.ws_xpixel = ws.ws_ypixel = 0;
565#ifndef DEBUG_SIZE 655#ifndef DEBUG_SIZE
566 (void)ioctl(fd, TIOCSWINSZ, &ws); 656 (void)ioctl (cmd_fd, TIOCSWINSZ, &ws);
567#else 657#else
568 if (ioctl(fd, TIOCSWINSZ, &ws) < 0) { 658 if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0)
569 D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); 659 D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd));
570 }
571# ifdef SIGWINCH 660# ifdef SIGWINCH
572 else if (pid) /* force through to the command */ 661 else if (cmd_pid) /* force through to the command */
573 kill(pid, SIGWINCH); 662 kill (cmd_pid, SIGWINCH);
574# endif 663# endif
575#endif 664#endif
576} 665}
577 666
578/*----------------------------------------------------------------------*/ 667/*----------------------------------------------------------------------*/
691 XSetWindowBackground(R->Xdisplay, R->TermWin.vt, 780 XSetWindowBackground(R->Xdisplay, R->TermWin.vt,
692 R->PixColors[Color_bg]); 781 R->PixColors[Color_bg]);
693 782
694/* handle Color_BD, scrollbar background, etc. */ 783/* handle Color_BD, scrollbar background, etc. */
695 784
696 rxvt_set_colorfgbg(aR); 785 rxvt_set_colorfgbg (aR);
697 rxvt_recolour_cursor(aR); 786 R->recolour_cursor ();
698/* the only reasonable way to enforce a clean update */ 787/* the only reasonable way to enforce a clean update */
699 rxvt_scr_poweron(aR); 788 rxvt_scr_poweron (aR);
700} 789}
701 790
702#else 791#else
703# define rxvt_set_window_color(aR_ idx,color) ((void)0) 792# define rxvt_set_window_color(aR_ idx,color) ((void)0)
704#endif /* XTERM_COLOR_CHANGE */ 793#endif /* XTERM_COLOR_CHANGE */
705 794
706/* EXTPROTO */
707void 795void
708rxvt_recolour_cursor(pR) 796rxvt_term::recolour_cursor ()
709{ 797{
710 rxvt_color xcol[2];
711
712#if TODO 798#if TODO
799 rxvt_color xcol[2];
800
713 xcol[0] = R->PixColors[Color_pointer]; 801 xcol[0] = PixColors[Color_pointer];
714 xcol[1] = R->PixColors[Color_bg]; 802 xcol[1] = PixColors[Color_bg];
715 XQueryColors(R->Xdisplay, XCMAP, xcol, 2); 803 XQueryColors (Xdisplay, XCMAP, xcol, 2);
716 XRecolorCursor(R->Xdisplay, R->TermWin_cursor, &(xcol[0]), &(xcol[1])); 804 XRecolorCursor (Xdisplay, TermWin_cursor, &(xcol[0]), &(xcol[1]));
717#endif 805#endif
718} 806}
719 807
720/*----------------------------------------------------------------------*/ 808/*----------------------------------------------------------------------*/
721/* 809/*
873 uint16_t old_ncol = R->prev_ncol; 961 uint16_t old_ncol = R->prev_ncol;
874 962
875 /* scr_reset only works on the primary screen */ 963 /* scr_reset only works on the primary screen */
876 if (R->old_height) /* this is not the first time through */ 964 if (R->old_height) /* this is not the first time through */
877 curr_screen = rxvt_scr_change_screen(aR_ PRIMARY); 965 curr_screen = rxvt_scr_change_screen(aR_ PRIMARY);
966
878 rxvt_scr_reset(aR); 967 R->scr_reset();
968
879 if (curr_screen >= 0) { /* this is not the first time through */ 969 if (curr_screen >= 0) { /* this is not the first time through */
880 rxvt_scr_change_screen(aR_ curr_screen); 970 rxvt_scr_change_screen(aR_ curr_screen);
881 rxvt_selection_check(aR_(old_ncol != R->TermWin.ncol ? 4 : 0)); 971 rxvt_selection_check(aR_(old_ncol != R->TermWin.ncol ? 4 : 0));
882 } 972 }
883 } 973 }
997 return; 1087 return;
998} 1088}
999 1089
1000/* INTPROTO */ 1090/* INTPROTO */
1001void 1091void
1002rxvt_setPreeditArea(pR_ XRectangle * preedit_rect, XRectangle * status_rect, 1092rxvt_setPreeditArea (pR_ XRectangle * preedit_rect, XRectangle * status_rect,
1003 XRectangle * needed_rect) 1093 XRectangle * needed_rect)
1004{ 1094{
1005 int mbh, vtx = 0; 1095 int mbh, vtx = 0;
1006 1096
1007 if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) 1097 if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right))
1008 vtx = scrollbar_TotalWidth(); 1098 vtx = scrollbar_TotalWidth();
1052rxvt_IMInstantiateCallback(Display * unused 1142rxvt_IMInstantiateCallback(Display * unused
1053 __attribute__ ((unused)), XPointer client_data 1143 __attribute__ ((unused)), XPointer client_data
1054 __attribute__ ((unused)), XPointer call_data 1144 __attribute__ ((unused)), XPointer call_data
1055 __attribute__ ((unused))) 1145 __attribute__ ((unused)))
1056{ 1146{
1057 dR; 1147 dR;
1058 int i, found, had_im; 1148 int i, found, had_im;
1059 const char *p; 1149 const char *p;
1060 char **s; 1150 char **s;
1061 char buf[IMBUFSIZ]; 1151 char buf[IMBUFSIZ];
1062 1152
1063 D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); 1153 D_MAIN((stderr, "rxvt_IMInstantiateCallback()"));
1064 if (R->Input_Context) 1154 if (R->Input_Context)
1065 return; 1155 return;
1066 1156
1067 found = had_im = 0; 1157#if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1158 if (R->rs[Rs_imLocale])
1159 setlocale (LC_CTYPE, R->rs[Rs_imLocale]);
1160#endif
1161
1068 p = R->rs[Rs_inputMethod]; 1162 p = R->rs[Rs_inputMethod];
1069 if (p && *p) { 1163 if (p && *p)
1070 had_im = 1; 1164 {
1165 bool found = false;
1166
1071 s = rxvt_splitcommastring(p); 1167 s = rxvt_splitcommastring (p);
1072 for (i = 0; s[i]; i++) { 1168 for (i = 0; s[i]; i++)
1169 {
1073 if (*s[i]) { 1170 if (*s[i])
1171 {
1074 STRCPY(buf, "@im="); 1172 STRCPY (buf, "@im=");
1075 STRNCAT(buf, s[i], IMBUFSIZ - 5); 1173 STRNCAT (buf, s[i], IMBUFSIZ - 5);
1076 if ((p = XSetLocaleModifiers(buf)) != NULL && *p 1174 if ((p = XSetLocaleModifiers (buf)) && *p
1077 && (rxvt_IM_get_IC(aR) == True)) { 1175 && rxvt_IM_get_IC (aR))
1176 {
1078 found = 1; 1177 found = true;
1079 break; 1178 break;
1080 } 1179 }
1081 } 1180 }
1082 } 1181 }
1083 for (i = 0; s[i]; i++) 1182 for (i = 0; s[i]; i++)
1084 free(s[i]); 1183 free(s[i]);
1085 free(s); 1184 free(s);
1086 } 1185
1087 if (found) 1186 if (found)
1088 return; 1187 goto done;
1188 }
1089 1189
1090/* try with XMODIFIERS env. var. */ 1190/* try with XMODIFIERS env. var. */
1091 if ((p = XSetLocaleModifiers("")) != NULL && *p) { 1191 if ((p = XSetLocaleModifiers ("")) && *p
1092 rxvt_IM_get_IC(aR); 1192 && rxvt_IM_get_IC (aR))
1093 return; 1193 goto done;
1094 }
1095 1194
1096/* try with no modifiers base IF the user didn't specify an IM */ 1195/* try with no modifiers base IF the user didn't specify an IM */
1097 if (!had_im && (p = XSetLocaleModifiers("@im=none")) != NULL && *p 1196 if ((p = XSetLocaleModifiers ("@im=none")) && *p
1098 && rxvt_IM_get_IC(aR) == True) 1197 && rxvt_IM_get_IC (aR) == True)
1099 return; 1198 goto done;
1199
1200done:
1201#if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1202 if (R->rs[Rs_imLocale])
1203 setlocale (LC_CTYPE, R->locale);
1204#endif
1100} 1205}
1101 1206
1102/* 1207/*
1103 * Try to open a XIM with the current modifiers, then see if we can 1208 * Try to open a XIM with the current modifiers, then see if we can
1104 * open a suitable preedit type 1209 * open a suitable preedit type

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines