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.3 by pcg, Tue Nov 25 11:52:42 2003 UTC vs.
Revision 1.12 by pcg, Tue Dec 16 23:04:13 2003 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: main.c 2 * File: main.c
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: main.C,v 1.3 2003/11/25 11:52:42 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>
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
47rxvt_term::rxvt_term()
48: pty_ev(this, &rxvt_term::pty_cb),
49 x_ev (this, &rxvt_term::x_cb)
50{
51 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
52}
53
54void * 47void *
55rxvt_term::operator new (size_t s) 48rxvt_term::operator new (size_t s)
56{ 49{
57 void *p = malloc (s); 50 void *p = malloc (s);
58 51
62 55
63void 56void
64rxvt_term::operator delete (void *p, size_t s) 57rxvt_term::operator delete (void *p, size_t s)
65{ 58{
66 free (p); 59 free (p);
60}
61
62rxvt_term::rxvt_term ()
63: pty_ev (this, &rxvt_term::pty_cb),
64#ifdef CURSOR_BLINK
65 blink_ev (this, &rxvt_term::blink_cb),
66#endif
67#ifdef POINTER_BLANK
68 pointer_ev (this, &rxvt_term::pointer_cb),
69#endif
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)
74{
75 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
76}
77
78rxvt_term::~rxvt_term ()
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
102 delete PixColors;
103}
104
105void
106rxvt_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
121void
122rxvt_term::destroy_cb (time_watcher &w)
123{
124 SET_R (this);
125
126 delete this;
67} 127}
68 128
69/*----------------------------------------------------------------------*/ 129/*----------------------------------------------------------------------*/
70/* rxvt_init() */ 130/* rxvt_init() */
71/* LIBPROTO */ 131/* LIBPROTO */
72rxvt_t 132rxvt_t
73rxvt_init(int argc, const char *const *argv) 133rxvt_init (int argc, const char *const *argv)
74{ 134{
75 const char **cmd_argv;
76
77 SET_R(new rxvt_term); 135 SET_R (new rxvt_term);
78 dR; 136 dR;
79 137
80 if (rxvt_init_vars(aR) < 0) 138 if (!R->init_vars () || !R->init (argc, argv))
81 { 139 {
82 free(R); 140 delete R;
83 return NULL; 141 return NULL;
84 } 142 }
85 143
86/* 144 return R;
145}
146
147/* EXTPROTO */
148void
149rxvt_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);
175}
176
177bool
178rxvt_term::init (int argc, const char *const *argv)
179{
180 dR;//TODO (scrollbar, setidle)
181
182 /*
87 * Save and then give up any super-user privileges 183 * Save and then give up any super-user privileges
88 * If we need privileges in any area then we must specifically request it. 184 * If we need privileges in any area then we must specifically request it.
89 * We should only need to be root in these cases: 185 * We should only need to be root in these cases:
90 * 1. write utmp entries on some systems 186 * 1. write utmp entries on some systems
91 * 2. chown tty on some systems 187 * 2. chown tty on some systems
92 */ 188 */
93 rxvt_privileges(aR_ SAVE); 189 rxvt_privileges (this, SAVE);
94 rxvt_privileges(aR_ IGNORE); 190 rxvt_privileges (this, IGNORE);
95 191
96 rxvt_init_secondary(aR); 192 rxvt_init_secondary (this);
97 193
98 cmd_argv = rxvt_init_resources(aR_ argc, argv); 194 const char **cmd_argv = rxvt_init_resources (this, argc, argv);
99
100 R->x_ev.start (ConnectionNumber (R->Xdisplay), EVENT_READ);
101 195
102#if (MENUBAR_MAX) 196#if (MENUBAR_MAX)
103 rxvt_menubar_read(aR_ R->rs[Rs_menu]); 197 rxvt_menubar_read (this, rs[Rs_menu]);
104#endif 198#endif
105#ifdef HAVE_SCROLLBARS 199#ifdef HAVE_SCROLLBARS
106 if (R->Options & Opt_scrollBar) 200 if (Options & Opt_scrollBar)
107 scrollbar_setIdle(); /* set existence for size calculations */ 201 scrollbar_setIdle (); /* set existence for size calculations */
108#endif 202#endif
109 203
110 rxvt_Create_Windows(aR_ argc, argv); 204 rxvt_Create_Windows (this, argc, argv);
111 205
112 rxvt_init_xlocale(aR); 206 rxvt_init_xlocale (this);
113 207
114 rxvt_scr_reset(aR); /* initialize screen */ 208 rxvt_scr_reset (this); /* initialize screen */
115#ifdef RXVT_GRAPHICS 209#ifdef RXVT_GRAPHICS
116 rxvt_Gr_reset(aR); /* reset graphics */ 210 rxvt_Gr_reset (this); /* reset graphics */
117#endif 211#endif
118 212
119#if 0 213#if 0
120#ifdef DEBUG_X 214#ifdef DEBUG_X
121 XSynchronize(R->Xdisplay, True); 215 XSynchronize(Xdisplay, True);
122 XSetErrorHandler((XErrorHandler) abort);
123#else
124 XSetErrorHandler((XErrorHandler) rxvt_xerror_handler);
125#endif 216#endif
126#endif 217#endif
127 218
128#ifdef HAVE_SCROLLBARS 219#ifdef HAVE_SCROLLBARS
129 if (R->Options & Opt_scrollBar) 220 if (Options & Opt_scrollBar)
130 rxvt_Resize_scrollBar(aR); /* create and map scrollbar */ 221 rxvt_Resize_scrollBar (this); /* create and map scrollbar */
131#endif 222#endif
132#if (MENUBAR_MAX) 223#if (MENUBAR_MAX)
133 if (menubar_visible(r)) 224 if (menubar_visible(r))
134 XMapWindow(R->Xdisplay, R->menuBar.win); 225 XMapWindow (Xdisplay, menuBar.win);
135#endif 226#endif
136#ifdef TRANSPARENT 227#ifdef TRANSPARENT
137 if (R->Options & Opt_transparent) 228 if (Options & Opt_transparent)
138 { 229 {
139 XSelectInput(R->Xdisplay, Xroot, PropertyChangeMask); 230 XSelectInput (Xdisplay, Xroot, PropertyChangeMask);
140 rxvt_check_our_parents(aR); 231 rxvt_check_our_parents (this);
141 } 232 }
142#endif 233#endif
143 XMapWindow(R->Xdisplay, R->TermWin.vt); 234 XMapWindow (Xdisplay, TermWin.vt);
144 XMapWindow(R->Xdisplay, R->TermWin.parent[0]); 235 XMapWindow (Xdisplay, TermWin.parent[0]);
145 236
146 rxvt_init_env(aR); 237 rxvt_init_env (this);
147 rxvt_init_command(aR_ cmd_argv); 238 rxvt_init_command (this, cmd_argv);
148 239
240 x_ev.start (Xfd, EVENT_READ);
149 R->pty_ev.start (R->cmd_fd, EVENT_READ); 241 pty_ev.start (cmd_fd, EVENT_READ);
150 242
151 return R; 243 check_ev.start ();
244
245 process_x_events ();
246
247 return true;
152} 248}
153 249
154/* ------------------------------------------------------------------------- * 250/* ------------------------------------------------------------------------- *
155 * SIGNAL HANDLING & EXIT HANDLER * 251 * SIGNAL HANDLING & EXIT HANDLER *
156 * ------------------------------------------------------------------------- */ 252 * ------------------------------------------------------------------------- */
160/* ARGSUSED */ 256/* ARGSUSED */
161/* EXTPROTO */ 257/* EXTPROTO */
162RETSIGTYPE 258RETSIGTYPE
163rxvt_Child_signal(int sig __attribute__ ((unused))) 259rxvt_Child_signal(int sig __attribute__ ((unused)))
164{ 260{
165 dR;
166 int pid, save_errno = errno; 261 int pid, save_errno = errno;
167
168 do {
169 errno = 0;
170 } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR); 262 while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR)
263 ;
264 errno = save_errno;
171 265
266#if 0
172 if (pid == R->cmd_pid) 267 if (pid == R->cmd_pid)
173 exit(EXIT_SUCCESS); 268 exit (EXIT_SUCCESS);
174 269#endif
175 errno = save_errno;
176 signal(SIGCHLD, rxvt_Child_signal);
177} 270}
178 271
179/* 272/*
180 * Catch a fatal signal and tidy up before quitting 273 * Catch a fatal signal and tidy up before quitting
181 */ 274 */
195/* INTPROTO */ 288/* INTPROTO */
196int 289int
197rxvt_xerror_handler(const Display * display 290rxvt_xerror_handler(const Display * display
198 __attribute__ ((unused)), const XErrorEvent * event) 291 __attribute__ ((unused)), const XErrorEvent * event)
199{ 292{
200 dR; 293 dR;
201 294
202 if (R->allowedxerror == -1) { 295 if (R->allowedxerror == -1)
203 R->allowedxerror = event->error_code; 296 R->allowedxerror = event->error_code;
204 return 0; /* ignored anyway */ 297 else
205 } 298 {
206 rxvt_print_error("XError: Request: %d . %d, Error: %d", 299 rxvt_print_error("XError: Request: %d . %d, Error: %d",
207 event->request_code, event->minor_code, 300 event->request_code, event->minor_code,
208 event->error_code); 301 event->error_code);
209/* XXX: probably should call rxvt_clean_exit() bypassing X routines */ 302
210 exit(EXIT_FAILURE); 303 R->destroy ();
211/* NOTREACHED */ 304 }
305
306 return 0;
212} 307}
213 308
214/*----------------------------------------------------------------------*/ 309/*----------------------------------------------------------------------*/
215/* 310/*
216 * Exit gracefully, clearing the utmp entry and restoring tty attributes 311 * Exit gracefully, clearing the utmp entry and restoring tty attributes
217 * 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
218 */ 313 */
219/* EXTPROTO */ 314/* EXTPROTO */
220void 315void
221rxvt_clean_exit(void) 316rxvt_clean_exit ()
222{ 317{
223 dR; 318 dR;
224 319
225#ifdef DEBUG_SCREEN 320 R->destroy ();
226 rxvt_scr_release(aR);
227#endif
228#ifndef NO_SETOWNER_TTYDEV
229 rxvt_privileged_ttydev(aR_ RESTORE);
230#endif
231#ifdef UTMP_SUPPORT
232 rxvt_privileged_utmp(aR_ RESTORE);
233#endif
234#ifdef USE_XIM
235 if (R->Input_Context != NULL) {
236 XDestroyIC(R->Input_Context);
237 R->Input_Context = NULL;
238 }
239#endif
240} 321}
241 322
242/* ------------------------------------------------------------------------- * 323/* ------------------------------------------------------------------------- *
243 * MEMORY ALLOCATION WRAPPERS * 324 * MEMORY ALLOCATION WRAPPERS *
244 * ------------------------------------------------------------------------- */ 325 * ------------------------------------------------------------------------- */
536/*----------------------------------------------------------------------*/ 617/*----------------------------------------------------------------------*/
537/* 618/*
538 * Tell the teletype handler what size the window is. 619 * Tell the teletype handler what size the window is.
539 * Called after a window size change. 620 * Called after a window size change.
540 */ 621 */
541/* EXTPROTO */
542void 622void
543rxvt_tt_winsize(int fd, unsigned short col, unsigned short row, int pid) 623rxvt_term::tt_winch ()
544{ 624{
545 struct winsize ws; 625 struct winsize ws;
546 626
547 if (fd < 0) 627 if (cmd_fd < 0)
548 return; 628 return;
629
549 ws.ws_col = col; 630 ws.ws_col = TermWin.ncol;
550 ws.ws_row = row; 631 ws.ws_row = TermWin.nrow;
551 ws.ws_xpixel = ws.ws_ypixel = 0; 632 ws.ws_xpixel = ws.ws_ypixel = 0;
552#ifndef DEBUG_SIZE 633#ifndef DEBUG_SIZE
553 (void)ioctl(fd, TIOCSWINSZ, &ws); 634 (void)ioctl (cmd_fd, TIOCSWINSZ, &ws);
554#else 635#else
555 if (ioctl(fd, TIOCSWINSZ, &ws) < 0) { 636 if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0)
556 D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); 637 D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd));
557 }
558# ifdef SIGWINCH 638# ifdef SIGWINCH
559 else if (pid) /* force through to the command */ 639 else if (cmd_pid) /* force through to the command */
560 kill(pid, SIGWINCH); 640 kill (cmd_pid, SIGWINCH);
561# endif 641# endif
562#endif 642#endif
563} 643}
564 644
565/*----------------------------------------------------------------------*/ 645/*----------------------------------------------------------------------*/
984 return; 1064 return;
985} 1065}
986 1066
987/* INTPROTO */ 1067/* INTPROTO */
988void 1068void
989rxvt_setPreeditArea(pR_ XRectangle * preedit_rect, XRectangle * status_rect, 1069rxvt_setPreeditArea (pR_ XRectangle * preedit_rect, XRectangle * status_rect,
990 XRectangle * needed_rect) 1070 XRectangle * needed_rect)
991{ 1071{
992 int mbh, vtx = 0; 1072 int mbh, vtx = 0;
993 1073
994 if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) 1074 if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right))
995 vtx = scrollbar_TotalWidth(); 1075 vtx = scrollbar_TotalWidth();
1039rxvt_IMInstantiateCallback(Display * unused 1119rxvt_IMInstantiateCallback(Display * unused
1040 __attribute__ ((unused)), XPointer client_data 1120 __attribute__ ((unused)), XPointer client_data
1041 __attribute__ ((unused)), XPointer call_data 1121 __attribute__ ((unused)), XPointer call_data
1042 __attribute__ ((unused))) 1122 __attribute__ ((unused)))
1043{ 1123{
1044 dR; 1124 dR;
1045 int i, found, had_im; 1125 int i, found, had_im;
1046 const char *p; 1126 const char *p;
1047 char **s; 1127 char **s;
1048 char buf[IMBUFSIZ]; 1128 char buf[IMBUFSIZ];
1049 1129
1050 D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); 1130 D_MAIN((stderr, "rxvt_IMInstantiateCallback()"));
1051 if (R->Input_Context) 1131 if (R->Input_Context)
1052 return; 1132 return;
1053 1133
1054 found = had_im = 0;
1055 p = R->rs[Rs_inputMethod]; 1134 p = R->rs[Rs_inputMethod];
1056 if (p && *p) { 1135 if (p && *p)
1057 had_im = 1; 1136 {
1137 bool found = false;
1138
1058 s = rxvt_splitcommastring(p); 1139 s = rxvt_splitcommastring (p);
1059 for (i = 0; s[i]; i++) { 1140 for (i = 0; s[i]; i++)
1141 {
1060 if (*s[i]) { 1142 if (*s[i])
1143 {
1061 STRCPY(buf, "@im="); 1144 STRCPY (buf, "@im=");
1062 STRNCAT(buf, s[i], IMBUFSIZ - 5); 1145 STRNCAT (buf, s[i], IMBUFSIZ - 5);
1063 if ((p = XSetLocaleModifiers(buf)) != NULL && *p 1146 if ((p = XSetLocaleModifiers (buf)) && *p
1064 && (rxvt_IM_get_IC(aR) == True)) { 1147 && rxvt_IM_get_IC (aR))
1148 {
1065 found = 1; 1149 found = true;
1066 break; 1150 break;
1067 } 1151 }
1068 } 1152 }
1069 } 1153 }
1070 for (i = 0; s[i]; i++) 1154 for (i = 0; s[i]; i++)
1071 free(s[i]); 1155 free(s[i]);
1072 free(s); 1156 free(s);
1073 } 1157
1074 if (found) 1158 if (found)
1075 return; 1159 return;
1160 }
1076 1161
1077/* try with XMODIFIERS env. var. */ 1162/* try with XMODIFIERS env. var. */
1078 if ((p = XSetLocaleModifiers("")) != NULL && *p) { 1163 if ((p = XSetLocaleModifiers ("")) && *p
1079 rxvt_IM_get_IC(aR); 1164 && rxvt_IM_get_IC (aR))
1080 return; 1165 return;
1081 }
1082 1166
1083/* 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 */
1084 if (!had_im && (p = XSetLocaleModifiers("@im=none")) != NULL && *p 1168 if ((p = XSetLocaleModifiers ("@im=none")) && *p
1085 && rxvt_IM_get_IC(aR) == True) 1169 && rxvt_IM_get_IC (aR) == True)
1086 return; 1170 return;
1087} 1171}
1088 1172
1089/* 1173/*
1090 * 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
1091 * open a suitable preedit type 1175 * open a suitable preedit type

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines