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.129 by sf-exg, Sun Jan 24 21:53:13 2010 UTC vs.
Revision 1.138 by root, Mon Feb 21 08:10:10 2011 UTC

1/*----------------------------------------------------------------------* 1/*----------------------------------------------------------------------*
2 * File: rxvtperl.xs 2 * File: rxvtperl.xs
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2008 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2005-2008,2011 Marc Lehmann <schmorp@schmorp.de>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
302 THIS->refresh_check (); 302 THIS->refresh_check ();
303} 303}
304 304
305///////////////////////////////////////////////////////////////////////////// 305/////////////////////////////////////////////////////////////////////////////
306 306
307#define IOM_CLASS "urxvt"
308#define IOM_WARN rxvt_warn
309#include "iom_perl.h" 307#include "iom_perl.h"
310 308
311///////////////////////////////////////////////////////////////////////////// 309/////////////////////////////////////////////////////////////////////////////
312 310
313struct rxvt_perl_interp rxvt_perl; 311struct rxvt_perl_interp rxvt_perl;
329{ 327{
330 if (!perl) 328 if (!perl)
331 { 329 {
332 rxvt_push_locale (""); // perl init destroys current locale 330 rxvt_push_locale (""); // perl init destroys current locale
333 331
332 {
334 perl_environ = rxvt_environ; 333 perl_environ = rxvt_environ;
335 swap (perl_environ, environ); 334 localise_env set_environ (perl_environ);
336 335
337 char *args[] = { 336 char *args[] = {
338 "", 337 "",
339 "-e" 338 "-e"
340 "BEGIN {" 339 "BEGIN {"
341 " urxvt->bootstrap;" 340 " urxvt->bootstrap;"
342 " unshift @INC, '" LIBDIR "';" 341 " unshift @INC, '" LIBDIR "';"
343 "}" 342 "}"
344 "" 343 ""
345 "use urxvt;" 344 "use urxvt;"
346 }; 345 };
347 int argc = sizeof (args) / sizeof (args[0]); 346 int argc = ARRAY_LENGTH(args);
348 char **argv = args; 347 char **argv = args;
349 348
350 PERL_SYS_INIT3 (&argc, &argv, &environ); 349 PERL_SYS_INIT3 (&argc, &argv, &environ);
351 perl = perl_alloc (); 350 perl = perl_alloc ();
352 perl_construct (perl); 351 perl_construct (perl);
353 352
354 if (perl_parse (perl, xs_init, argc, argv, (char **)NULL) 353 if (perl_parse (perl, xs_init, argc, argv, (char **)NULL)
355 || perl_run (perl)) 354 || perl_run (perl))
356 { 355 {
357 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n"); 356 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
358 357
359 perl_destruct (perl); 358 perl_destruct (perl);
360 perl_free (perl); 359 perl_free (perl);
361 perl = 0; 360 perl = 0;
362 } 361 }
363 362 }
364 swap (perl_environ, environ);
365 363
366 rxvt_pop_locale (); 364 rxvt_pop_locale ();
367 } 365 }
368 366
369 if (perl) 367 if (perl)
389rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) 387rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
390{ 388{
391 if (!perl || !term->perl.self) 389 if (!perl || !term->perl.self)
392 return false; 390 return false;
393 391
392 localise_env set_environ (perl_environ);
393
394 // pre-handling of some events 394 // pre-handling of some events
395 if (htype == HOOK_REFRESH_END) 395 if (htype == HOOK_REFRESH_END)
396 { 396 {
397 AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0)); 397 AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
398 398
399 for (int i = 0; i <= AvFILL (av); i++) 399 for (int i = 0; i <= AvFILL (av); i++)
400 ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap (); 400 ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap ();
401 } 401 }
402 402
403 swap (perl_environ, environ);
404
405 bool event_consumed; 403 bool event_consumed;
406 404
407 if (htype == HOOK_INIT || htype == HOOK_DESTROY // must be called always 405 if (htype == HOOK_INIT || htype == HOOK_DESTROY // must be called always
408 || term->perl.should_invoke [htype]) 406 || term->perl.should_invoke [htype])
409 try
410 { 407 {
411 dSP; 408 dSP;
412 va_list ap; 409 va_list ap;
413 410
414 va_start (ap, htype); 411 va_start (ap, htype);
415 412
416 ENTER; 413 ENTER;
417 SAVETMPS; 414 SAVETMPS;
418 415
419 PUSHMARK (SP); 416 PUSHMARK (SP);
420 417
418 EXTEND (SP, 2);
421 XPUSHs (sv_2mortal (newSVterm (term))); 419 PUSHs (sv_2mortal (newSVterm (term)));
422 XPUSHs (sv_2mortal (newSViv (htype))); 420 PUSHs (sv_2mortal (newSViv (htype)));
423 421
424 for (;;) { 422 for (;;) {
425 data_type dt = (data_type)va_arg (ap, int); 423 data_type dt = (data_type)va_arg (ap, int);
426 424
427 switch (dt) 425 switch (dt)
428 { 426 {
429 case DT_INT: 427 case DT_INT:
430 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int)))); 428 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
431 break; 429 break;
432 430
433 case DT_LONG: 431 case DT_LONG:
434 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long)))); 432 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
435 break; 433 break;
436 434
437 case DT_STR: 435 case DT_STR:
438 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); 436 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
439 break; 437 break;
440 438
441 case DT_STR_LEN: 439 case DT_STR_LEN:
442 { 440 {
443 char *str = va_arg (ap, char *); 441 char *str = va_arg (ap, char *);
444 int len = va_arg (ap, int); 442 int len = va_arg (ap, int);
445 443
446 XPUSHs (sv_2mortal (newSVpvn (str, len))); 444 XPUSHs (sv_2mortal (newSVpvn (str, len)));
447 } 445 }
448 break; 446 break;
449 447
450 case DT_WCS_LEN: 448 case DT_WCS_LEN:
451 { 449 {
452 wchar_t *wstr = va_arg (ap, wchar_t *); 450 wchar_t *wstr = va_arg (ap, wchar_t *);
453 int wlen = va_arg (ap, int); 451 int wlen = va_arg (ap, int);
454 452
455 XPUSHs (sv_2mortal (wcs2sv (wstr, wlen))); 453 XPUSHs (sv_2mortal (wcs2sv (wstr, wlen)));
456 } 454 }
457 break; 455 break;
458 456
459 case DT_LCS_LEN: 457 case DT_LCS_LEN:
460 { 458 {
461 long *lstr = va_arg (ap, long *); 459 long *lstr = va_arg (ap, long *);
462 int llen = va_arg (ap, int); 460 int llen = va_arg (ap, int);
463 461
464 XPUSHs (sv_2mortal (newSVpvn ((char *)lstr, llen * sizeof (long)))); 462 XPUSHs (sv_2mortal (newSVpvn ((char *)lstr, llen * sizeof (long))));
465 } 463 }
466 break; 464 break;
467 465
468 case DT_XEVENT: 466 case DT_XEVENT:
469 { 467 {
470 XEvent *xe = va_arg (ap, XEvent *); 468 XEvent *xe = va_arg (ap, XEvent *);
471 HV *hv = newHV (); 469 HV *hv = newHV ();
472 470
473# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0) 471# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
474# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0) 472# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
475# define setuv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSVuv (val), 0) 473# define setuv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSVuv (val), 0)
476# undef set 474# undef set
477 475
478 setiv (type, xe->type); 476 setiv (type, xe->type);
479 setiv (send_event, xe->xany.send_event); 477 setiv (send_event, xe->xany.send_event);
480 setiv (serial, xe->xany.serial); 478 setiv (serial, xe->xany.serial);
481 479
482 switch (xe->type) 480 switch (xe->type)
483 { 481 {
484 case KeyPress: 482 case KeyPress:
485 case KeyRelease: 483 case KeyRelease:
486 case ButtonPress: 484 case ButtonPress:
487 case ButtonRelease: 485 case ButtonRelease:
488 case MotionNotify: 486 case MotionNotify:
489 setuv (window, xe->xmotion.window); 487 setuv (window, xe->xmotion.window);
490 setuv (root, xe->xmotion.root); 488 setuv (root, xe->xmotion.root);
491 setuv (subwindow, xe->xmotion.subwindow); 489 setuv (subwindow, xe->xmotion.subwindow);
492 setuv (time, xe->xmotion.time); 490 setuv (time, xe->xmotion.time);
493 setiv (x, xe->xmotion.x); 491 setiv (x, xe->xmotion.x);
494 setiv (y, xe->xmotion.y); 492 setiv (y, xe->xmotion.y);
495 setiv (row, xe->xmotion.y / term->fheight + term->view_start); 493 setiv (row, xe->xmotion.y / term->fheight + term->view_start);
496 setiv (col, xe->xmotion.x / term->fwidth); 494 setiv (col, xe->xmotion.x / term->fwidth);
497 setiv (x_root, xe->xmotion.x_root); 495 setiv (x_root, xe->xmotion.x_root);
498 setiv (y_root, xe->xmotion.y_root); 496 setiv (y_root, xe->xmotion.y_root);
499 setuv (state, xe->xmotion.state); 497 setuv (state, xe->xmotion.state);
500 498
501 switch (xe->type) 499 switch (xe->type)
502 { 500 {
503 case KeyPress: 501 case KeyPress:
504 case KeyRelease: 502 case KeyRelease:
505 setuv (keycode, xe->xkey.keycode); 503 setuv (keycode, xe->xkey.keycode);
506 break; 504 break;
507 505
508 case ButtonPress: 506 case ButtonPress:
509 case ButtonRelease: 507 case ButtonRelease:
510 setuv (button, xe->xbutton.button); 508 setuv (button, xe->xbutton.button);
511 break; 509 break;
512 510
513 case MotionNotify: 511 case MotionNotify:
514 setiv (is_hint, xe->xmotion.is_hint); 512 setiv (is_hint, xe->xmotion.is_hint);
515 break; 513 break;
516 } 514 }
517 515
518 break; 516 break;
519 517
520 case MapNotify: 518 case MapNotify:
521 case UnmapNotify: 519 case UnmapNotify:
522 case ConfigureNotify: 520 case ConfigureNotify:
523 setuv (event, xe->xconfigure.event); 521 setuv (event, xe->xconfigure.event);
524 setuv (window, xe->xconfigure.window); 522 setuv (window, xe->xconfigure.window);
525 523
526 switch (xe->type) 524 switch (xe->type)
527 { 525 {
528 case ConfigureNotify: 526 case ConfigureNotify:
529 setiv (x, xe->xconfigure.x); 527 setiv (x, xe->xconfigure.x);
530 setiv (y, xe->xconfigure.y); 528 setiv (y, xe->xconfigure.y);
531 setiv (width, xe->xconfigure.width); 529 setiv (width, xe->xconfigure.width);
532 setiv (height, xe->xconfigure.height); 530 setiv (height, xe->xconfigure.height);
533 setuv (above, xe->xconfigure.above); 531 setuv (above, xe->xconfigure.above);
534 break; 532 break;
535 } 533 }
536 534
537 break; 535 break;
538 536
539 case PropertyNotify: 537 case PropertyNotify:
540 setuv (window, xe->xproperty.window); 538 setuv (window, xe->xproperty.window);
541 setuv (atom, xe->xproperty.atom); 539 setuv (atom, xe->xproperty.atom);
542 setuv (time, xe->xproperty.time); 540 setuv (time, xe->xproperty.time);
543 setiv (state, xe->xproperty.state); 541 setiv (state, xe->xproperty.state);
544 break; 542 break;
545 543
546 case ClientMessage: 544 case ClientMessage:
547 setuv (window, xe->xclient.window); 545 setuv (window, xe->xclient.window);
548 setuv (message_type, xe->xclient.message_type); 546 setuv (message_type, xe->xclient.message_type);
549 setuv (format, xe->xclient.format); 547 setuv (format, xe->xclient.format);
550 setuv (l0, xe->xclient.data.l[0]); 548 setuv (l0, xe->xclient.data.l[0]);
551 setuv (l1, xe->xclient.data.l[1]); 549 setuv (l1, xe->xclient.data.l[1]);
552 setuv (l2, xe->xclient.data.l[2]); 550 setuv (l2, xe->xclient.data.l[2]);
553 setuv (l3, xe->xclient.data.l[3]); 551 setuv (l3, xe->xclient.data.l[3]);
554 setuv (l4, xe->xclient.data.l[4]); 552 setuv (l4, xe->xclient.data.l[4]);
555 break; 553 break;
556 } 554 }
557 555
558 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 556 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
559 } 557 }
560 break; 558 break;
561 559
562 case DT_END: 560 case DT_END:
563 goto call; 561 goto call;
564 562
565 default: 563 default:
566 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt); 564 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
567 } 565 }
566 }
567
568 call:
569 va_end (ap);
570
571 PUTBACK;
572 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
573 SPAGAIN;
574
575 if (count)
576 {
577 SV *status = POPs;
578 count = SvTRUE (status);
568 } 579 }
569 580
570 call:
571 va_end (ap);
572
573 PUTBACK; 581 PUTBACK;
574 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
575 SPAGAIN;
576
577 if (count)
578 {
579 SV *status = POPs;
580 count = SvTRUE (status);
581 }
582
583 PUTBACK;
584 FREETMPS; 582 FREETMPS;
585 LEAVE; 583 LEAVE;
586 584
587 if (SvTRUE (ERRSV)) 585 if (SvTRUE (ERRSV))
588 { 586 {
589 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); 587 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPVbyte_nolen (ERRSV));
590 ungrab (term); // better lose the grab than the session 588 ungrab (term); // better lose the grab than the session
591 } 589 }
592 590
593 event_consumed = !!count; 591 event_consumed = !!count;
594 } 592 }
595 catch (...)
596 {
597 swap (perl_environ, environ);
598 throw;
599 }
600 else 593 else
601 event_consumed = false; 594 event_consumed = false;
602 595
603 // post-handling of some events 596 // post-handling of some events
604 if (htype == HOOK_REFRESH_BEGIN) 597 if (htype == HOOK_REFRESH_BEGIN)
615 608
616 // don't allow further calls 609 // don't allow further calls
617 term->perl.self = 0; 610 term->perl.self = 0;
618 } 611 }
619 612
620 swap (perl_environ, environ);
621
622 return event_consumed; 613 return event_consumed;
614}
615
616void
617rxvt_perl_interp::selection_finish (rxvt_selection *sel, char *data, unsigned int len)
618{
619 localise_env set_environ (perl_environ);
620
621 ENTER;
622 SAVETMPS;
623
624 dSP;
625 XPUSHs (sv_2mortal (newSVpvn (data, len)));
626 call_sv ((SV *)sel->cb_sv, G_VOID | G_DISCARD | G_EVAL);
627
628 if (SvTRUE (ERRSV))
629 rxvt_warn ("perl selection callback evaluation error: %s", SvPVbyte_nolen (ERRSV));
630
631 FREETMPS;
632 LEAVE;
623} 633}
624 634
625///////////////////////////////////////////////////////////////////////////// 635/////////////////////////////////////////////////////////////////////////////
626 636
627MODULE = urxvt PACKAGE = urxvt 637MODULE = urxvt PACKAGE = urxvt
639# define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0)); 649# define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
640# include "hookinc.h" 650# include "hookinc.h"
641# undef def 651# undef def
642 652
643 HV *option = get_hv ("urxvt::OPTION", 1); 653 HV *option = get_hv ("urxvt::OPTION", 1);
644# define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0); 654# define def(name) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0);
645# define nodef(name) 655# define nodef(name)
646# include "optinc.h" 656# include "optinc.h"
647# undef nodef 657# undef nodef
648# undef def 658# undef def
649 659
654 } *civ, const_iv[] = { 664 } *civ, const_iv[] = {
655# define const_iv(name) { # name, (IV)name } 665# define const_iv(name) { # name, (IV)name }
656 const_iv (NUM_RESOURCES), 666 const_iv (NUM_RESOURCES),
657 const_iv (DEFAULT_RSTYLE), 667 const_iv (DEFAULT_RSTYLE),
658 const_iv (OVERLAY_RSTYLE), 668 const_iv (OVERLAY_RSTYLE),
669 const_iv (Color_Bits),
670 const_iv (RS_bgShift), const_iv (RS_bgMask),
671 const_iv (RS_fgShift), const_iv (RS_fgMask),
672 const_iv (RS_Careful),
673 const_iv (RS_fontCount),
674 const_iv (RS_fontShift),
675 const_iv (RS_fontMask),
676 const_iv (RS_baseattrMask),
677 const_iv (RS_attrMask),
678 const_iv (RS_redraw),
679 const_iv (RS_Sel),
659 const_iv (RS_Bold), 680 const_iv (RS_Bold),
660 const_iv (RS_Italic), 681 const_iv (RS_Italic),
661 const_iv (RS_Blink), 682 const_iv (RS_Blink),
662 const_iv (RS_RVid), 683 const_iv (RS_RVid),
663 const_iv (RS_Uline), 684 const_iv (RS_Uline),
767 const_iv (XIMDontChange), 788 const_iv (XIMDontChange),
768# endif 789# endif
769# endif 790# endif
770 }; 791 };
771 792
772 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 793 for (civ = const_iv + ARRAY_LENGTH(const_iv); civ-- > const_iv; )
773 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 794 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
774} 795}
775 796
776void 797void
777warn (const char *msg) 798warn (const char *msg)
1445# include "rsinc.h" 1466# include "rsinc.h"
1446# undef def 1467# undef def
1447# undef reserve 1468# undef reserve
1448 }; 1469 };
1449 1470
1450 rs = rslist + sizeof (rslist) / sizeof (rslist [0]); 1471 rs = rslist + ARRAY_LENGTH(rslist);
1451 1472
1452 if (*name) 1473 if (*name)
1453 { 1474 {
1454 do { 1475 do {
1455 if (rs-- == rslist) 1476 if (rs-- == rslist)
1597 1618
1598void 1619void
1599rxvt_term::selection_clear (bool clipboard = false) 1620rxvt_term::selection_clear (bool clipboard = false)
1600 1621
1601void 1622void
1602rxvt_term::clipboard_copy (Time eventtime)
1603
1604void
1605rxvt_term::selection_make (Time eventtime, bool rect = false) 1623rxvt_term::selection_make (Time eventtime, bool rect = false)
1606 CODE: 1624 CODE:
1607 THIS->selection.op = SELECTION_CONT; 1625 THIS->selection.op = SELECTION_CONT;
1608 THIS->selection.rect = rect; 1626 THIS->selection.rect = rect;
1609 THIS->selection_make (eventtime); 1627 THIS->selection_make (eventtime);
1610 1628
1611int 1629int
1612rxvt_term::selection_grab (Time eventtime, bool clipboard = false) 1630rxvt_term::selection_grab (Time eventtime, bool clipboard = false)
1613 1631
1614void 1632void
1615rxvt_term::selection (SV *newtext = 0) 1633rxvt_term::selection (SV *newtext = 0, bool clipboard = false)
1616 PPCODE: 1634 PPCODE:
1617{ 1635{
1636 wchar_t * &text = clipboard ? THIS->selection.clip_text : THIS->selection.text;
1637 unsigned int &len = clipboard ? THIS->selection.clip_len : THIS->selection.len;
1638
1618 if (GIMME_V != G_VOID) 1639 if (GIMME_V != G_VOID)
1619 XPUSHs (THIS->selection.text 1640 XPUSHs (text
1620 ? sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)) 1641 ? sv_2mortal (wcs2sv (text, len))
1621 : &PL_sv_undef); 1642 : &PL_sv_undef);
1622 1643
1623 if (newtext) 1644 if (newtext)
1624 { 1645 {
1625 free (THIS->selection.text); 1646 free (text);
1626 1647
1627 THIS->selection.text = sv2wcs (newtext); 1648 text = sv2wcs (newtext);
1628 THIS->selection.len = wcslen (THIS->selection.text); 1649 len = wcslen (text);
1629 } 1650 }
1630} 1651}
1631 1652
1632char 1653char
1633rxvt_term::cur_charset () 1654rxvt_term::cur_charset ()
1664 char *str = SvPVbyte (octets, len); 1685 char *str = SvPVbyte (octets, len);
1665 C_ARGS: 1686 C_ARGS:
1666 str, len 1687 str, len
1667 1688
1668void 1689void
1690rxvt_term::tt_paste (SV *octets)
1691 INIT:
1692 STRLEN len;
1693 char *str = SvPVbyte (octets, len);
1694 C_ARGS:
1695 str, len
1696
1697void
1669rxvt_term::cmd_parse (SV *octets) 1698rxvt_term::cmd_parse (SV *octets)
1670 CODE: 1699 CODE:
1671{ 1700{
1672 STRLEN len; 1701 STRLEN len;
1673 char *str = SvPVbyte (octets, len); 1702 char *str = SvPVbyte (octets, len);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines