1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: main.c |
2 | * File: main.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: main.C,v 1.8 2003/11/27 10:12:10 pcg Exp $ |
4 | * $Id: main.C,v 1.15 2003/12/18 05:45:11 pcg Exp $ |
5 | * |
5 | * |
6 | * All portions of code are copyright by their respective author/s. |
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> |
7 | * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> |
8 | * - original version |
8 | * - original version |
9 | * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> |
9 | * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> |
… | |
… | |
42 | |
42 | |
43 | #ifdef HAVE_TERMIOS_H |
43 | #ifdef HAVE_TERMIOS_H |
44 | # include <termios.h> |
44 | # include <termios.h> |
45 | #endif |
45 | #endif |
46 | |
46 | |
|
|
47 | #include <cstring> |
|
|
48 | |
|
|
49 | static char curlocale[128]; |
|
|
50 | |
|
|
51 | void |
|
|
52 | rxvt_set_locale (const char *locale) |
|
|
53 | { |
|
|
54 | if (locale && strncmp (locale, curlocale, 128)) |
|
|
55 | { |
|
|
56 | strncpy (curlocale, locale, 128); |
|
|
57 | setlocale (LC_CTYPE, curlocale); |
|
|
58 | } |
|
|
59 | } |
|
|
60 | |
47 | void * |
61 | void * |
48 | rxvt_term::operator new (size_t s) |
62 | rxvt_term::operator new (size_t s) |
49 | { |
63 | { |
50 | void *p = malloc (s); |
64 | void *p = malloc (s); |
51 | |
65 | |
… | |
… | |
66 | #endif |
80 | #endif |
67 | #ifdef POINTER_BLANK |
81 | #ifdef POINTER_BLANK |
68 | pointer_ev (this, &rxvt_term::pointer_cb), |
82 | pointer_ev (this, &rxvt_term::pointer_cb), |
69 | #endif |
83 | #endif |
70 | x_ev (this, &rxvt_term::x_cb), |
84 | x_ev (this, &rxvt_term::x_cb), |
71 | destroy_ev (this, &rxvt_term::destroy_cb) |
85 | destroy_ev (this, &rxvt_term::destroy_cb), |
|
|
86 | check_ev (this, &rxvt_term::check_cb), |
|
|
87 | incr_ev (this, &rxvt_term::incr_cb) |
72 | { |
88 | { |
73 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
89 | cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; |
74 | } |
90 | } |
75 | |
91 | |
76 | rxvt_term::~rxvt_term () |
92 | rxvt_term::~rxvt_term () |
77 | { |
93 | { |
78 | rxvt_scr_release (this); |
94 | scr_release (); |
|
|
95 | |
|
|
96 | free (locale); |
|
|
97 | free (codeset); |
79 | |
98 | |
80 | #ifndef NO_SETOWNER_TTYDEV |
99 | #ifndef NO_SETOWNER_TTYDEV |
81 | rxvt_privileged_ttydev (this, RESTORE); |
100 | rxvt_privileged_ttydev (this, RESTORE); |
82 | #endif |
101 | #endif |
83 | #ifdef UTMP_SUPPORT |
102 | #ifdef UTMP_SUPPORT |
… | |
… | |
101 | } |
120 | } |
102 | |
121 | |
103 | void |
122 | void |
104 | rxvt_term::destroy () |
123 | rxvt_term::destroy () |
105 | { |
124 | { |
|
|
125 | check_ev.stop (); |
106 | pty_ev.stop (); |
126 | pty_ev.stop (); |
107 | x_ev.stop (); |
127 | x_ev.stop (); |
108 | #ifdef CURSOR_BLINK |
128 | #ifdef CURSOR_BLINK |
109 | blink_ev.stop (); |
129 | blink_ev.stop (); |
110 | #endif |
130 | #endif |
… | |
… | |
125 | |
145 | |
126 | /*----------------------------------------------------------------------*/ |
146 | /*----------------------------------------------------------------------*/ |
127 | /* rxvt_init() */ |
147 | /* rxvt_init() */ |
128 | /* LIBPROTO */ |
148 | /* LIBPROTO */ |
129 | rxvt_t |
149 | rxvt_t |
130 | rxvt_init(int argc, const char *const *argv) |
150 | rxvt_init (int argc, const char *const *argv) |
131 | { |
151 | { |
132 | SET_R(new rxvt_term); |
152 | SET_R (new rxvt_term); |
133 | dR; |
153 | dR; |
134 | |
154 | |
135 | if (!R->init_vars () || !R->init (argc, argv)) |
155 | if (!R->init_vars () || !R->init (argc, argv)) |
136 | { |
156 | { |
137 | delete R; |
157 | delete R; |
… | |
… | |
148 | /* install exit handler for cleanup */ |
168 | /* install exit handler for cleanup */ |
149 | #if 0 |
169 | #if 0 |
150 | #ifdef HAVE_ATEXIT |
170 | #ifdef HAVE_ATEXIT |
151 | atexit(rxvt_clean_exit); |
171 | atexit(rxvt_clean_exit); |
152 | #else |
172 | #else |
153 | # ifdef HAVE_ON_EXIT |
|
|
154 | on_exit(rxvt_clean_exit, NULL); /* non-ANSI exit handler */ |
|
|
155 | # endif |
|
|
156 | #endif |
173 | #endif |
157 | #endif |
174 | #endif |
158 | |
175 | |
159 | struct sigaction sa; |
176 | struct sigaction sa; |
160 | |
177 | |
161 | sigfillset (&sa.sa_mask); |
178 | sigfillset (&sa.sa_mask); |
162 | sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; |
179 | sa.sa_flags = SA_NOCLDSTOP | SA_RESTART; |
163 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGHUP , &sa, 0); //TODO, also: SIGPIPE |
180 | sa.sa_handler = SIG_IGN; sigaction (SIGHUP , &sa, 0); |
|
|
181 | sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, 0); |
164 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0); |
182 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0); |
165 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0); |
183 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0); |
166 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0); |
184 | sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0); |
167 | sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0); |
185 | sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0); |
168 | |
186 | |
… | |
… | |
174 | } |
192 | } |
175 | |
193 | |
176 | bool |
194 | bool |
177 | rxvt_term::init (int argc, const char *const *argv) |
195 | rxvt_term::init (int argc, const char *const *argv) |
178 | { |
196 | { |
179 | dR;//TODO (scrollbar, setidle) |
|
|
180 | |
|
|
181 | /* |
197 | /* |
182 | * Save and then give up any super-user privileges |
198 | * Save and then give up any super-user privileges |
183 | * If we need privileges in any area then we must specifically request it. |
199 | * If we need privileges in any area then we must specifically request it. |
184 | * We should only need to be root in these cases: |
200 | * We should only need to be root in these cases: |
185 | * 1. write utmp entries on some systems |
201 | * 1. write utmp entries on some systems |
186 | * 2. chown tty on some systems |
202 | * 2. chown tty on some systems |
187 | */ |
203 | */ |
188 | rxvt_privileges (this, SAVE); |
204 | rxvt_privileges (this, SAVE); |
189 | rxvt_privileges (this, IGNORE); |
205 | rxvt_privileges (this, IGNORE); |
190 | |
206 | |
|
|
207 | #if HAVE_XSETLOCALE || HAVE_SETLOCALE |
|
|
208 | locale = strdup (setlocale (LC_CTYPE, "")); |
|
|
209 | #endif |
|
|
210 | #if HAVE_NL_LANGINFO |
|
|
211 | codeset = strdup (nl_langinfo (CODESET)); |
|
|
212 | #endif |
|
|
213 | |
191 | rxvt_init_secondary (this); |
214 | init_secondary (); |
192 | |
215 | |
193 | const char **cmd_argv = rxvt_init_resources (this, argc, argv); |
216 | const char **cmd_argv = init_resources (argc, argv); |
194 | |
217 | |
195 | #if (MENUBAR_MAX) |
218 | #if (MENUBAR_MAX) |
196 | rxvt_menubar_read (this, rs[Rs_menu]); |
219 | rxvt_menubar_read (this, rs[Rs_menu]); |
197 | #endif |
220 | #endif |
198 | #ifdef HAVE_SCROLLBARS |
221 | #ifdef HAVE_SCROLLBARS |
199 | if (Options & Opt_scrollBar) |
222 | if (Options & Opt_scrollBar) |
200 | scrollbar_setIdle (); /* set existence for size calculations */ |
223 | scrollBar.setIdle (); /* set existence for size calculations */ |
201 | #endif |
224 | #endif |
202 | |
225 | |
203 | rxvt_Create_Windows (this, argc, argv); |
226 | rxvt_Create_Windows (this, argc, argv); |
204 | |
227 | |
205 | rxvt_init_xlocale (this); |
228 | init_xlocale (); |
206 | |
229 | |
207 | rxvt_scr_reset (this); /* initialize screen */ |
230 | scr_reset (); /* initialize screen */ |
208 | #ifdef RXVT_GRAPHICS |
231 | #ifdef RXVT_GRAPHICS |
209 | rxvt_Gr_reset (this); /* reset graphics */ |
232 | rxvt_Gr_reset (this); /* reset graphics */ |
210 | #endif |
233 | #endif |
211 | |
234 | |
212 | #if 0 |
235 | #if 0 |
… | |
… | |
231 | } |
254 | } |
232 | #endif |
255 | #endif |
233 | XMapWindow (Xdisplay, TermWin.vt); |
256 | XMapWindow (Xdisplay, TermWin.vt); |
234 | XMapWindow (Xdisplay, TermWin.parent[0]); |
257 | XMapWindow (Xdisplay, TermWin.parent[0]); |
235 | |
258 | |
236 | rxvt_init_env (this); |
259 | init_env (); |
237 | rxvt_init_command (this, cmd_argv); |
260 | init_command (cmd_argv); |
238 | |
261 | |
239 | x_ev.start (Xfd, EVENT_READ); |
262 | x_ev.start (Xfd, EVENT_READ); |
240 | pty_ev.start (cmd_fd, EVENT_READ); |
263 | pty_ev.start (cmd_fd, EVENT_READ); |
241 | |
264 | |
242 | flush (); |
265 | check_ev.start (); |
|
|
266 | |
|
|
267 | process_x_events (); |
243 | |
268 | |
244 | return true; |
269 | return true; |
245 | } |
270 | } |
246 | |
271 | |
247 | /* ------------------------------------------------------------------------- * |
272 | /* ------------------------------------------------------------------------- * |
… | |
… | |
614 | /*----------------------------------------------------------------------*/ |
639 | /*----------------------------------------------------------------------*/ |
615 | /* |
640 | /* |
616 | * Tell the teletype handler what size the window is. |
641 | * Tell the teletype handler what size the window is. |
617 | * Called after a window size change. |
642 | * Called after a window size change. |
618 | */ |
643 | */ |
619 | /* EXTPROTO */ |
|
|
620 | void |
644 | void |
621 | rxvt_tt_winsize(int fd, unsigned short col, unsigned short row, int pid) |
645 | rxvt_term::tt_winch () |
622 | { |
646 | { |
623 | struct winsize ws; |
647 | struct winsize ws; |
624 | |
648 | |
625 | if (fd < 0) |
649 | if (cmd_fd < 0) |
626 | return; |
650 | return; |
|
|
651 | |
627 | ws.ws_col = col; |
652 | ws.ws_col = TermWin.ncol; |
628 | ws.ws_row = row; |
653 | ws.ws_row = TermWin.nrow; |
629 | ws.ws_xpixel = ws.ws_ypixel = 0; |
654 | ws.ws_xpixel = ws.ws_ypixel = 0; |
630 | #ifndef DEBUG_SIZE |
655 | #ifndef DEBUG_SIZE |
631 | (void)ioctl(fd, TIOCSWINSZ, &ws); |
656 | (void)ioctl (cmd_fd, TIOCSWINSZ, &ws); |
632 | #else |
657 | #else |
633 | if (ioctl(fd, TIOCSWINSZ, &ws) < 0) { |
658 | if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0) |
634 | D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); |
659 | D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); |
635 | } |
|
|
636 | # ifdef SIGWINCH |
660 | # ifdef SIGWINCH |
637 | else if (pid) /* force through to the command */ |
661 | else if (cmd_pid) /* force through to the command */ |
638 | kill(pid, SIGWINCH); |
662 | kill (cmd_pid, SIGWINCH); |
639 | # endif |
663 | # endif |
640 | #endif |
664 | #endif |
641 | } |
665 | } |
642 | |
666 | |
643 | /*----------------------------------------------------------------------*/ |
667 | /*----------------------------------------------------------------------*/ |
… | |
… | |
756 | XSetWindowBackground(R->Xdisplay, R->TermWin.vt, |
780 | XSetWindowBackground(R->Xdisplay, R->TermWin.vt, |
757 | R->PixColors[Color_bg]); |
781 | R->PixColors[Color_bg]); |
758 | |
782 | |
759 | /* handle Color_BD, scrollbar background, etc. */ |
783 | /* handle Color_BD, scrollbar background, etc. */ |
760 | |
784 | |
761 | rxvt_set_colorfgbg(aR); |
785 | rxvt_set_colorfgbg (aR); |
762 | rxvt_recolour_cursor(aR); |
786 | R->recolour_cursor (); |
763 | /* the only reasonable way to enforce a clean update */ |
787 | /* the only reasonable way to enforce a clean update */ |
764 | rxvt_scr_poweron(aR); |
788 | rxvt_scr_poweron (aR); |
765 | } |
789 | } |
766 | |
790 | |
767 | #else |
791 | #else |
768 | # define rxvt_set_window_color(aR_ idx,color) ((void)0) |
792 | # define rxvt_set_window_color(aR_ idx,color) ((void)0) |
769 | #endif /* XTERM_COLOR_CHANGE */ |
793 | #endif /* XTERM_COLOR_CHANGE */ |
770 | |
794 | |
771 | /* EXTPROTO */ |
|
|
772 | void |
795 | void |
773 | rxvt_recolour_cursor(pR) |
796 | rxvt_term::recolour_cursor () |
774 | { |
797 | { |
775 | rxvt_color xcol[2]; |
|
|
776 | |
|
|
777 | #if TODO |
798 | #if TODO |
|
|
799 | rxvt_color xcol[2]; |
|
|
800 | |
778 | xcol[0] = R->PixColors[Color_pointer]; |
801 | xcol[0] = PixColors[Color_pointer]; |
779 | xcol[1] = R->PixColors[Color_bg]; |
802 | xcol[1] = PixColors[Color_bg]; |
780 | XQueryColors(R->Xdisplay, XCMAP, xcol, 2); |
803 | XQueryColors (Xdisplay, XCMAP, xcol, 2); |
781 | XRecolorCursor(R->Xdisplay, R->TermWin_cursor, &(xcol[0]), &(xcol[1])); |
804 | XRecolorCursor (Xdisplay, TermWin_cursor, &(xcol[0]), &(xcol[1])); |
782 | #endif |
805 | #endif |
783 | } |
806 | } |
784 | |
807 | |
785 | /*----------------------------------------------------------------------*/ |
808 | /*----------------------------------------------------------------------*/ |
786 | /* |
809 | /* |
… | |
… | |
938 | uint16_t old_ncol = R->prev_ncol; |
961 | uint16_t old_ncol = R->prev_ncol; |
939 | |
962 | |
940 | /* scr_reset only works on the primary screen */ |
963 | /* scr_reset only works on the primary screen */ |
941 | if (R->old_height) /* this is not the first time through */ |
964 | if (R->old_height) /* this is not the first time through */ |
942 | curr_screen = rxvt_scr_change_screen(aR_ PRIMARY); |
965 | curr_screen = rxvt_scr_change_screen(aR_ PRIMARY); |
|
|
966 | |
943 | rxvt_scr_reset(aR); |
967 | R->scr_reset(); |
|
|
968 | |
944 | if (curr_screen >= 0) { /* this is not the first time through */ |
969 | if (curr_screen >= 0) { /* this is not the first time through */ |
945 | rxvt_scr_change_screen(aR_ curr_screen); |
970 | rxvt_scr_change_screen(aR_ curr_screen); |
946 | rxvt_selection_check(aR_(old_ncol != R->TermWin.ncol ? 4 : 0)); |
971 | rxvt_selection_check(aR_(old_ncol != R->TermWin.ncol ? 4 : 0)); |
947 | } |
972 | } |
948 | } |
973 | } |
… | |
… | |
1062 | return; |
1087 | return; |
1063 | } |
1088 | } |
1064 | |
1089 | |
1065 | /* INTPROTO */ |
1090 | /* INTPROTO */ |
1066 | void |
1091 | void |
1067 | rxvt_setPreeditArea(pR_ XRectangle * preedit_rect, XRectangle * status_rect, |
1092 | rxvt_setPreeditArea (pR_ XRectangle * preedit_rect, XRectangle * status_rect, |
1068 | XRectangle * needed_rect) |
1093 | XRectangle * needed_rect) |
1069 | { |
1094 | { |
1070 | int mbh, vtx = 0; |
1095 | int mbh, vtx = 0; |
1071 | |
1096 | |
1072 | if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) |
1097 | if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) |
1073 | vtx = scrollbar_TotalWidth(); |
1098 | vtx = scrollbar_TotalWidth(); |
… | |
… | |
1117 | rxvt_IMInstantiateCallback(Display * unused |
1142 | rxvt_IMInstantiateCallback(Display * unused |
1118 | __attribute__ ((unused)), XPointer client_data |
1143 | __attribute__ ((unused)), XPointer client_data |
1119 | __attribute__ ((unused)), XPointer call_data |
1144 | __attribute__ ((unused)), XPointer call_data |
1120 | __attribute__ ((unused))) |
1145 | __attribute__ ((unused))) |
1121 | { |
1146 | { |
1122 | dR; |
1147 | dR; |
1123 | int i, found, had_im; |
1148 | int i, found, had_im; |
1124 | const char *p; |
1149 | const char *p; |
1125 | char **s; |
1150 | char **s; |
1126 | char buf[IMBUFSIZ]; |
1151 | char buf[IMBUFSIZ]; |
1127 | |
1152 | |
1128 | D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); |
1153 | D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); |
1129 | if (R->Input_Context) |
1154 | if (R->Input_Context) |
1130 | return; |
1155 | return; |
1131 | |
1156 | |
1132 | found = had_im = 0; |
1157 | #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE) |
|
|
1158 | if (R->rs[Rs_imLocale]) |
|
|
1159 | setlocale (LC_CTYPE, R->rs[Rs_imLocale]); |
|
|
1160 | #endif |
|
|
1161 | |
1133 | p = R->rs[Rs_inputMethod]; |
1162 | p = R->rs[Rs_inputMethod]; |
1134 | if (p && *p) { |
1163 | if (p && *p) |
1135 | had_im = 1; |
1164 | { |
|
|
1165 | bool found = false; |
|
|
1166 | |
1136 | s = rxvt_splitcommastring(p); |
1167 | s = rxvt_splitcommastring (p); |
1137 | for (i = 0; s[i]; i++) { |
1168 | for (i = 0; s[i]; i++) |
|
|
1169 | { |
1138 | if (*s[i]) { |
1170 | if (*s[i]) |
|
|
1171 | { |
1139 | STRCPY(buf, "@im="); |
1172 | STRCPY (buf, "@im="); |
1140 | STRNCAT(buf, s[i], IMBUFSIZ - 5); |
1173 | STRNCAT (buf, s[i], IMBUFSIZ - 5); |
1141 | if ((p = XSetLocaleModifiers(buf)) != NULL && *p |
1174 | if ((p = XSetLocaleModifiers (buf)) && *p |
1142 | && (rxvt_IM_get_IC(aR) == True)) { |
1175 | && rxvt_IM_get_IC (aR)) |
|
|
1176 | { |
1143 | found = 1; |
1177 | found = true; |
1144 | break; |
1178 | break; |
1145 | } |
1179 | } |
1146 | } |
1180 | } |
1147 | } |
1181 | } |
1148 | for (i = 0; s[i]; i++) |
1182 | for (i = 0; s[i]; i++) |
1149 | free(s[i]); |
1183 | free(s[i]); |
1150 | free(s); |
1184 | free(s); |
1151 | } |
1185 | |
1152 | if (found) |
1186 | if (found) |
1153 | return; |
1187 | goto done; |
|
|
1188 | } |
1154 | |
1189 | |
1155 | /* try with XMODIFIERS env. var. */ |
1190 | /* try with XMODIFIERS env. var. */ |
1156 | if ((p = XSetLocaleModifiers("")) != NULL && *p) { |
1191 | if ((p = XSetLocaleModifiers ("")) && *p |
1157 | rxvt_IM_get_IC(aR); |
1192 | && rxvt_IM_get_IC (aR)) |
1158 | return; |
1193 | goto done; |
1159 | } |
|
|
1160 | |
1194 | |
1161 | /* try with no modifiers base IF the user didn't specify an IM */ |
1195 | /* try with no modifiers base IF the user didn't specify an IM */ |
1162 | if (!had_im && (p = XSetLocaleModifiers("@im=none")) != NULL && *p |
1196 | if ((p = XSetLocaleModifiers ("@im=none")) && *p |
1163 | && rxvt_IM_get_IC(aR) == True) |
1197 | && rxvt_IM_get_IC (aR) == True) |
1164 | return; |
1198 | goto done; |
|
|
1199 | |
|
|
1200 | done: |
|
|
1201 | #if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE) |
|
|
1202 | if (R->rs[Rs_imLocale]) |
|
|
1203 | setlocale (LC_CTYPE, R->locale); |
|
|
1204 | #endif |
1165 | } |
1205 | } |
1166 | |
1206 | |
1167 | /* |
1207 | /* |
1168 | * Try to open a XIM with the current modifiers, then see if we can |
1208 | * Try to open a XIM with the current modifiers, then see if we can |
1169 | * open a suitable preedit type |
1209 | * open a suitable preedit type |