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)
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 ****************************************************************
26 /*Binding structure & functions*/
28 struct binding
*bindings
= NULL
;
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;
42 extern struct binding lua_binding
;
45 void LoadBindings(void)
48 register_binding(&lua_binding
);
53 FinalizeBindings (void)
55 struct binding
*binding
=bindings
;
60 binding
= binding
->b_next
;
65 ScriptSource(int argc
, const char **argv
)
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
== '-')
77 /* check for (-a | -async) */
78 const char *arg
= *argv
;
79 if ((arg
[1] == 'a' && !arg
[2])
80 || strcmp(arg
, "-async") == 0)
82 /* check for (-b | -binding) */
83 else if ((arg
[1] == 'b' && !arg
[2])
84 || strcmp(arg
, "-binding") == 0) {
93 if (!bd_select
|| strcmp(bd_select
, binding
->name
) == 0) {
94 /*dynamically initialize the binding*/
101 /*and source the script*/
102 if (ret
= binding
->bd_Source(script
, async
))
105 binding
= binding
->b_next
;
108 LMsg(1, "Could not source specified script %s", script
);
112 ScriptCall(const char *func
, const char **argv
)
115 return LuaCall(func
, argv
);
119 ScriptCmd(int argc
, const char **argv
)
121 const char * sub
= *argv
;
123 if (!strcmp(sub
, "call"))
124 ScriptCall(*argv
, argv
+1);
125 else if (!strcmp(sub
, "source"))
126 ScriptSource(argc
, argv
);
129 /* Event notification handling */
131 struct gevents globalevents
;
133 /* To add a new event, introduce a field for that event to the object in
134 * question, and don't forget to put an descriptor here. NOTE: keep the
135 * name field sorted in alphabet order, the searching relies on it.
137 * the params string specifies the expected parameters. The length of the
138 * string equals to the number of parameters. Each char specifies the type of
139 * the parameter, with its meaning similar to those in printf().
142 * S: string array (char **)
147 struct sev_description
{
153 {"global_cmdexecuted", "sS", offsetof(struct gevents
, cmdexecuted
)},
154 {"global_detached", "di", offsetof(struct gevents
, detached
)},
155 /* The command "detach" triggers both 'cmdexecuted' and 'detached' events.
156 However, we need the 'detached' event to trigger callbacks from remote detaches.
160 {"window_resize", "", offsetof(struct win
, w_sev
.resize
)},
161 {"window_can_resize", "", offsetof(struct win
, w_sev
.canresize
)}
164 /* Get the event queue with the given name in the obj. If the obj is NULL,
165 * global events are searched. If no event is found, a NULL is returned.
167 struct script_event
*
168 object_get_event(char *obj
, const char *name
)
172 obj
= (char *)&globalevents
;
175 n
= hi
= sizeof(event_table
) / sizeof(struct sev_description
);
179 half
= (lo
+ hi
) >> 1;
180 cmp
= strcmp(name
, event_table
[half
].name
);
187 if (lo
>= n
|| strcmp(name
, event_table
[lo
].name
))
192 struct script_event
*res
;
193 res
= (struct script_event
*) (obj
+ event_table
[lo
].offset
);
194 /*Setup the parameter record.*/
195 res
->params
= event_table
[lo
].params
;
200 /* Put a listener in a proper position in the chain
201 * according to the privlege.
202 * Not insert duplicate entry. return zero if successful.*/
204 register_listener(struct script_event
*ev
, struct listener
*l
)
206 unsigned int priv
= l
->priv
;
207 struct listener
*p
, *iter
= &ev
->listeners
;
210 while(iter
->chain
&& priv
>= iter
->chain
->priv
)
213 /* return if duplicate found*/
214 if (iter
->handler
== l
->handler
215 && iter
->dispatcher
== l
->dispatcher
)
230 unregister_listener(struct listener
*l
)
232 struct listener
*p
= l
->prev
;
236 l
->chain
= l
->prev
= 0;
241 /* Trigger event with given parameters.*/
243 trigger_sevent(struct script_event
*ev
, VA_DOTS
)
246 struct listener
*chain
;
249 /*invalid or un-registered event structure*/
250 if (!ev
|| !ev
->params
)
253 /*process the chain in order, stop if any of the handler returns true.*/
254 chain
= ev
->listeners
.chain
;
259 res
= chain
->dispatcher(chain
->handler
, params
, va
);
263 chain
= chain
->chain
;
269 #define ALL_SCRIPTS(fn, params, stop) do { \
270 struct binding *iter; \
271 for (iter = bindings; iter; iter = iter->b_next) \
273 if (iter->fns && iter->fns->fn && (ret = (iter->fns->fn params)) && stop) \
278 void ScriptForeWindowChanged(void)
281 ALL_SCRIPTS(sf_ForeWindowChanged
, (), 0);
284 int ScriptProcessCaption(const char *str
, struct win
*win
, int len
)
287 ALL_SCRIPTS(sf_ProcessCaption
, (str
, win
, len
), 1);