summaryrefslogtreecommitdiffstats
path: root/src/iom_perl.h
blob: eb73190f9b019ef8bd9f2c7094a9e4fea92df3ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#define IOM_CLASS "urxvt"
typedef int IOM_CHAINED;

static SV *
iom_new_ref (HV *hv, const char *klass)
{
  return sv_bless (newRV ((SV *)hv), gv_stashpv (klass, 1));
}

/////////////////////////////////////////////////////////////////////////////

#define SvWATCHER(sv) (perl_watcher *)SvPTR (sv, IOM_CLASS "::watcher")

struct perl_watcher
{
  SV *cbsv;
  HV *self;

  perl_watcher ()
  : cbsv (0)
  {
  }

  ~perl_watcher ()
  {
    SvREFCNT_dec (cbsv);
  }

  void cb (SV *cb)
  {
    SvREFCNT_dec (cbsv);
    cbsv = newSVsv (cb);
  }

  void invoke (const char *type, SV *self, int arg = -1);
};

void
perl_watcher::invoke (const char *type, SV *self, int arg)
{
  dSP;

  ENTER;
  SAVETMPS;

  PUSHMARK (SP);

  XPUSHs (sv_2mortal (self));

  if (arg >= 0)
    XPUSHs (sv_2mortal (newSViv (arg)));

  PUTBACK;
  call_sv (cbsv, G_VOID | G_EVAL | G_DISCARD);
  SPAGAIN;

  PUTBACK;
  FREETMPS;
  LEAVE;

  if (SvTRUE (ERRSV))
    rxvt_warn ("%s callback evaluation error: %s", type, SvPV_nolen (ERRSV));
}

#define newSVtimer(timer) iom_new_ref ((timer)->self, IOM_CLASS "::timer")
#define SvTIMER(sv) (timer *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::timer")

struct timer : perl_watcher, ev::timer
{
  timer ()
  {
    set<timer, &timer::execute> (this);
  }

  void execute (ev::timer &w, int revents)
  {
    invoke (IOM_CLASS "::timer", newSVtimer (this));
  }
};

#define newSViow(iow) iom_new_ref ((iow)->self, IOM_CLASS "::iow")
#define SvIOW(sv) (iow *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::iow")

struct iow : perl_watcher, ev::io
{
  iow ()
  {
    set<iow, &iow::execute> (this);
  }

  void execute (ev::io &w, int revents)
  {
    invoke (IOM_CLASS "::iow", newSViow (this), revents);
  }
};

#define newSViw(iw) iom_new_ref ((iw)->self, IOM_CLASS "::iw")
#define SvIW(sv) (iw *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::iw")

struct iw : perl_watcher, ev::idle
{
  iw ()
  {
    set<iw, &iw::execute> (this);
  }

  void execute (ev::idle &w, int revents)
  {
    invoke (IOM_CLASS "::iw", newSViw (this));
  }
};

#define newSVpw(pw) iom_new_ref ((pw)->self, IOM_CLASS "::pw")
#define SvPW(sv) (pw *)(perl_watcher *)SvPTR ((sv), IOM_CLASS "::pw")

struct pw : perl_watcher, ev::child
{
  pw ()
  {
    set<pw, &pw::execute> (this);
  }

  void execute (ev::child &w, int revents)
  {
    invoke (IOM_CLASS "::pw", newSVpw (this), w.rstatus);
  }
};