ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
(Generate patch)

Comparing rxvt-unicode/src/rxvtperl.xs (file contents):
Revision 1.4 by root, Mon Jan 2 19:05:05 2006 UTC vs.
Revision 1.49 by root, Mon Jan 9 19:25:57 2006 UTC

1/*----------------------------------------------------------------------* 1/*----------------------------------------------------------------------*
2 * File: rxvtperl.xs 2 * File: rxvtperl.xs
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * 4 *
5 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-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.
26#include <XSUB.h> 26#include <XSUB.h>
27#undef line_t 27#undef line_t
28 28
29#include "../config.h" 29#include "../config.h"
30 30
31#include <cstddef>
31#include <cstdarg> 32#include <cstdarg>
32 33
33#include "rxvt.h" 34#include "rxvt.h"
34#include "iom.h" 35#include "iom.h"
35#include "rxvtutil.h" 36#include "rxvtutil.h"
36#include "rxvtperl.h" 37#include "rxvtperl.h"
37 38
38#include "perlxsi.c" 39#include "perlxsi.c"
39 40
41#if defined(HAVE_SCROLLBARS) || defined(MENUBAR)
42# define GRAB_CURSOR THIS->leftptr_cursor
43#else
44# define GRAB_CURSOR None
45#endif
46
47#undef LINENO
48#define LINENO(n) MOD (THIS->term_start + int(n), THIS->total_rows)
49#undef ROW
50#define ROW(n) THIS->row_buf [LINENO (n)]
51
40///////////////////////////////////////////////////////////////////////////// 52/////////////////////////////////////////////////////////////////////////////
53
54static SV *
55taint (SV *sv)
56{
57 SvTAINT (sv);
58 return sv;
59}
60
61static SV *
62taint_if (SV *sv, SV *src)
63{
64 if (SvTAINTED (src))
65 SvTAINT (sv);
66
67 return sv;
68}
41 69
42static wchar_t * 70static wchar_t *
43sv2wcs (SV *sv) 71sv2wcs (SV *sv)
44{ 72{
45 STRLEN len; 73 STRLEN len;
46 char *str = SvPVutf8 (sv, len); 74 char *str = SvPVutf8 (sv, len);
47 return rxvt_utf8towcs (str, len); 75 return rxvt_utf8towcs (str, len);
48} 76}
49 77
50static SV * 78static SV *
79wcs2sv (wchar_t *wstr, int len = -1)
80{
81 char *str = rxvt_wcstoutf8 (wstr, len);
82
83 SV *sv = newSVpv (str, 0);
84 SvUTF8_on (sv);
85 free (str);
86
87 return sv;
88}
89
90static SV *
51new_ref (HV *hv, const char *klass) 91new_ref (HV *hv, const char *klass)
52{ 92{
53 return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1)); 93 return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
54} 94}
55 95
56//TODO: use magic 96//TODO: use magic
57static SV * 97static SV *
58newSVptr (void *ptr, const char *klass) 98newSVptr (void *ptr, const char *klass)
59{ 99{
60 HV *hv = newHV (); 100 HV *hv = newHV ();
61 hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0); 101 sv_magic ((SV *)hv, 0, PERL_MAGIC_ext, (char *)ptr, 0);
62 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 102 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
103}
104
105static void
106clearSVptr (SV *sv)
107{
108 if (SvROK (sv))
109 sv = SvRV (sv);
110
111 hv_clear ((HV *)sv);
112 sv_unmagic (sv, PERL_MAGIC_ext);
63} 113}
64 114
65static long 115static long
66SvPTR (SV *sv, const char *klass) 116SvPTR (SV *sv, const char *klass)
67{ 117{
68 if (!sv_derived_from (sv, klass)) 118 if (!sv_derived_from (sv, klass))
69 croak ("object of type %s expected", klass); 119 croak ("object of type %s expected", klass);
70 120
71 IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1)); 121 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
72 122
73 if (!iv) 123 if (!mg)
74 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass); 124 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
75 125
76 return (long)iv; 126 return (long)mg->mg_ptr;
77} 127}
78 128
79#define newSVterm(term) SvREFCNT_inc ((SV *)term->self) 129#define newSVterm(term) SvREFCNT_inc ((SV *)term->perl.self)
80#define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term") 130#define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
81 131
82///////////////////////////////////////////////////////////////////////////// 132/////////////////////////////////////////////////////////////////////////////
83 133
84struct perl_watcher 134struct perl_watcher
134#define newSVtimer(timer) new_ref (timer->self, "urxvt::timer") 184#define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
135#define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer") 185#define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
136 186
137struct timer : time_watcher, perl_watcher 187struct timer : time_watcher, perl_watcher
138{ 188{
189 tstamp interval;
190
139 timer () 191 timer ()
140 : time_watcher (this, &timer::execute) 192 : time_watcher (this, &timer::execute)
141 { 193 {
142 } 194 }
143 195
144 void execute (time_watcher &w) 196 void execute (time_watcher &w)
145 { 197 {
198 if (interval)
199 start (at + interval);
200
146 invoke ("urxvt::timer", newSVtimer (this)); 201 invoke ("urxvt::timer", newSVtimer (this));
147 } 202 }
148}; 203};
149 204
150#define newSViow(iow) new_ref (iow->self, "urxvt::iow") 205#define newSViow(iow) new_ref (iow->self, "urxvt::iow")
163 } 218 }
164}; 219};
165 220
166///////////////////////////////////////////////////////////////////////////// 221/////////////////////////////////////////////////////////////////////////////
167 222
223#define SvOVERLAY(sv) (overlay *)SvPTR (sv, "urxvt::overlay")
224
225struct overlay {
226 HV *self;
227 rxvt_term *THIS;
228 int x, y, w, h;
229 int border;
230 text_t **text;
231 rend_t **rend;
232
233 overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border);
234 ~overlay ();
235
236 void show ();
237 void hide ();
238
239 void swap ();
240
241 void set (int x, int y, SV *str, SV *rend);
242};
243
244overlay::overlay (rxvt_term *THIS, int x_, int y_, int w_, int h_, rend_t rstyle, int border)
245: THIS(THIS), x(x_), y(y_), w(w_), h(h_), border(border == 2)
246{
247 if (border == 2)
248 {
249 w += 2;
250 h += 2;
251 }
252
253 text = new text_t *[h];
254 rend = new rend_t *[h];
255
256 for (int y = 0; y < h; y++)
257 {
258 text_t *tp = text[y] = new text_t[w];
259 rend_t *rp = rend[y] = new rend_t[w];
260
261 text_t t0, t1, t2;
262 rend_t r = rstyle;
263
264 if (border == 2)
265 {
266 if (y == 0)
267 t0 = 0x2554, t1 = 0x2550, t2 = 0x2557;
268 else if (y < h - 1)
269 t0 = 0x2551, t1 = 0x0020, t2 = 0x2551;
270 else
271 t0 = 0x255a, t1 = 0x2550, t2 = 0x255d;
272
273 *tp++ = t0;
274 *rp++ = r;
275
276 for (int x = w - 2; x-- > 0; )
277 {
278 *tp++ = t1;
279 *rp++ = r;
280 }
281
282 *tp = t2;
283 *rp = r;
284 }
285 else
286 for (int x = w; x-- > 0; )
287 {
288 *tp++ = 0x0020;
289 *rp++ = r;
290 }
291 }
292
293 show ();
294 THIS->want_refresh = 1;
295}
296
297overlay::~overlay ()
298{
299 hide ();
300
301 for (int y = h; y--; )
302 {
303 delete [] text[y];
304 delete [] rend[y];
305 }
306
307 delete [] text;
308 delete [] rend;
309
310 THIS->want_refresh = 1;
311}
312
313void
314overlay::show ()
315{
316 char key[33]; sprintf (key, "%32lx", (long)this);
317
318 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0));
319 hv_store (hv, key, 32, newSViv ((long)this), 0);
320}
321
322void
323overlay::hide ()
324{
325 SV **ovs = hv_fetch ((HV *)SvRV ((SV *)THIS->perl.self), "_overlay", 8, 0);
326
327 if (ovs)
328 {
329 char key[33]; sprintf (key, "%32lx", (long)this);
330
331 HV *hv = (HV *)SvRV (*ovs);
332 hv_delete (hv, key, 32, G_DISCARD);
333 }
334}
335
336void overlay::swap ()
337{
338 int ov_x = max (0, min (MOD (x, THIS->ncol), THIS->ncol - w));
339 int ov_y = max (0, min (MOD (y, THIS->nrow), THIS->nrow - h));
340
341 int ov_w = min (w, THIS->ncol - ov_x);
342 int ov_h = min (h, THIS->nrow - ov_y);
343
344 for (int y = ov_h; y--; )
345 {
346 text_t *t1 = text [y];
347 rend_t *r1 = rend [y];
348
349 text_t *t2 = ROW(y + ov_y - THIS->view_start).t + ov_x;
350 rend_t *r2 = ROW(y + ov_y - THIS->view_start).r + ov_x;
351
352 for (int x = ov_w; x--; )
353 {
354 text_t t = *t1; *t1++ = *t2; *t2++ = t;
355 rend_t r = *r1; *r1++ = *r2; *r2++ = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (t));
356 }
357 }
358
359}
360
361void overlay::set (int x, int y, SV *text, SV *rend)
362{
363 x += border;
364 y += border;
365
366 if (!IN_RANGE_EXC (y, 0, h - border))
367 return;
368
369 wchar_t *wtext = sv2wcs (text);
370
371 for (int col = min (wcslen (wtext), w - x - border); col--; )
372 this->text [y][x + col] = wtext [col];
373
374 free (wtext);
375
376 if (rend)
377 {
378 if (!SvROK (rend) || SvTYPE (SvRV (rend)) != SVt_PVAV)
379 croak ("rend must be arrayref");
380
381 AV *av = (AV *)SvRV (rend);
382
383 for (int col = min (av_len (av) + 1, w - x - border); col--; )
384 this->rend [y][x + col] = SvIV (*av_fetch (av, col, 1));
385 }
386
387 THIS->want_refresh = 1;
388}
389
390
391/////////////////////////////////////////////////////////////////////////////
392
168struct rxvt_perl_interp rxvt_perl; 393struct rxvt_perl_interp rxvt_perl;
169 394
170static PerlInterpreter *perl; 395static PerlInterpreter *perl;
171 396
172rxvt_perl_interp::rxvt_perl_interp () 397rxvt_perl_interp::rxvt_perl_interp ()
187{ 412{
188 if (!perl) 413 if (!perl)
189 { 414 {
190 char *argv[] = { 415 char *argv[] = {
191 "", 416 "",
417 "-T",
192 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1", 418 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
193 }; 419 };
194 420
195 perl = perl_alloc (); 421 perl = perl_alloc ();
196 perl_construct (perl); 422 perl_construct (perl);
197 423
198 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL) 424 if (perl_parse (perl, xs_init, 3, argv, (char **)NULL)
199 || perl_run (perl)) 425 || perl_run (perl))
200 { 426 {
201 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n"); 427 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
202 428
203 perl_destruct (perl); 429 perl_destruct (perl);
205 perl = 0; 431 perl = 0;
206 } 432 }
207 } 433 }
208} 434}
209 435
436static void
437ungrab (rxvt_term *THIS)
438{
439 if (THIS->perl.grabtime)
440 {
441 XUngrabKeyboard (THIS->display->display, THIS->perl.grabtime);
442 XUngrabPointer (THIS->display->display, THIS->perl.grabtime);
443 THIS->perl.grabtime = 0;
444 }
445}
446
210bool 447bool
211rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) 448rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
212{ 449{
213 // INIT and DESTROY must be requested by the runtime 450 if (!perl)
214
215 if (!perl || !should_invoke [htype])
216 return false; 451 return false;
217 452
218 if (htype == HOOK_INIT) // first hook ever called 453 if (htype == HOOK_INIT) // first hook ever called
454 {
219 term->self = (void *)newSVptr ((void *)term, "urxvt::term"); 455 term->perl.self = (void *)newSVptr ((void *)term, "urxvt::term");
456 hv_store ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, newRV_noinc ((SV *)newHV ()), 0);
457 }
458 else if (!term->perl.self)
459 return false; // perl not initialized for this instance
460 else if (htype == HOOK_DESTROY)
461 {
462 // handled later
463 }
464 else if (htype == HOOK_REFRESH_BEGIN || htype == HOOK_REFRESH_END)
465 {
466 HV *hv = (HV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
467
468 if (HvKEYS (hv))
469 {
470 hv_iterinit (hv);
471
472 while (HE *he = hv_iternext (hv))
473 ((overlay *)SvIV (hv_iterval (hv, he)))->swap ();
474 }
475 }
476 else if (!should_invoke [htype])
477 return false;
220 478
221 dSP; 479 dSP;
222 va_list ap; 480 va_list ap;
223 481
224 va_start (ap, htype); 482 va_start (ap, htype);
242 500
243 case DT_LONG: 501 case DT_LONG:
244 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long)))); 502 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
245 break; 503 break;
246 504
505 case DT_STR:
506 XPUSHs (taint (sv_2mortal (newSVpv (va_arg (ap, char *), 0))));
507 break;
508
509 case DT_STR_LEN:
510 {
511 char *str = va_arg (ap, char *);
512 int len = va_arg (ap, int);
513
514 XPUSHs (taint (sv_2mortal (newSVpvn (str, len))));
515 }
516 break;
517
518 case DT_WCS_LEN:
519 {
520 wchar_t *wstr = va_arg (ap, wchar_t *);
521 int wlen = va_arg (ap, int);
522
523 XPUSHs (taint (sv_2mortal (wcs2sv (wstr, wlen))));
524 }
525 break;
526
527 case DT_XEVENT:
528 {
529 XEvent *xe = va_arg (ap, XEvent *);
530 HV *hv = newHV ();
531
532# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
533# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
534# undef set
535
536 setiv (type, xe->type);
537 setiv (send_event, xe->xany.send_event);
538 setiv (serial, xe->xany.serial);
539
540 switch (xe->type)
541 {
542 case KeyPress:
543 case KeyRelease:
544 case ButtonPress:
545 case ButtonRelease:
546 case MotionNotify:
547 setiv (time, xe->xmotion.time);
548 setiv (x, xe->xmotion.x);
549 setiv (y, xe->xmotion.y);
550 setiv (row, xe->xmotion.y / term->fheight);
551 setiv (col, xe->xmotion.x / term->fwidth);
552 setiv (x_root, xe->xmotion.x_root);
553 setiv (y_root, xe->xmotion.y_root);
554 setiv (state, xe->xmotion.state);
555 break;
556 }
557
558 switch (xe->type)
559 {
560 case KeyPress:
561 case KeyRelease:
562 setiv (keycode, xe->xkey.keycode);
563 break;
564
565 case ButtonPress:
566 case ButtonRelease:
567 setiv (button, xe->xbutton.button);
568 break;
569
570 case MotionNotify:
571 setiv (is_hint, xe->xmotion.is_hint);
572 break;
573 }
574
575 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
576 }
577 break;
578
247 case DT_END: 579 case DT_END:
248 { 580 {
249 va_end (ap); 581 va_end (ap);
250 582
251 PUTBACK; 583 PUTBACK;
261 PUTBACK; 593 PUTBACK;
262 FREETMPS; 594 FREETMPS;
263 LEAVE; 595 LEAVE;
264 596
265 if (SvTRUE (ERRSV)) 597 if (SvTRUE (ERRSV))
598 {
266 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); 599 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
600 ungrab (term); // better lose the grab than the session
601 }
267 602
268 if (htype == HOOK_DESTROY) 603 if (htype == HOOK_DESTROY)
269 { 604 {
270 // TODO: clear magic
271 hv_clear ((HV *)SvRV ((SV *)term->self)); 605 clearSVptr ((SV *)term->perl.self);
272 SvREFCNT_dec ((SV *)term->self); 606 SvREFCNT_dec ((SV *)term->perl.self);
273 } 607 }
274 608
275 return count; 609 return count;
276 } 610 }
277 611
287 621
288PROTOTYPES: ENABLE 622PROTOTYPES: ENABLE
289 623
290BOOT: 624BOOT:
291{ 625{
292# define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0)) 626 sv_setsv (get_sv ("urxvt::LIBDIR", 1), newSVpvn (LIBDIR, sizeof (LIBDIR) - 1));
627
293 AV *hookname = get_av ("urxvt::HOOKNAME", 1); 628 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
294 set_hookname (LOAD); 629# define def(sym) av_store (hookname, HOOK_ ## sym, newSVpv (# sym, 0));
295 set_hookname (INIT); 630# include "hookinc.h"
296 set_hookname (RESET); 631# undef def
297 set_hookname (START);
298 set_hookname (DESTROY);
299 set_hookname (SEL_BEGIN);
300 set_hookname (SEL_EXTEND);
301 set_hookname (SEL_MAKE);
302 set_hookname (SEL_GRAB);
303 set_hookname (FOCUS_IN);
304 set_hookname (FOCUS_OUT);
305 set_hookname (VIEW_CHANGE);
306 set_hookname (SCROLL_BACK);
307 set_hookname (TTY_ACTIVITY);
308 set_hookname (REFRESH_BEGIN);
309 set_hookname (REFRESH_END);
310 632
311 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR); 633 HV *option = get_hv ("urxvt::OPTION", 1);
634# define def(name,val) hv_store (option, # name, sizeof (# name) - 1, newSVuv (Opt_ ## name), 0);
635# define nodef(name)
636# include "optinc.h"
637# undef nodef
638# undef def
639
640 HV *stash = gv_stashpv ("urxvt", 1);
641# define export_const_iv(name) newCONSTSUB (stash, # name, newSViv (name));
642 export_const_iv (DEFAULT_RSTYLE);
643 export_const_iv (OVERLAY_RSTYLE);
644 export_const_iv (RS_Bold);
645 export_const_iv (RS_Italic);
646 export_const_iv (RS_Blink);
647 export_const_iv (RS_RVid);
648 export_const_iv (RS_Uline);
649
650 export_const_iv (CurrentTime);
651 export_const_iv (ShiftMask);
652 export_const_iv (LockMask);
653 export_const_iv (ControlMask);
654 export_const_iv (Mod1Mask);
655 export_const_iv (Mod2Mask);
656 export_const_iv (Mod3Mask);
657 export_const_iv (Mod4Mask);
658 export_const_iv (Mod5Mask);
659 export_const_iv (Button1Mask);
660 export_const_iv (Button2Mask);
661 export_const_iv (Button3Mask);
662 export_const_iv (Button4Mask);
663 export_const_iv (Button5Mask);
664 export_const_iv (AnyModifier);
312} 665}
666
667SV *
668new (...)
669 CODE:
670{
671 stringvec *argv = new stringvec;
672 bool success;
673
674 for (int i = 0; i < items ;i++)
675 argv->push_back (strdup (SvPVbyte_nolen (ST (i))));
676
677 rxvt_term *term = new rxvt_term;
678
679 term->argv = argv;
680
681 try
682 {
683 if (!term->init (argv->size (), argv->begin ()))
684 term = 0;
685 }
686 catch (const class rxvt_failure_exception &e)
687 {
688 term->destroy ();
689 croak ("exception caught while initializing new terminal instance");
690 }
691
692 RETVAL = term && term->perl.self ? newSVterm (term) : &PL_sv_undef;
693}
694 OUTPUT:
695 RETVAL
313 696
314void 697void
315set_should_invoke (int htype, int value) 698set_should_invoke (int htype, int value)
316 CODE: 699 CODE:
317 rxvt_perl.should_invoke [htype] = value; 700 rxvt_perl.should_invoke [htype] = value;
323 706
324void 707void
325fatal (const char *msg) 708fatal (const char *msg)
326 CODE: 709 CODE:
327 rxvt_fatal ("%s", msg); 710 rxvt_fatal ("%s", msg);
711
712SV *
713untaint (SV *sv)
714 CODE:
715 RETVAL = newSVsv (sv);
716 SvTAINTED_off (RETVAL);
717 OUTPUT:
718 RETVAL
719
720bool
721safe ()
722 CODE:
723 RETVAL = !rxvt_tainted ();
724 OUTPUT:
725 RETVAL
328 726
329NV 727NV
330NOW () 728NOW ()
331 CODE: 729 CODE:
332 RETVAL = NOW; 730 RETVAL = NOW;
333 OUTPUT: 731 OUTPUT:
334 RETVAL 732 RETVAL
335 733
734int
735GET_BASEFG (int rend)
736 CODE:
737 RETVAL = GET_BASEFG (rend);
738 OUTPUT:
739 RETVAL
740
741int
742GET_BASEBG (int rend)
743 CODE:
744 RETVAL = GET_BASEBG (rend);
745 OUTPUT:
746 RETVAL
747
748int
749SET_FGCOLOR (int rend, int new_color)
750 CODE:
751 RETVAL = SET_FGCOLOR (rend, new_color);
752 OUTPUT:
753 RETVAL
754
755int
756SET_BGCOLOR (int rend, int new_color)
757 CODE:
758 RETVAL = SET_BGCOLOR (rend, new_color);
759 OUTPUT:
760 RETVAL
761
762int
763GET_CUSTOM (int rend)
764 CODE:
765 RETVAL = (rend && RS_customMask) >> RS_customShift;
766 OUTPUT:
767 RETVAL
768
769int
770SET_CUSTOM (int rend, int new_value)
771 CODE:
772{
773 if (!IN_RANGE_EXC (new_value, 0, RS_customCount))
774 croak ("custom value out of range, must be 0..%d", RS_customCount - 1);
775
776 RETVAL = (rend & ~RS_customMask)
777 | ((new_value << RS_customShift) & RS_customMask);
778}
779 OUTPUT:
780 RETVAL
781
336MODULE = urxvt PACKAGE = urxvt::term 782MODULE = urxvt PACKAGE = urxvt::term
783
784void
785rxvt_term::destroy ()
786
787void
788rxvt_term::grab_button (int button, U32 modifiers)
789 CODE:
790 XGrabButton (THIS->display->display, button, modifiers, THIS->vt, 1,
791 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
792 GrabModeSync, GrabModeSync, None, GRAB_CURSOR);
793
794bool
795rxvt_term::grab (U32 eventtime, int sync = 0)
796 CODE:
797{
798 int mode = sync ? GrabModeSync : GrabModeAsync;
799
800 THIS->perl.grabtime = 0;
801
802 if (!XGrabPointer (THIS->display->display, THIS->vt, 0,
803 ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
804 mode, mode, None, GRAB_CURSOR, eventtime))
805 if (!XGrabKeyboard (THIS->display->display, THIS->vt, 0, mode, mode, eventtime))
806 THIS->perl.grabtime = eventtime;
807 else
808 XUngrabPointer (THIS->display->display, eventtime);
809
810 RETVAL = !!THIS->perl.grabtime;
811}
812 OUTPUT:
813 RETVAL
814
815void
816rxvt_term::allow_events_async ()
817 CODE:
818 XAllowEvents (THIS->display->display, AsyncBoth, THIS->perl.grabtime);
819
820void
821rxvt_term::allow_events_sync ()
822 CODE:
823 XAllowEvents (THIS->display->display, SyncBoth, THIS->perl.grabtime);
824
825void
826rxvt_term::allow_events_replay ()
827 CODE:
828 XAllowEvents (THIS->display->display, ReplayPointer, THIS->perl.grabtime);
829 XAllowEvents (THIS->display->display, ReplayKeyboard, THIS->perl.grabtime);
830
831void
832rxvt_term::ungrab ()
833 CODE:
834 ungrab (THIS);
337 835
338int 836int
339rxvt_term::strwidth (SV *str) 837rxvt_term::strwidth (SV *str)
340 CODE: 838 CODE:
341{ 839{
360 char *mbstr = rxvt_wcstombs (wstr); 858 char *mbstr = rxvt_wcstombs (wstr);
361 rxvt_pop_locale (); 859 rxvt_pop_locale ();
362 860
363 free (wstr); 861 free (wstr);
364 862
365 RETVAL = newSVpv (mbstr, 0); 863 RETVAL = taint_if (newSVpv (mbstr, 0), str);
366 free (mbstr); 864 free (mbstr);
367} 865}
368 OUTPUT: 866 OUTPUT:
369 RETVAL 867 RETVAL
370 868
377 875
378 rxvt_push_locale (THIS->locale); 876 rxvt_push_locale (THIS->locale);
379 wchar_t *wstr = rxvt_mbstowcs (data, len); 877 wchar_t *wstr = rxvt_mbstowcs (data, len);
380 rxvt_pop_locale (); 878 rxvt_pop_locale ();
381 879
382 char *str = rxvt_wcstoutf8 (wstr); 880 RETVAL = taint_if (wcs2sv (wstr), octets);
383 free (wstr); 881 free (wstr);
882}
883 OUTPUT:
884 RETVAL
384 885
385 RETVAL = newSVpv (str, 0); 886#define TERM_OFFSET(sym) offsetof (TermWin_t, sym)
386 SvUTF8_on (RETVAL); 887
888#define TERM_OFFSET_width TERM_OFFSET(width)
889#define TERM_OFFSET_height TERM_OFFSET(height)
890#define TERM_OFFSET_fwidth TERM_OFFSET(fwidth)
891#define TERM_OFFSET_fheight TERM_OFFSET(fheight)
892#define TERM_OFFSET_fbase TERM_OFFSET(fbase)
893#define TERM_OFFSET_nrow TERM_OFFSET(nrow)
894#define TERM_OFFSET_ncol TERM_OFFSET(ncol)
895#define TERM_OFFSET_focus TERM_OFFSET(focus)
896#define TERM_OFFSET_mapped TERM_OFFSET(mapped)
897#define TERM_OFFSET_saveLines TERM_OFFSET(saveLines)
898#define TERM_OFFSET_total_rows TERM_OFFSET(total_rows)
899#define TERM_OFFSET_nsaved TERM_OFFSET(nsaved)
900
901int
902rxvt_term::width ()
903 ALIAS:
904 width = TERM_OFFSET_width
905 height = TERM_OFFSET_height
906 fwidth = TERM_OFFSET_fwidth
907 fheight = TERM_OFFSET_fheight
908 fbase = TERM_OFFSET_fbase
909 nrow = TERM_OFFSET_nrow
910 ncol = TERM_OFFSET_ncol
911 focus = TERM_OFFSET_focus
912 mapped = TERM_OFFSET_mapped
913 saveLines = TERM_OFFSET_saveLines
914 total_rows = TERM_OFFSET_total_rows
915 nsaved = TERM_OFFSET_nsaved
916 CODE:
917 RETVAL = *(int *)((char *)THIS + ix);
918 OUTPUT:
919 RETVAL
920
921unsigned int
922rxvt_term::ModLevel3Mask ()
923 ALIAS:
924 ModLevel3Mask = 0
925 ModMetaMask = 1
926 ModNumLockMask = 2
927 CODE:
928 switch (ix)
929 {
930 case 0: RETVAL = THIS->ModLevel3Mask; break;
931 case 1: RETVAL = THIS->ModMetaMask; break;
932 case 2: RETVAL = THIS->ModNumLockMask; break;
933 }
934 OUTPUT:
935 RETVAL
936
937char *
938rxvt_term::display_id ()
939 ALIAS:
940 display_id = 0
941 locale = 1
942 CODE:
943 switch (ix)
944 {
945 case 0: RETVAL = THIS->display->id; break;
946 case 1: RETVAL = THIS->locale; break;
947 }
948 OUTPUT:
949 RETVAL
950
951U32
952rxvt_term::parent ()
953 CODE:
954 RETVAL = (U32)THIS->parent [0];
955 OUTPUT:
956 RETVAL
957
958U32
959rxvt_term::vt ()
960 CODE:
961 RETVAL = (U32)THIS->vt;
962 OUTPUT:
963 RETVAL
964
965U32
966rxvt_term::rstyle (U32 new_rstyle = THIS->rstyle)
967 CODE:
968{
969 RETVAL = THIS->rstyle;
970 THIS->rstyle = new_rstyle;
971}
972 OUTPUT:
973 RETVAL
974
975int
976rxvt_term::view_start (int newval = -1)
977 CODE:
978{
979 RETVAL = THIS->view_start;
980
981 if (newval >= 0)
982 {
983 THIS->view_start = min (newval, THIS->nsaved);
984 THIS->scr_changeview (RETVAL);
985 }
986}
987 OUTPUT:
988 RETVAL
989
990void
991rxvt_term::want_refresh ()
992 CODE:
993 THIS->want_refresh = 1;
994
995void
996rxvt_term::ROW_t (int row_number, SV *new_text = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
997 PPCODE:
998{
999 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1000 XSRETURN_EMPTY;
1001
1002 line_t &l = ROW(row_number);
1003
1004 if (GIMME_V != G_VOID)
1005 {
1006 wchar_t *wstr = new wchar_t [THIS->ncol];
1007
1008 for (int col = 0; col < THIS->ncol; col++)
1009 wstr [col] = l.t [col];
1010
1011 XPUSHs (taint (sv_2mortal (wcs2sv (wstr, THIS->ncol))));
1012
1013 delete [] wstr;
1014 }
1015
1016 if (new_text)
1017 {
1018 wchar_t *wstr = sv2wcs (new_text);
1019
1020 int len = min (wcslen (wstr) - start_ofs, max_len);
1021
1022 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
1023 {
1024 free (wstr);
1025 croak ("new_text extends beyond horizontal margins");
1026 }
1027
1028 for (int col = start_col; col < start_col + len; col++)
1029 {
1030 l.t [col] = wstr [start_ofs + col - start_col];
1031 l.r [col] = SET_FONT (l.r [col], THIS->fontset [GET_STYLE (l.r [col])]->find_font (l.t [col]));
1032 }
1033
387 free (str); 1034 free (wstr);
1035 }
1036}
1037
1038void
1039rxvt_term::ROW_r (int row_number, SV *new_rend = 0, int start_col = 0, int start_ofs = 0, int max_len = MAX_COLS)
1040 PPCODE:
1041{
1042 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1043 XSRETURN_EMPTY;
1044
1045 line_t &l = ROW(row_number);
1046
1047 if (GIMME_V != G_VOID)
1048 {
1049 AV *av = newAV ();
1050
1051 av_extend (av, THIS->ncol - 1);
1052 for (int col = 0; col < THIS->ncol; col++)
1053 av_store (av, col, newSViv (l.r [col]));
1054
1055 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
1056 }
1057
1058 if (new_rend)
1059 {
1060 if (!SvROK (new_rend) || SvTYPE (SvRV (new_rend)) != SVt_PVAV)
1061 croak ("new_rend must be arrayref");
1062
1063 AV *av = (AV *)SvRV (new_rend);
1064 int len = min (av_len (av) + 1 - start_ofs, max_len);
1065
1066 if (!IN_RANGE_INC (start_col, 0, THIS->ncol - len))
1067 croak ("new_rend array extends beyond horizontal margins");
1068
1069 for (int col = start_col; col < start_col + len; col++)
1070 {
1071 rend_t r = SvIV (*av_fetch (av, start_ofs + col - start_col, 1)) & ~RS_fontMask;
1072
1073 l.r [col] = SET_FONT (r, THIS->fontset [GET_STYLE (r)]->find_font (l.t [col]));
1074 }
1075 }
1076}
1077
1078int
1079rxvt_term::ROW_l (int row_number, int new_length = -1)
1080 CODE:
1081{
1082 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1083 XSRETURN_EMPTY;
1084
1085 line_t &l = ROW(row_number);
1086 RETVAL = l.l;
1087
1088 if (new_length >= 0)
1089 l.l = new_length;
1090}
1091 OUTPUT:
1092 RETVAL
1093
1094bool
1095rxvt_term::ROW_is_longer (int row_number, int new_is_longer = -1)
1096 CODE:
1097{
1098 if (!IN_RANGE_EXC (row_number, -THIS->nsaved, THIS->nrow))
1099 XSRETURN_EMPTY;
1100
1101 line_t &l = ROW(row_number);
1102 RETVAL = l.is_longer ();
1103
1104 if (new_is_longer >= 0)
1105 l.is_longer (new_is_longer);
1106}
1107 OUTPUT:
1108 RETVAL
1109
1110SV *
1111rxvt_term::special_encode (SV *string)
1112 CODE:
1113{
1114 wchar_t *wstr = sv2wcs (string);
1115 int wlen = wcslen (wstr);
1116 wchar_t *rstr = new wchar_t [wlen]; // cannot become longer
1117
1118 rxvt_push_locale (THIS->locale);
1119
1120 wchar_t *r = rstr;
1121 for (wchar_t *s = wstr; *s; s++)
1122 if (wcwidth (*s) == 0)
1123 {
1124 if (r == rstr)
1125 croak ("leading combining character unencodable");
1126
1127 unicode_t n = rxvt_compose (r[-1], *s);
1128 if (n == NOCHAR)
1129 n = rxvt_composite.compose (r[-1], *s);
1130
1131 r[-1] = n;
1132 }
1133#if !UNICODE_3
1134 else if (*s >= 0x10000)
1135 *r++ = rxvt_composite.compose (*s);
1136#endif
1137 else
1138 *r++ = *s;
1139
1140 rxvt_pop_locale ();
1141
1142 RETVAL = taint_if (wcs2sv (rstr, r - rstr), string);
1143
1144 delete [] rstr;
1145}
1146 OUTPUT:
1147 RETVAL
1148
1149SV *
1150rxvt_term::special_decode (SV *text)
1151 CODE:
1152{
1153 wchar_t *wstr = sv2wcs (text);
1154 int wlen = wcslen (wstr);
1155 int dlen = 0;
1156
1157 // find length
1158 for (wchar_t *s = wstr; *s; s++)
1159 if (*s == NOCHAR)
1160 ;
1161 else if (IS_COMPOSE (*s))
1162 dlen += rxvt_composite.expand (*s, 0);
1163 else
1164 dlen++;
1165
1166 wchar_t *rstr = new wchar_t [dlen];
1167
1168 // decode
1169 wchar_t *r = rstr;
1170 for (wchar_t *s = wstr; *s; s++)
1171 if (*s == NOCHAR)
1172 ;
1173 else if (IS_COMPOSE (*s))
1174 r += rxvt_composite.expand (*s, r);
1175 else
1176 *r++ = *s;
1177
1178 RETVAL = taint_if (wcs2sv (rstr, r - rstr), text);
1179
1180 delete [] rstr;
388} 1181}
389 OUTPUT: 1182 OUTPUT:
390 RETVAL 1183 RETVAL
391 1184
392void 1185void
393rxvt_term::_resource (char *name, int index, SV *newval = 0) 1186rxvt_term::_resource (char *name, int index, SV *newval = 0)
394 PPCODE: 1187 PPCODE:
395{ 1188{
396 struct resval { const char *name; int value; } rslist [] = { 1189 struct resval { const char *name; int value; } rslist [] = {
397# define Rs_def(name) { # name, Rs_ ## name }, 1190# define def(name) { # name, Rs_ ## name },
398# define Rs_reserve(name,count) 1191# define reserve(name,count)
399# include "rsinc.h" 1192# include "rsinc.h"
400# undef Rs_def 1193# undef def
401# undef Rs_reserve 1194# undef reserve
402 }; 1195 };
403 1196
404 struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]); 1197 struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
405 1198
406 do { 1199 do {
412 1205
413 if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES)) 1206 if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
414 croak ("requested out-of-bound resource %s+%d,", name, index - rs->value); 1207 croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
415 1208
416 if (GIMME_V != G_VOID) 1209 if (GIMME_V != G_VOID)
417 XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef); 1210 XPUSHs (THIS->rs [index] ? sv_2mortal (taint (newSVpv (THIS->rs [index], 0))) : &PL_sv_undef);
418 1211
419 if (newval) 1212 if (newval)
420 { 1213 {
421 if (SvOK (newval)) 1214 if (SvOK (newval))
422 { 1215 {
427 else 1220 else
428 THIS->rs [index] = 0; 1221 THIS->rs [index] = 0;
429 } 1222 }
430} 1223}
431 1224
1225bool
1226rxvt_term::option (U32 optval, int set = -1)
1227 CODE:
1228{
1229 RETVAL = THIS->options & optval;
1230
1231 if (set >= 0)
1232 {
1233 if (set)
1234 THIS->options |= optval;
1235 else
1236 THIS->options &= ~optval;
1237
1238 switch (optval)
1239 {
1240 case Opt_skipBuiltinGlyphs:
1241 THIS->set_fonts ();
1242 THIS->scr_remap_chars ();
1243 THIS->scr_touch (true);
1244 THIS->want_refresh = 1;
1245 break;
1246
1247 case Opt_cursorUnderline:
1248 THIS->want_refresh = 1;
1249 break;
1250
1251# case Opt_scrollBar_floating:
1252# case Opt_scrollBar_right:
1253# THIS->resize_all_windows (THIS->width, THIS->height, 1);
1254# break;
1255 }
1256 }
1257}
1258 OUTPUT:
1259 RETVAL
1260
432void 1261void
433rxvt_term::selection_mark (...) 1262rxvt_term::screen_cur (...)
434 PROTOTYPE: $;$$ 1263 PROTOTYPE: $;$$
435 ALIAS: 1264 ALIAS:
1265 screen_cur = 0
436 selection_beg = 1 1266 selection_beg = 1
437 selection_end = 2 1267 selection_end = 2
1268 selection_mark = 3
438 PPCODE: 1269 PPCODE:
439{ 1270{
440 row_col_t &sel = ix == 1 ? THIS->selection.beg 1271 row_col_t &rc = ix == 0 ? THIS->screen.cur
1272 : ix == 1 ? THIS->selection.beg
441 : ix == 2 ? THIS->selection.end 1273 : ix == 2 ? THIS->selection.end
442 : THIS->selection.mark; 1274 : THIS->selection.mark;
443 1275
444 if (GIMME_V != G_VOID) 1276 if (GIMME_V != G_VOID)
445 { 1277 {
446 EXTEND (SP, 2); 1278 EXTEND (SP, 2);
447 PUSHs (sv_2mortal (newSViv (sel.row))); 1279 PUSHs (sv_2mortal (newSViv (rc.row)));
448 PUSHs (sv_2mortal (newSViv (sel.col))); 1280 PUSHs (sv_2mortal (newSViv (rc.col)));
449 } 1281 }
450 1282
451 if (items == 3) 1283 if (items == 3)
452 { 1284 {
453 sel.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1); 1285 rc.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
454 sel.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1); 1286 rc.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
455 1287
456 if (ix) 1288 if (ix)
457 THIS->want_refresh = 1; 1289 THIS->want_refresh = 1;
458 } 1290 }
459} 1291}
460 1292
461int 1293int
462rxvt_term::selection_grab (int eventtime) 1294rxvt_term::selection_grab (U32 eventtime)
463 1295
464void 1296void
465rxvt_term::selection (SV *newtext = 0) 1297rxvt_term::selection (SV *newtext = 0)
466 PPCODE: 1298 PPCODE:
467{ 1299{
468 if (GIMME_V != G_VOID) 1300 if (GIMME_V != G_VOID)
469 { 1301 XPUSHs (THIS->selection.text
470 char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len); 1302 ? taint (sv_2mortal (wcs2sv (THIS->selection.text, THIS->selection.len)))
471 SV *sv = newSVpv (sel, 0); 1303 : &PL_sv_undef);
472 SvUTF8_on (sv);
473 free (sel);
474 XPUSHs (sv_2mortal (sv));
475 }
476 1304
477 if (newtext) 1305 if (newtext)
478 { 1306 {
479 free (THIS->selection.text); 1307 free (THIS->selection.text);
480 1308
481 THIS->selection.text = sv2wcs (newtext); 1309 THIS->selection.text = sv2wcs (newtext);
482 THIS->selection.len = wcslen (THIS->selection.text); 1310 THIS->selection.len = wcslen (THIS->selection.text);
483 } 1311 }
484} 1312}
485
486void
487rxvt_term::scr_overlay_new (int x, int y, int w, int h)
488 1313
489void 1314void
490rxvt_term::scr_overlay_off () 1315rxvt_term::scr_add_lines (SV *string)
491
492void
493rxvt_term::scr_overlay_set_char (int x, int y, U32 text, U32 rend = OVERLAY_RSTYLE)
494 CODE: 1316 CODE:
495 THIS->scr_overlay_set (x, y, text, rend);
496
497void
498rxvt_term::scr_overlay_set (int x, int y, SV *text)
499 CODE:
500{ 1317{
501 wchar_t *wtext = sv2wcs (text); 1318 wchar_t *wstr = sv2wcs (string);
502 THIS->scr_overlay_set (x, y, wtext); 1319 THIS->scr_add_lines (wstr, wcslen (wstr));
503 free (wtext); 1320 free (wstr);
504} 1321}
505 1322
506void 1323void
507rxvt_term::tt_write (SV *octets) 1324rxvt_term::tt_write (SV *octets)
508 INIT: 1325 INIT:
509 STRLEN len; 1326 STRLEN len;
510 char *str = SvPVbyte (octets, len); 1327 char *str = SvPVbyte (octets, len);
511 C_ARGS: 1328 C_ARGS:
512 (unsigned char *)str, len 1329 str, len
1330
1331void
1332rxvt_term::cmd_parse (SV *octets)
1333 CODE:
1334{
1335 STRLEN len;
1336 char *str = SvPVbyte (octets, len);
1337
1338 char *old_cmdbuf_ptr = THIS->cmdbuf_ptr;
1339 char *old_cmdbuf_endp = THIS->cmdbuf_endp;
1340
1341 THIS->cmdbuf_ptr = str;
1342 THIS->cmdbuf_endp = str + len;
1343
1344 rxvt_push_locale (THIS->locale);
1345 THIS->cmd_parse ();
1346 rxvt_pop_locale ();
1347
1348 THIS->cmdbuf_ptr = old_cmdbuf_ptr;
1349 THIS->cmdbuf_endp = old_cmdbuf_endp;
1350}
1351
1352SV *
1353rxvt_term::overlay (int x, int y, int w, int h, int rstyle = OVERLAY_RSTYLE, int border = 2)
1354 CODE:
1355{
1356 overlay *o = new overlay (THIS, x, y, w, h, rstyle, border);
1357 RETVAL = newSVptr ((void *)o, "urxvt::overlay");
1358 o->self = (HV *)SvRV (RETVAL);
1359}
1360 OUTPUT:
1361 RETVAL
1362
1363MODULE = urxvt PACKAGE = urxvt::overlay
1364
1365void
1366overlay::set (int x, int y, SV *text, SV *rend = 0)
1367
1368void
1369overlay::show ()
1370
1371void
1372overlay::hide ()
1373
1374void
1375overlay::DESTROY ()
513 1376
514MODULE = urxvt PACKAGE = urxvt::timer 1377MODULE = urxvt PACKAGE = urxvt::timer
515 1378
516SV * 1379SV *
517timer::new () 1380timer::new ()
518 CODE: 1381 CODE:
519 timer *w = new timer; 1382 timer *w = new timer;
1383 w->start (NOW);
520 RETVAL = newSVptr ((void *)w, "urxvt::timer"); 1384 RETVAL = newSVptr ((void *)w, "urxvt::timer");
521 w->self = (HV *)SvRV (RETVAL); 1385 w->self = (HV *)SvRV (RETVAL);
522 OUTPUT: 1386 OUTPUT:
523 RETVAL 1387 RETVAL
524 1388
532 1396
533NV 1397NV
534timer::at () 1398timer::at ()
535 CODE: 1399 CODE:
536 RETVAL = THIS->at; 1400 RETVAL = THIS->at;
1401 OUTPUT:
1402 RETVAL
1403
1404timer *
1405timer::interval (NV interval)
1406 CODE:
1407 THIS->interval = interval;
1408 RETVAL = THIS;
537 OUTPUT: 1409 OUTPUT:
538 RETVAL 1410 RETVAL
539 1411
540timer * 1412timer *
541timer::set (NV tstamp) 1413timer::set (NV tstamp)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines