6df0a78a2b605771a3b124f625cff0553d91373d
[screen-lua.git] / src / script.c
blob6df0a78a2b605771a3b124f625cff0553d91373d
1 /* Copyright (c) 2008 Sadrul Habib Chowdhury (sadrul@users.sf.net)
2 * 2009 Rui Guo (firemeteor.guo@gmail.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program (see the file COPYING); if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 ****************************************************************
22 #include "config.h"
23 #include "screen.h"
24 #include <stddef.h>
26 /*Binding structure & functions*/
28 struct binding *bindings = NULL;
30 static void
31 register_binding (struct binding *new_binding)
33 if (!new_binding->registered)
35 new_binding->b_next = bindings;
36 bindings = new_binding;
37 new_binding->registered = 1;
41 #ifdef LUA_BINDING
42 extern struct binding lua_binding;
43 #endif
45 void LoadBindings(void)
47 #ifdef LUA_BINDING
48 register_binding(&lua_binding);
49 #endif
52 void
53 FinalizeBindings (void)
55 struct binding *binding=bindings;
56 while(binding)
58 if (binding->inited)
59 binding->bd_Finit();
60 binding = binding->b_next;
64 void
65 ScriptSource(int argc, const char **argv)
67 int ret = 0;
68 int async = 0;
69 const char *bd_select = 0, *script;
70 struct binding *binding = bindings;
72 /* Parse the commandline options
73 * sourcescript [-async|-a] [-binding|-b <binding>] script
75 while (*argv && **argv == '-') {
76 /* check for (-a | -async) */
77 if ((*argv[1] == 'a' && !*argv[2])
78 || strcmp(*argv, "-async") == 0)
79 async = 1;
80 /* check for (-b | -binding) */
81 else if ((*argv[1] == 'b' && !*argv[2])
82 || strcmp(*argv, "-binding") == 0) {
83 argv++;
84 bd_select = *argv;
86 argv++;
88 script = *argv;
90 while (binding) {
91 if (!bd_select || strcmp(bd_select, binding->name) == 0) {
92 /*dynamically initialize the binding*/
93 if (!binding->inited)
95 binding->bd_Init();
96 binding->inited = 1;
99 /*and source the script*/
100 if (ret = binding->bd_Source(script, async))
101 break;
103 binding = binding->b_next;
105 if (!ret)
106 LMsg(1, "Could not source specified script %s", script);
110 ScriptCall(const char *func, const char **argv)
112 /*TODO*/
113 return LuaCall(func, argv);
116 void
117 ScriptCmd(int argc, const char **argv)
119 const char * sub = *argv;
120 argv++;argc--;
121 if (!strcmp(sub, "call"))
122 ScriptCall(*argv, argv+1);
123 else if (!strcmp(sub, "source"))
124 ScriptSource(argc, argv);
127 /* Event notification handling */
129 struct gevents globalevents;
131 /* To add a new event, introduce a field for that event to the object in
132 * question, and don't forget to put an descriptor here. NOTE: keep the
133 * name field sorted in alphabet order, the searching relies on it.
135 * the params string specifies the expected parameters. The length of the
136 * string equals to the number of parameters. Each char specifies the type of
137 * the parameter, with its meaning similar to those in printf().
139 * s: string (char *)
140 * S: string array (char **)
141 * i: signed int
142 * d: display
145 struct sev_description {
146 char *name;
147 char *params;
148 int offset;
149 } event_table[] = {
150 /* Global events */
151 {"global_cmdexecuted", "sS", offsetof(struct gevents, cmdexecuted)},
152 {"global_detached", "di", offsetof(struct gevents, detached)},
153 /* The command "detach" triggers both 'cmdexecuted' and 'detached' events.
154 However, we need the 'detached' event to trigger callbacks from remote detaches.
157 /* Window events */
158 {"window_resize", "", offsetof(struct win, w_sev.resize)},
159 {"window_can_resize", "", offsetof(struct win, w_sev.canresize)}
162 /* Get the event queue with the given name in the obj. If the obj is NULL,
163 * global events are searched. If no event is found, a NULL is returned.
165 struct script_event *
166 object_get_event(char *obj, const char *name)
168 int lo, hi, n, cmp;
169 if (!obj)
170 obj = (char *)&globalevents;
172 lo = 0;
173 n = hi = sizeof(event_table) / sizeof(struct sev_description);
174 while (lo < hi)
176 int half;
177 half = (lo + hi) >> 1;
178 cmp = strcmp(name, event_table[half].name);
179 if (cmp > 0)
180 lo = half + 1;
181 else
182 hi = half;
185 if (lo >= n || strcmp(name, event_table[lo].name))
186 return 0;
187 else
189 /*found an entry.*/
190 struct script_event *res;
191 res = (struct script_event *) (obj + event_table[lo].offset);
192 /*Setup the parameter record.*/
193 res->params = event_table[lo].params;
194 return res;
198 /* Put a listener in a proper position in the chain
199 * according to the privlege.
200 * Not insert duplicate entry. return zero if successful.*/
202 register_listener(struct script_event *ev, struct listener *l)
204 unsigned int priv = l->priv;
205 struct listener *p, *iter = &ev->listeners;
207 #if 0
208 while(iter->chain && priv >= iter->chain->priv)
210 iter = iter->chain;
211 /* return if duplicate found*/
212 if (iter->handler == l->handler
213 && iter->dispatcher == l->dispatcher)
214 return 1;
216 #endif
217 p = iter;
219 l->chain = p->chain;
220 l->prev = p;
221 if (p->chain)
222 p->chain->prev = l;
223 p->chain = l;
224 return 0;
227 void
228 unregister_listener(struct listener *l)
230 struct listener *p = l->prev;
231 p->chain = l->chain;
232 if (l->chain)
233 l->chain->prev = p;
234 l->chain = l->prev = 0;
235 l->handler = 0;
236 free(l);
239 /* Trigger event with given parameters.*/
241 trigger_sevent(struct script_event *ev, VA_DOTS)
243 int res = 0;
244 struct listener *chain;
245 char *params;
246 VA_LIST(va);
247 /*invalid or un-registered event structure*/
248 if (!ev || !ev->params)
249 return 0;
251 /*process the chain in order, stop if any of the handler returns true.*/
252 chain = ev->listeners.chain;
253 params = ev->params;
254 while (chain)
256 VA_START(va, ev);
257 res = chain->dispatcher(chain->handler, params, va);
258 VA_END(va);
259 if (res)
260 break;
261 chain = chain->chain;
264 return res;
267 #define ALL_SCRIPTS(fn, params, stop) do { \
268 struct binding *iter; \
269 for (iter = bindings; iter; iter = iter->b_next) \
271 if (iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
272 break; \
274 } while (0)
276 void ScriptForeWindowChanged(void)
278 int ret;
279 ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
282 int ScriptProcessCaption(const char *str, struct win *win, int len)
284 int ret = 0;
285 ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
286 return ret;