ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.93
Committed: Wed Jan 25 15:02:47 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-7_3
Changes since 1.92: +26 -24 lines
Log Message:
*** empty log message ***

File Contents

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