summaryrefslogtreecommitdiff
path: root/src/script.c
blob: 2b09a9ceb3ee70d82fba5c3886a65cec32555870 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
 * 2009 Rui Guo (firemeteor.guo@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see the file COPYING); if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 ****************************************************************
 */

#include "config.h"
#include "screen.h"
#include <stddef.h>

/*Binding structure & functions*/

struct binding *bindings = NULL;

static void
register_binding (struct binding *new_binding)
{
  if (!new_binding->registered)
    {
      new_binding->b_next = bindings;
      bindings = new_binding;
      new_binding->registered = 1;
    }
}

#ifdef LUA_BINDING
extern struct binding lua_binding;
#endif

void LoadBindings(void)
{
#ifdef LUA_BINDING
  register_binding(&lua_binding);
#endif
}

void
FinalizeBindings (void)
{
  struct binding *binding=bindings;
  while(binding)
    {
      if (binding->inited)
        binding->bd_Finit();
      binding = binding->b_next;
    }
}

void
ScriptSource(int argc, const char **argv)
{
  int ret = 0;
  int async = 0;
  const char *bd_select = 0, *script;
  struct binding *binding = bindings;

  /* Parse the commandline options
   * sourcescript [-async|-a] [-binding|-b <binding>] script
   */
  while (*argv && **argv == '-')
    {
      /* check for (-a | -async) */
      const char *arg = *argv;
      if ((arg[1] == 'a' && !arg[2])
          || strcmp(arg, "-async") == 0)
        async = 1;
      /* check for (-b | -binding) */
      else if ((arg[1] == 'b' && !arg[2])
               || strcmp(arg, "-binding") == 0) {
          argv++;
          bd_select = *argv;
      }
      argv++;
  }
  script = *argv;

  while (binding) {
      if (!bd_select || strcmp(bd_select, binding->name) == 0) {
          /*dynamically initialize the binding*/
          if (!binding->inited)
	    {
	      binding->bd_Init();
	      binding->inited = 1;
	    }

          /*and source the script*/
          if (ret = binding->bd_Source(script, async))
            break;
      }
      binding = binding->b_next;
  }
  if (!ret)
    LMsg(1, "Could not source specified script %s", script);
}

int
ScriptCall(const char *func, const char **argv)
{
  /*TODO*/
  return LuaCall(func, argv);
}

void
ScriptCmd(int argc, const char **argv)
{
  const char * sub = *argv;
  argv++;argc--;
  if (!strcmp(sub, "call"))
    ScriptCall(*argv, argv+1);
  else if (!strcmp(sub, "source"))
    ScriptSource(argc, argv);
}

/* Event notification handling */

struct gevents globalevents;

/* To add a new event, introduce a field for that event to the object in
 * question, and don't forget to put an descriptor here.  NOTE: keep the
 * name field sorted in alphabet order, the searching relies on it.
 *
 * the params string specifies the expected parameters. The length of the
 * string equals to the number of parameters. Each char specifies the type of
 * the parameter, with its meaning similar to those in printf().
 *
 * s: string (char *)
 * S: string array (char **)
 * i: signed int
 * d: display
 */

struct sev_description {
    char *name;
    char *params;
    int offset;
} event_table[] = {
      /* Global events */
      {"global_cmdexecuted", "sS", offsetof(struct gevents, cmdexecuted)},
      {"global_detached", "di", offsetof(struct gevents, detached)},
	  /* The command "detach" triggers both 'cmdexecuted' and 'detached' events.
	     However, we need the 'detached' event to trigger callbacks from remote detaches.
	   */

      /* Window events */
      {"window_resize", "", offsetof(struct win, w_sev.resize)},
      {"window_can_resize", "", offsetof(struct win, w_sev.canresize)}
};

/* Get the event queue with the given name in the obj.  If the obj is NULL,
 * global events are searched.  If no event is found, a NULL is returned.
 */
struct script_event *
object_get_event(char *obj, const char *name) 
{
  int lo, hi, n, cmp;
  if (!obj)
    obj = (char *)&globalevents;

  lo = 0;
  n = hi = sizeof(event_table) / sizeof(struct sev_description);
  while (lo < hi) 
    {
      int half;
      half = (lo + hi) >> 1;
      cmp = strcmp(name, event_table[half].name);
      if (cmp > 0)
        lo = half + 1;
      else
        hi = half;
    }

  if (lo >= n || strcmp(name, event_table[lo].name))
    return 0;
  else 
    {
      /*found an entry.*/
      struct script_event *res;
      res = (struct script_event *) (obj + event_table[lo].offset);
      /*Setup the parameter record.*/
      res->params = event_table[lo].params;
      return res;
    }
}

/* Put a listener in a proper position in the chain 
 * according to the privlege.
 * Not insert duplicate entry. return zero if successful.*/
int
register_listener(struct script_event *ev, struct listener *l)
{
  unsigned int priv = l->priv;
  struct listener *p, *iter = &ev->listeners;

#if 0
  while(iter->chain && priv >= iter->chain->priv)
    {
      iter = iter->chain;
      /* return if duplicate found*/
      if (iter->handler == l->handler
          && iter->dispatcher == l->dispatcher)
        return 1;
    }
#endif
  p = iter;

  l->chain = p->chain;
  l->prev = p;
  if (p->chain)
    p->chain->prev = l;
  p->chain = l;
  return 0;
}

void
unregister_listener(struct listener *l)
{
  struct listener *p = l->prev;
  p->chain = l->chain;
  if (l->chain)
    l->chain->prev = p;
  l->chain = l->prev = 0;
  l->handler = 0;
  free(l);
}

/* Trigger event with given parameters.*/
int
trigger_sevent(struct script_event *ev, VA_DOTS)
{
  int res = 0;
  struct listener *chain;
  char *params;
  VA_LIST(va);
  /*invalid or un-registered event structure*/
  if (!ev || !ev->params)
    return 0;

  /*process the chain in order, stop if any of the handler returns true.*/
  chain = ev->listeners.chain;
  params = ev->params;
  while (chain)
    {
      VA_START(va, ev);
      res = chain->dispatcher(chain->handler, params, va);
      VA_END(va);
      if (res)
        break;
      chain = chain->chain;
    }

  return res;
}

#define ALL_SCRIPTS(fn, params, stop) do { \
  struct binding *iter; \
  for (iter = bindings; iter; iter = iter->b_next) \
    { \
      if (iter->fns && iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
	break; \
    } \
} while (0)

void ScriptForeWindowChanged(void)
{
  int ret;
  ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
}

int ScriptProcessCaption(const char *str, struct win *win, int len)
{
  int ret = 0;
  ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
  return ret;
}