Add log func handling

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2013-05-30 09:00:13 +10:00
parent 8034fdd0d7
commit e19994c34c
3 changed files with 46 additions and 0 deletions

View file

@ -41,6 +41,8 @@ struct libevdev {
libevdev_callback_proc sync_callback;
void *userdata;
libevdev_log_func_t log;
char name[MAX_NAME];
struct input_id ids;
unsigned long bits[NLONGS(EV_CNT)];

View file

@ -25,6 +25,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "libevdev.h"
#include "libevdev-int.h"
@ -134,6 +135,21 @@ init_event_queue(struct libevdev *dev)
return 0;
}
static void
_libevdev_log(struct libevdev *dev, const char *format, ...)
{
va_list args;
va_start(args, format);
dev->log(format, args);
va_end(args);
}
static void
libevdev_noop_log_func(const char *format, va_list args)
{
}
struct libevdev*
libevdev_new(int fd)
{
@ -141,6 +157,7 @@ libevdev_new(int fd)
dev = calloc(1, sizeof(*dev));
dev->num_slots = -1;
dev->log = libevdev_noop_log_func;
if (fd >= 0)
libevdev_set_fd(dev, fd);
@ -155,6 +172,12 @@ libevdev_free(struct libevdev *dev)
free(dev);
}
void
libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
{
dev->log = logfunc ? logfunc : libevdev_noop_log_func;
}
int
libevdev_change_fd(struct libevdev *dev, int fd)
{
@ -171,6 +194,7 @@ libevdev_set_fd(struct libevdev* dev, int fd)
int i;
if (dev->fd == -1) {
libevdev_log_func_t log;
libevdev_callback_proc cb, scb;
void *userdata;
@ -178,6 +202,7 @@ libevdev_set_fd(struct libevdev* dev, int fd)
cb = dev->callback;
scb = dev->sync_callback;
userdata = dev->userdata;
log = dev->log;
memset(dev, 0, sizeof(*dev));
@ -185,6 +210,7 @@ libevdev_set_fd(struct libevdev* dev, int fd)
dev->callback = cb;
dev->sync_callback = scb;
dev->userdata = userdata;
dev->log = log;
}
rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);

View file

@ -52,6 +52,24 @@ struct libevdev* libevdev_new(int fd);
*/
void libevdev_free(struct libevdev *dev);
/**
* Logging function called by library-internal logging.
* This function is expected to treat it's input like printf would.
*
* @param format printf-style format string
* @param args List of arguments
*
* @see libevdev_set_log_handler
*/
typedef void (*libevdev_log_func_t)(const char *format, va_list args);
/**
* Set a printf-style logging handler for library-internal logging.
*
* @note This function may be called before libevdev_set_fd.
*/
void libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc);
/**
* Grab or ungrab the device through a kernel EVIOCGRAB. This prevents other
* clients (including kernel-internal ones such as rfkill) from receiving