Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * trigger.cpp - Fawkes Lua Trigger Support 00004 * 00005 * Created: Mon Jun 23 10:28:05 2008 00006 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include <lua/trigger.h> 00024 #include <lua/context.h> 00025 00026 #include <core/exceptions/system.h> 00027 00028 #include <cstring> 00029 #include <cstdlib> 00030 #include <cstdio> 00031 00032 namespace fawkes { 00033 00034 /** @class LuaTriggerManager <lua/trigger.h> 00035 * Lua Trigger Manager. 00036 * This class interfaces with a trigger sub-system running inside Lua (with 00037 * the trigger system provided by Fawkes' Lua packages). 00038 * @author Tim Niemueller 00039 */ 00040 00041 /** Constructor. 00042 * @param lua Lua context to use that has a running trigger system 00043 * @param trigger_var the name of the (global) variable pointing to the 00044 * trigger system 00045 */ 00046 LuaTriggerManager::LuaTriggerManager(LuaContext *lua, const char *trigger_var) 00047 { 00048 __lua = lua; 00049 __trigger_var = strdup(trigger_var); 00050 } 00051 00052 00053 /** Destructor. */ 00054 LuaTriggerManager::~LuaTriggerManager() 00055 { 00056 free(__trigger_var); 00057 } 00058 00059 00060 /** Cause a trigger event. 00061 * @param event name of the event to trigger 00062 * @param param_format a format string for a string passed plain as Lua code 00063 * in the trigger() function call as second argument. The code executed looks 00064 * like "lua_trigger_var:trigger(event, string)" with string being what you 00065 * pass, so it can be any number of arguments, for instance you could pass 00066 * @code 00067 * {x=%f, y=%f} 00068 * @endcode 00069 * which would result in a table set with the two floats you provide in the 00070 * ellipsis. 00071 */ 00072 void 00073 LuaTriggerManager::trigger(const char *event, const char *param_format, ...) 00074 { 00075 va_list args; 00076 char *params = NULL; 00077 if ( param_format ) { 00078 va_start(args, param_format); 00079 if (vasprintf(¶ms, param_format, args) == -1) { 00080 throw OutOfMemoryException("Lua trigger: Could not allocate param string"); 00081 } 00082 va_end(args); 00083 00084 __lua->do_string("%s:trigger(\"%s\", %s)", __trigger_var, event, params); 00085 free(params); 00086 } else { 00087 __lua->do_string("%s:trigger(\"%s\")", __trigger_var, event); 00088 } 00089 } 00090 00091 } // end of namespace fawkes