9e2fd39f4dfcac3991e18ded6ab87571a50e9b82
[screen-lua.git] / src / script.c
blob9e2fd39f4dfcac3991e18ded6ab87571a50e9b82
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 extern struct binding py_binding;
47 void LoadBindings(void)
49 #ifdef LUA_BINDING
50 register_binding(&lua_binding);
51 #endif
53 register_binding(&py_binding);
56 void
57 FinalizeBindings (void)
59 struct binding *binding=bindings;
60 while(binding)
62 if (binding->inited)
63 binding->bd_Finit();
64 binding = binding->b_next;
68 void
69 ScriptSource(int argc, const char **argv)
71 int ret = 0;
72 int async = 0;
73 const char *bd_select = 0, *script;
74 struct binding *binding = bindings;
76 /* Parse the commandline options
77 * sourcescript [-async|-a] [-binding|-b <binding>] script
79 while (*argv && **argv == '-')
81 /* check for (-a | -async) */
82 const char *arg = *argv;
83 if ((arg[1] == 'a' && !arg[2])
84 || strcmp(arg, "-async") == 0)
85 async = 1;
86 /* check for (-b | -binding) */
87 else if ((arg[1] == 'b' && !arg[2])
88 || strcmp(arg, "-binding") == 0) {
89 argv++;
90 bd_select = *argv;
92 argv++;
94 script = *argv;
96 while (binding) {
97 if (!bd_select || strcmp(bd_select, binding->name) == 0) {
98 /*dynamically initialize the binding*/
99 if (!binding->inited)
101 binding->bd_Init();
102 binding->inited = 1;
105 /*and source the script*/
106 if (ret = binding->bd_Source(script, async))
107 break;
109 binding = binding->b_next;
111 if (!ret)
112 LMsg(1, "Could not source specified script %s", script);
116 ScriptCall(const char *func, const char **argv)
118 /*TODO*/
119 return LuaCall(func, argv);
122 void
123 ScriptCmd(int argc, const char **argv)
125 const char * sub = *argv;
126 argv++;argc--;
127 if (!strcmp(sub, "call"))
128 ScriptCall(*argv, argv+1);
129 else if (!strcmp(sub, "source"))
130 ScriptSource(argc, argv);
133 /* Event notification handling */
135 struct gevents globalevents;
137 /* To add a new event, introduce a field for that event to the object in
138 * question, and don't forget to put an descriptor here. NOTE: keep the
139 * name field sorted in alphabet order, the searching relies on it.
141 * the params string specifies the expected parameters. The length of the
142 * string equals to the number of parameters. Each char specifies the type of
143 * the parameter, with its meaning similar to those in printf().
145 * s: string (char *)
146 * S: string array (char **)
147 * i: signed int
148 * d: display
151 struct sev_description {
152 char *name;
153 char *params;
154 int offset;
155 } event_table[] = {
156 /* Global events */
157 {"global_cmdexecuted", "sS", offsetof(struct gevents, cmdexecuted)},
158 {"global_detached", "di", offsetof(struct gevents, detached)},
159 /* The command "detach" triggers both 'cmdexecuted' and 'detached' events.
160 However, we need the 'detached' event to trigger callbacks from remote detaches.
163 /* Window events */
164 {"window_resize", "", offsetof(struct win, w_sev.resize)},
165 {"window_can_resize", "", offsetof(struct win, w_sev.canresize)}
168 /* Get the event queue with the given name in the obj. If the obj is NULL,
169 * global events are searched. If no event is found, a NULL is returned.
171 struct script_event *
172 object_get_event(char *obj, const char *name)
174 int lo, hi, n, cmp;
175 if (!obj)
176 obj = (char *)&globalevents;
178 lo = 0;
179 n = hi = sizeof(event_table) / sizeof(struct sev_description);
180 while (lo < hi)
182 int half;
183 half = (lo + hi) >> 1;
184 cmp = strcmp(name, event_table[half].name);
185 if (cmp > 0)
186 lo = half + 1;
187 else
188 hi = half;
191 if (lo >= n || strcmp(name, event_table[lo].name))
192 return 0;
193 else
195 /*found an entry.*/
196 struct script_event *res;
197 res = (struct script_event *) (obj + event_table[lo].offset);
198 /*Setup the parameter record.*/
199 res->params = event_table[lo].params;
200 return res;
204 /* Put a listener in a proper position in the chain
205 * according to the privlege.
206 * Not insert duplicate entry. return zero if successful.*/
208 register_listener(struct script_event *ev, struct listener *l)
210 unsigned int priv = l->priv;
211 struct listener *p, *iter = &ev->listeners;
213 #if 0
214 while(iter->chain && priv >= iter->chain->priv)
216 iter = iter->chain;
217 /* return if duplicate found*/
218 if (iter->handler == l->handler
219 && iter->dispatcher == l->dispatcher)
220 return 1;
222 #endif
223 p = iter;
225 l->chain = p->chain;
226 l->prev = p;
227 if (p->chain)
228 p->chain->prev = l;
229 p->chain = l;
230 return 0;
233 void
234 unregister_listener(struct listener *l)
236 struct listener *p = l->prev;
237 p->chain = l->chain;
238 if (l->chain)
239 l->chain->prev = p;
240 l->chain = l->prev = 0;
241 l->handler = 0;
242 free(l);
245 /* Trigger event with given parameters.*/
247 trigger_sevent(struct script_event *ev, VA_DOTS)
249 int res = 0;
250 struct listener *chain;
251 char *params;
252 VA_LIST(va);
253 /*invalid or un-registered event structure*/
254 if (!ev || !ev->params)
255 return 0;
257 /*process the chain in order, stop if any of the handler returns true.*/
258 chain = ev->listeners.chain;
259 params = ev->params;
260 while (chain)
262 VA_START(va, ev);
263 res = chain->dispatcher(chain->handler, params, va);
264 VA_END(va);
265 if (res)
266 break;
267 chain = chain->chain;
270 return res;
273 #define ALL_SCRIPTS(fn, params, stop) do { \
274 struct binding *iter; \
275 for (iter = bindings; iter; iter = iter->b_next) \
277 if (iter->fns && iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
278 break; \
280 } while (0)
282 void ScriptForeWindowChanged(void)
284 int ret;
285 ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
288 int ScriptProcessCaption(const char *str, struct win *win, int len)
290 int ret = 0;
291 ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
292 return ret;