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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines