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