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.29 by pcg, Wed Feb 11 08:13:45 2004 UTC vs.
Revision 1.118 by root, Thu Dec 9 06:20:41 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines