1 /*--------------------------------*-C-*---------------------------------*;
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-2004 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 */
35 #include "main.intpro" /* PROTOS for internal routines */
39 #ifdef TTY_GID_SUPPORT
49 static char curlocale[128];
52 rxvt_set_locale (const char *locale)
54 if (locale && STRNCMP (locale, curlocale, 128))
56 STRNCPY (curlocale, locale, 128);
57 setlocale (LC_CTYPE, curlocale);
62 rxvt_term::operator new (size_t s)
71 rxvt_term::operator delete (void *p, size_t s)
76 rxvt_term::rxvt_term ()
78 rootwin_ev (this, &rxvt_term::rootwin_cb),
79 termwin_ev (this, &rxvt_term::x_cb),
80 vt_ev (this, &rxvt_term::x_cb),
81 #ifdef HAVE_SCROLLBARS
82 scrollbar_ev (this, &rxvt_term::x_cb),
85 menubar_ev (this, &rxvt_term::x_cb), fixme
88 cursor_blink_ev (this, &rxvt_term::cursor_blink_cb),
91 text_blink_ev (this, &rxvt_term::text_blink_cb),
94 pointer_ev (this, &rxvt_term::pointer_cb),
97 im_ev (this, &rxvt_term::im_cb),
99 check_ev (this, &rxvt_term::check_cb),
100 destroy_ev (this, &rxvt_term::destroy_cb),
101 pty_ev (this, &rxvt_term::pty_cb),
102 incr_ev (this, &rxvt_term::incr_cb)
104 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
107 rxvt_term::~rxvt_term ()
112 #ifndef NO_SETOWNER_TTYDEV
113 privileged_ttydev (RESTORE);
116 privileged_utmp (RESTORE);
119 delete TermWin.fontset;
122 if (TermWin.parent[0])
123 XDestroyWindow (display->display, TermWin.parent[0]);
125 // TODO: free pixcolours, colours should become part of rxvt_display
129 displays.put (display);
136 free (env_colorfgbg);
145 rxvt_term::destroy ()
149 rootwin_ev.stop (display);
150 termwin_ev.stop (display);
151 vt_ev.stop (display);
154 im_ev.stop (display);
156 #ifdef HAVE_SCROLLBARS
157 scrollbar_ev.stop (display);
160 menubar_ev.stop (display);
167 cursor_blink_ev.stop ();
170 text_blink_ev.stop ();
176 destroy_ev.start (0);
180 rxvt_term::destroy_cb (time_watcher &w)
187 /*----------------------------------------------------------------------*/
191 rxvt_init (int argc, const char *const *argv)
193 SET_R (new rxvt_term);
195 if (!GET_R->init_vars () || !GET_R->init (argc, argv))
204 static int (*old_xerror_handler) (Display *dpy, XErrorEvent *event);
209 /* install exit handler for cleanup */
212 atexit (rxvt_clean_exit);
219 sigfillset (&sa.sa_mask);
220 sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
221 sa.sa_handler = SIG_IGN; sigaction (SIGHUP , &sa, 0);
222 sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0);
223 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0);
224 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0);
225 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0);
226 sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0);
228 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
229 /* signal (SIGURG, SIG_DFL); */
231 old_xerror_handler = XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler);
232 //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler);
236 rxvt_term::init (int argc, const char *const *argv)
239 * Save and then give up any super-user privileges
240 * If we need privileges in any area then we must specifically request it.
241 * We should only need to be root in these cases:
242 * 1. write utmp entries on some systems
243 * 2. chown tty on some systems
250 const char **cmd_argv = init_resources (argc, argv);
255 menubar_read (rs[Rs_menu]);
257 #ifdef HAVE_SCROLLBARS
258 if (Options & Opt_scrollBar)
259 scrollBar.setIdle (); /* set existence for size calculations */
262 create_windows (argc, argv);
266 scr_reset (); /* initialize screen */
268 Gr_reset (); /* reset graphics */
272 XSynchronize (display->display, True);
275 #ifdef HAVE_SCROLLBARS
276 if (Options & Opt_scrollBar)
277 resize_scrollbar (); /* create and map scrollbar */
280 if (menubar_visible (r))
281 XMapWindow (display->display, menuBar.win);
284 if (Options & Opt_transparent)
286 XSelectInput (display->display, display->root, PropertyChangeMask);
287 check_our_parents ();
291 rootwin_ev.start (display, display->root);
293 XMapWindow (display->display, TermWin.vt);
294 XMapWindow (display->display, TermWin.parent[0]);
296 init_command (cmd_argv);
298 pty_ev.start (cmd_fd, EVENT_READ);
305 /* ------------------------------------------------------------------------- *
306 * SIGNAL HANDLING & EXIT HANDLER *
307 * ------------------------------------------------------------------------- */
309 * Catch a SIGCHLD signal and exit if the direct child has died
314 rxvt_Child_signal (int sig __attribute__ ((unused)))
316 int pid, save_errno = errno;
317 while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR)
328 * Catch a fatal signal and tidy up before quitting
332 rxvt_Exit_signal (int sig)
334 signal (sig, SIG_DFL);
336 rxvt_print_error ("signal %d", sig);
339 kill (getpid (), sig);
344 rxvt_xerror_handler (Display *display, XErrorEvent *event)
346 if (GET_R->allowedxerror == -1)
347 GET_R->allowedxerror = event->error_code;
350 old_xerror_handler (display, event);
357 /*----------------------------------------------------------------------*/
359 * Exit gracefully, clearing the utmp entry and restoring tty attributes
360 * TODO: if debugging, this should free up any known resources if we can
366 // TODO: rxvtd should clean up all ressources
371 /* ------------------------------------------------------------------------- *
372 * MEMORY ALLOCATION WRAPPERS *
373 * ------------------------------------------------------------------------- */
376 rxvt_malloc (size_t size)
384 fprintf (stderr, APL_NAME ": memory allocation failure. Aborting");
392 rxvt_calloc (size_t number, size_t size)
396 p = calloc (number, size);
400 fprintf (stderr, APL_NAME ": memory allocation failure. Aborting");
408 rxvt_realloc (void *ptr, size_t size)
413 p = realloc (ptr, size);
419 fprintf (stderr, APL_NAME ": memory allocation failure. Aborting");
425 /* ------------------------------------------------------------------------- *
426 * PRIVILEGED OPERATIONS *
427 * ------------------------------------------------------------------------- */
428 /* take care of suid/sgid super-user (root) privileges */
430 rxvt_term::privileges (int mode)
432 #if ! defined(__CYGWIN32__)
433 # if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
434 /* setreuid () is the poor man's setuid (), seteuid () */
435 # define seteuid(a) setreuid(-1, (a))
436 # define setegid(a) setregid(-1, (a))
437 # define HAVE_SETEUID
444 * change effective uid/gid - not real uid/gid - so we can switch
445 * back to root later, as required
477 rxvt_term::privileged_utmp (char action)
479 D_MAIN ((stderr, "rxvt_privileged_utmp (%c); waiting for: %c (pid: %d)",
480 action, next_utmp_action, getpid ()));
481 if (next_utmp_action != action || (action != SAVE && action != RESTORE)
482 || (Options & Opt_utmpInhibit)
483 || ttydev == NULL || *ttydev == '\0')
486 privileges (RESTORE);
489 next_utmp_action = RESTORE;
490 makeutent (ttydev, rs[Rs_display_name]);
493 { /* action == RESTORE */
494 next_utmp_action = IGNORE;
501 #ifndef NO_SETOWNER_TTYDEV
503 rxvt_term::privileged_ttydev (char action)
506 "privileged_ttydev (%c); waiting for: %c (pid: %d)",
507 action, next_tty_action, getpid ()));
508 if (next_tty_action != action || (action != SAVE && action != RESTORE)
509 || ttydev == NULL || *ttydev == '\0')
512 privileges (RESTORE);
516 next_tty_action = RESTORE;
517 # ifndef RESET_TTY_TO_COMMON_DEFAULTS
518 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */
519 if (lstat (ttydev, &ttyfd_stat) < 0) /* you lose out */
520 next_tty_action = IGNORE;
525 chown (ttydev, getuid (), ttygid); /* fail silently */
526 chmod (ttydev, ttymode);
534 { /* action == RESTORE */
535 next_tty_action = IGNORE;
536 # ifndef RESET_TTY_TO_COMMON_DEFAULTS
537 chmod (ttydev, ttyfd_stat.st_mode);
538 chown (ttydev, ttyfd_stat.st_uid, ttyfd_stat.st_gid);
541 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
542 chown (ttydev, 0, 0);
549 # ifndef RESET_TTY_TO_COMMON_DEFAULTS
550 D_MAIN ((stderr, "%s \"%s\": mode %03o, uid %d, gid %d",
551 action == RESTORE ? "Restoring" : (action ==
553 "UNKNOWN ERROR for"), ttydev,
554 ttyfd_stat.st_mode, ttyfd_stat.st_uid,
560 /*----------------------------------------------------------------------*/
562 * window size/position calculcations for XSizeHint and other storage.
563 * if width/height are non-zero then override calculated width/height
566 rxvt_term::window_calc (unsigned int width, unsigned int height)
568 short recalc_x, recalc_y;
569 int x, y, sb_w, mb_h, flags;
571 unsigned int max_width, max_height;
573 D_SIZE ((stderr, "< Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d",
574 TermWin.ncol, TermWin.nrow, szHint.width,
576 szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity;
577 szHint.win_gravity = NorthWestGravity;
578 /* szHint.min_aspect.x = szHint.min_aspect.y = 1; */
580 recalc_x = recalc_y = 0;
582 if (!parsed_geometry)
586 flags = XParseGeometry (rs[Rs_geometry], &x, &y, &w, &h);
587 if (flags & WidthValue)
589 TermWin.ncol = BOUND_POSITIVE_INT16 (w);
590 szHint.flags |= USSize;
592 if (flags & HeightValue)
594 TermWin.nrow = BOUND_POSITIVE_INT16 (h);
595 szHint.flags |= USSize;
600 szHint.flags |= USPosition;
601 if (flags & XNegative)
604 szHint.win_gravity = NorthEastGravity;
610 szHint.flags |= USPosition;
611 if (flags & YNegative)
614 if (szHint.win_gravity == NorthEastGravity)
615 szHint.win_gravity = SouthEastGravity;
617 szHint.win_gravity = SouthWestGravity;
622 TermWin.width = TermWin.ncol * TermWin.fwidth;
623 TermWin.height = TermWin.nrow * TermWin.fheight;
624 max_width = MAX_COLS * TermWin.fwidth;
625 max_height = MAX_ROWS * TermWin.fheight;
627 szHint.base_width = szHint.base_height = 2 * TermWin.int_bwidth;
630 window_vt_x = window_vt_y = 0;
631 if (scrollbar_visible ())
633 sb_w = scrollbar_TotalWidth ();
634 szHint.base_width += sb_w;
635 if (! (Options & Opt_scrollBar_right))
638 if (menubar_visible ())
640 mb_h = menuBar_TotalHeight ();
641 szHint.base_height += mb_h;
644 szHint.width_inc = TermWin.fwidth;
645 szHint.height_inc = TermWin.fheight;
646 szHint.min_width = szHint.base_width + szHint.width_inc;
647 szHint.min_height = szHint.base_height + szHint.height_inc;
649 if (width && width - szHint.base_width < max_width)
651 szHint.width = width;
652 TermWin.width = width - szHint.base_width;
656 MIN_IT (TermWin.width, max_width);
657 szHint.width = szHint.base_width + TermWin.width;
659 if (height && height - szHint.base_height < max_height)
661 szHint.height = height;
662 TermWin.height = height - szHint.base_height;
666 MIN_IT (TermWin.height, max_height);
667 szHint.height = szHint.base_height + TermWin.height;
669 if (scrollbar_visible () && (Options & Opt_scrollBar_right))
670 window_sb_x = szHint.width - sb_w;
673 szHint.x += (DisplayWidth (display->display, DefaultScreen (display->display))
674 - szHint.width - 2 * TermWin.ext_bwidth);
676 szHint.y += (DisplayHeight (display->display, DefaultScreen (display->display))
677 - szHint.height - 2 * TermWin.ext_bwidth);
679 TermWin.ncol = TermWin.width / TermWin.fwidth;
680 TermWin.nrow = TermWin.height / TermWin.fheight;
681 D_SIZE ((stderr, "> Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d",
682 TermWin.ncol, TermWin.nrow, szHint.width,
687 /*----------------------------------------------------------------------*/
689 * Tell the teletype handler what size the window is.
690 * Called after a window size change.
693 rxvt_term::tt_winch ()
700 ws.ws_col = TermWin.ncol;
701 ws.ws_row = TermWin.nrow;
702 ws.ws_xpixel = ws.ws_ypixel = 0;
704 (void)ioctl (cmd_fd, TIOCSWINSZ, &ws);
706 if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0)
707 D_SIZE ((stderr, "Failed to send TIOCSWINSZ to fd %d", fd));
709 else if (cmd_pid) /* force through to the command */
710 kill (cmd_pid, SIGWINCH);
715 /*----------------------------------------------------------------------*/
716 /* rxvt_change_font () - Switch to a new font */
718 * init = 1 - initialize
720 * fontname == FONT_UP - switch to bigger font
721 * fontname == FONT_DN - switch to smaller font
724 rxvt_term::change_font (int init, const char *fontname)
728 rxvt_term::font_up_down (int n, int direction)
731 /*----------------------------------------------------------------------*/
732 /*----------------------------------------------------------------------*/
733 /* xterm sequences - title, iconName, color (exptl) */
735 rxvt_term::set_title (const char *str)
737 #ifndef SMART_WINDOW_TITLE
738 XStoreName (display->display, TermWin.parent[0], str);
742 if (XFetchName (display->display, TermWin.parent[0], &name) == 0)
744 if (name == NULL || STRCMP (name, str))
745 XStoreName (display->display, TermWin.parent[0], str);
752 rxvt_term::set_iconName (const char *str)
754 #ifndef SMART_WINDOW_TITLE
755 XSetIconName (display->display, TermWin.parent[0], str);
759 if (XGetIconName (display->display, TermWin.parent[0], &name))
761 if (name == NULL || STRCMP (name, str))
762 XSetIconName (display->display, TermWin.parent[0], str);
768 #ifdef XTERM_COLOR_CHANGE
770 rxvt_term::set_window_color (int idx, const char *color)
775 if (color == NULL || *color == '\0')
778 /* handle color aliases */
779 if (isdigit (*color))
782 if (i >= 8 && i <= 15)
783 { /* bright colors */
785 # ifndef NO_BRIGHTCOLOR
786 PixColors[idx] = PixColors[minBrightCOLOR + i];
792 if (i >= 0 && i <= 7)
793 { /* normal colors */
794 PixColors[idx] = PixColors[minCOLOR + i];
799 if (!rXParseAllocColor (& xcol, color))
801 /* XStoreColor (display->display, XCMAP, XColor*); */
804 * FIXME: should free colors here, but no idea how to do it so instead,
805 * so just keep gobbling up the colormap
808 for (i = Color_Black; i <= Color_White; i++)
809 if (PixColors[idx] == PixColors[i])
813 /* fprintf (stderr, "XFreeColors: PixColors [%d] = %lu\n", idx, PixColors [idx]); */
814 XFreeColors (display->display, XCMAP, (PixColors + idx), 1,
815 DisplayPlanes (display->display, display->screen));
819 PixColors[idx] = xcol;
822 /* XSetWindowAttributes attr; */
825 if (idx == Color_bg && ! (Options & Opt_transparent))
826 XSetWindowBackground (display->display, TermWin.vt,
827 PixColors[Color_bg]);
829 /* handle Color_BD, scrollbar background, etc. */
833 /* the only reasonable way to enforce a clean update */
838 # define set_window_color (idx,color) ((void)0)
839 #endif /* XTERM_COLOR_CHANGE */
842 rxvt_term::recolour_cursor ()
847 xcol[0] = PixColors[Color_pointer];
848 xcol[1] = PixColors[Color_bg];
849 XQueryColors (display->display, XCMAP, xcol, 2);
850 XRecolorCursor (display->display, TermWin_cursor, & (xcol[0]), & (xcol[1]));
854 /*----------------------------------------------------------------------*/
856 * find if fg/bg matches any of the normal (low-intensity) colors
859 rxvt_term::set_colorfgbg ()
862 const char *xpmb = "\0";
863 char fstr[sizeof ("default") + 1], bstr[sizeof ("default") + 1];
866 (char *)rxvt_malloc (sizeof ("COLORFGBG=default;default;bg") + 1);
867 STRCPY (fstr, "default");
868 STRCPY (bstr, "default");
869 for (i = Color_Black; i <= Color_White; i++)
870 if (PixColors[Color_fg] == PixColors[i])
872 sprintf (fstr, "%d", (i - Color_Black));
875 for (i = Color_Black; i <= Color_White; i++)
876 if (PixColors[Color_bg] == PixColors[i])
878 sprintf (bstr, "%d", (i - Color_Black));
879 #ifdef XPM_BACKGROUND
885 sprintf (env_colorfgbg, "COLORFGBG=%s;%s%s", fstr, xpmb, bstr);
887 #ifndef NO_BRIGHTCOLOR
888 colorfgbg = DEFAULT_RSTYLE;
889 for (i = minCOLOR; i <= maxCOLOR; i++)
891 if (PixColors[Color_fg] == PixColors[i])
892 colorfgbg = SET_FGCOLOR (colorfgbg, i);
893 if (PixColors[Color_bg] == PixColors[i])
894 colorfgbg = SET_BGCOLOR (colorfgbg, i);
899 /*----------------------------------------------------------------------*/
901 * Colour determination for low colour displays, routine from
902 * Hans de Goede <hans@highrise.nl>
906 rxvt_term::rXParseAllocColor (rxvt_color *screen_in_out, const char *colour)
908 if (!screen_in_out->set (display, colour))
910 rxvt_print_error ("can't allocate colour: %s", colour);
917 /* -------------------------------------------------------------------- *
918 * - WINDOW RESIZING - *
919 * -------------------------------------------------------------------- */
921 rxvt_term::resize_all_windows (unsigned int width, unsigned int height, int ignoreparent)
926 int old_width = szHint.width, old_height = szHint.height;
929 window_calc (width, height);
930 XSetWMNormalHints (display->display, TermWin.parent[0], &szHint);
935 * resize by Marius Gedminas <marius.gedminas@uosis.mif.vu.lt>
936 * reposition window on resize depending on placement on screen
940 unsigned int unused_w1, unused_h1, unused_b1, unused_d1;
943 XTranslateCoordinates (display->display, TermWin.parent[0], display->root,
944 0, 0, &x, &y, &unused_cr);
945 XGetGeometry (display->display, TermWin.parent[0], &unused_cr, &x1, &y1,
946 &unused_w1, &unused_h1, &unused_b1, &unused_d1);
948 * if display->root isn't the parent window, a WM will probably have offset
949 * our position for handles and decorations. Counter it
951 if (x1 != x || y1 != y)
957 x1 = (DisplayWidth (display->display, display->screen) - old_width) / 2;
958 y1 = (DisplayHeight (display->display, display->screen) - old_height) / 2;
959 dx = old_width - szHint.width;
960 dy = old_height - szHint.height;
962 /* Check position of the center of the window */
963 if (x < x1) /* left half */
965 else if (x == x1) /* exact center */
967 if (y < y1) /* top half */
969 else if (y == y1) /* exact center */
972 XMoveResizeWindow (display->display, TermWin.parent[0], x + dx, y + dy,
973 szHint.width, szHint.height);
975 XResizeWindow (display->display, TermWin.parent[0], szHint.width,
981 fix_screen = (TermWin.ncol != prev_ncol
982 || TermWin.nrow != prev_nrow);
984 if (fix_screen || width != old_width || height != old_height)
986 if (scrollbar_visible ())
988 XMoveResizeWindow (display->display, scrollBar.win, window_sb_x,
989 0, scrollbar_TotalWidth (), szHint.height);
993 if (menubar_visible ())
994 XMoveResizeWindow (display->display, menuBar.win, window_vt_x,
995 0, TermWin_TotalWidth (), menuBar_TotalHeight ());
997 XMoveResizeWindow (display->display, TermWin.vt, window_vt_x,
998 window_vt_y, TermWin_TotalWidth (),
999 TermWin_TotalHeight ());
1000 #ifdef RXVT_GRAPHICS
1002 Gr_Resize (old_width - szHint.base_width,
1003 old_height - szHint.base_height);
1006 #ifdef XPM_BACKGROUND
1012 if (fix_screen || old_height == 0)
1014 int curr_screen = -1;
1015 int old_ncol = prev_ncol;
1017 /* scr_reset only works on the primary screen */
1018 if (old_height) /* this is not the first time through */
1020 unsigned int ncol = TermWin.ncol;
1021 TermWin.ncol = prev_ncol; // save b/c scr_blank_screen_mem uses this
1022 curr_screen = scr_change_screen (PRIMARY);
1023 TermWin.ncol = ncol;
1028 if (curr_screen >= 0) /* this is not the first time through */
1030 scr_change_screen (curr_screen);
1031 selection_check (old_ncol != TermWin.ncol ? 4 : 0);
1035 old_width = szHint.width;
1036 old_height = szHint.height;
1039 IMSetStatusPosition ();
1044 * Set the width/height of the vt window in characters. Units are pixels.
1045 * good for toggling 80/132 columns
1048 rxvt_term::set_widthheight (unsigned int width, unsigned int height)
1050 XWindowAttributes wattr;
1052 if (width == 0 || height == 0)
1054 XGetWindowAttributes (display->display, display->root, &wattr);
1056 width = wattr.width - szHint.base_width;
1058 height = wattr.height - szHint.base_height;
1060 if (width != TermWin.width || height != TermWin.height)
1062 width += szHint.base_width;
1063 height += szHint.base_height;
1064 resize_all_windows (width, height, 0);
1068 /* -------------------------------------------------------------------- *
1069 * - X INPUT METHOD ROUTINES - *
1070 * -------------------------------------------------------------------- */
1074 rxvt_term::im_set_size (XRectangle *size)
1076 size->x = TermWin.int_bwidth;
1077 size->y = TermWin.int_bwidth;
1078 size->width = Width2Pixel (TermWin.ncol);
1079 size->height = Height2Pixel (TermWin.nrow);
1083 rxvt_term::im_set_color (unsigned long *fg, unsigned long *bg)
1085 *fg = PixColors[Color_fg];
1086 *bg = PixColors[Color_bg];
1089 /* Checking whether input method is running. */
1091 rxvt_term::IMisRunning ()
1096 char server[IMBUFSIZ];
1098 /* get current locale modifier */
1099 if ((p = XSetLocaleModifiers (NULL)) != NULL)
1101 STRCPY (server, "@server=");
1102 STRNCAT (server, & (p[4]), IMBUFSIZ - 9); /* skip "@im=" */
1103 if ((p = STRCHR (server + 1, '@')) != NULL) /* first one only */
1106 atom = XInternAtom (display->display, server, False);
1107 win = XGetSelectionOwner (display->display, atom);
1115 rxvt_term::IMSendSpot ()
1118 XVaNestedList preedit_attr;
1120 if (Input_Context == NULL
1121 || !TermWin.focus || ! (input_style & XIMPreeditPosition)
1122 || ! (event_type == KeyPress
1123 || event_type == Expose
1124 || event_type == NoExpose
1125 || event_type == SelectionNotify
1126 || event_type == ButtonRelease || event_type == FocusIn)
1130 im_set_position (&spot);
1132 preedit_attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
1133 XSetICValues (Input_Context, XNPreeditAttributes, preedit_attr, NULL);
1134 XFree (preedit_attr);
1138 rxvt_term::im_set_preedit_area (XRectangle * preedit_rect, XRectangle * status_rect,
1139 XRectangle * needed_rect)
1143 if (scrollbar_visible () && ! (Options & Opt_scrollBar_right))
1144 vtx = scrollbar_TotalWidth ();
1146 mbh = menubar_visible () ? menuBar_TotalHeight () : 0;
1147 mbh -= TermWin.lineSpace;
1149 preedit_rect->x = needed_rect->width + vtx;
1150 preedit_rect->y = Height2Pixel (TermWin.nrow - 1) + mbh;
1152 preedit_rect->width = Width2Pixel (TermWin.ncol + 1) - needed_rect->width + vtx;
1153 preedit_rect->height = Height2Pixel (1);
1155 status_rect->x = vtx;
1156 status_rect->y = Height2Pixel (TermWin.nrow - 1) + mbh;
1158 status_rect->width = needed_rect->width ? needed_rect->width : Width2Pixel (TermWin.ncol + 1);
1159 status_rect->height = Height2Pixel (1);
1163 rxvt_term::im_destroy ()
1167 XDestroyIC (Input_Context);
1168 Input_Context = NULL;
1173 display->put_xim (input_method);
1179 * Try to open a XIM with the current modifiers, then see if we can
1180 * open a suitable preedit type
1183 rxvt_term::IM_get_IC (const char *modifiers)
1188 XRectangle rect, status_rect, needed_rect;
1189 unsigned long fg, bg;
1192 XIMStyles *xim_styles;
1193 XVaNestedList preedit_attr, status_attr;
1195 if (! ((p = XSetLocaleModifiers (modifiers)) && *p))
1198 D_MAIN ((stderr, "rxvt_IM_get_IC ()"));
1199 input_method = display->get_xim (locale, modifiers);
1200 if (input_method == NULL)
1203 xim = input_method->xim;
1206 if (XGetIMValues (xim, XNQueryInputStyle, &xim_styles, NULL)
1207 || !xim_styles || !xim_styles->count_styles)
1209 display->put_xim (input_method);
1213 p = rs[Rs_preeditType] ? rs[Rs_preeditType] : "OverTheSpot,OffTheSpot,Root";
1214 s = rxvt_splitcommastring (p);
1215 for (i = found = 0; !found && s[i]; i++)
1217 if (!STRCMP (s[i], "OverTheSpot"))
1218 input_style = (XIMPreeditPosition | XIMStatusNothing);
1219 else if (!STRCMP (s[i], "OffTheSpot"))
1220 input_style = (XIMPreeditArea | XIMStatusArea);
1221 else if (!STRCMP (s[i], "Root"))
1222 input_style = (XIMPreeditNothing | XIMStatusNothing);
1224 for (j = 0; j < xim_styles->count_styles; j++)
1225 if (input_style == xim_styles->supported_styles[j])
1232 for (i = 0; s[i]; i++)
1240 display->put_xim (input_method);
1244 preedit_attr = status_attr = NULL;
1246 if (input_style & XIMPreeditPosition)
1248 im_set_size (&rect);
1249 im_set_position (&spot);
1250 im_set_color (&fg, &bg);
1252 preedit_attr = XVaCreateNestedList (0, XNArea, &rect,
1253 XNSpotLocation, &spot,
1254 XNForeground, fg, XNBackground, bg,
1255 //XNFontSet, TermWin.fontset,
1258 else if (input_style & XIMPreeditArea)
1260 im_set_color (&fg, &bg);
1263 * The necessary width of preedit area is unknown
1264 * until create input context.
1266 needed_rect.width = 0;
1268 im_set_preedit_area (&rect, &status_rect, &needed_rect);
1270 preedit_attr = XVaCreateNestedList (0, XNArea, &rect,
1271 XNForeground, fg, XNBackground, bg,
1272 //XNFontSet, TermWin.fontset,
1274 status_attr = XVaCreateNestedList (0, XNArea, &status_rect,
1275 XNForeground, fg, XNBackground, bg,
1276 //XNFontSet, TermWin.fontset,
1280 Input_Context = XCreateIC (xim, XNInputStyle, input_style,
1281 XNClientWindow, TermWin.parent[0],
1282 XNFocusWindow, TermWin.parent[0],
1283 preedit_attr ? XNPreeditAttributes : NULL,
1285 status_attr ? XNStatusAttributes : NULL,
1287 if (preedit_attr) XFree (preedit_attr);
1288 if (status_attr) XFree (status_attr);
1290 if (Input_Context == NULL)
1292 rxvt_print_error ("failed to create input context");
1293 display->put_xim (input_method);
1297 if (input_style & XIMPreeditArea)
1298 IMSetStatusPosition ();
1300 D_MAIN ((stderr, "rxvt_IM_get_IC () - successful connection"));
1307 int i, found, had_im;
1314 D_MAIN ((stderr, "rxvt_IMInstantiateCallback ()"));
1318 #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1319 if (rs[Rs_imLocale])
1320 SET_LOCALE (rs[Rs_imLocale]);
1323 p = rs[Rs_inputMethod];
1328 s = rxvt_splitcommastring (p);
1329 for (i = 0; s[i]; i++)
1333 STRCPY (buf, "@im=");
1334 STRNCAT (buf, s[i], IMBUFSIZ - 5);
1335 if (IM_get_IC (buf))
1342 for (i = 0; s[i]; i++)
1350 /* try with XMODIFIERS env. var. */
1354 /* try with no modifiers base IF the user didn't specify an IM */
1355 if (IM_get_IC ("@im=none"))
1359 #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1360 if (rs[Rs_imLocale])
1361 SET_LOCALE (locale);
1366 rxvt_term::IMSetStatusPosition ()
1368 XRectangle preedit_rect, status_rect, *needed_rect;
1369 XVaNestedList preedit_attr, status_attr;
1371 if (Input_Context == NULL
1372 || !TermWin.focus || ! (input_style & XIMPreeditArea)
1376 /* Getting the necessary width of preedit area */
1377 status_attr = XVaCreateNestedList (0, XNAreaNeeded, &needed_rect, NULL);
1378 XGetICValues (Input_Context, XNStatusAttributes, status_attr, NULL);
1379 XFree (status_attr);
1381 im_set_preedit_area (&preedit_rect, &status_rect, needed_rect);
1383 preedit_attr = XVaCreateNestedList (0, XNArea, &preedit_rect, NULL);
1384 status_attr = XVaCreateNestedList (0, XNArea, &status_rect, NULL);
1386 XSetICValues (Input_Context,
1387 XNPreeditAttributes, preedit_attr,
1388 XNStatusAttributes, status_attr, NULL);
1390 XFree (preedit_attr);
1391 XFree (status_attr);
1393 #endif /* USE_XIM */
1395 /*----------------------------------------------------------------------*/
1396 rxvt_t rxvt_current_term;
1398 /*----------------------- end-of-file (C source) -----------------------*/