Fawkes API  Fawkes Development Version
trigger.cpp
1 
2 /***************************************************************************
3  * trigger.cpp - Fawkes Lua Trigger Support
4  *
5  * Created: Mon Jun 23 10:28:05 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <lua/trigger.h>
24 #include <lua/context.h>
25 
26 #include <core/exceptions/system.h>
27 
28 #include <cstring>
29 #include <cstdlib>
30 #include <cstdio>
31 
32 namespace fawkes {
33 
34 /** @class LuaTriggerManager <lua/trigger.h>
35  * Lua Trigger Manager.
36  * This class interfaces with a trigger sub-system running inside Lua (with
37  * the trigger system provided by Fawkes' Lua packages).
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor.
42  * @param lua Lua context to use that has a running trigger system
43  * @param trigger_var the name of the (global) variable pointing to the
44  * trigger system
45  */
46 LuaTriggerManager::LuaTriggerManager(LuaContext *lua, const char *trigger_var)
47 {
48  __lua = lua;
49  __trigger_var = strdup(trigger_var);
50 }
51 
52 
53 /** Destructor. */
55 {
56  free(__trigger_var);
57 }
58 
59 
60 /** Cause a trigger event.
61  * @param event name of the event to trigger
62  * @param param_format a format string for a string passed plain as Lua code
63  * in the trigger() function call as second argument. The code executed looks
64  * like "lua_trigger_var:trigger(event, string)" with string being what you
65  * pass, so it can be any number of arguments, for instance you could pass
66  * @code
67  * {x=%f, y=%f}
68  * @endcode
69  * which would result in a table set with the two floats you provide in the
70  * ellipsis.
71  */
72 void
73 LuaTriggerManager::trigger(const char *event, const char *param_format, ...)
74 {
75  va_list args;
76  char *params = NULL;
77  if ( param_format ) {
78  va_start(args, param_format);
79  if (vasprintf(&params, param_format, args) == -1) {
80  throw OutOfMemoryException("Lua trigger: Could not allocate param string");
81  }
82  va_end(args);
83 
84  __lua->do_string("%s:trigger(\"%s\", %s)", __trigger_var, event, params);
85  free(params);
86  } else {
87  __lua->do_string("%s:trigger(\"%s\")", __trigger_var, event);
88  }
89 }
90 
91 } // end of namespace fawkes
Fawkes library namespace.
~LuaTriggerManager()
Destructor.
Definition: trigger.cpp:54
void do_string(const char *format,...)
Execute string.
Definition: context.cpp:548
Lua C++ wrapper.
Definition: context.h:47
void trigger(const char *event, const char *param_format=0,...)
Cause a trigger event.
Definition: trigger.cpp:73
LuaTriggerManager(LuaContext *lua, const char *trigger_var)
Constructor.
Definition: trigger.cpp:46
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32