4759c27558fe905c87514d9912fdbe4dee79ea56
[screen-lua.git] / src / script.c
blob4759c27558fe905c87514d9912fdbe4dee79ea56
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)
94 binding->bd_Init();
96 /*and source the script*/
97 if (ret = binding->bd_Source(script, async))
98 break;
100 binding = binding->b_next;
102 if (!ret)
103 LMsg(1, "Could not source specified script %s", script);
107 ScriptCall(const char *func, const char **argv)
109 /*TODO*/
110 return LuaCall(func, argv);
113 void
114 ScriptCmd(int argc, const char **argv)
116 const char * sub = *argv;
117 argv++;argc--;
118 if (!strcmp(sub, "call"))
119 ScriptCall(*argv, argv+1);
120 else if (!strcmp(sub, "source"))
121 ScriptSource(argc, argv);
124 /* Event notification handling */
126 struct gevents globalevents;
128 /* To add a new event, introduce a field for that event to the object in
129 * question, and don't forget to put an descriptor here. NOTE: keep the
130 * name field sorted in alphabet order, the searching relies on it.
132 * the params string specifies the expected parameters. The length of the
133 * string equals to the number of parameters. Each char specifies the type of
134 * the parameter, with its meaning similar to those in printf().
136 * s: string (char *)
137 * S: string array (char **)
138 * i: signed int
142 struct sev_description {
143 char *name;
144 char *params;
145 int offset;
146 } event_table[] = {
147 {"global_cmdexecuted", "sS", offsetof(struct gevents, cmdexecuted)},
148 {"global_detached", "", offsetof(struct gevents, detached)},
149 {"window_resize", "", offsetof(struct win, w_sev.resize)},
150 {"window_can_resize", "", offsetof(struct win, w_sev.canresize)}
153 /* Get the event queue with the given name in the obj. If the obj is NULL,
154 * global events are searched. If no event is found, a NULL is returned.
156 struct script_event *
157 object_get_event(char *obj, const char *name)
159 int lo, hi, n, cmp;
160 if (!obj)
161 obj = (char *)&globalevents;
163 lo = 0;
164 n = hi = sizeof(event_table) / sizeof(struct sev_description);
165 while (lo < hi)
167 int half;
168 half = (lo + hi) >> 1;
169 cmp = strcmp(name, event_table[half].name);
170 if (cmp > 0)
171 lo = half + 1;
172 else
173 hi = half;
176 if (lo >= n || strcmp(name, event_table[lo].name))
177 return 0;
178 else
180 /*found an entry.*/
181 struct script_event *res;
182 res = (struct script_event *) (obj + event_table[lo].offset);
183 /*Setup the parameter record.*/
184 res->params = event_table[lo].params;
185 return res;
189 /* Put a listener in a proper position in the chain
190 * according to the privlege.
191 * Not insert duplicate entry. return zero if successful.*/
193 register_listener(struct script_event *ev, struct listener *l)
195 unsigned int priv = l->priv;
196 struct listener *p, *iter = &ev->listeners;
198 while(iter->chain && priv >= iter->chain->priv)
200 iter = iter->chain;
201 /* return if duplicate found*/
202 if (iter->handler == l->handler
203 && iter->dispatcher == l->dispatcher)
204 return 1;
206 p = iter;
208 while(iter->chain)
210 iter = iter->chain;
211 /* return if duplicate found*/
212 if (iter->handler == l->handler
213 && iter->dispatcher == l->dispatcher)
214 return 1;
217 l->chain = p->chain;
218 l->prev = p;
219 if (p->chain)
220 p->chain->prev = l;
221 p->chain = l;
222 return 0;
225 void
226 unregister_listener(struct listener *l)
228 struct listener *p = l->prev;
229 p->chain = l->chain;
230 if (l->chain)
231 l->chain->prev = p;
232 l->chain = l->prev = 0;
233 l->handler = 0;
234 free(l);
237 /* Trigger event with given parameters.*/
239 trigger_sevent(struct script_event *ev, VA_DOTS)
241 int res = 0;
242 struct listener *chain;
243 char *params;
244 VA_LIST(va);
245 /*invalid or un-registered event structure*/
246 if (!ev || !ev->params)
247 return 0;
249 /*process the chain in order, stop if any of the handler returns true.*/
250 chain = ev->listeners.chain;
251 params = ev->params;
252 while (chain)
254 VA_START(va, ev);
255 res = chain->dispatcher(chain->handler, params, va);
256 VA_END(va);
257 if (res)
258 break;
259 chain = chain->chain;
262 return res;
265 #define ALL_SCRIPTS(fn, params, stop) do { \
266 struct binding *iter; \
267 for (iter = bindings; iter; iter = iter->b_next) \
269 if (iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
270 break; \
272 } while (0)
274 void ScriptForeWindowChanged(void)
276 int ret;
277 ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
280 int ScriptProcessCaption(const char *str, struct win *win, int len)
282 int ret = 0;
283 ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
284 return ret;