1 /*----------------------------------------------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2008 Marc Lehmann <pcg@goof.com>
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
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
23 #define line_t perl_line_t
29 #include "../config.h"
44 #ifdef HAVE_SCROLLBARS
45 # define GRAB_CURSOR THIS->scrollBar.leftptr_cursor
47 # define GRAB_CURSOR None
51 #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
53 #define ROW(n) THIS->row_buf [LINENO (n)]
55 /////////////////////////////////////////////////////////////////////////////
61 char *str = SvPVutf8 (sv, len);
62 return rxvt_utf8towcs (str, len);
66 wcs2sv (wchar_t *wstr, int len = -1)
68 char *str = rxvt_wcstoutf8 (wstr, len);
70 SV *sv = newSVpv (str, 0);
78 newSVptr (void *ptr, const char *klass)
81 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
82 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
92 sv_unmagic (sv, PERL_MAGIC_ext);
96 SvPTR (SV *sv, const char *klass)
98 if (!sv_derived_from (sv, klass))
99 croak ("object of type %s expected", klass);
101 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
104 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
106 return (long)mg->mg_ptr;
109 #define newSVterm(term) SvREFCNT_inc ((SV *)(term)->perl.self)
110 #define SvTERM(sv) (rxvt_term *)SvPTR ((sv), "urxvt::term")
112 /////////////////////////////////////////////////////////////////////////////
114 #define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
127 overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
135 void set (int x, int y, SV *str, SV *rend);
138 overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
139 : THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2), overlay_av (0)
150 text = new text_t *[h];
151 rend = new rend_t *[h];
153 for (int y = 0; y < h; y++)
155 text_t *tp = text[y] = new text_t[w];
156 rend_t *rp = rend[y] = new rend_t[w];
164 t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
166 t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
168 t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
173 for (int x = w - 2; x-- > 0; )
183 for (int x = w; x-- > 0; )
197 for (int y = h; y--; )
213 overlay_av = (AV *)SvREFCNT_inc (SvRV (
214 *hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0)
216 av_push (overlay_av, newSViv ((long)this));
218 THIS->want_refresh = 1;
219 THIS->refresh_check ();
230 for (i = AvFILL (overlay_av); i >= 0; i--)
231 if (SvIV (*av_fetch (overlay_av, i, 1)) == (long)this)
234 for (; i < AvFILL (overlay_av); i++)
235 av_store (overlay_av, i, SvREFCNT_inc (*av_fetch (overlay_av, i + 1, 0)));
239 SvREFCNT_dec (overlay_av);
242 THIS->want_refresh = 1;
243 THIS->refresh_check ();
246 void overlay::swap ()
248 int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
249 int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
251 int ov_w = min (w, THIS->ncol - ov_x);
252 int ov_h = min (h, THIS->nrow - ov_y);
254 for (int y = ov_h; y--; )
256 text_t *t1 = text [y];
257 rend_t *r1 = rend [y];
259 text_t *t2 = ROW(y + ov_y + THIS->view_start).t + ov_x;
260 rend_t *r2 = ROW(y + ov_y + THIS->view_start).r + ov_x;
262 for (int x = ov_w; x--; )
264 text_t t = *t1; *t1++ = *t2; *t2++ = t;
265 rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
271 void overlay::set (int x, int y, SV *text, SV *rend)
276 if (!IN_RANGE_EXC (y, 0, h - border))
279 wchar_t *wtext = sv2wcs (text);
281 for (int col = min (wcslen (wtext), w - x - border); col--; )
282 this->text [y][x + col] = wtext [col];
288 if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
289 croak ("rend must be arrayref");
291 AV *av = (AV *)SvRV (rend);
293 for (int col = min (AvFILL (av) + 1, w - x - border); col--; )
294 this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
297 THIS->want_refresh = 1;
298 THIS->refresh_check ();
301 /////////////////////////////////////////////////////////////////////////////
303 #define IOM_CLASS "urxvt"
304 #define IOM_WARN rxvt_warn
305 #include "iom_perl.h"
307 /////////////////////////////////////////////////////////////////////////////
309 struct rxvt_perl_interp rxvt_perl;
311 static PerlInterpreter *perl;
313 rxvt_perl_interp::~rxvt_perl_interp ()
317 perl_destruct (perl);
323 rxvt_perl_interp::init (rxvt_term *term)
327 rxvt_push_locale (""); // perl init destroys current locale
329 perl_environ = rxvt_environ;
330 swap (perl_environ, environ);
337 " unshift @INC, '" LIBDIR "';"
343 perl = perl_alloc ();
344 perl_construct (perl);
346 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
349 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
351 perl_destruct (perl);
356 swap (perl_environ, environ);
363 // runs outside of perls ENV
364 term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term");
365 hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newAV ()), 0);
370 ungrab (rxvt_term *THIS)
372 if (THIS->perl.grabtime)
374 XUngrabKeyboard (THIS->dpy, THIS->perl.grabtime);
375 XUngrabPointer (THIS->dpy, THIS->perl.grabtime);
376 THIS->perl.grabtime = 0;
381 rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
383 if (!perl || !term->perl.self)
386 // pre-handling of some events
387 if (htype == HOOK_REFRESH_END)
389 AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
391 for (int i = 0; i <= AvFILL (av); i++)
392 ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap ();
395 swap (perl_environ, environ);
399 if (htype == HOOK_INIT || htype == HOOK_DESTROY // must be called always
400 || term->perl.should_invoke [htype])
406 va_start (ap, htype);
413 XPUSHs (sv_2mortal (newSVterm (term)));
414 XPUSHs (sv_2mortal (newSViv (htype)));
417 data_type dt = (data_type)va_arg (ap, int);
422 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
426 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
430 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
435 char *str = va_arg (ap, char *);
436 int len = va_arg (ap, int);
438 XPUSHs (sv_2mortal (newSVpvn (str, len)));
444 wchar_t *wstr = va_arg (ap, wchar_t *);
445 int wlen = va_arg (ap, int);
447 XPUSHs (sv_2mortal (wcs2sv (wstr, wlen)));
453 long *lstr = va_arg (ap, long *);
454 int llen = va_arg (ap, int);
456 XPUSHs (sv_2mortal (newSVpvn ((char *)lstr, llen * sizeof (long))));
462 XEvent *xe = va_arg (ap, XEvent *);
465 # define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
466 # define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
467 # define setuv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSVuv (val), 0)
470 setiv (type, xe->type);
471 setiv (send_event, xe->xany.send_event);
472 setiv (serial, xe->xany.serial);
481 setuv (window, xe->xmotion.window);
482 setuv (root, xe->xmotion.root);
483 setuv (subwindow, xe->xmotion.subwindow);
484 setuv (time, xe->xmotion.time);
485 setiv (x, xe->xmotion.x);
486 setiv (y, xe->xmotion.y);
487 setiv (row, xe->xmotion.y / term->fheight + term->view_start);
488 setiv (col, xe->xmotion.x / term->fwidth);
489 setiv (x_root, xe->xmotion.x_root);
490 setiv (y_root, xe->xmotion.y_root);
491 setuv (state, xe->xmotion.state);
497 setuv (keycode, xe->xkey.keycode);
502 setuv (button, xe->xbutton.button);
506 setiv (is_hint, xe->xmotion.is_hint);
514 case ConfigureNotify:
515 setuv (event, xe->xconfigure.event);
516 setuv (window, xe->xconfigure.window);
520 case ConfigureNotify:
521 setiv (x, xe->xconfigure.x);
522 setiv (y, xe->xconfigure.y);
523 setiv (width, xe->xconfigure.width);
524 setiv (height, xe->xconfigure.height);
525 setuv (above, xe->xconfigure.above);
532 setuv (window, xe->xproperty.window);
533 setuv (atom, xe->xproperty.atom);
534 setuv (time, xe->xproperty.time);
535 setiv (state, xe->xproperty.state);
539 setuv (window, xe->xclient.window);
540 setuv (message_type, xe->xclient.message_type);
541 setuv (format, xe->xclient.format);
542 setuv (l0, xe->xclient.data.l[0]);
543 setuv (l1, xe->xclient.data.l[1]);
544 setuv (l2, xe->xclient.data.l[2]);
545 setuv (l3, xe->xclient.data.l[3]);
546 setuv (l4, xe->xclient.data.l[4]);
550 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
558 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
566 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
572 count = SvTRUE (status);
581 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
582 ungrab (term); // better lose the grab than the session
585 event_consumed = !!count;
589 swap (perl_environ, environ);
593 event_consumed = false;
595 // post-handling of some events
596 if (htype == HOOK_REFRESH_BEGIN)
598 AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
600 for (int i = AvFILL (av); i >= 0; i--)
601 ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap ();
603 else if (htype == HOOK_DESTROY)
605 clearSVptr ((SV *)term->perl.self);
606 SvREFCNT_dec ((SV *)term->perl.self);
608 // don't allow further calls
612 swap (perl_environ, environ);
614 return event_consumed;
617 /////////////////////////////////////////////////////////////////////////////
619 MODULE = urxvt PACKAGE = urxvt
625 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
626 sv_setsv (get_sv ("urxvt::RESNAME", 1), newSVpvn (RESNAME, sizeof (RESNAME) - 1));
627 sv_setsv (get_sv ("urxvt::RESCLASS", 1), newSVpvn (RESCLASS, sizeof (RESCLASS) - 1));
628 sv_setsv (get_sv ("urxvt::RXVTNAME", 1), newSVpvn (RXVTNAME, sizeof (RXVTNAME) - 1));
630 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
631 # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
632 # include "hookinc.h"
635 HV *option = get_hv ("urxvt::OPTION", 1);
636 # define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0);
642 HV *stash = gv_stashpv ("urxvt", 1);
643 static const struct {
646 } *civ, const_iv[] = {
647 # define const_iv(name) { # name, (IV)name }
648 const_iv (DEFAULT_RSTYLE),
649 const_iv (OVERLAY_RSTYLE),
651 const_iv (RS_Italic),
656 const_iv (CurrentTime),
657 const_iv (ShiftMask),
659 const_iv (ControlMask),
665 const_iv (Button1Mask),
666 const_iv (Button2Mask),
667 const_iv (Button3Mask),
668 const_iv (Button4Mask),
669 const_iv (Button5Mask),
670 const_iv (AnyModifier),
673 const_iv (GrabModeSync),
674 const_iv (GrabModeAsync),
676 const_iv (NoEventMask),
677 const_iv (KeyPressMask),
678 const_iv (KeyReleaseMask),
679 const_iv (ButtonPressMask),
680 const_iv (ButtonReleaseMask),
681 const_iv (EnterWindowMask),
682 const_iv (LeaveWindowMask),
683 const_iv (PointerMotionMask),
684 const_iv (PointerMotionHintMask),
685 const_iv (Button1MotionMask),
686 const_iv (Button2MotionMask),
687 const_iv (Button3MotionMask),
688 const_iv (Button4MotionMask),
689 const_iv (Button5MotionMask),
690 const_iv (ButtonMotionMask),
691 const_iv (KeymapStateMask),
692 const_iv (ExposureMask),
693 const_iv (VisibilityChangeMask),
694 const_iv (StructureNotifyMask),
695 const_iv (ResizeRedirectMask),
696 const_iv (SubstructureNotifyMask),
697 const_iv (SubstructureRedirectMask),
698 const_iv (FocusChangeMask),
699 const_iv (PropertyChangeMask),
700 const_iv (ColormapChangeMask),
701 const_iv (OwnerGrabButtonMask),
704 const_iv (KeyRelease),
705 const_iv (ButtonPress),
706 const_iv (ButtonRelease),
707 const_iv (MotionNotify),
708 const_iv (EnterNotify),
709 const_iv (LeaveNotify),
712 const_iv (KeymapNotify),
714 const_iv (GraphicsExpose),
716 const_iv (VisibilityNotify),
717 const_iv (CreateNotify),
718 const_iv (DestroyNotify),
719 const_iv (UnmapNotify),
720 const_iv (MapNotify),
721 const_iv (MapRequest),
722 const_iv (ReparentNotify),
723 const_iv (ConfigureNotify),
724 const_iv (ConfigureRequest),
725 const_iv (GravityNotify),
726 const_iv (ResizeRequest),
727 const_iv (CirculateNotify),
728 const_iv (CirculateRequest),
729 const_iv (PropertyNotify),
730 const_iv (SelectionClear),
731 const_iv (SelectionRequest),
732 const_iv (SelectionNotify),
733 const_iv (ColormapNotify),
734 const_iv (ClientMessage),
735 const_iv (MappingNotify),
736 # if ENABLE_XIM_ONTHESPOT
737 const_iv (XIMReverse),
738 const_iv (XIMUnderline),
739 const_iv (XIMHighlight),
740 const_iv (XIMPrimary),
741 const_iv (XIMSecondary),
742 const_iv (XIMTertiary),
743 const_iv (XIMVisibleToForward),
744 const_iv (XIMVisibleToBackword),
745 const_iv (XIMVisibleToCenter),
747 const_iv (XIMForwardChar),
748 const_iv (XIMBackwardChar),
749 const_iv (XIMForwardWord),
750 const_iv (XIMBackwardWord),
751 const_iv (XIMCaretUp),
752 const_iv (XIMCaretDown),
753 const_iv (XIMNextLine),
754 const_iv (XIMPreviousLine),
755 const_iv (XIMLineStart),
756 const_iv (XIMLineEnd),
757 const_iv (XIMAbsolutePosition),
758 const_iv (XIMDontChange),
763 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
764 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
768 warn (const char *msg)
770 rxvt_warn ("%s", msg);
773 fatal (const char *msg)
775 rxvt_fatal ("%s", msg);
788 GET_BASEFG (int rend)
790 RETVAL = GET_BASEFG (rend);
795 GET_BASEBG (int rend)
797 RETVAL = GET_BASEBG (rend);
802 SET_FGCOLOR (int rend, int new_color)
804 RETVAL = SET_FGCOLOR (rend, clamp (new_color, 0, TOTAL_COLORS - 1));
809 SET_BGCOLOR (int rend, int new_color)
811 RETVAL = SET_BGCOLOR (rend, clamp (new_color, 0, TOTAL_COLORS - 1));
816 GET_CUSTOM (int rend)
818 RETVAL = (rend & RS_customMask) >> RS_customShift;
823 SET_CUSTOM (int rend, int new_value)
826 if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
827 croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
829 RETVAL = (rend & ~RS_customMask)
830 | ((new_value << RS_customShift) & RS_customMask);
839 EXTEND (SP, rxvt_term::termlist.size ());
841 for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
843 PUSHs (sv_2mortal (newSVterm (*t)));
846 MODULE = urxvt PACKAGE = urxvt::term
849 _new (AV *env, AV *arg)
852 rxvt_term *term = new rxvt_term;
854 stringvec *argv = new stringvec;
855 stringvec *envv = new stringvec;
857 for (int i = 0; i <= AvFILL (arg); i++)
858 argv->push_back (strdup (SvPVbyte_nolen (*av_fetch (arg, i, 1))));
860 for (int i = AvFILL (env) + 1; i--; )
861 envv->push_back (strdup (SvPVbyte_nolen (*av_fetch (env, i, 1))));
867 term->init (argv, envv);
869 catch (const class rxvt_failure_exception &e)
872 croak ("error while initializing new terminal instance");
875 RETVAL = term && term->perl.self
876 ? newSVterm (term) : &PL_sv_undef;
882 rxvt_term::destroy ()
885 rxvt_term::set_should_invoke (int htype, int inc)
887 THIS->perl.should_invoke [htype] += inc;
890 rxvt_term::grab_button (int button, U32 modifiers, Window window = THIS->vt)
892 RETVAL = XGrabButton (THIS->dpy, button, modifiers, window, 1,
893 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
894 GrabModeSync, GrabModeSync, None, GRAB_CURSOR);
898 rxvt_term::ungrab_button (int button, U32 modifiers, Window window = THIS->vt)
900 RETVAL = XUngrabButton (THIS->dpy, button, modifiers, window);
904 rxvt_term::XGrabKey (int keycode, U32 modifiers, Window window = THIS->vt, \
905 int owner_events = 1, int pointer_mode = GrabModeAsync, int keyboard_mode = GrabModeAsync)
907 XGrabKey (THIS->dpy, keycode, modifiers, window, owner_events, pointer_mode, keyboard_mode);
910 rxvt_term::XUngrabKey (int keycode, U32 modifiers, Window window = THIS->vt)
912 XUngrabKey (THIS->dpy, keycode, modifiers, window);
915 rxvt_term::XUngrabKeyboard (Time eventtime)
917 XUngrabKeyboard (THIS->dpy, eventtime);
920 rxvt_term::grab (Time eventtime, int sync = 0)
923 int mode = sync ? GrabModeSync : GrabModeAsync;
925 THIS->perl.grabtime = 0;
927 if (!XGrabPointer (THIS->dpy, THIS->vt, 0,
928 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
929 mode, mode, None, GRAB_CURSOR, eventtime))
930 if (!XGrabKeyboard (THIS->dpy, THIS->vt, 0, mode, mode, eventtime))
931 THIS->perl.grabtime = eventtime;
933 XUngrabPointer (THIS->dpy, eventtime);
935 RETVAL = !!THIS->perl.grabtime;
941 rxvt_term::allow_events_async ()
943 XAllowEvents (THIS->dpy, AsyncBoth, THIS->perl.grabtime);
946 rxvt_term::allow_events_sync ()
948 XAllowEvents (THIS->dpy, SyncBoth, THIS->perl.grabtime);
951 rxvt_term::allow_events_replay ()
953 XAllowEvents (THIS->dpy, ReplayPointer, THIS->perl.grabtime);
954 XAllowEvents (THIS->dpy, ReplayKeyboard, THIS->perl.grabtime);
962 rxvt_term::XStringToKeysym (char *string)
964 RETVAL = XStringToKeysym (string);
968 rxvt_term::XKeysymToString (int sym)
970 RETVAL = XKeysymToString (sym);
974 rxvt_term::XKeysymToKeycode (int sym)
976 RETVAL = XKeysymToKeycode (THIS->dpy, sym);
980 rxvt_term::XKeycodeToKeysym (int code, int index)
982 RETVAL = XKeycodeToKeysym (THIS->dpy, code, index);
986 rxvt_term::strwidth (SV *str)
989 wchar_t *wstr = sv2wcs (str);
991 rxvt_push_locale (THIS->locale);
993 for (wchar_t *wc = wstr; *wc; wc++)
995 int w = WCWIDTH (*wc);
998 RETVAL += max (w, 1);
1008 rxvt_term::locale_encode (SV *str)
1011 wchar_t *wstr = sv2wcs (str);
1013 rxvt_push_locale (THIS->locale);
1014 char *mbstr = rxvt_wcstombs (wstr);
1019 RETVAL = newSVpv (mbstr, 0);
1026 rxvt_term::locale_decode (SV *octets)
1030 char *data = SvPVbyte (octets, len);
1032 rxvt_push_locale (THIS->locale);
1033 wchar_t *wstr = rxvt_mbstowcs (data, len);
1036 RETVAL = wcs2sv (wstr);
1043 rxvt_term::locale ()
1045 RETVAL = THIS->locale;
1049 #define TERM_OFFSET(sym) offsetof (TermWin_t, sym)
1051 #define TERM_OFFSET_width TERM_OFFSET(width)
1052 #define TERM_OFFSET_height TERM_OFFSET(height)
1053 #define TERM_OFFSET_fwidth TERM_OFFSET(fwidth)
1054 #define TERM_OFFSET_fheight TERM_OFFSET(fheight)
1055 #define TERM_OFFSET_fbase TERM_OFFSET(fbase)
1056 #define TERM_OFFSET_nrow TERM_OFFSET(nrow)
1057 #define TERM_OFFSET_ncol TERM_OFFSET(ncol)
1058 #define TERM_OFFSET_focus TERM_OFFSET(focus)
1059 #define TERM_OFFSET_mapped TERM_OFFSET(mapped)
1060 #define TERM_OFFSET_int_bwidth TERM_OFFSET(int_bwidth)
1061 #define TERM_OFFSET_ext_bwidth TERM_OFFSET(ext_bwidth)
1062 #define TERM_OFFSET_lineSpace TERM_OFFSET(lineSpace)
1063 #define TERM_OFFSET_saveLines TERM_OFFSET(saveLines)
1064 #define TERM_OFFSET_total_rows TERM_OFFSET(total_rows)
1065 #define TERM_OFFSET_top_row TERM_OFFSET(top_row)
1070 width = TERM_OFFSET_width
1071 height = TERM_OFFSET_height
1072 fwidth = TERM_OFFSET_fwidth
1073 fheight = TERM_OFFSET_fheight
1074 fbase = TERM_OFFSET_fbase
1075 nrow = TERM_OFFSET_nrow
1076 ncol = TERM_OFFSET_ncol
1077 focus = TERM_OFFSET_focus
1078 mapped = TERM_OFFSET_mapped
1079 int_bwidth = TERM_OFFSET_int_bwidth
1080 ext_bwidth = TERM_OFFSET_ext_bwidth
1081 lineSpace = TERM_OFFSET_lineSpace
1082 saveLines = TERM_OFFSET_saveLines
1083 total_rows = TERM_OFFSET_total_rows
1084 top_row = TERM_OFFSET_top_row
1086 RETVAL = *(int *)((char *)THIS + ix);
1091 rxvt_term::ModLevel3Mask ()
1101 case 0: RETVAL = THIS->ModLevel3Mask; break;
1102 case 1: RETVAL = THIS->ModMetaMask; break;
1103 case 2: RETVAL = THIS->ModNumLockMask; break;
1104 case 3: RETVAL = THIS->current_screen; break;
1105 case 4: RETVAL = THIS->hidden_cursor; break;
1111 rxvt_term::display_id ()
1118 case 0: RETVAL = THIS->display->id; break;
1119 case 1: RETVAL = THIS->locale; break;
1130 stringvec *vec = ix ? THIS->argv : THIS->envv;
1132 EXTEND (SP, vec->size ());
1134 for (char **i = vec->begin (); i != vec->end (); ++i)
1136 PUSHs (sv_2mortal (newSVpv (*i, 0)));
1140 rxvt_term::pty_ev_events (int events = ev::UNDEF)
1142 RETVAL = THIS->pty_ev.events;
1143 if (events != ev::UNDEF)
1144 THIS->pty_ev.set (events);
1149 rxvt_term::pty_fd ()
1151 RETVAL = THIS->pty->pty;
1156 rxvt_term::parent ()
1158 RETVAL = THIS->parent [0];
1170 rxvt_term::vt_emask_add (U32 emask)
1172 THIS->vt_emask_perl |= emask;
1173 THIS->vt_select_input ();
1176 rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
1178 RETVAL = THIS->rstyle;
1179 THIS->rstyle = new_rstyle;
1184 rxvt_term::view_start (int newval = 1)
1188 RETVAL = THIS->view_start;
1191 THIS->scr_changeview (max (newval, THIS->top_row));
1197 rxvt_term::focus_in ()
1200 rxvt_term::focus_out ()
1203 rxvt_term::key_press (unsigned int state, unsigned int keycode, Time time = CurrentTime)
1210 memset (&xkey, 0, sizeof (xkey));
1214 xkey.keycode = keycode;
1216 xkey.type = ix ? KeyRelease : KeyPress;
1217 xkey.display = THIS->dpy;
1218 xkey.window = THIS->vt;
1219 xkey.root = THIS->display->root;
1220 xkey.subwindow = THIS->vt;
1223 THIS->key_release (xkey);
1225 THIS->key_press (xkey);
1229 rxvt_term::want_refresh ()
1231 THIS->want_refresh = 1;
1232 THIS->refresh_check ();
1235 rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1238 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1241 line_t &l = ROW(row_number);
1243 if (GIMME_V != G_VOID)
1245 wchar_t *wstr = rxvt_temp_buf<wchar_t> (THIS->ncol);
1247 for (int col = 0; col < THIS->ncol; col++)
1248 wstr [col] = l.t [col];
1250 XPUSHs (sv_2mortal (wcs2sv (wstr, THIS->ncol)));
1255 wchar_t *wstr = sv2wcs (new_text);
1257 int len = min (wcslen (wstr) - start_ofs, max_len);
1259 if (start_col < 0 || start_col + len > THIS->ncol)
1262 croak ("new_text extends beyond horizontal margins");
1265 for (int col = start_col; col < start_col + len; col++)
1267 l.t [col] = wstr [start_ofs + col - start_col];
1268 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
1276 rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1279 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1282 line_t &l = ROW(row_number);
1284 if (GIMME_V != G_VOID)
1288 av_extend (av, THIS->ncol - 1);
1289 for (int col = 0; col < THIS->ncol; col++)
1290 av_store (av, col, newSViv (l.r [col]));
1292 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1297 if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
1298 croak ("new_rend must be arrayref");
1300 AV *av = (AV *)SvRV (new_rend);
1301 int len = min (AvFILL (av) + 1 - start_ofs, max_len);
1303 if (start_col < 0 || start_col + len > THIS->ncol)
1304 croak ("new_rend array extends beyond horizontal margins");
1306 for (int col = start_col; col < start_col + len; col++)
1308 rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask;
1310 l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
1316 rxvt_term::ROW_l (int row_number, int new_length = -1)
1319 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1322 line_t &l = ROW(row_number);
1325 if (new_length >= 0)
1332 rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
1335 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1338 line_t &l = ROW(row_number);
1339 RETVAL = l.is_longer ();
1341 if (new_is_longer >= 0)
1342 l.is_longer (new_is_longer);
1348 rxvt_term::special_encode (SV *string)
1351 wchar_t *wstr = sv2wcs (string);
1352 int wlen = wcslen (wstr);
1353 wchar_t *rstr = rxvt_temp_buf<wchar_t> (wlen * 2); // cannot become longer
1355 rxvt_push_locale (THIS->locale);
1358 for (wchar_t *s = wstr; *s; s++)
1360 int w = WCWIDTH (*s);
1365 croak ("leading combining character unencodable");
1367 unicode_t n = rxvt_compose (r[-1], *s);
1369 n = rxvt_composite.compose (r[-1], *s);
1374 else if (*s >= 0x10000)
1375 *r++ = rxvt_composite.compose (*s);
1380 // the *2 above only allows wcwidth <= 2
1387 RETVAL = wcs2sv (rstr, r - rstr);
1393 rxvt_term::special_decode (SV *text)
1396 wchar_t *wstr = sv2wcs (text);
1400 for (wchar_t *s = wstr; *s; s++)
1403 else if (IS_COMPOSE (*s))
1404 dlen += rxvt_composite.expand (*s, 0);
1408 wchar_t *rstr = rxvt_temp_buf<wchar_t> (dlen);
1412 for (wchar_t *s = wstr; *s; s++)
1415 else if (IS_COMPOSE (*s))
1416 r += rxvt_composite.expand (*s, r);
1420 RETVAL = wcs2sv (rstr, r - rstr);
1426 rxvt_term::_resource (char *name, int index, SV *newval = 0)
1429 static const struct resval { const char *name; int value; } *rs, rslist [] = {
1430 # define def(name) { # name, Rs_ ## name },
1431 # define reserve(name,count)
1437 rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
1441 croak ("no such resource '%s', requested", name);
1442 } while (strcmp (name, rs->name));
1446 if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
1447 croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
1449 if (GIMME_V != G_VOID)
1450 XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
1456 char *str = strdup (SvPVbyte_nolen (newval));
1457 THIS->rs [index] = str;
1458 THIS->allocated.push_back (str);
1461 THIS->rs [index] = 0;
1466 rxvt_term::x_resource (const char *name)
1469 rxvt_term::option (U8 optval, int set = -1)
1472 RETVAL = THIS->option (optval);
1476 THIS->set_option (optval, set);
1478 if (THIS->env_colorfgbg [0]) // avoid doing this before START
1481 case Opt_skipBuiltinGlyphs:
1483 THIS->scr_remap_chars ();
1484 THIS->scr_touch (true);
1485 THIS->want_refresh = 1;
1486 THIS->refresh_check ();
1489 case Opt_cursorUnderline:
1490 THIS->want_refresh = 1;
1491 THIS->refresh_check ();
1494 # case Opt_scrollBar_floating:
1495 # case Opt_scrollBar_right:
1496 # THIS->resize_all_windows (THIS->width, THIS->height, 1);
1505 rxvt_term::parse_keysym (char *keysym, char *str)
1507 RETVAL = 0 < THIS->parse_keysym (keysym, str);
1508 THIS->keyboard->register_done ();
1513 rxvt_term::screen_cur (...)
1522 row_col_t &rc = ix == 0 ? THIS->screen.cur
1523 : ix == 1 ? THIS->selection.beg
1524 : ix == 2 ? THIS->selection.end
1525 : THIS->selection.mark;
1527 if (GIMME_V != G_VOID)
1530 PUSHs (sv_2mortal (newSViv (rc.row)));
1531 PUSHs (sv_2mortal (newSViv (rc.col)));
1536 rc.row = SvIV (ST (1));
1537 rc.col = SvIV (ST (2));
1543 // col == 0 means end of previous line
1545 rc.col = THIS->ncol;
1547 else if (IN_RANGE_EXC (rc.row, THIS->top_row, THIS->nrow)
1548 && rc.col > ROW(rc.row).l)
1550 // col >= length means while line and add newline
1551 rc.col = THIS->ncol;
1555 clamp_it (rc.col, 0, THIS->ncol);
1556 clamp_it (rc.row, THIS->top_row, THIS->nrow - 1);
1560 THIS->want_refresh = 1;
1561 THIS->refresh_check ();
1567 rxvt_term::cur_charset ()
1569 RETVAL = THIS->charsets [THIS->screen.charset];
1574 rxvt_term::selection_clear ()
1577 rxvt_term::selection_make (Time eventtime, bool rect = false)
1579 THIS->selection.op = SELECTION_CONT;
1580 THIS->selection.rect = rect;
1581 THIS->selection_make (eventtime);
1584 rxvt_term::selection_grab (Time eventtime)
1587 rxvt_term::selection (SV *newtext = 0)
1590 if (GIMME_V != G_VOID)
1591 XPUSHs (THIS->selection.text
1592 ? sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))
1597 free (THIS->selection.text);
1599 THIS->selection.text = sv2wcs (newtext);
1600 THIS->selection.len = wcslen (THIS->selection.text);
1605 rxvt_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)
1608 rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle = RS_RVid)
1611 rxvt_term::scr_bell ()
1614 rxvt_term::scr_change_screen (int screen)
1617 rxvt_term::scr_add_lines (SV *string)
1620 wchar_t *wstr = sv2wcs (string);
1621 THIS->scr_add_lines (wstr, wcslen (wstr));
1626 rxvt_term::tt_write (SV *octets)
1629 char *str = SvPVbyte (octets, len);
1634 rxvt_term::cmd_parse (SV *octets)
1638 char *str = SvPVbyte (octets, len);
1640 char *old_cmdbuf_ptr = THIS->cmdbuf_ptr;
1641 char *old_cmdbuf_endp = THIS->cmdbuf_endp;
1643 THIS->cmdbuf_ptr = str;
1644 THIS->cmdbuf_endp = str + len;
1646 rxvt_push_locale (THIS->locale);
1650 THIS->cmdbuf_ptr = old_cmdbuf_ptr;
1651 THIS->cmdbuf_endp = old_cmdbuf_endp;
1655 rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
1658 overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
1659 RETVAL = newSVptr ((void *)o, "urxvt::overlay");
1660 o->self = (HV *)SvRV (RETVAL);
1665 #############################################################################
1666 # Various X Utility Functions
1667 #############################################################################
1670 rxvt_term::XListProperties (Window window)
1674 Atom *props = XListProperties (THIS->dpy, window, &count);
1678 PUSHs (newSVuv ((U32)props [count]));
1684 rxvt_term::XGetWindowProperty (Window window, Atom property)
1689 unsigned long nitems;
1690 unsigned long bytes_after;
1691 unsigned char *prop;
1693 XGetWindowProperty (THIS->dpy, window, property,
1694 0, 1<<24, 0, AnyPropertyType,
1695 &type, &format, &nitems, &bytes_after, &prop);
1699 int elemsize = format == 16 ? sizeof (short)
1700 : format == 32 ? sizeof (long)
1704 PUSHs (newSVuv ((U32)type));
1705 PUSHs (newSViv (format));
1706 PUSHs (newSVpvn ((char *)prop, nitems * elemsize));
1712 rxvt_term::XChangeWindowProperty (Window window, Atom property, Atom type, int format, SV *data)
1716 char *data_ = SvPVbyte (data, len);
1718 int elemsize = format == 16 ? sizeof (short)
1719 : format == 32 ? sizeof (long)
1722 XChangeProperty (THIS->dpy, window, property,
1723 type, format, PropModeReplace,
1724 (unsigned char *)data_, len / elemsize);
1728 XInternAtom (rxvt_term *term, char *atom_name, int only_if_exists = FALSE)
1729 C_ARGS: term->dpy, atom_name, only_if_exists
1732 XGetAtomName (rxvt_term *term, Atom atom)
1733 C_ARGS: term->dpy, atom
1738 XDeleteProperty (rxvt_term *term, Window window, Atom property)
1739 C_ARGS: term->dpy, window, property
1742 rxvt_term::DefaultRootWindow ()
1744 RETVAL = THIS->display->root;
1751 XCreateSimpleWindow (rxvt_term *term, Window parent, int x, int y, unsigned int width, unsigned int height)
1752 C_ARGS: term->dpy, (Window)parent,
1753 x, y, width, height, 0,
1754 term->pix_colors_focused[Color_border],
1755 term->pix_colors_focused[Color_border]
1760 XReparentWindow (rxvt_term *term, Window window, Window parent, int x = 0, int y = 0)
1761 C_ARGS: term->dpy, window, parent, x, y
1764 XMapWindow (rxvt_term *term, Window window)
1765 C_ARGS: term->dpy, window
1768 XUnmapWindow (rxvt_term *term, Window window)
1769 C_ARGS: term->dpy, window
1772 XMoveResizeWindow (rxvt_term *term, Window window, int x, int y, unsigned int width, unsigned int height)
1773 C_ARGS: term->dpy, window, x, y, width, height
1776 rxvt_term::XChangeInput (Window window, U32 add_events, U32 del_events = 0)
1779 XWindowAttributes attr;
1780 XGetWindowAttributes (THIS->dpy, window, &attr);
1781 XSelectInput (THIS->dpy, window, attr.your_event_mask | add_events & ~del_events);
1785 rxvt_term::XTranslateCoordinates (Window src, Window dst, int x, int y)
1791 if (XTranslateCoordinates (THIS->dpy, src, dst, x, y, &dx, &dy, &child))
1794 PUSHs (newSViv (dx));
1795 PUSHs (newSViv (dy));
1796 PUSHs (newSVuv (child));
1800 #############################################################################
1802 #############################################################################
1804 MODULE = urxvt PACKAGE = urxvt::overlay
1807 overlay::set (int x, int y, SV *text, SV *rend = 0)
1818 INCLUDE: $PERL <iom_perl.xs -pe s/IOM_MODULE/urxvt/g,s/IOM_CLASS/urxvt/g |