diff options
| author | cbdev <cb@cbcdn.com> | 2019-07-14 16:49:31 +0200 | 
|---|---|---|
| committer | cbdev <cb@cbcdn.com> | 2019-07-14 16:49:31 +0200 | 
| commit | e776e02531aca45f424f7139e5d7304ba3096b45 (patch) | |
| tree | b5221228c7bca8f7f6804d573ed28d63197e8ae2 | |
| parent | f19d6e66b23ba719f474171b10e1ee294fb38d55 (diff) | |
| download | midimonster-e776e02531aca45f424f7139e5d7304ba3096b45.tar.gz midimonster-e776e02531aca45f424f7139e5d7304ba3096b45.tar.bz2 midimonster-e776e02531aca45f424f7139e5d7304ba3096b45.zip  | |
Work around missing timerfd on OSX/Windows
| -rw-r--r-- | backend.c | 2 | ||||
| -rw-r--r-- | backends/lua.c | 40 | ||||
| -rw-r--r-- | backends/lua.h | 6 | ||||
| -rw-r--r-- | backends/lua.md | 4 | ||||
| -rw-r--r-- | configs/lua.cfg | 2 | 
5 files changed, 50 insertions, 4 deletions
@@ -227,7 +227,7 @@ struct timeval backend_timeout(){  	struct timeval tv = {  		secs, -		msecs +		msecs * 1000  	};  	return tv;  } diff --git a/backends/lua.c b/backends/lua.c index 8069f8f..7fcd0ab 100644 --- a/backends/lua.c +++ b/backends/lua.c @@ -10,10 +10,17 @@  static size_t timers = 0;  static lua_timer* timer = NULL;  uint64_t timer_interval = 0; +#ifdef MMBACKEND_LUA_TIMERFD  static int timer_fd = -1; +#else +static uint64_t last_timestamp; +#endif  int init(){  	backend lua = { +		#ifndef MMBACKEND_LUA_TIMERFD +		.interval = lua_interval, +		#endif  		.name = BACKEND_NAME,  		.conf = lua_configure,  		.create = lua_instance, @@ -31,21 +38,33 @@ int init(){  		return 1;  	} +	#ifdef MMBACKEND_LUA_TIMERFD  	//create the timer to expire intervals  	timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);  	if(timer_fd < 0){  		fprintf(stderr, "Failed to create timer for Lua backend\n");  		return 1;  	} +	#endif  	return 0;  } +static uint32_t lua_interval(){ +	//FIXME Return delta for next timer here +	if(timer_interval){ +		return timer_interval; +	} +	return 1000; +} +  static int lua_update_timerfd(){  	uint64_t interval, gcd, residual;  	size_t n = 0; +	#ifdef MMBACKEND_LUA_TIMERFD  	struct itimerspec timer_config = {  		0  	}; +	#endif  	//find the minimum for the lower interval bounds  	for(n = 0; n < timers; n++){ @@ -72,16 +91,20 @@ static int lua_update_timerfd(){  			}  		} +		#ifdef MMBACKEND_LUA_TIMERFD  		timer_config.it_interval.tv_sec = timer_config.it_value.tv_sec = interval / 1000;  		timer_config.it_interval.tv_nsec = timer_config.it_value.tv_nsec = (interval % 1000) * 1e6; +		#endif  	}  	if(interval == timer_interval){  		return 0;  	} +	#ifdef MMBACKEND_LUA_TIMERFD  	//configure the new interval  	timerfd_settime(timer_fd, 0, &timer_config, NULL); +	#endif  	timer_interval = interval;  	return 0;  } @@ -359,6 +382,7 @@ static int lua_handle(size_t num, managed_fd* fds){  	uint64_t delta = timer_interval;  	size_t n; +	#ifdef MMBACKEND_LUA_TIMERFD  	if(!num){  		return 0;  	} @@ -368,6 +392,18 @@ static int lua_handle(size_t num, managed_fd* fds){  		fprintf(stderr, "Failed to read from Lua timer: %s\n", strerror(errno));  		return 1;  	} +	#else +		if(!last_timestamp){ +			last_timestamp = mm_timestamp(); +		} +		delta = mm_timestamp() - last_timestamp; +		last_timestamp = mm_timestamp(); +	#endif + +	//no timers active +	if(!timer_interval){ +		return 0; +	}  	//add delta to all active timers  	for(n = 0; n < timers; n++){ @@ -418,11 +454,13 @@ static int lua_start(){  		return 0;  	} +	#ifdef MMBACKEND_LUA_TIMERFD  	//register the timer with the core  	fprintf(stderr, "Lua backend registering 1 descriptor to core\n");  	if(mm_manage_fd(timer_fd, BACKEND_NAME, 1, NULL)){  		return 1;  	} +	#endif  	return 0;  } @@ -457,8 +495,10 @@ static int lua_shutdown(){  	free(timer);  	timer = NULL;  	timers = 0; +	#ifdef MMBACKEND_LUA_TIMERFD  	close(timer_fd);  	timer_fd = -1; +	#endif  	fprintf(stderr, "Lua backend shut down\n");  	return 0; diff --git a/backends/lua.h b/backends/lua.h index ccffef7..7aad891 100644 --- a/backends/lua.h +++ b/backends/lua.h @@ -4,6 +4,11 @@  #include <lualib.h>  #include <lauxlib.h> +//OSX and Windows don't have the cool new toys... +#ifdef __linux__ +	#define MMBACKEND_LUA_TIMERFD +#endif +  int init();  static int lua_configure(char* option, char* value);  static int lua_configure_instance(instance* inst, char* option, char* value); @@ -13,6 +18,7 @@ static int lua_set(instance* inst, size_t num, channel** c, channel_value* v);  static int lua_handle(size_t num, managed_fd* fds);  static int lua_start();  static int lua_shutdown(); +static uint32_t lua_interval();  typedef struct /*_lua_instance_data*/ {  	size_t channels; diff --git a/backends/lua.md b/backends/lua.md index 970e8e2..e273b28 100644 --- a/backends/lua.md +++ b/backends/lua.md @@ -15,7 +15,7 @@ The following functions are provided within the Lua interpreter for interaction  | `output(string, number)`	| `output("foo", 0.75)`		| Output a value event to a channel	|  | `interval(function, number)`	| `interval(update, 100)`	| Register a function to be called periodically. Intervals are milliseconds (rounded to the nearest 10 ms) |  | `input_value(string)`		| `input_value("foo")`		| Get the last input value on a channel	| -| `output_value(string)`	| `output_value("bar")		| Get the last output value on a channel | +| `output_value(string)`	| `output_value("bar")`		| Get the last output value on a channel |  Example script: @@ -58,7 +58,7 @@ lua1.foo > lua2.bar  #### Known bugs / problems -Using any of the interface functions (`output`, `interval`, `input\_value`, `output\_value`) as an +Using any of the interface functions (`output`, `interval`, `input_value`, `output_value`) as an  input channel name to a Lua instance will not call any handler functions.  Using these names as arguments to the output and value interface functions works as intended. diff --git a/configs/lua.cfg b/configs/lua.cfg index 42fd27d..b892e91 100644 --- a/configs/lua.cfg +++ b/configs/lua.cfg @@ -8,7 +8,7 @@ bind = 0.0.0.0  device = /dev/input/by-path/platform-i8042-serio-2-event-mouse  [evdev xbox] -device = /dev/input/event17 +;device = /dev/input/event17  axis.ABS_X = 34300 0 65535 255 4095  axis.ABS_Y = 34300 0 65535 255 4095  | 
