--- rxvt-unicode/src/main.C 2003/11/25 11:52:42 1.3 +++ rxvt-unicode/src/main.C 2003/12/08 23:14:40 1.11 @@ -1,7 +1,7 @@ /*--------------------------------*-C-*---------------------------------* * File: main.c *----------------------------------------------------------------------* - * $Id: main.C,v 1.3 2003/11/25 11:52:42 pcg Exp $ + * $Id: main.C,v 1.11 2003/12/08 23:14:40 pcg Exp $ * * All portions of code are copyright by their respective author/s. * Copyright (c) 1992 John Bovey, University of Kent at Canterbury @@ -44,13 +44,6 @@ # include #endif -rxvt_term::rxvt_term() -: pty_ev(this, &rxvt_term::pty_cb), - x_ev (this, &rxvt_term::x_cb) -{ - cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; -} - void * rxvt_term::operator new (size_t s) { @@ -66,89 +59,191 @@ free (p); } +rxvt_term::rxvt_term () +: pty_ev (this, &rxvt_term::pty_cb), +#ifdef CURSOR_BLINK + blink_ev (this, &rxvt_term::blink_cb), +#endif +#ifdef POINTER_BLANK + pointer_ev (this, &rxvt_term::pointer_cb), +#endif + x_ev (this, &rxvt_term::x_cb), + destroy_ev (this, &rxvt_term::destroy_cb), + check_ev (this, &rxvt_term::check_cb) +{ + cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; +} + +rxvt_term::~rxvt_term () +{ + rxvt_scr_release (this); + +#ifndef NO_SETOWNER_TTYDEV + rxvt_privileged_ttydev (this, RESTORE); +#endif +#ifdef UTMP_SUPPORT + rxvt_privileged_utmp (this, RESTORE); +#endif +#ifdef USE_XIM + if (Input_Context != NULL) + { + XDestroyIC (Input_Context); + Input_Context = NULL; + } +#endif + + if (cmd_fd >= 0) + close (cmd_fd); + + if (Xfd >= 0) + XCloseDisplay (Xdisplay); + + delete PixColors; +} + +void +rxvt_term::destroy () +{ + check_ev.stop (); + pty_ev.stop (); + x_ev.stop (); +#ifdef CURSOR_BLINK + blink_ev.stop (); +#endif +#ifdef POINTER_BLANK + pointer_ev.stop (); +#endif + + destroy_ev.start (0); +} + +void +rxvt_term::destroy_cb (time_watcher &w) +{ + SET_R (this); + + delete this; +} + /*----------------------------------------------------------------------*/ /* rxvt_init() */ /* LIBPROTO */ rxvt_t -rxvt_init(int argc, const char *const *argv) +rxvt_init (int argc, const char *const *argv) { - const char **cmd_argv; - - SET_R(new rxvt_term); + SET_R (new rxvt_term); dR; - if (rxvt_init_vars(aR) < 0) + if (!R->init_vars () || !R->init (argc, argv)) { - free(R); + delete R; return NULL; } -/* - * Save and then give up any super-user privileges - * If we need privileges in any area then we must specifically request it. - * We should only need to be root in these cases: - * 1. write utmp entries on some systems - * 2. chown tty on some systems - */ - rxvt_privileges(aR_ SAVE); - rxvt_privileges(aR_ IGNORE); + return R; +} + +/* EXTPROTO */ +void +rxvt_init_signals () +{ +/* install exit handler for cleanup */ +#if 0 +#ifdef HAVE_ATEXIT + atexit(rxvt_clean_exit); +#else +#endif +#endif + + struct sigaction sa; - rxvt_init_secondary(aR); + sigfillset (&sa.sa_mask); + sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; + sa.sa_handler = SIG_IGN; sigaction (SIGHUP , &sa, 0); + sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0); + sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0); + sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0); + sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0); + sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0); + +/* need to trap SIGURG for SVR4 (Unixware) rlogin */ +/* signal (SIGURG, SIG_DFL); */ + + XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler); + //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler); +} + +bool +rxvt_term::init (int argc, const char *const *argv) +{ + dR;//TODO (scrollbar, setidle) + + /* + * Save and then give up any super-user privileges + * If we need privileges in any area then we must specifically request it. + * We should only need to be root in these cases: + * 1. write utmp entries on some systems + * 2. chown tty on some systems + */ + rxvt_privileges (this, SAVE); + rxvt_privileges (this, IGNORE); - cmd_argv = rxvt_init_resources(aR_ argc, argv); + rxvt_init_secondary (this); - R->x_ev.start (ConnectionNumber (R->Xdisplay), EVENT_READ); + const char **cmd_argv = rxvt_init_resources (this, argc, argv); #if (MENUBAR_MAX) - rxvt_menubar_read(aR_ R->rs[Rs_menu]); + rxvt_menubar_read (this, rs[Rs_menu]); #endif #ifdef HAVE_SCROLLBARS - if (R->Options & Opt_scrollBar) - scrollbar_setIdle(); /* set existence for size calculations */ + if (Options & Opt_scrollBar) + scrollbar_setIdle (); /* set existence for size calculations */ #endif - rxvt_Create_Windows(aR_ argc, argv); + rxvt_Create_Windows (this, argc, argv); - rxvt_init_xlocale(aR); + rxvt_init_xlocale (this); - rxvt_scr_reset(aR); /* initialize screen */ + rxvt_scr_reset (this); /* initialize screen */ #ifdef RXVT_GRAPHICS - rxvt_Gr_reset(aR); /* reset graphics */ + rxvt_Gr_reset (this); /* reset graphics */ #endif #if 0 #ifdef DEBUG_X - XSynchronize(R->Xdisplay, True); - XSetErrorHandler((XErrorHandler) abort); -#else - XSetErrorHandler((XErrorHandler) rxvt_xerror_handler); + XSynchronize(Xdisplay, True); #endif #endif #ifdef HAVE_SCROLLBARS - if (R->Options & Opt_scrollBar) - rxvt_Resize_scrollBar(aR); /* create and map scrollbar */ + if (Options & Opt_scrollBar) + rxvt_Resize_scrollBar (this); /* create and map scrollbar */ #endif #if (MENUBAR_MAX) - if (menubar_visible(r)) - XMapWindow(R->Xdisplay, R->menuBar.win); + if (menubar_visible(r)) + XMapWindow (Xdisplay, menuBar.win); #endif #ifdef TRANSPARENT - if (R->Options & Opt_transparent) - { - XSelectInput(R->Xdisplay, Xroot, PropertyChangeMask); - rxvt_check_our_parents(aR); - } + if (Options & Opt_transparent) + { + XSelectInput (Xdisplay, Xroot, PropertyChangeMask); + rxvt_check_our_parents (this); + } #endif - XMapWindow(R->Xdisplay, R->TermWin.vt); - XMapWindow(R->Xdisplay, R->TermWin.parent[0]); + XMapWindow (Xdisplay, TermWin.vt); + XMapWindow (Xdisplay, TermWin.parent[0]); + + rxvt_init_env (this); + rxvt_init_command (this, cmd_argv); + + x_ev.start (Xfd, EVENT_READ); + pty_ev.start (cmd_fd, EVENT_READ); - rxvt_init_env(aR); - rxvt_init_command(aR_ cmd_argv); + check_ev.start (); - R->pty_ev.start (R->cmd_fd, EVENT_READ); + process_x_events (); - return R; + return true; } /* ------------------------------------------------------------------------- * @@ -162,18 +257,15 @@ RETSIGTYPE rxvt_Child_signal(int sig __attribute__ ((unused))) { - dR; - int pid, save_errno = errno; - - do { - errno = 0; - } while ((pid = waitpid(-1, NULL, WNOHANG)) == -1 && errno == EINTR); + int pid, save_errno = errno; + while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR) + ; + errno = save_errno; +#if 0 if (pid == R->cmd_pid) - exit(EXIT_SUCCESS); - - errno = save_errno; - signal(SIGCHLD, rxvt_Child_signal); + exit (EXIT_SUCCESS); +#endif } /* @@ -197,18 +289,20 @@ rxvt_xerror_handler(const Display * display __attribute__ ((unused)), const XErrorEvent * event) { - dR; + dR; - if (R->allowedxerror == -1) { - R->allowedxerror = event->error_code; - return 0; /* ignored anyway */ - } - rxvt_print_error("XError: Request: %d . %d, Error: %d", - event->request_code, event->minor_code, - event->error_code); -/* XXX: probably should call rxvt_clean_exit() bypassing X routines */ - exit(EXIT_FAILURE); -/* NOTREACHED */ + if (R->allowedxerror == -1) + R->allowedxerror = event->error_code; + else + { + rxvt_print_error("XError: Request: %d . %d, Error: %d", + event->request_code, event->minor_code, + event->error_code); + + R->destroy (); + } + + return 0; } /*----------------------------------------------------------------------*/ @@ -218,25 +312,11 @@ */ /* EXTPROTO */ void -rxvt_clean_exit(void) +rxvt_clean_exit () { - dR; + dR; -#ifdef DEBUG_SCREEN - rxvt_scr_release(aR); -#endif -#ifndef NO_SETOWNER_TTYDEV - rxvt_privileged_ttydev(aR_ RESTORE); -#endif -#ifdef UTMP_SUPPORT - rxvt_privileged_utmp(aR_ RESTORE); -#endif -#ifdef USE_XIM - if (R->Input_Context != NULL) { - XDestroyIC(R->Input_Context); - R->Input_Context = NULL; - } -#endif + R->destroy (); } /* ------------------------------------------------------------------------- * @@ -538,26 +618,25 @@ * Tell the teletype handler what size the window is. * Called after a window size change. */ -/* EXTPROTO */ void -rxvt_tt_winsize(int fd, unsigned short col, unsigned short row, int pid) +rxvt_term::tt_winch () { - struct winsize ws; + struct winsize ws; - if (fd < 0) - return; - ws.ws_col = col; - ws.ws_row = row; - ws.ws_xpixel = ws.ws_ypixel = 0; + if (cmd_fd < 0) + return; + + ws.ws_col = TermWin.ncol; + ws.ws_row = TermWin.nrow; + ws.ws_xpixel = ws.ws_ypixel = 0; #ifndef DEBUG_SIZE - (void)ioctl(fd, TIOCSWINSZ, &ws); + (void)ioctl (cmd_fd, TIOCSWINSZ, &ws); #else - if (ioctl(fd, TIOCSWINSZ, &ws) < 0) { - D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); - } + if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0) + D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); # ifdef SIGWINCH - else if (pid) /* force through to the command */ - kill(pid, SIGWINCH); + else if (cmd_pid) /* force through to the command */ + kill (cmd_pid, SIGWINCH); # endif #endif } @@ -986,8 +1065,8 @@ /* INTPROTO */ void -rxvt_setPreeditArea(pR_ XRectangle * preedit_rect, XRectangle * status_rect, - XRectangle * needed_rect) +rxvt_setPreeditArea (pR_ XRectangle * preedit_rect, XRectangle * status_rect, + XRectangle * needed_rect) { int mbh, vtx = 0; @@ -1041,49 +1120,53 @@ __attribute__ ((unused)), XPointer call_data __attribute__ ((unused))) { - dR; - int i, found, had_im; - const char *p; - char **s; - char buf[IMBUFSIZ]; + dR; + int i, found, had_im; + const char *p; + char **s; + char buf[IMBUFSIZ]; - D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); - if (R->Input_Context) - return; + D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); + if (R->Input_Context) + return; + + p = R->rs[Rs_inputMethod]; + if (p && *p) + { + bool found = false; - found = had_im = 0; - p = R->rs[Rs_inputMethod]; - if (p && *p) { - had_im = 1; - s = rxvt_splitcommastring(p); - for (i = 0; s[i]; i++) { - if (*s[i]) { - STRCPY(buf, "@im="); - STRNCAT(buf, s[i], IMBUFSIZ - 5); - if ((p = XSetLocaleModifiers(buf)) != NULL && *p - && (rxvt_IM_get_IC(aR) == True)) { - found = 1; - break; + s = rxvt_splitcommastring (p); + for (i = 0; s[i]; i++) + { + if (*s[i]) + { + STRCPY (buf, "@im="); + STRNCAT (buf, s[i], IMBUFSIZ - 5); + if ((p = XSetLocaleModifiers (buf)) && *p + && rxvt_IM_get_IC (aR)) + { + found = true; + break; } } } - for (i = 0; s[i]; i++) - free(s[i]); - free(s); - } - if (found) - return; + for (i = 0; s[i]; i++) + free(s[i]); + free(s); -/* try with XMODIFIERS env. var. */ - if ((p = XSetLocaleModifiers("")) != NULL && *p) { - rxvt_IM_get_IC(aR); + if (found) return; } +/* try with XMODIFIERS env. var. */ + if ((p = XSetLocaleModifiers ("")) && *p + && rxvt_IM_get_IC (aR)) + return; + /* try with no modifiers base IF the user didn't specify an IM */ - if (!had_im && (p = XSetLocaleModifiers("@im=none")) != NULL && *p - && rxvt_IM_get_IC(aR) == True) - return; + if ((p = XSetLocaleModifiers ("@im=none")) && *p + && rxvt_IM_get_IC (aR) == True) + return; } /*