--- rxvt-unicode/src/rxvtperl.xs 2006/01/03 02:01:27 1.11 +++ rxvt-unicode/src/rxvtperl.xs 2006/01/03 19:10:54 1.18 @@ -63,22 +63,32 @@ newSVptr (void *ptr, const char *klass) { HV *hv = newHV (); - hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0); + sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0); return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); } +static void +clearSVptr (SV *sv) +{ + if (SvROK (sv)) + sv = SvRV (sv); + + hv_clear ((HV *)sv); + sv_unmagic (sv, PERL_MAGIC_ext); +} + static long SvPTR (SV *sv, const char *klass) { if (!sv_derived_from (sv, klass)) croak ("object of type %s expected", klass); - IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1)); + MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); - if (!iv) + if (!mg) croak ("perl code used %s object, but C++ object is already destroyed, caught", klass); - return (long)iv; + return (long)mg->mg_ptr; } #define newSVterm(term) SvREFCNT_inc ((SV *)term->self) @@ -141,6 +151,8 @@ struct timer : time_watcher, perl_watcher { + tstamp interval; + timer () : time_watcher (this, &timer::execute) { @@ -148,6 +160,9 @@ void execute (time_watcher &w) { + if (interval) + start (at + interval); + invoke ("urxvt::timer", newSVtimer (this)); } }; @@ -170,6 +185,176 @@ ///////////////////////////////////////////////////////////////////////////// +#define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay") + +struct overlay { + HV *self; + rxvt_term *THIS; + int x, y, w, h; + int border; + text_t **text; + rend_t **rend; + + overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border); + ~overlay (); + + void show (); + void hide (); + + void swap (); + + void set (int x, int y, SV *str, SV *rend); +}; + +overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border) +: THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2) +{ + if (border == 2) + { + w += 2; + h += 2; + } + + text = new text_t *[h]; + rend = new rend_t *[h]; + + for (int y = 0; y < h; y++) + { + text_t *tp = text[y] = new text_t[w]; + rend_t *rp = rend[y] = new rend_t[w]; + + text_t t0, t1, t2; + rend_t r = rstyle; + + if (border == 2) + { + if (y == 0) + t0 = 0x2554, t1 = 0x2550, t2 = 0x2557; + else if (y < h - 1) + t0 = 0x2551, t1 = 0x0020, t2 = 0x2551; + else + t0 = 0x255a, t1 = 0x2550, t2 = 0x255d; + + *tp++ = t0; + *rp++ = r; + + for (int x = w - 2; x-- > 0; ) + { + *tp++ = t1; + *rp++ = r; + } + + *tp = t2; + *rp = r; + } + else + for (int x = w; x-- > 0; ) + { + *tp++ = 0x0020; + *rp++ = r; + } + } + + show (); + THIS->want_refresh = 1; +} + +overlay::~overlay () +{ + hide (); + + for (int y = h; y--; ) + { + delete [] text[y]; + delete [] rend[y]; + } + + delete [] text; + delete [] rend; + + THIS->want_refresh = 1; +} + +void +overlay::show () +{ + char key[33]; sprintf (key, "%32lx", (long)this); + + HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->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); + + if (ovs) + { + char key[33]; sprintf (key, "%32lx", (long)this); + + HV *hv = (HV *)SvRV (*ovs); + hv_delete (hv, key, 32, G_DISCARD); + } +} + +void overlay::swap () +{ + int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w)); + int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h)); + + int ov_w = min (w, THIS->ncol - ov_x); + int ov_h = min (h, THIS->nrow - ov_y); + + for (int y = ov_h; y--; ) + { + text_t *t1 = text [y]; + rend_t *r1 = rend [y]; + + text_t *t2 = ROW(y + ov_y - THIS->view_start).t + ov_x; + rend_t *r2 = ROW(y + ov_y - THIS->view_start).r + ov_x; + + for (int x = ov_w; x--; ) + { + text_t t = *t1; *t1++ = *t2; *t2++ = t; + rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t)); + } + } + +} + +void overlay::set (int x, int y, SV *text, SV *rend) +{ + x += border; + y += border; + + if (!IN_RANGE_EXC (y, 0, h - border)) + return; + + wchar_t *wtext = sv2wcs (text); + + for (int col = min (wcslen (wtext), w - x - border); col--; ) + this->text [y][x + col] = wtext [col]; + + free (wtext); + + if (rend) + { + if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV) + croak ("rend must be arrayref"); + + AV *av = (AV *)SvRV (rend); + + for (int col = min (av_len (av) + 1, w - x - border); col--; ) + this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1)); + } + + THIS->want_refresh = 1; +} + + +///////////////////////////////////////////////////////////////////////////// + struct rxvt_perl_interp rxvt_perl; static PerlInterpreter *perl; @@ -215,12 +400,34 @@ bool rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) { - if (!perl - || (!should_invoke [htype] && htype != HOOK_INIT && htype != HOOK_DESTROY)) + if (!perl) return false; if (htype == HOOK_INIT) // first hook ever called - term->self = (void *)newSVptr ((void *)term, "urxvt::term"); + { + term->self = (void *)newSVptr ((void *)term, "urxvt::term"); + hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0); + } + else if (!term->self) + return false; // perl not initialized for this instance + else if (htype == HOOK_DESTROY) + { + // handled later + } + else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END) + { + HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0)); + + if (HvKEYS (hv)) + { + hv_iterinit (hv); + + while (HE *he = hv_iternext (hv)) + ((overlay *)SvIV (hv_iterval (hv, he)))->swap (); + } + } + else if (!should_invoke [htype]) + return false; dSP; va_list ap; @@ -275,8 +482,7 @@ if (htype == HOOK_DESTROY) { - // TODO: clear magic - hv_clear ((HV *)SvRV ((SV *)term->self)); + clearSVptr ((SV *)term->self); SvREFCNT_dec ((SV *)term->self); } @@ -365,16 +571,36 @@ RETVAL int -SET_FGCOLOR (int rend, int color) +SET_FGCOLOR (int rend, int new_color) + CODE: + RETVAL = SET_FGCOLOR (rend, new_color); + OUTPUT: + RETVAL + +int +SET_BGCOLOR (int rend, int new_color) + CODE: + RETVAL = SET_BGCOLOR (rend, new_color); + OUTPUT: + RETVAL + +int +GET_CUSTOM (int rend) CODE: - RETVAL = SET_FGCOLOR (rend, color); + RETVAL = (rend && RS_customMask) >> RS_customShift; OUTPUT: RETVAL int -SET_BGCOLOR (int rend, int color) +SET_CUSTOM (int rend, int new_value) CODE: - RETVAL = SET_BGCOLOR (rend, color); +{ + if (!IN_RANGE_EXC (new_value, 0, RS_customCount)) + croak ("custom value out of range, must be 0..%d", RS_customCount - 1); + + RETVAL = (rend & ~RS_customMask) + | ((new_value << RS_customShift) & RS_customMask); +} OUTPUT: RETVAL @@ -502,9 +728,7 @@ if (new_text) { - STRLEN slen; - char *str = SvPVutf8 (new_text, slen); - wchar_t *wstr = rxvt_utf8towcs (str, slen); + wchar_t *wstr = sv2wcs (new_text); int len = wcslen (wstr); @@ -660,7 +884,7 @@ } int -rxvt_term::selection_grab (int eventtime) +rxvt_term::selection_grab (int eventtime = CurrentTime) void rxvt_term::selection (SV *newtext = 0) @@ -685,32 +909,37 @@ } void -rxvt_term::scr_overlay_new (int x, int y, int w, int h) +rxvt_term::tt_write (SV *octets) + INIT: + STRLEN len; + char *str = SvPVbyte (octets, len); + C_ARGS: + (unsigned char *)str, len + +SV * +rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2) + CODE: +{ + overlay *o = new overlay (THIS, x, y, w, h, rstyle, border); + RETVAL = newSVptr ((void *)o, "urxvt::overlay"); + o->self = (HV *)SvRV (RETVAL); +} + OUTPUT: + RETVAL + +MODULE = urxvt PACKAGE = urxvt::overlay void -rxvt_term::scr_overlay_off () +overlay::set (int x, int y, SV *text, SV *rend = 0) void -rxvt_term::scr_overlay_set_char (int x, int y, U32 text, U32 rend = OVERLAY_RSTYLE) - CODE: - THIS->scr_overlay_set (x, y, text, rend); +overlay::show () void -rxvt_term::scr_overlay_set (int x, int y, SV *text) - CODE: -{ - wchar_t *wtext = sv2wcs (text); - THIS->scr_overlay_set (x, y, wtext); - free (wtext); -} +overlay::hide () void -rxvt_term::tt_write (SV *octets) - INIT: - STRLEN len; - char *str = SvPVbyte (octets, len); - C_ARGS: - (unsigned char *)str, len +overlay::DESTROY () MODULE = urxvt PACKAGE = urxvt::timer @@ -718,6 +947,7 @@ timer::new () CODE: timer *w = new timer; + w->start (NOW); RETVAL = newSVptr ((void *)w, "urxvt::timer"); w->self = (HV *)SvRV (RETVAL); OUTPUT: @@ -738,6 +968,14 @@ OUTPUT: RETVAL +timer * +timer::interval (NV interval) + CODE: + THIS->interval = interval; + RETVAL = THIS; + OUTPUT: + RETVAL + timer * timer::set (NV tstamp) CODE: