Fixed NULL pointer dereferences.

This commit is contained in:
Armin Novak 2016-10-19 11:05:12 +02:00
parent 92befb0171
commit ab733d76e5
2 changed files with 507 additions and 327 deletions

View File

@ -41,7 +41,8 @@
#define TARGET_SEAT_INTERFACE 5
#define TARGET_XDG_VERSION 5 /* The version of xdg-shell that we implement */
static const char *event_names[] = {
static const char* event_names[] =
{
"new seat",
"removed seat",
"new output",
@ -64,17 +65,18 @@ static const char *event_names[] = {
NULL
};
bool uwac_default_error_handler(UwacDisplay *display, UwacReturnCode code, const char *msg, ...) {
bool uwac_default_error_handler(UwacDisplay* display, UwacReturnCode code, const char* msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stderr, "%s", args);
return false;
}
UwacErrorHandler uwacErrorHandler = uwac_default_error_handler;
void UwacInstallErrorHandler(UwacErrorHandler handler) {
void UwacInstallErrorHandler(UwacErrorHandler handler)
{
if (handler)
uwacErrorHandler = handler;
else
@ -95,7 +97,8 @@ static void cb_shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
}
struct wl_shm_listener shm_listener = {
struct wl_shm_listener shm_listener =
{
cb_shm_format
};
@ -104,7 +107,8 @@ static void xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
xdg_shell_pong(shell, serial);
}
static const struct xdg_shell_listener xdg_shell_listener = {
static const struct xdg_shell_listener xdg_shell_listener =
{
xdg_shell_ping,
};
@ -114,7 +118,8 @@ static void fullscreen_capability(void *data, struct _wl_fullscreen_shell *_wl_f
{
}
static const struct _wl_fullscreen_shell_listener fullscreen_shell_listener = {
static const struct _wl_fullscreen_shell_listener fullscreen_shell_listener =
{
fullscreen_capability,
};
#endif
@ -123,14 +128,14 @@ static const struct _wl_fullscreen_shell_listener fullscreen_shell_listener = {
static UwacSeat* display_destroy_seat(UwacDisplay* d, uint32_t name)
{
UwacSeat* seat;
wl_list_for_each(seat, &d->seats, link) {
if (seat->seat_id == name) {
wl_list_for_each(seat, &d->seats, link)
{
if (seat->seat_id == name)
{
UwacSeatDestroy(seat);
return seat;
}
}
return NULL;
}
@ -139,137 +144,175 @@ static void registry_handle_global(void *data, struct wl_registry *registry, uin
{
UwacDisplay* d = data;
UwacGlobal* global;
global = xmalloc(sizeof * global);
global->name = id;
global->interface = xstrdup(interface);
global->version = version;
wl_list_insert(d->globals.prev, &global->link);
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) {
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)
{
d->shm = wl_registry_bind(registry, id, &wl_shm_interface, min(TARGET_SHM_INTERFACE, version));
wl_shm_add_listener(d->shm, &shm_listener, d);
} else if (strcmp(interface, "wl_output") == 0) {
}
else if (strcmp(interface, "wl_output") == 0)
{
UwacOutput* output;
UwacOutputNewEvent* ev;
output = UwacCreateOutput(d, id, version);
if (!output) {
if (!output)
{
assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create output\n"));
return;
}
ev = (UwacOutputNewEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_NEW_OUTPUT);
if (ev)
ev->output = output;
} else if (strcmp(interface, "wl_seat") == 0) {
}
else if (strcmp(interface, "wl_seat") == 0)
{
UwacSeatNewEvent* ev;
UwacSeat* seat;
seat = UwacSeatNew(d, id, min(version, TARGET_SEAT_INTERFACE));
if (!seat) {
if (!seat)
{
assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create new seat\n"));
return;
}
ev = (UwacSeatNewEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_NEW_SEAT);
if (!ev) {
if (!ev)
{
assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create new seat event\n"));
return;
}
ev->seat = seat;
} else if (strcmp(interface, "wl_data_device_manager") == 0) {
d->data_device_manager = wl_registry_bind(registry, id, &wl_data_device_manager_interface, min(TARGET_DDM_INTERFACE, version));
} else if (strcmp(interface, "wl_shell") == 0) {
d->shell = wl_registry_bind(registry, id, &wl_shell_interface, min(TARGET_SHELL_INTERFACE, version));
} else if (strcmp(interface, "xdg_shell") == 0) {
}
else if (strcmp(interface, "wl_data_device_manager") == 0)
{
d->data_device_manager = wl_registry_bind(registry, id, &wl_data_device_manager_interface,
min(TARGET_DDM_INTERFACE, version));
}
else if (strcmp(interface, "wl_shell") == 0)
{
d->shell = wl_registry_bind(registry, id, &wl_shell_interface, min(TARGET_SHELL_INTERFACE,
version));
}
else if (strcmp(interface, "xdg_shell") == 0)
{
d->xdg_shell = wl_registry_bind(registry, id, &xdg_shell_interface, 1);
xdg_shell_use_unstable_version(d->xdg_shell, TARGET_XDG_VERSION);
xdg_shell_add_listener(d->xdg_shell, &xdg_shell_listener, d);
#if BUILD_IVI
} else if (strcmp(interface, "ivi_application") == 0) {
}
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, "_wl_fullscreen_shell") == 0) {
}
else if (strcmp(interface, "_wl_fullscreen_shell") == 0)
{
d->fullscreen_shell = wl_registry_bind(registry, id, &_wl_fullscreen_shell_interface, 1);
_wl_fullscreen_shell_add_listener(d->fullscreen_shell, &fullscreen_shell_listener, d);
#endif
#if 0
} else if (strcmp(interface, "text_cursor_position") == 0) {
}
else if (strcmp(interface, "text_cursor_position") == 0)
{
d->text_cursor_position = wl_registry_bind(registry, id, &text_cursor_position_interface, 1);
} else if (strcmp(interface, "workspace_manager") == 0) {
}
else if (strcmp(interface, "workspace_manager") == 0)
{
//init_workspace_manager(d, id);
} else if (strcmp(interface, "wl_subcompositor") == 0) {
}
else if (strcmp(interface, "wl_subcompositor") == 0)
{
d->subcompositor = wl_registry_bind(registry, id, &wl_subcompositor_interface, 1);
#endif
}
}
static void registry_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
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) {
wl_list_for_each_safe(global, tmp, &d->globals, link)
{
if (global->name != name)
continue;
#if 0
if (strcmp(global->interface, "wl_output") == 0)
display_destroy_output(d, name);
#endif
if (strcmp(global->interface, "wl_seat") == 0) {
if (strcmp(global->interface, "wl_seat") == 0)
{
UwacSeatRemovedEvent* ev;
UwacSeat* seat;
seat = display_destroy_seat(d, name);
ev = (UwacSeatRemovedEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_REMOVED_SEAT);
if (ev)
ev->seat = seat;
}
wl_list_remove(&global->link);
free(global->interface);
free(global);
}
}
void UwacDestroyGlobal(UwacGlobal *global) {
void UwacDestroyGlobal(UwacGlobal* global)
{
free(global->interface);
wl_list_remove(&global->link);
free(global);
}
void *display_bind(UwacDisplay *display, uint32_t name, const struct wl_interface *interface, uint32_t version) {
void* display_bind(UwacDisplay* display, uint32_t name, const struct wl_interface* interface,
uint32_t version)
{
return wl_registry_bind(display->registry, name, interface, version);
}
static const struct wl_registry_listener registry_listener = {
static const struct wl_registry_listener registry_listener =
{
registry_handle_global,
registry_handle_global_remove
};
int UwacDisplayWatchFd(UwacDisplay *display, int fd, uint32_t events, UwacTask *task) {
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);
}
void UwacDisplayUnwatchFd(UwacDisplay *display, int fd) {
void UwacDisplayUnwatchFd(UwacDisplay* display, int fd)
{
epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
}
static void display_exit(UwacDisplay *display) {
static void display_exit(UwacDisplay* display)
{
display->running = false;
}
@ -278,29 +321,37 @@ static void display_dispatch_events(UwacTask *task, uint32_t events)
UwacDisplay* display = container_of(task, UwacDisplay, dispatch_fd_task);
struct epoll_event ep;
int ret;
display->display_fd_events = events;
if ((events & EPOLLERR) || (events & EPOLLHUP)) {
if ((events & EPOLLERR) || (events & EPOLLHUP))
{
display_exit(display);
return;
}
if (events & EPOLLIN) {
if (events & EPOLLIN)
{
ret = wl_display_dispatch(display->display);
if (ret == -1) {
if (ret == -1)
{
display_exit(display);
return;
}
}
if (events & EPOLLOUT) {
if (events & EPOLLOUT)
{
ret = wl_display_flush(display->display);
if (ret == 0) {
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);
} else if (ret == -1 && errno != EAGAIN) {
}
else if (ret == -1 && errno != EAGAIN)
{
display_exit(display);
return;
}
@ -308,11 +359,13 @@ static void display_dispatch_events(UwacTask *task, uint32_t events)
}
UwacDisplay *UwacOpenDisplay(const char *name, UwacReturnCode *err) {
UwacDisplay* UwacOpenDisplay(const char* name, UwacReturnCode* err)
{
UwacDisplay* ret;
ret = (UwacDisplay*)calloc(1, sizeof(*ret));
if (!ret) {
if (!ret)
{
*err = UWAC_ERROR_NOMEMORY;
return NULL;
}
@ -321,38 +374,46 @@ UwacDisplay *UwacOpenDisplay(const char *name, UwacReturnCode *err) {
wl_list_init(&ret->seats);
wl_list_init(&ret->outputs);
wl_list_init(&ret->windows);
ret->display = wl_display_connect(name);
if (ret->display == NULL) {
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();
if (ret->epoll_fd < 0) {
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);
if (!ret->registry) {
if (!ret->registry)
{
*err = UWAC_ERROR_NOMEMORY;
goto out_close_epoll;
}
wl_registry_add_listener(ret->registry, &registry_listener, ret);
if ((wl_display_roundtrip(ret->display) < 0) || (wl_display_roundtrip(ret->display) < 0)) {
if ((wl_display_roundtrip(ret->display) < 0) || (wl_display_roundtrip(ret->display) < 0))
{
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;
if (UwacDisplayWatchFd(ret, ret->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP, &ret->dispatch_fd_task) < 0) {
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;
@ -361,7 +422,6 @@ UwacDisplay *UwacOpenDisplay(const char *name, UwacReturnCode *err) {
ret->running = true;
ret->last_error = *err = UWAC_SUCCESS;
return ret;
out_free_registry:
wl_registry_destroy(ret->registry);
out_close_epoll:
@ -373,28 +433,33 @@ out_free:
return NULL;
}
int UwacDisplayDispatch(UwacDisplay *display, int timeout) {
int UwacDisplayDispatch(UwacDisplay* display, int timeout)
{
int ret, count, i;
UwacTask* task;
struct epoll_event ep[16];
wl_display_dispatch_pending(display->display);
if (!display->running)
return 0;
ret = wl_display_flush(display->display);
if (ret < 0 && errno == EAGAIN) {
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]);
} else if (ret < 0) {
}
else if (ret < 0)
{
return -1;
}
count = epoll_wait(display->epoll_fd, ep, ARRAY_LENGTH(ep), timeout);
for (i = 0; i < count; i++) {
for (i = 0; i < count; i++)
{
task = ep[i].data.ptr;
task->run(task, ep[i].events);
}
@ -404,74 +469,85 @@ int UwacDisplayDispatch(UwacDisplay *display, int timeout) {
UwacReturnCode UwacDisplayGetLastError(const UwacDisplay *display) {
UwacReturnCode UwacDisplayGetLastError(const UwacDisplay* display)
{
return display->last_error;
}
UwacReturnCode UwacCloseDisplay(UwacDisplay **pdisplay) {
UwacReturnCode UwacCloseDisplay(UwacDisplay** pdisplay)
{
UwacDisplay* display;
UwacSeat* seat, *tmpSeat;
UwacWindow* window, *tmpWindow;
UwacOutput* output, *tmpOutput;
UwacGlobal* global, *tmpGlobal;
assert(pdisplay);
display = *pdisplay;
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
/* destroy windows */
wl_list_for_each_safe(window, tmpWindow, &display->windows, link) {
wl_list_for_each_safe(window, tmpWindow, &display->windows, link)
{
UwacDestroyWindow(&window);
}
/* destroy seats */
wl_list_for_each_safe(seat, tmpSeat, &display->seats, link) {
wl_list_for_each_safe(seat, tmpSeat, &display->seats, link)
{
UwacSeatDestroy(seat);
}
/* destroy output */
wl_list_for_each_safe(output, tmpOutput, &display->outputs, link) {
wl_list_for_each_safe(output, tmpOutput, &display->outputs, link)
{
UwacDestroyOutput(output);
}
/* destroy globals */
wl_list_for_each_safe(global, tmpGlobal, &display->globals, link) {
wl_list_for_each_safe(global, tmpGlobal, &display->globals, link)
{
UwacDestroyGlobal(global);
}
if (display->compositor)
wl_compositor_destroy(display->compositor);
#ifdef BUILD_FULLSCREEN_SHELL
if (display->fullscreen_shell)
_wl_fullscreen_shell_destroy(display->fullscreen_shell);
#endif
#ifdef BUILD_IVI
if (display->ivi_application)
ivi_application_destroy(display->ivi_application);
#endif
if (display->xdg_shell)
xdg_shell_destroy(display->xdg_shell);
if (display->shell)
wl_shell_destroy(display->shell);
if (display->shm)
wl_shm_destroy(display->shm);
if (display->subcompositor)
wl_subcompositor_destroy(display->subcompositor);
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 */
while (display->push_queue) {
while (display->push_queue)
{
UwacEventListItem* item = display->push_queue;
display->push_queue = item->tail;
free(item);
}
@ -481,11 +557,13 @@ UwacReturnCode UwacCloseDisplay(UwacDisplay **pdisplay) {
return UWAC_SUCCESS;
}
int UwacDisplayGetFd(UwacDisplay *display) {
int UwacDisplayGetFd(UwacDisplay* display)
{
return display->epoll_fd;
}
static const char *errorStrings[] = {
static const char* errorStrings[] =
{
"success",
"out of memory error",
"unable to connect to wayland display",
@ -498,37 +576,44 @@ static const char *errorStrings[] = {
"internal error",
};
const char *UwacErrorString(UwacReturnCode error) {
const char* UwacErrorString(UwacReturnCode error)
{
if (error < UWAC_SUCCESS || error >= UWAC_ERROR_LAST)
return "invalid error code";
return errorStrings[error];
}
UwacReturnCode UwacDisplayQueryInterfaceVersion(const UwacDisplay *display, const char *name, uint32_t *version) {
UwacReturnCode UwacDisplayQueryInterfaceVersion(const UwacDisplay* display, const char* name,
uint32_t* version)
{
const UwacGlobal* global;
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
wl_list_for_each(global, &display->globals, link) {
if (strcmp(global->interface, name) == 0) {
wl_list_for_each(global, &display->globals, link)
{
if (strcmp(global->interface, name) == 0)
{
if (version)
*version = global->version;
return UWAC_SUCCESS;
}
}
return UWAC_NOT_FOUND;
}
uint32_t UwacDisplayQueryGetNbShmFormats(UwacDisplay *display) {
if (!display) {
display->last_error = UWAC_ERROR_INVALID_DISPLAY;
uint32_t UwacDisplayQueryGetNbShmFormats(UwacDisplay* display)
{
if (!display)
{
return 0;
}
if (!display->shm) {
if (!display->shm)
{
display->last_error = UWAC_NOT_FOUND;
return 0;
}
@ -538,28 +623,35 @@ uint32_t UwacDisplayQueryGetNbShmFormats(UwacDisplay *display) {
}
UwacReturnCode UwacDisplayQueryShmFormats(const UwacDisplay *display, enum wl_shm_format *formats, int formats_size, int *filled) {
UwacReturnCode UwacDisplayQueryShmFormats(const UwacDisplay* display, enum wl_shm_format* formats,
int formats_size, int* filled)
{
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
*filled = min(display->shm_formats_nb, formats_size);
memcpy(formats, (const void*)display->shm_formats, *filled * sizeof(enum wl_shm_format));
return UWAC_SUCCESS;
}
uint32_t UwacDisplayGetNbOutputs(UwacDisplay *display) {
uint32_t UwacDisplayGetNbOutputs(UwacDisplay* display)
{
return wl_list_length(&display->outputs);
}
UwacOutput *UwacDisplayGetOutput(UwacDisplay *display, int index) {
UwacOutput* UwacDisplayGetOutput(UwacDisplay* display, int index)
{
struct wl_list* l;
int i;
if (!display)
return NULL;
for (i = 0, l = &display->outputs; l && i < index; i++, l = l->next)
;
if (!l) {
if (!l)
{
display->last_error = UWAC_NOT_FOUND;
return NULL;
}
@ -568,51 +660,62 @@ UwacOutput *UwacDisplayGetOutput(UwacDisplay *display, int index) {
return container_of(l, UwacOutput, link);
}
UwacReturnCode UwacOutputGetResolution(UwacOutput *output, UwacSize *resolution) {
UwacReturnCode UwacOutputGetResolution(UwacOutput* output, UwacSize* resolution)
{
*resolution = output->resolution;
return UWAC_SUCCESS;
}
UwacEvent *UwacDisplayNewEvent(UwacDisplay *display, int type) {
UwacEvent* UwacDisplayNewEvent(UwacDisplay* display, int type)
{
UwacEventListItem* ret;
if (!display) {
display->last_error = UWAC_ERROR_INVALID_DISPLAY;
if (!display)
{
return 0;
}
ret = zalloc(sizeof(UwacEventListItem));
if (!ret) {
assert(uwacErrorHandler(display, UWAC_ERROR_NOMEMORY, "unable to allocate a '%s' event", event_names[type]));
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;
if (ret->tail)
ret->tail->head = ret;
else
display->pop_queue = ret;
display->push_queue = ret;
return &ret->event;
}
bool UwacHasEvent(UwacDisplay *display) {
bool UwacHasEvent(UwacDisplay* display)
{
return display->pop_queue != NULL;
}
UwacReturnCode UwacNextEvent(UwacDisplay *display, UwacEvent *event) {
UwacReturnCode UwacNextEvent(UwacDisplay* display, UwacEvent* event)
{
UwacEventListItem* prevItem;
int ret;
if (!display)
return UWAC_ERROR_INVALID_DISPLAY;
while (!display->pop_queue) {
while (!display->pop_queue)
{
ret = UwacDisplayDispatch(display, 1 * 1000);
if (ret < 0)
return UWAC_ERROR_INTERNAL;
else if (ret == 0)
@ -623,9 +726,11 @@ UwacReturnCode UwacNextEvent(UwacDisplay *display, UwacEvent *event) {
*event = display->pop_queue->event;
free(display->pop_queue);
display->pop_queue = prevItem;
if (prevItem)
prevItem->tail = NULL;
else
display->push_queue = NULL;
return UWAC_SUCCESS;
}

View File

@ -35,8 +35,10 @@
#define UWAC_INITIAL_BUFFERS 3
static int bppFromShmFormat(enum wl_shm_format format) {
switch (format) {
static int bppFromShmFormat(enum wl_shm_format format)
{
switch (format)
{
case WL_SHM_FORMAT_ARGB8888:
case WL_SHM_FORMAT_XRGB8888:
default:
@ -45,20 +47,23 @@ static int bppFromShmFormat(enum wl_shm_format format) {
}
static void buffer_release(void *data, struct wl_buffer *buffer) {
static void buffer_release(void* data, struct wl_buffer* buffer)
{
UwacBuffer* uwacBuffer = (UwacBuffer*)data;
uwacBuffer->used = false;
}
static const struct wl_buffer_listener buffer_listener = {
static const struct wl_buffer_listener buffer_listener =
{
buffer_release
};
void UwacWindowDestroyBuffers(UwacWindow *w) {
void UwacWindowDestroyBuffers(UwacWindow* w)
{
int i;
for (i = 0; i < w->nbuffers; i++) {
for (i = 0; i < w->nbuffers; i++)
{
UwacBuffer* buffer = &w->buffers[i];
#ifdef HAVE_PIXMAN_REGION
pixman_region32_fini(&buffer->damage);
@ -85,56 +90,66 @@ static void xdg_handle_configure(void *data, struct xdg_surface *surface,
UwacConfigureEvent* event;
int ret, surfaceState;
enum xdg_surface_state* state;
surfaceState = 0;
wl_array_for_each(state, states) {
switch (*state) {
wl_array_for_each(state, states)
{
switch (*state)
{
case XDG_SURFACE_STATE_MAXIMIZED:
surfaceState |= UWAC_WINDOW_MAXIMIZED;
break;
case XDG_SURFACE_STATE_FULLSCREEN:
surfaceState |= UWAC_WINDOW_FULLSCREEN;
break;
case XDG_SURFACE_STATE_ACTIVATED:
surfaceState |= UWAC_WINDOW_ACTIVATED;
break;
case XDG_SURFACE_STATE_RESIZING:
surfaceState |= UWAC_WINDOW_RESIZING;
break;
default:
break;
}
}
window->surfaceStates = surfaceState;
event = (UwacConfigureEvent*)UwacDisplayNewEvent(window->display, UWAC_EVENT_CONFIGURE);
if(!event) {
assert(uwacErrorHandler(window->display, UWAC_ERROR_NOMEMORY, "failed to allocate a configure event\n"));
if (!event)
{
assert(uwacErrorHandler(window->display, UWAC_ERROR_NOMEMORY,
"failed to allocate a configure event\n"));
goto ack;
}
event->window = window;
event->states = surfaceState;
if (width && height) {
if (width && height)
{
event->width = width;
event->height = height;
UwacWindowDestroyBuffers(window);
window->width = width;
window->stride = width * bppFromShmFormat(window->format);
window->height = height;
ret = UwacWindowShmAllocBuffers(window, UWAC_INITIAL_BUFFERS, window->stride * height,
width, height, window->format);
if (ret != UWAC_SUCCESS) {
if (ret != UWAC_SUCCESS)
{
assert(uwacErrorHandler(window->display, ret, "failed to reallocate a wayland buffers\n"));
window->drawingBuffer = window->pendingBuffer = NULL;
goto ack;
}
window->drawingBuffer = window->pendingBuffer = &window->buffers[0];
} else {
}
else
{
event->width = window->width;
event->height = window->height;
}
@ -147,17 +162,20 @@ static void xdg_handle_close(void *data, struct xdg_surface *xdg_surface)
{
UwacCloseEvent* event;
UwacWindow* window = (UwacWindow*)data;
event = (UwacCloseEvent*)UwacDisplayNewEvent(window->display, UWAC_EVENT_CLOSE);
if(!event) {
assert(uwacErrorHandler(window->display, UWAC_ERROR_INTERNAL, "failed to allocate a close event\n"));
if (!event)
{
assert(uwacErrorHandler(window->display, UWAC_ERROR_INTERNAL,
"failed to allocate a close event\n"));
return;
}
event->window = window;
}
static const struct xdg_surface_listener xdg_surface_listener = {
static const struct xdg_surface_listener xdg_surface_listener =
{
xdg_handle_configure,
xdg_handle_close,
};
@ -170,41 +188,47 @@ static void ivi_handle_configure(void *data, struct ivi_surface *surface,
UwacWindow* window = (UwacWindow*)data;
UwacConfigureEvent* event;
int ret;
event = (UwacConfigureEvent*)UwacDisplayNewEvent(window->display, UWAC_EVENT_CONFIGURE);
if(!event) {
assert(uwacErrorHandler(window->display, UWAC_ERROR_NOMEMORY, "failed to allocate a configure event\n"));
if (!event)
{
assert(uwacErrorHandler(window->display, UWAC_ERROR_NOMEMORY,
"failed to allocate a configure event\n"));
return;
}
event->window = window;
event->states = 0;
if (width && height) {
if (width && height)
{
event->width = width;
event->height = height;
UwacWindowDestroyBuffers(window);
window->width = width;
window->stride = width * bppFromShmFormat(window->format);
window->height = height;
ret = UwacWindowShmAllocBuffers(window, UWAC_INITIAL_BUFFERS, window->stride * height,
width, height, window->format);
if (ret != UWAC_SUCCESS) {
if (ret != UWAC_SUCCESS)
{
assert(uwacErrorHandler(window->display, ret, "failed to reallocate a wayland buffers\n"));
window->drawingBuffer = window->pendingBuffer = NULL;
return;
}
window->drawingBuffer = window->pendingBuffer = &window->buffers[0];
} else {
}
else
{
event->width = window->width;
event->height = window->height;
}
}
static const struct ivi_surface_listener ivi_surface_listener = {
static const struct ivi_surface_listener ivi_surface_listener =
{
ivi_handle_configure,
};
#endif
@ -220,38 +244,43 @@ void shell_configure(void *data, struct wl_shell_surface *surface, uint32_t edge
UwacWindow* window = (UwacWindow*)data;
UwacConfigureEvent* event;
int ret;
event = (UwacConfigureEvent*)UwacDisplayNewEvent(window->display, UWAC_EVENT_CONFIGURE);
if(!event) {
assert(uwacErrorHandler(window->display, UWAC_ERROR_NOMEMORY, "failed to allocate a configure event\n"));
if (!event)
{
assert(uwacErrorHandler(window->display, UWAC_ERROR_NOMEMORY,
"failed to allocate a configure event\n"));
return;
}
event->window = window;
event->states = 0;
if (width && height) {
if (width && height)
{
event->width = width;
event->height = height;
UwacWindowDestroyBuffers(window);
window->width = width;
window->stride = width * bppFromShmFormat(window->format);
window->height = height;
ret = UwacWindowShmAllocBuffers(window, UWAC_INITIAL_BUFFERS, window->stride * height,
width, height, window->format);
if (ret != UWAC_SUCCESS) {
if (ret != UWAC_SUCCESS)
{
assert(uwacErrorHandler(window->display, ret, "failed to reallocate a wayland buffers\n"));
window->drawingBuffer = window->pendingBuffer = NULL;
return;
}
window->drawingBuffer = window->pendingBuffer = &window->buffers[0];
} else {
}
else
{
event->width = window->width;
event->height = window->height;
}
}
@ -260,7 +289,8 @@ void shell_popup_done(void *data, struct wl_shell_surface *surface)
}
static const struct wl_shell_surface_listener shell_listener = {
static const struct wl_shell_surface_listener shell_listener =
{
shell_ping,
shell_configure,
shell_popup_done
@ -275,33 +305,38 @@ int UwacWindowShmAllocBuffers(UwacWindow *w, int nbuffers, int allocSize, uint32
int i, fd;
void* data;
struct wl_shm_pool* pool;
newBuffers = realloc(w->buffers, (w->nbuffers + nbuffers) * sizeof(UwacBuffer));
if (!newBuffers)
return UWAC_ERROR_NOMEMORY;
w->buffers = newBuffers;
memset(w->buffers + w->nbuffers, 0, sizeof(UwacBuffer) * nbuffers);
fd = uwac_create_anonymous_file(allocSize * nbuffers);
if (fd < 0) {
if (fd < 0)
{
return UWAC_ERROR_INTERNAL;
}
data = mmap(NULL, allocSize * nbuffers, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
if (data == MAP_FAILED)
{
ret = UWAC_ERROR_NOMEMORY;
goto error_mmap;
}
pool = wl_shm_create_pool(w->display->shm, fd, allocSize * nbuffers);
if (!pool) {
if (!pool)
{
ret = UWAC_ERROR_NOMEMORY;
goto error_mmap;
}
for (i = 0; i < nbuffers; i++) {
for (i = 0; i < nbuffers; i++)
{
UwacBuffer* buffer = &w->buffers[w->nbuffers + i];
#ifdef HAVE_PIXMAN_REGION
pixman_region32_init(&buffer->damage);
@ -309,32 +344,35 @@ int UwacWindowShmAllocBuffers(UwacWindow *w, int nbuffers, int allocSize, uint32
region16_init(&buffer->damage);
#endif
buffer->data = data + (allocSize * i);
buffer->wayland_buffer = wl_shm_pool_create_buffer(pool, allocSize * i, width, height, w->stride, format);
buffer->wayland_buffer = wl_shm_pool_create_buffer(pool, allocSize * i, width, height, w->stride,
format);
wl_buffer_add_listener(buffer->wayland_buffer, &buffer_listener, buffer);
}
wl_shm_pool_destroy(pool);
w->nbuffers += nbuffers;
error_mmap:
close(fd);
return ret;
}
UwacBuffer *UwacWindowFindFreeBuffer(UwacWindow *w) {
UwacBuffer* UwacWindowFindFreeBuffer(UwacWindow* w)
{
int i, ret;
for (i = 0; i < w->nbuffers; i++) {
if (!w->buffers[i].used) {
for (i = 0; i < w->nbuffers; i++)
{
if (!w->buffers[i].used)
{
w->buffers[i].used = true;
return &w->buffers[i];
}
}
ret = UwacWindowShmAllocBuffers(w, 2, w->stride * w->height, w->width, w->height, w->format);
if (ret != UWAC_SUCCESS) {
if (ret != UWAC_SUCCESS)
{
w->display->last_error = ret;
return NULL;
}
@ -344,17 +382,21 @@ UwacBuffer *UwacWindowFindFreeBuffer(UwacWindow *w) {
}
UwacWindow *UwacCreateWindowShm(UwacDisplay *display, uint32_t width, uint32_t height, enum wl_shm_format format) {
UwacWindow* UwacCreateWindowShm(UwacDisplay* display, uint32_t width, uint32_t height,
enum wl_shm_format format)
{
UwacWindow* w;
int allocSize, ret;
if (!display) {
display->last_error = UWAC_ERROR_INVALID_DISPLAY;
if (!display)
{
return NULL;
}
w = zalloc(sizeof(*w));
if (!w) {
if (!w)
{
display->last_error = UWAC_ERROR_NOMEMORY;
return NULL;
}
@ -365,60 +407,65 @@ UwacWindow *UwacCreateWindowShm(UwacDisplay *display, uint32_t width, uint32_t h
w->height = height;
w->stride = width * bppFromShmFormat(format);
allocSize = w->stride * height;
ret = UwacWindowShmAllocBuffers(w, UWAC_INITIAL_BUFFERS, allocSize, width, height, format);
if (ret != UWAC_SUCCESS) {
if (ret != UWAC_SUCCESS)
{
display->last_error = ret;
goto out_error_free;
}
w->buffers[0].used = true;
w->drawingBuffer = &w->buffers[0];
w->surface = wl_compositor_create_surface(display->compositor);
if (!w->surface) {
if (!w->surface)
{
display->last_error = UWAC_ERROR_NOMEMORY;
goto out_error_surface;
}
wl_surface_set_user_data(w->surface, w);
if (display->xdg_shell) {
if (display->xdg_shell)
{
w->xdg_surface = xdg_shell_get_xdg_surface(display->xdg_shell, w->surface);
if (!w->xdg_surface) {
if (!w->xdg_surface)
{
display->last_error = UWAC_ERROR_NOMEMORY;
goto out_error_shell;
}
assert(w->xdg_surface);
xdg_surface_add_listener(w->xdg_surface, &xdg_surface_listener, w);
#if BUILD_IVI
} else if (display->ivi_application) {
}
else if (display->ivi_application)
{
w->ivi_surface = ivi_application_surface_create(display->ivi_application, 1, w->surface);
assert(w->ivi_surface);
ivi_surface_add_listener(w->ivi_surface, &ivi_surface_listener, w);
#endif
#if BUILD_FULLSCREEN_SHELL
} else if (display->fullscreen_shell) {
}
else if (display->fullscreen_shell)
{
_wl_fullscreen_shell_present_surface(display->fullscreen_shell, w->surface,
_WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER, NULL);
#endif
} else {
}
else
{
w->shell_surface = wl_shell_get_shell_surface(display->shell, w->surface);
assert(w->shell_surface);
wl_shell_surface_add_listener(w->shell_surface, &shell_listener, w);
wl_shell_surface_set_toplevel(w->shell_surface);
}
wl_list_insert(display->windows.prev, &w->link);
display->last_error = UWAC_SUCCESS;
return w;
out_error_shell:
wl_surface_destroy(w->surface);
out_error_surface:
@ -429,11 +476,10 @@ out_error_free:
}
UwacReturnCode UwacDestroyWindow(UwacWindow **pwindow) {
UwacReturnCode UwacDestroyWindow(UwacWindow** pwindow)
{
UwacWindow* w;
assert(pwindow);
w = *pwindow;
UwacWindowDestroyBuffers(w);
@ -442,9 +488,12 @@ UwacReturnCode UwacDestroyWindow(UwacWindow **pwindow) {
if (w->xdg_surface)
xdg_surface_destroy(w->xdg_surface);
#if BUILD_IVI
if (w->ivi_surface)
ivi_surface_destroy(w->ivi_surface);
#endif
if (w->opaque_region)
@ -456,7 +505,6 @@ UwacReturnCode UwacDestroyWindow(UwacWindow **pwindow) {
wl_surface_destroy(w->surface);
wl_list_remove(&w->link);
free(w);
*pwindow = NULL;
return UWAC_SUCCESS;
}
@ -471,6 +519,7 @@ UwacReturnCode UwacWindowSetOpaqueRegion(UwacWindow *window, uint32_t x, uint32_
wl_region_destroy(window->opaque_region);
window->opaque_region = wl_compositor_create_region(window->display->compositor);
if (!window->opaque_region)
return UWAC_ERROR_NOMEMORY;
@ -479,13 +528,16 @@ UwacReturnCode UwacWindowSetOpaqueRegion(UwacWindow *window, uint32_t x, uint32_
return UWAC_SUCCESS;
}
UwacReturnCode UwacWindowSetInputRegion(UwacWindow *window, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
UwacReturnCode UwacWindowSetInputRegion(UwacWindow* window, uint32_t x, uint32_t y, uint32_t width,
uint32_t height)
{
assert(window);
if (window->input_region)
wl_region_destroy(window->input_region);
window->input_region = wl_compositor_create_region(window->display->compositor);
if (!window->input_region)
return UWAC_ERROR_NOMEMORY;
@ -495,35 +547,41 @@ UwacReturnCode UwacWindowSetInputRegion(UwacWindow *window, uint32_t x, uint32_t
}
void *UwacWindowGetDrawingBuffer(UwacWindow *window) {
void* UwacWindowGetDrawingBuffer(UwacWindow* window)
{
return window->drawingBuffer->data;
}
static void frame_done_cb(void* data, struct wl_callback* callback, uint32_t time);
static const struct wl_callback_listener frame_listener = {
static const struct wl_callback_listener frame_listener =
{
frame_done_cb
};
static void UwacSubmitBufferPtr(UwacWindow *window, UwacBuffer *buffer) {
static void UwacSubmitBufferPtr(UwacWindow* window, UwacBuffer* buffer)
{
UINT32 nrects, i;
#ifdef HAVE_PIXMAN_REGION
const pixman_box32_t* box;
#else
const RECTANGLE_16* box;
#endif
wl_surface_attach(window->surface, buffer->wayland_buffer, 0, 0);
#ifdef HAVE_PIXMAN_REGION
box = pixman_region32_rectangles(&buffer->damage, &nrects);
for (i = 0; i < nrects; i++, box++)
wl_surface_damage(window->surface, box->x1, box->y1, (box->x2 - box->x1), (box->y2 - box->y1));
#else
box = region16_rects(&buffer->damage, &nrects);
for (i = 0; i < nrects; i++, box++)
wl_surface_damage(window->surface, box->left, box->top, (box->right - box->left), (box->bottom - box->top));
wl_surface_damage(window->surface, box->left, box->top, (box->right - box->left),
(box->bottom - box->top));
#endif
if (window->frame_callback)
@ -531,9 +589,7 @@ static void UwacSubmitBufferPtr(UwacWindow *window, UwacBuffer *buffer) {
window->frame_callback = wl_surface_frame(window->surface);
wl_callback_add_listener(window->frame_callback, &frame_listener, window);
wl_surface_commit(window->surface);
#ifdef HAVE_PIXMAN_REGION
pixman_region32_clear(&buffer->damage);
#else
@ -542,24 +598,32 @@ static void UwacSubmitBufferPtr(UwacWindow *window, UwacBuffer *buffer) {
}
static void frame_done_cb(void *data, struct wl_callback *callback, uint32_t time) {
static void frame_done_cb(void* data, struct wl_callback* callback, uint32_t time)
{
UwacWindow* window = (UwacWindow*)data;
UwacFrameDoneEvent* event;
window->pendingBuffer = NULL;
event = (UwacFrameDoneEvent*)UwacDisplayNewEvent(window->display, UWAC_EVENT_FRAME_DONE);
if (event)
event->window = window;
}
UwacReturnCode UwacWindowAddDamage(UwacWindow *window, uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
UwacReturnCode UwacWindowAddDamage(UwacWindow* window, uint32_t x, uint32_t y, uint32_t width,
uint32_t height)
{
#ifdef HAVE_PIXMAN_REGION
if (!pixman_region32_union_rect(&window->drawingBuffer->damage, &window->drawingBuffer->damage, x, y, width, height))
if (!pixman_region32_union_rect(&window->drawingBuffer->damage, &window->drawingBuffer->damage, x,
y, width, height))
#else
RECTANGLE_16 box;
box.left = x; box.top = y;
box.right = x + width; box.bottom = y + height;
box.left = x;
box.top = y;
box.right = x + width;
box.bottom = y + height;
if (!region16_union_rect(&window->drawingBuffer->damage, &window->drawingBuffer->damage, &box))
#endif
@ -569,50 +633,61 @@ UwacReturnCode UwacWindowAddDamage(UwacWindow *window, uint32_t x, uint32_t y, u
}
UwacReturnCode UwacWindowSubmitBuffer(UwacWindow *window, bool copyContentForNextFrame) {
UwacReturnCode UwacWindowSubmitBuffer(UwacWindow* window, bool copyContentForNextFrame)
{
UwacBuffer* drawingBuffer = window->drawingBuffer;
if (window->pendingBuffer) {
if (window->pendingBuffer)
{
/* we already have a pending frame, don't do anything*/
return UWAC_SUCCESS;
}
UwacSubmitBufferPtr(window, drawingBuffer);
window->pendingBuffer = window->drawingBuffer;
window->drawingBuffer = UwacWindowFindFreeBuffer(window);
if (!window->drawingBuffer)
return UWAC_ERROR_NOMEMORY;
if (copyContentForNextFrame) {
if (copyContentForNextFrame)
{
memcpy(window->drawingBuffer->data, window->pendingBuffer->data, window->stride * window->height);
}
return UWAC_SUCCESS;
}
UwacReturnCode UwacWindowGetGeometry(UwacWindow *window, UwacSize *geometry) {
UwacReturnCode UwacWindowGetGeometry(UwacWindow* window, UwacSize* geometry)
{
assert(window);
assert(geometry);
geometry->width = window->width;
geometry->height = window->height;
return UWAC_SUCCESS;
}
UwacReturnCode UwacWindowSetFullscreenState(UwacWindow *window, UwacOutput *output, bool isFullscreen) {
if (window->xdg_surface) {
if (isFullscreen) {
UwacReturnCode UwacWindowSetFullscreenState(UwacWindow* window, UwacOutput* output,
bool isFullscreen)
{
if (window->xdg_surface)
{
if (isFullscreen)
{
xdg_surface_set_fullscreen(window->xdg_surface, output ? output->output : NULL);
} else {
}
else
{
xdg_surface_unset_fullscreen(window->xdg_surface);
}
}
return UWAC_SUCCESS;
}
void UwacWindowSetTitle(UwacWindow *window, const char *name) {
void UwacWindowSetTitle(UwacWindow* window, const char* name)
{
if (window->xdg_surface)
xdg_surface_set_title(window->xdg_surface, name);
else if (window->shell_surface)