ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.39
Committed: Sat Jan 7 23:18:56 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
Changes since 1.38: +10 -5 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     #include "rxvt.h"
35     #include "iom.h"
36     #include "rxvtutil.h"
37     #include "rxvtperl.h"
38    
39     #include "perlxsi.c"
40    
41 root 1.8 #undef LINENO
42     #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
43     #undef ROW
44     #define ROW(n) THIS->row_buf [LINENO (n)]
45    
46 root 1.1 /////////////////////////////////////////////////////////////////////////////
47    
48     static wchar_t *
49     sv2wcs (SV *sv)
50     {
51     STRLEN len;
52     char *str = SvPVutf8 (sv, len);
53     return rxvt_utf8towcs (str, len);
54     }
55    
56     static SV *
57 root 1.25 wcs2sv (wchar_t *wstr, int len = -1)
58     {
59     char *str = rxvt_wcstoutf8 (wstr, len);
60    
61     SV *sv = newSVpv (str, 0);
62     SvUTF8_on (sv);
63     free (str);
64    
65     return sv;
66     }
67    
68     static SV *
69 root 1.1 new_ref (HV *hv, const char *klass)
70     {
71     return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
72     }
73    
74     //TODO: use magic
75     static SV *
76     newSVptr (void *ptr, const char *klass)
77     {
78     HV *hv = newHV ();
79 root 1.18 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
80 root 1.1 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
81     }
82    
83 root 1.18 static void
84     clearSVptr (SV *sv)
85     {
86     if (SvROK (sv))
87     sv = SvRV (sv);
88    
89     hv_clear ((HV *)sv);
90     sv_unmagic (sv, PERL_MAGIC_ext);
91     }
92    
93 root 1.1 static long
94     SvPTR (SV *sv, const char *klass)
95     {
96     if (!sv_derived_from (sv, klass))
97     croak ("object of type %s expected", klass);
98    
99 root 1.18 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
100 root 1.1
101 root 1.18 if (!mg)
102 root 1.1 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
103    
104 root 1.18 return (long)mg->mg_ptr;
105 root 1.1 }
106    
107 root 1.36 #define newSVterm(term) SvREFCNT_inc ((SV *)term->perl.self)
108 root 1.1 #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
109    
110     /////////////////////////////////////////////////////////////////////////////
111    
112     struct perl_watcher
113     {
114     SV *cbsv;
115     HV *self;
116    
117     perl_watcher ()
118     : cbsv (newSV (0))
119     {
120     }
121    
122     ~perl_watcher ()
123     {
124     SvREFCNT_dec (cbsv);
125     }
126    
127     void cb (SV *cb)
128     {
129     sv_setsv (cbsv, cb);
130     }
131    
132     void invoke (const char *type, SV *self, int arg = -1);
133     };
134    
135     void
136     perl_watcher::invoke (const char *type, SV *self, int arg)
137     {
138     dSP;
139    
140     ENTER;
141     SAVETMPS;
142    
143     PUSHMARK (SP);
144    
145     XPUSHs (sv_2mortal (self));
146    
147     if (arg >= 0)
148     XPUSHs (sv_2mortal (newSViv (arg)));
149    
150     PUTBACK;
151     call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
152     SPAGAIN;
153    
154     PUTBACK;
155     FREETMPS;
156     LEAVE;
157    
158     if (SvTRUE (ERRSV))
159     rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
160     }
161    
162     #define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
163     #define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
164    
165     struct timer : time_watcher, perl_watcher
166     {
167 root 1.13 tstamp interval;
168    
169 root 1.1 timer ()
170     : time_watcher (this, &timer::execute)
171     {
172     }
173    
174     void execute (time_watcher &w)
175     {
176 root 1.13 if (interval)
177     start (at + interval);
178    
179 root 1.1 invoke ("urxvt::timer", newSVtimer (this));
180     }
181     };
182    
183     #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
184     #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
185    
186     struct iow : io_watcher, perl_watcher
187     {
188     iow ()
189     : io_watcher (this, &iow::execute)
190     {
191     }
192    
193     void execute (io_watcher &w, short revents)
194     {
195     invoke ("urxvt::iow", newSViow (this), revents);
196     }
197     };
198    
199     /////////////////////////////////////////////////////////////////////////////
200    
201 root 1.13 #define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
202    
203     struct overlay {
204     HV *self;
205     rxvt_term *THIS;
206     int x, y, w, h;
207     int border;
208     text_t **text;
209     rend_t **rend;
210    
211     overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
212     ~overlay ();
213    
214 root 1.18 void show ();
215     void hide ();
216    
217 root 1.13 void swap ();
218    
219     void set (int x, int y, SV *str, SV *rend);
220     };
221    
222     overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
223     : THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2)
224     {
225     if (border == 2)
226     {
227     w += 2;
228     h += 2;
229     }
230    
231     text = new text_t *[h];
232     rend = new rend_t *[h];
233 root 1.24
234 root 1.13 for (int y = 0; y < h; y++)
235 root 1.24 {
236 root 1.13 text_t *tp = text[y] = new text_t[w];
237     rend_t *rp = rend[y] = new rend_t[w];
238 root 1.24
239 root 1.13 text_t t0, t1, t2;
240     rend_t r = rstyle;
241 root 1.24
242 root 1.13 if (border == 2)
243     {
244     if (y == 0)
245     t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
246     else if (y < h - 1)
247     t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
248     else
249     t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
250 root 1.24
251     *tp++ = t0;
252 root 1.13 *rp++ = r;
253 root 1.24
254 root 1.13 for (int x = w - 2; x-- > 0; )
255     {
256     *tp++ = t1;
257     *rp++ = r;
258 root 1.24 }
259 root 1.13
260     *tp = t2;
261 root 1.24 *rp = r;
262 root 1.13 }
263     else
264     for (int x = w; x-- > 0; )
265     {
266     *tp++ = 0x0020;
267     *rp++ = r;
268 root 1.24 }
269     }
270 root 1.13
271 root 1.18 show ();
272 root 1.13 THIS->want_refresh = 1;
273     }
274    
275     overlay::~overlay ()
276     {
277 root 1.18 hide ();
278    
279 root 1.13 for (int y = h; y--; )
280     {
281     delete [] text[y];
282     delete [] rend[y];
283     }
284    
285     delete [] text;
286     delete [] rend;
287    
288     THIS->want_refresh = 1;
289     }
290    
291 root 1.18 void
292     overlay::show ()
293     {
294     char key[33]; sprintf (key, "%32lx", (long)this);
295    
296 root 1.36 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0));
297 root 1.18 hv_store (hv, key, 32, newSViv ((long)this), 0);
298     }
299    
300     void
301     overlay::hide ()
302     {
303 root 1.36 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0);
304 root 1.18
305     if (ovs)
306     {
307     char key[33]; sprintf (key, "%32lx", (long)this);
308    
309     HV *hv = (HV *)SvRV (*ovs);
310     hv_delete (hv, key, 32, G_DISCARD);
311     }
312     }
313    
314 root 1.13 void overlay::swap ()
315     {
316     int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
317     int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
318    
319     int ov_w = min (w, THIS->ncol - ov_x);
320     int ov_h = min (h, THIS->nrow - ov_y);
321    
322     for (int y = ov_h; y--; )
323     {
324     text_t *t1 = text [y];
325     rend_t *r1 = rend [y];
326 root 1.24
327 root 1.13 text_t *t2 = ROW(y + ov_y - THIS->view_start).t + ov_x;
328     rend_t *r2 = ROW(y + ov_y - THIS->view_start).r + ov_x;
329    
330     for (int x = ov_w; x--; )
331 root 1.24 {
332 root 1.13 text_t t = *t1; *t1++ = *t2; *t2++ = t;
333     rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
334     }
335     }
336    
337     }
338    
339 root 1.14 void overlay::set (int x, int y, SV *text, SV *rend)
340 root 1.13 {
341     x += border;
342     y += border;
343    
344     if (!IN_RANGE_EXC (y, 0, h - border))
345     return;
346    
347 root 1.14 wchar_t *wtext = sv2wcs (text);
348 root 1.13
349 root 1.14 for (int col = min (wcslen (wtext), w - x - border); col--; )
350     this->text [y][x + col] = wtext [col];
351 root 1.24
352 root 1.14 free (wtext);
353    
354     if (rend)
355     {
356     if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
357     croak ("rend must be arrayref");
358    
359     AV *av = (AV *)SvRV (rend);
360    
361     for (int col = min (av_len (av) + 1, w - x - border); col--; )
362     this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
363     }
364 root 1.13
365     THIS->want_refresh = 1;
366     }
367    
368    
369     /////////////////////////////////////////////////////////////////////////////
370    
371 root 1.1 struct rxvt_perl_interp rxvt_perl;
372    
373     static PerlInterpreter *perl;
374    
375     rxvt_perl_interp::rxvt_perl_interp ()
376     {
377     }
378    
379     rxvt_perl_interp::~rxvt_perl_interp ()
380     {
381     if (perl)
382     {
383     perl_destruct (perl);
384     perl_free (perl);
385     }
386     }
387    
388     void
389     rxvt_perl_interp::init ()
390     {
391     if (!perl)
392     {
393     char *argv[] = {
394     "",
395     "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
396     };
397    
398     perl = perl_alloc ();
399     perl_construct (perl);
400    
401     if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
402     || perl_run (perl))
403     {
404     rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
405    
406     perl_destruct (perl);
407     perl_free (perl);
408     perl = 0;
409     }
410     }
411     }
412    
413     bool
414     rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
415     {
416 root 1.13 if (!perl)
417 root 1.1 return false;
418 root 1.24
419 root 1.1 if (htype == HOOK_INIT) // first hook ever called
420 root 1.13 {
421 root 1.36 term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term");
422     hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
423 root 1.13 }
424 root 1.36 else if (!term->perl.self)
425 root 1.17 return false; // perl not initialized for this instance
426 root 1.13 else if (htype == HOOK_DESTROY)
427     {
428     // handled later
429     }
430     else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
431     {
432 root 1.36 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
433 root 1.13
434     if (HvKEYS (hv))
435     {
436     hv_iterinit (hv);
437    
438     while (HE *he = hv_iternext (hv))
439     ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
440     }
441     }
442     else if (!should_invoke [htype])
443     return false;
444 root 1.1
445     dSP;
446     va_list ap;
447    
448     va_start (ap, htype);
449    
450     ENTER;
451     SAVETMPS;
452    
453     PUSHMARK (SP);
454    
455     XPUSHs (sv_2mortal (newSVterm (term)));
456     XPUSHs (sv_2mortal (newSViv (htype)));
457    
458     for (;;) {
459     data_type dt = (data_type)va_arg (ap, int);
460    
461     switch (dt)
462     {
463     case DT_INT:
464     XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
465     break;
466    
467     case DT_LONG:
468     XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
469     break;
470    
471 root 1.32 case DT_STR:
472 root 1.6 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
473     break;
474    
475 root 1.32 case DT_STR_LEN:
476 root 1.29 {
477     char *str = va_arg (ap, char *);
478     int len = va_arg (ap, int);
479    
480     XPUSHs (sv_2mortal (newSVpvn (str, len)));
481     }
482     break;
483    
484 root 1.32 case DT_WCS_LEN:
485     {
486     wchar_t *wstr = va_arg (ap, wchar_t *);
487     int wlen = va_arg (ap, int);
488    
489     XPUSHs (sv_2mortal (wcs2sv (wstr, wlen)));
490     }
491 root 1.34 break;
492 root 1.32
493 root 1.29 case DT_XEVENT:
494     {
495     XEvent *xe = va_arg (ap, XEvent *);
496     HV *hv = newHV ();
497    
498     # define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
499     # define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
500     # undef set
501    
502     setiv (type, xe->type);
503     setiv (send_event, xe->xany.send_event);
504     setiv (serial, xe->xany.serial);
505    
506     switch (xe->type)
507     {
508     case KeyPress:
509     case KeyRelease:
510     case ButtonPress:
511     case ButtonRelease:
512     case MotionNotify:
513     setiv (time, xe->xmotion.time);
514     setiv (x, xe->xmotion.x);
515     setiv (y, xe->xmotion.y);
516 root 1.30 setiv (row, xe->xmotion.y / term->fheight);
517     setiv (col, xe->xmotion.x / term->fwidth);
518 root 1.29 setiv (x_root, xe->xmotion.x_root);
519     setiv (y_root, xe->xmotion.y_root);
520     setiv (state, xe->xmotion.state);
521     break;
522     }
523    
524     switch (xe->type)
525     {
526     case KeyPress:
527     case KeyRelease:
528     setiv (keycode, xe->xkey.keycode);
529     break;
530    
531     case ButtonPress:
532     case ButtonRelease:
533     setiv (button, xe->xbutton.button);
534     break;
535    
536     case MotionNotify:
537     setiv (is_hint, xe->xmotion.is_hint);
538     break;
539     }
540    
541     XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
542     }
543     break;
544    
545 root 1.1 case DT_END:
546     {
547     va_end (ap);
548    
549     PUTBACK;
550     int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
551     SPAGAIN;
552    
553     if (count)
554     {
555     SV *status = POPs;
556     count = SvTRUE (status);
557     }
558    
559     PUTBACK;
560     FREETMPS;
561     LEAVE;
562    
563     if (SvTRUE (ERRSV))
564     rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
565    
566 root 1.4 if (htype == HOOK_DESTROY)
567     {
568 root 1.36 clearSVptr ((SV *)term->perl.self);
569     SvREFCNT_dec ((SV *)term->perl.self);
570 root 1.4 }
571    
572 root 1.1 return count;
573     }
574    
575     default:
576     rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
577     }
578     }
579     }
580    
581     /////////////////////////////////////////////////////////////////////////////
582    
583     MODULE = urxvt PACKAGE = urxvt
584    
585     PROTOTYPES: ENABLE
586    
587     BOOT:
588     {
589 root 1.39 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
590 root 1.37
591 root 1.1 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
592 root 1.21 # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
593     # include "hookinc.h"
594     # undef def
595 root 1.1
596 root 1.39 HV *option = get_hv ("urxvt::OPTION", 1);
597     # define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0);
598     # define nodef(name)
599     # include "optinc.h"
600     # undef nodef
601     # undef def
602    
603     HV *stash = gv_stashpv ("urxvt", 1);
604     # define export_const_iv(name) newCONSTSUB (stash, # name, newSViv (name));
605 root 1.37 export_const_iv (DEFAULT_RSTYLE);
606     export_const_iv (OVERLAY_RSTYLE);
607     export_const_iv (RS_Bold);
608     export_const_iv (RS_Italic);
609     export_const_iv (RS_Blink);
610     export_const_iv (RS_RVid);
611     export_const_iv (RS_Uline);
612 root 1.38
613 root 1.37 export_const_iv (CurrentTime);
614 root 1.38 export_const_iv (ShiftMask);
615     export_const_iv (LockMask);
616     export_const_iv (ControlMask);
617     export_const_iv (Mod1Mask);
618     export_const_iv (Mod2Mask);
619     export_const_iv (Mod3Mask);
620     export_const_iv (Mod4Mask);
621     export_const_iv (Mod5Mask);
622     export_const_iv (Button1Mask);
623     export_const_iv (Button2Mask);
624     export_const_iv (Button3Mask);
625     export_const_iv (Button4Mask);
626     export_const_iv (Button5Mask);
627     export_const_iv (AnyModifier);
628 root 1.1 }
629    
630 root 1.28 SV *
631     new (...)
632     CODE:
633     {
634     stringvec *argv = new stringvec;
635     bool success;
636    
637     for (int i = 0; i < items ;i++)
638     argv->push_back (strdup (SvPVbyte_nolen (ST (i))));
639    
640     rxvt_term *term = new rxvt_term;
641    
642     term->argv = argv;
643    
644     try
645     {
646     if (!term->init (argv->size (), argv->begin ()))
647     term = 0;
648     }
649     catch (const class rxvt_failure_exception &e)
650     {
651     term->destroy ();
652     croak ("exception caught while initializing new terminal instance");
653     }
654    
655 root 1.36 RETVAL = term && term->perl.self ? newSVterm (term) : &PL_sv_undef;
656 root 1.28 }
657     OUTPUT:
658     RETVAL
659    
660 root 1.1 void
661     set_should_invoke (int htype, int value)
662     CODE:
663     rxvt_perl.should_invoke [htype] = value;
664    
665     void
666     warn (const char *msg)
667     CODE:
668     rxvt_warn ("%s", msg);
669    
670     void
671     fatal (const char *msg)
672     CODE:
673     rxvt_fatal ("%s", msg);
674    
675 root 1.3 NV
676     NOW ()
677     CODE:
678     RETVAL = NOW;
679     OUTPUT:
680     RETVAL
681    
682 root 1.11 int
683     GET_BASEFG (int rend)
684     CODE:
685     RETVAL = GET_BASEFG (rend);
686     OUTPUT:
687     RETVAL
688    
689     int
690     GET_BASEBG (int rend)
691     CODE:
692     RETVAL = GET_BASEBG (rend);
693     OUTPUT:
694     RETVAL
695    
696     int
697 root 1.12 SET_FGCOLOR (int rend, int new_color)
698 root 1.11 CODE:
699 root 1.12 RETVAL = SET_FGCOLOR (rend, new_color);
700 root 1.11 OUTPUT:
701     RETVAL
702    
703     int
704 root 1.12 SET_BGCOLOR (int rend, int new_color)
705 root 1.11 CODE:
706 root 1.12 RETVAL = SET_BGCOLOR (rend, new_color);
707     OUTPUT:
708     RETVAL
709    
710     int
711     GET_CUSTOM (int rend)
712     CODE:
713     RETVAL = (rend && RS_customMask) >> RS_customShift;
714     OUTPUT:
715     RETVAL
716    
717     int
718     SET_CUSTOM (int rend, int new_value)
719     CODE:
720     {
721     if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
722     croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
723    
724     RETVAL = (rend & ~RS_customMask)
725     | ((new_value << RS_customShift) & RS_customMask);
726     }
727 root 1.11 OUTPUT:
728     RETVAL
729    
730 root 1.3 MODULE = urxvt PACKAGE = urxvt::term
731    
732 root 1.28 void
733     rxvt_term::destroy ()
734    
735 root 1.35 void
736 root 1.36 rxvt_term::grab_button (int button, U32 modifiers)
737     CODE:
738     XGrabButton (THIS->display->display, button, modifiers, THIS->vt, 1,
739     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
740     GrabModeSync, GrabModeSync, None, None);
741    
742     bool
743     rxvt_term::grab (U32 eventtime, int sync = 0)
744 root 1.35 CODE:
745     {
746 root 1.36 int mode = sync ? GrabModeSync : GrabModeAsync;
747    
748     THIS->perl.grabtime = 0;
749    
750     if (!XGrabPointer (THIS->display->display, THIS->vt, 0,
751     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
752     mode, mode, None, None, eventtime))
753     if (!XGrabKeyboard (THIS->display->display, THIS->vt, 0, mode, mode, eventtime))
754     THIS->perl.grabtime = eventtime;
755     else
756     XUngrabPointer (THIS->display->display, eventtime);
757    
758     RETVAL = !!THIS->perl.grabtime;
759 root 1.35 }
760 root 1.36 OUTPUT:
761     RETVAL
762    
763     void
764     rxvt_term::allow_events_async (U32 eventtime = THIS->perl.grabtime)
765     CODE:
766     XAllowEvents (THIS->display->display, AsyncBoth, eventtime);
767    
768     void
769     rxvt_term::allow_events_sync (U32 eventtime = THIS->perl.grabtime)
770     CODE:
771     XAllowEvents (THIS->display->display, SyncBoth, eventtime);
772    
773     void
774     rxvt_term::allow_events_replay (U32 eventtime = THIS->perl.grabtime)
775     CODE:
776     XAllowEvents (THIS->display->display, ReplayPointer, eventtime);
777     XAllowEvents (THIS->display->display, ReplayKeyboard, eventtime);
778    
779     void
780     rxvt_term::ungrab (U32 eventtime = THIS->perl.grabtime)
781     CODE:
782     THIS->perl.grabtime = 0;
783     XUngrabKeyboard (THIS->display->display, eventtime);
784     XUngrabPointer (THIS->display->display, eventtime);
785 root 1.35
786 root 1.1 int
787 root 1.3 rxvt_term::strwidth (SV *str)
788 root 1.1 CODE:
789     {
790     wchar_t *wstr = sv2wcs (str);
791 root 1.3
792     rxvt_push_locale (THIS->locale);
793 root 1.1 RETVAL = wcswidth (wstr, wcslen (wstr));
794 root 1.3 rxvt_pop_locale ();
795    
796 root 1.1 free (wstr);
797     }
798     OUTPUT:
799     RETVAL
800    
801 root 1.3 SV *
802     rxvt_term::locale_encode (SV *str)
803 root 1.1 CODE:
804 root 1.3 {
805     wchar_t *wstr = sv2wcs (str);
806    
807     rxvt_push_locale (THIS->locale);
808     char *mbstr = rxvt_wcstombs (wstr);
809     rxvt_pop_locale ();
810    
811     free (wstr);
812    
813     RETVAL = newSVpv (mbstr, 0);
814     free (mbstr);
815     }
816     OUTPUT:
817 root 1.1 RETVAL
818    
819 root 1.3 SV *
820     rxvt_term::locale_decode (SV *octets)
821     CODE:
822     {
823     STRLEN len;
824     char *data = SvPVbyte (octets, len);
825    
826     rxvt_push_locale (THIS->locale);
827     wchar_t *wstr = rxvt_mbstowcs (data, len);
828     rxvt_pop_locale ();
829    
830 root 1.25 RETVAL = wcs2sv (wstr);
831 root 1.3 free (wstr);
832     }
833     OUTPUT:
834     RETVAL
835 root 1.1
836 root 1.38 #define TERM_OFFSET(sym) offsetof (TermWin_t, sym)
837 root 1.24
838     #define TERM_OFFSET_width TERM_OFFSET(width)
839     #define TERM_OFFSET_height TERM_OFFSET(height)
840     #define TERM_OFFSET_fwidth TERM_OFFSET(fwidth)
841     #define TERM_OFFSET_fheight TERM_OFFSET(fheight)
842     #define TERM_OFFSET_fbase TERM_OFFSET(fbase)
843     #define TERM_OFFSET_nrow TERM_OFFSET(nrow)
844     #define TERM_OFFSET_ncol TERM_OFFSET(ncol)
845     #define TERM_OFFSET_focus TERM_OFFSET(focus)
846     #define TERM_OFFSET_mapped TERM_OFFSET(mapped)
847     #define TERM_OFFSET_saveLines TERM_OFFSET(saveLines)
848     #define TERM_OFFSET_total_rows TERM_OFFSET(total_rows)
849     #define TERM_OFFSET_nsaved TERM_OFFSET(nsaved)
850    
851     int
852     rxvt_term::width ()
853     ALIAS:
854     width = TERM_OFFSET_width
855     height = TERM_OFFSET_height
856     fwidth = TERM_OFFSET_fwidth
857     fheight = TERM_OFFSET_fheight
858     fbase = TERM_OFFSET_fbase
859     nrow = TERM_OFFSET_nrow
860     ncol = TERM_OFFSET_ncol
861     focus = TERM_OFFSET_focus
862     mapped = TERM_OFFSET_mapped
863     saveLines = TERM_OFFSET_saveLines
864     total_rows = TERM_OFFSET_total_rows
865     nsaved = TERM_OFFSET_nsaved
866     CODE:
867     RETVAL = *(int *)((char *)THIS + ix);
868     OUTPUT:
869     RETVAL
870    
871 root 1.38 unsigned int
872     rxvt_term::ModLevel3Mask ()
873     ALIAS:
874     ModLevel3Mask = 0
875     ModMetaMask = 1
876     ModNumLockMask = 2
877     CODE:
878     switch (ix)
879     {
880     case 0: RETVAL = THIS->ModLevel3Mask; break;
881     case 1: RETVAL = THIS->ModMetaMask; break;
882     case 2: RETVAL = THIS->ModNumLockMask; break;
883     }
884     OUTPUT:
885     RETVAL
886    
887 root 1.24 U32
888 root 1.31 rxvt_term::parent ()
889     CODE:
890     RETVAL = (U32)THIS->parent [0];
891     OUTPUT:
892     RETVAL
893    
894     U32
895 root 1.30 rxvt_term::vt ()
896     CODE:
897 root 1.31 RETVAL = (U32)THIS->vt;
898 root 1.30 OUTPUT:
899     RETVAL
900    
901     U32
902 root 1.25 rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
903 root 1.7 CODE:
904 root 1.24 {
905 root 1.25 RETVAL = THIS->rstyle;
906     THIS->rstyle = new_rstyle;
907 root 1.24 }
908 root 1.7 OUTPUT:
909 root 1.24 RETVAL
910 root 1.7
911     int
912     rxvt_term::view_start (int newval = -1)
913     CODE:
914     {
915     RETVAL = THIS->view_start;
916    
917     if (newval >= 0)
918     {
919     THIS->view_start = min (newval, THIS->nsaved);
920     THIS->scr_changeview (RETVAL);
921     }
922     }
923     OUTPUT:
924     RETVAL
925    
926 root 1.8 void
927 root 1.9 rxvt_term::want_refresh ()
928     CODE:
929     THIS->want_refresh = 1;
930    
931     void
932 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)
933 root 1.8 PPCODE:
934     {
935     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
936 root 1.19 XSRETURN_EMPTY;
937 root 1.8
938     line_t &l = ROW(row_number);
939    
940     if (GIMME_V != G_VOID)
941     {
942     wchar_t *wstr = new wchar_t [THIS->ncol];
943    
944 root 1.33 for (int col = 0; col < THIS->ncol; col++)
945 root 1.8 wstr [col] = l.t [col];
946    
947 root 1.33 XPUSHs (sv_2mortal (wcs2sv (wstr, THIS->ncol)));
948 root 1.8
949 root 1.25 delete [] wstr;
950 root 1.8 }
951    
952     if (new_text)
953     {
954 root 1.13 wchar_t *wstr = sv2wcs (new_text);
955 root 1.8
956 root 1.27 int len = min (wcslen (wstr) - start_ofs, max_len);
957 root 1.8
958 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
959 root 1.8 {
960     free (wstr);
961 root 1.10 croak ("new_text extends beyond horizontal margins");
962 root 1.8 }
963    
964     for (int col = start_col; col < start_col + len; col++)
965     {
966 root 1.27 l.t [col] = wstr [start_ofs + col - start_col];
967 root 1.8 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
968     }
969 root 1.9
970     free (wstr);
971 root 1.8 }
972     }
973    
974     void
975 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)
976 root 1.8 PPCODE:
977     {
978     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
979 root 1.19 XSRETURN_EMPTY;
980 root 1.8
981     line_t &l = ROW(row_number);
982    
983     if (GIMME_V != G_VOID)
984     {
985     AV *av = newAV ();
986    
987     av_extend (av, THIS->ncol - 1);
988     for (int col = 0; col < THIS->ncol; col++)
989     av_store (av, col, newSViv (l.r [col]));
990    
991     XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
992     }
993    
994     if (new_rend)
995     {
996     if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
997     croak ("new_rend must be arrayref");
998    
999     AV *av = (AV *)SvRV (new_rend);
1000 root 1.27 int len = min (av_len (av) + 1 - start_ofs, max_len);
1001 root 1.8
1002 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
1003     croak ("new_rend array extends beyond horizontal margins");
1004 root 1.8
1005     for (int col = start_col; col < start_col + len; col++)
1006     {
1007 root 1.27 rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask;
1008 root 1.8
1009     l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
1010     }
1011     }
1012     }
1013    
1014     int
1015 root 1.26 rxvt_term::ROW_l (int row_number, int new_length = -1)
1016 root 1.8 CODE:
1017     {
1018     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1019 root 1.19 XSRETURN_EMPTY;
1020 root 1.8
1021     line_t &l = ROW(row_number);
1022 root 1.26 RETVAL = l.l;
1023 root 1.8
1024 root 1.26 if (new_length >= 0)
1025 root 1.8 l.l = new_length;
1026     }
1027     OUTPUT:
1028     RETVAL
1029    
1030 root 1.19 bool
1031 root 1.26 rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
1032 root 1.19 CODE:
1033     {
1034     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1035     XSRETURN_EMPTY;
1036    
1037     line_t &l = ROW(row_number);
1038 root 1.26 RETVAL = l.is_longer ();
1039    
1040     if (new_is_longer >= 0)
1041     l.is_longer (new_is_longer);
1042 root 1.19 }
1043     OUTPUT:
1044     RETVAL
1045    
1046 root 1.8 SV *
1047 root 1.33 rxvt_term::special_encode (SV *string)
1048 root 1.8 CODE:
1049 root 1.33 {
1050     wchar_t *wstr = sv2wcs (string);
1051     int wlen = wcslen (wstr);
1052     wchar_t *rstr = new wchar_t [wlen]; // cannot become longer
1053    
1054     rxvt_push_locale (THIS->locale);
1055    
1056     wchar_t *r = rstr;
1057     for (wchar_t *s = wstr; *s; s++)
1058     if (wcwidth (*s) == 0)
1059     {
1060     if (r == rstr)
1061     croak ("leading combining character unencodable");
1062    
1063     unicode_t n = rxvt_compose (r[-1], *s);
1064     if (n == NOCHAR)
1065     n = rxvt_composite.compose (r[-1], *s);
1066    
1067     r[-1] = n;
1068     }
1069     #if !UNICODE_3
1070     else if (*s >= 0x10000)
1071     *r++ = rxvt_composite.compose (*s);
1072     #endif
1073     else
1074     *r++ = *s;
1075    
1076     rxvt_pop_locale ();
1077    
1078     RETVAL = wcs2sv (rstr, r - rstr);
1079    
1080     delete [] rstr;
1081     }
1082     OUTPUT:
1083     RETVAL
1084 root 1.8
1085     SV *
1086 root 1.33 rxvt_term::special_decode (SV *text)
1087 root 1.8 CODE:
1088 root 1.33 {
1089     wchar_t *wstr = sv2wcs (text);
1090     int wlen = wcslen (wstr);
1091     int dlen = 0;
1092    
1093     // find length
1094     for (wchar_t *s = wstr; *s; s++)
1095     if (*s == NOCHAR)
1096     ;
1097     else if (IS_COMPOSE (*s))
1098     dlen += rxvt_composite.expand (*s, 0);
1099     else
1100     dlen++;
1101    
1102     wchar_t *rstr = new wchar_t [dlen];
1103    
1104     // decode
1105     wchar_t *r = rstr;
1106     for (wchar_t *s = wstr; *s; s++)
1107     if (*s == NOCHAR)
1108     ;
1109     else if (IS_COMPOSE (*s))
1110     r += rxvt_composite.expand (*s, r);
1111     else
1112     *r++ = *s;
1113    
1114     RETVAL = wcs2sv (rstr, r - rstr);
1115    
1116     delete [] rstr;
1117     }
1118     OUTPUT:
1119     RETVAL
1120 root 1.8
1121 root 1.1 void
1122 root 1.2 rxvt_term::_resource (char *name, int index, SV *newval = 0)
1123     PPCODE:
1124     {
1125     struct resval { const char *name; int value; } rslist [] = {
1126 root 1.21 # define def(name) { # name, Rs_ ## name },
1127     # define reserve(name,count)
1128 root 1.2 # include "rsinc.h"
1129 root 1.21 # undef def
1130     # undef reserve
1131 root 1.2 };
1132    
1133     struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
1134    
1135     do {
1136     if (rs-- == rslist)
1137     croak ("no such resource '%s', requested", name);
1138     } while (strcmp (name, rs->name));
1139    
1140     index += rs->value;
1141    
1142     if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
1143     croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
1144    
1145     if (GIMME_V != G_VOID)
1146     XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
1147    
1148     if (newval)
1149     {
1150     if (SvOK (newval))
1151     {
1152     char *str = strdup (SvPVbyte_nolen (newval));
1153     THIS->rs [index] = str;
1154     THIS->allocated.push_back (str);
1155     }
1156     else
1157     THIS->rs [index] = 0;
1158     }
1159     }
1160    
1161     void
1162 root 1.24 rxvt_term::cur (...)
1163 root 1.1 PROTOTYPE: $;$$
1164     ALIAS:
1165 root 1.24 screen_cur = 0
1166     selection_beg = 1
1167     selection_end = 2
1168     selection_mark = 3
1169 root 1.1 PPCODE:
1170     {
1171 root 1.24 row_col_t &rc = ix == 0 ? THIS->screen.cur
1172     : ix == 1 ? THIS->selection.beg
1173     : ix == 2 ? THIS->selection.end
1174     : THIS->selection.mark;
1175 root 1.1
1176     if (GIMME_V != G_VOID)
1177     {
1178     EXTEND (SP, 2);
1179 root 1.24 PUSHs (sv_2mortal (newSViv (rc.row)));
1180     PUSHs (sv_2mortal (newSViv (rc.col)));
1181 root 1.1 }
1182    
1183     if (items == 3)
1184     {
1185 root 1.24 rc.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
1186     rc.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
1187 root 1.1
1188     if (ix)
1189     THIS->want_refresh = 1;
1190     }
1191     }
1192    
1193     int
1194 root 1.36 rxvt_term::selection_grab (U32 eventtime)
1195 root 1.1
1196     void
1197     rxvt_term::selection (SV *newtext = 0)
1198     PPCODE:
1199     {
1200     if (GIMME_V != G_VOID)
1201 root 1.25 XPUSHs (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)));
1202 root 1.1
1203     if (newtext)
1204     {
1205     free (THIS->selection.text);
1206    
1207     THIS->selection.text = sv2wcs (newtext);
1208     THIS->selection.len = wcslen (THIS->selection.text);
1209     }
1210     }
1211 root 1.24
1212 root 1.1 void
1213 root 1.25 rxvt_term::scr_add_lines (SV *string)
1214     CODE:
1215     {
1216     wchar_t *wstr = sv2wcs (string);
1217 root 1.32 THIS->scr_add_lines (wstr, wcslen (wstr));
1218 root 1.25 free (wstr);
1219     }
1220    
1221     void
1222 root 1.13 rxvt_term::tt_write (SV *octets)
1223     INIT:
1224     STRLEN len;
1225     char *str = SvPVbyte (octets, len);
1226     C_ARGS:
1227 root 1.23 str, len
1228 root 1.13
1229 root 1.28 void
1230     rxvt_term::cmd_parse (SV *octets)
1231     CODE:
1232     {
1233     STRLEN len;
1234     char *str = SvPVbyte (octets, len);
1235    
1236     char *old_cmdbuf_ptr = THIS->cmdbuf_ptr;
1237     char *old_cmdbuf_endp = THIS->cmdbuf_endp;
1238    
1239     THIS->cmdbuf_ptr = str;
1240     THIS->cmdbuf_endp = str + len;
1241    
1242 root 1.35 rxvt_push_locale (THIS->locale);
1243 root 1.28 THIS->cmd_parse ();
1244 root 1.35 rxvt_pop_locale ();
1245 root 1.28
1246     THIS->cmdbuf_ptr = old_cmdbuf_ptr;
1247     THIS->cmdbuf_endp = old_cmdbuf_endp;
1248     }
1249    
1250 root 1.13 SV *
1251     rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
1252     CODE:
1253     {
1254     overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
1255     RETVAL = newSVptr ((void *)o, "urxvt::overlay");
1256     o->self = (HV *)SvRV (RETVAL);
1257     }
1258     OUTPUT:
1259     RETVAL
1260    
1261     MODULE = urxvt PACKAGE = urxvt::overlay
1262 root 1.1
1263     void
1264 root 1.13 overlay::set (int x, int y, SV *text, SV *rend = 0)
1265 root 1.1
1266     void
1267 root 1.18 overlay::show ()
1268    
1269     void
1270     overlay::hide ()
1271    
1272     void
1273 root 1.13 overlay::DESTROY ()
1274 root 1.1
1275     MODULE = urxvt PACKAGE = urxvt::timer
1276    
1277     SV *
1278     timer::new ()
1279     CODE:
1280     timer *w = new timer;
1281 root 1.13 w->start (NOW);
1282 root 1.1 RETVAL = newSVptr ((void *)w, "urxvt::timer");
1283     w->self = (HV *)SvRV (RETVAL);
1284     OUTPUT:
1285     RETVAL
1286    
1287     timer *
1288     timer::cb (SV *cb)
1289     CODE:
1290     THIS->cb (cb);
1291     RETVAL = THIS;
1292     OUTPUT:
1293     RETVAL
1294    
1295     NV
1296     timer::at ()
1297     CODE:
1298     RETVAL = THIS->at;
1299     OUTPUT:
1300     RETVAL
1301    
1302     timer *
1303 root 1.13 timer::interval (NV interval)
1304     CODE:
1305     THIS->interval = interval;
1306     RETVAL = THIS;
1307     OUTPUT:
1308     RETVAL
1309    
1310     timer *
1311 root 1.1 timer::set (NV tstamp)
1312     CODE:
1313     THIS->set (tstamp);
1314     RETVAL = THIS;
1315     OUTPUT:
1316     RETVAL
1317    
1318     timer *
1319     timer::start (NV tstamp = THIS->at)
1320     CODE:
1321     THIS->start (tstamp);
1322     RETVAL = THIS;
1323     OUTPUT:
1324     RETVAL
1325    
1326     timer *
1327     timer::stop ()
1328     CODE:
1329     THIS->stop ();
1330     RETVAL = THIS;
1331     OUTPUT:
1332     RETVAL
1333    
1334     void
1335     timer::DESTROY ()
1336    
1337     MODULE = urxvt PACKAGE = urxvt::iow
1338    
1339     SV *
1340     iow::new ()
1341     CODE:
1342     iow *w = new iow;
1343     RETVAL = newSVptr ((void *)w, "urxvt::iow");
1344     w->self = (HV *)SvRV (RETVAL);
1345     OUTPUT:
1346     RETVAL
1347    
1348     iow *
1349     iow::cb (SV *cb)
1350     CODE:
1351     THIS->cb (cb);
1352     RETVAL = THIS;
1353     OUTPUT:
1354     RETVAL
1355    
1356     iow *
1357     iow::fd (int fd)
1358     CODE:
1359     THIS->fd = fd;
1360     RETVAL = THIS;
1361     OUTPUT:
1362     RETVAL
1363    
1364     iow *
1365     iow::events (short events)
1366     CODE:
1367     THIS->events = events;
1368     RETVAL = THIS;
1369     OUTPUT:
1370     RETVAL
1371    
1372     iow *
1373     iow::start ()
1374     CODE:
1375     THIS->start ();
1376     RETVAL = THIS;
1377     OUTPUT:
1378     RETVAL
1379    
1380     iow *
1381     iow::stop ()
1382     CODE:
1383     THIS->stop ();
1384     RETVAL = THIS;
1385     OUTPUT:
1386     RETVAL
1387    
1388     void
1389     iow::DESTROY ()
1390    
1391