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