ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/main.C
Revision: 1.132
Committed: Thu Feb 3 07:18:30 2005 UTC (19 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-4_9
Changes since 1.128: +10 -3 lines
Log Message:
*** empty log message ***

File Contents

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