LuPPC/src/c/event/event.c

141 lines
3.1 KiB
C
Raw Normal View History

2016-02-27 06:09:38 +11:00
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#define WIN32
#endif
#ifdef WIN32
#define LOCAL_SOCKETPAIR_AF AF_INET
#else
#define LOCAL_SOCKETPAIR_AF AF_UNIX
#endif
2016-02-27 06:09:38 +11:00
#include <event2/event.h>
#include <event2/event_struct.h>
struct event_base *base;
struct event stdinEvent;
int nevt = 0;
#ifdef _WIN32
struct event winEvent;
evutil_socket_t pfd[2] = {0,0};
void pokeWinEvt(char ch) {
send(pfd[1], &ch, 1, 0);
}
static void handleWinevent(evutil_socket_t fd, short what, void *ptr) {
if(what != EV_READ) return;
char buf;
int r = recv(fd, &buf, 1, 0); /* TODO: Wide chars? */
if(r > 0) {
logi(buf);
logn(" < win char");
lua_State* L = getL();
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_down");
lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_up");
lua_pushstring(L, "TODO:SetThisUuid");
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
nevt += 2;
}
}
#endif
2016-02-27 06:09:38 +11:00
static void handleStdin(evutil_socket_t fd, short what, void *ptr) {
char buf;
if(what != EV_READ) return;
2016-02-27 06:09:38 +11:00
int r = read(fd, &buf, 1); /* TODO: Wide chars? */
if(r > 0) {
lua_State* L = getL();
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_down");
lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
lua_getglobal(L, "pushEvent");
lua_pushstring(L, "key_up");
lua_pushstring(L, "TODO:SetThisUuid");
lua_pushnumber(L, buf);
lua_pushnumber(L, -1);
lua_pushstring(L, "root");
lua_call(L, 5, 0);
nevt += 2;
}
}
2016-02-27 06:09:38 +11:00
void event_prepare() {
2016-02-27 09:19:03 +11:00
struct event_config* cfg = event_config_new();
event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
base = event_base_new_with_config(cfg);
2016-02-27 09:19:03 +11:00
evutil_make_socket_nonblocking(STDIN_FILENO);
2016-02-27 06:09:38 +11:00
event_assign(&stdinEvent, base, STDIN_FILENO, EV_READ, handleStdin, NULL);
#ifdef _WIN32
evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pfd);
evutil_make_socket_nonblocking(pfd[0]);
event_assign(&winEvent, base, pfd[0], EV_READ, handleWinevent, NULL);
#endif
2016-02-27 06:09:38 +11:00
}
static void add_events(struct timeval* timeout) {
#ifndef _WIN32
2016-02-27 06:09:38 +11:00
event_add(&stdinEvent, timeout);
#endif
#ifdef _WIN32
event_add(&winEvent, timeout);
#endif
2016-02-27 06:09:38 +11:00
}
2016-02-27 09:19:03 +11:00
2016-02-27 06:09:38 +11:00
int event_pull(int _timeout) {
2016-03-03 04:48:12 +11:00
int n = 0;
2016-02-27 06:09:38 +11:00
if(_timeout > 0) { /* wait max this much time for event */
2016-02-27 09:19:03 +11:00
struct timeval timeout = {_timeout / 1000, (_timeout % 1000) * 1000};
2016-02-27 06:09:38 +11:00
add_events(&timeout);
event_base_loop(base, EVLOOP_ONCE);
} else if(_timeout == 0) { /* Get event without blocking */
add_events(NULL);
event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
} else { /* wait for event to appear */
add_events(NULL);
event_base_loop(base, EVLOOP_ONCE);
}
2016-03-03 04:48:12 +11:00
n = nevt;
2016-02-27 06:09:38 +11:00
nevt = 0;
return n;
}