1 /*----------------------------------------------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
8 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
9 * - extensive modifications
10 * Copyright (c) 1995 Garrett D'Amore <garrett@netcom.com>
11 * Copyright (c) 1997 mj olesen <olesen@me.QueensU.CA>
12 * - extensive modifications
13 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
14 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
15 * - extensive modifications
16 * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 *---------------------------------------------------------------------*/
33 #include "../config.h" /* NECESSARY */
34 #include "rxvt.h" /* NECESSARY */
43 #ifdef TTY_GID_SUPPORT
51 vector<rxvt_term *> rxvt_term::termlist;
53 // used to tell global functions which terminal instance is "active"
54 rxvt_t rxvt_current_term;
56 static char curlocale[128], savelocale[128];
59 rxvt_set_locale (const char *locale) NOTHROW
61 if (!locale || !strncmp (locale, curlocale, 128))
64 strncpy (curlocale, locale, 128);
65 setlocale (LC_CTYPE, curlocale);
70 rxvt_push_locale (const char *locale) NOTHROW
72 strcpy (savelocale, curlocale);
73 rxvt_set_locale (locale);
77 rxvt_pop_locale () NOTHROW
79 rxvt_set_locale (savelocale);
83 class rxvt_composite_vec rxvt_composite;
85 text_t rxvt_composite_vec::compose (unicode_t c1, unicode_t c2)
89 // break compose chains, as stupid readline really likes to duplicate
90 // composing characters for some reason near the end of a line.
94 if (cc->c2 == c2) return c1;
98 // check to see wether this combination already exists otherwise
99 for (cc = v.end (); cc-- > v.begin (); )
101 if (cc->c1 == c1 && cc->c2 == c2)
102 return COMPOSE_LO + (cc - v.begin ());
105 // allocate a new combination
106 if (v.size () == COMPOSE_HI - COMPOSE_LO + 1)
111 fprintf (stderr, "too many unrepresentable composite characters, try --enable-unicode3\n");
113 return REPLACEMENT_CHAR;
116 v.push_back (compose_char (c1, c2));
118 return v.size () - 1 + COMPOSE_LO;
121 int rxvt_composite_vec::expand (unicode_t c, wchar_t *r)
123 compose_char *cc = (*this)[c];
131 int len = expand (cc->c1, r);
135 if (cc->c2 != NOCHAR)
138 if (r) *r++ = cc->c2;
146 rxvt_term::rxvt_term ()
149 rootwin_ev (this, &rxvt_term::rootwin_cb),
151 #ifdef HAVE_SCROLLBARS
152 scrollbar_ev (this, &rxvt_term::x_cb),
155 cursor_blink_ev (this, &rxvt_term::cursor_blink_cb),
158 text_blink_ev (this, &rxvt_term::text_blink_cb),
160 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
161 cont_scroll_ev (this, &rxvt_term::cont_scroll_cb),
163 #ifdef SELECTION_SCROLLING
164 sel_scroll_ev (this, &rxvt_term::sel_scroll_cb),
166 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
167 slip_wheel_ev (this, &rxvt_term::slip_wheel_cb),
170 pointer_ev (this, &rxvt_term::pointer_cb),
173 im_ev (this, &rxvt_term::im_cb),
176 bell_ev (this, &rxvt_term::bell_cb),
178 termwin_ev (this, &rxvt_term::x_cb),
179 vt_ev (this, &rxvt_term::x_cb),
180 child_ev (this, &rxvt_term::child_cb),
181 check_ev (this, &rxvt_term::check_cb),
182 flush_ev (this, &rxvt_term::flush_cb),
183 destroy_ev (this, &rxvt_term::destroy_cb),
184 pty_ev (this, &rxvt_term::pty_cb),
185 incr_ev (this, &rxvt_term::incr_cb)
187 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
189 termlist.push_back (this);
191 #ifdef KEYSYM_RESOURCE
192 keyboard = new keyboard_manager;
196 // clean up the most important stuff, do *not* call x or free mem etc.
197 // for use before an emergency exit
198 void rxvt_term::emergency_cleanup ()
201 kill (-cmd_pid, SIGHUP);
207 rxvt_term::~rxvt_term ()
209 termlist.erase (find (termlist.begin (), termlist.end(), this));
211 emergency_cleanup ();
214 for (int i = RS_styleCount; --i; )
215 if (fontset[i] != fontset[0])
227 #ifdef XTERM_SCROLLBAR
228 if (xscrollbarGC) XFreeGC (dpy, xscrollbarGC);
229 if (ShadowGC) XFreeGC (dpy, ShadowGC);
231 #ifdef PLAIN_SCROLLBAR
232 if (pscrollbarGC) XFreeGC (dpy, pscrollbarGC);
234 #ifdef NEXT_SCROLLBAR
235 if (blackGC) XFreeGC (dpy, blackGC);
236 if (whiteGC) XFreeGC (dpy, whiteGC);
237 if (grayGC) XFreeGC (dpy, grayGC);
238 if (darkGC) XFreeGC (dpy, darkGC);
239 if (stippleGC) XFreeGC (dpy, stippleGC);
240 if (dimple) XFreePixmap (dpy, dimple);
241 if (upArrow) XFreePixmap (dpy, upArrow);
242 if (downArrow) XFreePixmap (dpy, downArrow);
243 if (upArrowHi) XFreePixmap (dpy, upArrowHi);
244 if (downArrowHi) XFreePixmap (dpy, downArrowHi);
246 #ifdef RXVT_SCROLLBAR
247 if (topShadowGC) XFreeGC (dpy, topShadowGC);
248 if (botShadowGC) XFreeGC (dpy, botShadowGC);
249 if (scrollbarGC) XFreeGC (dpy, scrollbarGC);
251 if (gc) XFreeGC (dpy, gc);
254 // destroy all windows
256 XDestroyWindow (dpy, parent[0]);
258 for (int i = 0; i < TOTAL_COLORS; i++)
259 if (ISSET_PIXCOLOR (i))
261 pix_colors_focused [i].free (this);
263 pix_colors_unfocused [i].free (this);
270 delete pix_colors_focused;
272 delete pix_colors_unfocused;
275 displays.put (display);
279 /* clear all resources */
280 for (int i = 0; i < allocated.size (); i++)
281 free (allocated [i]);
283 free (selection.text);
284 // TODO: manage env vars in child only(!)
294 #ifdef KEYSYM_RESOURCE
299 // child has exited, usually destroys
301 rxvt_term::child_cb (child_watcher &w, int status)
303 HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END));
307 if (!OPTION (Opt_hold))
312 rxvt_term::destroy ()
314 if (destroy_ev.active)
317 HOOK_INVOKE ((this, HOOK_DESTROY, DT_END));
326 im_ev.stop (display);
329 scrollbar_ev.stop (display);
332 rootwin_ev.stop (display);
335 termwin_ev.stop (display);
336 vt_ev.stop (display);
342 cursor_blink_ev.stop ();
345 text_blink_ev.stop ();
347 #ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
348 cont_scroll_ev.stop ();
350 #ifdef SELECTION_SCROLLING
351 sel_scroll_ev.stop ();
357 destroy_ev.start (0);
361 rxvt_term::destroy_cb (time_watcher &w)
368 /*----------------------------------------------------------------------*/
370 * Exit gracefully, clearing the utmp entry and restoring tty attributes
371 * TODO: if debugging, this should free up any known resources if we can
373 static XErrorHandler old_xerror_handler;
376 rxvt_emergency_cleanup ()
378 for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
379 (*t)->emergency_cleanup ();
384 print_x_error (Display *dpy, XErrorEvent *event)
389 char *mtype = "XlibMessage";
390 XGetErrorText(dpy, event->error_code, buffer, BUFSIZ);
391 XGetErrorDatabaseText(dpy, mtype, "XError", "X Error", mesg, BUFSIZ);
392 rxvt_warn ("An X Error occured, trying to continue after report.\n");
393 rxvt_warn ("%s: %s\n", mesg, buffer);
394 XGetErrorDatabaseText(dpy, mtype, "MajorCode", "Request Major code %d", mesg, BUFSIZ);
395 rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->request_code);
396 sprintf(number, "%d", event->request_code);
397 XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ);
398 rxvt_warn ("(which is %s)\n", buffer);
399 if (event->request_code >= 128) {
400 XGetErrorDatabaseText(dpy, mtype, "MinorCode", "Request Minor code %d",
402 rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->minor_code);
404 if ((event->error_code == BadWindow) ||
405 (event->error_code == BadPixmap) ||
406 (event->error_code == BadCursor) ||
407 (event->error_code == BadFont) ||
408 (event->error_code == BadDrawable) ||
409 (event->error_code == BadColor) ||
410 (event->error_code == BadGC) ||
411 (event->error_code == BadIDChoice) ||
412 (event->error_code == BadValue) ||
413 (event->error_code == BadAtom)) {
414 if (event->error_code == BadValue)
415 XGetErrorDatabaseText(dpy, mtype, "Value", "Value 0x%x",
417 else if (event->error_code == BadAtom)
418 XGetErrorDatabaseText(dpy, mtype, "AtomID", "AtomID 0x%x",
421 XGetErrorDatabaseText(dpy, mtype, "ResourceID", "ResourceID 0x%x",
423 rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->resourceid);
425 XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d",
427 rxvt_warn (strncat (mesg, "\n", BUFSIZ), event->serial);
432 rxvt_xerror_handler (Display *display, XErrorEvent *event)
434 if (GET_R->allowedxerror == -1)
435 GET_R->allowedxerror = event->error_code;
438 // GET_R is most likely not the terminal which caused the error,
439 // so just output the error and continue
441 old_xerror_handler (display, event);
443 print_x_error (display, event);
451 rxvt_xioerror_handler (Display *display)
453 rxvt_warn ("X connection to '%s' broken, unable to recover, exiting.\n",
454 DisplayString (display));
455 rxvt_emergency_cleanup ();
456 _exit (EXIT_FAILURE);
459 /*----------------------------------------------------------------------*/
461 rxvt_term::init (int argc, const char *const *argv, stringvec *envv)
467 set_environ (envv); // few things in X do not call setlocale :(
474 const char **cmd_argv = init_resources (argc, argv);
476 #ifdef KEYSYM_RESOURCE
477 keyboard->register_done ();
480 #ifdef HAVE_SCROLLBARS
481 if (OPTION (Opt_scrollBar))
482 scrollBar.setIdle (); /* set existence for size calculations */
485 pty = ptytty::create ();
487 create_windows (argc, argv);
491 scr_reset (); // initialize screen
494 XSynchronize (dpy, True);
497 #ifdef HAVE_SCROLLBARS
498 if (OPTION (Opt_scrollBar))
499 resize_scrollbar (); /* create and map scrollbar */
502 if (OPTION (Opt_transparent))
504 XSelectInput (dpy, display->root, PropertyChangeMask);
505 check_our_parents ();
506 rootwin_ev.start (display, display->root);
512 init_command (cmd_argv);
517 pty_ev.start (pty->pty, EVENT_READ);
521 HOOK_INVOKE ((this, HOOK_START, DT_END));
526 long info[2] = { 0, XEMBED_MAPPED };
528 XChangeProperty (dpy, parent[0], xa[XA_XEMBED_INFO], xa[XA_XEMBED_INFO],
529 32, PropModeReplace, (unsigned char *)&info, 2);
533 XMapWindow (dpy, vt);
534 XMapWindow (dpy, parent[0]);
539 static struct sig_handlers
541 sig_watcher sw_term, sw_int;
544 * Catch a fatal signal and tidy up before quitting
547 sig_term (sig_watcher &w)
549 rxvt_emergency_cleanup ();
550 signal (w.signum, SIG_DFL);
551 kill (getpid (), w.signum);
555 : sw_term (this, &sig_handlers::sig_term),
556 sw_int (this, &sig_handlers::sig_term)
561 char **rxvt_environ; // startup environment
568 rxvt_environ = environ;
570 signal (SIGHUP, SIG_IGN);
571 signal (SIGPIPE, SIG_IGN);
573 sig_handlers.sw_term.start (SIGTERM);
574 sig_handlers.sw_int.start (SIGINT);
576 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
577 /* signal (SIGURG, SIG_DFL); */
579 old_xerror_handler = XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler);
580 // TODO: handle this with exceptions and tolerate the memory loss
581 XSetIOErrorHandler (rxvt_xioerror_handler);
586 /* ------------------------------------------------------------------------- *
587 * MEMORY ALLOCATION WRAPPERS *
588 * ------------------------------------------------------------------------- */
590 rxvt_malloc (size_t size)
592 void *p = malloc (size);
595 rxvt_fatal ("memory allocation failure. aborting.\n");
601 rxvt_calloc (size_t number, size_t size)
603 void *p = calloc (number, size);
606 rxvt_fatal ("memory allocation failure. aborting.\n");
612 rxvt_realloc (void *ptr, size_t size)
614 void *p = realloc (ptr, size);
617 rxvt_fatal ("memory allocation failure. aborting.\n");
622 /*----------------------------------------------------------------------*/
624 * window size/position calculcations for XSizeHint and other storage.
625 * if width/height are non-zero then override calculated width/height
628 rxvt_term::window_calc (unsigned int newwidth, unsigned int newheight)
630 short recalc_x, recalc_y;
631 int x, y, sb_w, flags;
633 unsigned int max_width, max_height;
635 szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity;
636 szHint.win_gravity = NorthWestGravity;
637 /* szHint.min_aspect.x = szHint.min_aspect.y = 1; */
639 recalc_x = recalc_y = 0;
642 if (!parsed_geometry)
647 flags = XParseGeometry (rs[Rs_geometry], &x, &y, &w, &h);
649 if (flags & WidthValue)
651 ncol = clamp (w, 0, std::numeric_limits<int16_t>::max ());
652 szHint.flags |= USSize;
655 if (flags & HeightValue)
657 nrow = clamp (h, 0, std::numeric_limits<int16_t>::max ());
658 szHint.flags |= USSize;
664 szHint.flags |= USPosition;
666 if (flags & XNegative)
669 szHint.win_gravity = NorthEastGravity;
676 szHint.flags |= USPosition;
678 if (flags & YNegative)
682 if (szHint.win_gravity == NorthEastGravity)
683 szHint.win_gravity = SouthEastGravity;
685 szHint.win_gravity = SouthWestGravity;
691 width = ncol * fwidth;
692 height = nrow * fheight;
693 max_width = MAX_COLS * fwidth;
694 max_height = MAX_ROWS * fheight;
696 szHint.base_width = szHint.base_height = 2 * int_bwidth;
699 window_vt_x = window_vt_y = int_bwidth;
703 sb_w = scrollbar_TotalWidth ();
704 szHint.base_width += sb_w;
706 if (!OPTION (Opt_scrollBar_right))
710 szHint.width_inc = fwidth;
711 szHint.height_inc = fheight;
712 szHint.min_width = szHint.base_width + szHint.width_inc;
713 szHint.min_height = szHint.base_height + szHint.height_inc;
715 if (newwidth && newwidth - szHint.base_width < max_width)
717 szHint.width = newwidth;
718 width = newwidth - szHint.base_width;
722 min_it (width, max_width);
723 szHint.width = szHint.base_width + width;
726 if (newheight && newheight - szHint.base_height < max_height)
728 szHint.height = newheight;
729 height = newheight - szHint.base_height;
733 min_it (height, max_height);
734 szHint.height = szHint.base_height + height;
737 if (scrollBar.state && OPTION (Opt_scrollBar_right))
738 window_sb_x = szHint.width - sb_w;
741 szHint.x += DisplayWidth (dpy, display->screen) - szHint.width - 2 * ext_bwidth;
744 szHint.y += DisplayHeight (dpy, display->screen) - szHint.height - 2 * ext_bwidth;
746 ncol = width / fwidth;
747 nrow = height / fheight;
750 /*----------------------------------------------------------------------*/
752 * Tell the teletype handler what size the window is.
753 * Called after a window size change.
756 rxvt_term::tt_winch ()
765 ws.ws_xpixel = width;
766 ws.ws_ypixel = height;
767 (void)ioctl (pty->pty, TIOCSWINSZ, &ws);
770 // TIOCSWINSZ⎈ is supposed to do this automatically and correctly
771 if (cmd_pid) /* force through to the command */
772 kill (-cmd_pid, SIGWINCH);
776 /*----------------------------------------------------------------------*/
777 /* set_fonts () - load and set the various fonts
779 * init = 1 - initialize
781 * fontname == FONT_UP - switch to bigger font
782 * fontname == FONT_DN - switch to smaller font
785 rxvt_term::set_fonts ()
787 rxvt_fontset *fs = new rxvt_fontset (this);
791 || !fs->populate (rs[Rs_font] ? rs[Rs_font] : "fixed")
792 || !fs->realize_font (1))
799 for (int i = RS_styleCount; --i; )
800 if (fontset[i] != fontset[0])
807 prop = (*fs)[1]->properties ();
808 prop.height += lineSpace;
812 fheight = prop.height;
815 for (int style = 1; style < 4; style++)
818 const char *res = rs[Rs_font + style];
821 fontset[style] = fontset[0];
824 fontset[style] = fs = new rxvt_fontset (this);
825 rxvt_fontprop prop2 = prop;
828 prop2.weight = prop2.slant = rxvt_fontprop::unset;
831 res = fontset[0]->fontdesc;
833 if (SET_STYLE (0, style) & RS_Bold) prop2.weight = rxvt_fontprop::bold;
834 if (SET_STYLE (0, style) & RS_Italic) prop2.slant = rxvt_fontprop::italic;
838 fs->set_prop (prop2);
841 fontset[style] = fontset[0];
847 resize_all_windows (0, 0, 0);
855 void rxvt_term::set_string_property (Atom prop, const char *str, int len)
857 XChangeProperty (dpy, parent[0],
858 prop, XA_STRING, 8, PropModeReplace,
859 (const unsigned char *)str, len >= 0 ? len : strlen (str));
862 void rxvt_term::set_utf8_property (Atom prop, const char *str, int len)
864 wchar_t *ws = rxvt_mbstowcs (str, len);
865 char *s = rxvt_wcstoutf8 (ws);
867 XChangeProperty (dpy, parent[0],
868 prop, xa[XA_UTF8_STRING], 8, PropModeReplace,
869 (const unsigned char *)s, strlen (s));
875 /*----------------------------------------------------------------------*/
876 /*----------------------------------------------------------------------*/
877 /* xterm sequences - title, iconName, color (exptl) */
879 rxvt_term::set_title (const char *str)
881 set_string_property (XA_WM_NAME, str);
883 set_utf8_property (xa[XA_NET_WM_NAME], str);
888 rxvt_term::set_icon_name (const char *str)
890 set_string_property (XA_WM_ICON_NAME, str);
892 set_utf8_property (xa[XA_NET_WM_ICON_NAME], str);
896 #ifdef XTERM_COLOR_CHANGE
898 rxvt_term::set_window_color (int idx, const char *color)
903 if (color == NULL || *color == '\0')
906 color = strdup (color);
907 allocated.push_back ((void *)color);
908 rs[Rs_color + idx] = color;
910 /* handle color aliases */
911 if (isdigit (*color))
915 if (i >= 8 && i <= 15)
916 { /* bright colors */
918 pix_colors_focused[idx] = pix_colors_focused[minBrightCOLOR + i];
922 if (i >= 0 && i <= 7)
923 { /* normal colors */
924 pix_colors_focused[idx] = pix_colors_focused[minCOLOR + i];
929 set_color (xcol, color);
932 * FIXME: should free colors here, but no idea how to do it so instead,
933 * so just keep gobbling up the colormap
936 pix_colors_focused[idx] = xcol;
938 /* XSetWindowAttributes attr; */
941 /*TODO: handle Color_BD, scrollbar background, etc. */
943 update_fade_color (idx);
949 # define set_window_color (idx,color) ((void)0)
950 #endif /* XTERM_COLOR_CHANGE */
953 rxvt_term::recolour_cursor ()
957 (ISSET_PIXCOLOR (Color_pointer_fg)
958 ? pix_colors_focused[Color_pointer_fg]
959 : pix_colors_focused[Color_fg]).get (fg);
961 (ISSET_PIXCOLOR (Color_pointer_bg)
962 ? pix_colors_focused[Color_pointer_bg]
963 : pix_colors_focused[Color_bg]).get (bg);
965 XRecolorCursor (dpy, TermWin_cursor, &fg, &bg);
968 /*----------------------------------------------------------------------*/
970 * find if fg/bg matches any of the normal (low-intensity) colors
973 rxvt_term::set_colorfgbg ()
976 const char *xpmb = "\0";
977 char fstr[sizeof ("default") + 1], bstr[sizeof ("default") + 1];
979 strcpy (fstr, "default");
980 strcpy (bstr, "default");
981 for (i = Color_Black; i <= Color_White; i++)
982 if (pix_colors[Color_fg] == pix_colors[i])
984 sprintf (fstr, "%d", (i - Color_Black));
988 for (i = Color_Black; i <= Color_White; i++)
989 if (pix_colors[Color_bg] == pix_colors[i])
991 sprintf (bstr, "%d", (i - Color_Black));
992 #ifdef XPM_BACKGROUND
998 sprintf (env_colorfgbg, "COLORFGBG=%s;%s%s", fstr, xpmb, bstr);
1001 /*----------------------------------------------------------------------*/
1004 rxvt_term::set_color (rxvt_color &color, const char *name)
1006 if (color.set (this, name))
1009 rxvt_warn ("can't get colour '%s', continuing without.\n", name);
1014 rxvt_term::alias_color (int dst, int src)
1016 pix_colors[dst].set (this, rs[Rs_color + dst] = rs[Rs_color + src]);
1019 /* -------------------------------------------------------------------- *
1020 * - WINDOW RESIZING - *
1021 * -------------------------------------------------------------------- */
1023 rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, int ignoreparent)
1026 int old_width = szHint.width;
1027 int old_height = szHint.height;
1029 window_calc (newwidth, newheight);
1031 if (!HOOK_INVOKE ((this, HOOK_RESIZE_ALL_WINDOWS, DT_INT, newwidth, DT_INT, newheight, DT_END)))
1032 XSetWMNormalHints (dpy, parent[0], &szHint);
1038 * resize by Marius Gedminas <marius.gedminas@uosis.mif.vu.lt>
1039 * reposition window on resize depending on placement on screen
1043 unsigned int unused_w1, unused_h1, unused_b1, unused_d1;
1046 XTranslateCoordinates (dpy, parent[0], display->root,
1047 0, 0, &x, &y, &unused_cr);
1048 XGetGeometry (dpy, parent[0], &unused_cr, &x1, &y1,
1049 &unused_w1, &unused_h1, &unused_b1, &unused_d1);
1051 * if display->root isn't the parent window, a WM will probably have offset
1052 * our position for handles and decorations. Counter it
1054 if (x1 != x || y1 != y)
1060 x1 = (DisplayWidth (dpy, display->screen) - old_width ) / 2;
1061 y1 = (DisplayHeight (dpy, display->screen) - old_height) / 2;
1062 dx = old_width - szHint.width;
1063 dy = old_height - szHint.height;
1065 /* Check position of the center of the window */
1066 if (x < x1) /* left half */
1068 else if (x == x1) /* exact center */
1070 if (y < y1) /* top half */
1072 else if (y == y1) /* exact center */
1075 XMoveResizeWindow (dpy, parent[0], x + dx, y + dy,
1076 szHint.width, szHint.height);
1078 XResizeWindow (dpy, parent[0], szHint.width, szHint.height);
1082 fix_screen = ncol != prev_ncol || nrow != prev_nrow;
1084 if (fix_screen || newwidth != old_width || newheight != old_height)
1086 if (scrollBar.state)
1088 XMoveResizeWindow (dpy, scrollBar.win,
1090 scrollbar_TotalWidth (), szHint.height);
1091 resize_scrollbar ();
1094 XMoveResizeWindow (dpy, vt,
1095 window_vt_x, window_vt_y,
1099 #ifdef XPM_BACKGROUND
1104 if (fix_screen || old_height == 0)
1107 // TODO, with nvidia-8178, resizes kill the alpha channel, report if not fixed in newer version
1108 //scr_touch (false);
1110 #ifdef XPM_BACKGROUND
1121 * Set the width/height of the vt window in characters. Units are pixels.
1122 * good for toggling 80/132 columns
1125 rxvt_term::set_widthheight (unsigned int newwidth, unsigned int newheight)
1127 XWindowAttributes wattr;
1129 if (newwidth == 0 || newheight == 0)
1131 XGetWindowAttributes (dpy, display->root, &wattr);
1134 newwidth = wattr.width - szHint.base_width;
1136 newheight = wattr.height - szHint.base_height;
1139 if (newwidth != width || newheight != height)
1141 newwidth += szHint.base_width;
1142 newheight += szHint.base_height;
1143 resize_all_windows (newwidth, newheight, 0);
1147 /* -------------------------------------------------------------------- *
1148 * - X INPUT METHOD ROUTINES - *
1149 * -------------------------------------------------------------------- */
1153 rxvt_term::im_set_color (unsigned long &fg, unsigned long &bg)
1155 fg = pix_colors [Color_fg];
1156 bg = pix_colors [Color_bg];
1160 rxvt_term::im_set_size (XRectangle &size)
1162 // the int_bwidth terms make no sense to me
1163 size.x = int_bwidth;
1164 size.y = int_bwidth;
1165 size.width = Width2Pixel (ncol) + int_bwidth;
1166 size.height = Height2Pixel (nrow) + int_bwidth;
1170 rxvt_term::im_set_preedit_area (XRectangle &preedit_rect,
1171 XRectangle &status_rect,
1172 const XRectangle &needed_rect)
1174 preedit_rect.x = needed_rect.width;
1176 preedit_rect.width = Width2Pixel (ncol) - needed_rect.width + 1;
1177 preedit_rect.height = fheight;
1181 status_rect.width = needed_rect.width ? needed_rect.width : Width2Pixel (ncol) + 1;
1182 status_rect.height = fheight;
1185 /* Checking whether input method is running. */
1187 rxvt_term::IMisRunning ()
1192 char server[IMBUFSIZ];
1194 /* get current locale modifier */
1195 if ((p = XSetLocaleModifiers (NULL)) != NULL)
1197 strcpy (server, "@server=");
1198 strncat (server, & (p[4]), IMBUFSIZ - 9); /* skip "@im=" */
1200 if ((p = strchr (server + 1, '@')) != NULL) /* first one only */
1203 atom = XInternAtom (dpy, server, False);
1204 win = XGetSelectionOwner (dpy, atom);
1214 rxvt_term::IMSendSpot ()
1217 XVaNestedList preedit_attr;
1221 || !(input_style & (XIMPreeditPosition | XIMPreeditCallbacks)))
1224 im_set_position (nspot);
1226 if (nspot.x == spot.x && nspot.y == spot.y)
1231 preedit_attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
1232 XSetICValues (Input_Context, XNPreeditAttributes, preedit_attr, NULL);
1233 XFree (preedit_attr);
1237 rxvt_term::im_destroy ()
1241 if (Input_Context && input_method->xim)
1242 XDestroyIC (Input_Context);
1244 display->put_xim (input_method);
1251 #ifdef ENABLE_XIM_ONTHESPOT
1254 xim_preedit_start (XIC ic, XPointer client_data, XPointer call_data)
1256 ((rxvt_term *)client_data)->make_current ();
1257 HOOK_INVOKE (((rxvt_term *)client_data, HOOK_XIM_PREEDIT_START, DT_END));
1261 xim_preedit_done (XIC ic, XPointer client_data, XPointer call_data)
1263 ((rxvt_term *)client_data)->make_current ();
1264 HOOK_INVOKE (((rxvt_term *)client_data, HOOK_XIM_PREEDIT_DONE, DT_END));
1268 xim_preedit_draw (XIC ic, XPointer client_data, XIMPreeditDrawCallbackStruct *call_data)
1270 rxvt_term *term = (rxvt_term *)client_data;
1271 XIMText *text = call_data->text;
1273 term->make_current ();
1279 if (!text->encoding_is_wchar && text->string.multi_byte)
1281 // of course, X makes it ugly again
1282 if (term->rs[Rs_imLocale])
1283 SET_LOCALE (term->rs[Rs_imLocale]);
1285 str = rxvt_temp_buf ((text->length + 1) * sizeof (wchar_t));
1286 mbstowcs ((wchar_t *)str, text->string.multi_byte, text->length + 1);
1288 if (term->rs[Rs_imLocale])
1289 SET_LOCALE (term->locale);
1292 str = (void *)text->string.wide_char;
1294 HOOK_INVOKE ((term, HOOK_XIM_PREEDIT_DRAW,
1295 DT_INT, call_data->caret,
1296 DT_INT, call_data->chg_first,
1297 DT_INT, call_data->chg_length,
1298 DT_LCS_LEN, (void *)text->feedback, text->feedback ? (int)text->length : 0,
1299 DT_WCS_LEN, str, str ? (int)text->length : 0,
1303 HOOK_INVOKE ((term, HOOK_XIM_PREEDIT_DRAW,
1304 DT_INT, call_data->caret,
1305 DT_INT, call_data->chg_first,
1306 DT_INT, call_data->chg_length,
1312 xim_preedit_caret (XIC ic, XPointer client_data, XIMPreeditCaretCallbackStruct *call_data)
1314 ((rxvt_term *)client_data)->make_current ();
1315 HOOK_INVOKE (((rxvt_term *)client_data, HOOK_XIM_PREEDIT_CARET,
1316 DT_INT, call_data->position,
1317 DT_INT, call_data->direction,
1318 DT_INT, call_data->style,
1326 * Try to open a XIM with the current modifiers, then see if we can
1327 * open a suitable preedit type
1330 rxvt_term::IM_get_IC (const char *modifiers)
1335 XRectangle rect, status_rect, needed_rect;
1336 unsigned long fg, bg;
1339 XIMStyles *xim_styles;
1340 #ifdef ENABLE_XIM_ONTHESPOT
1346 if (! ((p = XSetLocaleModifiers (modifiers)) && *p))
1349 input_method = display->get_xim (locale, modifiers);
1350 if (input_method == NULL)
1353 xim = input_method->xim;
1354 spot.x = spot.y = -1;
1357 if (XGetIMValues (xim, XNQueryInputStyle, &xim_styles, NULL)
1358 || !xim_styles || !xim_styles->count_styles)
1364 const char *pet[] = { rs[Rs_preeditType], "OverTheSpot,OffTheSpot,Root,None" };
1366 for (int pi = 0; pi < 2; pi++)
1373 s = rxvt_splitcommastring (p);
1375 for (i = found = 0; !found && s[i]; i++)
1377 if (!strcmp (s[i], "OverTheSpot"))
1378 input_style = XIMPreeditPosition | XIMStatusNothing;
1379 else if (!strcmp (s[i], "OffTheSpot"))
1380 input_style = XIMPreeditArea | XIMStatusArea;
1381 else if (!strcmp (s[i], "Root"))
1382 input_style = XIMPreeditNothing | XIMStatusNothing;
1383 else if (!strcmp (s[i], "None"))
1384 input_style = XIMPreeditNone | XIMStatusNone;
1385 #ifdef ENABLE_XIM_ONTHESPOT
1386 else if (SHOULD_INVOKE (HOOK_XIM_PREEDIT_START) && !strcmp (s[i], "OnTheSpot"))
1387 input_style = XIMPreeditCallbacks | XIMStatusNothing;
1390 input_style = XIMPreeditNothing | XIMStatusNothing;
1392 for (j = 0; j < xim_styles->count_styles; j++)
1393 if (input_style == xim_styles->supported_styles[j])
1395 rxvt_freecommastring (s);
1403 rxvt_freecommastring (s);
1417 XVaNestedList preedit_attr = 0, status_attr = 0;
1419 if (input_style & (XIMPreeditPosition | XIMPreeditArea))
1421 // fake us a font-set, please
1422 char **missing_charset_list;
1423 int missing_charset_count;
1428 "-*-*-*-R-*-*-%d-*-*-*-*-*-*,"
1429 "-*-*-*-R-*-*-%d-*-*-*-*-*-*,"
1430 "-*-*-*-R-*-*-%d-*-*-*-*-*-*,"
1431 "-*-*-*-R-*-*-%d-*-*-*-*-*-*,"
1432 "-*-*-*-R-*-*-%d-*-*-*-*-*-*,"
1435 fheight + 1, fheight - 1,
1436 fheight - 2, fheight + 2);
1438 fs = XCreateFontSet (dpy, rs[Rs_imFont] ? rs[Rs_imFont] : pat,
1439 &missing_charset_list, &missing_charset_count, &def_string);
1441 if (missing_charset_list)
1442 XFreeStringList (missing_charset_list);
1446 input_style &= ~(XIMPreeditPosition | XIMPreeditArea);
1447 rxvt_warn ("unable to create fontset for input method, try \"-pt Root\". Continuing.\n");
1451 if (input_style & XIMPreeditPosition)
1454 im_set_position (spot);
1455 im_set_color (fg, bg);
1457 preedit_attr = XVaCreateNestedList (0,
1461 XNSpotLocation, &spot,
1465 else if (input_style & XIMPreeditArea)
1467 im_set_color (fg, bg);
1470 * The necessary width of preedit area is unknown
1471 * until create input context.
1473 needed_rect.width = 0;
1474 im_set_preedit_area (rect, status_rect, needed_rect);
1476 preedit_attr = XVaCreateNestedList (0,
1482 status_attr = XVaCreateNestedList (0,
1485 XNArea, &status_rect,
1489 #if ENABLE_XIM_ONTHESPOT
1490 else if (input_style & XIMPreeditCallbacks)
1492 im_set_position (spot);
1494 xcb[0].client_data = (XPointer)this; xcb[0].callback = (XIMProc)xim_preedit_start;
1495 xcb[1].client_data = (XPointer)this; xcb[1].callback = (XIMProc)xim_preedit_done;
1496 xcb[2].client_data = (XPointer)this; xcb[2].callback = (XIMProc)xim_preedit_draw;
1498 xcb[3].client_data = (XPointer)this; xcb[3].callback = (XIMProc)xim_preedit_caret;
1501 preedit_attr = XVaCreateNestedList (0,
1502 XNSpotLocation, &spot,
1503 XNPreeditStartCallback, &xcb[0],
1504 XNPreeditDoneCallback , &xcb[1],
1505 XNPreeditDrawCallback , &xcb[2],
1507 XNPreeditCaretCallback, &xcb[3],
1513 Input_Context = XCreateIC (xim,
1514 XNInputStyle, input_style,
1516 XNFocusWindow, parent[0],
1517 preedit_attr ? XNPreeditAttributes : NULL,
1519 status_attr ? XNStatusAttributes : NULL,
1522 if (preedit_attr) XFree (preedit_attr);
1523 if (status_attr) XFree (status_attr);
1524 if (fs) XFreeFontSet (dpy, fs);
1526 if (Input_Context == NULL)
1528 rxvt_warn ("failed to create input context, continuing without XIM.\n");
1534 // unfortunately, only the focus window is used by XIM, hard to fix
1535 if (!XGetICValues (Input_Context, XNFilterEvents, &vt_emask_xim, NULL))
1559 #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1560 if (rs[Rs_imLocale])
1561 SET_LOCALE (rs[Rs_imLocale]);
1564 p = rs[Rs_inputMethod];
1569 s = rxvt_splitcommastring (p);
1571 for (i = 0; s[i]; i++)
1575 strcpy (buf, "@im=");
1576 strncat (buf, s[i], IMBUFSIZ - 5);
1577 if (IM_get_IC (buf))
1585 rxvt_freecommastring (s);
1591 /* try with XMODIFIERS env. var. */
1595 /* try with no modifiers base IF the user didn't specify an IM */
1596 if (IM_get_IC ("@im=none"))
1600 #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1601 if (rs[Rs_imLocale])
1602 SET_LOCALE (locale);
1607 rxvt_term::IMSetPosition ()
1609 XRectangle preedit_rect, status_rect, *needed_rect;
1610 XVaNestedList preedit_attr, status_attr;
1614 || !(input_style & (XIMPreeditArea | XIMPreeditPosition))
1618 if (input_style & XIMPreeditPosition)
1620 im_set_size (preedit_rect);
1621 preedit_attr = XVaCreateNestedList (0, XNArea, &preedit_rect, NULL);
1623 XSetICValues (Input_Context,
1624 XNPreeditAttributes, preedit_attr, NULL);
1628 /* Getting the necessary width of preedit area */
1629 status_attr = XVaCreateNestedList (0, XNAreaNeeded, &needed_rect, NULL);
1630 XGetICValues (Input_Context, XNStatusAttributes, status_attr, NULL);
1631 XFree (status_attr);
1633 im_set_preedit_area (preedit_rect, status_rect, *needed_rect);
1634 XFree (needed_rect);
1636 preedit_attr = XVaCreateNestedList (0, XNArea, &preedit_rect, NULL);
1637 status_attr = XVaCreateNestedList (0, XNArea, &status_rect, NULL);
1639 XSetICValues (Input_Context,
1640 XNPreeditAttributes, preedit_attr,
1641 XNStatusAttributes, status_attr,
1644 XFree (status_attr);
1647 XFree (preedit_attr);
1649 #endif /* USE_XIM */
1651 /*----------------------- end-of-file (C source) -----------------------*/