ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/main.C
(Generate patch)

Comparing rxvt-unicode/src/main.C (file contents):
Revision 1.187 by root, Tue Jan 17 16:22:41 2006 UTC vs.
Revision 1.199 by root, Tue Jan 24 19:40:12 2006 UTC

48# include <termios.h> 48# include <termios.h>
49#endif 49#endif
50 50
51vector<rxvt_term *> rxvt_term::termlist; 51vector<rxvt_term *> rxvt_term::termlist;
52 52
53// used to tell global functions which terminal instance is "active"
54rxvt_t rxvt_current_term;
55
53static char curlocale[128], savelocale[128]; 56static char curlocale[128], savelocale[128];
54 57
55bool 58bool
56rxvt_set_locale (const char *locale) 59rxvt_set_locale (const char *locale) NOTHROW
57{ 60{
58 if (!locale || !strncmp (locale, curlocale, 128)) 61 if (!locale || !strncmp (locale, curlocale, 128))
59 return false; 62 return false;
60 63
61 strncpy (curlocale, locale, 128); 64 strncpy (curlocale, locale, 128);
62 setlocale (LC_CTYPE, curlocale); 65 setlocale (LC_CTYPE, curlocale);
63 return true; 66 return true;
64} 67}
65 68
66void 69void
67rxvt_push_locale (const char *locale) 70rxvt_push_locale (const char *locale) NOTHROW
68{ 71{
69 strcpy (savelocale, curlocale); 72 strcpy (savelocale, curlocale);
70 rxvt_set_locale (locale); 73 rxvt_set_locale (locale);
71} 74}
72 75
73void 76void
74rxvt_pop_locale () 77rxvt_pop_locale () NOTHROW
75{ 78{
76 rxvt_set_locale (savelocale); 79 rxvt_set_locale (savelocale);
77} 80}
78 81
79#if ENABLE_COMBINING 82#if ENABLE_COMBINING
167 pointer_ev (this, &rxvt_term::pointer_cb), 170 pointer_ev (this, &rxvt_term::pointer_cb),
168#endif 171#endif
169#ifdef USE_XIM 172#ifdef USE_XIM
170 im_ev (this, &rxvt_term::im_cb), 173 im_ev (this, &rxvt_term::im_cb),
171#endif 174#endif
175#ifndef NO_BELL
176 bell_ev (this, &rxvt_term::bell_cb),
177#endif
172 termwin_ev (this, &rxvt_term::x_cb), 178 termwin_ev (this, &rxvt_term::x_cb),
173 vt_ev (this, &rxvt_term::x_cb), 179 vt_ev (this, &rxvt_term::x_cb),
180 child_ev (this, &rxvt_term::child_cb),
174 check_ev (this, &rxvt_term::check_cb), 181 check_ev (this, &rxvt_term::check_cb),
175 flush_ev (this, &rxvt_term::flush_cb), 182 flush_ev (this, &rxvt_term::flush_cb),
176 destroy_ev (this, &rxvt_term::destroy_cb), 183 destroy_ev (this, &rxvt_term::destroy_cb),
177 pty_ev (this, &rxvt_term::pty_cb), 184 pty_ev (this, &rxvt_term::pty_cb),
178 incr_ev (this, &rxvt_term::incr_cb) 185 incr_ev (this, &rxvt_term::incr_cb)
181 188
182 termlist.push_back (this); 189 termlist.push_back (this);
183 190
184#ifdef KEYSYM_RESOURCE 191#ifdef KEYSYM_RESOURCE
185 keyboard = new keyboard_manager; 192 keyboard = new keyboard_manager;
186
187 if (!keyboard)
188 rxvt_fatal ("out of memory, aborting.\n");
189#endif 193#endif
190} 194}
191 195
192// clean up the most important stuff, do *not* call x or free mem etc. 196// clean up the most important stuff, do *not* call x or free mem etc.
193// for use before an emergency exit 197// for use before an emergency exit
199 delete pty; pty = 0; 203 delete pty; pty = 0;
200} 204}
201 205
202rxvt_term::~rxvt_term () 206rxvt_term::~rxvt_term ()
203{ 207{
204 HOOK_INVOKE ((this, HOOK_DESTROY, DT_END));
205
206 termlist.erase (find (termlist.begin (), termlist.end(), this)); 208 termlist.erase (find (termlist.begin (), termlist.end(), this));
207 209
208 emergency_cleanup (); 210 emergency_cleanup ();
209 211
210#if ENABLE_STYLES 212#if ENABLE_STYLES
285#ifdef KEYSYM_RESOURCE 287#ifdef KEYSYM_RESOURCE
286 delete keyboard; 288 delete keyboard;
287#endif 289#endif
288} 290}
289 291
292// child has exited, usually destroys
290void 293void
291rxvt_term::child_exit () 294rxvt_term::child_cb (child_watcher &w, int status)
292{ 295{
296 HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END));
297
293 cmd_pid = 0; 298 cmd_pid = 0;
294 299
295 if (!OPTION (Opt_hold)) 300 if (!OPTION (Opt_hold))
296 destroy (); 301 destroy ();
297} 302}
298 303
299void 304void
300rxvt_term::destroy () 305rxvt_term::destroy ()
301{ 306{
302 if (destroy_ev.active) 307 if (destroy_ev.active)
308 return;
309
310 if (HOOK_INVOKE ((this, HOOK_DESTROY, DT_END)))
303 return; 311 return;
304 312
305#if ENABLE_OVERLAY 313#if ENABLE_OVERLAY
306 scr_overlay_off (); 314 scr_overlay_off ();
307#endif 315#endif
442 _exit (EXIT_FAILURE); 450 _exit (EXIT_FAILURE);
443} 451}
444 452
445/*----------------------------------------------------------------------*/ 453/*----------------------------------------------------------------------*/
446bool 454bool
447rxvt_term::init (int argc, const char *const *argv) 455rxvt_term::init (int argc, const char *const *argv, stringvec *envv)
448{ 456{
457 this->envv = envv;
458
449 SET_R (this); 459 SET_R (this);
450 set_locale (""); 460 set_locale ("");
451 set_environ (envv); // few things in X do not call setlocale :( 461 set_environ (envv); // few things in X do not call setlocale :(
452 462
453 if (!init_vars ()) 463 if (!init_vars ())
464#ifdef HAVE_SCROLLBARS 474#ifdef HAVE_SCROLLBARS
465 if (OPTION (Opt_scrollBar)) 475 if (OPTION (Opt_scrollBar))
466 scrollBar.setIdle (); /* set existence for size calculations */ 476 scrollBar.setIdle (); /* set existence for size calculations */
467#endif 477#endif
468 478
469#if ENABLE_PERL 479 pty = ptytty::create ();
470 if (!rs[Rs_perl_ext_1])
471 rs[Rs_perl_ext_1] = "default";
472
473 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1])
474 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2])
475 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
476 {
477 rxvt_perl.init (this);
478 HOOK_INVOKE ((this, HOOK_INIT, DT_END));
479 }
480#endif
481
482 pty = rxvt_new_ptytty ();
483 480
484 create_windows (argc, argv); 481 create_windows (argc, argv);
485 482
486 dDisp; 483 dDisp;
487 484
504 check_our_parents (); 501 check_our_parents ();
505 rootwin_ev.start (display, display->root); 502 rootwin_ev.start (display, display->root);
506 } 503 }
507#endif 504#endif
508 505
506 set_colorfgbg ();
507
508 init_command (cmd_argv);
509
510 free (cmd_argv);
511
512 if (pty->pty >= 0)
513 pty_ev.start (pty->pty, EVENT_READ);
514
515 check_ev.start ();
516
517 HOOK_INVOKE ((this, HOOK_START, DT_END));
518
509 XMapWindow (disp, vt); 519 XMapWindow (disp, vt);
510 XMapWindow (disp, parent[0]); 520 XMapWindow (disp, parent[0]);
511 521
512 set_colorfgbg ();
513
514 init_command (cmd_argv);
515
516 free (cmd_argv);
517
518 if (pty->pty >= 0)
519 pty_ev.start (pty->pty, EVENT_READ);
520
521 check_ev.start ();
522
523 HOOK_INVOKE ((this, HOOK_START, DT_END));
524
525 return true; 522 return true;
526} 523}
527 524
528static struct sig_handlers 525static struct sig_handlers
529{ 526{
530 sig_watcher sw_chld, sw_term, sw_int; 527 sig_watcher sw_term, sw_int;
531 528
532 void sig_chld (sig_watcher &w)
533 {
534 // we are being called for every SIGCHLD, find the corresponding term
535 int pid;
536
537 while ((pid = waitpid (-1, NULL, WNOHANG)) > 0)
538 for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
539 if (pid == (*t)->cmd_pid)
540 {
541 (*t)->child_exit ();
542 break;
543 }
544 }
545
546 /* 529 /*
547 * Catch a fatal signal and tidy up before quitting 530 * Catch a fatal signal and tidy up before quitting
548 */ 531 */
549 void 532 void
550 sig_term (sig_watcher &w) 533 sig_term (sig_watcher &w)
551 { 534 {
552#ifdef DEBUG_CMD
553 rxvt_warn ("caught signal %d, exiting.\n", w.signum);
554#endif
555 rxvt_emergency_cleanup (); 535 rxvt_emergency_cleanup ();
556 signal (w.signum, SIG_DFL); 536 signal (w.signum, SIG_DFL);
557 kill (getpid (), w.signum); 537 kill (getpid (), w.signum);
558 } 538 }
559 539
560 sig_handlers () 540 sig_handlers ()
561 : sw_chld (this, &sig_handlers::sig_chld),
562 sw_term (this, &sig_handlers::sig_term), 541 : sw_term (this, &sig_handlers::sig_term),
563 sw_int (this, &sig_handlers::sig_term) 542 sw_int (this, &sig_handlers::sig_term)
564 { 543 {
565 } 544 }
566} sig_handlers; 545} sig_handlers;
567 546
568char **rxvt_environ; // startup environment 547char **rxvt_environ; // startup environment
569 548
570void 549void
571rxvt_init () 550rxvt_init ()
572{ 551{
573 uid_t uid = getuid (); 552 ptytty::init ();
574 gid_t gid = getgid ();
575
576 // before doing anything else, check for setuid/setgid operation,
577 // start the helper process and drop privileges
578 if (uid != geteuid ()
579 || 1 //D
580 || gid != getegid ())
581 {
582#if PTYTTY_HELPER
583 rxvt_ptytty_server ();
584#else
585 rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
586#endif
587
588 // drop privileges
589#if HAVE_SETRESUID
590 setresgid (gid, gid, gid);
591 setresuid (uid, uid, uid);
592#elif HAVE_SETREUID
593 setregid (gid, gid);
594 setreuid (uid, uid);
595#elif HAVE_SETUID
596 setgid (gid);
597 setuid (uid);
598#endif
599
600 if (uid != geteuid ()
601 || gid != getegid ())
602 rxvt_fatal ("unable to drop privileges, aborting.\n");
603 }
604 553
605 rxvt_environ = environ; 554 rxvt_environ = environ;
606 555
607 signal (SIGHUP, SIG_IGN); 556 signal (SIGHUP, SIG_IGN);
608 signal (SIGPIPE, SIG_IGN); 557 signal (SIGPIPE, SIG_IGN);
609 558
610 sig_handlers.sw_chld.start (SIGCHLD);
611 sig_handlers.sw_term.start (SIGTERM); 559 sig_handlers.sw_term.start (SIGTERM);
612 sig_handlers.sw_int.start (SIGINT); 560 sig_handlers.sw_int.start (SIGINT);
613 561
614 /* need to trap SIGURG for SVR4 (Unixware) rlogin */ 562 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
615 /* signal (SIGURG, SIG_DFL); */ 563 /* signal (SIGURG, SIG_DFL); */
644 rxvt_fatal ("memory allocation failure. aborting.\n"); 592 rxvt_fatal ("memory allocation failure. aborting.\n");
645 593
646 return p; 594 return p;
647} 595}
648 596
649void * 597void *
650rxvt_realloc (void *ptr, size_t size) 598rxvt_realloc (void *ptr, size_t size)
651{ 599{
652 void *p = realloc (ptr, size); 600 void *p = realloc (ptr, size);
653 601
654 if (!p) 602 if (!p)
668 short recalc_x, recalc_y; 616 short recalc_x, recalc_y;
669 int x, y, sb_w, flags; 617 int x, y, sb_w, flags;
670 unsigned int w, h; 618 unsigned int w, h;
671 unsigned int max_width, max_height; 619 unsigned int max_width, max_height;
672 dDisp; 620 dDisp;
673
674 D_SIZE ((stderr, "< Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d",
675 ncol, nrow, szHint.width, szHint.height));
676 621
677 szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity; 622 szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity;
678 szHint.win_gravity = NorthWestGravity; 623 szHint.win_gravity = NorthWestGravity;
679 /* szHint.min_aspect.x = szHint.min_aspect.y = 1; */ 624 /* szHint.min_aspect.x = szHint.min_aspect.y = 1; */
680 625
783 if (recalc_y) 728 if (recalc_y)
784 szHint.y += DisplayHeight (disp, display->screen) - szHint.height - 2 * ext_bwidth; 729 szHint.y += DisplayHeight (disp, display->screen) - szHint.height - 2 * ext_bwidth;
785 730
786 ncol = width / fwidth; 731 ncol = width / fwidth;
787 nrow = height / fheight; 732 nrow = height / fheight;
788 D_SIZE ((stderr, "> Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d",
789 ncol, nrow, szHint.width,
790 szHint.height));
791 return;
792} 733}
793 734
794/*----------------------------------------------------------------------*/ 735/*----------------------------------------------------------------------*/
795/* 736/*
796 * Tell the teletype handler what size the window is. 737 * Tell the teletype handler what size the window is.
1080 * -------------------------------------------------------------------- */ 1021 * -------------------------------------------------------------------- */
1081void 1022void
1082rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, int ignoreparent) 1023rxvt_term::resize_all_windows (unsigned int newwidth, unsigned int newheight, int ignoreparent)
1083{ 1024{
1084 int fix_screen; 1025 int fix_screen;
1085 int old_width = szHint.width, old_height = szHint.height; 1026 int old_width = szHint.width;
1027 int old_height = szHint.height;
1086 dDisp; 1028 dDisp;
1087 1029
1088 window_calc (newwidth, newheight); 1030 window_calc (newwidth, newheight);
1089 XSetWMNormalHints (disp, parent[0], &szHint); 1031 XSetWMNormalHints (disp, parent[0], &szHint);
1090 1032
1112 { 1054 {
1113 x -= x1; 1055 x -= x1;
1114 y -= y1; 1056 y -= y1;
1115 } 1057 }
1116 1058
1117 x1 = (DisplayWidth (disp, display->screen) - old_width) / 2; 1059 x1 = (DisplayWidth (disp, display->screen) - old_width ) / 2;
1118 y1 = (DisplayHeight (disp, display->screen) - old_height) / 2; 1060 y1 = (DisplayHeight (disp, display->screen) - old_height) / 2;
1119 dx = old_width - szHint.width; 1061 dx = old_width - szHint.width;
1120 dy = old_height - szHint.height; 1062 dy = old_height - szHint.height;
1121 1063
1122 /* Check position of the center of the window */ 1064 /* Check position of the center of the window */
1123 if (x < x1) /* left half */ 1065 if (x < x1) /* left half */
1124 dx = 0; 1066 dx = 0;
1157 resize_pixmap (); 1099 resize_pixmap ();
1158#endif 1100#endif
1159 } 1101 }
1160 1102
1161 if (fix_screen || old_height == 0) 1103 if (fix_screen || old_height == 0)
1162 {
1163 int curr_screen = -1;
1164 int old_ncol = prev_ncol;
1165
1166 /* scr_reset only works on the primary screen */
1167 if (old_height) /* this is not the first time through */
1168 {
1169 unsigned int ocol = ncol;
1170 ncol = prev_ncol; // save b/c scr_blank_screen_mem uses this
1171 curr_screen = scr_change_screen (PRIMARY);
1172 ncol = ocol;
1173 }
1174
1175 scr_reset (); 1104 scr_reset ();
1176
1177 if (curr_screen >= 0) /* this is not the first time through */
1178 {
1179 scr_change_screen (curr_screen);
1180 selection_check (old_ncol != ncol ? 4 : 0);
1181 }
1182 }
1183
1184 old_width = szHint.width;
1185 old_height = szHint.height;
1186 1105
1187#ifdef XPM_BACKGROUND 1106#ifdef XPM_BACKGROUND
1188 if (pixmap) 1107 if (pixmap)
1189 scr_touch (false); 1108 scr_touch (false);
1190#endif 1109#endif
1344 set_environ (envv); 1263 set_environ (envv);
1345 1264
1346 if (! ((p = XSetLocaleModifiers (modifiers)) && *p)) 1265 if (! ((p = XSetLocaleModifiers (modifiers)) && *p))
1347 return false; 1266 return false;
1348 1267
1349 D_MAIN ((stderr, "rxvt_IM_get_IC ()"));
1350 input_method = display->get_xim (locale, modifiers); 1268 input_method = display->get_xim (locale, modifiers);
1351 if (input_method == NULL) 1269 if (input_method == NULL)
1352 return false; 1270 return false;
1353 1271
1354 xim = input_method->xim; 1272 xim = input_method->xim;
1500 rxvt_warn ("failed to create input context, continuing without XIM.\n"); 1418 rxvt_warn ("failed to create input context, continuing without XIM.\n");
1501 im_destroy (); 1419 im_destroy ();
1502 return false; 1420 return false;
1503 } 1421 }
1504 1422
1423 if (!XGetICValues (Input_Context, XNFilterEvents, &vt_emask_xim, NULL))
1424 vt_select_input ();
1425
1505 if (input_style & XIMPreeditArea) 1426 if (input_style & XIMPreeditArea)
1506 IMSetStatusPosition (); 1427 IMSetStatusPosition ();
1507 1428
1508 D_MAIN ((stderr, "rxvt_IM_get_IC () - successful connection"));
1509 return true; 1429 return true;
1510} 1430}
1511 1431
1512void 1432void
1513rxvt_term::im_cb () 1433rxvt_term::im_cb ()
1519 1439
1520 make_current (); 1440 make_current ();
1521 1441
1522 im_destroy (); 1442 im_destroy ();
1523 1443
1524 D_MAIN ((stderr, "rxvt_IMInstantiateCallback ()"));
1525 if (Input_Context) 1444 if (Input_Context)
1526 return; 1445 return;
1527 1446
1528#if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE) 1447#if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1529 if (rs[Rs_imLocale]) 1448 if (rs[Rs_imLocale])
1602 XFree (preedit_attr); 1521 XFree (preedit_attr);
1603 XFree (status_attr); 1522 XFree (status_attr);
1604} 1523}
1605#endif /* USE_XIM */ 1524#endif /* USE_XIM */
1606 1525
1607/*----------------------------------------------------------------------*/
1608rxvt_t rxvt_current_term;
1609
1610/*----------------------- end-of-file (C source) -----------------------*/ 1526/*----------------------- end-of-file (C source) -----------------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines