diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2014-01-17 09:24:20 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2014-01-22 08:28:41 +1000 |
commit | 77bf0fe228b86e78d7b61f0f66e7526a1c5a0c50 (patch) | |
tree | 5906274c951e2eeae8c5d12ac979a5e57920646f | |
parent | 4eeda3bc47ba682a96ab06a402250c040a1d0e07 (diff) | |
download | libevdev-77bf0fe228b86e78d7b61f0f66e7526a1c5a0c50.tar.gz |
Count the number of events needed for a full sync
Make sure we have a queue that is at least large enough to do a full sync
after a SYN_DROPPED, plus store a few extra events in case some came in after
the sync.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: David Herrmann <dh.herrmann@gmail.com>
-rw-r--r-- | libevdev/libevdev.c | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c index 8a37204..59625a4 100644 --- a/libevdev/libevdev.c +++ b/libevdev/libevdev.c @@ -41,12 +41,38 @@ static int sync_mt_state(struct libevdev *dev, int create_events); static int init_event_queue(struct libevdev *dev) { - /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per - EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */ + const int MIN_QUEUE_SIZE = 256; + int nevents = 1; /* terminating SYN_REPORT */ + int nslots; + unsigned int type, code; + + /* count the number of axes, keys, etc. to get a better idea at how + many events per EV_SYN we could possibly get. That's the max we + may get during SYN_DROPPED too. Use double that, just so we have + room for events while syncing an event. + */ + for (type = EV_KEY; type < EV_MAX; type++) { + int max = libevdev_event_type_get_max(type); + for (code = 0; max > 0 && code < (unsigned int) max; code++) { + if (libevdev_has_event_code(dev, type, code)) + nevents++; + } + } + + nslots = libevdev_get_num_slots(dev); + if (nslots > 1) { + int num_mt_axes = 0; - const int QUEUE_SIZE = 256; + for (code = ABS_MT_SLOT; code < ABS_MAX; code++) { + if (libevdev_has_event_code(dev, EV_ABS, code)) + num_mt_axes++; + } + + /* We already counted the first slot in the initial count */ + nevents += num_mt_axes * (nslots - 1); + } - return queue_alloc(dev, QUEUE_SIZE); + return queue_alloc(dev, min(MIN_QUEUE_SIZE, nevents * 2)); } static void |