Change the script command to the stlye of 'layout' commands.
[screen-lua.git] / src / script.c
blob156ca31f50a4ea04689be20cd641e397aa340d80
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);
106 void
107 ScriptCmd(int argc, const char **argv)
109 const char * sub = *argv;
110 argv++;argc--;
111 if (!strcmp(sub, "call"))
112 LuaCall(argv);
113 else if (!strcmp(sub, "source"))
114 ScriptSource(argc, argv);
117 /* Event notification handling */
119 struct gevents {
120 struct script_event cmdexecuted;
121 } globalevents;
123 /* To add new event, introduce a filed for that event to the object in
124 * question, don't forget to put a name, offset pair here. NOTE: keep the
125 * name field sorted in alphabet order, the searching relies on it.
128 struct {
129 char *name;
130 int offset;
131 } event_table[] = {
132 {"global_cmdexecuted", offsetof(struct gevents, cmdexecuted)},
133 {"window_resize", offsetof(struct win, resize)},
134 {"window_can_resize", offsetof(struct win, canresize)}
137 struct script_event *
138 get_object_event_queue(char *name, char *obj) {
139 int lo, hi, n, cmp;
140 if (!obj)
141 obj = (char *)&globalevents;
143 lo = 0;
144 n = hi = sizeof(event_table);
145 while (lo < hi) {
146 int half;
147 half = (lo + hi) >> 1;
148 cmp = strcmp(name, event_table[half].name);
149 if (cmp > 0)
150 lo = half + 1;
151 else
152 hi = half;
155 if (lo >= n || strcmp(name, event_table[lo].name))
156 return 0;
157 else
158 return (struct event *)(obj + event_table[lo].offset);
161 void
162 register_listener(struct script_event * event, struct listener *listener)
166 #define ALL_SCRIPTS(fn, params, stop) do { \
167 struct binding *iter; \
168 for (iter = bindings; iter; iter = iter->b_next) \
170 if (iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
171 break; \
173 } while (0)
175 void ScriptForeWindowChanged(void)
177 int ret;
178 ALL_SCRIPTS(sf_ForeWindowChanged, (), 0);
181 int ScriptProcessCaption(const char *str, struct win *win, int len)
183 int ret = 0;
184 ALL_SCRIPTS(sf_ProcessCaption, (str, win, len), 1);
185 return ret;
188 int ScriptCommandExecuted(const char *command, const char **args, int argc)
190 int ret = 0;
191 ALL_SCRIPTS(sf_CommandExecuted, (command, args, argc), 0);
192 return ret;