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