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.12 2003/12/16 23:04:13 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> |
… | |
… | |
58 | { |
58 | { |
59 | free (p); |
59 | free (p); |
60 | } |
60 | } |
61 | |
61 | |
62 | rxvt_term::rxvt_term () |
62 | rxvt_term::rxvt_term () |
63 | : pty_ev (this, &rxvt_term::pty_cb), |
63 | : pty_ev (this, &rxvt_term::pty_cb), |
64 | #ifdef CURSOR_BLINK |
64 | #ifdef CURSOR_BLINK |
65 | blink_ev (this, &rxvt_term::blink_cb), |
65 | blink_ev (this, &rxvt_term::blink_cb), |
66 | #endif |
66 | #endif |
|
|
67 | #ifdef POINTER_BLANK |
|
|
68 | pointer_ev (this, &rxvt_term::pointer_cb), |
|
|
69 | #endif |
67 | x_ev (this, &rxvt_term::x_cb) |
70 | x_ev (this, &rxvt_term::x_cb), |
|
|
71 | destroy_ev (this, &rxvt_term::destroy_cb), |
|
|
72 | check_ev (this, &rxvt_term::check_cb), |
|
|
73 | incr_ev (this, &rxvt_term::incr_cb) |
68 | { |
74 | { |
69 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
75 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
70 | } |
76 | } |
71 | |
77 | |
72 | rxvt_term::~rxvt_term () |
78 | rxvt_term::~rxvt_term () |
73 | { |
79 | { |
|
|
80 | rxvt_scr_release (this); |
|
|
81 | |
|
|
82 | #ifndef NO_SETOWNER_TTYDEV |
|
|
83 | rxvt_privileged_ttydev (this, RESTORE); |
|
|
84 | #endif |
|
|
85 | #ifdef UTMP_SUPPORT |
|
|
86 | rxvt_privileged_utmp (this, RESTORE); |
|
|
87 | #endif |
|
|
88 | #ifdef USE_XIM |
|
|
89 | if (Input_Context != NULL) |
|
|
90 | { |
|
|
91 | XDestroyIC (Input_Context); |
|
|
92 | Input_Context = NULL; |
|
|
93 | } |
|
|
94 | #endif |
|
|
95 | |
|
|
96 | if (cmd_fd >= 0) |
|
|
97 | close (cmd_fd); |
|
|
98 | |
|
|
99 | if (Xfd >= 0) |
|
|
100 | XCloseDisplay (Xdisplay); |
|
|
101 | |
74 | delete PixColors; |
102 | delete PixColors; |
|
|
103 | } |
|
|
104 | |
|
|
105 | void |
|
|
106 | rxvt_term::destroy () |
|
|
107 | { |
|
|
108 | check_ev.stop (); |
|
|
109 | pty_ev.stop (); |
|
|
110 | x_ev.stop (); |
|
|
111 | #ifdef CURSOR_BLINK |
|
|
112 | blink_ev.stop (); |
|
|
113 | #endif |
|
|
114 | #ifdef POINTER_BLANK |
|
|
115 | pointer_ev.stop (); |
|
|
116 | #endif |
|
|
117 | |
|
|
118 | destroy_ev.start (0); |
|
|
119 | } |
|
|
120 | |
|
|
121 | void |
|
|
122 | rxvt_term::destroy_cb (time_watcher &w) |
|
|
123 | { |
|
|
124 | SET_R (this); |
|
|
125 | |
|
|
126 | delete this; |
75 | } |
127 | } |
76 | |
128 | |
77 | /*----------------------------------------------------------------------*/ |
129 | /*----------------------------------------------------------------------*/ |
78 | /* rxvt_init() */ |
130 | /* rxvt_init() */ |
79 | /* LIBPROTO */ |
131 | /* LIBPROTO */ |
80 | rxvt_t |
132 | rxvt_t |
81 | rxvt_init(int argc, const char *const *argv) |
133 | rxvt_init (int argc, const char *const *argv) |
82 | { |
134 | { |
83 | SET_R(new rxvt_term); |
135 | SET_R (new rxvt_term); |
84 | dR; |
136 | dR; |
85 | |
137 | |
86 | if (!R->init_vars () || !R->init (argc, argv)) |
138 | if (!R->init_vars () || !R->init (argc, argv)) |
87 | { |
139 | { |
88 | delete R; |
140 | delete R; |
89 | return NULL; |
141 | return NULL; |
90 | } |
142 | } |
91 | |
143 | |
92 | return R; |
144 | return R; |
|
|
145 | } |
|
|
146 | |
|
|
147 | /* EXTPROTO */ |
|
|
148 | void |
|
|
149 | rxvt_init_signals () |
|
|
150 | { |
|
|
151 | /* install exit handler for cleanup */ |
|
|
152 | #if 0 |
|
|
153 | #ifdef HAVE_ATEXIT |
|
|
154 | atexit(rxvt_clean_exit); |
|
|
155 | #else |
|
|
156 | #endif |
|
|
157 | #endif |
|
|
158 | |
|
|
159 | struct sigaction sa; |
|
|
160 | |
|
|
161 | sigfillset (&sa.sa_mask); |
|
|
162 | sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; |
|
|
163 | sa.sa_handler = SIG_IGN; sigaction (SIGHUP , &sa, 0); |
|
|
164 | sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0); |
|
|
165 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0); |
|
|
166 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0); |
|
|
167 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0); |
|
|
168 | sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0); |
|
|
169 | |
|
|
170 | /* need to trap SIGURG for SVR4 (Unixware) rlogin */ |
|
|
171 | /* signal (SIGURG, SIG_DFL); */ |
|
|
172 | |
|
|
173 | XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler); |
|
|
174 | //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler); |
93 | } |
175 | } |
94 | |
176 | |
95 | bool |
177 | bool |
96 | rxvt_term::init (int argc, const char *const *argv) |
178 | rxvt_term::init (int argc, const char *const *argv) |
97 | { |
179 | { |
… | |
… | |
129 | #endif |
211 | #endif |
130 | |
212 | |
131 | #if 0 |
213 | #if 0 |
132 | #ifdef DEBUG_X |
214 | #ifdef DEBUG_X |
133 | XSynchronize(Xdisplay, True); |
215 | XSynchronize(Xdisplay, True); |
134 | XSetErrorHandler((XErrorHandler) abort); |
|
|
135 | #else |
|
|
136 | XSetErrorHandler((XErrorHandler) rxvt_xerror_handler); |
|
|
137 | #endif |
216 | #endif |
138 | #endif |
217 | #endif |
139 | |
218 | |
140 | #ifdef HAVE_SCROLLBARS |
219 | #ifdef HAVE_SCROLLBARS |
141 | if (Options & Opt_scrollBar) |
220 | if (Options & Opt_scrollBar) |
… | |
… | |
158 | rxvt_init_env (this); |
237 | rxvt_init_env (this); |
159 | rxvt_init_command (this, cmd_argv); |
238 | rxvt_init_command (this, cmd_argv); |
160 | |
239 | |
161 | x_ev.start (Xfd, EVENT_READ); |
240 | x_ev.start (Xfd, EVENT_READ); |
162 | pty_ev.start (cmd_fd, EVENT_READ); |
241 | pty_ev.start (cmd_fd, EVENT_READ); |
|
|
242 | |
|
|
243 | check_ev.start (); |
|
|
244 | |
|
|
245 | process_x_events (); |
163 | |
246 | |
164 | return true; |
247 | return true; |
165 | } |
248 | } |
166 | |
249 | |
167 | /* ------------------------------------------------------------------------- * |
250 | /* ------------------------------------------------------------------------- * |
… | |
… | |
173 | /* ARGSUSED */ |
256 | /* ARGSUSED */ |
174 | /* EXTPROTO */ |
257 | /* EXTPROTO */ |
175 | RETSIGTYPE |
258 | RETSIGTYPE |
176 | rxvt_Child_signal(int sig __attribute__ ((unused))) |
259 | rxvt_Child_signal(int sig __attribute__ ((unused))) |
177 | { |
260 | { |
178 | dR; |
|
|
179 | int pid, save_errno = errno; |
261 | int pid, save_errno = errno; |
180 | |
|
|
181 | do { |
|
|
182 | errno = 0; |
|
|
183 | } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR); |
262 | while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR) |
|
|
263 | ; |
|
|
264 | errno = save_errno; |
184 | |
265 | |
|
|
266 | #if 0 |
185 | if (pid == R->cmd_pid) |
267 | if (pid == R->cmd_pid) |
186 | exit(EXIT_SUCCESS); |
268 | exit (EXIT_SUCCESS); |
187 | |
269 | #endif |
188 | errno = save_errno; |
|
|
189 | signal(SIGCHLD, rxvt_Child_signal); |
|
|
190 | } |
270 | } |
191 | |
271 | |
192 | /* |
272 | /* |
193 | * Catch a fatal signal and tidy up before quitting |
273 | * Catch a fatal signal and tidy up before quitting |
194 | */ |
274 | */ |
… | |
… | |
208 | /* INTPROTO */ |
288 | /* INTPROTO */ |
209 | int |
289 | int |
210 | rxvt_xerror_handler(const Display * display |
290 | rxvt_xerror_handler(const Display * display |
211 | __attribute__ ((unused)), const XErrorEvent * event) |
291 | __attribute__ ((unused)), const XErrorEvent * event) |
212 | { |
292 | { |
213 | dR; |
293 | dR; |
214 | |
294 | |
215 | if (R->allowedxerror == -1) { |
295 | if (R->allowedxerror == -1) |
216 | R->allowedxerror = event->error_code; |
296 | R->allowedxerror = event->error_code; |
217 | return 0; /* ignored anyway */ |
297 | else |
218 | } |
298 | { |
219 | rxvt_print_error("XError: Request: %d . %d, Error: %d", |
299 | rxvt_print_error("XError: Request: %d . %d, Error: %d", |
220 | event->request_code, event->minor_code, |
300 | event->request_code, event->minor_code, |
221 | event->error_code); |
301 | event->error_code); |
222 | /* XXX: probably should call rxvt_clean_exit() bypassing X routines */ |
302 | |
223 | exit(EXIT_FAILURE); |
303 | R->destroy (); |
224 | /* NOTREACHED */ |
304 | } |
|
|
305 | |
|
|
306 | return 0; |
225 | } |
307 | } |
226 | |
308 | |
227 | /*----------------------------------------------------------------------*/ |
309 | /*----------------------------------------------------------------------*/ |
228 | /* |
310 | /* |
229 | * Exit gracefully, clearing the utmp entry and restoring tty attributes |
311 | * Exit gracefully, clearing the utmp entry and restoring tty attributes |
230 | * TODO: if debugging, this should free up any known resources if we can |
312 | * TODO: if debugging, this should free up any known resources if we can |
231 | */ |
313 | */ |
232 | /* EXTPROTO */ |
314 | /* EXTPROTO */ |
233 | void |
315 | void |
234 | rxvt_clean_exit(void) |
316 | rxvt_clean_exit () |
235 | { |
317 | { |
236 | dR; |
318 | dR; |
237 | |
319 | |
238 | #ifdef DEBUG_SCREEN |
320 | 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 | } |
321 | } |
254 | |
322 | |
255 | /* ------------------------------------------------------------------------- * |
323 | /* ------------------------------------------------------------------------- * |
256 | * MEMORY ALLOCATION WRAPPERS * |
324 | * MEMORY ALLOCATION WRAPPERS * |
257 | * ------------------------------------------------------------------------- */ |
325 | * ------------------------------------------------------------------------- */ |
… | |
… | |
549 | /*----------------------------------------------------------------------*/ |
617 | /*----------------------------------------------------------------------*/ |
550 | /* |
618 | /* |
551 | * Tell the teletype handler what size the window is. |
619 | * Tell the teletype handler what size the window is. |
552 | * Called after a window size change. |
620 | * Called after a window size change. |
553 | */ |
621 | */ |
554 | /* EXTPROTO */ |
|
|
555 | void |
622 | void |
556 | rxvt_tt_winsize(int fd, unsigned short col, unsigned short row, int pid) |
623 | rxvt_term::tt_winch () |
557 | { |
624 | { |
558 | struct winsize ws; |
625 | struct winsize ws; |
559 | |
626 | |
560 | if (fd < 0) |
627 | if (cmd_fd < 0) |
561 | return; |
628 | return; |
|
|
629 | |
562 | ws.ws_col = col; |
630 | ws.ws_col = TermWin.ncol; |
563 | ws.ws_row = row; |
631 | ws.ws_row = TermWin.nrow; |
564 | ws.ws_xpixel = ws.ws_ypixel = 0; |
632 | ws.ws_xpixel = ws.ws_ypixel = 0; |
565 | #ifndef DEBUG_SIZE |
633 | #ifndef DEBUG_SIZE |
566 | (void)ioctl(fd, TIOCSWINSZ, &ws); |
634 | (void)ioctl (cmd_fd, TIOCSWINSZ, &ws); |
567 | #else |
635 | #else |
568 | if (ioctl(fd, TIOCSWINSZ, &ws) < 0) { |
636 | if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0) |
569 | D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); |
637 | D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); |
570 | } |
|
|
571 | # ifdef SIGWINCH |
638 | # ifdef SIGWINCH |
572 | else if (pid) /* force through to the command */ |
639 | else if (cmd_pid) /* force through to the command */ |
573 | kill(pid, SIGWINCH); |
640 | kill (cmd_pid, SIGWINCH); |
574 | # endif |
641 | # endif |
575 | #endif |
642 | #endif |
576 | } |
643 | } |
577 | |
644 | |
578 | /*----------------------------------------------------------------------*/ |
645 | /*----------------------------------------------------------------------*/ |
… | |
… | |
997 | return; |
1064 | return; |
998 | } |
1065 | } |
999 | |
1066 | |
1000 | /* INTPROTO */ |
1067 | /* INTPROTO */ |
1001 | void |
1068 | void |
1002 | rxvt_setPreeditArea(pR_ XRectangle * preedit_rect, XRectangle * status_rect, |
1069 | rxvt_setPreeditArea (pR_ XRectangle * preedit_rect, XRectangle * status_rect, |
1003 | XRectangle * needed_rect) |
1070 | XRectangle * needed_rect) |
1004 | { |
1071 | { |
1005 | int mbh, vtx = 0; |
1072 | int mbh, vtx = 0; |
1006 | |
1073 | |
1007 | if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) |
1074 | if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) |
1008 | vtx = scrollbar_TotalWidth(); |
1075 | vtx = scrollbar_TotalWidth(); |
… | |
… | |
1052 | rxvt_IMInstantiateCallback(Display * unused |
1119 | rxvt_IMInstantiateCallback(Display * unused |
1053 | __attribute__ ((unused)), XPointer client_data |
1120 | __attribute__ ((unused)), XPointer client_data |
1054 | __attribute__ ((unused)), XPointer call_data |
1121 | __attribute__ ((unused)), XPointer call_data |
1055 | __attribute__ ((unused))) |
1122 | __attribute__ ((unused))) |
1056 | { |
1123 | { |
1057 | dR; |
1124 | dR; |
1058 | int i, found, had_im; |
1125 | int i, found, had_im; |
1059 | const char *p; |
1126 | const char *p; |
1060 | char **s; |
1127 | char **s; |
1061 | char buf[IMBUFSIZ]; |
1128 | char buf[IMBUFSIZ]; |
1062 | |
1129 | |
1063 | D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); |
1130 | D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); |
1064 | if (R->Input_Context) |
1131 | if (R->Input_Context) |
1065 | return; |
1132 | return; |
1066 | |
1133 | |
1067 | found = had_im = 0; |
|
|
1068 | p = R->rs[Rs_inputMethod]; |
1134 | p = R->rs[Rs_inputMethod]; |
1069 | if (p && *p) { |
1135 | if (p && *p) |
1070 | had_im = 1; |
1136 | { |
|
|
1137 | bool found = false; |
|
|
1138 | |
1071 | s = rxvt_splitcommastring(p); |
1139 | s = rxvt_splitcommastring (p); |
1072 | for (i = 0; s[i]; i++) { |
1140 | for (i = 0; s[i]; i++) |
|
|
1141 | { |
1073 | if (*s[i]) { |
1142 | if (*s[i]) |
|
|
1143 | { |
1074 | STRCPY(buf, "@im="); |
1144 | STRCPY (buf, "@im="); |
1075 | STRNCAT(buf, s[i], IMBUFSIZ - 5); |
1145 | STRNCAT (buf, s[i], IMBUFSIZ - 5); |
1076 | if ((p = XSetLocaleModifiers(buf)) != NULL && *p |
1146 | if ((p = XSetLocaleModifiers (buf)) && *p |
1077 | && (rxvt_IM_get_IC(aR) == True)) { |
1147 | && rxvt_IM_get_IC (aR)) |
|
|
1148 | { |
1078 | found = 1; |
1149 | found = true; |
1079 | break; |
1150 | break; |
1080 | } |
1151 | } |
1081 | } |
1152 | } |
1082 | } |
1153 | } |
1083 | for (i = 0; s[i]; i++) |
1154 | for (i = 0; s[i]; i++) |
1084 | free(s[i]); |
1155 | free(s[i]); |
1085 | free(s); |
1156 | free(s); |
1086 | } |
1157 | |
1087 | if (found) |
1158 | if (found) |
1088 | return; |
1159 | return; |
|
|
1160 | } |
1089 | |
1161 | |
1090 | /* try with XMODIFIERS env. var. */ |
1162 | /* try with XMODIFIERS env. var. */ |
1091 | if ((p = XSetLocaleModifiers("")) != NULL && *p) { |
1163 | if ((p = XSetLocaleModifiers ("")) && *p |
1092 | rxvt_IM_get_IC(aR); |
1164 | && rxvt_IM_get_IC (aR)) |
1093 | return; |
1165 | return; |
1094 | } |
|
|
1095 | |
1166 | |
1096 | /* try with no modifiers base IF the user didn't specify an IM */ |
1167 | /* try with no modifiers base IF the user didn't specify an IM */ |
1097 | if (!had_im && (p = XSetLocaleModifiers("@im=none")) != NULL && *p |
1168 | if ((p = XSetLocaleModifiers ("@im=none")) && *p |
1098 | && rxvt_IM_get_IC(aR) == True) |
1169 | && rxvt_IM_get_IC (aR) == True) |
1099 | return; |
1170 | return; |
1100 | } |
1171 | } |
1101 | |
1172 | |
1102 | /* |
1173 | /* |
1103 | * Try to open a XIM with the current modifiers, then see if we can |
1174 | * Try to open a XIM with the current modifiers, then see if we can |
1104 | * open a suitable preedit type |
1175 | * open a suitable preedit type |