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-2005 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com> |
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. |
… | |
… | |
51 | char *str = SvPVutf8 (sv, len); |
51 | char *str = SvPVutf8 (sv, len); |
52 | return rxvt_utf8towcs (str, len); |
52 | return rxvt_utf8towcs (str, len); |
53 | } |
53 | } |
54 | |
54 | |
55 | static SV * |
55 | static SV * |
|
|
56 | wcs2sv (wchar_t *wstr, int len = -1) |
|
|
57 | { |
|
|
58 | char *str = rxvt_wcstoutf8 (wstr, len); |
|
|
59 | |
|
|
60 | SV *sv = newSVpv (str, 0); |
|
|
61 | SvUTF8_on (sv); |
|
|
62 | free (str); |
|
|
63 | |
|
|
64 | return sv; |
|
|
65 | } |
|
|
66 | |
|
|
67 | static SV * |
56 | new_ref (HV *hv, const char *klass) |
68 | new_ref (HV *hv, const char *klass) |
57 | { |
69 | { |
58 | return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1)); |
70 | return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1)); |
59 | } |
71 | } |
60 | |
72 | |
… | |
… | |
457 | |
469 | |
458 | case DT_STRING: |
470 | case DT_STRING: |
459 | XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); |
471 | XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); |
460 | break; |
472 | break; |
461 | |
473 | |
|
|
474 | case DT_STRING_LEN: |
|
|
475 | { |
|
|
476 | char *str = va_arg (ap, char *); |
|
|
477 | int len = va_arg (ap, int); |
|
|
478 | |
|
|
479 | XPUSHs (sv_2mortal (newSVpvn (str, len))); |
|
|
480 | } |
|
|
481 | break; |
|
|
482 | |
|
|
483 | case DT_XEVENT: |
|
|
484 | { |
|
|
485 | XEvent *xe = va_arg (ap, XEvent *); |
|
|
486 | HV *hv = newHV (); |
|
|
487 | |
|
|
488 | # define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0) |
|
|
489 | # define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0) |
|
|
490 | # undef set |
|
|
491 | |
|
|
492 | setiv (type, xe->type); |
|
|
493 | setiv (send_event, xe->xany.send_event); |
|
|
494 | setiv (serial, xe->xany.serial); |
|
|
495 | |
|
|
496 | switch (xe->type) |
|
|
497 | { |
|
|
498 | case KeyPress: |
|
|
499 | case KeyRelease: |
|
|
500 | case ButtonPress: |
|
|
501 | case ButtonRelease: |
|
|
502 | case MotionNotify: |
|
|
503 | setiv (time, xe->xmotion.time); |
|
|
504 | setiv (x, xe->xmotion.x); |
|
|
505 | setiv (y, xe->xmotion.y); |
|
|
506 | setiv (row, xe->xmotion.y / term->fheight); |
|
|
507 | setiv (col, xe->xmotion.x / term->fwidth); |
|
|
508 | setiv (x_root, xe->xmotion.x_root); |
|
|
509 | setiv (y_root, xe->xmotion.y_root); |
|
|
510 | setiv (state, xe->xmotion.state); |
|
|
511 | break; |
|
|
512 | } |
|
|
513 | |
|
|
514 | switch (xe->type) |
|
|
515 | { |
|
|
516 | case KeyPress: |
|
|
517 | case KeyRelease: |
|
|
518 | setiv (keycode, xe->xkey.keycode); |
|
|
519 | break; |
|
|
520 | |
|
|
521 | case ButtonPress: |
|
|
522 | case ButtonRelease: |
|
|
523 | setiv (button, xe->xbutton.button); |
|
|
524 | break; |
|
|
525 | |
|
|
526 | case MotionNotify: |
|
|
527 | setiv (is_hint, xe->xmotion.is_hint); |
|
|
528 | break; |
|
|
529 | } |
|
|
530 | |
|
|
531 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
|
|
532 | } |
|
|
533 | break; |
|
|
534 | |
|
|
535 | case DT_USTRING_LEN: |
|
|
536 | { |
|
|
537 | unicode_t *ustr = va_arg (ap, unicode_t *); |
|
|
538 | int ulen = va_arg (ap, int); |
|
|
539 | wchar_t *wstr = new wchar_t [ulen]; |
|
|
540 | |
|
|
541 | for (int i = ulen; i--; ) |
|
|
542 | wstr [i] = ustr [i]; |
|
|
543 | |
|
|
544 | XPUSHs (sv_2mortal (wcs2sv (wstr, ulen))); |
|
|
545 | |
|
|
546 | delete [] wstr; |
|
|
547 | } |
|
|
548 | |
462 | case DT_END: |
549 | case DT_END: |
463 | { |
550 | { |
464 | va_end (ap); |
551 | va_end (ap); |
465 | |
552 | |
466 | PUTBACK; |
553 | PUTBACK; |
… | |
… | |
518 | export_const (RS_Uline); |
605 | export_const (RS_Uline); |
519 | |
606 | |
520 | sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR); |
607 | sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR); |
521 | } |
608 | } |
522 | |
609 | |
|
|
610 | SV * |
|
|
611 | new (...) |
|
|
612 | CODE: |
|
|
613 | { |
|
|
614 | stringvec *argv = new stringvec; |
|
|
615 | bool success; |
|
|
616 | |
|
|
617 | for (int i = 0; i < items ;i++) |
|
|
618 | argv->push_back (strdup (SvPVbyte_nolen (ST (i)))); |
|
|
619 | |
|
|
620 | rxvt_term *term = new rxvt_term; |
|
|
621 | |
|
|
622 | term->argv = argv; |
|
|
623 | |
|
|
624 | try |
|
|
625 | { |
|
|
626 | if (!term->init (argv->size (), argv->begin ())) |
|
|
627 | term = 0; |
|
|
628 | } |
|
|
629 | catch (const class rxvt_failure_exception &e) |
|
|
630 | { |
|
|
631 | term->destroy (); |
|
|
632 | croak ("exception caught while initializing new terminal instance"); |
|
|
633 | } |
|
|
634 | |
|
|
635 | RETVAL = term && term->self ? newSVterm (term) : &PL_sv_undef; |
|
|
636 | } |
|
|
637 | OUTPUT: |
|
|
638 | RETVAL |
|
|
639 | |
523 | void |
640 | void |
524 | set_should_invoke (int htype, int value) |
641 | set_should_invoke (int htype, int value) |
525 | CODE: |
642 | CODE: |
526 | rxvt_perl.should_invoke [htype] = value; |
643 | rxvt_perl.should_invoke [htype] = value; |
527 | |
644 | |
… | |
… | |
589 | } |
706 | } |
590 | OUTPUT: |
707 | OUTPUT: |
591 | RETVAL |
708 | RETVAL |
592 | |
709 | |
593 | MODULE = urxvt PACKAGE = urxvt::term |
710 | MODULE = urxvt PACKAGE = urxvt::term |
|
|
711 | |
|
|
712 | void |
|
|
713 | rxvt_term::destroy () |
594 | |
714 | |
595 | int |
715 | int |
596 | rxvt_term::strwidth (SV *str) |
716 | rxvt_term::strwidth (SV *str) |
597 | CODE: |
717 | CODE: |
598 | { |
718 | { |
… | |
… | |
634 | |
754 | |
635 | rxvt_push_locale (THIS->locale); |
755 | rxvt_push_locale (THIS->locale); |
636 | wchar_t *wstr = rxvt_mbstowcs (data, len); |
756 | wchar_t *wstr = rxvt_mbstowcs (data, len); |
637 | rxvt_pop_locale (); |
757 | rxvt_pop_locale (); |
638 | |
758 | |
639 | char *str = rxvt_wcstoutf8 (wstr); |
759 | RETVAL = wcs2sv (wstr); |
640 | free (wstr); |
760 | free (wstr); |
641 | |
|
|
642 | RETVAL = newSVpv (str, 0); |
|
|
643 | SvUTF8_on (RETVAL); |
|
|
644 | free (str); |
|
|
645 | } |
761 | } |
646 | OUTPUT: |
762 | OUTPUT: |
647 | RETVAL |
763 | RETVAL |
648 | |
764 | |
649 | # very portable, especially on objects as opposed to pods |
765 | # very portable, especially on objects as opposed to pods |
… | |
… | |
681 | RETVAL = *(int *)((char *)THIS + ix); |
797 | RETVAL = *(int *)((char *)THIS + ix); |
682 | OUTPUT: |
798 | OUTPUT: |
683 | RETVAL |
799 | RETVAL |
684 | |
800 | |
685 | U32 |
801 | U32 |
|
|
802 | rxvt_term::parent () |
|
|
803 | CODE: |
|
|
804 | RETVAL = (U32)THIS->parent [0]; |
|
|
805 | OUTPUT: |
|
|
806 | RETVAL |
|
|
807 | |
|
|
808 | U32 |
|
|
809 | rxvt_term::vt () |
|
|
810 | CODE: |
|
|
811 | RETVAL = (U32)THIS->vt; |
|
|
812 | OUTPUT: |
|
|
813 | RETVAL |
|
|
814 | |
|
|
815 | U32 |
686 | rxvt_term::screen_rstyle (U32 new_rstyle = THIS->screen.s_rstyle) |
816 | rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle) |
687 | CODE: |
817 | CODE: |
688 | { |
818 | { |
689 | RETVAL = THIS->screen.s_rstyle; |
819 | RETVAL = THIS->rstyle; |
690 | THIS->screen.s_rstyle = new_rstyle; |
820 | THIS->rstyle = new_rstyle; |
691 | } |
821 | } |
692 | OUTPUT: |
822 | OUTPUT: |
693 | RETVAL |
823 | RETVAL |
694 | |
824 | |
695 | int |
825 | int |
… | |
… | |
711 | rxvt_term::want_refresh () |
841 | rxvt_term::want_refresh () |
712 | CODE: |
842 | CODE: |
713 | THIS->want_refresh = 1; |
843 | THIS->want_refresh = 1; |
714 | |
844 | |
715 | void |
845 | void |
716 | rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0) |
846 | rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS) |
717 | PPCODE: |
847 | PPCODE: |
718 | { |
848 | { |
719 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
849 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
720 | XSRETURN_EMPTY; |
850 | XSRETURN_EMPTY; |
721 | |
851 | |
… | |
… | |
726 | wchar_t *wstr = new wchar_t [THIS->ncol]; |
856 | wchar_t *wstr = new wchar_t [THIS->ncol]; |
727 | |
857 | |
728 | for (int col = 0; col <THIS->ncol; col++) |
858 | for (int col = 0; col <THIS->ncol; col++) |
729 | wstr [col] = l.t [col]; |
859 | wstr [col] = l.t [col]; |
730 | |
860 | |
731 | char *str = rxvt_wcstoutf8 (wstr, THIS->ncol); |
|
|
732 | free (wstr); |
|
|
733 | |
|
|
734 | SV *sv = newSVpv (str, 0); |
|
|
735 | SvUTF8_on (sv); |
|
|
736 | XPUSHs (sv_2mortal (sv)); |
861 | XPUSHs (sv_2mortal (wcs2sv (wstr))); |
737 | free (str); |
862 | |
|
|
863 | delete [] wstr; |
738 | } |
864 | } |
739 | |
865 | |
740 | if (new_text) |
866 | if (new_text) |
741 | { |
867 | { |
742 | wchar_t *wstr = sv2wcs (new_text); |
868 | wchar_t *wstr = sv2wcs (new_text); |
743 | |
869 | |
744 | int len = wcslen (wstr); |
870 | int len = min (wcslen (wstr) - start_ofs, max_len); |
745 | |
871 | |
746 | if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) |
872 | if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) |
747 | { |
873 | { |
748 | free (wstr); |
874 | free (wstr); |
749 | croak ("new_text extends beyond horizontal margins"); |
875 | croak ("new_text extends beyond horizontal margins"); |
750 | } |
876 | } |
751 | |
877 | |
752 | for (int col = start_col; col < start_col + len; col++) |
878 | for (int col = start_col; col < start_col + len; col++) |
753 | { |
879 | { |
754 | l.t [col] = wstr [col - start_col]; |
880 | l.t [col] = wstr [start_ofs + col - start_col]; |
755 | l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col])); |
881 | l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col])); |
756 | } |
882 | } |
757 | |
883 | |
758 | free (wstr); |
884 | free (wstr); |
759 | } |
885 | } |
760 | } |
886 | } |
761 | |
887 | |
762 | void |
888 | void |
763 | rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0) |
889 | rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS) |
764 | PPCODE: |
890 | PPCODE: |
765 | { |
891 | { |
766 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
892 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
767 | XSRETURN_EMPTY; |
893 | XSRETURN_EMPTY; |
768 | |
894 | |
… | |
… | |
783 | { |
909 | { |
784 | if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV) |
910 | if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV) |
785 | croak ("new_rend must be arrayref"); |
911 | croak ("new_rend must be arrayref"); |
786 | |
912 | |
787 | AV *av = (AV *)SvRV (new_rend); |
913 | AV *av = (AV *)SvRV (new_rend); |
788 | int len = av_len (av) + 1; |
914 | int len = min (av_len (av) + 1 - start_ofs, max_len); |
789 | |
915 | |
790 | if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) |
916 | if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len)) |
791 | croak ("new_rend array extends beyond horizontal margins"); |
917 | croak ("new_rend array extends beyond horizontal margins"); |
792 | |
918 | |
793 | for (int col = start_col; col < start_col + len; col++) |
919 | for (int col = start_col; col < start_col + len; col++) |
794 | { |
920 | { |
795 | rend_t r = SvIV (*av_fetch (av, col - start_col, 1)) & ~RS_fontMask; |
921 | rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask; |
796 | |
922 | |
797 | l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col])); |
923 | l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col])); |
798 | } |
924 | } |
799 | } |
925 | } |
800 | } |
926 | } |
801 | |
927 | |
802 | int |
928 | int |
803 | rxvt_term::ROW_l (int row_number, int new_length = -2) |
929 | rxvt_term::ROW_l (int row_number, int new_length = -1) |
804 | CODE: |
930 | CODE: |
805 | { |
931 | { |
806 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
932 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
807 | XSRETURN_EMPTY; |
933 | XSRETURN_EMPTY; |
808 | |
934 | |
809 | line_t &l = ROW(row_number); |
935 | line_t &l = ROW(row_number); |
810 | RETVAL = l.l < 0 ? THIS->ncol : l.l; |
936 | RETVAL = l.l; |
811 | |
937 | |
812 | if (new_length >= -1) |
938 | if (new_length >= 0) |
813 | l.l = new_length; |
939 | l.l = new_length; |
814 | } |
940 | } |
815 | OUTPUT: |
941 | OUTPUT: |
816 | RETVAL |
942 | RETVAL |
817 | |
943 | |
818 | bool |
944 | bool |
819 | rxvt_term::ROW_is_longer (int row_number) |
945 | rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1) |
820 | CODE: |
946 | CODE: |
821 | { |
947 | { |
822 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
948 | if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow)) |
823 | XSRETURN_EMPTY; |
949 | XSRETURN_EMPTY; |
824 | |
950 | |
825 | line_t &l = ROW(row_number); |
951 | line_t &l = ROW(row_number); |
826 | RETVAL = l.l < 0; |
952 | RETVAL = l.is_longer (); |
|
|
953 | |
|
|
954 | if (new_is_longer >= 0) |
|
|
955 | l.is_longer (new_is_longer); |
827 | } |
956 | } |
828 | OUTPUT: |
957 | OUTPUT: |
829 | RETVAL |
958 | RETVAL |
830 | |
959 | |
831 | SV * |
960 | SV * |
… | |
… | |
916 | void |
1045 | void |
917 | rxvt_term::selection (SV *newtext = 0) |
1046 | rxvt_term::selection (SV *newtext = 0) |
918 | PPCODE: |
1047 | PPCODE: |
919 | { |
1048 | { |
920 | if (GIMME_V != G_VOID) |
1049 | if (GIMME_V != G_VOID) |
921 | { |
1050 | XPUSHs (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))); |
922 | char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len); |
|
|
923 | SV *sv = newSVpv (sel, 0); |
|
|
924 | SvUTF8_on (sv); |
|
|
925 | free (sel); |
|
|
926 | XPUSHs (sv_2mortal (sv)); |
|
|
927 | } |
|
|
928 | |
1051 | |
929 | if (newtext) |
1052 | if (newtext) |
930 | { |
1053 | { |
931 | free (THIS->selection.text); |
1054 | free (THIS->selection.text); |
932 | |
1055 | |
933 | THIS->selection.text = sv2wcs (newtext); |
1056 | THIS->selection.text = sv2wcs (newtext); |
934 | THIS->selection.len = wcslen (THIS->selection.text); |
1057 | THIS->selection.len = wcslen (THIS->selection.text); |
935 | } |
1058 | } |
|
|
1059 | } |
|
|
1060 | |
|
|
1061 | void |
|
|
1062 | rxvt_term::scr_add_lines (SV *string) |
|
|
1063 | CODE: |
|
|
1064 | { |
|
|
1065 | wchar_t *wstr = sv2wcs (string); |
|
|
1066 | int wlen = wcslen (wstr); |
|
|
1067 | unicode_t *ustr = new unicode_t [wlen]; |
|
|
1068 | int nlines = 0; |
|
|
1069 | |
|
|
1070 | for (int i = wlen; i--; ) |
|
|
1071 | { |
|
|
1072 | ustr [i] = wstr [i]; |
|
|
1073 | nlines += ustr [i] == '\012'; |
|
|
1074 | } |
|
|
1075 | |
|
|
1076 | THIS->scr_add_lines (ustr, nlines, wlen); |
|
|
1077 | |
|
|
1078 | free (wstr); |
|
|
1079 | delete [] ustr; |
936 | } |
1080 | } |
937 | |
1081 | |
938 | void |
1082 | void |
939 | rxvt_term::tt_write (SV *octets) |
1083 | rxvt_term::tt_write (SV *octets) |
940 | INIT: |
1084 | INIT: |
941 | STRLEN len; |
1085 | STRLEN len; |
942 | char *str = SvPVbyte (octets, len); |
1086 | char *str = SvPVbyte (octets, len); |
943 | C_ARGS: |
1087 | C_ARGS: |
944 | str, len |
1088 | str, len |
945 | |
1089 | |
|
|
1090 | void |
|
|
1091 | rxvt_term::cmd_parse (SV *octets) |
|
|
1092 | CODE: |
|
|
1093 | { |
|
|
1094 | STRLEN len; |
|
|
1095 | char *str = SvPVbyte (octets, len); |
|
|
1096 | |
|
|
1097 | char *old_cmdbuf_ptr = THIS->cmdbuf_ptr; |
|
|
1098 | char *old_cmdbuf_endp = THIS->cmdbuf_endp; |
|
|
1099 | |
|
|
1100 | THIS->cmdbuf_ptr = str; |
|
|
1101 | THIS->cmdbuf_endp = str + len; |
|
|
1102 | |
|
|
1103 | THIS->cmd_parse (); |
|
|
1104 | |
|
|
1105 | THIS->cmdbuf_ptr = old_cmdbuf_ptr; |
|
|
1106 | THIS->cmdbuf_endp = old_cmdbuf_endp; |
|
|
1107 | } |
|
|
1108 | |
946 | SV * |
1109 | SV * |
947 | rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2) |
1110 | rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2) |
948 | CODE: |
1111 | CODE: |
949 | { |
1112 | { |
950 | overlay *o = new overlay (THIS, x, y, w, h, rstyle, border); |
1113 | overlay *o = new overlay (THIS, x, y, w, h, rstyle, border); |