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.44 by root, Sun Jan 8 22:58:13 2006 UTC vs.
Revision 1.58 by root, Thu Jan 12 01:38:37 2006 UTC

29#include "../config.h" 29#include "../config.h"
30 30
31#include <cstddef> 31#include <cstddef>
32#include <cstdarg> 32#include <cstdarg>
33 33
34#include "iom.h"
34#include "rxvt.h" 35#include "rxvt.h"
35#include "iom.h" 36#include "keyboard.h"
36#include "rxvtutil.h" 37#include "rxvtutil.h"
37#include "rxvtperl.h" 38#include "rxvtperl.h"
38 39
39#include "perlxsi.c" 40#include "perlxsi.c"
40 41
392 393
393struct rxvt_perl_interp rxvt_perl; 394struct rxvt_perl_interp rxvt_perl;
394 395
395static PerlInterpreter *perl; 396static PerlInterpreter *perl;
396 397
397rxvt_perl_interp::rxvt_perl_interp ()
398{
399}
400
401rxvt_perl_interp::~rxvt_perl_interp () 398rxvt_perl_interp::~rxvt_perl_interp ()
402{ 399{
403 if (perl) 400 if (perl)
404 { 401 {
405 perl_destruct (perl); 402 perl_destruct (perl);
410void 407void
411rxvt_perl_interp::init () 408rxvt_perl_interp::init ()
412{ 409{
413 if (!perl) 410 if (!perl)
414 { 411 {
412 perl_environ = rxvt_environ;
413 swap (perl_environ, environ);
414
415 char *argv[] = { 415 char *argv[] = {
416 "", 416 "",
417 "-T", 417 "-T",
418 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1", 418 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
419 }; 419 };
428 428
429 perl_destruct (perl); 429 perl_destruct (perl);
430 perl_free (perl); 430 perl_free (perl);
431 perl = 0; 431 perl = 0;
432 } 432 }
433
434 swap (perl_environ, environ);
435 }
436}
437
438static void
439ungrab (rxvt_term *THIS)
440{
441 if (THIS->perl.grabtime)
442 {
443 XUngrabKeyboard (THIS->display->display, THIS->perl.grabtime);
444 XUngrabPointer (THIS->display->display, THIS->perl.grabtime);
445 THIS->perl.grabtime = 0;
433 } 446 }
434} 447}
435 448
436bool 449bool
437rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) 450rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
448 return false; // perl not initialized for this instance 461 return false; // perl not initialized for this instance
449 else if (htype == HOOK_DESTROY) 462 else if (htype == HOOK_DESTROY)
450 { 463 {
451 // handled later 464 // handled later
452 } 465 }
453 else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END) 466 else
454 { 467 {
455 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0)); 468 if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
456
457 if (HvKEYS (hv))
458 { 469 {
470 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
471
472 if (HvKEYS (hv))
473 {
459 hv_iterinit (hv); 474 hv_iterinit (hv);
460 475
461 while (HE *he = hv_iternext (hv)) 476 while (HE *he = hv_iternext (hv))
462 ((overlay *)SvIV (hv_iterval (hv, he)))->swap (); 477 ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
478 }
479
463 } 480 }
481
482 if (!should_invoke [htype])
483 return false;
464 } 484 }
465 else if (!should_invoke [htype])
466 return false;
467 485
486 swap (perl_environ, environ);
487
488 try
489 {
468 dSP; 490 dSP;
469 va_list ap; 491 va_list ap;
470 492
471 va_start (ap, htype); 493 va_start (ap, htype);
472 494
473 ENTER; 495 ENTER;
474 SAVETMPS; 496 SAVETMPS;
475 497
476 PUSHMARK (SP); 498 PUSHMARK (SP);
477 499
478 XPUSHs (sv_2mortal (newSVterm (term))); 500 XPUSHs (sv_2mortal (newSVterm (term)));
479 XPUSHs (sv_2mortal (newSViv (htype))); 501 XPUSHs (sv_2mortal (newSViv (htype)));
480 502
481 for (;;) { 503 for (;;) {
482 data_type dt = (data_type)va_arg (ap, int); 504 data_type dt = (data_type)va_arg (ap, int);
483 505
484 switch (dt) 506 switch (dt)
485 { 507 {
486 case DT_INT: 508 case DT_INT:
487 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int)))); 509 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
488 break; 510 break;
489 511
490 case DT_LONG: 512 case DT_LONG:
491 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long)))); 513 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
492 break; 514 break;
493 515
494 case DT_STR: 516 case DT_STR:
495 XPUSHs (taint (sv_2mortal (newSVpv (va_arg (ap, char *), 0)))); 517 XPUSHs (taint (sv_2mortal (newSVpv (va_arg (ap, char *), 0))));
496 break; 518 break;
497 519
498 case DT_STR_LEN: 520 case DT_STR_LEN:
499 { 521 {
500 char *str = va_arg (ap, char *); 522 char *str = va_arg (ap, char *);
501 int len = va_arg (ap, int); 523 int len = va_arg (ap, int);
502 524
503 XPUSHs (taint (sv_2mortal (newSVpvn (str, len)))); 525 XPUSHs (taint (sv_2mortal (newSVpvn (str, len))));
504 } 526 }
505 break; 527 break;
506 528
507 case DT_WCS_LEN: 529 case DT_WCS_LEN:
508 { 530 {
509 wchar_t *wstr = va_arg (ap, wchar_t *); 531 wchar_t *wstr = va_arg (ap, wchar_t *);
510 int wlen = va_arg (ap, int); 532 int wlen = va_arg (ap, int);
511 533
512 XPUSHs (taint (sv_2mortal (wcs2sv (wstr, wlen)))); 534 XPUSHs (taint (sv_2mortal (wcs2sv (wstr, wlen))));
513 } 535 }
514 break; 536 break;
515 537
516 case DT_XEVENT: 538 case DT_XEVENT:
517 { 539 {
518 XEvent *xe = va_arg (ap, XEvent *); 540 XEvent *xe = va_arg (ap, XEvent *);
519 HV *hv = newHV (); 541 HV *hv = newHV ();
520 542
521# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0) 543# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
522# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0) 544# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
523# undef set 545# undef set
524 546
525 setiv (type, xe->type); 547 setiv (type, xe->type);
526 setiv (send_event, xe->xany.send_event); 548 setiv (send_event, xe->xany.send_event);
527 setiv (serial, xe->xany.serial); 549 setiv (serial, xe->xany.serial);
528 550
529 switch (xe->type) 551 switch (xe->type)
552 {
553 case KeyPress:
554 case KeyRelease:
555 case ButtonPress:
556 case ButtonRelease:
557 case MotionNotify:
558 setiv (time, xe->xmotion.time);
559 setiv (x, xe->xmotion.x);
560 setiv (y, xe->xmotion.y);
561 setiv (row, xe->xmotion.y / term->fheight);
562 setiv (col, xe->xmotion.x / term->fwidth);
563 setiv (x_root, xe->xmotion.x_root);
564 setiv (y_root, xe->xmotion.y_root);
565 setiv (state, xe->xmotion.state);
566 break;
567 }
568
569 switch (xe->type)
570 {
571 case KeyPress:
572 case KeyRelease:
573 setiv (keycode, xe->xkey.keycode);
574 break;
575
576 case ButtonPress:
577 case ButtonRelease:
578 setiv (button, xe->xbutton.button);
579 break;
580
581 case MotionNotify:
582 setiv (is_hint, xe->xmotion.is_hint);
583 break;
584 }
585
586 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
587 }
588 break;
589
590 case DT_END:
530 { 591 {
531 case KeyPress: 592 va_end (ap);
532 case KeyRelease: 593
533 case ButtonPress: 594 PUTBACK;
534 case ButtonRelease: 595 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
535 case MotionNotify: 596 SPAGAIN;
536 setiv (time, xe->xmotion.time); 597
537 setiv (x, xe->xmotion.x); 598 if (count)
538 setiv (y, xe->xmotion.y);
539 setiv (row, xe->xmotion.y / term->fheight);
540 setiv (col, xe->xmotion.x / term->fwidth);
541 setiv (x_root, xe->xmotion.x_root);
542 setiv (y_root, xe->xmotion.y_root);
543 setiv (state, xe->xmotion.state);
544 break; 599 {
600 SV *status = POPs;
601 count = SvTRUE (status);
602 }
603
604 PUTBACK;
605 FREETMPS;
606 LEAVE;
607
608 if (SvTRUE (ERRSV))
609 {
610 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
611 ungrab (term); // better lose the grab than the session
612 }
613
614 if (htype == HOOK_DESTROY)
615 {
616 clearSVptr ((SV *)term->perl.self);
617 SvREFCNT_dec ((SV *)term->perl.self);
618 }
619
620 swap (perl_environ, environ);
621 return count;
545 } 622 }
546 623
547 switch (xe->type)
548 {
549 case KeyPress:
550 case KeyRelease:
551 setiv (keycode, xe->xkey.keycode);
552 break;
553
554 case ButtonPress:
555 case ButtonRelease:
556 setiv (button, xe->xbutton.button);
557 break;
558
559 case MotionNotify:
560 setiv (is_hint, xe->xmotion.is_hint);
561 break;
562 }
563
564 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
565 }
566 break;
567
568 case DT_END:
569 {
570 va_end (ap);
571
572 PUTBACK;
573 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
574 SPAGAIN;
575
576 if (count)
577 {
578 SV *status = POPs;
579 count = SvTRUE (status);
580 }
581
582 PUTBACK;
583 FREETMPS;
584 LEAVE;
585
586 if (SvTRUE (ERRSV))
587 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
588
589 if (htype == HOOK_DESTROY)
590 {
591 clearSVptr ((SV *)term->perl.self);
592 SvREFCNT_dec ((SV *)term->perl.self);
593 }
594
595 return count;
596 }
597
598 default: 624 default:
599 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt); 625 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
626 }
600 } 627 }
601 } 628 }
629 catch (...)
630 {
631 swap (perl_environ, environ);
632 throw;
633 }
602} 634}
603 635
604///////////////////////////////////////////////////////////////////////////// 636/////////////////////////////////////////////////////////////////////////////
605 637
606MODULE = urxvt PACKAGE = urxvt 638MODULE = urxvt PACKAGE = urxvt
607 639
608PROTOTYPES: ENABLE 640PROTOTYPES: ENABLE
609 641
610BOOT: 642BOOT:
611{ 643{
612 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1)); 644 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
645 sv_setsv (get_sv ("urxvt::RESNAME", 1), newSVpvn (RESNAME, sizeof (RESNAME) - 1));
646 sv_setsv (get_sv ("urxvt::RESCLASS", 1), newSVpvn (RESCLASS, sizeof (RESCLASS) - 1));
647 sv_setsv (get_sv ("urxvt::RXVTNAME", 1), newSVpvn (RXVTNAME, sizeof (RXVTNAME) - 1));
613 648
614 AV *hookname = get_av ("urxvt::HOOKNAME", 1); 649 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
615# define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0)); 650# define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
616# include "hookinc.h" 651# include "hookinc.h"
617# undef def 652# undef def
646 export_const_iv (Button2Mask); 681 export_const_iv (Button2Mask);
647 export_const_iv (Button3Mask); 682 export_const_iv (Button3Mask);
648 export_const_iv (Button4Mask); 683 export_const_iv (Button4Mask);
649 export_const_iv (Button5Mask); 684 export_const_iv (Button5Mask);
650 export_const_iv (AnyModifier); 685 export_const_iv (AnyModifier);
686
687 export_const_iv (EVENT_NONE);
688 export_const_iv (EVENT_READ);
689 export_const_iv (EVENT_WRITE);
651} 690}
691
692void
693set_should_invoke (int htype, int value)
694 CODE:
695 rxvt_perl.should_invoke [htype] = value;
696
697void
698warn (const char *msg)
699 CODE:
700 rxvt_warn ("%s", msg);
701
702void
703fatal (const char *msg)
704 CODE:
705 rxvt_fatal ("%s", msg);
652 706
653SV * 707SV *
654new (...) 708untaint (SV *sv)
655 CODE: 709 CODE:
656{ 710 RETVAL = newSVsv (sv);
657 stringvec *argv = new stringvec; 711 SvTAINTED_off (RETVAL);
658 bool success; 712 OUTPUT:
659
660 for (int i = 0; i < items ;i++)
661 argv->push_back (strdup (SvPVbyte_nolen (ST (i))));
662
663 rxvt_term *term = new rxvt_term;
664
665 term->argv = argv;
666
667 try
668 {
669 if (!term->init (argv->size (), argv->begin ()))
670 term = 0;
671 }
672 catch (const class rxvt_failure_exception &e)
673 {
674 term->destroy ();
675 croak ("exception caught while initializing new terminal instance");
676 }
677
678 RETVAL = term && term->perl.self ? newSVterm (term) : &PL_sv_undef;
679}
680 OUTPUT:
681 RETVAL 713 RETVAL
682 714
683void 715bool
684set_should_invoke (int htype, int value) 716safe ()
685 CODE: 717 CODE:
686 rxvt_perl.should_invoke [htype] = value; 718 RETVAL = !rxvt_tainted ();
687 719 OUTPUT:
688void 720 RETVAL
689warn (const char *msg)
690 CODE:
691 rxvt_warn ("%s", msg);
692
693void
694fatal (const char *msg)
695 CODE:
696 rxvt_fatal ("%s", msg);
697 721
698NV 722NV
699NOW () 723NOW ()
700 CODE: 724 CODE:
701 RETVAL = NOW; 725 RETVAL = NOW;
749} 773}
750 OUTPUT: 774 OUTPUT:
751 RETVAL 775 RETVAL
752 776
753MODULE = urxvt PACKAGE = urxvt::term 777MODULE = urxvt PACKAGE = urxvt::term
778
779SV *
780_new (...)
781 CODE:
782{
783 if (items < 1 || !SvROK (ST (0)) || SvTYPE (SvRV (ST (0))) != SVt_PVAV)
784 croak ("first argument to urxvt::term->_new must be arrayref");
785
786 rxvt_term *term = new rxvt_term;
787
788 term->argv = new stringvec;
789 term->envv = new stringvec;
790
791 for (int i = 1; i < items; i++)
792 term->argv->push_back (strdup (SvPVbyte_nolen (ST (i))));
793
794 AV *envv = (AV *)SvRV (ST (0));
795 for (int i = av_len (envv) + 1; i--; )
796 term->envv->push_back (strdup (SvPVbyte_nolen (*av_fetch (envv, i, 1))));
797
798 term->envv->push_back (0);
799
800 bool success;
801
802 try
803 {
804 success = term->init (term->argv->size (), term->argv->begin ());
805 }
806 catch (const class rxvt_failure_exception &e)
807 {
808 success = false;
809 }
810
811 if (!success)
812 {
813 term->destroy ();
814 croak ("error while initializing new terminal instance");
815 }
816
817 RETVAL = term && term->perl.self
818 ? newSVterm (term) : &PL_sv_undef;
819}
820 OUTPUT:
821 RETVAL
754 822
755void 823void
756rxvt_term::destroy () 824rxvt_term::destroy ()
757 825
758void 826void
800 XAllowEvents (THIS->display->display, ReplayKeyboard, THIS->perl.grabtime); 868 XAllowEvents (THIS->display->display, ReplayKeyboard, THIS->perl.grabtime);
801 869
802void 870void
803rxvt_term::ungrab () 871rxvt_term::ungrab ()
804 CODE: 872 CODE:
805{ 873 ungrab (THIS);
806 if (THIS->perl.grabtime)
807 {
808 XUngrabKeyboard (THIS->display->display, THIS->perl.grabtime);
809 XUngrabPointer (THIS->display->display, THIS->perl.grabtime);
810 THIS->perl.grabtime = 0;
811 }
812}
813 874
814int 875int
815rxvt_term::strwidth (SV *str) 876rxvt_term::strwidth (SV *str)
816 CODE: 877 CODE:
817{ 878{
910 case 2: RETVAL = THIS->ModNumLockMask; break; 971 case 2: RETVAL = THIS->ModNumLockMask; break;
911 } 972 }
912 OUTPUT: 973 OUTPUT:
913 RETVAL 974 RETVAL
914 975
976char *
977rxvt_term::display_id ()
978 ALIAS:
979 display_id = 0
980 locale = 1
981 CODE:
982 switch (ix)
983 {
984 case 0: RETVAL = THIS->display->id; break;
985 case 1: RETVAL = THIS->locale; break;
986 }
987 OUTPUT:
988 RETVAL
989
990SV *
991rxvt_term::_env ()
992 CODE:
993{
994 if (THIS->envv)
995 {
996 AV *av = newAV ();
997
998 for (char **i = THIS->envv->begin (); i != THIS->envv->end (); ++i)
999 if (*i)
1000 av_push (av, newSVpv (*i, 0));
1001
1002 RETVAL = newRV_noinc ((SV *)av);
1003 }
1004 else
1005 RETVAL = &PL_sv_undef;
1006}
1007 OUTPUT:
1008 RETVAL
1009
1010int
1011rxvt_term::pty_ev_events (int events = EVENT_UNDEF)
1012 CODE:
1013 RETVAL = THIS->pty_ev.events;
1014 if (events != EVENT_UNDEF)
1015 THIS->pty_ev.set (events);
1016 OUTPUT:
1017 RETVAL
1018
915U32 1019U32
916rxvt_term::parent () 1020rxvt_term::parent ()
917 CODE: 1021 CODE:
918 RETVAL = (U32)THIS->parent [0]; 1022 RETVAL = (U32)THIS->parent [0];
919 OUTPUT: 1023 OUTPUT:
1184 else 1288 else
1185 THIS->rs [index] = 0; 1289 THIS->rs [index] = 0;
1186 } 1290 }
1187} 1291}
1188 1292
1293const char *
1294rxvt_term::x_resource (const char *name)
1295 CLEANUP:
1296 SvTAINTED_on (ST (0));
1297
1189bool 1298bool
1190rxvt_term::option (U32 optval, int set = -1) 1299rxvt_term::option (U32 optval, int set = -1)
1191 CODE: 1300 CODE:
1192{ 1301{
1193 RETVAL = THIS->options & optval; 1302 RETVAL = THIS->options & optval;
1220 } 1329 }
1221} 1330}
1222 OUTPUT: 1331 OUTPUT:
1223 RETVAL 1332 RETVAL
1224 1333
1334bool
1335rxvt_term::parse_keysym (char *keysym, char *str)
1336 CODE:
1337 RETVAL = 0 < THIS->parse_keysym (keysym, str);
1338 THIS->keyboard->register_done ();
1339 OUTPUT:
1340 RETVAL
1341
1225void 1342void
1226rxvt_term::cur (...) 1343rxvt_term::screen_cur (...)
1227 PROTOTYPE: $;$$ 1344 PROTOTYPE: $;$$
1228 ALIAS: 1345 ALIAS:
1229 screen_cur = 0 1346 screen_cur = 0
1230 selection_beg = 1 1347 selection_beg = 1
1231 selection_end = 2 1348 selection_end = 2
1252 if (ix) 1369 if (ix)
1253 THIS->want_refresh = 1; 1370 THIS->want_refresh = 1;
1254 } 1371 }
1255} 1372}
1256 1373
1374char
1375rxvt_term::cur_charset ()
1376 CODE:
1377 RETVAL = THIS->charsets [THIS->screen.charset];
1378 OUTPUT:
1379 RETVAL
1380
1257int 1381int
1258rxvt_term::selection_grab (U32 eventtime) 1382rxvt_term::selection_grab (U32 eventtime)
1259 1383
1260void 1384void
1261rxvt_term::selection (SV *newtext = 0) 1385rxvt_term::selection (SV *newtext = 0)
1262 PPCODE: 1386 PPCODE:
1263{ 1387{
1264 if (GIMME_V != G_VOID) 1388 if (GIMME_V != G_VOID)
1389 XPUSHs (THIS->selection.text
1265 XPUSHs (taint (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)))); 1390 ? taint (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)))
1391 : &PL_sv_undef);
1266 1392
1267 if (newtext) 1393 if (newtext)
1268 { 1394 {
1269 free (THIS->selection.text); 1395 free (THIS->selection.text);
1270 1396
1271 THIS->selection.text = sv2wcs (newtext); 1397 THIS->selection.text = sv2wcs (newtext);
1272 THIS->selection.len = wcslen (THIS->selection.text); 1398 THIS->selection.len = wcslen (THIS->selection.text);
1273 } 1399 }
1274} 1400}
1401
1402void
1403rxvt_term::scr_xor_rect (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle1 = RS_RVid, U32 rstyle2 = RS_RVid | RS_Uline)
1404
1405void
1406rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle = RS_RVid)
1407
1408void
1409rxvt_term::scr_bell ()
1275 1410
1276void 1411void
1277rxvt_term::scr_add_lines (SV *string) 1412rxvt_term::scr_add_lines (SV *string)
1278 CODE: 1413 CODE:
1279{ 1414{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines