2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
2023-10-20 04:36:34 +00:00
|
|
|
* Copyright © 2017-2023 The Looking Glass Authors
|
2021-06-06 01:26:18 +00:00
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2021-02-20 06:23:48 +00:00
|
|
|
|
|
|
|
#include "wayland.h"
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <wayland-client.h>
|
|
|
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/locking.h"
|
|
|
|
|
|
|
|
bool waylandPollInit(void)
|
|
|
|
{
|
|
|
|
wlWm.epollFd = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
if (wlWm.epollFd < 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize epoll: %s", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wl_list_init(&wlWm.poll);
|
|
|
|
wl_list_init(&wlWm.pollFree);
|
|
|
|
LG_LOCK_INIT(wlWm.pollLock);
|
|
|
|
LG_LOCK_INIT(wlWm.pollFreeLock);
|
|
|
|
|
2023-11-09 05:30:27 +00:00
|
|
|
return wlWm.desktop->pollInit(wlWm.display);
|
2021-02-20 06:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waylandWait(unsigned int time)
|
|
|
|
{
|
2023-11-09 05:30:27 +00:00
|
|
|
wlWm.desktop->pollWait(wlWm.display, wlWm.epollFd, time);
|
2021-02-20 06:23:48 +00:00
|
|
|
INTERLOCKED_SECTION(wlWm.pollFreeLock,
|
|
|
|
{
|
|
|
|
struct WaylandPoll * node;
|
|
|
|
struct WaylandPoll * temp;
|
|
|
|
wl_list_for_each_safe(node, temp, &wlWm.pollFree, link)
|
|
|
|
{
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
free(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:51:12 +00:00
|
|
|
static void waylandPollRemoveNode(struct WaylandPoll * node)
|
2021-02-20 06:23:48 +00:00
|
|
|
{
|
|
|
|
INTERLOCKED_SECTION(wlWm.pollLock,
|
|
|
|
{
|
|
|
|
wl_list_remove(&node->link);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:51:12 +00:00
|
|
|
bool waylandPollRegister(int fd, WaylandPollCallback callback, void * opaque, uint32_t events)
|
2021-02-20 06:23:48 +00:00
|
|
|
{
|
2021-08-15 16:18:22 +00:00
|
|
|
struct WaylandPoll * node = malloc(sizeof(*node));
|
2021-02-20 06:23:48 +00:00
|
|
|
if (!node)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node->fd = fd;
|
|
|
|
node->removed = false;
|
|
|
|
node->callback = callback;
|
|
|
|
node->opaque = opaque;
|
|
|
|
|
|
|
|
INTERLOCKED_SECTION(wlWm.pollLock,
|
|
|
|
{
|
|
|
|
wl_list_insert(&wlWm.poll, &node->link);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (epoll_ctl(wlWm.epollFd, EPOLL_CTL_ADD, fd, &(struct epoll_event) {
|
|
|
|
.events = events,
|
|
|
|
.data = (epoll_data_t) { .ptr = node },
|
|
|
|
}) < 0)
|
|
|
|
{
|
2021-03-02 04:51:12 +00:00
|
|
|
waylandPollRemoveNode(node);
|
2021-02-20 06:23:48 +00:00
|
|
|
free(node);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:51:12 +00:00
|
|
|
bool waylandPollUnregister(int fd)
|
2021-02-20 06:23:48 +00:00
|
|
|
{
|
|
|
|
struct WaylandPoll * node = NULL;
|
|
|
|
INTERLOCKED_SECTION(wlWm.pollLock,
|
|
|
|
{
|
|
|
|
wl_list_for_each(node, &wlWm.poll, link)
|
|
|
|
{
|
|
|
|
if (node->fd == fd)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Attempt to unregister a fd that was not registered: %d", fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->removed = true;
|
|
|
|
if (epoll_ctl(wlWm.epollFd, EPOLL_CTL_DEL, fd, NULL) < 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to unregistered from epoll: %s", strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:51:12 +00:00
|
|
|
waylandPollRemoveNode(node);
|
2021-02-20 06:23:48 +00:00
|
|
|
|
|
|
|
INTERLOCKED_SECTION(wlWm.pollFreeLock,
|
|
|
|
{
|
|
|
|
wl_list_insert(&wlWm.pollFree, &node->link);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|