ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.5
Committed: Mon Jan 2 20:35:39 2006 UTC (18 years, 6 months ago) by root
Branch: MAIN
Changes since 1.4: +2 -4 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     /////////////////////////////////////////////////////////////////////////////
41    
42     static wchar_t *
43     sv2wcs (SV *sv)
44     {
45     STRLEN len;
46     char *str = SvPVutf8 (sv, len);
47     return rxvt_utf8towcs (str, len);
48     }
49    
50     static SV *
51     new_ref (HV *hv, const char *klass)
52     {
53     return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
54     }
55    
56     //TODO: use magic
57     static SV *
58     newSVptr (void *ptr, const char *klass)
59     {
60     HV *hv = newHV ();
61     hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0);
62     return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
63     }
64    
65     static long
66     SvPTR (SV *sv, const char *klass)
67     {
68     if (!sv_derived_from (sv, klass))
69     croak ("object of type %s expected", klass);
70    
71     IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1));
72    
73     if (!iv)
74     croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
75    
76     return (long)iv;
77     }
78    
79     #define newSVterm(term) SvREFCNT_inc ((SV *)term->self)
80     #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
81    
82     /////////////////////////////////////////////////////////////////////////////
83    
84     struct perl_watcher
85     {
86     SV *cbsv;
87     HV *self;
88    
89     perl_watcher ()
90     : cbsv (newSV (0))
91     {
92     }
93    
94     ~perl_watcher ()
95     {
96     SvREFCNT_dec (cbsv);
97     }
98    
99     void cb (SV *cb)
100     {
101     sv_setsv (cbsv, cb);
102     }
103    
104     void invoke (const char *type, SV *self, int arg = -1);
105     };
106    
107     void
108     perl_watcher::invoke (const char *type, SV *self, int arg)
109     {
110     dSP;
111    
112     ENTER;
113     SAVETMPS;
114    
115     PUSHMARK (SP);
116    
117     XPUSHs (sv_2mortal (self));
118    
119     if (arg >= 0)
120     XPUSHs (sv_2mortal (newSViv (arg)));
121    
122     PUTBACK;
123     call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
124     SPAGAIN;
125    
126     PUTBACK;
127     FREETMPS;
128     LEAVE;
129    
130     if (SvTRUE (ERRSV))
131     rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
132     }
133    
134     #define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
135     #define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
136    
137     struct timer : time_watcher, perl_watcher
138     {
139     timer ()
140     : time_watcher (this, &timer::execute)
141     {
142     }
143    
144     void execute (time_watcher &w)
145     {
146     invoke ("urxvt::timer", newSVtimer (this));
147     }
148     };
149    
150     #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
151     #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
152    
153     struct iow : io_watcher, perl_watcher
154     {
155     iow ()
156     : io_watcher (this, &iow::execute)
157     {
158     }
159    
160     void execute (io_watcher &w, short revents)
161     {
162     invoke ("urxvt::iow", newSViow (this), revents);
163     }
164     };
165    
166     /////////////////////////////////////////////////////////////////////////////
167    
168     struct rxvt_perl_interp rxvt_perl;
169    
170     static PerlInterpreter *perl;
171    
172     rxvt_perl_interp::rxvt_perl_interp ()
173     {
174     }
175    
176     rxvt_perl_interp::~rxvt_perl_interp ()
177     {
178     if (perl)
179     {
180     perl_destruct (perl);
181     perl_free (perl);
182     }
183     }
184    
185     void
186     rxvt_perl_interp::init ()
187     {
188     if (!perl)
189     {
190     char *argv[] = {
191     "",
192     "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
193     };
194    
195     perl = perl_alloc ();
196     perl_construct (perl);
197    
198     if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
199     || perl_run (perl))
200     {
201     rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
202    
203     perl_destruct (perl);
204     perl_free (perl);
205     perl = 0;
206     }
207     }
208     }
209    
210     bool
211     rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
212     {
213 root 1.5 if (!perl
214     || (!should_invoke [htype] && htype != HOOK_INIT && htype != HOOK_DESTROY))
215 root 1.1 return false;
216 root 1.4
217 root 1.1 if (htype == HOOK_INIT) // first hook ever called
218     term->self = (void *)newSVptr ((void *)term, "urxvt::term");
219    
220     dSP;
221     va_list ap;
222    
223     va_start (ap, htype);
224    
225     ENTER;
226     SAVETMPS;
227    
228     PUSHMARK (SP);
229    
230     XPUSHs (sv_2mortal (newSVterm (term)));
231     XPUSHs (sv_2mortal (newSViv (htype)));
232    
233     for (;;) {
234     data_type dt = (data_type)va_arg (ap, int);
235    
236     switch (dt)
237     {
238     case DT_INT:
239     XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
240     break;
241    
242     case DT_LONG:
243     XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
244     break;
245    
246     case DT_END:
247     {
248     va_end (ap);
249    
250     PUTBACK;
251     int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
252     SPAGAIN;
253    
254     if (count)
255     {
256     SV *status = POPs;
257     count = SvTRUE (status);
258     }
259    
260     PUTBACK;
261     FREETMPS;
262     LEAVE;
263    
264     if (SvTRUE (ERRSV))
265     rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
266    
267 root 1.4 if (htype == HOOK_DESTROY)
268     {
269     // TODO: clear magic
270     hv_clear ((HV *)SvRV ((SV *)term->self));
271     SvREFCNT_dec ((SV *)term->self);
272     }
273    
274 root 1.1 return count;
275     }
276    
277     default:
278     rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
279     }
280     }
281     }
282    
283     /////////////////////////////////////////////////////////////////////////////
284    
285     MODULE = urxvt PACKAGE = urxvt
286    
287     PROTOTYPES: ENABLE
288    
289     BOOT:
290     {
291     # define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0))
292     AV *hookname = get_av ("urxvt::HOOKNAME", 1);
293     set_hookname (INIT);
294     set_hookname (RESET);
295     set_hookname (START);
296     set_hookname (DESTROY);
297     set_hookname (SEL_BEGIN);
298     set_hookname (SEL_EXTEND);
299     set_hookname (SEL_MAKE);
300     set_hookname (SEL_GRAB);
301     set_hookname (FOCUS_IN);
302     set_hookname (FOCUS_OUT);
303     set_hookname (VIEW_CHANGE);
304     set_hookname (SCROLL_BACK);
305     set_hookname (TTY_ACTIVITY);
306     set_hookname (REFRESH_BEGIN);
307     set_hookname (REFRESH_END);
308    
309     sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR);
310     }
311    
312     void
313     set_should_invoke (int htype, int value)
314     CODE:
315     rxvt_perl.should_invoke [htype] = value;
316    
317     void
318     warn (const char *msg)
319     CODE:
320     rxvt_warn ("%s", msg);
321    
322     void
323     fatal (const char *msg)
324     CODE:
325     rxvt_fatal ("%s", msg);
326    
327 root 1.3 NV
328     NOW ()
329     CODE:
330     RETVAL = NOW;
331     OUTPUT:
332     RETVAL
333    
334     MODULE = urxvt PACKAGE = urxvt::term
335    
336 root 1.1 int
337 root 1.3 rxvt_term::strwidth (SV *str)
338 root 1.1 CODE:
339     {
340     wchar_t *wstr = sv2wcs (str);
341 root 1.3
342     rxvt_push_locale (THIS->locale);
343 root 1.1 RETVAL = wcswidth (wstr, wcslen (wstr));
344 root 1.3 rxvt_pop_locale ();
345    
346 root 1.1 free (wstr);
347     }
348     OUTPUT:
349     RETVAL
350    
351 root 1.3 SV *
352     rxvt_term::locale_encode (SV *str)
353 root 1.1 CODE:
354 root 1.3 {
355     wchar_t *wstr = sv2wcs (str);
356    
357     rxvt_push_locale (THIS->locale);
358     char *mbstr = rxvt_wcstombs (wstr);
359     rxvt_pop_locale ();
360    
361     free (wstr);
362    
363     RETVAL = newSVpv (mbstr, 0);
364     free (mbstr);
365     }
366     OUTPUT:
367 root 1.1 RETVAL
368    
369 root 1.3 SV *
370     rxvt_term::locale_decode (SV *octets)
371     CODE:
372     {
373     STRLEN len;
374     char *data = SvPVbyte (octets, len);
375    
376     rxvt_push_locale (THIS->locale);
377     wchar_t *wstr = rxvt_mbstowcs (data, len);
378     rxvt_pop_locale ();
379    
380     char *str = rxvt_wcstoutf8 (wstr);
381     free (wstr);
382    
383     RETVAL = newSVpv (str, 0);
384     SvUTF8_on (RETVAL);
385     free (str);
386     }
387     OUTPUT:
388     RETVAL
389 root 1.1
390     void
391 root 1.2 rxvt_term::_resource (char *name, int index, SV *newval = 0)
392     PPCODE:
393     {
394     struct resval { const char *name; int value; } rslist [] = {
395     # define Rs_def(name) { # name, Rs_ ## name },
396     # define Rs_reserve(name,count)
397     # include "rsinc.h"
398     # undef Rs_def
399     # undef Rs_reserve
400     };
401    
402     struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
403    
404     do {
405     if (rs-- == rslist)
406     croak ("no such resource '%s', requested", name);
407     } while (strcmp (name, rs->name));
408    
409     index += rs->value;
410    
411     if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
412     croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
413    
414     if (GIMME_V != G_VOID)
415     XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
416    
417     if (newval)
418     {
419     if (SvOK (newval))
420     {
421     char *str = strdup (SvPVbyte_nolen (newval));
422     THIS->rs [index] = str;
423     THIS->allocated.push_back (str);
424     }
425     else
426     THIS->rs [index] = 0;
427     }
428     }
429    
430     void
431 root 1.1 rxvt_term::selection_mark (...)
432     PROTOTYPE: $;$$
433     ALIAS:
434     selection_beg = 1
435     selection_end = 2
436     PPCODE:
437     {
438     row_col_t &sel = ix == 1 ? THIS->selection.beg
439     : ix == 2 ? THIS->selection.end
440     : THIS->selection.mark;
441    
442     if (GIMME_V != G_VOID)
443     {
444     EXTEND (SP, 2);
445     PUSHs (sv_2mortal (newSViv (sel.row)));
446     PUSHs (sv_2mortal (newSViv (sel.col)));
447     }
448    
449     if (items == 3)
450     {
451     sel.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
452     sel.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
453    
454     if (ix)
455     THIS->want_refresh = 1;
456     }
457     }
458    
459     int
460     rxvt_term::selection_grab (int eventtime)
461    
462     void
463     rxvt_term::selection (SV *newtext = 0)
464     PPCODE:
465     {
466     if (GIMME_V != G_VOID)
467     {
468     char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len);
469     SV *sv = newSVpv (sel, 0);
470     SvUTF8_on (sv);
471     free (sel);
472     XPUSHs (sv_2mortal (sv));
473     }
474    
475     if (newtext)
476     {
477     free (THIS->selection.text);
478    
479     THIS->selection.text = sv2wcs (newtext);
480     THIS->selection.len = wcslen (THIS->selection.text);
481     }
482     }
483    
484     void
485     rxvt_term::scr_overlay_new (int x, int y, int w, int h)
486    
487     void
488     rxvt_term::scr_overlay_off ()
489    
490     void
491     rxvt_term::scr_overlay_set_char (int x, int y, U32 text, U32 rend = OVERLAY_RSTYLE)
492     CODE:
493     THIS->scr_overlay_set (x, y, text, rend);
494    
495     void
496     rxvt_term::scr_overlay_set (int x, int y, SV *text)
497     CODE:
498     {
499     wchar_t *wtext = sv2wcs (text);
500     THIS->scr_overlay_set (x, y, wtext);
501     free (wtext);
502     }
503    
504 root 1.3 void
505     rxvt_term::tt_write (SV *octets)
506     INIT:
507     STRLEN len;
508     char *str = SvPVbyte (octets, len);
509     C_ARGS:
510     (unsigned char *)str, len
511    
512 root 1.1 MODULE = urxvt PACKAGE = urxvt::timer
513    
514     SV *
515     timer::new ()
516     CODE:
517     timer *w = new timer;
518     RETVAL = newSVptr ((void *)w, "urxvt::timer");
519     w->self = (HV *)SvRV (RETVAL);
520     OUTPUT:
521     RETVAL
522    
523     timer *
524     timer::cb (SV *cb)
525     CODE:
526     THIS->cb (cb);
527     RETVAL = THIS;
528     OUTPUT:
529     RETVAL
530    
531     NV
532     timer::at ()
533     CODE:
534     RETVAL = THIS->at;
535     OUTPUT:
536     RETVAL
537    
538     timer *
539     timer::set (NV tstamp)
540     CODE:
541     THIS->set (tstamp);
542     RETVAL = THIS;
543     OUTPUT:
544     RETVAL
545    
546     timer *
547     timer::start (NV tstamp = THIS->at)
548     CODE:
549     THIS->start (tstamp);
550     RETVAL = THIS;
551     OUTPUT:
552     RETVAL
553    
554     timer *
555     timer::stop ()
556     CODE:
557     THIS->stop ();
558     RETVAL = THIS;
559     OUTPUT:
560     RETVAL
561    
562     void
563     timer::DESTROY ()
564    
565     MODULE = urxvt PACKAGE = urxvt::iow
566    
567     SV *
568     iow::new ()
569     CODE:
570     iow *w = new iow;
571     RETVAL = newSVptr ((void *)w, "urxvt::iow");
572     w->self = (HV *)SvRV (RETVAL);
573     OUTPUT:
574     RETVAL
575    
576     iow *
577     iow::cb (SV *cb)
578     CODE:
579     THIS->cb (cb);
580     RETVAL = THIS;
581     OUTPUT:
582     RETVAL
583    
584     iow *
585     iow::fd (int fd)
586     CODE:
587     THIS->fd = fd;
588     RETVAL = THIS;
589     OUTPUT:
590     RETVAL
591    
592     iow *
593     iow::events (short events)
594     CODE:
595     THIS->events = events;
596     RETVAL = THIS;
597     OUTPUT:
598     RETVAL
599    
600     iow *
601     iow::start ()
602     CODE:
603     THIS->start ();
604     RETVAL = THIS;
605     OUTPUT:
606     RETVAL
607    
608     iow *
609     iow::stop ()
610     CODE:
611     THIS->stop ();
612     RETVAL = THIS;
613     OUTPUT:
614     RETVAL
615    
616     void
617     iow::DESTROY ()
618    
619