commit 09023ff8933523d926512b84b01ed1761a92ea6a
parent 22c4feed469bfc5f5e70c09a8d0204da502c0ceb
Author: Michael Forney <mforney@mforney.org>
Date: Sun, 20 Jul 2014 21:48:41 -0700
Use wl_container_of instead of CONTAINER_OF
Since wayland-1.5, wl_container_of has defined behavior when called with
an uninitialized pointer for the "sample" argument.
Diffstat:
16 files changed, 44 insertions(+), 63 deletions(-)
diff --git a/Makefile b/Makefile
@@ -22,7 +22,8 @@ TARGETS := swc.pc
SUBDIRS := launch libswc protocol cursor example
CLEAN_FILES := $(TARGETS)
-libinput_CONSTRAINTS := >= 0.4
+libinput_CONSTRAINTS := >= 0.4
+wayland-server_CONSTRAINTS := >= 1.5.0
include config.mk
diff --git a/libswc/compositor.c b/libswc/compositor.c
@@ -98,7 +98,7 @@ static void handle_screen_event(struct wl_listener * listener, void * data)
if (event->type == SWC_SCREEN_DESTROYED)
{
struct target * target
- = CONTAINER_OF(listener, typeof(*target), screen_listener);
+ = wl_container_of(listener, target, screen_listener);
wld_destroy_surface(target->surface);
free(target);
@@ -111,16 +111,14 @@ static struct target * target_get(struct screen * screen)
= wl_signal_get(&screen->base.event_signal, &handle_screen_event);
struct target * target;
- return listener ? CONTAINER_OF(listener, typeof(*target), screen_listener)
- : NULL;
+ return listener ? wl_container_of(listener, target, screen_listener) : NULL;
}
static void handle_screen_view_event(struct wl_listener * listener, void * data)
{
struct swc_event * event = data;
struct view_event_data * event_data = event->data;
- struct target * target
- = CONTAINER_OF(listener, typeof(*target), view_listener);
+ struct target * target = wl_container_of(listener, target, view_listener);
switch (event->type)
{
diff --git a/libswc/cursor_plane.c b/libswc/cursor_plane.c
@@ -40,7 +40,7 @@ static bool update(struct view * view)
static bool attach(struct view * view, struct wld_buffer * buffer)
{
- struct cursor_plane * plane = CONTAINER_OF(view, typeof(*plane), view);
+ struct cursor_plane * plane = wl_container_of(view, plane, view);
if (buffer)
{
@@ -75,7 +75,7 @@ static bool attach(struct view * view, struct wld_buffer * buffer)
static bool move(struct view * view, int32_t x, int32_t y)
{
- struct cursor_plane * plane = CONTAINER_OF(view, typeof(*plane), view);
+ struct cursor_plane * plane = wl_container_of(view, plane, view);
if (drmModeMoveCursor(swc.drm->fd, plane->crtc,
x - plane->origin->x, y - plane->origin->y) != 0)
@@ -99,7 +99,7 @@ static void handle_launch_event(struct wl_listener * listener, void * data)
{
struct swc_event * event = data;
struct cursor_plane * plane
- = CONTAINER_OF(listener, typeof(*plane), launch_listener);
+ = wl_container_of(listener, plane, launch_listener);
switch (event->type)
{
diff --git a/libswc/data_device.c b/libswc/data_device.c
@@ -68,8 +68,8 @@ static struct wl_data_device_interface data_device_implementation = {
static void handle_selection_destroy(struct wl_listener * listener, void * data)
{
- struct data_device * data_device = CONTAINER_OF
- (listener, typeof(*data_device), selection_destroy_listener);
+ struct data_device * data_device
+ = wl_container_of(listener, data_device, selection_destroy_listener);
data_device->selection = NULL;
swc_send_event(&data_device->event_signal,
diff --git a/libswc/framebuffer_plane.c b/libswc/framebuffer_plane.c
@@ -50,7 +50,7 @@ static bool framebuffer_export(struct wld_exporter * exporter,
uint32_t type, union wld_object * object)
{
struct framebuffer * framebuffer
- = CONTAINER_OF(exporter, typeof(*framebuffer), exporter);
+ = wl_container_of(exporter, framebuffer, exporter);
switch (type)
{
@@ -66,7 +66,7 @@ static bool framebuffer_export(struct wld_exporter * exporter,
static void framebuffer_destroy(struct wld_destructor * destructor)
{
struct framebuffer * framebuffer
- = CONTAINER_OF(destructor, typeof(*framebuffer), destructor);
+ = wl_container_of(destructor, framebuffer, destructor);
drmModeRmFB(swc.drm->fd, framebuffer->id);
free(framebuffer);
@@ -86,8 +86,7 @@ static void send_frame(void * data)
static bool attach(struct view * view, struct wld_buffer * buffer)
{
- struct framebuffer_plane * plane
- = CONTAINER_OF(view, typeof(*plane), view);
+ struct framebuffer_plane * plane = wl_container_of(view, plane, view);
union wld_object object;
if (!wld_export(buffer, WLD_USER_OBJECT_FRAMEBUFFER, &object))
@@ -163,7 +162,7 @@ const static struct view_impl view_impl = {
static void handle_page_flip(struct swc_drm_handler * handler, uint32_t time)
{
struct framebuffer_plane * plane
- = CONTAINER_OF(handler, typeof(*plane), drm_handler);
+ = wl_container_of(handler, plane, drm_handler);
view_frame(&plane->view, time);
}
@@ -172,7 +171,7 @@ static void handle_launch_event(struct wl_listener * listener, void * data)
{
struct swc_event * event = data;
struct framebuffer_plane * plane
- = CONTAINER_OF(listener, typeof(*plane), launch_listener);
+ = wl_container_of(listener, plane, launch_listener);
switch (event->type)
{
diff --git a/libswc/input.c b/libswc/input.c
@@ -66,7 +66,7 @@ static void handle_focus_view_destroy(struct wl_listener * listener,
void * data)
{
struct input_focus * input_focus
- = CONTAINER_OF(listener, typeof(*input_focus), view_destroy_listener);
+ = wl_container_of(listener, input_focus, view_destroy_listener);
input_focus->resource = NULL;
input_focus->view = NULL;
diff --git a/libswc/keyboard.c b/libswc/keyboard.c
@@ -38,7 +38,7 @@ static void enter(struct input_focus_handler * handler,
struct wl_resource * resource, struct compositor_view * view)
{
struct keyboard * keyboard
- = CONTAINER_OF(handler, typeof(*keyboard), focus_handler);
+ = wl_container_of(handler, keyboard, focus_handler);
uint32_t serial;
serial = wl_display_next_serial(swc.display);
diff --git a/libswc/panel.c b/libswc/panel.c
@@ -89,7 +89,7 @@ static void dock(struct wl_client * client, struct wl_resource * resource,
struct swc_output * output = output_resource
? wl_resource_get_user_data(output_resource) : NULL;
struct screen * screen = output
- ? output->screen : CONTAINER_OF(swc.screens.next, struct screen, link);
+ ? output->screen : wl_container_of(swc.screens.next, screen, link);
bool screen_changed = screen != panel->screen;
uint32_t length;
@@ -169,7 +169,7 @@ static void modify(struct screen_modifier * modifier,
const struct swc_rectangle * geometry,
pixman_region32_t * usable)
{
- struct panel * panel = CONTAINER_OF(modifier, typeof(*panel), modifier);
+ struct panel * panel = wl_container_of(modifier, panel, modifier);
pixman_box32_t box = {
.x1 = geometry->x, .y1 = geometry->y,
.x2 = geometry->x + geometry->width,
@@ -222,11 +222,9 @@ static void destroy_panel(struct wl_resource * resource)
static void handle_view_event(struct wl_listener * listener, void * data)
{
- struct panel * panel;
+ struct panel * panel = wl_container_of(listener, panel, view_listener);
struct swc_event * event = data;
- panel = CONTAINER_OF(listener, typeof(*panel), view_listener);
-
switch (event->type)
{
case VIEW_EVENT_RESIZED:
@@ -237,9 +235,9 @@ static void handle_view_event(struct wl_listener * listener, void * data)
static void handle_surface_destroy(struct wl_listener * listener, void * data)
{
- struct panel * panel;
+ struct panel * panel
+ = wl_container_of(listener, panel, surface_destroy_listener);
- panel = CONTAINER_OF(listener, typeof(*panel), surface_destroy_listener);
wl_resource_destroy(panel->resource);
}
diff --git a/libswc/pointer.c b/libswc/pointer.c
@@ -37,16 +37,13 @@
static void enter(struct input_focus_handler * handler,
struct wl_resource * resource, struct compositor_view * view)
{
- struct pointer * pointer;
+ struct pointer * pointer = wl_container_of(handler, pointer, focus_handler);
uint32_t serial;
wl_fixed_t surface_x, surface_y;
- pointer = CONTAINER_OF(handler, typeof(*pointer), focus_handler);
serial = wl_display_next_serial(swc.display);
-
surface_x = pointer->x - wl_fixed_from_int(view->base.geometry.x);
surface_y = pointer->y - wl_fixed_from_int(view->base.geometry.y);
-
wl_pointer_send_enter(resource, serial, view->surface->resource,
surface_x, surface_y);
}
@@ -65,7 +62,7 @@ static void handle_cursor_surface_destroy(struct wl_listener * listener,
void * data)
{
struct pointer * pointer
- = CONTAINER_OF(listener, typeof(*pointer), cursor.destroy_listener);
+ = wl_container_of(listener, pointer, cursor.destroy_listener);
view_attach(&pointer->cursor.view, NULL);
pointer->cursor.surface = NULL;
@@ -78,8 +75,7 @@ static bool update(struct view * view)
static bool attach(struct view * view, struct wld_buffer * buffer)
{
- struct pointer * pointer
- = CONTAINER_OF(view, typeof(*pointer), cursor.view);
+ struct pointer * pointer = wl_container_of(view, pointer, cursor.view);
struct swc_surface * surface = pointer->cursor.surface;
if (surface && !pixman_region32_not_empty(&surface->state.damage))
@@ -160,7 +156,7 @@ static bool client_handle_button
struct press * press, uint32_t state)
{
struct pointer * pointer
- = CONTAINER_OF(handler, typeof(*pointer), client_handler);
+ = wl_container_of(handler, pointer, client_handler);
if (!pointer->focus.resource)
return false;
@@ -175,7 +171,7 @@ static bool client_handle_axis(struct pointer_handler * handler,
uint32_t time, uint32_t axis, wl_fixed_t amount)
{
struct pointer * pointer
- = CONTAINER_OF(handler, typeof(*pointer), client_handler);
+ = wl_container_of(handler, pointer, client_handler);
if (!pointer->focus.resource)
return false;
@@ -189,7 +185,7 @@ static bool client_handle_motion(struct pointer_handler * handler,
uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
struct pointer * pointer
- = CONTAINER_OF(handler, typeof(*pointer), client_handler);
+ = wl_container_of(handler, pointer, client_handler);
if (!pointer->focus.resource)
return false;
@@ -207,7 +203,7 @@ bool pointer_initialize(struct pointer * pointer)
struct screen * screen;
/* Center cursor in the geometry of the first screen. */
- screen = CONTAINER_OF(swc.screens.next, typeof(*screen), link);
+ screen = wl_container_of(swc.screens.next, screen, link);
pointer->x = wl_fixed_from_int
(screen->base.geometry.x + screen->base.geometry.width / 2);
pointer->y = wl_fixed_from_int
diff --git a/libswc/shell_surface.c b/libswc/shell_surface.c
@@ -114,7 +114,7 @@ static void set_fullscreen(struct wl_client * client,
struct screen * screen;
screen = output ? output->screen
- : CONTAINER_OF(swc.screens.next, typeof(*screen), link);
+ : wl_container_of(swc.screens.next, screen, link);
/* TODO: Handle fullscreen windows. */
@@ -188,7 +188,7 @@ static void configure(struct window * window,
const struct swc_rectangle * geometry)
{
struct shell_surface * shell_surface
- = CONTAINER_OF(window, typeof(*shell_surface), window);
+ = wl_container_of(window, shell_surface, window);
wl_shell_surface_send_configure(shell_surface->resource,
WL_SHELL_SURFACE_RESIZE_NONE,
@@ -201,8 +201,8 @@ static const struct window_impl shell_window_impl = {
static void handle_surface_destroy(struct wl_listener * listener, void * data)
{
- struct shell_surface * shell_surface = CONTAINER_OF
- (listener, typeof(*shell_surface), surface_destroy_listener);
+ struct shell_surface * shell_surface
+ = wl_container_of(listener, shell_surface, surface_destroy_listener);
wl_resource_destroy(shell_surface->resource);
}
diff --git a/libswc/shm.c b/libswc/shm.c
@@ -77,7 +77,7 @@ static void destroy_pool_resource(struct wl_resource * resource)
static void handle_buffer_destroy(struct wld_destructor * destructor)
{
struct pool_reference * reference
- = CONTAINER_OF(destructor, typeof(*reference), destructor);
+ = wl_container_of(destructor, reference, destructor);
unref_pool(reference->pool);
}
diff --git a/libswc/surface.c b/libswc/surface.c
@@ -42,7 +42,7 @@ static void handle_buffer_destroy(struct wl_listener * listener, void * data)
{
struct swc_surface_state * state;
- state = CONTAINER_OF(listener, typeof(*state), buffer_destroy_listener);
+ state = wl_container_of(listener, state, buffer_destroy_listener);
state->buffer = NULL;
}
@@ -299,7 +299,7 @@ static void surface_destroy(struct wl_resource * resource)
static void handle_view_event(struct wl_listener * listener, void * data)
{
struct swc_surface * surface
- = CONTAINER_OF(listener, typeof(*surface), view_listener);
+ = wl_container_of(listener, surface, view_listener);
struct swc_event * event = data;
struct view_event_data * event_data = event->data;
diff --git a/libswc/util.h b/libswc/util.h
@@ -58,17 +58,6 @@
# define DEBUG(format, ...)
#endif
-#ifdef offsetof
-# define OFFSET_OF offsetof
-#else
-# define OFFSET_OF __builtin_offsetof
-#endif
-
-#define CONTAINER_OF(ptr, type, member) ({ \
- const typeof(((type *) 0)->member) *__mptr = (ptr); \
- ((type *) ((uintptr_t) __mptr - OFFSET_OF(type, member))); \
-})
-
#define ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
struct wl_resource;
diff --git a/libswc/window.c b/libswc/window.c
@@ -187,7 +187,7 @@ static bool move_motion(struct pointer_handler * handler, uint32_t time,
wl_fixed_t fx, wl_fixed_t fy)
{
struct window * window
- = CONTAINER_OF(handler, typeof(*window), move.interaction.handler);
+ = wl_container_of(handler, window, move.interaction.handler);
view_move(&window->view->base, wl_fixed_to_int(fx) + window->move.offset.x,
wl_fixed_to_int(fy) + window->move.offset.y);
@@ -207,7 +207,7 @@ static bool handle_button(struct pointer_handler * handler, uint32_t time,
struct press * press, uint32_t state)
{
struct window_pointer_interaction * interaction
- = CONTAINER_OF(handler, typeof(*interaction), handler);
+ = wl_container_of(handler, interaction, handler);
if (state != WL_POINTER_BUTTON_STATE_RELEASED
|| !interaction->original_handler)
diff --git a/libswc/xserver.c b/libswc/xserver.c
@@ -58,7 +58,7 @@ struct swc_xserver swc_xserver;
static int open_socket(struct sockaddr_un * addr, size_t path_size)
{
int fd;
- socklen_t size = OFFSET_OF(typeof(*addr), sun_path) + path_size + 1;
+ socklen_t size = offsetof(typeof(*addr), sun_path) + path_size + 1;
if ((fd = socket(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0)
goto error0;
diff --git a/libswc/xwm.c b/libswc/xwm.c
@@ -128,7 +128,7 @@ static void configure(struct window * window,
{
uint32_t mask, values[4];
struct xwl_window * xwl_window
- = CONTAINER_OF(window, typeof(*xwl_window), window);
+ = wl_container_of(window, xwl_window, window);
mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
| XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
@@ -143,9 +143,9 @@ static void configure(struct window * window,
static void focus(struct window * window)
{
- xcb_window_t id = window ? CONTAINER_OF(window, struct xwl_window,
- window)->id
- : XCB_NONE;
+ struct xwl_window * xwl_window
+ = wl_container_of(window, xwl_window, window);
+ xcb_window_t id = window ? xwl_window->id : XCB_NONE;
xcb_set_input_focus(xwm.connection, XCB_INPUT_FOCUS_NONE,
id, XCB_CURRENT_TIME);
@@ -160,7 +160,7 @@ static const struct window_impl xwl_window_handler = {
static void handle_surface_destroy(struct wl_listener * listener, void * data)
{
struct xwl_window * xwl_window
- = CONTAINER_OF(listener, typeof(*xwl_window), surface_destroy_listener);
+ = wl_container_of(listener, xwl_window, surface_destroy_listener);
window_finalize(&xwl_window->window);
wl_list_remove(&xwl_window->link);