ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.38
Committed: Sat Jan 7 21:22:02 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
Changes since 1.37: +33 -2 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.37 HV *stash = gv_stashpv ("urxvt", 1);
590     # define export_const_iv(name) newCONSTSUB (stash, # name, newSViv (name));
591    
592 root 1.1 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
593 root 1.21 # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
594     # include "hookinc.h"
595     # undef def
596 root 1.1
597 root 1.37
598     export_const_iv (DEFAULT_RSTYLE);
599     export_const_iv (OVERLAY_RSTYLE);
600     export_const_iv (RS_Bold);
601     export_const_iv (RS_Italic);
602     export_const_iv (RS_Blink);
603     export_const_iv (RS_RVid);
604     export_const_iv (RS_Uline);
605 root 1.38
606 root 1.37 export_const_iv (CurrentTime);
607 root 1.38 export_const_iv (ShiftMask);
608     export_const_iv (LockMask);
609     export_const_iv (ControlMask);
610     export_const_iv (Mod1Mask);
611     export_const_iv (Mod2Mask);
612     export_const_iv (Mod3Mask);
613     export_const_iv (Mod4Mask);
614     export_const_iv (Mod5Mask);
615     export_const_iv (Button1Mask);
616     export_const_iv (Button2Mask);
617     export_const_iv (Button3Mask);
618     export_const_iv (Button4Mask);
619     export_const_iv (Button5Mask);
620     export_const_iv (AnyModifier);
621 root 1.9
622 root 1.37 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
623 root 1.1 }
624    
625 root 1.28 SV *
626     new (...)
627     CODE:
628     {
629     stringvec *argv = new stringvec;
630     bool success;
631    
632     for (int i = 0; i < items ;i++)
633     argv->push_back (strdup (SvPVbyte_nolen (ST (i))));
634    
635     rxvt_term *term = new rxvt_term;
636    
637     term->argv = argv;
638    
639     try
640     {
641     if (!term->init (argv->size (), argv->begin ()))
642     term = 0;
643     }
644     catch (const class rxvt_failure_exception &e)
645     {
646     term->destroy ();
647     croak ("exception caught while initializing new terminal instance");
648     }
649    
650 root 1.36 RETVAL = term && term->perl.self ? newSVterm (term) : &PL_sv_undef;
651 root 1.28 }
652     OUTPUT:
653     RETVAL
654    
655 root 1.1 void
656     set_should_invoke (int htype, int value)
657     CODE:
658     rxvt_perl.should_invoke [htype] = value;
659    
660     void
661     warn (const char *msg)
662     CODE:
663     rxvt_warn ("%s", msg);
664    
665     void
666     fatal (const char *msg)
667     CODE:
668     rxvt_fatal ("%s", msg);
669    
670 root 1.3 NV
671     NOW ()
672     CODE:
673     RETVAL = NOW;
674     OUTPUT:
675     RETVAL
676    
677 root 1.11 int
678     GET_BASEFG (int rend)
679     CODE:
680     RETVAL = GET_BASEFG (rend);
681     OUTPUT:
682     RETVAL
683    
684     int
685     GET_BASEBG (int rend)
686     CODE:
687     RETVAL = GET_BASEBG (rend);
688     OUTPUT:
689     RETVAL
690    
691     int
692 root 1.12 SET_FGCOLOR (int rend, int new_color)
693 root 1.11 CODE:
694 root 1.12 RETVAL = SET_FGCOLOR (rend, new_color);
695 root 1.11 OUTPUT:
696     RETVAL
697    
698     int
699 root 1.12 SET_BGCOLOR (int rend, int new_color)
700 root 1.11 CODE:
701 root 1.12 RETVAL = SET_BGCOLOR (rend, new_color);
702     OUTPUT:
703     RETVAL
704    
705     int
706     GET_CUSTOM (int rend)
707     CODE:
708     RETVAL = (rend && RS_customMask) >> RS_customShift;
709     OUTPUT:
710     RETVAL
711    
712     int
713     SET_CUSTOM (int rend, int new_value)
714     CODE:
715     {
716     if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
717     croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
718    
719     RETVAL = (rend & ~RS_customMask)
720     | ((new_value << RS_customShift) & RS_customMask);
721     }
722 root 1.11 OUTPUT:
723     RETVAL
724    
725 root 1.3 MODULE = urxvt PACKAGE = urxvt::term
726    
727 root 1.28 void
728     rxvt_term::destroy ()
729    
730 root 1.35 void
731 root 1.36 rxvt_term::grab_button (int button, U32 modifiers)
732     CODE:
733     XGrabButton (THIS->display->display, button, modifiers, THIS->vt, 1,
734     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
735     GrabModeSync, GrabModeSync, None, None);
736    
737     bool
738     rxvt_term::grab (U32 eventtime, int sync = 0)
739 root 1.35 CODE:
740     {
741 root 1.36 int mode = sync ? GrabModeSync : GrabModeAsync;
742    
743     THIS->perl.grabtime = 0;
744    
745     if (!XGrabPointer (THIS->display->display, THIS->vt, 0,
746     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
747     mode, mode, None, None, eventtime))
748     if (!XGrabKeyboard (THIS->display->display, THIS->vt, 0, mode, mode, eventtime))
749     THIS->perl.grabtime = eventtime;
750     else
751     XUngrabPointer (THIS->display->display, eventtime);
752    
753     RETVAL = !!THIS->perl.grabtime;
754 root 1.35 }
755 root 1.36 OUTPUT:
756     RETVAL
757    
758     void
759     rxvt_term::allow_events_async (U32 eventtime = THIS->perl.grabtime)
760     CODE:
761     XAllowEvents (THIS->display->display, AsyncBoth, eventtime);
762    
763     void
764     rxvt_term::allow_events_sync (U32 eventtime = THIS->perl.grabtime)
765     CODE:
766     XAllowEvents (THIS->display->display, SyncBoth, eventtime);
767    
768     void
769     rxvt_term::allow_events_replay (U32 eventtime = THIS->perl.grabtime)
770     CODE:
771     XAllowEvents (THIS->display->display, ReplayPointer, eventtime);
772     XAllowEvents (THIS->display->display, ReplayKeyboard, eventtime);
773    
774     void
775     rxvt_term::ungrab (U32 eventtime = THIS->perl.grabtime)
776     CODE:
777     THIS->perl.grabtime = 0;
778     XUngrabKeyboard (THIS->display->display, eventtime);
779     XUngrabPointer (THIS->display->display, eventtime);
780 root 1.35
781 root 1.1 int
782 root 1.3 rxvt_term::strwidth (SV *str)
783 root 1.1 CODE:
784     {
785     wchar_t *wstr = sv2wcs (str);
786 root 1.3
787     rxvt_push_locale (THIS->locale);
788 root 1.1 RETVAL = wcswidth (wstr, wcslen (wstr));
789 root 1.3 rxvt_pop_locale ();
790    
791 root 1.1 free (wstr);
792     }
793     OUTPUT:
794     RETVAL
795    
796 root 1.3 SV *
797     rxvt_term::locale_encode (SV *str)
798 root 1.1 CODE:
799 root 1.3 {
800     wchar_t *wstr = sv2wcs (str);
801    
802     rxvt_push_locale (THIS->locale);
803     char *mbstr = rxvt_wcstombs (wstr);
804     rxvt_pop_locale ();
805    
806     free (wstr);
807    
808     RETVAL = newSVpv (mbstr, 0);
809     free (mbstr);
810     }
811     OUTPUT:
812 root 1.1 RETVAL
813    
814 root 1.3 SV *
815     rxvt_term::locale_decode (SV *octets)
816     CODE:
817     {
818     STRLEN len;
819     char *data = SvPVbyte (octets, len);
820    
821     rxvt_push_locale (THIS->locale);
822     wchar_t *wstr = rxvt_mbstowcs (data, len);
823     rxvt_pop_locale ();
824    
825 root 1.25 RETVAL = wcs2sv (wstr);
826 root 1.3 free (wstr);
827     }
828     OUTPUT:
829     RETVAL
830 root 1.1
831 root 1.38 #define TERM_OFFSET(sym) offsetof (TermWin_t, sym)
832 root 1.24
833     #define TERM_OFFSET_width TERM_OFFSET(width)
834     #define TERM_OFFSET_height TERM_OFFSET(height)
835     #define TERM_OFFSET_fwidth TERM_OFFSET(fwidth)
836     #define TERM_OFFSET_fheight TERM_OFFSET(fheight)
837     #define TERM_OFFSET_fbase TERM_OFFSET(fbase)
838     #define TERM_OFFSET_nrow TERM_OFFSET(nrow)
839     #define TERM_OFFSET_ncol TERM_OFFSET(ncol)
840     #define TERM_OFFSET_focus TERM_OFFSET(focus)
841     #define TERM_OFFSET_mapped TERM_OFFSET(mapped)
842     #define TERM_OFFSET_saveLines TERM_OFFSET(saveLines)
843     #define TERM_OFFSET_total_rows TERM_OFFSET(total_rows)
844     #define TERM_OFFSET_nsaved TERM_OFFSET(nsaved)
845    
846     int
847     rxvt_term::width ()
848     ALIAS:
849     width = TERM_OFFSET_width
850     height = TERM_OFFSET_height
851     fwidth = TERM_OFFSET_fwidth
852     fheight = TERM_OFFSET_fheight
853     fbase = TERM_OFFSET_fbase
854     nrow = TERM_OFFSET_nrow
855     ncol = TERM_OFFSET_ncol
856     focus = TERM_OFFSET_focus
857     mapped = TERM_OFFSET_mapped
858     saveLines = TERM_OFFSET_saveLines
859     total_rows = TERM_OFFSET_total_rows
860     nsaved = TERM_OFFSET_nsaved
861     CODE:
862     RETVAL = *(int *)((char *)THIS + ix);
863     OUTPUT:
864     RETVAL
865    
866 root 1.38 unsigned int
867     rxvt_term::ModLevel3Mask ()
868     ALIAS:
869     ModLevel3Mask = 0
870     ModMetaMask = 1
871     ModNumLockMask = 2
872     CODE:
873     switch (ix)
874     {
875     case 0: RETVAL = THIS->ModLevel3Mask; break;
876     case 1: RETVAL = THIS->ModMetaMask; break;
877     case 2: RETVAL = THIS->ModNumLockMask; break;
878     }
879     OUTPUT:
880     RETVAL
881    
882 root 1.24 U32
883 root 1.31 rxvt_term::parent ()
884     CODE:
885     RETVAL = (U32)THIS->parent [0];
886     OUTPUT:
887     RETVAL
888    
889     U32
890 root 1.30 rxvt_term::vt ()
891     CODE:
892 root 1.31 RETVAL = (U32)THIS->vt;
893 root 1.30 OUTPUT:
894     RETVAL
895    
896     U32
897 root 1.25 rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
898 root 1.7 CODE:
899 root 1.24 {
900 root 1.25 RETVAL = THIS->rstyle;
901     THIS->rstyle = new_rstyle;
902 root 1.24 }
903 root 1.7 OUTPUT:
904 root 1.24 RETVAL
905 root 1.7
906     int
907     rxvt_term::view_start (int newval = -1)
908     CODE:
909     {
910     RETVAL = THIS->view_start;
911    
912     if (newval >= 0)
913     {
914     THIS->view_start = min (newval, THIS->nsaved);
915     THIS->scr_changeview (RETVAL);
916     }
917     }
918     OUTPUT:
919     RETVAL
920    
921 root 1.8 void
922 root 1.9 rxvt_term::want_refresh ()
923     CODE:
924     THIS->want_refresh = 1;
925    
926     void
927 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)
928 root 1.8 PPCODE:
929     {
930     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
931 root 1.19 XSRETURN_EMPTY;
932 root 1.8
933     line_t &l = ROW(row_number);
934    
935     if (GIMME_V != G_VOID)
936     {
937     wchar_t *wstr = new wchar_t [THIS->ncol];
938    
939 root 1.33 for (int col = 0; col < THIS->ncol; col++)
940 root 1.8 wstr [col] = l.t [col];
941    
942 root 1.33 XPUSHs (sv_2mortal (wcs2sv (wstr, THIS->ncol)));
943 root 1.8
944 root 1.25 delete [] wstr;
945 root 1.8 }
946    
947     if (new_text)
948     {
949 root 1.13 wchar_t *wstr = sv2wcs (new_text);
950 root 1.8
951 root 1.27 int len = min (wcslen (wstr) - start_ofs, max_len);
952 root 1.8
953 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
954 root 1.8 {
955     free (wstr);
956 root 1.10 croak ("new_text extends beyond horizontal margins");
957 root 1.8 }
958    
959     for (int col = start_col; col < start_col + len; col++)
960     {
961 root 1.27 l.t [col] = wstr [start_ofs + col - start_col];
962 root 1.8 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
963     }
964 root 1.9
965     free (wstr);
966 root 1.8 }
967     }
968    
969     void
970 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)
971 root 1.8 PPCODE:
972     {
973     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
974 root 1.19 XSRETURN_EMPTY;
975 root 1.8
976     line_t &l = ROW(row_number);
977    
978     if (GIMME_V != G_VOID)
979     {
980     AV *av = newAV ();
981    
982     av_extend (av, THIS->ncol - 1);
983     for (int col = 0; col < THIS->ncol; col++)
984     av_store (av, col, newSViv (l.r [col]));
985    
986     XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
987     }
988    
989     if (new_rend)
990     {
991     if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
992     croak ("new_rend must be arrayref");
993    
994     AV *av = (AV *)SvRV (new_rend);
995 root 1.27 int len = min (av_len (av) + 1 - start_ofs, max_len);
996 root 1.8
997 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
998     croak ("new_rend array extends beyond horizontal margins");
999 root 1.8
1000     for (int col = start_col; col < start_col + len; col++)
1001     {
1002 root 1.27 rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask;
1003 root 1.8
1004     l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
1005     }
1006     }
1007     }
1008    
1009     int
1010 root 1.26 rxvt_term::ROW_l (int row_number, int new_length = -1)
1011 root 1.8 CODE:
1012     {
1013     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1014 root 1.19 XSRETURN_EMPTY;
1015 root 1.8
1016     line_t &l = ROW(row_number);
1017 root 1.26 RETVAL = l.l;
1018 root 1.8
1019 root 1.26 if (new_length >= 0)
1020 root 1.8 l.l = new_length;
1021     }
1022     OUTPUT:
1023     RETVAL
1024    
1025 root 1.19 bool
1026 root 1.26 rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
1027 root 1.19 CODE:
1028     {
1029     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1030     XSRETURN_EMPTY;
1031    
1032     line_t &l = ROW(row_number);
1033 root 1.26 RETVAL = l.is_longer ();
1034    
1035     if (new_is_longer >= 0)
1036     l.is_longer (new_is_longer);
1037 root 1.19 }
1038     OUTPUT:
1039     RETVAL
1040    
1041 root 1.8 SV *
1042 root 1.33 rxvt_term::special_encode (SV *string)
1043 root 1.8 CODE:
1044 root 1.33 {
1045     wchar_t *wstr = sv2wcs (string);
1046     int wlen = wcslen (wstr);
1047     wchar_t *rstr = new wchar_t [wlen]; // cannot become longer
1048    
1049     rxvt_push_locale (THIS->locale);
1050    
1051     wchar_t *r = rstr;
1052     for (wchar_t *s = wstr; *s; s++)
1053     if (wcwidth (*s) == 0)
1054     {
1055     if (r == rstr)
1056     croak ("leading combining character unencodable");
1057    
1058     unicode_t n = rxvt_compose (r[-1], *s);
1059     if (n == NOCHAR)
1060     n = rxvt_composite.compose (r[-1], *s);
1061    
1062     r[-1] = n;
1063     }
1064     #if !UNICODE_3
1065     else if (*s >= 0x10000)
1066     *r++ = rxvt_composite.compose (*s);
1067     #endif
1068     else
1069     *r++ = *s;
1070    
1071     rxvt_pop_locale ();
1072    
1073     RETVAL = wcs2sv (rstr, r - rstr);
1074    
1075     delete [] rstr;
1076     }
1077     OUTPUT:
1078     RETVAL
1079 root 1.8
1080     SV *
1081 root 1.33 rxvt_term::special_decode (SV *text)
1082 root 1.8 CODE:
1083 root 1.33 {
1084     wchar_t *wstr = sv2wcs (text);
1085     int wlen = wcslen (wstr);
1086     int dlen = 0;
1087    
1088     // find length
1089     for (wchar_t *s = wstr; *s; s++)
1090     if (*s == NOCHAR)
1091     ;
1092     else if (IS_COMPOSE (*s))
1093     dlen += rxvt_composite.expand (*s, 0);
1094     else
1095     dlen++;
1096    
1097     wchar_t *rstr = new wchar_t [dlen];
1098    
1099     // decode
1100     wchar_t *r = rstr;
1101     for (wchar_t *s = wstr; *s; s++)
1102     if (*s == NOCHAR)
1103     ;
1104     else if (IS_COMPOSE (*s))
1105     r += rxvt_composite.expand (*s, r);
1106     else
1107     *r++ = *s;
1108    
1109     RETVAL = wcs2sv (rstr, r - rstr);
1110    
1111     delete [] rstr;
1112     }
1113     OUTPUT:
1114     RETVAL
1115 root 1.8
1116 root 1.1 void
1117 root 1.2 rxvt_term::_resource (char *name, int index, SV *newval = 0)
1118     PPCODE:
1119     {
1120     struct resval { const char *name; int value; } rslist [] = {
1121 root 1.21 # define def(name) { # name, Rs_ ## name },
1122     # define reserve(name,count)
1123 root 1.2 # include "rsinc.h"
1124 root 1.21 # undef def
1125     # undef reserve
1126 root 1.2 };
1127    
1128     struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
1129    
1130     do {
1131     if (rs-- == rslist)
1132     croak ("no such resource '%s', requested", name);
1133     } while (strcmp (name, rs->name));
1134    
1135     index += rs->value;
1136    
1137     if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
1138     croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
1139    
1140     if (GIMME_V != G_VOID)
1141     XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
1142    
1143     if (newval)
1144     {
1145     if (SvOK (newval))
1146     {
1147     char *str = strdup (SvPVbyte_nolen (newval));
1148     THIS->rs [index] = str;
1149     THIS->allocated.push_back (str);
1150     }
1151     else
1152     THIS->rs [index] = 0;
1153     }
1154     }
1155    
1156     void
1157 root 1.24 rxvt_term::cur (...)
1158 root 1.1 PROTOTYPE: $;$$
1159     ALIAS:
1160 root 1.24 screen_cur = 0
1161     selection_beg = 1
1162     selection_end = 2
1163     selection_mark = 3
1164 root 1.1 PPCODE:
1165     {
1166 root 1.24 row_col_t &rc = ix == 0 ? THIS->screen.cur
1167     : ix == 1 ? THIS->selection.beg
1168     : ix == 2 ? THIS->selection.end
1169     : THIS->selection.mark;
1170 root 1.1
1171     if (GIMME_V != G_VOID)
1172     {
1173     EXTEND (SP, 2);
1174 root 1.24 PUSHs (sv_2mortal (newSViv (rc.row)));
1175     PUSHs (sv_2mortal (newSViv (rc.col)));
1176 root 1.1 }
1177    
1178     if (items == 3)
1179     {
1180 root 1.24 rc.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
1181     rc.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
1182 root 1.1
1183     if (ix)
1184     THIS->want_refresh = 1;
1185     }
1186     }
1187    
1188     int
1189 root 1.36 rxvt_term::selection_grab (U32 eventtime)
1190 root 1.1
1191     void
1192     rxvt_term::selection (SV *newtext = 0)
1193     PPCODE:
1194     {
1195     if (GIMME_V != G_VOID)
1196 root 1.25 XPUSHs (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)));
1197 root 1.1
1198     if (newtext)
1199     {
1200     free (THIS->selection.text);
1201    
1202     THIS->selection.text = sv2wcs (newtext);
1203     THIS->selection.len = wcslen (THIS->selection.text);
1204     }
1205     }
1206 root 1.24
1207 root 1.1 void
1208 root 1.25 rxvt_term::scr_add_lines (SV *string)
1209     CODE:
1210     {
1211     wchar_t *wstr = sv2wcs (string);
1212 root 1.32 THIS->scr_add_lines (wstr, wcslen (wstr));
1213 root 1.25 free (wstr);
1214     }
1215    
1216     void
1217 root 1.13 rxvt_term::tt_write (SV *octets)
1218     INIT:
1219     STRLEN len;
1220     char *str = SvPVbyte (octets, len);
1221     C_ARGS:
1222 root 1.23 str, len
1223 root 1.13
1224 root 1.28 void
1225     rxvt_term::cmd_parse (SV *octets)
1226     CODE:
1227     {
1228     STRLEN len;
1229     char *str = SvPVbyte (octets, len);
1230    
1231     char *old_cmdbuf_ptr = THIS->cmdbuf_ptr;
1232     char *old_cmdbuf_endp = THIS->cmdbuf_endp;
1233    
1234     THIS->cmdbuf_ptr = str;
1235     THIS->cmdbuf_endp = str + len;
1236    
1237 root 1.35 rxvt_push_locale (THIS->locale);
1238 root 1.28 THIS->cmd_parse ();
1239 root 1.35 rxvt_pop_locale ();
1240 root 1.28
1241     THIS->cmdbuf_ptr = old_cmdbuf_ptr;
1242     THIS->cmdbuf_endp = old_cmdbuf_endp;
1243     }
1244    
1245 root 1.13 SV *
1246     rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
1247     CODE:
1248     {
1249     overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
1250     RETVAL = newSVptr ((void *)o, "urxvt::overlay");
1251     o->self = (HV *)SvRV (RETVAL);
1252     }
1253     OUTPUT:
1254     RETVAL
1255    
1256     MODULE = urxvt PACKAGE = urxvt::overlay
1257 root 1.1
1258     void
1259 root 1.13 overlay::set (int x, int y, SV *text, SV *rend = 0)
1260 root 1.1
1261     void
1262 root 1.18 overlay::show ()
1263    
1264     void
1265     overlay::hide ()
1266    
1267     void
1268 root 1.13 overlay::DESTROY ()
1269 root 1.1
1270     MODULE = urxvt PACKAGE = urxvt::timer
1271    
1272     SV *
1273     timer::new ()
1274     CODE:
1275     timer *w = new timer;
1276 root 1.13 w->start (NOW);
1277 root 1.1 RETVAL = newSVptr ((void *)w, "urxvt::timer");
1278     w->self = (HV *)SvRV (RETVAL);
1279     OUTPUT:
1280     RETVAL
1281    
1282     timer *
1283     timer::cb (SV *cb)
1284     CODE:
1285     THIS->cb (cb);
1286     RETVAL = THIS;
1287     OUTPUT:
1288     RETVAL
1289    
1290     NV
1291     timer::at ()
1292     CODE:
1293     RETVAL = THIS->at;
1294     OUTPUT:
1295     RETVAL
1296    
1297     timer *
1298 root 1.13 timer::interval (NV interval)
1299     CODE:
1300     THIS->interval = interval;
1301     RETVAL = THIS;
1302     OUTPUT:
1303     RETVAL
1304    
1305     timer *
1306 root 1.1 timer::set (NV tstamp)
1307     CODE:
1308     THIS->set (tstamp);
1309     RETVAL = THIS;
1310     OUTPUT:
1311     RETVAL
1312    
1313     timer *
1314     timer::start (NV tstamp = THIS->at)
1315     CODE:
1316     THIS->start (tstamp);
1317     RETVAL = THIS;
1318     OUTPUT:
1319     RETVAL
1320    
1321     timer *
1322     timer::stop ()
1323     CODE:
1324     THIS->stop ();
1325     RETVAL = THIS;
1326     OUTPUT:
1327     RETVAL
1328    
1329     void
1330     timer::DESTROY ()
1331    
1332     MODULE = urxvt PACKAGE = urxvt::iow
1333    
1334     SV *
1335     iow::new ()
1336     CODE:
1337     iow *w = new iow;
1338     RETVAL = newSVptr ((void *)w, "urxvt::iow");
1339     w->self = (HV *)SvRV (RETVAL);
1340     OUTPUT:
1341     RETVAL
1342    
1343     iow *
1344     iow::cb (SV *cb)
1345     CODE:
1346     THIS->cb (cb);
1347     RETVAL = THIS;
1348     OUTPUT:
1349     RETVAL
1350    
1351     iow *
1352     iow::fd (int fd)
1353     CODE:
1354     THIS->fd = fd;
1355     RETVAL = THIS;
1356     OUTPUT:
1357     RETVAL
1358    
1359     iow *
1360     iow::events (short events)
1361     CODE:
1362     THIS->events = events;
1363     RETVAL = THIS;
1364     OUTPUT:
1365     RETVAL
1366    
1367     iow *
1368     iow::start ()
1369     CODE:
1370     THIS->start ();
1371     RETVAL = THIS;
1372     OUTPUT:
1373     RETVAL
1374    
1375     iow *
1376     iow::stop ()
1377     CODE:
1378     THIS->stop ();
1379     RETVAL = THIS;
1380     OUTPUT:
1381     RETVAL
1382    
1383     void
1384     iow::DESTROY ()
1385    
1386