libeis: fix the client ref handling and move the list to the eis context

Let the context take care of adding the device so we have better separation
here. Removal isn't handled in a special way because any list node can remove
itself safel anyway.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2020-08-07 10:56:54 +10:00
parent 119b9c74b0
commit 35d676e7f7
5 changed files with 21 additions and 5 deletions

View file

@ -245,6 +245,8 @@ eis_client_disconnect(struct eis_client *client)
source_remove(client->source);
break;
}
eis_client_unref(client);
}
static int
@ -596,7 +598,6 @@ eis_client_new(struct eis *eis, int fd)
client->id = ++client_id;
list_init(&client->devices);
list_append(&eis->clients, &client->link);
struct source *s = source_add_autoclose(eis->sink, fd,
client_dispatch, client);
client->source = source_ref(s);
@ -605,6 +606,8 @@ eis_client_new(struct eis *eis, int fd)
if (rc != 0)
client = eis_client_unref(client);
eis_add_client(eis, eis_client_ref(client));
return client;
}

View file

@ -84,8 +84,11 @@ eis_backend_fd_add_fd(struct eis *eis, int fd)
assert(eis);
assert(eis->backend);
if (eis_client_new(eis, fd) != NULL)
return 0;
else
struct eis_client *client = eis_client_new(eis, fd);
if (client == NULL)
return -ENOMEM;
eis_client_unref(client);
return 0;
}

View file

@ -114,6 +114,9 @@ eis_client_new(struct eis *eis, int fd);
struct eis*
eis_client_get_context(struct eis_client *client);
void
eis_add_client(struct eis *eis, struct eis_client *client);
void
eis_client_connect_device(struct eis_client *client,
struct eis_device *device);

View file

@ -89,7 +89,8 @@ listener_dispatch(struct source *source, void *data)
if (fd == -1)
return;
eis_client_new(eis, fd);
struct eis_client *client = eis_client_new(eis, fd);
eis_client_unref(client);
}
_public_ int

View file

@ -334,3 +334,9 @@ eis_next_event_type(struct eis *eis)
struct eis_event *e = list_first_entry(&eis->event_queue, e, link);
return e->type;
}
void
eis_add_client(struct eis *eis, struct eis_client *client)
{
list_append(&eis->clients, &client->link);
}