1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
4 * $Id: init.C,v 1.5 2003-11-25 15:44:38 pcg Exp $
6 * All portions of code are copyright by their respective author/s.
7 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
9 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
10 * - extensive modifications
11 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
12 * - extensive modifications
13 * Copyright (c) 1999 D J Hawkey Jr <hawkeyd@visi.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *---------------------------------------------------------------------*/
31 * Initialisation routines.
34 #include "../config.h" /* NECESSARY */
35 #include "rxvt.h" /* NECESSARY */
40 const char *const def_colorName[] = {
43 /* low-intensity colors */
44 "Black", /* 0: black (#000000) */
45 #ifndef NO_BRIGHTCOLOR
46 "Red3", /* 1: red (#CD0000) */
47 "Green3", /* 2: green (#00CD00) */
48 "Yellow3", /* 3: yellow (#CDCD00) */
49 "Blue3", /* 4: blue (#0000CD) */
50 "Magenta3", /* 5: magenta (#CD00CD) */
51 "Cyan3", /* 6: cyan (#00CDCD) */
53 "Grey90", /* 7: white (#E5E5E5) */
55 "AntiqueWhite", /* 7: white (#FAEBD7) */
57 /* high-intensity colors */
59 "Grey30", /* 8: bright black (#4D4D4D) */
61 "Grey25", /* 8: bright black (#404040) */
63 #endif /* NO_BRIGHTCOLOR */
64 "Red", /* 1/9: bright red (#FF0000) */
65 "Green", /* 2/10: bright green (#00FF00) */
66 "Yellow", /* 3/11: bright yellow (#FFFF00) */
67 "Blue", /* 4/12: bright blue (#0000FF) */
68 "Magenta", /* 5/13: bright magenta (#FF00FF) */
69 "Cyan", /* 6/14: bright cyan (#00FFFF) */
70 "White", /* 7/15: bright white (#FFFFFF) */
72 "rgb:00/00/00", /* default 16-255 color table */
313 #ifndef NO_CURSORCOLOR
314 COLOR_CURSOR_BACKGROUND,
315 COLOR_CURSOR_FOREGROUND,
316 #endif /* ! NO_CURSORCOLOR */
317 NULL, /* Color_pointer */
318 NULL, /* Color_border */
319 #ifndef NO_BOLD_UNDERLINE_REVERSE
323 #endif /* ! NO_BOLD_UNDERLINE_REVERSE */
327 #ifdef KEEP_SCROLLCOLOR
330 #endif /* KEEP_SCROLLCOLOR */
333 const char *const xa_names[NUM_XA] = {
352 /*----------------------------------------------------------------------*/
353 /* substitute system functions */
354 #if defined(__svr4__) && ! defined(_POSIX_VERSION)
357 rxvt_getdtablesize(void)
361 getrlimit(RLIMIT_NOFILE, &rlim);
362 return rlim.rlim_cur;
365 /*----------------------------------------------------------------------*/
367 rxvt_term::init_vars()
369 PixColors = new rxvt_color [TOTAL_COLORS];
370 if (PixColors == NULL)
373 #if defined(XPM_BACKGROUND) || defined(TRANSPARENT)
374 TermWin.pixmap = None;
377 next_utmp_action = SAVE;
379 #ifndef NO_SETOWNER_TTYDEV
380 next_tty_action = SAVE;
382 MEvent.time = CurrentTime;
383 MEvent.button = AnyButton;
384 Options = DEFAULT_OPTIONS;
387 cmd_fd = tty_fd = Xfd = -1;
388 PrivateModes = SavedModes = PrivMode_Default;
392 TermWin.int_bwidth = INTERNALBORDERWIDTH;
393 TermWin.ext_bwidth = EXTERNALBORDERWIDTH;
394 TermWin.lineSpace = LINESPACE;
395 TermWin.saveLines = SAVELINES;
396 numPixColors = TOTAL_COLORS;
397 #ifndef NO_NEW_SELECTION
398 selection_style = NEW_SELECT;
400 selection_style = OLD_SELECT;
402 #ifndef NO_BRIGHTCOLOR
403 colorfgbg = DEFAULT_RSTYLE;
405 #if defined (HOTKEY_CTRL) || defined (HOTKEY_META)
406 ks_bigfont = XK_greater;
407 ks_smallfont = XK_less;
410 ks_greekmodeswith = GREEK_KEYBOARD_MODESWITCH;
413 refresh_type = SLOW_REFRESH;
414 prev_nrow = prev_ncol = 0;
416 # ifdef MULTICHAR_ENCODING
417 encoding_method = MULTICHAR_ENCODING;
419 multichar_decode = rxvt_euc2jis;
421 oldcursor.row = oldcursor.col = -1;
422 #ifdef XPM_BACKGROUND
423 /* bgPixmap.w = bgPixmap.h = 0; */
424 bgPixmap.x = bgPixmap.y = 50;
425 bgPixmap.pixmap = None;
427 last_bot = last_state = -1;
430 # if !(MENUBAR_MAX > 1)
431 CurrentBar = &(BarList);
432 # endif /* (MENUBAR_MAX > 1) */
440 rxvt_init_secondary(pR)
443 #ifdef TTY_GID_SUPPORT
444 struct group *gr = getgrnam("tty");
446 if (gr) { /* change group ownership of tty to "tty" */
447 R->ttymode = S_IRUSR | S_IWUSR | S_IWGRP;
448 R->ttygid = gr->gr_gid;
450 #endif /* TTY_GID_SUPPORT */
452 R->ttymode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
453 R->ttygid = getgid();
455 #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
456 R->locale = setlocale(LC_CTYPE, "");
459 /* get number of available file descriptors */
460 #if defined(_POSIX_VERSION) || ! defined(__svr4__)
461 R->num_fds = (int)sysconf(_SC_OPEN_MAX);
463 R->num_fds = rxvt_getdtablesize();
467 * Close all unused file descriptors
468 * We don't want them, we don't need them.
470 if ((i = open("/dev/null", O_RDONLY)) < 0) {
472 dup2(STDERR_FILENO, STDIN_FILENO);
473 } else if (i > STDIN_FILENO) {
474 dup2(i, STDIN_FILENO);
477 dup2(STDERR_FILENO, STDOUT_FILENO);
478 for (i = STDERR_FILENO + 1; i < R->num_fds; i++) {
479 #ifdef __sgi /* Alex Coventry says we need 4 & 7 too */
480 if (i == 4 || i == 7)
487 /*----------------------------------------------------------------------*/
490 rxvt_init_resources(pR_ int argc, const char *const *argv)
494 const char **cmd_argv, **r_argv;
498 * Look for -exec option. Find => split and make cmd_argv[] of command args
500 for (r_argc = 0; r_argc < argc; r_argc++)
501 if (!STRCMP(argv[r_argc], "-e") || !STRCMP(argv[r_argc], "-exec"))
503 r_argv = (const char **)rxvt_malloc(sizeof(char *) * (r_argc + 1));
505 for (i = 0; i < r_argc; i++)
506 r_argv[i] = (const char *)argv[i];
511 cmd_argv = (const char **)rxvt_malloc(sizeof(char *) * (argc - r_argc));
513 for (i = 0; i < argc - r_argc - 1; i++)
514 cmd_argv[i] = (const char *)argv[i + r_argc + 1];
518 /* clear all resources */
520 for (i = 0; i < NUM_RESOURCES;)
523 rs[Rs_name] = rxvt_r_basename(argv[0]);
525 * Open display, get options/resources and create the window
527 if ((rs[Rs_display_name] = getenv("DISPLAY")) == NULL)
528 rs[Rs_display_name] = ":0";
530 rxvt_get_options(aR_ r_argc, r_argv);
533 #ifdef LOCAL_X_IS_UNIX
534 if (rs[Rs_display_name][0] == ':') {
535 val = rxvt_malloc(5 + STRLEN(rs[Rs_display_name]));
537 STRCAT(val, rs[Rs_display_name]);
538 R->Xdisplay = XOpenDisplay(val);
543 if (R->Xdisplay == NULL
544 && (R->Xdisplay = XOpenDisplay(rs[Rs_display_name])) == NULL) {
545 rxvt_print_error("can't open display %s", rs[Rs_display_name]);
549 rxvt_extract_resources(aR_ R->Xdisplay, rs[Rs_name]);
552 * set any defaults not already set
554 if (cmd_argv && cmd_argv[0]) {
556 rs[Rs_title] = rxvt_r_basename(cmd_argv[0]);
557 if (!rs[Rs_iconName])
558 rs[Rs_iconName] = rs[Rs_title];
561 rs[Rs_title] = rs[Rs_name];
562 if (!rs[Rs_iconName])
563 rs[Rs_iconName] = rs[Rs_name];
565 if (rs[Rs_saveLines] && (i = atoi(rs[Rs_saveLines])) >= 0)
566 R->TermWin.saveLines = BOUND_POSITIVE_INT16(i);
568 if (rs[Rs_int_bwidth] && (i = atoi(rs[Rs_int_bwidth])) >= 0)
569 R->TermWin.int_bwidth = min(i, 100); /* arbitrary limit */
570 if (rs[Rs_ext_bwidth] && (i = atoi(rs[Rs_ext_bwidth])) >= 0)
571 R->TermWin.ext_bwidth = min(i, 100); /* arbitrary limit */
574 if (rs[Rs_lineSpace] && (i = atoi(rs[Rs_lineSpace])) >= 0)
575 R->TermWin.lineSpace = min(i, 100); /* arbitrary limit */
579 if (rs[Rs_pointerBlankDelay] && (i = atoi(rs[Rs_pointerBlankDelay])) >= 0)
580 R->pointerBlankDelay = i;
582 R->pointerBlankDelay = 2;
585 /* no point having a scrollbar without having any scrollback! */
586 if (!R->TermWin.saveLines)
587 R->Options &= ~Opt_scrollBar;
590 if (!rs[Rs_print_pipe])
591 rs[Rs_print_pipe] = PRINTPIPE;
593 if (!rs[Rs_cutchars])
594 rs[Rs_cutchars] = CUTCHARS;
596 if (!rs[Rs_acs_chars])
597 rs[Rs_acs_chars] = ACS_CHARS;
598 if ((i = STRLEN(rs[Rs_acs_chars])) < 0x20) {
599 val = rxvt_realloc((void *)rs[Rs_acs_chars], 0x20);
602 rs[Rs_acs_chars] = val;
605 #ifndef NO_BACKSPACE_KEY
606 if (!rs[Rs_backspace_key])
607 # ifdef DEFAULT_BACKSPACE
608 R->key_backspace = DEFAULT_BACKSPACE;
610 R->key_backspace = "DEC"; /* can toggle between \010 or \177 */
613 val = STRDUP(rs[Rs_backspace_key]);
615 rxvt_Str_escaped(val);
616 R->key_backspace = val;
619 #ifndef NO_DELETE_KEY
620 if (!rs[Rs_delete_key])
621 # ifdef DEFAULT_DELETE
622 R->key_delete = DEFAULT_DELETE;
624 R->key_delete = "\033[3~";
627 val = STRDUP(rs[Rs_delete_key]);
629 rxvt_Str_escaped(val);
633 if (rs[Rs_answerbackstring]) {
634 rxvt_Str_trim((char *)rs[Rs_answerbackstring]);
635 rxvt_Str_escaped((char *)rs[Rs_answerbackstring]);
638 if (rs[Rs_selectstyle]) {
639 if (STRNCASECMP(rs[Rs_selectstyle], "oldword", 7) == 0)
640 R->selection_style = OLD_WORD_SELECT;
641 #ifndef NO_OLD_SELECTION
642 else if (STRNCASECMP(rs[Rs_selectstyle], "old", 3) == 0)
643 R->selection_style = OLD_SELECT;
647 #ifdef HAVE_SCROLLBARS
648 rxvt_setup_scrollbar(aR_ rs[Rs_scrollBar_align], rs[Rs_scrollstyle],
649 rs[Rs_scrollBar_thickness]);
652 R->TermWin.fontset = new rxvt_fontset (aR);
653 R->TermWin.fontset->populate (rs[Rs_font]);
654 R->TermWin.fwidth = R->TermWin.fontset->base_font ()->width;
655 R->TermWin.fheight = R->TermWin.fontset->base_font ()->height;
656 R->TermWin.fbase = R->TermWin.fontset->base_font ()->ascent;
658 #ifdef XTERM_REVERSE_VIDEO
659 /* this is how xterm implements reverseVideo */
660 if (R->Options & Opt_reverseVideo) {
661 if (!rs[Rs_color + Color_fg])
662 rs[Rs_color + Color_fg] = def_colorName[Color_bg];
663 if (!rs[Rs_color + Color_bg])
664 rs[Rs_color + Color_bg] = def_colorName[Color_fg];
668 for (i = 0; i < NRS_COLORS; i++)
669 if (!rs[Rs_color + i])
670 rs[Rs_color + i] = def_colorName[i];
672 #ifndef XTERM_REVERSE_VIDEO
673 /* this is how we implement reverseVideo */
674 if (R->Options & Opt_reverseVideo)
675 SWAP_IT(rs[Rs_color + Color_fg], rs[Rs_color + Color_bg], const char *);
678 /* convenient aliases for setting fg/bg to colors */
679 rxvt_color_aliases(aR_ Color_fg);
680 rxvt_color_aliases(aR_ Color_bg);
681 #ifndef NO_CURSORCOLOR
682 rxvt_color_aliases(aR_ Color_cursor);
683 rxvt_color_aliases(aR_ Color_cursor2);
684 #endif /* NO_CURSORCOLOR */
685 rxvt_color_aliases(aR_ Color_pointer);
686 rxvt_color_aliases(aR_ Color_border);
687 #ifndef NO_BOLD_UNDERLINE_REVERSE
688 rxvt_color_aliases(aR_ Color_BD);
689 rxvt_color_aliases(aR_ Color_UL);
690 rxvt_color_aliases(aR_ Color_RV);
691 #endif /* ! NO_BOLD_UNDERLINE_REVERSE */
696 /*----------------------------------------------------------------------*/
706 /* Fixup display_name for export over pty to any interested terminal
707 * clients via "ESC[7n" (e.g. shells). Note we use the pure IP number
708 * (for the first non-loopback interface) that we get from
709 * rxvt_network_display(). This is more "name-resolution-portable", if you
710 * will, and probably allows for faster x-client startup if your name
711 * server is beyond a slow link or overloaded at client startup. Of
712 * course that only helps the shell's child processes, not us.
714 * Giving out the display_name also affords a potential security hole
716 val = rxvt_network_display(R->rs[Rs_display_name]);
717 R->rs[Rs_display_name] = (const char *)val;
719 #endif /* DISPLAY_IS_IP */
720 val = XDisplayString(R->Xdisplay);
721 if (R->rs[Rs_display_name] == NULL)
722 R->rs[Rs_display_name] = val; /* use broken `:0' value */
725 R->env_display = (char *)rxvt_malloc((i + 9) * sizeof(char));
727 sprintf(R->env_display, "DISPLAY=%s", val);
729 /* avoiding the math library:
730 * i = (int)(ceil(log10((unsigned int)R->TermWin.parent[0]))) */
731 for (i = 0, u = (unsigned int)R->TermWin.parent[0]; u; u /= 10, i++) ;
733 R->env_windowid = (char *)rxvt_malloc((i + 10) * sizeof(char));
735 sprintf(R->env_windowid, "WINDOWID=%u",
736 (unsigned int)R->TermWin.parent[0]);
738 /* add entries to the environment:
739 * @ DISPLAY: in case we started with -display
740 * @ WINDOWID: X window id number of the window
741 * @ COLORTERM: terminal sub-name and also indicates its color
742 * @ TERM: terminal name
743 * @ TERMINFO: path to terminfo directory
745 putenv(R->env_display);
746 putenv(R->env_windowid);
748 putenv("TERMINFO=" RXVT_TERMINFO);
751 putenv("COLORTERM=" COLORTERMENV "-mono");
753 putenv("COLORTERM=" COLORTERMENVFULL);
754 if (R->rs[Rs_term_name] != NULL) {
755 R->env_term = (char *)rxvt_malloc((STRLEN(R->rs[Rs_term_name]) + 6) * sizeof(char));
756 sprintf(R->env_term, "TERM=%s", R->rs[Rs_term_name]);
759 putenv("TERM=" TERMENV);
762 /* avoid passing old settings and confusing term size */
765 unsetenv("TERMCAP"); /* terminfo should be okay */
766 #endif /* HAVE_UNSETENV */
769 /*----------------------------------------------------------------------*/
771 * This is more or less stolen straight from XFree86 xterm.
772 * This should support all European type languages.
776 rxvt_init_xlocale(pR)
779 if (R->locale == NULL)
780 rxvt_print_error("Setting locale failed.");
784 wmlocale = XInternAtom(R->Xdisplay, "WM_LOCALE_NAME", False);
785 XChangeProperty(R->Xdisplay, R->TermWin.parent[0], wmlocale,
786 XA_STRING, 8, PropModeReplace,
787 (unsigned char *)R->locale, STRLEN(R->locale));
789 if (XSupportsLocale() != True) {
790 rxvt_print_error("The locale is not supported by Xlib");
793 rxvt_setTermFontSet(aR_ 0);
795 /* see if we can connect yet */
796 rxvt_IMInstantiateCallback(R->Xdisplay, NULL, NULL);
798 /* To avoid Segmentation Fault in C locale: Solaris only? */
799 if (STRCMP(R->locale, "C"))
800 XRegisterIMInstantiateCallback(R->Xdisplay, NULL, NULL, NULL,
801 rxvt_IMInstantiateCallback, NULL);
806 /*----------------------------------------------------------------------*/
809 rxvt_init_command(pR_ const char *const *argv)
812 * Initialize the command connection.
813 * This should be called after the X server connection is established.
817 for (i = 0; i < NUM_XA; i++)
818 R->xa[i] = XInternAtom(R->Xdisplay, xa_names[i], False);
820 /* Enable delete window protocol */
821 XSetWMProtocols(R->Xdisplay, R->TermWin.parent[0],
822 &(R->xa[XA_WMDELETEWINDOW]), 1);
825 /* enable W11 callbacks */
826 W11AddEventHandler(R->Xdisplay, rxvt_W11_process_x_event);
830 R->meta_char = (R->Options & Opt_meta8 ? 0x80 : C0_ESC);
833 rxvt_get_ourmods(aR);
835 if (!(R->Options & Opt_scrollTtyOutput))
836 R->PrivateModes |= PrivMode_TtyOutputInh;
837 if (R->Options & Opt_scrollTtyKeypress)
838 R->PrivateModes |= PrivMode_Keypress;
839 if (!(R->Options & Opt_jumpScroll))
840 R->PrivateModes |= PrivMode_smoothScroll;
841 #ifndef NO_BACKSPACE_KEY
842 if (STRCMP(R->key_backspace, "DEC") == 0)
843 R->PrivateModes |= PrivMode_HaveBackSpace;
845 /* add value for scrollBar */
846 if (scrollbar_visible(R))
848 R->PrivateModes |= PrivMode_scrollBar;
849 R->SavedModes |= PrivMode_scrollBar;
851 if (menubar_visible(R))
853 R->PrivateModes |= PrivMode_menuBar;
854 R->SavedModes |= PrivMode_menuBar;
860 R->Xfd = XConnectionNumber (R->Xdisplay);
863 if (R->Options & Opt_cursorBlink)
864 (void)gettimeofday(&R->lastcursorchange, NULL);
867 if ((R->cmd_fd = rxvt_run_command (aR_ argv)) < 0)
869 rxvt_print_error ("aborting");
874 /*----------------------------------------------------------------------*/
881 for (i = 0; i < (XDEPTH <= 2 ? 2 : NRS_COLORS); i++) {
884 if (!R->rs[Rs_color + i])
887 if (!rxvt_rXParseAllocColor(aR_ &xcol, R->rs[Rs_color + i])) {
888 #ifndef XTERM_REVERSE_VIDEO
889 if (i < 2 && (R->Options & Opt_reverseVideo)) {
890 R->rs[Rs_color + i] = def_colorName[!i];
893 R->rs[Rs_color + i] = def_colorName[i];
894 if (!R->rs[Rs_color + i])
896 if (!rxvt_rXParseAllocColor(aR_ &xcol, R->rs[Rs_color + i])) {
900 /* fatal: need bg/fg color */
901 rxvt_print_error("aborting");
905 #ifndef NO_CURSORCOLOR
907 xcol = R->PixColors[Color_fg];
909 #endif /* ! NO_CURSORCOLOR */
911 xcol = R->PixColors[Color_fg];
914 xcol = R->PixColors[Color_bg]; /* None */
919 R->PixColors[i] = xcol;
923 if (XDEPTH <= 2 || !R->rs[Rs_color + Color_pointer])
924 R->PixColors[Color_pointer] = R->PixColors[Color_fg];
925 if (XDEPTH <= 2 || !R->rs[Rs_color + Color_border])
926 R->PixColors[Color_border] = R->PixColors[Color_fg];
929 * get scrollBar/menuBar shadow colors
931 * The calculations of topShadow/bottomShadow values are adapted
932 * from the fvwm window manager.
934 #ifdef KEEP_SCROLLCOLOR
935 if (XDEPTH <= 2) { /* Monochrome */
936 R->PixColors[Color_scroll] = R->PixColors[Color_fg];
937 R->PixColors[Color_topShadow] = R->PixColors[Color_bg];
938 R->PixColors[Color_bottomShadow] = R->PixColors[Color_bg];
942 * xcol[1] == top shadow
943 * xcol[2] == bot shadow */
945 xcol[1] = R->PixColors[Color_scroll];
947 xcol[0].set (r, -1, -1, -1);
948 /* XFreeColors(R->Xdisplay, XCMAP, &(xcol[0].pixel), 1, ~0); */
950 xcol[0].set (WhitePixel(R->Xdisplay, Xscreen));
953 unsigned short R1, G1, B1, R0, G0, B0;
955 xcol[0].get (r, R0, G0, B0);
956 xcol[1].get (r, R1, G1, B1);
958 /* bottomShadowColor */
959 if (!xcol[2].set (aR_ R1 / 2, G1 / 2, B1 / 2))
960 xcol[2] = R->PixColors[Color_Black];
962 R->PixColors[Color_bottomShadow] = xcol[2];
965 if (!xcol[1].set (aR_
966 min (R0, max (R0 / 5, R1) * 7 / 5),
967 min (G0, max (G0 / 5, G1) * 7 / 5),
968 min (B0, max (B0 / 5, B1) * 7 / 5)))
969 xcol[1] = R->PixColors[Color_White];
971 R->PixColors[Color_topShadow] = xcol[1];
973 #endif /* KEEP_SCROLLCOLOR */
976 /*----------------------------------------------------------------------*/
977 /* color aliases, fg/bg bright-bold */
980 rxvt_color_aliases(pR_ int idx)
982 if (R->rs[Rs_color + idx] && isdigit(*(R->rs[Rs_color + idx]))) {
983 int i = atoi(R->rs[Rs_color + idx]);
985 if (i >= 8 && i <= 15) { /* bright colors */
987 #ifndef NO_BRIGHTCOLOR
988 R->rs[Rs_color + idx] = R->rs[Rs_color + minBrightCOLOR + i];
992 if (i >= 0 && i <= 7) /* normal colors */
993 R->rs[Rs_color + idx] = R->rs[Rs_color + minCOLOR + i];
997 /*----------------------------------------------------------------------*/
999 * Probe the modifier keymap to get the Meta (Alt) and Num_Lock settings
1000 * Use resource ``modifier'' to override the Meta modifier
1004 rxvt_get_ourmods(pR)
1007 int requestedmeta, realmeta, realalt;
1008 const char *cm, *rsmod;
1009 XModifierKeymap *map;
1011 const unsigned int modmasks[] =
1012 { Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask };
1014 requestedmeta = realmeta = realalt = 0;
1015 rsmod = R->rs[Rs_modifier];
1017 && STRCASECMP(rsmod, "mod1") >= 0 && STRCASECMP(rsmod, "mod5") <= 0)
1018 requestedmeta = rsmod[3] - '0';
1020 map = XGetModifierMapping(R->Xdisplay);
1021 kc = map->modifiermap;
1022 for (i = 1; i < 6; i++) {
1023 k = (i + 2) * map->max_keypermod; /* skip shift/lock/control */
1024 for (j = map->max_keypermod; j--; k++) {
1027 switch (XKeycodeToKeysym(R->Xdisplay, kc[k], 0)) {
1029 R->ModNumLockMask = modmasks[i - 1];
1032 continue; /* for(;;) */
1052 if (rsmod && STRNCASECMP(rsmod, cm, STRLEN(cm)) == 0)
1056 XFreeModifiermap(map);
1057 i = (requestedmeta ? requestedmeta
1058 : (realmeta ? realmeta
1059 : (realalt ? realalt : 0)));
1061 R->ModMetaMask = modmasks[i - 1];
1064 /*----------------------------------------------------------------------*/
1065 /* rxvt_Create_Windows() - Open and map the window */
1068 rxvt_Create_Windows(pR_ int argc, const char *const *argv)
1070 XClassHint classHint;
1076 XSetWindowAttributes attributes;
1077 XWindowAttributes gattr;
1079 XCMAP = DefaultColormap(R->Xdisplay, Xscreen);
1080 XVISUAL = DefaultVisual(R->Xdisplay, Xscreen);
1082 #ifdef POINTER_BLANK
1083 static rxvt_color blackcolour;
1084 blackcolour.set (r, 0, 0, 0);
1086 if (R->Options & Opt_transparent) {
1087 XGetWindowAttributes(R->Xdisplay, RootWindow(R->Xdisplay, Xscreen),
1089 XDEPTH = gattr.depth;
1091 XDEPTH = DefaultDepth(R->Xdisplay, Xscreen);
1093 * If depth is not 24, look for a 24bit visual.
1098 if (XMatchVisualInfo(R->Xdisplay, Xscreen, 24, TrueColor, &vinfo)) {
1100 XVISUAL = vinfo.visual;
1101 XCMAP = XCreateColormap(R->Xdisplay,
1102 RootWindow(R->Xdisplay, Xscreen),
1103 XVISUAL, AllocNone);
1109 /* grab colors before netscape does */
1110 rxvt_Get_Colours(aR);
1112 rxvt_change_font(aR_ 1, NULL);
1113 rxvt_window_calc(aR_ 0, 0);
1114 R->old_width = R->szHint.width;
1115 R->old_height = R->szHint.height;
1117 /* parent window - reverse video so we can see placement errors
1118 * sub-window placement & size in rxvt_resize_subwindows()
1122 attributes.background_pixel = R->PixColors[Color_fg];
1123 attributes.border_pixel = R->PixColors[Color_border];
1124 attributes.colormap = XCMAP;
1125 R->TermWin.parent[0] = XCreateWindow(R->Xdisplay, Xroot,
1126 R->szHint.x, R->szHint.y,
1127 R->szHint.width, R->szHint.height,
1128 R->TermWin.ext_bwidth,
1129 XDEPTH, InputOutput,
1131 CWBackPixel | CWBorderPixel
1132 | CWColormap, &attributes);
1134 R->TermWin.parent[0] = XCreateSimpleWindow(R->Xdisplay, Xroot,
1135 R->szHint.x, R->szHint.y,
1138 R->TermWin.ext_bwidth,
1139 R->PixColors[Color_border],
1140 R->PixColors[Color_fg]);
1142 rxvt_xterm_seq(aR_ XTerm_title, R->rs[Rs_title], CHAR_ST);
1143 rxvt_xterm_seq(aR_ XTerm_iconName, R->rs[Rs_iconName], CHAR_ST);
1145 classHint.res_name = (char *)R->rs[Rs_name];
1146 classHint.res_class = (char *)APL_CLASS;
1148 wmHint.flags = (InputHint | StateHint | WindowGroupHint);
1149 wmHint.input = True;
1150 wmHint.initial_state = (R->Options & Opt_iconic ? IconicState
1152 wmHint.window_group = R->TermWin.parent[0];
1154 XSetWMProperties(R->Xdisplay, R->TermWin.parent[0], NULL, NULL,
1155 (char **)argv, argc, &R->szHint, &wmHint, &classHint);
1156 XSelectInput(R->Xdisplay, R->TermWin.parent[0],
1158 #if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
1161 | FocusChangeMask | VisibilityChangeMask
1162 | StructureNotifyMask));
1164 /* vt cursor: Black-on-White is standard, but this is more popular */
1165 R->TermWin_cursor = XCreateFontCursor(R->Xdisplay, XC_xterm);
1167 #if defined(HAVE_SCROLLBARS) || defined(MENUBAR)
1168 /* cursor (menuBar/scrollBar): Black-on-White */
1169 R->pointer_leftptr = XCreateFontCursor(R->Xdisplay, XC_left_ptr);
1172 #ifdef POINTER_BLANK
1173 R->pointer_blank = XCreateGlyphCursor(R->Xdisplay, R->TermWin.font->fid,
1174 R->TermWin.font->fid, ' ', ' ',
1175 &blackcolour, &blackcolour);
1179 R->TermWin.vt = XCreateSimpleWindow(R->Xdisplay, R->TermWin.parent[0],
1180 R->window_vt_x, R->window_vt_y,
1181 TermWin_TotalWidth(),
1182 TermWin_TotalHeight(),
1184 R->PixColors[Color_fg],
1185 R->PixColors[Color_bg]);
1187 XStoreName(R->Xdisplay, R->TermWin.vt, "vt window");
1189 rxvt_pointer_unblank(aR);
1190 vt_emask = (ExposureMask | ButtonPressMask | ButtonReleaseMask
1191 | PropertyChangeMask);
1192 #ifdef POINTER_BLANK
1193 if ((R->Options & Opt_pointerBlank))
1194 vt_emask |= PointerMotionMask;
1197 vt_emask |= (Button1MotionMask | Button3MotionMask);
1198 XSelectInput(R->Xdisplay, R->TermWin.vt, vt_emask);
1200 #if defined(MENUBAR) && (MENUBAR_MAX > 1)
1201 if (menuBar_height()) {
1202 R->menuBar.win = XCreateSimpleWindow(R->Xdisplay, R->TermWin.parent[0],
1204 TermWin_TotalWidth(),
1205 menuBar_TotalHeight(),
1207 R->PixColors[Color_fg],
1208 R->PixColors[Color_scroll]);
1210 XStoreName(R->Xdisplay, R->menuBar.win, "menubar");
1212 XDefineCursor(R->Xdisplay, R->menuBar.win, R->pointer_leftptr);
1213 XSelectInput(R->Xdisplay, R->menuBar.win,
1214 (ExposureMask | ButtonPressMask | ButtonReleaseMask
1215 | Button1MotionMask));
1218 #ifdef XPM_BACKGROUND
1219 if (R->rs[Rs_backgroundPixmap] != NULL
1220 && !(R->Options & Opt_transparent)) {
1221 const char *p = R->rs[Rs_backgroundPixmap];
1223 if ((p = STRCHR(p, ';')) != NULL) {
1225 rxvt_scale_pixmap(aR_ p);
1227 rxvt_set_bgPixmap(aR_ R->rs[Rs_backgroundPixmap]);
1228 rxvt_scr_touch(aR_ True);
1232 /* graphics context for the vt window */
1233 gcvalue.foreground = R->PixColors[Color_fg];
1234 gcvalue.background = R->PixColors[Color_bg];
1235 gcvalue.graphics_exposures = 1;
1236 R->TermWin.gc = XCreateGC(R->Xdisplay, R->TermWin.vt,
1237 GCForeground | GCBackground
1238 | GCGraphicsExposures, &gcvalue);
1240 #if defined(MENUBAR) || defined(RXVT_SCROLLBAR)
1241 gcvalue.foreground = R->PixColors[Color_topShadow];
1242 R->topShadowGC = XCreateGC(R->Xdisplay, R->TermWin.vt,
1243 GCForeground, &gcvalue);
1244 gcvalue.foreground = R->PixColors[Color_bottomShadow];
1245 R->botShadowGC = XCreateGC(R->Xdisplay, R->TermWin.vt,
1246 GCForeground, &gcvalue);
1247 gcvalue.foreground = R->PixColors[(XDEPTH <= 2 ? Color_fg
1249 R->scrollbarGC = XCreateGC(R->Xdisplay, R->TermWin.vt,
1250 GCForeground, &gcvalue);
1254 /*----------------------------------------------------------------------*/
1256 * Run the command in a subprocess and return a file descriptor for the
1257 * master end of the pseudo-teletype pair with the command talking to
1262 rxvt_run_command(pR_ const char *const *argv)
1266 /* get master (pty) */
1267 if ((cfd = rxvt_get_pty(&(R->tty_fd), &(R->ttydev))) < 0) {
1268 rxvt_print_error("can't open pseudo-tty");
1272 if (R->Xfd > FD_SETSIZE || cfd > FD_SETSIZE) {
1273 rxvt_print_error("fd too high: %d max", FD_SETSIZE);
1278 fcntl (cfd, F_SETFL, O_NONBLOCK);
1280 /* get slave (tty) */
1281 if (R->tty_fd < 0) {
1282 #ifndef NO_SETOWNER_TTYDEV
1283 rxvt_privileged_ttydev(aR_ SAVE);
1285 if ((R->tty_fd = rxvt_get_tty(R->ttydev)) < 0) {
1287 rxvt_print_error("can't open slave tty %s", R->ttydev);
1291 #ifndef NO_BACKSPACE_KEY
1292 if (R->key_backspace[0] && !R->key_backspace[1])
1293 er = R->key_backspace[0];
1294 else if (STRCMP(R->key_backspace, "DEC") == 0)
1295 er = '\177'; /* the initial state anyway */
1299 rxvt_get_ttymode(&(R->tio), er);
1301 /* install exit handler for cleanup */
1303 atexit(rxvt_clean_exit);
1305 # ifdef HAVE_ON_EXIT
1306 on_exit(rxvt_clean_exit, NULL); /* non-ANSI exit handler */
1310 signal(SIGHUP, rxvt_Exit_signal);
1312 signal(SIGINT, rxvt_Exit_signal);
1314 signal(SIGQUIT, rxvt_Exit_signal);
1315 signal(SIGTERM, rxvt_Exit_signal);
1316 signal(SIGCHLD, rxvt_Child_signal);
1318 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
1319 /* signal (SIGURG, SIG_DFL); */
1322 /* spin off the command interpreter */
1323 switch (R->cmd_pid = fork()) {
1325 rxvt_print_error("can't fork");
1328 close(cfd); /* only keep R->tty_fd and STDERR open */
1330 if (rxvt_control_tty(R->tty_fd, R->ttydev) < 0)
1331 rxvt_print_error("could not obtain control of tty");
1333 /* Reopen stdin, stdout and stderr over the tty file descriptor */
1334 dup2(R->tty_fd, STDIN_FILENO);
1335 dup2(R->tty_fd, STDOUT_FILENO);
1336 dup2(R->tty_fd, STDERR_FILENO);
1339 rxvt_run_child(aR_ argv);
1345 #if defined(HAVE_STRUCT_UTMP) && defined(HAVE_TTYSLOT)
1348 fdstdin = dup(STDIN_FILENO);
1349 dup2(R->tty_fd, STDIN_FILENO);
1351 rxvt_privileged_utmp(aR_ SAVE);
1352 #if defined(HAVE_STRUCT_UTMP) && defined(HAVE_TTYSLOT)
1353 dup2(fdstdin, STDIN_FILENO);
1357 close(R->tty_fd); /* keep STDERR_FILENO, R->cmd_fd, R->Xfd open */
1360 #else /* __QNX__ uses qnxspawn() */
1361 fchmod(R->tty_fd, 0622);
1362 fcntl(R->tty_fd, F_SETFD, FD_CLOEXEC);
1363 fcntl(cfd, F_SETFD, FD_CLOEXEC);
1365 if (rxvt_run_child(aR_ argv) == -1)
1369 * Reduce R->num_fds to what we use, so select() is more efficient
1371 R->num_fds = max(STDERR_FILENO, cfd);
1372 MAX_IT(R->num_fds, R->Xfd);
1373 #ifdef __sgi /* Alex Coventry says we need 4 & 7 too */
1374 MAX_IT(R->num_fds, 7);
1376 R->num_fds++; /* counts from 0 */
1381 /* ------------------------------------------------------------------------- *
1382 * CHILD PROCESS OPERATIONS *
1383 * ------------------------------------------------------------------------- */
1385 * The only open file descriptor is the slave tty - so no error messages.
1390 rxvt_run_child(pR_ const char *const *argv)
1394 SET_TTYMODE(STDIN_FILENO, &(R->tio)); /* init terminal attributes */
1396 if (R->Options & Opt_console) { /* be virtual console, fail silently */
1398 unsigned int on = 1;
1400 ioctl(STDIN_FILENO, TIOCCONS, &on);
1401 #elif defined (SRIOCSREDIR)
1404 fd = open(CONSOLE, O_WRONLY, 0);
1406 if (ioctl(fd, SRIOCSREDIR, NULL) < 0)
1409 #endif /* SRIOCSREDIR */
1412 /* reset signals and spin off the command interpreter */
1413 signal(SIGINT, SIG_DFL);
1414 signal(SIGQUIT, SIG_DFL);
1415 signal(SIGCHLD, SIG_DFL);
1417 * mimick login's behavior by disabling the job control signals
1418 * a shell that wants them can turn them back on
1421 signal(SIGTSTP, SIG_IGN);
1422 signal(SIGTTIN, SIG_IGN);
1423 signal(SIGTTOU, SIG_IGN);
1424 #endif /* SIGTSTP */
1426 /* set window size */
1427 rxvt_tt_winsize(STDIN_FILENO, R->TermWin.ncol, R->TermWin.nrow, 0);
1430 /* command interpreter path */
1435 for (i = 0; argv[i]; i++)
1436 fprintf(stderr, "argv [%d] = \"%s\"\n", i, argv[i]);
1438 execvp(argv[0], (char *const *)argv);
1439 /* no error message: STDERR is closed! */
1441 const char *argv0, *shell;
1443 if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1446 argv0 = (const char *)rxvt_r_basename(shell);
1447 if (R->Options & Opt_loginShell) {
1448 login = (char *)rxvt_malloc((STRLEN(argv0) + 2) * sizeof(char));
1451 STRCPY(&login[1], argv0);
1454 execlp(shell, argv0, NULL);
1455 /* no error message: STDERR is closed! */
1457 #else /* __QNX__ uses qnxspawn() */
1459 char iov_a[10] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
1460 char *command = NULL, fullcommand[_MAX_PATH];
1461 char **arg_v, *arg_a[2] = { NULL, NULL };
1464 if (access(argv[0], X_OK) == -1) {
1465 if (STRCHR(argv[0], '/') == NULL) {
1466 searchenv(argv[0], "PATH", fullcommand);
1467 if (fullcommand[0] != '\0')
1468 command = fullcommand;
1470 if (access(command, X_OK) == -1)
1476 if ((command = getenv("SHELL")) == NULL || *command == '\0')
1477 command = "/bin/sh";
1479 arg_a[0] = my_basename(command);
1480 if (R->Options & Opt_loginShell) {
1481 login = rxvt_malloc((STRLEN(arg_a[0]) + 2) * sizeof(char));
1484 STRCPY(&login[1], arg_a[0]);
1489 iov_a[0] = iov_a[1] = iov_a[2] = R->tty_fd;
1490 R->cmd_pid = qnx_spawn(0, 0, 0, -1, -1,
1491 _SPAWN_SETSID | _SPAWN_TCSETPGRP,
1492 command, arg_v, environ, iov_a, 0);
1502 /* ------------------------------------------------------------------------- *
1503 * GET TTY CURRENT STATE *
1504 * ------------------------------------------------------------------------- */
1505 /* rxvt_get_ttymode() */
1508 rxvt_get_ttymode(ttymode_t *tio, int erase)
1510 #ifdef HAVE_TERMIOS_H
1512 * standard System V termios interface
1514 if (GET_TERMIOS(STDIN_FILENO, tio) < 0) {
1515 /* return error - use system defaults */
1516 tio->c_cc[VINTR] = CINTR;
1517 tio->c_cc[VQUIT] = CQUIT;
1518 tio->c_cc[VERASE] = CERASE;
1519 tio->c_cc[VKILL] = CKILL;
1520 tio->c_cc[VSTART] = CSTART;
1521 tio->c_cc[VSTOP] = CSTOP;
1522 tio->c_cc[VSUSP] = CSUSP;
1524 tio->c_cc[VDSUSP] = CDSUSP;
1527 tio->c_cc[VREPRINT] = CRPRNT;
1530 tio->c_cc[VDISCRD] = CFLUSH;
1533 tio->c_cc[VWERSE] = CWERASE;
1536 tio->c_cc[VLNEXT] = CLNEXT;
1539 tio->c_cc[VEOF] = CEOF;
1540 tio->c_cc[VEOL] = VDISABLE;
1542 tio->c_cc[VEOL2] = VDISABLE;
1545 tio->c_cc[VSWTC] = VDISABLE;
1548 tio->c_cc[VSWTCH] = VDISABLE;
1551 tio->c_cc[VMIN] = 1;
1554 tio->c_cc[VTIME] = 0;
1557 tio->c_cc[VERASE] = (char)erase;
1560 tio->c_iflag = (BRKINT | IGNPAR | ICRNL
1567 tio->c_oflag = (OPOST | ONLCR);
1570 tio->c_cflag = (CS8 | CREAD);
1572 /* line discipline modes */
1573 tio->c_lflag = (ISIG | ICANON | IEXTEN | ECHO
1574 # if defined (ECHOCTL) && defined (ECHOKE)
1578 # else /* HAVE_TERMIOS_H */
1584 /* get parameters -- gtty */
1585 if (ioctl(STDIN_FILENO, TIOCGETP, &(tio->sg)) < 0) {
1586 tio->sg.sg_erase = CERASE; /* ^H */
1587 tio->sg.sg_kill = CKILL; /* ^U */
1590 tio->sg.sg_erase = (char)erase;
1592 tio->sg.sg_flags = (CRMOD | ECHO | EVENP | ODDP);
1594 /* get special characters */
1595 if (ioctl(STDIN_FILENO, TIOCGETC, &(tio->tc)) < 0) {
1596 tio->tc.t_intrc = CINTR; /* ^C */
1597 tio->tc.t_quitc = CQUIT; /* ^\ */
1598 tio->tc.t_startc = CSTART; /* ^Q */
1599 tio->tc.t_stopc = CSTOP; /* ^S */
1600 tio->tc.t_eofc = CEOF; /* ^D */
1601 tio->tc.t_brkc = -1;
1603 /* get local special chars */
1604 if (ioctl(STDIN_FILENO, TIOCGLTC, &(tio->lc)) < 0) {
1605 tio->lc.t_suspc = CSUSP; /* ^Z */
1606 tio->lc.t_dsuspc = CDSUSP; /* ^Y */
1607 tio->lc.t_rprntc = CRPRNT; /* ^R */
1608 tio->lc.t_flushc = CFLUSH; /* ^O */
1609 tio->lc.t_werasc = CWERASE; /* ^W */
1610 tio->lc.t_lnextc = CLNEXT; /* ^V */
1612 /* get line discipline */
1613 ioctl(STDIN_FILENO, TIOCGETD, &(tio->line));
1615 tio->line = NTTYDISC;
1616 # endif /* NTTYDISC */
1617 tio->local = (LCRTBS | LCRTERA | LCTLECH | LPASS8 | LCRTKIL);
1618 #endif /* HAVE_TERMIOS_H */
1623 #ifdef DEBUG_TTYMODE
1624 #ifdef HAVE_TERMIOS_H
1626 fprintf(stderr, "Input flags\n");
1628 /* cpp token stringize doesn't work on all machines <sigh> */
1629 # define FOO(flag,name) \
1630 if ((tio->c_iflag) & flag) \
1631 fprintf (stderr, "%s ", name)
1634 FOO(IGNBRK, "IGNBRK");
1635 FOO(BRKINT, "BRKINT");
1636 FOO(IGNPAR, "IGNPAR");
1637 FOO(PARMRK, "PARMRK");
1638 FOO(INPCK, "INPCK");
1639 FOO(ISTRIP, "ISTRIP");
1640 FOO(INLCR, "INLCR");
1641 FOO(IGNCR, "IGNCR");
1642 FOO(ICRNL, "ICRNL");
1644 FOO(IXOFF, "IXOFF");
1646 FOO(IUCLC, "IUCLC");
1649 FOO(IXANY, "IXANY");
1652 FOO(IMAXBEL, "IMAXBEL");
1654 fprintf(stderr, "\n");
1657 # define FOO(entry, name) \
1658 fprintf(stderr, "%-8s = %#04o\n", name, tio->c_cc [entry])
1660 FOO(VINTR, "VINTR");
1661 FOO(VQUIT, "VQUIT");
1662 FOO(VERASE, "VERASE");
1663 FOO(VKILL, "VKILL");
1667 FOO(VEOL2, "VEOL2");
1670 FOO(VSWTC, "VSWTC");
1673 FOO(VSWTCH, "VSWTCH");
1675 FOO(VSTART, "VSTART");
1676 FOO(VSTOP, "VSTOP");
1677 FOO(VSUSP, "VSUSP");
1679 FOO(VDSUSP, "VDSUSP");
1682 FOO(VREPRINT, "VREPRINT");
1685 FOO(VDISCRD, "VDISCRD");
1688 FOO(VWERSE, "VWERSE");
1691 FOO(VLNEXT, "VLNEXT");
1693 fprintf(stderr, "\n");
1695 # endif /* HAVE_TERMIOS_H */
1696 #endif /* DEBUG_TTYMODE */
1699 /*----------------------- end-of-file (C source) -----------------------*/