--- rxvt-unicode/src/rxvtperl.xs 2006/01/03 04:18:47 1.13 +++ 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) @@ -188,6 +198,9 @@ 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); @@ -242,11 +255,14 @@ } } + show (); THIS->want_refresh = 1; } overlay::~overlay () { + hide (); + for (int y = h; y--; ) { delete [] text[y]; @@ -259,6 +275,29 @@ 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)); @@ -284,7 +323,7 @@ } -void overlay::set (int x, int y, SV *str, SV *rend) +void overlay::set (int x, int y, SV *text, SV *rend) { x += border; y += border; @@ -292,12 +331,23 @@ if (!IN_RANGE_EXC (y, 0, h - border)) return; - wchar_t *wstr = sv2wcs (str); + wchar_t *wtext = sv2wcs (text); - for (int col = min (wcslen (wstr), w - x - border); col--; ) - text [y][x + col] = wstr [col]; + for (int col = min (wcslen (wtext), w - x - border); col--; ) + this->text [y][x + col] = wtext [col]; - free (wstr); + 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; } @@ -358,6 +408,8 @@ 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 @@ -430,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); } @@ -833,7 +884,7 @@ } int -rxvt_term::selection_grab (int eventtime) +rxvt_term::selection_grab (int eventtime = CurrentTime) void rxvt_term::selection (SV *newtext = 0) @@ -872,10 +923,6 @@ overlay *o = new overlay (THIS, x, y, w, h, rstyle, border); RETVAL = newSVptr ((void *)o, "urxvt::overlay"); o->self = (HV *)SvRV (RETVAL); - - HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0)); - char key[33]; sprintf (key, "%32lx", (long)o); - hv_store (hv, key, 32, newSViv ((long)o), 0); } OUTPUT: RETVAL @@ -886,19 +933,13 @@ overlay::set (int x, int y, SV *text, SV *rend = 0) void -overlay::DESTROY () - CODE: -{ - SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->THIS->self), "_overlay", 8, 0); - if (ovs) - { - HV *hv = (HV *)SvRV (*ovs); - char key[33]; sprintf (key, "%32lx", (long)THIS); - hv_delete (hv, key, 32, G_DISCARD); - } +overlay::show () - delete THIS; -} +void +overlay::hide () + +void +overlay::DESTROY () MODULE = urxvt PACKAGE = urxvt::timer