Commit graph

53 commits

Author SHA1 Message Date
Peter Hutterer
2e4ce7008b libei: implement ei_peek_event()
At least for the test suite, I'll need something that allows me to look at an
event without removing it from the event queue. Just the event type isn't
enough, I need things like "what are the caps on the device". Simplest way to
allow this is a peek function that doesn't remove the event from the queue.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:19:03 +10:00
Peter Hutterer
fb03ac78a2 libei: implement device_get_name()
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:19:03 +10:00
Peter Hutterer
69448e247c libeis: rename to eis_setup_backend_socket()
Now that the backend is more of an implementation detail, namespace for
eis_setup_backend_foo()

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
261f8c910b utils: rename the sources close behavior for extra clarity
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
8a0bec7b87 libei: fix source removal on ei_disconnect
The sink dispatch() loop gets the source from the epollfd data pointer, so
unref'ing the source would cause it to be destroyed when we may try to access
it later. Fix the whole source ownership properly, the first ref is owned by
the epollfd and moves to the list of removed sources, the caller gets the
second ref.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
15cf73ad11 libei: remove unused connection field
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
d2ecf43abd libeis: switch to a single eis_new() function
The main reason for having different functions to create the initial context
was so we can nest the eis struct inside the context-specific struct. That
shouldn't leak into the API though.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
bc5be06549 libei: add a backend to work off a single fd
For the case where someone else opens the sockets for us - like a test suite
or eventually the portal.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
e98e931615 libeis: don't ref the event on return
We're removing it from our own list when returning the event, so increasing
the refcount isn't required.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
562f2b4579 libeis: handle other events during unref
Refcounting error means we never hit this code, so let's fix it before we fix
the refcounting.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 15:12:19 +10:00
Peter Hutterer
48f2f5e4c7 libei: don't ref the event on return
We're removing it from our own list when returning the event, so increasing
the refcount isn't required.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 14:53:08 +10:00
Peter Hutterer
311bc6b4fc libei: rename to ei_setup_backend_socket()
Now that the backend is more of an implementation detail, namespace for
ei_setup_backend_foo()

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 14:52:54 +10:00
Peter Hutterer
283cc87416 libei: switch to a single ei_new() function
The main reason for having different functions to create the initial context
was so we can nest the ei struct inside the context-specific struct. That
shouldn't leak into the API though and our use-case is probably one where we
create one context, then iterate through possible backends until one succeds -
having different contexts here doesn't help then.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 14:47:40 +10:00
Peter Hutterer
0ab1458241 test: add some internal unit-tests to libei
Similar in style to Rust where the unit tests are in the same file. Let's see
how far we can get with that in C. Auto-discovery of tests by forcing the
respective suites into a special test section so we can collect it later from
our unit runner.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 14:47:40 +10:00
Peter Hutterer
b4552374ca libei: documentation updates
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 14:47:40 +10:00
Peter Hutterer
6dc848b2c3 libei: add a few comments and extra checks
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-06 14:47:40 +10:00
Peter Hutterer
b5f1082419 libei: implement a few missing functions
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-04 10:53:38 +10:00
Peter Hutterer
4f0637f726 libei: switch to passive naming for accepted/removed messages
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-04 10:29:33 +10:00
Peter Hutterer
cb192945d2 Make the button events carry event codes, not indices
Brings this more in line with libinput and, well, everything on linux, really.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-03 20:46:13 +10:00
Peter Hutterer
a9f07368a5 Implement pointer button and keyboard key events
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-03 12:00:31 +10:00
Peter Hutterer
81d4556dcc proto: add a fixed-length frame message
protobuf relies on external framing and exact buffer lengths to parse things
correctly. So let's provide that by sending a fixed-length Frame message
before every real message.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-03 11:59:05 +10:00
Peter Hutterer
718beb926e utils: Rename OBJECT_DECLARE... to OBJECT_IMPLEMENT
More technically correct since that's what all these crazy macros do.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-03 07:34:52 +10:00
Peter Hutterer
6610319832 Add a missing empty line
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-08-03 07:32:32 +10:00
Peter Hutterer
40682a0420 Use send() instead of write() to prevent SIGPIPE
We're in a library here, so expecting the caller to handle SIGPIPE just in
case we get it is a bit demanding. Let's use send() instead so we can just
prevent that altogether.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-31 14:27:18 +10:00
Peter Hutterer
9f4827135d Process multiple messages in one dispatch
We may get more than one message per dispatch so let's make sure we iterate
through all the available data.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-31 14:14:21 +10:00
Peter Hutterer
9f851bcccb Replace the custom message parsing with protobuf
Plain-text was useful for the initial implementation where the counterpart was
netcat but now that both parts are in place, protobuf is a much more
convenient system to handle a frequently-changing protocol.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-31 13:05:11 +10:00
Peter Hutterer
0426c99278 util: add some documentation for the iobuf helpers
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-31 13:05:11 +10:00
Peter Hutterer
2931f0fd13 util: fix the fd reading
buf is the iobuf pointer, we need to check whether we read sizeof(data) bytes
before looping around to fetch more.

