ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.14
Committed: Tue Jan 3 04:45:03 2006 UTC (18 years, 6 months ago) by root
Branch: MAIN
Changes since 1.13: +16 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*----------------------------------------------------------------------*
2     * File: rxvtperl.xs
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 2005-2005 Marc Lehmann <pcg@goof.com>
7     *
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     #include <cstdarg>
32    
33     #include "rxvt.h"
34     #include "iom.h"
35     #include "rxvtutil.h"
36     #include "rxvtperl.h"
37    
38     #include "perlxsi.c"
39    
40 root 1.8 #undef LINENO
41     #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
42     #undef ROW
43     #define ROW(n) THIS->row_buf [LINENO (n)]
44    
45 root 1.1 /////////////////////////////////////////////////////////////////////////////
46    
47     static wchar_t *
48     sv2wcs (SV *sv)
49     {
50     STRLEN len;
51     char *str = SvPVutf8 (sv, len);
52     return rxvt_utf8towcs (str, len);
53     }
54    
55     static SV *
56     new_ref (HV *hv, const char *klass)
57     {
58     return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
59     }
60    
61     //TODO: use magic
62     static SV *
63     newSVptr (void *ptr, const char *klass)
64     {
65     HV *hv = newHV ();
66     hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0);
67     return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
68     }
69    
70     static long
71     SvPTR (SV *sv, const char *klass)
72     {
73     if (!sv_derived_from (sv, klass))
74     croak ("object of type %s expected", klass);
75    
76     IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1));
77    
78     if (!iv)
79     croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
80    
81     return (long)iv;
82     }
83    
84     #define newSVterm(term) SvREFCNT_inc ((SV *)term->self)
85     #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
86    
87     /////////////////////////////////////////////////////////////////////////////
88    
89     struct perl_watcher
90     {
91     SV *cbsv;
92     HV *self;
93    
94     perl_watcher ()
95     : cbsv (newSV (0))
96     {
97     }
98    
99     ~perl_watcher ()
100     {
101     SvREFCNT_dec (cbsv);
102     }
103    
104     void cb (SV *cb)
105     {
106     sv_setsv (cbsv, cb);
107     }
108    
109     void invoke (const char *type, SV *self, int arg = -1);
110     };
111    
112     void
113     perl_watcher::invoke (const char *type, SV *self, int arg)
114     {
115     dSP;
116    
117     ENTER;
118     SAVETMPS;
119    
120     PUSHMARK (SP);
121    
122     XPUSHs (sv_2mortal (self));
123    
124     if (arg >= 0)
125     XPUSHs (sv_2mortal (newSViv (arg)));
126    
127     PUTBACK;
128     call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
129     SPAGAIN;
130    
131     PUTBACK;
132     FREETMPS;
133     LEAVE;
134    
135     if (SvTRUE (ERRSV))
136     rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
137     }
138    
139     #define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
140     #define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
141    
142     struct timer : time_watcher, perl_watcher
143     {
144 root 1.13 tstamp interval;
145    
146 root 1.1 timer ()
147     : time_watcher (this, &timer::execute)
148     {
149     }
150    
151     void execute (time_watcher &w)
152     {
153 root 1.13 if (interval)
154     start (at + interval);
155    
156 root 1.1 invoke ("urxvt::timer", newSVtimer (this));
157     }
158     };
159    
160     #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
161     #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
162    
163     struct iow : io_watcher, perl_watcher
164     {
165     iow ()
166     : io_watcher (this, &iow::execute)
167     {
168     }
169    
170     void execute (io_watcher &w, short revents)
171     {
172     invoke ("urxvt::iow", newSViow (this), revents);
173     }
174     };
175    
176     /////////////////////////////////////////////////////////////////////////////
177    
178 root 1.13 #define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
179    
180     struct overlay {
181     HV *self;
182     rxvt_term *THIS;
183     int x, y, w, h;
184     int border;
185     text_t **text;
186     rend_t **rend;
187    
188     overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
189     ~overlay ();
190    
191     void swap ();
192    
193     void set (int x, int y, SV *str, SV *rend);
194     };
195    
196     overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
197     : THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2)
198     {
199     if (border == 2)
200     {
201     w += 2;
202     h += 2;
203     }
204    
205     text = new text_t *[h];
206     rend = new rend_t *[h];
207    
208     for (int y = 0; y < h; y++)
209     {
210     text_t *tp = text[y] = new text_t[w];
211     rend_t *rp = rend[y] = new rend_t[w];
212    
213     text_t t0, t1, t2;
214     rend_t r = rstyle;
215    
216     if (border == 2)
217     {
218     if (y == 0)
219     t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
220     else if (y < h - 1)
221     t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
222     else
223     t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
224    
225     *tp++ = t0;
226     *rp++ = r;
227    
228     for (int x = w - 2; x-- > 0; )
229     {
230     *tp++ = t1;
231     *rp++ = r;
232     }
233    
234     *tp = t2;
235     *rp = r;
236     }
237     else
238     for (int x = w; x-- > 0; )
239     {
240     *tp++ = 0x0020;
241     *rp++ = r;
242     }
243     }
244    
245     THIS->want_refresh = 1;
246     }
247    
248     overlay::~overlay ()
249     {
250     for (int y = h; y--; )
251     {
252     delete [] text[y];
253     delete [] rend[y];
254     }
255    
256     delete [] text;
257     delete [] rend;
258    
259     THIS->want_refresh = 1;
260     }
261    
262     void overlay::swap ()
263     {
264     int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
265     int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
266    
267     int ov_w = min (w, THIS->ncol - ov_x);
268     int ov_h = min (h, THIS->nrow - ov_y);
269    
270     for (int y = ov_h; y--; )
271     {
272     text_t *t1 = text [y];
273     rend_t *r1 = rend [y];
274    
275     text_t *t2 = ROW(y + ov_y - THIS->view_start).t + ov_x;
276     rend_t *r2 = ROW(y + ov_y - THIS->view_start).r + ov_x;
277    
278     for (int x = ov_w; x--; )
279     {
280     text_t t = *t1; *t1++ = *t2; *t2++ = t;
281     rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
282     }
283     }
284    
285     }
286    
287 root 1.14 void overlay::set (int x, int y, SV *text, SV *rend)
288 root 1.13 {
289     x += border;
290     y += border;
291    
292     if (!IN_RANGE_EXC (y, 0, h - border))
293     return;
294    
295 root 1.14 wchar_t *wtext = sv2wcs (text);
296 root 1.13
297 root 1.14 for (int col = min (wcslen (wtext), w - x - border); col--; )
298     this->text [y][x + col] = wtext [col];
299 root 1.13
300 root 1.14 free (wtext);
301    
302     if (rend)
303     {
304     if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
305     croak ("rend must be arrayref");
306    
307     AV *av = (AV *)SvRV (rend);
308    
309     for (int col = min (av_len (av) + 1, w - x - border); col--; )
310     this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
311     }
312 root 1.13
313     THIS->want_refresh = 1;
314     }
315    
316    
317     /////////////////////////////////////////////////////////////////////////////
318    
319 root 1.1 struct rxvt_perl_interp rxvt_perl;
320    
321     static PerlInterpreter *perl;
322    
323     rxvt_perl_interp::rxvt_perl_interp ()
324     {
325     }
326    
327     rxvt_perl_interp::~rxvt_perl_interp ()
328     {
329     if (perl)
330     {
331     perl_destruct (perl);
332     perl_free (perl);
333     }
334     }
335    
336     void
337     rxvt_perl_interp::init ()
338     {
339     if (!perl)
340     {
341     char *argv[] = {
342     "",
343     "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
344     };
345    
346     perl = perl_alloc ();
347     perl_construct (perl);
348    
349     if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
350     || perl_run (perl))
351     {
352     rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
353    
354     perl_destruct (perl);
355     perl_free (perl);
356     perl = 0;
357     }
358     }
359     }
360    
361     bool
362     rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
363     {
364 root 1.13 if (!perl)
365 root 1.1 return false;
366 root 1.4
367 root 1.1 if (htype == HOOK_INIT) // first hook ever called
368 root 1.13 {
369     term->self = (void *)newSVptr ((void *)term, "urxvt::term");
370     hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
371     }
372     else if (htype == HOOK_DESTROY)
373     {
374     // handled later
375     }
376     else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
377     {
378     HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0));
379    
380     if (HvKEYS (hv))
381     {
382     hv_iterinit (hv);
383    
384     while (HE *he = hv_iternext (hv))
385     ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
386     }
387     }
388     else if (!should_invoke [htype])
389     return false;
390 root 1.1
391     dSP;
392     va_list ap;
393    
394     va_start (ap, htype);
395    
396     ENTER;
397     SAVETMPS;
398    
399     PUSHMARK (SP);
400    
401     XPUSHs (sv_2mortal (newSVterm (term)));
402     XPUSHs (sv_2mortal (newSViv (htype)));
403    
404     for (;;) {
405     data_type dt = (data_type)va_arg (ap, int);
406    
407     switch (dt)
408     {
409     case DT_INT:
410     XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
411     break;
412    
413     case DT_LONG:
414     XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
415     break;
416    
417 root 1.6 case DT_STRING:
418     XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
419     break;
420    
421 root 1.1 case DT_END:
422     {
423     va_end (ap);
424    
425     PUTBACK;
426     int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
427     SPAGAIN;
428    
429     if (count)
430     {
431     SV *status = POPs;
432     count = SvTRUE (status);
433     }
434    
435     PUTBACK;
436     FREETMPS;
437     LEAVE;
438    
439     if (SvTRUE (ERRSV))
440     rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
441    
442 root 1.4 if (htype == HOOK_DESTROY)
443     {
444     // TODO: clear magic
445     hv_clear ((HV *)SvRV ((SV *)term->self));
446     SvREFCNT_dec ((SV *)term->self);
447     }
448    
449 root 1.1 return count;
450     }
451    
452     default:
453     rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
454     }
455     }
456     }
457    
458     /////////////////////////////////////////////////////////////////////////////
459    
460     MODULE = urxvt PACKAGE = urxvt
461    
462     PROTOTYPES: ENABLE
463    
464     BOOT:
465     {
466     # define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0))
467 root 1.11 # define export_const(name) newCONSTSUB (gv_stashpv ("urxvt", 1), #name, newSViv (name));
468 root 1.1 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
469     set_hookname (INIT);
470     set_hookname (RESET);
471     set_hookname (START);
472     set_hookname (DESTROY);
473     set_hookname (SEL_BEGIN);
474     set_hookname (SEL_EXTEND);
475     set_hookname (SEL_MAKE);
476     set_hookname (SEL_GRAB);
477     set_hookname (FOCUS_IN);
478     set_hookname (FOCUS_OUT);
479     set_hookname (VIEW_CHANGE);
480     set_hookname (SCROLL_BACK);
481     set_hookname (TTY_ACTIVITY);
482     set_hookname (REFRESH_BEGIN);
483     set_hookname (REFRESH_END);
484 root 1.6 set_hookname (KEYBOARD_COMMAND);
485 root 1.1
486 root 1.11 export_const (DEFAULT_RSTYLE);
487     export_const (OVERLAY_RSTYLE);
488     export_const (RS_Bold);
489     export_const (RS_Italic);
490     export_const (RS_Blink);
491     export_const (RS_RVid);
492     export_const (RS_Uline);
493 root 1.9
494 root 1.1 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR);
495     }
496    
497     void
498     set_should_invoke (int htype, int value)
499     CODE:
500     rxvt_perl.should_invoke [htype] = value;
501    
502     void
503     warn (const char *msg)
504     CODE:
505     rxvt_warn ("%s", msg);
506    
507     void
508     fatal (const char *msg)
509     CODE:
510     rxvt_fatal ("%s", msg);
511    
512 root 1.3 NV
513     NOW ()
514     CODE:
515     RETVAL = NOW;
516     OUTPUT:
517     RETVAL
518    
519 root 1.11 int
520     GET_BASEFG (int rend)
521     CODE:
522     RETVAL = GET_BASEFG (rend);
523     OUTPUT:
524     RETVAL
525    
526     int
527     GET_BASEBG (int rend)
528     CODE:
529     RETVAL = GET_BASEBG (rend);
530     OUTPUT:
531     RETVAL
532    
533     int
534 root 1.12 SET_FGCOLOR (int rend, int new_color)
535 root 1.11 CODE:
536 root 1.12 RETVAL = SET_FGCOLOR (rend, new_color);
537 root 1.11 OUTPUT:
538     RETVAL
539    
540     int
541 root 1.12 SET_BGCOLOR (int rend, int new_color)
542 root 1.11 CODE:
543 root 1.12 RETVAL = SET_BGCOLOR (rend, new_color);
544     OUTPUT:
545     RETVAL
546    
547     int
548     GET_CUSTOM (int rend)
549     CODE:
550     RETVAL = (rend && RS_customMask) >> RS_customShift;
551     OUTPUT:
552     RETVAL
553    
554     int
555     SET_CUSTOM (int rend, int new_value)
556     CODE:
557     {
558     if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
559     croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
560    
561     RETVAL = (rend & ~RS_customMask)
562     | ((new_value << RS_customShift) & RS_customMask);
563     }
564 root 1.11 OUTPUT:
565     RETVAL
566    
567 root 1.3 MODULE = urxvt PACKAGE = urxvt::term
568    
569 root 1.1 int
570 root 1.3 rxvt_term::strwidth (SV *str)
571 root 1.1 CODE:
572     {
573     wchar_t *wstr = sv2wcs (str);
574 root 1.3
575     rxvt_push_locale (THIS->locale);
576 root 1.1 RETVAL = wcswidth (wstr, wcslen (wstr));
577 root 1.3 rxvt_pop_locale ();
578    
579 root 1.1 free (wstr);
580     }
581     OUTPUT:
582     RETVAL
583    
584 root 1.3 SV *
585     rxvt_term::locale_encode (SV *str)
586 root 1.1 CODE:
587 root 1.3 {
588     wchar_t *wstr = sv2wcs (str);
589    
590     rxvt_push_locale (THIS->locale);
591     char *mbstr = rxvt_wcstombs (wstr);
592     rxvt_pop_locale ();
593    
594     free (wstr);
595    
596     RETVAL = newSVpv (mbstr, 0);
597     free (mbstr);
598     }
599     OUTPUT:
600 root 1.1 RETVAL
601    
602 root 1.3 SV *
603     rxvt_term::locale_decode (SV *octets)
604     CODE:
605     {
606     STRLEN len;
607     char *data = SvPVbyte (octets, len);
608    
609     rxvt_push_locale (THIS->locale);
610     wchar_t *wstr = rxvt_mbstowcs (data, len);
611     rxvt_pop_locale ();
612    
613     char *str = rxvt_wcstoutf8 (wstr);
614     free (wstr);
615    
616     RETVAL = newSVpv (str, 0);
617     SvUTF8_on (RETVAL);
618     free (str);
619     }
620     OUTPUT:
621     RETVAL
622 root 1.1
623 root 1.7 int
624     rxvt_term::nsaved ()
625     CODE:
626     RETVAL = THIS->nsaved;
627     OUTPUT:
628     RETVAL
629    
630     int
631     rxvt_term::view_start (int newval = -1)
632     CODE:
633     {
634     RETVAL = THIS->view_start;
635    
636     if (newval >= 0)
637     {
638     THIS->view_start = min (newval, THIS->nsaved);
639     THIS->scr_changeview (RETVAL);
640     }
641     }
642     OUTPUT:
643     RETVAL
644    
645 root 1.8 int
646     rxvt_term::nrow ()
647     CODE:
648     RETVAL = THIS->nrow;
649     OUTPUT:
650     RETVAL
651    
652     int
653     rxvt_term::ncol ()
654     CODE:
655     RETVAL = THIS->ncol;
656     OUTPUT:
657     RETVAL
658    
659     void
660 root 1.9 rxvt_term::want_refresh ()
661     CODE:
662     THIS->want_refresh = 1;
663    
664     void
665 root 1.8 rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0)
666     PPCODE:
667     {
668     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
669     croak ("row_number number of out range");
670    
671     line_t &l = ROW(row_number);
672    
673     if (GIMME_V != G_VOID)
674     {
675     wchar_t *wstr = new wchar_t [THIS->ncol];
676    
677     for (int col = 0; col <THIS->ncol; col++)
678     wstr [col] = l.t [col];
679    
680     char *str = rxvt_wcstoutf8 (wstr, THIS->ncol);
681     free (wstr);
682    
683     SV *sv = newSVpv (str, 0);
684     SvUTF8_on (sv);
685     XPUSHs (sv_2mortal (sv));
686     free (str);
687     }
688    
689     if (new_text)
690     {
691 root 1.13 wchar_t *wstr = sv2wcs (new_text);
692 root 1.8
693     int len = wcslen (wstr);
694    
695 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
696 root 1.8 {
697     free (wstr);
698 root 1.10 croak ("new_text extends beyond horizontal margins");
699 root 1.8 }
700    
701     for (int col = start_col; col < start_col + len; col++)
702     {
703 root 1.9 l.t [col] = wstr [col - start_col];
704 root 1.8 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
705     }
706 root 1.9
707     free (wstr);
708 root 1.8 }
709     }
710    
711     void
712     rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0)
713     PPCODE:
714     {
715     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
716     croak ("row_number number of out range");
717    
718     line_t &l = ROW(row_number);
719    
720     if (GIMME_V != G_VOID)
721     {
722     AV *av = newAV ();
723    
724     av_extend (av, THIS->ncol - 1);
725     for (int col = 0; col < THIS->ncol; col++)
726     av_store (av, col, newSViv (l.r [col]));
727    
728     XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
729     }
730    
731     if (new_rend)
732     {
733     if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
734     croak ("new_rend must be arrayref");
735    
736     AV *av = (AV *)SvRV (new_rend);
737     int len = av_len (av) + 1;
738    
739 root 1.10 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
740     croak ("new_rend array extends beyond horizontal margins");
741 root 1.8
742     for (int col = start_col; col < start_col + len; col++)
743     {
744 root 1.9 rend_t r = SvIV (*av_fetch (av, col - start_col, 1)) & ~RS_fontMask;
745 root 1.8
746     l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
747     }
748     }
749     }
750    
751     int
752     rxvt_term::ROW_l (int row_number, int new_length = -2)
753     CODE:
754     {
755     if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
756     croak ("row_number number of out range");
757    
758     line_t &l = ROW(row_number);
759     RETVAL = l.l;
760    
761     if (new_length >= -1)
762     l.l = new_length;
763     }
764     OUTPUT:
765     RETVAL
766    
767     SV *
768     rxvt_term::special_encode (SV *str)
769     CODE:
770     abort ();//TODO
771    
772     SV *
773     rxvt_term::special_decode (SV *str)
774     CODE:
775     abort ();//TODO
776    
777 root 1.1 void
778 root 1.2 rxvt_term::_resource (char *name, int index, SV *newval = 0)
779     PPCODE:
780     {
781     struct resval { const char *name; int value; } rslist [] = {
782     # define Rs_def(name) { # name, Rs_ ## name },
783     # define Rs_reserve(name,count)
784     # include "rsinc.h"
785     # undef Rs_def
786     # undef Rs_reserve
787     };
788    
789     struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
790    
791     do {
792     if (rs-- == rslist)
793     croak ("no such resource '%s', requested", name);
794     } while (strcmp (name, rs->name));
795    
796     index += rs->value;
797    
798     if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
799     croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
800    
801     if (GIMME_V != G_VOID)
802     XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
803    
804     if (newval)
805     {
806     if (SvOK (newval))
807     {
808     char *str = strdup (SvPVbyte_nolen (newval));
809     THIS->rs [index] = str;
810     THIS->allocated.push_back (str);
811     }
812     else
813     THIS->rs [index] = 0;
814     }
815     }
816    
817     void
818 root 1.1 rxvt_term::selection_mark (...)
819     PROTOTYPE: $;$$
820     ALIAS:
821     selection_beg = 1
822     selection_end = 2
823     PPCODE:
824     {
825     row_col_t &sel = ix == 1 ? THIS->selection.beg
826     : ix == 2 ? THIS->selection.end
827     : THIS->selection.mark;
828    
829     if (GIMME_V != G_VOID)
830     {
831     EXTEND (SP, 2);
832     PUSHs (sv_2mortal (newSViv (sel.row)));
833     PUSHs (sv_2mortal (newSViv (sel.col)));
834     }
835    
836     if (items == 3)
837     {
838     sel.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
839     sel.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
840    
841     if (ix)
842     THIS->want_refresh = 1;
843     }
844     }
845    
846     int
847     rxvt_term::selection_grab (int eventtime)
848    
849     void
850     rxvt_term::selection (SV *newtext = 0)
851     PPCODE:
852     {
853     if (GIMME_V != G_VOID)
854     {
855     char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len);
856     SV *sv = newSVpv (sel, 0);
857     SvUTF8_on (sv);
858     free (sel);
859     XPUSHs (sv_2mortal (sv));
860     }
861    
862     if (newtext)
863     {
864     free (THIS->selection.text);
865    
866     THIS->selection.text = sv2wcs (newtext);
867     THIS->selection.len = wcslen (THIS->selection.text);
868     }
869     }
870    
871     void
872 root 1.13 rxvt_term::tt_write (SV *octets)
873     INIT:
874     STRLEN len;
875     char *str = SvPVbyte (octets, len);
876     C_ARGS:
877     (unsigned char *)str, len
878    
879     SV *
880     rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
881     CODE:
882     {
883     overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
884     RETVAL = newSVptr ((void *)o, "urxvt::overlay");
885     o->self = (HV *)SvRV (RETVAL);
886 root 1.1
887 root 1.13 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0));
888     char key[33]; sprintf (key, "%32lx", (long)o);
889     hv_store (hv, key, 32, newSViv ((long)o), 0);
890     }
891     OUTPUT:
892     RETVAL
893    
894     MODULE = urxvt PACKAGE = urxvt::overlay
895 root 1.1
896     void
897 root 1.13 overlay::set (int x, int y, SV *text, SV *rend = 0)
898 root 1.1
899     void
900 root 1.13 overlay::DESTROY ()
901 root 1.1 CODE:
902     {
903 root 1.13 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->THIS->self), "_overlay", 8, 0);
904     if (ovs)
905     {
906     HV *hv = (HV *)SvRV (*ovs);
907     char key[33]; sprintf (key, "%32lx", (long)THIS);
908     hv_delete (hv, key, 32, G_DISCARD);
909     }
910    
911     delete THIS;
912 root 1.1 }
913    
914     MODULE = urxvt PACKAGE = urxvt::timer
915    
916     SV *
917     timer::new ()
918     CODE:
919     timer *w = new timer;
920 root 1.13 w->start (NOW);
921 root 1.1 RETVAL = newSVptr ((void *)w, "urxvt::timer");
922     w->self = (HV *)SvRV (RETVAL);
923     OUTPUT:
924     RETVAL
925    
926     timer *
927     timer::cb (SV *cb)
928     CODE:
929     THIS->cb (cb);
930     RETVAL = THIS;
931     OUTPUT:
932     RETVAL
933    
934     NV
935     timer::at ()
936     CODE:
937     RETVAL = THIS->at;
938     OUTPUT:
939     RETVAL
940    
941     timer *
942 root 1.13 timer::interval (NV interval)
943     CODE:
944     THIS->interval = interval;
945     RETVAL = THIS;
946     OUTPUT:
947     RETVAL
948    
949     timer *
950 root 1.1 timer::set (NV tstamp)
951     CODE:
952     THIS->set (tstamp);
953     RETVAL = THIS;
954     OUTPUT:
955     RETVAL
956    
957     timer *
958     timer::start (NV tstamp = THIS->at)
959     CODE:
960     THIS->start (tstamp);
961     RETVAL = THIS;
962     OUTPUT:
963     RETVAL
964    
965     timer *
966     timer::stop ()
967     CODE:
968     THIS->stop ();
969     RETVAL = THIS;
970     OUTPUT:
971     RETVAL
972    
973     void
974     timer::DESTROY ()
975    
976     MODULE = urxvt PACKAGE = urxvt::iow
977    
978     SV *
979     iow::new ()
980     CODE:
981     iow *w = new iow;
982     RETVAL = newSVptr ((void *)w, "urxvt::iow");
983     w->self = (HV *)SvRV (RETVAL);
984     OUTPUT:
985     RETVAL
986    
987     iow *
988     iow::cb (SV *cb)
989     CODE:
990     THIS->cb (cb);
991     RETVAL = THIS;
992     OUTPUT:
993     RETVAL
994    
995     iow *
996     iow::fd (int fd)
997     CODE:
998     THIS->fd = fd;
999     RETVAL = THIS;
1000     OUTPUT:
1001     RETVAL
1002    
1003     iow *
1004     iow::events (short events)
1005     CODE:
1006     THIS->events = events;
1007     RETVAL = THIS;
1008     OUTPUT:
1009     RETVAL
1010    
1011     iow *
1012     iow::start ()
1013     CODE:
1014     THIS->start ();
1015     RETVAL = THIS;
1016     OUTPUT:
1017     RETVAL
1018    
1019     iow *
1020     iow::stop ()
1021     CODE:
1022     THIS->stop ();
1023     RETVAL = THIS;
1024     OUTPUT:
1025     RETVAL
1026    
1027     void
1028     iow::DESTROY ()
1029    
1030