1 /*----------------------------------------------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2005 Marc Lehmann <pcg@goof.com>
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.
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.
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 *----------------------------------------------------------------------*/
23 #define line_t perl_line_t
29 #include "../config.h"
41 #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
43 #define ROW(n) THIS->row_buf [LINENO (n)]
45 /////////////////////////////////////////////////////////////////////////////
51 char *str = SvPVutf8 (sv, len);
52 return rxvt_utf8towcs (str, len);
56 new_ref (HV *hv, const char *klass)
58 return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
63 newSVptr (void *ptr, const char *klass)
66 hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0);
67 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
71 SvPTR (SV *sv, const char *klass)
73 if (!sv_derived_from (sv, klass))
74 croak ("object of type %s expected", klass);
76 IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1));
79 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
84 #define newSVterm(term) SvREFCNT_inc ((SV *)term->self)
85 #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
87 /////////////////////////////////////////////////////////////////////////////
109 void invoke (const char *type, SV *self, int arg = -1);
113 perl_watcher::invoke (const char *type, SV *self, int arg)
122 XPUSHs (sv_2mortal (self));
125 XPUSHs (sv_2mortal (newSViv (arg)));
128 call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
136 rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
139 #define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
140 #define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
142 struct timer : time_watcher, perl_watcher
147 : time_watcher (this, &timer::execute)
151 void execute (time_watcher &w)
154 start (at + interval);
156 invoke ("urxvt::timer", newSVtimer (this));
160 #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
161 #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
163 struct iow : io_watcher, perl_watcher
166 : io_watcher (this, &iow::execute)
170 void execute (io_watcher &w, short revents)
172 invoke ("urxvt::iow", newSViow (this), revents);
176 /////////////////////////////////////////////////////////////////////////////
178 #define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
188 overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
193 void set (int x, int y, SV *str, SV *rend);
196 overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
197 : THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2)
205 text = new text_t *[h];
206 rend = new rend_t *[h];
208 for (int y = 0; y < h; y++)
210 text_t *tp = text[y] = new text_t[w];
211 rend_t *rp = rend[y] = new rend_t[w];
219 t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
221 t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
223 t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
228 for (int x = w - 2; x-- > 0; )
238 for (int x = w; x-- > 0; )
245 THIS->want_refresh = 1;
250 for (int y = h; y--; )
259 THIS->want_refresh = 1;
262 void overlay::swap ()
264 int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
265 int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
267 int ov_w = min (w, THIS->ncol - ov_x);
268 int ov_h = min (h, THIS->nrow - ov_y);
270 for (int y = ov_h; y--; )
272 text_t *t1 = text [y];
273 rend_t *r1 = rend [y];
275 text_t *t2 = ROW(y + ov_y - THIS->view_start).t + ov_x;
276 rend_t *r2 = ROW(y + ov_y - THIS->view_start).r + ov_x;
278 for (int x = ov_w; x--; )
280 text_t t = *t1; *t1++ = *t2; *t2++ = t;
281 rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
287 void overlay::set (int x, int y, SV *text, SV *rend)
292 if (!IN_RANGE_EXC (y, 0, h - border))
295 wchar_t *wtext = sv2wcs (text);
297 for (int col = min (wcslen (wtext), w - x - border); col--; )
298 this->text [y][x + col] = wtext [col];
304 if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
305 croak ("rend must be arrayref");
307 AV *av = (AV *)SvRV (rend);
309 for (int col = min (av_len (av) + 1, w - x - border); col--; )
310 this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
313 THIS->want_refresh = 1;
317 /////////////////////////////////////////////////////////////////////////////
319 struct rxvt_perl_interp rxvt_perl;
321 static PerlInterpreter *perl;
323 rxvt_perl_interp::rxvt_perl_interp ()
327 rxvt_perl_interp::~rxvt_perl_interp ()
331 perl_destruct (perl);
337 rxvt_perl_interp::init ()
343 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
346 perl = perl_alloc ();
347 perl_construct (perl);
349 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
352 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
354 perl_destruct (perl);
362 rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
367 if (htype == HOOK_INIT) // first hook ever called
369 term->self = (void *)newSVptr ((void *)term, "urxvt::term");
370 hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
372 else if (htype == HOOK_DESTROY)
376 else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
378 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0));
384 while (HE *he = hv_iternext (hv))
385 ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
388 else if (!should_invoke [htype])
394 va_start (ap, htype);
401 XPUSHs (sv_2mortal (newSVterm (term)));
402 XPUSHs (sv_2mortal (newSViv (htype)));
405 data_type dt = (data_type)va_arg (ap, int);
410 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
414 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
418 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
426 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
432 count = SvTRUE (status);
440 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
442 if (htype == HOOK_DESTROY)
445 hv_clear ((HV *)SvRV ((SV *)term->self));
446 SvREFCNT_dec ((SV *)term->self);
453 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
458 /////////////////////////////////////////////////////////////////////////////
460 MODULE = urxvt PACKAGE = urxvt
466 # define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0))
467 # define export_const(name) newCONSTSUB (gv_stashpv ("urxvt", 1), #name, newSViv (name));
468 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
470 set_hookname (RESET);
471 set_hookname (START);
472 set_hookname (DESTROY);
473 set_hookname (SEL_BEGIN);
474 set_hookname (SEL_EXTEND);
475 set_hookname (SEL_MAKE);
476 set_hookname (SEL_GRAB);
477 set_hookname (FOCUS_IN);
478 set_hookname (FOCUS_OUT);
479 set_hookname (VIEW_CHANGE);
480 set_hookname (SCROLL_BACK);
481 set_hookname (TTY_ACTIVITY);
482 set_hookname (REFRESH_BEGIN);
483 set_hookname (REFRESH_END);
484 set_hookname (KEYBOARD_COMMAND);
486 export_const (DEFAULT_RSTYLE);
487 export_const (OVERLAY_RSTYLE);
488 export_const (RS_Bold);
489 export_const (RS_Italic);
490 export_const (RS_Blink);
491 export_const (RS_RVid);
492 export_const (RS_Uline);
494 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR);
498 set_should_invoke (int htype, int value)
500 rxvt_perl.should_invoke [htype] = value;
503 warn (const char *msg)
505 rxvt_warn ("%s", msg);
508 fatal (const char *msg)
510 rxvt_fatal ("%s", msg);
520 GET_BASEFG (int rend)
522 RETVAL = GET_BASEFG (rend);
527 GET_BASEBG (int rend)
529 RETVAL = GET_BASEBG (rend);
534 SET_FGCOLOR (int rend, int new_color)
536 RETVAL = SET_FGCOLOR (rend, new_color);
541 SET_BGCOLOR (int rend, int new_color)
543 RETVAL = SET_BGCOLOR (rend, new_color);
548 GET_CUSTOM (int rend)
550 RETVAL = (rend && RS_customMask) >> RS_customShift;
555 SET_CUSTOM (int rend, int new_value)
558 if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
559 croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
561 RETVAL = (rend & ~RS_customMask)
562 | ((new_value << RS_customShift) & RS_customMask);
567 MODULE = urxvt PACKAGE = urxvt::term
570 rxvt_term::strwidth (SV *str)
573 wchar_t *wstr = sv2wcs (str);
575 rxvt_push_locale (THIS->locale);
576 RETVAL = wcswidth (wstr, wcslen (wstr));
585 rxvt_term::locale_encode (SV *str)
588 wchar_t *wstr = sv2wcs (str);
590 rxvt_push_locale (THIS->locale);
591 char *mbstr = rxvt_wcstombs (wstr);
596 RETVAL = newSVpv (mbstr, 0);
603 rxvt_term::locale_decode (SV *octets)
607 char *data = SvPVbyte (octets, len);
609 rxvt_push_locale (THIS->locale);
610 wchar_t *wstr = rxvt_mbstowcs (data, len);
613 char *str = rxvt_wcstoutf8 (wstr);
616 RETVAL = newSVpv (str, 0);
626 RETVAL = THIS->nsaved;
631 rxvt_term::view_start (int newval = -1)
634 RETVAL = THIS->view_start;
638 THIS->view_start = min (newval, THIS->nsaved);
639 THIS->scr_changeview (RETVAL);
660 rxvt_term::want_refresh ()
662 THIS->want_refresh = 1;
665 rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0)
668 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
669 croak ("row_number number of out range");
671 line_t &l = ROW(row_number);
673 if (GIMME_V != G_VOID)
675 wchar_t *wstr = new wchar_t [THIS->ncol];
677 for (int col = 0; col <THIS->ncol; col++)
678 wstr [col] = l.t [col];
680 char *str = rxvt_wcstoutf8 (wstr, THIS->ncol);
683 SV *sv = newSVpv (str, 0);
685 XPUSHs (sv_2mortal (sv));
691 wchar_t *wstr = sv2wcs (new_text);
693 int len = wcslen (wstr);
695 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
698 croak ("new_text extends beyond horizontal margins");
701 for (int col = start_col; col < start_col + len; col++)
703 l.t [col] = wstr [col - start_col];
704 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
712 rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0)
715 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
716 croak ("row_number number of out range");
718 line_t &l = ROW(row_number);
720 if (GIMME_V != G_VOID)
724 av_extend (av, THIS->ncol - 1);
725 for (int col = 0; col < THIS->ncol; col++)
726 av_store (av, col, newSViv (l.r [col]));
728 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
733 if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
734 croak ("new_rend must be arrayref");
736 AV *av = (AV *)SvRV (new_rend);
737 int len = av_len (av) + 1;
739 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
740 croak ("new_rend array extends beyond horizontal margins");
742 for (int col = start_col; col < start_col + len; col++)
744 rend_t r = SvIV (*av_fetch (av, col - start_col, 1)) & ~RS_fontMask;
746 l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
752 rxvt_term::ROW_l (int row_number, int new_length = -2)
755 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
756 croak ("row_number number of out range");
758 line_t &l = ROW(row_number);
761 if (new_length >= -1)
768 rxvt_term::special_encode (SV *str)
773 rxvt_term::special_decode (SV *str)
778 rxvt_term::_resource (char *name, int index, SV *newval = 0)
781 struct resval { const char *name; int value; } rslist [] = {
782 # define Rs_def(name) { # name, Rs_ ## name },
783 # define Rs_reserve(name,count)
789 struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
793 croak ("no such resource '%s', requested", name);
794 } while (strcmp (name, rs->name));
798 if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
799 croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
801 if (GIMME_V != G_VOID)
802 XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
808 char *str = strdup (SvPVbyte_nolen (newval));
809 THIS->rs [index] = str;
810 THIS->allocated.push_back (str);
813 THIS->rs [index] = 0;
818 rxvt_term::selection_mark (...)
825 row_col_t &sel = ix == 1 ? THIS->selection.beg
826 : ix == 2 ? THIS->selection.end
827 : THIS->selection.mark;
829 if (GIMME_V != G_VOID)
832 PUSHs (sv_2mortal (newSViv (sel.row)));
833 PUSHs (sv_2mortal (newSViv (sel.col)));
838 sel.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
839 sel.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
842 THIS->want_refresh = 1;
847 rxvt_term::selection_grab (int eventtime = CurrentTime)
850 rxvt_term::selection (SV *newtext = 0)
853 if (GIMME_V != G_VOID)
855 char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len);
856 SV *sv = newSVpv (sel, 0);
859 XPUSHs (sv_2mortal (sv));
864 free (THIS->selection.text);
866 THIS->selection.text = sv2wcs (newtext);
867 THIS->selection.len = wcslen (THIS->selection.text);
872 rxvt_term::tt_write (SV *octets)
875 char *str = SvPVbyte (octets, len);
877 (unsigned char *)str, len
880 rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
883 overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
884 RETVAL = newSVptr ((void *)o, "urxvt::overlay");
885 o->self = (HV *)SvRV (RETVAL);
887 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0));
888 char key[33]; sprintf (key, "%32lx", (long)o);
889 hv_store (hv, key, 32, newSViv ((long)o), 0);
894 MODULE = urxvt PACKAGE = urxvt::overlay
897 overlay::set (int x, int y, SV *text, SV *rend = 0)
903 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->THIS->self), "_overlay", 8, 0);
906 HV *hv = (HV *)SvRV (*ovs);
907 char key[33]; sprintf (key, "%32lx", (long)THIS);
908 hv_delete (hv, key, 32, G_DISCARD);
914 MODULE = urxvt PACKAGE = urxvt::timer
919 timer *w = new timer;
921 RETVAL = newSVptr ((void *)w, "urxvt::timer");
922 w->self = (HV *)SvRV (RETVAL);
942 timer::interval (NV interval)
944 THIS->interval = interval;
950 timer::set (NV tstamp)
958 timer::start (NV tstamp = THIS->at)
960 THIS->start (tstamp);
976 MODULE = urxvt PACKAGE = urxvt::iow
982 RETVAL = newSVptr ((void *)w, "urxvt::iow");
983 w->self = (HV *)SvRV (RETVAL);
1004 iow::events (short events)
1006 THIS->events = events;