And fix the return value for a zero read, because that could either be EAGAIN
or EOF.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-31 13:05:11 +10:00
Peter Hutterer
3c7820baca libeis: only remove the source on disconnect, don't destroy it
Removed sources will be cleaned up at the end of dispatch.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 21:17:54 +10:00
Peter Hutterer
708ad85dd4 util: remove the source from the removed_list on destroy
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 21:17:49 +10:00
Peter Hutterer
6a0f25e72a utils: update the buffer length when appending data
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 21:02:07 +10:00
Peter Hutterer
8ec0f4bf7c Send the trailing null byte for composed string messages
The iobuf treats this as binary data but then we're using it as strings in the
message parsing code. So make sure all messages have a trailing null byte.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 21:01:09 +10:00
Peter Hutterer
1bdca21772 libeis: remove the socket on exit
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 15:38:40 +10:00
Peter Hutterer
6062a0f676 libei: take the LIBEI_SOCKET variable if the socket path is NULL
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 15:38:40 +10:00
Peter Hutterer
b1231dfa1f libei: add a hook to configure the client name
Better than the current "myclient". Lacks safety and whatnot so long-term it
will have to get replaced with something else.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-30 15:38:40 +10:00
Peter Hutterer
802dfd4ca6 Add the minimal implementation for a UNIX socket libei client
This is the minimum framework to support connecting to a libeis server, adding
a device and sending relative pointer motion events.It's missing a bunch of
checks and verifications, many of the hooks are still missing, etc.

See the tools/ directory for an example. The protocol is as outlined in
commit f7a24b2fbd.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 16:18:24 +10:00
Peter Hutterer
27670000e4 libeis: only log client state transitions if they changed
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 11:52:23 +10:00
Peter Hutterer
247b6d0dcb libeis: fix wrong message field assignment
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 10:59:07 +10:00
Peter Hutterer
b79f309a6c libeis: whitespace fix
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 09:59:11 +10:00
Peter Hutterer
23c34fc77d client: handle the case of zero data bytes
When terminating the connection, we might get a read of zero

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 09:42:42 +10:00
Peter Hutterer
fb115a1f43 libeis: avoid a cleanup double-free
Aparently the compiler is not smart enough to handle the case correctly where
a cleanup variable is introduced after a goto statement.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 09:41:56 +10:00
Peter Hutterer
b840c510bf libeis: remove existing devices before disconnecting the client
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-29 09:41:07 +10:00
Peter Hutterer
f7a24b2fbd Add the minimal implementation for a UNIX socket libeis server
This is the minimum framework to support new clients, added devices and
pointer relative motion events. It's missing a bunch of checks and
verification, most of the server hooks aren't there yet, the only
implementation is a UNIX socket and the protocol is plain text (but at least
the last two makes it netcat-compatible).

Protocol is plain text for now and interaction is like this (S is server, C is client):
S: hello
C: connect myclientname
S: connected
C: add 2 4
S: accept 2
C: rel 2 -1 1
C: rel 2 5 4

Where the last two lines are: add device with id 2 and capability mask 0x4,
send a relative pointer motion event for device 2 with coordinates -1/1, then
5/4.

The implementation relies heavily on some abstraction and macros galore, see
the various util-* files. These are largely copied from libinput, with a few
parts removed and a few other parts added.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-28 19:33:34 +10:00
Peter Hutterer
805ba96271 libeis: declare a few extra functions that will be useful
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-28 14:18:32 +10:00
Peter Hutterer
a09a99b6d9 libeis: add the init bits for a new libeis context for unix sockets
This is the easiest to implement, so let's start with that.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-28 14:17:58 +10:00
Peter Hutterer
7f53c30157 libei: add a HELLO message on connect
This allows for some negotiation - server sends hello with some magic auth
data, client sends connect with the reply to the hello. Not sure yet how or
whether this makes sense, but at least it makes testing easy.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-27 13:16:56 +10:00
Peter Hutterer
91781ad3e0 Drop the _ctx suffix to the main structs
No proper reason, it just goes easier with the macros I'm about to implement.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-25 16:18:33 +10:00
Peter Hutterer
1fea949114 Update keymap handling
keymap handling is difficult because a lot of it relies on specific server
implementation details. So let's provide the API for a client to assign a
specific keymap to the device and for the server to accept/refuse/override
that keymap.

Where the server refuses, it's up to the client to figure out the rest.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-16 13:32:27 +10:00
Peter Hutterer
68f9fa4b1c Simplify the suspend/resume behavior
The use-case of suspending individual capabilities is niche - where a client
needs to rely on this it should create multiple devices with different
capabilities so they can be suspended separately.

Also clarify that the server *must* process events even from a suspended
device. This works around a race condition where a client has sent several
events already before the suspend comes in - it has to assume that any event
sent before the server event is handled updates the state in the server.

Implementation-wise, libei will start filting events once ei_dispatch()
returned the suspend event to the client.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-16 11:01:05 +10:00
Peter Hutterer
61bdb2c891 libeis: caps shouldn't be a bitmask
Only allow changing or querying for a single capability at a time.
More verbose but less ambiguous and it removes any bugs resulting from bitmask
errors. There is no supported use-case where the server *adds* a
capability to a client-requested device, so has_capability and
disable_capability is enough.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2020-07-16 10:19:36 +10:00