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 | |
313 | struct rxvt_perl_interp rxvt_perl; |
311 | struct 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) |
… | |
… | |
389 | rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) |
387 | rxvt_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 | |
|
|
616 | void |
|
|
617 | rxvt_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 | |
627 | MODULE = urxvt PACKAGE = urxvt |
637 | MODULE = 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 | |
… | |
… | |
778 | const_iv (XIMDontChange), |
788 | const_iv (XIMDontChange), |
779 | # endif |
789 | # endif |
780 | # endif |
790 | # endif |
781 | }; |
791 | }; |
782 | |
792 | |
783 | 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; ) |
784 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
794 | newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); |
785 | } |
795 | } |
786 | |
796 | |
787 | void |
797 | void |
788 | warn (const char *msg) |
798 | warn (const char *msg) |
… | |
… | |
1456 | # include "rsinc.h" |
1466 | # include "rsinc.h" |
1457 | # undef def |
1467 | # undef def |
1458 | # undef reserve |
1468 | # undef reserve |
1459 | }; |
1469 | }; |
1460 | |
1470 | |
1461 | rs = rslist + sizeof (rslist) / sizeof (rslist [0]); |
1471 | rs = rslist + ARRAY_LENGTH(rslist); |
1462 | |
1472 | |
1463 | if (*name) |
1473 | if (*name) |
1464 | { |
1474 | { |
1465 | do { |
1475 | do { |
1466 | if (rs-- == rslist) |
1476 | if (rs-- == rslist) |