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.12 by pcg, Tue Dec 16 23:04:13 2003 UTC vs.
Revision 1.77 by root, Tue Aug 10 20:39:19 2004 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: main.c 2 * File: main.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: main.C,v 1.12 2003/12/16 23:04:13 pcg Exp $
5 * 4 *
6 * All portions of code are copyright by their respective author/s. 5 * 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> 6 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
8 * - original version 7 * - original version
9 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> 8 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
12 * Copyright (c) 1997 mj olesen <olesen@me.QueensU.CA> 11 * Copyright (c) 1997 mj olesen <olesen@me.QueensU.CA>
13 * - extensive modifications 12 * - extensive modifications
14 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> 13 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
15 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com> 14 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
16 * - extensive modifications 15 * - extensive modifications
16 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
17 * 17 *
18 * This program is free software; you can redistribute it and/or modify 18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by 19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or 20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version. 21 * (at your option) any later version.
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
49vector<rxvt_term *> rxvt_term::termlist;
50
51static char curlocale[128];
52
53bool
54rxvt_set_locale (const char *locale)
55{
56 if (!locale || !STRNCMP (locale, curlocale, 128))
57 return false;
58
59 STRNCPY (curlocale, locale, 128);
60 setlocale (LC_CTYPE, curlocale);
61 return true;
62}
63
64#if ENABLE_COMBINING
65class rxvt_composite_vec rxvt_composite;
66
67text_t rxvt_composite_vec::compose (unicode_t c1, unicode_t c2)
68{
69 compose_char *cc;
70
71 // break compose chains, as stupid readline really likes to duplicate
72 // composing characters for some reason near the end of a line.
73 cc = (*this)[c1];
74 while (cc)
75 {
76 if (cc->c2 == c2) return c1;
77 cc = (*this)[cc->c1];
78 }
79
80 // check to see wether this combination already exists otherwise
81 for (cc = v.end (); cc-- > v.begin (); )
82 {
83 if (cc->c1 == c1 && cc->c2 == c2)
84 return COMPOSE_LO + (cc - v.begin ());
85 }
86
87 // allocate a new combination
88 if (v.size () == COMPOSE_HI - COMPOSE_LO + 1)
89 {
90 static int seen;
91
92 if (!seen++)
93 fprintf (stderr, "too many unrepresentable composite characters, try --enable-unicode3\n");
94
95 return REPLACEMENT_CHAR;
96 }
97
98 v.push_back (compose_char (c1, c2));
99
100 return v.size () - 1 + COMPOSE_LO;
101}
102
103int rxvt_composite_vec::expand (unicode_t c, wchar_t *r)
104{
105 compose_char *cc = (*this)[c];
106
107 if (!cc)
108 {
109 if (r) *r = c;
110 return 1;
111 }
112
113 int len = expand (cc->c1, r);
114
115 if (r) r += len;
116
117 if (cc->c2 != NOCHAR)
118 {
119 len++;
120 if (r) *r++ = cc->c2;
121 }
122
123 return len;
124
125}
126#endif
127
47void * 128void *
48rxvt_term::operator new (size_t s) 129rxvt_term::operator new (size_t s)
49{ 130{
50 void *p = malloc (s); 131 void *p = malloc (s);
51 132
58{ 139{
59 free (p); 140 free (p);
60} 141}
61 142
62rxvt_term::rxvt_term () 143rxvt_term::rxvt_term ()
144 :
145 rootwin_ev (this, &rxvt_term::rootwin_cb),
146 termwin_ev (this, &rxvt_term::x_cb),
63: pty_ev (this, &rxvt_term::pty_cb), 147 vt_ev (this, &rxvt_term::x_cb),
148#ifdef HAVE_SCROLLBARS
149 scrollbar_ev (this, &rxvt_term::x_cb),
150#endif
151#ifdef MENUBAR
152 menubar_ev (this, &rxvt_term::x_cb),
153#endif
64#ifdef CURSOR_BLINK 154#ifdef CURSOR_BLINK
155 cursor_blink_ev (this, &rxvt_term::cursor_blink_cb),
156#endif
157#ifdef TEXT_BLINK
65 blink_ev (this, &rxvt_term::blink_cb), 158 text_blink_ev (this, &rxvt_term::text_blink_cb),
159#endif
160#ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
161 cont_scroll_ev (this, &rxvt_term::cont_scroll_cb),
162#endif
163#ifdef SELECTION_SCROLLING
164 sel_scroll_ev (this, &rxvt_term::sel_scroll_cb),
165#endif
166#if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
167 slip_wheel_ev (this, &rxvt_term::slip_wheel_cb),
66#endif 168#endif
67#ifdef POINTER_BLANK 169#ifdef POINTER_BLANK
68 pointer_ev (this, &rxvt_term::pointer_cb), 170 pointer_ev (this, &rxvt_term::pointer_cb),
69#endif 171#endif
172#ifdef USE_XIM
70 x_ev (this, &rxvt_term::x_cb), 173 im_ev (this, &rxvt_term::im_cb),
174#endif
175 check_ev (this, &rxvt_term::check_cb),
176 flush_ev (this, &rxvt_term::flush_cb),
71 destroy_ev (this, &rxvt_term::destroy_cb), 177 destroy_ev (this, &rxvt_term::destroy_cb),
72 check_ev (this, &rxvt_term::check_cb), 178 pty_ev (this, &rxvt_term::pty_cb),
73 incr_ev (this, &rxvt_term::incr_cb) 179 incr_ev (this, &rxvt_term::incr_cb)
74{ 180{
75 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base; 181 cmdbuf_ptr = cmdbuf_endp = cmdbuf_base;
182
183 termlist.push_back (this);
76} 184}
77 185
78rxvt_term::~rxvt_term () 186rxvt_term::~rxvt_term ()
79{ 187{
80 rxvt_scr_release (this); 188 termlist.erase (find (termlist.begin (), termlist.end(), this));
81
82#ifndef NO_SETOWNER_TTYDEV
83 rxvt_privileged_ttydev (this, RESTORE);
84#endif
85#ifdef UTMP_SUPPORT
86 rxvt_privileged_utmp (this, RESTORE);
87#endif
88#ifdef USE_XIM
89 if (Input_Context != NULL)
90 {
91 XDestroyIC (Input_Context);
92 Input_Context = NULL;
93 }
94#endif
95 189
96 if (cmd_fd >= 0) 190 if (cmd_fd >= 0)
97 close (cmd_fd); 191 close (cmd_fd);
98 192
99 if (Xfd >= 0) 193#ifndef NO_SETOWNER_TTYDEV
100 XCloseDisplay (Xdisplay); 194 privileged_ttydev (RESTORE);
195#endif
196#ifdef UTMP_SUPPORT
197 privileged_utmp (RESTORE);
198#endif
101 199
200 delete TermWin.fontset;
201
202 if (display)
203 {
204 selection_clear ();
205
206#ifdef USE_XIM
207 im_destroy ();
208#endif
209#ifdef MENUBAR
210 if (menubarGC) XFreeGC (display->display, menubarGC);
211#endif
212#ifdef XTERM_SCROLLBAR
213 if (xscrollbarGC) XFreeGC (display->display, xscrollbarGC);
214 if (ShadowGC) XFreeGC (display->display, ShadowGC);
215#endif
216#ifdef PLAIN_SCROLLBAR
217 if (pscrollbarGC) XFreeGC (display->display, pscrollbarGC);
218#endif
219#ifdef NEXT_SCROLLBAR
220 if (blackGC) XFreeGC (display->display, blackGC);
221 if (whiteGC) XFreeGC (display->display, whiteGC);
222 if (grayGC) XFreeGC (display->display, grayGC);
223 if (darkGC) XFreeGC (display->display, darkGC);
224 if (stippleGC) XFreeGC (display->display, stippleGC);
225 if (dimple) XFreePixmap (display->display, dimple);
226 if (upArrow) XFreePixmap (display->display, upArrow);
227 if (downArrow) XFreePixmap (display->display, downArrow);
228 if (upArrowHi) XFreePixmap (display->display, upArrowHi);
229 if (downArrowHi) XFreePixmap (display->display, downArrowHi);
230#endif
231#if defined(MENUBAR) || defined(RXVT_SCROLLBAR)
232 if (topShadowGC) XFreeGC (display->display, topShadowGC);
233 if (botShadowGC) XFreeGC (display->display, botShadowGC);
234 if (scrollbarGC) XFreeGC (display->display, scrollbarGC);
235#endif
236 if (TermWin.gc) XFreeGC (display->display, TermWin.gc);
237
238#if defined(MENUBAR) && (MENUBAR_MAX > 1)
239 delete menuBar.drawable;
240 //if (menuBar.win)
241 // XDestroyWindow (display->display, menuBar.win);
242#endif
243 delete TermWin.drawable;
244 // destroy all windows
245 if (TermWin.parent[0])
246 XDestroyWindow (display->display, TermWin.parent[0]);
247 }
248
249 // TODO: free pixcolours, colours should become part of rxvt_display
250
102 delete PixColors; 251 delete PixColorsFocused;
252#ifdef OFF_FOCUS_FADING
253 delete PixColorsUnFocused;
254#endif
255
256 displays.put (display);
257
258 scr_release ();
259
260 /* clear all resources */
261 for (int i = 0; i < allocated.size (); i++)
262 free (allocated[i]);
263
264 free (selection.text);
265 // TODO: manage env vars in child only(!)
266 free (env_windowid);
267 free (env_display);
268 free (env_term);
269 free (env_colorfgbg);
270 free (locale);
271#if 0
272 free (codeset);
273#endif
274
275 delete envv;
276 delete argv;
103} 277}
104 278
105void 279void
106rxvt_term::destroy () 280rxvt_term::destroy ()
107{ 281{
282 if (destroy_ev.active)
283 return;
284
285 if (display)
286 {
287#ifdef USE_XIM
288 im_ev.stop (display);
289#endif
290#ifdef HAVE_SCROLLBARS
291 scrollbar_ev.stop (display);
292#endif
293#ifdef MENUBAR
294 menubar_ev.stop (display);
295#endif
296 rootwin_ev.stop (display);
297 termwin_ev.stop (display);
298 vt_ev.stop (display);
299 }
300
108 check_ev.stop (); 301 check_ev.stop ();
109 pty_ev.stop (); 302 pty_ev.stop ();
110 x_ev.stop ();
111#ifdef CURSOR_BLINK 303#ifdef CURSOR_BLINK
304 cursor_blink_ev.stop ();
305#endif
306#ifdef TEXT_BLINK
112 blink_ev.stop (); 307 text_blink_ev.stop ();
308#endif
309#ifndef NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING
310 cont_scroll_ev.stop ();
311#endif
312#ifdef SELECTION_SCROLLING
313 sel_scroll_ev.stop ();
113#endif 314#endif
114#ifdef POINTER_BLANK 315#ifdef POINTER_BLANK
115 pointer_ev.stop (); 316 pointer_ev.stop ();
116#endif 317#endif
117 318
125 326
126 delete this; 327 delete this;
127} 328}
128 329
129/*----------------------------------------------------------------------*/ 330/*----------------------------------------------------------------------*/
130/* rxvt_init() */ 331/* rxvt_init () */
131/* LIBPROTO */ 332bool
132rxvt_t
133rxvt_init (int argc, const char *const *argv) 333rxvt_term::init (int argc, const char *const *argv)
134{ 334{
135 SET_R (new rxvt_term); 335 SET_R (this);
136 dR;
137 336
138 if (!R->init_vars () || !R->init (argc, argv)) 337 if (!init_vars ())
139 { 338 return false;
140 delete R; 339
141 return NULL; 340 /*
341 * Save and then give up any super-user privileges
342 * If we need privileges in any area then we must specifically request it.
343 * We should only need to be root in these cases:
344 * 1. write utmp entries on some systems
345 * 2. chown tty on some systems
346 */
347 privileges (SAVE);
348 privileges (IGNORE);
349
350 init_secondary ();
351
352 const char **cmd_argv = init_resources (argc, argv);
353
354 set_locale ("");
355
356#if MENUBAR_MAX
357 menubar_read (rs[Rs_menu]);
358#endif
359#ifdef HAVE_SCROLLBARS
360 if (Options & Opt_scrollBar)
361 scrollBar.setIdle (); /* set existence for size calculations */
362#endif
363
364 create_windows (argc, argv);
365
366 init_xlocale ();
367
368 scr_reset (); /* initialize screen */
369
370#if 0
371 XSynchronize (display->display, True);
372#endif
373
374#ifdef HAVE_SCROLLBARS
375 if (Options & Opt_scrollBar)
376 resize_scrollbar (); /* create and map scrollbar */
377#endif
378#if (MENUBAR_MAX)
379 if (menubar_visible ())
380 XMapWindow (display->display, menuBar.win);
381#endif
382#ifdef TRANSPARENT
383 if (Options & Opt_transparent)
142 } 384 {
385 XSelectInput (display->display, display->root, PropertyChangeMask);
386 check_our_parents ();
387 }
388#endif
143 389
390 rootwin_ev.start (display, display->root);
391
392 XMapWindow (display->display, TermWin.vt);
393 XMapWindow (display->display, TermWin.parent[0]);
394
395 init_command (cmd_argv);
396
397 pty_ev.start (cmd_fd, EVENT_READ);
398
399 check_ev.start ();
400
144 return R; 401 return true;
145} 402}
146 403
147/* EXTPROTO */ 404static int (*old_xerror_handler) (Display *dpy, XErrorEvent *event);
405
148void 406void
149rxvt_init_signals () 407rxvt_init_signals ()
150{ 408{
151/* install exit handler for cleanup */ 409 /* install exit handler for cleanup */
152#if 0 410#if 0
153#ifdef HAVE_ATEXIT 411#ifdef HAVE_ATEXIT
154 atexit(rxvt_clean_exit); 412 atexit (rxvt_clean_exit);
155#else 413#else
156#endif 414#endif
157#endif 415#endif
158 416
159 struct sigaction sa; 417 struct sigaction sa;
165 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0); 423 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGINT , &sa, 0);
166 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0); 424 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGQUIT, &sa, 0);
167 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0); 425 sa.sa_handler = rxvt_Exit_signal; sigaction (SIGTERM, &sa, 0);
168 sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0); 426 sa.sa_handler = rxvt_Child_signal; sigaction (SIGCHLD, &sa, 0);
169 427
170/* need to trap SIGURG for SVR4 (Unixware) rlogin */ 428 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
171/* signal (SIGURG, SIG_DFL); */ 429 /* signal (SIGURG, SIG_DFL); */
172 430
173 XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler); 431 old_xerror_handler = XSetErrorHandler ((XErrorHandler) rxvt_xerror_handler);
174 //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler); 432 //XSetIOErrorHandler ((XErrorHandler) rxvt_xioerror_handler);
175}
176
177bool
178rxvt_term::init (int argc, const char *const *argv)
179{
180 dR;//TODO (scrollbar, setidle)
181
182 /*
183 * Save and then give up any super-user privileges
184 * If we need privileges in any area then we must specifically request it.
185 * We should only need to be root in these cases:
186 * 1. write utmp entries on some systems
187 * 2. chown tty on some systems
188 */
189 rxvt_privileges (this, SAVE);
190 rxvt_privileges (this, IGNORE);
191
192 rxvt_init_secondary (this);
193
194 const char **cmd_argv = rxvt_init_resources (this, argc, argv);
195
196#if (MENUBAR_MAX)
197 rxvt_menubar_read (this, rs[Rs_menu]);
198#endif
199#ifdef HAVE_SCROLLBARS
200 if (Options & Opt_scrollBar)
201 scrollbar_setIdle (); /* set existence for size calculations */
202#endif
203
204 rxvt_Create_Windows (this, argc, argv);
205
206 rxvt_init_xlocale (this);
207
208 rxvt_scr_reset (this); /* initialize screen */
209#ifdef RXVT_GRAPHICS
210 rxvt_Gr_reset (this); /* reset graphics */
211#endif
212
213#if 0
214#ifdef DEBUG_X
215 XSynchronize(Xdisplay, True);
216#endif
217#endif
218
219#ifdef HAVE_SCROLLBARS
220 if (Options & Opt_scrollBar)
221 rxvt_Resize_scrollBar (this); /* create and map scrollbar */
222#endif
223#if (MENUBAR_MAX)
224 if (menubar_visible(r))
225 XMapWindow (Xdisplay, menuBar.win);
226#endif
227#ifdef TRANSPARENT
228 if (Options & Opt_transparent)
229 {
230 XSelectInput (Xdisplay, Xroot, PropertyChangeMask);
231 rxvt_check_our_parents (this);
232 }
233#endif
234 XMapWindow (Xdisplay, TermWin.vt);
235 XMapWindow (Xdisplay, TermWin.parent[0]);
236
237 rxvt_init_env (this);
238 rxvt_init_command (this, cmd_argv);
239
240 x_ev.start (Xfd, EVENT_READ);
241 pty_ev.start (cmd_fd, EVENT_READ);
242
243 check_ev.start ();
244
245 process_x_events ();
246
247 return true;
248} 433}
249 434
250/* ------------------------------------------------------------------------- * 435/* ------------------------------------------------------------------------- *
251 * SIGNAL HANDLING & EXIT HANDLER * 436 * SIGNAL HANDLING & EXIT HANDLER *
252 * ------------------------------------------------------------------------- */ 437 * ------------------------------------------------------------------------- */
253/* 438/*
254 * Catch a SIGCHLD signal and exit if the direct child has died 439 * Catch a SIGCHLD signal and exit if the direct child has died
255 */ 440 */
441
442void rxvt_term::child_exited (int pid)
443{
444 for (rxvt_term **t = termlist.begin (); t < termlist.end (); t++)
445 if (pid == (*t)->cmd_pid)
446 {
447 (*t)->destroy ();
448 break;
449 }
450}
451
256/* ARGSUSED */ 452/* ARGSUSED */
257/* EXTPROTO */ 453/* EXTPROTO */
258RETSIGTYPE 454RETSIGTYPE
259rxvt_Child_signal(int sig __attribute__ ((unused))) 455rxvt_Child_signal (int sig __attribute__ ((unused)))
260{ 456{
261 int pid, save_errno = errno; 457 int pid, save_errno = errno;
458
262 while ((pid = waitpid (-1, NULL, WNOHANG)) == -1 && errno == EINTR) 459 while ((pid = waitpid (-1, NULL, WNOHANG)) > 0)
263 ; 460 rxvt_term::child_exited (pid);
461
264 errno = save_errno; 462 errno = save_errno;
265
266#if 0
267 if (pid == R->cmd_pid)
268 exit (EXIT_SUCCESS);
269#endif
270} 463}
271 464
272/* 465/*
273 * Catch a fatal signal and tidy up before quitting 466 * Catch a fatal signal and tidy up before quitting
274 */ 467 */
275/* EXTPROTO */ 468/* EXTPROTO */
276RETSIGTYPE 469RETSIGTYPE
277rxvt_Exit_signal(int sig) 470rxvt_Exit_signal (int sig)
278{ 471{
279 signal(sig, SIG_DFL); 472 signal (sig, SIG_DFL);
280#ifdef DEBUG_CMD 473#ifdef DEBUG_CMD
281 rxvt_print_error("signal %d", sig); 474 rxvt_warn ("caught signal %d, exiting.\n", sig);
282#endif 475#endif
283 rxvt_clean_exit(); 476 rxvt_clean_exit ();
284 kill(getpid(), sig); 477 kill (getpid (), sig);
285} 478}
286 479
287/* ARGSUSED */
288/* INTPROTO */ 480/* INTPROTO */
289int 481int
290rxvt_xerror_handler(const Display * display 482rxvt_xerror_handler (Display *display, XErrorEvent *event)
291 __attribute__ ((unused)), const XErrorEvent * event)
292{ 483{
293 dR;
294
295 if (R->allowedxerror == -1) 484 if (GET_R->allowedxerror == -1)
296 R->allowedxerror = event->error_code; 485 GET_R->allowedxerror = event->error_code;
297 else 486 else
298 { 487 {
299 rxvt_print_error("XError: Request: %d . %d, Error: %d", 488 old_xerror_handler (display, event);
300 event->request_code, event->minor_code,
301 event->error_code);
302
303 R->destroy (); 489 GET_R->destroy ();
304 } 490 }
305 491
306 return 0; 492 return 0;
307} 493}
308 494
313 */ 499 */
314/* EXTPROTO */ 500/* EXTPROTO */
315void 501void
316rxvt_clean_exit () 502rxvt_clean_exit ()
317{ 503{
318 dR; 504 // TODO: rxvtd should clean up all ressources
319 505 if (GET_R)
320 R->destroy (); 506 GET_R->destroy ();
321} 507}
322 508
323/* ------------------------------------------------------------------------- * 509/* ------------------------------------------------------------------------- *
324 * MEMORY ALLOCATION WRAPPERS * 510 * MEMORY ALLOCATION WRAPPERS *
325 * ------------------------------------------------------------------------- */ 511 * ------------------------------------------------------------------------- */
326/* EXTPROTO */ 512/* EXTPROTO */
327void * 513void *
328rxvt_malloc(size_t size) 514rxvt_malloc (size_t size)
329{ 515{
330 void *p; 516 void *p;
331 517
332 p = malloc(size); 518 p = malloc (size);
333 if (p) 519 if (p)
334 return p; 520 return p;
335 521
336 fprintf(stderr, APL_NAME ": memory allocation failure. Aborting"); 522 fprintf (stderr, RESNAME ": memory allocation failure. Aborting");
337 rxvt_clean_exit(); 523 rxvt_clean_exit ();
338 exit(EXIT_FAILURE); 524 exit (EXIT_FAILURE);
339/* NOTREACHED */ 525 /* NOTREACHED */
340} 526}
341 527
342/* EXTPROTO */ 528/* EXTPROTO */
343void * 529void *
344rxvt_calloc(size_t number, size_t size) 530rxvt_calloc (size_t number, size_t size)
345{ 531{
346 void *p; 532 void *p;
347 533
348 p = calloc(number, size); 534 p = calloc (number, size);
349 if (p) 535 if (p)
350 return p; 536 return p;
351 537
352 fprintf(stderr, APL_NAME ": memory allocation failure. Aborting"); 538 fprintf (stderr, RESNAME ": memory allocation failure. Aborting");
353 rxvt_clean_exit(); 539 rxvt_clean_exit ();
354 exit(EXIT_FAILURE); 540 exit (EXIT_FAILURE);
355/* NOTREACHED */ 541 /* NOTREACHED */
356} 542}
357 543
358/* EXTPROTO */ 544/* EXTPROTO */
359void * 545void *
360rxvt_realloc(void *ptr, size_t size) 546rxvt_realloc (void *ptr, size_t size)
361{ 547{
362 void *p; 548 void *p;
363 549
364 if (ptr) 550 if (ptr)
365 p = realloc(ptr, size); 551 p = realloc (ptr, size);
366 else 552 else
367 p = malloc(size); 553 p = malloc (size);
368 if (p) 554 if (p)
369 return p; 555 return p;
370 556
371 fprintf(stderr, APL_NAME ": memory allocation failure. Aborting"); 557 fprintf (stderr, RESNAME ": memory allocation failure. Aborting");
372 rxvt_clean_exit(); 558 rxvt_clean_exit ();
373 exit(EXIT_FAILURE); 559 exit (EXIT_FAILURE);
374/* NOTREACHED */ 560 /* NOTREACHED */
375} 561}
376 562
377/* ------------------------------------------------------------------------- * 563/* ------------------------------------------------------------------------- *
378 * PRIVILEGED OPERATIONS * 564 * PRIVILEGED OPERATIONS *
379 * ------------------------------------------------------------------------- */ 565 * ------------------------------------------------------------------------- */
380/* take care of suid/sgid super-user (root) privileges */ 566/* take care of suid/sgid super-user (root) privileges */
381/* INTPROTO */
382void 567void
383rxvt_privileges(pR_ int mode) 568rxvt_term::privileges (int mode)
384{ 569{
385#if ! defined(__CYGWIN32__) 570#if ! defined(__CYGWIN32__)
386# if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) 571# if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
387/* setreuid() is the poor man's setuid(), seteuid() */ 572 /* setreuid () is the poor man's setuid (), seteuid () */
388# define seteuid(a) setreuid(-1, (a)) 573# define seteuid(a) setreuid(-1, (a))
389# define setegid(a) setregid(-1, (a)) 574# define setegid(a) setregid(-1, (a))
390# define HAVE_SETEUID 575# define HAVE_SETEUID
391# endif 576# endif
392# ifdef HAVE_SETEUID 577# ifdef HAVE_SETEUID
393 switch (mode) { 578 switch (mode)
579 {
394 case IGNORE: 580 case IGNORE:
395 /* 581 /*
396 * change effective uid/gid - not real uid/gid - so we can switch 582 * change effective uid/gid - not real uid/gid - so we can switch
397 * back to root later, as required 583 * back to root later, as required
398 */ 584 */
399 seteuid(getuid()); 585 seteuid (getuid ());
400 setegid(getgid()); 586 setegid (getgid ());
401 break; 587 break;
402 case SAVE: 588 case SAVE:
403 R->euid = geteuid(); 589 euid = geteuid ();
404 R->egid = getegid(); 590 egid = getegid ();
405 break; 591 break;
406 case RESTORE: 592 case RESTORE:
407 seteuid(R->euid); 593 seteuid (euid);
408 setegid(R->egid); 594 setegid (egid);
409 break; 595 break;
410 } 596 }
411# else 597# else
412 switch (mode) { 598 switch (mode)
599 {
413 case IGNORE: 600 case IGNORE:
414 setuid(getuid()); 601 setuid (getuid ());
415 setgid(getgid()); 602 setgid (getgid ());
416 /* FALLTHROUGH */ 603 /* FALLTHROUGH */
417 case SAVE: 604 case SAVE:
418 /* FALLTHROUGH */ 605 /* FALLTHROUGH */
419 case RESTORE: 606 case RESTORE:
420 break; 607 break;
421 } 608 }
422# endif 609# endif
423#endif 610#endif
424} 611}
425 612
426#ifdef UTMP_SUPPORT 613#ifdef UTMP_SUPPORT
427/* EXTPROTO */
428void 614void
429rxvt_privileged_utmp(pR_ char action) 615rxvt_term::privileged_utmp (char action)
430{ 616{
431 D_MAIN((stderr, "rxvt_privileged_utmp(%c); waiting for: %c (pid: %d)", 617 D_MAIN ((stderr, "rxvt_privileged_utmp (%c); waiting for: %c (pid: %d)",
432 action, R->next_utmp_action, getpid())); 618 action, next_utmp_action, getpid ()));
433 if (R->next_utmp_action != action || (action != SAVE && action != RESTORE) 619 if (next_utmp_action != action || (action != SAVE && action != RESTORE)
434 || (R->Options & Opt_utmpInhibit) 620 || (Options & Opt_utmpInhibit)
435 || R->ttydev == NULL || *R->ttydev == '\0') 621 || ttydev == NULL || *ttydev == '\0')
436 return; 622 return;
437 623
438 rxvt_privileges(aR_ RESTORE); 624 privileges (RESTORE);
439 if (action == SAVE) { 625 if (action == SAVE)
626 {
440 R->next_utmp_action = RESTORE; 627 next_utmp_action = RESTORE;
441 rxvt_makeutent(aR_ R->ttydev, R->rs[Rs_display_name]); 628 makeutent (ttydev, rs[Rs_display_name]);
629 }
630 else
442 } else { /* action == RESTORE */ 631 { /* action == RESTORE */
443 R->next_utmp_action = IGNORE; 632 next_utmp_action = IGNORE;
444 rxvt_cleanutent(aR); 633 cleanutent ();
445 } 634 }
446 rxvt_privileges(aR_ IGNORE); 635 privileges (IGNORE);
447} 636}
448#endif 637#endif
449 638
450#ifndef NO_SETOWNER_TTYDEV 639#ifndef NO_SETOWNER_TTYDEV
451/* EXTPROTO */
452void 640void
453rxvt_privileged_ttydev(pR_ char action) 641rxvt_term::privileged_ttydev (char action)
454{ 642{
455 D_MAIN((stderr, 643 D_MAIN ((stderr,
456 "rxvt_privileged_ttydev(aR_ %c); waiting for: %c (pid: %d)", 644 "privileged_ttydev (%c); waiting for: %c (pid: %d)",
457 action, R->next_tty_action, getpid())); 645 action, next_tty_action, getpid ()));
458 if (R->next_tty_action != action || (action != SAVE && action != RESTORE) 646 if (next_tty_action != action || (action != SAVE && action != RESTORE)
459 || R->ttydev == NULL || *R->ttydev == '\0') 647 || ttydev == NULL || *ttydev == '\0')
460 return; 648 return;
461 649
462 rxvt_privileges(aR_ RESTORE); 650 privileges (RESTORE);
463 651
464 if (action == SAVE) { 652 if (action == SAVE)
653 {
465 R->next_tty_action = RESTORE; 654 next_tty_action = RESTORE;
466# ifndef RESET_TTY_TO_COMMON_DEFAULTS 655# ifndef RESET_TTY_TO_COMMON_DEFAULTS
467/* store original tty status for restoration rxvt_clean_exit() -- rgg 04/12/95 */ 656 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */
468 if (lstat(R->ttydev, &R->ttyfd_stat) < 0) /* you lose out */ 657 if (lstat (ttydev, &ttyfd_stat) < 0) /* you lose out */
469 R->next_tty_action = IGNORE; 658 next_tty_action = IGNORE;
470 else 659 else
471# endif 660# endif
661
472 { 662 {
473 chown(R->ttydev, getuid(), R->ttygid); /* fail silently */ 663 chown (ttydev, getuid (), ttygid); /* fail silently */
474 chmod(R->ttydev, R->ttymode); 664 chmod (ttydev, ttymode);
475# ifdef HAVE_REVOKE 665# ifdef HAVE_REVOKE
476 revoke(R->ttydev); 666 revoke (ttydev);
477# endif 667# endif
668
478 } 669 }
670 }
671 else
479 } else { /* action == RESTORE */ 672 { /* action == RESTORE */
480 R->next_tty_action = IGNORE; 673 next_tty_action = IGNORE;
481# ifndef RESET_TTY_TO_COMMON_DEFAULTS 674# ifndef RESET_TTY_TO_COMMON_DEFAULTS
482 chmod(R->ttydev, R->ttyfd_stat.st_mode); 675 chmod (ttydev, ttyfd_stat.st_mode);
483 chown(R->ttydev, R->ttyfd_stat.st_uid, R->ttyfd_stat.st_gid); 676 chown (ttydev, ttyfd_stat.st_uid, ttyfd_stat.st_gid);
484# else 677# else
485 chmod(R->ttydev, 678 chmod (ttydev,
486 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); 679 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
487 chown(R->ttydev, 0, 0); 680 chown (ttydev, 0, 0);
488# endif 681# endif
489 }
490 682
683 }
684
491 rxvt_privileges(aR_ IGNORE); 685 privileges (IGNORE);
492 686
493# ifndef RESET_TTY_TO_COMMON_DEFAULTS 687# ifndef RESET_TTY_TO_COMMON_DEFAULTS
494 D_MAIN((stderr, "%s \"%s\": mode %03o, uid %d, gid %d", 688 D_MAIN ((stderr, "%s \"%s\": mode %03o, uid %d, gid %d",
495 action == RESTORE ? "Restoring" : (action == 689 action == RESTORE ? "Restoring" : (action ==
496 SAVE ? "Saving" : 690 SAVE ? "Saving" :
497 "UNKNOWN ERROR for"), R->ttydev, 691 "UNKNOWN ERROR for"), ttydev,
498 R->ttyfd_stat.st_mode, R->ttyfd_stat.st_uid, 692 ttyfd_stat.st_mode, ttyfd_stat.st_uid,
499 R->ttyfd_stat.st_gid)); 693 ttyfd_stat.st_gid));
500# endif 694# endif
501} 695}
502#endif 696#endif
503 697
504/*----------------------------------------------------------------------*/ 698/*----------------------------------------------------------------------*/
505/* 699/*
506 * window size/position calculcations for XSizeHint and other storage. 700 * window size/position calculcations for XSizeHint and other storage.
507 * if width/height are non-zero then override calculated width/height 701 * if width/height are non-zero then override calculated width/height
508 */ 702 */
509/* EXTPROTO */
510void 703void
511rxvt_window_calc(pR_ unsigned int width, unsigned int height) 704rxvt_term::window_calc (unsigned int width, unsigned int height)
512{ 705{
513 short recalc_x, recalc_y; 706 short recalc_x, recalc_y;
514 int x, y, sb_w, mb_h, flags; 707 int x, y, sb_w, mb_h, flags;
515 unsigned int w, h; 708 unsigned int w, h;
516 unsigned int max_width, max_height; 709 unsigned int max_width, max_height;
517 710
518 D_SIZE((stderr, "< Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d", 711 D_SIZE ((stderr, "< Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d",
519 R->TermWin.ncol, R->TermWin.nrow, R->szHint.width, 712 TermWin.ncol, TermWin.nrow, szHint.width,
520 R->szHint.height)); 713 szHint.height));
521 R->szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity; 714 szHint.flags = PMinSize | PResizeInc | PBaseSize | PWinGravity;
522 R->szHint.win_gravity = NorthWestGravity; 715 szHint.win_gravity = NorthWestGravity;
523/* R->szHint.min_aspect.x = R->szHint.min_aspect.y = 1; */ 716 /* szHint.min_aspect.x = szHint.min_aspect.y = 1; */
524 717
525 recalc_x = recalc_y = 0; 718 recalc_x = recalc_y = 0;
526 flags = 0; 719 flags = 0;
527 if (!R->parsed_geometry) { 720 if (!parsed_geometry)
721 {
528 R->parsed_geometry = 1; 722 parsed_geometry = 1;
529 if (R->rs[Rs_geometry]) 723 if (rs[Rs_geometry])
530 flags = XParseGeometry(R->rs[Rs_geometry], &x, &y, &w, &h); 724 flags = XParseGeometry (rs[Rs_geometry], &x, &y, &w, &h);
531 if (flags & WidthValue) { 725 if (flags & WidthValue)
726 {
532 R->TermWin.ncol = BOUND_POSITIVE_INT16(w); 727 TermWin.ncol = BOUND_POSITIVE_INT16 (w);
533 R->szHint.flags |= USSize; 728 szHint.flags |= USSize;
534 } 729 }
535 if (flags & HeightValue) { 730 if (flags & HeightValue)
731 {
536 R->TermWin.nrow = BOUND_POSITIVE_INT16(h); 732 TermWin.nrow = BOUND_POSITIVE_INT16 (h);
537 R->szHint.flags |= USSize; 733 szHint.flags |= USSize;
538 } 734 }
539 if (flags & XValue) { 735 if (flags & XValue)
736 {
540 R->szHint.x = x; 737 szHint.x = x;
541 R->szHint.flags |= USPosition; 738 szHint.flags |= USPosition;
542 if (flags & XNegative) { 739 if (flags & XNegative)
740 {
543 recalc_x = 1; 741 recalc_x = 1;
544 R->szHint.win_gravity = NorthEastGravity; 742 szHint.win_gravity = NorthEastGravity;
545 } 743 }
546 } 744 }
547 if (flags & YValue) { 745 if (flags & YValue)
746 {
548 R->szHint.y = y; 747 szHint.y = y;
549 R->szHint.flags |= USPosition; 748 szHint.flags |= USPosition;
550 if (flags & YNegative) { 749 if (flags & YNegative)
750 {
551 recalc_y = 1; 751 recalc_y = 1;
552 if (R->szHint.win_gravity == NorthEastGravity) 752 if (szHint.win_gravity == NorthEastGravity)
553 R->szHint.win_gravity = SouthEastGravity; 753 szHint.win_gravity = SouthEastGravity;
554 else 754 else
555 R->szHint.win_gravity = SouthWestGravity; 755 szHint.win_gravity = SouthWestGravity;
556 } 756 }
557 } 757 }
558 } 758 }
759
559/* TODO: BOUNDS */ 760 /* TODO: BOUNDS */
560 R->TermWin.width = R->TermWin.ncol * R->TermWin.fwidth; 761 TermWin.width = TermWin.ncol * TermWin.fwidth;
561 R->TermWin.height = R->TermWin.nrow * R->TermWin.fheight; 762 TermWin.height = TermWin.nrow * TermWin.fheight;
562 max_width = MAX_COLS * R->TermWin.fwidth; 763 max_width = MAX_COLS * TermWin.fwidth;
563 max_height = MAX_ROWS * R->TermWin.fheight; 764 max_height = MAX_ROWS * TermWin.fheight;
564 765
565 R->szHint.base_width = R->szHint.base_height = 2 * R->TermWin.int_bwidth; 766 szHint.base_width = szHint.base_height = 2 * TermWin.int_bwidth;
566 767
567 sb_w = mb_h = 0; 768 sb_w = mb_h = 0;
568 R->window_vt_x = R->window_vt_y = 0; 769 window_vt_x = window_vt_y = TermWin.int_bwidth;
770
569 if (scrollbar_visible(R)) { 771 if (scrollbar_visible ())
772 {
570 sb_w = scrollbar_TotalWidth(); 773 sb_w = scrollbar_TotalWidth ();
571 R->szHint.base_width += sb_w; 774 szHint.base_width += sb_w;
572 if (!(R->Options & Opt_scrollBar_right)) 775 if (!(Options & Opt_scrollBar_right))
573 R->window_vt_x = sb_w; 776 window_vt_x += sb_w;
574 } 777 }
778
575 if (menubar_visible(R)) { 779 if (menubar_visible ())
780 {
576 mb_h = menuBar_TotalHeight(); 781 mb_h = menuBar_TotalHeight ();
577 R->szHint.base_height += mb_h; 782 szHint.base_height += mb_h;
578 R->window_vt_y = mb_h; 783 window_vt_y += mb_h;
579 } 784 }
785
580 R->szHint.width_inc = R->TermWin.fwidth; 786 szHint.width_inc = TermWin.fwidth;
581 R->szHint.height_inc = R->TermWin.fheight; 787 szHint.height_inc = TermWin.fheight;
582 R->szHint.min_width = R->szHint.base_width + R->szHint.width_inc; 788 szHint.min_width = szHint.base_width + szHint.width_inc;
583 R->szHint.min_height = R->szHint.base_height + R->szHint.height_inc; 789 szHint.min_height = szHint.base_height + szHint.height_inc;
584 790
585 if (width && width - R->szHint.base_width < max_width) { 791 if (width && width - szHint.base_width < max_width)
792 {
586 R->szHint.width = width; 793 szHint.width = width;
587 R->TermWin.width = width - R->szHint.base_width; 794 TermWin.width = width - szHint.base_width;
588 } else { 795 }
796 else
797 {
589 MIN_IT(R->TermWin.width, max_width); 798 MIN_IT (TermWin.width, max_width);
590 R->szHint.width = R->szHint.base_width + R->TermWin.width; 799 szHint.width = szHint.base_width + TermWin.width;
591 } 800 }
801
592 if (height && height - R->szHint.base_height < max_height) { 802 if (height && height - szHint.base_height < max_height)
803 {
593 R->szHint.height = height; 804 szHint.height = height;
594 R->TermWin.height = height - R->szHint.base_height; 805 TermWin.height = height - szHint.base_height;
595 } else { 806 }
807 else
808 {
596 MIN_IT(R->TermWin.height, max_height); 809 MIN_IT (TermWin.height, max_height);
597 R->szHint.height = R->szHint.base_height + R->TermWin.height; 810 szHint.height = szHint.base_height + TermWin.height;
598 } 811 }
812
599 if (scrollbar_visible(R) && (R->Options & Opt_scrollBar_right)) 813 if (scrollbar_visible () && (Options & Opt_scrollBar_right))
600 R->window_sb_x = R->szHint.width - sb_w; 814 window_sb_x = szHint.width - sb_w;
601 815
602 if (recalc_x) 816 if (recalc_x)
603 R->szHint.x += (DisplayWidth(R->Xdisplay, Xscreen) 817 szHint.x += (DisplayWidth (display->display, DefaultScreen (display->display))
604 - R->szHint.width - 2 * R->TermWin.ext_bwidth); 818 - szHint.width - 2 * TermWin.ext_bwidth);
605 if (recalc_y) 819 if (recalc_y)
606 R->szHint.y += (DisplayHeight(R->Xdisplay, Xscreen) 820 szHint.y += (DisplayHeight (display->display, DefaultScreen (display->display))
607 - R->szHint.height - 2 * R->TermWin.ext_bwidth); 821 - szHint.height - 2 * TermWin.ext_bwidth);
608 822
609 R->TermWin.ncol = R->TermWin.width / R->TermWin.fwidth; 823 TermWin.ncol = TermWin.width / TermWin.fwidth;
610 R->TermWin.nrow = R->TermWin.height / R->TermWin.fheight; 824 TermWin.nrow = TermWin.height / TermWin.fheight;
611 D_SIZE((stderr, "> Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d", 825 D_SIZE ((stderr, "> Cols/Rows: %3d x %3d ; Width/Height: %4d x %4d",
612 R->TermWin.ncol, R->TermWin.nrow, R->szHint.width, 826 TermWin.ncol, TermWin.nrow, szHint.width,
613 R->szHint.height)); 827 szHint.height));
614 return; 828 return;
615} 829}
616 830
617/*----------------------------------------------------------------------*/ 831/*----------------------------------------------------------------------*/
618/* 832/*
619 * Tell the teletype handler what size the window is. 833 * Tell the teletype handler what size the window is.
620 * Called after a window size change. 834 * Called after a window size change.
621 */ 835 */
622void 836void
623rxvt_term::tt_winch () 837rxvt_term::tt_winch ()
624{ 838{
625 struct winsize ws; 839 struct winsize ws;
626 840
627 if (cmd_fd < 0) 841 if (cmd_fd < 0)
628 return; 842 return;
629 843
630 ws.ws_col = TermWin.ncol; 844 ws.ws_col = TermWin.ncol;
632 ws.ws_xpixel = ws.ws_ypixel = 0; 846 ws.ws_xpixel = ws.ws_ypixel = 0;
633#ifndef DEBUG_SIZE 847#ifndef DEBUG_SIZE
634 (void)ioctl (cmd_fd, TIOCSWINSZ, &ws); 848 (void)ioctl (cmd_fd, TIOCSWINSZ, &ws);
635#else 849#else
636 if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0) 850 if (ioctl (cmd_fd, TIOCSWINSZ, &ws) < 0)
637 D_SIZE((stderr, "Failed to send TIOCSWINSZ to fd %d", fd)); 851 D_SIZE ((stderr, "Failed to send TIOCSWINSZ to fd %d", fd));
638# ifdef SIGWINCH 852# ifdef SIGWINCH
639 else if (cmd_pid) /* force through to the command */ 853 else if (cmd_pid) /* force through to the command */
640 kill (cmd_pid, SIGWINCH); 854 kill (cmd_pid, SIGWINCH);
641# endif 855# endif
642#endif 856#endif
643} 857}
644 858
645/*----------------------------------------------------------------------*/ 859/*----------------------------------------------------------------------*/
646/* rxvt_change_font() - Switch to a new font */ 860/* rxvt_change_font () - Switch to a new font */
647/* 861/*
648 * init = 1 - initialize 862 * init = 1 - initialize
649 * 863 *
650 * fontname == FONT_UP - switch to bigger font 864 * fontname == FONT_UP - switch to bigger font
651 * fontname == FONT_DN - switch to smaller font 865 * fontname == FONT_DN - switch to smaller font
652 */ 866 */
653/* EXTPROTO */ 867bool
654void
655rxvt_change_font(pR_ int init, const char *fontname) 868rxvt_term::change_font (const char *fontname)
656{ 869{
657} 870 if (fontname == FONT_UP)
871 {
872 // TODO
873 }
874 else if (fontname == FONT_DN)
875 {
876 // TODO
877 }
878 else
879 {
880 rxvt_fontset *fs = new rxvt_fontset (this);
658 881
659/* INTPROTO */ 882 if (fs && fs->populate (fontname ? fontname : "fixed"))
660void 883 {
884 delete TermWin.fontset;
885 TermWin.fontset = fs;
886 TermWin.fwidth = fs->base_font ()->width;
887 TermWin.fheight = fs->base_font ()->height;
888 TermWin.fbase = fs->base_font ()->ascent;
889
890 if (TermWin.parent[0])
891 {
892 resize_all_windows (0, 0, 0);
893 scr_remap_chars ();
894 scr_touch (true);
895 }
896
897 for (unicode_t ch = 0x20; ch <= 0x7f; ch++)
898 TermWin.ascii_map [ch - 0x20] = fs->find_font (ch);
899
900 return true;
901 }
902 }
903
904 return false;
905}
906
907bool
661rxvt_font_up_down(pR_ int n, int direction) 908rxvt_term::font_up_down (int n, int direction)
662{ 909{
910 return false;
663} 911}
664 912
665/*----------------------------------------------------------------------*/ 913/*----------------------------------------------------------------------*/
666/*----------------------------------------------------------------------*/ 914/*----------------------------------------------------------------------*/
667/* xterm sequences - title, iconName, color (exptl) */ 915/* xterm sequences - title, iconName, color (exptl) */
668/* EXTPROTO */
669void 916void
670rxvt_set_title(pR_ const char *str) 917rxvt_term::set_title (const char *str)
671{ 918{
672#ifndef SMART_WINDOW_TITLE 919#ifdef SMART_WINDOW_TITLE
920 char *name;
921
922 if (!XFetchName (display->display, TermWin.parent[0], &name))
923 name = NULL;
924
925 if (name == NULL || STRCMP (name, str))
926#endif
673 XStoreName(R->Xdisplay, R->TermWin.parent[0], str); 927 XStoreName (display->display, TermWin.parent[0], str);
928
929#ifdef SMART_WINDOW_TITLE
930 if (name)
931 XFree (name);
932#endif
933}
934
935void
936rxvt_term::set_icon_name (const char *str)
937{
938#ifdef SMART_WINDOW_TITLE
939 char *name;
940
941 if (!XGetIconName (display->display, TermWin.parent[0], &name))
942 name = NULL;
943
944 if (name == NULL || STRCMP (name, str))
945#endif
946 XSetIconName (display->display, TermWin.parent[0], str);
947
948#ifdef SMART_WINDOW_TITLE
949 if (name)
950 XFree (name);
951#endif
952}
953
954#ifdef XTERM_COLOR_CHANGE
955void
956rxvt_term::set_window_color (int idx, const char *color)
957{
958 rxvt_color xcol;
959 int i;
960
961 if (color == NULL || *color == '\0')
962 return;
963
964 /* handle color aliases */
965 if (isdigit (*color))
966 {
967 i = atoi (color);
968 if (i >= 8 && i <= 15)
969 { /* bright colors */
970 i -= 8;
971# ifndef NO_BRIGHTCOLOR
972 PixColorsFocused[idx] = PixColorsFocused[minBrightCOLOR + i];
973 SET_PIXCOLOR (idx);
974 goto Done;
975# endif
976
977 }
978 if (i >= 0 && i <= 7)
979 { /* normal colors */
980 PixColorsFocused[idx] = PixColorsFocused[minCOLOR + i];
981 SET_PIXCOLOR (idx);
982 goto Done;
983 }
984 }
985
986 if (!rXParseAllocColor (& xcol, color))
987 return;
988
989 /* XStoreColor (display->display, display->cmap, XColor*); */
990
991 /*
992 * FIXME: should free colors here, but no idea how to do it so instead,
993 * so just keep gobbling up the colormap
994 */
995# if 0
996 for (i = Color_Black; i <= Color_White; i++)
997 if (PixColors[idx] == PixColors[i])
998 break;
999 if (i > Color_White)
1000 {
1001 /* fprintf (stderr, "XFreeColors: PixColors [%d] = %lu\n", idx, PixColors [idx]); */
1002 XFreeColors (display->display, display->cmap, (PixColors + idx), 1,
1003 DisplayPlanes (display->display, display->screen));
1004 }
1005# endif
1006
1007 PixColorsFocused[idx] = xcol;
1008 SET_PIXCOLOR (idx);
1009
1010 /* XSetWindowAttributes attr; */
1011 /* Cursor cursor; */
1012Done:
1013#ifdef OFF_FOCUS_FADING
1014 PixColorsUnFocused[idx] = PixColorsFocused[idx].fade (display, atoi (rs[Rs_fade]));
1015#endif
1016 if (idx == Color_bg && ! (Options & Opt_transparent))
1017 XSetWindowBackground (display->display, TermWin.vt, PixColors[Color_bg]);
1018
1019 /* handle Color_BD, scrollbar background, etc. */
1020
1021 set_colorfgbg ();
1022 recolour_cursor ();
1023 scr_touch (true);
1024}
1025
674#else 1026#else
675 char *name;
676
677 if (XFetchName(R->Xdisplay, R->TermWin.parent[0], &name) == 0)
678 name = NULL;
679 if (name == NULL || STRCMP(name, str))
680 XStoreName(R->Xdisplay, R->TermWin.parent[0], str);
681 if (name)
682 XFree(name);
683#endif
684}
685
686/* EXTPROTO */
687void
688rxvt_set_iconName(pR_ const char *str)
689{
690#ifndef SMART_WINDOW_TITLE
691 XSetIconName(R->Xdisplay, R->TermWin.parent[0], str);
692#else
693 char *name;
694
695 if (XGetIconName(R->Xdisplay, R->TermWin.parent[0], &name))
696 name = NULL;
697 if (name == NULL || STRCMP(name, str))
698 XSetIconName(R->Xdisplay, R->TermWin.parent[0], str);
699 if (name)
700 XFree(name);
701#endif
702}
703
704#ifdef XTERM_COLOR_CHANGE
705/* EXTPROTO */
706void
707rxvt_set_window_color(pR_ int idx, const char *color)
708{
709 rxvt_color xcol;
710 int i;
711
712 if (color == NULL || *color == '\0')
713 return;
714
715/* handle color aliases */
716 if (isdigit(*color)) {
717 i = atoi(color);
718 if (i >= 8 && i <= 15) { /* bright colors */
719 i -= 8;
720# ifndef NO_BRIGHTCOLOR
721 R->PixColors[idx] = R->PixColors[minBrightCOLOR + i];
722 SET_PIXCOLOR(R, idx);
723 goto Done;
724# endif
725 }
726 if (i >= 0 && i <= 7) { /* normal colors */
727 R->PixColors[idx] = R->PixColors[minCOLOR + i];
728 SET_PIXCOLOR(R, idx);
729 goto Done;
730 }
731 }
732 if (!rxvt_rXParseAllocColor(aR_ & xcol, color))
733 return;
734/* XStoreColor (R->Xdisplay, XCMAP, XColor*); */
735
736/*
737 * FIXME: should free colors here, but no idea how to do it so instead,
738 * so just keep gobbling up the colormap
739 */
740# if 0
741 for (i = Color_Black; i <= Color_White; i++)
742 if (R->PixColors[idx] == R->PixColors[i])
743 break;
744 if (i > Color_White) {
745 /* fprintf (stderr, "XFreeColors: R->PixColors [%d] = %lu\n", idx, R->PixColors [idx]); */
746 XFreeColors(R->Xdisplay, XCMAP, (R->PixColors + idx), 1,
747 DisplayPlanes(R->Xdisplay, Xscreen));
748 }
749# endif
750
751 R->PixColors[idx] = xcol;
752 SET_PIXCOLOR(R, idx);
753
754/* XSetWindowAttributes attr; */
755/* Cursor cursor; */
756 Done:
757 if (idx == Color_bg && !(R->Options & Opt_transparent))
758 XSetWindowBackground(R->Xdisplay, R->TermWin.vt,
759 R->PixColors[Color_bg]);
760
761/* handle Color_BD, scrollbar background, etc. */
762
763 rxvt_set_colorfgbg(aR);
764 rxvt_recolour_cursor(aR);
765/* the only reasonable way to enforce a clean update */
766 rxvt_scr_poweron(aR);
767}
768
769#else
770# define rxvt_set_window_color(aR_ idx,color) ((void)0) 1027# define set_window_color (idx,color) ((void)0)
771#endif /* XTERM_COLOR_CHANGE */ 1028#endif /* XTERM_COLOR_CHANGE */
772 1029
773/* EXTPROTO */
774void 1030void
775rxvt_recolour_cursor(pR) 1031rxvt_term::recolour_cursor ()
776{ 1032{
777 rxvt_color xcol[2]; 1033 XColor xcol[2];
778 1034
779#if TODO 1035 xcol[0].pixel = ISSET_PIXCOLOR (Color_pointer_fg) ? PixColorsFocused[Color_pointer_fg] : PixColorsFocused[Color_fg];
780 xcol[0] = R->PixColors[Color_pointer]; 1036 xcol[1].pixel = ISSET_PIXCOLOR (Color_pointer_bg) ? PixColorsFocused[Color_pointer_bg] : PixColorsFocused[Color_bg];
781 xcol[1] = R->PixColors[Color_bg]; 1037 XQueryColors (display->display, display->cmap, xcol, 2);
782 XQueryColors(R->Xdisplay, XCMAP, xcol, 2);
783 XRecolorCursor(R->Xdisplay, R->TermWin_cursor, &(xcol[0]), &(xcol[1])); 1038 XRecolorCursor (display->display, TermWin_cursor, xcol + 0, xcol + 1);
784#endif
785} 1039}
786 1040
787/*----------------------------------------------------------------------*/ 1041/*----------------------------------------------------------------------*/
788/* 1042/*
789 * find if fg/bg matches any of the normal (low-intensity) colors 1043 * find if fg/bg matches any of the normal (low-intensity) colors
790 */ 1044 */
791/* INTPROTO */
792void 1045void
793rxvt_set_colorfgbg(pR) 1046rxvt_term::set_colorfgbg ()
794{ 1047{
795 unsigned int i; 1048 unsigned int i;
796 const char *xpmb = "\0"; 1049 const char *xpmb = "\0";
797 char fstr[sizeof("default") + 1], bstr[sizeof("default") + 1]; 1050 char fstr[sizeof ("default") + 1], bstr[sizeof ("default") + 1];
798 1051
799 R->env_colorfgbg = 1052 env_colorfgbg =
800 (char *)rxvt_malloc(sizeof("COLORFGBG=default;default;bg") + 1); 1053 (char *)rxvt_malloc (sizeof ("COLORFGBG=default;default;bg") + 1);
801 STRCPY(fstr, "default"); 1054 STRCPY (fstr, "default");
802 STRCPY(bstr, "default"); 1055 STRCPY (bstr, "default");
803 for (i = Color_Black; i <= Color_White; i++) 1056 for (i = Color_Black; i <= Color_White; i++)
804 if (R->PixColors[Color_fg] == R->PixColors[i]) { 1057 if (PixColors[Color_fg] == PixColors[i])
1058 {
805 sprintf(fstr, "%d", (i - Color_Black)); 1059 sprintf (fstr, "%d", (i - Color_Black));
806 break; 1060 break;
807 } 1061 }
808 for (i = Color_Black; i <= Color_White; i++) 1062 for (i = Color_Black; i <= Color_White; i++)
809 if (R->PixColors[Color_bg] == R->PixColors[i]) { 1063 if (PixColors[Color_bg] == PixColors[i])
1064 {
810 sprintf(bstr, "%d", (i - Color_Black)); 1065 sprintf (bstr, "%d", (i - Color_Black));
811#ifdef XPM_BACKGROUND 1066#ifdef XPM_BACKGROUND
812 xpmb = "default;"; 1067 xpmb = "default;";
813#endif 1068#endif
814 break; 1069 break;
815 } 1070 }
1071
816 sprintf(R->env_colorfgbg, "COLORFGBG=%s;%s%s", fstr, xpmb, bstr); 1072 sprintf (env_colorfgbg, "COLORFGBG=%s;%s%s", fstr, xpmb, bstr);
817 putenv(R->env_colorfgbg);
818 1073
819#ifndef NO_BRIGHTCOLOR 1074#ifndef NO_BRIGHTCOLOR
820 R->colorfgbg = DEFAULT_RSTYLE; 1075 colorfgbg = DEFAULT_RSTYLE;
821 for (i = minCOLOR; i <= maxCOLOR; i++) { 1076 for (i = minCOLOR; i <= maxCOLOR; i++)
1077 {
822 if (R->PixColors[Color_fg] == R->PixColors[i]) 1078 if (PixColors[Color_fg] == PixColors[i])
823 R->colorfgbg = SET_FGCOLOR(R->colorfgbg, i); 1079 colorfgbg = SET_FGCOLOR (colorfgbg, i);
824 if (R->PixColors[Color_bg] == R->PixColors[i]) 1080 if (PixColors[Color_bg] == PixColors[i])
825 R->colorfgbg = SET_BGCOLOR(R->colorfgbg, i); 1081 colorfgbg = SET_BGCOLOR (colorfgbg, i);
826 } 1082 }
827#endif 1083#endif
828} 1084}
829 1085
830/*----------------------------------------------------------------------*/ 1086/*----------------------------------------------------------------------*/
831/* 1087/*
832 * Colour determination for low colour displays, routine from 1088 * Colour determination for low colour displays, routine from
833 * Hans de Goede <hans@highrise.nl> 1089 * Hans de Goede <hans@highrise.nl>
834 */ 1090 */
835 1091
836/* EXTPROTO */
837int 1092int
838rxvt_rXParseAllocColor(pR_ rxvt_color * screen_in_out, const char *colour) 1093rxvt_term::rXParseAllocColor (rxvt_color *screen_in_out, const char *colour)
839{ 1094{
840 screen_in_out->set(aR_ colour);
841
842 if (!screen_in_out->set(aR_ colour)) { 1095 if (!screen_in_out->set (display, colour))
843 rxvt_print_error("can't allocate colour: %s", colour); 1096 {
1097 rxvt_warn ("can't get colour '%s', continuing without.\n", colour);
844 return false; 1098 return false;
845 } 1099 }
846 1100
847 return true; 1101 return true;
848} 1102}
849 1103
850/* -------------------------------------------------------------------- * 1104/* -------------------------------------------------------------------- *
851 * - WINDOW RESIZING - * 1105 * - WINDOW RESIZING - *
852 * -------------------------------------------------------------------- */ 1106 * -------------------------------------------------------------------- */
853/* EXTPROTO */
854void 1107void
855rxvt_resize_all_windows(pR_ unsigned int width, unsigned int height, 1108rxvt_term::resize_all_windows (unsigned int width, unsigned int height, int ignoreparent)
856 int ignoreparent)
857{ 1109{
858 int fix_screen; 1110 int fix_screen;
859 1111
860#ifdef SMART_RESIZE 1112#ifdef SMART_RESIZE
861 int old_width = R->szHint.width, old_height = R->szHint.height; 1113 int old_width = szHint.width, old_height = szHint.height;
862#endif 1114#endif
863 1115
864 rxvt_window_calc(aR_ width, height); 1116 window_calc (width, height);
865 XSetWMNormalHints(R->Xdisplay, R->TermWin.parent[0], &R->szHint); 1117 XSetWMNormalHints (display->display, TermWin.parent[0], &szHint);
1118
866 if (!ignoreparent) { 1119 if (!ignoreparent)
1120 {
867#ifdef SMART_RESIZE 1121#ifdef SMART_RESIZE
868/* 1122 /*
869 * resize by Marius Gedminas <marius.gedminas@uosis.mif.vu.lt> 1123 * resize by Marius Gedminas <marius.gedminas@uosis.mif.vu.lt>
870 * reposition window on resize depending on placement on screen 1124 * reposition window on resize depending on placement on screen
871 */ 1125 */
872 int x, y, x1, y1; 1126 int x, y, x1, y1;
873 int dx, dy; 1127 int dx, dy;
874 unsigned int unused_w1, unused_h1, unused_b1, unused_d1; 1128 unsigned int unused_w1, unused_h1, unused_b1, unused_d1;
875 Window unused_cr; 1129 Window unused_cr;
876 1130
877 XTranslateCoordinates(R->Xdisplay, R->TermWin.parent[0], Xroot, 1131 XTranslateCoordinates (display->display, TermWin.parent[0], display->root,
878 0, 0, &x, &y, &unused_cr); 1132 0, 0, &x, &y, &unused_cr);
879 XGetGeometry(R->Xdisplay, R->TermWin.parent[0], &unused_cr, &x1, &y1, 1133 XGetGeometry (display->display, TermWin.parent[0], &unused_cr, &x1, &y1,
880 &unused_w1, &unused_h1, &unused_b1, &unused_d1); 1134 &unused_w1, &unused_h1, &unused_b1, &unused_d1);
881 /* 1135 /*
882 * if Xroot isn't the parent window, a WM will probably have offset 1136 * if display->root isn't the parent window, a WM will probably have offset
883 * our position for handles and decorations. Counter it 1137 * our position for handles and decorations. Counter it
884 */ 1138 */
885 if (x1 != x || y1 != y) { 1139 if (x1 != x || y1 != y)
1140 {
886 x -= x1; 1141 x -= x1;
887 y -= y1; 1142 y -= y1;
888 } 1143 }
889 1144
890 x1 = (DisplayWidth(R->Xdisplay, Xscreen) - old_width) / 2; 1145 x1 = (DisplayWidth (display->display, display->screen) - old_width) / 2;
891 y1 = (DisplayHeight(R->Xdisplay, Xscreen) - old_height) / 2; 1146 y1 = (DisplayHeight (display->display, display->screen) - old_height) / 2;
892 dx = old_width - R->szHint.width; 1147 dx = old_width - szHint.width;
893 dy = old_height - R->szHint.height; 1148 dy = old_height - szHint.height;
894 1149
895 /* Check position of the center of the window */ 1150 /* Check position of the center of the window */
896 if (x < x1) /* left half */ 1151 if (x < x1) /* left half */
897 dx = 0; 1152 dx = 0;
898 else if (x == x1) /* exact center */ 1153 else if (x == x1) /* exact center */
899 dx /= 2; 1154 dx /= 2;
900 if (y < y1) /* top half */ 1155 if (y < y1) /* top half */
901 dy = 0; 1156 dy = 0;
902 else if (y == y1) /* exact center */ 1157 else if (y == y1) /* exact center */
903 dy /= 2; 1158 dy /= 2;
904 1159
905 XMoveResizeWindow(R->Xdisplay, R->TermWin.parent[0], x + dx, y + dy, 1160 XMoveResizeWindow (display->display, TermWin.parent[0], x + dx, y + dy,
906 R->szHint.width, R->szHint.height); 1161 szHint.width, szHint.height);
907#else 1162#else
908 XResizeWindow(R->Xdisplay, R->TermWin.parent[0], R->szHint.width, 1163 XResizeWindow (display->display, TermWin.parent[0], szHint.width, szHint.height);
909 R->szHint.height);
910#endif 1164#endif
911 } 1165 }
912 1166
913 fix_screen = (R->TermWin.ncol != R->prev_ncol 1167 fix_screen = TermWin.ncol != prev_ncol || TermWin.nrow != prev_nrow;
914 || R->TermWin.nrow != R->prev_nrow); 1168
915 if (fix_screen || width != R->old_width || height != R->old_height) { 1169 if (fix_screen || width != old_width || height != old_height)
1170 {
916 if (scrollbar_visible(R)) { 1171 if (scrollbar_visible ())
1172 {
917 XMoveResizeWindow(R->Xdisplay, R->scrollBar.win, R->window_sb_x, 1173 XMoveResizeWindow (display->display, scrollBar.win,
1174 window_sb_x, 0,
918 0, scrollbar_TotalWidth(), R->szHint.height); 1175 scrollbar_TotalWidth (), szHint.height);
919 rxvt_Resize_scrollBar(aR); 1176 resize_scrollbar ();
920 } 1177 }
1178
921 if (menubar_visible(R)) 1179 if (menubar_visible ())
922 XMoveResizeWindow(R->Xdisplay, R->menuBar.win, R->window_vt_x, 1180 XMoveResizeWindow (display->display, menuBar.win,
1181 window_vt_x, 0,
923 0, TermWin_TotalWidth(), menuBar_TotalHeight()); 1182 TermWin_TotalWidth (), menuBar_TotalHeight ());
924 XMoveResizeWindow(R->Xdisplay, R->TermWin.vt, R->window_vt_x, 1183
925 R->window_vt_y, TermWin_TotalWidth(), 1184 XMoveResizeWindow (display->display, TermWin.vt,
1185 window_vt_x, window_vt_y,
926 TermWin_TotalHeight()); 1186 TermWin_TotalWidth (), TermWin_TotalHeight ());
927#ifdef RXVT_GRAPHICS
928 if (R->old_height)
929 rxvt_Gr_Resize(aR_ R->old_width - R->szHint.base_width,
930 R->old_height - R->szHint.base_height);
931#endif
932 rxvt_scr_clear(aR); 1187 scr_clear ();
933#ifdef XPM_BACKGROUND 1188#ifdef XPM_BACKGROUND
934 rxvt_resize_pixmap(aR); 1189 resize_pixmap ();
935#endif 1190#endif
936 } 1191 }
937 1192
938 if (fix_screen || R->old_height == 0) { 1193 if (fix_screen || old_height == 0)
1194 {
939 int curr_screen = -1; 1195 int curr_screen = -1;
940 uint16_t old_ncol = R->prev_ncol; 1196 int old_ncol = prev_ncol;
941 1197
942 /* scr_reset only works on the primary screen */ 1198 /* scr_reset only works on the primary screen */
943 if (R->old_height) /* this is not the first time through */ 1199 if (old_height) /* this is not the first time through */
1200 {
1201 unsigned int ncol = TermWin.ncol;
1202 TermWin.ncol = prev_ncol; // save b/c scr_blank_screen_mem uses this
944 curr_screen = rxvt_scr_change_screen(aR_ PRIMARY); 1203 curr_screen = scr_change_screen (PRIMARY);
945 rxvt_scr_reset(aR); 1204 TermWin.ncol = ncol;
946 if (curr_screen >= 0) { /* this is not the first time through */
947 rxvt_scr_change_screen(aR_ curr_screen);
948 rxvt_selection_check(aR_(old_ncol != R->TermWin.ncol ? 4 : 0));
949 } 1205 }
950 }
951 1206
1207 scr_reset ();
1208
1209 if (curr_screen >= 0) /* this is not the first time through */
1210 {
1211 scr_change_screen (curr_screen);
1212 selection_check (old_ncol != TermWin.ncol ? 4 : 0);
1213 }
1214 }
1215
952 R->old_width = R->szHint.width; 1216 old_width = szHint.width;
953 R->old_height = R->szHint.height; 1217 old_height = szHint.height;
954 1218
955#ifdef USE_XIM 1219#ifdef USE_XIM
956 rxvt_IMSetStatusPosition(aR); 1220 IMSetStatusPosition ();
957#endif 1221#endif
958} 1222}
959 1223
960/* 1224/*
961 * Set the width/height of the vt window in characters. Units are pixels. 1225 * Set the width/height of the vt window in characters. Units are pixels.
962 * good for toggling 80/132 columns 1226 * good for toggling 80/132 columns
963 */ 1227 */
964/* EXTPROTO */
965void 1228void
966rxvt_set_widthheight(pR_ unsigned int width, unsigned int height) 1229rxvt_term::set_widthheight (unsigned int width, unsigned int height)
967{ 1230{
968 XWindowAttributes wattr; 1231 XWindowAttributes wattr;
969 1232
970 if (width == 0 || height == 0) { 1233 if (width == 0 || height == 0)
1234 {
971 XGetWindowAttributes(R->Xdisplay, Xroot, &wattr); 1235 XGetWindowAttributes (display->display, display->root, &wattr);
972 if (width == 0) 1236 if (width == 0)
973 width = wattr.width - R->szHint.base_width; 1237 width = wattr.width - szHint.base_width;
974 if (height == 0) 1238 if (height == 0)
975 height = wattr.height - R->szHint.base_height; 1239 height = wattr.height - szHint.base_height;
976 } 1240 }
977 if (width != R->TermWin.width || height != R->TermWin.height) { 1241 if (width != TermWin.width || height != TermWin.height)
1242 {
978 width += R->szHint.base_width; 1243 width += szHint.base_width;
979 height += R->szHint.base_height; 1244 height += szHint.base_height;
980 rxvt_resize_all_windows(aR_ width, height, 0); 1245 resize_all_windows (width, height, 0);
981 } 1246 }
982} 1247}
983 1248
984/* -------------------------------------------------------------------- * 1249/* -------------------------------------------------------------------- *
985 * - X INPUT METHOD ROUTINES - * 1250 * - X INPUT METHOD ROUTINES - *
986 * -------------------------------------------------------------------- */ 1251 * -------------------------------------------------------------------- */
987#ifdef USE_XIM 1252#ifdef USE_XIM
988/* INTPROTO */ 1253
989void 1254void
990rxvt_setSize(pR_ XRectangle * size) 1255rxvt_term::im_set_size (XRectangle *size)
991{ 1256{
992 size->x = R->TermWin.int_bwidth; 1257 size->x = TermWin.int_bwidth;
993 size->y = R->TermWin.int_bwidth; 1258 size->y = TermWin.int_bwidth;
994 size->width = Width2Pixel(R->TermWin.ncol); 1259 size->width = Width2Pixel (TermWin.ncol);
995 size->height = Height2Pixel(R->TermWin.nrow); 1260 size->height = Height2Pixel (TermWin.nrow);
996} 1261}
997 1262
998/* INTPROTO */
999void 1263void
1000rxvt_setColor(pR_ unsigned long *fg, unsigned long *bg) 1264rxvt_term::im_set_color (unsigned long *fg, unsigned long *bg)
1001{ 1265{
1002 *fg = R->PixColors[Color_fg]; 1266 *fg = PixColors[Color_fg];
1003 *bg = R->PixColors[Color_bg]; 1267 *bg = PixColors[Color_bg];
1004} 1268}
1005 1269
1006/* Checking whether input method is running. */ 1270/* Checking whether input method is running. */
1007/* INTPROTO */
1008Bool 1271bool
1009rxvt_IMisRunning(pR) 1272rxvt_term::IMisRunning ()
1010{ 1273{
1011 char *p; 1274 char *p;
1012 Atom atom; 1275 Atom atom;
1013 Window win; 1276 Window win;
1014 char server[IMBUFSIZ]; 1277 char server[IMBUFSIZ];
1015 1278
1016/* get current locale modifier */ 1279 /* get current locale modifier */
1017 if ((p = XSetLocaleModifiers(NULL)) != NULL) { 1280 if ((p = XSetLocaleModifiers (NULL)) != NULL)
1281 {
1018 STRCPY(server, "@server="); 1282 STRCPY (server, "@server=");
1019 STRNCAT(server, &(p[4]), IMBUFSIZ - 9); /* skip "@im=" */ 1283 STRNCAT (server, & (p[4]), IMBUFSIZ - 9); /* skip "@im=" */
1020 if ((p = STRCHR(server + 1, '@')) != NULL) /* first one only */ 1284 if ((p = STRCHR (server + 1, '@')) != NULL) /* first one only */
1021 *p = '\0'; 1285 *p = '\0';
1022 1286
1023 atom = XInternAtom(R->Xdisplay, server, False); 1287 atom = XInternAtom (display->display, server, False);
1024 win = XGetSelectionOwner(R->Xdisplay, atom); 1288 win = XGetSelectionOwner (display->display, atom);
1025 if (win != None) 1289 if (win != None)
1026 return True; 1290 return True;
1027 } 1291 }
1028 return False; 1292 return False;
1029} 1293}
1030 1294
1031/* EXTPROTO */
1032void 1295void
1033rxvt_IMSendSpot(pR) 1296rxvt_term::IMSendSpot ()
1034{ 1297{
1035 XPoint spot; 1298 XPoint spot;
1036 XVaNestedList preedit_attr; 1299 XVaNestedList preedit_attr;
1037 1300
1038 if (R->Input_Context == NULL 1301 if (Input_Context == NULL
1039 || !R->TermWin.focus || !(R->input_style & XIMPreeditPosition) 1302 || !TermWin.focus || ! (input_style & XIMPreeditPosition)
1040 || !(R->event_type == KeyPress 1303 || ! (event_type == KeyPress
1041 || R->event_type == Expose 1304 || event_type == Expose
1042 || R->event_type == NoExpose 1305 || event_type == NoExpose
1043 || R->event_type == SelectionNotify 1306 || event_type == SelectionNotify
1044 || R->event_type == ButtonRelease || R->event_type == FocusIn) 1307 || event_type == ButtonRelease || event_type == FocusIn)
1045 || !rxvt_IMisRunning(aR)) 1308 || !IMisRunning ())
1046 return; 1309 return;
1047 1310
1048 rxvt_setPosition(aR_ & spot); 1311 im_set_position (&spot);
1049 1312
1050 preedit_attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL); 1313 preedit_attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
1051 XSetICValues(R->Input_Context, XNPreeditAttributes, preedit_attr, NULL); 1314 XSetICValues (Input_Context, XNPreeditAttributes, preedit_attr, NULL);
1052 XFree(preedit_attr); 1315 XFree (preedit_attr);
1053} 1316}
1054 1317
1055/* EXTPROTO */
1056void 1318void
1057rxvt_setTermFontSet(pR_ int idx)
1058{
1059 char *string;
1060 long length;
1061 int success = 0;
1062
1063 if (idx < 0 || idx >= MAX_NFONTS)
1064 return;
1065}
1066
1067/* INTPROTO */
1068void
1069rxvt_setPreeditArea (pR_ XRectangle * preedit_rect, XRectangle * status_rect, 1319rxvt_term::im_set_preedit_area (XRectangle * preedit_rect, XRectangle * status_rect,
1070 XRectangle * needed_rect) 1320 XRectangle * needed_rect)
1071{ 1321{
1072 int mbh, vtx = 0; 1322 int mbh, vtx = 0;
1073 1323
1074 if (scrollbar_visible(R) && !(R->Options & Opt_scrollBar_right)) 1324 if (scrollbar_visible () && ! (Options & Opt_scrollBar_right))
1075 vtx = scrollbar_TotalWidth(); 1325 vtx = scrollbar_TotalWidth ();
1326
1076 mbh = menubar_visible(R) ? menuBar_TotalHeight() : 0; 1327 mbh = menubar_visible () ? menuBar_TotalHeight () : 0;
1077 mbh -= R->TermWin.lineSpace; 1328 mbh -= TermWin.lineSpace;
1078 1329
1079 preedit_rect->x = needed_rect->width + vtx; 1330 preedit_rect->x = needed_rect->width + vtx;
1080 preedit_rect->y = Height2Pixel(R->TermWin.nrow - 1) + mbh; 1331 preedit_rect->y = Height2Pixel (TermWin.nrow - 1) + mbh;
1081 1332
1082 preedit_rect->width = Width2Pixel(R->TermWin.ncol + 1) - needed_rect->width 1333 preedit_rect->width = Width2Pixel (TermWin.ncol + 1) - needed_rect->width + vtx;
1083 + vtx;
1084 preedit_rect->height = Height2Pixel(1); 1334 preedit_rect->height = Height2Pixel (1);
1085 1335
1086 status_rect->x = vtx; 1336 status_rect->x = vtx;
1087 status_rect->y = Height2Pixel(R->TermWin.nrow - 1) + mbh; 1337 status_rect->y = Height2Pixel (TermWin.nrow - 1) + mbh;
1088 1338
1089 status_rect->width = needed_rect->width ? needed_rect->width 1339 status_rect->width = needed_rect->width ? needed_rect->width : Width2Pixel (TermWin.ncol + 1);
1090 : Width2Pixel(R->TermWin.ncol + 1);
1091 status_rect->height = Height2Pixel(1); 1340 status_rect->height = Height2Pixel (1);
1092} 1341}
1093 1342
1094/* ARGSUSED */
1095/* INTPROTO */
1096void 1343void
1097rxvt_IMDestroyCallback(XIM xim __attribute__ ((unused)), XPointer client_data 1344rxvt_term::im_destroy ()
1098 __attribute__ ((unused)), XPointer call_data
1099 __attribute__ ((unused)))
1100{ 1345{
1101 dR; 1346 if (Input_Context)
1102 1347 {
1348 XDestroyIC (Input_Context);
1103 R->Input_Context = NULL; 1349 Input_Context = NULL;
1104/* To avoid Segmentation Fault in C locale: Solaris only? */ 1350 }
1105 if (STRCMP(R->locale, "C")) 1351
1106 XRegisterIMInstantiateCallback(R->Xdisplay, NULL, NULL, NULL, 1352 if (input_method)
1107 rxvt_IMInstantiateCallback, NULL); 1353 {
1354 display->put_xim (input_method);
1355 input_method = 0;
1356 }
1108} 1357}
1109 1358
1110/* 1359/*
1111 * X manual pages and include files don't match on some systems: 1360 * Try to open a XIM with the current modifiers, then see if we can
1112 * some think this is an XIDProc and others an XIMProc so we can't 1361 * open a suitable preedit type
1113 * use the first argument - need to update this to be nice for
1114 * both types via some sort of configure detection
1115 */ 1362 */
1116/* ARGSUSED */ 1363bool
1117/* EXTPROTO */ 1364rxvt_term::IM_get_IC (const char *modifiers)
1118void
1119rxvt_IMInstantiateCallback(Display * unused
1120 __attribute__ ((unused)), XPointer client_data
1121 __attribute__ ((unused)), XPointer call_data
1122 __attribute__ ((unused)))
1123{ 1365{
1124 dR; 1366 int i, j, found;
1367 XIM xim;
1368 XPoint spot;
1369 XRectangle rect, status_rect, needed_rect;
1370 unsigned long fg, bg;
1371 const char *p;
1372 char **s;
1373 XIMStyles *xim_styles;
1374 XVaNestedList preedit_attr, status_attr;
1375
1376 if (! ((p = XSetLocaleModifiers (modifiers)) && *p))
1377 return false;
1378
1379 D_MAIN ((stderr, "rxvt_IM_get_IC ()"));
1380 input_method = display->get_xim (locale, modifiers);
1381 if (input_method == NULL)
1382 return false;
1383
1384 xim = input_method->xim;
1385
1386 xim_styles = NULL;
1387 if (XGetIMValues (xim, XNQueryInputStyle, &xim_styles, NULL)
1388 || !xim_styles || !xim_styles->count_styles)
1389 {
1390 display->put_xim (input_method);
1391 return false;
1392 }
1393
1394 p = rs[Rs_preeditType] ? rs[Rs_preeditType] : "OverTheSpot,OffTheSpot,Root";
1395 s = rxvt_splitcommastring (p);
1396 for (i = found = 0; !found && s[i]; i++)
1397 {
1398 if (!STRCMP (s[i], "OverTheSpot"))
1399 input_style = (XIMPreeditPosition | XIMStatusNothing);
1400 else if (!STRCMP (s[i], "OffTheSpot"))
1401 input_style = (XIMPreeditArea | XIMStatusArea);
1402 else if (!STRCMP (s[i], "Root"))
1403 input_style = (XIMPreeditNothing | XIMStatusNothing);
1404
1405 for (j = 0; j < xim_styles->count_styles; j++)
1406 if (input_style == xim_styles->supported_styles[j])
1407 {
1408 found = 1;
1409 break;
1410 }
1411 }
1412
1413 for (i = 0; s[i]; i++)
1414 free (s[i]);
1415
1416 free (s);
1417 XFree (xim_styles);
1418
1419 if (!found)
1420 {
1421 display->put_xim (input_method);
1422 return false;
1423 }
1424
1425 preedit_attr = status_attr = NULL;
1426
1427 if (input_style & XIMPreeditPosition)
1428 {
1429 im_set_size (&rect);
1430 im_set_position (&spot);
1431 im_set_color (&fg, &bg);
1432
1433 preedit_attr = XVaCreateNestedList (0, XNArea, &rect,
1434 XNSpotLocation, &spot,
1435 XNForeground, fg, XNBackground, bg,
1436 //XNFontSet, TermWin.fontset,
1437 NULL);
1438 }
1439 else if (input_style & XIMPreeditArea)
1440 {
1441 im_set_color (&fg, &bg);
1442
1443 /*
1444 * The necessary width of preedit area is unknown
1445 * until create input context.
1446 */
1447 needed_rect.width = 0;
1448
1449 im_set_preedit_area (&rect, &status_rect, &needed_rect);
1450
1451 preedit_attr = XVaCreateNestedList (0, XNArea, &rect,
1452 XNForeground, fg, XNBackground, bg,
1453 //XNFontSet, TermWin.fontset,
1454 NULL);
1455 status_attr = XVaCreateNestedList (0, XNArea, &status_rect,
1456 XNForeground, fg, XNBackground, bg,
1457 //XNFontSet, TermWin.fontset,
1458 NULL);
1459 }
1460
1461 Input_Context = XCreateIC (xim, XNInputStyle, input_style,
1462 XNClientWindow, TermWin.parent[0],
1463 XNFocusWindow, TermWin.parent[0],
1464 preedit_attr ? XNPreeditAttributes : NULL,
1465 preedit_attr,
1466 status_attr ? XNStatusAttributes : NULL,
1467 status_attr, NULL);
1468 if (preedit_attr) XFree (preedit_attr);
1469 if (status_attr) XFree (status_attr);
1470
1471 if (Input_Context == NULL)
1472 {
1473 rxvt_warn ("failed to create input context, continuing without XIM.\n");
1474 display->put_xim (input_method);
1475 return false;
1476 }
1477
1478 if (input_style & XIMPreeditArea)
1479 IMSetStatusPosition ();
1480
1481 D_MAIN ((stderr, "rxvt_IM_get_IC () - successful connection"));
1482 return true;
1483}
1484
1485void
1486rxvt_term::im_cb ()
1487{
1125 int i, found, had_im; 1488 int i, found, had_im;
1126 const char *p; 1489 const char *p;
1127 char **s; 1490 char **s;
1128 char buf[IMBUFSIZ]; 1491 char buf[IMBUFSIZ];
1129 1492
1493 im_destroy ();
1494
1130 D_MAIN((stderr, "rxvt_IMInstantiateCallback()")); 1495 D_MAIN ((stderr, "rxvt_IMInstantiateCallback ()"));
1131 if (R->Input_Context) 1496 if (Input_Context)
1132 return; 1497 return;
1133 1498
1499#if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1500 if (rs[Rs_imLocale])
1501 SET_LOCALE (rs[Rs_imLocale]);
1502#endif
1503
1134 p = R->rs[Rs_inputMethod]; 1504 p = rs[Rs_inputMethod];
1135 if (p && *p) 1505 if (p && *p)
1136 { 1506 {
1137 bool found = false; 1507 bool found = false;
1138 1508
1139 s = rxvt_splitcommastring (p); 1509 s = rxvt_splitcommastring (p);
1141 { 1511 {
1142 if (*s[i]) 1512 if (*s[i])
1143 { 1513 {
1144 STRCPY (buf, "@im="); 1514 STRCPY (buf, "@im=");
1145 STRNCAT (buf, s[i], IMBUFSIZ - 5); 1515 STRNCAT (buf, s[i], IMBUFSIZ - 5);
1146 if ((p = XSetLocaleModifiers (buf)) && *p 1516 if (IM_get_IC (buf))
1147 && rxvt_IM_get_IC (aR))
1148 { 1517 {
1149 found = true; 1518 found = true;
1150 break; 1519 break;
1151 } 1520 }
1152 } 1521 }
1153 } 1522 }
1154 for (i = 0; s[i]; i++) 1523 for (i = 0; s[i]; i++)
1155 free(s[i]); 1524 free (s[i]);
1156 free(s); 1525 free (s);
1157 1526
1158 if (found) 1527 if (found)
1159 return; 1528 goto done;
1160 } 1529 }
1161 1530
1162/* try with XMODIFIERS env. var. */ 1531 /* try with XMODIFIERS env. var. */
1163 if ((p = XSetLocaleModifiers ("")) && *p 1532 if (IM_get_IC (""))
1164 && rxvt_IM_get_IC (aR)) 1533 goto done;
1534
1535 /* try with no modifiers base IF the user didn't specify an IM */
1536 if (IM_get_IC ("@im=none"))
1537 goto done;
1538
1539done:
1540#if defined(HAVE_XSETLOCALE) || defined(HAVE_SETLOCALE)
1541 if (rs[Rs_imLocale])
1542 SET_LOCALE (locale);
1543#endif
1544}
1545
1546void
1547rxvt_term::IMSetStatusPosition ()
1548{
1549 XRectangle preedit_rect, status_rect, *needed_rect;
1550 XVaNestedList preedit_attr, status_attr;
1551
1552 if (Input_Context == NULL
1553 || !TermWin.focus || ! (input_style & XIMPreeditArea)
1554 || !IMisRunning ())
1165 return; 1555 return;
1166 1556
1167/* try with no modifiers base IF the user didn't specify an IM */
1168 if ((p = XSetLocaleModifiers ("@im=none")) && *p
1169 && rxvt_IM_get_IC (aR) == True)
1170 return;
1171}
1172
1173/*
1174 * Try to open a XIM with the current modifiers, then see if we can
1175 * open a suitable preedit type
1176 */
1177/* INTPROTO */
1178Bool
1179rxvt_IM_get_IC(pR)
1180{
1181 int i, j, found;
1182 XIM xim;
1183 XPoint spot;
1184 XRectangle rect, status_rect, needed_rect;
1185 unsigned long fg, bg;
1186 const char *p;
1187 char **s;
1188 XIMStyles *xim_styles;
1189 XVaNestedList preedit_attr, status_attr;
1190 XIMCallback ximcallback;
1191
1192 D_MAIN((stderr, "rxvt_IM_get_IC()"));
1193 xim = XOpenIM(R->Xdisplay, NULL, NULL, NULL);
1194 if (xim == NULL)
1195 return False;
1196
1197 xim_styles = NULL;
1198 if (XGetIMValues(xim, XNQueryInputStyle, &xim_styles, NULL)
1199 || !xim_styles || !xim_styles->count_styles) {
1200 XCloseIM(xim);
1201 return False;
1202 }
1203
1204 p = R->rs[Rs_preeditType] ? R->rs[Rs_preeditType]
1205 : "OverTheSpot,OffTheSpot,Root";
1206 s = rxvt_splitcommastring(p);
1207 for (i = found = 0; !found && s[i]; i++) {
1208 if (!STRCMP(s[i], "OverTheSpot"))
1209 R->input_style = (XIMPreeditPosition | XIMStatusNothing);
1210 else if (!STRCMP(s[i], "OffTheSpot"))
1211 R->input_style = (XIMPreeditArea | XIMStatusArea);
1212 else if (!STRCMP(s[i], "Root"))
1213 R->input_style = (XIMPreeditNothing | XIMStatusNothing);
1214
1215 for (j = 0; j < xim_styles->count_styles; j++)
1216 if (R->input_style == xim_styles->supported_styles[j]) {
1217 found = 1;
1218 break;
1219 }
1220 }
1221 for (i = 0; s[i]; i++)
1222 free(s[i]);
1223 free(s);
1224 XFree(xim_styles);
1225
1226 if (!found) {
1227 XCloseIM(xim);
1228 return False;
1229 }
1230
1231 ximcallback.callback = rxvt_IMDestroyCallback;
1232
1233/* XXX: not sure why we need this (as well as IC one below) */
1234 XSetIMValues(xim, XNDestroyCallback, &ximcallback, NULL);
1235
1236 preedit_attr = status_attr = NULL;
1237
1238 if (R->input_style & XIMPreeditPosition) {
1239 rxvt_setSize(aR_ & rect);
1240 rxvt_setPosition(aR_ & spot);
1241 rxvt_setColor(aR_ & fg, &bg);
1242
1243 preedit_attr = XVaCreateNestedList(0, XNArea, &rect,
1244 XNSpotLocation, &spot,
1245 XNForeground, fg, XNBackground, bg,
1246 //XNFontSet, R->TermWin.fontset,
1247 NULL);
1248 } else if (R->input_style & XIMPreeditArea) {
1249 rxvt_setColor(aR_ & fg, &bg);
1250
1251 /*
1252 * The necessary width of preedit area is unknown
1253 * until create input context.
1254 */
1255 needed_rect.width = 0;
1256
1257 rxvt_setPreeditArea(aR_ & rect, &status_rect, &needed_rect);
1258
1259 preedit_attr = XVaCreateNestedList(0, XNArea, &rect,
1260 XNForeground, fg, XNBackground, bg,
1261 //XNFontSet, R->TermWin.fontset,
1262 NULL);
1263 status_attr = XVaCreateNestedList(0, XNArea, &status_rect,
1264 XNForeground, fg, XNBackground, bg,
1265 //XNFontSet, R->TermWin.fontset,
1266 NULL);
1267 }
1268 R->Input_Context = XCreateIC(xim, XNInputStyle, R->input_style,
1269 XNClientWindow, R->TermWin.parent[0],
1270 XNFocusWindow, R->TermWin.parent[0],
1271 XNDestroyCallback, &ximcallback,
1272 preedit_attr ? XNPreeditAttributes : NULL,
1273 preedit_attr,
1274 status_attr ? XNStatusAttributes : NULL,
1275 status_attr, NULL);
1276 if (preedit_attr)
1277 XFree(preedit_attr);
1278 if (status_attr)
1279 XFree(status_attr);
1280 if (R->Input_Context == NULL) {
1281 rxvt_print_error("failed to create input context");
1282 XCloseIM(xim);
1283 return False;
1284 }
1285 if (R->input_style & XIMPreeditArea)
1286 rxvt_IMSetStatusPosition(aR);
1287 D_MAIN((stderr, "rxvt_IM_get_IC() - successful connection"));
1288 return True;
1289}
1290
1291/* EXTPROTO */
1292void
1293rxvt_IMSetStatusPosition(pR)
1294{
1295 XRectangle preedit_rect, status_rect, *needed_rect;
1296 XVaNestedList preedit_attr, status_attr;
1297
1298 if (R->Input_Context == NULL
1299 || !R->TermWin.focus || !(R->input_style & XIMPreeditArea)
1300 || !rxvt_IMisRunning(aR))
1301 return;
1302
1303/* Getting the necessary width of preedit area */ 1557 /* Getting the necessary width of preedit area */
1304 status_attr = XVaCreateNestedList(0, XNAreaNeeded, &needed_rect, NULL); 1558 status_attr = XVaCreateNestedList (0, XNAreaNeeded, &needed_rect, NULL);
1305 XGetICValues(R->Input_Context, XNStatusAttributes, status_attr, NULL); 1559 XGetICValues (Input_Context, XNStatusAttributes, status_attr, NULL);
1306 XFree(status_attr); 1560 XFree (status_attr);
1307 1561
1308 rxvt_setPreeditArea(aR_ & preedit_rect, &status_rect, needed_rect); 1562 im_set_preedit_area (&preedit_rect, &status_rect, needed_rect);
1309 1563
1310 preedit_attr = XVaCreateNestedList(0, XNArea, &preedit_rect, NULL); 1564 preedit_attr = XVaCreateNestedList (0, XNArea, &preedit_rect, NULL);
1311 status_attr = XVaCreateNestedList(0, XNArea, &status_rect, NULL); 1565 status_attr = XVaCreateNestedList (0, XNArea, &status_rect, NULL);
1312 1566
1313 XSetICValues(R->Input_Context, 1567 XSetICValues (Input_Context,
1314 XNPreeditAttributes, preedit_attr, 1568 XNPreeditAttributes, preedit_attr,
1315 XNStatusAttributes, status_attr, NULL); 1569 XNStatusAttributes, status_attr, NULL);
1316 1570
1317 XFree(preedit_attr); 1571 XFree (preedit_attr);
1318 XFree(status_attr); 1572 XFree (status_attr);
1319} 1573}
1320#endif /* USE_XIM */ 1574#endif /* USE_XIM */
1321 1575
1322/*----------------------------------------------------------------------*/ 1576/*----------------------------------------------------------------------*/
1323rxvt_t rxvt_current_term; 1577rxvt_t rxvt_current_term;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines