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