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.133 by root, Tue Dec 7 17:51:27 2010 UTC vs.
Revision 1.137 by root, Mon Feb 21 07:41:03 2011 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-2008 Marc Lehmann <pcg@goof.com> 6 * Copyright (c) 2005-2008,2011 Marc Lehmann <schmorp@schmorp.de>
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.
302 THIS->refresh_check (); 302 THIS->refresh_check ();
303} 303}
304 304
305///////////////////////////////////////////////////////////////////////////// 305/////////////////////////////////////////////////////////////////////////////
306 306
307#define IOM_CLASS "urxvt"
308#define IOM_WARN rxvt_warn
309#include "iom_perl.h" 307#include "iom_perl.h"
310 308
311///////////////////////////////////////////////////////////////////////////// 309/////////////////////////////////////////////////////////////////////////////
312 310
313struct rxvt_perl_interp rxvt_perl; 311struct rxvt_perl_interp rxvt_perl;
329{ 327{
330 if (!perl) 328 if (!perl)
331 { 329 {
332 rxvt_push_locale (""); // perl init destroys current locale 330 rxvt_push_locale (""); // perl init destroys current locale
333 331
332 {
334 perl_environ = rxvt_environ; 333 perl_environ = rxvt_environ;
335 swap (perl_environ, environ); 334 localise_env set_environ (perl_environ);
336 335
337 char *args[] = { 336 char *args[] = {
338 "", 337 "",
339 "-e" 338 "-e"
340 "BEGIN {" 339 "BEGIN {"
341 " urxvt->bootstrap;" 340 " urxvt->bootstrap;"
342 " unshift @INC, '" LIBDIR "';" 341 " unshift @INC, '" LIBDIR "';"
343 "}" 342 "}"
344 "" 343 ""
345 "use urxvt;" 344 "use urxvt;"
346 }; 345 };
347 int argc = sizeof (args) / sizeof (args[0]); 346 int argc = ARRAY_LENGTH(args);
348 char **argv = args; 347 char **argv = args;
349 348
350 PERL_SYS_INIT3 (&argc, &argv, &environ); 349 PERL_SYS_INIT3 (&argc, &argv, &environ);
351 perl = perl_alloc (); 350 perl = perl_alloc ();
352 perl_construct (perl); 351 perl_construct (perl);
353 352
354 if (perl_parse (perl, xs_init, argc, argv, (char **)NULL) 353 if (perl_parse (perl, xs_init, argc, argv, (char **)NULL)
355 || perl_run (perl)) 354 || perl_run (perl))
356 { 355 {
357 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n"); 356 rxvt_warn ("unable to initialize perl-interpreter, continuing without.\n");
358 357
359 perl_destruct (perl); 358 perl_destruct (perl);
360 perl_free (perl); 359 perl_free (perl);
361 perl = 0; 360 perl = 0;
362 } 361 }
363 362 }
364 swap (perl_environ, environ);
365 363
366 rxvt_pop_locale (); 364 rxvt_pop_locale ();
367 } 365 }
368 366
369 if (perl) 367 if (perl)
389rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...) 387rxvt_perl_interp::invoke (rxvt_term *term, hook_type htype, ...)
390{ 388{
391 if (!perl || !term->perl.self) 389 if (!perl || !term->perl.self)
392 return false; 390 return false;
393 391
392 localise_env set_environ (perl_environ);
393
394 // pre-handling of some events 394 // pre-handling of some events
395 if (htype == HOOK_REFRESH_END) 395 if (htype == HOOK_REFRESH_END)
396 { 396 {
397 AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0)); 397 AV *av = (AV *)SvRV (*hv_fetch ((HV *)SvRV ((SV *)term->perl.self), "_overlay", 8, 0));
398 398
399 for (int i = 0; i <= AvFILL (av); i++) 399 for (int i = 0; i <= AvFILL (av); i++)
400 ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap (); 400 ((overlay *)SvIV (*av_fetch (av, i, 0)))->swap ();
401 } 401 }
402 402
403 swap (perl_environ, environ);
404
405 bool event_consumed; 403 bool event_consumed;
406 404
407 if (htype == HOOK_INIT || htype == HOOK_DESTROY // must be called always 405 if (htype == HOOK_INIT || htype == HOOK_DESTROY // must be called always
408 || term->perl.should_invoke [htype]) 406 || term->perl.should_invoke [htype])
409 try
410 { 407 {
411 dSP; 408 dSP;
412 va_list ap; 409 va_list ap;
413 410
414 va_start (ap, htype); 411 va_start (ap, htype);
415 412
416 ENTER; 413 ENTER;
417 SAVETMPS; 414 SAVETMPS;
418 415
419 PUSHMARK (SP); 416 PUSHMARK (SP);
420 417
421 XPUSHs (sv_2mortal (newSVterm (term))); 418 XPUSHs (sv_2mortal (newSVterm (term)));
422 XPUSHs (sv_2mortal (newSViv (htype))); 419 XPUSHs (sv_2mortal (newSViv (htype)));
423 420
424 for (;;) { 421 for (;;) {
425 data_type dt = (data_type)va_arg (ap, int); 422 data_type dt = (data_type)va_arg (ap, int);
426 423
427 switch (dt) 424 switch (dt)
428 { 425 {
429 case DT_INT: 426 case DT_INT:
430 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int)))); 427 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
431 break; 428 break;
432 429
433 case DT_LONG: 430 case DT_LONG:
434 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long)))); 431 XPUSHs (sv_2mortal (newSViv (va_arg (ap, long))));
435 break; 432 break;
436 433
437 case DT_STR: 434 case DT_STR:
438 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0))); 435 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
439 break; 436 break;
440 437
441 case DT_STR_LEN: 438 case DT_STR_LEN:
442 { 439 {
443 char *str = va_arg (ap, char *); 440 char *str = va_arg (ap, char *);
444 int len = va_arg (ap, int); 441 int len = va_arg (ap, int);
445 442
446 XPUSHs (sv_2mortal (newSVpvn (str, len))); 443 XPUSHs (sv_2mortal (newSVpvn (str, len)));
447 } 444 }
448 break; 445 break;
449 446
450 case DT_WCS_LEN: 447 case DT_WCS_LEN:
451 { 448 {
452 wchar_t *wstr = va_arg (ap, wchar_t *); 449 wchar_t *wstr = va_arg (ap, wchar_t *);
453 int wlen = va_arg (ap, int); 450 int wlen = va_arg (ap, int);
454 451
455 XPUSHs (sv_2mortal (wcs2sv (wstr, wlen))); 452 XPUSHs (sv_2mortal (wcs2sv (wstr, wlen)));
456 } 453 }
457 break; 454 break;
458 455
459 case DT_LCS_LEN: 456 case DT_LCS_LEN:
460 { 457 {
461 long *lstr = va_arg (ap, long *); 458 long *lstr = va_arg (ap, long *);
462 int llen = va_arg (ap, int); 459 int llen = va_arg (ap, int);
463 460
464 XPUSHs (sv_2mortal (newSVpvn ((char *)lstr, llen * sizeof (long)))); 461 XPUSHs (sv_2mortal (newSVpvn ((char *)lstr, llen * sizeof (long))));
465 } 462 }
466 break; 463 break;
467 464
468 case DT_XEVENT: 465 case DT_XEVENT:
469 { 466 {
470 XEvent *xe = va_arg (ap, XEvent *); 467 XEvent *xe = va_arg (ap, XEvent *);
471 HV *hv = newHV (); 468 HV *hv = newHV ();
472 469
473# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0) 470# define set(name, sv) hv_store (hv, # name, sizeof (# name) - 1, sv, 0)
474# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0) 471# define setiv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSViv (val), 0)
475# define setuv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSVuv (val), 0) 472# define setuv(name, val) hv_store (hv, # name, sizeof (# name) - 1, newSVuv (val), 0)
476# undef set 473# undef set
477 474
478 setiv (type, xe->type); 475 setiv (type, xe->type);
479 setiv (send_event, xe->xany.send_event); 476 setiv (send_event, xe->xany.send_event);
480 setiv (serial, xe->xany.serial); 477 setiv (serial, xe->xany.serial);
481 478
482 switch (xe->type) 479 switch (xe->type)
483 { 480 {
484 case KeyPress: 481 case KeyPress:
485 case KeyRelease: 482 case KeyRelease:
486 case ButtonPress: 483 case ButtonPress:
487 case ButtonRelease: 484 case ButtonRelease:
488 case MotionNotify: 485 case MotionNotify:
489 setuv (window, xe->xmotion.window); 486 setuv (window, xe->xmotion.window);
490 setuv (root, xe->xmotion.root); 487 setuv (root, xe->xmotion.root);
491 setuv (subwindow, xe->xmotion.subwindow); 488 setuv (subwindow, xe->xmotion.subwindow);
492 setuv (time, xe->xmotion.time); 489 setuv (time, xe->xmotion.time);
493 setiv (x, xe->xmotion.x); 490 setiv (x, xe->xmotion.x);
494 setiv (y, xe->xmotion.y); 491 setiv (y, xe->xmotion.y);
495 setiv (row, xe->xmotion.y / term->fheight + term->view_start); 492 setiv (row, xe->xmotion.y / term->fheight + term->view_start);
496 setiv (col, xe->xmotion.x / term->fwidth); 493 setiv (col, xe->xmotion.x / term->fwidth);
497 setiv (x_root, xe->xmotion.x_root); 494 setiv (x_root, xe->xmotion.x_root);
498 setiv (y_root, xe->xmotion.y_root); 495 setiv (y_root, xe->xmotion.y_root);
499 setuv (state, xe->xmotion.state); 496 setuv (state, xe->xmotion.state);
500 497
501 switch (xe->type) 498 switch (xe->type)
502 { 499 {
503 case KeyPress: 500 case KeyPress:
504 case KeyRelease: 501 case KeyRelease:
505 setuv (keycode, xe->xkey.keycode); 502 setuv (keycode, xe->xkey.keycode);
506 break; 503 break;
507 504
508 case ButtonPress: 505 case ButtonPress:
509 case ButtonRelease: 506 case ButtonRelease:
510 setuv (button, xe->xbutton.button); 507 setuv (button, xe->xbutton.button);
511 break; 508 break;
512 509
513 case MotionNotify: 510 case MotionNotify:
514 setiv (is_hint, xe->xmotion.is_hint); 511 setiv (is_hint, xe->xmotion.is_hint);
515 break; 512 break;
516 } 513 }
517 514
518 break; 515 break;
519 516
520 case MapNotify: 517 case MapNotify:
521 case UnmapNotify: 518 case UnmapNotify:
522 case ConfigureNotify: 519 case ConfigureNotify:
523 setuv (event, xe->xconfigure.event); 520 setuv (event, xe->xconfigure.event);
524 setuv (window, xe->xconfigure.window); 521 setuv (window, xe->xconfigure.window);
525 522
526 switch (xe->type) 523 switch (xe->type)
527 { 524 {
528 case ConfigureNotify: 525 case ConfigureNotify:
529 setiv (x, xe->xconfigure.x); 526 setiv (x, xe->xconfigure.x);
530 setiv (y, xe->xconfigure.y); 527 setiv (y, xe->xconfigure.y);
531 setiv (width, xe->xconfigure.width); 528 setiv (width, xe->xconfigure.width);
532 setiv (height, xe->xconfigure.height); 529 setiv (height, xe->xconfigure.height);
533 setuv (above, xe->xconfigure.above); 530 setuv (above, xe->xconfigure.above);
534 break; 531 break;
535 } 532 }
536 533
537 break; 534 break;
538 535
539 case PropertyNotify: 536 case PropertyNotify:
540 setuv (window, xe->xproperty.window); 537 setuv (window, xe->xproperty.window);
541 setuv (atom, xe->xproperty.atom); 538 setuv (atom, xe->xproperty.atom);
542 setuv (time, xe->xproperty.time); 539 setuv (time, xe->xproperty.time);
543 setiv (state, xe->xproperty.state); 540 setiv (state, xe->xproperty.state);
544 break; 541 break;
545 542
546 case ClientMessage: 543 case ClientMessage:
547 setuv (window, xe->xclient.window); 544 setuv (window, xe->xclient.window);
548 setuv (message_type, xe->xclient.message_type); 545 setuv (message_type, xe->xclient.message_type);
549 setuv (format, xe->xclient.format); 546 setuv (format, xe->xclient.format);
550 setuv (l0, xe->xclient.data.l[0]); 547 setuv (l0, xe->xclient.data.l[0]);
551 setuv (l1, xe->xclient.data.l[1]); 548 setuv (l1, xe->xclient.data.l[1]);
552 setuv (l2, xe->xclient.data.l[2]); 549 setuv (l2, xe->xclient.data.l[2]);
553 setuv (l3, xe->xclient.data.l[3]); 550 setuv (l3, xe->xclient.data.l[3]);
554 setuv (l4, xe->xclient.data.l[4]); 551 setuv (l4, xe->xclient.data.l[4]);
555 break; 552 break;
556 } 553 }
557 554
558 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 555 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
559 } 556 }
560 break; 557 break;
561 558
562 case DT_END: 559 case DT_END:
563 goto call; 560 goto call;
564 561
565 default: 562 default:
566 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt); 563 rxvt_fatal ("FATAL: unable to pass data type %d\n", dt);
567 } 564 }
565 }
566
567 call:
568 va_end (ap);
569
570 PUTBACK;
571 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
572 SPAGAIN;
573
574 if (count)
575 {
576 SV *status = POPs;
577 count = SvTRUE (status);
568 } 578 }
569 579
570 call:
571 va_end (ap);
572
573 PUTBACK; 580 PUTBACK;
574 int count = call_pv ("urxvt::invoke", G_ARRAY | G_EVAL);
575 SPAGAIN;
576
577 if (count)
578 {
579 SV *status = POPs;
580 count = SvTRUE (status);
581 }
582
583 PUTBACK;
584 FREETMPS; 581 FREETMPS;
585 LEAVE; 582 LEAVE;
586 583
587 if (SvTRUE (ERRSV)) 584 if (SvTRUE (ERRSV))
588 { 585 {
589 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV)); 586 rxvt_warn ("perl hook %d evaluation error: %s", htype, SvPV_nolen (ERRSV));
590 ungrab (term); // better lose the grab than the session 587 ungrab (term); // better lose the grab than the session
591 } 588 }
592 589
593 event_consumed = !!count; 590 event_consumed = !!count;
594 } 591 }
595 catch (...)
596 {
597 swap (perl_environ, environ);
598 throw;
599 }
600 else 592 else
601 event_consumed = false; 593 event_consumed = false;
602 594
603 // post-handling of some events 595 // post-handling of some events
604 if (htype == HOOK_REFRESH_BEGIN) 596 if (htype == HOOK_REFRESH_BEGIN)
614 SvREFCNT_dec ((SV *)term->perl.self); 606 SvREFCNT_dec ((SV *)term->perl.self);
615 607
616 // don't allow further calls 608 // don't allow further calls
617 term->perl.self = 0; 609 term->perl.self = 0;
618 } 610 }
619
620 swap (perl_environ, environ);
621 611
622 return event_consumed; 612 return event_consumed;
623} 613}
624 614
625///////////////////////////////////////////////////////////////////////////// 615/////////////////////////////////////////////////////////////////////////////
778 const_iv (XIMDontChange), 768 const_iv (XIMDontChange),
779# endif 769# endif
780# endif 770# endif
781 }; 771 };
782 772
783 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 773 for (civ = const_iv + ARRAY_LENGTH(const_iv); civ-- > const_iv; )
784 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 774 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
785} 775}
786 776
787void 777void
788warn (const char *msg) 778warn (const char *msg)
1456# include "rsinc.h" 1446# include "rsinc.h"
1457# undef def 1447# undef def
1458# undef reserve 1448# undef reserve
1459 }; 1449 };
1460 1450
1461 rs = rslist + sizeof (rslist) / sizeof (rslist [0]); 1451 rs = rslist + ARRAY_LENGTH(rslist);
1462 1452
1463 if (*name) 1453 if (*name)
1464 { 1454 {
1465 do { 1455 do {
1466 if (rs-- == rslist) 1456 if (rs-- == rslist)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines