Browse Source

Added love.signal

bart@bart 16 years ago
parent
commit
18e2b9abb9

+ 2 - 0
src/love.cpp

@@ -39,6 +39,7 @@
 #include <mouse/sdl/wrap_Mouse.h>
 #include <mouse/sdl/wrap_Mouse.h>
 #include <native/tcc/wrap_Native.h>
 #include <native/tcc/wrap_Native.h>
 #include <physics/box2d/wrap_Physics.h>
 #include <physics/box2d/wrap_Physics.h>
+#include <signal/posix/wrap_Signal.h>
 #include <sound/wrap_Sound.h>
 #include <sound/wrap_Sound.h>
 #include <timer/sdl/wrap_Timer.h>
 #include <timer/sdl/wrap_Timer.h>
 
 
@@ -61,6 +62,7 @@ static const luaL_Reg modules[] = {
 	{ "love.mouse", love::mouse::sdl::luaopen_love_mouse },
 	{ "love.mouse", love::mouse::sdl::luaopen_love_mouse },
 	{ "love.native", love::native::tcc::luaopen_love_native },
 	{ "love.native", love::native::tcc::luaopen_love_native },
 	{ "love.physics", love::physics::box2d::luaopen_love_physics },
 	{ "love.physics", love::physics::box2d::luaopen_love_physics },
+	{ "love.signal", love::signal::posix::luaopen_love_signal },
 	{ "love.sound", love::sound::luaopen_love_sound },
 	{ "love.sound", love::sound::luaopen_love_sound },
 	{ "love.timer", love::timer::sdl::luaopen_love_timer },
 	{ "love.timer", love::timer::sdl::luaopen_love_timer },
 	{ 0, 0 }
 	{ 0, 0 }

+ 78 - 0
src/modules/signal/posix/Signal.cpp

@@ -0,0 +1,78 @@
+/**
+* Copyright (c) 2006-2009 LOVE Development Team
+* 
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* 
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#include "Signal.h"
+
+namespace love
+{
+namespace signal
+{
+namespace posix
+{
+
+	Signal::Signal()
+		: signals(0)
+	{
+		cb = 0;
+	}
+	
+	Signal::~Signal()
+	{
+		::signal(signals, SIG_DFL);
+	}
+	
+	bool Signal::registerSignal(int sgn)
+	{
+		signals |= sgn;
+		::signal(sgn, (void (*)(int)) &handler);
+	}
+	
+	void Signal::setCallback(lua_State *L)
+	{
+		luax_assert_argc(L, 1, 1);
+		luax_assert_function(L, -1);
+
+		if(cb != 0)
+		{
+			delete cb;
+			cb = 0;
+		}
+
+		cb = new Reference(L);
+	}
+	
+	void handler(int signal)
+	{
+		if (cb == 0)
+			return;
+		lua_State *L = cb->getL();
+		cb->push();
+		lua_pushnumber(L, signal);
+		lua_call(L, 1, 0);
+	}
+	
+	const char * Signal::getName() const
+	{
+		return "love.signal.posix";
+	}
+
+} // posix
+} // signal
+} // love

+ 56 - 0
src/modules/signal/posix/Signal.h

@@ -0,0 +1,56 @@
+/**
+* Copyright (c) 2006-2009 LOVE Development Team
+* 
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* 
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#ifndef LOVE_SIGNAL_POSIX_SIGNAL_H
+#define LOVE_SIGNAL_POSIX_SIGNAL_H
+
+// signal
+#include <csignal>
+
+// LOVE
+#include <common/Module.h>
+#include <common/Reference.h>
+
+namespace love
+{
+namespace signal
+{
+namespace posix
+{
+	class Signal : public Module
+	{
+	private:
+		int signals;
+
+	public:
+		Signal();
+		~Signal();
+		bool registerSignal(int sgn);
+		void setCallback(lua_State *L);
+		const char * getName() const;
+	}; // Signal
+	
+	void handler(int signal);
+	static Reference *cb;
+} // posix
+} // signal
+} // love
+
+#endif // LOVE_SIGNAL_POSIX_SIGNAL_H

+ 87 - 0
src/modules/signal/posix/wrap_Signal.cpp

@@ -0,0 +1,87 @@
+/**
+* Copyright (c) 2006-2009 LOVE Development Team
+* 
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* 
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#include "wrap_Signal.h"
+
+// LOVE
+#include <common/runtime.h>
+
+#include "Signal.h"
+
+namespace love
+{
+namespace signal
+{
+namespace posix
+{
+	static Signal * instance = 0;
+	
+	int w_registerSignal(lua_State *L)
+	{
+		luaL_argcheck(L, lua_isnumber(L, 1), 1, "Expected number");
+		lua_pushboolean(L, instance->registerSignal(lua_tonumber(L, 1)));
+		return 1;
+	}
+	
+	int w_setCallback(lua_State *L)
+	{
+		luaL_argcheck(L, lua_isfunction(L, 1), 1, "Expected function");
+		instance->setCallback(L);
+	}
+
+	// List of functions to wrap.
+	static const luaL_Reg functions[] = {
+		{ "registerSignal", w_registerSignal },
+		{ "setCallback", w_setCallback }, 
+		{ 0, 0 }
+	};
+	
+	static const LuaConstant constants[] = {
+		{ "signal_abrt", SIGABRT },
+		{ "signal_fpe", SIGFPE },
+		{ "signal_ill", SIGILL },
+		{ "signal_int", SIGINT },
+		{ "signal_segv", SIGSEGV },
+		{ "signal_term", SIGTERM },
+		{ 0, 0 }
+	};
+
+	int luaopen_love_signal(lua_State * L)
+	{
+		if(instance == 0)
+		{
+			try 
+			{
+				instance = new Signal();
+			} 
+			catch(Exception & e)
+			{
+				return luaL_error(L, e.what());
+			}
+		}
+
+		luax_register_gc(L, instance);
+
+		return luax_register_module(L, functions, 0, constants, "signal");
+	}
+
+} // posix
+} // signal
+} // love

+ 43 - 0
src/modules/signal/posix/wrap_Signal.h

@@ -0,0 +1,43 @@
+/**
+* Copyright (c) 2006-2009 LOVE Development Team
+* 
+* This software is provided 'as-is', without any express or implied
+* warranty.  In no event will the authors be held liable for any damages
+* arising from the use of this software.
+* 
+* Permission is granted to anyone to use this software for any purpose,
+* including commercial applications, and to alter it and redistribute it
+* freely, subject to the following restrictions:
+* 
+* 1. The origin of this software must not be misrepresented; you must not
+*    claim that you wrote the original software. If you use this software
+*    in a product, an acknowledgment in the product documentation would be
+*    appreciated but is not required.
+* 2. Altered source versions must be plainly marked as such, and must not be
+*    misrepresented as being the original software.
+* 3. This notice may not be removed or altered from any source distribution.
+**/
+
+#ifndef LOVE_SIGNAL_POSIX_WRAP_SIGNAL_H
+#define LOVE_SIGNAL_POSIX_WRAP_SIGNAL_H
+
+// LOVE
+#include <common/config.h>
+#include "Signal.h"
+
+namespace love
+{
+namespace signal
+{
+namespace posix
+{
+	int w_registerSignal(lua_State *L);
+	int w_setCallback(lua_State *L);
+
+	extern "C" LOVE_EXPORT int luaopen_love_signal(lua_State * L);
+
+} // posix
+} // signal
+} // love
+
+#endif // LOVE_SIGNAL_POSIX_WRAP_SIGNAL_H