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) 1996 Chuck Blake <cblake@BBN.COM>
11 * Copyright (c) 1997 mj olesen <olesen@me.queensu.ca>
12 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
13 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
14 * - extensive modifications
15 * Copyright (c) 2003-2008 Marc Lehmann <pcg@goof.com>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *---------------------------------------------------------------------*/
32 * Initialisation routines.
35 #include "../config.h" /* NECESSARY */
36 #include "rxvt.h" /* NECESSARY */
47 #ifdef HAVE_XSETLOCALE
49 # include <X11/Xlocale.h>
51 # ifdef HAVE_SETLOCALE
56 #ifdef HAVE_NL_LANGINFO
57 # include <langinfo.h>
61 /* On Solaris link with -lsocket and -lnsl */
62 #include <sys/types.h>
63 #include <sys/socket.h>
65 /* these next two are probably only on Sun (not Solaris) */
66 #ifdef HAVE_SYS_SOCKIO_H
67 #include <sys/sockio.h>
69 #ifdef HAVE_SYS_BYTEORDER_H
70 #include <sys/byteorder.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
76 #include <net/if_arp.h>
79 rxvt_network_display (const char *display)
81 char buffer[1024], *rval = NULL;
86 if (display[0] != ':' && strncmp (display, "unix:", 5))
87 return (char *) display; /* nothing to do */
89 ifc.ifc_len = sizeof (buffer); /* Get names of all ifaces */
92 if ((skfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
98 if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0)
100 perror ("SIOCGIFCONF");
105 for (i = 0, ifr = ifc.ifc_req;
106 i < (ifc.ifc_len / sizeof (struct ifreq));
111 strcpy (ifr2.ifr_name, ifr->ifr_name);
113 if (ioctl (skfd, SIOCGIFADDR, &ifr2) >= 0)
116 struct sockaddr_in *p_addr;
118 p_addr = (struct sockaddr_in *) &ifr2.ifr_addr;
119 addr = htonl ((unsigned long)p_addr->sin_addr.s_addr);
122 * not "0.0.0.0" or "127.0.0.1" - so format the address
124 if (addr && addr != 0x7F000001)
126 char *colon = strchr (display, ':');
131 rval = rxvt_malloc (strlen (colon) + 16);
132 sprintf (rval, "%d.%d.%d.%d%s",
133 (int) ((addr >> 030) & 0xFF),
134 (int) ((addr >> 020) & 0xFF),
135 (int) ((addr >> 010) & 0xFF),
136 (int) (addr & 0xFF), colon);
148 const char *const def_colorName[] =
152 /* low-intensity colors */
153 "rgb:00/00/00", // 0: black (Black)
154 "rgb:cd/00/00", // 1: red (Red3)
155 "rgb:00/cd/00", // 2: green (Green3)
156 "rgb:cd/cd/00", // 3: yellow (Yellow3)
157 "rgb:00/00/cd", // 4: blue (Blue3)
158 "rgb:cd/00/cd", // 5: magenta (Magenta3)
159 "rgb:00/cd/cd", // 6: cyan (Cyan3)
161 "rgb:e5/e5/e5", // 7: white (Grey90)
163 "rgb:fa/eb/d7", // 7: white (AntiqueWhite)
165 /* high-intensity colors */
167 "rgb:4d/4d/4d", // 8: bright black (Grey30)
169 "rgb:40/40/40", // 8: bright black (Grey25)
171 "rgb:ff/00/00", // 1/9: bright red (Reed)
172 "rgb:00/ff/00", // 2/10: bright green (Green)
173 "rgb:ff/ff/00", // 3/11: bright yellow (Yellow)
174 "rgb:00/00/ff", // 4/12: bright blue (Blue)
175 "rgb:ff/00/ff", // 5/13: bright magenta (Magenta)
176 "rgb:00/ff/ff", // 6/14: bright cyan (Cyan)
177 "rgb:ff/ff/ff", // 7/15: bright white (White)
179 #ifndef USE_256_COLORS
497 #ifndef NO_CURSORCOLOR
498 COLOR_CURSOR_BACKGROUND,
499 COLOR_CURSOR_FOREGROUND,
500 #endif /* ! NO_CURSORCOLOR */
501 NULL, /* Color_pointer_fg */
502 NULL, /* Color_pointer_bg */
503 NULL, /* Color_border */
504 #ifndef NO_BOLD_UNDERLINE_REVERSE
509 #endif /* ! NO_BOLD_UNDERLINE_REVERSE */
511 NULL, // Color_underline
518 #ifdef RXVT_SCROLLBAR
521 #if ENABLE_TRANSPARENCY
530 rxvt_term::init_vars ()
532 pix_colors = pix_colors_focused;
534 MEvent.time = CurrentTime;
535 MEvent.button = AnyButton;
537 priv_modes = SavedModes = PrivMode_Default;
540 int_bwidth = INTERNALBORDERWIDTH;
541 ext_bwidth = EXTERNALBORDERWIDTH;
542 lineSpace = LINESPACE;
543 letterSpace = LETTERSPACE;
544 saveLines = SAVELINES;
546 refresh_type = SLOW_REFRESH;
548 oldcursor.row = oldcursor.col = -1;
550 set_option (Opt_scrollBar);
551 set_option (Opt_scrollTtyOutput);
552 set_option (Opt_jumpScroll);
553 set_option (Opt_skipScroll);
554 set_option (Opt_secondaryScreen);
555 set_option (Opt_secondaryScroll);
556 set_option (Opt_pastableTabs);
557 set_option (Opt_intensityStyles);
558 set_option (Opt_iso14755);
559 set_option (Opt_iso14755_52);
560 set_option (Opt_buffered);
568 if ((i = open ("/dev/null", O_RDONLY)) < 0)
570 dup2 (STDERR_FILENO, STDIN_FILENO);
572 else if (i != STDIN_FILENO)
574 dup2 (i, STDIN_FILENO);
578 dup2 (STDERR_FILENO, STDOUT_FILENO);
581 /*----------------------------------------------------------------------*/
583 rxvt_term::init_resources (int argc, const char *const *argv)
586 const char **cmd_argv, **r_argv;
589 * Look for -e option. Find => split and make cmd_argv[] of command args
591 for (r_argc = 0; r_argc < argc; r_argc++)
592 if (!strcmp (argv[r_argc], "-e"))
597 else if (!argv[r_argc + 1])
598 rxvt_fatal ("option '-e' requires an argument, aborting.\n");
601 cmd_argv = (const char **)rxvt_malloc (sizeof (char *) * (argc - r_argc));
603 for (i = 0; i < argc - r_argc - 1; i++)
604 cmd_argv[i] = (const char *)argv[i + r_argc + 1];
609 r_argv = (const char **)rxvt_malloc (sizeof (char *) * (r_argc + 1));
611 for (i = 0; i < r_argc; i++)
612 r_argv[i] = (const char *)argv[i];
616 rs[Rs_name] = rxvt_basename (argv[0]);
619 * Open display, get options/resources and create the window
622 if ((rs[Rs_display_name] = getenv ("DISPLAY")) == NULL)
623 rs[Rs_display_name] = ":0";
625 get_options (r_argc, r_argv);
627 if (!(display = displays.get (rs[Rs_display_name])))
630 rxvt_fatal ("can't open display %s, aborting.\n", rs[Rs_display_name]);
633 // using a local pointer decreases code size a lot
637 extract_resources ();
641 select_visual (strtol (rs[Rs_depth], 0, 0));
646 for (int i = NUM_RESOURCES; i--; )
647 if (rs [i] == resval_undef)
650 #ifdef HAVE_AFTERIMAGE
651 set_application_name ((char *)rs[Rs_name]);
652 set_output_threshold (OUTPUT_LEVEL_WARNING);
656 if (!rs[Rs_perl_ext_1])
657 rs[Rs_perl_ext_1] = "default";
659 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1])
660 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2])
661 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
663 rxvt_perl.init (this);
664 HOOK_INVOKE ((this, HOOK_INIT, DT_END));
669 * set any defaults not already set
671 if (cmd_argv && cmd_argv[0])
674 rs[Rs_title] = rxvt_basename (cmd_argv[0]);
676 if (!rs[Rs_iconName])
677 rs[Rs_iconName] = rs[Rs_title];
682 rs[Rs_title] = rs[Rs_name];
684 if (!rs[Rs_iconName])
685 rs[Rs_iconName] = rs[Rs_name];
688 if (rs[Rs_saveLines] && (i = atoi (rs[Rs_saveLines])) >= 0)
689 saveLines = min (i, MAX_SAVELINES);
692 if (rs[Rs_int_bwidth] && (i = atoi (rs[Rs_int_bwidth])) >= 0)
693 int_bwidth = min (i, std::numeric_limits<int16_t>::max ());
695 if (rs[Rs_ext_bwidth] && (i = atoi (rs[Rs_ext_bwidth])) >= 0)
696 ext_bwidth = min (i, std::numeric_limits<int16_t>::max ());
698 if (rs[Rs_lineSpace] && (i = atoi (rs[Rs_lineSpace])) >= 0)
699 lineSpace = min (i, std::numeric_limits<int16_t>::max ());
701 if (rs[Rs_letterSpace])
702 letterSpace = atoi (rs[Rs_letterSpace]);
706 if (rs[Rs_pointerBlankDelay] && (i = atoi (rs[Rs_pointerBlankDelay])) >= 0)
707 pointerBlankDelay = i;
709 pointerBlankDelay = 2;
712 /* no point having a scrollbar without having any scrollback! */
714 set_option (Opt_scrollBar, 0);
716 if (!rs[Rs_cutchars])
717 rs[Rs_cutchars] = CUTCHARS;
719 #ifndef NO_BACKSPACE_KEY
720 if (!rs[Rs_backspace_key])
721 # ifdef DEFAULT_BACKSPACE
722 rs[Rs_backspace_key] = DEFAULT_BACKSPACE;
724 rs[Rs_backspace_key] = "DEC"; /* can toggle between \010 or \177 */
728 #ifndef NO_DELETE_KEY
729 if (!rs[Rs_delete_key])
730 # ifdef DEFAULT_DELETE
731 rs[Rs_delete_key] = DEFAULT_DELETE;
733 rs[Rs_delete_key] = "\033[3~";
737 scrollBar.setup (this);
739 #ifdef XTERM_REVERSE_VIDEO
740 /* this is how xterm implements reverseVideo */
741 if (option (Opt_reverseVideo))
743 if (!rs[Rs_color + Color_fg])
744 rs[Rs_color + Color_fg] = def_colorName[Color_bg];
746 if (!rs[Rs_color + Color_bg])
747 rs[Rs_color + Color_bg] = def_colorName[Color_fg];
751 for (i = 0; i < NRS_COLORS; i++)
752 if (!rs[Rs_color + i])
753 rs[Rs_color + i] = def_colorName[i];
755 #ifndef XTERM_REVERSE_VIDEO
756 /* this is how we implement reverseVideo */
757 if (option (Opt_reverseVideo))
758 ::swap (rs[Rs_color + Color_fg], rs[Rs_color + Color_bg]);
761 /* convenient aliases for setting fg/bg to colors */
762 color_aliases (Color_fg);
763 color_aliases (Color_bg);
764 #ifndef NO_CURSORCOLOR
765 color_aliases (Color_cursor);
766 color_aliases (Color_cursor2);
767 #endif /* NO_CURSORCOLOR */
768 color_aliases (Color_pointer_fg);
769 color_aliases (Color_pointer_bg);
770 color_aliases (Color_border);
771 #ifndef NO_BOLD_UNDERLINE_REVERSE
772 color_aliases (Color_BD);
773 color_aliases (Color_UL);
774 color_aliases (Color_RV);
775 #endif /* ! NO_BOLD_UNDERLINE_REVERSE */
777 if (!rs[Rs_color + Color_border])
778 rs[Rs_color + Color_border] = rs[Rs_color + Color_bg];
783 /*----------------------------------------------------------------------*/
785 rxvt_term::init (int argc, const char *const *argv, stringvec *envv)
791 set_environ (envv); // a few things in X do not call setlocale :(
797 const char **cmd_argv = init_resources (argc, argv);
799 #ifdef KEYSYM_RESOURCE
800 keyboard->register_done ();
803 if (const char *path = rs[Rs_chdir])
804 if (*path) // ignored if empty
807 rxvt_fatal ("specified shell working directory must start with a slash, aborting.\n");
810 rxvt_fatal ("unable to change into specified shell working directory, aborting.\n");
813 if (option (Opt_scrollBar))
814 scrollBar.state = STATE_IDLE; /* set existence for size calculations */
816 pty = ptytty::create ();
818 create_windows (argc, argv);
822 scr_poweron (); // initialize screen
825 XSynchronize (dpy, True);
828 if (option (Opt_scrollBar))
829 scrollBar.resize (); /* create and map scrollbar */
830 #ifdef HAVE_BG_PIXMAP
832 bgPixmap.set_target (this);
833 bgPixmap.invalidate ();
835 #ifdef ENABLE_TRANSPARENCY
836 if (option (Opt_transparent))
838 bgPixmap.set_transparent ();
840 #ifdef HAVE_AFTERIMAGE
841 if (rs [Rs_blurradius])
842 bgPixmap.set_blur_radius (rs [Rs_blurradius]);
844 if (ISSET_PIXCOLOR (Color_tint))
845 bgPixmap.set_tint (pix_colors_focused [Color_tint]);
848 bgPixmap.set_shade (rs [Rs_shade]);
850 bgPixmap.set_root_pixmap ();
851 XSelectInput (dpy, display->root, PropertyChangeMask);
852 rootwin_ev.start (display, display->root);
856 #ifdef BG_IMAGE_FROM_FILE
857 if (rs[Rs_backgroundPixmap])
859 const char *p = rs[Rs_backgroundPixmap];
861 if ((p = strchr (p, ';')) != 0)
864 bgPixmap.set_geometry (p);
867 bgPixmap.set_defaultGeometry ();
869 if (bgPixmap.set_file (rs[Rs_backgroundPixmap]))
870 if (!bgPixmap.window_position_sensitive ())
871 update_background ();
878 rootwin_ev.start (display, display->root);
883 init_command (cmd_argv);
888 pty_ev.start (pty->pty, ev::READ);
890 HOOK_INVOKE ((this, HOOK_START, DT_END));
895 long info[2] = { 0, XEMBED_MAPPED };
897 XChangeProperty (dpy, parent[0], xa[XA_XEMBED_INFO], xa[XA_XEMBED_INFO],
898 32, PropModeReplace, (unsigned char *)&info, 2);
902 XMapWindow (dpy, vt);
903 XMapWindow (dpy, parent[0]);
908 /*----------------------------------------------------------------------*/
910 rxvt_term::init_env ()
916 /* Fixup display_name for export over pty to any interested terminal
917 * clients via "ESC[7n" (e.g. shells). Note we use the pure IP number
918 * (for the first non-loopback interface) that we get from
919 * rxvt_network_display (). This is more "name-resolution-portable", if you
920 * will, and probably allows for faster x-client startup if your name
921 * server is beyond a slow link or overloaded at client startup. Of
922 * course that only helps the shell's child processes, not us.
924 * Giving out the display_name also affords a potential security hole
926 val = rxvt_network_display (rs[Rs_display_name]);
927 rs[Rs_display_name] = (const char *)val;
930 #endif /* DISPLAY_IS_IP */
931 val = XDisplayString (dpy);
933 if (rs[Rs_display_name] == NULL)
934 rs[Rs_display_name] = val; /* use broken `:0' value */
937 env_display = (char *)rxvt_malloc (i + 9);
939 sprintf (env_display, "DISPLAY=%s", val);
941 sprintf (env_windowid, "WINDOWID=%lu", (unsigned long)parent[0]);
943 /* add entries to the environment:
944 * @ DISPLAY: in case we started with -display
945 * @ WINDOWID: X window id number of the window
946 * @ COLORTERM: terminal sub-name and also indicates its color
947 * @ TERM: terminal name
948 * @ TERMINFO: path to terminfo directory
949 * @ COLORFGBG: fg;bg color codes
951 putenv (env_display);
952 putenv (env_windowid);
955 putenv (env_colorfgbg);
958 putenv ("TERMINFO=" RXVT_TERMINFO);
962 putenv ("COLORTERM=" COLORTERMENV "-mono");
964 putenv ("COLORTERM=" COLORTERMENVFULL);
966 if (rs[Rs_term_name] != NULL)
968 env_term = (char *)rxvt_malloc (strlen (rs[Rs_term_name]) + 6);
969 sprintf (env_term, "TERM=%s", rs[Rs_term_name]);
973 putenv ("TERM=" TERMENV);
976 /* avoid passing old settings and confusing term size */
978 unsetenv ("COLUMNS");
979 unsetenv ("TERMCAP"); /* terminfo should be okay */
980 #endif /* HAVE_UNSETENV */
983 /*----------------------------------------------------------------------*/
985 * This is more or less stolen straight from XFree86 xterm.
986 * This should support all European type languages.
989 rxvt_term::set_locale (const char *locale)
993 #if HAVE_XSETLOCALE || HAVE_SETLOCALE
995 this->locale = setlocale (LC_CTYPE, locale);
1001 rxvt_warn ("unable to set locale \"%s\", using C locale instead.\n", locale);
1002 setlocale (LC_CTYPE, "C");
1005 rxvt_warn ("default locale unavailable, check LC_* and LANG variables. Continuing.\n");
1011 this->locale = strdup (this->locale);
1012 SET_LOCALE (this->locale);
1016 #if HAVE_NL_LANGINFO
1017 char *codeset = strdup (nl_langinfo (CODESET));
1019 enc_utf8 = (codeset[0] == 'U' || codeset[0] == 'u')
1020 && (codeset[1] == 'T' || codeset[1] == 't')
1021 && (codeset[2] == 'F' || codeset[2] == 'f')
1022 && (codeset[3] == '8' || codeset[4] == '8');
1030 rxvt_term::init_xlocale ()
1036 rxvt_warn ("setting locale failed, working without locale support.\n");
1039 set_string_property (xa[XA_WM_LOCALE_NAME], locale);
1041 if (!XSupportsLocale ())
1043 rxvt_warn ("the locale is not supported by Xlib, working without locale support.\n");
1047 im_ev.start (display);
1049 /* see if we can connect already */
1055 /*----------------------------------------------------------------------*/
1057 rxvt_term::init_command (const char *const *argv)
1060 * Initialize the command connection.
1061 * This should be called after the X server connection is established.
1065 meta_char = option (Opt_meta8) ? 0x80 : C0_ESC;
1070 if (!option (Opt_scrollTtyOutput))
1071 priv_modes |= PrivMode_TtyOutputInh;
1072 if (option (Opt_scrollTtyKeypress))
1073 priv_modes |= PrivMode_Keypress;
1074 if (!option (Opt_jumpScroll))
1075 priv_modes |= PrivMode_smoothScroll;
1077 #ifndef NO_BACKSPACE_KEY
1078 if (strcmp (rs[Rs_backspace_key], "DEC") == 0)
1079 priv_modes |= PrivMode_HaveBackSpace;
1082 /* add value for scrollBar */
1083 if (scrollBar.state)
1085 priv_modes |= PrivMode_scrollBar;
1086 SavedModes |= PrivMode_scrollBar;
1092 /*----------------------------------------------------------------------*/
1094 rxvt_term::get_colours ()
1098 #ifdef OFF_FOCUS_FADING
1099 pix_colors = pix_colors_focused;
1102 for (i = 0; i < (depth <= 2 ? 2 : NRS_COLORS); i++)
1104 const char *name = rs[Rs_color + i];
1111 if (!set_color (xcol, name))
1113 #ifndef XTERM_REVERSE_VIDEO
1114 if (i < 2 && option (Opt_reverseVideo))
1115 name = def_colorName [1 - i];
1118 name = def_colorName [i];
1123 if (!set_color (xcol, name))
1129 rxvt_warn ("unable to get foreground/background colour, continuing.\n");
1132 #ifndef NO_CURSORCOLOR
1135 case Color_pointer_fg:
1136 name = rs[Rs_color + Color_fg];
1137 xcol.set (this, name);
1140 name = rs[Rs_color + Color_bg];
1141 xcol.set (this, name);
1147 pix_colors[i] = xcol;
1148 rs[Rs_color + i] = name;
1153 if (!rs[Rs_color + Color_pointer_fg]) alias_color (Color_pointer_fg, Color_fg);
1154 if (!rs[Rs_color + Color_pointer_bg]) alias_color (Color_pointer_bg, Color_bg);
1155 if (!rs[Rs_color + Color_border] ) alias_color (Color_border, Color_fg);
1159 * get scrollBar shadow colors
1161 * The calculations of topShadow/bottomShadow values are adapted
1162 * from the fvwm window manager.
1164 #ifdef RXVT_SCROLLBAR
1168 alias_color (Color_scroll, Color_fg);
1169 alias_color (Color_topShadow, Color_bg);
1170 alias_color (Color_bottomShadow, Color_bg);
1174 pix_colors [Color_scroll].fade (this, 50, pix_colors [Color_bottomShadow]);
1177 pix_colors [Color_scroll].get (cscroll);
1179 /* topShadowColor */
1180 if (!pix_colors[Color_topShadow].set (this,
1182 min ((int)rgba::MAX_CC, max (cscroll.r / 5, cscroll.r) * 7 / 5),
1183 min ((int)rgba::MAX_CC, max (cscroll.g / 5, cscroll.g) * 7 / 5),
1184 min ((int)rgba::MAX_CC, max (cscroll.b / 5, cscroll.b) * 7 / 5),
1187 alias_color (Color_topShadow, Color_White);
1191 #ifdef OFF_FOCUS_FADING
1192 for (i = 0; i < (depth <= 2 ? 2 : NRS_COLORS); i++)
1193 update_fade_color (i);
1197 /*----------------------------------------------------------------------*/
1198 /* color aliases, fg/bg bright-bold */
1200 rxvt_term::color_aliases (int idx)
1202 if (rs[Rs_color + idx] && isdigit (* (rs[Rs_color + idx])))
1204 int i = atoi (rs[Rs_color + idx]);
1206 if (i >= 8 && i <= 15)
1208 rs[Rs_color + idx] = rs[Rs_color + minBrightCOLOR + i - 8];
1209 else if (i >= 0 && i <= 7)
1211 rs[Rs_color + idx] = rs[Rs_color + minCOLOR + i];
1215 /*----------------------------------------------------------------------*/
1217 * Probe the modifier keymap to get the Meta (Alt) and Num_Lock settings
1218 * Use resource ``modifier'' to override the Meta modifier
1221 rxvt_term::get_ourmods ()
1224 int requestedmeta, realmeta, realalt;
1225 const char *cm, *rsmod;
1226 XModifierKeymap *map;
1228 const unsigned int modmasks[] =
1230 Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
1233 requestedmeta = realmeta = realalt = 0;
1234 rsmod = rs[Rs_modifier];
1237 && strcasecmp (rsmod, "mod1") >= 0 && strcasecmp (rsmod, "mod5") <= 0)
1238 requestedmeta = rsmod[3] - '0';
1240 map = XGetModifierMapping (dpy);
1241 kc = map->modifiermap;
1243 for (i = 1; i < 6; i++)
1245 k = (i + 2) * map->max_keypermod; /* skip shift/lock/control */
1247 for (j = map->max_keypermod; j--; k++)
1252 switch (XKeycodeToKeysym (dpy, kc[k], 0))
1255 ModNumLockMask = modmasks[i - 1];
1258 case XK_ISO_Level3_Shift:
1259 ModLevel3Mask = modmasks[i - 1];
1288 if (rsmod && strncasecmp (rsmod, cm, strlen (cm)) == 0)
1293 XFreeModifiermap (map);
1295 i = requestedmeta ? requestedmeta
1296 : realmeta ? realmeta
1301 ModMetaMask = modmasks[i - 1];
1304 /*----------------------------------------------------------------------*/
1305 /* rxvt_Create_Windows () - Open and map the window */
1307 rxvt_term::create_windows (int argc, const char *const *argv)
1309 XClassHint classHint;
1312 MWMHints mwmhints = { };
1315 XSetWindowAttributes attributes;
1318 dLocal (Display *, dpy);
1320 /* grab colors before netscape does */
1324 rxvt_fatal ("unable to load base fontset, please specify a valid one using -fn, aborting.\n");
1326 parent = display->root;
1328 attributes.override_redirect = !!option (Opt_override_redirect);
1331 if (option (Opt_borderLess))
1333 if (XInternAtom (dpy, "_MOTIF_WM_INFO", True) == None)
1335 // rxvt_warn("Window Manager does not support MWM hints. Bypassing window manager control for borderless window.\n");
1336 attributes.override_redirect = true;
1340 mwmhints.flags = MWM_HINTS_DECORATIONS;
1348 XWindowAttributes wattr;
1350 parent = strtol (rs[Rs_embed], 0, 0);
1352 if (!XGetWindowAttributes (dpy, parent, &wattr))
1353 rxvt_fatal ("invalid window-id specified with -embed, aborting.\n");
1355 window_calc (wattr.width, wattr.height);
1361 /* sub-window placement & size in rxvt_resize_subwindows () */
1362 attributes.background_pixel = pix_colors_focused [Color_border];
1363 attributes.border_pixel = pix_colors_focused [Color_border];
1364 attributes.colormap = cmap;
1366 top = XCreateWindow (dpy, parent,
1368 szHint.width, szHint.height,
1370 depth, InputOutput, visual,
1371 CWColormap | CWBackPixel | CWBorderPixel | CWOverrideRedirect,
1374 this->parent[0] = top;
1376 old_width = szHint.width;
1377 old_height = szHint.height;
1379 set_title (rs [Rs_title]);
1380 set_icon_name (rs [Rs_iconName]);
1382 classHint.res_name = (char *)rs[Rs_name];
1383 classHint.res_class = (char *)RESCLASS;
1385 wmHint.flags = InputHint | StateHint | WindowGroupHint;
1386 wmHint.input = True;
1387 wmHint.initial_state = option (Opt_iconic) ? IconicState : NormalState;
1388 wmHint.window_group = top;
1390 XmbSetWMProperties (dpy, top, NULL, NULL, (char **)argv, argc,
1391 &szHint, &wmHint, &classHint);
1393 # ifdef HAVE_AFTERIMAGE
1396 * rs [Rs_iconfile] is path to icon, asv has been created in init_resources
1399 if (rs [Rs_iconfile])
1403 ASImage *im = file2ASImage (rs [Rs_iconfile], 0xFFFFFFFF, SCREEN_GAMMA, 0, NULL);
1408 long *buffer = (long *)malloc ((2 + w * h) * sizeof (long));
1409 ASImage *result = scale_asimage (asv, im,
1411 100, ASIMAGE_QUALITY_DEFAULT);
1412 destroy_asimage (&im);
1414 if (buffer && result)
1416 ARGB32 *asbuf = result->alt.argb32;
1420 for (unsigned int i = 0; i < w * h; ++i)
1421 buffer [i + 2] = asbuf [i];
1423 destroy_asimage (&result);
1424 XChangeProperty (dpy, top, xa[XA_NET_WM_ICON], XA_CARDINAL, 32,
1425 PropModeReplace, (const unsigned char *) buffer, 2 + w * h);
1431 rxvt_warn ("Memory allocation for icon hint failed, continuing without.\n");
1434 rxvt_warn ("Icon image transformation to ARGB failed, continuing without.\n");
1438 rxvt_warn ("Loading image icon failed, continuing without.\n");
1445 XChangeProperty (dpy, top, xa[XA_MOTIF_WM_HINTS], xa[XA_MOTIF_WM_HINTS], 32,
1446 PropModeReplace, (unsigned char *)&mwmhints, PROP_MWM_HINTS_ELEMENTS);
1449 Atom protocols[] = {
1450 xa[XA_WM_DELETE_WINDOW],
1456 XSetWMProtocols (dpy, top, protocols, sizeof (protocols) / sizeof (protocols[0]));
1459 if (rs[Rs_transient_for])
1460 XSetTransientForHint (dpy, top, (Window)strtol (rs[Rs_transient_for], 0, 0));
1464 long pid = getpid ();
1466 XChangeProperty (dpy, top,
1467 xa[XA_NET_WM_PID], XA_CARDINAL, 32,
1468 PropModeReplace, (unsigned char *)&pid, 1);
1470 // _NET_WM_WINDOW_TYPE is NORMAL, which is the default
1473 XSelectInput (dpy, top,
1475 #if (MOUSE_WHEEL && MOUSE_SLIP_WHEELING) || ENABLE_FRILLS || ISO_14755
1478 | FocusChangeMask | VisibilityChangeMask
1479 | ExposureMask | StructureNotifyMask);
1481 termwin_ev.start (display, top);
1483 /* vt cursor: Black-on-White is standard, but this is more popular */
1484 TermWin_cursor = XCreateFontCursor (dpy, XC_xterm);
1487 vt = XCreateSimpleWindow (dpy, top,
1488 window_vt_x, window_vt_y,
1491 pix_colors_focused[Color_fg],
1492 pix_colors_focused[Color_bg]);
1494 attributes.bit_gravity = NorthWestGravity;
1495 XChangeWindowAttributes (dpy, vt, CWBitGravity, &attributes);
1497 vt_emask = ExposureMask | ButtonPressMask | ButtonReleaseMask | PropertyChangeMask;
1499 if (option (Opt_pointerBlank))
1500 vt_emask |= PointerMotionMask;
1502 vt_emask |= Button1MotionMask | Button3MotionMask;
1506 vt_ev.start (display, vt);
1508 /* graphics context for the vt window */
1509 gcvalue.foreground = pix_colors[Color_fg];
1510 gcvalue.background = pix_colors[Color_bg];
1511 gcvalue.graphics_exposures = 0;
1513 gc = XCreateGC (dpy, vt,
1514 GCForeground | GCBackground | GCGraphicsExposures,
1517 drawable = new rxvt_drawable (this, vt);
1519 #ifdef OFF_FOCUS_FADING
1520 // initially we are in unfocused state
1522 pix_colors = pix_colors_unfocused;
1529 /* ------------------------------------------------------------------------- *
1530 * GET TTY CURRENT STATE *
1531 * ------------------------------------------------------------------------- */
1533 rxvt_get_ttymode (struct termios *tio, int erase)
1536 * standard System V termios interface
1538 if (tcgetattr (STDIN_FILENO, tio) < 0)
1540 // return error - use system defaults,
1541 // where possible, and zero elsewhere
1542 memset (tio, 0, sizeof (struct termios));
1544 tio->c_cc[VINTR] = CINTR;
1545 tio->c_cc[VQUIT] = CQUIT;
1546 tio->c_cc[VERASE] = CERASE;
1548 tio->c_cc[VERASE2] = CERASE2;
1550 tio->c_cc[VKILL] = CKILL;
1551 tio->c_cc[VSTART] = CSTART;
1552 tio->c_cc[VSTOP] = CSTOP;
1553 tio->c_cc[VSUSP] = CSUSP;
1555 tio->c_cc[VDSUSP] = CDSUSP;
1558 tio->c_cc[VREPRINT] = CRPRNT;
1561 tio->c_cc[VDISCRD] = CFLUSH;
1564 tio->c_cc[VWERSE] = CWERASE;
1567 tio->c_cc[VLNEXT] = CLNEXT;
1571 tio->c_cc[VEOF] = CEOF;
1572 tio->c_cc[VEOL] = VDISABLE;
1574 tio->c_cc[VEOL2] = VDISABLE;
1577 tio->c_cc[VSWTC] = VDISABLE;
1580 tio->c_cc[VSWTCH] = VDISABLE;
1583 tio->c_cc[VMIN] = 1;
1586 tio->c_cc[VTIME] = 0;
1590 tio->c_cc[VERASE] = (char)erase;
1593 tio->c_iflag = (BRKINT | IGNPAR | ICRNL
1600 tio->c_oflag = (OPOST | ONLCR);
1603 tio->c_cflag = (CS8 | CREAD);
1605 /* line discipline modes */
1606 tio->c_lflag = (ISIG | ICANON | IEXTEN | ECHO
1607 # if defined (ECHOCTL) && defined (ECHOKE)
1615 #ifdef DEBUG_TTYMODE
1617 fprintf (stderr, "Input flags\n");
1619 /* cpp token stringize doesn't work on all machines <sigh> */
1620 # define FOO(flag,name) \
1621 if ((tio->c_iflag) & flag) \
1622 fprintf (stderr, "%s ", name)
1625 FOO (IGNBRK, "IGNBRK");
1626 FOO (BRKINT, "BRKINT");
1627 FOO (IGNPAR, "IGNPAR");
1628 FOO (PARMRK, "PARMRK");
1629 FOO (INPCK, "INPCK");
1630 FOO (ISTRIP, "ISTRIP");
1631 FOO (INLCR, "INLCR");
1632 FOO (IGNCR, "IGNCR");
1633 FOO (ICRNL, "ICRNL");
1635 FOO (IXOFF, "IXOFF");
1637 FOO (IUCLC, "IUCLC");
1640 FOO (IXANY, "IXANY");
1643 FOO (IMAXBEL, "IMAXBEL");
1646 fprintf (stderr, "\n");
1649 # define FOO(entry, name) \
1650 fprintf (stderr, "%-8s = %#04o\n", name, tio->c_cc [entry])
1652 FOO (VINTR, "VINTR");
1653 FOO (VQUIT, "VQUIT");
1654 FOO (VERASE, "VERASE");
1655 FOO (VKILL, "VKILL");
1659 FOO (VEOL2, "VEOL2");
1662 FOO (VSWTC, "VSWTC");
1665 FOO (VSWTCH, "VSWTCH");
1667 FOO (VSTART, "VSTART");
1668 FOO (VSTOP, "VSTOP");
1669 FOO (VSUSP, "VSUSP");
1671 FOO (VDSUSP, "VDSUSP");
1674 FOO (VREPRINT, "VREPRINT");
1677 FOO (VDISCRD, "VDISCRD");
1680 FOO (VWERSE, "VWERSE");
1683 FOO (VLNEXT, "VLNEXT");
1686 fprintf (stderr, "\n");
1688 #endif /* DEBUG_TTYMODE */
1691 /*----------------------------------------------------------------------*/
1693 * Run the command in a subprocess and return a file descriptor for the
1694 * master end of the pseudo-teletype pair with the command talking to
1698 rxvt_term::run_command (const char *const *argv)
1703 pty->pty = atoi (rs[Rs_pty_fd]);
1708 pty->pty = (*getfd_hook) (pty->pty);
1710 if (pty->pty < 0 || fcntl (pty->pty, F_SETFL, O_NONBLOCK))
1711 rxvt_fatal ("unusable pty-fd filehandle, aborting.\n");
1717 rxvt_fatal ("can't initialize pseudo-tty, aborting.\n");
1721 #ifndef NO_BACKSPACE_KEY
1722 if (rs[Rs_backspace_key][0] && !rs[Rs_backspace_key][1])
1723 er = rs[Rs_backspace_key][0];
1724 else if (strcmp (rs[Rs_backspace_key], "DEC") == 0)
1725 er = '\177'; /* the initial state anyway */
1730 rxvt_get_ttymode (&tio, er);
1731 /* init terminal attributes */
1732 cfsetospeed (&tio, BAUDRATE);
1733 cfsetispeed (&tio, BAUDRATE);
1734 tcsetattr (pty->tty, TCSANOW, &tio);
1735 pty->set_utf8_mode (enc_utf8);
1737 /* set initial window size */
1745 /* spin off the command interpreter */
1746 switch (cmd_pid = fork ())
1751 rxvt_fatal ("can't fork, aborting.\n");
1756 if (!pty->make_controlling_tty ())
1757 fprintf (stderr, "%s: could not obtain control of tty.", RESNAME);
1760 /* Reopen stdin, stdout and stderr over the tty file descriptor */
1761 dup2 (pty->tty, STDIN_FILENO);
1762 dup2 (pty->tty, STDOUT_FILENO);
1763 dup2 (pty->tty, STDERR_FILENO);
1765 // close all our file handles that we do no longer need
1766 for (rxvt_term **t = termlist.begin (); t < termlist.end (); t++)
1768 if ((*t)->pty->pty > 2) close ((*t)->pty->pty);
1769 if ((*t)->pty->tty > 2) close ((*t)->pty->tty);
1773 fprintf (stderr, "%s: unable to exec child.", RESNAME);
1776 _exit (EXIT_FAILURE);
1779 if (!option (Opt_utmpInhibit))
1780 pty->login (cmd_pid, option (Opt_loginShell), rs[Rs_display_name]);
1784 child_ev.start (cmd_pid);
1786 HOOK_INVOKE ((this, HOOK_CHILD_START, DT_INT, cmd_pid, DT_END));
1791 /* ------------------------------------------------------------------------- *
1792 * CHILD PROCESS OPERATIONS *
1793 * ------------------------------------------------------------------------- */
1795 * The only open file descriptor is the slave tty - so no error messages.
1799 rxvt_term::run_child (const char *const *argv)
1803 if (option (Opt_console))
1805 /* be virtual console, fail silently */
1807 unsigned int on = 1;
1809 ioctl (STDIN_FILENO, TIOCCONS, &on);
1810 #elif defined (SRIOCSREDIR)
1813 fd = open (CONSOLE, O_WRONLY, 0);
1815 if (ioctl (fd, SRIOCSREDIR, NULL) < 0)
1817 #endif /* SRIOCSREDIR */
1820 /* reset signals and spin off the command interpreter */
1821 signal (SIGINT, SIG_DFL);
1822 signal (SIGQUIT, SIG_DFL);
1823 signal (SIGCHLD, SIG_DFL);
1824 signal (SIGHUP, SIG_DFL);
1825 signal (SIGPIPE, SIG_DFL);
1827 * mimick login's behavior by disabling the job control signals
1828 * a shell that wants them can turn them back on
1831 signal (SIGTSTP, SIG_IGN);
1832 signal (SIGTTIN, SIG_IGN);
1833 signal (SIGTTOU, SIG_IGN);
1834 #endif /* SIGTSTP */
1836 /* command interpreter path */
1842 for (i = 0; argv[i]; i++)
1843 fprintf (stderr, "argv [%d] = \"%s\"\n", i, argv[i]);
1846 execvp (argv[0], (char *const *)argv);
1847 /* no error message: STDERR is closed! */
1851 const char *argv0, *shell;
1853 if ((shell = getenv ("SHELL")) == NULL || *shell == '\0')
1856 argv0 = rxvt_basename (shell);
1858 if (option (Opt_loginShell))
1860 login = (char *)rxvt_malloc (strlen (argv0) + 2);
1863 strcpy (&login[1], argv0);
1867 execlp (shell, argv0, (char *)0);
1868 /* no error message: STDERR is closed! */
1874 /*----------------------- end-of-file (C source) -----------------------*/