upiwin/src/sysinput.c

72 lines
1.4 KiB
C
Raw Normal View History

#include <stddef.h>
#include <signal.h>
#include <pthread.h>
#include "log.h"
#include "msg_queue.h"
#include "gpio.h"
2019-12-01 01:24:15 -07:00
volatile PMSG_QUEUE Sys_Queue = NULL;
static pthread_t ithread;
static volatile sig_atomic_t running = 1;
static int last_bstate = 0;
static void *input_thread(void *arg)
{
int st, down, up, mask;
uintptr_t attr;
while (running)
{
/* poll hardware buttons */
st = Gpio_read_buttons();
if (st != last_bstate)
{
up = last_bstate & ~st;
down = st & ~last_bstate;
for (attr = 1, mask = 1; attr <= 4; attr++, mask <<= 1)
{
if (up & mask)
2019-12-01 01:11:46 -07:00
{
Log(LDEBUG, "posting WM_HWBUTTONUP(%d)", (int)attr);
MqPost1(Sys_Queue, 0, WM_HWBUTTONUP, attr);
2019-12-01 01:11:46 -07:00
}
else if (down & mask)
2019-12-01 01:11:46 -07:00
{
Log(LDEBUG, "posting WM_HWBUTTONDOWN(%d)", (int)attr);
MqPost1(Sys_Queue, 0, WM_HWBUTTONDOWN, attr);
2019-12-01 01:11:46 -07:00
}
}
last_bstate = st;
}
/* additional poll activity here */
}
return NULL;
}
int SysEnableInput(void)
{
int rc;
Sys_Queue = MqAlloc(64);
if (!Sys_Queue)
{
Log(LFATAL, "Unable to allocate system message queue.");
return 1;
}
running = 1;
rc = pthread_create(&ithread, NULL, input_thread, NULL);
if (rc != 0)
Log(LFATAL, "Unable to start system input thread (%d).", rc);
return rc;
}
void SysDisableInput(void)
{
running = 0;
pthread_join(ithread, NULL);
MqDestroy(Sys_Queue);
Sys_Queue = NULL;
}