ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtperl.xs
Revision: 1.2
Committed: Mon Jan 2 17:17:02 2006 UTC (18 years, 5 months ago) by root
Branch: MAIN
Changes since 1.1: +40 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*----------------------------------------------------------------------*
2 * File: rxvtperl.xs
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2005-2005 Marc Lehmann <pcg@goof.com>
7 *
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 #include <cstdarg>
32
33 #include "rxvt.h"
34 #include "iom.h"
35 #include "rxvtutil.h"
36 #include "rxvtperl.h"
37
38 #include "perlxsi.c"
39
40 /////////////////////////////////////////////////////////////////////////////
41
42 static wchar_t *
43 sv2wcs (SV *sv)
44 {
45 STRLEN len;
46 char *str = SvPVutf8 (sv, len);
47 return rxvt_utf8towcs (str, len);
48 }
49
50 static SV *
51 new_ref (HV *hv, const char *klass)
52 {
53 return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
54 }
55
56 //TODO: use magic
57 static SV *
58 newSVptr (void *ptr, const char *klass)
59 {
60 HV *hv = newHV ();
61 hv_store (hv, "_ptr", 4, newSViv ((long)ptr), 0);
62 return sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
63 }
64
65 static long
66 SvPTR (SV *sv, const char *klass)
67 {
68 if (!sv_derived_from (sv, klass))
69 croak ("object of type %s expected", klass);
70
71 IV iv = SvIV (*hv_fetch ((HV *)SvRV (sv), "_ptr", 4, 1));
72
73 if (!iv)
74 croak ("perl code used %s object, but C++ object is already destroyed, caught", klass);
75
76 return (long)iv;
77 }
78
79 #define newSVterm(term) SvREFCNT_inc ((SV *)term->self)
80 #define SvTERM(sv) (rxvt_term *)SvPTR (sv, "urxvt::term")
81
82 /////////////////////////////////////////////////////////////////////////////
83
84 struct perl_watcher
85 {
86 SV *cbsv;
87 HV *self;
88
89 perl_watcher ()
90 : cbsv (newSV (0))
91 {
92 }
93
94 ~perl_watcher ()
95 {
96 SvREFCNT_dec (cbsv);
97 }
98
99 void cb (SV *cb)
100 {
101 sv_setsv (cbsv, cb);
102 }
103
104 void invoke (const char *type, SV *self, int arg = -1);
105 };
106
107 void
108 perl_watcher::invoke (const char *type, SV *self, int arg)
109 {
110 dSP;
111
112 ENTER;
113 SAVETMPS;
114
115 PUSHMARK (SP);
116
117 XPUSHs (sv_2mortal (self));
118
119 if (arg >= 0)
120 XPUSHs (sv_2mortal (newSViv (arg)));
121
122 PUTBACK;
123 call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
124 SPAGAIN;
125
126 PUTBACK;
127 FREETMPS;
128 LEAVE;
129
130 if (SvTRUE (ERRSV))
131 rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
132 }
133
134 #define newSVtimer(timer) new_ref (timer->self, "urxvt::timer")
135 #define SvTIMER(sv) (timer *)SvPTR (sv, "urxvt::timer")
136
137 struct timer : time_watcher, perl_watcher
138 {
139 timer ()
140 : time_watcher (this, &timer::execute)
141 {
142 }
143
144 void execute (time_watcher &w)
145 {
146 invoke ("urxvt::timer", newSVtimer (this));
147 }
148 };
149
150 #define newSViow(iow) new_ref (iow->self, "urxvt::iow")
151 #define SvIOW(sv) (iow *)SvPTR (sv, "urxvt::iow")
152
153 struct iow : io_watcher, perl_watcher
154 {
155 iow ()
156 : io_watcher (this, &iow::execute)
157 {
158 }
159
160 void execute (io_watcher &w, short revents)
161 {
162 invoke ("urxvt::iow", newSViow (this), revents);
163 }
164 };
165
166 /////////////////////////////////////////////////////////////////////////////
167
168 struct rxvt_perl_interp rxvt_perl;
169
170 static PerlInterpreter *perl;
171
172 rxvt_perl_interp::rxvt_perl_interp ()
173 {
174 }
175
176 rxvt_perl_interp::~rxvt_perl_interp ()
177 {
178 if (perl)
179 {
180 perl_destruct (perl);
181 perl_free (perl);
182 }
183 }
184
185 void
186 rxvt_perl_interp::init ()
187 {
188 if (!perl)
189 {
190 char *argv[] = {
191 "",
192 "-edo '" LIBDIR "/urxvt.pm' or ($@ and die $@) or exit 1",
193 };
194
195 perl = perl_alloc ();
196 perl_construct (perl);
197
198 if (perl_parse (perl, xs_init, 2, argv, (char **)NULL)
199 || perl_run (perl))
200 {
201 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
202
203 perl_destruct (perl);
204 perl_free (perl);
205 perl = 0;
206 }
207 }
208 }
209
210 bool
211 rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
212 {
213 if (!perl)
214 return false;
215
216 if (htype == HOOK_INIT) // first hook ever called
217 term->self = (void *)newSVptr ((void *)term, "urxvt::term");
218 else if (htype == HOOK_DESTROY)
219 {
220 // TODO: clear magic
221 hv_clear ((HV *)SvRV ((SV *)term->self));
222 SvREFCNT_dec ((SV *)term->self);
223 }
224
225 if (!should_invoke [htype])
226 return false;
227
228 dSP;
229 va_list ap;
230
231 va_start (ap, htype);
232
233 ENTER;
234 SAVETMPS;
235
236 PUSHMARK (SP);
237
238 XPUSHs (sv_2mortal (newSVterm (term)));
239 XPUSHs (sv_2mortal (newSViv (htype)));
240
241 for (;;) {
242 data_type dt = (data_type)va_arg (ap, int);
243
244 switch (dt)
245 {
246 case DT_INT:
247 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
248 break;
249
250 case DT_LONG:
251 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
252 break;
253
254 case DT_END:
255 {
256 va_end (ap);
257
258 PUTBACK;
259 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
260 SPAGAIN;
261
262 if (count)
263 {
264 SV *status = POPs;
265 count = SvTRUE (status);
266 }
267
268 PUTBACK;
269 FREETMPS;
270 LEAVE;
271
272 if (SvTRUE (ERRSV))
273 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
274
275 return count;
276 }
277
278 default:
279 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
280 }
281 }
282 }
283
284 /////////////////////////////////////////////////////////////////////////////
285
286 MODULE = urxvt PACKAGE = urxvt
287
288 PROTOTYPES: ENABLE
289
290 BOOT:
291 {
292 # define set_hookname(sym) av_store (hookname, PP_CONCAT(HOOK_, sym), newSVpv (PP_STRINGIFY(sym), 0))
293 AV *hookname = get_av ("urxvt::HOOKNAME", 1);
294 set_hookname (LOAD);
295 set_hookname (INIT);
296 set_hookname (RESET);
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
311 sv_setpv (get_sv ("urxvt::LIBDIR", 1), LIBDIR);
312 }
313
314 void
315 set_should_invoke (int htype, int value)
316 CODE:
317 rxvt_perl.should_invoke [htype] = value;
318
319 void
320 warn (const char *msg)
321 CODE:
322 rxvt_warn ("%s", msg);
323
324 void
325 fatal (const char *msg)
326 CODE:
327 rxvt_fatal ("%s", msg);
328
329 int
330 wcswidth (SV *str)
331 CODE:
332 {
333 wchar_t *wstr = sv2wcs (str);
334 RETVAL = wcswidth (wstr, wcslen (wstr));
335 free (wstr);
336 }
337 OUTPUT:
338 RETVAL
339
340 NV
341 NOW ()
342 CODE:
343 RETVAL = NOW;
344 OUTPUT:
345 RETVAL
346
347 MODULE = urxvt PACKAGE = urxvt::term
348
349 void
350 rxvt_term::_resource (char *name, int index, SV *newval = 0)
351 PPCODE:
352 {
353 struct resval { const char *name; int value; } rslist [] = {
354 # define Rs_def(name) { # name, Rs_ ## name },
355 # define Rs_reserve(name,count)
356 # include "rsinc.h"
357 # undef Rs_def
358 # undef Rs_reserve
359 };
360
361 struct resval *rs = rslist + sizeof (rslist) / sizeof (rslist [0]);
362
363 do {
364 if (rs-- == rslist)
365 croak ("no such resource '%s', requested", name);
366 } while (strcmp (name, rs->name));
367
368 index += rs->value;
369
370 if (!IN_RANGE_EXC (index, 0, NUM_RESOURCES))
371 croak ("requested out-of-bound resource %s+%d,", name, index - rs->value);
372
373 if (GIMME_V != G_VOID)
374 XPUSHs (THIS->rs [index] ? sv_2mortal (newSVpv (THIS->rs [index], 0)) : &PL_sv_undef);
375
376 if (newval)
377 {
378 if (SvOK (newval))
379 {
380 char *str = strdup (SvPVbyte_nolen (newval));
381 THIS->rs [index] = str;
382 THIS->allocated.push_back (str);
383 }
384 else
385 THIS->rs [index] = 0;
386 }
387 }
388
389 void
390 rxvt_term::selection_mark (...)
391 PROTOTYPE: $;$$
392 ALIAS:
393 selection_beg = 1
394 selection_end = 2
395 PPCODE:
396 {
397 row_col_t &sel = ix == 1 ? THIS->selection.beg
398 : ix == 2 ? THIS->selection.end
399 : THIS->selection.mark;
400
401 if (GIMME_V != G_VOID)
402 {
403 EXTEND (SP, 2);
404 PUSHs (sv_2mortal (newSViv (sel.row)));
405 PUSHs (sv_2mortal (newSViv (sel.col)));
406 }
407
408 if (items == 3)
409 {
410 sel.row = clamp (SvIV (ST (1)), -THIS->nsaved, THIS->nrow - 1);
411 sel.col = clamp (SvIV (ST (2)), 0, THIS->ncol - 1);
412
413 if (ix)
414 THIS->want_refresh = 1;
415 }
416 }
417
418 int
419 rxvt_term::selection_grab (int eventtime)
420
421 void
422 rxvt_term::selection (SV *newtext = 0)
423 PPCODE:
424 {
425 if (GIMME_V != G_VOID)
426 {
427 char *sel = rxvt_wcstoutf8 (THIS->selection.text, THIS->selection.len);
428 SV *sv = newSVpv (sel, 0);
429 SvUTF8_on (sv);
430 free (sel);
431 XPUSHs (sv_2mortal (sv));
432 }
433
434 if (newtext)
435 {
436 free (THIS->selection.text);
437
438 THIS->selection.text = sv2wcs (newtext);
439 THIS->selection.len = wcslen (THIS->selection.text);
440 }
441 }
442
443 void
444 rxvt_term::scr_overlay_new (int x, int y, int w, int h)
445
446 void
447 rxvt_term::scr_overlay_off ()
448
449 void
450 rxvt_term::scr_overlay_set_char (int x, int y, U32 text, U32 rend = OVERLAY_RSTYLE)
451 CODE:
452 THIS->scr_overlay_set (x, y, text, rend);
453
454 void
455 rxvt_term::scr_overlay_set (int x, int y, SV *text)
456 CODE:
457 {
458 wchar_t *wtext = sv2wcs (text);
459 THIS->scr_overlay_set (x, y, wtext);
460 free (wtext);
461 }
462
463 MODULE = urxvt PACKAGE = urxvt::timer
464
465 SV *
466 timer::new ()
467 CODE:
468 timer *w = new timer;
469 RETVAL = newSVptr ((void *)w, "urxvt::timer");
470 w->self = (HV *)SvRV (RETVAL);
471 OUTPUT:
472 RETVAL
473
474 timer *
475 timer::cb (SV *cb)
476 CODE:
477 THIS->cb (cb);
478 RETVAL = THIS;
479 OUTPUT:
480 RETVAL
481
482 NV
483 timer::at ()
484 CODE:
485 RETVAL = THIS->at;
486 OUTPUT:
487 RETVAL
488
489 timer *
490 timer::set (NV tstamp)
491 CODE:
492 THIS->set (tstamp);
493 RETVAL = THIS;
494 OUTPUT:
495 RETVAL
496
497 timer *
498 timer::start (NV tstamp = THIS->at)
499 CODE:
500 THIS->start (tstamp);
501 RETVAL = THIS;
502 OUTPUT:
503 RETVAL
504
505 timer *
506 timer::stop ()
507 CODE:
508 THIS->stop ();
509 RETVAL = THIS;
510 OUTPUT:
511 RETVAL
512
513 void
514 timer::DESTROY ()
515
516 MODULE = urxvt PACKAGE = urxvt::iow
517
518 SV *
519 iow::new ()
520 CODE:
521 iow *w = new iow;
522 RETVAL = newSVptr ((void *)w, "urxvt::iow");
523 w->self = (HV *)SvRV (RETVAL);
524 OUTPUT:
525 RETVAL
526
527 iow *
528 iow::cb (SV *cb)
529 CODE:
530 THIS->cb (cb);
531 RETVAL = THIS;
532 OUTPUT:
533 RETVAL
534
535 iow *
536 iow::fd (int fd)
537 CODE:
538 THIS->fd = fd;
539 RETVAL = THIS;
540 OUTPUT:
541 RETVAL
542
543 iow *
544 iow::events (short events)
545 CODE:
546 THIS->events = events;
547 RETVAL = THIS;
548 OUTPUT:
549 RETVAL
550
551 iow *
552 iow::start ()
553 CODE:
554 THIS->start ();
555 RETVAL = THIS;
556 OUTPUT:
557 RETVAL
558
559 iow *
560 iow::stop ()
561 CODE:
562 THIS->stop ();
563 RETVAL = THIS;
564 OUTPUT:
565 RETVAL
566
567 void
568 iow::DESTROY ()
569
570