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.2 by root, Mon Jan 2 17:17:02 2006 UTC vs.
Revision 1.14 by root, Tue Jan 3 04:45:03 2006 UTC

35#include "rxvtutil.h" 35#include "rxvtutil.h"
36#include "rxvtperl.h" 36#include "rxvtperl.h"
37 37
38#include "perlxsi.c" 38#include "perlxsi.c"
39 39
40#undef LINENO
41#define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
42#undef ROW
43#define ROW(n) THIS->row_buf [LINENO (n)]
44
40///////////////////////////////////////////////////////////////////////////// 45/////////////////////////////////////////////////////////////////////////////
41 46
42static wchar_t * 47static wchar_t *
43sv2wcs (SV *sv) 48sv2wcs (SV *sv)
44{ 49{
134#define newSVtimer(timer) new_ref (timer->self, "urxvt::timer") 139#define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
135#define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer") 140#define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
136 141
137struct timer : time_watcher, perl_watcher 142struct timer : time_watcher, perl_watcher
138{ 143{
144 tstamp interval;
145
139 timer () 146 timer ()
140 : time_watcher (this, &timer::execute) 147 : time_watcher (this, &timer::execute)
141 { 148 {
142 } 149 }
143 150
144 void execute (time_watcher &w) 151 void execute (time_watcher &w)
145 { 152 {
153 if (interval)
154 start (at + interval);
155
146 invoke ("urxvt::timer", newSVtimer (this)); 156 invoke ("urxvt::timer", newSVtimer (this));
147 } 157 }
148}; 158};
149 159
150#define newSViow(iow) new_ref (iow->self, "urxvt::iow") 160#define newSViow(iow) new_ref (iow->self, "urxvt::iow")
160 void execute (io_watcher &w, short revents) 170 void execute (io_watcher &w, short revents)
161 { 171 {
162 invoke ("urxvt::iow", newSViow (this), revents); 172 invoke ("urxvt::iow", newSViow (this), revents);
163 } 173 }
164}; 174};
175
176/////////////////////////////////////////////////////////////////////////////
177
178#define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
179
180struct overlay {
181 HV *self;
182 rxvt_term *THIS;
183 int x, y, w, h;
184 int border;
185 text_t **text;
186 rend_t **rend;
187
188 overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
189 ~overlay ();
190
191 void swap ();
192
193 void set (int x, int y, SV *str, SV *rend);
194};
195
196overlay::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)
198{
199 if (border == 2)
200 {
201 w += 2;
202 h += 2;
203 }
204
205 text = new text_t *[h];
206 rend = new rend_t *[h];
207
208 for (int y = 0; y < h; y++)
209 {
210 text_t *tp = text[y] = new text_t[w];
211 rend_t *rp = rend[y] = new rend_t[w];
212
213 text_t t0, t1, t2;
214 rend_t r = rstyle;
215
216 if (border == 2)
217 {
218 if (y == 0)
219 t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
220 else if (y < h - 1)
221 t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
222 else
223 t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
224
225 *tp++ = t0;
226 *rp++ = r;
227
228 for (int x = w - 2; x-- > 0; )
229 {
230 *tp++ = t1;
231 *rp++ = r;
232 }
233
234 *tp = t2;
235 *rp = r;
236 }
237 else
238 for (int x = w; x-- > 0; )
239 {
240 *tp++ = 0x0020;
241 *rp++ = r;
242 }
243 }
244
245 THIS->want_refresh = 1;
246}
247
248overlay::~overlay ()
249{
250 for (int y = h; y--; )
251 {
252 delete [] text[y];
253 delete [] rend[y];
254 }
255
256 delete [] text;
257 delete [] rend;
258
259 THIS->want_refresh = 1;
260}
261
262void overlay::swap ()
263{
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));
266
267 int ov_w = min (w, THIS->ncol - ov_x);
268 int ov_h = min (h, THIS->nrow - ov_y);
269
270 for (int y = ov_h; y--; )
271 {
272 text_t *t1 = text [y];
273 rend_t *r1 = rend [y];
274
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;
277
278 for (int x = ov_w; x--; )
279 {
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));
282 }
283 }
284
285}
286
287void overlay::set (int x, int y, SV *text, SV *rend)
288{
289 x += border;
290 y += border;
291
292 if (!IN_RANGE_EXC (y, 0, h - border))
293 return;
294
295 wchar_t *wtext = sv2wcs (text);
296
297 for (int col = min (wcslen (wtext), w - x - border); col--; )
298 this->text [y][x + col] = wtext [col];
299
300 free (wtext);
301
302 if (rend)
303 {
304 if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
305 croak ("rend must be arrayref");
306
307 AV *av = (AV *)SvRV (rend);
308
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));
311 }
312
313 THIS->want_refresh = 1;
314}
315
165 316
166///////////////////////////////////////////////////////////////////////////// 317/////////////////////////////////////////////////////////////////////////////
167 318
168struct rxvt_perl_interp rxvt_perl; 319struct rxvt_perl_interp rxvt_perl;
169 320
210bool 361bool
211rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) 362rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
212{ 363{
213 if (!perl) 364 if (!perl)
214 return false; 365 return false;
215 366
216 if (htype == HOOK_INIT) // first hook ever called 367 if (htype == HOOK_INIT) // first hook ever called
368 {
217 term->self = (void *)newSVptr ((void *)term, "urxvt::term"); 369 term->self = (void *)newSVptr ((void *)term, "urxvt::term");
370 hv_store ((HV *)SvRV ((SV *)term->self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
371 }
218 else if (htype == HOOK_DESTROY) 372 else if (htype == HOOK_DESTROY)
219 { 373 {
220 // TODO: clear magic 374 // handled later
221 hv_clear ((HV *)SvRV ((SV *)term->self));
222 SvREFCNT_dec ((SV *)term->self);
223 } 375 }
376 else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
377 {
378 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->self), "_overlay", 8, 0));
224 379
380 if (HvKEYS (hv))
381 {
382 hv_iterinit (hv);
383
384 while (HE *he = hv_iternext (hv))
385 ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
386 }
387 }
225 if (!should_invoke [htype]) 388 else if (!should_invoke [htype])
226 return false; 389 return false;
227 390
228 dSP; 391 dSP;
229 va_list ap; 392 va_list ap;
230 393
231 va_start (ap, htype); 394 va_start (ap, htype);
232 395
249 412
250 case DT_LONG: 413 case DT_LONG:
251 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long)))); 414 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
252 break; 415 break;
253 416
417 case DT_STRING:
418 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
419 break;
420
254 case DT_END: 421 case DT_END:
255 { 422 {
256 va_end (ap); 423 va_end (ap);
257 424
258 PUTBACK; 425 PUTBACK;
270 LEAVE; 437 LEAVE;
271 438
272 if (SvTRUE (ERRSV)) 439 if (SvTRUE (ERRSV))
273 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); 440 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
274 441
442 if (htype == HOOK_DESTROY)
443 {
444 // TODO: clear magic
445 hv_clear ((HV *)SvRV ((SV *)term->self));
446 SvREFCNT_dec ((SV *)term->self);
447 }
448
275 return count; 449 return count;
276 } 450 }
277 451
278 default: 452 default:
279 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt); 453 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
288PROTOTYPES: ENABLE 462PROTOTYPES: ENABLE
289 463
290BOOT: 464BOOT:
291{ 465{
292# define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0)) 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));
293 AV *hookname = get_av ("urxvt::HOOKNAME", 1); 468 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
294 set_hookname (LOAD);
295 set_hookname (INIT); 469 set_hookname (INIT);
296 set_hookname (RESET); 470 set_hookname (RESET);
297 set_hookname (START); 471 set_hookname (START);
298 set_hookname (DESTROY); 472 set_hookname (DESTROY);
299 set_hookname (SEL_BEGIN); 473 set_hookname (SEL_BEGIN);
305 set_hookname (VIEW_CHANGE); 479 set_hookname (VIEW_CHANGE);
306 set_hookname (SCROLL_BACK); 480 set_hookname (SCROLL_BACK);
307 set_hookname (TTY_ACTIVITY); 481 set_hookname (TTY_ACTIVITY);
308 set_hookname (REFRESH_BEGIN); 482 set_hookname (REFRESH_BEGIN);
309 set_hookname (REFRESH_END); 483 set_hookname (REFRESH_END);
484 set_hookname (KEYBOARD_COMMAND);
485
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);
310 493
311 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR); 494 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR);
312} 495}
313 496
314void 497void
323 506
324void 507void
325fatal (const char *msg) 508fatal (const char *msg)
326 CODE: 509 CODE:
327 rxvt_fatal ("%s", msg); 510 rxvt_fatal ("%s", msg);
328
329int
330wcswidth (SV *str)
331 CODE:
332{
333 wchar_t *wstr = sv2wcs (str);
334 RETVAL = wcswidth (wstr, wcslen (wstr));
335 free (wstr);
336}
337 OUTPUT:
338 RETVAL
339 511
340NV 512NV
341NOW () 513NOW ()
342 CODE: 514 CODE:
343 RETVAL = NOW; 515 RETVAL = NOW;
344 OUTPUT: 516 OUTPUT:
345 RETVAL 517 RETVAL
346 518
519int
520GET_BASEFG (int rend)
521 CODE:
522 RETVAL = GET_BASEFG (rend);
523 OUTPUT:
524 RETVAL
525
526int
527GET_BASEBG (int rend)
528 CODE:
529 RETVAL = GET_BASEBG (rend);
530 OUTPUT:
531 RETVAL
532
533int
534SET_FGCOLOR (int rend, int new_color)
535 CODE:
536 RETVAL = SET_FGCOLOR (rend, new_color);
537 OUTPUT:
538 RETVAL
539
540int
541SET_BGCOLOR (int rend, int new_color)
542 CODE:
543 RETVAL = SET_BGCOLOR (rend, new_color);
544 OUTPUT:
545 RETVAL
546
547int
548GET_CUSTOM (int rend)
549 CODE:
550 RETVAL = (rend && RS_customMask) >> RS_customShift;
551 OUTPUT:
552 RETVAL
553
554int
555SET_CUSTOM (int rend, int new_value)
556 CODE:
557{
558 if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
559 croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
560
561 RETVAL = (rend & ~RS_customMask)
562 | ((new_value << RS_customShift) & RS_customMask);
563}
564 OUTPUT:
565 RETVAL
566
347MODULE = urxvt PACKAGE = urxvt::term 567MODULE = urxvt PACKAGE = urxvt::term
568
569int
570rxvt_term::strwidth (SV *str)
571 CODE:
572{
573 wchar_t *wstr = sv2wcs (str);
574
575 rxvt_push_locale (THIS->locale);
576 RETVAL = wcswidth (wstr, wcslen (wstr));
577 rxvt_pop_locale ();
578
579 free (wstr);
580}
581 OUTPUT:
582 RETVAL
583
584SV *
585rxvt_term::locale_encode (SV *str)
586 CODE:
587{
588 wchar_t *wstr = sv2wcs (str);
589
590 rxvt_push_locale (THIS->locale);
591 char *mbstr = rxvt_wcstombs (wstr);
592 rxvt_pop_locale ();
593
594 free (wstr);
595
596 RETVAL = newSVpv (mbstr, 0);
597 free (mbstr);
598}
599 OUTPUT:
600 RETVAL
601
602SV *
603rxvt_term::locale_decode (SV *octets)
604 CODE:
605{
606 STRLEN len;
607 char *data = SvPVbyte (octets, len);
608
609 rxvt_push_locale (THIS->locale);
610 wchar_t *wstr = rxvt_mbstowcs (data, len);
611 rxvt_pop_locale ();
612
613 char *str = rxvt_wcstoutf8 (wstr);
614 free (wstr);
615
616 RETVAL = newSVpv (str, 0);
617 SvUTF8_on (RETVAL);
618 free (str);
619}
620 OUTPUT:
621 RETVAL
622
623int
624rxvt_term::nsaved ()
625 CODE:
626 RETVAL = THIS->nsaved;
627 OUTPUT:
628 RETVAL
629
630int
631rxvt_term::view_start (int newval = -1)
632 CODE:
633{
634 RETVAL = THIS->view_start;
635
636 if (newval >= 0)
637 {
638 THIS->view_start = min (newval, THIS->nsaved);
639 THIS->scr_changeview (RETVAL);
640 }
641}
642 OUTPUT:
643 RETVAL
644
645int
646rxvt_term::nrow ()
647 CODE:
648 RETVAL = THIS->nrow;
649 OUTPUT:
650 RETVAL
651
652int
653rxvt_term::ncol ()
654 CODE:
655 RETVAL = THIS->ncol;
656 OUTPUT:
657 RETVAL
658
659void
660rxvt_term::want_refresh ()
661 CODE:
662 THIS->want_refresh = 1;
663
664void
665rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0)
666 PPCODE:
667{
668 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
669 croak ("row_number number of out range");
670
671 line_t &l = ROW(row_number);
672
673 if (GIMME_V != G_VOID)
674 {
675 wchar_t *wstr = new wchar_t [THIS->ncol];
676
677 for (int col = 0; col <THIS->ncol; col++)
678 wstr [col] = l.t [col];
679
680 char *str = rxvt_wcstoutf8 (wstr, THIS->ncol);
681 free (wstr);
682
683 SV *sv = newSVpv (str, 0);
684 SvUTF8_on (sv);
685 XPUSHs (sv_2mortal (sv));
686 free (str);
687 }
688
689 if (new_text)
690 {
691 wchar_t *wstr = sv2wcs (new_text);
692
693 int len = wcslen (wstr);
694
695 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
696 {
697 free (wstr);
698 croak ("new_text extends beyond horizontal margins");
699 }
700
701 for (int col = start_col; col < start_col + len; col++)
702 {
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]));
705 }
706
707 free (wstr);
708 }
709}
710
711void
712rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0)
713 PPCODE:
714{
715 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
716 croak ("row_number number of out range");
717
718 line_t &l = ROW(row_number);
719
720 if (GIMME_V != G_VOID)
721 {
722 AV *av = newAV ();
723
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]));
727
728 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
729 }
730
731 if (new_rend)
732 {
733 if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
734 croak ("new_rend must be arrayref");
735
736 AV *av = (AV *)SvRV (new_rend);
737 int len = av_len (av) + 1;
738
739 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
740 croak ("new_rend array extends beyond horizontal margins");
741
742 for (int col = start_col; col < start_col + len; col++)
743 {
744 rend_t r = SvIV (*av_fetch (av, col - start_col, 1)) & ~RS_fontMask;
745
746 l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
747 }
748 }
749}
750
751int
752rxvt_term::ROW_l (int row_number, int new_length = -2)
753 CODE:
754{
755 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
756 croak ("row_number number of out range");
757
758 line_t &l = ROW(row_number);
759 RETVAL = l.l;
760
761 if (new_length >= -1)
762 l.l = new_length;
763}
764 OUTPUT:
765 RETVAL
766
767SV *
768rxvt_term::special_encode (SV *str)
769 CODE:
770 abort ();//TODO
771
772SV *
773rxvt_term::special_decode (SV *str)
774 CODE:
775 abort ();//TODO
348 776
349void 777void
350rxvt_term::_resource (char *name, int index, SV *newval = 0) 778rxvt_term::_resource (char *name, int index, SV *newval = 0)
351 PPCODE: 779 PPCODE:
352{ 780{
439 THIS->selection.len = wcslen (THIS->selection.text); 867 THIS->selection.len = wcslen (THIS->selection.text);
440 } 868 }
441} 869}
442 870
443void 871void
444rxvt_term::scr_overlay_new (int x, int y, int w, int h) 872rxvt_term::tt_write (SV *octets)
873 INIT:
874 STRLEN len;
875 char *str = SvPVbyte (octets, len);
876 C_ARGS:
877 (unsigned char *)str, len
445 878
446void 879SV *
447rxvt_term::scr_overlay_off () 880rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
448
449void
450rxvt_term::scr_overlay_set_char (int x, int y, U32 text, U32 rend = OVERLAY_RSTYLE)
451 CODE: 881 CODE:
452 THIS->scr_overlay_set (x, y, text, rend);
453
454void
455rxvt_term::scr_overlay_set (int x, int y, SV *text)
456 CODE:
457{ 882{
458 wchar_t *wtext = sv2wcs (text); 883 overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
459 THIS->scr_overlay_set (x, y, wtext); 884 RETVAL = newSVptr ((void *)o, "urxvt::overlay");
460 free (wtext); 885 o->self = (HV *)SvRV (RETVAL);
886
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);
890}
891 OUTPUT:
892 RETVAL
893
894MODULE = urxvt PACKAGE = urxvt::overlay
895
896void
897overlay::set (int x, int y, SV *text, SV *rend = 0)
898
899void
900overlay::DESTROY ()
901 CODE:
902{
903 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->THIS->self), "_overlay", 8, 0);
904 if (ovs)
905 {
906 HV *hv = (HV *)SvRV (*ovs);
907 char key[33]; sprintf (key, "%32lx", (long)THIS);
908 hv_delete (hv, key, 32, G_DISCARD);
909 }
910
911 delete THIS;
461} 912}
462 913
463MODULE = urxvt PACKAGE = urxvt::timer 914MODULE = urxvt PACKAGE = urxvt::timer
464 915
465SV * 916SV *
466timer::new () 917timer::new ()
467 CODE: 918 CODE:
468 timer *w = new timer; 919 timer *w = new timer;
920 w->start (NOW);
469 RETVAL = newSVptr ((void *)w, "urxvt::timer"); 921 RETVAL = newSVptr ((void *)w, "urxvt::timer");
470 w->self = (HV *)SvRV (RETVAL); 922 w->self = (HV *)SvRV (RETVAL);
471 OUTPUT: 923 OUTPUT:
472 RETVAL 924 RETVAL
473 925
481 933
482NV 934NV
483timer::at () 935timer::at ()
484 CODE: 936 CODE:
485 RETVAL = THIS->at; 937 RETVAL = THIS->at;
938 OUTPUT:
939 RETVAL
940
941timer *
942timer::interval (NV interval)
943 CODE:
944 THIS->interval = interval;
945 RETVAL = THIS;
486 OUTPUT: 946 OUTPUT:
487 RETVAL 947 RETVAL
488 948
489timer * 949timer *
490timer::set (NV tstamp) 950timer::set (NV tstamp)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines