--- rxvt-unicode/src/rxvtperl.xs 2006/01/05 01:04:10 1.25 +++ rxvt-unicode/src/rxvtperl.xs 2006/01/09 01:36:56 1.46 @@ -3,7 +3,7 @@ *----------------------------------------------------------------------* * * All portions of code are copyright by their respective author/s. - * Copyright (c) 2005-2005 Marc Lehmann + * Copyright (c) 2005-2006 Marc Lehmann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ #include "../config.h" +#include #include #include "rxvt.h" @@ -37,6 +38,12 @@ #include "perlxsi.c" +#if defined(HAVE_SCROLLBARS) || defined(MENUBAR) +# define GRAB_CURSOR THIS->leftptr_cursor +#else +# define GRAB_CURSOR None +#endif + #undef LINENO #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows) #undef ROW @@ -44,6 +51,22 @@ ///////////////////////////////////////////////////////////////////////////// +static SV * +taint (SV *sv) +{ + SvTAINT (sv); + return sv; +} + +static SV * +taint_if (SV *sv, SV *src) +{ + if (SvTAINTED (src)) + SvTAINT (sv); + + return sv; +} + static wchar_t * sv2wcs (SV *sv) { @@ -103,7 +126,7 @@ return (long)mg->mg_ptr; } -#define newSVterm(term) SvREFCNT_inc ((SV *)term->self) +#define newSVterm(term) SvREFCNT_inc ((SV *)term->perl.self) #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term") ///////////////////////////////////////////////////////////////////////////// @@ -292,14 +315,14 @@ { char key[33]; sprintf (key, "%32lx", (long)this); - HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0)); + HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0)); hv_store (hv, key, 32, newSViv ((long)this), 0); } void overlay::hide () { - SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0); + SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0); if (ovs) { @@ -391,13 +414,14 @@ { char *argv[] = { "", + "-T", "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1", }; perl = perl_alloc (); perl_construct (perl); - if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) + if (perl_parse (perl, xs_init, 3, argv, (char **)NULL) || perl_run (perl)) { rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n"); @@ -409,6 +433,17 @@ } } +static void +ungrab (rxvt_term *THIS) +{ + if (THIS->perl.grabtime) + { + XUngrabKeyboard (THIS->display->display, THIS->perl.grabtime); + XUngrabPointer (THIS->display->display, THIS->perl.grabtime); + THIS->perl.grabtime = 0; + } +} + bool rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) { @@ -417,10 +452,10 @@ if (htype == HOOK_INIT) // first hook ever called { - term->self = (void *)newSVptr ((void *)term, "urxvt::term"); - hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0); + term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term"); + hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0); } - else if (!term->self) + else if (!term->perl.self) return false; // perl not initialized for this instance else if (htype == HOOK_DESTROY) { @@ -428,7 +463,7 @@ } else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END) { - HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0)); + HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0)); if (HvKEYS (hv)) { @@ -467,23 +502,79 @@ XPUSHs (sv_2mortal (newSViv (va_arg (ap, long)))); break; - case DT_STRING: - XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); + case DT_STR: + XPUSHs (taint (sv_2mortal (newSVpv (va_arg (ap, char *), 0)))); + break; + + case DT_STR_LEN: + { + char *str = va_arg (ap, char *); + int len = va_arg (ap, int); + + XPUSHs (taint (sv_2mortal (newSVpvn (str, len)))); + } break; - case DT_USTRING_LEN: + case DT_WCS_LEN: { - unicode_t *ustr = va_arg (ap, unicode_t *); - int ulen = va_arg (ap, int); - wchar_t *wstr = new wchar_t [ulen]; + wchar_t *wstr = va_arg (ap, wchar_t *); + int wlen = va_arg (ap, int); + + XPUSHs (taint (sv_2mortal (wcs2sv (wstr, wlen)))); + } + break; - for (int i = ulen; i--; ) - wstr [i] = ustr [i]; + case DT_XEVENT: + { + XEvent *xe = va_arg (ap, XEvent *); + HV *hv = newHV (); - XPUSHs (sv_2mortal (wcs2sv (wstr, ulen))); +# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0) +# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0) +# undef set + + setiv (type, xe->type); + setiv (send_event, xe->xany.send_event); + setiv (serial, xe->xany.serial); - delete [] wstr; + switch (xe->type) + { + case KeyPress: + case KeyRelease: + case ButtonPress: + case ButtonRelease: + case MotionNotify: + setiv (time, xe->xmotion.time); + setiv (x, xe->xmotion.x); + setiv (y, xe->xmotion.y); + setiv (row, xe->xmotion.y / term->fheight); + setiv (col, xe->xmotion.x / term->fwidth); + setiv (x_root, xe->xmotion.x_root); + setiv (y_root, xe->xmotion.y_root); + setiv (state, xe->xmotion.state); + break; + } + + switch (xe->type) + { + case KeyPress: + case KeyRelease: + setiv (keycode, xe->xkey.keycode); + break; + + case ButtonPress: + case ButtonRelease: + setiv (button, xe->xbutton.button); + break; + + case MotionNotify: + setiv (is_hint, xe->xmotion.is_hint); + break; + } + + XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); } + break; case DT_END: { @@ -504,12 +595,15 @@ LEAVE; if (SvTRUE (ERRSV)) - rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); + { + rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); + ungrab (term); // better lose the grab than the session + } if (htype == HOOK_DESTROY) { - clearSVptr ((SV *)term->self); - SvREFCNT_dec ((SV *)term->self); + clearSVptr ((SV *)term->perl.self); + SvREFCNT_dec ((SV *)term->perl.self); } return count; @@ -529,23 +623,77 @@ BOOT: { -# define export_const(name) newCONSTSUB (gv_stashpv ("urxvt", 1), # name, newSViv (name)); + sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1)); + AV *hookname = get_av ("urxvt::HOOKNAME", 1); # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0)); # include "hookinc.h" # undef def - export_const (DEFAULT_RSTYLE); - export_const (OVERLAY_RSTYLE); - export_const (RS_Bold); - export_const (RS_Italic); - export_const (RS_Blink); - export_const (RS_RVid); - export_const (RS_Uline); + HV *option = get_hv ("urxvt::OPTION", 1); +# define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0); +# define nodef(name) +# include "optinc.h" +# undef nodef +# undef def - sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR); + HV *stash = gv_stashpv ("urxvt", 1); +# define export_const_iv(name) newCONSTSUB (stash, # name, newSViv (name)); + export_const_iv (DEFAULT_RSTYLE); + export_const_iv (OVERLAY_RSTYLE); + export_const_iv (RS_Bold); + export_const_iv (RS_Italic); + export_const_iv (RS_Blink); + export_const_iv (RS_RVid); + export_const_iv (RS_Uline); + + export_const_iv (CurrentTime); + export_const_iv (ShiftMask); + export_const_iv (LockMask); + export_const_iv (ControlMask); + export_const_iv (Mod1Mask); + export_const_iv (Mod2Mask); + export_const_iv (Mod3Mask); + export_const_iv (Mod4Mask); + export_const_iv (Mod5Mask); + export_const_iv (Button1Mask); + export_const_iv (Button2Mask); + export_const_iv (Button3Mask); + export_const_iv (Button4Mask); + export_const_iv (Button5Mask); + export_const_iv (AnyModifier); } +SV * +new (...) + CODE: +{ + stringvec *argv = new stringvec; + bool success; + + for (int i = 0; i < items ;i++) + argv->push_back (strdup (SvPVbyte_nolen (ST (i)))); + + rxvt_term *term = new rxvt_term; + + term->argv = argv; + + try + { + if (!term->init (argv->size (), argv->begin ())) + term = 0; + } + catch (const class rxvt_failure_exception &e) + { + term->destroy (); + croak ("exception caught while initializing new terminal instance"); + } + + RETVAL = term && term->perl.self ? newSVterm (term) : &PL_sv_undef; +} + OUTPUT: + RETVAL + void set_should_invoke (int htype, int value) CODE: @@ -561,6 +709,14 @@ CODE: rxvt_fatal ("%s", msg); +SV * +untaint (SV *sv) + CODE: + RETVAL = newSVsv (sv); + SvTAINTED_off (RETVAL); + OUTPUT: + RETVAL + NV NOW () CODE: @@ -618,6 +774,58 @@ MODULE = urxvt PACKAGE = urxvt::term +void +rxvt_term::destroy () + +void +rxvt_term::grab_button (int button, U32 modifiers) + CODE: + XGrabButton (THIS->display->display, button, modifiers, THIS->vt, 1, + ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, + GrabModeSync, GrabModeSync, None, GRAB_CURSOR); + +bool +rxvt_term::grab (U32 eventtime, int sync = 0) + CODE: +{ + int mode = sync ? GrabModeSync : GrabModeAsync; + + THIS->perl.grabtime = 0; + + if (!XGrabPointer (THIS->display->display, THIS->vt, 0, + ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, + mode, mode, None, GRAB_CURSOR, eventtime)) + if (!XGrabKeyboard (THIS->display->display, THIS->vt, 0, mode, mode, eventtime)) + THIS->perl.grabtime = eventtime; + else + XUngrabPointer (THIS->display->display, eventtime); + + RETVAL = !!THIS->perl.grabtime; +} + OUTPUT: + RETVAL + +void +rxvt_term::allow_events_async () + CODE: + XAllowEvents (THIS->display->display, AsyncBoth, THIS->perl.grabtime); + +void +rxvt_term::allow_events_sync () + CODE: + XAllowEvents (THIS->display->display, SyncBoth, THIS->perl.grabtime); + +void +rxvt_term::allow_events_replay () + CODE: + XAllowEvents (THIS->display->display, ReplayPointer, THIS->perl.grabtime); + XAllowEvents (THIS->display->display, ReplayKeyboard, THIS->perl.grabtime); + +void +rxvt_term::ungrab () + CODE: + ungrab (THIS); + int rxvt_term::strwidth (SV *str) CODE: @@ -645,7 +853,7 @@ free (wstr); - RETVAL = newSVpv (mbstr, 0); + RETVAL = taint_if (newSVpv (mbstr, 0), str); free (mbstr); } OUTPUT: @@ -662,14 +870,13 @@ wchar_t *wstr = rxvt_mbstowcs (data, len); rxvt_pop_locale (); - RETVAL = wcs2sv (wstr); + RETVAL = taint_if (wcs2sv (wstr), octets); free (wstr); } OUTPUT: RETVAL -# very portable, especially on objects as opposed to pods -#define TERM_OFFSET(sym) (((char *)&((TermWin_t *)0)->sym) - (char *)(TermWin_t *)0) +#define TERM_OFFSET(sym) offsetof (TermWin_t, sym) #define TERM_OFFSET_width TERM_OFFSET(width) #define TERM_OFFSET_height TERM_OFFSET(height) @@ -704,6 +911,36 @@ OUTPUT: RETVAL +unsigned int +rxvt_term::ModLevel3Mask () + ALIAS: + ModLevel3Mask = 0 + ModMetaMask = 1 + ModNumLockMask = 2 + CODE: + switch (ix) + { + case 0: RETVAL = THIS->ModLevel3Mask; break; + case 1: RETVAL = THIS->ModMetaMask; break; + case 2: RETVAL = THIS->ModNumLockMask; break; + } + OUTPUT: + RETVAL + +U32 +rxvt_term::parent () + CODE: + RETVAL = (U32)THIS->parent [0]; + OUTPUT: + RETVAL + +U32 +rxvt_term::vt () + CODE: + RETVAL = (U32)THIS->vt; + OUTPUT: + RETVAL + U32 rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle) CODE: @@ -735,7 +972,7 @@ THIS->want_refresh = 1; void -rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0) +rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS) PPCODE: { if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) @@ -747,10 +984,10 @@ { wchar_t *wstr = new wchar_t [THIS->ncol]; - for (int col = 0; col ncol; col++) + for (int col = 0; col < THIS->ncol; col++) wstr [col] = l.t [col]; - XPUSHs (sv_2mortal (wcs2sv (wstr))); + XPUSHs (taint (sv_2mortal (wcs2sv (wstr, THIS->ncol)))); delete [] wstr; } @@ -759,7 +996,7 @@ { wchar_t *wstr = sv2wcs (new_text); - int len = wcslen (wstr); + int len = min (wcslen (wstr) - start_ofs, max_len); if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) { @@ -769,7 +1006,7 @@ for (int col = start_col; col < start_col + len; col++) { - l.t [col] = wstr [col - start_col]; + l.t [col] = wstr [start_ofs + col - start_col]; l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col])); } @@ -778,7 +1015,7 @@ } void -rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0) +rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS) PPCODE: { if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) @@ -803,14 +1040,14 @@ croak ("new_rend must be arrayref"); AV *av = (AV *)SvRV (new_rend); - int len = av_len (av) + 1; + int len = min (av_len (av) + 1 - start_ofs, max_len); if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) croak ("new_rend array extends beyond horizontal margins"); for (int col = start_col; col < start_col + len; col++) { - rend_t r = SvIV (*av_fetch (av, col - start_col, 1)) & ~RS_fontMask; + rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask; l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col])); } @@ -818,43 +1055,111 @@ } int -rxvt_term::ROW_l (int row_number, int new_length = -2) +rxvt_term::ROW_l (int row_number, int new_length = -1) CODE: { if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) XSRETURN_EMPTY; line_t &l = ROW(row_number); - RETVAL = l.l < 0 ? THIS->ncol : l.l; + RETVAL = l.l; - if (new_length >= -1) + if (new_length >= 0) l.l = new_length; } OUTPUT: RETVAL bool -rxvt_term::ROW_is_longer (int row_number) +rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1) CODE: { if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) XSRETURN_EMPTY; line_t &l = ROW(row_number); - RETVAL = l.l < 0; + RETVAL = l.is_longer (); + + if (new_is_longer >= 0) + l.is_longer (new_is_longer); } OUTPUT: RETVAL SV * -rxvt_term::special_encode (SV *str) +rxvt_term::special_encode (SV *string) CODE: - abort ();//TODO +{ + wchar_t *wstr = sv2wcs (string); + int wlen = wcslen (wstr); + wchar_t *rstr = new wchar_t [wlen]; // cannot become longer + + rxvt_push_locale (THIS->locale); + + wchar_t *r = rstr; + for (wchar_t *s = wstr; *s; s++) + if (wcwidth (*s) == 0) + { + if (r == rstr) + croak ("leading combining character unencodable"); + + unicode_t n = rxvt_compose (r[-1], *s); + if (n == NOCHAR) + n = rxvt_composite.compose (r[-1], *s); + + r[-1] = n; + } +#if !UNICODE_3 + else if (*s >= 0x10000) + *r++ = rxvt_composite.compose (*s); +#endif + else + *r++ = *s; + + rxvt_pop_locale (); + + RETVAL = taint_if (wcs2sv (rstr, r - rstr), string); + + delete [] rstr; +} + OUTPUT: + RETVAL SV * -rxvt_term::special_decode (SV *str) +rxvt_term::special_decode (SV *text) CODE: - abort ();//TODO +{ + wchar_t *wstr = sv2wcs (text); + int wlen = wcslen (wstr); + int dlen = 0; + + // find length + for (wchar_t *s = wstr; *s; s++) + if (*s == NOCHAR) + ; + else if (IS_COMPOSE (*s)) + dlen += rxvt_composite.expand (*s, 0); + else + dlen++; + + wchar_t *rstr = new wchar_t [dlen]; + + // decode + wchar_t *r = rstr; + for (wchar_t *s = wstr; *s; s++) + if (*s == NOCHAR) + ; + else if (IS_COMPOSE (*s)) + r += rxvt_composite.expand (*s, r); + else + *r++ = *s; + + RETVAL = taint_if (wcs2sv (rstr, r - rstr), text); + + delete [] rstr; +} + OUTPUT: + RETVAL void rxvt_term::_resource (char *name, int index, SV *newval = 0) @@ -881,7 +1186,7 @@ croak ("requested out-of-bound resource %s+%d,", name, index - rs->value); if (GIMME_V != G_VOID) - XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef); + XPUSHs (THIS->rs [index] ? sv_2mortal (taint (newSVpv (THIS->rs [index], 0))) : &PL_sv_undef); if (newval) { @@ -896,8 +1201,44 @@ } } +bool +rxvt_term::option (U32 optval, int set = -1) + CODE: +{ + RETVAL = THIS->options & optval; + + if (set >= 0) + { + if (set) + THIS->options |= optval; + else + THIS->options &= ~optval; + + switch (optval) + { + case Opt_skipBuiltinGlyphs: + THIS->set_fonts (); + THIS->scr_remap_chars (); + THIS->scr_touch (true); + THIS->want_refresh = 1; + break; + + case Opt_cursorUnderline: + THIS->want_refresh = 1; + break; + +# case Opt_scrollBar_floating: +# case Opt_scrollBar_right: +# THIS->resize_all_windows (THIS->width, THIS->height, 1); +# break; + } + } +} + OUTPUT: + RETVAL + void -rxvt_term::cur (...) +rxvt_term::screen_cur (...) PROTOTYPE: $;$$ ALIAS: screen_cur = 0 @@ -929,14 +1270,16 @@ } int -rxvt_term::selection_grab (int eventtime = CurrentTime) +rxvt_term::selection_grab (U32 eventtime) void rxvt_term::selection (SV *newtext = 0) PPCODE: { if (GIMME_V != G_VOID) - XPUSHs (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))); + XPUSHs (THIS->selection.text + ? taint (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))) + : &PL_sv_undef); if (newtext) { @@ -952,20 +1295,8 @@ CODE: { wchar_t *wstr = sv2wcs (string); - int wlen = wcslen (wstr); - unicode_t *ustr = new unicode_t [wlen]; - int nlines = 0; - - for (int i = wlen; i--; ) - { - ustr [i] = wstr [i]; - nlines += ustr [i] == '\012'; - } - - THIS->scr_add_lines (ustr, nlines, wlen); - + THIS->scr_add_lines (wstr, wcslen (wstr)); free (wstr); - delete [] ustr; } void @@ -976,6 +1307,27 @@ C_ARGS: str, len +void +rxvt_term::cmd_parse (SV *octets) + CODE: +{ + STRLEN len; + char *str = SvPVbyte (octets, len); + + char *old_cmdbuf_ptr = THIS->cmdbuf_ptr; + char *old_cmdbuf_endp = THIS->cmdbuf_endp; + + THIS->cmdbuf_ptr = str; + THIS->cmdbuf_endp = str + len; + + rxvt_push_locale (THIS->locale); + THIS->cmd_parse (); + rxvt_pop_locale (); + + THIS->cmdbuf_ptr = old_cmdbuf_ptr; + THIS->cmdbuf_endp = old_cmdbuf_endp; +} + SV * rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2) CODE: