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