ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.112
Committed: Thu Dec 14 17:40:03 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-8_2
Changes since 1.111: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*----------------------------------------------------------------------*
2     * File: rxvtperl.xs
3     *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6 root 1.31 * Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com>
7 root 1.1 *
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.
12     *
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.
17     *
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     *----------------------------------------------------------------------*/
22    
23     #define line_t perl_line_t
24     #include <EXTERN.h>
25     #include <perl.h>
26     #include <XSUB.h>
27     #undef line_t
28    
29     #include "../config.h"
30    
31 root 1.38 #include <cstddef>
32 root 1.1 #include <cstdarg>
33    
34 root 1.59 #include "unistd.h"
35    
36 root 1.50 #include "iom.h"
37 root 1.1 #include "rxvt.h"
38 root 1.50 #include "keyboard.h"
39 root 1.1 #include "rxvtutil.h"
40     #include "rxvtperl.h"
41    
42     #include "perlxsi.c"
43    
44 root 1.69 #ifdef HAVE_SCROLLBARS
45 root 1.41 # define GRAB_CURSOR THIS->leftptr_cursor
46     #else
47     # define GRAB_CURSOR None
48     #endif
49    
50 root 1.8 #undef LINENO
51     #define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
52     #undef ROW
53     #define ROW(n) THIS->row_buf [LINENO (n)]
54    
55 root 1.1 /////////////////////////////////////////////////////////////////////////////
56    
57     static wchar_t *
58     sv2wcs (SV *sv)
59     {
60     STRLEN len;
61     char *str = SvPVutf8 (sv, len);
62     return rxvt_utf8towcs (str, len);
63     }
64    
65     static SV *
66 root 1.25 wcs2sv (wchar_t *wstr, int len = -1)
67     {
68     char *str = rxvt_wcstoutf8 (wstr, len);
69    
70     SV *sv = newSVpv (str, 0);
71     SvUTF8_on (sv);
72     free (str);
73    
74     return sv;
75     }
76    
77     static SV *
78 root 1.1 new_ref (HV *hv, const char *klass)
79     {
80     return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
81     }
82    
83     static SV *
84     newSVptr (void *ptr, const char *klass)
85     {
86     HV *hv = newHV ();
87 root 1.18 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
88 root 1.1 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
89     }
90    
91 root 1.18 static void
92     clearSVptr (SV *sv)
93     {
94     if (SvROK (sv))
95     sv = SvRV (sv);
96    
97     hv_clear ((HV *)sv);
98     sv_unmagic (sv, PERL_MAGIC_ext);
99     }
100    
101 root 1.1 static long
102     SvPTR (SV *sv, const char *klass)
103     {
104     if (!sv_derived_from (sv, klass))
105     croak ("object of type %s expected", klass);
106    
107 root 1.18 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
108 root 1.1
109 root 1.18 if (!mg)
110 root 1.1 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
111    
112 root 1.18 return (long)mg->mg_ptr;
113 root 1.1 }
114    
115 root 1.95 #define newSVterm(term) SvREFCNT_inc ((SV *)(term)->perl.self)
116     #define SvTERM(sv) (rxvt_term *)SvPTR ((sv), "urxvt::term")
117 root 1.1
118     /////////////////////////////////////////////////////////////////////////////
119    
120 root 1.13 #define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
121    
122 root 1.93 class overlay {
123 root 1.13 rxvt_term *THIS;
124 root 1.93 AV *overlay_av;
125 root 1.13 int x, y, w, h;
126     int border;
127     text_t **text;
128     rend_t **rend;
129    
130 root 1.93 public:
131     HV *self;
132    
133 root 1.13 overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
134     ~overlay ();
135    
136 root 1.18 void show ();
137     void hide ();
138    
139 root 1.13 void swap ();
140    
141     void set (int x, int y, SV *str, SV *rend);
142     };
143    
144     overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
145 root 1.93 : THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2), overlay_av (0)
146 root 1.13 {
147 root 1.103 if (w < 0) w = 0;
148     if (h < 0) h = 0;
149    
150 root 1.13 if (border == 2)
151     {
152     w += 2;
153     h += 2;
154     }
155    
156     text = new text_t *[h];
157     rend = new rend_t *[h];
158 root 1.24
159 root 1.13 for (int y = 0; y < h; y++)
160 root 1.24 {
161 root 1.13 text_t *tp = text[y] = new text_t[w];
162     rend_t *rp = rend[y] = new rend_t[w];
163 root 1.24
164 root 1.13 text_t t0, t1, t2;
165     rend_t r = rstyle;
166 root 1.24
167 root 1.13 if (border == 2)
168     {
169     if (y == 0)
170     t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
171     else if (y < h - 1)
172     t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
173     else
174     t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
175 root 1.24
176     *tp++ = t0;
177 root 1.13 *rp++ = r;
178 root 1.24
179 root 1.13 for (int x = w - 2; x-- > 0; )
180     {
181     *tp++ = t1;
182     *rp++ = r;
183 root 1.24 }
184 root 1.13
185     *tp = t2;
186 root 1.24 *rp = r;
187 root 1.13 }
188     else
189     for (int x = w; x-- > 0; )
190     {
191     *tp++ = 0x0020;
192     *rp++ = r;
193 root 1.24 }
194     }
195 root 1.13
196 root 1.18 show ();
197 root 1.13 }
198    
199     overlay::~overlay ()
200     {
201 root 1.18 hide ();
202    
203 root 1.13 for (int y = h; y--; )
204     {
205     delete [] text[y];
206     delete [] rend[y];
207     }
208    
209     delete [] text;
210     delete [] rend;
211     }
212    
213 root 1.18 void
214     overlay::show ()
215     {
216 root 1.93 if (overlay_av)
217 root 1.76 return;
218    
219 root 1.93 overlay_av = (AV *)SvREFCNT_inc (SvRV (
220     *hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0)
221     ));
222     av_push (overlay_av, newSViv ((long)this));
223 root 1.18
224 root 1.93 THIS->want_refresh = 1;
225 root 1.18 }
226    
227     void
228     overlay::hide ()
229     {
230 root 1.93 if (!overlay_av)
231 root 1.76 return;
232    
233     int i;
234 root 1.18
235 root 1.93 for (i = AvFILL (overlay_av); i >= 0; i--)
236     if (SvIV (*av_fetch (overlay_av, i, 1)) == (long)this)
237 root 1.101 break;
238 root 1.76
239 root 1.93 for (; i < AvFILL (overlay_av); i++)
240     av_store (overlay_av, i, SvREFCNT_inc (*av_fetch (overlay_av, i + 1, 0)));
241    
242     av_pop (overlay_av);
243 root 1.18
244 root 1.93 SvREFCNT_dec (overlay_av);
245     overlay_av = 0;
246    
247     THIS->want_refresh = 1;
248 root 1.18 }
249    
250 root 1.13 void overlay::swap ()
251     {
252     int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
253     int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
254    
255     int ov_w = min (w, THIS->ncol - ov_x);
256     int ov_h = min (h, THIS->nrow - ov_y);
257    
258     for (int y = ov_h; y--; )
259     {
260     text_t *t1 = text [y];
261     rend_t *r1 = rend [y];
262 root 1.24
263 root 1.67 text_t *t2 = ROW(y + ov_y + THIS->view_start).t + ov_x;
264     rend_t *r2 = ROW(y + ov_y + THIS->view_start).r + ov_x;
265 root 1.13
266     for (int x = ov_w; x--; )
267 root 1.24 {
268 root 1.13 text_t t = *t1; *t1++ = *t2; *t2++ = t;
269     rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
270     }
271     }
272    
273     }
274    
275 root 1.14 void overlay::set (int x, int y, SV *text, SV *rend)
276 root 1.13 {
277     x += border;
278     y += border;
279    
280     if (!IN_RANGE_EXC (y, 0, h - border))
281     return;
282    
283 root 1.14 wchar_t *wtext = sv2wcs (text);
284 root 1.13
285 root 1.14 for (int col = min (wcslen (wtext), w - x - border); col--; )
286     this->text [y][x + col] = wtext [col];
287 root 1.24
288 root 1.14 free (wtext);
289    
290     if (rend)
291     {
292     if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
293     croak ("rend must be arrayref");
294    
295     AV *av = (AV *)SvRV (rend);
296    
297 root 1.76 for (int col = min (AvFILL (av) + 1, w - x - border); col--; )
298 root 1.14 this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
299     }
300 root 1.13
301     THIS->want_refresh = 1;
302     }
303    
304 root 1.109 /////////////////////////////////////////////////////////////////////////////
305    
306     #define IOM_CLASS "urxvt"
307 root 1.112 #define IOM_WARN rxvt_warn
308 root 1.109 #include <iom_perl.h>
309 root 1.13
310     /////////////////////////////////////////////////////////////////////////////
311    
312 root 1.1 struct rxvt_perl_interp rxvt_perl;
313    
314     static PerlInterpreter *perl;
315    
316     rxvt_perl_interp::~rxvt_perl_interp ()
317     {
318     if (perl)
319     {
320     perl_destruct (perl);
321     perl_free (perl);
322     }
323     }
324    
325     void
326 root 1.68 rxvt_perl_interp::init (rxvt_term *term)
327 root 1.1 {
328     if (!perl)
329     {
330 root 1.68 rxvt_push_locale (""); // perl init destroys current locale
331    
332 root 1.57 perl_environ = rxvt_environ;
333     swap (perl_environ, environ);
334 root 1.56
335 root 1.1 char *argv[] = {
336     "",
337 root 1.72 "-e"
338     "BEGIN {"
339     " urxvt->bootstrap;"
340     " unshift @INC, '" LIBDIR "';"
341     "}"
342     ""
343     "use urxvt;"
344 root 1.1 };
345    
346     perl = perl_alloc ();
347     perl_construct (perl);
348    
349 root 1.71 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
350 root 1.1 || perl_run (perl))
351     {
352     rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
353    
354     perl_destruct (perl);
355     perl_free (perl);
356     perl = 0;
357     }
358 root 1.57
359     swap (perl_environ, environ);
360 root 1.68
361     rxvt_pop_locale ();
362 root 1.1 }
363 root 1.68
364 root 1.70 if (perl)
365     {
366     // runs outside of perls ENV
367     term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term");
368 root 1.76 hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newAV ()), 0);
369 root 1.70 }
370 root 1.1 }
371    
372 root 1.45 static void
373     ungrab (rxvt_term *THIS)
374     {
375     if (THIS->perl.grabtime)
376     {
377 root 1.96 XUngrabKeyboard (THIS->dpy, THIS->perl.grabtime);
378     XUngrabPointer (THIS->dpy, THIS->perl.grabtime);
379 root 1.45 THIS->perl.grabtime = 0;
380     }
381     }
382    
383 root 1.1 bool
384     rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
385     {
386 root 1.68 if (!perl || !term->perl.self)
387 root 1.1 return false;
388 root 1.24
389 root 1.68 // pre-handling of some events
390     if (htype == HOOK_REFRESH_END)
391 root 1.76 {
392     AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
393    
394     for (int i = 0; i <= AvFILL (av); i++)
395     ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap ();
396     }
397 root 1.1
398 root 1.57 swap (perl_environ, environ);
399 root 1.56
400 root 1.68 bool event_consumed;
401 root 1.1
402 root 1.68 if (htype == HOOK_INIT || htype == HOOK_DESTROY // must be called always
403     || term->perl.should_invoke [htype])
404     try
405     {
406     dSP;
407     va_list ap;
408 root 1.1
409 root 1.68 va_start (ap, htype);
410 root 1.1
411 root 1.68 ENTER;
412     SAVETMPS;
413 root 1.1
414 root 1.68 PUSHMARK (SP);
415 root 1.1
416 root 1.68 XPUSHs (sv_2mortal (newSVterm (term)));
417     XPUSHs (sv_2mortal (newSViv (htype)));
418 root 1.1
419 root 1.68 for (;;) {
420     data_type dt = (data_type)va_arg (ap, int);
421 root 1.1
422 root 1.68 switch (dt)
423     {
424     case DT_INT:
425     XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
426     break;
427    
428     case DT_LONG:
429     XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
430     break;
431    
432     case DT_STR:
433 root 1.71 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
434 root 1.68 break;
435    
436     case DT_STR_LEN:
437     {
438     char *str = va_arg (ap, char *);
439 root 1.91 int len = va_arg (ap, int);
440 root 1.68
441 root 1.71 XPUSHs (sv_2mortal (newSVpvn (str, len)));
442 root 1.68 }
443     break;
444    
445     case DT_WCS_LEN:
446     {
447     wchar_t *wstr = va_arg (ap, wchar_t *);
448 root 1.91 int wlen = va_arg (ap, int);
449 root 1.68
450 root 1.71 XPUSHs (sv_2mortal (wcs2sv (wstr, wlen)));
451 root 1.68 }
452     break;
453    
454 root 1.91 case DT_LCS_LEN:
455     {
456     long *lstr = va_arg (ap, long *);
457     int llen = va_arg (ap, int);
458    
459     XPUSHs (sv_2mortal (newSVpvn ((char *)lstr, llen * sizeof (long))));
460     }
461     break;
462    
463 root 1.68 case DT_XEVENT:
464     {
465     XEvent *xe = va_arg (ap, XEvent *);
466     HV *hv = newHV ();
467 root 1.29
468 root 1.91 # define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
469     # define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
470     # define setuv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSVuv (val), 0)
471     # undef set
472 root 1.29
473 root 1.68 setiv (type, xe->type);
474     setiv (send_event, xe->xany.send_event);
475     setiv (serial, xe->xany.serial);
476    
477     switch (xe->type)
478     {
479     case KeyPress:
480     case KeyRelease:
481     case ButtonPress:
482     case ButtonRelease:
483     case MotionNotify:
484 root 1.74 setuv (window, xe->xmotion.window);
485     setuv (root, xe->xmotion.root);
486     setuv (subwindow, xe->xmotion.subwindow);
487     setuv (time, xe->xmotion.time);
488     setiv (x, xe->xmotion.x);
489     setiv (y, xe->xmotion.y);
490 root 1.85 setiv (row, xe->xmotion.y / term->fheight + term->view_start);
491 root 1.74 setiv (col, xe->xmotion.x / term->fwidth);
492     setiv (x_root, xe->xmotion.x_root);
493     setiv (y_root, xe->xmotion.y_root);
494     setuv (state, xe->xmotion.state);
495    
496     switch (xe->type)
497     {
498     case KeyPress:
499     case KeyRelease:
500     setuv (keycode, xe->xkey.keycode);
501     break;
502    
503     case ButtonPress:
504     case ButtonRelease:
505     setuv (button, xe->xbutton.button);
506     break;
507    
508     case MotionNotify:
509     setiv (is_hint, xe->xmotion.is_hint);
510     break;
511     }
512 root 1.68
513     break;
514    
515 root 1.74 case MapNotify:
516     case UnmapNotify:
517     case ConfigureNotify:
518     setuv (event, xe->xconfigure.event);
519     setuv (window, xe->xconfigure.window);
520    
521     switch (xe->type)
522     {
523     case ConfigureNotify:
524     setiv (x, xe->xconfigure.x);
525     setiv (y, xe->xconfigure.y);
526     setiv (width, xe->xconfigure.width);
527     setiv (height, xe->xconfigure.height);
528     setuv (above, xe->xconfigure.above);
529     break;
530     }
531 root 1.68
532     break;
533 root 1.80
534 root 1.84 case PropertyNotify:
535     setuv (window, xe->xproperty.window);
536     setuv (atom, xe->xproperty.atom);
537     setuv (time, xe->xproperty.time);
538     setiv (state, xe->xproperty.state);
539     break;
540    
541 root 1.80 case ClientMessage:
542     setuv (window, xe->xclient.window);
543     setuv (message_type, xe->xclient.message_type);
544     setuv (format, xe->xclient.format);
545     setuv (l0, xe->xclient.data.l[0]);
546     setuv (l1, xe->xclient.data.l[1]);
547     setuv (l2, xe->xclient.data.l[2]);
548     setuv (l3, xe->xclient.data.l[3]);
549     setuv (l4, xe->xclient.data.l[4]);
550 root 1.84 break;
551 root 1.68 }
552    
553     XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
554     }
555     break;
556    
557     case DT_END:
558     goto call;
559    
560     default:
561     rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
562     }
563     }
564 root 1.29
565 root 1.68 call:
566     va_end (ap);
567 root 1.29
568 root 1.68 PUTBACK;
569     int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
570     SPAGAIN;
571 root 1.29
572 root 1.68 if (count)
573     {
574     SV *status = POPs;
575     count = SvTRUE (status);
576     }
577 root 1.29
578 root 1.68 PUTBACK;
579     FREETMPS;
580     LEAVE;
581 root 1.29
582 root 1.68 if (SvTRUE (ERRSV))
583     {
584     rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
585     ungrab (term); // better lose the grab than the session
586 root 1.29 }
587 root 1.68
588     event_consumed = !!count;
589 root 1.1 }
590 root 1.68 catch (...)
591     {
592     swap (perl_environ, environ);
593     throw;
594     }
595     else
596     event_consumed = false;
597    
598     // post-handling of some events
599     if (htype == HOOK_REFRESH_BEGIN)
600 root 1.76 {
601     AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
602    
603     for (int i = AvFILL (av); i >= 0; i--)
604     ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap ();
605     }
606 root 1.68 else if (htype == HOOK_DESTROY)
607 root 1.57 {
608 root 1.68 clearSVptr ((SV *)term->perl.self);
609     SvREFCNT_dec ((SV *)term->perl.self);
610 root 1.91
611     // don't allow further calls
612     term->perl.self = 0;
613 root 1.57 }
614 root 1.68
615     swap (perl_environ, environ);
616    
617     return event_consumed;
618 root 1.1 }
619    
620     /////////////////////////////////////////////////////////////////////////////
621    
622     MODULE = urxvt PACKAGE = urxvt
623    
624     PROTOTYPES: ENABLE
625    
626     BOOT:
627     {
628 root 1.52 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
629     sv_setsv (get_sv ("urxvt::RESNAME", 1), newSVpvn (RESNAME, sizeof (RESNAME) - 1));
630     sv_setsv (get_sv ("urxvt::RESCLASS", 1), newSVpvn (RESCLASS, sizeof (RESCLASS) - 1));
631     sv_setsv (get_sv ("urxvt::RXVTNAME", 1), newSVpvn (RXVTNAME, sizeof (RXVTNAME) - 1));
632 root 1.37
633 root 1.1 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
634 root 1.21 # define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
635     # include "hookinc.h"
636     # undef def
637 root 1.1
638 root 1.39 HV *option = get_hv ("urxvt::OPTION", 1);
639     # define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0);
640     # define nodef(name)
641     # include "optinc.h"
642     # undef nodef
643     # undef def
644    
645     HV *stash = gv_stashpv ("urxvt", 1);
646 root 1.97 static const struct {
647 root 1.62 const char *name;
648     IV iv;
649     } *civ, const_iv[] = {
650     # define const_iv(name) { # name, (IV)name }
651     const_iv (DEFAULT_RSTYLE),
652     const_iv (OVERLAY_RSTYLE),
653     const_iv (RS_Bold),
654     const_iv (RS_Italic),
655     const_iv (RS_Blink),
656     const_iv (RS_RVid),
657     const_iv (RS_Uline),
658    
659     const_iv (CurrentTime),
660     const_iv (ShiftMask),
661     const_iv (LockMask),
662     const_iv (ControlMask),
663     const_iv (Mod1Mask),
664     const_iv (Mod2Mask),
665     const_iv (Mod3Mask),
666     const_iv (Mod4Mask),
667     const_iv (Mod5Mask),
668     const_iv (Button1Mask),
669     const_iv (Button2Mask),
670     const_iv (Button3Mask),
671     const_iv (Button4Mask),
672     const_iv (Button5Mask),
673     const_iv (AnyModifier),
674    
675 root 1.102 const_iv (NoSymbol),
676     const_iv (GrabModeSync),
677     const_iv (GrabModeAsync),
678    
679 root 1.62 const_iv (NoEventMask),
680     const_iv (KeyPressMask),
681     const_iv (KeyReleaseMask),
682     const_iv (ButtonPressMask),
683     const_iv (ButtonReleaseMask),
684     const_iv (EnterWindowMask),
685     const_iv (LeaveWindowMask),
686     const_iv (PointerMotionMask),
687     const_iv (PointerMotionHintMask),
688     const_iv (Button1MotionMask),
689     const_iv (Button2MotionMask),
690     const_iv (Button3MotionMask),
691     const_iv (Button4MotionMask),
692     const_iv (Button5MotionMask),
693     const_iv (ButtonMotionMask),
694     const_iv (KeymapStateMask),
695     const_iv (ExposureMask),
696     const_iv (VisibilityChangeMask),
697     const_iv (StructureNotifyMask),
698     const_iv (ResizeRedirectMask),
699     const_iv (SubstructureNotifyMask),
700     const_iv (SubstructureRedirectMask),
701     const_iv (FocusChangeMask),
702     const_iv (PropertyChangeMask),
703     const_iv (ColormapChangeMask),
704     const_iv (OwnerGrabButtonMask),
705    
706     const_iv (KeyPress),
707     const_iv (KeyRelease),
708     const_iv (ButtonPress),
709     const_iv (ButtonRelease),
710     const_iv (MotionNotify),
711     const_iv (EnterNotify),
712     const_iv (LeaveNotify),
713     const_iv (FocusIn),
714     const_iv (FocusOut),
715     const_iv (KeymapNotify),
716     const_iv (Expose),
717     const_iv (GraphicsExpose),
718     const_iv (NoExpose),
719     const_iv (VisibilityNotify),
720     const_iv (CreateNotify),
721     const_iv (DestroyNotify),
722     const_iv (UnmapNotify),
723     const_iv (MapNotify),
724     const_iv (MapRequest),
725     const_iv (ReparentNotify),
726     const_iv (ConfigureNotify),
727     const_iv (ConfigureRequest),
728     const_iv (GravityNotify),
729     const_iv (ResizeRequest),
730     const_iv (CirculateNotify),
731     const_iv (CirculateRequest),
732     const_iv (PropertyNotify),
733     const_iv (SelectionClear),
734     const_iv (SelectionRequest),
735     const_iv (SelectionNotify),
736     const_iv (ColormapNotify),
737     const_iv (ClientMessage),
738     const_iv (MappingNotify),
739 root 1.91 # if ENABLE_XIM_ONTHESPOT
740     const_iv (XIMReverse),
741     const_iv (XIMUnderline),
742     const_iv (XIMHighlight),
743     const_iv (XIMPrimary),
744     const_iv (XIMSecondary),
745     const_iv (XIMTertiary),
746     const_iv (XIMVisibleToForward),
747     const_iv (XIMVisibleToBackword),
748     const_iv (XIMVisibleToCenter),
749 root 1.92 # if 0
750 root 1.91 const_iv (XIMForwardChar),
751     const_iv (XIMBackwardChar),
752     const_iv (XIMForwardWord),
753     const_iv (XIMBackwardWord),
754     const_iv (XIMCaretUp),
755     const_iv (XIMCaretDown),
756     const_iv (XIMNextLine),
757     const_iv (XIMPreviousLine),
758     const_iv (XIMLineStart),
759     const_iv (XIMLineEnd),
760     const_iv (XIMAbsolutePosition),
761     const_iv (XIMDontChange),
762     # endif
763 root 1.92 # endif
764 root 1.62 };
765    
766 root 1.96 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
767 root 1.62 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
768 root 1.1 }
769    
770     void
771     warn (const char *msg)
772     CODE:
773     rxvt_warn ("%s", msg);
774    
775     void
776     fatal (const char *msg)
777     CODE:
778     rxvt_fatal ("%s", msg);
779    
780 root 1.59 void
781     _exit (int status)
782    
783 root 1.3 NV
784     NOW ()
785     CODE:
786     RETVAL = NOW;
787     OUTPUT:
788     RETVAL
789    
790 root 1.11 int
791     GET_BASEFG (int rend)
792     CODE:
793     RETVAL = GET_BASEFG (rend);
794     OUTPUT:
795     RETVAL
796    
797     int
798     GET_BASEBG (int rend)
799     CODE:
800     RETVAL = GET_BASEBG (rend);
801     OUTPUT:
802     RETVAL
803    
804     int
805 root 1.12 SET_FGCOLOR (int rend, int new_color)
806 root 1.11 CODE:
807 root 1.98 RETVAL = SET_FGCOLOR (rend, clamp (new_color, 0, TOTAL_COLORS - 1));
808 root 1.11 OUTPUT:
809     RETVAL
810    
811     int
812 root 1.12 SET_BGCOLOR (int rend, int new_color)
813 root 1.11 CODE:
814 root 1.98 RETVAL = SET_BGCOLOR (rend, clamp (new_color, 0, TOTAL_COLORS - 1));
815 root 1.12 OUTPUT:
816     RETVAL
817    
818     int
819     GET_CUSTOM (int rend)
820     CODE:
821 root 1.108 RETVAL = (rend & RS_customMask) >> RS_customShift;
822 root 1.12 OUTPUT:
823     RETVAL
824    
825     int
826     SET_CUSTOM (int rend, int new_value)
827     CODE:
828     {
829     if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
830     croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
831    
832     RETVAL = (rend & ~RS_customMask)
833     | ((new_value << RS_customShift) & RS_customMask);
834     }
835 root 1.11 OUTPUT:
836     RETVAL
837    
838 root 1.95 void
839     termlist ()
840     PPCODE:
841     {
842     EXTEND (SP, rxvt_term::termlist.size ());
843    
844     for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
845     if ((*t)->perl.self)
846     PUSHs (sv_2mortal (newSVterm (*t)));
847     }
848    
849 root 1.3 MODULE = urxvt PACKAGE = urxvt::term
850    
851 root 1.54 SV *
852 root 1.95 _new (AV *env, AV *arg)
853 root 1.54 CODE:
854     {
855     rxvt_term *term = new rxvt_term;
856    
857 root 1.88 stringvec *argv = new stringvec;
858     stringvec *envv = new stringvec;
859 root 1.54
860 root 1.95 for (int i = 0; i <= AvFILL (arg); i++)
861     argv->push_back (strdup (SvPVbyte_nolen (*av_fetch (arg, i, 1))));
862 root 1.54
863 root 1.88 for (int i = AvFILL (env) + 1; i--; )
864     envv->push_back (strdup (SvPVbyte_nolen (*av_fetch (env, i, 1))));
865 root 1.54
866 root 1.88 envv->push_back (0);
867 root 1.54
868     bool success;
869    
870     try
871     {
872 root 1.88 success = term->init (argv, envv);
873 root 1.54 }
874     catch (const class rxvt_failure_exception &e)
875     {
876     success = false;
877     }
878    
879     if (!success)
880     {
881     term->destroy ();
882     croak ("error while initializing new terminal instance");
883     }
884    
885     RETVAL = term && term->perl.self
886     ? newSVterm (term) : &PL_sv_undef;
887     }
888     OUTPUT:
889     RETVAL
890    
891 root 1.28 void
892     rxvt_term::destroy ()
893    
894 root 1.35 void
895 root 1.62 rxvt_term::set_should_invoke (int htype, int inc)
896     CODE:
897     THIS->perl.should_invoke [htype] += inc;
898    
899 root 1.102 int
900 root 1.95 rxvt_term::grab_button (int button, U32 modifiers, Window window = THIS->vt)
901 root 1.36 CODE:
902 root 1.102 RETVAL = XGrabButton (THIS->dpy, button, modifiers, window, 1,
903     ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
904     GrabModeSync, GrabModeSync, None, GRAB_CURSOR);
905     OUTPUT: RETVAL
906 root 1.36
907 root 1.102 int
908 root 1.95 rxvt_term::ungrab_button (int button, U32 modifiers, Window window = THIS->vt)
909     CODE:
910 root 1.102 RETVAL = XUngrabButton (THIS->dpy, button, modifiers, window);
911     OUTPUT: RETVAL
912 root 1.95
913 root 1.102 void
914     rxvt_term::XGrabKey (int keycode, U32 modifiers, Window window = THIS->vt, \
915     int owner_events = 1, int pointer_mode = GrabModeAsync, int keyboard_mode = GrabModeAsync)
916     CODE:
917     XGrabKey (THIS->dpy, keycode, modifiers, window, owner_events, pointer_mode, keyboard_mode);
918 root 1.95
919     void
920 root 1.102 rxvt_term::XUngrabKey (int keycode, U32 modifiers, Window window = THIS->vt)
921     CODE:
922     XUngrabKey (THIS->dpy, keycode, modifiers, window);
923 root 1.95
924     void
925 root 1.102 rxvt_term::XUngrabKeyboard (Time eventtime)
926     CODE:
927     XUngrabKeyboard (THIS->dpy, eventtime);
928 root 1.95
929 root 1.36 bool
930 root 1.79 rxvt_term::grab (Time eventtime, int sync = 0)
931 root 1.35 CODE:
932     {
933 root 1.36 int mode = sync ? GrabModeSync : GrabModeAsync;
934    
935     THIS->perl.grabtime = 0;
936    
937 root 1.96 if (!XGrabPointer (THIS->dpy, THIS->vt, 0,
938 root 1.36 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
939 root 1.41 mode, mode, None, GRAB_CURSOR, eventtime))
940 root 1.96 if (!XGrabKeyboard (THIS->dpy, THIS->vt, 0, mode, mode, eventtime))
941 root 1.36 THIS->perl.grabtime = eventtime;
942     else
943 root 1.96 XUngrabPointer (THIS->dpy, eventtime);
944 root 1.36
945     RETVAL = !!THIS->perl.grabtime;
946 root 1.35 }
947 root 1.36 OUTPUT:
948     RETVAL
949    
950     void
951 root 1.44 rxvt_term::allow_events_async ()
952 root 1.36 CODE:
953 root 1.96 XAllowEvents (THIS->dpy, AsyncBoth, THIS->perl.grabtime);
954 root 1.36
955     void
956 root 1.44 rxvt_term::allow_events_sync ()
957 root 1.36 CODE:
958 root 1.96 XAllowEvents (THIS->dpy, SyncBoth, THIS->perl.grabtime);
959 root 1.36
960     void
961 root 1.44 rxvt_term::allow_events_replay ()
962 root 1.36 CODE:
963 root 1.96 XAllowEvents (THIS->dpy, ReplayPointer, THIS->perl.grabtime);
964     XAllowEvents (THIS->dpy, ReplayKeyboard, THIS->perl.grabtime);
965 root 1.36
966     void
967 root 1.44 rxvt_term::ungrab ()
968 root 1.36 CODE:
969 root 1.45 ungrab (THIS);
970 root 1.35
971 root 1.1 int
972 root 1.102 rxvt_term::XStringToKeysym (char *string)
973     CODE:
974     RETVAL = XStringToKeysym (string);
975     OUTPUT: RETVAL
976    
977     char *
978     rxvt_term::XKeysymToString (int sym)
979     CODE:
980     RETVAL = XKeysymToString (sym);
981     OUTPUT: RETVAL
982    
983     int
984     rxvt_term::XKeysymToKeycode (int sym)
985     CODE:
986     RETVAL = XKeysymToKeycode (THIS->dpy, sym);
987     OUTPUT: RETVAL
988    
989     int
990     rxvt_term::XKeycodeToKeysym (int code, int index)
991     CODE:
992     RETVAL = XKeycodeToKeysym (THIS->dpy, code, index);
993     OUTPUT: RETVAL
994    
995     int
996 root 1.3 rxvt_term::strwidth (SV *str)
997 root 1.1 CODE:
998     {
999     wchar_t *wstr = sv2wcs (str);
1000 root 1.3
1001     rxvt_push_locale (THIS->locale);
1002 root 1.105 RETVAL = 0;
1003     for (wchar_t *wc = wstr; *wc; wc++)
1004     {
1005     int w = WCWIDTH (*wc);
1006    
1007     if (w)
1008 root 1.107 RETVAL += max (w, 1);
1009 root 1.105 }
1010 root 1.3 rxvt_pop_locale ();
1011    
1012 root 1.1 free (wstr);
1013     }
1014     OUTPUT:
1015     RETVAL
1016    
1017 root 1.3 SV *
1018     rxvt_term::locale_encode (SV *str)
1019 root 1.1 CODE:
1020 root 1.3 {
1021     wchar_t *wstr = sv2wcs (str);
1022    
1023     rxvt_push_locale (THIS->locale);
1024     char *mbstr = rxvt_wcstombs (wstr);
1025     rxvt_pop_locale ();
1026    
1027     free (wstr);
1028    
1029 root 1.71 RETVAL = newSVpv (mbstr, 0);
1030 root 1.3 free (mbstr);
1031     }
1032     OUTPUT:
1033 root 1.1 RETVAL
1034    
1035 root 1.3 SV *
1036     rxvt_term::locale_decode (SV *octets)
1037     CODE:
1038     {
1039     STRLEN len;
1040     char *data = SvPVbyte (octets, len);
1041    
1042     rxvt_push_locale (THIS->locale);
1043     wchar_t *wstr = rxvt_mbstowcs (data, len);
1044     rxvt_pop_locale ();
1045    
1046 root 1.71 RETVAL = wcs2sv (wstr);
1047 root 1.3 free (wstr);
1048     }
1049     OUTPUT:
1050     RETVAL
1051 root 1.1
1052 root 1.106 char *
1053     rxvt_term::locale ()
1054     CODE:
1055     RETVAL = THIS->locale;
1056     OUTPUT:
1057     RETVAL
1058    
1059 root 1.38 #define TERM_OFFSET(sym) offsetof (TermWin_t, sym)
1060 root 1.24
1061     #define TERM_OFFSET_width TERM_OFFSET(width)
1062     #define TERM_OFFSET_height TERM_OFFSET(height)
1063     #define TERM_OFFSET_fwidth TERM_OFFSET(fwidth)
1064     #define TERM_OFFSET_fheight TERM_OFFSET(fheight)
1065     #define TERM_OFFSET_fbase TERM_OFFSET(fbase)
1066     #define TERM_OFFSET_nrow TERM_OFFSET(nrow)
1067     #define TERM_OFFSET_ncol TERM_OFFSET(ncol)
1068     #define TERM_OFFSET_focus TERM_OFFSET(focus)
1069     #define TERM_OFFSET_mapped TERM_OFFSET(mapped)
1070 root 1.80 #define TERM_OFFSET_int_bwidth TERM_OFFSET(int_bwidth)
1071     #define TERM_OFFSET_ext_bwidth TERM_OFFSET(ext_bwidth)
1072     #define TERM_OFFSET_lineSpace TERM_OFFSET(lineSpace)
1073 root 1.24 #define TERM_OFFSET_saveLines TERM_OFFSET(saveLines)
1074     #define TERM_OFFSET_total_rows TERM_OFFSET(total_rows)
1075 root 1.67 #define TERM_OFFSET_top_row TERM_OFFSET(top_row)
1076 root 1.24
1077     int
1078     rxvt_term::width ()
1079     ALIAS:
1080     width = TERM_OFFSET_width
1081     height = TERM_OFFSET_height
1082     fwidth = TERM_OFFSET_fwidth
1083     fheight = TERM_OFFSET_fheight
1084     fbase = TERM_OFFSET_fbase
1085     nrow = TERM_OFFSET_nrow
1086     ncol = TERM_OFFSET_ncol
1087     focus = TERM_OFFSET_focus
1088     mapped = TERM_OFFSET_mapped
1089 root 1.80 int_bwidth = TERM_OFFSET_int_bwidth
1090     ext_bwidth = TERM_OFFSET_ext_bwidth
1091     lineSpace = TERM_OFFSET_lineSpace
1092 root 1.24 saveLines = TERM_OFFSET_saveLines
1093     total_rows = TERM_OFFSET_total_rows
1094 root 1.67 top_row = TERM_OFFSET_top_row
1095 root 1.24 CODE:
1096     RETVAL = *(int *)((char *)THIS + ix);
1097     OUTPUT:
1098     RETVAL
1099    
1100 root 1.38 unsigned int
1101     rxvt_term::ModLevel3Mask ()
1102     ALIAS:
1103     ModLevel3Mask = 0
1104     ModMetaMask = 1
1105     ModNumLockMask = 2
1106 root 1.86 current_screen = 3
1107 root 1.87 hidden_cursor = 4
1108 root 1.38 CODE:
1109     switch (ix)
1110     {
1111 root 1.87 case 0: RETVAL = THIS->ModLevel3Mask; break;
1112     case 1: RETVAL = THIS->ModMetaMask; break;
1113     case 2: RETVAL = THIS->ModNumLockMask; break;
1114     case 3: RETVAL = THIS->current_screen; break;
1115     case 4: RETVAL = THIS->hidden_cursor; break;
1116 root 1.38 }
1117     OUTPUT:
1118     RETVAL
1119    
1120 root 1.48 char *
1121     rxvt_term::display_id ()
1122 root 1.49 ALIAS:
1123     display_id = 0
1124     locale = 1
1125 root 1.48 CODE:
1126 root 1.49 switch (ix)
1127     {
1128     case 0: RETVAL = THIS->display->id; break;
1129     case 1: RETVAL = THIS->locale; break;
1130     }
1131 root 1.48 OUTPUT:
1132     RETVAL
1133    
1134 root 1.54 SV *
1135 root 1.100 rxvt_term::envv ()
1136     ALIAS:
1137     argv = 1
1138     PPCODE:
1139 root 1.54 {
1140 root 1.100 stringvec *vec = ix ? THIS->argv : THIS->envv;
1141 root 1.54
1142 root 1.100 EXTEND (SP, vec->size ());
1143 root 1.54
1144 root 1.100 for (char **i = vec->begin (); i != vec->end (); ++i)
1145     if (*i)
1146     PUSHs (sv_2mortal (newSVpv (*i, 0)));
1147 root 1.54 }
1148    
1149 root 1.50 int
1150     rxvt_term::pty_ev_events (int events = EVENT_UNDEF)
1151     CODE:
1152     RETVAL = THIS->pty_ev.events;
1153     if (events != EVENT_UNDEF)
1154     THIS->pty_ev.set (events);
1155     OUTPUT:
1156     RETVAL
1157    
1158 root 1.90 int
1159     rxvt_term::pty_fd ()
1160     CODE:
1161     RETVAL = THIS->pty->pty;
1162     OUTPUT:
1163     RETVAL
1164    
1165 root 1.79 Window
1166 root 1.31 rxvt_term::parent ()
1167     CODE:
1168 root 1.79 RETVAL = THIS->parent [0];
1169 root 1.31 OUTPUT:
1170     RETVAL
1171    
1172 root 1.79 Window
1173 root 1.30 rxvt_term::vt ()
1174     CODE:
1175 root 1.79 RETVAL = THIS->vt;
1176 root 1.30 OUTPUT:
1177     RETVAL
1178    
1179 root 1.62 void
1180     rxvt_term::vt_emask_add (U32 emask)
1181     CODE:
1182     THIS->vt_emask_perl |= emask;
1183     THIS->vt_select_input ();
1184    
1185 root 1.30 U32
1186 root 1.25 rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
1187 root 1.7 CODE:
1188 root 1.25 RETVAL = THIS->rstyle;
1189     THIS->rstyle = new_rstyle;
1190 root 1.7 OUTPUT:
1191 root 1.24 RETVAL
1192 root 1.7
1193     int
1194 root 1.67 rxvt_term::view_start (int newval = 1)
1195 root 1.61 PROTOTYPE: $;$
1196 root 1.7 CODE:
1197     {
1198     RETVAL = THIS->view_start;
1199    
1200 root 1.67 if (newval <= 0)
1201     THIS->scr_changeview (max (newval, THIS->top_row));
1202 root 1.7 }
1203     OUTPUT:
1204     RETVAL
1205    
1206 root 1.8 void
1207 root 1.83 rxvt_term::focus_in ()
1208    
1209     void
1210     rxvt_term::focus_out ()
1211    
1212     void
1213 root 1.98 rxvt_term::key_press (unsigned int state, unsigned int keycode, Time time = CurrentTime)
1214     ALIAS:
1215     key_release = 1
1216     CODE:
1217     {
1218     XKeyEvent xkey;
1219    
1220     memset (&xkey, 0, sizeof (xkey));
1221    
1222     xkey.time = time;
1223     xkey.state = state;
1224     xkey.keycode = keycode;
1225    
1226     xkey.type = ix ? KeyRelease : KeyPress;
1227     xkey.display = THIS->dpy;
1228     xkey.window = THIS->vt;
1229     xkey.root = THIS->display->root;
1230     xkey.subwindow = THIS->vt;
1231    
1232     if (ix)
1233     THIS->key_release (xkey);
1234     else
1235     THIS->key_press (xkey);
1236     }
1237    
1238     void
1239 root 1.9 rxvt_term::want_refresh ()
1240     CODE:
1241     THIS->want_refresh = 1;
1242    
1243     void
1244 root 1.27 rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1245 root 1.8 PPCODE:
1246     {
1247 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1248 root 1.19 XSRETURN_EMPTY;
1249 root 1.8
1250     line_t &l = ROW(row_number);
1251    
1252     if (GIMME_V != G_VOID)
1253     {
1254 root 1.91 wchar_t *wstr = rxvt_temp_buf<wchar_t> (THIS->ncol);
1255 root 1.8
1256 root 1.33 for (int col = 0; col < THIS->ncol; col++)
1257 root 1.8 wstr [col] = l.t [col];
1258    
1259 root 1.71 XPUSHs (sv_2mortal (wcs2sv (wstr, THIS->ncol)));
1260 root 1.8 }
1261    
1262     if (new_text)
1263     {
1264 root 1.13 wchar_t *wstr = sv2wcs (new_text);
1265 root 1.8
1266 root 1.27 int len = min (wcslen (wstr) - start_ofs, max_len);
1267 root 1.8
1268 root 1.83 if (start_col < 0 || start_col + len > THIS->ncol)
1269 root 1.8 {
1270     free (wstr);
1271 root 1.10 croak ("new_text extends beyond horizontal margins");
1272 root 1.8 }
1273    
1274     for (int col = start_col; col < start_col + len; col++)
1275     {
1276 root 1.27 l.t [col] = wstr [start_ofs + col - start_col];
1277 root 1.8 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
1278     }
1279 root 1.9
1280     free (wstr);
1281 root 1.8 }
1282     }
1283    
1284     void
1285 root 1.27 rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1286 root 1.8 PPCODE:
1287     {
1288 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1289 root 1.19 XSRETURN_EMPTY;
1290 root 1.8
1291     line_t &l = ROW(row_number);
1292    
1293     if (GIMME_V != G_VOID)
1294     {
1295     AV *av = newAV ();
1296    
1297     av_extend (av, THIS->ncol - 1);
1298     for (int col = 0; col < THIS->ncol; col++)
1299     av_store (av, col, newSViv (l.r [col]));
1300    
1301     XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1302     }
1303    
1304     if (new_rend)
1305     {
1306     if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
1307     croak ("new_rend must be arrayref");
1308    
1309     AV *av = (AV *)SvRV (new_rend);
1310 root 1.76 int len = min (AvFILL (av) + 1 - start_ofs, max_len);
1311 root 1.8
1312 root 1.83 if (start_col < 0 || start_col + len > THIS->ncol)
1313 root 1.10 croak ("new_rend array extends beyond horizontal margins");
1314 root 1.8
1315     for (int col = start_col; col < start_col + len; col++)
1316     {
1317 root 1.27 rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask;
1318 root 1.8
1319     l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
1320     }
1321     }
1322     }
1323    
1324     int
1325 root 1.26 rxvt_term::ROW_l (int row_number, int new_length = -1)
1326 root 1.8 CODE:
1327     {
1328 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1329 root 1.19 XSRETURN_EMPTY;
1330 root 1.8
1331     line_t &l = ROW(row_number);
1332 root 1.26 RETVAL = l.l;
1333 root 1.8
1334 root 1.26 if (new_length >= 0)
1335 root 1.8 l.l = new_length;
1336     }
1337     OUTPUT:
1338     RETVAL
1339    
1340 root 1.19 bool
1341 root 1.26 rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
1342 root 1.19 CODE:
1343     {
1344 root 1.67 if (!IN_RANGE_EXC (row_number, THIS->top_row, THIS->nrow))
1345 root 1.19 XSRETURN_EMPTY;
1346    
1347     line_t &l = ROW(row_number);
1348 root 1.26 RETVAL = l.is_longer ();
1349    
1350     if (new_is_longer >= 0)
1351     l.is_longer (new_is_longer);
1352 root 1.19 }
1353     OUTPUT:
1354     RETVAL
1355    
1356 root 1.8 SV *
1357 root 1.33 rxvt_term::special_encode (SV *string)
1358 root 1.8 CODE:
1359 root 1.33 {
1360     wchar_t *wstr = sv2wcs (string);
1361     int wlen = wcslen (wstr);
1362 root 1.91 wchar_t *rstr = rxvt_temp_buf<wchar_t> (wlen * 2); // cannot become longer
1363 root 1.33
1364     rxvt_push_locale (THIS->locale);
1365    
1366     wchar_t *r = rstr;
1367     for (wchar_t *s = wstr; *s; s++)
1368 root 1.91 {
1369     int w = WCWIDTH (*s);
1370    
1371     if (w == 0)
1372     {
1373     if (r == rstr)
1374     croak ("leading combining character unencodable");
1375 root 1.33
1376 root 1.91 unicode_t n = rxvt_compose (r[-1], *s);
1377     if (n == NOCHAR)
1378     n = rxvt_composite.compose (r[-1], *s);
1379    
1380     r[-1] = n;
1381     }
1382 root 1.33 #if !UNICODE_3
1383 root 1.91 else if (*s >= 0x10000)
1384     *r++ = rxvt_composite.compose (*s);
1385 root 1.33 #endif
1386 root 1.91 else
1387     *r++ = *s;
1388    
1389     // the *2 above only allows wcwidth <= 2
1390     if (w > 1)
1391     *r++ = NOCHAR;
1392     }
1393 root 1.33
1394     rxvt_pop_locale ();
1395    
1396 root 1.71 RETVAL = wcs2sv (rstr, r - rstr);
1397 root 1.33 }
1398     OUTPUT:
1399     RETVAL
1400 root 1.8
1401     SV *
1402 root 1.33 rxvt_term::special_decode (SV *text)
1403 root 1.8 CODE:
1404 root 1.33 {
1405     wchar_t *wstr = sv2wcs (text);
1406     int wlen = wcslen (wstr);
1407     int dlen = 0;
1408    
1409     // find length
1410     for (wchar_t *s = wstr; *s; s++)
1411     if (*s == NOCHAR)
1412     ;
1413     else if (IS_COMPOSE (*s))
1414     dlen += rxvt_composite.expand (*s, 0);
1415     else
1416     dlen++;
1417    
1418 root 1.91 wchar_t *rstr = rxvt_temp_buf<wchar_t> (dlen);
1419 root 1.33
1420     // decode
1421     wchar_t *r = rstr;
1422     for (wchar_t *s = wstr; *s; s++)
1423     if (*s == NOCHAR)
1424     ;
1425     else if (IS_COMPOSE (*s))
1426     r += rxvt_composite.expand (*s, r);
1427     else
1428     *r++ = *s;
1429    
1430 root 1.71 RETVAL = wcs2sv (rstr, r - rstr);
1431 root 1.33 }
1432     OUTPUT:
1433     RETVAL
1434 root 1.8
1435 root 1.1 void
1436 root 1.2 rxvt_term::_resource (char *name, int index, SV *newval = 0)
1437     PPCODE:
1438     {
1439 root 1.97 static const struct resval { const char *name; int value; } *rs, rslist [] = {
1440 root 1.21 # define def(name) { # name, Rs_ ## name },
1441     # define reserve(name,count)
1442 root 1.2 # include "rsinc.h"
1443 root 1.21 # undef def
1444     # undef reserve
1445 root 1.2 };
1446    
1447 root 1.97 rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
1448 root 1.2
1449     do {
1450     if (rs-- == rslist)
1451     croak ("no such resource '%s', requested", name);
1452     } while (strcmp (name, rs->name));
1453    
1454     index += rs->value;
1455    
1456     if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
1457     croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
1458    
1459     if (GIMME_V != G_VOID)
1460 root 1.71 XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
1461 root 1.2
1462     if (newval)
1463     {
1464     if (SvOK (newval))
1465     {
1466     char *str = strdup (SvPVbyte_nolen (newval));
1467     THIS->rs [index] = str;
1468     THIS->allocated.push_back (str);
1469     }
1470     else
1471     THIS->rs [index] = 0;
1472     }
1473     }
1474    
1475 root 1.55 const char *
1476     rxvt_term::x_resource (const char *name)
1477    
1478 root 1.40 bool
1479     rxvt_term::option (U32 optval, int set = -1)
1480     CODE:
1481     {
1482     RETVAL = THIS->options & optval;
1483    
1484     if (set >= 0)
1485     {
1486     if (set)
1487     THIS->options |= optval;
1488     else
1489     THIS->options &= ~optval;
1490    
1491 root 1.78 if (THIS->check_ev.is_active ()) // avoid doing this before START
1492     switch (optval)
1493     {
1494     case Opt_skipBuiltinGlyphs:
1495     THIS->set_fonts ();
1496     THIS->scr_remap_chars ();
1497     THIS->scr_touch (true);
1498     THIS->want_refresh = 1;
1499     break;
1500    
1501     case Opt_cursorUnderline:
1502     THIS->want_refresh = 1;
1503     break;
1504 root 1.40
1505     # case Opt_scrollBar_floating:
1506     # case Opt_scrollBar_right:
1507     # THIS->resize_all_windows (THIS->width, THIS->height, 1);
1508     # break;
1509 root 1.78 }
1510 root 1.40 }
1511     }
1512     OUTPUT:
1513     RETVAL
1514    
1515 root 1.50 bool
1516     rxvt_term::parse_keysym (char *keysym, char *str)
1517     CODE:
1518     RETVAL = 0 < THIS->parse_keysym (keysym, str);
1519     THIS->keyboard->register_done ();
1520     OUTPUT:
1521     RETVAL
1522    
1523 root 1.2 void
1524 root 1.46 rxvt_term::screen_cur (...)
1525 root 1.1 PROTOTYPE: $;$$
1526     ALIAS:
1527 root 1.24 screen_cur = 0
1528     selection_beg = 1
1529     selection_end = 2
1530     selection_mark = 3
1531 root 1.1 PPCODE:
1532     {
1533 root 1.24 row_col_t &rc = ix == 0 ? THIS->screen.cur
1534     : ix == 1 ? THIS->selection.beg
1535     : ix == 2 ? THIS->selection.end
1536     : THIS->selection.mark;
1537 root 1.1
1538     if (GIMME_V != G_VOID)
1539     {
1540     EXTEND (SP, 2);
1541 root 1.24 PUSHs (sv_2mortal (newSViv (rc.row)));
1542     PUSHs (sv_2mortal (newSViv (rc.col)));
1543 root 1.1 }
1544    
1545     if (items == 3)
1546     {
1547 root 1.73 rc.row = SvIV (ST (1));
1548     rc.col = SvIV (ST (2));
1549    
1550 root 1.89 if (ix == 2)
1551 root 1.73 {
1552 root 1.89 if (rc.col == 0)
1553     {
1554     // col == 0 means end of previous line
1555     rc.row--;
1556     rc.col = THIS->ncol;
1557     }
1558     else if (IN_RANGE_EXC (rc.row, THIS->top_row, THIS->nrow)
1559     && rc.col > ROW(rc.row).l)
1560     {
1561     // col >= length means while line and add newline
1562     rc.col = THIS->ncol;
1563     }
1564 root 1.73 }
1565    
1566     clamp_it (rc.col, 0, THIS->ncol);
1567     clamp_it (rc.row, THIS->top_row, THIS->nrow - 1);
1568 root 1.1
1569     if (ix)
1570     THIS->want_refresh = 1;
1571     }
1572     }
1573    
1574 root 1.53 char
1575     rxvt_term::cur_charset ()
1576     CODE:
1577     RETVAL = THIS->charsets [THIS->screen.charset];
1578     OUTPUT:
1579     RETVAL
1580    
1581 root 1.93 void
1582     rxvt_term::selection_clear ()
1583 root 1.60
1584     void
1585 root 1.79 rxvt_term::selection_make (Time eventtime, bool rect = false)
1586 root 1.60 CODE:
1587     THIS->selection.op = SELECTION_CONT;
1588     THIS->selection.rect = rect;
1589     THIS->selection_make (eventtime);
1590    
1591 root 1.1 int
1592 root 1.79 rxvt_term::selection_grab (Time eventtime)
1593 root 1.1
1594     void
1595     rxvt_term::selection (SV *newtext = 0)
1596     PPCODE:
1597     {
1598     if (GIMME_V != G_VOID)
1599 root 1.45 XPUSHs (THIS->selection.text
1600 root 1.71 ? sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len))
1601 root 1.45 : &PL_sv_undef);
1602 root 1.1
1603     if (newtext)
1604     {
1605     free (THIS->selection.text);
1606    
1607     THIS->selection.text = sv2wcs (newtext);
1608     THIS->selection.len = wcslen (THIS->selection.text);
1609     }
1610     }
1611 root 1.24
1612 root 1.1 void
1613 root 1.51 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)
1614    
1615     void
1616     rxvt_term::scr_xor_span (int beg_row, int beg_col, int end_row, int end_col, U32 rstyle = RS_RVid)
1617    
1618     void
1619 root 1.50 rxvt_term::scr_bell ()
1620    
1621     void
1622 root 1.86 rxvt_term::scr_change_screen (int screen)
1623    
1624     void
1625 root 1.25 rxvt_term::scr_add_lines (SV *string)
1626     CODE:
1627     {
1628     wchar_t *wstr = sv2wcs (string);
1629 root 1.32 THIS->scr_add_lines (wstr, wcslen (wstr));
1630 root 1.25 free (wstr);
1631     }
1632    
1633     void
1634 root 1.13 rxvt_term::tt_write (SV *octets)
1635     INIT:
1636     STRLEN len;
1637     char *str = SvPVbyte (octets, len);
1638     C_ARGS:
1639 root 1.23 str, len
1640 root 1.13
1641 root 1.28 void
1642     rxvt_term::cmd_parse (SV *octets)
1643     CODE:
1644     {
1645     STRLEN len;
1646     char *str = SvPVbyte (octets, len);
1647    
1648     char *old_cmdbuf_ptr = THIS->cmdbuf_ptr;
1649     char *old_cmdbuf_endp = THIS->cmdbuf_endp;
1650    
1651     THIS->cmdbuf_ptr = str;
1652     THIS->cmdbuf_endp = str + len;
1653    
1654 root 1.35 rxvt_push_locale (THIS->locale);
1655 root 1.28 THIS->cmd_parse ();
1656 root 1.35 rxvt_pop_locale ();
1657 root 1.28
1658     THIS->cmdbuf_ptr = old_cmdbuf_ptr;
1659     THIS->cmdbuf_endp = old_cmdbuf_endp;
1660     }
1661    
1662 root 1.13 SV *
1663     rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
1664     CODE:
1665     {
1666     overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
1667     RETVAL = newSVptr ((void *)o, "urxvt::overlay");
1668     o->self = (HV *)SvRV (RETVAL);
1669     }
1670     OUTPUT:
1671     RETVAL
1672    
1673 root 1.79 #############################################################################
1674     # Various X Utility Functions
1675     #############################################################################
1676    
1677     void
1678     rxvt_term::XListProperties (Window window)
1679     PPCODE:
1680     {
1681     int count;
1682 root 1.96 Atom *props = XListProperties (THIS->dpy, window, &count);
1683 root 1.79
1684     EXTEND (SP, count);
1685     while (count--)
1686     PUSHs (newSVuv ((U32)props [count]));
1687    
1688     XFree (props);
1689     }
1690    
1691     void
1692     rxvt_term::XGetWindowProperty (Window window, Atom property)
1693     PPCODE:
1694     {
1695     Atom type;
1696     int format;
1697     unsigned long nitems;
1698     unsigned long bytes_after;
1699     unsigned char *prop;
1700 root 1.81
1701 root 1.96 XGetWindowProperty (THIS->dpy, window, property,
1702 root 1.81 0, 1<<24, 0, AnyPropertyType,
1703 root 1.79 &type, &format, &nitems, &bytes_after, &prop);
1704 root 1.81
1705 root 1.79 if (type != None)
1706     {
1707 root 1.81 int elemsize = format == 16 ? sizeof (short)
1708     : format == 32 ? sizeof (long)
1709     : 1;
1710    
1711 root 1.79 EXTEND (SP, 3);
1712     PUSHs (newSVuv ((U32)type));
1713     PUSHs (newSViv (format));
1714 root 1.81 PUSHs (newSVpvn ((char *)prop, nitems * elemsize));
1715 root 1.79 XFree (prop);
1716     }
1717     }
1718    
1719     void
1720     rxvt_term::XChangeWindowProperty (Window window, Atom property, Atom type, int format, SV *data)
1721     CODE:
1722     {
1723     STRLEN len;
1724     char *data_ = SvPVbyte (data, len);
1725    
1726 root 1.81 int elemsize = format == 16 ? sizeof (short)
1727     : format == 32 ? sizeof (long)
1728     : 1;
1729    
1730 root 1.96 XChangeProperty (THIS->dpy, window, property,
1731 root 1.79 type, format, PropModeReplace,
1732 root 1.81 (unsigned char *)data_, len / elemsize);
1733 root 1.79 }
1734    
1735 root 1.81 Atom
1736     XInternAtom (rxvt_term *term, char *atom_name, int only_if_exists = FALSE)
1737 root 1.96 C_ARGS: term->dpy, atom_name, only_if_exists
1738 root 1.81
1739     char *
1740     XGetAtomName (rxvt_term *term, Atom atom)
1741 root 1.96 C_ARGS: term->dpy, atom
1742 root 1.81 CLEANUP:
1743     XFree (RETVAL);
1744    
1745 root 1.79 void
1746     XDeleteProperty (rxvt_term *term, Window window, Atom property)
1747 root 1.96 C_ARGS: term->dpy, window, property
1748 root 1.79
1749     Window
1750     rxvt_term::DefaultRootWindow ()
1751     CODE:
1752     RETVAL = THIS->display->root;
1753     OUTPUT:
1754     RETVAL
1755    
1756 root 1.81 #if 0
1757    
1758 root 1.79 Window
1759     XCreateSimpleWindow (rxvt_term *term, Window parent, int x, int y, unsigned int width, unsigned int height)
1760 root 1.96 C_ARGS: term->dpy, (Window)parent,
1761 root 1.79 x, y, width, height, 0,
1762     term->pix_colors_focused[Color_border],
1763     term->pix_colors_focused[Color_border]
1764    
1765 root 1.81 #endif
1766    
1767 root 1.79 void
1768     XReparentWindow (rxvt_term *term, Window window, Window parent, int x = 0, int y = 0)
1769 root 1.96 C_ARGS: term->dpy, window, parent, x, y
1770 root 1.79
1771     void
1772     XMapWindow (rxvt_term *term, Window window)
1773 root 1.96 C_ARGS: term->dpy, window
1774 root 1.79
1775     void
1776     XUnmapWindow (rxvt_term *term, Window window)
1777 root 1.96 C_ARGS: term->dpy, window
1778 root 1.79
1779     void
1780     XMoveResizeWindow (rxvt_term *term, Window window, int x, int y, unsigned int width, unsigned int height)
1781 root 1.96 C_ARGS: term->dpy, window, x, y, width, height
1782 root 1.84
1783     void
1784     rxvt_term::XChangeInput (Window window, U32 add_events, U32 del_events = 0)
1785     CODE:
1786     {
1787     XWindowAttributes attr;
1788 root 1.96 XGetWindowAttributes (THIS->dpy, window, &attr);
1789     XSelectInput (THIS->dpy, window, attr.your_event_mask | add_events & ~del_events);
1790 root 1.84 }
1791 root 1.79
1792 root 1.82 void
1793     rxvt_term::XTranslateCoordinates (Window src, Window dst, int x, int y)
1794     PPCODE:
1795     {
1796     int dx, dy;
1797     Window child;
1798    
1799 root 1.96 if (XTranslateCoordinates (THIS->dpy, src, dst, x, y, &dx, &dy, &child))
1800 root 1.82 {
1801     EXTEND (SP, 3);
1802     PUSHs (newSViv (dx));
1803     PUSHs (newSViv (dy));
1804     PUSHs (newSVuv (child));
1805     }
1806     }
1807    
1808 root 1.79 #############################################################################
1809     # urxvt::overlay
1810     #############################################################################
1811    
1812 root 1.13 MODULE = urxvt PACKAGE = urxvt::overlay
1813 root 1.1
1814     void
1815 root 1.13 overlay::set (int x, int y, SV *text, SV *rend = 0)
1816 root 1.1
1817     void
1818 root 1.18 overlay::show ()
1819    
1820     void
1821     overlay::hide ()
1822    
1823     void
1824 root 1.13 overlay::DESTROY ()
1825 root 1.1
1826 root 1.109 INCLUDE: $PERL <iom_perl.xs -pe s/IOM_MODULE/urxvt/g,s/IOM_CLASS/urxvt/g |
1827 root 1.1