ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.67
Committed: Sun Jan 15 06:02:41 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
Changes since 1.66: +36 -29 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*----------------------------------------------------------------------*
2     * File: rxvtperl.xs
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6 root 1.31 * Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com>
7 root 1.1 *
8     * This program is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License as published by
10     * the Free Software Foundation; either version 2 of the License, or
11     * (at your option) any later version.
12     *
13     * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     * GNU General Public License for more details.
17     *
18     * You should have received a copy of the GNU General Public License
19     * along with this program; if not, write to the Free Software
20     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21     *----------------------------------------------------------------------*/
22    
23     #define line_t perl_line_t
24     #include <EXTERN.h>
25     #include <perl.h>
26     #include <XSUB.h>
27     #undef line_t
28    
29     #include "../config.h"
30    
31 root 1.38 #include <cstddef>
32 root 1.1 #include <cstdarg>
33    
34 root 1.59 #include "unistd.h"
35    
36 root 1.50 #include "iom.h"
37 root 1.1 #include "rxvt.h"
38 root 1.50 #include "keyboard.h"
39 root 1.1 #include "rxvtutil.h"
40     #include "rxvtperl.h"
41    
42     #include "perlxsi.c"
43    
44 root 1.41 #if defined(HAVE_SCROLLBARS) || defined(MENUBAR)
45     # define GRAB_CURSOR THIS->leftptr_cursor
46     #else
47     # define GRAB_CURSOR None
48     #endif
49    
50 root 1.8 #undef LINENO
51     #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
52     #undef ROW
53     #define ROW(n) THIS->row_buf [LINENO (n)]
54    
55 root 1.66 #define ENABLE_PERL_FRILLS 1
56 root 1.64
57 root 1.1 /////////////////////////////////////////////////////////////////////////////
58    
59 root 1.42 static SV *
60     taint (SV *sv)
61     {
62     SvTAINT (sv);
63     return sv;
64     }
65    
66     static SV *
67     taint_if (SV *sv, SV *src)
68     {
69     if (SvTAINTED (src))
70     SvTAINT (sv);
71    
72     return sv;
73     }
74    
75 root 1.1 static wchar_t *
76     sv2wcs (SV *sv)
77     {
78     STRLEN len;
79     char *str = SvPVutf8 (sv, len);
80     return rxvt_utf8towcs (str, len);
81     }
82    
83     static SV *
84 root 1.25 wcs2sv (wchar_t *wstr, int len = -1)
85     {
86     char *str = rxvt_wcstoutf8 (wstr, len);
87    
88     SV *sv = newSVpv (str, 0);
89     SvUTF8_on (sv);
90     free (str);
91    
92     return sv;
93     }
94    
95     static SV *
96 root 1.1 new_ref (HV *hv, const char *klass)
97     {
98     return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
99     }
100    
101     //TODO: use magic
102     static SV *
103     newSVptr (void *ptr, const char *klass)
104     {
105     HV *hv = newHV ();
106 root 1.18 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
107 root 1.1 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
108     }
109    
110 root 1.18 static void
111     clearSVptr (SV *sv)
112     {
113     if (SvROK (sv))
114     sv = SvRV (sv);
115    
116     hv_clear ((HV *)sv);
117     sv_unmagic (sv, PERL_MAGIC_ext);
118     }
119    
120 root 1.1 static long
121     SvPTR (SV *sv, const char *klass)
122     {
123     if (!sv_derived_from (sv, klass))
124     croak ("object of type %s expected", klass);
125    
126 root 1.18 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
127 root 1.1
128 root 1.18 if (!mg)
129 root 1.1 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
130    
131 root 1.18 return (long)mg->mg_ptr;
132 root 1.1 }
133    
134 root 1.36 #define newSVterm(term) SvREFCNT_inc ((SV *)term->perl.self)
135 root 1.1 #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
136    
137     /////////////////////////////////////////////////////////////////////////////
138    
139     struct perl_watcher
140     {
141     SV *cbsv;
142     HV *self;
143    
144     perl_watcher ()
145     : cbsv (newSV (0))
146     {
147     }
148    
149     ~perl_watcher ()
150     {
151     SvREFCNT_dec (cbsv);
152     }
153    
154     void cb (SV *cb)
155     {
156     sv_setsv (cbsv, cb);
157     }
158    
159     void invoke (const char *type, SV *self, int arg = -1);
160     };
161    
162     void
163     perl_watcher::invoke (const char *type, SV *self, int arg)
164     {
165     dSP;
166    
167     ENTER;
168     SAVETMPS;
169    
170     PUSHMARK (SP);
171    
172     XPUSHs (sv_2mortal (self));
173    
174     if (arg >= 0)
175     XPUSHs (sv_2mortal (newSViv (arg)));
176    
177     PUTBACK;
178     call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
179     SPAGAIN;
180    
181     PUTBACK;
182     FREETMPS;
183     LEAVE;
184    
185     if (SvTRUE (ERRSV))
186     rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
187     }
188    
189     #define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
190     #define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
191    
192     struct timer : time_watcher, perl_watcher
193     {
194 root 1.13 tstamp interval;
195    
196 root 1.1 timer ()
197     : time_watcher (this, &timer::execute)
198     {
199     }
200    
201     void execute (time_watcher &w)
202     {
203 root 1.13 if (interval)
204     start (at + interval);
205    
206 root 1.1 invoke ("urxvt::timer", newSVtimer (this));
207     }
208     };
209    
210     #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
211     #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
212    
213     struct iow : io_watcher, perl_watcher
214     {
215     iow ()
216     : io_watcher (this, &iow::execute)
217     {
218     }
219    
220     void execute (io_watcher &w, short revents)
221     {
222     invoke ("urxvt::iow", newSViow (this), revents);
223     }
224     };
225    
226     /////////////////////////////////////////////////////////////////////////////
227    
228 root 1.13 #define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
229    
230     struct overlay {
231     HV *self;
232     rxvt_term *THIS;
233     int x, y, w, h;
234     int border;
235     text_t **text;
236     rend_t **rend;
237    
238     overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
239     ~overlay ();
240    
241 root 1.18 void show ();
242     void hide ();
243    
244 root 1.13 void swap ();
245    
246     void set (int x, int y, SV *str, SV *rend);
247     };
248    
249     overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
250     : THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2)
251     {
252     if (border == 2)
253     {
254     w += 2;
255     h += 2;
256     }
257    
258     text = new text_t *[h];
259     rend = new rend_t *[h];
260 root 1.24
261 root 1.13 for (int y = 0; y < h; y++)
262 root 1.24 {
263 root 1.13 text_t *tp = text[y] = new text_t[w];
264     rend_t *rp = rend[y] = new rend_t[w];
265 root 1.24
266 root 1.13 text_t t0, t1, t2;
267     rend_t r = rstyle;
268 root 1.24
269 root 1.13 if (border == 2)
270     {
271     if (y == 0)
272     t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
273     else if (y < h - 1)
274     t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
275     else
276     t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
277 root 1.24
278     *tp++ = t0;
279 root 1.13 *rp++ = r;
280 root 1.24
281 root 1.13 for (int x = w - 2; x-- > 0; )
282     {
283     *tp++ = t1;
284     *rp++ = r;
285 root 1.24 }
286 root 1.13
287     *tp = t2;
288 root 1.24 *rp = r;
289 root 1.13 }
290     else
291     for (int x = w; x-- > 0; )
292     {
293     *tp++ = 0x0020;
294     *rp++ = r;
295 root 1.24 }
296     }
297 root 1.13
298 root 1.18 show ();
299 root 1.13 THIS->want_refresh = 1;
300     }
301    
302     overlay::~overlay ()
303     {
304 root 1.18 hide ();
305    
306 root 1.13 for (int y = h; y--; )
307     {
308     delete [] text[y];
309     delete [] rend[y];
310     }
311    
312     delete [] text;
313     delete [] rend;
314    
315     THIS->want_refresh = 1;
316     }
317    
318 root 1.18 void
319     overlay::show ()
320     {
321     char key[33]; sprintf (key, "%32lx", (long)this);
322    
323 root 1.36 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0));
324 root 1.18 hv_store (hv, key, 32, newSViv ((long)this), 0);
325     }
326    
327     void
328     overlay::hide ()
329     {
330 root 1.36 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0);
331 root 1.18
332     if (ovs)
333     {
334     char key[33]; sprintf (key, "%32lx", (long)this);
335    
336     HV *hv = (HV *)SvRV (*ovs);
337     hv_delete (hv, key, 32, G_DISCARD);
338     }
339     }
340    
341 root 1.13 void overlay::swap ()
342     {
343     int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
344     int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
345    
346     int ov_w = min (w, THIS->ncol - ov_x);
347     int ov_h = min (h, THIS->nrow - ov_y);
348    
349     for (int y = ov_h; y--; )
350     {
351     text_t *t1 = text [y];
352     rend_t *r1 = rend [y];
353 root 1.24
354 root 1.67 text_t *t2 = ROW(y + ov_y + THIS->view_start).t + ov_x;
355     rend_t *r2 = ROW(y + ov_y + THIS->view_start).r + ov_x;
356 root 1.13
357     for (int x = ov_w; x--; )
358 root 1.24 {
359 root 1.13 text_t t = *t1; *t1++ = *t2; *t2++ = t;
360     rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
361     }
362     }
363    
364     }
365    
366 root 1.14 void overlay::set (int x, int y, SV *text, SV *rend)
367 root 1.13 {
368     x += border;
369     y += border;
370    
371     if (!IN_RANGE_EXC (y, 0, h - border))
372     return;
373    
374 root 1.14 wchar_t *wtext = sv2wcs (text);
375 root 1.13
376 root 1.14 for (int col = min (wcslen (wtext), w - x - border); col--; )
377     this->text [y][x + col] = wtext [col];
378 root 1.24
379 root 1.14 free (wtext);
380    
381     if (rend)
382     {
383     if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
384     croak ("rend must be arrayref");
385    
386     AV *av = (AV *)SvRV (rend);
387    
388     for (int col = min (av_len (av) + 1, w - x - border); col--; )
389     this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
390     }
391 root 1.13
392     THIS->want_refresh = 1;
393     }
394    
395    
396     /////////////////////////////////////////////////////////////////////////////
397    
398 root 1.1 struct rxvt_perl_interp rxvt_perl;
399    
400     static PerlInterpreter *perl;
401    
402     rxvt_perl_interp::~rxvt_perl_interp ()
403     {
404     if (perl)
405     {
406     perl_destruct (perl);
407     perl_free (perl);
408     }
409     }
410    
411     void
412 root 1.43 rxvt_perl_interp::init ()
413 root 1.1 {
414     if (!perl)
415     {
416 root 1.57 perl_environ = rxvt_environ;
417     swap (perl_environ, environ);
418 root 1.56
419 root 1.1 char *argv[] = {
420     "",
421 root 1.43 "-T",
422 root 1.1 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
423     };
424    
425     perl = perl_alloc ();
426     perl_construct (perl);
427    
428 root 1.43 if (perl_parse (perl, xs_init, 3, argv, (char **)NULL)
429 root 1.1 || perl_run (perl))
430     {
431     rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
432    
433     perl_destruct (perl);
434     perl_free (perl);
435     perl = 0;
436     }
437 root 1.57
438     swap (perl_environ, environ);
439 root 1.1 }
440     }
441    
442 root 1.45 static void
443     ungrab (rxvt_term *THIS)
444     {
445     if (THIS->perl.grabtime)
446     {
447     XUngrabKeyboard (THIS->display->display, THIS->perl.grabtime);
448     XUngrabPointer (THIS->display->display, THIS->perl.grabtime);
449     THIS->perl.grabtime = 0;
450     }
451     }
452    
453 root 1.67 static void
454     swap_overlays (rxvt_term *term)
455     {
456     HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
457    
458     if (HvKEYS (hv))
459     {
460     hv_iterinit (hv);
461    
462     while (HE *he = hv_iternext (hv))
463     ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
464     }
465     }
466    
467 root 1.1 bool
468     rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
469     {
470 root 1.13 if (!perl)
471 root 1.1 return false;
472 root 1.24
473 root 1.1 if (htype == HOOK_INIT) // first hook ever called
474 root 1.13 {
475 root 1.36 term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term");
476     hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
477 root 1.13 }
478 root 1.36 else if (!term->perl.self)
479 root 1.17 return false; // perl not initialized for this instance
480 root 1.13 else if (htype == HOOK_DESTROY)
481     {
482     // handled later
483     }
484 root 1.50 else
485 root 1.13 {
486 root 1.67 if (htype == HOOK_REFRESH_END)
487     swap_overlays (term);
488    
489     if (!term->perl.should_invoke [htype])
490 root 1.50 {
491 root 1.67 if (htype == HOOK_REFRESH_BEGIN)
492     swap_overlays (term);
493 root 1.13
494 root 1.67 return false;
495 root 1.13 }
496     }
497 root 1.1
498 root 1.57 swap (perl_environ, environ);
499 root 1.56
500 root 1.57 try
501     {
502     dSP;
503     va_list ap;
504 root 1.1
505 root 1.57 va_start (ap, htype);
506 root 1.1
507 root 1.57 ENTER;
508     SAVETMPS;
509 root 1.1
510 root 1.57 PUSHMARK (SP);
511 root 1.1
512 root 1.57 XPUSHs (sv_2mortal (newSVterm (term)));
513     XPUSHs (sv_2mortal (newSViv (htype)));
514 root 1.1
515 root 1.57 for (;;) {
516     data_type dt = (data_type)va_arg (ap, int);
517 root 1.1
518 root 1.57 switch (dt)
519     {
520     case DT_INT:
521     XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
522     break;
523    
524     case DT_LONG:
525     XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
526     break;
527    
528     case DT_STR:
529     XPUSHs (taint (sv_2mortal (newSVpv (va_arg (ap, char *), 0))));
530     break;
531 root 1.1
532 root 1.57 case DT_STR_LEN:
533     {
534     char *str = va_arg (ap, char *);
535     int len = va_arg (ap, int);
536 root 1.1
537 root 1.57 XPUSHs (taint (sv_2mortal (newSVpvn (str, len))));
538     }
539     break;
540 root 1.6
541 root 1.57 case DT_WCS_LEN:
542     {
543     wchar_t *wstr = va_arg (ap, wchar_t *);
544     int wlen = va_arg (ap, int);
545 root 1.29
546 root 1.57 XPUSHs (taint (sv_2mortal (wcs2sv (wstr, wlen))));
547     }
548     break;
549 root 1.29
550 root 1.57 case DT_XEVENT:
551     {
552     XEvent *xe = va_arg (ap, XEvent *);
553     HV *hv = newHV ();
554 root 1.29
555     # define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
556     # define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
557     # undef set
558    
559 root 1.57 setiv (type, xe->type);
560     setiv (send_event, xe->xany.send_event);
561     setiv (serial, xe->xany.serial);
562    
563     switch (xe->type)
564     {
565     case KeyPress:
566     case KeyRelease:
567     case ButtonPress:
568     case ButtonRelease:
569     case MotionNotify:
570     setiv (time, xe->xmotion.time);
571     setiv (x, xe->xmotion.x);
572     setiv (y, xe->xmotion.y);
573     setiv (row, xe->xmotion.y / term->fheight);
574     setiv (col, xe->xmotion.x / term->fwidth);
575     setiv (x_root, xe->xmotion.x_root);
576     setiv (y_root, xe->xmotion.y_root);
577     setiv (state, xe->xmotion.state);
578     break;
579     }
580    
581     switch (xe->type)
582     {
583     case KeyPress:
584     case KeyRelease:
585     setiv (keycode, xe->xkey.keycode);
586     break;
587    
588     case ButtonPress:
589     case ButtonRelease:
590     setiv (button, xe->xbutton.button);
591     break;
592    
593     case MotionNotify:
594     setiv (is_hint, xe->xmotion.is_hint);
595     break;
596     }
597 root 1.29
598 root 1.57 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
599 root 1.29 }
600 root 1.57 break;
601 root 1.29
602 root 1.57 case DT_END:
603 root 1.29 {
604 root 1.57 va_end (ap);
605 root 1.29
606 root 1.57 PUTBACK;
607     int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
608     SPAGAIN;
609    
610     if (count)
611     {
612     SV *status = POPs;
613     count = SvTRUE (status);
614     }
615    
616     PUTBACK;
617     FREETMPS;
618     LEAVE;
619    
620     if (SvTRUE (ERRSV))
621     {
622     rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
623     ungrab (term); // better lose the grab than the session
624     }
625    
626 root 1.67 if (htype == HOOK_REFRESH_BEGIN)
627     swap_overlays (term);
628     else if (htype == HOOK_DESTROY)
629 root 1.57 {
630     clearSVptr ((SV *)term->perl.self);
631     SvREFCNT_dec ((SV *)term->perl.self);
632     }
633 root 1.29
634 root 1.57 swap (perl_environ, environ);
635     return count;
636 root 1.29 }
637    
638 root 1.57 default:
639     rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
640 root 1.29 }
641 root 1.1 }
642 root 1.57 }
643     catch (...)
644     {
645     swap (perl_environ, environ);
646     throw;
647     }
648 root 1.1 }
649    
650     /////////////////////////////////////////////////////////////////////////////
651    
652     MODULE = urxvt PACKAGE = urxvt
653    
654     PROTOTYPES: ENABLE
655    
656     BOOT:
657     {
658 root 1.52 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
659     sv_setsv (get_sv ("urxvt::RESNAME", 1), newSVpvn (RESNAME, sizeof (RESNAME) - 1));
660     sv_setsv (get_sv ("urxvt::RESCLASS", 1), newSVpvn (RESCLASS, sizeof (RESCLASS) - 1));
661     sv_setsv (get_sv ("urxvt::RXVTNAME", 1), newSVpvn (RXVTNAME, sizeof (RXVTNAME) - 1));
662 root 1.37
663 root 1.1 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
664 root 1.21 # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
665     # include "hookinc.h"
666     # undef def
667 root 1.1
668 root 1.39 HV *option = get_hv ("urxvt::OPTION", 1);
669     # define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0);
670     # define nodef(name)
671     # include "optinc.h"
672     # undef nodef
673     # undef def
674    
675     HV *stash = gv_stashpv ("urxvt", 1);
676 root 1.62 struct {
677     const char *name;
678     IV iv;
679     } *civ, const_iv[] = {
680     # define const_iv(name) { # name, (IV)name }
681     const_iv (DEFAULT_RSTYLE),
682     const_iv (OVERLAY_RSTYLE),
683     const_iv (RS_Bold),
684     const_iv (RS_Italic),
685     const_iv (RS_Blink),
686     const_iv (RS_RVid),
687     const_iv (RS_Uline),
688    
689     const_iv (CurrentTime),
690     const_iv (ShiftMask),
691     const_iv (LockMask),
692     const_iv (ControlMask),
693     const_iv (Mod1Mask),
694     const_iv (Mod2Mask),
695     const_iv (Mod3Mask),
696     const_iv (Mod4Mask),
697     const_iv (Mod5Mask),
698     const_iv (Button1Mask),
699     const_iv (Button2Mask),
700     const_iv (Button3Mask),
701     const_iv (Button4Mask),
702     const_iv (Button5Mask),
703     const_iv (AnyModifier),
704    
705     const_iv (EVENT_NONE),
706     const_iv (EVENT_READ),
707     const_iv (EVENT_WRITE),
708    
709     const_iv (NoEventMask),
710     const_iv (KeyPressMask),
711     const_iv (KeyReleaseMask),
712     const_iv (ButtonPressMask),
713     const_iv (ButtonReleaseMask),
714     const_iv (EnterWindowMask),
715     const_iv (LeaveWindowMask),
716     const_iv (PointerMotionMask),
717     const_iv (PointerMotionHintMask),
718     const_iv (Button1MotionMask),
719     const_iv (Button2MotionMask),
720     const_iv (Button3MotionMask),
721     const_iv (Button4MotionMask),
722     const_iv (Button5MotionMask),
723     const_iv (ButtonMotionMask),
724     const_iv (KeymapStateMask),
725     const_iv (ExposureMask),
726     const_iv (VisibilityChangeMask),
727     const_iv (StructureNotifyMask),
728     const_iv (ResizeRedirectMask),
729     const_iv (SubstructureNotifyMask),
730     const_iv (SubstructureRedirectMask),
731     const_iv (FocusChangeMask),
732     const_iv (PropertyChangeMask),
733     const_iv (ColormapChangeMask),
734     const_iv (OwnerGrabButtonMask),
735    
736     const_iv (KeyPress),
737     const_iv (KeyRelease),
738     const_iv (ButtonPress),
739     const_iv (ButtonRelease),
740     const_iv (MotionNotify),
741     const_iv (EnterNotify),
742     const_iv (LeaveNotify),
743     const_iv (FocusIn),
744     const_iv (FocusOut),
745     const_iv (KeymapNotify),
746     const_iv (Expose),
747     const_iv (GraphicsExpose),
748     const_iv (NoExpose),
749     const_iv (VisibilityNotify),
750     const_iv (CreateNotify),
751     const_iv (DestroyNotify),
752     const_iv (UnmapNotify),
753     const_iv (MapNotify),
754     const_iv (MapRequest),
755     const_iv (ReparentNotify),
756     const_iv (ConfigureNotify),
757     const_iv (ConfigureRequest),
758     const_iv (GravityNotify),
759     const_iv (ResizeRequest),
760     const_iv (CirculateNotify),
761     const_iv (CirculateRequest),
762     const_iv (PropertyNotify),
763     const_iv (SelectionClear),
764     const_iv (SelectionRequest),
765     const_iv (SelectionNotify),
766     const_iv (ColormapNotify),
767     const_iv (ClientMessage),
768     const_iv (MappingNotify),
769     };
770    
771     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]);
772     civ-- > const_iv; )
773     newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
774 root 1.1 }
775    
776     void
777     warn (const char *msg)
778     CODE:
779     rxvt_warn ("%s", msg);
780    
781     void
782     fatal (const char *msg)
783     CODE:
784     rxvt_fatal ("%s", msg);
785    
786 root 1.45 SV *
787     untaint (SV *sv)
788     CODE:
789     RETVAL = newSVsv (sv);
790     SvTAINTED_off (RETVAL);
791     OUTPUT:
792     RETVAL
793    
794 root 1.59 void
795     _exit (int status)
796    
797 root 1.47 bool
798     safe ()
799     CODE:
800     RETVAL = !rxvt_tainted ();
801     OUTPUT:
802     RETVAL
803    
804 root 1.3 NV
805     NOW ()
806     CODE:
807     RETVAL = NOW;
808     OUTPUT:
809     RETVAL
810    
811 root 1.11 int
812     GET_BASEFG (int rend)
813     CODE:
814     RETVAL = GET_BASEFG (rend);
815     OUTPUT:
816     RETVAL
817    
818     int
819     GET_BASEBG (int rend)
820     CODE:
821     RETVAL = GET_BASEBG (rend);
822     OUTPUT:
823     RETVAL
824    
825     int
826 root 1.12 SET_FGCOLOR (int rend, int new_color)
827 root 1.11 CODE:
828 root 1.12 RETVAL = SET_FGCOLOR (rend, new_color);
829 root 1.11 OUTPUT:
830     RETVAL
831    
832     int
833 root 1.12 SET_BGCOLOR (int rend, int new_color)
834 root 1.11 CODE:
835 root 1.12 RETVAL = SET_BGCOLOR (rend, new_color);
836     OUTPUT:
837     RETVAL
838    
839     int
840     GET_CUSTOM (int rend)
841     CODE:
842     RETVAL = (rend && RS_customMask) >> RS_customShift;
843     OUTPUT:
844     RETVAL
845    
846     int
847     SET_CUSTOM (int rend, int new_value)
848     CODE:
849     {
850     if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
851     croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
852    
853     RETVAL = (rend & ~RS_customMask)
854     | ((new_value << RS_customShift) & RS_customMask);
855     }
856 root 1.11 OUTPUT:
857     RETVAL
858    
859 root 1.3 MODULE = urxvt PACKAGE = urxvt::term
860    
861 root 1.54 SV *
862     _new (...)
863     CODE:
864     {
865     if (items < 1 || !SvROK (ST (0)) || SvTYPE (SvRV (ST (0))) != SVt_PVAV)
866     croak ("first argument to urxvt::term->_new must be arrayref");
867    
868     rxvt_term *term = new rxvt_term;
869    
870     term->argv = new stringvec;
871     term->envv = new stringvec;
872    
873     for (int i = 1; i < items; i++)
874     term->argv->push_back (strdup (SvPVbyte_nolen (ST (i))));
875    
876     AV *envv = (AV *)SvRV (ST (0));
877     for (int i = av_len (envv) + 1; i--; )
878     term->envv->push_back (strdup (SvPVbyte_nolen (*av_fetch (envv, i, 1))));
879    
880     term->envv->push_back (0);
881    
882     bool success;
883    
884     try
885     {
886     success = term->init (term->argv->size (), term->argv->begin ());
887     }
888     catch (const class rxvt_failure_exception &e)
889     {
890     success = false;
891     }
892    
893     if (!success)
894     {
895     term->destroy ();
896     croak ("error while initializing new terminal instance");
897     }
898    
899     RETVAL = term && term->perl.self
900     ? newSVterm (term) : &PL_sv_undef;
901     }
902     OUTPUT:
903     RETVAL
904    
905 root 1.28 void
906     rxvt_term::destroy ()
907    
908 root 1.64 #if ENABLE_PERL_FRILLS
909    
910     void
911     rxvt_term::XListProperties (U32 window)
912     PPCODE:
913     {
914     int count;
915     Atom *props = XListProperties (THIS->display->display, (Window)window, &count);
916    
917     EXTEND (SP, count);
918     while (count--)
919     PUSHs (newSVuv ((U32)props [count]));
920    
921     XFree (props);
922     }
923    
924     void
925     rxvt_term::XGetWindowProperty (U32 window, U32 property)
926     PPCODE:
927     {
928     Atom type;
929     int format;
930     unsigned long nitems;
931     unsigned long bytes_after;
932     unsigned char *prop;
933     XGetWindowProperty (THIS->display->display, (Window)window, (Atom)property,
934     0, 1<<30, 0, AnyPropertyType,
935     &type, &format, &nitems, &bytes_after, &prop);
936     if (type != None)
937     {
938     EXTEND (SP, 3);
939     PUSHs (newSVuv ((U32)type));
940     PUSHs (newSViv (format));
941     PUSHs (newSVpvn ((char *)prop, nitems * format / 8));
942     XFree (prop);
943     }
944     }
945    
946     void
947     rxvt_term::XChangeWindowProperty (U32 window, U32 property, U32 type, int format, SV *data)
948     CODE:
949     {
950     STRLEN len;
951     char *data_ = SvPVbyte (data, len);
952    
953     XChangeProperty (THIS->display->display, (Window)window, (Atom)property,
954     type, format, PropModeReplace,
955     (unsigned char *)data, len * 8 / format);
956     }
957    
958     void
959     rxvt_term::XDeleteProperty (U32 window, U32 property)
960     CODE:
961     XDeleteProperty (THIS->display->display, (Window)window, (Atom)property);
962    
963     U32
964     rxvt_term::DefaultRootWindow ()
965     CODE:
966     RETVAL = (U32)THIS->display->root;
967     OUTPUT:
968     RETVAL
969    
970     U32
971     rxvt_term::XCreateSimpleWindow (U32 parent, int x, int y, unsigned int width, unsigned int height)
972     CODE:
973     RETVAL = XCreateSimpleWindow (THIS->display->display, (Window)parent,
974     x, y, width, height, 0,
975     THIS->pix_colors_focused[Color_border],
976     THIS->pix_colors_focused[Color_border]);
977     OUTPUT:
978     RETVAL
979    
980     void
981     rxvt_term::XReparentWindow (U32 window, U32 parent, int x = 0, int y = 0)
982     CODE:
983 root 1.66 XReparentWindow (THIS->display->display, (Window)window, (Window)parent, x, y);
984    
985     void
986     rxvt_term::XMapWindow (U32 window)
987     CODE:
988     XMapWindow (THIS->display->display, (Window)window);
989    
990     void
991     rxvt_term::XUnmapWindow (U32 window)
992     CODE:
993     XUnmapWindow (THIS->display->display, (Window)window);
994 root 1.64
995     #endif
996    
997 root 1.35 void
998 root 1.62 rxvt_term::set_should_invoke (int htype, int inc)
999     CODE:
1000     THIS->perl.should_invoke [htype] += inc;
1001    
1002     void
1003 root 1.36 rxvt_term::grab_button (int button, U32 modifiers)
1004     CODE:
1005     XGrabButton (THIS->display->display, button, modifiers, THIS->vt, 1,
1006     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
1007 root 1.41 GrabModeSync, GrabModeSync, None, GRAB_CURSOR);
1008 root 1.36
1009     bool
1010     rxvt_term::grab (U32 eventtime, int sync = 0)
1011 root 1.35 CODE:
1012     {
1013 root 1.36 int mode = sync ? GrabModeSync : GrabModeAsync;
1014    
1015     THIS->perl.grabtime = 0;
1016    
1017     if (!XGrabPointer (THIS->display->display, THIS->vt, 0,
1018     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
1019 root 1.41 mode, mode, None, GRAB_CURSOR, eventtime))
1020 root 1.36 if (!XGrabKeyboard (THIS->display->display, THIS->vt, 0, mode, mode, eventtime))
1021     THIS->perl.grabtime = eventtime;
1022     else
1023     XUngrabPointer (THIS->display->display, eventtime);
1024    
1025     RETVAL = !!THIS->perl.grabtime;
1026 root 1.35 }
1027 root 1.36 OUTPUT:
1028     RETVAL
1029    
1030     void
1031 root 1.44 rxvt_term::allow_events_async ()
1032 root 1.36 CODE:
1033 root 1.44 XAllowEvents (THIS->display->display, AsyncBoth, THIS->perl.grabtime);
1034 root 1.36
1035     void
1036 root 1.44 rxvt_term::allow_events_sync ()
1037 root 1.36 CODE:
1038 root 1.44 XAllowEvents (THIS->display->display, SyncBoth, THIS->perl.grabtime);
1039 root 1.36
1040     void
1041 root 1.44 rxvt_term::allow_events_replay ()
1042 root 1.36 CODE:
1043 root 1.44 XAllowEvents (THIS->display->display, ReplayPointer, THIS->perl.grabtime);
1044     XAllowEvents (THIS->display->display, ReplayKeyboard, THIS->perl.grabtime);
1045 root 1.36
1046     void
1047 root 1.44 rxvt_term::ungrab ()
1048 root 1.36 CODE:
1049 root 1.45 ungrab (THIS);
1050 root 1.35
1051 root 1.1 int
1052 root 1.3 rxvt_term::strwidth (SV *str)
1053 root 1.1 CODE:
1054     {
1055     wchar_t *wstr = sv2wcs (str);
1056 root 1.3
1057     rxvt_push_locale (THIS->locale);
1058 root 1.1 RETVAL = wcswidth (wstr, wcslen (wstr));
1059 root 1.3 rxvt_pop_locale ();
1060    
1061 root 1.1 free (wstr);
1062     }
1063     OUTPUT:
1064     RETVAL
1065    
1066 root 1.3 SV *
1067     rxvt_term::locale_encode (SV *str)
1068 root 1.1 CODE:
1069 root 1.3 {
1070     wchar_t *wstr = sv2wcs (str);
1071    
1072     rxvt_push_locale (THIS->locale);
1073     char *mbstr = rxvt_wcstombs (wstr);
1074     rxvt_pop_locale ();
1075    
1076     free (wstr);
1077    
1078 root 1.42 RETVAL = taint_if (newSVpv (mbstr, 0), str);
1079 root 1.3 free (mbstr);
1080     }
1081     OUTPUT:
1082 root 1.1 RETVAL
1083    
1084 root 1.3 SV *
1085     rxvt_term::locale_decode (SV *octets)
1086     CODE:
1087     {
1088     STRLEN len;
1089     char *data = SvPVbyte (octets, len);
1090    
1091     rxvt_push_locale (THIS->locale);
1092     wchar_t *wstr = rxvt_mbstowcs (data, len);
1093     rxvt_pop_locale ();
1094    
1095 root 1.42 RETVAL = taint_if (wcs2sv (wstr), octets);
1096 root 1.3 free (wstr);
1097     }
1098     OUTPUT:
1099     RETVAL
1100 root 1.1
1101 root 1.38 #define TERM_OFFSET(sym) offsetof (TermWin_t, sym)
1102 root 1.24
1103     #define TERM_OFFSET_width TERM_OFFSET(width)
1104     #define TERM_OFFSET_height TERM_OFFSET(height)
1105     #define TERM_OFFSET_fwidth TERM_OFFSET(fwidth)
1106     #define TERM_OFFSET_fheight TERM_OFFSET(fheight)
1107     #define TERM_OFFSET_fbase TERM_OFFSET(fbase)
1108     #define TERM_OFFSET_nrow TERM_OFFSET(nrow)
1109     #define TERM_OFFSET_ncol TERM_OFFSET(ncol)
1110     #define TERM_OFFSET_focus TERM_OFFSET(focus)
1111     #define TERM_OFFSET_mapped TERM_OFFSET(mapped)
1112     #define TERM_OFFSET_saveLines TERM_OFFSET(saveLines)
1113     #define TERM_OFFSET_total_rows TERM_OFFSET(total_rows)
1114 root 1.67 #define TERM_OFFSET_top_row TERM_OFFSET(top_row)
1115 root 1.24
1116     int
1117     rxvt_term::width ()
1118     ALIAS:
1119     width = TERM_OFFSET_width
1120     height = TERM_OFFSET_height
1121     fwidth = TERM_OFFSET_fwidth
1122     fheight = TERM_OFFSET_fheight
1123     fbase = TERM_OFFSET_fbase
1124     nrow = TERM_OFFSET_nrow
1125     ncol = TERM_OFFSET_ncol
1126     focus = TERM_OFFSET_focus
1127     mapped = TERM_OFFSET_mapped
1128     saveLines = TERM_OFFSET_saveLines
1129     total_rows = TERM_OFFSET_total_rows
1130 root 1.67 top_row = TERM_OFFSET_top_row
1131 root 1.24 CODE:
1132     RETVAL = *(int *)((char *)THIS + ix);
1133     OUTPUT:
1134     RETVAL
1135    
1136 root 1.38 unsigned int
1137     rxvt_term::ModLevel3Mask ()
1138     ALIAS:
1139     ModLevel3Mask = 0
1140     ModMetaMask = 1
1141     ModNumLockMask = 2
1142     CODE:
1143     switch (ix)
1144     {
1145     case 0: RETVAL = THIS->ModLevel3Mask; break;
1146     case 1: RETVAL = THIS->ModMetaMask; break;
1147     case 2: RETVAL = THIS->ModNumLockMask; break;
1148     }
1149     OUTPUT:
1150     RETVAL
1151    
1152 root 1.48 char *
1153     rxvt_term::display_id ()
1154 root 1.49 ALIAS:
1155     display_id = 0
1156     locale = 1
1157 root 1.48 CODE:
1158 root 1.49 switch (ix)
1159     {
1160     case 0: RETVAL = THIS->display->id; break;
1161     case 1: RETVAL = THIS->locale; break;
1162     }
1163 root 1.48 OUTPUT:
1164     RETVAL
1165    
1166 root 1.54 SV *
1167     rxvt_term::_env ()
1168     CODE:
1169     {
1170     if (THIS->envv)
1171     {
1172     AV *av = newAV ();
1173    
1174     for (char **i = THIS->envv->begin (); i != THIS->envv->end (); ++i)
1175     if (*i)
1176     av_push (av, newSVpv (*i, 0));
1177    
1178     RETVAL = newRV_noinc ((SV *)av);
1179     }
1180     else
1181     RETVAL = &PL_sv_undef;
1182     }
1183     OUTPUT:
1184     RETVAL
1185    
1186 root 1.50 int
1187     rxvt_term::pty_ev_events (int events = EVENT_UNDEF)
1188     CODE:
1189     RETVAL = THIS->pty_ev.events;
1190     if (events != EVENT_UNDEF)
1191     THIS->pty_ev.set (events);
1192     OUTPUT:
1193     RETVAL
1194    
1195 root 1.24 U32
1196 root 1.31 rxvt_term::parent ()
1197     CODE:
1198     RETVAL = (U32)THIS->parent [0];
1199     OUTPUT:
1200     RETVAL
1201    
1202     U32
1203 root 1.30 rxvt_term::vt ()
1204     CODE:
1205 root 1.31 RETVAL = (U32)THIS->vt;
1206 root 1.30 OUTPUT:
1207     RETVAL
1208    
1209 root 1.62 void
1210     rxvt_term::vt_emask_add (U32 emask)
1211     CODE:
1212     THIS->vt_emask_perl |= emask;
1213     THIS->vt_select_input ();
1214    
1215 root 1.30 U32
1216 root 1.25 rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
1217 root 1.7 CODE:
1218 root 1.25 RETVAL = THIS->rstyle;
1219     THIS->rstyle = new_rstyle;
1220 root 1.7 OUTPUT:
1221 root 1.24 RETVAL
1222 root 1.7
1223     int
1224 root 1.67 rxvt_term::view_start (int newval = 1)
1225 root 1.61 PROTOTYPE: $;$
1226 root 1.7 CODE:
1227     {
1228     RETVAL = THIS->view_start;
1229    
1230 root 1.67 if (newval <= 0)
1231     THIS->scr_changeview (max (newval, THIS->top_row));
1232 root 1.7 }
1233     OUTPUT:
1234     RETVAL
1235    
1236 root 1.8 void
1237 root 1.9 rxvt_term::want_refresh ()
1238     CODE:
1239     THIS->want_refresh = 1;
1240    
1241     void
1242 root 1.27 rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1243 root 1.8 PPCODE:
1244     {
1245 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1246 root 1.19 XSRETURN_EMPTY;
1247 root 1.8
1248     line_t &l = ROW(row_number);
1249    
1250     if (GIMME_V != G_VOID)
1251     {
1252     wchar_t *wstr = new wchar_t [THIS->ncol];
1253    
1254 root 1.33 for (int col = 0; col < THIS->ncol; col++)
1255 root 1.8 wstr [col] = l.t [col];
1256    
1257 root 1.42 XPUSHs (taint (sv_2mortal (wcs2sv (wstr, THIS->ncol))));
1258 root 1.8
1259 root 1.25 delete [] wstr;
1260 root 1.8 }
1261    
1262     if (new_text)
1263     {
1264 root 1.13 wchar_t *wstr = sv2wcs (new_text);
1265 root 1.8
1266 root 1.27 int len = min (wcslen (wstr) - start_ofs, max_len);
1267 root 1.8
1268 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
1269 root 1.8 {
1270     free (wstr);
1271 root 1.10 croak ("new_text extends beyond horizontal margins");
1272 root 1.8 }
1273    
1274     for (int col = start_col; col < start_col + len; col++)
1275     {
1276 root 1.27 l.t [col] = wstr [start_ofs + col - start_col];
1277 root 1.8 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
1278     }
1279 root 1.9
1280     free (wstr);
1281 root 1.8 }
1282     }
1283    
1284     void
1285 root 1.27 rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1286 root 1.8 PPCODE:
1287     {
1288 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1289 root 1.19 XSRETURN_EMPTY;
1290 root 1.8
1291     line_t &l = ROW(row_number);
1292    
1293     if (GIMME_V != G_VOID)
1294     {
1295     AV *av = newAV ();
1296    
1297     av_extend (av, THIS->ncol - 1);
1298     for (int col = 0; col < THIS->ncol; col++)
1299     av_store (av, col, newSViv (l.r [col]));
1300    
1301     XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1302     }
1303    
1304     if (new_rend)
1305     {
1306     if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
1307     croak ("new_rend must be arrayref");
1308    
1309     AV *av = (AV *)SvRV (new_rend);
1310 root 1.27 int len = min (av_len (av) + 1 - start_ofs, max_len);
1311 root 1.8
1312 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
1313     croak ("new_rend array extends beyond horizontal margins");
1314 root 1.8
1315     for (int col = start_col; col < start_col + len; col++)
1316     {
1317 root 1.27 rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask;
1318 root 1.8
1319     l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
1320     }
1321     }
1322     }
1323    
1324     int
1325 root 1.26 rxvt_term::ROW_l (int row_number, int new_length = -1)
1326 root 1.8 CODE:
1327     {
1328 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1329 root 1.19 XSRETURN_EMPTY;
1330 root 1.8
1331     line_t &l = ROW(row_number);
1332 root 1.26 RETVAL = l.l;
1333 root 1.8
1334 root 1.26 if (new_length >= 0)
1335 root 1.8 l.l = new_length;
1336     }
1337     OUTPUT:
1338     RETVAL
1339    
1340 root 1.19 bool
1341 root 1.26 rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
1342 root 1.19 CODE:
1343     {
1344 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1345 root 1.19 XSRETURN_EMPTY;
1346    
1347     line_t &l = ROW(row_number);
1348 root 1.26 RETVAL = l.is_longer ();
1349    
1350     if (new_is_longer >= 0)
1351     l.is_longer (new_is_longer);
1352 root 1.19 }
1353     OUTPUT:
1354     RETVAL
1355    
1356 root 1.8 SV *
1357 root 1.33 rxvt_term::special_encode (SV *string)
1358 root 1.8 CODE:
1359 root 1.33 {
1360     wchar_t *wstr = sv2wcs (string);
1361     int wlen = wcslen (wstr);
1362     wchar_t *rstr = new wchar_t [wlen]; // cannot become longer
1363    
1364     rxvt_push_locale (THIS->locale);
1365    
1366     wchar_t *r = rstr;
1367     for (wchar_t *s = wstr; *s; s++)
1368     if (wcwidth (*s) == 0)
1369     {
1370     if (r == rstr)
1371     croak ("leading combining character unencodable");
1372    
1373     unicode_t n = rxvt_compose (r[-1], *s);
1374     if (n == NOCHAR)
1375     n = rxvt_composite.compose (r[-1], *s);
1376    
1377     r[-1] = n;
1378     }
1379     #if !UNICODE_3
1380     else if (*s >= 0x10000)
1381     *r++ = rxvt_composite.compose (*s);
1382     #endif
1383     else
1384     *r++ = *s;
1385    
1386     rxvt_pop_locale ();
1387    
1388 root 1.42 RETVAL = taint_if (wcs2sv (rstr, r - rstr), string);
1389 root 1.33
1390     delete [] rstr;
1391     }
1392     OUTPUT:
1393     RETVAL
1394 root 1.8
1395     SV *
1396 root 1.33 rxvt_term::special_decode (SV *text)
1397 root 1.8 CODE:
1398 root 1.33 {
1399     wchar_t *wstr = sv2wcs (text);
1400     int wlen = wcslen (wstr);
1401     int dlen = 0;
1402    
1403     // find length
1404     for (wchar_t *s = wstr; *s; s++)
1405     if (*s == NOCHAR)
1406     ;
1407     else if (IS_COMPOSE (*s))
1408     dlen += rxvt_composite.expand (*s, 0);
1409     else
1410     dlen++;
1411    
1412     wchar_t *rstr = new wchar_t [dlen];
1413    
1414     // decode
1415     wchar_t *r = rstr;
1416     for (wchar_t *s = wstr; *s; s++)
1417     if (*s == NOCHAR)
1418     ;
1419     else if (IS_COMPOSE (*s))
1420     r += rxvt_composite.expand (*s, r);
1421     else
1422     *r++ = *s;
1423    
1424 root 1.42 RETVAL = taint_if (wcs2sv (rstr, r - rstr), text);
1425 root 1.33
1426     delete [] rstr;
1427     }
1428     OUTPUT:
1429     RETVAL
1430 root 1.8
1431 root 1.1 void
1432 root 1.2 rxvt_term::_resource (char *name, int index, SV *newval = 0)
1433     PPCODE:
1434     {
1435     struct resval { const char *name; int value; } rslist [] = {
1436 root 1.21 # define def(name) { # name, Rs_ ## name },
1437     # define reserve(name,count)
1438 root 1.2 # include "rsinc.h"
1439 root 1.21 # undef def
1440     # undef reserve
1441 root 1.2 };
1442    
1443     struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
1444    
1445     do {
1446     if (rs-- == rslist)
1447     croak ("no such resource '%s', requested", name);
1448     } while (strcmp (name, rs->name));
1449    
1450     index += rs->value;
1451    
1452     if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
1453     croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
1454    
1455     if (GIMME_V != G_VOID)
1456 root 1.42 XPUSHs (THIS->rs [index] ? sv_2mortal (taint (newSVpv (THIS->rs [index], 0))) : &PL_sv_undef);
1457 root 1.2
1458     if (newval)
1459     {
1460     if (SvOK (newval))
1461     {
1462     char *str = strdup (SvPVbyte_nolen (newval));
1463     THIS->rs [index] = str;
1464     THIS->allocated.push_back (str);
1465     }
1466     else
1467     THIS->rs [index] = 0;
1468     }
1469     }
1470    
1471 root 1.55 const char *
1472     rxvt_term::x_resource (const char *name)
1473     CLEANUP:
1474     SvTAINTED_on (ST (0));
1475    
1476 root 1.40 bool
1477     rxvt_term::option (U32 optval, int set = -1)
1478     CODE:
1479     {
1480     RETVAL = THIS->options & optval;
1481    
1482     if (set >= 0)
1483     {
1484     if (set)
1485     THIS->options |= optval;
1486     else
1487     THIS->options &= ~optval;
1488    
1489     switch (optval)
1490     {
1491     case Opt_skipBuiltinGlyphs:
1492     THIS->set_fonts ();
1493     THIS->scr_remap_chars ();
1494     THIS->scr_touch (true);
1495     THIS->want_refresh = 1;
1496     break;
1497    
1498     case Opt_cursorUnderline:
1499     THIS->want_refresh = 1;
1500     break;
1501    
1502     # case Opt_scrollBar_floating:
1503     # case Opt_scrollBar_right:
1504     # THIS->resize_all_windows (THIS->width, THIS->height, 1);
1505     # break;
1506     }
1507     }
1508     }
1509     OUTPUT:
1510     RETVAL
1511    
1512 root 1.50 bool
1513     rxvt_term::parse_keysym (char *keysym, char *str)
1514     CODE:
1515     RETVAL = 0 < THIS->parse_keysym (keysym, str);
1516     THIS->keyboard->register_done ();
1517     OUTPUT:
1518     RETVAL
1519    
1520 root 1.2 void
1521 root 1.46 rxvt_term::screen_cur (...)
1522 root 1.1 PROTOTYPE: $;$$
1523     ALIAS:
1524 root 1.24 screen_cur = 0
1525     selection_beg = 1
1526     selection_end = 2
1527     selection_mark = 3
1528 root 1.1 PPCODE:
1529     {
1530 root 1.24 row_col_t &rc = ix == 0 ? THIS->screen.cur
1531     : ix == 1 ? THIS->selection.beg
1532     : ix == 2 ? THIS->selection.end
1533     : THIS->selection.mark;
1534 root 1.1
1535     if (GIMME_V != G_VOID)
1536     {
1537     EXTEND (SP, 2);
1538 root 1.24 PUSHs (sv_2mortal (newSViv (rc.row)));
1539     PUSHs (sv_2mortal (newSViv (rc.col)));
1540 root 1.1 }
1541    
1542     if (items == 3)
1543     {
1544 root 1.67 rc.row = clamp (SvIV (ST (1)), THIS->top_row, THIS->nrow - 1);
1545 root 1.24 rc.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
1546 root 1.1
1547     if (ix)
1548     THIS->want_refresh = 1;
1549     }
1550     }
1551    
1552 root 1.53 char
1553     rxvt_term::cur_charset ()
1554     CODE:
1555     RETVAL = THIS->charsets [THIS->screen.charset];
1556     OUTPUT:
1557     RETVAL
1558    
1559 root 1.60 #void
1560     #rxvt_term::selection_clear ()
1561    
1562     void
1563     rxvt_term::selection_make (U32 eventtime, bool rect = false)
1564     CODE:
1565     THIS->selection.op = SELECTION_CONT;
1566     THIS->selection.rect = rect;
1567     THIS->selection_make (eventtime);
1568    
1569 root 1.1 int
1570 root 1.36 rxvt_term::selection_grab (U32 eventtime)
1571 root 1.1
1572     void
1573     rxvt_term::selection (SV *newtext = 0)
1574     PPCODE:
1575     {
1576     if (GIMME_V != G_VOID)
1577 root 1.45 XPUSHs (THIS->selection.text
1578     ? taint (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)))
1579     : &PL_sv_undef);
1580 root 1.1
1581     if (newtext)
1582     {
1583     free (THIS->selection.text);
1584    
1585     THIS->selection.text = sv2wcs (newtext);
1586     THIS->selection.len = wcslen (THIS->selection.text);
1587     }
1588     }
1589 root 1.24
1590 root 1.1 void
1591 root 1.51 rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle1 = RS_RVid, U32 rstyle2 = RS_RVid | RS_Uline)
1592    
1593     void
1594     rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle = RS_RVid)
1595    
1596     void
1597 root 1.50 rxvt_term::scr_bell ()
1598    
1599     void
1600 root 1.25 rxvt_term::scr_add_lines (SV *string)
1601     CODE:
1602     {
1603     wchar_t *wstr = sv2wcs (string);
1604 root 1.32 THIS->scr_add_lines (wstr, wcslen (wstr));
1605 root 1.25 free (wstr);
1606     }
1607    
1608     void
1609 root 1.13 rxvt_term::tt_write (SV *octets)
1610     INIT:
1611     STRLEN len;
1612     char *str = SvPVbyte (octets, len);
1613     C_ARGS:
1614 root 1.23 str, len
1615 root 1.13
1616 root 1.28 void
1617     rxvt_term::cmd_parse (SV *octets)
1618     CODE:
1619     {
1620     STRLEN len;
1621     char *str = SvPVbyte (octets, len);
1622    
1623     char *old_cmdbuf_ptr = THIS->cmdbuf_ptr;
1624     char *old_cmdbuf_endp = THIS->cmdbuf_endp;
1625    
1626     THIS->cmdbuf_ptr = str;
1627     THIS->cmdbuf_endp = str + len;
1628    
1629 root 1.35 rxvt_push_locale (THIS->locale);
1630 root 1.28 THIS->cmd_parse ();
1631 root 1.35 rxvt_pop_locale ();
1632 root 1.28
1633     THIS->cmdbuf_ptr = old_cmdbuf_ptr;
1634     THIS->cmdbuf_endp = old_cmdbuf_endp;
1635     }
1636    
1637 root 1.13 SV *
1638     rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
1639     CODE:
1640     {
1641     overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
1642     RETVAL = newSVptr ((void *)o, "urxvt::overlay");
1643     o->self = (HV *)SvRV (RETVAL);
1644     }
1645     OUTPUT:
1646     RETVAL
1647    
1648     MODULE = urxvt PACKAGE = urxvt::overlay
1649 root 1.1
1650     void
1651 root 1.13 overlay::set (int x, int y, SV *text, SV *rend = 0)
1652 root 1.1
1653     void
1654 root 1.18 overlay::show ()
1655    
1656     void
1657     overlay::hide ()
1658    
1659     void
1660 root 1.13 overlay::DESTROY ()
1661 root 1.1
1662     MODULE = urxvt PACKAGE = urxvt::timer
1663    
1664     SV *
1665     timer::new ()
1666     CODE:
1667     timer *w = new timer;
1668 root 1.13 w->start (NOW);
1669 root 1.1 RETVAL = newSVptr ((void *)w, "urxvt::timer");
1670     w->self = (HV *)SvRV (RETVAL);
1671     OUTPUT:
1672     RETVAL
1673    
1674     timer *
1675     timer::cb (SV *cb)
1676     CODE:
1677     THIS->cb (cb);
1678     RETVAL = THIS;
1679     OUTPUT:
1680     RETVAL
1681    
1682     NV
1683     timer::at ()
1684     CODE:
1685     RETVAL = THIS->at;
1686     OUTPUT:
1687     RETVAL
1688    
1689     timer *
1690 root 1.13 timer::interval (NV interval)
1691     CODE:
1692     THIS->interval = interval;
1693     RETVAL = THIS;
1694     OUTPUT:
1695     RETVAL
1696    
1697     timer *
1698 root 1.1 timer::set (NV tstamp)
1699     CODE:
1700     THIS->set (tstamp);
1701     RETVAL = THIS;
1702     OUTPUT:
1703     RETVAL
1704    
1705     timer *
1706     timer::start (NV tstamp = THIS->at)
1707     CODE:
1708     THIS->start (tstamp);
1709     RETVAL = THIS;
1710     OUTPUT:
1711     RETVAL
1712    
1713     timer *
1714     timer::stop ()
1715     CODE:
1716     THIS->stop ();
1717     RETVAL = THIS;
1718     OUTPUT:
1719     RETVAL
1720    
1721     void
1722     timer::DESTROY ()
1723    
1724     MODULE = urxvt PACKAGE = urxvt::iow
1725    
1726     SV *
1727     iow::new ()
1728     CODE:
1729     iow *w = new iow;
1730     RETVAL = newSVptr ((void *)w, "urxvt::iow");
1731     w->self = (HV *)SvRV (RETVAL);
1732     OUTPUT:
1733     RETVAL
1734    
1735     iow *
1736     iow::cb (SV *cb)
1737     CODE:
1738     THIS->cb (cb);
1739     RETVAL = THIS;
1740     OUTPUT:
1741     RETVAL
1742    
1743     iow *
1744     iow::fd (int fd)
1745     CODE:
1746     THIS->fd = fd;
1747     RETVAL = THIS;
1748     OUTPUT:
1749     RETVAL
1750    
1751     iow *
1752     iow::events (short events)
1753     CODE:
1754     THIS->events = events;
1755     RETVAL = THIS;
1756     OUTPUT:
1757     RETVAL
1758    
1759     iow *
1760     iow::start ()
1761     CODE:
1762     THIS->start ();
1763     RETVAL = THIS;
1764     OUTPUT:
1765     RETVAL
1766    
1767     iow *
1768     iow::stop ()
1769     CODE:
1770     THIS->stop ();
1771     RETVAL = THIS;
1772     OUTPUT:
1773     RETVAL
1774    
1775     void
1776     iow::DESTROY ()
1777    
1778