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