ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.12
Committed: Tue Jan 3 02:42:17 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
Changes since 1.11: +24 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 #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 /////////////////////////////////////////////////////////////////////////////
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 timer ()
145 : time_watcher (this, &timer::execute)
146 {
147 }
148
149 void execute (time_watcher &w)
150 {
151 invoke ("urxvt::timer", newSVtimer (this));
152 }
153 };
154
155 #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
156 #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
157
158 struct iow : io_watcher, perl_watcher
159 {
160 iow ()
161 : io_watcher (this, &iow::execute)
162 {
163 }
164
165 void execute (io_watcher &w, short revents)
166 {
167 invoke ("urxvt::iow", newSViow (this), revents);
168 }
169 };
170
171 /////////////////////////////////////////////////////////////////////////////
172
173 struct rxvt_perl_interp rxvt_perl;
174
175 static PerlInterpreter *perl;
176
177 rxvt_perl_interp::rxvt_perl_interp ()
178 {
179 }
180
181 rxvt_perl_interp::~rxvt_perl_interp ()
182 {
183 if (perl)
184 {
185 perl_destruct (perl);
186 perl_free (perl);
187 }
188 }
189
190 void
191 rxvt_perl_interp::init ()
192 {
193 if (!perl)
194 {
195 char *argv[] = {
196 "",
197 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
198 };
199
200 perl = perl_alloc ();
201 perl_construct (perl);
202
203 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
204 || perl_run (perl))
205 {
206 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
207
208 perl_destruct (perl);
209 perl_free (perl);
210 perl = 0;
211 }
212 }
213 }
214
215 bool
216 rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
217 {
218 if (!perl
219 || (!should_invoke [htype] && htype != HOOK_INIT && htype != HOOK_DESTROY))
220 return false;
221
222 if (htype == HOOK_INIT) // first hook ever called
223 term->self = (void *)newSVptr ((void *)term, "urxvt::term");
224
225 dSP;
226 va_list ap;
227
228 va_start (ap, htype);
229
230 ENTER;
231 SAVETMPS;
232
233 PUSHMARK (SP);
234
235 XPUSHs (sv_2mortal (newSVterm (term)));
236 XPUSHs (sv_2mortal (newSViv (htype)));
237
238 for (;;) {
239 data_type dt = (data_type)va_arg (ap, int);
240
241 switch (dt)
242 {
243 case DT_INT:
244 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
245 break;
246
247 case DT_LONG:
248 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
249 break;
250
251 case DT_STRING:
252 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
253 break;
254
255 case DT_END:
256 {
257 va_end (ap);
258
259 PUTBACK;
260 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
261 SPAGAIN;
262
263 if (count)
264 {
265 SV *status = POPs;
266 count = SvTRUE (status);
267 }
268
269 PUTBACK;
270 FREETMPS;
271 LEAVE;
272
273 if (SvTRUE (ERRSV))
274 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
275
276 if (htype == HOOK_DESTROY)
277 {
278 // TODO: clear magic
279 hv_clear ((HV *)SvRV ((SV *)term->self));
280 SvREFCNT_dec ((SV *)term->self);
281 }
282
283 return count;
284 }
285
286 default:
287 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
288 }
289 }
290 }
291
292 /////////////////////////////////////////////////////////////////////////////
293
294 MODULE = urxvt PACKAGE = urxvt
295
296 PROTOTYPES: ENABLE
297
298 BOOT:
299 {
300 # define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0))
301 # define export_const(name) newCONSTSUB (gv_stashpv ("urxvt", 1), #name, newSViv (name));
302 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
303 set_hookname (INIT);
304 set_hookname (RESET);
305 set_hookname (START);
306 set_hookname (DESTROY);
307 set_hookname (SEL_BEGIN);
308 set_hookname (SEL_EXTEND);
309 set_hookname (SEL_MAKE);
310 set_hookname (SEL_GRAB);
311 set_hookname (FOCUS_IN);
312 set_hookname (FOCUS_OUT);
313 set_hookname (VIEW_CHANGE);
314 set_hookname (SCROLL_BACK);
315 set_hookname (TTY_ACTIVITY);
316 set_hookname (REFRESH_BEGIN);
317 set_hookname (REFRESH_END);
318 set_hookname (KEYBOARD_COMMAND);
319
320 export_const (DEFAULT_RSTYLE);
321 export_const (OVERLAY_RSTYLE);
322 export_const (RS_Bold);
323 export_const (RS_Italic);
324 export_const (RS_Blink);
325 export_const (RS_RVid);
326 export_const (RS_Uline);
327
328 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR);
329 }
330
331 void
332 set_should_invoke (int htype, int value)
333 CODE:
334 rxvt_perl.should_invoke [htype] = value;
335
336 void
337 warn (const char *msg)
338 CODE:
339 rxvt_warn ("%s", msg);
340
341 void
342 fatal (const char *msg)
343 CODE:
344 rxvt_fatal ("%s", msg);
345
346 NV
347 NOW ()
348 CODE:
349 RETVAL = NOW;
350 OUTPUT:
351 RETVAL
352
353 int
354 GET_BASEFG (int rend)
355 CODE:
356 RETVAL = GET_BASEFG (rend);
357 OUTPUT:
358 RETVAL
359
360 int
361 GET_BASEBG (int rend)
362 CODE:
363 RETVAL = GET_BASEBG (rend);
364 OUTPUT:
365 RETVAL
366
367 int
368 SET_FGCOLOR (int rend, int new_color)
369 CODE:
370 RETVAL = SET_FGCOLOR (rend, new_color);
371 OUTPUT:
372 RETVAL
373
374 int
375 SET_BGCOLOR (int rend, int new_color)
376 CODE:
377 RETVAL = SET_BGCOLOR (rend, new_color);
378 OUTPUT:
379 RETVAL
380
381 int
382 GET_CUSTOM (int rend)
383 CODE:
384 RETVAL = (rend && RS_customMask) >> RS_customShift;
385 OUTPUT:
386 RETVAL
387
388 int
389 SET_CUSTOM (int rend, int new_value)
390 CODE:
391 {
392 if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
393 croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
394
395 RETVAL = (rend & ~RS_customMask)
396 | ((new_value << RS_customShift) & RS_customMask);
397 }
398 OUTPUT:
399 RETVAL
400
401 MODULE = urxvt PACKAGE = urxvt::term
402
403 int
404 rxvt_term::strwidth (SV *str)
405 CODE:
406 {
407 wchar_t *wstr = sv2wcs (str);
408
409 rxvt_push_locale (THIS->locale);
410 RETVAL = wcswidth (wstr, wcslen (wstr));
411 rxvt_pop_locale ();
412
413 free (wstr);
414 }
415 OUTPUT:
416 RETVAL
417
418 SV *
419 rxvt_term::locale_encode (SV *str)
420 CODE:
421 {
422 wchar_t *wstr = sv2wcs (str);
423
424 rxvt_push_locale (THIS->locale);
425 char *mbstr = rxvt_wcstombs (wstr);
426 rxvt_pop_locale ();
427
428 free (wstr);
429
430 RETVAL = newSVpv (mbstr, 0);
431 free (mbstr);
432 }
433 OUTPUT:
434 RETVAL
435
436 SV *
437 rxvt_term::locale_decode (SV *octets)
438 CODE:
439 {
440 STRLEN len;
441 char *data = SvPVbyte (octets, len);
442
443 rxvt_push_locale (THIS->locale);
444 wchar_t *wstr = rxvt_mbstowcs (data, len);
445 rxvt_pop_locale ();
446
447 char *str = rxvt_wcstoutf8 (wstr);
448 free (wstr);
449
450 RETVAL = newSVpv (str, 0);
451 SvUTF8_on (RETVAL);
452 free (str);
453 }
454 OUTPUT:
455 RETVAL
456
457 int
458 rxvt_term::nsaved ()
459 CODE:
460 RETVAL = THIS->nsaved;
461 OUTPUT:
462 RETVAL
463
464 int
465 rxvt_term::view_start (int newval = -1)
466 CODE:
467 {
468 RETVAL = THIS->view_start;
469
470 if (newval >= 0)
471 {
472 THIS->view_start = min (newval, THIS->nsaved);
473 THIS->scr_changeview (RETVAL);
474 }
475 }
476 OUTPUT:
477 RETVAL
478
479 int
480 rxvt_term::nrow ()
481 CODE:
482 RETVAL = THIS->nrow;
483 OUTPUT:
484 RETVAL
485
486 int
487 rxvt_term::ncol ()
488 CODE:
489 RETVAL = THIS->ncol;
490 OUTPUT:
491 RETVAL
492
493 void
494 rxvt_term::want_refresh ()
495 CODE:
496 THIS->want_refresh = 1;
497
498 void
499 rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0)
500 PPCODE:
501 {
502 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
503 croak ("row_number number of out range");
504
505 line_t &l = ROW(row_number);
506
507 if (GIMME_V != G_VOID)
508 {
509 wchar_t *wstr = new wchar_t [THIS->ncol];
510
511 for (int col = 0; col <THIS->ncol; col++)
512 wstr [col] = l.t [col];
513
514 char *str = rxvt_wcstoutf8 (wstr, THIS->ncol);
515 free (wstr);
516
517 SV *sv = newSVpv (str, 0);
518 SvUTF8_on (sv);
519 XPUSHs (sv_2mortal (sv));
520 free (str);
521 }
522
523 if (new_text)
524 {
525 STRLEN slen;
526 char *str = SvPVutf8 (new_text, slen);
527 wchar_t *wstr = rxvt_utf8towcs (str, slen);
528
529 int len = wcslen (wstr);
530
531 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
532 {
533 free (wstr);
534 croak ("new_text extends beyond horizontal margins");
535 }
536
537 for (int col = start_col; col < start_col + len; col++)
538 {
539 l.t [col] = wstr [col - start_col];
540 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
541 }
542
543 free (wstr);
544 }
545 }
546
547 void
548 rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0)
549 PPCODE:
550 {
551 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
552 croak ("row_number number of out range");
553
554 line_t &l = ROW(row_number);
555
556 if (GIMME_V != G_VOID)
557 {
558 AV *av = newAV ();
559
560 av_extend (av, THIS->ncol - 1);
561 for (int col = 0; col < THIS->ncol; col++)
562 av_store (av, col, newSViv (l.r [col]));
563
564 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
565 }
566
567 if (new_rend)
568 {
569 if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
570 croak ("new_rend must be arrayref");
571
572 AV *av = (AV *)SvRV (new_rend);
573 int len = av_len (av) + 1;
574
575 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
576 croak ("new_rend array extends beyond horizontal margins");
577
578 for (int col = start_col; col < start_col + len; col++)
579 {
580 rend_t r = SvIV (*av_fetch (av, col - start_col, 1)) & ~RS_fontMask;
581
582 l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
583 }
584 }
585 }
586
587 int
588 rxvt_term::ROW_l (int row_number, int new_length = -2)
589 CODE:
590 {
591 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
592 croak ("row_number number of out range");
593
594 line_t &l = ROW(row_number);
595 RETVAL = l.l;
596
597 if (new_length >= -1)
598 l.l = new_length;
599 }
600 OUTPUT:
601 RETVAL
602
603 SV *
604 rxvt_term::special_encode (SV *str)
605 CODE:
606 abort ();//TODO
607
608 SV *
609 rxvt_term::special_decode (SV *str)
610 CODE:
611 abort ();//TODO
612
613 void
614 rxvt_term::_resource (char *name, int index, SV *newval = 0)
615 PPCODE:
616 {
617 struct resval { const char *name; int value; } rslist [] = {
618 # define Rs_def(name) { # name, Rs_ ## name },
619 # define Rs_reserve(name,count)
620 # include "rsinc.h"
621 # undef Rs_def
622 # undef Rs_reserve
623 };
624
625 struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
626
627 do {
628 if (rs-- == rslist)
629 croak ("no such resource '%s', requested", name);
630 } while (strcmp (name, rs->name));
631
632 index += rs->value;
633
634 if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
635 croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
636
637 if (GIMME_V != G_VOID)
638 XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
639
640 if (newval)
641 {
642 if (SvOK (newval))
643 {
644 char *str = strdup (SvPVbyte_nolen (newval));
645 THIS->rs [index] = str;
646 THIS->allocated.push_back (str);
647 }
648 else
649 THIS->rs [index] = 0;
650 }
651 }
652
653 void
654 rxvt_term::selection_mark (...)
655 PROTOTYPE: $;$$
656 ALIAS:
657 selection_beg = 1
658 selection_end = 2
659 PPCODE:
660 {
661 row_col_t &sel = ix == 1 ? THIS->selection.beg
662 : ix == 2 ? THIS->selection.end
663 : THIS->selection.mark;
664
665 if (GIMME_V != G_VOID)
666 {
667 EXTEND (SP, 2);
668 PUSHs (sv_2mortal (newSViv (sel.row)));
669 PUSHs (sv_2mortal (newSViv (sel.col)));
670 }
671
672 if (items == 3)
673 {
674 sel.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
675 sel.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
676
677 if (ix)
678 THIS->want_refresh = 1;
679 }
680 }
681
682 int
683 rxvt_term::selection_grab (int eventtime)
684
685 void
686 rxvt_term::selection (SV *newtext = 0)
687 PPCODE:
688 {
689 if (GIMME_V != G_VOID)
690 {
691 char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len);
692 SV *sv = newSVpv (sel, 0);
693 SvUTF8_on (sv);
694 free (sel);
695 XPUSHs (sv_2mortal (sv));
696 }
697
698 if (newtext)
699 {
700 free (THIS->selection.text);
701
702 THIS->selection.text = sv2wcs (newtext);
703 THIS->selection.len = wcslen (THIS->selection.text);
704 }
705 }
706
707 void
708 rxvt_term::scr_overlay_new (int x, int y, int w, int h)
709
710 void
711 rxvt_term::scr_overlay_off ()
712
713 void
714 rxvt_term::scr_overlay_set_char (int x, int y, U32 text, U32 rend = OVERLAY_RSTYLE)
715 CODE:
716 THIS->scr_overlay_set (x, y, text, rend);
717
718 void
719 rxvt_term::scr_overlay_set (int x, int y, SV *text)
720 CODE:
721 {
722 wchar_t *wtext = sv2wcs (text);
723 THIS->scr_overlay_set (x, y, wtext);
724 free (wtext);
725 }
726
727 void
728 rxvt_term::tt_write (SV *octets)
729 INIT:
730 STRLEN len;
731 char *str = SvPVbyte (octets, len);
732 C_ARGS:
733 (unsigned char *)str, len
734
735 MODULE = urxvt PACKAGE = urxvt::timer
736
737 SV *
738 timer::new ()
739 CODE:
740 timer *w = new timer;
741 RETVAL = newSVptr ((void *)w, "urxvt::timer");
742 w->self = (HV *)SvRV (RETVAL);
743 OUTPUT:
744 RETVAL
745
746 timer *
747 timer::cb (SV *cb)
748 CODE:
749 THIS->cb (cb);
750 RETVAL = THIS;
751 OUTPUT:
752 RETVAL
753
754 NV
755 timer::at ()
756 CODE:
757 RETVAL = THIS->at;
758 OUTPUT:
759 RETVAL
760
761 timer *
762 timer::set (NV tstamp)
763 CODE:
764 THIS->set (tstamp);
765 RETVAL = THIS;
766 OUTPUT:
767 RETVAL
768
769 timer *
770 timer::start (NV tstamp = THIS->at)
771 CODE:
772 THIS->start (tstamp);
773 RETVAL = THIS;
774 OUTPUT:
775 RETVAL
776
777 timer *
778 timer::stop ()
779 CODE:
780 THIS->stop ();
781 RETVAL = THIS;
782 OUTPUT:
783 RETVAL
784
785 void
786 timer::DESTROY ()
787
788 MODULE = urxvt PACKAGE = urxvt::iow
789
790 SV *
791 iow::new ()
792 CODE:
793 iow *w = new iow;
794 RETVAL = newSVptr ((void *)w, "urxvt::iow");
795 w->self = (HV *)SvRV (RETVAL);
796 OUTPUT:
797 RETVAL
798
799 iow *
800 iow::cb (SV *cb)
801 CODE:
802 THIS->cb (cb);
803 RETVAL = THIS;
804 OUTPUT:
805 RETVAL
806
807 iow *
808 iow::fd (int fd)
809 CODE:
810 THIS->fd = fd;
811 RETVAL = THIS;
812 OUTPUT:
813 RETVAL
814
815 iow *
816 iow::events (short events)
817 CODE:
818 THIS->events = events;
819 RETVAL = THIS;
820 OUTPUT:
821 RETVAL
822
823 iow *
824 iow::start ()
825 CODE:
826 THIS->start ();
827 RETVAL = THIS;
828 OUTPUT:
829 RETVAL
830
831 iow *
832 iow::stop ()
833 CODE:
834 THIS->stop ();
835 RETVAL = THIS;
836 OUTPUT:
837 RETVAL
838
839 void
840 iow::DESTROY ()
841
842