FreeRDP/uwac/libuwac/uwac-display.c

760 lines
18 KiB
C
Raw Normal View History

/*
* Copyright © 2014 David FORT <contact@hardening-consulting.com>
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include "uwac-priv.h"
#include "uwac-utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/epoll.h>
#include "uwac-os.h"
2019-01-25 15:05:51 +03:00
#include "wayland-cursor.h"
2019-02-07 16:21:04 +03:00
#define TARGET_COMPOSITOR_INTERFACE 3U
#define TARGET_SHM_INTERFACE 1U
#define TARGET_SHELL_INTERFACE 1U
#define TARGET_DDM_INTERFACE 1U
#define TARGET_SEAT_INTERFACE 5U
#define TARGET_XDG_VERSION 5U /* The version of xdg-shell that we implement */
2019-11-06 17:24:51 +03:00
static const char* event_names[] = {
"new seat", "removed seat", "new output", "configure", "pointer enter",
"pointer leave", "pointer motion", "pointer buttons", "pointer axis", "keyboard enter",
"key", "touch frame begin", "touch up", "touch down", "touch motion",
"touch cancel", "touch frame end", "frame done", "close", NULL
};
2019-11-20 13:30:14 +03:00
static bool uwac_default_error_handler(UwacDisplay* display, UwacReturnCode code, const char* msg,
...)
2016-10-19 12:05:12 +03:00
{
va_list args;
va_start(args, msg);
vfprintf(stderr, "%s", args);
va_end(args);
return false;
}
UwacErrorHandler uwacErrorHandler = uwac_default_error_handler;
2016-10-19 12:05:12 +03:00
void UwacInstallErrorHandler(UwacErrorHandler handler)
{
if (handler)
uwacErrorHandler = handler;
else
uwacErrorHandler = uwac_default_error_handler;
}
2016-10-19 12:05:12 +03:00
static void cb_shm_format(void* data, struct wl_shm* wl_shm, uint32_t format)
{
2016-10-19 12:05:12 +03:00
UwacDisplay* d = data;
if (format == WL_SHM_FORMAT_RGB565)
d->has_rgb565 = true;
d->shm_formats_nb++;
2019-11-06 17:24:51 +03:00
d->shm_formats =
xrealloc((void*)d->shm_formats, sizeof(enum wl_shm_format) * d->shm_formats_nb);
d->shm_formats[d->shm_formats_nb - 1] = format;
}
2019-11-06 17:24:51 +03:00
struct wl_shm_listener shm_listener = { cb_shm_format };
2019-11-06 17:24:51 +03:00
static void xdg_shell_ping(void* data, struct xdg_wm_base* xdg_wm_base, uint32_t serial)
{
xdg_wm_base_pong(xdg_wm_base, serial);
}
2019-11-06 17:24:51 +03:00
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
xdg_shell_ping,
};
#ifdef BUILD_FULLSCREEN_SHELL
2019-11-06 17:24:51 +03:00
static void fullscreen_capability(void* data,
struct zwp_fullscreen_shell_v1* zwp_fullscreen_shell_v1,
uint32_t capability)
{
}
2019-11-06 17:24:51 +03:00
static const struct zwp_fullscreen_shell_v1_listener fullscreen_shell_listener = {
2016-10-19 12:05:12 +03:00
fullscreen_capability,
};
#endif
static void display_destroy_seat(UwacDisplay* d, uint32_t name)
{
2019-11-06 17:24:51 +03:00
UwacSeat *seat, *tmp;
2019-02-19 11:29:17 +03:00
wl_list_for_each_safe(seat, tmp, &d->seats, link)
2016-10-19 12:05:12 +03:00
{
if (seat->seat_id == name)
{
UwacSeatDestroy(seat);
}
}
}
2019-11-06 17:24:51 +03:00
static void UwacSeatRegisterDDM(UwacSeat* seat)
{
2019-11-06 17:24:51 +03:00
UwacDisplay* d = seat->display;
if (!d->data_device_manager)
return;
if (!seat->data_device)
2019-11-06 17:24:51 +03:00
seat->data_device =
wl_data_device_manager_get_data_device(d->data_device_manager, seat->seat);
}
2019-01-25 15:05:51 +03:00
static void UwacRegisterCursor(UwacSeat* seat)
{
if (!seat || !seat->display || !seat->display->compositor)
return;
seat->pointer_surface = wl_compositor_create_surface(seat->display->compositor);
}
2016-10-19 12:05:12 +03:00
static void registry_handle_global(void* data, struct wl_registry* registry, uint32_t id,
const char* interface, uint32_t version)
{
2016-10-19 12:05:12 +03:00
UwacDisplay* d = data;
UwacGlobal* global;
2019-11-06 17:24:51 +03:00
global = xzalloc(sizeof *global);
global->name = id;
global->interface = xstrdup(interface);
global->version = version;
wl_list_insert(d->globals.prev, &global->link);
2016-10-19 12:05:12 +03:00
if (strcmp(interface, "wl_compositor") == 0)
{
d->compositor = wl_registry_bind(registry, id, &wl_compositor_interface,
min(TARGET_COMPOSITOR_INTERFACE, version));
}
else if (strcmp(interface, "wl_shm") == 0)
{
2019-11-06 17:24:51 +03:00
d->shm =
wl_registry_bind(registry, id, &wl_shm_interface, min(TARGET_SHM_INTERFACE, version));
wl_shm_add_listener(d->shm, &shm_listener, d);
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "wl_output") == 0)
{
UwacOutput* output;
UwacOutputNewEvent* ev;
output = UwacCreateOutput(d, id, version);
2016-10-19 12:05:12 +03:00
if (!output)
{
assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create output\n"));
return;
}
2016-10-19 12:05:12 +03:00
ev = (UwacOutputNewEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_NEW_OUTPUT);
if (ev)
ev->output = output;
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "wl_seat") == 0)
{
UwacSeatNewEvent* ev;
UwacSeat* seat;
seat = UwacSeatNew(d, id, min(version, TARGET_SEAT_INTERFACE));
2016-10-19 12:05:12 +03:00
if (!seat)
{
assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create new seat\n"));
return;
}
UwacSeatRegisterDDM(seat);
2018-12-19 18:17:59 +03:00
UwacSeatRegisterClipboard(seat);
2019-01-25 15:05:51 +03:00
UwacRegisterCursor(seat);
2016-10-19 12:05:12 +03:00
ev = (UwacSeatNewEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_NEW_SEAT);
if (!ev)
{
assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create new seat event\n"));
return;
}
ev->seat = seat;
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "wl_data_device_manager") == 0)
{
2019-11-06 17:24:51 +03:00
UwacSeat *seat, *tmp;
2016-10-19 12:05:12 +03:00
d->data_device_manager = wl_registry_bind(registry, id, &wl_data_device_manager_interface,
2019-11-06 17:24:51 +03:00
min(TARGET_DDM_INTERFACE, version));
2019-02-19 11:29:17 +03:00
wl_list_for_each_safe(seat, tmp, &d->seats, link)
{
UwacSeatRegisterDDM(seat);
UwacSeatRegisterClipboard(seat);
2019-01-25 15:05:51 +03:00
UwacRegisterCursor(seat);
}
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "wl_shell") == 0)
{
2019-11-06 17:24:51 +03:00
d->shell = wl_registry_bind(registry, id, &wl_shell_interface,
min(TARGET_SHELL_INTERFACE, version));
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "xdg_wm_base") == 0)
2016-10-19 12:05:12 +03:00
{
d->xdg_base = wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(d->xdg_base, &xdg_wm_base_listener, d);
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "zwp_keyboard_shortcuts_inhibit_manager_v1") == 0)
{
2019-11-06 17:24:51 +03:00
d->keyboard_inhibit_manager =
wl_registry_bind(registry, id, &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
}
2019-01-22 17:45:15 +03:00
else if (strcmp(interface, "zxdg_decoration_manager_v1") == 0)
{
d->deco_manager = wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
}
else if (strcmp(interface, "org_kde_kwin_server_decoration_manager") == 0)
{
2019-11-06 17:24:51 +03:00
d->kde_deco_manager =
wl_registry_bind(registry, id, &org_kde_kwin_server_decoration_manager_interface, 1);
}
#if BUILD_IVI
2016-10-19 12:05:12 +03:00
else if (strcmp(interface, "ivi_application") == 0)
{
d->ivi_application = wl_registry_bind(registry, id, &ivi_application_interface, 1);
}
#endif
#if BUILD_FULLSCREEN_SHELL
else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0)
2016-10-19 12:05:12 +03:00
{
d->fullscreen_shell = wl_registry_bind(registry, id, &zwp_fullscreen_shell_v1_interface, 1);
zwp_fullscreen_shell_v1_add_listener(d->fullscreen_shell, &fullscreen_shell_listener, d);
}
#endif
#if 0
2016-10-19 12:05:12 +03:00
else if (strcmp(interface, "text_cursor_position") == 0)
{
d->text_cursor_position = wl_registry_bind(registry, id, &text_cursor_position_interface, 1);
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "workspace_manager") == 0)
{
//init_workspace_manager(d, id);
2016-10-19 12:05:12 +03:00
}
else if (strcmp(interface, "wl_subcompositor") == 0)
{
d->subcompositor = wl_registry_bind(registry, id, &wl_subcompositor_interface, 1);
#endif
}
2016-10-19 12:05:12 +03:00
static void registry_handle_global_remove(void* data, struct wl_registry* registry, uint32_t name)
{
UwacDisplay* d = data;
UwacGlobal* global;
UwacGlobal* tmp;
wl_list_for_each_safe(global, tmp, &d->globals, link)
{
if (global->name != name)
continue;
#if 0
2016-10-19 12:05:12 +03:00
if (strcmp(global->interface, "wl_output") == 0)
display_destroy_output(d, name);
2016-10-19 12:05:12 +03:00
#endif
2016-10-19 12:05:12 +03:00
if (strcmp(global->interface, "wl_seat") == 0)
{
UwacSeatRemovedEvent* ev;
display_destroy_seat(d, name);
2016-10-19 12:05:12 +03:00
ev = (UwacSeatRemovedEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_REMOVED_SEAT);
if (ev)
ev->id = name;
}
wl_list_remove(&global->link);
free(global->interface);
free(global);
}
}
2019-11-20 13:30:14 +03:00
static void UwacDestroyGlobal(UwacGlobal* global)
2016-10-19 12:05:12 +03:00
{
free(global->interface);
wl_list_remove(&global->link);
free(global);
}
2019-11-20 13:30:14 +03:00
static void* display_bind(UwacDisplay* display, uint32_t name, const struct wl_interface* interface,
uint32_t version)
2016-10-19 12:05:12 +03:00
{
return wl_registry_bind(display->registry, name, interface, version);
}
2019-11-06 17:24:51 +03:00
static const struct wl_registry_listener registry_listener = { registry_handle_global,
registry_handle_global_remove };
2016-10-19 12:05:12 +03:00
int UwacDisplayWatchFd(UwacDisplay* display, int fd, uint32_t events, UwacTask* task)
{
struct epoll_event ep;
ep.events = events;
ep.data.ptr = task;
return epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
}
2019-11-20 13:30:14 +03:00
static void UwacDisplayUnwatchFd(UwacDisplay* display, int fd)
2016-10-19 12:05:12 +03:00
{
epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
}
2016-10-19 12:05:12 +03:00
static void display_exit(UwacDisplay* display)
{
display->running = false;
}
2016-10-19 12:05:12 +03:00
static void display_dispatch_events(UwacTask* task, uint32_t events)
{
2016-10-19 12:05:12 +03:00
UwacDisplay* display = container_of(task, UwacDisplay, dispatch_fd_task);
struct epoll_event ep;
int ret;
display->display_fd_events = events;
2016-10-19 12:05:12 +03:00
if ((events & EPOLLERR) || (events & EPOLLHUP))
{
display_exit(display);
return;
}
2016-10-19 12:05:12 +03:00
if (events & EPOLLIN)
{
ret = wl_display_dispatch(display->display);
2016-10-19 12:05:12 +03:00
if (ret == -1)
{
display_exit(display);
return;
}
}
2016-10-19 12:05:12 +03:00
if (events & EPOLLOUT)
{
ret = wl_display_flush(display->display);
2016-10-19 12:05:12 +03:00
if (ret == 0)
{
ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
ep.data.ptr = &display->dispatch_fd_task;
epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, display->display_fd, &ep);
2016-10-19 12:05:12 +03:00
}
else if (ret == -1 && errno != EAGAIN)
{
display_exit(display);
return;
}
}
}
2016-10-19 12:05:12 +03:00
UwacDisplay* UwacOpenDisplay(const char* name, UwacReturnCode* err)
{
UwacDisplay* ret;
ret = (UwacDisplay*)xzalloc(sizeof(*ret));
2016-10-19 12:05:12 +03:00
if (!ret)
{
*err = UWAC_ERROR_NOMEMORY;
return NULL;
}
wl_list_init(&ret->globals);
wl_list_init(&ret->seats);
wl_list_init(&ret->outputs);
wl_list_init(&ret->windows);
ret->display = wl_display_connect(name);
2016-10-19 12:05:12 +03:00
if (ret->display == NULL)
{
fprintf(stderr, "failed to connect to Wayland display %s: %m\n", name);
*err = UWAC_ERROR_UNABLE_TO_CONNECT;
goto out_free;
}
ret->epoll_fd = uwac_os_epoll_create_cloexec();
2016-10-19 12:05:12 +03:00
if (ret->epoll_fd < 0)
{
*err = UWAC_NOT_ENOUGH_RESOURCES;
goto out_disconnect;
}
ret->display_fd = wl_display_get_fd(ret->display);
ret->registry = wl_display_get_registry(ret->display);
2016-10-19 12:05:12 +03:00
if (!ret->registry)
{
*err = UWAC_ERROR_NOMEMORY;
goto out_close_epoll;
}
wl_registry_add_listener(ret->registry, &registry_listener, ret);
2016-10-19 12:05:12 +03:00
if ((wl_display_roundtrip(ret->display) < 0) || (wl_display_roundtrip(ret->display) < 0))
{
2019-11-06 17:24:51 +03:00
uwacErrorHandler(ret, UWAC_ERROR_UNABLE_TO_CONNECT,
"Failed to process Wayland connection: %m\n");
*err = UWAC_ERROR_UNABLE_TO_CONNECT;
goto out_free_registry;
}
ret->dispatch_fd_task.run = display_dispatch_events;
2016-10-19 12:05:12 +03:00
if (UwacDisplayWatchFd(ret, ret->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
&ret->dispatch_fd_task) < 0)
{
uwacErrorHandler(ret, UWAC_ERROR_INTERNAL, "unable to watch display fd: %m\n");
*err = UWAC_ERROR_INTERNAL;
goto out_free_registry;
}
ret->running = true;
ret->last_error = *err = UWAC_SUCCESS;
return ret;
out_free_registry:
wl_registry_destroy(ret->registry);
out_close_epoll:
close(ret->epoll_fd);
out_disconnect:
wl_display_disconnect(ret->display);
out_free:
free(ret);
return NULL;
}
2016-10-19 12:05:12 +03:00
int UwacDisplayDispatch(UwacDisplay* display, int timeout)
{
int ret, count, i;
2016-10-19 12:05:12 +03:00
UwacTask* task;
struct epoll_event ep[16];
wl_display_dispatch_pending(display->display);
if (!display->running)
return 0;
ret = wl_display_flush(display->display);
2016-10-19 12:05:12 +03:00
if (ret < 0 && errno == EAGAIN)
{
ep[0].events = (EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP);
ep[0].data.ptr = &display->dispatch_fd_task;
epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, display->display_fd, &ep[0]);
2016-10-19 12:05:12 +03:00
}
else if (ret < 0)
{
return -1;
}
count = epoll_wait(display->epoll_fd, ep, ARRAY_LENGTH(ep), timeout);
2016-10-19 12:05:12 +03:00
for (i = 0; i < count; i++)
{
task = ep[i].data.ptr;
task->run(task, ep[i].events);
}
return 1;
}
2016-10-19 12:05:12 +03:00
UwacReturnCode UwacDisplayGetLastError(const UwacDisplay* display)
{
return display->last_error;
}
2016-10-19 12:05:12 +03:00
UwacReturnCode UwacCloseDisplay(UwacDisplay** pdisplay)
{
UwacDisplay* display;
2019-11-06 17:24:51 +03:00
UwacSeat *seat, *tmpSeat;
UwacWindow *window, *tmpWindow;
UwacOutput *output, *tmpOutput;
UwacGlobal *global, *tmpGlobal;
assert(pdisplay);
display = *pdisplay;
2016-10-19 12:05:12 +03:00
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
/* destroy windows */
2016-10-19 12:05:12 +03:00
wl_list_for_each_safe(window, tmpWindow, &display->windows, link)
{
UwacDestroyWindow(&window);
}
/* destroy seats */
2016-10-19 12:05:12 +03:00
wl_list_for_each_safe(seat, tmpSeat, &display->seats, link)
{
UwacSeatDestroy(seat);
}
/* destroy output */
2016-10-19 12:05:12 +03:00
wl_list_for_each_safe(output, tmpOutput, &display->outputs, link)
{
UwacDestroyOutput(output);
}
/* destroy globals */
2016-10-19 12:05:12 +03:00
wl_list_for_each_safe(global, tmpGlobal, &display->globals, link)
{
UwacDestroyGlobal(global);
}
if (display->compositor)
wl_compositor_destroy(display->compositor);
2016-10-19 12:05:12 +03:00
if (display->keyboard_inhibit_manager)
zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(display->keyboard_inhibit_manager);
2019-01-22 17:45:15 +03:00
if (display->deco_manager)
zxdg_decoration_manager_v1_destroy(display->deco_manager);
if (display->kde_deco_manager)
org_kde_kwin_server_decoration_manager_destroy(display->kde_deco_manager);
#ifdef BUILD_FULLSCREEN_SHELL
2016-10-19 12:05:12 +03:00
if (display->fullscreen_shell)
zwp_fullscreen_shell_v1_destroy(display->fullscreen_shell);
2016-10-19 12:05:12 +03:00
#endif
#ifdef BUILD_IVI
2016-10-19 12:05:12 +03:00
if (display->ivi_application)
ivi_application_destroy(display->ivi_application);
2016-10-19 12:05:12 +03:00
#endif
2016-10-19 12:05:12 +03:00
if (display->xdg_toplevel)
xdg_toplevel_destroy(display->xdg_toplevel);
if (display->xdg_base)
xdg_wm_base_destroy(display->xdg_base);
2016-10-19 12:05:12 +03:00
if (display->shell)
wl_shell_destroy(display->shell);
2016-10-19 12:05:12 +03:00
if (display->shm)
wl_shm_destroy(display->shm);
2016-10-19 12:05:12 +03:00
if (display->subcompositor)
wl_subcompositor_destroy(display->subcompositor);
2016-10-19 12:05:12 +03:00
if (display->data_device_manager)
wl_data_device_manager_destroy(display->data_device_manager);
free(display->shm_formats);
wl_registry_destroy(display->registry);
close(display->epoll_fd);
wl_display_disconnect(display->display);
/* cleanup the event queue */
2016-10-19 12:05:12 +03:00
while (display->push_queue)
{
UwacEventListItem* item = display->push_queue;
display->push_queue = item->tail;
free(item);
}
free(display);
*pdisplay = NULL;
return UWAC_SUCCESS;
}
2016-10-19 12:05:12 +03:00
int UwacDisplayGetFd(UwacDisplay* display)
{
return display->epoll_fd;
}
2019-11-06 17:24:51 +03:00
static const char* errorStrings[] = {
"success",
"out of memory error",
"unable to connect to wayland display",
"invalid UWAC display",
"not enough resources",
"timed out",
"not found",
"closed connection",
"internal error",
};
2016-10-19 12:05:12 +03:00
const char* UwacErrorString(UwacReturnCode error)
{
if (error < UWAC_SUCCESS || error >= UWAC_ERROR_LAST)
return "invalid error code";
return errorStrings[error];
}
2016-10-19 12:05:12 +03:00
UwacReturnCode UwacDisplayQueryInterfaceVersion(const UwacDisplay* display, const char* name,
2019-11-06 17:24:51 +03:00
uint32_t* version)
2016-10-19 12:05:12 +03:00
{
2019-11-06 17:24:51 +03:00
const UwacGlobal *global, *tmp;
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
2019-02-19 11:29:17 +03:00
wl_list_for_each_safe(global, tmp, &display->globals, link)
2016-10-19 12:05:12 +03:00
{
if (strcmp(global->interface, name) == 0)
{
if (version)
*version = global->version;
2016-10-19 12:05:12 +03:00
return UWAC_SUCCESS;
}
}
return UWAC_NOT_FOUND;
}
2016-10-19 12:05:12 +03:00
uint32_t UwacDisplayQueryGetNbShmFormats(UwacDisplay* display)
{
if (!display)
{
return 0;
}
2016-10-19 12:05:12 +03:00
if (!display->shm)
{
display->last_error = UWAC_NOT_FOUND;
return 0;
}
display->last_error = UWAC_SUCCESS;
return display->shm_formats_nb;
}
2016-10-19 12:05:12 +03:00
UwacReturnCode UwacDisplayQueryShmFormats(const UwacDisplay* display, enum wl_shm_format* formats,
2019-11-06 17:24:51 +03:00
int formats_size, int* filled)
2016-10-19 12:05:12 +03:00
{
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
2019-02-07 16:21:04 +03:00
*filled = min((int64_t)display->shm_formats_nb, formats_size);
2016-10-19 12:05:12 +03:00
memcpy(formats, (const void*)display->shm_formats, *filled * sizeof(enum wl_shm_format));
return UWAC_SUCCESS;
}
2016-10-19 12:05:12 +03:00
uint32_t UwacDisplayGetNbOutputs(UwacDisplay* display)
{
return wl_list_length(&display->outputs);
}
2016-10-19 12:05:12 +03:00
UwacOutput* UwacDisplayGetOutput(UwacDisplay* display, int index)
{
struct wl_list* l;
int i;
2016-10-19 12:05:12 +03:00
if (!display)
return NULL;
for (i = 0, l = &display->outputs; l && i < index; i++, l = l->next)
;
2016-10-19 12:05:12 +03:00
if (!l)
{
display->last_error = UWAC_NOT_FOUND;
return NULL;
}
display->last_error = UWAC_SUCCESS;
return container_of(l, UwacOutput, link);
}
2016-10-19 12:05:12 +03:00
UwacReturnCode UwacOutputGetResolution(UwacOutput* output, UwacSize* resolution)
{
*resolution = output->resolution;
return UWAC_SUCCESS;
}
2016-10-19 12:05:12 +03:00
UwacEvent* UwacDisplayNewEvent(UwacDisplay* display, int type)
{
UwacEventListItem* ret;
2016-10-19 12:05:12 +03:00
if (!display)
{
return 0;
}
ret = xzalloc(sizeof(UwacEventListItem));
2016-10-19 12:05:12 +03:00
if (!ret)
{
assert(uwacErrorHandler(display, UWAC_ERROR_NOMEMORY, "unable to allocate a '%s' event",
event_names[type]));
display->last_error = UWAC_ERROR_NOMEMORY;
return 0;
}
ret->event.type = type;
ret->tail = display->push_queue;
2016-10-19 12:05:12 +03:00
if (ret->tail)
ret->tail->head = ret;
else
display->pop_queue = ret;
2016-10-19 12:05:12 +03:00
display->push_queue = ret;
return &ret->event;
}
2016-10-19 12:05:12 +03:00
bool UwacHasEvent(UwacDisplay* display)
{
return display->pop_queue != NULL;
}
2016-10-19 12:05:12 +03:00
UwacReturnCode UwacNextEvent(UwacDisplay* display, UwacEvent* event)
{
UwacEventListItem* prevItem;
int ret;
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
2016-10-19 12:05:12 +03:00
while (!display->pop_queue)
{
ret = UwacDisplayDispatch(display, 1 * 1000);
2016-10-19 12:05:12 +03:00
if (ret < 0)
return UWAC_ERROR_INTERNAL;
else if (ret == 0)
return UWAC_ERROR_CLOSED;
}
prevItem = display->pop_queue->head;
*event = display->pop_queue->event;
free(display->pop_queue);
display->pop_queue = prevItem;
2016-10-19 12:05:12 +03:00
if (prevItem)
prevItem->tail = NULL;
else
display->push_queue = NULL;
2016-10-19 12:05:12 +03:00
return UWAC_SUCCESS;
}