ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
(Generate patch)

Comparing rxvt-unicode/src/rxvtperl.xs (file contents):
Revision 1.12 by root, Tue Jan 3 02:42:17 2006 UTC vs.
Revision 1.18 by root, Tue Jan 3 19:10:54 2006 UTC

61//TODO: use magic 61//TODO: use magic
62static SV * 62static SV *
63newSVptr (void *ptr, const char *klass) 63newSVptr (void *ptr, const char *klass)
64{ 64{
65 HV *hv = newHV (); 65 HV *hv = newHV ();
66 hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0); 66 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
67 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 67 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
68}
69
70static void
71clearSVptr (SV *sv)
72{
73 if (SvROK (sv))
74 sv = SvRV (sv);
75
76 hv_clear ((HV *)sv);
77 sv_unmagic (sv, PERL_MAGIC_ext);
68} 78}
69 79
70static long 80static long
71SvPTR (SV *sv, const char *klass) 81SvPTR (SV *sv, const char *klass)
72{ 82{
73 if (!sv_derived_from (sv, klass)) 83 if (!sv_derived_from (sv, klass))
74 croak ("object of type %s expected", klass); 84 croak ("object of type %s expected", klass);
75 85
76 IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1)); 86 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
77 87
78 if (!iv) 88 if (!mg)
79 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass); 89 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
80 90
81 return (long)iv; 91 return (long)mg->mg_ptr;
82} 92}
83 93
84#define newSVterm(term) SvREFCNT_inc ((SV *)term->self) 94#define newSVterm(term) SvREFCNT_inc ((SV *)term->self)
85#define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term") 95#define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
86 96
139#define newSVtimer(timer) new_ref (timer->self, "urxvt::timer") 149#define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
140#define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer") 150#define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
141 151
142struct timer : time_watcher, perl_watcher 152struct timer : time_watcher, perl_watcher
143{ 153{
154 tstamp interval;
155
144 timer () 156 timer ()
145 : time_watcher (this, &timer::execute) 157 : time_watcher (this, &timer::execute)
146 { 158 {
147 } 159 }
148 160
149 void execute (time_watcher &w) 161 void execute (time_watcher &w)
150 { 162 {
163 if (interval)
164 start (at + interval);
165
151 invoke ("urxvt::timer", newSVtimer (this)); 166 invoke ("urxvt::timer", newSVtimer (this));
152 } 167 }
153}; 168};
154 169
155#define newSViow(iow) new_ref (iow->self, "urxvt::iow") 170#define newSViow(iow) new_ref (iow->self, "urxvt::iow")
165 void execute (io_watcher &w, short revents) 180 void execute (io_watcher &w, short revents)
166 { 181 {
167 invoke ("urxvt::iow", newSViow (this), revents); 182 invoke ("urxvt::iow", newSViow (this), revents);
168 } 183 }
169}; 184};
185
186/////////////////////////////////////////////////////////////////////////////
187
188#define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
189
190struct overlay {
191 HV *self;
192 rxvt_term *THIS;
193 int x, y, w, h;
194 int border;
195 text_t **text;
196 rend_t **rend;
197
198 overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
199 ~overlay ();
200
201 void show ();
202 void hide ();
203
204 void swap ();
205
206 void set (int x, int y, SV *str, SV *rend);
207};
208
209overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
210: THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2)
211{
212 if (border == 2)
213 {
214 w += 2;
215 h += 2;
216 }
217
218 text = new text_t *[h];
219 rend = new rend_t *[h];
220
221 for (int y = 0; y < h; y++)
222 {
223 text_t *tp = text[y] = new text_t[w];
224 rend_t *rp = rend[y] = new rend_t[w];
225
226 text_t t0, t1, t2;
227 rend_t r = rstyle;
228
229 if (border == 2)
230 {
231 if (y == 0)
232 t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
233 else if (y < h - 1)
234 t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
235 else
236 t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
237
238 *tp++ = t0;
239 *rp++ = r;
240
241 for (int x = w - 2; x-- > 0; )
242 {
243 *tp++ = t1;
244 *rp++ = r;
245 }
246
247 *tp = t2;
248 *rp = r;
249 }
250 else
251 for (int x = w; x-- > 0; )
252 {
253 *tp++ = 0x0020;
254 *rp++ = r;
255 }
256 }
257
258 show ();
259 THIS->want_refresh = 1;
260}
261
262overlay::~overlay ()
263{
264 hide ();
265
266 for (int y = h; y--; )
267 {
268 delete [] text[y];
269 delete [] rend[y];
270 }
271
272 delete [] text;
273 delete [] rend;
274
275 THIS->want_refresh = 1;
276}
277
278void
279overlay::show ()
280{
281 char key[33]; sprintf (key, "%32lx", (long)this);
282
283 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0));
284 hv_store (hv, key, 32, newSViv ((long)this), 0);
285}
286
287void
288overlay::hide ()
289{
290 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->self), "_overlay", 8, 0);
291
292 if (ovs)
293 {
294 char key[33]; sprintf (key, "%32lx", (long)this);
295
296 HV *hv = (HV *)SvRV (*ovs);
297 hv_delete (hv, key, 32, G_DISCARD);
298 }
299}
300
301void overlay::swap ()
302{
303 int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
304 int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
305
306 int ov_w = min (w, THIS->ncol - ov_x);
307 int ov_h = min (h, THIS->nrow - ov_y);
308
309 for (int y = ov_h; y--; )
310 {
311 text_t *t1 = text [y];
312 rend_t *r1 = rend [y];
313
314 text_t *t2 = ROW(y + ov_y - THIS->view_start).t + ov_x;
315 rend_t *r2 = ROW(y + ov_y - THIS->view_start).r + ov_x;
316
317 for (int x = ov_w; x--; )
318 {
319 text_t t = *t1; *t1++ = *t2; *t2++ = t;
320 rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
321 }
322 }
323
324}
325
326void overlay::set (int x, int y, SV *text, SV *rend)
327{
328 x += border;
329 y += border;
330
331 if (!IN_RANGE_EXC (y, 0, h - border))
332 return;
333
334 wchar_t *wtext = sv2wcs (text);
335
336 for (int col = min (wcslen (wtext), w - x - border); col--; )
337 this->text [y][x + col] = wtext [col];
338
339 free (wtext);
340
341 if (rend)
342 {
343 if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
344 croak ("rend must be arrayref");
345
346 AV *av = (AV *)SvRV (rend);
347
348 for (int col = min (av_len (av) + 1, w - x - border); col--; )
349 this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
350 }
351
352 THIS->want_refresh = 1;
353}
354
170 355
171///////////////////////////////////////////////////////////////////////////// 356/////////////////////////////////////////////////////////////////////////////
172 357
173struct rxvt_perl_interp rxvt_perl; 358struct rxvt_perl_interp rxvt_perl;
174 359
213} 398}
214 399
215bool 400bool
216rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) 401rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
217{ 402{
218 if (!perl 403 if (!perl)
219 || (!should_invoke [htype] && htype != HOOK_INIT && htype != HOOK_DESTROY))
220 return false; 404 return false;
221 405
222 if (htype == HOOK_INIT) // first hook ever called 406 if (htype == HOOK_INIT) // first hook ever called
407 {
223 term->self = (void *)newSVptr ((void *)term, "urxvt::term"); 408 term->self = (void *)newSVptr ((void *)term, "urxvt::term");
409 hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
410 }
411 else if (!term->self)
412 return false; // perl not initialized for this instance
413 else if (htype == HOOK_DESTROY)
414 {
415 // handled later
416 }
417 else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
418 {
419 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0));
420
421 if (HvKEYS (hv))
422 {
423 hv_iterinit (hv);
424
425 while (HE *he = hv_iternext (hv))
426 ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
427 }
428 }
429 else if (!should_invoke [htype])
430 return false;
224 431
225 dSP; 432 dSP;
226 va_list ap; 433 va_list ap;
227 434
228 va_start (ap, htype); 435 va_start (ap, htype);
273 if (SvTRUE (ERRSV)) 480 if (SvTRUE (ERRSV))
274 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); 481 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
275 482
276 if (htype == HOOK_DESTROY) 483 if (htype == HOOK_DESTROY)
277 { 484 {
278 // TODO: clear magic
279 hv_clear ((HV *)SvRV ((SV *)term->self)); 485 clearSVptr ((SV *)term->self);
280 SvREFCNT_dec ((SV *)term->self); 486 SvREFCNT_dec ((SV *)term->self);
281 } 487 }
282 488
283 return count; 489 return count;
284 } 490 }
520 free (str); 726 free (str);
521 } 727 }
522 728
523 if (new_text) 729 if (new_text)
524 { 730 {
525 STRLEN slen;
526 char *str = SvPVutf8 (new_text, slen); 731 wchar_t *wstr = sv2wcs (new_text);
527 wchar_t *wstr = rxvt_utf8towcs (str, slen);
528 732
529 int len = wcslen (wstr); 733 int len = wcslen (wstr);
530 734
531 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) 735 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
532 { 736 {
678 THIS->want_refresh = 1; 882 THIS->want_refresh = 1;
679 } 883 }
680} 884}
681 885
682int 886int
683rxvt_term::selection_grab (int eventtime) 887rxvt_term::selection_grab (int eventtime = CurrentTime)
684 888
685void 889void
686rxvt_term::selection (SV *newtext = 0) 890rxvt_term::selection (SV *newtext = 0)
687 PPCODE: 891 PPCODE:
688{ 892{
703 THIS->selection.len = wcslen (THIS->selection.text); 907 THIS->selection.len = wcslen (THIS->selection.text);
704 } 908 }
705} 909}
706 910
707void 911void
708rxvt_term::scr_overlay_new (int x, int y, int w, int h)
709
710void
711rxvt_term::scr_overlay_off ()
712
713void
714rxvt_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
718void
719rxvt_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
727void
728rxvt_term::tt_write (SV *octets) 912rxvt_term::tt_write (SV *octets)
729 INIT: 913 INIT:
730 STRLEN len; 914 STRLEN len;
731 char *str = SvPVbyte (octets, len); 915 char *str = SvPVbyte (octets, len);
732 C_ARGS: 916 C_ARGS:
733 (unsigned char *)str, len 917 (unsigned char *)str, len
734 918
919SV *
920rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
921 CODE:
922{
923 overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
924 RETVAL = newSVptr ((void *)o, "urxvt::overlay");
925 o->self = (HV *)SvRV (RETVAL);
926}
927 OUTPUT:
928 RETVAL
929
930MODULE = urxvt PACKAGE = urxvt::overlay
931
932void
933overlay::set (int x, int y, SV *text, SV *rend = 0)
934
935void
936overlay::show ()
937
938void
939overlay::hide ()
940
941void
942overlay::DESTROY ()
943
735MODULE = urxvt PACKAGE = urxvt::timer 944MODULE = urxvt PACKAGE = urxvt::timer
736 945
737SV * 946SV *
738timer::new () 947timer::new ()
739 CODE: 948 CODE:
740 timer *w = new timer; 949 timer *w = new timer;
950 w->start (NOW);
741 RETVAL = newSVptr ((void *)w, "urxvt::timer"); 951 RETVAL = newSVptr ((void *)w, "urxvt::timer");
742 w->self = (HV *)SvRV (RETVAL); 952 w->self = (HV *)SvRV (RETVAL);
743 OUTPUT: 953 OUTPUT:
744 RETVAL 954 RETVAL
745 955
753 963
754NV 964NV
755timer::at () 965timer::at ()
756 CODE: 966 CODE:
757 RETVAL = THIS->at; 967 RETVAL = THIS->at;
968 OUTPUT:
969 RETVAL
970
971timer *
972timer::interval (NV interval)
973 CODE:
974 THIS->interval = interval;
975 RETVAL = THIS;
758 OUTPUT: 976 OUTPUT:
759 RETVAL 977 RETVAL
760 978
761timer * 979timer *
762timer::set (NV tstamp) 980timer::set (NV tstamp)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines