--- rxvt-unicode/src/main.C 2004/02/01 01:34:41 1.26 +++ rxvt-unicode/src/main.C 2004/02/13 12:11:05 1.31 @@ -74,6 +74,15 @@ rxvt_term::rxvt_term () : + rootwin_ev (this, &rxvt_term::rootwin_cb), + termwin_ev (this, &rxvt_term::x_cb), + vt_ev (this, &rxvt_term::x_cb), +#ifdef HAVE_SCROLLBARS + scrollbar_ev (this, &rxvt_term::x_cb), +#endif +#ifdef MENUBAR + menubar_ev (this, &rxvt_term::x_cb), fixme +#endif #ifdef CURSOR_BLINK cursor_blink_ev (this, &rxvt_term::cursor_blink_cb), #endif @@ -86,14 +95,19 @@ check_ev (this, &rxvt_term::check_cb), destroy_ev (this, &rxvt_term::destroy_cb), pty_ev (this, &rxvt_term::pty_cb), - x_ev (this, &rxvt_term::x_cb), - incr_ev (this, &rxvt_term::incr_cb) + incr_ev (this, &rxvt_term::incr_cb), +#ifdef USE_XIM + im_ev (this, &rxvt_term::im_cb) +#endif { cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; } rxvt_term::~rxvt_term () { + if (cmd_fd >= 0) + close (cmd_fd); + scr_release (); free (locale); @@ -105,29 +119,41 @@ #ifdef UTMP_SUPPORT privileged_utmp (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); + // TODO: free pixcolours, colours should become part of rxvt_display delete PixColors; + delete TermWin.fontset; + + displays.put (display); } void rxvt_term::destroy () { + if (display) + { + if (TermWin.parent[0]) + XDestroyWindow (display->display, TermWin.parent[0]); + + rootwin_ev.stop (display); + termwin_ev.stop (display); + vt_ev.stop (display); + +#ifdef USE_XIM + im_destroy (); + im_ev.stop (display); +#endif +#ifdef HAVE_SCROLLBARS + scrollbar_ev.stop (display); +#endif +#ifdef MENUBAR + menubar_ev.stop (display); +#endif + } + check_ev.stop (); pty_ev.stop (); - x_ev.stop (); #ifdef CURSOR_BLINK cursor_blink_ev.stop (); #endif @@ -166,6 +192,8 @@ return GET_R; } +static int (*old_xerror_handler)(Display *dpy, XErrorEvent *event); + void rxvt_init_signals () { @@ -191,7 +219,7 @@ /* need to trap SIGURG for SVR4 (Unixware) rlogin */ /* signal (SIGURG, SIG_DFL); */ - XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler); + old_xerror_handler = XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler); //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler); } @@ -233,7 +261,7 @@ #if 0 #ifdef DEBUG_X - XSynchronize(Xdisplay, True); + XSynchronize(display->display, True); #endif #endif @@ -243,28 +271,28 @@ #endif #if (MENUBAR_MAX) if (menubar_visible(r)) - XMapWindow (Xdisplay, menuBar.win); + XMapWindow (display->display, menuBar.win); #endif #ifdef TRANSPARENT if (Options & Opt_transparent) { - XSelectInput (Xdisplay, Xroot, PropertyChangeMask); + XSelectInput (display->display, display->root, PropertyChangeMask); check_our_parents (); } #endif - XMapWindow (Xdisplay, TermWin.vt); - XMapWindow (Xdisplay, TermWin.parent[0]); + + rootwin_ev.start (display, display->root); + + XMapWindow (display->display, TermWin.vt); + XMapWindow (display->display, TermWin.parent[0]); init_env (); init_command (cmd_argv); - x_ev.start (Xfd, EVENT_READ); pty_ev.start (cmd_fd, EVENT_READ); check_ev.start (); - process_x_events (); - return true; } @@ -297,28 +325,23 @@ RETSIGTYPE rxvt_Exit_signal(int sig) { - signal(sig, SIG_DFL); + signal (sig, SIG_DFL); #ifdef DEBUG_CMD - rxvt_print_error("signal %d", sig); + rxvt_print_error ("signal %d", sig); #endif rxvt_clean_exit(); - kill(getpid(), sig); + kill (getpid (), sig); } -/* ARGSUSED */ -/* EXTPROTO */ +/* INTPROTO */ int -rxvt_xerror_handler(const Display * display - __attribute__ ((unused)), const XErrorEvent * event) +rxvt_xerror_handler (Display *display, XErrorEvent *event) { if (GET_R->allowedxerror == -1) GET_R->allowedxerror = event->error_code; else { - rxvt_print_error("XError: Request: %d . %d, Error: %d", - event->request_code, event->minor_code, - event->error_code); - + old_xerror_handler (display, event); GET_R->destroy (); } @@ -334,7 +357,9 @@ void rxvt_clean_exit () { - GET_R->destroy (); + // TODO: rxvtd should clean up all ressources + if (GET_R) + GET_R->destroy (); } /* ------------------------------------------------------------------------- * @@ -639,10 +664,10 @@ window_sb_x = szHint.width - sb_w; if (recalc_x) - szHint.x += (DisplayWidth (Xdisplay, DefaultScreen (Xdisplay)) + szHint.x += (DisplayWidth (display->display, DefaultScreen (display->display)) - szHint.width - 2 * TermWin.ext_bwidth); if (recalc_y) - szHint.y += (DisplayHeight (Xdisplay, DefaultScreen (Xdisplay)) + szHint.y += (DisplayHeight (display->display, DefaultScreen (display->display)) - szHint.height - 2 * TermWin.ext_bwidth); TermWin.ncol = TermWin.width / TermWin.fwidth; @@ -704,14 +729,14 @@ rxvt_term::set_title (const char *str) { #ifndef SMART_WINDOW_TITLE - XStoreName(Xdisplay, TermWin.parent[0], str); + XStoreName(display->display, TermWin.parent[0], str); #else char *name; - if (XFetchName(Xdisplay, TermWin.parent[0], &name) == 0) + if (XFetchName(display->display, TermWin.parent[0], &name) == 0) name = NULL; if (name == NULL || STRCMP(name, str)) - XStoreName(Xdisplay, TermWin.parent[0], str); + XStoreName(display->display, TermWin.parent[0], str); if (name) XFree(name); #endif @@ -721,14 +746,14 @@ rxvt_term::set_iconName (const char *str) { #ifndef SMART_WINDOW_TITLE - XSetIconName(Xdisplay, TermWin.parent[0], str); + XSetIconName(display->display, TermWin.parent[0], str); #else char *name; - if (XGetIconName(Xdisplay, TermWin.parent[0], &name)) + if (XGetIconName(display->display, TermWin.parent[0], &name)) name = NULL; if (name == NULL || STRCMP(name, str)) - XSetIconName(Xdisplay, TermWin.parent[0], str); + XSetIconName(display->display, TermWin.parent[0], str); if (name) XFree(name); #endif @@ -767,7 +792,7 @@ } if (!rXParseAllocColor (& xcol, color)) return; - /* XStoreColor (Xdisplay, XCMAP, XColor*); */ + /* XStoreColor (display->display, XCMAP, XColor*); */ /* * FIXME: should free colors here, but no idea how to do it so instead, @@ -780,8 +805,8 @@ if (i > Color_White) { /* fprintf (stderr, "XFreeColors: PixColors [%d] = %lu\n", idx, PixColors [idx]); */ - XFreeColors(Xdisplay, XCMAP, (PixColors + idx), 1, - DisplayPlanes(Xdisplay, Xscreen)); + XFreeColors(display->display, XCMAP, (PixColors + idx), 1, + DisplayPlanes(display->display, display->screen)); } # endif @@ -792,7 +817,7 @@ /* Cursor cursor; */ Done: if (idx == Color_bg && !(Options & Opt_transparent)) - XSetWindowBackground(Xdisplay, TermWin.vt, + XSetWindowBackground(display->display, TermWin.vt, PixColors[Color_bg]); /* handle Color_BD, scrollbar background, etc. */ @@ -815,8 +840,8 @@ xcol[0] = PixColors[Color_pointer]; xcol[1] = PixColors[Color_bg]; - XQueryColors (Xdisplay, XCMAP, xcol, 2); - XRecolorCursor (Xdisplay, TermWin_cursor, &(xcol[0]), &(xcol[1])); + XQueryColors (display->display, XCMAP, xcol, 2); + XRecolorCursor (display->display, TermWin_cursor, &(xcol[0]), &(xcol[1])); #endif } @@ -874,7 +899,7 @@ int rxvt_term::rXParseAllocColor (rxvt_color *screen_in_out, const char *colour) { - if (!screen_in_out->set (this, colour)) + if (!screen_in_out->set (display, colour)) { rxvt_print_error("can't allocate colour: %s", colour); return false; @@ -896,7 +921,7 @@ #endif window_calc (width, height); - XSetWMNormalHints (Xdisplay, TermWin.parent[0], &szHint); + XSetWMNormalHints (display->display, TermWin.parent[0], &szHint); if (!ignoreparent) { #ifdef SMART_RESIZE @@ -909,12 +934,12 @@ unsigned int unused_w1, unused_h1, unused_b1, unused_d1; Window unused_cr; - XTranslateCoordinates (Xdisplay, TermWin.parent[0], Xroot, + XTranslateCoordinates (display->display, TermWin.parent[0], display->root, 0, 0, &x, &y, &unused_cr); - XGetGeometry (Xdisplay, TermWin.parent[0], &unused_cr, &x1, &y1, + XGetGeometry (display->display, TermWin.parent[0], &unused_cr, &x1, &y1, &unused_w1, &unused_h1, &unused_b1, &unused_d1); /* - * if Xroot isn't the parent window, a WM will probably have offset + * if display->root isn't the parent window, a WM will probably have offset * our position for handles and decorations. Counter it */ if (x1 != x || y1 != y) @@ -923,8 +948,8 @@ y -= y1; } - x1 = (DisplayWidth (Xdisplay, Xscreen) - old_width) / 2; - y1 = (DisplayHeight (Xdisplay, Xscreen) - old_height) / 2; + x1 = (DisplayWidth (display->display, display->screen) - old_width) / 2; + y1 = (DisplayHeight (display->display, display->screen) - old_height) / 2; dx = old_width - szHint.width; dy = old_height - szHint.height; @@ -938,10 +963,10 @@ else if (y == y1) /* exact center */ dy /= 2; - XMoveResizeWindow (Xdisplay, TermWin.parent[0], x + dx, y + dy, + XMoveResizeWindow (display->display, TermWin.parent[0], x + dx, y + dy, szHint.width, szHint.height); #else - XResizeWindow (Xdisplay, TermWin.parent[0], szHint.width, + XResizeWindow (display->display, TermWin.parent[0], szHint.width, szHint.height); #endif @@ -954,16 +979,16 @@ { if (scrollbar_visible ()) { - XMoveResizeWindow (Xdisplay, scrollBar.win, window_sb_x, + XMoveResizeWindow (display->display, scrollBar.win, window_sb_x, 0, scrollbar_TotalWidth (), szHint.height); resize_scrollbar (); } if (menubar_visible ()) - XMoveResizeWindow (Xdisplay, menuBar.win, window_vt_x, + XMoveResizeWindow (display->display, menuBar.win, window_vt_x, 0, TermWin_TotalWidth (), menuBar_TotalHeight ()); - XMoveResizeWindow (Xdisplay, TermWin.vt, window_vt_x, + XMoveResizeWindow (display->display, TermWin.vt, window_vt_x, window_vt_y, TermWin_TotalWidth (), TermWin_TotalHeight ()); #ifdef RXVT_GRAPHICS @@ -1020,7 +1045,7 @@ if (width == 0 || height == 0) { - XGetWindowAttributes(Xdisplay, Xroot, &wattr); + XGetWindowAttributes(display->display, display->root, &wattr); if (width == 0) width = wattr.width - szHint.base_width; if (height == 0) @@ -1038,8 +1063,9 @@ * - X INPUT METHOD ROUTINES - * * -------------------------------------------------------------------- */ #ifdef USE_XIM + void -rxvt_term::set_size (XRectangle *size) +rxvt_term::im_set_size (XRectangle *size) { size->x = TermWin.int_bwidth; size->y = TermWin.int_bwidth; @@ -1048,7 +1074,7 @@ } void -rxvt_term::set_color (unsigned long *fg, unsigned long *bg) +rxvt_term::im_set_color (unsigned long *fg, unsigned long *bg) { *fg = PixColors[Color_fg]; *bg = PixColors[Color_bg]; @@ -1071,8 +1097,8 @@ if ((p = STRCHR(server + 1, '@')) != NULL) /* first one only */ *p = '\0'; - atom = XInternAtom(Xdisplay, server, False); - win = XGetSelectionOwner(Xdisplay, atom); + atom = XInternAtom(display->display, server, False); + win = XGetSelectionOwner(display->display, atom); if (win != None) return True; } @@ -1095,7 +1121,7 @@ || !IMisRunning ()) return; - set_position (&spot); + im_set_position (&spot); preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL); XSetICValues(Input_Context, XNPreeditAttributes, preedit_attr, NULL); @@ -1103,8 +1129,8 @@ } void -rxvt_term::set_preedit_area (XRectangle * preedit_rect, XRectangle * status_rect, - XRectangle * needed_rect) +rxvt_term::im_set_preedit_area (XRectangle * preedit_rect, XRectangle * status_rect, + XRectangle * needed_rect) { int mbh, vtx = 0; @@ -1127,18 +1153,20 @@ status_rect->height = Height2Pixel(1); } -/* ARGSUSED */ -/* EXTPROTO */ void -rxvt_IMDestroyCallback(XIM xim __attribute__ ((unused)), XPointer client_data - __attribute__ ((unused)), XPointer call_data - __attribute__ ((unused))) -{ - GET_R->Input_Context = NULL; - /* To avoid Segmentation Fault in C locale: Solaris only? */ - if (STRCMP (GET_R->locale, "C")) - XRegisterIMInstantiateCallback(GET_R->Xdisplay, NULL, NULL, NULL, - rxvt_IMInstantiateCallback, NULL); +rxvt_term::im_destroy () +{ + if (Input_Context) + { + XDestroyIC (Input_Context); + Input_Context = NULL; + } + + if (input_method) + { + display->put_xim (input_method); + input_method = 0; + } } /* @@ -1146,7 +1174,7 @@ * open a suitable preedit type */ bool -rxvt_term::IM_get_IC () +rxvt_term::IM_get_IC (const char *modifiers) { int i, j, found; XIM xim; @@ -1157,23 +1185,26 @@ char **s; XIMStyles *xim_styles; XVaNestedList preedit_attr, status_attr; - XIMCallback ximcallback; + + if (!((p = XSetLocaleModifiers (modifiers)) && *p)) + return false; D_MAIN((stderr, "rxvt_IM_get_IC()")); - xim = XOpenIM (Xdisplay, NULL, NULL, NULL); - if (xim == NULL) - return False; + input_method = display->get_xim (locale, modifiers); + if (input_method == NULL) + return false; + + xim = input_method->xim; xim_styles = NULL; if (XGetIMValues (xim, XNQueryInputStyle, &xim_styles, NULL) || !xim_styles || !xim_styles->count_styles) { - XCloseIM(xim); - return False; + display->put_xim (input_method); + return false; } - p = rs[Rs_preeditType] ? rs[Rs_preeditType] - : "OverTheSpot,OffTheSpot,Root"; + p = rs[Rs_preeditType] ? rs[Rs_preeditType] : "OverTheSpot,OffTheSpot,Root"; s = rxvt_splitcommastring(p); for (i = found = 0; !found && s[i]; i++) { @@ -1191,29 +1222,26 @@ break; } } + for (i = 0; s[i]; i++) - free(s[i]); - free(s); - XFree(xim_styles); + free (s[i]); + + free (s); + XFree (xim_styles); if (!found) { - XCloseIM(xim); - return False; + display->put_xim (input_method); + return false; } - ximcallback.callback = rxvt_IMDestroyCallback; - - /* XXX: not sure why we need this (as well as IC one below) */ - XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL); - preedit_attr = status_attr = NULL; if (input_style & XIMPreeditPosition) { - set_size (&rect); - set_position (&spot); - set_color (&fg, &bg); + im_set_size (&rect); + im_set_position (&spot); + im_set_color (&fg, &bg); preedit_attr = XVaCreateNestedList(0, XNArea, &rect, XNSpotLocation, &spot, @@ -1223,7 +1251,7 @@ } else if (input_style & XIMPreeditArea) { - set_color (&fg, &bg); + im_set_color (&fg, &bg); /* * The necessary width of preedit area is unknown @@ -1231,7 +1259,7 @@ */ needed_rect.width = 0; - set_preedit_area(&rect, &status_rect, &needed_rect); + im_set_preedit_area (&rect, &status_rect, &needed_rect); preedit_attr = XVaCreateNestedList(0, XNArea, &rect, XNForeground, fg, XNBackground, bg, @@ -1242,59 +1270,51 @@ //XNFontSet, TermWin.fontset, NULL); } + Input_Context = XCreateIC(xim, XNInputStyle, input_style, XNClientWindow, TermWin.parent[0], XNFocusWindow, TermWin.parent[0], - XNDestroyCallback, &ximcallback, preedit_attr ? XNPreeditAttributes : NULL, preedit_attr, status_attr ? XNStatusAttributes : NULL, status_attr, NULL); - if (preedit_attr) - XFree(preedit_attr); - if (status_attr) - XFree(status_attr); + if (preedit_attr) XFree(preedit_attr); + if (status_attr) XFree(status_attr); + if (Input_Context == NULL) { rxvt_print_error("failed to create input context"); - XCloseIM(xim); - return False; + display->put_xim (input_method); + return false; } + if (input_style & XIMPreeditArea) IMSetStatusPosition (); + D_MAIN((stderr, "rxvt_IM_get_IC() - successful connection")); - return True; + return true; } -/* - * X manual pages and include files don't match on some systems: - * some think this is an XIDProc and others an XIMProc so we can't - * use the first argument - need to update this to be nice for - * both types via some sort of configure detection - */ -/* ARGSUSED */ -/* EXTPROTO */ void -rxvt_IMInstantiateCallback(Display * unused - __attribute__ ((unused)), XPointer client_data - __attribute__ ((unused)), XPointer call_data - __attribute__ ((unused))) +rxvt_term::im_cb () { int i, found, had_im; const char *p; char **s; char buf[IMBUFSIZ]; + im_destroy (); + D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); - if (GET_R->Input_Context) + if (Input_Context) return; #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE) - if (GET_R->rs[Rs_imLocale]) - setlocale (LC_CTYPE, GET_R->rs[Rs_imLocale]); + if (rs[Rs_imLocale]) + setlocale (LC_CTYPE, rs[Rs_imLocale]); #endif - p = GET_R->rs[Rs_inputMethod]; + p = rs[Rs_inputMethod]; if (p && *p) { bool found = false; @@ -1306,7 +1326,7 @@ { STRCPY (buf, "@im="); STRNCAT (buf, s[i], IMBUFSIZ - 5); - if ((p = XSetLocaleModifiers (buf)) && *p && GET_R->IM_get_IC ()) + if (IM_get_IC (buf)) { found = true; break; @@ -1322,19 +1342,17 @@ } /* try with XMODIFIERS env. var. */ - if ((p = XSetLocaleModifiers ("")) && *p - && GET_R->IM_get_IC ()) + if (IM_get_IC ("")) goto done; /* try with no modifiers base IF the user didn't specify an IM */ - if ((p = XSetLocaleModifiers ("@im=none")) && *p - && GET_R->IM_get_IC () == True) + if (IM_get_IC ("@im=none")) goto done; done: #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE) - if (GET_R->rs[Rs_imLocale]) - setlocale (LC_CTYPE, GET_R->locale); + if (rs[Rs_imLocale]) + setlocale (LC_CTYPE, locale); #endif } @@ -1354,7 +1372,7 @@ XGetICValues(Input_Context, XNStatusAttributes, status_attr, NULL); XFree(status_attr); - set_preedit_area(&preedit_rect, &status_rect, needed_rect); + im_set_preedit_area (&preedit_rect, &status_rect, needed_rect); preedit_attr = XVaCreateNestedList(0, XNArea, &preedit_rect, NULL); status_attr = XVaCreateNestedList(0, XNArea, &status_rect, NULL);