swc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/swc
Log | Files | Refs | README | LICENSE

commit 0cac6819df94dce46184db40e49d6e2b56d9237b
parent 7c9f1276daeefa0b95bf3eb63515051d1bcf65bd
Author: Michael Forney <mforney@mforney.org>
Date:   Mon, 18 May 2015 05:40:08 +0000

Add .clang-format and run clang-format

Diffstat:
A.clang-format | 24++++++++++++++++++++++++
Mcursor/convert_font.c | 157+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mexample/wm.c | 325++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlaunch/launch.c | 866+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlaunch/protocol.c | 158++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mlaunch/protocol.h | 71++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/bindings.c | 211++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/bindings.h | 8+++-----
Mlibswc/compositor.c | 1109+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/compositor.h | 75++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/cursor_plane.c | 125+++++++++++++++++++++++++++++++++++++------------------------------------------
Mlibswc/cursor_plane.h | 18++++++++----------
Mlibswc/data.c | 191+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mlibswc/data.h | 15+++++++--------
Mlibswc/data_device.c | 148+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mlibswc/data_device.h | 31++++++++++++++-----------------
Mlibswc/data_device_manager.c | 70++++++++++++++++++++++++++++++++++++----------------------------------
Mlibswc/data_device_manager.h | 1-
Mlibswc/drm.c | 608+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/drm.h | 17+++++++----------
Mlibswc/evdev_device.c | 462+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/evdev_device.h | 84++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mlibswc/event.h | 21++++++++++-----------
Mlibswc/framebuffer_plane.c | 324++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mlibswc/framebuffer_plane.h | 28+++++++++++++---------------
Mlibswc/input.c | 161++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/input.h | 68+++++++++++++++++++++++++++++++-------------------------------------
Mlibswc/internal.h | 37+++++++++++++++++--------------------
Mlibswc/keyboard.c | 470+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/keyboard.h | 71+++++++++++++++++++++++++++++++++--------------------------------------
Mlibswc/launch.c | 153++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/launch.h | 3+--
Mlibswc/mode.c | 28+++++++++++++++-------------
Mlibswc/mode.h | 18++++++++----------
Mlibswc/output.c | 172+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/output.h | 30++++++++++++++----------------
Mlibswc/panel.c | 365++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/panel.h | 5++---
Mlibswc/panel_manager.c | 59+++++++++++++++++++++++++++++++----------------------------
Mlibswc/panel_manager.h | 1-
Mlibswc/pointer.c | 590+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/pointer.h | 102++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mlibswc/region.c | 76++++++++++++++++++++++++++++++++++++++++------------------------------------
Mlibswc/region.h | 12+++++-------
Mlibswc/screen.c | 322+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/screen.h | 58++++++++++++++++++++++++++++------------------------------
Mlibswc/seat.c | 704+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/seat.h | 12+++++-------
Mlibswc/shell.c | 55+++++++++++++++++++++++++++++--------------------------
Mlibswc/shell.h | 1-
Mlibswc/shell_surface.c | 302++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/shell_surface.h | 7+++----
Mlibswc/shm.c | 324++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/shm.h | 8+++-----
Mlibswc/subcompositor.c | 66++++++++++++++++++++++++++++++++++--------------------------------
Mlibswc/subcompositor.h | 1-
Mlibswc/subsurface.c | 97++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mlibswc/subsurface.h | 10++++------
Mlibswc/surface.c | 504+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/surface.h | 66+++++++++++++++++++++++++++++++-----------------------------------
Mlibswc/swc.c | 326++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mlibswc/swc.h | 182+++++++++++++++++++++++++++++++++++++------------------------------------------
Mlibswc/util.c | 9++++-----
Mlibswc/util.h | 71+++++++++++++++++++++++++++++++++++++----------------------------------
Mlibswc/view.c | 167+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mlibswc/view.h | 79+++++++++++++++++++++++++++++++++++++------------------------------------------
Mlibswc/wayland_buffer.c | 54+++++++++++++++++++++++++++---------------------------
Mlibswc/wayland_buffer.h | 8+++-----
Mlibswc/window.c | 641+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mlibswc/window.h | 127+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/xdg_popup.c | 103++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/xdg_popup.h | 11+++++------
Mlibswc/xdg_shell.c | 154+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mlibswc/xdg_shell.h | 1-
Mlibswc/xdg_surface.c | 415++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mlibswc/xdg_surface.h | 7+++----
Mlibswc/xkb.c | 212++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mlibswc/xkb.h | 42++++++++++++++++++++----------------------
Mlibswc/xserver.c | 477+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/xserver.h | 6++----
Mlibswc/xwm.c | 832+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mlibswc/xwm.h | 1-
82 files changed, 6952 insertions(+), 7078 deletions(-)

diff --git a/.clang-format b/.clang-format @@ -0,0 +1,24 @@ +# clang-format isn't authoritative for the style decisions in this project, but +# these rules mostly align with the suggested style, and clang-format can be +# used fix any potential style-mistakes. +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: true +BinPackArguments: true +BinPackParameters: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Linux +ColumnLimit: 0 +Cpp11BracedListStyle: false +ForEachMacros: [wl_list_for_each, wl_array_for_each, wl_list_for_each_safe, wl_list_for_each_reverse, wl_resource_for_each] +IndentWidth: 8 +MaxEmptyLinesToKeep: 1 +PointerAlignment: Right +SpaceAfterCStyleCast: false +TabWidth: 8 +UseTab: ForIndentation +... +# vim: ft=yaml diff --git a/cursor/convert_font.c b/cursor/convert_font.c @@ -53,24 +53,24 @@ struct glyph { static struct { int count; struct glyph *glyphs; -} extracted_font = {0, NULL}; +} extracted_font = { 0, NULL }; -#define PCF_PROPERTIES (1<<0) -#define PCF_ACCELERATORS (1<<1) -#define PCF_METRICS (1<<2) -#define PCF_BITMAPS (1<<3) -#define PCF_INK_METRICS (1<<4) -#define PCF_BDF_ENCODINGS (1<<5) -#define PCF_SWIDTHS (1<<6) -#define PCF_GLYPH_NAMES (1<<7) -#define PCF_BDF_ACCELERATORS (1<<8) +#define PCF_PROPERTIES (1 << 0) +#define PCF_ACCELERATORS (1 << 1) +#define PCF_METRICS (1 << 2) +#define PCF_BITMAPS (1 << 3) +#define PCF_INK_METRICS (1 << 4) +#define PCF_BDF_ENCODINGS (1 << 5) +#define PCF_SWIDTHS (1 << 6) +#define PCF_GLYPH_NAMES (1 << 7) +#define PCF_BDF_ACCELERATORS (1 << 8) -#define PCF_DEFAULT_FORMAT 0x00000000 -#define PCF_INKBOUNDS 0x00000200 -#define PCF_ACCEL_W_INKBOUNDS 0x00000100 -#define PCF_COMPRESSED_METRICS 0x00000100 +#define PCF_DEFAULT_FORMAT 0x00000000 +#define PCF_INKBOUNDS 0x00000200 +#define PCF_ACCEL_W_INKBOUNDS 0x00000100 +#define PCF_COMPRESSED_METRICS 0x00000100 -#define PCF_FORMAT_MASK 0xffffff00 +#define PCF_FORMAT_MASK 0xffffff00 struct pcf_header { char header[4]; @@ -137,12 +137,12 @@ handle_compressed_metrics(int32_t count, struct compressed_metrics *m) for (i = 0; i < count; ++i) { struct glyph *glyph = &extracted_font.glyphs[i]; glyph->left_bearing = - ((int16_t) m[i].left_sided_bearing) - 0x80; + ((int16_t)m[i].left_sided_bearing) - 0x80; glyph->right_bearing = - ((int16_t) m[i].right_side_bearing) - 0x80; - glyph->width = ((int16_t) m[i].character_width) - 0x80; - glyph->ascent = ((int16_t) m[i].character_ascent) - 0x80; - glyph->descent = ((int16_t) m[i].character_descent) - 0x80; + ((int16_t)m[i].right_side_bearing) - 0x80; + glyph->width = ((int16_t)m[i].character_width) - 0x80; + glyph->ascent = ((int16_t)m[i].character_ascent) - 0x80; + glyph->descent = ((int16_t)m[i].character_descent) - 0x80; /* computed stuff */ glyph->height = glyph->ascent + glyph->descent; @@ -160,8 +160,7 @@ handle_metrics(void *metricbuf) if ((metrics->format & PCF_FORMAT_MASK) == PCF_DEFAULT_FORMAT) { fprintf(stderr, "todo...\n"); - } else if ((metrics->format & PCF_FORMAT_MASK) == - PCF_COMPRESSED_METRICS) { + } else if ((metrics->format & PCF_FORMAT_MASK) == PCF_COMPRESSED_METRICS) { handle_compressed_metrics( metrics->compressed.count, &metrics->compressed.compressed_metrics[0]); @@ -182,13 +181,13 @@ handle_glyph_names(struct glyph_names *names) fprintf(stderr, "glyph names format %x\n", names->format); - char *names_start = ((char *) names) + sizeof(struct glyph_names) - + (names->glyph_count + 1) * sizeof(int32_t); + char *names_start = ((char *)names) + sizeof(struct glyph_names) + + (names->glyph_count + 1) * sizeof(int32_t); int i; for (i = 0; i < names->glyph_count; ++i) { int32_t start = names->offsets[i]; - int32_t end = names->offsets[i+1]; + int32_t end = names->offsets[i + 1]; char *name = names_start + start; extracted_font.glyphs[i].name = calloc(1, end - start + 1); memcpy(extracted_font.glyphs[i].name, name, end - start); @@ -211,8 +210,8 @@ handle_bitmaps(struct bitmaps *bitmaps) abort(); } - char *bitmaps_start = ((char*) bitmaps) + sizeof(struct bitmaps) - + (bitmaps->glyph_count + 4) * sizeof(int32_t); + char *bitmaps_start = ((char *)bitmaps) + sizeof(struct bitmaps) + + (bitmaps->glyph_count + 4) * sizeof(int32_t); for (unsigned i = 0; i < bitmaps->glyph_count; ++i) { int32_t offset = bitmaps->offsets[i]; @@ -233,11 +232,11 @@ handle_pcf(void *fontbuf) struct toc_entry *entry = &header->tables[i]; fprintf(stderr, "type: %d\n", entry->type); if (entry->type == PCF_METRICS) { - handle_metrics((void *)((uintptr_t) fontbuf + entry->offset)); + handle_metrics((void *)((uintptr_t)fontbuf + entry->offset)); } else if (entry->type == PCF_GLYPH_NAMES) { - handle_glyph_names((void *)((uintptr_t) fontbuf + entry->offset)); + handle_glyph_names((void *)((uintptr_t)fontbuf + entry->offset)); } else if (entry->type == PCF_BITMAPS) { - handle_bitmaps((void *)((uintptr_t) fontbuf + entry->offset)); + handle_bitmaps((void *)((uintptr_t)fontbuf + entry->offset)); } } } @@ -248,12 +247,11 @@ get_glyph_pixel(struct glyph *glyph, int x, int y) int absx = glyph->hotx + x; int absy = glyph->hoty + y; - if (absx < 0 || absx >= glyph->width || - absy < 0 || absy >= glyph->height) + if (absx < 0 || absx >= glyph->width || absy < 0 || absy >= glyph->height) return 0; int stride = (glyph->width + 31) / 32 * 4; - unsigned char block = glyph->data[absy * stride + (absx/8)]; + unsigned char block = glyph->data[absy * stride + (absx / 8)]; int idx = absx % 8; return (block >> idx) & 1; } @@ -277,8 +275,8 @@ add_pixel(uint32_t pixel) if (data_buffer.size == data_buffer.capacity) { data_buffer.capacity *= 2; data_buffer.data = - realloc(data_buffer.data, - sizeof(uint32_t) * data_buffer.capacity); + realloc(data_buffer.data, + sizeof(uint32_t) * data_buffer.capacity); } data_buffer.data[data_buffer.size++] = pixel; } @@ -292,14 +290,14 @@ struct reconstructed_glyph { static void reconstruct_glyph(struct glyph *cursor, struct glyph *mask, char *name, - struct reconstructed_glyph *glyph) + struct reconstructed_glyph *glyph) { int minx = min(-cursor->hotx, -mask->hotx); int maxx = max(cursor->right_bearing, mask->right_bearing); int miny = min(-cursor->hoty, -mask->hoty); int maxy = max(cursor->height - cursor->hoty, - mask->height - mask->hoty); + mask->height - mask->hoty); int width = maxx - minx; int height = maxy - miny; @@ -330,28 +328,28 @@ reconstruct_glyph(struct glyph *cursor, struct glyph *mask, char *name, /* From http://cgit.freedesktop.org/xorg/lib/libXfont/tree/src/builtins/fonts.c */ static const char cursor_licence[] = - "/*\n" - "* Copyright 1999 SuSE, Inc.\n" - "*\n" - "* Permission to use, copy, modify, distribute, and sell this software and its\n" - "* documentation for any purpose is hereby granted without fee, provided that\n" - "* the above copyright notice appear in all copies and that both that\n" - "* copyright notice and this permission notice appear in supporting\n" - "* documentation, and that the name of SuSE not be used in advertising or\n" - "* publicity pertaining to distribution of the software without specific,\n" - "* written prior permission. SuSE makes no representations about the\n" - "* suitability of this software for any purpose. It is provided \"as is\"\n" - "* without express or implied warranty.\n" - "*\n" - "* SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL\n" - "* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE\n" - "* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n" - "* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION\n" - "* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN\n" - "* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n" - "*\n" - "* Author: Keith Packard, SuSE, Inc.\n" - "*/\n"; + "/*\n" + "* Copyright 1999 SuSE, Inc.\n" + "*\n" + "* Permission to use, copy, modify, distribute, and sell this software and its\n" + "* documentation for any purpose is hereby granted without fee, provided that\n" + "* the above copyright notice appear in all copies and that both that\n" + "* copyright notice and this permission notice appear in supporting\n" + "* documentation, and that the name of SuSE not be used in advertising or\n" + "* publicity pertaining to distribution of the software without specific,\n" + "* written prior permission. SuSE makes no representations about the\n" + "* suitability of this software for any purpose. It is provided \"as is\"\n" + "* without express or implied warranty.\n" + "*\n" + "* SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL\n" + "* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE\n" + "* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\n" + "* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION\n" + "* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN\n" + "* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n" + "*\n" + "* Author: Keith Packard, SuSE, Inc.\n" + "*/\n"; static void write_output_file(FILE *file, struct reconstructed_glyph *glyphs, int n) @@ -384,17 +382,17 @@ write_output_file(FILE *file, struct reconstructed_glyph *glyphs, int n) fputs("};\n\n", file); fprintf(file, - "static struct cursor {\n" - "\tint width, height;\n" - "\tint hotspot_x, hotspot_y;\n" - "\tsize_t offset;\n" - "} cursor_metadata[] = {\n"); + "static struct cursor {\n" + "\tint width, height;\n" + "\tint hotspot_x, hotspot_y;\n" + "\tsize_t offset;\n" + "} cursor_metadata[] = {\n"); for (i = 0; i < n; ++i) fprintf(file, "\t{ %d, %d, %d, %d, %zu }, /* %s */\n", - glyphs[i].width, glyphs[i].height, - glyphs[i].hotspot_x, glyphs[i].hotspot_y, - glyphs[i].offset, glyphs[i].name); + glyphs[i].width, glyphs[i].height, + glyphs[i].hotspot_x, glyphs[i].hotspot_y, + glyphs[i].offset, glyphs[i].name); fputs("};\n", file); } @@ -410,9 +408,7 @@ find_mask_glyph(char *name) for (i = 0; i < extracted_font.count; ++i) { struct glyph *g = &extracted_font.glyphs[i]; int l2 = strlen(g->name); - if ((l2 == len + masklen) && - (memcmp(g->name, name, len) == 0) && - (memcmp(g->name + len, mask, masklen) == 0)) { + if ((l2 == len + masklen) && (memcmp(g->name, name, len) == 0) && (memcmp(g->name + len, mask, masklen) == 0)) { return g; } } @@ -421,8 +417,8 @@ find_mask_glyph(char *name) static void find_cursor_and_mask(const char *name, - struct glyph **cursor, - struct glyph **mask) + struct glyph **cursor, + struct glyph **mask) { int i; char mask_name[100]; @@ -463,31 +459,32 @@ output_interesting_cursors(FILE *file) int i; int n = sizeof(interesting_cursors) / sizeof(interesting_cursors[0]); struct reconstructed_glyph *glyphs = - malloc(n * sizeof(*glyphs)); + malloc(n * sizeof(*glyphs)); for (i = 0; i < n; ++i) { struct glyph *cursor, *mask; find_cursor_and_mask(interesting_cursors[i].source_name, - &cursor, &mask); + &cursor, &mask); if (!cursor) { fprintf(stderr, "no cursor for %s\n", - interesting_cursors[i].source_name); + interesting_cursors[i].source_name); abort(); } if (!mask) { fprintf(stderr, "no mask for %s\n", - interesting_cursors[i].source_name); + interesting_cursors[i].source_name); abort(); } reconstruct_glyph(cursor, mask, - interesting_cursors[i].target_name, - &glyphs[i]); + interesting_cursors[i].target_name, + &glyphs[i]); } write_output_file(file, glyphs, n); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s input.pcf output.h\n", argv[0]); @@ -500,7 +497,7 @@ int main(int argc, char *argv[]) fstat(fd, &filestat); void *fontbuf = mmap(NULL, filestat.st_size, PROT_READ, - MAP_PRIVATE, fd, 0); + MAP_PRIVATE, fd, 0); handle_pcf(fontbuf); diff --git a/example/wm.c b/example/wm.c @@ -27,246 +27,249 @@ #include <wayland-server.h> #include <xkbcommon/xkbcommon.h> -struct screen -{ - struct swc_screen * swc; - struct wl_list windows; - unsigned num_windows; +struct screen { + struct swc_screen *swc; + struct wl_list windows; + unsigned num_windows; }; -struct window -{ - struct swc_window * swc; - struct screen * screen; - struct wl_list link; +struct window { + struct swc_window *swc; + struct screen *screen; + struct wl_list link; }; -static const char * terminal_command[] = { "st-wl", NULL }; -static const char * dmenu_command[] = { "dmenu_run-wl", NULL }; +static const char *terminal_command[] = { "st-wl", NULL }; +static const char *dmenu_command[] = { "dmenu_run-wl", NULL }; static const uint32_t border_width = 1; static const uint32_t border_color_active = 0xff333388; static const uint32_t border_color_normal = 0xff888888; -static struct screen * active_screen; -static struct window * focused_window; -static struct wl_display * display; -static struct wl_event_loop * event_loop; +static struct screen *active_screen; +static struct window *focused_window; +static struct wl_display *display; +static struct wl_event_loop *event_loop; /* This is a basic grid arrange function that tries to give each window an * equal space. */ -static void arrange(struct screen * screen) +static void +arrange(struct screen *screen) { - struct window * window = NULL; - unsigned num_columns, num_rows, column_index, row_index; - struct swc_rectangle geometry; - struct swc_rectangle * screen_geometry = &screen->swc->usable_geometry; - - if (screen->num_windows == 0) return; - - num_columns = ceil(sqrt(screen->num_windows)); - num_rows = screen->num_windows / num_columns + 1; - window = wl_container_of(screen->windows.next, window, link); - - for (column_index = 0; &window->link != &screen->windows; ++column_index) - { - geometry.x = screen_geometry->x + border_width - + screen_geometry->width * column_index / num_columns; - geometry.width = screen_geometry->width / num_columns - - 2 * border_width; - - if (column_index == screen->num_windows % num_columns) - --num_rows; - - for (row_index = 0; row_index < num_rows; ++row_index) - { - geometry.y = screen_geometry->y + border_width - + screen_geometry->height * row_index / num_rows; - geometry.height = screen_geometry->height / num_rows - - 2 * border_width; - - swc_window_set_geometry(window->swc, &geometry); - window = wl_container_of(window->link.next, window, link); - } - } + struct window *window = NULL; + unsigned num_columns, num_rows, column_index, row_index; + struct swc_rectangle geometry; + struct swc_rectangle *screen_geometry = &screen->swc->usable_geometry; + + if (screen->num_windows == 0) + return; + + num_columns = ceil(sqrt(screen->num_windows)); + num_rows = screen->num_windows / num_columns + 1; + window = wl_container_of(screen->windows.next, window, link); + + for (column_index = 0; &window->link != &screen->windows; ++column_index) { + geometry.x = screen_geometry->x + border_width + + screen_geometry->width * column_index / num_columns; + geometry.width = screen_geometry->width / num_columns + - 2 * border_width; + + if (column_index == screen->num_windows % num_columns) + --num_rows; + + for (row_index = 0; row_index < num_rows; ++row_index) { + geometry.y = screen_geometry->y + border_width + + screen_geometry->height * row_index / num_rows; + geometry.height = screen_geometry->height / num_rows + - 2 * border_width; + + swc_window_set_geometry(window->swc, &geometry); + window = wl_container_of(window->link.next, window, link); + } + } } -static void screen_add_window(struct screen * screen, struct window * window) +static void +screen_add_window(struct screen *screen, struct window *window) { - window->screen = screen; - wl_list_insert(&screen->windows, &window->link); - ++screen->num_windows; - swc_window_show(window->swc); - arrange(screen); + window->screen = screen; + wl_list_insert(&screen->windows, &window->link); + ++screen->num_windows; + swc_window_show(window->swc); + arrange(screen); } -static void screen_remove_window(struct screen * screen, struct window * window) +static void +screen_remove_window(struct screen *screen, struct window *window) { - window->screen = NULL; - wl_list_remove(&window->link); - --screen->num_windows; - swc_window_hide(window->swc); - arrange(screen); + window->screen = NULL; + wl_list_remove(&window->link); + --screen->num_windows; + swc_window_hide(window->swc); + arrange(screen); } -static void focus(struct window * window) +static void +focus(struct window *window) { - if (focused_window) - { - swc_window_set_border(focused_window->swc, - border_color_normal, border_width); - } - - if (window) - { - swc_window_set_border(window->swc, border_color_active, border_width); - swc_window_focus(window->swc); - } - else - swc_window_focus(NULL); - - focused_window = window; + if (focused_window) { + swc_window_set_border(focused_window->swc, + border_color_normal, border_width); + } + + if (window) { + swc_window_set_border(window->swc, border_color_active, border_width); + swc_window_focus(window->swc); + } else + swc_window_focus(NULL); + + focused_window = window; } -static void screen_usable_geometry_changed(void * data) +static void +screen_usable_geometry_changed(void *data) { - struct screen * screen = data; + struct screen *screen = data; - /* If the usable geometry of the screen changes, for example when a panel is + /* If the usable geometry of the screen changes, for example when a panel is * docked to the edge of the screen, we need to rearrange the windows to * ensure they are all within the new usable geometry. */ - arrange(screen); + arrange(screen); } -static void screen_entered(void * data) +static void +screen_entered(void *data) { - struct screen * screen = data; + struct screen *screen = data; - active_screen = screen; + active_screen = screen; } static const struct swc_screen_handler screen_handler = { - .usable_geometry_changed = &screen_usable_geometry_changed, - .entered = &screen_entered, + .usable_geometry_changed = &screen_usable_geometry_changed, + .entered = &screen_entered, }; -static void window_destroy(void * data) +static void +window_destroy(void *data) { - struct window * window = data, * next_focus; + struct window *window = data, *next_focus; - if (focused_window == window) - { - /* Try to find a new focus nearby the old one. */ - next_focus = wl_container_of(window->link.next, window, link); + if (focused_window == window) { + /* Try to find a new focus nearby the old one. */ + next_focus = wl_container_of(window->link.next, window, link); - if (&next_focus->link == &window->screen->windows) - { - next_focus = wl_container_of(window->link.prev, - window, link); + if (&next_focus->link == &window->screen->windows) { + next_focus = wl_container_of(window->link.prev, + window, link); - if (&next_focus->link == &window->screen->windows) - next_focus = NULL; - } + if (&next_focus->link == &window->screen->windows) + next_focus = NULL; + } - focus(next_focus); - } + focus(next_focus); + } - screen_remove_window(window->screen, window); - free(window); + screen_remove_window(window->screen, window); + free(window); } -static void window_entered(void * data) +static void +window_entered(void *data) { - struct window * window = data; + struct window *window = data; - focus(window); + focus(window); } static const struct swc_window_handler window_handler = { - .destroy = &window_destroy, - .entered = &window_entered, + .destroy = &window_destroy, + .entered = &window_entered, }; -static void new_screen(struct swc_screen * swc) +static void +new_screen(struct swc_screen *swc) { - struct screen * screen; + struct screen *screen; - screen = malloc(sizeof *screen); + screen = malloc(sizeof *screen); - if (!screen) - return; + if (!screen) + return; - screen->swc = swc; - screen->num_windows = 0; - wl_list_init(&screen->windows); - swc_screen_set_handler(swc, &screen_handler, screen); - active_screen = screen; + screen->swc = swc; + screen->num_windows = 0; + wl_list_init(&screen->windows); + swc_screen_set_handler(swc, &screen_handler, screen); + active_screen = screen; } -static void new_window(struct swc_window * swc) +static void +new_window(struct swc_window *swc) { - struct window * window; + struct window *window; - window = malloc(sizeof *window); + window = malloc(sizeof *window); - if (!window) - return; + if (!window) + return; - window->swc = swc; - window->screen = NULL; - swc_window_set_handler(swc, &window_handler, window); - swc_window_set_tiled(swc); - screen_add_window(active_screen, window); - focus(window); + window->swc = swc; + window->screen = NULL; + swc_window_set_handler(swc, &window_handler, window); + swc_window_set_tiled(swc); + screen_add_window(active_screen, window); + focus(window); } const struct swc_manager manager = { &new_screen, &new_window }; -static void spawn(void * data, uint32_t time, uint32_t value, uint32_t state) +static void +spawn(void *data, uint32_t time, uint32_t value, uint32_t state) { - char * const * command = data; + char *const *command = data; - if (state != WL_KEYBOARD_KEY_STATE_PRESSED) - return; + if (state != WL_KEYBOARD_KEY_STATE_PRESSED) + return; - if (fork() == 0) - { - execvp(command[0], command); - exit(EXIT_FAILURE); - } + if (fork() == 0) { + execvp(command[0], command); + exit(EXIT_FAILURE); + } } -static void quit(void * data, uint32_t time, uint32_t value, uint32_t state) +static void +quit(void *data, uint32_t time, uint32_t value, uint32_t state) { - if (state != WL_KEYBOARD_KEY_STATE_PRESSED) - return; + if (state != WL_KEYBOARD_KEY_STATE_PRESSED) + return; - wl_display_terminate(display); + wl_display_terminate(display); } -int main(int argc, char * argv[]) +int +main(int argc, char *argv[]) { - display = wl_display_create(); + display = wl_display_create(); - if (!display) - return EXIT_FAILURE; + if (!display) + return EXIT_FAILURE; - if (wl_display_add_socket(display, NULL) != 0) - return EXIT_FAILURE; + if (wl_display_add_socket(display, NULL) != 0) + return EXIT_FAILURE; - if (!swc_initialize(display, NULL, &manager)) - return EXIT_FAILURE; + if (!swc_initialize(display, NULL, &manager)) + return EXIT_FAILURE; - swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_Return, - &spawn, terminal_command); - swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_r, - &spawn, dmenu_command); - swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_q, - &quit, NULL); + swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_Return, + &spawn, terminal_command); + swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_r, + &spawn, dmenu_command); + swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_q, + &quit, NULL); - event_loop = wl_display_get_event_loop(display); - wl_display_run(display); - wl_display_destroy(display); + event_loop = wl_display_get_event_loop(display); + wl_display_run(display); + wl_display_destroy(display); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } - diff --git a/launch/launch.c b/launch/launch.c @@ -45,528 +45,514 @@ #include <xf86drm.h> #ifndef DRM_MAJOR -# define DRM_MAJOR 226 +#define DRM_MAJOR 226 #endif #ifndef EVIOCREVOKE -# define EVIOCREVOKE _IOW('E', 0x91, int) +#define EVIOCREVOKE _IOW('E', 0x91, int) #endif -#define ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0]) +#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) pid_t child_pid = -1; static struct -{ - int socket; - int input_fds[128]; - unsigned num_input_fds; - int drm_fds[16]; - unsigned num_drm_fds; - int tty_fd; - bool active; + { + int socket; + int input_fds[128]; + unsigned num_input_fds; + int drm_fds[16]; + unsigned num_drm_fds; + int tty_fd; + bool active; } launcher; static struct -{ - bool altered; - int vt; - long kb_mode; - long console_mode; + { + bool altered; + int vt; + long kb_mode; + long console_mode; } original_vt_state; static bool nflag; -static void __attribute__((noreturn,format(printf,1,2))) - die(const char * format, ...); +static void __attribute__((noreturn, format(printf, 1, 2))) +die(const char *format, ...); -static void __attribute__((noreturn)) usage(const char * name) +static void __attribute__((noreturn)) usage(const char *name) { - fprintf(stderr, "Usage: %s [-n] [-s <server-socket>] [-t <tty-device>] " - "[--] <server> [server arguments...]\n", name); - exit(2); + fprintf(stderr, "Usage: %s [-n] [-s <server-socket>] [-t <tty-device>] " + "[--] <server> [server arguments...]\n", + name); + exit(2); } -static void start_devices(void) +static void +start_devices(void) { - unsigned index; + unsigned index; - for (index = 0; index < launcher.num_drm_fds; ++index) - { - if (drmSetMaster(launcher.drm_fds[index]) < 0) - die("Failed to set DRM master"); - } + for (index = 0; index < launcher.num_drm_fds; ++index) { + if (drmSetMaster(launcher.drm_fds[index]) < 0) + die("Failed to set DRM master"); + } } -static void stop_devices(bool fatal) +static void +stop_devices(bool fatal) { - unsigned index; + unsigned index; - for (index = 0; index < launcher.num_drm_fds; ++index) - { - if (drmDropMaster(launcher.drm_fds[index]) < 0 && fatal) - die("Failed to drop DRM master"); - } + for (index = 0; index < launcher.num_drm_fds; ++index) { + if (drmDropMaster(launcher.drm_fds[index]) < 0 && fatal) + die("Failed to drop DRM master"); + } - for (index = 0; index < launcher.num_input_fds; ++index) - { - if (ioctl(launcher.input_fds[index], EVIOCREVOKE, 0) == -1 - && errno != ENODEV && fatal) - { - die("FATAL: Your kernel does not support EVIOCREVOKE; " - "input devices cannot be revoked: %s", strerror(errno)); - } + for (index = 0; index < launcher.num_input_fds; ++index) { + if (ioctl(launcher.input_fds[index], EVIOCREVOKE, 0) == -1 + && errno != ENODEV && fatal) { + die("FATAL: Your kernel does not support EVIOCREVOKE; " + "input devices cannot be revoked: %s", + strerror(errno)); + } - close(launcher.input_fds[index]); - } + close(launcher.input_fds[index]); + } - launcher.num_input_fds = 0; + launcher.num_input_fds = 0; } -static void cleanup(void) +static void +cleanup(void) { - struct vt_mode mode = { .mode = VT_AUTO }; + struct vt_mode mode = {.mode = VT_AUTO }; - if (!original_vt_state.altered) - return; + if (!original_vt_state.altered) + return; - /* Cleanup VT */ - fprintf(stderr, "Restoring VT to original state\n"); - ioctl(launcher.tty_fd, VT_SETMODE, &mode); - ioctl(launcher.tty_fd, KDSETMODE, original_vt_state.console_mode); - ioctl(launcher.tty_fd, KDSKBMODE, original_vt_state.kb_mode); + /* Cleanup VT */ + fprintf(stderr, "Restoring VT to original state\n"); + ioctl(launcher.tty_fd, VT_SETMODE, &mode); + ioctl(launcher.tty_fd, KDSETMODE, original_vt_state.console_mode); + ioctl(launcher.tty_fd, KDSKBMODE, original_vt_state.kb_mode); - /* Stop devices before switching the VT to make sure we have released the + /* Stop devices before switching the VT to make sure we have released the * DRM device before the next session tries to claim it. */ - stop_devices(false); - ioctl(launcher.tty_fd, VT_ACTIVATE, original_vt_state.vt); + stop_devices(false); + ioctl(launcher.tty_fd, VT_ACTIVATE, original_vt_state.vt); - kill(child_pid, SIGTERM); + kill(child_pid, SIGTERM); } -void __attribute__((noreturn,format(printf,1,2))) - die(const char * format, ...) +void __attribute__((noreturn, format(printf, 1, 2))) +die(const char *format, ...) { - va_list args; + va_list args; - fputs("FATAL: ", stderr); - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); + fputs("FATAL: ", stderr); + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); - if (errno != 0) - fprintf(stderr, ": %s", strerror(errno)); + if (errno != 0) + fprintf(stderr, ": %s", strerror(errno)); - fputc('\n', stderr); + fputc('\n', stderr); - if (child_pid) - cleanup(); + if (child_pid) + cleanup(); - exit(EXIT_FAILURE); + exit(EXIT_FAILURE); } -static void activate(void) +static void +activate(void) { - struct swc_launch_event event = { .type = SWC_LAUNCH_EVENT_ACTIVATE }; + struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_ACTIVATE }; - start_devices(); - send(launcher.socket, &event, sizeof event, 0); - launcher.active = true; + start_devices(); + send(launcher.socket, &event, sizeof event, 0); + launcher.active = true; } -static void deactivate(void) +static void +deactivate(void) { - struct swc_launch_event event = { .type = SWC_LAUNCH_EVENT_DEACTIVATE }; + struct swc_launch_event event = {.type = SWC_LAUNCH_EVENT_DEACTIVATE }; - send(launcher.socket, &event, sizeof event, 0); - stop_devices(true); - launcher.active = false; + send(launcher.socket, &event, sizeof event, 0); + stop_devices(true); + launcher.active = false; } -static void handle_chld(int signal) +static void +handle_chld(int signal) { - int status; + int status; - wait(&status); - fprintf(stderr, "Server exited with status %d\n", WEXITSTATUS(status)); - cleanup(); - exit(WEXITSTATUS(status)); + wait(&status); + fprintf(stderr, "Server exited with status %d\n", WEXITSTATUS(status)); + cleanup(); + exit(WEXITSTATUS(status)); } -static void handle_usr1(int signal) +static void +handle_usr1(int signal) { - deactivate(); - ioctl(launcher.tty_fd, VT_RELDISP, 1); + deactivate(); + ioctl(launcher.tty_fd, VT_RELDISP, 1); } -static void handle_usr2(int signal) +static void +handle_usr2(int signal) { - ioctl(launcher.tty_fd, VT_RELDISP, VT_ACKACQ); - activate(); + ioctl(launcher.tty_fd, VT_RELDISP, VT_ACKACQ); + activate(); } -static void forward_signal(int signal) +static void +forward_signal(int signal) { - kill(child_pid, signal); + kill(child_pid, signal); } -static void handle_socket_data(int socket) +static void +handle_socket_data(int socket) { - char buffer[BUFSIZ]; - struct swc_launch_request * request = (void *) &buffer; - struct swc_launch_event response; - int fd = -1; - struct stat st; - ssize_t size; - - size = receive_fd(socket, &fd, buffer, sizeof buffer); - - if (size == -1 || size == 0) - return; - - response.type = SWC_LAUNCH_EVENT_RESPONSE; - response.serial = request->serial; - - switch (request->type) - { - case SWC_LAUNCH_REQUEST_OPEN_DEVICE: - if (request->path[size - __builtin_offsetof(typeof(*request), - path) - 1] != '\0') - { - fprintf(stderr, "Path is not NULL terminated\n"); - goto fail; - } - - if (stat(request->path, &st) == -1) - { - fprintf(stderr, "Could not stat %s\n", request->path); - goto fail; - } - - switch (major(st.st_rdev)) - { - case INPUT_MAJOR: - if (!launcher.active) - goto fail; - - if (launcher.num_input_fds - == ARRAY_LENGTH(launcher.input_fds)) - { - fprintf(stderr, "Too many input devices opened\n"); - goto fail; - } - break; - case DRM_MAJOR: - if (launcher.num_drm_fds - == ARRAY_LENGTH(launcher.drm_fds)) - { - fprintf(stderr, "Too many DRM devices opened\n"); - goto fail; - } - break; - default: - fprintf(stderr, "Device is not an input device\n"); - goto fail; - } - - fd = open(request->path, request->flags); - - if (fd == -1) - { - fprintf(stderr, "Could not open device %s\n", request->path); - goto fail; - } - - switch (major(st.st_rdev)) - { - case INPUT_MAJOR: - launcher.input_fds[launcher.num_input_fds++] = fd; - break; - case DRM_MAJOR: - launcher.drm_fds[launcher.num_drm_fds++] = fd; - break; - } - - break; - case SWC_LAUNCH_REQUEST_ACTIVATE_VT: - if (!launcher.active) - goto fail; - - if (ioctl(launcher.tty_fd, VT_ACTIVATE, request->vt) == -1) - { - fprintf(stderr, "Could not activate VT %d: %s\n", - request->vt, strerror(errno)); - } - break; - default: - fprintf(stderr, "Unknown request %u\n", request->type); - goto fail; - } - - response.success = true; - goto done; - - fail: - response.success = false; - fd = -1; - done: - send_fd(socket, fd, &response, sizeof response); + char buffer[BUFSIZ]; + struct swc_launch_request *request = (void *)&buffer; + struct swc_launch_event response; + int fd = -1; + struct stat st; + ssize_t size; + + size = receive_fd(socket, &fd, buffer, sizeof buffer); + + if (size == -1 || size == 0) + return; + + response.type = SWC_LAUNCH_EVENT_RESPONSE; + response.serial = request->serial; + + switch (request->type) { + case SWC_LAUNCH_REQUEST_OPEN_DEVICE: + if (request->path[size - __builtin_offsetof(typeof(*request), + path) - 1] != '\0') { + fprintf(stderr, "Path is not NULL terminated\n"); + goto fail; + } + + if (stat(request->path, &st) == -1) { + fprintf(stderr, "Could not stat %s\n", request->path); + goto fail; + } + + switch (major(st.st_rdev)) { + case INPUT_MAJOR: + if (!launcher.active) + goto fail; + + if (launcher.num_input_fds + == ARRAY_LENGTH(launcher.input_fds)) { + fprintf(stderr, "Too many input devices opened\n"); + goto fail; + } + break; + case DRM_MAJOR: + if (launcher.num_drm_fds + == ARRAY_LENGTH(launcher.drm_fds)) { + fprintf(stderr, "Too many DRM devices opened\n"); + goto fail; + } + break; + default: + fprintf(stderr, "Device is not an input device\n"); + goto fail; + } + + fd = open(request->path, request->flags); + + if (fd == -1) { + fprintf(stderr, "Could not open device %s\n", request->path); + goto fail; + } + + switch (major(st.st_rdev)) { + case INPUT_MAJOR: + launcher.input_fds[launcher.num_input_fds++] = fd; + break; + case DRM_MAJOR: + launcher.drm_fds[launcher.num_drm_fds++] = fd; + break; + } + + break; + case SWC_LAUNCH_REQUEST_ACTIVATE_VT: + if (!launcher.active) + goto fail; + + if (ioctl(launcher.tty_fd, VT_ACTIVATE, request->vt) == -1) { + fprintf(stderr, "Could not activate VT %d: %s\n", + request->vt, strerror(errno)); + } + break; + default: + fprintf(stderr, "Unknown request %u\n", request->type); + goto fail; + } + + response.success = true; + goto done; + +fail: + response.success = false; + fd = -1; +done: + send_fd(socket, fd, &response, sizeof response); } -static void find_vt(char * vt, size_t size) +static void +find_vt(char *vt, size_t size) { - char * vt_num_string; - - if ((vt_num_string = getenv("XDG_VTNR"))) - { - if (snprintf(vt, size, "/dev/tty%s", vt_num_string) >= size) - die("XDG_VTNR is too long"); - } - else - { - int tty0_fd, vt_num; - - tty0_fd = open("/dev/tty0", O_RDWR); - if (tty0_fd == -1) - die("Could not open /dev/tty0 to find unused VT"); - if (ioctl(tty0_fd, VT_OPENQRY, &vt_num) != 0) - die("Could not find unused VT"); - close(tty0_fd); - if (snprintf(vt, size, "/dev/tty%d", vt_num) >= size) - die("VT number is too large"); - } + char *vt_num_string; + + if ((vt_num_string = getenv("XDG_VTNR"))) { + if (snprintf(vt, size, "/dev/tty%s", vt_num_string) >= size) + die("XDG_VTNR is too long"); + } else { + int tty0_fd, vt_num; + + tty0_fd = open("/dev/tty0", O_RDWR); + if (tty0_fd == -1) + die("Could not open /dev/tty0 to find unused VT"); + if (ioctl(tty0_fd, VT_OPENQRY, &vt_num) != 0) + die("Could not find unused VT"); + close(tty0_fd); + if (snprintf(vt, size, "/dev/tty%d", vt_num) >= size) + die("VT number is too large"); + } } -static int open_tty(const char * tty_name) +static int +open_tty(const char *tty_name) { - char * stdin_tty; + char *stdin_tty; - /* Check if we are running on the desired VT */ - if ((stdin_tty = ttyname(STDIN_FILENO)) && strcmp(tty_name, stdin_tty) == 0) - return STDIN_FILENO; - else - { - int fd; + /* Check if we are running on the desired VT */ + if ((stdin_tty = ttyname(STDIN_FILENO)) && strcmp(tty_name, stdin_tty) == 0) + return STDIN_FILENO; + else { + int fd; - /* Open the new TTY. */ - fd = open(tty_name, O_RDWR | O_NOCTTY); + /* Open the new TTY. */ + fd = open(tty_name, O_RDWR | O_NOCTTY); - if (fd < 0) - die("Could not open %s", tty_name); + if (fd < 0) + die("Could not open %s", tty_name); - return fd; - } + return fd; + } } -static void setup_tty(int fd) +static void +setup_tty(int fd) { - struct stat st; - int vt; - struct vt_stat state; - struct vt_mode mode = { - .mode = VT_PROCESS, - .relsig = SIGUSR1, - .acqsig = SIGUSR2 - }; - - if (fstat(fd, &st) == -1) - die("Could not stat TTY fd"); - - vt = minor(st.st_rdev); - - if (major(st.st_rdev) != TTY_MAJOR || vt == 0) - die("Not a valid VT"); - - if (ioctl(fd, VT_GETSTATE, &state) == -1) - die("Could not get the current VT state"); - - original_vt_state.vt = state.v_active; - - if (ioctl(fd, KDGKBMODE, &original_vt_state.kb_mode)) - die("Could not get keyboard mode"); - - if (ioctl(fd, KDGETMODE, &original_vt_state.console_mode)) - die("Could not get console mode"); - - if (ioctl(fd, KDSKBMODE, K_OFF) == -1) - die("Could not set keyboard mode to K_OFF"); - - if (ioctl(fd, KDSETMODE, KD_GRAPHICS) == -1) - { - perror("Could not set console mode to KD_GRAPHICS"); - goto error0; - } - - if (ioctl(fd, VT_SETMODE, &mode) == -1) - { - perror("Could not set VT mode"); - goto error1; - } - - if (vt == original_vt_state.vt) - activate(); - else if (!nflag) - { - if (ioctl(fd, VT_ACTIVATE, vt) == -1) - { - perror("Could not activate VT"); - goto error2; - } - - if (ioctl(fd, VT_WAITACTIVE, vt) == -1) - { - perror("Could not wait for VT to become active"); - goto error2; - } - } - - original_vt_state.altered = true; - - return; - - error2: - mode = (struct vt_mode) { .mode = VT_AUTO }; - ioctl(fd, VT_SETMODE, &mode); - error1: - ioctl(fd, KDSETMODE, original_vt_state.console_mode); - error0: - ioctl(fd, KDSKBMODE, original_vt_state.kb_mode); - exit(EXIT_FAILURE); + struct stat st; + int vt; + struct vt_stat state; + struct vt_mode mode = { + .mode = VT_PROCESS, + .relsig = SIGUSR1, + .acqsig = SIGUSR2 + }; + + if (fstat(fd, &st) == -1) + die("Could not stat TTY fd"); + + vt = minor(st.st_rdev); + + if (major(st.st_rdev) != TTY_MAJOR || vt == 0) + die("Not a valid VT"); + + if (ioctl(fd, VT_GETSTATE, &state) == -1) + die("Could not get the current VT state"); + + original_vt_state.vt = state.v_active; + + if (ioctl(fd, KDGKBMODE, &original_vt_state.kb_mode)) + die("Could not get keyboard mode"); + + if (ioctl(fd, KDGETMODE, &original_vt_state.console_mode)) + die("Could not get console mode"); + + if (ioctl(fd, KDSKBMODE, K_OFF) == -1) + die("Could not set keyboard mode to K_OFF"); + + if (ioctl(fd, KDSETMODE, KD_GRAPHICS) == -1) { + perror("Could not set console mode to KD_GRAPHICS"); + goto error0; + } + + if (ioctl(fd, VT_SETMODE, &mode) == -1) { + perror("Could not set VT mode"); + goto error1; + } + + if (vt == original_vt_state.vt) + activate(); + else if (!nflag) { + if (ioctl(fd, VT_ACTIVATE, vt) == -1) { + perror("Could not activate VT"); + goto error2; + } + + if (ioctl(fd, VT_WAITACTIVE, vt) == -1) { + perror("Could not wait for VT to become active"); + goto error2; + } + } + + original_vt_state.altered = true; + + return; + +error2: + mode = (struct vt_mode){.mode = VT_AUTO }; + ioctl(fd, VT_SETMODE, &mode); +error1: + ioctl(fd, KDSETMODE, original_vt_state.console_mode); +error0: + ioctl(fd, KDSKBMODE, original_vt_state.kb_mode); + exit(EXIT_FAILURE); } -int main(int argc, char * argv[]) +int +main(int argc, char *argv[]) { - int option; - int sockets[2]; - char * vt = NULL, vt_buf[64]; - struct sigaction action = { 0 }; - sigset_t set; - - while ((option = getopt(argc, argv, "ns:t:")) != -1) - { - switch (option) - { - case 'n': - nflag = true; - break; - case 's': - setenv("WAYLAND_DISPLAY", optarg, true); - break; - case 't': - vt = optarg; - break; - default: - usage(argv[0]); - } - } - - if (argc - optind < 1) - usage(argv[0]); - - if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, sockets) == -1) - die("Could not create socket pair"); - - launcher.socket = sockets[0]; - - if (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) == -1) - die("Could not set CLOEXEC on socket"); - - action.sa_handler = &handle_chld; - if (sigaction(SIGCHLD, &action, NULL) == -1) - die("Failed to register signal handler for SIGCHLD"); - - action.sa_handler = &handle_usr1; - if (sigaction(SIGUSR1, &action, NULL) == -1) - die("Failed to register signal handler for SIGUSR1"); - - action.sa_handler = &handle_usr2; - if (sigaction(SIGUSR2, &action, NULL) == -1) - die("Failed to register signal handler for SIGUSR2"); - - action.sa_handler = &forward_signal; - if (sigaction(SIGINT, &action, NULL) == -1) - die("Failed to register signal handler for SIGINT"); - if (sigaction(SIGTERM, &action, NULL) == -1) - die("Failed to register signal handler for SIGTERM"); - - sigfillset(&set); - sigdelset(&set, SIGCHLD); - sigdelset(&set, SIGUSR1); - sigdelset(&set, SIGUSR2); - sigdelset(&set, SIGINT); - sigdelset(&set, SIGTERM); - sigprocmask(SIG_SETMASK, &set, NULL); - - if (!vt) - { - find_vt(vt_buf, sizeof vt_buf); - vt = vt_buf; - } - - fprintf(stderr, "Running on %s\n", vt); - launcher.tty_fd = open_tty(vt); - setup_tty(launcher.tty_fd); - - child_pid = fork(); - - /* Child */ - if (child_pid == 0) - { - char string[64]; - - /* Reset signal handlers to defaults */ - action.sa_handler = SIG_DFL; - if (sigaction(SIGCHLD, &action, NULL) == -1) - die("Failed to set default signal handler for SIGCHLD"); - if (sigaction(SIGUSR1, &action, NULL) == -1) - die("Failed to set default signal handler for SIGUSR1"); - if (sigaction(SIGUSR2, &action, NULL) == -1) - die("Failed to set default signal handler for SIGUSR2"); - if (sigaction(SIGINT, &action, NULL) == -1) - die("Failed to set default signal handler for SIGINT"); - if (sigaction(SIGTERM, &action, NULL) == -1) - die("Failed to set default signal handler for SIGTERM"); - - /* Set empty signal mask */ - sigemptyset(&set); - sigprocmask(SIG_SETMASK, &set, NULL); - - sprintf(string, "%d", sockets[1]); - setenv(SWC_LAUNCH_SOCKET_ENV, string, 1); - - sprintf(string, "%d", launcher.tty_fd); - setenv(SWC_LAUNCH_TTY_FD_ENV, string, 1); - - if (setuid(getuid()) != 0 || setgid(getgid()) != 0) - die("Failed to drop permission before executing display server"); - - execvp(argv[optind], argv + optind); - die("Could not exec %s", argv[optind]); - } - /* Parent */ - else - { - struct pollfd pollfd; - int ret; - - pollfd.fd = sockets[0]; - pollfd.events = POLLIN; - - while (true) - { - ret = poll(&pollfd, 1, -1); - - if (ret == -1) - { - if (errno == EINTR) - continue; - else - die("Error while polling on socket fd"); - } - - handle_socket_data(pollfd.fd); - } - } - - return EXIT_SUCCESS; + int option; + int sockets[2]; + char *vt = NULL, vt_buf[64]; + struct sigaction action = { 0 }; + sigset_t set; + + while ((option = getopt(argc, argv, "ns:t:")) != -1) { + switch (option) { + case 'n': + nflag = true; + break; + case 's': + setenv("WAYLAND_DISPLAY", optarg, true); + break; + case 't': + vt = optarg; + break; + default: + usage(argv[0]); + } + } + + if (argc - optind < 1) + usage(argv[0]); + + if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, sockets) == -1) + die("Could not create socket pair"); + + launcher.socket = sockets[0]; + + if (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) == -1) + die("Could not set CLOEXEC on socket"); + + action.sa_handler = &handle_chld; + if (sigaction(SIGCHLD, &action, NULL) == -1) + die("Failed to register signal handler for SIGCHLD"); + + action.sa_handler = &handle_usr1; + if (sigaction(SIGUSR1, &action, NULL) == -1) + die("Failed to register signal handler for SIGUSR1"); + + action.sa_handler = &handle_usr2; + if (sigaction(SIGUSR2, &action, NULL) == -1) + die("Failed to register signal handler for SIGUSR2"); + + action.sa_handler = &forward_signal; + if (sigaction(SIGINT, &action, NULL) == -1) + die("Failed to register signal handler for SIGINT"); + if (sigaction(SIGTERM, &action, NULL) == -1) + die("Failed to register signal handler for SIGTERM"); + + sigfillset(&set); + sigdelset(&set, SIGCHLD); + sigdelset(&set, SIGUSR1); + sigdelset(&set, SIGUSR2); + sigdelset(&set, SIGINT); + sigdelset(&set, SIGTERM); + sigprocmask(SIG_SETMASK, &set, NULL); + + if (!vt) { + find_vt(vt_buf, sizeof vt_buf); + vt = vt_buf; + } + + fprintf(stderr, "Running on %s\n", vt); + launcher.tty_fd = open_tty(vt); + setup_tty(launcher.tty_fd); + + child_pid = fork(); + + /* Child */ + if (child_pid == 0) { + char string[64]; + + /* Reset signal handlers to defaults */ + action.sa_handler = SIG_DFL; + if (sigaction(SIGCHLD, &action, NULL) == -1) + die("Failed to set default signal handler for SIGCHLD"); + if (sigaction(SIGUSR1, &action, NULL) == -1) + die("Failed to set default signal handler for SIGUSR1"); + if (sigaction(SIGUSR2, &action, NULL) == -1) + die("Failed to set default signal handler for SIGUSR2"); + if (sigaction(SIGINT, &action, NULL) == -1) + die("Failed to set default signal handler for SIGINT"); + if (sigaction(SIGTERM, &action, NULL) == -1) + die("Failed to set default signal handler for SIGTERM"); + + /* Set empty signal mask */ + sigemptyset(&set); + sigprocmask(SIG_SETMASK, &set, NULL); + + sprintf(string, "%d", sockets[1]); + setenv(SWC_LAUNCH_SOCKET_ENV, string, 1); + + sprintf(string, "%d", launcher.tty_fd); + setenv(SWC_LAUNCH_TTY_FD_ENV, string, 1); + + if (setuid(getuid()) != 0 || setgid(getgid()) != 0) + die("Failed to drop permission before executing display server"); + + execvp(argv[optind], argv + optind); + die("Could not exec %s", argv[optind]); + } + /* Parent */ + else { + struct pollfd pollfd; + int ret; + + pollfd.fd = sockets[0]; + pollfd.events = POLLIN; + + while (true) { + ret = poll(&pollfd, 1, -1); + + if (ret == -1) { + if (errno == EINTR) + continue; + else + die("Error while polling on socket fd"); + } + + handle_socket_data(pollfd.fd); + } + } + + return EXIT_SUCCESS; } - diff --git a/launch/protocol.c b/launch/protocol.c @@ -4,90 +4,84 @@ #include <stdio.h> #include <string.h> -ssize_t send_fd(int socket, int fd, const void * buffer, ssize_t buffer_size) +ssize_t +send_fd(int socket, int fd, const void *buffer, ssize_t buffer_size) { - char control[CMSG_SPACE(sizeof(int))]; - struct iovec iov = { - .iov_base = (void *) buffer, - .iov_len = buffer_size - }; - struct msghdr message = { - .msg_name = NULL, - .msg_namelen = 0, - .msg_iov = &iov, - .msg_iovlen = 1, - }; - struct cmsghdr * cmsg; - - if (fd != -1) - { - message.msg_control = control, - message.msg_controllen = sizeof control; - - cmsg = CMSG_FIRSTHDR(&message); - cmsg->cmsg_len = CMSG_LEN(sizeof fd); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - - memcpy(CMSG_DATA(cmsg), &fd, sizeof fd); - } - else - { - message.msg_control = NULL; - message.msg_controllen = 0; - } - - return sendmsg(socket, &message, 0); + char control[CMSG_SPACE(sizeof(int))]; + struct iovec iov = { + .iov_base = (void *)buffer, + .iov_len = buffer_size + }; + struct msghdr message = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = &iov, + .msg_iovlen = 1, + }; + struct cmsghdr *cmsg; + + if (fd != -1) { + message.msg_control = control, + message.msg_controllen = sizeof control; + + cmsg = CMSG_FIRSTHDR(&message); + cmsg->cmsg_len = CMSG_LEN(sizeof fd); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + + memcpy(CMSG_DATA(cmsg), &fd, sizeof fd); + } else { + message.msg_control = NULL; + message.msg_controllen = 0; + } + + return sendmsg(socket, &message, 0); } -ssize_t receive_fd(int socket, int * fd, void * buffer, - ssize_t buffer_size) +ssize_t +receive_fd(int socket, int *fd, void *buffer, + ssize_t buffer_size) { - ssize_t size; - - if (fd) - { - char control[CMSG_SPACE(sizeof(int))]; - struct iovec iov = { - .iov_base = buffer, - .iov_len = buffer_size - }; - struct msghdr message = { - .msg_name = NULL, - .msg_namelen = 0, - .msg_iov = &iov, - .msg_iovlen = 1, - .msg_control = &control, - .msg_controllen = sizeof control - }; - struct cmsghdr * cmsg; - - size = recvmsg(socket, &message, 0); - - if (size < 0) - goto nofd; - - cmsg = CMSG_FIRSTHDR(&message); - - if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(int)) - || cmsg->cmsg_level != SOL_SOCKET - || cmsg->cmsg_type != SCM_RIGHTS) - { - goto nofd; - } - - memcpy(fd, CMSG_DATA(cmsg), sizeof *fd); - } - else - { - size = recv(socket, buffer, buffer_size, 0); - } - - goto done; - - nofd: - *fd = -1; - done: - return size; + ssize_t size; + + if (fd) { + char control[CMSG_SPACE(sizeof(int))]; + struct iovec iov = { + .iov_base = buffer, + .iov_len = buffer_size + }; + struct msghdr message = { + .msg_name = NULL, + .msg_namelen = 0, + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = &control, + .msg_controllen = sizeof control + }; + struct cmsghdr *cmsg; + + size = recvmsg(socket, &message, 0); + + if (size < 0) + goto nofd; + + cmsg = CMSG_FIRSTHDR(&message); + + if (!cmsg || cmsg->cmsg_len != CMSG_LEN(sizeof(int)) + || cmsg->cmsg_level != SOL_SOCKET + || cmsg->cmsg_type != SCM_RIGHTS) { + goto nofd; + } + + memcpy(fd, CMSG_DATA(cmsg), sizeof *fd); + } else { + size = recv(socket, buffer, buffer_size, 0); + } + + goto done; + +nofd: + *fd = -1; +done: + return size; } - diff --git a/launch/protocol.h b/launch/protocol.h @@ -31,53 +31,46 @@ #define SWC_LAUNCH_SOCKET_ENV "SWC_LAUNCH_SOCKET" #define SWC_LAUNCH_TTY_FD_ENV "SWC_LAUNCH_TTY_FD" -struct swc_launch_request -{ - enum - { - SWC_LAUNCH_REQUEST_OPEN_DEVICE, - SWC_LAUNCH_REQUEST_ACTIVATE_VT, - } type; +struct swc_launch_request { + enum { + SWC_LAUNCH_REQUEST_OPEN_DEVICE, + SWC_LAUNCH_REQUEST_ACTIVATE_VT, + } type; - uint32_t serial; + uint32_t serial; - union - { - struct /* OPEN_DEVICE */ - { - int flags; - char path[]; - }; - struct /* ACTIVATE_VT */ - { - unsigned vt; - }; - }; + union { + struct /* OPEN_DEVICE */ + { + int flags; + char path[]; + }; + struct /* ACTIVATE_VT */ + { + unsigned vt; + }; + }; }; -struct swc_launch_event -{ - enum - { - SWC_LAUNCH_EVENT_RESPONSE, - SWC_LAUNCH_EVENT_ACTIVATE, - SWC_LAUNCH_EVENT_DEACTIVATE, - } type; +struct swc_launch_event { + enum { + SWC_LAUNCH_EVENT_RESPONSE, + SWC_LAUNCH_EVENT_ACTIVATE, + SWC_LAUNCH_EVENT_DEACTIVATE, + } type; - union - { - struct /* RESPONSE */ - { - uint32_t serial; - bool success; - }; - }; + union { + struct /* RESPONSE */ + { + uint32_t serial; + bool success; + }; + }; }; -ssize_t send_fd(int socket, int fd, const void * buffer, ssize_t buffer_size); +ssize_t send_fd(int socket, int fd, const void *buffer, ssize_t buffer_size); -ssize_t receive_fd(int socket, int * fd, void * buffer, +ssize_t receive_fd(int socket, int *fd, void *buffer, ssize_t buffer_size); #endif - diff --git a/libswc/bindings.c b/libswc/bindings.c @@ -32,163 +32,164 @@ #include <errno.h> #include <wayland-util.h> -struct binding -{ - uint32_t value; - uint32_t modifiers; - swc_binding_handler handler; - void * data; +struct binding { + uint32_t value; + uint32_t modifiers; + swc_binding_handler handler; + void *data; }; -static bool handle_key(struct keyboard * keyboard, uint32_t time, - struct key * key, uint32_t state); +static bool handle_key(struct keyboard *keyboard, uint32_t time, + struct key *key, uint32_t state); static struct keyboard_handler key_binding_handler = { - .key = &handle_key + .key = &handle_key }; -static bool handle_button(struct pointer_handler * handler, uint32_t time, - struct button * button, uint32_t state); +static bool handle_button(struct pointer_handler *handler, uint32_t time, + struct button *button, uint32_t state); static struct pointer_handler button_binding_handler = { - .button = &handle_button + .button = &handle_button }; static struct wl_array key_bindings, button_bindings; const struct swc_bindings swc_bindings = { - .keyboard_handler = &key_binding_handler, - .pointer_handler = &button_binding_handler + .keyboard_handler = &key_binding_handler, + .pointer_handler = &button_binding_handler }; -static struct binding * find_binding(struct wl_array * bindings, - uint32_t modifiers, uint32_t value) +static struct binding * +find_binding(struct wl_array *bindings, + uint32_t modifiers, uint32_t value) { - struct binding * binding; - - wl_array_for_each(binding, bindings) - { - if (binding->value == value && (binding->modifiers == modifiers - || binding->modifiers == SWC_MOD_ANY)) - { - return binding; - } - } - - return NULL; + struct binding *binding; + + wl_array_for_each (binding, bindings) { + if (binding->value == value && (binding->modifiers == modifiers + || binding->modifiers == SWC_MOD_ANY)) { + return binding; + } + } + + return NULL; } -static struct binding * find_key_binding(uint32_t modifiers, uint32_t key) +static struct binding * +find_key_binding(uint32_t modifiers, uint32_t key) { - struct binding * binding; - struct xkb * xkb = &swc.seat->keyboard->xkb; - xkb_keysym_t keysym; + struct binding *binding; + struct xkb *xkb = &swc.seat->keyboard->xkb; + xkb_keysym_t keysym; - /* First try the keysym the keymap generates in it's current state. */ - keysym = xkb_state_key_get_one_sym(xkb->state, XKB_KEY(key)); - binding = find_binding(&key_bindings, modifiers, keysym); + /* First try the keysym the keymap generates in it's current state. */ + keysym = xkb_state_key_get_one_sym(xkb->state, XKB_KEY(key)); + binding = find_binding(&key_bindings, modifiers, keysym); - if (binding) - return binding; + if (binding) + return binding; - xkb_layout_index_t layout; - const xkb_keysym_t * keysyms; + xkb_layout_index_t layout; + const xkb_keysym_t *keysyms; - /* Then try the keysym associated with shift-level 0 for the key. */ - layout = xkb_state_key_get_layout(xkb->state, XKB_KEY(key)); - xkb_keymap_key_get_syms_by_level(xkb->keymap.map, XKB_KEY(key), - layout, 0, &keysyms); + /* Then try the keysym associated with shift-level 0 for the key. */ + layout = xkb_state_key_get_layout(xkb->state, XKB_KEY(key)); + xkb_keymap_key_get_syms_by_level(xkb->keymap.map, XKB_KEY(key), + layout, 0, &keysyms); - if (!keysyms) - return NULL; + if (!keysyms) + return NULL; - binding = find_binding(&key_bindings, modifiers, keysyms[0]); + binding = find_binding(&key_bindings, modifiers, keysyms[0]); - return binding; + return binding; } -static struct binding * find_button_binding(uint32_t modifiers, uint32_t value) +static struct binding * +find_button_binding(uint32_t modifiers, uint32_t value) { - return find_binding(&button_bindings, modifiers, value); + return find_binding(&button_bindings, modifiers, value); } -static bool handle_binding - (uint32_t time, struct press * press, uint32_t state, - struct binding * (* find_binding)(uint32_t, uint32_t)) +static bool +handle_binding(uint32_t time, struct press *press, uint32_t state, + struct binding *(*find_binding)(uint32_t, uint32_t)) { - struct binding * binding; + struct binding *binding; - if (state) - { - binding = find_binding(swc.seat->keyboard->modifiers, press->value); + if (state) { + binding = find_binding(swc.seat->keyboard->modifiers, press->value); - if (!binding) - return false; + if (!binding) + return false; - press->data = binding; - } - else - binding = press->data; + press->data = binding; + } else + binding = press->data; - binding->handler(binding->data, time, binding->value, state); + binding->handler(binding->data, time, binding->value, state); - return true; + return true; } -bool handle_key(struct keyboard * keyboard, uint32_t time, - struct key * key, uint32_t state) +bool +handle_key(struct keyboard *keyboard, uint32_t time, + struct key *key, uint32_t state) { - return handle_binding(time, &key->press, state, &find_key_binding); + return handle_binding(time, &key->press, state, &find_key_binding); } -bool handle_button(struct pointer_handler * handler, uint32_t time, - struct button * button, uint32_t state) +bool +handle_button(struct pointer_handler *handler, uint32_t time, + struct button *button, uint32_t state) { - return handle_binding(time, &button->press, state, &find_button_binding); + return handle_binding(time, &button->press, state, &find_button_binding); } -bool bindings_initialize(void) +bool +bindings_initialize(void) { - wl_array_init(&key_bindings); - wl_array_init(&button_bindings); + wl_array_init(&key_bindings); + wl_array_init(&button_bindings); - return true; + return true; } -void bindings_finalize(void) +void +bindings_finalize(void) { - wl_array_release(&key_bindings); - wl_array_release(&button_bindings); + wl_array_release(&key_bindings); + wl_array_release(&button_bindings); } EXPORT -int swc_add_binding(enum swc_binding_type type, - uint32_t modifiers, uint32_t value, - swc_binding_handler handler, void * data) +int +swc_add_binding(enum swc_binding_type type, + uint32_t modifiers, uint32_t value, + swc_binding_handler handler, void *data) { - struct binding * binding; - struct wl_array * bindings; - - switch (type) - { - case SWC_BINDING_KEY: - bindings = &key_bindings; - break; - case SWC_BINDING_BUTTON: - bindings = &button_bindings; - break; - default: - return -EINVAL; - } - - if (!(binding = wl_array_add(bindings, sizeof *binding))) - return -ENOMEM; - - binding->value = value; - binding->modifiers = modifiers; - binding->handler = handler; - binding->data = data; - - return 0; + struct binding *binding; + struct wl_array *bindings; + + switch (type) { + case SWC_BINDING_KEY: + bindings = &key_bindings; + break; + case SWC_BINDING_BUTTON: + bindings = &button_bindings; + break; + default: + return -EINVAL; + } + + if (!(binding = wl_array_add(bindings, sizeof *binding))) + return -ENOMEM; + + binding->value = value; + binding->modifiers = modifiers; + binding->handler = handler; + binding->data = data; + + return 0; } - diff --git a/libswc/bindings.h b/libswc/bindings.h @@ -26,14 +26,12 @@ #include <stdbool.h> -struct swc_bindings -{ - struct keyboard_handler * keyboard_handler; - struct pointer_handler * pointer_handler; +struct swc_bindings { + struct keyboard_handler *keyboard_handler; + struct pointer_handler *pointer_handler; }; bool bindings_initialize(void); void bindings_finalize(void); #endif - diff --git a/libswc/compositor.c b/libswc/compositor.c @@ -52,274 +52,267 @@ #include <wld/drm.h> #include <xkbcommon/xkbcommon-keysyms.h> -struct target -{ - struct wld_surface * surface; - struct wld_buffer * next_buffer, * current_buffer; - struct view * view; - struct view_handler view_handler; - uint32_t mask; - - struct wl_listener screen_destroy_listener; +struct target { + struct wld_surface *surface; + struct wld_buffer *next_buffer, *current_buffer; + struct view *view; + struct view_handler view_handler; + uint32_t mask; + + struct wl_listener screen_destroy_listener; }; -static bool handle_motion(struct pointer_handler * handler, uint32_t time, +static bool handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t x, wl_fixed_t y); -static void perform_update(void * data); +static void perform_update(void *data); static struct pointer_handler pointer_handler = { - .motion = &handle_motion + .motion = &handle_motion }; static struct -{ - struct wl_list views; - pixman_region32_t damage, opaque; - struct wl_listener swc_listener; + { + struct wl_list views; + pixman_region32_t damage, opaque; + struct wl_listener swc_listener; - /* A mask of screens that have been repainted but are waiting on a page + /* A mask of screens that have been repainted but are waiting on a page * flip. */ - uint32_t pending_flips; + uint32_t pending_flips; - /* A mask of screens that are scheduled to be repainted on the next + /* A mask of screens that are scheduled to be repainted on the next * idle. */ - uint32_t scheduled_updates; + uint32_t scheduled_updates; - bool updating; - struct wl_global * global; + bool updating; + struct wl_global *global; } compositor; struct swc_compositor swc_compositor = { - .pointer_handler = &pointer_handler + .pointer_handler = &pointer_handler }; -static void handle_screen_destroy(struct wl_listener * listener, void * data) +static void +handle_screen_destroy(struct wl_listener *listener, void *data) { - struct target * target - = wl_container_of(listener, target, screen_destroy_listener); + struct target *target = wl_container_of(listener, target, screen_destroy_listener); - wld_destroy_surface(target->surface); - free(target); + wld_destroy_surface(target->surface); + free(target); } -static struct target * target_get(struct screen * screen) +static struct target * +target_get(struct screen *screen) { - struct wl_listener * listener - = wl_signal_get(&screen->destroy_signal, &handle_screen_destroy); - struct target * target; + struct wl_listener *listener = wl_signal_get(&screen->destroy_signal, &handle_screen_destroy); + struct target *target; - return listener ? wl_container_of(listener, target, screen_destroy_listener) - : NULL; + return listener ? wl_container_of(listener, target, screen_destroy_listener) + : NULL; } -static void handle_screen_frame(struct view_handler * handler, uint32_t time) +static void +handle_screen_frame(struct view_handler *handler, uint32_t time) { - struct target * target = wl_container_of(handler, target, view_handler); - struct compositor_view * view; + struct target *target = wl_container_of(handler, target, view_handler); + struct compositor_view *view; - compositor.pending_flips &= ~target->mask; + compositor.pending_flips &= ~target->mask; - wl_list_for_each(view, &compositor.views, link) - { - if (view->visible && view->base.screens & target->mask) - view_frame(&view->base, time); - } + wl_list_for_each (view, &compositor.views, link) { + if (view->visible && view->base.screens & target->mask) + view_frame(&view->base, time); + } - if (target->current_buffer) - wld_surface_release(target->surface, target->current_buffer); + if (target->current_buffer) + wld_surface_release(target->surface, target->current_buffer); - target->current_buffer = target->next_buffer; + target->current_buffer = target->next_buffer; - /* If we had scheduled updates that couldn't run because we were + /* If we had scheduled updates that couldn't run because we were * waiting on a page flip, run them now. If the compositor is * currently updating, then the frame finished immediately, and we * can be sure that there are no pending updates. */ - if (compositor.scheduled_updates && !compositor.updating) - perform_update(NULL); + if (compositor.scheduled_updates && !compositor.updating) + perform_update(NULL); } static const struct view_handler_impl screen_view_handler = { - .frame = &handle_screen_frame, + .frame = &handle_screen_frame, }; -static int target_swap_buffers(struct target * target) +static int +target_swap_buffers(struct target *target) { - target->next_buffer = wld_surface_take(target->surface); - return view_attach(target->view, target->next_buffer); + target->next_buffer = wld_surface_take(target->surface); + return view_attach(target->view, target->next_buffer); } -static struct target * target_new(struct screen * screen) +static struct target * +target_new(struct screen *screen) { - struct target * target; + struct target *target; - if (!(target = malloc(sizeof *target))) - goto error0; + if (!(target = malloc(sizeof *target))) + goto error0; - target->surface = wld_create_surface(swc.drm->context, - screen->base.geometry.width, - screen->base.geometry.height, - WLD_FORMAT_XRGB8888, - WLD_DRM_FLAG_SCANOUT); + target->surface = wld_create_surface(swc.drm->context, + screen->base.geometry.width, + screen->base.geometry.height, + WLD_FORMAT_XRGB8888, + WLD_DRM_FLAG_SCANOUT); - if (!target->surface) - goto error1; + if (!target->surface) + goto error1; - target->view = &screen->planes.framebuffer.view; - target->view_handler.impl = &screen_view_handler; - wl_list_insert(&target->view->handlers, &target->view_handler.link); - target->current_buffer = NULL; - target->mask = screen_mask(screen); + target->view = &screen->planes.framebuffer.view; + target->view_handler.impl = &screen_view_handler; + wl_list_insert(&target->view->handlers, &target->view_handler.link); + target->current_buffer = NULL; + target->mask = screen_mask(screen); - target->screen_destroy_listener.notify = &handle_screen_destroy; - wl_signal_add(&screen->destroy_signal, &target->screen_destroy_listener); + target->screen_destroy_listener.notify = &handle_screen_destroy; + wl_signal_add(&screen->destroy_signal, &target->screen_destroy_listener); - return target; + return target; - error1: - free(target); - error0: - return NULL; +error1: + free(target); +error0: + return NULL; } /* Rendering {{{ */ -static void repaint_view(struct target * target, struct compositor_view * view, - pixman_region32_t * damage) +static void +repaint_view(struct target *target, struct compositor_view *view, + pixman_region32_t *damage) { - pixman_region32_t view_region, view_damage, border_damage; - const struct swc_rectangle * geometry = &view->base.geometry; + pixman_region32_t view_region, view_damage, border_damage; + const struct swc_rectangle *geometry = &view->base.geometry; - if (!view->base.buffer) - return; + if (!view->base.buffer) + return; - pixman_region32_init_rect(&view_region, geometry->x, geometry->y, - geometry->width, geometry->height); - pixman_region32_init_with_extents(&view_damage, &view->extents); - pixman_region32_init(&border_damage); + pixman_region32_init_rect(&view_region, geometry->x, geometry->y, + geometry->width, geometry->height); + pixman_region32_init_with_extents(&view_damage, &view->extents); + pixman_region32_init(&border_damage); - pixman_region32_intersect(&view_damage, &view_damage, damage); - pixman_region32_subtract(&view_damage, &view_damage, &view->clip); - pixman_region32_subtract(&border_damage, &view_damage, &view_region); - pixman_region32_intersect(&view_damage, &view_damage, &view_region); + pixman_region32_intersect(&view_damage, &view_damage, damage); + pixman_region32_subtract(&view_damage, &view_damage, &view->clip); + pixman_region32_subtract(&border_damage, &view_damage, &view_region); + pixman_region32_intersect(&view_damage, &view_damage, &view_region); - pixman_region32_fini(&view_region); + pixman_region32_fini(&view_region); - if (pixman_region32_not_empty(&view_damage)) - { - pixman_region32_translate(&view_damage, -geometry->x, -geometry->y); - wld_copy_region(swc.drm->renderer, view->buffer, - geometry->x - target->view->geometry.x, - geometry->y - target->view->geometry.y, &view_damage); - } + if (pixman_region32_not_empty(&view_damage)) { + pixman_region32_translate(&view_damage, -geometry->x, -geometry->y); + wld_copy_region(swc.drm->renderer, view->buffer, + geometry->x - target->view->geometry.x, + geometry->y - target->view->geometry.y, &view_damage); + } - pixman_region32_fini(&view_damage); + pixman_region32_fini(&view_damage); - /* Draw border */ - if (pixman_region32_not_empty(&border_damage)) - { - pixman_region32_translate(&border_damage, - -target->view->geometry.x, - -target->view->geometry.y); - wld_fill_region(swc.drm->renderer, view->border.color, &border_damage); - } + /* Draw border */ + if (pixman_region32_not_empty(&border_damage)) { + pixman_region32_translate(&border_damage, + -target->view->geometry.x, + -target->view->geometry.y); + wld_fill_region(swc.drm->renderer, view->border.color, &border_damage); + } - pixman_region32_fini(&border_damage); + pixman_region32_fini(&border_damage); } -static void renderer_repaint(struct target * target, - pixman_region32_t * damage, - pixman_region32_t * base_damage, - struct wl_list * views) +static void +renderer_repaint(struct target *target, + pixman_region32_t *damage, + pixman_region32_t *base_damage, + struct wl_list *views) { - struct compositor_view * view; + struct compositor_view *view; - DEBUG("Rendering to target { x: %d, y: %d, w: %u, h: %u }\n", - target->view->geometry.x, target->view->geometry.y, - target->view->geometry.width, target->view->geometry.height); + DEBUG("Rendering to target { x: %d, y: %d, w: %u, h: %u }\n", + target->view->geometry.x, target->view->geometry.y, + target->view->geometry.width, target->view->geometry.height); - wld_set_target_surface(swc.drm->renderer, target->surface); + wld_set_target_surface(swc.drm->renderer, target->surface); - /* Paint base damage black. */ - if (pixman_region32_not_empty(base_damage)) - { - pixman_region32_translate(base_damage, - -target->view->geometry.x, - -target->view->geometry.y); - wld_fill_region(swc.drm->renderer, 0xff000000, base_damage); - } + /* Paint base damage black. */ + if (pixman_region32_not_empty(base_damage)) { + pixman_region32_translate(base_damage, + -target->view->geometry.x, + -target->view->geometry.y); + wld_fill_region(swc.drm->renderer, 0xff000000, base_damage); + } - wl_list_for_each_reverse(view, views, link) - { - if (view->visible && view->base.screens & target->mask) - repaint_view(target, view, damage); - } + wl_list_for_each_reverse (view, views, link) { + if (view->visible && view->base.screens & target->mask) + repaint_view(target, view, damage); + } - wld_flush(swc.drm->renderer); + wld_flush(swc.drm->renderer); } -static int renderer_attach(struct compositor_view * view, - struct wld_buffer * client_buffer) +static int +renderer_attach(struct compositor_view *view, + struct wld_buffer *client_buffer) { - struct wld_buffer * buffer; - bool was_proxy = view->buffer != view->base.buffer; - bool needs_proxy = client_buffer - && !(wld_capabilities(swc.drm->renderer, - client_buffer) & WLD_CAPABILITY_READ); - bool resized = view->buffer && client_buffer - && (view->buffer->width != client_buffer->width - || view->buffer->height != client_buffer->height); + struct wld_buffer *buffer; + bool was_proxy = view->buffer != view->base.buffer; + bool needs_proxy = client_buffer + && !(wld_capabilities(swc.drm->renderer, + client_buffer) & WLD_CAPABILITY_READ); + bool resized = view->buffer && client_buffer + && (view->buffer->width != client_buffer->width + || view->buffer->height != client_buffer->height); - if (client_buffer) - { - /* Create a proxy buffer if necessary (for example a hardware buffer + if (client_buffer) { + /* Create a proxy buffer if necessary (for example a hardware buffer * backing a SHM buffer). */ - if (needs_proxy) - { - if (!was_proxy || resized) - { - DEBUG("Creating a proxy buffer\n"); - buffer = wld_create_buffer(swc.drm->context, - client_buffer->width, - client_buffer->height, - client_buffer->format, WLD_FLAG_MAP); - - if (!buffer) - return -ENOMEM; - } - else - { - /* Otherwise we can keep the original proxy buffer. */ - buffer = view->buffer; - } - } - else - buffer = client_buffer; - } - else - buffer = NULL; - - /* If we no longer need a proxy buffer, or the original buffer is of a + if (needs_proxy) { + if (!was_proxy || resized) { + DEBUG("Creating a proxy buffer\n"); + buffer = wld_create_buffer(swc.drm->context, + client_buffer->width, + client_buffer->height, + client_buffer->format, WLD_FLAG_MAP); + + if (!buffer) + return -ENOMEM; + } else { + /* Otherwise we can keep the original proxy buffer. */ + buffer = view->buffer; + } + } else + buffer = client_buffer; + } else + buffer = NULL; + + /* If we no longer need a proxy buffer, or the original buffer is of a * different size, destroy the old proxy image. */ - if (view->buffer && ((!needs_proxy && was_proxy) - || (needs_proxy && resized))) - { - wld_buffer_unreference(view->buffer); - } + if (view->buffer && ((!needs_proxy && was_proxy) + || (needs_proxy && resized))) { + wld_buffer_unreference(view->buffer); + } - view->buffer = buffer; + view->buffer = buffer; - return 0; + return 0; } -static void renderer_flush_view(struct compositor_view * view) +static void +renderer_flush_view(struct compositor_view *view) { - if (view->buffer == view->base.buffer) - return; + if (view->buffer == view->base.buffer) + return; - wld_set_target_buffer(swc.shm->renderer, view->buffer); - wld_copy_region(swc.shm->renderer, view->base.buffer, - 0, 0, &view->surface->state.damage); - wld_flush(swc.shm->renderer); + wld_set_target_buffer(swc.shm->renderer, view->buffer); + wld_copy_region(swc.shm->renderer, view->base.buffer, + 0, 0, &view->surface->state.damage); + wld_flush(swc.shm->renderer); } /* }}} */ @@ -330,553 +323,557 @@ static void renderer_flush_view(struct compositor_view * view) * Adds damage from the region below a view, taking into account it's clip * region, to the region specified by `damage'. */ -static void damage_below_view(struct compositor_view * view) +static void +damage_below_view(struct compositor_view *view) { - pixman_region32_t damage_below; + pixman_region32_t damage_below; - pixman_region32_init_with_extents(&damage_below, &view->extents); - pixman_region32_subtract(&damage_below, &damage_below, &view->clip); - pixman_region32_union(&compositor.damage, &compositor.damage, - &damage_below); - pixman_region32_fini(&damage_below); + pixman_region32_init_with_extents(&damage_below, &view->extents); + pixman_region32_subtract(&damage_below, &damage_below, &view->clip); + pixman_region32_union(&compositor.damage, &compositor.damage, + &damage_below); + pixman_region32_fini(&damage_below); } /** * Completely damages the surface and its border. */ -static void damage_view(struct compositor_view * view) +static void +damage_view(struct compositor_view *view) { - damage_below_view(view); - view->border.damaged = true; + damage_below_view(view); + view->border.damaged = true; } -static void update_extents(struct compositor_view * view) +static void +update_extents(struct compositor_view *view) { - view->extents.x1 = view->base.geometry.x - view->border.width; - view->extents.y1 = view->base.geometry.y - view->border.width; - view->extents.x2 = view->base.geometry.x + view->base.geometry.width - + view->border.width; - view->extents.y2 = view->base.geometry.y + view->base.geometry.height - + view->border.width; + view->extents.x1 = view->base.geometry.x - view->border.width; + view->extents.y1 = view->base.geometry.y - view->border.width; + view->extents.x2 = view->base.geometry.x + view->base.geometry.width + + view->border.width; + view->extents.y2 = view->base.geometry.y + view->base.geometry.height + + view->border.width; - /* Damage border. */ - view->border.damaged = true; + /* Damage border. */ + view->border.damaged = true; } -static void schedule_updates(uint32_t screens) +static void +schedule_updates(uint32_t screens) { - if (compositor.scheduled_updates == 0) - wl_event_loop_add_idle(swc.event_loop, &perform_update, NULL); + if (compositor.scheduled_updates == 0) + wl_event_loop_add_idle(swc.event_loop, &perform_update, NULL); - if (screens == -1) - { - struct screen * screen; + if (screens == -1) { + struct screen *screen; - screens = 0; - wl_list_for_each(screen, &swc.screens, link) - screens |= screen_mask(screen); - } + screens = 0; + wl_list_for_each (screen, &swc.screens, link) + screens |= screen_mask(screen); + } - compositor.scheduled_updates |= screens; + compositor.scheduled_updates |= screens; } -static bool update(struct view * base) +static bool +update(struct view *base) { - struct compositor_view * view = (void *) base; + struct compositor_view *view = (void *)base; - if (!swc.active || !view->visible) - return false; + if (!swc.active || !view->visible) + return false; - schedule_updates(view->base.screens); + schedule_updates(view->base.screens); - return true; + return true; } -static int attach(struct view * base, struct wld_buffer * buffer) +static int +attach(struct view *base, struct wld_buffer *buffer) { - struct compositor_view * view = (void *) base; - int ret; + struct compositor_view *view = (void *)base; + int ret; - if ((ret = renderer_attach(view, buffer)) < 0) - return ret; + if ((ret = renderer_attach(view, buffer)) < 0) + return ret; - if (view->visible && view->base.buffer) - { - damage_below_view(view); - update(&view->base); - } + if (view->visible && view->base.buffer) { + damage_below_view(view); + update(&view->base); + } - if (view_set_size_from_buffer(&view->base, buffer)) - { - update_extents(view); + if (view_set_size_from_buffer(&view->base, buffer)) { + update_extents(view); - if (view->visible && buffer) - { - view_update_screens(&view->base); - damage_below_view(view); - update(&view->base); - } - } + if (view->visible && buffer) { + view_update_screens(&view->base); + damage_below_view(view); + update(&view->base); + } + } - return 0; + return 0; } -static bool move(struct view * base, int32_t x, int32_t y) +static bool +move(struct view *base, int32_t x, int32_t y) { - struct compositor_view * view = (void *) base; + struct compositor_view *view = (void *)base; - if (view->visible) - { - damage_below_view(view); - update(&view->base); - } + if (view->visible) { + damage_below_view(view); + update(&view->base); + } - if (view_set_position(&view->base, x, y)) - { - update_extents(view); + if (view_set_position(&view->base, x, y)) { + update_extents(view); - if (view->visible) - { - /* Assume worst-case no clipping until we draw the next frame (in + if (view->visible) { + /* Assume worst-case no clipping until we draw the next frame (in * case the surface gets moved again before that). */ - pixman_region32_init(&view->clip); + pixman_region32_init(&view->clip); - view_update_screens(&view->base); - damage_below_view(view); - update(&view->base); - } - } + view_update_screens(&view->base); + damage_below_view(view); + update(&view->base); + } + } - return true; + return true; } const static struct view_impl view_impl = { - .update = &update, - .attach = &attach, - .move = &move + .update = &update, + .attach = &attach, + .move = &move }; -struct compositor_view * compositor_create_view(struct surface * surface) +struct compositor_view * +compositor_create_view(struct surface *surface) { - struct compositor_view * view; + struct compositor_view *view; - view = malloc(sizeof *view); + view = malloc(sizeof *view); - if (!view) - return NULL; + if (!view) + return NULL; - view_initialize(&view->base, &view_impl); - view->surface = surface; - view->buffer = NULL; - view->window = NULL; - view->parent = NULL; - view->visible = false; - view->extents.x1 = 0; - view->extents.y1 = 0; - view->extents.x2 = 0; - view->extents.y2 = 0; - view->border.width = 0; - view->border.color = 0x000000; - view->border.damaged = false; - pixman_region32_init(&view->clip); - wl_signal_init(&view->destroy_signal); - surface_set_view(surface, &view->base); - wl_list_insert(&compositor.views, &view->link); + view_initialize(&view->base, &view_impl); + view->surface = surface; + view->buffer = NULL; + view->window = NULL; + view->parent = NULL; + view->visible = false; + view->extents.x1 = 0; + view->extents.y1 = 0; + view->extents.x2 = 0; + view->extents.y2 = 0; + view->border.width = 0; + view->border.color = 0x000000; + view->border.damaged = false; + pixman_region32_init(&view->clip); + wl_signal_init(&view->destroy_signal); + surface_set_view(surface, &view->base); + wl_list_insert(&compositor.views, &view->link); - return view; + return view; } -void compositor_view_destroy(struct compositor_view * view) +void +compositor_view_destroy(struct compositor_view *view) { - wl_signal_emit(&view->destroy_signal, NULL); - compositor_view_hide(view); - surface_set_view(view->surface, NULL); - view_finalize(&view->base); - pixman_region32_fini(&view->clip); - wl_list_remove(&view->link); - free(view); + wl_signal_emit(&view->destroy_signal, NULL); + compositor_view_hide(view); + surface_set_view(view->surface, NULL); + view_finalize(&view->base); + pixman_region32_fini(&view->clip); + wl_list_remove(&view->link); + free(view); } -struct compositor_view * compositor_view(struct view * view) +struct compositor_view * +compositor_view(struct view *view) { - return view->impl == &view_impl ? (struct compositor_view *) view : NULL; + return view->impl == &view_impl ? (struct compositor_view *)view : NULL; } -void compositor_view_set_parent(struct compositor_view * view, - struct compositor_view * parent) +void +compositor_view_set_parent(struct compositor_view *view, + struct compositor_view *parent) { - view->parent = view; + view->parent = view; - if (parent->visible) - compositor_view_show(view); - else - compositor_view_hide(view); + if (parent->visible) + compositor_view_show(view); + else + compositor_view_hide(view); } -void compositor_view_show(struct compositor_view * view) +void +compositor_view_show(struct compositor_view *view) { - struct compositor_view * other; + struct compositor_view *other; - if (view->visible) - return; + if (view->visible) + return; - view->visible = true; - view_update_screens(&view->base); + view->visible = true; + view_update_screens(&view->base); - /* Assume worst-case no clipping until we draw the next frame (in case the + /* Assume worst-case no clipping until we draw the next frame (in case the * surface gets moved before that. */ - pixman_region32_clear(&view->clip); - damage_view(view); - update(&view->base); + pixman_region32_clear(&view->clip); + damage_view(view); + update(&view->base); - wl_list_for_each(other, &compositor.views, link) - { - if (other->parent == view) - compositor_view_show(other); - } + wl_list_for_each (other, &compositor.views, link) { + if (other->parent == view) + compositor_view_show(other); + } } -void compositor_view_hide(struct compositor_view * view) +void +compositor_view_hide(struct compositor_view *view) { - struct compositor_view * other; + struct compositor_view *other; - if (!view->visible) - return; + if (!view->visible) + return; - /* Update all the screens the view was on. */ - update(&view->base); - damage_below_view(view); + /* Update all the screens the view was on. */ + update(&view->base); + damage_below_view(view); - view_set_screens(&view->base, 0); - view->visible = false; + view_set_screens(&view->base, 0); + view->visible = false; - wl_list_for_each(other, &compositor.views, link) - { - if (other->parent == view) - compositor_view_hide(other); - } + wl_list_for_each (other, &compositor.views, link) { + if (other->parent == view) + compositor_view_hide(other); + } } -void compositor_view_set_border_width(struct compositor_view * view, - uint32_t width) +void +compositor_view_set_border_width(struct compositor_view *view, + uint32_t width) { - if (view->border.width == width) - return; + if (view->border.width == width) + return; - view->border.width = width; - view->border.damaged = true; + view->border.width = width; + view->border.damaged = true; - /* XXX: Damage above surface for transparent surfaces? */ + /* XXX: Damage above surface for transparent surfaces? */ - update_extents(view); - update(&view->base); + update_extents(view); + update(&view->base); } -void compositor_view_set_border_color(struct compositor_view * view, - uint32_t color) +void +compositor_view_set_border_color(struct compositor_view *view, + uint32_t color) { - if (view->border.color == color) - return; + if (view->border.color == color) + return; - view->border.color = color; - view->border.damaged = true; + view->border.color = color; + view->border.damaged = true; - /* XXX: Damage above surface for transparent surfaces? */ + /* XXX: Damage above surface for transparent surfaces? */ - update(&view->base); + update(&view->base); } /* }}} */ -static void calculate_damage(void) +static void +calculate_damage(void) { - struct compositor_view * view; - pixman_region32_t surface_opaque, * surface_damage; + struct compositor_view *view; + pixman_region32_t surface_opaque, *surface_damage; - pixman_region32_clear(&compositor.opaque); - pixman_region32_init(&surface_opaque); + pixman_region32_clear(&compositor.opaque); + pixman_region32_init(&surface_opaque); - /* Go through views top-down to calculate clipping regions. */ - wl_list_for_each(view, &compositor.views, link) - { - if (!view->visible) - continue; + /* Go through views top-down to calculate clipping regions. */ + wl_list_for_each (view, &compositor.views, link) { + if (!view->visible) + continue; - /* Clip the surface by the opaque region covering it. */ - pixman_region32_copy(&view->clip, &compositor.opaque); + /* Clip the surface by the opaque region covering it. */ + pixman_region32_copy(&view->clip, &compositor.opaque); - /* Translate the opaque region to global coordinates. */ - pixman_region32_copy(&surface_opaque, &view->surface->state.opaque); - pixman_region32_translate(&surface_opaque, - view->base.geometry.x, view->base.geometry.y); + /* Translate the opaque region to global coordinates. */ + pixman_region32_copy(&surface_opaque, &view->surface->state.opaque); + pixman_region32_translate(&surface_opaque, + view->base.geometry.x, view->base.geometry.y); - /* Add the surface's opaque region to the accumulated opaque + /* Add the surface's opaque region to the accumulated opaque * region. */ - pixman_region32_union(&compositor.opaque, &compositor.opaque, - &surface_opaque); + pixman_region32_union(&compositor.opaque, &compositor.opaque, + &surface_opaque); - surface_damage = &view->surface->state.damage; + surface_damage = &view->surface->state.damage; - if (pixman_region32_not_empty(surface_damage)) - { - renderer_flush_view(view); + if (pixman_region32_not_empty(surface_damage)) { + renderer_flush_view(view); - /* Translate surface damage to global coordinates. */ - pixman_region32_translate - (surface_damage, view->base.geometry.x, view->base.geometry.y); + /* Translate surface damage to global coordinates. */ + pixman_region32_translate(surface_damage, view->base.geometry.x, view->base.geometry.y); - /* Add the surface damage to the compositor damage. */ - pixman_region32_union(&compositor.damage, &compositor.damage, - surface_damage); - pixman_region32_clear(surface_damage); - } + /* Add the surface damage to the compositor damage. */ + pixman_region32_union(&compositor.damage, &compositor.damage, + surface_damage); + pixman_region32_clear(surface_damage); + } - if (view->border.damaged) - { - pixman_region32_t border_region, view_region; + if (view->border.damaged) { + pixman_region32_t border_region, view_region; - pixman_region32_init_with_extents(&border_region, &view->extents); - pixman_region32_init_rect - (&view_region, view->base.geometry.x, view->base.geometry.y, - view->base.geometry.width, view->base.geometry.height); + pixman_region32_init_with_extents(&border_region, &view->extents); + pixman_region32_init_rect(&view_region, view->base.geometry.x, view->base.geometry.y, + view->base.geometry.width, view->base.geometry.height); - pixman_region32_subtract(&border_region, &border_region, - &view_region); + pixman_region32_subtract(&border_region, &border_region, + &view_region); - pixman_region32_union(&compositor.damage, &compositor.damage, - &border_region); + pixman_region32_union(&compositor.damage, &compositor.damage, + &border_region); - pixman_region32_fini(&border_region); - pixman_region32_fini(&view_region); + pixman_region32_fini(&border_region); + pixman_region32_fini(&view_region); - view->border.damaged = false; - } - } + view->border.damaged = false; + } + } - pixman_region32_fini(&surface_opaque); + pixman_region32_fini(&surface_opaque); } -static void update_screen(struct screen * screen) +static void +update_screen(struct screen *screen) { - struct target * target; - const struct swc_rectangle * geometry = &screen->base.geometry; - pixman_region32_t damage, * total_damage; + struct target *target; + const struct swc_rectangle *geometry = &screen->base.geometry; + pixman_region32_t damage, *total_damage; - if (!(compositor.scheduled_updates & screen_mask(screen))) - return; + if (!(compositor.scheduled_updates & screen_mask(screen))) + return; - if (!(target = target_get(screen))) - return; + if (!(target = target_get(screen))) + return; - pixman_region32_init(&damage); - pixman_region32_intersect_rect(&damage, &compositor.damage, - geometry->x, geometry->y, - geometry->width, geometry->height); - pixman_region32_translate(&damage, -geometry->x, -geometry->y); - total_damage = wld_surface_damage(target->surface, &damage); + pixman_region32_init(&damage); + pixman_region32_intersect_rect(&damage, &compositor.damage, + geometry->x, geometry->y, + geometry->width, geometry->height); + pixman_region32_translate(&damage, -geometry->x, -geometry->y); + total_damage = wld_surface_damage(target->surface, &damage); - /* Don't repaint the screen if it is waiting for a page flip. */ - if (compositor.pending_flips & screen_mask(screen)) - { - pixman_region32_fini(&damage); - return; - } - - pixman_region32_t base_damage; - pixman_region32_copy(&damage, total_damage); - pixman_region32_translate(&damage, geometry->x, geometry->y); - pixman_region32_init(&base_damage); - pixman_region32_subtract(&base_damage, &damage, &compositor.opaque); - renderer_repaint(target, &damage, &base_damage, &compositor.views); - pixman_region32_fini(&damage); - pixman_region32_fini(&base_damage); - - switch (target_swap_buffers(target)) - { - case -EACCES: - /* If we get an EACCES, it is because this session is being + /* Don't repaint the screen if it is waiting for a page flip. */ + if (compositor.pending_flips & screen_mask(screen)) { + pixman_region32_fini(&damage); + return; + } + + pixman_region32_t base_damage; + pixman_region32_copy(&damage, total_damage); + pixman_region32_translate(&damage, geometry->x, geometry->y); + pixman_region32_init(&base_damage); + pixman_region32_subtract(&base_damage, &damage, &compositor.opaque); + renderer_repaint(target, &damage, &base_damage, &compositor.views); + pixman_region32_fini(&damage); + pixman_region32_fini(&base_damage); + + switch (target_swap_buffers(target)) { + case -EACCES: + /* If we get an EACCES, it is because this session is being * deactivated, but we haven't yet received the deactivate signal * from swc-launch. */ - swc_deactivate(); - break; - case 0: - compositor.pending_flips |= screen_mask(screen); - break; - } + swc_deactivate(); + break; + case 0: + compositor.pending_flips |= screen_mask(screen); + break; + } } -static void perform_update(void * data) +static void +perform_update(void *data) { - struct screen * screen; - uint32_t updates = compositor.scheduled_updates - & ~compositor.pending_flips; + struct screen *screen; + uint32_t updates = compositor.scheduled_updates + & ~compositor.pending_flips; - if (!swc.active || !updates) - return; + if (!swc.active || !updates) + return; - DEBUG("Performing update\n"); + DEBUG("Performing update\n"); - compositor.updating = true; - calculate_damage(); + compositor.updating = true; + calculate_damage(); - wl_list_for_each(screen, &swc.screens, link) - update_screen(screen); + wl_list_for_each (screen, &swc.screens, link) + update_screen(screen); - /* XXX: Should assert that all damage was covered by some output */ - pixman_region32_clear(&compositor.damage); - compositor.scheduled_updates &= ~updates; - compositor.updating = false; + /* XXX: Should assert that all damage was covered by some output */ + pixman_region32_clear(&compositor.damage); + compositor.scheduled_updates &= ~updates; + compositor.updating = false; } -bool handle_motion(struct pointer_handler * handler, uint32_t time, - wl_fixed_t fx, wl_fixed_t fy) +bool +handle_motion(struct pointer_handler *handler, uint32_t time, + wl_fixed_t fx, wl_fixed_t fy) { - struct compositor_view * view; - bool found = false; - int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy); + struct compositor_view *view; + bool found = false; + int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy); - wl_list_for_each(view, &compositor.views, link) - { - if (!view->visible) - continue; + wl_list_for_each (view, &compositor.views, link) { + if (!view->visible) + continue; - if (rectangle_contains_point(&view->base.geometry, x, y) - && pixman_region32_contains_point(&view->surface->state.input, - x - view->base.geometry.x, - y - view->base.geometry.y, NULL)) - { - found = true; - break; - } - } + if (rectangle_contains_point(&view->base.geometry, x, y) + && pixman_region32_contains_point(&view->surface->state.input, + x - view->base.geometry.x, + y - view->base.geometry.y, NULL)) { + found = true; + break; + } + } - pointer_set_focus(swc.seat->pointer, found ? view : NULL); + pointer_set_focus(swc.seat->pointer, found ? view : NULL); - return false; + return false; } -static void handle_terminate(void * data, uint32_t time, - uint32_t value, uint32_t state) +static void +handle_terminate(void *data, uint32_t time, + uint32_t value, uint32_t state) { - if (state == WL_KEYBOARD_KEY_STATE_PRESSED) - wl_display_terminate(swc.display); + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) + wl_display_terminate(swc.display); } -static void handle_switch_vt(void * data, uint32_t time, - uint32_t value, uint32_t state) +static void +handle_switch_vt(void *data, uint32_t time, + uint32_t value, uint32_t state) { - uint8_t vt = value - XKB_KEY_XF86Switch_VT_1 + 1; + uint8_t vt = value - XKB_KEY_XF86Switch_VT_1 + 1; - if (state == WL_KEYBOARD_KEY_STATE_PRESSED) - launch_activate_vt(vt); + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) + launch_activate_vt(vt); } -static void handle_swc_event(struct wl_listener * listener, void * data) +static void +handle_swc_event(struct wl_listener *listener, void *data) { - struct event * event = data; + struct event *event = data; - switch (event->type) - { - case SWC_EVENT_ACTIVATED: - schedule_updates(-1); - break; - case SWC_EVENT_DEACTIVATED: - compositor.scheduled_updates = 0; - break; - } + switch (event->type) { + case SWC_EVENT_ACTIVATED: + schedule_updates(-1); + break; + case SWC_EVENT_DEACTIVATED: + compositor.scheduled_updates = 0; + break; + } } -static void create_surface(struct wl_client * client, - struct wl_resource * resource, uint32_t id) +static void +create_surface(struct wl_client *client, + struct wl_resource *resource, uint32_t id) { - struct surface * surface; + struct surface *surface; - /* Initialize surface. */ - surface = surface_new(client, wl_resource_get_version(resource), id); + /* Initialize surface. */ + surface = surface_new(client, wl_resource_get_version(resource), id); - if (!surface) - { - wl_resource_post_no_memory(resource); - return; - } + if (!surface) { + wl_resource_post_no_memory(resource); + return; + } - wl_signal_emit(&swc_compositor.signal.new_surface, surface); + wl_signal_emit(&swc_compositor.signal.new_surface, surface); } -static void create_region(struct wl_client * client, - struct wl_resource * resource, uint32_t id) +static void +create_region(struct wl_client *client, + struct wl_resource *resource, uint32_t id) { - struct region * region; + struct region *region; - region = region_new(client, wl_resource_get_version(resource), id); + region = region_new(client, wl_resource_get_version(resource), id); - if (!region) - wl_resource_post_no_memory(resource); + if (!region) + wl_resource_post_no_memory(resource); } static struct wl_compositor_interface compositor_implementation = { - .create_surface = &create_surface, - .create_region = &create_region + .create_surface = &create_surface, + .create_region = &create_region }; -static void bind_compositor(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_compositor(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 3) - version = 3; + if (version > 3) + version = 3; - resource = wl_resource_create(client, &wl_compositor_interface, - version, id); - wl_resource_set_implementation(resource, &compositor_implementation, - NULL, NULL); + resource = wl_resource_create(client, &wl_compositor_interface, + version, id); + wl_resource_set_implementation(resource, &compositor_implementation, + NULL, NULL); } -bool compositor_initialize(void) +bool +compositor_initialize(void) { - struct screen * screen; - uint32_t keysym; + struct screen *screen; + uint32_t keysym; - compositor.global = wl_global_create - (swc.display, &wl_compositor_interface, 3, NULL, &bind_compositor); + compositor.global = wl_global_create(swc.display, &wl_compositor_interface, 3, NULL, &bind_compositor); - if (!compositor.global) - return false; + if (!compositor.global) + return false; - compositor.scheduled_updates = 0; - compositor.pending_flips = 0; - compositor.updating = false; - pixman_region32_init(&compositor.damage); - pixman_region32_init(&compositor.opaque); - wl_list_init(&compositor.views); - wl_signal_init(&swc_compositor.signal.new_surface); - compositor.swc_listener.notify = &handle_swc_event; - wl_signal_add(&swc.event_signal, &compositor.swc_listener); + compositor.scheduled_updates = 0; + compositor.pending_flips = 0; + compositor.updating = false; + pixman_region32_init(&compositor.damage); + pixman_region32_init(&compositor.opaque); + wl_list_init(&compositor.views); + wl_signal_init(&swc_compositor.signal.new_surface); + compositor.swc_listener.notify = &handle_swc_event; + wl_signal_add(&swc.event_signal, &compositor.swc_listener); - wl_list_for_each(screen, &swc.screens, link) - target_new(screen); - if (swc.active) - schedule_updates(-1); + wl_list_for_each (screen, &swc.screens, link) + target_new(screen); + if (swc.active) + schedule_updates(-1); - swc_add_binding(SWC_BINDING_KEY, SWC_MOD_CTRL | SWC_MOD_ALT, - XKB_KEY_BackSpace, &handle_terminate, NULL); + swc_add_binding(SWC_BINDING_KEY, SWC_MOD_CTRL | SWC_MOD_ALT, + XKB_KEY_BackSpace, &handle_terminate, NULL); - for (keysym = XKB_KEY_XF86Switch_VT_1; - keysym <= XKB_KEY_XF86Switch_VT_12; - ++keysym) - { - swc_add_binding(SWC_BINDING_KEY, SWC_MOD_ANY, keysym, - &handle_switch_vt, NULL); - } + for (keysym = XKB_KEY_XF86Switch_VT_1; + keysym <= XKB_KEY_XF86Switch_VT_12; + ++keysym) { + swc_add_binding(SWC_BINDING_KEY, SWC_MOD_ANY, keysym, + &handle_switch_vt, NULL); + } - return true; + return true; } -void compositor_finalize(void) +void +compositor_finalize(void) { - pixman_region32_fini(&compositor.damage); - pixman_region32_fini(&compositor.opaque); - wl_global_destroy(compositor.global); + pixman_region32_fini(&compositor.damage); + pixman_region32_fini(&compositor.opaque); + wl_global_destroy(compositor.global); } - diff --git a/libswc/compositor.h b/libswc/compositor.h @@ -29,72 +29,69 @@ #include <stdbool.h> #include <pixman.h> -struct swc_compositor -{ - struct pointer_handler * const pointer_handler; - struct - { - /** +struct swc_compositor { + struct pointer_handler *const pointer_handler; + struct + { + /** * Emitted when a new surface is created. * * The data argument of the signal refers to the surface that has been * created. */ - struct wl_signal new_surface; - } signal; + struct wl_signal new_surface; + } signal; }; bool compositor_initialize(void); void compositor_finalize(void); -struct compositor_view -{ - struct view base; - struct surface * surface; - struct wld_buffer * buffer; - struct window * window; - struct compositor_view * parent; +struct compositor_view { + struct view base; + struct surface *surface; + struct wld_buffer *buffer; + struct window *window; + struct compositor_view *parent; - /* Whether or not the view is visible (mapped). */ - bool visible; + /* Whether or not the view is visible (mapped). */ + bool visible; - /* The box that the surface covers (including it's border). */ - pixman_box32_t extents; + /* The box that the surface covers (including it's border). */ + pixman_box32_t extents; - /* The region that is covered by opaque regions of surfaces above this + /* The region that is covered by opaque regions of surfaces above this * surface. */ - pixman_region32_t clip; + pixman_region32_t clip; - struct - { - uint32_t width; - uint32_t color; - bool damaged; - } border; + struct + { + uint32_t width; + uint32_t color; + bool damaged; + } border; - struct wl_list link; - struct wl_signal destroy_signal; + struct wl_list link; + struct wl_signal destroy_signal; }; -struct compositor_view * compositor_create_view(struct surface * surface); +struct compositor_view *compositor_create_view(struct surface *surface); -void compositor_view_destroy(struct compositor_view * view); +void compositor_view_destroy(struct compositor_view *view); /** * Returns view as a compositor_view, or NULL if view is not a compositor_view. */ -struct compositor_view * compositor_view(struct view * view); +struct compositor_view *compositor_view(struct view *view); -void compositor_view_set_parent(struct compositor_view * view, - struct compositor_view * parent); +void compositor_view_set_parent(struct compositor_view *view, + struct compositor_view *parent); -void compositor_view_show(struct compositor_view * view); -void compositor_view_hide(struct compositor_view * view); +void compositor_view_show(struct compositor_view *view); +void compositor_view_hide(struct compositor_view *view); -void compositor_view_set_border_color(struct compositor_view * view, +void compositor_view_set_border_color(struct compositor_view *view, uint32_t color); -void compositor_view_set_border_width(struct compositor_view * view, +void compositor_view_set_border_width(struct compositor_view *view, uint32_t width); #endif - diff --git a/libswc/cursor_plane.c b/libswc/cursor_plane.c @@ -34,96 +34,89 @@ #include <wld/drm.h> #include <xf86drmMode.h> -static bool update(struct view * view) +static bool +update(struct view *view) { - return true; + return true; } -static int attach(struct view * view, struct wld_buffer * buffer) +static int +attach(struct view *view, struct wld_buffer *buffer) { - struct cursor_plane * plane = wl_container_of(view, plane, view); + struct cursor_plane *plane = wl_container_of(view, plane, view); - if (buffer) - { - union wld_object object; + if (buffer) { + union wld_object object; - if (!wld_export(buffer, WLD_DRM_OBJECT_HANDLE, &object)) - { - ERROR("Could not get export buffer to DRM handle\n"); - /* XXX: Not the best error code, but we don't know better until wld + if (!wld_export(buffer, WLD_DRM_OBJECT_HANDLE, &object)) { + ERROR("Could not get export buffer to DRM handle\n"); + /* XXX: Not the best error code, but we don't know better until wld * returns an actual error code. */ - return -EINVAL; - } - - if (swc.active && drmModeSetCursor(swc.drm->fd, plane->crtc, object.u32, - buffer->width, buffer->height) < 0) - { - ERROR("Could not set cursor: %s\n", strerror(errno)); - return -errno; - } - } - else if (swc.active && drmModeSetCursor(swc.drm->fd, plane->crtc, - 0, 0, 0) < 0) - { - ERROR("Could not unset cursor: %s\n", strerror(errno)); - return -errno; - } - - view_set_size_from_buffer(view, buffer); - return 0; + return -EINVAL; + } + + if (swc.active && drmModeSetCursor(swc.drm->fd, plane->crtc, object.u32, buffer->width, buffer->height) < 0) { + ERROR("Could not set cursor: %s\n", strerror(errno)); + return -errno; + } + } else if (swc.active && drmModeSetCursor(swc.drm->fd, plane->crtc, 0, 0, 0) < 0) { + ERROR("Could not unset cursor: %s\n", strerror(errno)); + return -errno; + } + + view_set_size_from_buffer(view, buffer); + return 0; } -static bool move(struct view * view, int32_t x, int32_t y) +static bool +move(struct view *view, int32_t x, int32_t y) { - struct cursor_plane * plane = wl_container_of(view, plane, view); + struct cursor_plane *plane = wl_container_of(view, plane, view); - if (swc.active && drmModeMoveCursor(swc.drm->fd, plane->crtc, - x - plane->origin->x, - y - plane->origin->y) != 0) - { - ERROR("Could not move cursor: %s\n", strerror(errno)); - return false; - } + if (swc.active && drmModeMoveCursor(swc.drm->fd, plane->crtc, x - plane->origin->x, y - plane->origin->y) != 0) { + ERROR("Could not move cursor: %s\n", strerror(errno)); + return false; + } - view_set_position(view, x, y); + view_set_position(view, x, y); - return true; + return true; } static const struct view_impl view_impl = { - .update = &update, - .attach = &attach, - .move = &move + .update = &update, + .attach = &attach, + .move = &move }; -static void handle_swc_event(struct wl_listener * listener, void * data) +static void +handle_swc_event(struct wl_listener *listener, void *data) { - struct event * event = data; - struct cursor_plane * plane - = wl_container_of(listener, plane, swc_listener); - - switch (event->type) - { - case SWC_EVENT_ACTIVATED: - move(&plane->view, plane->view.geometry.x, plane->view.geometry.y); - attach(&plane->view, plane->view.buffer); - break; - } + struct event *event = data; + struct cursor_plane *plane = wl_container_of(listener, plane, swc_listener); + + switch (event->type) { + case SWC_EVENT_ACTIVATED: + move(&plane->view, plane->view.geometry.x, plane->view.geometry.y); + attach(&plane->view, plane->view.buffer); + break; + } } -bool cursor_plane_initialize(struct cursor_plane * plane, uint32_t crtc, - const struct swc_rectangle * origin) +bool +cursor_plane_initialize(struct cursor_plane *plane, uint32_t crtc, + const struct swc_rectangle *origin) { - plane->origin = origin; - plane->crtc = crtc; - plane->swc_listener.notify = &handle_swc_event; - wl_signal_add(&swc.event_signal, &plane->swc_listener); - view_initialize(&plane->view, &view_impl); + plane->origin = origin; + plane->crtc = crtc; + plane->swc_listener.notify = &handle_swc_event; + wl_signal_add(&swc.event_signal, &plane->swc_listener); + view_initialize(&plane->view, &view_impl); - return true; + return true; } -void cursor_plane_finalize(struct cursor_plane * plane) +void +cursor_plane_finalize(struct cursor_plane *plane) { } - diff --git a/libswc/cursor_plane.h b/libswc/cursor_plane.h @@ -26,18 +26,16 @@ #include "view.h" -struct cursor_plane -{ - struct view view; - const struct swc_rectangle * origin; - uint32_t crtc; - struct wl_listener swc_listener; +struct cursor_plane { + struct view view; + const struct swc_rectangle *origin; + uint32_t crtc; + struct wl_listener swc_listener; }; -bool cursor_plane_initialize(struct cursor_plane * plane, uint32_t crtc, - const struct swc_rectangle * origin); +bool cursor_plane_initialize(struct cursor_plane *plane, uint32_t crtc, + const struct swc_rectangle *origin); -void cursor_plane_finalize(struct cursor_plane * plane); +void cursor_plane_finalize(struct cursor_plane *plane); #endif - diff --git a/libswc/data.c b/libswc/data.c @@ -29,85 +29,90 @@ #include <unistd.h> #include <wayland-server.h> -struct data -{ - struct wl_array mime_types; - struct wl_resource * source; - struct wl_list offers; +struct data { + struct wl_array mime_types; + struct wl_resource *source; + struct wl_list offers; }; -static void offer_accept(struct wl_client * client, - struct wl_resource * offer, - uint32_t serial, const char * mime_type) +static void +offer_accept(struct wl_client *client, + struct wl_resource *offer, + uint32_t serial, const char *mime_type) { - struct data * data = wl_resource_get_user_data(offer); + struct data *data = wl_resource_get_user_data(offer); - /* Protect against expired data_offers being used. */ - if (!data) - return; + /* Protect against expired data_offers being used. */ + if (!data) + return; - wl_data_source_send_target(data->source, mime_type); + wl_data_source_send_target(data->source, mime_type); } -static void offer_receive(struct wl_client * client, - struct wl_resource * offer, - const char * mime_type, int fd) +static void +offer_receive(struct wl_client *client, + struct wl_resource *offer, + const char *mime_type, int fd) { - struct data * data = wl_resource_get_user_data(offer); + struct data *data = wl_resource_get_user_data(offer); - /* Protect against expired data_offers being used. */ - if (!data) - return; + /* Protect against expired data_offers being used. */ + if (!data) + return; - wl_data_source_send_send(data->source, mime_type, fd); - close(fd); + wl_data_source_send_send(data->source, mime_type, fd); + close(fd); } -static void offer_destroy(struct wl_client * client, - struct wl_resource * offer) +static void +offer_destroy(struct wl_client *client, + struct wl_resource *offer) { - wl_resource_destroy(offer); + wl_resource_destroy(offer); } static struct wl_data_offer_interface data_offer_implementation = { - .accept = &offer_accept, - .receive = &offer_receive, - .destroy = &offer_destroy + .accept = &offer_accept, + .receive = &offer_receive, + .destroy = &offer_destroy }; -static void source_offer(struct wl_client * client, - struct wl_resource * source, - const char * mime_type) +static void +source_offer(struct wl_client *client, + struct wl_resource *source, + const char *mime_type) { - struct data * data = wl_resource_get_user_data(source); - char ** destination; + struct data *data = wl_resource_get_user_data(source); + char **destination; - destination = wl_array_add(&data->mime_types, sizeof *destination); - *destination = strdup(mime_type); + destination = wl_array_add(&data->mime_types, sizeof *destination); + *destination = strdup(mime_type); } -static void source_destroy(struct wl_client * client, - struct wl_resource * source) +static void +source_destroy(struct wl_client *client, + struct wl_resource *source) { - wl_resource_destroy(source); + wl_resource_destroy(source); } static struct wl_data_source_interface data_source_implementation = { - .offer = &source_offer, - .destroy = &source_destroy + .offer = &source_offer, + .destroy = &source_destroy }; -static void data_destroy(struct wl_resource * source) +static void +data_destroy(struct wl_resource *source) { - struct data * data = wl_resource_get_user_data(source); - struct wl_resource * offer; - char ** mime_type; + struct data *data = wl_resource_get_user_data(source); + struct wl_resource *offer; + char **mime_type; - wl_array_for_each(mime_type, &data->mime_types) - free(*mime_type); - wl_array_release(&data->mime_types); + wl_array_for_each (mime_type, &data->mime_types) + free(*mime_type); + wl_array_release(&data->mime_types); - /* After this data_source is destroyed, each of the data_offer objects + /* After this data_source is destroyed, each of the data_offer objects * associated with the data_source has a pointer to a free'd struct. We * can't destroy the resources because this results in a segfault on the * client when it correctly tries to call data_source.destroy. However, a @@ -115,73 +120,75 @@ static void data_destroy(struct wl_resource * source) * data_offer, which would crash the server. * * So, we clear the user data on each of the offers to protect us. */ - wl_resource_for_each(offer, &data->offers) - { - wl_resource_set_user_data(offer, NULL); - wl_resource_set_destructor(offer, NULL); - } + wl_resource_for_each (offer, &data->offers) { + wl_resource_set_user_data(offer, NULL); + wl_resource_set_destructor(offer, NULL); + } - free(data); + free(data); } -static struct data * data_new(void) +static struct data * +data_new(void) { - struct data * data; + struct data *data; - data = malloc(sizeof *data); + data = malloc(sizeof *data); - if (!data) - return NULL; + if (!data) + return NULL; - wl_array_init(&data->mime_types); - wl_list_init(&data->offers); + wl_array_init(&data->mime_types); + wl_list_init(&data->offers); - return data; + return data; } -struct wl_resource * data_source_new(struct wl_client * client, - uint32_t version, uint32_t id) +struct wl_resource * +data_source_new(struct wl_client *client, + uint32_t version, uint32_t id) { - struct data * data; + struct data *data; - data = data_new(); + data = data_new(); - if (!data) - return NULL; + if (!data) + return NULL; - /* Add the data source to the client. */ - data->source = wl_resource_create(client, &wl_data_source_interface, - version, id); + /* Add the data source to the client. */ + data->source = wl_resource_create(client, &wl_data_source_interface, + version, id); - /* Destroy the data object when the source disappears. */ - wl_resource_set_implementation(data->source, &data_source_implementation, - data, &data_destroy); + /* Destroy the data object when the source disappears. */ + wl_resource_set_implementation(data->source, &data_source_implementation, + data, &data_destroy); - return data->source; + return data->source; } -struct wl_resource * data_offer_new(struct wl_client * client, - struct wl_resource * source, - uint32_t version) +struct wl_resource * +data_offer_new(struct wl_client *client, + struct wl_resource *source, + uint32_t version) { - struct data * data = wl_resource_get_user_data(source); - struct wl_resource * offer; + struct data *data = wl_resource_get_user_data(source); + struct wl_resource *offer; - offer = wl_resource_create(client, &wl_data_offer_interface, version, 0); - wl_resource_set_implementation(offer, &data_offer_implementation, - data, &remove_resource); - wl_list_insert(&data->offers, wl_resource_get_link(offer)); + offer = wl_resource_create(client, &wl_data_offer_interface, version, 0); + wl_resource_set_implementation(offer, &data_offer_implementation, + data, &remove_resource); + wl_list_insert(&data->offers, wl_resource_get_link(offer)); - return offer; + return offer; } -void data_send_mime_types(struct wl_resource * source, - struct wl_resource * offer) +void +data_send_mime_types(struct wl_resource *source, + struct wl_resource *offer) { - struct data * data = wl_resource_get_user_data(source); - char ** mime_type; + struct data *data = wl_resource_get_user_data(source); + char **mime_type; - wl_array_for_each(mime_type, &data->mime_types) - wl_data_offer_send_offer(offer, *mime_type); + wl_array_for_each (mime_type, &data->mime_types) + wl_data_offer_send_offer(offer, *mime_type); } - diff --git a/libswc/data.h b/libswc/data.h @@ -28,15 +28,14 @@ struct wl_client; -struct wl_resource * data_source_new(struct wl_client * client, - uint32_t version, uint32_t id); +struct wl_resource *data_source_new(struct wl_client *client, + uint32_t version, uint32_t id); -struct wl_resource * data_offer_new(struct wl_client * client, - struct wl_resource * source, - uint32_t version); +struct wl_resource *data_offer_new(struct wl_client *client, + struct wl_resource *source, + uint32_t version); -void data_send_mime_types(struct wl_resource * source, - struct wl_resource * offer); +void data_send_mime_types(struct wl_resource *source, + struct wl_resource *offer); #endif - diff --git a/libswc/data_device.c b/libswc/data_device.c @@ -25,116 +25,120 @@ #include "data.h" #include "util.h" -static void start_drag(struct wl_client * client, struct wl_resource * resource, - struct wl_resource * source_resource, - struct wl_resource * origin_resource, - struct wl_resource * icon_resource, uint32_t serial) +static void +start_drag(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *source_resource, + struct wl_resource *origin_resource, + struct wl_resource *icon_resource, uint32_t serial) { - /* XXX: Implement */ + /* XXX: Implement */ } -static void set_selection(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * data_source, uint32_t serial) +static void +set_selection(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *data_source, uint32_t serial) { - struct data_device * data_device = wl_resource_get_user_data(resource); + struct data_device *data_device = wl_resource_get_user_data(resource); - /* Check if this data source is already the current selection. */ - if (data_source == data_device->selection) - return; + /* Check if this data source is already the current selection. */ + if (data_source == data_device->selection) + return; - if (data_device->selection) - { - wl_data_source_send_cancelled(data_device->selection); - wl_list_remove(&data_device->selection_destroy_listener.link); - } + if (data_device->selection) { + wl_data_source_send_cancelled(data_device->selection); + wl_list_remove(&data_device->selection_destroy_listener.link); + } - data_device->selection = data_source; + data_device->selection = data_source; - if (data_source) - { - wl_resource_add_destroy_listener - (data_source, &data_device->selection_destroy_listener); - } + if (data_source) { + wl_resource_add_destroy_listener(data_source, &data_device->selection_destroy_listener); + } - send_event(&data_device->event_signal, - DATA_DEVICE_EVENT_SELECTION_CHANGED, NULL); + send_event(&data_device->event_signal, + DATA_DEVICE_EVENT_SELECTION_CHANGED, NULL); } static struct wl_data_device_interface data_device_implementation = { - .start_drag = &start_drag, - .set_selection = &set_selection + .start_drag = &start_drag, + .set_selection = &set_selection }; -static void handle_selection_destroy(struct wl_listener * listener, void * data) +static void +handle_selection_destroy(struct wl_listener *listener, void *data) { - struct data_device * data_device - = wl_container_of(listener, data_device, selection_destroy_listener); + struct data_device *data_device = wl_container_of(listener, data_device, selection_destroy_listener); - data_device->selection = NULL; - send_event(&data_device->event_signal, - DATA_DEVICE_EVENT_SELECTION_CHANGED, NULL); + data_device->selection = NULL; + send_event(&data_device->event_signal, + DATA_DEVICE_EVENT_SELECTION_CHANGED, NULL); } -bool data_device_initialize(struct data_device * data_device) +bool +data_device_initialize(struct data_device *data_device) { - data_device->selection_destroy_listener.notify = &handle_selection_destroy; - wl_signal_init(&data_device->event_signal); - wl_list_init(&data_device->resources); + data_device->selection_destroy_listener.notify = &handle_selection_destroy; + wl_signal_init(&data_device->event_signal); + wl_list_init(&data_device->resources); - return true; + return true; } -void data_device_finalize(struct data_device * data_device) +void +data_device_finalize(struct data_device *data_device) { - struct wl_resource * resource, * tmp; + struct wl_resource *resource, *tmp; - wl_list_for_each_safe(resource, tmp, &data_device->resources, link) - wl_resource_destroy(resource); + wl_list_for_each_safe (resource, tmp, &data_device->resources, link) + wl_resource_destroy(resource); } -void data_device_bind(struct data_device * data_device, - struct wl_client * client, uint32_t version, uint32_t id) +void +data_device_bind(struct data_device *data_device, + struct wl_client *client, uint32_t version, uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - resource = wl_resource_create(client, &wl_data_device_interface, - version, id); - wl_resource_set_implementation(resource, &data_device_implementation, - data_device, &remove_resource); - wl_list_insert(&data_device->resources, &resource->link); + resource = wl_resource_create(client, &wl_data_device_interface, + version, id); + wl_resource_set_implementation(resource, &data_device_implementation, + data_device, &remove_resource); + wl_list_insert(&data_device->resources, &resource->link); } -static struct wl_resource * new_offer(struct wl_resource * resource, - struct wl_client * client, - struct wl_resource * source) +static struct wl_resource * +new_offer(struct wl_resource *resource, + struct wl_client *client, + struct wl_resource *source) { - struct wl_resource * offer; + struct wl_resource *offer; - offer = data_offer_new(client, source, wl_resource_get_version(resource)); - wl_data_device_send_data_offer(resource, offer); - data_send_mime_types(source, offer); + offer = data_offer_new(client, source, wl_resource_get_version(resource)); + wl_data_device_send_data_offer(resource, offer); + data_send_mime_types(source, offer); - return offer; + return offer; } -void data_device_offer_selection(struct data_device * data_device, - struct wl_client * client) +void +data_device_offer_selection(struct data_device *data_device, + struct wl_client *client) { - struct wl_resource * resource; - struct wl_resource * offer; + struct wl_resource *resource; + struct wl_resource *offer; - /* Look for the client's data_device resource. */ - resource = wl_resource_find_for_client(&data_device->resources, client); + /* Look for the client's data_device resource. */ + resource = wl_resource_find_for_client(&data_device->resources, client); - /* If the client does not have a data device, there is nothing to do. */ - if (!resource) - return; + /* If the client does not have a data device, there is nothing to do. */ + if (!resource) + return; - /* If we don't have a selection, send NULL to the client. */ - offer = data_device->selection - ? new_offer(resource, client, data_device->selection) : NULL; + /* If we don't have a selection, send NULL to the client. */ + offer = data_device->selection + ? new_offer(resource, client, data_device->selection) + : NULL; - wl_data_device_send_selection(resource, offer); + wl_data_device_send_selection(resource, offer); } - diff --git a/libswc/data_device.h b/libswc/data_device.h @@ -29,29 +29,26 @@ #include <stdbool.h> #include <wayland-server.h> -enum -{ - DATA_DEVICE_EVENT_SELECTION_CHANGED +enum { + DATA_DEVICE_EVENT_SELECTION_CHANGED }; -struct data_device -{ - /* The data source corresponding to the current selection. */ - struct wl_resource * selection; - struct wl_listener selection_destroy_listener; +struct data_device { + /* The data source corresponding to the current selection. */ + struct wl_resource *selection; + struct wl_listener selection_destroy_listener; - struct wl_signal event_signal; - struct wl_list resources; + struct wl_signal event_signal; + struct wl_list resources; }; -bool data_device_initialize(struct data_device * data_device); -void data_device_finalize(struct data_device * data_device); +bool data_device_initialize(struct data_device *data_device); +void data_device_finalize(struct data_device *data_device); -void data_device_bind(struct data_device * data_device, - struct wl_client * client, uint32_t version, uint32_t id); +void data_device_bind(struct data_device *data_device, + struct wl_client *client, uint32_t version, uint32_t id); -void data_device_offer_selection(struct data_device * data_device, - struct wl_client * client); +void data_device_offer_selection(struct data_device *data_device, + struct wl_client *client); #endif - diff --git a/libswc/data_device_manager.c b/libswc/data_device_manager.c @@ -28,61 +28,63 @@ #include "seat.h" static struct -{ - struct wl_global * global; + { + struct wl_global *global; } data_device_manager; -static void create_data_source(struct wl_client * client, - struct wl_resource * resource, uint32_t id) +static void +create_data_source(struct wl_client *client, + struct wl_resource *resource, uint32_t id) { - struct wl_resource * data_source; + struct wl_resource *data_source; - data_source = data_source_new(client, - wl_resource_get_version(resource), id); + data_source = data_source_new(client, + wl_resource_get_version(resource), id); - if (!data_source) - wl_resource_post_no_memory(resource); + if (!data_source) + wl_resource_post_no_memory(resource); } -static void get_data_device(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - struct wl_resource * seat_resource) +static void +get_data_device(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *seat_resource) { - data_device_bind(swc.seat->data_device, client, - wl_resource_get_version(resource), id); + data_device_bind(swc.seat->data_device, client, + wl_resource_get_version(resource), id); } static struct wl_data_device_manager_interface data_device_manager_implementation = { - .create_data_source = &create_data_source, - .get_data_device = &get_data_device -}; + .create_data_source = &create_data_source, + .get_data_device = &get_data_device + }; -static void bind_data_device_manager(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_data_device_manager(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 1) - version = 1; + if (version > 1) + version = 1; - resource = wl_resource_create(client, &wl_data_device_manager_interface, - version, id); - wl_resource_set_implementation - (resource, &data_device_manager_implementation, NULL, NULL); + resource = wl_resource_create(client, &wl_data_device_manager_interface, + version, id); + wl_resource_set_implementation(resource, &data_device_manager_implementation, NULL, NULL); } -bool data_device_manager_initialize(void) +bool +data_device_manager_initialize(void) { - data_device_manager.global - = wl_global_create(swc.display, &wl_data_device_manager_interface, 1, - NULL, &bind_data_device_manager); + data_device_manager.global = wl_global_create(swc.display, &wl_data_device_manager_interface, 1, + NULL, &bind_data_device_manager); - return data_device_manager.global != NULL; + return data_device_manager.global != NULL; } -void data_device_manager_finalize(void) +void +data_device_manager_finalize(void) { - wl_global_destroy(data_device_manager.global); + wl_global_destroy(data_device_manager.global); } - diff --git a/libswc/data_device_manager.h b/libswc/data_device_manager.h @@ -30,4 +30,3 @@ bool data_device_manager_initialize(void); void data_device_manager_finalize(void); #endif - diff --git a/libswc/drm.c b/libswc/drm.c @@ -47,384 +47,374 @@ struct swc_drm swc_drm; static struct -{ - char path[128]; + { + char path[128]; - uint32_t taken_ids; + uint32_t taken_ids; - struct wl_global * global; - struct wl_event_source * event_source; + struct wl_global *global; + struct wl_event_source *event_source; } drm; -static void authenticate(struct wl_client * client, - struct wl_resource * resource, uint32_t magic) +static void +authenticate(struct wl_client *client, + struct wl_resource *resource, uint32_t magic) { - wl_drm_send_authenticated(resource); + wl_drm_send_authenticated(resource); } -static void create_buffer(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - uint32_t name, int32_t width, int32_t height, - uint32_t stride, uint32_t format) +static void +create_buffer(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + uint32_t name, int32_t width, int32_t height, + uint32_t stride, uint32_t format) { - wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME, - "GEM names are not supported, " - "use a PRIME fd instead"); + wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_NAME, + "GEM names are not supported, " + "use a PRIME fd instead"); } -static void create_planar_buffer(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - uint32_t name, int32_t width, int32_t height, - uint32_t format, - int32_t offset0, int32_t stride0, - int32_t offset1, int32_t stride1, - int32_t offset2, int32_t stride2) +static void +create_planar_buffer(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + uint32_t name, int32_t width, int32_t height, + uint32_t format, + int32_t offset0, int32_t stride0, + int32_t offset1, int32_t stride1, + int32_t offset2, int32_t stride2) { - wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT, - "planar buffers are not supported\n"); + wl_resource_post_error(resource, WL_DRM_ERROR_INVALID_FORMAT, + "planar buffers are not supported\n"); } -static void create_prime_buffer(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - int32_t fd, int32_t width, int32_t height, - uint32_t format, - int32_t offset0, int32_t stride0, - int32_t offset1, int32_t stride1, - int32_t offset2, int32_t stride2) +static void +create_prime_buffer(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + int32_t fd, int32_t width, int32_t height, + uint32_t format, + int32_t offset0, int32_t stride0, + int32_t offset1, int32_t stride1, + int32_t offset2, int32_t stride2) { - struct wld_buffer * buffer; - struct wl_resource * buffer_resource; - union wld_object object = { .i = fd }; + struct wld_buffer *buffer; + struct wl_resource *buffer_resource; + union wld_object object = {.i = fd }; - buffer = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_PRIME_FD, - object, width, height, format, stride0); - close(fd); + buffer = wld_import_buffer(swc.drm->context, WLD_DRM_OBJECT_PRIME_FD, + object, width, height, format, stride0); + close(fd); - if (!buffer) - goto error0; + if (!buffer) + goto error0; - buffer_resource = wayland_buffer_create_resource - (client, wl_resource_get_version(resource), id, buffer); + buffer_resource = wayland_buffer_create_resource(client, wl_resource_get_version(resource), id, buffer); - if (!buffer_resource) - goto error1; + if (!buffer_resource) + goto error1; - return; + return; - error1: - wld_buffer_unreference(buffer); - error0: - wl_resource_post_no_memory(resource); +error1: + wld_buffer_unreference(buffer); +error0: + wl_resource_post_no_memory(resource); } static const struct wl_drm_interface drm_implementation = { - .authenticate = &authenticate, - .create_buffer = &create_buffer, - .create_planar_buffer = &create_planar_buffer, - .create_prime_buffer = &create_prime_buffer + .authenticate = &authenticate, + .create_buffer = &create_buffer, + .create_planar_buffer = &create_planar_buffer, + .create_prime_buffer = &create_prime_buffer }; -static int select_card(const struct dirent * entry) +static int +select_card(const struct dirent *entry) { - unsigned num; + unsigned num; - return sscanf(entry->d_name, "card%u", &num) == 1; + return sscanf(entry->d_name, "card%u", &num) == 1; } -static bool find_primary_drm_device(char * path, size_t size) +static bool +find_primary_drm_device(char *path, size_t size) { - struct dirent ** cards, * card = NULL; - int num_cards, ret; - unsigned index; - FILE * file; - unsigned char boot_vga; + struct dirent **cards, *card = NULL; + int num_cards, ret; + unsigned index; + FILE *file; + unsigned char boot_vga; - num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort); + num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort); - if (num_cards == -1) - return false; + if (num_cards == -1) + return false; - for (index = 0; index < num_cards; ++index) - { - snprintf(path, size, "/sys/class/drm/%s/device/boot_vga", - cards[index]->d_name); - - if ((file = fopen(path, "r"))) - { - ret = fscanf(file, "%hhu", &boot_vga); - fclose(file); - - if (ret == 1 && boot_vga) - { - free(card); - card = cards[index]; - DEBUG("/dev/dri/%s is the primary GPU\n", card->d_name); - break; - } - } - - if (!card) - card = cards[index]; - else - free(cards[index]); - } - - free(cards); - - if (!card) - return false; - - if (snprintf(path, size, "/dev/dri/%s", card->d_name) >= size) - return false; - - free(card); - return true; + for (index = 0; index < num_cards; ++index) { + snprintf(path, size, "/sys/class/drm/%s/device/boot_vga", + cards[index]->d_name); + + if ((file = fopen(path, "r"))) { + ret = fscanf(file, "%hhu", &boot_vga); + fclose(file); + + if (ret == 1 && boot_vga) { + free(card); + card = cards[index]; + DEBUG("/dev/dri/%s is the primary GPU\n", card->d_name); + break; + } + } + + if (!card) + card = cards[index]; + else + free(cards[index]); + } + + free(cards); + + if (!card) + return false; + + if (snprintf(path, size, "/dev/dri/%s", card->d_name) >= size) + return false; + + free(card); + return true; } -static bool find_available_crtc(drmModeRes * resources, - drmModeConnector * connector, - uint32_t taken_crtcs, uint32_t * crtc) +static bool +find_available_crtc(drmModeRes *resources, + drmModeConnector *connector, + uint32_t taken_crtcs, uint32_t *crtc) { - uint32_t encoder_index, crtc_index; - uint32_t possible_crtcs; - drmModeEncoder * encoder; - - for (encoder_index = 0; - encoder_index < connector->count_encoders; - ++encoder_index) - { - encoder = drmModeGetEncoder(swc.drm->fd, - connector->encoders[encoder_index]); - possible_crtcs = encoder->possible_crtcs; - drmModeFreeEncoder(encoder); - - for (crtc_index = 0; crtc_index < resources->count_crtcs; ++crtc_index) - { - if ((possible_crtcs & (1 << crtc_index)) - && !(taken_crtcs & (1 << crtc_index))) - { - *crtc = crtc_index; - return true; - } - } - } - - return false; + uint32_t encoder_index, crtc_index; + uint32_t possible_crtcs; + drmModeEncoder *encoder; + + for (encoder_index = 0; + encoder_index < connector->count_encoders; + ++encoder_index) { + encoder = drmModeGetEncoder(swc.drm->fd, + connector->encoders[encoder_index]); + possible_crtcs = encoder->possible_crtcs; + drmModeFreeEncoder(encoder); + + for (crtc_index = 0; crtc_index < resources->count_crtcs; ++crtc_index) { + if ((possible_crtcs & (1 << crtc_index)) + && !(taken_crtcs & (1 << crtc_index))) { + *crtc = crtc_index; + return true; + } + } + } + + return false; } -static bool find_available_id(uint32_t * id) +static bool +find_available_id(uint32_t *id) { - uint32_t index = __builtin_ffsl(~drm.taken_ids); + uint32_t index = __builtin_ffsl(~drm.taken_ids); - if (index == 0) - return false; + if (index == 0) + return false; - *id = index - 1; - return true; + *id = index - 1; + return true; } -static void handle_vblank(int fd, unsigned int sequence, unsigned int sec, - unsigned int usec, void * data) +static void +handle_vblank(int fd, unsigned int sequence, unsigned int sec, + unsigned int usec, void *data) { } -static void handle_page_flip(int fd, unsigned int sequence, unsigned int sec, - unsigned int usec, void * data) +static void +handle_page_flip(int fd, unsigned int sequence, unsigned int sec, + unsigned int usec, void *data) { - struct drm_handler * handler = data; + struct drm_handler *handler = data; - handler->page_flip(handler, sec * 1000 + usec / 1000); + handler->page_flip(handler, sec * 1000 + usec / 1000); } static drmEventContext event_context = { - .version = DRM_EVENT_CONTEXT_VERSION, - .vblank_handler = &handle_vblank, - .page_flip_handler = &handle_page_flip + .version = DRM_EVENT_CONTEXT_VERSION, + .vblank_handler = &handle_vblank, + .page_flip_handler = &handle_page_flip }; -static int handle_data(int fd, uint32_t mask, void * data) +static int +handle_data(int fd, uint32_t mask, void *data) { - drmHandleEvent(fd, &event_context); + drmHandleEvent(fd, &event_context); - return 1; + return 1; } -static void bind_drm(struct wl_client * client, void * data, uint32_t version, - uint32_t id) +static void +bind_drm(struct wl_client *client, void *data, uint32_t version, + uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 2) - version = 2; + if (version > 2) + version = 2; - resource = wl_resource_create(client, &wl_drm_interface, version, id); - wl_resource_set_implementation(resource, &drm_implementation, NULL, NULL); + resource = wl_resource_create(client, &wl_drm_interface, version, id); + wl_resource_set_implementation(resource, &drm_implementation, NULL, NULL); - if (version >= 2) - wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME); + if (version >= 2) + wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME); - wl_drm_send_device(resource, drm.path); - wl_drm_send_format(resource, WL_DRM_FORMAT_XRGB8888); - wl_drm_send_format(resource, WL_DRM_FORMAT_ARGB8888); + wl_drm_send_device(resource, drm.path); + wl_drm_send_format(resource, WL_DRM_FORMAT_XRGB8888); + wl_drm_send_format(resource, WL_DRM_FORMAT_ARGB8888); } -bool drm_initialize(void) +bool +drm_initialize(void) { - struct stat master, render; - - if (!find_primary_drm_device(drm.path, sizeof drm.path)) - { - ERROR("Could not find DRM device\n"); - goto error0; - } - - drm.taken_ids = 0; - swc.drm->fd = launch_open_device(drm.path, O_RDWR | O_CLOEXEC); - - if (swc.drm->fd == -1) - { - ERROR("Could not open DRM device at %s\n", drm.path); - goto error0; - } - - if (fstat(swc.drm->fd, &master) != 0) - { - ERROR("Could not fstat DRM FD: %s\n", strerror(errno)); - goto error1; - } - - if (snprintf(drm.path, sizeof drm.path, "/dev/dri/renderD%d", - minor(master.st_rdev) + 0x80) >= sizeof drm.path) - { - ERROR("Render node path is too long"); - goto error1; - } - - if (stat(drm.path, &render) != 0) - { - ERROR("Could not stat render node for primary DRM device: %s\n", - strerror(errno)); - goto error1; - } - - if (master.st_mode != render.st_mode - || minor(master.st_rdev) + 0x80 != minor(render.st_rdev)) - { - ERROR("Render node does not have expected mode or minor number\n"); - goto error1; - } - - if (!(swc.drm->context = wld_drm_create_context(swc.drm->fd))) - { - ERROR("Could not create WLD DRM context\n"); - goto error1; - } - - if (!(swc.drm->renderer = wld_create_renderer(swc.drm->context))) - { - ERROR("Could not create WLD DRM renderer\n"); - goto error2; - } - - drm.event_source = wl_event_loop_add_fd - (swc.event_loop, swc.drm->fd, WL_EVENT_READABLE, &handle_data, NULL); - - if (!drm.event_source) - { - ERROR("Could not create DRM event source\n"); - goto error3; - } - - if (!wld_drm_is_dumb(swc.drm->context)) - { - drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, - NULL, &bind_drm); - - if (!drm.global) - { - ERROR("Could not create wl_drm global\n"); - goto error4; - } - } - - return true; - - error4: - wl_event_source_remove(drm.event_source); - error3: - wld_destroy_renderer(swc.drm->renderer); - error2: - wld_destroy_context(swc.drm->context); - error1: - close(swc.drm->fd); - error0: - return false; + struct stat master, render; + + if (!find_primary_drm_device(drm.path, sizeof drm.path)) { + ERROR("Could not find DRM device\n"); + goto error0; + } + + drm.taken_ids = 0; + swc.drm->fd = launch_open_device(drm.path, O_RDWR | O_CLOEXEC); + + if (swc.drm->fd == -1) { + ERROR("Could not open DRM device at %s\n", drm.path); + goto error0; + } + + if (fstat(swc.drm->fd, &master) != 0) { + ERROR("Could not fstat DRM FD: %s\n", strerror(errno)); + goto error1; + } + + if (snprintf(drm.path, sizeof drm.path, "/dev/dri/renderD%d", + minor(master.st_rdev) + 0x80) >= sizeof drm.path) { + ERROR("Render node path is too long"); + goto error1; + } + + if (stat(drm.path, &render) != 0) { + ERROR("Could not stat render node for primary DRM device: %s\n", + strerror(errno)); + goto error1; + } + + if (master.st_mode != render.st_mode + || minor(master.st_rdev) + 0x80 != minor(render.st_rdev)) { + ERROR("Render node does not have expected mode or minor number\n"); + goto error1; + } + + if (!(swc.drm->context = wld_drm_create_context(swc.drm->fd))) { + ERROR("Could not create WLD DRM context\n"); + goto error1; + } + + if (!(swc.drm->renderer = wld_create_renderer(swc.drm->context))) { + ERROR("Could not create WLD DRM renderer\n"); + goto error2; + } + + drm.event_source = wl_event_loop_add_fd(swc.event_loop, swc.drm->fd, WL_EVENT_READABLE, &handle_data, NULL); + + if (!drm.event_source) { + ERROR("Could not create DRM event source\n"); + goto error3; + } + + if (!wld_drm_is_dumb(swc.drm->context)) { + drm.global = wl_global_create(swc.display, &wl_drm_interface, 2, + NULL, &bind_drm); + + if (!drm.global) { + ERROR("Could not create wl_drm global\n"); + goto error4; + } + } + + return true; + +error4: + wl_event_source_remove(drm.event_source); +error3: + wld_destroy_renderer(swc.drm->renderer); +error2: + wld_destroy_context(swc.drm->context); +error1: + close(swc.drm->fd); +error0: + return false; } -void drm_finalize(void) +void +drm_finalize(void) { - if (drm.global) - wl_global_destroy(drm.global); - wl_event_source_remove(drm.event_source); - wld_destroy_renderer(swc.drm->renderer); - wld_destroy_context(swc.drm->context); - close(swc.drm->fd); + if (drm.global) + wl_global_destroy(drm.global); + wl_event_source_remove(drm.event_source); + wld_destroy_renderer(swc.drm->renderer); + wld_destroy_context(swc.drm->context); + close(swc.drm->fd); } -bool drm_create_screens(struct wl_list * screens) +bool +drm_create_screens(struct wl_list *screens) { - drmModeRes * resources; - drmModeConnector * connector; - uint32_t index; - struct output * output; - uint32_t taken_crtcs = 0; - - if (!(resources = drmModeGetResources(swc.drm->fd))) - { - ERROR("Could not get DRM resources\n"); - return false; - } - - for (index = 0; index < resources->count_connectors; - ++index, drmModeFreeConnector(connector)) - { - connector = drmModeGetConnector(swc.drm->fd, - resources->connectors[index]); - - if (connector->connection == DRM_MODE_CONNECTED) - { - uint32_t crtc_index; - uint32_t id; - - if (!find_available_crtc(resources, connector, taken_crtcs, - &crtc_index)) - { - WARNING("Could not find CRTC for connector %u\n", index); - continue; - } - - if (!find_available_id(&id)) - { - WARNING("No more available output IDs\n"); - drmModeFreeConnector(connector); - break; - } - - if (!(output = output_new(connector))) - continue; - - output->screen = screen_new(resources->crtcs[crtc_index], output); - output->screen->id = id; - - taken_crtcs |= 1 << crtc_index; - drm.taken_ids |= 1 << id; - - wl_list_insert(screens, &output->screen->link); - } - } - - drmModeFreeResources(resources); - - return true; + drmModeRes *resources; + drmModeConnector *connector; + uint32_t index; + struct output *output; + uint32_t taken_crtcs = 0; + + if (!(resources = drmModeGetResources(swc.drm->fd))) { + ERROR("Could not get DRM resources\n"); + return false; + } + + for (index = 0; index < resources->count_connectors; + ++index, drmModeFreeConnector(connector)) { + connector = drmModeGetConnector(swc.drm->fd, + resources->connectors[index]); + + if (connector->connection == DRM_MODE_CONNECTED) { + uint32_t crtc_index; + uint32_t id; + + if (!find_available_crtc(resources, connector, taken_crtcs, + &crtc_index)) { + WARNING("Could not find CRTC for connector %u\n", index); + continue; + } + + if (!find_available_id(&id)) { + WARNING("No more available output IDs\n"); + drmModeFreeConnector(connector); + break; + } + + if (!(output = output_new(connector))) + continue; + + output->screen = screen_new(resources->crtcs[crtc_index], output); + output->screen->id = id; + + taken_crtcs |= 1 << crtc_index; + drm.taken_ids |= 1 << id; + + wl_list_insert(screens, &output->screen->link); + } + } + + drmModeFreeResources(resources); + + return true; } - diff --git a/libswc/drm.h b/libswc/drm.h @@ -5,22 +5,19 @@ #include <stdint.h> #include <wayland-server.h> -struct drm_handler -{ - void (* page_flip)(struct drm_handler * handler, uint32_t time); +struct drm_handler { + void (*page_flip)(struct drm_handler *handler, uint32_t time); }; -struct swc_drm -{ - int fd; - struct wld_context * context; - struct wld_renderer * renderer; +struct swc_drm { + int fd; + struct wld_context *context; + struct wld_renderer *renderer; }; bool drm_initialize(void); void drm_finalize(void); -bool drm_create_screens(struct wl_list * screens); +bool drm_create_screens(struct wl_list *screens); #endif - diff --git a/libswc/evdev_device.c b/libswc/evdev_device.c @@ -43,279 +43,271 @@ #define AXIS_STEP_DISTANCE 10 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) -static inline uint32_t timeval_to_msec(struct timeval * time) +static inline uint32_t +timeval_to_msec(struct timeval *time) { - return time->tv_sec * 1000 + time->tv_usec / 1000; + return time->tv_sec * 1000 + time->tv_usec / 1000; } -static void handle_key_event(struct evdev_device * device, - struct input_event * input_event) +static void +handle_key_event(struct evdev_device *device, + struct input_event *input_event) { - uint32_t time = timeval_to_msec(&input_event->time); - uint32_t state; - - if ((input_event->code >= BTN_MISC && input_event->code <= BTN_GEAR_UP) - || input_event->code >= BTN_TRIGGER_HAPPY) - { - state = input_event->value ? WL_POINTER_BUTTON_STATE_PRESSED - : WL_POINTER_BUTTON_STATE_RELEASED; - device->handler->button(time, input_event->code, state); - } - else - { - state = input_event->value ? WL_KEYBOARD_KEY_STATE_PRESSED - : WL_KEYBOARD_KEY_STATE_RELEASED; - device->handler->key(time, input_event->code, state); - } + uint32_t time = timeval_to_msec(&input_event->time); + uint32_t state; + + if ((input_event->code >= BTN_MISC && input_event->code <= BTN_GEAR_UP) + || input_event->code >= BTN_TRIGGER_HAPPY) { + state = input_event->value ? WL_POINTER_BUTTON_STATE_PRESSED + : WL_POINTER_BUTTON_STATE_RELEASED; + device->handler->button(time, input_event->code, state); + } else { + state = input_event->value ? WL_KEYBOARD_KEY_STATE_PRESSED + : WL_KEYBOARD_KEY_STATE_RELEASED; + device->handler->key(time, input_event->code, state); + } } -static void handle_rel_event(struct evdev_device * device, - struct input_event * input_event) +static void +handle_rel_event(struct evdev_device *device, + struct input_event *input_event) { - uint32_t time = timeval_to_msec(&input_event->time); - uint32_t axis, amount; - - switch (input_event->code) - { - case REL_X: - device->motion.rel.dx += input_event->value; - device->motion.rel.pending = true; - return; - case REL_Y: - device->motion.rel.dy += input_event->value; - device->motion.rel.pending = true; - return; - case REL_WHEEL: - axis = WL_POINTER_AXIS_VERTICAL_SCROLL; - amount = -AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); - break; - case REL_HWHEEL: - axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; - amount = AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); - break; - default: return; - } - - device->handler->axis(time, axis, amount); + uint32_t time = timeval_to_msec(&input_event->time); + uint32_t axis, amount; + + switch (input_event->code) { + case REL_X: + device->motion.rel.dx += input_event->value; + device->motion.rel.pending = true; + return; + case REL_Y: + device->motion.rel.dy += input_event->value; + device->motion.rel.pending = true; + return; + case REL_WHEEL: + axis = WL_POINTER_AXIS_VERTICAL_SCROLL; + amount = -AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); + break; + case REL_HWHEEL: + axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; + amount = AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); + break; + default: + return; + } + + device->handler->axis(time, axis, amount); } -static void handle_abs_event(struct evdev_device * device, - struct input_event * input_event) +static void +handle_abs_event(struct evdev_device *device, + struct input_event *input_event) { } -static void (* event_handlers[])(struct evdev_device * device, - struct input_event * input_event) = { - [EV_KEY] = &handle_key_event, - [EV_REL] = &handle_rel_event, - [EV_ABS] = &handle_abs_event +static void (*event_handlers[])(struct evdev_device *device, + struct input_event *input_event) = { + [EV_KEY] = &handle_key_event, + [EV_REL] = &handle_rel_event, + [EV_ABS] = &handle_abs_event }; -static bool is_motion_event(struct input_event * event) +static bool +is_motion_event(struct input_event *event) { - return (event->type == EV_REL && (event->code == REL_X || event->code == REL_Y)) - || (event->type == EV_ABS && (event->code == ABS_X || event->code == ABS_Y)); + return (event->type == EV_REL && (event->code == REL_X || event->code == REL_Y)) + || (event->type == EV_ABS && (event->code == ABS_X || event->code == ABS_Y)); } -static void handle_motion_events(struct evdev_device * device, - uint32_t time) +static void +handle_motion_events(struct evdev_device *device, + uint32_t time) { - if (device->motion.rel.pending) - { - wl_fixed_t dx = wl_fixed_from_int(device->motion.rel.dx); - wl_fixed_t dy = wl_fixed_from_int(device->motion.rel.dy); + if (device->motion.rel.pending) { + wl_fixed_t dx = wl_fixed_from_int(device->motion.rel.dx); + wl_fixed_t dy = wl_fixed_from_int(device->motion.rel.dy); - device->handler->relative_motion(time, dx, dy); + device->handler->relative_motion(time, dx, dy); - device->motion.rel.pending = false; - device->motion.rel.dx = 0; - device->motion.rel.dy = 0; - } + device->motion.rel.pending = false; + device->motion.rel.dx = 0; + device->motion.rel.dy = 0; + } } -static void handle_event(struct evdev_device * device, - struct input_event * event) +static void +handle_event(struct evdev_device *device, + struct input_event *event) { - if (!is_motion_event(event)) - handle_motion_events(device, timeval_to_msec(&event->time)); - - if (event->type < ARRAY_SIZE(event_handlers) - && event_handlers[event->type]) - { - event_handlers[event->type](device, event); - } + if (!is_motion_event(event)) + handle_motion_events(device, timeval_to_msec(&event->time)); + + if (event->type < ARRAY_SIZE(event_handlers) + && event_handlers[event->type]) { + event_handlers[event->type](device, event); + } } -static void close_device(struct evdev_device * device) +static void +close_device(struct evdev_device *device) { - wl_event_source_remove(device->source); - close(device->fd); - device->source = NULL; - device->fd = -1; + wl_event_source_remove(device->source); + close(device->fd); + device->source = NULL; + device->fd = -1; } -static int handle_data(int fd, uint32_t mask, void * data) +static int +handle_data(int fd, uint32_t mask, void *data) { - struct evdev_device * device = data; - struct input_event event; - unsigned flags = device->needs_sync ? LIBEVDEV_READ_FLAG_FORCE_SYNC - : LIBEVDEV_READ_FLAG_NORMAL; - int ret; - - device->needs_sync = false; - - while (true) - { - ret = libevdev_next_event(device->dev, flags, &event); - - if (ret < 0) - goto done; - else if (ret == LIBEVDEV_READ_STATUS_SUCCESS) - handle_event(device, &event); - else - { - while (ret == LIBEVDEV_READ_STATUS_SYNC) - { - ret = libevdev_next_event(device->dev, LIBEVDEV_READ_FLAG_SYNC, - &event); - - if (ret < 0) - goto done; - - handle_event(device, &event); - } - } - } - - done: - if (ret == -ENODEV) - close_device(device); - - handle_motion_events(device, timeval_to_msec(&event.time)); - - return 1; + struct evdev_device *device = data; + struct input_event event; + unsigned flags = device->needs_sync ? LIBEVDEV_READ_FLAG_FORCE_SYNC + : LIBEVDEV_READ_FLAG_NORMAL; + int ret; + + device->needs_sync = false; + + while (true) { + ret = libevdev_next_event(device->dev, flags, &event); + + if (ret < 0) + goto done; + else if (ret == LIBEVDEV_READ_STATUS_SUCCESS) + handle_event(device, &event); + else { + while (ret == LIBEVDEV_READ_STATUS_SYNC) { + ret = libevdev_next_event(device->dev, LIBEVDEV_READ_FLAG_SYNC, + &event); + + if (ret < 0) + goto done; + + handle_event(device, &event); + } + } + } + +done: + if (ret == -ENODEV) + close_device(device); + + handle_motion_events(device, timeval_to_msec(&event.time)); + + return 1; } -struct evdev_device * evdev_device_new - (const char * path, const struct evdev_device_handler * handler) +struct evdev_device * +evdev_device_new(const char *path, const struct evdev_device_handler *handler) { - struct evdev_device * device; - - if (!(device = malloc(sizeof *device))) - goto error0; - - device->fd = launch_open_device(path, O_RDWR | O_NONBLOCK | O_CLOEXEC); - - if (device->fd == -1) - { - ERROR("Failed to open input device at %s\n", path); - goto error1; - } - - if (!(device->path = strdup(path))) - goto error2; - - if (libevdev_new_from_fd(device->fd, &device->dev) != 0) - { - ERROR("Failed to create libevdev device\n"); - goto error3; - } - - device->source = wl_event_loop_add_fd - (swc.event_loop, device->fd, WL_EVENT_READABLE, handle_data, device); - - if (!device->source) - { - ERROR("Failed to add event source\n"); - goto error4; - } - - DEBUG("Adding device %s\n", libevdev_get_name(device->dev)); - - device->needs_sync = false; - device->handler = handler; - device->capabilities = 0; - memset(&device->motion, 0, sizeof device->motion); - - if (libevdev_has_event_code(device->dev, EV_KEY, KEY_ENTER)) - { - device->capabilities |= WL_SEAT_CAPABILITY_KEYBOARD; - DEBUG("\tThis device is a keyboard\n"); - } - - if (libevdev_has_event_code(device->dev, EV_REL, REL_X) - && libevdev_has_event_code(device->dev, EV_REL, REL_Y) - && libevdev_has_event_code(device->dev, EV_KEY, BTN_MOUSE)) - { - device->capabilities |= WL_SEAT_CAPABILITY_POINTER; - DEBUG("\tThis device is a pointer\n"); - } - - /* XXX: touch devices */ - - return device; - - error4: - libevdev_free(device->dev); - error3: - free(device->path); - error2: - close(device->fd); - error1: - free(device); - error0: - return NULL; + struct evdev_device *device; + + if (!(device = malloc(sizeof *device))) + goto error0; + + device->fd = launch_open_device(path, O_RDWR | O_NONBLOCK | O_CLOEXEC); + + if (device->fd == -1) { + ERROR("Failed to open input device at %s\n", path); + goto error1; + } + + if (!(device->path = strdup(path))) + goto error2; + + if (libevdev_new_from_fd(device->fd, &device->dev) != 0) { + ERROR("Failed to create libevdev device\n"); + goto error3; + } + + device->source = wl_event_loop_add_fd(swc.event_loop, device->fd, WL_EVENT_READABLE, handle_data, device); + + if (!device->source) { + ERROR("Failed to add event source\n"); + goto error4; + } + + DEBUG("Adding device %s\n", libevdev_get_name(device->dev)); + + device->needs_sync = false; + device->handler = handler; + device->capabilities = 0; + memset(&device->motion, 0, sizeof device->motion); + + if (libevdev_has_event_code(device->dev, EV_KEY, KEY_ENTER)) { + device->capabilities |= WL_SEAT_CAPABILITY_KEYBOARD; + DEBUG("\tThis device is a keyboard\n"); + } + + if (libevdev_has_event_code(device->dev, EV_REL, REL_X) + && libevdev_has_event_code(device->dev, EV_REL, REL_Y) + && libevdev_has_event_code(device->dev, EV_KEY, BTN_MOUSE)) { + device->capabilities |= WL_SEAT_CAPABILITY_POINTER; + DEBUG("\tThis device is a pointer\n"); + } + + /* XXX: touch devices */ + + return device; + +error4: + libevdev_free(device->dev); +error3: + free(device->path); +error2: + close(device->fd); +error1: + free(device); +error0: + return NULL; } -void evdev_device_destroy(struct evdev_device * device) +void +evdev_device_destroy(struct evdev_device *device) { - if (device->source) - close_device(device); + if (device->source) + close_device(device); - libevdev_free(device->dev); - free(device->path); - free(device); + libevdev_free(device->dev); + free(device->path); + free(device); } -bool evdev_device_reopen(struct evdev_device * device) +bool +evdev_device_reopen(struct evdev_device *device) { - if (device->source) - close_device(device); - - device->fd = launch_open_device(device->path, - O_RDWR | O_NONBLOCK | O_CLOEXEC); - - if (device->fd == -1) - { - WARNING("Failed to reopen input device at %s: %s\n", - device->path, strerror(errno)); - goto error0; - } - - if (libevdev_change_fd(device->dev, device->fd) == -1) - { - ERROR("Failed to update libevdev fd\n"); - goto error1; - } - - /* According to libevdev documentation, after changing the fd for the + if (device->source) + close_device(device); + + device->fd = launch_open_device(device->path, + O_RDWR | O_NONBLOCK | O_CLOEXEC); + + if (device->fd == -1) { + WARNING("Failed to reopen input device at %s: %s\n", + device->path, strerror(errno)); + goto error0; + } + + if (libevdev_change_fd(device->dev, device->fd) == -1) { + ERROR("Failed to update libevdev fd\n"); + goto error1; + } + + /* According to libevdev documentation, after changing the fd for the * device, you should force a sync to bring it's state up to date. */ - device->needs_sync = true; - device->source = wl_event_loop_add_fd - (swc.event_loop, device->fd, WL_EVENT_READABLE, handle_data, device); - - if (!device->source) - { - ERROR("Failed to create event source\n"); - goto error1; - } - - return true; - - error1: - close(device->fd); - device->fd = -1; - error0: - return false; -} + device->needs_sync = true; + device->source = wl_event_loop_add_fd(swc.event_loop, device->fd, WL_EVENT_READABLE, handle_data, device); + + if (!device->source) { + ERROR("Failed to create event source\n"); + goto error1; + } + return true; + +error1: + close(device->fd); + device->fd = -1; +error0: + return false; +} diff --git a/libswc/evdev_device.h b/libswc/evdev_device.h @@ -35,55 +35,51 @@ struct evdev_device; struct wl_event_loop; -struct evdev_device_handler -{ - void (* key)(uint32_t time, uint32_t key, uint32_t state); - void (* button)(uint32_t time, uint32_t key, uint32_t state); - void (* axis)(uint32_t time, uint32_t axis, wl_fixed_t amount); - void (* relative_motion)(uint32_t time, wl_fixed_t dx, wl_fixed_t dy); +struct evdev_device_handler { + void (*key)(uint32_t time, uint32_t key, uint32_t state); + void (*button)(uint32_t time, uint32_t key, uint32_t state); + void (*axis)(uint32_t time, uint32_t axis, wl_fixed_t amount); + void (*relative_motion)(uint32_t time, wl_fixed_t dx, wl_fixed_t dy); }; -struct evdev_device -{ - char * path; - int fd; - struct libevdev * dev; - bool needs_sync; - - const struct evdev_device_handler * handler; - - struct - { - struct - { - struct - { - struct input_absinfo x, y; - } info; - - int32_t x, y; - bool pending; - } abs; - - struct - { - int32_t dx, dy; - bool pending; - } rel; - } motion; - - uint32_t capabilities; - - struct wl_event_source * source; - struct wl_list link; +struct evdev_device { + char *path; + int fd; + struct libevdev *dev; + bool needs_sync; + + const struct evdev_device_handler *handler; + + struct + { + struct + { + struct + { + struct input_absinfo x, y; + } info; + + int32_t x, y; + bool pending; + } abs; + + struct + { + int32_t dx, dy; + bool pending; + } rel; + } motion; + + uint32_t capabilities; + + struct wl_event_source *source; + struct wl_list link; }; -struct evdev_device * evdev_device_new - (const char * path, const struct evdev_device_handler * handler); +struct evdev_device *evdev_device_new(const char *path, const struct evdev_device_handler *handler); -void evdev_device_destroy(struct evdev_device * device); +void evdev_device_destroy(struct evdev_device *device); -bool evdev_device_reopen(struct evdev_device * device); +bool evdev_device_reopen(struct evdev_device *device); #endif - diff --git a/libswc/event.h b/libswc/event.h @@ -10,32 +10,31 @@ * An event is the data passed to the listeners of the event_signals of various * objects. */ -struct event -{ - /** +struct event { + /** * The type of event that was sent. * * The meaning of this field depends on the type of object containing the * event_signal that passed this event. */ - uint32_t type; + uint32_t type; - /** + /** * Data specific to the event type. * * Unless explicitly stated in the description of the event type, this * value is undefined. */ - void * data; + void *data; }; -static inline void send_event(struct wl_signal * signal, uint32_t type, - void * event_data) +static inline void +send_event(struct wl_signal *signal, uint32_t type, + void *event_data) { - struct event event = { .type = type, .data = event_data }; + struct event event = {.type = type, .data = event_data }; - wl_signal_emit(signal, &event); + wl_signal_emit(signal, &event); } #endif - diff --git a/libswc/framebuffer_plane.c b/libswc/framebuffer_plane.c @@ -34,210 +34,200 @@ #include <xf86drm.h> #include <xf86drmMode.h> -enum -{ - WLD_USER_OBJECT_FRAMEBUFFER = WLD_USER_ID +enum { + WLD_USER_OBJECT_FRAMEBUFFER = WLD_USER_ID }; -struct framebuffer -{ - struct wld_exporter exporter; - struct wld_destructor destructor; - uint32_t id; +struct framebuffer { + struct wld_exporter exporter; + struct wld_destructor destructor; + uint32_t id; }; -static bool framebuffer_export(struct wld_exporter * exporter, - struct wld_buffer * buffer, - uint32_t type, union wld_object * object) +static bool +framebuffer_export(struct wld_exporter *exporter, + struct wld_buffer *buffer, + uint32_t type, union wld_object *object) { - struct framebuffer * framebuffer - = wl_container_of(exporter, framebuffer, exporter); - - switch (type) - { - case WLD_USER_OBJECT_FRAMEBUFFER: - object->u32 = framebuffer->id; - break; - default: return false; - } - - return true; + struct framebuffer *framebuffer = wl_container_of(exporter, framebuffer, exporter); + + switch (type) { + case WLD_USER_OBJECT_FRAMEBUFFER: + object->u32 = framebuffer->id; + break; + default: + return false; + } + + return true; } -static void framebuffer_destroy(struct wld_destructor * destructor) +static void +framebuffer_destroy(struct wld_destructor *destructor) { - struct framebuffer * framebuffer - = wl_container_of(destructor, framebuffer, destructor); + struct framebuffer *framebuffer = wl_container_of(destructor, framebuffer, destructor); - drmModeRmFB(swc.drm->fd, framebuffer->id); - free(framebuffer); + drmModeRmFB(swc.drm->fd, framebuffer->id); + free(framebuffer); } -static bool update(struct view * view) +static bool +update(struct view *view) { - return true; + return true; } -static void send_frame(void * data) +static void +send_frame(void *data) { - struct framebuffer_plane * plane = data; + struct framebuffer_plane *plane = data; - view_frame(&plane->view, get_time()); + view_frame(&plane->view, get_time()); } -static int attach(struct view * view, struct wld_buffer * buffer) +static int +attach(struct view *view, struct wld_buffer *buffer) { - struct framebuffer_plane * plane = wl_container_of(view, plane, view); - union wld_object object; - int ret; - - if (!wld_export(buffer, WLD_USER_OBJECT_FRAMEBUFFER, &object)) - { - struct framebuffer * framebuffer; - - if (!wld_export(buffer, WLD_DRM_OBJECT_HANDLE, &object)) - { - ERROR("Could not get buffer handle\n"); - return -EINVAL; - } - - if (!(framebuffer = malloc(sizeof *framebuffer))) - return -ENOMEM; - - ret = drmModeAddFB(swc.drm->fd, buffer->width, buffer->height, 24, 32, - buffer->pitch, object.u32, &framebuffer->id); - - if (ret < 0) - { - free(framebuffer); - return ret; - } - - framebuffer->exporter.export = &framebuffer_export; - wld_buffer_add_exporter(buffer, &framebuffer->exporter); - framebuffer->destructor.destroy = &framebuffer_destroy; - wld_buffer_add_destructor(buffer, &framebuffer->destructor); - - object.u32 = framebuffer->id; - } - - if (plane->need_modeset) - { - ret = drmModeSetCrtc(swc.drm->fd, plane->crtc, object.u32, 0, 0, - plane->connectors.data, plane->connectors.size / 4, - &plane->mode.info); - - if (ret == 0) - { - wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); - plane->need_modeset = false; - } - else - { - ERROR("Could not set CRTC to next framebuffer: %s\n", - strerror(-ret)); - return ret; - } - } - else - { - ret = drmModePageFlip(swc.drm->fd, plane->crtc, object.u32, - DRM_MODE_PAGE_FLIP_EVENT, &plane->drm_handler); - - if (ret < 0) - { - ERROR("Page flip failed: %s\n", strerror(errno)); - return ret; - } - } - - return 0; + struct framebuffer_plane *plane = wl_container_of(view, plane, view); + union wld_object object; + int ret; + + if (!wld_export(buffer, WLD_USER_OBJECT_FRAMEBUFFER, &object)) { + struct framebuffer *framebuffer; + + if (!wld_export(buffer, WLD_DRM_OBJECT_HANDLE, &object)) { + ERROR("Could not get buffer handle\n"); + return -EINVAL; + } + + if (!(framebuffer = malloc(sizeof *framebuffer))) + return -ENOMEM; + + ret = drmModeAddFB(swc.drm->fd, buffer->width, buffer->height, 24, 32, + buffer->pitch, object.u32, &framebuffer->id); + + if (ret < 0) { + free(framebuffer); + return ret; + } + + framebuffer->exporter.export = &framebuffer_export; + wld_buffer_add_exporter(buffer, &framebuffer->exporter); + framebuffer->destructor.destroy = &framebuffer_destroy; + wld_buffer_add_destructor(buffer, &framebuffer->destructor); + + object.u32 = framebuffer->id; + } + + if (plane->need_modeset) { + ret = drmModeSetCrtc(swc.drm->fd, plane->crtc, object.u32, 0, 0, + plane->connectors.data, plane->connectors.size / 4, + &plane->mode.info); + + if (ret == 0) { + wl_event_loop_add_idle(swc.event_loop, &send_frame, plane); + plane->need_modeset = false; + } else { + ERROR("Could not set CRTC to next framebuffer: %s\n", + strerror(-ret)); + return ret; + } + } else { + ret = drmModePageFlip(swc.drm->fd, plane->crtc, object.u32, + DRM_MODE_PAGE_FLIP_EVENT, &plane->drm_handler); + + if (ret < 0) { + ERROR("Page flip failed: %s\n", strerror(errno)); + return ret; + } + } + + return 0; } -static bool move(struct view * view, int32_t x, int32_t y) +static bool +move(struct view *view, int32_t x, int32_t y) { - view_set_position(view, x, y); + view_set_position(view, x, y); - return true; + return true; } const static struct view_impl view_impl = { - .update = &update, - .attach = &attach, - .move = &move + .update = &update, + .attach = &attach, + .move = &move }; -static void handle_page_flip(struct drm_handler * handler, uint32_t time) +static void +handle_page_flip(struct drm_handler *handler, uint32_t time) { - struct framebuffer_plane * plane - = wl_container_of(handler, plane, drm_handler); + struct framebuffer_plane *plane = wl_container_of(handler, plane, drm_handler); - view_frame(&plane->view, time); + view_frame(&plane->view, time); } -static void handle_swc_event(struct wl_listener * listener, void * data) +static void +handle_swc_event(struct wl_listener *listener, void *data) { - struct event * event = data; - struct framebuffer_plane * plane - = wl_container_of(listener, plane, swc_listener); - - switch (event->type) - { - case SWC_EVENT_ACTIVATED: - plane->need_modeset = true; - break; - } + struct event *event = data; + struct framebuffer_plane *plane = wl_container_of(listener, plane, swc_listener); + + switch (event->type) { + case SWC_EVENT_ACTIVATED: + plane->need_modeset = true; + break; + } } -bool framebuffer_plane_initialize(struct framebuffer_plane * plane, - uint32_t crtc, struct mode * mode, - uint32_t * connectors, - uint32_t num_connectors) +bool +framebuffer_plane_initialize(struct framebuffer_plane *plane, + uint32_t crtc, struct mode *mode, + uint32_t *connectors, + uint32_t num_connectors) { - uint32_t * plane_connectors; - - if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) - { - ERROR("Failed to get CRTC state for CRTC %u: %s\n", - crtc, strerror(errno)); - goto error0; - } - - wl_array_init(&plane->connectors); - plane_connectors = wl_array_add(&plane->connectors, - num_connectors * sizeof connectors[0]); - - if (!plane_connectors) - { - ERROR("Failed to allocate connector array\n"); - goto error1; - } - - memcpy(plane_connectors, connectors, num_connectors * sizeof connectors[0]); - plane->crtc = crtc; - plane->need_modeset = true; - view_initialize(&plane->view, &view_impl); - plane->view.geometry.width = mode->width; - plane->view.geometry.height = mode->height; - plane->drm_handler.page_flip = &handle_page_flip; - plane->swc_listener.notify = &handle_swc_event; - plane->mode = *mode; - wl_signal_add(&swc.event_signal, &plane->swc_listener); - - return true; - - error1: - drmModeFreeCrtc(plane->original_crtc_state); - error0: - return false; + uint32_t *plane_connectors; + + if (!(plane->original_crtc_state = drmModeGetCrtc(swc.drm->fd, crtc))) { + ERROR("Failed to get CRTC state for CRTC %u: %s\n", + crtc, strerror(errno)); + goto error0; + } + + wl_array_init(&plane->connectors); + plane_connectors = wl_array_add(&plane->connectors, + num_connectors * sizeof connectors[0]); + + if (!plane_connectors) { + ERROR("Failed to allocate connector array\n"); + goto error1; + } + + memcpy(plane_connectors, connectors, num_connectors * sizeof connectors[0]); + plane->crtc = crtc; + plane->need_modeset = true; + view_initialize(&plane->view, &view_impl); + plane->view.geometry.width = mode->width; + plane->view.geometry.height = mode->height; + plane->drm_handler.page_flip = &handle_page_flip; + plane->swc_listener.notify = &handle_swc_event; + plane->mode = *mode; + wl_signal_add(&swc.event_signal, &plane->swc_listener); + + return true; + +error1: + drmModeFreeCrtc(plane->original_crtc_state); +error0: + return false; } -void framebuffer_plane_finalize(struct framebuffer_plane * plane) +void +framebuffer_plane_finalize(struct framebuffer_plane *plane) { - wl_array_release(&plane->connectors); - drmModeCrtcPtr crtc = plane->original_crtc_state; - drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, - crtc->x, crtc->y, NULL, 0, &crtc->mode); - drmModeFreeCrtc(crtc); + wl_array_release(&plane->connectors); + drmModeCrtcPtr crtc = plane->original_crtc_state; + drmModeSetCrtc(swc.drm->fd, crtc->crtc_id, crtc->buffer_id, + crtc->x, crtc->y, NULL, 0, &crtc->mode); + drmModeFreeCrtc(crtc); } - diff --git a/libswc/framebuffer_plane.h b/libswc/framebuffer_plane.h @@ -29,24 +29,22 @@ #include "surface.h" #include "view.h" -struct framebuffer_plane -{ - uint32_t crtc; - drmModeCrtcPtr original_crtc_state; - struct mode mode; - struct view view; - struct wl_array connectors; - bool need_modeset; - struct drm_handler drm_handler; - struct wl_listener swc_listener; +struct framebuffer_plane { + uint32_t crtc; + drmModeCrtcPtr original_crtc_state; + struct mode mode; + struct view view; + struct wl_array connectors; + bool need_modeset; + struct drm_handler drm_handler; + struct wl_listener swc_listener; }; -bool framebuffer_plane_initialize(struct framebuffer_plane * plane, - uint32_t crtc, struct mode * mode, - uint32_t * connectors, +bool framebuffer_plane_initialize(struct framebuffer_plane *plane, + uint32_t crtc, struct mode *mode, + uint32_t *connectors, uint32_t num_connectors); -void framebuffer_plane_finalize(struct framebuffer_plane * plane); +void framebuffer_plane_finalize(struct framebuffer_plane *plane); #endif - diff --git a/libswc/input.c b/libswc/input.c @@ -27,122 +27,123 @@ #include "surface.h" #include "util.h" -static inline void focus(struct input_focus * input_focus, - struct compositor_view * view) +static inline void +focus(struct input_focus *input_focus, + struct compositor_view *view) { - struct wl_resource * resource = NULL; + struct wl_resource *resource = NULL; - if (view) - { - struct wl_client * client; + if (view) { + struct wl_client *client; - client = wl_resource_get_client(view->surface->resource); - resource = wl_resource_find_for_client(&input_focus->resources, client); + client = wl_resource_get_client(view->surface->resource); + resource = wl_resource_find_for_client(&input_focus->resources, client); - wl_signal_add(&view->destroy_signal, - &input_focus->view_destroy_listener); + wl_signal_add(&view->destroy_signal, + &input_focus->view_destroy_listener); - if (resource) - input_focus->handler->enter(input_focus->handler, resource, view); - } + if (resource) + input_focus->handler->enter(input_focus->handler, resource, view); + } - input_focus->view = view; - input_focus->resource = resource; + input_focus->view = view; + input_focus->resource = resource; } -static inline void unfocus(struct input_focus * input_focus) +static inline void +unfocus(struct input_focus *input_focus) { - if (input_focus->view) - wl_list_remove(&input_focus->view_destroy_listener.link); - - if (input_focus->resource) - { - input_focus->handler->leave(input_focus->handler, input_focus->resource, - input_focus->view); - } + if (input_focus->view) + wl_list_remove(&input_focus->view_destroy_listener.link); + + if (input_focus->resource) { + input_focus->handler->leave(input_focus->handler, input_focus->resource, + input_focus->view); + } } -static void handle_focus_view_destroy(struct wl_listener * listener, - void * data) +static void +handle_focus_view_destroy(struct wl_listener *listener, + void *data) { - struct input_focus * input_focus - = wl_container_of(listener, input_focus, view_destroy_listener); + struct input_focus *input_focus = wl_container_of(listener, input_focus, view_destroy_listener); - input_focus->resource = NULL; - input_focus->view = NULL; + input_focus->resource = NULL; + input_focus->view = NULL; } -bool input_focus_initialize(struct input_focus * input_focus, - struct input_focus_handler * handler) +bool +input_focus_initialize(struct input_focus *input_focus, + struct input_focus_handler *handler) { - input_focus->resource = NULL; - input_focus->view = NULL; - input_focus->view_destroy_listener.notify = &handle_focus_view_destroy; - input_focus->handler = handler; + input_focus->resource = NULL; + input_focus->view = NULL; + input_focus->view_destroy_listener.notify = &handle_focus_view_destroy; + input_focus->handler = handler; - wl_list_init(&input_focus->resources); - wl_signal_init(&input_focus->event_signal); + wl_list_init(&input_focus->resources); + wl_signal_init(&input_focus->event_signal); - return true; + return true; } -void input_focus_finalize(struct input_focus * input_focus) +void +input_focus_finalize(struct input_focus *input_focus) { - /* XXX: Destroy resources? */ + /* XXX: Destroy resources? */ } -void input_focus_add_resource(struct input_focus * input_focus, - struct wl_resource * resource) +void +input_focus_add_resource(struct input_focus *input_focus, + struct wl_resource *resource) { - /* If this new input resource corresponds to our focus, set it as our + /* If this new input resource corresponds to our focus, set it as our * focus. */ - if (input_focus->view) - { - struct wl_client * client, * surface_client; - - client = wl_resource_get_client(resource); - surface_client = wl_resource_get_client - (input_focus->view->surface->resource); - - if (client == surface_client) - { - input_focus->handler->enter(input_focus->handler, resource, - input_focus->view); - input_focus->resource = resource; - } - } - - wl_list_insert(&input_focus->resources, wl_resource_get_link(resource)); + if (input_focus->view) { + struct wl_client *client, *surface_client; + + client = wl_resource_get_client(resource); + surface_client = wl_resource_get_client(input_focus->view->surface->resource); + + if (client == surface_client) { + input_focus->handler->enter(input_focus->handler, resource, + input_focus->view); + input_focus->resource = resource; + } + } + + wl_list_insert(&input_focus->resources, wl_resource_get_link(resource)); } -void input_focus_remove_resource(struct input_focus * input_focus, - struct wl_resource * resource) +void +input_focus_remove_resource(struct input_focus *input_focus, + struct wl_resource *resource) { - if (resource == input_focus->resource) - input_focus->resource = NULL; + if (resource == input_focus->resource) + input_focus->resource = NULL; - remove_resource(resource); + remove_resource(resource); } -void input_focus_set(struct input_focus * input_focus, - struct compositor_view * view) +void +input_focus_set(struct input_focus *input_focus, + struct compositor_view *view) { - struct input_focus_event_data data; + struct input_focus_event_data data; - if (view == input_focus->view) - return; + if (view == input_focus->view) + return; - data.old = input_focus->view; - data.new = view; + data.old = input_focus->view; + data.new = view; - /* Unfocus previously focused view. */ - unfocus(input_focus); + /* Unfocus previously focused view. */ + unfocus(input_focus); - /* Focus new view, if given. */ - focus(input_focus, view); + /* Focus new view, if given. */ + focus(input_focus, view); - send_event(&input_focus->event_signal, INPUT_FOCUS_EVENT_CHANGED, &data); + send_event(&input_focus->event_signal, INPUT_FOCUS_EVENT_CHANGED, &data); - return; + return; } - diff --git a/libswc/input.h b/libswc/input.h @@ -29,64 +29,58 @@ /* Focus {{{ */ -enum -{ - INPUT_FOCUS_EVENT_CHANGED +enum { + INPUT_FOCUS_EVENT_CHANGED }; -struct input_focus_event_data -{ - struct compositor_view * old, * new; +struct input_focus_event_data { + struct compositor_view *old, *new; }; -struct input_focus_handler -{ - void (* enter)(struct input_focus_handler * handler, - struct wl_resource * resource, - struct compositor_view * view); - void (* leave)(struct input_focus_handler * handler, - struct wl_resource * resource, - struct compositor_view * view); +struct input_focus_handler { + void (*enter)(struct input_focus_handler *handler, + struct wl_resource *resource, + struct compositor_view *view); + void (*leave)(struct input_focus_handler *handler, + struct wl_resource *resource, + struct compositor_view *view); }; -struct input_focus -{ - struct wl_resource * resource; - struct compositor_view * view; - struct wl_listener view_destroy_listener; +struct input_focus { + struct wl_resource *resource; + struct compositor_view *view; + struct wl_listener view_destroy_listener; - struct input_focus_handler * handler; - struct wl_list resources; + struct input_focus_handler *handler; + struct wl_list resources; - struct wl_signal event_signal; + struct wl_signal event_signal; }; -bool input_focus_initialize(struct input_focus * input_focus, - struct input_focus_handler * input_handler); +bool input_focus_initialize(struct input_focus *input_focus, + struct input_focus_handler *input_handler); -void input_focus_finalize(struct input_focus * input_focus); +void input_focus_finalize(struct input_focus *input_focus); -void input_focus_add_resource(struct input_focus * input_focus, - struct wl_resource * resource); +void input_focus_add_resource(struct input_focus *input_focus, + struct wl_resource *resource); -void input_focus_remove_resource(struct input_focus * input_focus, - struct wl_resource * resource); +void input_focus_remove_resource(struct input_focus *input_focus, + struct wl_resource *resource); -void input_focus_set(struct input_focus * input_focus, - struct compositor_view * view); +void input_focus_set(struct input_focus *input_focus, + struct compositor_view *view); /* }}} */ /* Key/button handling {{{ */ -struct press -{ - uint32_t value; - uint32_t serial; - void * data; +struct press { + uint32_t value; + uint32_t serial; + void *data; }; /* }}} */ #endif - diff --git a/libswc/internal.h b/libswc/internal.h @@ -27,29 +27,27 @@ #include <wayland-server.h> #include <stdbool.h> -enum -{ - SWC_EVENT_ACTIVATED, - SWC_EVENT_DEACTIVATED, +enum { + SWC_EVENT_ACTIVATED, + SWC_EVENT_DEACTIVATED, }; -struct swc -{ - struct wl_display * display; - struct wl_event_loop * event_loop; - const struct swc_manager * manager; - struct wl_signal event_signal; - bool active; - - const struct swc_seat * const seat; - const struct swc_bindings * const bindings; - struct wl_list screens; - struct swc_compositor * const compositor; - struct swc_shm * const shm; - struct swc_drm * const drm; +struct swc { + struct wl_display *display; + struct wl_event_loop *event_loop; + const struct swc_manager *manager; + struct wl_signal event_signal; + bool active; + + const struct swc_seat *const seat; + const struct swc_bindings *const bindings; + struct wl_list screens; + struct swc_compositor *const compositor; + struct swc_shm *const shm; + struct swc_drm *const drm; #ifdef ENABLE_XWAYLAND - const struct swc_xserver * const xserver; + const struct swc_xserver *const xserver; #endif }; @@ -59,4 +57,3 @@ void swc_activate(void); void swc_deactivate(void); #endif - diff --git a/libswc/keyboard.c b/libswc/keyboard.c @@ -38,294 +38,282 @@ static const int repeat_delay = 500, repeat_rate = 40; -static void enter(struct input_focus_handler * handler, - struct wl_resource * resource, struct compositor_view * view) +static void +enter(struct input_focus_handler *handler, + struct wl_resource *resource, struct compositor_view *view) { - struct keyboard * keyboard - = wl_container_of(handler, keyboard, focus_handler); - struct keyboard_modifier_state * state = &keyboard->modifier_state; - uint32_t serial; - - serial = wl_display_next_serial(swc.display); - wl_keyboard_send_modifiers(resource, serial, state->depressed, - state->locked, state->latched, state->group); - wl_keyboard_send_enter(resource, serial, view->surface->resource, - &keyboard->client_keys); + struct keyboard *keyboard = wl_container_of(handler, keyboard, focus_handler); + struct keyboard_modifier_state *state = &keyboard->modifier_state; + uint32_t serial; + + serial = wl_display_next_serial(swc.display); + wl_keyboard_send_modifiers(resource, serial, state->depressed, + state->locked, state->latched, state->group); + wl_keyboard_send_enter(resource, serial, view->surface->resource, + &keyboard->client_keys); } -static void leave(struct input_focus_handler * handler, - struct wl_resource * resource, struct compositor_view * view) +static void +leave(struct input_focus_handler *handler, + struct wl_resource *resource, struct compositor_view *view) { - uint32_t serial; + uint32_t serial; - serial = wl_display_next_serial(swc.display); - wl_keyboard_send_leave(resource, serial, view->surface->resource); + serial = wl_display_next_serial(swc.display); + wl_keyboard_send_leave(resource, serial, view->surface->resource); } -static bool client_handle_key(struct keyboard * keyboard, uint32_t time, - struct key * key, uint32_t state) +static bool +client_handle_key(struct keyboard *keyboard, uint32_t time, + struct key *key, uint32_t state) { - uint32_t * value; - - if (state == WL_KEYBOARD_KEY_STATE_PRESSED) - { - if (!(value = wl_array_add(&keyboard->client_keys, sizeof *value))) - return false; - - *value = key->press.value; - } - else - { - wl_array_for_each(value, &keyboard->client_keys) - { - if (*value == key->press.value) - { - array_remove(&keyboard->client_keys, value, sizeof *value); - break; - } - } - } - - if (keyboard->focus.resource) - { - wl_keyboard_send_key(keyboard->focus.resource, key->press.serial, time, - key->press.value, state); - } - - return true; + uint32_t *value; + + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { + if (!(value = wl_array_add(&keyboard->client_keys, sizeof *value))) + return false; + + *value = key->press.value; + } else { + wl_array_for_each (value, &keyboard->client_keys) { + if (*value == key->press.value) { + array_remove(&keyboard->client_keys, value, sizeof *value); + break; + } + } + } + + if (keyboard->focus.resource) { + wl_keyboard_send_key(keyboard->focus.resource, key->press.serial, time, + key->press.value, state); + } + + return true; } -static bool client_handle_modifiers - (struct keyboard * keyboard, const struct keyboard_modifier_state * state) +static bool +client_handle_modifiers(struct keyboard *keyboard, const struct keyboard_modifier_state *state) { - struct wl_client * client; - struct wl_display * display; - uint32_t serial; + struct wl_client *client; + struct wl_display *display; + uint32_t serial; - if (!keyboard->focus.resource) - return false; + if (!keyboard->focus.resource) + return false; - client = wl_resource_get_client(keyboard->focus.resource); - display = wl_client_get_display(client); - serial = wl_display_next_serial(display); - wl_keyboard_send_modifiers(keyboard->focus.resource, serial, - state->depressed, state->locked, state->latched, - state->group); + client = wl_resource_get_client(keyboard->focus.resource); + display = wl_client_get_display(client); + serial = wl_display_next_serial(display); + wl_keyboard_send_modifiers(keyboard->focus.resource, serial, + state->depressed, state->locked, state->latched, + state->group); - return true; + return true; } -bool keyboard_initialize(struct keyboard * keyboard) +bool +keyboard_initialize(struct keyboard *keyboard) { - if (!xkb_initialize(&keyboard->xkb)) - { - ERROR("Could not initialize XKB\n"); - goto error0; - } - - if (!input_focus_initialize(&keyboard->focus, &keyboard->focus_handler)) - goto error1; - - keyboard->modifier_state = (struct keyboard_modifier_state) { }; - keyboard->modifiers = 0; - keyboard->focus_handler.enter = &enter; - keyboard->focus_handler.leave = &leave; - keyboard->client_handler.key = &client_handle_key; - keyboard->client_handler.modifiers = &client_handle_modifiers; - wl_array_init(&keyboard->client_keys); - wl_array_init(&keyboard->keys); - wl_list_init(&keyboard->handlers); - wl_list_insert(&keyboard->handlers, &keyboard->client_handler.link); - - return true; - - error1: - xkb_finalize(&keyboard->xkb); - error0: - return false; + if (!xkb_initialize(&keyboard->xkb)) { + ERROR("Could not initialize XKB\n"); + goto error0; + } + + if (!input_focus_initialize(&keyboard->focus, &keyboard->focus_handler)) + goto error1; + + keyboard->modifier_state = (struct keyboard_modifier_state){}; + keyboard->modifiers = 0; + keyboard->focus_handler.enter = &enter; + keyboard->focus_handler.leave = &leave; + keyboard->client_handler.key = &client_handle_key; + keyboard->client_handler.modifiers = &client_handle_modifiers; + wl_array_init(&keyboard->client_keys); + wl_array_init(&keyboard->keys); + wl_list_init(&keyboard->handlers); + wl_list_insert(&keyboard->handlers, &keyboard->client_handler.link); + + return true; + +error1: + xkb_finalize(&keyboard->xkb); +error0: + return false; } -void keyboard_finalize(struct keyboard * keyboard) +void +keyboard_finalize(struct keyboard *keyboard) { - wl_array_release(&keyboard->client_keys); - wl_array_release(&keyboard->keys); - input_focus_finalize(&keyboard->focus); - xkb_finalize(&keyboard->xkb); + wl_array_release(&keyboard->client_keys); + wl_array_release(&keyboard->keys); + input_focus_finalize(&keyboard->focus); + xkb_finalize(&keyboard->xkb); } -void keyboard_reset(struct keyboard * keyboard) +void +keyboard_reset(struct keyboard *keyboard) { - struct key * key; - uint32_t time = get_time(); - - /* Send simulated key release events for all current key handlers. */ - wl_array_for_each(key, &keyboard->keys) - { - if (key->handler) - { - key->press.serial = wl_display_next_serial(swc.display); - key->handler->key(keyboard, time, key, - WL_KEYBOARD_KEY_STATE_RELEASED); - /* Don't bother updating the XKB state because we will be resetting + struct key *key; + uint32_t time = get_time(); + + /* Send simulated key release events for all current key handlers. */ + wl_array_for_each (key, &keyboard->keys) { + if (key->handler) { + key->press.serial = wl_display_next_serial(swc.display); + key->handler->key(keyboard, time, key, + WL_KEYBOARD_KEY_STATE_RELEASED); + /* Don't bother updating the XKB state because we will be resetting * it later on and it is unlikely that a key handler cares about the * keyboard state for release events. */ - } - } + } + } - /* We should have removed all the client keys by calling the client key + /* We should have removed all the client keys by calling the client key * handler. */ - assert(keyboard->client_keys.size == 0); - keyboard->keys.size = 0; - keyboard->modifier_state = (struct keyboard_modifier_state) { }; - keyboard->modifiers = 0; - xkb_reset_state(&keyboard->xkb); + assert(keyboard->client_keys.size == 0); + keyboard->keys.size = 0; + keyboard->modifier_state = (struct keyboard_modifier_state){}; + keyboard->modifiers = 0; + xkb_reset_state(&keyboard->xkb); } /** * Sets the focus of the keyboard to the specified surface. */ -void keyboard_set_focus(struct keyboard * keyboard, - struct compositor_view * view) +void +keyboard_set_focus(struct keyboard *keyboard, + struct compositor_view *view) { - input_focus_set(&keyboard->focus, view); + input_focus_set(&keyboard->focus, view); } -static void release(struct wl_client * client, struct wl_resource * resource) +static void +release(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } static struct wl_keyboard_interface keyboard_implementation = { - .release = &release, + .release = &release, }; -static void unbind(struct wl_resource * resource) +static void +unbind(struct wl_resource *resource) { - struct keyboard * keyboard = wl_resource_get_user_data(resource); + struct keyboard *keyboard = wl_resource_get_user_data(resource); - input_focus_remove_resource(&keyboard->focus, resource); + input_focus_remove_resource(&keyboard->focus, resource); } -struct wl_resource * keyboard_bind(struct keyboard * keyboard, - struct wl_client * client, - uint32_t version, uint32_t id) +struct wl_resource * +keyboard_bind(struct keyboard *keyboard, + struct wl_client *client, + uint32_t version, uint32_t id) { - struct wl_resource * client_resource; - - client_resource = wl_resource_create(client, &wl_keyboard_interface, - version, id); - wl_resource_set_implementation(client_resource, &keyboard_implementation, - keyboard, &unbind); - input_focus_add_resource(&keyboard->focus, client_resource); - - /* Subtract one to remove terminating NULL character. */ - wl_keyboard_send_keymap(client_resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - keyboard->xkb.keymap.fd, - keyboard->xkb.keymap.size - 1); - - if (version >= 4) - { - wl_keyboard_send_repeat_info(client_resource, - repeat_rate, repeat_delay); - } - - return client_resource; + struct wl_resource *client_resource; + + client_resource = wl_resource_create(client, &wl_keyboard_interface, + version, id); + wl_resource_set_implementation(client_resource, &keyboard_implementation, + keyboard, &unbind); + input_focus_add_resource(&keyboard->focus, client_resource); + + /* Subtract one to remove terminating NULL character. */ + wl_keyboard_send_keymap(client_resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, + keyboard->xkb.keymap.fd, + keyboard->xkb.keymap.size - 1); + + if (version >= 4) { + wl_keyboard_send_repeat_info(client_resource, + repeat_rate, repeat_delay); + } + + return client_resource; } -void keyboard_handle_key(struct keyboard * keyboard, uint32_t time, - uint32_t value, uint32_t state) +void +keyboard_handle_key(struct keyboard *keyboard, uint32_t time, + uint32_t value, uint32_t state) { - struct key * key; - struct keyboard_modifier_state modifier_state; - enum xkb_key_direction direction; - struct xkb * xkb = &keyboard->xkb; - struct keyboard_handler * handler; - uint32_t serial; - - serial = wl_display_next_serial(swc.display); - - /* First handle key release events associated with a particular handler. */ - wl_array_for_each(key, &keyboard->keys) - { - if (key->press.value == value) - { - /* Ignore repeat events. */ - if (state == WL_KEYBOARD_KEY_STATE_PRESSED) - return; - - if (key->handler) - { - key->press.serial = serial; - key->handler->key(keyboard, time, key, state); - } - - array_remove(&keyboard->keys, key, sizeof *key); - goto update_xkb_state; - } - } - - /* If we get a unpaired release event, just ignore it. */ - if (state == WL_KEYBOARD_KEY_STATE_RELEASED) - return; - - if (!(key = wl_array_add(&keyboard->keys, sizeof *key))) - goto update_xkb_state; - - key->press.value = value; - key->press.serial = serial; - key->handler = NULL; - - /* Go through handlers to see if any will accept this key event. */ - wl_list_for_each(handler, &keyboard->handlers, link) - { - if (handler->key && handler->key(keyboard, time, key, state)) - { - key->handler = handler; - break; - } - } - - /* Update XKB state. */ - update_xkb_state: - direction = state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN - : XKB_KEY_UP; - xkb_state_update_key(xkb->state, XKB_KEY(value), direction); - - modifier_state.depressed - = xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); - modifier_state.latched - = xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); - modifier_state.locked - = xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); - modifier_state.group - = xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); - - if (modifier_state.depressed != keyboard->modifier_state.depressed - || modifier_state.latched != keyboard->modifier_state.latched - || modifier_state.locked != keyboard->modifier_state.locked - || modifier_state.group != keyboard->modifier_state.group) - { - uint32_t mods_active = modifier_state.depressed - | modifier_state.latched; - - /* Update keyboard modifier state. */ - keyboard->modifier_state = modifier_state; - keyboard->modifiers = 0; - if (mods_active & (1 << keyboard->xkb.indices.ctrl)) - keyboard->modifiers |= SWC_MOD_CTRL; - if (mods_active & (1 << keyboard->xkb.indices.alt)) - keyboard->modifiers |= SWC_MOD_ALT; - if (mods_active & (1 << keyboard->xkb.indices.super)) - keyboard->modifiers |= SWC_MOD_LOGO; - if (mods_active & (1 << keyboard->xkb.indices.shift)) - keyboard->modifiers |= SWC_MOD_SHIFT; - - /* Run any modifier handlers. */ - wl_list_for_each(handler, &keyboard->handlers, link) - { - if (handler->modifiers) - handler->modifiers(keyboard, &modifier_state); - } - } + struct key *key; + struct keyboard_modifier_state modifier_state; + enum xkb_key_direction direction; + struct xkb *xkb = &keyboard->xkb; + struct keyboard_handler *handler; + uint32_t serial; + + serial = wl_display_next_serial(swc.display); + + /* First handle key release events associated with a particular handler. */ + wl_array_for_each (key, &keyboard->keys) { + if (key->press.value == value) { + /* Ignore repeat events. */ + if (state == WL_KEYBOARD_KEY_STATE_PRESSED) + return; + + if (key->handler) { + key->press.serial = serial; + key->handler->key(keyboard, time, key, state); + } + + array_remove(&keyboard->keys, key, sizeof *key); + goto update_xkb_state; + } + } + + /* If we get a unpaired release event, just ignore it. */ + if (state == WL_KEYBOARD_KEY_STATE_RELEASED) + return; + + if (!(key = wl_array_add(&keyboard->keys, sizeof *key))) + goto update_xkb_state; + + key->press.value = value; + key->press.serial = serial; + key->handler = NULL; + + /* Go through handlers to see if any will accept this key event. */ + wl_list_for_each (handler, &keyboard->handlers, link) { + if (handler->key && handler->key(keyboard, time, key, state)) { + key->handler = handler; + break; + } + } + +/* Update XKB state. */ +update_xkb_state: + direction = state == WL_KEYBOARD_KEY_STATE_PRESSED ? XKB_KEY_DOWN + : XKB_KEY_UP; + xkb_state_update_key(xkb->state, XKB_KEY(value), direction); + + modifier_state.depressed = xkb_state_serialize_mods(xkb->state, XKB_STATE_DEPRESSED); + modifier_state.latched = xkb_state_serialize_mods(xkb->state, XKB_STATE_LATCHED); + modifier_state.locked = xkb_state_serialize_mods(xkb->state, XKB_STATE_LOCKED); + modifier_state.group = xkb_state_serialize_layout(xkb->state, XKB_STATE_LAYOUT_EFFECTIVE); + + if (modifier_state.depressed != keyboard->modifier_state.depressed + || modifier_state.latched != keyboard->modifier_state.latched + || modifier_state.locked != keyboard->modifier_state.locked + || modifier_state.group != keyboard->modifier_state.group) { + uint32_t mods_active = modifier_state.depressed + | modifier_state.latched; + + /* Update keyboard modifier state. */ + keyboard->modifier_state = modifier_state; + keyboard->modifiers = 0; + if (mods_active & (1 << keyboard->xkb.indices.ctrl)) + keyboard->modifiers |= SWC_MOD_CTRL; + if (mods_active & (1 << keyboard->xkb.indices.alt)) + keyboard->modifiers |= SWC_MOD_ALT; + if (mods_active & (1 << keyboard->xkb.indices.super)) + keyboard->modifiers |= SWC_MOD_LOGO; + if (mods_active & (1 << keyboard->xkb.indices.shift)) + keyboard->modifiers |= SWC_MOD_SHIFT; + + /* Run any modifier handlers. */ + wl_list_for_each (handler, &keyboard->handlers, link) { + if (handler->modifiers) + handler->modifiers(keyboard, &modifier_state); + } + } } - diff --git a/libswc/keyboard.h b/libswc/keyboard.h @@ -32,55 +32,50 @@ struct keyboard; struct wl_client; -struct key -{ - struct press press; - struct keyboard_handler * handler; +struct key { + struct press press; + struct keyboard_handler *handler; }; -struct keyboard_modifier_state -{ - uint32_t depressed; - uint32_t latched; - uint32_t locked; - uint32_t group; +struct keyboard_modifier_state { + uint32_t depressed; + uint32_t latched; + uint32_t locked; + uint32_t group; }; -struct keyboard_handler -{ - bool (* key)(struct keyboard * keyboard, uint32_t time, - struct key * key, uint32_t state); - bool (* modifiers)(struct keyboard * keyboard, - const struct keyboard_modifier_state * state); +struct keyboard_handler { + bool (*key)(struct keyboard *keyboard, uint32_t time, + struct key *key, uint32_t state); + bool (*modifiers)(struct keyboard *keyboard, + const struct keyboard_modifier_state *state); - struct wl_list link; + struct wl_list link; }; -struct keyboard -{ - struct input_focus focus; - struct input_focus_handler focus_handler; - struct xkb xkb; +struct keyboard { + struct input_focus focus; + struct input_focus_handler focus_handler; + struct xkb xkb; - struct wl_array keys; - struct wl_list handlers; - struct keyboard_handler client_handler; - struct wl_array client_keys; + struct wl_array keys; + struct wl_list handlers; + struct keyboard_handler client_handler; + struct wl_array client_keys; - struct keyboard_modifier_state modifier_state; - uint32_t modifiers; + struct keyboard_modifier_state modifier_state; + uint32_t modifiers; }; -bool keyboard_initialize(struct keyboard * keyboard); -void keyboard_finalize(struct keyboard * keyboard); -void keyboard_reset(struct keyboard * keyboard); -void keyboard_set_focus(struct keyboard * keyboard, - struct compositor_view * view); -struct wl_resource * keyboard_bind(struct keyboard * keyboard, - struct wl_client * client, - uint32_t version, uint32_t id); -void keyboard_handle_key(struct keyboard * keyboard, uint32_t time, +bool keyboard_initialize(struct keyboard *keyboard); +void keyboard_finalize(struct keyboard *keyboard); +void keyboard_reset(struct keyboard *keyboard); +void keyboard_set_focus(struct keyboard *keyboard, + struct compositor_view *view); +struct wl_resource *keyboard_bind(struct keyboard *keyboard, + struct wl_client *client, + uint32_t version, uint32_t id); +void keyboard_handle_key(struct keyboard *keyboard, uint32_t time, uint32_t key, uint32_t state); #endif - diff --git a/libswc/launch.c b/libswc/launch.c @@ -31,118 +31,121 @@ #include <wayland-server.h> static struct -{ - int socket; - struct wl_event_source * source; - uint32_t next_serial; + { + int socket; + struct wl_event_source *source; + uint32_t next_serial; } launch; -static bool handle_event(struct swc_launch_event * event) +static bool +handle_event(struct swc_launch_event *event) { - switch (event->type) - { - case SWC_LAUNCH_EVENT_ACTIVATE: - swc_activate(); - break; - case SWC_LAUNCH_EVENT_DEACTIVATE: - swc_deactivate(); - break; - default: - return false; - } - - return true; + switch (event->type) { + case SWC_LAUNCH_EVENT_ACTIVATE: + swc_activate(); + break; + case SWC_LAUNCH_EVENT_DEACTIVATE: + swc_deactivate(); + break; + default: + return false; + } + + return true; } -static int handle_data(int fd, uint32_t mask, void * data) +static int +handle_data(int fd, uint32_t mask, void *data) { - struct swc_launch_event event; + struct swc_launch_event event; - if (receive_fd(fd, NULL, &event, sizeof event) != -1) - handle_event(&event); + if (receive_fd(fd, NULL, &event, sizeof event) != -1) + handle_event(&event); - return 1; + return 1; } -bool launch_initialize(void) +bool +launch_initialize(void) { - char * socket_string, * end; + char *socket_string, *end; - if (!(socket_string = getenv(SWC_LAUNCH_SOCKET_ENV))) - return false; + if (!(socket_string = getenv(SWC_LAUNCH_SOCKET_ENV))) + return false; - launch.socket = strtol(socket_string, &end, 10); + launch.socket = strtol(socket_string, &end, 10); - if (*end != '\0') - return false; + if (*end != '\0') + return false; - launch.source = wl_event_loop_add_fd(swc.event_loop, launch.socket, - WL_EVENT_READABLE, &handle_data, NULL); + launch.source = wl_event_loop_add_fd(swc.event_loop, launch.socket, + WL_EVENT_READABLE, &handle_data, NULL); - if (!launch.source) - return false; + if (!launch.source) + return false; - return true; + return true; } -void launch_finalize(void) +void +launch_finalize(void) { - wl_event_source_remove(launch.source); - close(launch.socket); + wl_event_source_remove(launch.source); + close(launch.socket); } -static bool send_request(struct swc_launch_request * request, size_t size, - struct swc_launch_event * event, - int out_fd, int * in_fd) +static bool +send_request(struct swc_launch_request *request, size_t size, + struct swc_launch_event *event, + int out_fd, int *in_fd) { - request->serial = ++launch.next_serial; + request->serial = ++launch.next_serial; - if (send_fd(launch.socket, out_fd, request, size) == -1) - return false; + if (send_fd(launch.socket, out_fd, request, size) == -1) + return false; - while (receive_fd(launch.socket, in_fd, event, sizeof *event) != -1) - { - if (event->type == SWC_LAUNCH_EVENT_RESPONSE - && event->serial == request->serial) - { - return true; - } + while (receive_fd(launch.socket, in_fd, event, sizeof *event) != -1) { + if (event->type == SWC_LAUNCH_EVENT_RESPONSE + && event->serial == request->serial) { + return true; + } - handle_event(event); - } + handle_event(event); + } - return false; + return false; } -int launch_open_device(const char * path, int flags) +int +launch_open_device(const char *path, int flags) { - size_t path_size = strlen(path); - char buffer[sizeof(struct swc_launch_request) + path_size + 1]; - struct swc_launch_request * request = (void *) buffer; - struct swc_launch_event response; - int fd; + size_t path_size = strlen(path); + char buffer[sizeof(struct swc_launch_request) + path_size + 1]; + struct swc_launch_request *request = (void *)buffer; + struct swc_launch_event response; + int fd; - request->type = SWC_LAUNCH_REQUEST_OPEN_DEVICE; - request->flags = flags; - strcpy(request->path, path); + request->type = SWC_LAUNCH_REQUEST_OPEN_DEVICE; + request->flags = flags; + strcpy(request->path, path); - if (!send_request(request, sizeof buffer, &response, -1, &fd)) - return -1; + if (!send_request(request, sizeof buffer, &response, -1, &fd)) + return -1; - return fd; + return fd; } -bool launch_activate_vt(unsigned vt) +bool +launch_activate_vt(unsigned vt) { - struct swc_launch_request request; - struct swc_launch_event response; + struct swc_launch_request request; + struct swc_launch_event response; - request.type = SWC_LAUNCH_REQUEST_ACTIVATE_VT; - request.vt = vt; + request.type = SWC_LAUNCH_REQUEST_ACTIVATE_VT; + request.vt = vt; - if (!send_request(&request, sizeof request, &response, -1, NULL)) - return false; + if (!send_request(&request, sizeof request, &response, -1, NULL)) + return false; - return response.success; + return response.success; } - diff --git a/libswc/launch.h b/libswc/launch.h @@ -30,8 +30,7 @@ bool launch_initialize(void); void launch_finalize(void); -int launch_open_device(const char * path, int flags); +int launch_open_device(const char *path, int flags); bool launch_activate_vt(unsigned vt); #endif - diff --git a/libswc/mode.c b/libswc/mode.c @@ -23,26 +23,28 @@ #include "mode.h" -bool mode_initialize(struct mode * mode, drmModeModeInfo * mode_info) +bool +mode_initialize(struct mode *mode, drmModeModeInfo *mode_info) { - mode->width = mode_info->hdisplay; - mode->height = mode_info->vdisplay; - mode->refresh = mode_info->vrefresh * 1000; - mode->preferred = mode_info->type & DRM_MODE_TYPE_PREFERRED; + mode->width = mode_info->hdisplay; + mode->height = mode_info->vdisplay; + mode->refresh = mode_info->vrefresh * 1000; + mode->preferred = mode_info->type & DRM_MODE_TYPE_PREFERRED; - mode->info = *mode_info; + mode->info = *mode_info; - return true; + return true; } -void mode_finish(struct mode * mode) +void +mode_finish(struct mode *mode) { } -bool mode_equal(const struct mode * mode1, const struct mode * mode2) +bool +mode_equal(const struct mode *mode1, const struct mode *mode2) { - return mode1->width == mode2->width - && mode1->height == mode2->height - && mode1->refresh == mode2->refresh; + return mode1->width == mode2->width + && mode1->height == mode2->height + && mode1->refresh == mode2->refresh; } - diff --git a/libswc/mode.h b/libswc/mode.h @@ -31,20 +31,18 @@ #include <wayland-util.h> -struct mode -{ - uint16_t width, height; - uint32_t refresh; +struct mode { + uint16_t width, height; + uint32_t refresh; - bool preferred; + bool preferred; - drmModeModeInfo info; + drmModeModeInfo info; }; -bool mode_initialize(struct mode * mode, drmModeModeInfo * mode_info); -void mode_finish(struct mode * mode); +bool mode_initialize(struct mode *mode, drmModeModeInfo *mode_info); +void mode_finish(struct mode *mode); -bool mode_equal(const struct mode * mode1, const struct mode * mode2); +bool mode_equal(const struct mode *mode1, const struct mode *mode2); #endif - diff --git a/libswc/output.c b/libswc/output.c @@ -11,113 +11,109 @@ #include <libdrm/drm.h> #include <xf86drm.h> -static void bind_output(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_output(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct output * output = data; - struct screen * screen = output->screen; - struct mode * mode; - struct wl_resource * resource; - uint32_t flags; - - if (version > 2) - version = 2; - - resource = wl_resource_create(client, &wl_output_interface, version, id); - - if (!resource) - { - wl_client_post_no_memory(client); - return; - } - - wl_resource_set_implementation(resource, NULL, output, &remove_resource); - wl_list_insert(&output->resources, wl_resource_get_link(resource)); - - wl_output_send_geometry - (resource, screen->base.geometry.x, screen->base.geometry.y, - output->physical_width, output->physical_height, 0, "unknown", - "unknown", WL_OUTPUT_TRANSFORM_NORMAL); - - wl_array_for_each(mode, &output->modes) - { - flags = 0; - if (mode->preferred) - flags |= WL_OUTPUT_MODE_PREFERRED; - if (mode_equal(&screen->planes.framebuffer.mode, mode)) - flags |= WL_OUTPUT_MODE_CURRENT; - - wl_output_send_mode(resource, flags, - mode->width, mode->height, mode->refresh); - } - - if (version >= 2) - wl_output_send_done(resource); + struct output *output = data; + struct screen *screen = output->screen; + struct mode *mode; + struct wl_resource *resource; + uint32_t flags; + + if (version > 2) + version = 2; + + resource = wl_resource_create(client, &wl_output_interface, version, id); + + if (!resource) { + wl_client_post_no_memory(client); + return; + } + + wl_resource_set_implementation(resource, NULL, output, &remove_resource); + wl_list_insert(&output->resources, wl_resource_get_link(resource)); + + wl_output_send_geometry(resource, screen->base.geometry.x, screen->base.geometry.y, + output->physical_width, output->physical_height, 0, "unknown", + "unknown", WL_OUTPUT_TRANSFORM_NORMAL); + + wl_array_for_each (mode, &output->modes) { + flags = 0; + if (mode->preferred) + flags |= WL_OUTPUT_MODE_PREFERRED; + if (mode_equal(&screen->planes.framebuffer.mode, mode)) + flags |= WL_OUTPUT_MODE_CURRENT; + + wl_output_send_mode(resource, flags, + mode->width, mode->height, mode->refresh); + } + + if (version >= 2) + wl_output_send_done(resource); } -struct output * output_new(drmModeConnectorPtr connector) +struct output * +output_new(drmModeConnectorPtr connector) { - struct output * output; - struct mode * modes; - uint32_t index; + struct output *output; + struct mode *modes; + uint32_t index; - if (!(output = malloc(sizeof *output))) - { - ERROR("Failed to allocated output\n"); - goto error0; - } + if (!(output = malloc(sizeof *output))) { + ERROR("Failed to allocated output\n"); + goto error0; + } - output->global = wl_global_create(swc.display, &wl_output_interface, 2, - output, &bind_output); + output->global = wl_global_create(swc.display, &wl_output_interface, 2, + output, &bind_output); - if (!output->global) - { - ERROR("Failed to create output global\n"); - goto error1; - } + if (!output->global) { + ERROR("Failed to create output global\n"); + goto error1; + } - output->physical_width = connector->mmWidth; - output->physical_height = connector->mmHeight; + output->physical_width = connector->mmWidth; + output->physical_height = connector->mmHeight; - wl_list_init(&output->resources); - wl_array_init(&output->modes); - pixman_region32_init(&output->current_damage); - pixman_region32_init(&output->previous_damage); + wl_list_init(&output->resources); + wl_array_init(&output->modes); + pixman_region32_init(&output->current_damage); + pixman_region32_init(&output->previous_damage); - output->connector = connector->connector_id; + output->connector = connector->connector_id; - modes = wl_array_add(&output->modes, connector->count_modes * sizeof *modes); + modes = wl_array_add(&output->modes, connector->count_modes * sizeof *modes); - if (!modes) - goto error2; + if (!modes) + goto error2; - for (index = 0; index < connector->count_modes; ++index) - { - mode_initialize(&modes[index], &connector->modes[index]); + for (index = 0; index < connector->count_modes; ++index) { + mode_initialize(&modes[index], &connector->modes[index]); - if (modes[index].preferred) - output->preferred_mode = &modes[index]; - } + if (modes[index].preferred) + output->preferred_mode = &modes[index]; + } - return output; + return output; - error2: - wl_global_destroy(output->global); - error1: - free(output); - error0: - return NULL; +error2: + wl_global_destroy(output->global); +error1: + free(output); +error0: + return NULL; } -void output_destroy(struct output * output) +void +output_destroy(struct output *output) { - struct mode * mode; + struct mode *mode; - wl_array_for_each(mode, &output->modes) - mode_finish(mode); - wl_array_release(&output->modes); + wl_array_for_each (mode, &output->modes) + mode_finish(mode); + wl_array_release(&output->modes); - wl_global_destroy(output->global); - free(output); + wl_global_destroy(output->global); + free(output); } - diff --git a/libswc/output.h b/libswc/output.h @@ -8,28 +8,26 @@ struct wl_display; -struct output -{ - struct screen * screen; +struct output { + struct screen *screen; - /* The physical dimensions (in mm) of this output */ - uint32_t physical_width, physical_height; + /* The physical dimensions (in mm) of this output */ + uint32_t physical_width, physical_height; - struct wl_array modes; - struct mode * preferred_mode; + struct wl_array modes; + struct mode *preferred_mode; - pixman_region32_t current_damage, previous_damage; + pixman_region32_t current_damage, previous_damage; - /* The DRM connector corresponding to this output */ - uint32_t connector; + /* The DRM connector corresponding to this output */ + uint32_t connector; - struct wl_global * global; - struct wl_list resources; - struct wl_list link; + struct wl_global *global; + struct wl_list resources; + struct wl_list link; }; -struct output * output_new(drmModeConnector * connector); -void output_destroy(struct output * output); +struct output *output_new(drmModeConnector *connector); +void output_destroy(struct output *output); #endif - diff --git a/libswc/panel.c b/libswc/panel.c @@ -36,237 +36,238 @@ #include <assert.h> #include <stdlib.h> -struct panel -{ - struct wl_resource * resource; - - struct wl_listener surface_destroy_listener; - struct compositor_view * view; - struct view_handler view_handler; - struct screen * screen; - struct screen_modifier modifier; - uint32_t edge; - uint32_t offset, strut_size; - bool docked; +struct panel { + struct wl_resource *resource; + + struct wl_listener surface_destroy_listener; + struct compositor_view *view; + struct view_handler view_handler; + struct screen *screen; + struct screen_modifier modifier; + uint32_t edge; + uint32_t offset, strut_size; + bool docked; }; -static void update_position(struct panel * panel) +static void +update_position(struct panel *panel) { - int32_t x, y; - struct swc_rectangle * screen = &panel->screen->base.geometry, - * view = &panel->view->base.geometry; - - switch (panel->edge) - { - case SWC_PANEL_EDGE_TOP: - x = screen->x + panel->offset; - y = screen->y; - break; - case SWC_PANEL_EDGE_BOTTOM: - x = screen->x + panel->offset; - y = screen->y + screen->height - view->height; - break; - case SWC_PANEL_EDGE_LEFT: - x = screen->x; - y = screen->y + screen->height - view->height - panel->offset; - break; - case SWC_PANEL_EDGE_RIGHT: - x = screen->x + screen->width - view->width; - y = screen->y + panel->offset; - break; - default: return; - } - - view_move(&panel->view->base, x, y); + int32_t x, y; + struct swc_rectangle *screen = &panel->screen->base.geometry, + *view = &panel->view->base.geometry; + + switch (panel->edge) { + case SWC_PANEL_EDGE_TOP: + x = screen->x + panel->offset; + y = screen->y; + break; + case SWC_PANEL_EDGE_BOTTOM: + x = screen->x + panel->offset; + y = screen->y + screen->height - view->height; + break; + case SWC_PANEL_EDGE_LEFT: + x = screen->x; + y = screen->y + screen->height - view->height - panel->offset; + break; + case SWC_PANEL_EDGE_RIGHT: + x = screen->x + screen->width - view->width; + y = screen->y + panel->offset; + break; + default: + return; + } + + view_move(&panel->view->base, x, y); } -static void dock(struct wl_client * client, struct wl_resource * resource, - uint32_t edge, struct wl_resource * screen_resource, - uint32_t focus) +static void +dock(struct wl_client *client, struct wl_resource *resource, + uint32_t edge, struct wl_resource *screen_resource, + uint32_t focus) { - struct panel * panel = wl_resource_get_user_data(resource); - struct screen * screen = screen_resource - ? wl_resource_get_user_data(screen_resource) - : wl_container_of(swc.screens.next, screen, link); - bool screen_changed = screen != panel->screen; - uint32_t length; - - switch (edge) - { - case SWC_PANEL_EDGE_TOP: - case SWC_PANEL_EDGE_BOTTOM: - length = screen->base.geometry.width; - break; - case SWC_PANEL_EDGE_LEFT: - case SWC_PANEL_EDGE_RIGHT: - length = screen->base.geometry.height; - break; - default: return; - } - - if (panel->screen && screen_changed) - { - wl_list_remove(&panel->modifier.link); - screen_update_usable_geometry(panel->screen); - } - - panel->screen = screen; - panel->edge = edge; - panel->docked = true; - - update_position(panel); - compositor_view_show(panel->view); - wl_list_insert(&screen->modifiers, &panel->modifier.link); - - if (focus) - keyboard_set_focus(swc.seat->keyboard, panel->view); - - swc_panel_send_docked(resource, length); + struct panel *panel = wl_resource_get_user_data(resource); + struct screen *screen = screen_resource + ? wl_resource_get_user_data(screen_resource) + : wl_container_of(swc.screens.next, screen, link); + bool screen_changed = screen != panel->screen; + uint32_t length; + + switch (edge) { + case SWC_PANEL_EDGE_TOP: + case SWC_PANEL_EDGE_BOTTOM: + length = screen->base.geometry.width; + break; + case SWC_PANEL_EDGE_LEFT: + case SWC_PANEL_EDGE_RIGHT: + length = screen->base.geometry.height; + break; + default: + return; + } + + if (panel->screen && screen_changed) { + wl_list_remove(&panel->modifier.link); + screen_update_usable_geometry(panel->screen); + } + + panel->screen = screen; + panel->edge = edge; + panel->docked = true; + + update_position(panel); + compositor_view_show(panel->view); + wl_list_insert(&screen->modifiers, &panel->modifier.link); + + if (focus) + keyboard_set_focus(swc.seat->keyboard, panel->view); + + swc_panel_send_docked(resource, length); } -static void set_offset(struct wl_client * client, struct wl_resource * resource, - uint32_t offset) +static void +set_offset(struct wl_client *client, struct wl_resource *resource, + uint32_t offset) { - struct panel * panel = wl_resource_get_user_data(resource); + struct panel *panel = wl_resource_get_user_data(resource); - panel->offset = offset; + panel->offset = offset; - if (panel->docked) - update_position(panel); + if (panel->docked) + update_position(panel); } -static void set_strut(struct wl_client * client, struct wl_resource * resource, - uint32_t size, uint32_t begin, uint32_t end) +static void +set_strut(struct wl_client *client, struct wl_resource *resource, + uint32_t size, uint32_t begin, uint32_t end) { - struct panel * panel = wl_resource_get_user_data(resource); + struct panel *panel = wl_resource_get_user_data(resource); - panel->strut_size = size; + panel->strut_size = size; - if (panel->docked) - screen_update_usable_geometry(panel->screen); + if (panel->docked) + screen_update_usable_geometry(panel->screen); } static const struct swc_panel_interface panel_implementation = { - .dock = &dock, - .set_offset = &set_offset, - .set_strut = &set_strut + .dock = &dock, + .set_offset = &set_offset, + .set_strut = &set_strut }; -static void handle_resize(struct view_handler * handler, - uint32_t old_width, uint32_t old_height) +static void +handle_resize(struct view_handler *handler, + uint32_t old_width, uint32_t old_height) { - struct panel * panel = wl_container_of(handler, panel, view_handler); + struct panel *panel = wl_container_of(handler, panel, view_handler); - update_position(panel); + update_position(panel); } static const struct view_handler_impl view_handler_impl = { - .resize = &handle_resize, + .resize = &handle_resize, }; -static void modify(struct screen_modifier * modifier, - const struct swc_rectangle * geometry, - pixman_region32_t * usable) +static void +modify(struct screen_modifier *modifier, + const struct swc_rectangle *geometry, + pixman_region32_t *usable) { - struct panel * panel = wl_container_of(modifier, panel, modifier); - pixman_box32_t box = { - .x1 = geometry->x, .y1 = geometry->y, - .x2 = geometry->x + geometry->width, - .y2 = geometry->y + geometry->height - }; - - assert(panel->docked); - - DEBUG("Original geometry { x1: %d, y1: %d, x2: %d, y2: %d }\n", - box.x1, box.y1, box.x2, box.y2); - - switch (panel->edge) - { - case SWC_PANEL_EDGE_TOP: - box.y1 = MAX(box.y1, geometry->y + panel->strut_size); - break; - case SWC_PANEL_EDGE_BOTTOM: - box.y2 = MIN(box.y2, geometry->y + geometry->height - - panel->strut_size); - break; - case SWC_PANEL_EDGE_LEFT: - box.x1 = MAX(box.x1, geometry->x + panel->strut_size); - break; - case SWC_PANEL_EDGE_RIGHT: - box.x2 = MIN(box.x2, geometry->x + geometry->width - - panel->strut_size); - break; - } - - DEBUG("Usable region { x1: %d, y1: %d, x2: %d, y2: %d }\n", - box.x1, box.y1, box.x2, box.y2); - - pixman_region32_reset(usable, &box); + struct panel *panel = wl_container_of(modifier, panel, modifier); + pixman_box32_t box = { + .x1 = geometry->x, .y1 = geometry->y, .x2 = geometry->x + geometry->width, .y2 = geometry->y + geometry->height + }; + + assert(panel->docked); + + DEBUG("Original geometry { x1: %d, y1: %d, x2: %d, y2: %d }\n", + box.x1, box.y1, box.x2, box.y2); + + switch (panel->edge) { + case SWC_PANEL_EDGE_TOP: + box.y1 = MAX(box.y1, geometry->y + panel->strut_size); + break; + case SWC_PANEL_EDGE_BOTTOM: + box.y2 = MIN(box.y2, geometry->y + geometry->height + - panel->strut_size); + break; + case SWC_PANEL_EDGE_LEFT: + box.x1 = MAX(box.x1, geometry->x + panel->strut_size); + break; + case SWC_PANEL_EDGE_RIGHT: + box.x2 = MIN(box.x2, geometry->x + geometry->width + - panel->strut_size); + break; + } + + DEBUG("Usable region { x1: %d, y1: %d, x2: %d, y2: %d }\n", + box.x1, box.y1, box.x2, box.y2); + + pixman_region32_reset(usable, &box); } -static void destroy_panel(struct wl_resource * resource) +static void +destroy_panel(struct wl_resource *resource) { - struct panel * panel = wl_resource_get_user_data(resource); + struct panel *panel = wl_resource_get_user_data(resource); - if (panel->docked) - { - wl_list_remove(&panel->modifier.link); - screen_update_usable_geometry(panel->screen); - } + if (panel->docked) { + wl_list_remove(&panel->modifier.link); + screen_update_usable_geometry(panel->screen); + } - compositor_view_destroy(panel->view); - free(panel); + compositor_view_destroy(panel->view); + free(panel); } -static void handle_surface_destroy(struct wl_listener * listener, void * data) +static void +handle_surface_destroy(struct wl_listener *listener, void *data) { - struct panel * panel - = wl_container_of(listener, panel, surface_destroy_listener); + struct panel *panel = wl_container_of(listener, panel, surface_destroy_listener); - wl_resource_destroy(panel->resource); + wl_resource_destroy(panel->resource); } -struct panel * panel_new(struct wl_client * client, uint32_t version, - uint32_t id, struct surface * surface) +struct panel * +panel_new(struct wl_client *client, uint32_t version, + uint32_t id, struct surface *surface) { - struct panel * panel; + struct panel *panel; - panel = malloc(sizeof *panel); + panel = malloc(sizeof *panel); - if (!panel) - goto error0; + if (!panel) + goto error0; - panel->resource = wl_resource_create(client, &swc_panel_interface, - version, id); + panel->resource = wl_resource_create(client, &swc_panel_interface, + version, id); - if (!panel->resource) - goto error1; + if (!panel->resource) + goto error1; - if (!(panel->view = compositor_create_view(surface))) - goto error2; + if (!(panel->view = compositor_create_view(surface))) + goto error2; - wl_resource_set_implementation(panel->resource, &panel_implementation, - panel, &destroy_panel); + wl_resource_set_implementation(panel->resource, &panel_implementation, + panel, &destroy_panel); - panel->surface_destroy_listener.notify = &handle_surface_destroy; - panel->view_handler.impl = &view_handler_impl; - panel->modifier.modify = &modify; - panel->screen = NULL; - panel->offset = 0; - panel->strut_size = 0; - panel->docked = false; + panel->surface_destroy_listener.notify = &handle_surface_destroy; + panel->view_handler.impl = &view_handler_impl; + panel->modifier.modify = &modify; + panel->screen = NULL; + panel->offset = 0; + panel->strut_size = 0; + panel->docked = false; - wl_list_insert(&panel->view->base.handlers, &panel->view_handler.link); - wl_resource_add_destroy_listener(surface->resource, - &panel->surface_destroy_listener); + wl_list_insert(&panel->view->base.handlers, &panel->view_handler.link); + wl_resource_add_destroy_listener(surface->resource, + &panel->surface_destroy_listener); - return panel; + return panel; - error2: - wl_resource_destroy(panel->resource); - error1: - free(panel); - error0: - return NULL; +error2: + wl_resource_destroy(panel->resource); +error1: + free(panel); +error0: + return NULL; } - diff --git a/libswc/panel.h b/libswc/panel.h @@ -29,8 +29,7 @@ #include <stdbool.h> #include <wayland-server.h> -struct panel * panel_new(struct wl_client * client, uint32_t version, - uint32_t id, struct surface * surface); +struct panel *panel_new(struct wl_client *client, uint32_t version, + uint32_t id, struct surface *surface); #endif - diff --git a/libswc/panel_manager.c b/libswc/panel_manager.c @@ -29,54 +29,57 @@ #include "protocol/swc-server-protocol.h" static struct -{ - struct wl_global * global; + { + struct wl_global *global; } panel_manager; -static void create_panel(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - struct wl_resource * surface_resource) +static void +create_panel(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *surface_resource) { - struct surface * surface = wl_resource_get_user_data(surface_resource); + struct surface *surface = wl_resource_get_user_data(surface_resource); - if (!panel_new(client, wl_resource_get_version(resource), id, surface)) - wl_client_post_no_memory(client); + if (!panel_new(client, wl_resource_get_version(resource), id, surface)) + wl_client_post_no_memory(client); } static const struct swc_panel_manager_interface panel_manager_implementation = { - .create_panel = &create_panel + .create_panel = &create_panel }; -static void bind_panel_manager(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_panel_manager(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 1) - version = 1; + if (version > 1) + version = 1; - resource = wl_resource_create(client, &swc_panel_manager_interface, - version, id); - wl_resource_set_implementation(resource, &panel_manager_implementation, - NULL, NULL); + resource = wl_resource_create(client, &swc_panel_manager_interface, + version, id); + wl_resource_set_implementation(resource, &panel_manager_implementation, + NULL, NULL); } -bool panel_manager_initialize(void) +bool +panel_manager_initialize(void) { - panel_manager.global = wl_global_create(swc.display, - &swc_panel_manager_interface, 1, - NULL, &bind_panel_manager); + panel_manager.global = wl_global_create(swc.display, + &swc_panel_manager_interface, 1, + NULL, &bind_panel_manager); - if (!panel_manager.global) - return false; + if (!panel_manager.global) + return false; - return true; + return true; } -void panel_manager_finalize(void) +void +panel_manager_finalize(void) { - wl_global_destroy(panel_manager.global); + wl_global_destroy(panel_manager.global); } // vim: fdm=syntax fo=croql et sw=4 sts=4 ts=8 - diff --git a/libswc/panel_manager.h b/libswc/panel_manager.h @@ -30,4 +30,3 @@ bool panel_manager_initialize(void); void panel_manager_finalize(void); #endif - diff --git a/libswc/pointer.c b/libswc/pointer.c @@ -34,410 +34,404 @@ #include <assert.h> #include <wld/wld.h> -static void enter(struct input_focus_handler * handler, - struct wl_resource * resource, struct compositor_view * view) +static void +enter(struct input_focus_handler *handler, + struct wl_resource *resource, struct compositor_view *view) { - struct pointer * pointer = wl_container_of(handler, pointer, focus_handler); - uint32_t serial; - wl_fixed_t surface_x, surface_y; - - 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); + struct pointer *pointer = wl_container_of(handler, pointer, focus_handler); + uint32_t serial; + wl_fixed_t surface_x, surface_y; + + 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); } -static void leave(struct input_focus_handler * handler, - struct wl_resource * resource, struct compositor_view * view) +static void +leave(struct input_focus_handler *handler, + struct wl_resource *resource, struct compositor_view *view) { - uint32_t serial; + uint32_t serial; - serial = wl_display_next_serial(swc.display); + serial = wl_display_next_serial(swc.display); - wl_pointer_send_leave(resource, serial, view->surface->resource); + wl_pointer_send_leave(resource, serial, view->surface->resource); } -static void handle_cursor_surface_destroy(struct wl_listener * listener, - void * data) +static void +handle_cursor_surface_destroy(struct wl_listener *listener, + void *data) { - struct pointer * pointer - = wl_container_of(listener, pointer, cursor.destroy_listener); + struct pointer *pointer = wl_container_of(listener, pointer, cursor.destroy_listener); - view_attach(&pointer->cursor.view, NULL); - pointer->cursor.surface = NULL; + view_attach(&pointer->cursor.view, NULL); + pointer->cursor.surface = NULL; } -static bool update(struct view * view) +static bool +update(struct view *view) { - return true; + return true; } -static int attach(struct view * view, struct wld_buffer * buffer) +static int +attach(struct view *view, struct wld_buffer *buffer) { - struct pointer * pointer = wl_container_of(view, pointer, cursor.view); - struct surface * surface = pointer->cursor.surface; + struct pointer *pointer = wl_container_of(view, pointer, cursor.view); + struct surface *surface = pointer->cursor.surface; - if (surface && !pixman_region32_not_empty(&surface->state.damage)) - return 0; + if (surface && !pixman_region32_not_empty(&surface->state.damage)) + return 0; - wld_set_target_buffer(swc.shm->renderer, pointer->cursor.buffer); - wld_fill_rectangle(swc.shm->renderer, 0x00000000, 0, 0, 64, 64); + wld_set_target_buffer(swc.shm->renderer, pointer->cursor.buffer); + wld_fill_rectangle(swc.shm->renderer, 0x00000000, 0, 0, 64, 64); - if (buffer) - { - wld_copy_rectangle(swc.shm->renderer, buffer, 0, 0, 0, 0, - buffer->width, buffer->height); - } + if (buffer) { + wld_copy_rectangle(swc.shm->renderer, buffer, 0, 0, 0, 0, + buffer->width, buffer->height); + } - wld_flush(swc.shm->renderer); + wld_flush(swc.shm->renderer); - if (surface) - pixman_region32_clear(&surface->state.damage); + if (surface) + pixman_region32_clear(&surface->state.damage); - /* TODO: Send an early release to the buffer */ + /* TODO: Send an early release to the buffer */ - if (view_set_size_from_buffer(view, buffer)) - view_update_screens(view); + if (view_set_size_from_buffer(view, buffer)) + view_update_screens(view); - return 0; + return 0; } -static bool move(struct view * view, int32_t x, int32_t y) +static bool +move(struct view *view, int32_t x, int32_t y) { - struct screen * screen; + struct screen *screen; - if (view_set_position(view, x, y)) - view_update_screens(view); + if (view_set_position(view, x, y)) + view_update_screens(view); - wl_list_for_each(screen, &swc.screens, link) - { - view_move(&screen->planes.cursor.view, - view->geometry.x, view->geometry.y); - } + wl_list_for_each (screen, &swc.screens, link) { + view_move(&screen->planes.cursor.view, + view->geometry.x, view->geometry.y); + } - return true; + return true; } static const struct view_impl view_impl = { - .update = &update, - .attach = &attach, - .move = &move, + .update = &update, + .attach = &attach, + .move = &move, }; -static inline void update_cursor(struct pointer * pointer) +static inline void +update_cursor(struct pointer *pointer) { - view_move(&pointer->cursor.view, - wl_fixed_to_int(pointer->x) - pointer->cursor.hotspot.x, - wl_fixed_to_int(pointer->y) - pointer->cursor.hotspot.y); + view_move(&pointer->cursor.view, + wl_fixed_to_int(pointer->x) - pointer->cursor.hotspot.x, + wl_fixed_to_int(pointer->y) - pointer->cursor.hotspot.y); } -void pointer_set_cursor(struct pointer * pointer, uint32_t id) +void +pointer_set_cursor(struct pointer *pointer, uint32_t id) { - struct cursor * cursor = &cursor_metadata[id]; - union wld_object object = { .ptr = &cursor_data[cursor->offset] }; - - if (pointer->cursor.internal_buffer) - wld_buffer_unreference(pointer->cursor.internal_buffer); - - pointer->cursor.internal_buffer = wld_import_buffer - (swc.shm->context, WLD_OBJECT_DATA, object, - cursor->width, cursor->height, WLD_FORMAT_ARGB8888, cursor->width * 4); - - if (!pointer->cursor.internal_buffer) - { - ERROR("Failed to create cursor buffer\n"); - return; - } - - pointer->cursor.hotspot.x = cursor->hotspot_x; - pointer->cursor.hotspot.y = cursor->hotspot_y; - update_cursor(pointer); - view_attach(&pointer->cursor.view, pointer->cursor.internal_buffer); + struct cursor *cursor = &cursor_metadata[id]; + union wld_object object = {.ptr = &cursor_data[cursor->offset] }; + + if (pointer->cursor.internal_buffer) + wld_buffer_unreference(pointer->cursor.internal_buffer); + + pointer->cursor.internal_buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, + cursor->width, cursor->height, WLD_FORMAT_ARGB8888, cursor->width * 4); + + if (!pointer->cursor.internal_buffer) { + ERROR("Failed to create cursor buffer\n"); + return; + } + + pointer->cursor.hotspot.x = cursor->hotspot_x; + pointer->cursor.hotspot.y = cursor->hotspot_y; + update_cursor(pointer); + view_attach(&pointer->cursor.view, pointer->cursor.internal_buffer); } -static bool client_handle_button - (struct pointer_handler * handler, uint32_t time, - struct button * button, uint32_t state) +static bool +client_handle_button(struct pointer_handler *handler, uint32_t time, + struct button *button, uint32_t state) { - struct pointer * pointer - = wl_container_of(handler, pointer, client_handler); + struct pointer *pointer = wl_container_of(handler, pointer, client_handler); - if (!pointer->focus.resource) - return false; + if (!pointer->focus.resource) + return false; - wl_pointer_send_button(pointer->focus.resource, button->press.serial, time, - button->press.value, state); + wl_pointer_send_button(pointer->focus.resource, button->press.serial, time, + button->press.value, state); - return true; + return true; } -static bool client_handle_axis(struct pointer_handler * handler, - uint32_t time, uint32_t axis, wl_fixed_t amount) +static bool +client_handle_axis(struct pointer_handler *handler, + uint32_t time, uint32_t axis, wl_fixed_t amount) { - struct pointer * pointer - = wl_container_of(handler, pointer, client_handler); + struct pointer *pointer = wl_container_of(handler, pointer, client_handler); - if (!pointer->focus.resource) - return false; + if (!pointer->focus.resource) + return false; - wl_pointer_send_axis(pointer->focus.resource, time, axis, amount); + wl_pointer_send_axis(pointer->focus.resource, time, axis, amount); - return true; + return true; } -static bool client_handle_motion(struct pointer_handler * handler, - uint32_t time, wl_fixed_t x, wl_fixed_t y) +static bool +client_handle_motion(struct pointer_handler *handler, + uint32_t time, wl_fixed_t x, wl_fixed_t y) { - struct pointer * pointer - = wl_container_of(handler, pointer, client_handler); + struct pointer *pointer = wl_container_of(handler, pointer, client_handler); - if (!pointer->focus.resource) - return false; + if (!pointer->focus.resource) + return false; - wl_pointer_send_motion - (pointer->focus.resource, time, - x - wl_fixed_from_int(pointer->focus.view->base.geometry.x), - y - wl_fixed_from_int(pointer->focus.view->base.geometry.y)); + wl_pointer_send_motion(pointer->focus.resource, time, + x - wl_fixed_from_int(pointer->focus.view->base.geometry.x), + y - wl_fixed_from_int(pointer->focus.view->base.geometry.y)); - return true; + return true; } -bool pointer_initialize(struct pointer * pointer) +bool +pointer_initialize(struct pointer *pointer) { - struct screen * screen; - - /* Center cursor in the geometry of the first screen. */ - 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 - (screen->base.geometry.y + screen->base.geometry.height / 2); - - pointer->focus_handler.enter = &enter; - pointer->focus_handler.leave = &leave; - pointer->client_handler.button = &client_handle_button; - pointer->client_handler.axis = &client_handle_axis; - pointer->client_handler.motion = &client_handle_motion; - wl_list_init(&pointer->handlers); - wl_list_insert(&pointer->handlers, &pointer->client_handler.link); - wl_array_init(&pointer->buttons); - - view_initialize(&pointer->cursor.view, &view_impl); - pointer->cursor.surface = NULL; - pointer->cursor.destroy_listener.notify = &handle_cursor_surface_destroy; - pointer->cursor.buffer = wld_create_buffer - (swc.drm->context, 64, 64, WLD_FORMAT_ARGB8888, WLD_FLAG_MAP); - pointer->cursor.internal_buffer = NULL; - - if (!pointer->cursor.buffer) - return false; - - pointer_set_cursor(pointer, cursor_left_ptr); - - wl_list_for_each(screen, &swc.screens, link) - view_attach(&screen->planes.cursor.view, pointer->cursor.buffer); - - input_focus_initialize(&pointer->focus, &pointer->focus_handler); - pixman_region32_init(&pointer->region); - - return true; + struct screen *screen; + + /* Center cursor in the geometry of the first screen. */ + 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(screen->base.geometry.y + screen->base.geometry.height / 2); + + pointer->focus_handler.enter = &enter; + pointer->focus_handler.leave = &leave; + pointer->client_handler.button = &client_handle_button; + pointer->client_handler.axis = &client_handle_axis; + pointer->client_handler.motion = &client_handle_motion; + wl_list_init(&pointer->handlers); + wl_list_insert(&pointer->handlers, &pointer->client_handler.link); + wl_array_init(&pointer->buttons); + + view_initialize(&pointer->cursor.view, &view_impl); + pointer->cursor.surface = NULL; + pointer->cursor.destroy_listener.notify = &handle_cursor_surface_destroy; + pointer->cursor.buffer = wld_create_buffer(swc.drm->context, 64, 64, WLD_FORMAT_ARGB8888, WLD_FLAG_MAP); + pointer->cursor.internal_buffer = NULL; + + if (!pointer->cursor.buffer) + return false; + + pointer_set_cursor(pointer, cursor_left_ptr); + + wl_list_for_each (screen, &swc.screens, link) + view_attach(&screen->planes.cursor.view, pointer->cursor.buffer); + + input_focus_initialize(&pointer->focus, &pointer->focus_handler); + pixman_region32_init(&pointer->region); + + return true; } -void pointer_finalize(struct pointer * pointer) +void +pointer_finalize(struct pointer *pointer) { - input_focus_finalize(&pointer->focus); - pixman_region32_fini(&pointer->region); + input_focus_finalize(&pointer->focus); + pixman_region32_fini(&pointer->region); } -void pointer_set_focus(struct pointer * pointer, struct compositor_view * view) +void +pointer_set_focus(struct pointer *pointer, struct compositor_view *view) { - input_focus_set(&pointer->focus, view); + input_focus_set(&pointer->focus, view); } -static void clip_position(struct pointer * pointer, - wl_fixed_t fx, wl_fixed_t fy) +static void +clip_position(struct pointer *pointer, + wl_fixed_t fx, wl_fixed_t fy) { - int32_t x, y, last_x, last_y; - pixman_box32_t box; - - x = wl_fixed_to_int(fx); - y = wl_fixed_to_int(fy); - last_x = wl_fixed_to_int(pointer->x); - last_y = wl_fixed_to_int(pointer->y); - - if (!pixman_region32_contains_point(&pointer->region, x, y, NULL)) - { - assert(pixman_region32_contains_point(&pointer->region, - last_x, last_y, &box)); - - /* Do some clipping. */ - x = MAX(MIN(x, box.x2 - 1), box.x1); - y = MAX(MIN(y, box.y2 - 1), box.y1); - } - - pointer->x = wl_fixed_from_int(x); - pointer->y = wl_fixed_from_int(y); + int32_t x, y, last_x, last_y; + pixman_box32_t box; + + x = wl_fixed_to_int(fx); + y = wl_fixed_to_int(fy); + last_x = wl_fixed_to_int(pointer->x); + last_y = wl_fixed_to_int(pointer->y); + + if (!pixman_region32_contains_point(&pointer->region, x, y, NULL)) { + assert(pixman_region32_contains_point(&pointer->region, + last_x, last_y, &box)); + + /* Do some clipping. */ + x = MAX(MIN(x, box.x2 - 1), box.x1); + y = MAX(MIN(y, box.y2 - 1), box.y1); + } + + pointer->x = wl_fixed_from_int(x); + pointer->y = wl_fixed_from_int(y); } -void pointer_set_region(struct pointer * pointer, pixman_region32_t * region) +void +pointer_set_region(struct pointer *pointer, pixman_region32_t *region) { - pixman_region32_copy(&pointer->region, region); - clip_position(pointer, pointer->x, pointer->y); + pixman_region32_copy(&pointer->region, region); + clip_position(pointer, pointer->x, pointer->y); } -static void set_cursor(struct wl_client * client, - struct wl_resource * resource, uint32_t serial, - struct wl_resource * surface_resource, - int32_t hotspot_x, int32_t hotspot_y) +static void +set_cursor(struct wl_client *client, + struct wl_resource *resource, uint32_t serial, + struct wl_resource *surface_resource, + int32_t hotspot_x, int32_t hotspot_y) { - struct pointer * pointer = wl_resource_get_user_data(resource); - struct surface * surface; - - if (!pointer->focus.resource || client != wl_resource_get_client(pointer->focus.resource)) - return; - - if (pointer->cursor.surface) - wl_list_remove(&pointer->cursor.destroy_listener.link); - - surface = surface_resource ? wl_resource_get_user_data(surface_resource) - : NULL; - pointer->cursor.surface = surface; - pointer->cursor.hotspot.x = hotspot_x; - pointer->cursor.hotspot.y = hotspot_y; - - if (surface) - { - surface_set_view(surface, &pointer->cursor.view); - wl_resource_add_destroy_listener(surface->resource, - &pointer->cursor.destroy_listener); - update_cursor(pointer); - } + struct pointer *pointer = wl_resource_get_user_data(resource); + struct surface *surface; + + if (!pointer->focus.resource || client != wl_resource_get_client(pointer->focus.resource)) + return; + + if (pointer->cursor.surface) + wl_list_remove(&pointer->cursor.destroy_listener.link); + + surface = surface_resource ? wl_resource_get_user_data(surface_resource) + : NULL; + pointer->cursor.surface = surface; + pointer->cursor.hotspot.x = hotspot_x; + pointer->cursor.hotspot.y = hotspot_y; + + if (surface) { + surface_set_view(surface, &pointer->cursor.view); + wl_resource_add_destroy_listener(surface->resource, + &pointer->cursor.destroy_listener); + update_cursor(pointer); + } } -static void release(struct wl_client * client, struct wl_resource * resource) +static void +release(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } static struct wl_pointer_interface pointer_implementation = { - .set_cursor = &set_cursor, - .release = &release, + .set_cursor = &set_cursor, + .release = &release, }; -static void unbind(struct wl_resource * resource) +static void +unbind(struct wl_resource *resource) { - struct pointer * pointer = wl_resource_get_user_data(resource); + struct pointer *pointer = wl_resource_get_user_data(resource); - input_focus_remove_resource(&pointer->focus, resource); + input_focus_remove_resource(&pointer->focus, resource); } -struct wl_resource * pointer_bind(struct pointer * pointer, - struct wl_client * client, - uint32_t version, uint32_t id) +struct wl_resource * +pointer_bind(struct pointer *pointer, + struct wl_client *client, + uint32_t version, uint32_t id) { - struct wl_resource * client_resource; + struct wl_resource *client_resource; - client_resource = wl_resource_create(client, &wl_pointer_interface, - version, id); - wl_resource_set_implementation(client_resource, &pointer_implementation, - pointer, &unbind); - input_focus_add_resource(&pointer->focus, client_resource); + client_resource = wl_resource_create(client, &wl_pointer_interface, + version, id); + wl_resource_set_implementation(client_resource, &pointer_implementation, + pointer, &unbind); + input_focus_add_resource(&pointer->focus, client_resource); - return client_resource; + return client_resource; } -struct button * pointer_get_button(struct pointer * pointer, uint32_t serial) +struct button * +pointer_get_button(struct pointer *pointer, uint32_t serial) { - struct button * button; + struct button *button; - wl_array_for_each(button, &pointer->buttons) - { - if (button->press.serial == serial) - return button; - } + wl_array_for_each (button, &pointer->buttons) { + if (button->press.serial == serial) + return button; + } - return NULL; + return NULL; } -void pointer_handle_button(struct pointer * pointer, uint32_t time, - uint32_t value, uint32_t state) +void +pointer_handle_button(struct pointer *pointer, uint32_t time, + uint32_t value, uint32_t state) { - struct pointer_handler * handler; - struct button * button; - uint32_t serial; - - serial = wl_display_next_serial(swc.display); - - if (state == WL_POINTER_BUTTON_STATE_RELEASED) - { - wl_array_for_each(button, &pointer->buttons) - { - if (button->press.value == value) - { - if (button->handler) - { - button->press.serial = serial; - button->handler->button(button->handler, time, - button, state); - } - - array_remove(&pointer->buttons, button, sizeof *button); - break; - } - } - } - else - { - button = wl_array_add(&pointer->buttons, sizeof *button); - - if (!button) - return; - - button->press.value = value; - button->press.serial = serial; - button->handler = NULL; - - wl_list_for_each(handler, &pointer->handlers, link) - { - if (handler->button && handler->button(handler, time, - button, state)) - { - button->handler = handler; - break; - } - } - } + struct pointer_handler *handler; + struct button *button; + uint32_t serial; + + serial = wl_display_next_serial(swc.display); + + if (state == WL_POINTER_BUTTON_STATE_RELEASED) { + wl_array_for_each (button, &pointer->buttons) { + if (button->press.value == value) { + if (button->handler) { + button->press.serial = serial; + button->handler->button(button->handler, time, + button, state); + } + + array_remove(&pointer->buttons, button, sizeof *button); + break; + } + } + } else { + button = wl_array_add(&pointer->buttons, sizeof *button); + + if (!button) + return; + + button->press.value = value; + button->press.serial = serial; + button->handler = NULL; + + wl_list_for_each (handler, &pointer->handlers, link) { + if (handler->button && handler->button(handler, time, button, state)) { + button->handler = handler; + break; + } + } + } } -void pointer_handle_axis(struct pointer * pointer, uint32_t time, - uint32_t axis, wl_fixed_t amount) +void +pointer_handle_axis(struct pointer *pointer, uint32_t time, + uint32_t axis, wl_fixed_t amount) { - struct pointer_handler * handler; + struct pointer_handler *handler; - wl_list_for_each(handler, &pointer->handlers, link) - { - if (handler->axis && handler->axis(handler, time, axis, amount)) - break; - } + wl_list_for_each (handler, &pointer->handlers, link) { + if (handler->axis && handler->axis(handler, time, axis, amount)) + break; + } } -void pointer_handle_relative_motion(struct pointer * pointer, uint32_t time, - wl_fixed_t dx, wl_fixed_t dy) +void +pointer_handle_relative_motion(struct pointer *pointer, uint32_t time, + wl_fixed_t dx, wl_fixed_t dy) { - struct pointer_handler * handler; + struct pointer_handler *handler; - clip_position(pointer, pointer->x + dx, pointer->y + dy); + clip_position(pointer, pointer->x + dx, pointer->y + dy); - wl_list_for_each(handler, &pointer->handlers, link) - { - if (handler->motion && handler->motion(handler, time, - pointer->x, pointer->y)) - { - break; - } - } + wl_list_for_each (handler, &pointer->handlers, link) { + if (handler->motion && handler->motion(handler, time, pointer->x, pointer->y)) { + break; + } + } - update_cursor(pointer); + update_cursor(pointer); } - diff --git a/libswc/pointer.h b/libswc/pointer.h @@ -31,70 +31,66 @@ #include <wayland-server.h> #include <pixman.h> -struct button -{ - struct press press; - struct pointer_handler * handler; +struct button { + struct press press; + struct pointer_handler *handler; }; -struct pointer_handler -{ - bool (* motion)(struct pointer_handler * handler, uint32_t time, - wl_fixed_t x, wl_fixed_t y); - bool (* button)(struct pointer_handler * handler, uint32_t time, - struct button * button, uint32_t state); - bool (* axis)(struct pointer_handler * handler, uint32_t time, - enum wl_pointer_axis axis, wl_fixed_t amount); +struct pointer_handler { + bool (*motion)(struct pointer_handler *handler, uint32_t time, + wl_fixed_t x, wl_fixed_t y); + bool (*button)(struct pointer_handler *handler, uint32_t time, + struct button *button, uint32_t state); + bool (*axis)(struct pointer_handler *handler, uint32_t time, + enum wl_pointer_axis axis, wl_fixed_t amount); - struct wl_list link; + struct wl_list link; }; -struct pointer -{ - struct input_focus focus; - struct input_focus_handler focus_handler; - - struct - { - struct view view; - struct surface * surface; - struct wl_listener destroy_listener; - struct wld_buffer * buffer; - - /* Used for cursors set with pointer_set_cursor */ - struct wld_buffer * internal_buffer; - - struct - { - int32_t x, y; - } hotspot; - } cursor; - - struct wl_array buttons; - struct wl_list handlers; - struct pointer_handler client_handler; - - wl_fixed_t x, y; - pixman_region32_t region; +struct pointer { + struct input_focus focus; + struct input_focus_handler focus_handler; + + struct + { + struct view view; + struct surface *surface; + struct wl_listener destroy_listener; + struct wld_buffer *buffer; + + /* Used for cursors set with pointer_set_cursor */ + struct wld_buffer *internal_buffer; + + struct + { + int32_t x, y; + } hotspot; + } cursor; + + struct wl_array buttons; + struct wl_list handlers; + struct pointer_handler client_handler; + + wl_fixed_t x, y; + pixman_region32_t region; }; -bool pointer_initialize(struct pointer * pointer); -void pointer_finalize(struct pointer * pointer); -void pointer_set_focus(struct pointer * pointer, struct compositor_view * view); -void pointer_set_region(struct pointer * pointer, pixman_region32_t * region); -void pointer_set_cursor(struct pointer * pointer, uint32_t id); +bool pointer_initialize(struct pointer *pointer); +void pointer_finalize(struct pointer *pointer); +void pointer_set_focus(struct pointer *pointer, struct compositor_view *view); +void pointer_set_region(struct pointer *pointer, pixman_region32_t *region); +void pointer_set_cursor(struct pointer *pointer, uint32_t id); -struct button * pointer_get_button(struct pointer * pointer, uint32_t serial); +struct button *pointer_get_button(struct pointer *pointer, uint32_t serial); -struct wl_resource * pointer_bind(struct pointer * pointer, - struct wl_client * client, - uint32_t version, uint32_t id); -void pointer_handle_button(struct pointer * pointer, uint32_t time, +struct wl_resource *pointer_bind(struct pointer *pointer, + struct wl_client *client, + uint32_t version, uint32_t id); +void pointer_handle_button(struct pointer *pointer, uint32_t time, uint32_t button, uint32_t state); -void pointer_handle_axis(struct pointer * pointer, uint32_t time, +void pointer_handle_axis(struct pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t amount); -void pointer_handle_relative_motion(struct pointer * pointer, uint32_t time, +void pointer_handle_relative_motion(struct pointer *pointer, uint32_t time, wl_fixed_t dx, wl_fixed_t dy); #endif - diff --git a/libswc/region.c b/libswc/region.c @@ -3,65 +3,69 @@ #include <stdlib.h> #include <wayland-server.h> -static void destroy(struct wl_client * client, struct wl_resource * resource) +static void +destroy(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } -static void add(struct wl_client * client, struct wl_resource * resource, - int32_t x, int32_t y, int32_t width, int32_t height) +static void +add(struct wl_client *client, struct wl_resource *resource, + int32_t x, int32_t y, int32_t width, int32_t height) { - struct region * region = wl_resource_get_user_data(resource); + struct region *region = wl_resource_get_user_data(resource); - pixman_region32_union_rect(&region->region, &region->region, - x, y, width, height); + pixman_region32_union_rect(&region->region, &region->region, + x, y, width, height); } -static void subtract(struct wl_client * client, struct wl_resource * resource, - int32_t x, int32_t y, int32_t width, int32_t height) +static void +subtract(struct wl_client *client, struct wl_resource *resource, + int32_t x, int32_t y, int32_t width, int32_t height) { - struct region * region = wl_resource_get_user_data(resource); - pixman_region32_t operand; + struct region *region = wl_resource_get_user_data(resource); + pixman_region32_t operand; - pixman_region32_init_rect(&operand, x, y, width, height); - pixman_region32_subtract(&region->region, &region->region, &operand); + pixman_region32_init_rect(&operand, x, y, width, height); + pixman_region32_subtract(&region->region, &region->region, &operand); } static const struct wl_region_interface region_implementation = { - .destroy = &destroy, - .add = &add, - .subtract = &subtract + .destroy = &destroy, + .add = &add, + .subtract = &subtract }; -static void region_destroy(struct wl_resource * resource) +static void +region_destroy(struct wl_resource *resource) { - struct region * region = wl_resource_get_user_data(resource); + struct region *region = wl_resource_get_user_data(resource); - /* Finish the region. */ - pixman_region32_fini(&region->region); + /* Finish the region. */ + pixman_region32_fini(&region->region); - free(region); + free(region); } -struct region * region_new(struct wl_client * client, - uint32_t version, uint32_t id) +struct region * +region_new(struct wl_client *client, + uint32_t version, uint32_t id) { - struct region * region; + struct region *region; - region = malloc(sizeof *region); + region = malloc(sizeof *region); - if (!region) - return NULL; + if (!region) + return NULL; - /* Initialize the region. */ - pixman_region32_init(&region->region); + /* Initialize the region. */ + pixman_region32_init(&region->region); - /* Add the region to the client. */ - region->resource = wl_resource_create(client, &wl_region_interface, - version, id); - wl_resource_set_implementation(region->resource, &region_implementation, - region, &region_destroy); + /* Add the region to the client. */ + region->resource = wl_resource_create(client, &wl_region_interface, + version, id); + wl_resource_set_implementation(region->resource, &region_implementation, + region, &region_destroy); - return region; + return region; } - diff --git a/libswc/region.h b/libswc/region.h @@ -6,14 +6,12 @@ struct wl_client; -struct region -{ - struct wl_resource * resource; - pixman_region32_t region; +struct region { + struct wl_resource *resource; + pixman_region32_t region; }; -struct region * region_new(struct wl_client * client, - uint32_t version, uint32_t id); +struct region *region_new(struct wl_client *client, + uint32_t version, uint32_t id); #endif - diff --git a/libswc/screen.c b/libswc/screen.c @@ -34,206 +34,204 @@ #include <stdlib.h> #include <sys/param.h> -#define INTERNAL(s) ((struct screen *) (s)) +#define INTERNAL(s) ((struct screen *)(s)) -static struct screen * active_screen; +static struct screen *active_screen; static const struct swc_screen_handler null_handler; -static bool handle_motion(struct pointer_handler * handler, uint32_t time, +static bool handle_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t x, wl_fixed_t y); struct pointer_handler screens_pointer_handler = { - .motion = &handle_motion + .motion = &handle_motion }; EXPORT -void swc_screen_set_handler(struct swc_screen * base, - const struct swc_screen_handler * handler, - void * data) +void +swc_screen_set_handler(struct swc_screen *base, + const struct swc_screen_handler *handler, + void *data) { - struct screen * screen = INTERNAL(base); + struct screen *screen = INTERNAL(base); - screen->handler = handler; - screen->handler_data = data; + screen->handler = handler; + screen->handler_data = data; } -bool screens_initialize(void) +bool +screens_initialize(void) { - wl_list_init(&swc.screens); + wl_list_init(&swc.screens); - if (!drm_create_screens(&swc.screens)) - return false; + if (!drm_create_screens(&swc.screens)) + return false; - if (wl_list_empty(&swc.screens)) - return false; + if (wl_list_empty(&swc.screens)) + return false; - return true; + return true; } -void screens_finalize(void) +void +screens_finalize(void) { - struct screen * screen, * tmp; + struct screen *screen, *tmp; - wl_list_for_each_safe(screen, tmp, &swc.screens, link) - screen_destroy(screen); + wl_list_for_each_safe (screen, tmp, &swc.screens, link) + screen_destroy(screen); } -static void bind_screen(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_screen(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct screen * screen = data; - struct wl_resource * resource; + struct screen *screen = data; + struct wl_resource *resource; - if (version > 1) - version = 1; + if (version > 1) + version = 1; - resource = wl_resource_create(client, &swc_screen_interface, version, id); + resource = wl_resource_create(client, &swc_screen_interface, version, id); - if (!resource) - { - wl_client_post_no_memory(client); - return; - } + if (!resource) { + wl_client_post_no_memory(client); + return; + } - wl_resource_set_implementation(resource, NULL, screen, &remove_resource); - wl_list_insert(&screen->resources, wl_resource_get_link(resource)); + wl_resource_set_implementation(resource, NULL, screen, &remove_resource); + wl_list_insert(&screen->resources, wl_resource_get_link(resource)); } -struct screen * screen_new(uint32_t crtc, struct output * output) +struct screen * +screen_new(uint32_t crtc, struct output *output) { - struct screen * screen; - int32_t x = 0; - - /* Simple heuristic for initial screen positioning. */ - wl_list_for_each(screen, &swc.screens, link) - x = MAX(x, screen->base.geometry.x + screen->base.geometry.width); - - if (!(screen = malloc(sizeof *screen))) - goto error0; - - screen->global = wl_global_create(swc.display, &swc_screen_interface, 1, - screen, &bind_screen); - - if (!screen->global) - { - ERROR("Failed to create screen global\n"); - goto error1; - } - - if (!framebuffer_plane_initialize(&screen->planes.framebuffer, crtc, - output->preferred_mode, - &output->connector, 1)) - { - ERROR("Failed to initialize framebuffer plane\n"); - goto error2; - } - - if (!cursor_plane_initialize(&screen->planes.cursor, crtc, - &screen->base.geometry)) - { - ERROR("Failed to initialize cursor plane\n"); - goto error3; - } - - screen->handler = &null_handler; - wl_signal_init(&screen->destroy_signal); - wl_list_init(&screen->resources); - wl_list_init(&screen->outputs); - wl_list_insert(&screen->outputs, &output->link); - wl_list_init(&screen->modifiers); - - view_move(&screen->planes.framebuffer.view, x, 0); - screen->base.geometry = screen->planes.framebuffer.view.geometry; - screen->base.usable_geometry = screen->base.geometry; - - swc.manager->new_screen(&screen->base); - - return screen; - - error3: - framebuffer_plane_finalize(&screen->planes.framebuffer); - error2: - wl_global_destroy(screen->global); - error1: - free(screen); - error0: - return NULL; + struct screen *screen; + int32_t x = 0; + + /* Simple heuristic for initial screen positioning. */ + wl_list_for_each (screen, &swc.screens, link) + x = MAX(x, screen->base.geometry.x + screen->base.geometry.width); + + if (!(screen = malloc(sizeof *screen))) + goto error0; + + screen->global = wl_global_create(swc.display, &swc_screen_interface, 1, + screen, &bind_screen); + + if (!screen->global) { + ERROR("Failed to create screen global\n"); + goto error1; + } + + if (!framebuffer_plane_initialize(&screen->planes.framebuffer, crtc, + output->preferred_mode, + &output->connector, 1)) { + ERROR("Failed to initialize framebuffer plane\n"); + goto error2; + } + + if (!cursor_plane_initialize(&screen->planes.cursor, crtc, + &screen->base.geometry)) { + ERROR("Failed to initialize cursor plane\n"); + goto error3; + } + + screen->handler = &null_handler; + wl_signal_init(&screen->destroy_signal); + wl_list_init(&screen->resources); + wl_list_init(&screen->outputs); + wl_list_insert(&screen->outputs, &output->link); + wl_list_init(&screen->modifiers); + + view_move(&screen->planes.framebuffer.view, x, 0); + screen->base.geometry = screen->planes.framebuffer.view.geometry; + screen->base.usable_geometry = screen->base.geometry; + + swc.manager->new_screen(&screen->base); + + return screen; + +error3: + framebuffer_plane_finalize(&screen->planes.framebuffer); +error2: + wl_global_destroy(screen->global); +error1: + free(screen); +error0: + return NULL; } -void screen_destroy(struct screen * screen) +void +screen_destroy(struct screen *screen) { - struct output * output, * next; - - if (active_screen == screen) - active_screen = NULL; - if (screen->handler->destroy) - screen->handler->destroy(screen->handler_data); - wl_signal_emit(&screen->destroy_signal, NULL); - wl_list_for_each_safe(output, next, &screen->outputs, link) - output_destroy(output); - framebuffer_plane_finalize(&screen->planes.framebuffer); - cursor_plane_finalize(&screen->planes.cursor); - free(screen); + struct output *output, *next; + + if (active_screen == screen) + active_screen = NULL; + if (screen->handler->destroy) + screen->handler->destroy(screen->handler_data); + wl_signal_emit(&screen->destroy_signal, NULL); + wl_list_for_each_safe (output, next, &screen->outputs, link) + output_destroy(output); + framebuffer_plane_finalize(&screen->planes.framebuffer); + cursor_plane_finalize(&screen->planes.cursor); + free(screen); } -void screen_update_usable_geometry(struct screen * screen) +void +screen_update_usable_geometry(struct screen *screen) { - pixman_region32_t total_usable, usable; - pixman_box32_t * extents; - struct screen_modifier * modifier; - - DEBUG("Updating usable geometry\n"); - - pixman_region32_init_rect(&total_usable, - screen->base.geometry.x, screen->base.geometry.y, - screen->base.geometry.width, - screen->base.geometry.height); - pixman_region32_init(&usable); - - wl_list_for_each(modifier, &screen->modifiers, link) - { - modifier->modify(modifier, &screen->base.geometry, &usable); - pixman_region32_intersect(&total_usable, &total_usable, &usable); - } - - extents = pixman_region32_extents(&total_usable); - - if (extents->x1 != screen->base.usable_geometry.x - || extents->y1 != screen->base.usable_geometry.y - || (extents->x2 - extents->x1) != screen->base.usable_geometry.width - || (extents->y2 - extents->y1) != screen->base.usable_geometry.height) - { - screen->base.usable_geometry.x = extents->x1; - screen->base.usable_geometry.y = extents->y1; - screen->base.usable_geometry.width = extents->x2 - extents->x1; - screen->base.usable_geometry.height = extents->y2 - extents->y1; - - if (screen->handler->usable_geometry_changed) - screen->handler->usable_geometry_changed(screen->handler_data); - } + pixman_region32_t total_usable, usable; + pixman_box32_t *extents; + struct screen_modifier *modifier; + + DEBUG("Updating usable geometry\n"); + + pixman_region32_init_rect(&total_usable, + screen->base.geometry.x, screen->base.geometry.y, + screen->base.geometry.width, + screen->base.geometry.height); + pixman_region32_init(&usable); + + wl_list_for_each (modifier, &screen->modifiers, link) { + modifier->modify(modifier, &screen->base.geometry, &usable); + pixman_region32_intersect(&total_usable, &total_usable, &usable); + } + + extents = pixman_region32_extents(&total_usable); + + if (extents->x1 != screen->base.usable_geometry.x + || extents->y1 != screen->base.usable_geometry.y + || (extents->x2 - extents->x1) != screen->base.usable_geometry.width + || (extents->y2 - extents->y1) != screen->base.usable_geometry.height) { + screen->base.usable_geometry.x = extents->x1; + screen->base.usable_geometry.y = extents->y1; + screen->base.usable_geometry.width = extents->x2 - extents->x1; + screen->base.usable_geometry.height = extents->y2 - extents->y1; + + if (screen->handler->usable_geometry_changed) + screen->handler->usable_geometry_changed(screen->handler_data); + } } -bool handle_motion(struct pointer_handler * handler, uint32_t time, - wl_fixed_t fx, wl_fixed_t fy) +bool +handle_motion(struct pointer_handler *handler, uint32_t time, + wl_fixed_t fx, wl_fixed_t fy) { - struct screen * screen; - int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy); - - wl_list_for_each(screen, &swc.screens, link) - { - if (rectangle_contains_point(&screen->base.geometry, x, y)) - { - if (screen != active_screen) - { - active_screen = screen; - - if (screen->handler->entered) - screen->handler->entered(screen->handler_data); - } - break; - } - } - - return false; + struct screen *screen; + int32_t x = wl_fixed_to_int(fx), y = wl_fixed_to_int(fy); + + wl_list_for_each (screen, &swc.screens, link) { + if (rectangle_contains_point(&screen->base.geometry, x, y)) { + if (screen != active_screen) { + active_screen = screen; + + if (screen->handler->entered) + screen->handler->entered(screen->handler_data); + } + break; + } + } + + return false; } - diff --git a/libswc/screen.h b/libswc/screen.h @@ -33,54 +33,52 @@ struct output; struct pixman_region32; -struct screen_modifier -{ - /** +struct screen_modifier { + /** * Takes the screen geometry and sets 'usable' to the usable region of the * screen. 'usable' is an already initialized pixman region. */ - void (* modify)(struct screen_modifier * modifier, - const struct swc_rectangle * geometry, - struct pixman_region32 * usable); + void (*modify)(struct screen_modifier *modifier, + const struct swc_rectangle *geometry, + struct pixman_region32 *usable); - struct wl_list link; + struct wl_list link; }; -struct screen -{ - struct swc_screen base; - const struct swc_screen_handler * handler; - void * handler_data; +struct screen { + struct swc_screen base; + const struct swc_screen_handler *handler; + void *handler_data; - struct wl_signal destroy_signal; - uint8_t id; + struct wl_signal destroy_signal; + uint8_t id; - struct - { - struct framebuffer_plane framebuffer; - struct cursor_plane cursor; - } planes; + struct + { + struct framebuffer_plane framebuffer; + struct cursor_plane cursor; + } planes; - struct wl_global * global; - struct wl_list resources; + struct wl_global *global; + struct wl_list resources; - struct wl_list outputs; - struct wl_list modifiers; - struct wl_list link; + struct wl_list outputs; + struct wl_list modifiers; + struct wl_list link; }; bool screens_initialize(void); void screens_finalize(void); -struct screen * screen_new(uint32_t crtc, struct output * output); -void screen_destroy(struct screen * screen); +struct screen *screen_new(uint32_t crtc, struct output *output); +void screen_destroy(struct screen *screen); -static inline uint32_t screen_mask(struct screen * screen) +static inline uint32_t +screen_mask(struct screen *screen) { - return 1 << screen->id; + return 1 << screen->id; } -void screen_update_usable_geometry(struct screen * screen); +void screen_update_usable_geometry(struct screen *screen); #endif - diff --git a/libswc/seat.c b/libswc/seat.c @@ -39,509 +39,499 @@ #include <string.h> #include <unistd.h> #ifdef ENABLE_LIBINPUT -# include <libudev.h> -# include <libinput.h> +#include <libudev.h> +#include <libinput.h> #endif static struct -{ - char * name; - uint32_t capabilities; + { + char *name; + uint32_t capabilities; #ifdef ENABLE_LIBINPUT - struct udev * udev; - struct libinput * libinput; - struct wl_event_source * libinput_source; + struct udev *udev; + struct libinput *libinput; + struct wl_event_source *libinput_source; #else - struct wl_list devices; + struct wl_list devices; #endif - struct wl_listener swc_listener; + struct wl_listener swc_listener; - struct keyboard keyboard; - struct pointer pointer; - struct data_device data_device; + struct keyboard keyboard; + struct pointer pointer; + struct data_device data_device; - struct wl_global * global; - struct wl_list resources; + struct wl_global *global; + struct wl_list resources; } seat; const struct swc_seat swc_seat = { - .pointer = &seat.pointer, - .keyboard = &seat.keyboard, - .data_device = &seat.data_device + .pointer = &seat.pointer, + .keyboard = &seat.keyboard, + .data_device = &seat.data_device }; -static void handle_key(uint32_t time, uint32_t key, uint32_t state) +static void +handle_key(uint32_t time, uint32_t key, uint32_t state) { - keyboard_handle_key(&seat.keyboard, time, key, state); + keyboard_handle_key(&seat.keyboard, time, key, state); } -static void handle_button(uint32_t time, uint32_t button, uint32_t state) +static void +handle_button(uint32_t time, uint32_t button, uint32_t state) { - pointer_handle_button(&seat.pointer, time, button, state); + pointer_handle_button(&seat.pointer, time, button, state); } -static void handle_axis(uint32_t time, uint32_t axis, wl_fixed_t amount) +static void +handle_axis(uint32_t time, uint32_t axis, wl_fixed_t amount) { - pointer_handle_axis(&seat.pointer, time, axis, amount); + pointer_handle_axis(&seat.pointer, time, axis, amount); } -static void handle_relative_motion(uint32_t time, wl_fixed_t dx, wl_fixed_t dy) +static void +handle_relative_motion(uint32_t time, wl_fixed_t dx, wl_fixed_t dy) { - pointer_handle_relative_motion(&seat.pointer, time, dx, dy); + pointer_handle_relative_motion(&seat.pointer, time, dx, dy); } -static void handle_keyboard_focus_event(struct wl_listener * listener, - void * data) +static void +handle_keyboard_focus_event(struct wl_listener *listener, + void *data) { - struct event * event = data; - struct input_focus_event_data * event_data = event->data; - - switch (event->type) - { - case INPUT_FOCUS_EVENT_CHANGED: - if (event_data->new) - { - struct wl_client * client = wl_resource_get_client - (event_data->new->surface->resource); - - /* Offer the selection to the new focus. */ - data_device_offer_selection(&seat.data_device, client); - } - break; - } + struct event *event = data; + struct input_focus_event_data *event_data = event->data; + + switch (event->type) { + case INPUT_FOCUS_EVENT_CHANGED: + if (event_data->new) { + struct wl_client *client = wl_resource_get_client(event_data->new->surface->resource); + + /* Offer the selection to the new focus. */ + data_device_offer_selection(&seat.data_device, client); + } + break; + } } static struct wl_listener keyboard_focus_listener = { - .notify = &handle_keyboard_focus_event + .notify = &handle_keyboard_focus_event }; -static void handle_data_device_event(struct wl_listener * listener, void * data) +static void +handle_data_device_event(struct wl_listener *listener, void *data) { - struct event * event = data; - - switch (event->type) - { - case DATA_DEVICE_EVENT_SELECTION_CHANGED: - if (seat.keyboard.focus.resource) - { - struct wl_client * client - = wl_resource_get_client(seat.keyboard.focus.resource); - data_device_offer_selection(&seat.data_device, client); - } - break; - } + struct event *event = data; + + switch (event->type) { + case DATA_DEVICE_EVENT_SELECTION_CHANGED: + if (seat.keyboard.focus.resource) { + struct wl_client *client = wl_resource_get_client(seat.keyboard.focus.resource); + data_device_offer_selection(&seat.data_device, client); + } + break; + } } static struct wl_listener data_device_listener = { - .notify = &handle_data_device_event + .notify = &handle_data_device_event }; -static void handle_swc_event(struct wl_listener * listener, void * data) +static void +handle_swc_event(struct wl_listener *listener, void *data) { - struct event * event = data; + struct event *event = data; - switch (event->type) - { - case SWC_EVENT_DEACTIVATED: + switch (event->type) { + case SWC_EVENT_DEACTIVATED: #ifdef ENABLE_LIBINPUT - libinput_suspend(seat.libinput); + libinput_suspend(seat.libinput); #endif - keyboard_reset(&seat.keyboard); - break; - case SWC_EVENT_ACTIVATED: - { + keyboard_reset(&seat.keyboard); + break; + case SWC_EVENT_ACTIVATED: { #ifdef ENABLE_LIBINPUT - if (libinput_resume(seat.libinput) != 0) - WARNING("Failed to resume libinput context\n"); + if (libinput_resume(seat.libinput) != 0) + WARNING("Failed to resume libinput context\n"); #else - struct evdev_device * device, * next; - - /* Re-open all input devices */ - wl_list_for_each_safe(device, next, &seat.devices, link) - { - if (!evdev_device_reopen(device)) - { - wl_list_remove(&device->link); - evdev_device_destroy(device); - } - } + struct evdev_device *device, *next; + + /* Re-open all input devices */ + wl_list_for_each_safe (device, next, &seat.devices, link) { + if (!evdev_device_reopen(device)) { + wl_list_remove(&device->link); + evdev_device_destroy(device); + } + } #endif - break; - } - } + break; + } + } } /* Wayland Seat Interface */ -static void get_pointer(struct wl_client * client, - struct wl_resource * resource, uint32_t id) +static void +get_pointer(struct wl_client *client, + struct wl_resource *resource, uint32_t id) { - pointer_bind(&seat.pointer, client, wl_resource_get_version(resource), id); + pointer_bind(&seat.pointer, client, wl_resource_get_version(resource), id); } -static void get_keyboard(struct wl_client * client, - struct wl_resource * resource, uint32_t id) +static void +get_keyboard(struct wl_client *client, + struct wl_resource *resource, uint32_t id) { - keyboard_bind(&seat.keyboard, client, - wl_resource_get_version(resource), id); + keyboard_bind(&seat.keyboard, client, + wl_resource_get_version(resource), id); } -static void get_touch(struct wl_client * client, struct wl_resource * resource, - uint32_t id) +static void +get_touch(struct wl_client *client, struct wl_resource *resource, + uint32_t id) { - /* XXX: Implement */ + /* XXX: Implement */ } static struct wl_seat_interface seat_implementation = { - .get_pointer = &get_pointer, - .get_keyboard = &get_keyboard, - .get_touch = &get_touch + .get_pointer = &get_pointer, + .get_keyboard = &get_keyboard, + .get_touch = &get_touch }; -static void bind_seat(struct wl_client * client, void * data, uint32_t version, - uint32_t id) +static void +bind_seat(struct wl_client *client, void *data, uint32_t version, + uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 4) - version = 4; + if (version > 4) + version = 4; - resource = wl_resource_create(client, &wl_seat_interface, version, id); - wl_resource_set_implementation(resource, &seat_implementation, NULL, - &remove_resource); - wl_list_insert(&seat.resources, wl_resource_get_link(resource)); + resource = wl_resource_create(client, &wl_seat_interface, version, id); + wl_resource_set_implementation(resource, &seat_implementation, NULL, + &remove_resource); + wl_list_insert(&seat.resources, wl_resource_get_link(resource)); - if (version >= 2) - wl_seat_send_name(resource, seat.name); + if (version >= 2) + wl_seat_send_name(resource, seat.name); - wl_seat_send_capabilities(resource, seat.capabilities); + wl_seat_send_capabilities(resource, seat.capabilities); } -static void update_capabilities(uint32_t capabilities) +static void +update_capabilities(uint32_t capabilities) { - if (~seat.capabilities & capabilities) - { - struct wl_resource * resource; + if (~seat.capabilities & capabilities) { + struct wl_resource *resource; - seat.capabilities |= capabilities; - wl_list_for_each(resource, &seat.resources, link) - wl_seat_send_capabilities(resource, seat.capabilities); - } + seat.capabilities |= capabilities; + wl_list_for_each (resource, &seat.resources, link) + wl_seat_send_capabilities(resource, seat.capabilities); + } } #ifdef ENABLE_LIBINPUT -static int open_restricted(const char * path, int flags, void * user_data) +static int +open_restricted(const char *path, int flags, void *user_data) { - return launch_open_device(path, flags); + return launch_open_device(path, flags); } -static void close_restricted(int fd, void * user_data) +static void +close_restricted(int fd, void *user_data) { - close(fd); + close(fd); } const struct libinput_interface libinput_interface = { - .open_restricted = &open_restricted, - .close_restricted = &close_restricted, + .open_restricted = &open_restricted, + .close_restricted = &close_restricted, }; -static uint32_t device_capabilities(struct libinput_device * device) +static uint32_t +device_capabilities(struct libinput_device *device) { - uint32_t capabilities = 0; + uint32_t capabilities = 0; - if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) - capabilities |= WL_SEAT_CAPABILITY_KEYBOARD; - if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) - capabilities |= WL_SEAT_CAPABILITY_POINTER; - /* TODO: Add touch device support + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) + capabilities |= WL_SEAT_CAPABILITY_KEYBOARD; + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) + capabilities |= WL_SEAT_CAPABILITY_POINTER; + /* TODO: Add touch device support if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) capabilities |= WL_SEAT_CAPABILITY_TOUCH; */ - return capabilities; + return capabilities; } -static void handle_libinput_axis_event(struct libinput_event_pointer * event, - enum libinput_pointer_axis axis) +static void +handle_libinput_axis_event(struct libinput_event_pointer *event, + enum libinput_pointer_axis axis) { - if (!libinput_event_pointer_has_axis(event, axis)) - return; + if (!libinput_event_pointer_has_axis(event, axis)) + return; - double amount; + double amount; - amount = libinput_event_pointer_get_axis_value(event, axis); - handle_axis(libinput_event_pointer_get_time(event), axis, - wl_fixed_from_double(amount)); + amount = libinput_event_pointer_get_axis_value(event, axis); + handle_axis(libinput_event_pointer_get_time(event), axis, + wl_fixed_from_double(amount)); } -static int handle_libinput_data(int fd, uint32_t mask, void * data) +static int +handle_libinput_data(int fd, uint32_t mask, void *data) { - struct libinput_event * generic_event; - - if (libinput_dispatch(seat.libinput) != 0) - { - WARNING("libinput_dispatch failed: %s\n", strerror(errno)); - return 0; - } - - while ((generic_event = libinput_get_event(seat.libinput))) - { - switch (libinput_event_get_type(generic_event)) - { - case LIBINPUT_EVENT_DEVICE_ADDED: - { - struct libinput_device * device; - - device = libinput_event_get_device(generic_event); - update_capabilities(device_capabilities(device)); - break; - } - case LIBINPUT_EVENT_KEYBOARD_KEY: - { - struct libinput_event_keyboard * event; - - event = libinput_event_get_keyboard_event(generic_event); - handle_key(libinput_event_keyboard_get_time(event), - libinput_event_keyboard_get_key(event), - libinput_event_keyboard_get_key_state(event)); - break; - } - case LIBINPUT_EVENT_POINTER_MOTION: - { - struct libinput_event_pointer * event; - wl_fixed_t dx, dy; - - event = libinput_event_get_pointer_event(generic_event); - dx = wl_fixed_from_double(libinput_event_pointer_get_dx(event)); - dy = wl_fixed_from_double(libinput_event_pointer_get_dy(event)); - handle_relative_motion(libinput_event_pointer_get_time(event), - dx, dy); - break; - } - case LIBINPUT_EVENT_POINTER_BUTTON: - { - struct libinput_event_pointer * event; - - event = libinput_event_get_pointer_event(generic_event); - handle_button(libinput_event_pointer_get_time(event), - libinput_event_pointer_get_button(event), - libinput_event_pointer_get_button_state(event)); - break; - } - case LIBINPUT_EVENT_POINTER_AXIS: - { - struct libinput_event_pointer * event; - - event = libinput_event_get_pointer_event(generic_event); - handle_libinput_axis_event - (event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL); - handle_libinput_axis_event - (event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL); - - break; - } - default: - break; - } - - libinput_event_destroy(generic_event); - } - - return 0; + struct libinput_event *generic_event; + + if (libinput_dispatch(seat.libinput) != 0) { + WARNING("libinput_dispatch failed: %s\n", strerror(errno)); + return 0; + } + + while ((generic_event = libinput_get_event(seat.libinput))) { + switch (libinput_event_get_type(generic_event)) { + case LIBINPUT_EVENT_DEVICE_ADDED: { + struct libinput_device *device; + + device = libinput_event_get_device(generic_event); + update_capabilities(device_capabilities(device)); + break; + } + case LIBINPUT_EVENT_KEYBOARD_KEY: { + struct libinput_event_keyboard *event; + + event = libinput_event_get_keyboard_event(generic_event); + handle_key(libinput_event_keyboard_get_time(event), + libinput_event_keyboard_get_key(event), + libinput_event_keyboard_get_key_state(event)); + break; + } + case LIBINPUT_EVENT_POINTER_MOTION: { + struct libinput_event_pointer *event; + wl_fixed_t dx, dy; + + event = libinput_event_get_pointer_event(generic_event); + dx = wl_fixed_from_double(libinput_event_pointer_get_dx(event)); + dy = wl_fixed_from_double(libinput_event_pointer_get_dy(event)); + handle_relative_motion(libinput_event_pointer_get_time(event), + dx, dy); + break; + } + case LIBINPUT_EVENT_POINTER_BUTTON: { + struct libinput_event_pointer *event; + + event = libinput_event_get_pointer_event(generic_event); + handle_button(libinput_event_pointer_get_time(event), + libinput_event_pointer_get_button(event), + libinput_event_pointer_get_button_state(event)); + break; + } + case LIBINPUT_EVENT_POINTER_AXIS: { + struct libinput_event_pointer *event; + + event = libinput_event_get_pointer_event(generic_event); + handle_libinput_axis_event(event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL); + handle_libinput_axis_event(event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL); + + break; + } + default: + break; + } + + libinput_event_destroy(generic_event); + } + + return 0; } -bool initialize_libinput(const char * seat_name) +bool +initialize_libinput(const char *seat_name) { - if (!(seat.udev = udev_new())) - { - ERROR("Could not create udev context\n"); - goto error0; - } - - seat.libinput = libinput_udev_create_context(&libinput_interface, NULL, - seat.udev); - - if (!seat.libinput) - { - ERROR("Could not create libinput context\n"); - goto error1; - } - - if (libinput_udev_assign_seat(seat.libinput, seat_name) != 0) - { - ERROR("Failed to assign seat to libinput context\n"); - goto error2; - } - - seat.libinput_source = wl_event_loop_add_fd - (swc.event_loop, libinput_get_fd(seat.libinput), WL_EVENT_READABLE, - &handle_libinput_data, NULL); - - if (!seat.libinput_source) - { - ERROR("Could not create event source for libinput\n"); - goto error2; - } - - if (!swc.active) - libinput_suspend(seat.libinput); - - return true; - - error2: - libinput_unref(seat.libinput); - error1: - udev_unref(seat.udev); - error0: - return false; + if (!(seat.udev = udev_new())) { + ERROR("Could not create udev context\n"); + goto error0; + } + + seat.libinput = libinput_udev_create_context(&libinput_interface, NULL, + seat.udev); + + if (!seat.libinput) { + ERROR("Could not create libinput context\n"); + goto error1; + } + + if (libinput_udev_assign_seat(seat.libinput, seat_name) != 0) { + ERROR("Failed to assign seat to libinput context\n"); + goto error2; + } + + seat.libinput_source = wl_event_loop_add_fd(swc.event_loop, libinput_get_fd(seat.libinput), WL_EVENT_READABLE, + &handle_libinput_data, NULL); + + if (!seat.libinput_source) { + ERROR("Could not create event source for libinput\n"); + goto error2; + } + + if (!swc.active) + libinput_suspend(seat.libinput); + + return true; + +error2: + libinput_unref(seat.libinput); +error1: + udev_unref(seat.udev); +error0: + return false; } -void finalize_libinput(void) +void +finalize_libinput(void) { - wl_event_source_remove(seat.libinput_source); - libinput_unref(seat.libinput); - udev_unref(seat.udev); + wl_event_source_remove(seat.libinput_source); + libinput_unref(seat.libinput); + udev_unref(seat.udev); } #else const static struct evdev_device_handler evdev_handler = { - .key = &handle_key, - .button = &handle_button, - .axis = &handle_axis, - .relative_motion = &handle_relative_motion, + .key = &handle_key, + .button = &handle_button, + .axis = &handle_axis, + .relative_motion = &handle_relative_motion, }; -static void add_device(const char * path) +static void +add_device(const char *path) { - struct evdev_device * device; + struct evdev_device *device; - if (!(device = evdev_device_new(path, &evdev_handler))) - { - ERROR("Could not create evdev device\n"); - return; - } + if (!(device = evdev_device_new(path, &evdev_handler))) { + ERROR("Could not create evdev device\n"); + return; + } - update_capabilities(device->capabilities); - wl_list_insert(&seat.devices, &device->link); + update_capabilities(device->capabilities); + wl_list_insert(&seat.devices, &device->link); } -static int select_device(const struct dirent * entry) +static int +select_device(const struct dirent *entry) { - unsigned num; + unsigned num; - return sscanf(entry->d_name, "event%u", &num) == 1; + return sscanf(entry->d_name, "event%u", &num) == 1; } -static bool add_devices(void) +static bool +add_devices(void) { - struct dirent ** devices; - int num_devices; - char path[64]; - unsigned index; + struct dirent **devices; + int num_devices; + char path[64]; + unsigned index; - num_devices = scandir("/dev/input", &devices, &select_device, &alphasort); + num_devices = scandir("/dev/input", &devices, &select_device, &alphasort); - if (num_devices == -1) - { - ERROR("Failed to scan /dev/input for event devices\n"); - return false; - } + if (num_devices == -1) { + ERROR("Failed to scan /dev/input for event devices\n"); + return false; + } - for (index = 0; index < num_devices; ++index) - { - snprintf(path, sizeof path, "/dev/input/%s", devices[index]->d_name); - free(devices[index]); - add_device(path); - } + for (index = 0; index < num_devices; ++index) { + snprintf(path, sizeof path, "/dev/input/%s", devices[index]->d_name); + free(devices[index]); + add_device(path); + } - free(devices); + free(devices); - return true; + return true; } #endif -bool seat_initialize(const char * seat_name) +bool +seat_initialize(const char *seat_name) { - if (!(seat.name = strdup(seat_name))) - { - ERROR("Could not allocate seat name string\n"); - goto error0; - } + if (!(seat.name = strdup(seat_name))) { + ERROR("Could not allocate seat name string\n"); + goto error0; + } - seat.global = wl_global_create(swc.display, &wl_seat_interface, 4, - NULL, &bind_seat); + seat.global = wl_global_create(swc.display, &wl_seat_interface, 4, + NULL, &bind_seat); - if (!seat.global) - goto error1; + if (!seat.global) + goto error1; - seat.capabilities = 0; - wl_list_init(&seat.resources); - seat.swc_listener.notify = &handle_swc_event; - wl_signal_add(&swc.event_signal, &seat.swc_listener); + seat.capabilities = 0; + wl_list_init(&seat.resources); + seat.swc_listener.notify = &handle_swc_event; + wl_signal_add(&swc.event_signal, &seat.swc_listener); - if (!data_device_initialize(&seat.data_device)) - { - ERROR("Could not initialize data device\n"); - goto error2; - } + if (!data_device_initialize(&seat.data_device)) { + ERROR("Could not initialize data device\n"); + goto error2; + } - wl_signal_add(&seat.data_device.event_signal, &data_device_listener); + wl_signal_add(&seat.data_device.event_signal, &data_device_listener); - if (!keyboard_initialize(&seat.keyboard)) - { - ERROR("Could not initialize keyboard\n"); - goto error3; - } + if (!keyboard_initialize(&seat.keyboard)) { + ERROR("Could not initialize keyboard\n"); + goto error3; + } - wl_signal_add(&seat.keyboard.focus.event_signal, &keyboard_focus_listener); + wl_signal_add(&seat.keyboard.focus.event_signal, &keyboard_focus_listener); - if (!pointer_initialize(&seat.pointer)) - { - ERROR("Could not initialize pointer\n"); - goto error4; - } + if (!pointer_initialize(&seat.pointer)) { + ERROR("Could not initialize pointer\n"); + goto error4; + } #ifdef ENABLE_LIBINPUT - if (!initialize_libinput(seat.name)) - goto error5; + if (!initialize_libinput(seat.name)) + goto error5; #else - wl_list_init(&seat.devices); + wl_list_init(&seat.devices); - if (!add_devices()) - goto error5; + if (!add_devices()) + goto error5; #endif - return true; - - error5: - pointer_finalize(&seat.pointer); - error4: - keyboard_finalize(&seat.keyboard); - error3: - data_device_finalize(&seat.data_device); - error2: - wl_global_destroy(seat.global); - error1: - free(seat.name); - error0: - return false; + return true; + +error5: + pointer_finalize(&seat.pointer); +error4: + keyboard_finalize(&seat.keyboard); +error3: + data_device_finalize(&seat.data_device); +error2: + wl_global_destroy(seat.global); +error1: + free(seat.name); +error0: + return false; } -void seat_finalize(void) +void +seat_finalize(void) { #ifdef ENABLE_LIBINPUT - finalize_libinput(); + finalize_libinput(); #else - struct evdev_device * device, * tmp; - wl_list_for_each_safe(device, tmp, &seat.devices, link) - evdev_device_destroy(device); + struct evdev_device *device, *tmp; + wl_list_for_each_safe (device, tmp, &seat.devices, link) + evdev_device_destroy(device); #endif - pointer_finalize(&seat.pointer); - keyboard_finalize(&seat.keyboard); - data_device_finalize(&seat.data_device); + pointer_finalize(&seat.pointer); + keyboard_finalize(&seat.keyboard); + data_device_finalize(&seat.data_device); - wl_global_destroy(seat.global); - free(seat.name); + wl_global_destroy(seat.global); + free(seat.name); } - diff --git a/libswc/seat.h b/libswc/seat.h @@ -26,16 +26,14 @@ #include <stdbool.h> -struct swc_seat -{ - struct pointer * pointer; - struct keyboard * keyboard; - struct data_device * data_device; +struct swc_seat { + struct pointer *pointer; + struct keyboard *keyboard; + struct data_device *data_device; }; -bool seat_initialize(const char * seat_name); +bool seat_initialize(const char *seat_name); void seat_finalize(void); void seat_reopen_devices(void); #endif - diff --git a/libswc/shell.c b/libswc/shell.c @@ -28,50 +28,53 @@ #include <wayland-server.h> static struct -{ - struct wl_global * global; + { + struct wl_global *global; } shell; -static void get_shell_surface(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - struct wl_resource * surface_resource) +static void +get_shell_surface(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *surface_resource) { - struct surface * surface = wl_resource_get_user_data(surface_resource); - struct shell_surface * shell_surface; + struct surface *surface = wl_resource_get_user_data(surface_resource); + struct shell_surface *shell_surface; - shell_surface = shell_surface_new(client, wl_resource_get_version(resource), - id, surface); + shell_surface = shell_surface_new(client, wl_resource_get_version(resource), + id, surface); - if (!shell_surface) - wl_resource_post_no_memory(resource); + if (!shell_surface) + wl_resource_post_no_memory(resource); } static const struct wl_shell_interface shell_implementation = { - &get_shell_surface + &get_shell_surface }; -static void bind_shell(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_shell(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 1) - version = 1; + if (version > 1) + version = 1; - resource = wl_resource_create(client, &wl_shell_interface, version, id); - wl_resource_set_implementation(resource, &shell_implementation, NULL, NULL); + resource = wl_resource_create(client, &wl_shell_interface, version, id); + wl_resource_set_implementation(resource, &shell_implementation, NULL, NULL); } -bool shell_initialize(void) +bool +shell_initialize(void) { - shell.global = wl_global_create(swc.display, &wl_shell_interface, 1, - NULL, &bind_shell); + shell.global = wl_global_create(swc.display, &wl_shell_interface, 1, + NULL, &bind_shell); - return shell.global; + return shell.global; } -void shell_finalize(void) +void +shell_finalize(void) { - wl_global_destroy(shell.global); + wl_global_destroy(shell.global); } - diff --git a/libswc/shell.h b/libswc/shell.h @@ -30,4 +30,3 @@ bool shell_initialize(void); void shell_finalize(void); #endif - diff --git a/libswc/shell_surface.c b/libswc/shell_surface.c @@ -36,228 +36,234 @@ #include <stdlib.h> #include <signal.h> -struct shell_surface -{ - struct window window; +struct shell_surface { + struct window window; - struct wl_resource * resource; - struct wl_listener surface_destroy_listener; + struct wl_resource *resource; + struct wl_listener surface_destroy_listener; }; -static void configure(struct window * window, uint32_t width, uint32_t height) +static void +configure(struct window *window, uint32_t width, uint32_t height) { - struct shell_surface * shell_surface - = wl_container_of(window, shell_surface, window); - - wl_shell_surface_send_configure(shell_surface->resource, - WL_SHELL_SURFACE_RESIZE_NONE, - width, height); - /* wl_shell does not support acknowledging configures. */ - window->configure.acknowledged = true; + struct shell_surface *shell_surface = wl_container_of(window, shell_surface, window); + + wl_shell_surface_send_configure(shell_surface->resource, + WL_SHELL_SURFACE_RESIZE_NONE, + width, height); + /* wl_shell does not support acknowledging configures. */ + window->configure.acknowledged = true; } -static void close(struct window * window) +static void +close(struct window *window) { - struct shell_surface * shell_surface - = wl_container_of(window, shell_surface, window); - struct wl_client * client; - pid_t pid; - - client = wl_resource_get_client(shell_surface->resource); - wl_client_get_credentials(client, &pid, NULL, NULL); - kill(pid, SIGTERM); + struct shell_surface *shell_surface = wl_container_of(window, shell_surface, window); + struct wl_client *client; + pid_t pid; + + client = wl_resource_get_client(shell_surface->resource); + wl_client_get_credentials(client, &pid, NULL, NULL); + kill(pid, SIGTERM); } static const struct window_impl window_impl = { - .configure = &configure, - .close = &close, + .configure = &configure, + .close = &close, }; -static void pong(struct wl_client * client, struct wl_resource * resource, - uint32_t serial) +static void +pong(struct wl_client *client, struct wl_resource *resource, + uint32_t serial) { } -static void move(struct wl_client * client, struct wl_resource * resource, - struct wl_resource * seat_resource, uint32_t serial) +static void +move(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *seat_resource, uint32_t serial) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); - struct button * button; + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); + struct button *button; - if (!(button = pointer_get_button(swc.seat->pointer, serial))) - return; + if (!(button = pointer_get_button(swc.seat->pointer, serial))) + return; - window_begin_move(&shell_surface->window, button); + window_begin_move(&shell_surface->window, button); } -static void resize(struct wl_client * client, struct wl_resource * resource, - struct wl_resource * seat_resource, uint32_t serial, - uint32_t edges) +static void +resize(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *seat_resource, uint32_t serial, + uint32_t edges) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); - struct button * button; + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); + struct button *button; - if (!(button = pointer_get_button(swc.seat->pointer, serial))) - return; + if (!(button = pointer_get_button(swc.seat->pointer, serial))) + return; - window_begin_resize(&shell_surface->window, edges, button); + window_begin_resize(&shell_surface->window, edges, button); } -static void set_toplevel(struct wl_client * client, - struct wl_resource * resource) +static void +set_toplevel(struct wl_client *client, + struct wl_resource *resource) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); - window_manage(&shell_surface->window); - window_set_parent(&shell_surface->window, NULL); + window_manage(&shell_surface->window); + window_set_parent(&shell_surface->window, NULL); } -static void set_transient(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * parent_resource, - int32_t x, int32_t y, uint32_t flags) +static void +set_transient(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *parent_resource, + int32_t x, int32_t y, uint32_t flags) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); - struct surface * parent_surface - = wl_resource_get_user_data(parent_resource); - struct compositor_view * parent_view - = compositor_view(parent_surface->view); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); + struct surface *parent_surface = wl_resource_get_user_data(parent_resource); + struct compositor_view *parent_view = compositor_view(parent_surface->view); - if (!parent_view || !parent_view->window) - return; + if (!parent_view || !parent_view->window) + return; - window_manage(&shell_surface->window); - window_set_parent(&shell_surface->window, parent_view->window); + window_manage(&shell_surface->window); + window_set_parent(&shell_surface->window, parent_view->window); } -static void set_fullscreen(struct wl_client * client, - struct wl_resource * resource, - uint32_t method, uint32_t framerate, - struct wl_resource * output_resource) +static void +set_fullscreen(struct wl_client *client, + struct wl_resource *resource, + uint32_t method, uint32_t framerate, + struct wl_resource *output_resource) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); - struct output * output = output_resource - ? wl_resource_get_user_data(output_resource) : NULL; - struct screen * screen; + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); + struct output *output = output_resource + ? wl_resource_get_user_data(output_resource) + : NULL; + struct screen *screen; - screen = output ? output->screen - : wl_container_of(swc.screens.next, screen, link); + screen = output ? output->screen + : wl_container_of(swc.screens.next, screen, link); - /* TODO: Handle fullscreen windows. */ + /* TODO: Handle fullscreen windows. */ - window_manage(&shell_surface->window); - window_set_parent(&shell_surface->window, NULL); + window_manage(&shell_surface->window); + window_set_parent(&shell_surface->window, NULL); } -static void set_popup(struct wl_client * client, struct wl_resource * resource, - struct wl_resource * seat_resource, uint32_t serial, - struct wl_resource * parent_resource, - int32_t x, int32_t y, uint32_t flags) +static void +set_popup(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *seat_resource, uint32_t serial, + struct wl_resource *parent_resource, + int32_t x, int32_t y, uint32_t flags) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); - struct surface * parent_surface - = wl_resource_get_user_data(parent_resource); - struct compositor_view * parent_view - = compositor_view(parent_surface->view); - - if (!parent_view || !parent_view->window) - return; - - window_unmanage(&shell_surface->window); - window_set_parent(&shell_surface->window, parent_view->window); - view_move(&shell_surface->window.view->base, - parent_view->base.geometry.x + x, - parent_view->base.geometry.y + y); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); + struct surface *parent_surface = wl_resource_get_user_data(parent_resource); + struct compositor_view *parent_view = compositor_view(parent_surface->view); + + if (!parent_view || !parent_view->window) + return; + + window_unmanage(&shell_surface->window); + window_set_parent(&shell_surface->window, parent_view->window); + view_move(&shell_surface->window.view->base, + parent_view->base.geometry.x + x, + parent_view->base.geometry.y + y); } -static void set_maximized(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * output_resource) +static void +set_maximized(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *output_resource) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); - /* TODO: Handle maximized windows. */ + /* TODO: Handle maximized windows. */ - window_manage(&shell_surface->window); - window_set_parent(&shell_surface->window, NULL); + window_manage(&shell_surface->window); + window_set_parent(&shell_surface->window, NULL); } -static void set_title(struct wl_client * client, struct wl_resource * resource, - const char * title) +static void +set_title(struct wl_client *client, struct wl_resource *resource, + const char *title) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); - window_set_title(&shell_surface->window, title, -1); + window_set_title(&shell_surface->window, title, -1); } -static void set_class(struct wl_client * client, struct wl_resource * resource, - const char * class) +static void +set_class(struct wl_client *client, struct wl_resource *resource, + const char *class) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); - window_set_app_id(&shell_surface->window, class); + window_set_app_id(&shell_surface->window, class); } static const struct wl_shell_surface_interface shell_surface_implementation = { - .pong = &pong, - .move = &move, - .resize = &resize, - .set_toplevel = &set_toplevel, - .set_transient = &set_transient, - .set_fullscreen = &set_fullscreen, - .set_popup = &set_popup, - .set_maximized = &set_maximized, - .set_title = &set_title, - .set_class = &set_class + .pong = &pong, + .move = &move, + .resize = &resize, + .set_toplevel = &set_toplevel, + .set_transient = &set_transient, + .set_fullscreen = &set_fullscreen, + .set_popup = &set_popup, + .set_maximized = &set_maximized, + .set_title = &set_title, + .set_class = &set_class }; -static void handle_surface_destroy(struct wl_listener * listener, void * data) +static void +handle_surface_destroy(struct wl_listener *listener, void *data) { - struct shell_surface * shell_surface - = wl_container_of(listener, 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); + wl_resource_destroy(shell_surface->resource); } -static void destroy_shell_surface(struct wl_resource * resource) +static void +destroy_shell_surface(struct wl_resource *resource) { - struct shell_surface * shell_surface = wl_resource_get_user_data(resource); + struct shell_surface *shell_surface = wl_resource_get_user_data(resource); - window_finalize(&shell_surface->window); - free(shell_surface); + window_finalize(&shell_surface->window); + free(shell_surface); } -struct shell_surface * shell_surface_new(struct wl_client * client, - uint32_t version, uint32_t id, - struct surface * surface) +struct shell_surface * +shell_surface_new(struct wl_client *client, + uint32_t version, uint32_t id, + struct surface *surface) { - struct shell_surface * shell_surface; + struct shell_surface *shell_surface; - shell_surface = malloc(sizeof *shell_surface); + shell_surface = malloc(sizeof *shell_surface); - if (!shell_surface) - goto error0; + if (!shell_surface) + goto error0; - shell_surface->resource = wl_resource_create - (client, &wl_shell_surface_interface, version, id); + shell_surface->resource = wl_resource_create(client, &wl_shell_surface_interface, version, id); - if (!shell_surface->resource) - goto error1; + if (!shell_surface->resource) + goto error1; - wl_resource_set_implementation(shell_surface->resource, - &shell_surface_implementation, - shell_surface, &destroy_shell_surface); + wl_resource_set_implementation(shell_surface->resource, + &shell_surface_implementation, + shell_surface, &destroy_shell_surface); - window_initialize(&shell_surface->window, &window_impl, surface); - shell_surface->surface_destroy_listener.notify = &handle_surface_destroy; - wl_resource_add_destroy_listener(surface->resource, - &shell_surface->surface_destroy_listener); + window_initialize(&shell_surface->window, &window_impl, surface); + shell_surface->surface_destroy_listener.notify = &handle_surface_destroy; + wl_resource_add_destroy_listener(surface->resource, + &shell_surface->surface_destroy_listener); - return shell_surface; + return shell_surface; - error1: - free(shell_surface); - error0: - return NULL; +error1: + free(shell_surface); +error0: + return NULL; } - diff --git a/libswc/shell_surface.h b/libswc/shell_surface.h @@ -29,9 +29,8 @@ struct surface; struct wl_client; -struct shell_surface * shell_surface_new(struct wl_client * client, - uint32_t version, uint32_t id, - struct surface * surface); +struct shell_surface *shell_surface_new(struct wl_client *client, + uint32_t version, uint32_t id, + struct surface *surface); #endif - diff --git a/libswc/shm.c b/libswc/shm.c @@ -40,231 +40,231 @@ struct swc_shm swc_shm; static struct -{ - struct wl_global * global; + { + struct wl_global *global; } shm; -struct pool -{ - struct wl_resource * resource; - void * data; - uint32_t size; - unsigned references; +struct pool { + struct wl_resource *resource; + void *data; + uint32_t size; + unsigned references; }; -struct pool_reference -{ - struct wld_destructor destructor; - struct pool * pool; +struct pool_reference { + struct wld_destructor destructor; + struct pool *pool; }; -static void unref_pool(struct pool * pool) +static void +unref_pool(struct pool *pool) { - if (--pool->references > 0) - return; + if (--pool->references > 0) + return; - munmap(pool->data, pool->size); - free(pool); + munmap(pool->data, pool->size); + free(pool); } -static void destroy_pool_resource(struct wl_resource * resource) +static void +destroy_pool_resource(struct wl_resource *resource) { - struct pool * pool = wl_resource_get_user_data(resource); + struct pool *pool = wl_resource_get_user_data(resource); - unref_pool(pool); + unref_pool(pool); } -static void handle_buffer_destroy(struct wld_destructor * destructor) +static void +handle_buffer_destroy(struct wld_destructor *destructor) { - struct pool_reference * reference - = wl_container_of(destructor, reference, destructor); + struct pool_reference *reference = wl_container_of(destructor, reference, destructor); - unref_pool(reference->pool); + unref_pool(reference->pool); } -static inline uint32_t format_shm_to_wld(uint32_t format) +static inline uint32_t +format_shm_to_wld(uint32_t format) { - switch (format) - { - case WL_SHM_FORMAT_ARGB8888: - return WLD_FORMAT_ARGB8888; - case WL_SHM_FORMAT_XRGB8888: - return WLD_FORMAT_XRGB8888; - default: - return format; - } + switch (format) { + case WL_SHM_FORMAT_ARGB8888: + return WLD_FORMAT_ARGB8888; + case WL_SHM_FORMAT_XRGB8888: + return WLD_FORMAT_XRGB8888; + default: + return format; + } } -static void create_buffer(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - int32_t offset, int32_t width, int32_t height, - int32_t stride, uint32_t format) +static void +create_buffer(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + int32_t offset, int32_t width, int32_t height, + int32_t stride, uint32_t format) { - struct pool * pool = wl_resource_get_user_data(resource); - struct pool_reference * reference; - struct wld_buffer * buffer; - struct wl_resource * buffer_resource; - union wld_object object; - - if (offset > pool->size || offset < 0) - { - wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_STRIDE, - "offset is too big or negative"); - return; - } - - object.ptr = (void *)((uintptr_t) pool->data + offset); - buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, - width, height, format_shm_to_wld(format), - stride); - - if (!buffer) - goto error0; - - buffer_resource = wayland_buffer_create_resource - (client, wl_resource_get_version(resource), id, buffer); - - if (!buffer_resource) - goto error1; - - if (!(reference = malloc(sizeof *reference))) - goto error2; - - reference->pool = pool; - reference->destructor.destroy = &handle_buffer_destroy; - wld_buffer_add_destructor(buffer, &reference->destructor); - ++pool->references; - - return; - - error2: - wl_resource_destroy(buffer_resource); - error1: - wld_buffer_unreference(buffer); - error0: - wl_resource_post_no_memory(resource); + struct pool *pool = wl_resource_get_user_data(resource); + struct pool_reference *reference; + struct wld_buffer *buffer; + struct wl_resource *buffer_resource; + union wld_object object; + + if (offset > pool->size || offset < 0) { + wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_STRIDE, + "offset is too big or negative"); + return; + } + + object.ptr = (void *)((uintptr_t)pool->data + offset); + buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, + width, height, format_shm_to_wld(format), + stride); + + if (!buffer) + goto error0; + + buffer_resource = wayland_buffer_create_resource(client, wl_resource_get_version(resource), id, buffer); + + if (!buffer_resource) + goto error1; + + if (!(reference = malloc(sizeof *reference))) + goto error2; + + reference->pool = pool; + reference->destructor.destroy = &handle_buffer_destroy; + wld_buffer_add_destructor(buffer, &reference->destructor); + ++pool->references; + + return; + +error2: + wl_resource_destroy(buffer_resource); +error1: + wld_buffer_unreference(buffer); +error0: + wl_resource_post_no_memory(resource); } -static void destroy(struct wl_client * client, struct wl_resource * resource) +static void +destroy(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } -static void resize(struct wl_client * client, struct wl_resource * resource, - int32_t size) +static void +resize(struct wl_client *client, struct wl_resource *resource, + int32_t size) { - struct pool * pool = wl_resource_get_user_data(resource); - void * data; + struct pool *pool = wl_resource_get_user_data(resource); + void *data; - data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE); + data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE); - if (data == MAP_FAILED) - { - wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, - "mremap failed: %s", strerror(errno)); - return; - } + if (data == MAP_FAILED) { + wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, + "mremap failed: %s", strerror(errno)); + return; + } - pool->data = data; - pool->size = size; + pool->data = data; + pool->size = size; } static struct wl_shm_pool_interface shm_pool_implementation = { - .create_buffer = &create_buffer, - .destroy = &destroy, - .resize = &resize + .create_buffer = &create_buffer, + .destroy = &destroy, + .resize = &resize }; -static void create_pool(struct wl_client * client, - struct wl_resource * resource, uint32_t id, - int32_t fd, int32_t size) +static void +create_pool(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + int32_t fd, int32_t size) { - struct pool * pool; + struct pool *pool; - if (!(pool = malloc(sizeof *pool))) - { - wl_resource_post_no_memory(resource); - return; - } + if (!(pool = malloc(sizeof *pool))) { + wl_resource_post_no_memory(resource); + return; + } - pool->resource = wl_resource_create(client, &wl_shm_pool_interface, - wl_resource_get_version(resource), id); + pool->resource = wl_resource_create(client, &wl_shm_pool_interface, + wl_resource_get_version(resource), id); - if (!pool->resource) - { - wl_resource_post_no_memory(resource); - goto error0; - } + if (!pool->resource) { + wl_resource_post_no_memory(resource); + goto error0; + } - wl_resource_set_implementation(pool->resource, &shm_pool_implementation, - pool, &destroy_pool_resource); - pool->data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + wl_resource_set_implementation(pool->resource, &shm_pool_implementation, + pool, &destroy_pool_resource); + pool->data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); - if (pool->data == MAP_FAILED) - { - wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, - "mmap failed: %s", strerror(errno)); - goto error1; - } + if (pool->data == MAP_FAILED) { + wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, + "mmap failed: %s", strerror(errno)); + goto error1; + } - pool->size = size; - pool->references = 1; + pool->size = size; + pool->references = 1; - return; + return; - error1: - wl_resource_destroy(pool->resource); - error0: - free(pool); +error1: + wl_resource_destroy(pool->resource); +error0: + free(pool); } static struct wl_shm_interface shm_implementation = { - .create_pool = &create_pool + .create_pool = &create_pool }; -static void bind_shm(struct wl_client * client, void * data, uint32_t version, - uint32_t id) +static void +bind_shm(struct wl_client *client, void *data, uint32_t version, + uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 1) - version = 1; + if (version > 1) + version = 1; - resource = wl_resource_create(client, &wl_shm_interface, version, id); - wl_resource_set_implementation(resource, &shm_implementation, NULL, NULL); + resource = wl_resource_create(client, &wl_shm_interface, version, id); + wl_resource_set_implementation(resource, &shm_implementation, NULL, NULL); - wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888); - wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888); + wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888); + wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888); } -bool shm_initialize(void) +bool +shm_initialize(void) { - if (!(swc.shm->context = wld_pixman_create_context())) - goto error0; + if (!(swc.shm->context = wld_pixman_create_context())) + goto error0; - if (!(swc.shm->renderer = wld_create_renderer(swc.shm->context))) - goto error1; + if (!(swc.shm->renderer = wld_create_renderer(swc.shm->context))) + goto error1; - shm.global = wl_global_create(swc.display, &wl_shm_interface, 1, - NULL, &bind_shm); + shm.global = wl_global_create(swc.display, &wl_shm_interface, 1, + NULL, &bind_shm); - if (!shm.global) - goto error2; + if (!shm.global) + goto error2; - return true; + return true; - error2: - wld_destroy_renderer(swc.shm->renderer); - error1: - wld_destroy_context(swc.shm->context); - error0: - return false; +error2: + wld_destroy_renderer(swc.shm->renderer); +error1: + wld_destroy_context(swc.shm->context); +error0: + return false; } -void shm_finalize(void) +void +shm_finalize(void) { - wl_global_destroy(shm.global); - wld_destroy_renderer(swc.shm->renderer); - wld_destroy_context(swc.shm->context); + wl_global_destroy(shm.global); + wld_destroy_renderer(swc.shm->renderer); + wld_destroy_context(swc.shm->context); } - diff --git a/libswc/shm.h b/libswc/shm.h @@ -26,10 +26,9 @@ #include <stdbool.h> -struct swc_shm -{ - struct wld_context * context; - struct wld_renderer * renderer; +struct swc_shm { + struct wld_context *context; + struct wld_renderer *renderer; }; bool shm_initialize(void); @@ -37,4 +36,3 @@ bool shm_initialize(void); void shm_finalize(void); #endif - diff --git a/libswc/subcompositor.c b/libswc/subcompositor.c @@ -26,56 +26,58 @@ #include "subcompositor.h" #include "subsurface.h" -static struct wl_global * global; +static struct wl_global *global; -static void get_subsurface(struct wl_client * client, - struct wl_resource * resource, - uint32_t id, - struct wl_resource * surface_resource, - struct wl_resource * parent_resource) +static void +get_subsurface(struct wl_client *client, + struct wl_resource *resource, + uint32_t id, + struct wl_resource *surface_resource, + struct wl_resource *parent_resource) { - struct subsurface * subsurface; + struct subsurface *subsurface; - subsurface = subsurface_new(client, wl_resource_get_version(resource), id); + subsurface = subsurface_new(client, wl_resource_get_version(resource), id); - if (!subsurface) - { - wl_resource_post_no_memory(resource); - return; - } + if (!subsurface) { + wl_resource_post_no_memory(resource); + return; + } } static struct wl_subcompositor_interface subcompositor_implementation = { - .get_subsurface = &get_subsurface, + .get_subsurface = &get_subsurface, }; -static void bind_subcompositor(struct wl_client * client, void * data, - uint32_t version, uint32_t id) +static void +bind_subcompositor(struct wl_client *client, void *data, + uint32_t version, uint32_t id) { - struct wl_resource * resource; + struct wl_resource *resource; - if (version > 1) - version = 1; + if (version > 1) + version = 1; - resource = wl_resource_create(client, &wl_subcompositor_interface, - version, id); - wl_resource_set_implementation(resource, &subcompositor_implementation, - NULL, NULL); + resource = wl_resource_create(client, &wl_subcompositor_interface, + version, id); + wl_resource_set_implementation(resource, &subcompositor_implementation, + NULL, NULL); } -bool subcompositor_initialize() +bool +subcompositor_initialize() { - global = wl_global_create(swc.display, &wl_subcompositor_interface, 1, - NULL, &bind_subcompositor); + global = wl_global_create(swc.display, &wl_subcompositor_interface, 1, + NULL, &bind_subcompositor); - if (!global) - return false; + if (!global) + return false; - return true; + return true; } -void subcompositor_finalize() +void +subcompositor_finalize() { - wl_global_destroy(global); + wl_global_destroy(global); } - diff --git a/libswc/subcompositor.h b/libswc/subcompositor.h @@ -29,4 +29,3 @@ bool subcompositor_initialize(); void subcompositor_finalize(); #endif - diff --git a/libswc/subsurface.c b/libswc/subsurface.c @@ -26,81 +26,88 @@ #include <stdlib.h> #include <wayland-server.h> -static void destroy(struct wl_client * client, struct wl_resource * resource) +static void +destroy(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } -static void set_position(struct wl_client * client, - struct wl_resource * resource, - int32_t x, int32_t y) +static void +set_position(struct wl_client *client, + struct wl_resource *resource, + int32_t x, int32_t y) { - /* TODO: Implement. */ + /* TODO: Implement. */ } -static void place_above(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * sibling_resource) +static void +place_above(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *sibling_resource) { - /* TODO: Implement. */ + /* TODO: Implement. */ } -static void place_below(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * sibling_resource) +static void +place_below(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *sibling_resource) { - /* TODO: Implement. */ + /* TODO: Implement. */ } -static void set_sync(struct wl_client * client, struct wl_resource * resource) +static void +set_sync(struct wl_client *client, struct wl_resource *resource) { - /* TODO: Implement. */ + /* TODO: Implement. */ } -static void set_desync(struct wl_client * client, struct wl_resource * resource) +static void +set_desync(struct wl_client *client, struct wl_resource *resource) { - /* TODO: Implement. */ + /* TODO: Implement. */ } static struct wl_subsurface_interface subsurface_implementation = { - .destroy = &destroy, - .set_position = &set_position, - .place_above = &place_above, - .place_below = &place_below, - .set_sync = &set_sync, - .set_desync = &set_desync, + .destroy = &destroy, + .set_position = &set_position, + .place_above = &place_above, + .place_below = &place_below, + .set_sync = &set_sync, + .set_desync = &set_desync, }; -static void subsurface_destroy(struct wl_resource * resource) +static void +subsurface_destroy(struct wl_resource *resource) { - struct subsurface * subsurface = wl_resource_get_user_data(resource); + struct subsurface *subsurface = wl_resource_get_user_data(resource); - free(subsurface); + free(subsurface); } -struct subsurface * subsurface_new(struct wl_client * client, - uint32_t version, uint32_t id) +struct subsurface * +subsurface_new(struct wl_client *client, + uint32_t version, uint32_t id) { - struct subsurface * subsurface; + struct subsurface *subsurface; - if (!(subsurface = malloc(sizeof *subsurface))) - goto error0; + if (!(subsurface = malloc(sizeof *subsurface))) + goto error0; - subsurface->resource = wl_resource_create(client, &wl_subsurface_interface, - version, id); + subsurface->resource = wl_resource_create(client, &wl_subsurface_interface, + version, id); - if (!subsurface->resource) - goto error1; + if (!subsurface->resource) + goto error1; - wl_resource_set_implementation(subsurface->resource, - &subsurface_implementation, - subsurface, &subsurface_destroy); + wl_resource_set_implementation(subsurface->resource, + &subsurface_implementation, + subsurface, &subsurface_destroy); - return subsurface; + return subsurface; - error1: - free(subsurface); - error0: - return NULL; +error1: + free(subsurface); +error0: + return NULL; } - diff --git a/libswc/subsurface.h b/libswc/subsurface.h @@ -28,13 +28,11 @@ struct wl_client; -struct subsurface -{ - struct wl_resource * resource; +struct subsurface { + struct wl_resource *resource; }; -struct subsurface * subsurface_new(struct wl_client * client, - uint32_t version, uint32_t id); +struct subsurface *subsurface_new(struct wl_client *client, + uint32_t version, uint32_t id); #endif - diff --git a/libswc/surface.c b/libswc/surface.c @@ -38,313 +38,309 @@ /** * Removes a buffer from a surface state. */ -static void handle_buffer_destroy(struct wl_listener * listener, void * data) +static void +handle_buffer_destroy(struct wl_listener *listener, void *data) { - struct surface_state * state; + struct surface_state *state; - state = wl_container_of(listener, state, buffer_destroy_listener); - state->buffer = NULL; + state = wl_container_of(listener, state, buffer_destroy_listener); + state->buffer = NULL; } -static void state_initialize(struct surface_state * state) +static void +state_initialize(struct surface_state *state) { - state->buffer = NULL; - state->buffer_destroy_listener.notify = &handle_buffer_destroy; + state->buffer = NULL; + state->buffer_destroy_listener.notify = &handle_buffer_destroy; - pixman_region32_init(&state->damage); - pixman_region32_init(&state->opaque); - pixman_region32_init_with_extents(&state->input, &infinite_extents); + pixman_region32_init(&state->damage); + pixman_region32_init(&state->opaque); + pixman_region32_init_with_extents(&state->input, &infinite_extents); - wl_list_init(&state->frame_callbacks); + wl_list_init(&state->frame_callbacks); } -static void state_finalize(struct surface_state * state) +static void +state_finalize(struct surface_state *state) { - struct wl_resource * resource, * tmp; + struct wl_resource *resource, *tmp; - if (state->buffer) - { - /* Remove any buffer listeners */ - wl_list_remove(&state->buffer_destroy_listener.link); - } + if (state->buffer) { + /* Remove any buffer listeners */ + wl_list_remove(&state->buffer_destroy_listener.link); + } - pixman_region32_fini(&state->damage); - pixman_region32_fini(&state->opaque); - pixman_region32_fini(&state->input); + pixman_region32_fini(&state->damage); + pixman_region32_fini(&state->opaque); + pixman_region32_fini(&state->input); - /* Remove all leftover callbacks. */ - wl_list_for_each_safe(resource, tmp, &state->frame_callbacks, link) - wl_resource_destroy(resource); + /* Remove all leftover callbacks. */ + wl_list_for_each_safe (resource, tmp, &state->frame_callbacks, link) + wl_resource_destroy(resource); } /** * In order to set the buffer of a surface state (current or pending), we need * to manage the destroy listeners we have for the new and old buffer. */ -static void state_set_buffer(struct surface_state * state, - struct wl_resource * resource) +static void +state_set_buffer(struct surface_state *state, + struct wl_resource *resource) { - struct wld_buffer * buffer = resource ? wayland_buffer_get(resource) : NULL; + struct wld_buffer *buffer = resource ? wayland_buffer_get(resource) : NULL; - if (state->buffer) - { - /* No longer need to worry about the old buffer being destroyed. */ - wl_list_remove(&state->buffer_destroy_listener.link); - } + if (state->buffer) { + /* No longer need to worry about the old buffer being destroyed. */ + wl_list_remove(&state->buffer_destroy_listener.link); + } - if (buffer) - { - /* Need to watch the new buffer for destruction so we can remove it + if (buffer) { + /* Need to watch the new buffer for destruction so we can remove it * from state. */ - wl_resource_add_destroy_listener(resource, - &state->buffer_destroy_listener); - } + wl_resource_add_destroy_listener(resource, + &state->buffer_destroy_listener); + } - state->buffer = buffer; - state->buffer_resource = resource; + state->buffer = buffer; + state->buffer_resource = resource; } -static void handle_frame(struct view_handler * handler, uint32_t time) +static void +handle_frame(struct view_handler *handler, uint32_t time) { - struct surface * surface - = wl_container_of(handler, surface, view_handler); - struct wl_resource * resource, * tmp; - - wl_list_for_each_safe(resource, tmp, - &surface->state.frame_callbacks, link) - { - wl_callback_send_done(resource, time); - wl_resource_destroy(resource); - } - - wl_list_init(&surface->state.frame_callbacks); + struct surface *surface = wl_container_of(handler, surface, view_handler); + struct wl_resource *resource, *tmp; + + wl_list_for_each_safe (resource, tmp, + &surface->state.frame_callbacks, link) { + wl_callback_send_done(resource, time); + wl_resource_destroy(resource); + } + + wl_list_init(&surface->state.frame_callbacks); } -static void handle_screens(struct view_handler * handler, - uint32_t entered, uint32_t left) +static void +handle_screens(struct view_handler *handler, + uint32_t entered, uint32_t left) { - struct surface * surface - = wl_container_of(handler, surface, view_handler); - struct screen * screen; - struct output * output; - struct wl_client * client; - struct wl_resource * resource; - - client = wl_resource_get_client(surface->resource); - - wl_list_for_each(screen, &swc.screens, link) - { - if (!((entered | left) & screen_mask(screen))) - continue; - - wl_list_for_each(output, &screen->outputs, link) - { - resource = wl_resource_find_for_client - (&output->resources, client); - - if (resource) - { - if (entered & screen_mask(screen)) - wl_surface_send_enter(surface->resource, resource); - else if (left & screen_mask(screen)) - wl_surface_send_leave(surface->resource, resource); - } - } - } + struct surface *surface = wl_container_of(handler, surface, view_handler); + struct screen *screen; + struct output *output; + struct wl_client *client; + struct wl_resource *resource; + + client = wl_resource_get_client(surface->resource); + + wl_list_for_each (screen, &swc.screens, link) { + if (!((entered | left) & screen_mask(screen))) + continue; + + wl_list_for_each (output, &screen->outputs, link) { + resource = wl_resource_find_for_client(&output->resources, client); + + if (resource) { + if (entered & screen_mask(screen)) + wl_surface_send_enter(surface->resource, resource); + else if (left & screen_mask(screen)) + wl_surface_send_leave(surface->resource, resource); + } + } + } } static const struct view_handler_impl view_handler_impl = { - .frame = &handle_frame, - .screens = &handle_screens, + .frame = &handle_frame, + .screens = &handle_screens, }; -static void destroy(struct wl_client * client, struct wl_resource * resource) +static void +destroy(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } -static void attach(struct wl_client * client, struct wl_resource * resource, - struct wl_resource * buffer_resource, int32_t x, int32_t y) +static void +attach(struct wl_client *client, struct wl_resource *resource, + struct wl_resource *buffer_resource, int32_t x, int32_t y) { - struct surface * surface = wl_resource_get_user_data(resource); + struct surface *surface = wl_resource_get_user_data(resource); - surface->pending.commit |= SURFACE_COMMIT_ATTACH; + surface->pending.commit |= SURFACE_COMMIT_ATTACH; - state_set_buffer(&surface->pending.state, buffer_resource); - surface->pending.x = x; - surface->pending.y = y; + state_set_buffer(&surface->pending.state, buffer_resource); + surface->pending.x = x; + surface->pending.y = y; } -static void damage(struct wl_client * client, struct wl_resource * resource, - int32_t x, int32_t y, int32_t width, int32_t height) +static void +damage(struct wl_client *client, struct wl_resource *resource, + int32_t x, int32_t y, int32_t width, int32_t height) { - struct surface * surface = wl_resource_get_user_data(resource); + struct surface *surface = wl_resource_get_user_data(resource); - surface->pending.commit |= SURFACE_COMMIT_DAMAGE; + surface->pending.commit |= SURFACE_COMMIT_DAMAGE; - pixman_region32_union_rect(&surface->pending.state.damage, - &surface->pending.state.damage, - x, y, width, height); + pixman_region32_union_rect(&surface->pending.state.damage, + &surface->pending.state.damage, + x, y, width, height); } -static void frame(struct wl_client * client, struct wl_resource * resource, - uint32_t id) +static void +frame(struct wl_client *client, struct wl_resource *resource, + uint32_t id) { - struct surface * surface = wl_resource_get_user_data(resource); - struct wl_resource * callback_resource; + struct surface *surface = wl_resource_get_user_data(resource); + struct wl_resource *callback_resource; - surface->pending.commit |= SURFACE_COMMIT_FRAME; + surface->pending.commit |= SURFACE_COMMIT_FRAME; - callback_resource = wl_resource_create(client, &wl_callback_interface, - 1, id); - wl_resource_set_implementation(callback_resource, NULL, NULL, - &remove_resource); - wl_list_insert(surface->pending.state.frame_callbacks.prev, - wl_resource_get_link(callback_resource)); + callback_resource = wl_resource_create(client, &wl_callback_interface, + 1, id); + wl_resource_set_implementation(callback_resource, NULL, NULL, + &remove_resource); + wl_list_insert(surface->pending.state.frame_callbacks.prev, + wl_resource_get_link(callback_resource)); } -static void set_opaque_region(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * region_resource) +static void +set_opaque_region(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *region_resource) { - struct surface * surface = wl_resource_get_user_data(resource); + struct surface *surface = wl_resource_get_user_data(resource); - surface->pending.commit |= SURFACE_COMMIT_OPAQUE; + surface->pending.commit |= SURFACE_COMMIT_OPAQUE; - if (region_resource) - { - struct region * region = wl_resource_get_user_data(region_resource); + if (region_resource) { + struct region *region = wl_resource_get_user_data(region_resource); - pixman_region32_copy(&surface->pending.state.opaque, &region->region); - } - else - pixman_region32_clear(&surface->pending.state.opaque); + pixman_region32_copy(&surface->pending.state.opaque, &region->region); + } else + pixman_region32_clear(&surface->pending.state.opaque); } -static void set_input_region(struct wl_client * client, - struct wl_resource * resource, - struct wl_resource * region_resource) +static void +set_input_region(struct wl_client *client, + struct wl_resource *resource, + struct wl_resource *region_resource) { - struct surface * surface = wl_resource_get_user_data(resource); + struct surface *surface = wl_resource_get_user_data(resource); - surface->pending.commit |= SURFACE_COMMIT_INPUT; + surface->pending.commit |= SURFACE_COMMIT_INPUT; - if (region_resource) - { - struct region * region = wl_resource_get_user_data(region_resource); + if (region_resource) { + struct region *region = wl_resource_get_user_data(region_resource); - pixman_region32_copy(&surface->pending.state.input, &region->region); - } - else - pixman_region32_reset(&surface->pending.state.input, &infinite_extents); + pixman_region32_copy(&surface->pending.state.input, &region->region); + } else + pixman_region32_reset(&surface->pending.state.input, &infinite_extents); } -static inline void trim_region(pixman_region32_t * region, - struct wld_buffer * buffer) +static inline void +trim_region(pixman_region32_t *region, + struct wld_buffer *buffer) { - pixman_region32_intersect_rect(region, region, 0, 0, - buffer ? buffer->width : 0, - buffer ? buffer->height : 0); + pixman_region32_intersect_rect(region, region, 0, 0, + buffer ? buffer->width : 0, + buffer ? buffer->height : 0); } -static void commit(struct wl_client * client, struct wl_resource * resource) +static void +commit(struct wl_client *client, struct wl_resource *resource) { - struct surface * surface = wl_resource_get_user_data(resource); - struct wld_buffer * buffer; - - /* Attach */ - if (surface->pending.commit & SURFACE_COMMIT_ATTACH) - { - if (surface->state.buffer - && surface->state.buffer != surface->pending.state.buffer) - { - wl_buffer_send_release(surface->state.buffer_resource); - } - - state_set_buffer(&surface->state, - surface->pending.state.buffer_resource); - } - - buffer = surface->state.buffer; - - /* Damage */ - if (surface->pending.commit & SURFACE_COMMIT_DAMAGE) - { - pixman_region32_union(&surface->state.damage, &surface->state.damage, - &surface->pending.state.damage); - pixman_region32_clear(&surface->pending.state.damage); - } - - /* Opaque */ - if (surface->pending.commit & SURFACE_COMMIT_OPAQUE) - { - pixman_region32_copy(&surface->state.opaque, - &surface->pending.state.opaque); - } - - /* Input */ - if (surface->pending.commit & SURFACE_COMMIT_INPUT) - { - pixman_region32_copy(&surface->state.input, - &surface->pending.state.input); - } - - /* Frame */ - if (surface->pending.commit & SURFACE_COMMIT_FRAME) - { - wl_list_insert_list(&surface->state.frame_callbacks, - &surface->pending.state.frame_callbacks); - wl_list_init(&surface->pending.state.frame_callbacks); - } - - trim_region(&surface->state.damage, buffer); - trim_region(&surface->state.opaque, buffer); - - if (surface->view) - { - if (surface->pending.commit & SURFACE_COMMIT_ATTACH) - view_attach(surface->view, buffer); - view_update(surface->view); - } - - surface->pending.commit = 0; + struct surface *surface = wl_resource_get_user_data(resource); + struct wld_buffer *buffer; + + /* Attach */ + if (surface->pending.commit & SURFACE_COMMIT_ATTACH) { + if (surface->state.buffer + && surface->state.buffer != surface->pending.state.buffer) { + wl_buffer_send_release(surface->state.buffer_resource); + } + + state_set_buffer(&surface->state, + surface->pending.state.buffer_resource); + } + + buffer = surface->state.buffer; + + /* Damage */ + if (surface->pending.commit & SURFACE_COMMIT_DAMAGE) { + pixman_region32_union(&surface->state.damage, &surface->state.damage, + &surface->pending.state.damage); + pixman_region32_clear(&surface->pending.state.damage); + } + + /* Opaque */ + if (surface->pending.commit & SURFACE_COMMIT_OPAQUE) { + pixman_region32_copy(&surface->state.opaque, + &surface->pending.state.opaque); + } + + /* Input */ + if (surface->pending.commit & SURFACE_COMMIT_INPUT) { + pixman_region32_copy(&surface->state.input, + &surface->pending.state.input); + } + + /* Frame */ + if (surface->pending.commit & SURFACE_COMMIT_FRAME) { + wl_list_insert_list(&surface->state.frame_callbacks, + &surface->pending.state.frame_callbacks); + wl_list_init(&surface->pending.state.frame_callbacks); + } + + trim_region(&surface->state.damage, buffer); + trim_region(&surface->state.opaque, buffer); + + if (surface->view) { + if (surface->pending.commit & SURFACE_COMMIT_ATTACH) + view_attach(surface->view, buffer); + view_update(surface->view); + } + + surface->pending.commit = 0; } -void set_buffer_transform(struct wl_client * client, - struct wl_resource * surface, int32_t transform) +void +set_buffer_transform(struct wl_client *client, + struct wl_resource *surface, int32_t transform) { - /* TODO: Implement */ + /* TODO: Implement */ } -void set_buffer_scale(struct wl_client * client, struct wl_resource * surface, - int32_t scale) +void +set_buffer_scale(struct wl_client *client, struct wl_resource *surface, + int32_t scale) { - /* TODO: Implement */ + /* TODO: Implement */ } static struct wl_surface_interface surface_implementation = { - .destroy = &destroy, - .attach = &attach, - .damage = &damage, - .frame = &frame, - .set_opaque_region = &set_opaque_region, - .set_input_region = &set_input_region, - .commit = &commit, - .set_buffer_transform = &set_buffer_transform, - .set_buffer_scale = &set_buffer_scale + .destroy = &destroy, + .attach = &attach, + .damage = &damage, + .frame = &frame, + .set_opaque_region = &set_opaque_region, + .set_input_region = &set_input_region, + .commit = &commit, + .set_buffer_transform = &set_buffer_transform, + .set_buffer_scale = &set_buffer_scale }; -static void surface_destroy(struct wl_resource * resource) +static void +surface_destroy(struct wl_resource *resource) { - struct surface * surface = wl_resource_get_user_data(resource); + struct surface *surface = wl_resource_get_user_data(resource); - state_finalize(&surface->state); - state_finalize(&surface->pending.state); + state_finalize(&surface->state); + state_finalize(&surface->pending.state); - if (surface->view) - wl_list_remove(&surface->view_handler.link); + if (surface->view) + wl_list_remove(&surface->view_handler.link); - free(surface); + free(surface); } /** @@ -354,48 +350,48 @@ static void surface_destroy(struct wl_resource * resource) * * @return The newly allocated surface. */ -struct surface * surface_new(struct wl_client * client, - uint32_t version, uint32_t id) +struct surface * +surface_new(struct wl_client *client, + uint32_t version, uint32_t id) { - struct surface * surface; + struct surface *surface; - surface = malloc(sizeof *surface); + surface = malloc(sizeof *surface); - if (!surface) - return NULL; + if (!surface) + return NULL; - /* Initialize the surface. */ - surface->pending.commit = 0; - surface->view = NULL; - surface->view_handler.impl = &view_handler_impl; + /* Initialize the surface. */ + surface->pending.commit = 0; + surface->view = NULL; + surface->view_handler.impl = &view_handler_impl; - state_initialize(&surface->state); - state_initialize(&surface->pending.state); + state_initialize(&surface->state); + state_initialize(&surface->pending.state); - /* Add the surface to the client. */ - surface->resource = wl_resource_create(client, &wl_surface_interface, - version, id); - wl_resource_set_implementation(surface->resource, &surface_implementation, - surface, &surface_destroy); + /* Add the surface to the client. */ + surface->resource = wl_resource_create(client, &wl_surface_interface, + version, id); + wl_resource_set_implementation(surface->resource, &surface_implementation, + surface, &surface_destroy); - return surface; + return surface; } -void surface_set_view(struct surface * surface, struct view * view) +void +surface_set_view(struct surface *surface, struct view *view) { - if (surface->view == view) - return; + if (surface->view == view) + return; - if (surface->view) - wl_list_remove(&surface->view_handler.link); + if (surface->view) + wl_list_remove(&surface->view_handler.link); - surface->view = view; + surface->view = view; - if (view) - { - wl_list_insert(&view->handlers, &surface->view_handler.link); - view_attach(view, surface->state.buffer); - view_update(view); - } + if (view) { + wl_list_insert(&view->handlers, &surface->view_handler.link); + view_attach(view, surface->state.buffer); + view_update(view); + } } - diff --git a/libswc/surface.h b/libswc/surface.h @@ -30,54 +30,50 @@ #include <wayland-server.h> #include <pixman.h> -enum -{ - SURFACE_COMMIT_ATTACH = (1 << 0), - SURFACE_COMMIT_DAMAGE = (1 << 1), - SURFACE_COMMIT_OPAQUE = (1 << 2), - SURFACE_COMMIT_INPUT = (1 << 3), - SURFACE_COMMIT_FRAME = (1 << 4) +enum { + SURFACE_COMMIT_ATTACH = (1 << 0), + SURFACE_COMMIT_DAMAGE = (1 << 1), + SURFACE_COMMIT_OPAQUE = (1 << 2), + SURFACE_COMMIT_INPUT = (1 << 3), + SURFACE_COMMIT_FRAME = (1 << 4) }; -struct surface_state -{ - struct wld_buffer * buffer; - struct wl_resource * buffer_resource; - struct wl_listener buffer_destroy_listener; +struct surface_state { + struct wld_buffer *buffer; + struct wl_resource *buffer_resource; + struct wl_listener buffer_destroy_listener; - /* The region that needs to be repainted. */ - pixman_region32_t damage; + /* The region that needs to be repainted. */ + pixman_region32_t damage; - /* The region that is opaque. */ - pixman_region32_t opaque; + /* The region that is opaque. */ + pixman_region32_t opaque; - /* The region that accepts input. */ - pixman_region32_t input; + /* The region that accepts input. */ + pixman_region32_t input; - struct wl_list frame_callbacks; + struct wl_list frame_callbacks; }; -struct surface -{ - struct wl_resource * resource; +struct surface { + struct wl_resource *resource; - struct surface_state state; + struct surface_state state; - struct - { - struct surface_state state; - uint32_t commit; - int32_t x, y; - } pending; + struct + { + struct surface_state state; + uint32_t commit; + int32_t x, y; + } pending; - struct view * view; - struct view_handler view_handler; + struct view *view; + struct view_handler view_handler; }; -struct surface * surface_new(struct wl_client * client, - uint32_t version, uint32_t id); +struct surface *surface_new(struct wl_client *client, + uint32_t version, uint32_t id); -void surface_set_view(struct surface * surface, struct view * view); +void surface_set_view(struct surface *surface, struct view *view); #endif - diff --git a/libswc/swc.c b/libswc/swc.c @@ -41,7 +41,7 @@ #include "window.h" #include "xdg_shell.h" #ifdef ENABLE_XWAYLAND -# include "xserver.h" +#include "xserver.h" #endif extern struct swc_launch swc_launch; @@ -57,200 +57,190 @@ extern struct swc_xserver swc_xserver; extern struct pointer_handler screens_pointer_handler; struct swc swc = { - .seat = &swc_seat, - .bindings = &swc_bindings, - .compositor = &swc_compositor, - .drm = &swc_drm, - .shm = &swc_shm, + .seat = &swc_seat, + .bindings = &swc_bindings, + .compositor = &swc_compositor, + .drm = &swc_drm, + .shm = &swc_shm, #ifdef ENABLE_XWAYLAND - .xserver = &swc_xserver, + .xserver = &swc_xserver, #endif }; -static void setup_compositor(void) +static void +setup_compositor(void) { - pixman_region32_t pointer_region; - struct screen * screen; - struct swc_rectangle * geometry; - - wl_list_insert(&swc.seat->keyboard->handlers, - &swc.bindings->keyboard_handler->link); - wl_list_insert(&swc.seat->pointer->handlers, - &swc.bindings->pointer_handler->link); - wl_list_insert(&swc.seat->pointer->handlers, - &swc.compositor->pointer_handler->link); - wl_list_insert(&swc.seat->pointer->handlers, - &screens_pointer_handler.link); - wl_signal_add(&swc.seat->pointer->focus.event_signal, - &window_enter_listener); - - /* Calculate pointer region */ - pixman_region32_init(&pointer_region); - - wl_list_for_each(screen, &swc.screens, link) - { - geometry = &screen->base.geometry; - pixman_region32_union_rect(&pointer_region, &pointer_region, - geometry->x, geometry->y, - geometry->width, geometry->height); - } - - pointer_set_region(swc.seat->pointer, &pointer_region); - pixman_region32_fini(&pointer_region); + pixman_region32_t pointer_region; + struct screen *screen; + struct swc_rectangle *geometry; + + wl_list_insert(&swc.seat->keyboard->handlers, + &swc.bindings->keyboard_handler->link); + wl_list_insert(&swc.seat->pointer->handlers, + &swc.bindings->pointer_handler->link); + wl_list_insert(&swc.seat->pointer->handlers, + &swc.compositor->pointer_handler->link); + wl_list_insert(&swc.seat->pointer->handlers, + &screens_pointer_handler.link); + wl_signal_add(&swc.seat->pointer->focus.event_signal, + &window_enter_listener); + + /* Calculate pointer region */ + pixman_region32_init(&pointer_region); + + wl_list_for_each (screen, &swc.screens, link) { + geometry = &screen->base.geometry; + pixman_region32_union_rect(&pointer_region, &pointer_region, + geometry->x, geometry->y, + geometry->width, geometry->height); + } + + pointer_set_region(swc.seat->pointer, &pointer_region); + pixman_region32_fini(&pointer_region); } -void swc_activate(void) +void +swc_activate(void) { - swc.active = true; - send_event(&swc.event_signal, SWC_EVENT_ACTIVATED, NULL); + swc.active = true; + send_event(&swc.event_signal, SWC_EVENT_ACTIVATED, NULL); } -void swc_deactivate(void) +void +swc_deactivate(void) { - swc.active = false; - send_event(&swc.event_signal, SWC_EVENT_DEACTIVATED, NULL); + swc.active = false; + send_event(&swc.event_signal, SWC_EVENT_DEACTIVATED, NULL); } EXPORT -bool swc_initialize(struct wl_display * display, - struct wl_event_loop * event_loop, - const struct swc_manager * manager) +bool +swc_initialize(struct wl_display *display, + struct wl_event_loop *event_loop, + const struct swc_manager *manager) { - swc.display = display; - swc.event_loop = event_loop ?: wl_display_get_event_loop(display); - swc.manager = manager; - const char * default_seat = "seat0"; - wl_signal_init(&swc.event_signal); - - if (!launch_initialize()) - { - ERROR("Could not connect to swc-launch\n"); - goto error0; - } - - if (!drm_initialize()) - { - ERROR("Could not initialize DRM\n"); - goto error1; - } - - if (!shm_initialize()) - { - ERROR("Could not initialize SHM\n"); - goto error2; - } - - if (!bindings_initialize()) - { - ERROR("Could not initialize bindings\n"); - goto error3; - } - - if (!subcompositor_initialize()) - { - ERROR("Could not initialize subcompositor\n"); - goto error4; - } - - if (!screens_initialize()) - { - ERROR("Could not initialize screens\n"); - goto error5; - } - - if (!compositor_initialize()) - { - ERROR("Could not initialize compositor\n"); - goto error6; - } - - if (!data_device_manager_initialize()) - { - ERROR("Could not initialize data device manager\n"); - goto error7; - } - - if (!seat_initialize(default_seat)) - { - ERROR("Could not initialize seat\n"); - goto error8; - } - - if (!shell_initialize()) - { - ERROR("Could not initialize shell\n"); - goto error9; - } - - if (!xdg_shell_initialize()) - { - ERROR("Could not initialize XDG shell\n"); - goto error10; - } - - if (!panel_manager_initialize()) - { - ERROR("Could not initialize panel manager\n"); - goto error11; - } + swc.display = display; + swc.event_loop = event_loop ?: wl_display_get_event_loop(display); + swc.manager = manager; + const char *default_seat = "seat0"; + wl_signal_init(&swc.event_signal); + + if (!launch_initialize()) { + ERROR("Could not connect to swc-launch\n"); + goto error0; + } + + if (!drm_initialize()) { + ERROR("Could not initialize DRM\n"); + goto error1; + } + + if (!shm_initialize()) { + ERROR("Could not initialize SHM\n"); + goto error2; + } + + if (!bindings_initialize()) { + ERROR("Could not initialize bindings\n"); + goto error3; + } + + if (!subcompositor_initialize()) { + ERROR("Could not initialize subcompositor\n"); + goto error4; + } + + if (!screens_initialize()) { + ERROR("Could not initialize screens\n"); + goto error5; + } + + if (!compositor_initialize()) { + ERROR("Could not initialize compositor\n"); + goto error6; + } + + if (!data_device_manager_initialize()) { + ERROR("Could not initialize data device manager\n"); + goto error7; + } + + if (!seat_initialize(default_seat)) { + ERROR("Could not initialize seat\n"); + goto error8; + } + + if (!shell_initialize()) { + ERROR("Could not initialize shell\n"); + goto error9; + } + + if (!xdg_shell_initialize()) { + ERROR("Could not initialize XDG shell\n"); + goto error10; + } + + if (!panel_manager_initialize()) { + ERROR("Could not initialize panel manager\n"); + goto error11; + } #ifdef ENABLE_XWAYLAND - if (!xserver_initialize()) - { - ERROR("Could not initialize xwayland\n"); - goto error12; - } + if (!xserver_initialize()) { + ERROR("Could not initialize xwayland\n"); + goto error12; + } #endif - setup_compositor(); + setup_compositor(); - return true; + return true; #ifdef ENABLE_XWAYLAND - error12: - panel_manager_finalize(); +error12: + panel_manager_finalize(); #endif - error11: - xdg_shell_finalize(); - error10: - shell_finalize(); - error9: - seat_finalize(); - error8: - data_device_manager_finalize(); - error7: - subcompositor_finalize(); - error6: - compositor_finalize(); - error5: - screens_finalize(); - error4: - bindings_finalize(); - error3: - shm_finalize(); - error2: - drm_finalize(); - error1: - launch_finalize(); - error0: - return false; +error11: + xdg_shell_finalize(); +error10: + shell_finalize(); +error9: + seat_finalize(); +error8: + data_device_manager_finalize(); +error7: + subcompositor_finalize(); +error6: + compositor_finalize(); +error5: + screens_finalize(); +error4: + bindings_finalize(); +error3: + shm_finalize(); +error2: + drm_finalize(); +error1: + launch_finalize(); +error0: + return false; } EXPORT -void swc_finalize(void) +void +swc_finalize(void) { #ifdef ENABLE_XWAYLAND - xserver_finalize(); + xserver_finalize(); #endif - panel_manager_finalize(); - shell_finalize(); - seat_finalize(); - data_device_manager_finalize(); - compositor_finalize(); - screens_finalize(); - bindings_finalize(); - shm_finalize(); - drm_finalize(); - launch_finalize(); + panel_manager_finalize(); + shell_finalize(); + seat_finalize(); + data_device_manager_finalize(); + compositor_finalize(); + screens_finalize(); + bindings_finalize(); + shm_finalize(); + drm_finalize(); + launch_finalize(); } - diff --git a/libswc/swc.h b/libswc/swc.h @@ -30,156 +30,151 @@ /* Rectangles {{{ */ -struct swc_rectangle -{ - int32_t x, y; - uint32_t width, height; +struct swc_rectangle { + int32_t x, y; + uint32_t width, height; }; /* }}} */ /* Screens {{{ */ -struct swc_screen_handler -{ - /** +struct swc_screen_handler { + /** * Called when the screen is about to be destroyed. * * After this is called, the screen is no longer valid. */ - void (* destroy)(void * data); + void (*destroy)(void *data); - /** + /** * Called when the total area of the screen has changed. */ - void (* geometry_changed)(void * data); + void (*geometry_changed)(void *data); - /** + /** * Called when the geometry of the screen available for laying out windows * has changed. * * A window manager should respond by making sure all visible windows are * within this area. */ - void (* usable_geometry_changed)(void * data); + void (*usable_geometry_changed)(void *data); - /** + /** * Called when the pointer enters the screen. */ - void (* entered)(void * data); + void (*entered)(void *data); }; -struct swc_screen -{ - /** +struct swc_screen { + /** * The total area of the screen. */ - struct swc_rectangle geometry; + struct swc_rectangle geometry; - /** + /** * The area of the screen available for placing windows. */ - struct swc_rectangle usable_geometry; + struct swc_rectangle usable_geometry; }; /** * Set the handler associated with this screen. */ -void swc_screen_set_handler(struct swc_screen * screen, - const struct swc_screen_handler * handler, - void * data); +void swc_screen_set_handler(struct swc_screen *screen, + const struct swc_screen_handler *handler, + void *data); /* }}} */ /* Windows {{{ */ -struct swc_window_handler -{ - /** +struct swc_window_handler { + /** * Called when the window is about to be destroyed. * * After this is called, the window is no longer valid. */ - void (* destroy)(void * data); + void (*destroy)(void *data); - /** + /** * Called when the window's title changes. */ - void (* title_changed)(void * data); + void (*title_changed)(void *data); - /** + /** * Called when the window's application identifier changes. */ - void (* app_id_changed)(void * data); + void (*app_id_changed)(void *data); - /** + /** * Called when the window's parent changes. * * This can occur when the window becomes a transient for another window, or * becomes a toplevel window. */ - void (* parent_changed)(void * data); + void (*parent_changed)(void *data); - /** + /** * Called when the pointer enters the window. */ - void (* entered)(void * data); + void (*entered)(void *data); - /** + /** * Called when the window wants to initiate an interactive move, but the * window is not in stacked mode. * * The window manager may respond by changing the window's mode, after which * the interactive move will be honored. */ - void (* move)(void * data); + void (*move)(void *data); - /** + /** * Called when the window wants to initiate an interactive resize, but the * window is not in stacked mode. * * The window manager may respond by changing the window's mode, after which * the interactive resize will be honored. */ - void (* resize)(void * data); + void (*resize)(void *data); }; -struct swc_window -{ - char * title; - char * app_id; +struct swc_window { + char *title; + char *app_id; - struct swc_window * parent; + struct swc_window *parent; }; /** * Set the handler associated with this window. */ -void swc_window_set_handler(struct swc_window * window, - const struct swc_window_handler * handler, - void * data); +void swc_window_set_handler(struct swc_window *window, + const struct swc_window_handler *handler, + void *data); /** * Request that the specified window close. */ -void swc_window_close(struct swc_window * window); +void swc_window_close(struct swc_window *window); /** * Make the specified window visible. */ -void swc_window_show(struct swc_window * window); +void swc_window_show(struct swc_window *window); /** * Make the specified window hidden. */ -void swc_window_hide(struct swc_window * window); +void swc_window_hide(struct swc_window *window); /** * Set the keyboard focus to the specified window. * * If window is NULL, the keyboard will have no focus. */ -void swc_window_focus(struct swc_window * window); +void swc_window_focus(struct swc_window *window); /** * Sets the window to stacked mode. @@ -190,7 +185,7 @@ void swc_window_focus(struct swc_window * window); * * Use of this mode is required to allow interactive moving and resizing. */ -void swc_window_set_stacked(struct swc_window * window); +void swc_window_set_stacked(struct swc_window *window); /** * Sets the window to tiled mode. @@ -201,13 +196,13 @@ void swc_window_set_stacked(struct swc_window * window); * * It is invalid to interactively move or resize a window in tiled mode. */ -void swc_window_set_tiled(struct swc_window * window); +void swc_window_set_tiled(struct swc_window *window); /** * Sets the window to fullscreen mode. */ -void swc_window_set_fullscreen(struct swc_window * window, - struct swc_screen * screen); +void swc_window_set_fullscreen(struct swc_window *window, + struct swc_screen *screen); /** * Set the window's position. @@ -215,7 +210,7 @@ void swc_window_set_fullscreen(struct swc_window * window, * The x and y coordinates refer to the top-left corner of the actual contents * of the window and should be adjusted for the border size. */ -void swc_window_set_position(struct swc_window * window, int32_t x, int32_t y); +void swc_window_set_position(struct swc_window *window, int32_t x, int32_t y); /** * Set the window's size. @@ -223,7 +218,7 @@ void swc_window_set_position(struct swc_window * window, int32_t x, int32_t y); * The width and height refer to the dimension of the actual contents of the * window and should be adjusted for the border size. */ -void swc_window_set_size(struct swc_window * window, +void swc_window_set_size(struct swc_window *window, uint32_t width, uint32_t height); /** @@ -232,8 +227,8 @@ void swc_window_set_size(struct swc_window * window, * This is a convenience function that is equivalent to calling * swc_window_set_size and then swc_window_set_position. */ -void swc_window_set_geometry(struct swc_window * window, - const struct swc_rectangle * geometry); +void swc_window_set_geometry(struct swc_window *window, + const struct swc_rectangle *geometry); /** * Set the window's border color and width. @@ -241,59 +236,56 @@ void swc_window_set_geometry(struct swc_window * window, * NOTE: The window's geometry remains unchanged, and should be updated if a * fixed top-left corner of the border is desired. */ -void swc_window_set_border(struct swc_window * window, +void swc_window_set_border(struct swc_window *window, uint32_t color, uint32_t width); /** * Begin an interactive move of the specified window. */ -void swc_window_begin_move(struct swc_window * window); +void swc_window_begin_move(struct swc_window *window); /** * End an interactive move of the specified window. */ -void swc_window_end_move(struct swc_window * window); - -enum -{ - SWC_WINDOW_EDGE_AUTO = 0, - SWC_WINDOW_EDGE_TOP = (1 << 0), - SWC_WINDOW_EDGE_BOTTOM = (1 << 1), - SWC_WINDOW_EDGE_LEFT = (1 << 2), - SWC_WINDOW_EDGE_RIGHT = (1 << 3) +void swc_window_end_move(struct swc_window *window); + +enum { + SWC_WINDOW_EDGE_AUTO = 0, + SWC_WINDOW_EDGE_TOP = (1 << 0), + SWC_WINDOW_EDGE_BOTTOM = (1 << 1), + SWC_WINDOW_EDGE_LEFT = (1 << 2), + SWC_WINDOW_EDGE_RIGHT = (1 << 3) }; /** * Begin an interactive resize of the specified window. */ -void swc_window_begin_resize(struct swc_window * window, uint32_t edges); +void swc_window_begin_resize(struct swc_window *window, uint32_t edges); /** * End an interactive resize of the specified window. */ -void swc_window_end_resize(struct swc_window * window); +void swc_window_end_resize(struct swc_window *window); /* }}} */ /* Bindings {{{ */ -enum -{ - SWC_MOD_CTRL = 1 << 0, - SWC_MOD_ALT = 1 << 1, - SWC_MOD_LOGO = 1 << 2, - SWC_MOD_SHIFT = 1 << 3, - SWC_MOD_ANY = ~0 +enum { + SWC_MOD_CTRL = 1 << 0, + SWC_MOD_ALT = 1 << 1, + SWC_MOD_LOGO = 1 << 2, + SWC_MOD_SHIFT = 1 << 3, + SWC_MOD_ANY = ~0 }; -enum swc_binding_type -{ - SWC_BINDING_KEY, - SWC_BINDING_BUTTON, +enum swc_binding_type { + SWC_BINDING_KEY, + SWC_BINDING_BUTTON, }; -typedef void (* swc_binding_handler)(void * data, uint32_t time, - uint32_t value, uint32_t state); +typedef void (*swc_binding_handler)(void *data, uint32_t time, + uint32_t value, uint32_t state); /** * Register a new input binding. @@ -302,7 +294,7 @@ typedef void (* swc_binding_handler)(void * data, uint32_t time, */ int swc_add_binding(enum swc_binding_type type, uint32_t modifiers, uint32_t value, - swc_binding_handler handler, void * data); + swc_binding_handler handler, void *data); /* }}} */ @@ -310,26 +302,25 @@ int swc_add_binding(enum swc_binding_type type, * This is a user-provided structure that swc will use to notify the display * server of new windows and screens. */ -struct swc_manager -{ - /** +struct swc_manager { + /** * Called when a new screen is created. */ - void (* new_screen)(struct swc_screen * screen); + void (*new_screen)(struct swc_screen *screen); - /** + /** * Called when a new window is created. */ - void (* new_window)(struct swc_window * window); + void (*new_window)(struct swc_window *window); }; /** * Initializes the compositor using the specified display, event_loop, and * manager. */ -bool swc_initialize(struct wl_display * display, - struct wl_event_loop * event_loop, - const struct swc_manager * manager); +bool swc_initialize(struct wl_display *display, + struct wl_event_loop *event_loop, + const struct swc_manager *manager); /** * Stops the compositor, releasing any used resources. @@ -339,4 +330,3 @@ void swc_finalize(void); #endif /* vim: set fdm=marker : */ - diff --git a/libswc/util.c b/libswc/util.c @@ -26,12 +26,11 @@ #include <wayland-server.h> pixman_box32_t infinite_extents = { - .x1 = INT32_MIN, .y1 = INT32_MIN, - .x2 = INT32_MAX, .y2 = INT32_MAX + .x1 = INT32_MIN, .y1 = INT32_MIN, .x2 = INT32_MAX, .y2 = INT32_MAX }; -void remove_resource(struct wl_resource * resource) +void +remove_resource(struct wl_resource *resource) { - wl_list_remove(wl_resource_get_link(resource)); + wl_list_remove(wl_resource_get_link(resource)); } - diff --git a/libswc/util.h b/libswc/util.h @@ -38,66 +38,69 @@ #define EXPORT __attribute__((visibility("default"))) #if ENABLE_DEBUG -# define MESSAGE_SOURCE \ - fprintf(stderr, "[swc:%s:%d] ", __FILE__, __LINE__); +#define MESSAGE_SOURCE \ + fprintf(stderr, "[swc:%s:%d] ", __FILE__, __LINE__); #else -# define MESSAGE_SOURCE +#define MESSAGE_SOURCE #endif -#define MESSAGE(type, format, ...) \ - do { MESSAGE_SOURCE \ - fprintf(stderr, type ": " format, ## __VA_ARGS__); } \ - while (false) +#define MESSAGE(type, format, ...) \ + do { \ + MESSAGE_SOURCE \ + fprintf(stderr, type ": " format, ##__VA_ARGS__); \ + } while (false) -#define WARNING(format, ...) MESSAGE("WARNING", format, ## __VA_ARGS__) -#define ERROR(format, ...) MESSAGE("ERROR", format, ## __VA_ARGS__) +#define WARNING(format, ...) MESSAGE("WARNING", format, ##__VA_ARGS__) +#define ERROR(format, ...) MESSAGE("ERROR", format, ##__VA_ARGS__) #if ENABLE_DEBUG -# define DEBUG(format, ...) MESSAGE("DEBUG", format, ## __VA_ARGS__) +#define DEBUG(format, ...) MESSAGE("DEBUG", format, ##__VA_ARGS__) #else -# define DEBUG(format, ...) +#define DEBUG(format, ...) #endif -#define ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0]) +#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0]) struct wl_resource; -void remove_resource(struct wl_resource * resource); +void remove_resource(struct wl_resource *resource); -static inline uint32_t get_time(void) +static inline uint32_t +get_time(void) { - struct timeval timeval; + struct timeval timeval; - gettimeofday(&timeval, NULL); - return timeval.tv_sec * 1000 + timeval.tv_usec / 1000; + gettimeofday(&timeval, NULL); + return timeval.tv_sec * 1000 + timeval.tv_usec / 1000; } extern pixman_box32_t infinite_extents; -static inline bool rectangle_contains_point - (const struct swc_rectangle * rectangle, int32_t x, int32_t y) +static inline bool +rectangle_contains_point(const struct swc_rectangle *rectangle, int32_t x, int32_t y) { - return x > rectangle->x && x < rectangle->x + rectangle->width - && y > rectangle->y && y < rectangle->y + rectangle->height; + return x > rectangle->x && x < rectangle->x + rectangle->width + && y > rectangle->y && y < rectangle->y + rectangle->height; } -static inline bool rectangle_overlap(const struct swc_rectangle * r1, - const struct swc_rectangle * r2) +static inline bool +rectangle_overlap(const struct swc_rectangle *r1, + const struct swc_rectangle *r2) { - return (MAX(r1->x + r1->width, r2->x + r2->width) - MIN(r1->x, r2->x) - < r1->width + r2->width) - && (MAX(r1->y + r1->height, r2->y + r2->height) - MIN(r1->y, r2->y) - < r1->height + r2->height); + return (MAX(r1->x + r1->width, r2->x + r2->width) - MIN(r1->x, r2->x) + < r1->width + r2->width) + && (MAX(r1->y + r1->height, r2->y + r2->height) - MIN(r1->y, r2->y) + < r1->height + r2->height); } -static inline void array_remove(struct wl_array * array, - void * item, size_t size) +static inline void +array_remove(struct wl_array *array, + void *item, size_t size) { - size_t bytes = array->size - ((intptr_t) item + size - (intptr_t) array->data); - if (bytes > 0) - memmove(item, (void *)((intptr_t) item + size), bytes); - array->size -= size; + size_t bytes = array->size - ((intptr_t)item + size - (intptr_t)array->data); + if (bytes > 0) + memmove(item, (void *)((intptr_t)item + size), bytes); + array->size -= size; } #endif - diff --git a/libswc/view.c b/libswc/view.c @@ -29,132 +29,139 @@ #include <wld/wld.h> -#define HANDLE(view, handler, method, ...) \ - do \ - { \ - wl_list_for_each(handler, &view->handlers, link) \ - { \ - if (handler->impl->method) \ - handler->impl->method(handler, ## __VA_ARGS__); \ - } \ - } while (0) - -void view_initialize(struct view * view, const struct view_impl * impl) +#define HANDLE(view, handler, method, ...) \ + do { \ + wl_list_for_each (handler, &view->handlers, link) { \ + if (handler->impl->method) \ + handler->impl->method(handler, ##__VA_ARGS__); \ + } \ + } while (0) + +void +view_initialize(struct view *view, const struct view_impl *impl) { - view->impl = impl; - view->geometry.x = 0; - view->geometry.y = 0; - view->geometry.width = 0; - view->geometry.height = 0; - view->buffer = NULL; - view->screens = 0; - wl_list_init(&view->handlers); + view->impl = impl; + view->geometry.x = 0; + view->geometry.y = 0; + view->geometry.width = 0; + view->geometry.height = 0; + view->buffer = NULL; + view->screens = 0; + wl_list_init(&view->handlers); } -void view_finalize(struct view * view) +void +view_finalize(struct view *view) { - if (view->buffer) - wld_buffer_unreference(view->buffer); + if (view->buffer) + wld_buffer_unreference(view->buffer); } -int view_attach(struct view * view, struct wld_buffer * buffer) +int +view_attach(struct view *view, struct wld_buffer *buffer) { - int ret; - struct view_handler * handler; + int ret; + struct view_handler *handler; - if ((ret = view->impl->attach(view, buffer)) < 0) - return ret; + if ((ret = view->impl->attach(view, buffer)) < 0) + return ret; - if (view->buffer) - wld_buffer_unreference(view->buffer); + if (view->buffer) + wld_buffer_unreference(view->buffer); - if (buffer) - wld_buffer_reference(buffer); + if (buffer) + wld_buffer_reference(buffer); - view->buffer = buffer; - HANDLE(view, handler, attach); + view->buffer = buffer; + HANDLE(view, handler, attach); - return 0; + return 0; } -bool view_update(struct view * view) +bool +view_update(struct view *view) { - return view->impl->update(view); + return view->impl->update(view); } -bool view_move(struct view * view, int32_t x, int32_t y) +bool +view_move(struct view *view, int32_t x, int32_t y) { - return view->impl->move(view, x, y); + return view->impl->move(view, x, y); } -bool view_set_position(struct view * view, int32_t x, int32_t y) +bool +view_set_position(struct view *view, int32_t x, int32_t y) { - struct view_handler * handler; + struct view_handler *handler; - if (x == view->geometry.x && y == view->geometry.y) - return false; + if (x == view->geometry.x && y == view->geometry.y) + return false; - view->geometry.x = x; - view->geometry.y = y; - HANDLE(view, handler, move); + view->geometry.x = x; + view->geometry.y = y; + HANDLE(view, handler, move); - return true; + return true; } -bool view_set_size(struct view * view, uint32_t width, uint32_t height) +bool +view_set_size(struct view *view, uint32_t width, uint32_t height) { - struct view_handler * handler; + struct view_handler *handler; - if (view->geometry.width == width && view->geometry.height == height) - return false; + if (view->geometry.width == width && view->geometry.height == height) + return false; - uint32_t old_width = view->geometry.width, - old_height = view->geometry.height; + uint32_t old_width = view->geometry.width, + old_height = view->geometry.height; - view->geometry.width = width; - view->geometry.height = height; - HANDLE(view, handler, resize, old_width, old_height); + view->geometry.width = width; + view->geometry.height = height; + HANDLE(view, handler, resize, old_width, old_height); - return true; + return true; } -bool view_set_size_from_buffer(struct view * view, struct wld_buffer * buffer) +bool +view_set_size_from_buffer(struct view *view, struct wld_buffer *buffer) { - return view_set_size(view, buffer ? buffer->width : 0, - buffer ? buffer->height : 0); + return view_set_size(view, buffer ? buffer->width : 0, + buffer ? buffer->height : 0); } -void view_set_screens(struct view * view, uint32_t screens) +void +view_set_screens(struct view *view, uint32_t screens) { - if (view->screens == screens) - return; + if (view->screens == screens) + return; - uint32_t entered = screens & ~view->screens, - left = view->screens & ~screens; - struct view_handler * handler; + uint32_t entered = screens & ~view->screens, + left = view->screens & ~screens; + struct view_handler *handler; - view->screens = screens; - HANDLE(view, handler, screens, entered, left); + view->screens = screens; + HANDLE(view, handler, screens, entered, left); } -void view_update_screens(struct view * view) +void +view_update_screens(struct view *view) { - uint32_t screens = 0; - struct screen * screen; + uint32_t screens = 0; + struct screen *screen; - wl_list_for_each(screen, &swc.screens, link) - { - if (rectangle_overlap(&screen->base.geometry, &view->geometry)) - screens |= screen_mask(screen); - } + wl_list_for_each (screen, &swc.screens, link) { + if (rectangle_overlap(&screen->base.geometry, &view->geometry)) + screens |= screen_mask(screen); + } - view_set_screens(view, screens); + view_set_screens(view, screens); } -void view_frame(struct view * view, uint32_t time) +void +view_frame(struct view *view, uint32_t time) { - struct view_handler * handler; + struct view_handler *handler; - HANDLE(view, handler, frame, time); + HANDLE(view, handler, frame, time); } - diff --git a/libswc/view.h b/libswc/view.h @@ -38,21 +38,19 @@ * way, allowing operations like setting the output view of a surface directly * to an output's framebuffer plane, bypassing the compositor. */ -struct view -{ - const struct view_impl * impl; - struct wl_list handlers; +struct view { + const struct view_impl *impl; + struct wl_list handlers; - struct swc_rectangle geometry; - uint32_t screens; + struct swc_rectangle geometry; + uint32_t screens; - struct wld_buffer * buffer; + struct wld_buffer *buffer; }; -struct view_handler -{ - const struct view_handler_impl * impl; - struct wl_list link; +struct view_handler { + const struct view_handler_impl *impl; + struct wl_list link; }; /** @@ -60,27 +58,25 @@ struct view_handler * * For descriptions, see the corresponding view_* function. */ -struct view_impl -{ - bool (* update)(struct view * view); - int (* attach)(struct view * view, struct wld_buffer * buffer); - bool (* move)(struct view * view, int32_t x, int32_t y); +struct view_impl { + bool (*update)(struct view *view); + int (*attach)(struct view *view, struct wld_buffer *buffer); + bool (*move)(struct view *view, int32_t x, int32_t y); }; -struct view_handler_impl -{ - /* Called when the view has displayed the next frame. */ - void (* frame)(struct view_handler * handler, uint32_t time); - /* Called when a new buffer is attached to the view. */ - void (* attach)(struct view_handler * handler); - /* Called after the view's position changes. */ - void (* move)(struct view_handler * handler); - /* Called after the view's size changes. */ - void (* resize)(struct view_handler * handler, - uint32_t old_width, uint32_t old_height); - /* Called when the set of screens the view is visible on changes. */ - void (* screens)(struct view_handler * handler, - uint32_t left, uint32_t entered); +struct view_handler_impl { + /* Called when the view has displayed the next frame. */ + void (*frame)(struct view_handler *handler, uint32_t time); + /* Called when a new buffer is attached to the view. */ + void (*attach)(struct view_handler *handler); + /* Called after the view's position changes. */ + void (*move)(struct view_handler *handler); + /* Called after the view's size changes. */ + void (*resize)(struct view_handler *handler, + uint32_t old_width, uint32_t old_height); + /* Called when the set of screens the view is visible on changes. */ + void (*screens)(struct view_handler *handler, + uint32_t left, uint32_t entered); }; /** @@ -90,39 +86,39 @@ struct view_handler_impl * * @return 0 on success, negative error code otherwise. */ -int view_attach(struct view * view, struct wld_buffer * buffer); +int view_attach(struct view *view, struct wld_buffer *buffer); /** * Display a new frame consisting of the currently attached buffer. * * @return Whether or not the update succeeds. */ -bool view_update(struct view * view); +bool view_update(struct view *view); /** * Move the view to the specified coordinates, if supported. * * @return Whether or not the move succeeds. */ -bool view_move(struct view * view, int32_t x, int32_t y); +bool view_move(struct view *view, int32_t x, int32_t y); /**** For internal view use only ****/ /** * Initialize a new view with the specified implementation. */ -void view_initialize(struct view * view, const struct view_impl * impl); +void view_initialize(struct view *view, const struct view_impl *impl); /** * Release any resources associated with this view. */ -void view_finalize(struct view * view); +void view_finalize(struct view *view); -bool view_set_position(struct view * view, int32_t x, int32_t y); -bool view_set_size(struct view * view, uint32_t width, uint32_t height); -bool view_set_size_from_buffer(struct view * view, struct wld_buffer * bufer); -void view_set_screens(struct view * view, uint32_t screens); -void view_update_screens(struct view * view); +bool view_set_position(struct view *view, int32_t x, int32_t y); +bool view_set_size(struct view *view, uint32_t width, uint32_t height); +bool view_set_size_from_buffer(struct view *view, struct wld_buffer *bufer); +void view_set_screens(struct view *view, uint32_t screens); +void view_update_screens(struct view *view); /** * Send a new frame event through the view's event signal. @@ -131,7 +127,6 @@ void view_update_screens(struct view * view); * the user. If time information is not available, swc_time() can be passed * instead. */ -void view_frame(struct view * view, uint32_t time); +void view_frame(struct view *view, uint32_t time); #endif - diff --git a/libswc/wayland_buffer.c b/libswc/wayland_buffer.c @@ -29,50 +29,50 @@ #include <wld/wld.h> #include <wld/pixman.h> -static void destroy(struct wl_client * client, struct wl_resource * resource) +static void +destroy(struct wl_client *client, struct wl_resource *resource) { - wl_resource_destroy(resource); + wl_resource_destroy(resource); } static const struct wl_buffer_interface buffer_implementation = { - .destroy = &destroy + .destroy = &destroy }; -struct wld_buffer * wayland_buffer_get(struct wl_resource * resource) +struct wld_buffer * +wayland_buffer_get(struct wl_resource *resource) { - if (wl_resource_instance_of(resource, &wl_buffer_interface, - &buffer_implementation)) - { - return wl_resource_get_user_data(resource); - } + if (wl_resource_instance_of(resource, &wl_buffer_interface, + &buffer_implementation)) { + return wl_resource_get_user_data(resource); + } - return NULL; + return NULL; } -static void destroy_buffer(struct wl_resource * resource) +static void +destroy_buffer(struct wl_resource *resource) { - struct wld_buffer * buffer = wl_resource_get_user_data(resource); + struct wld_buffer *buffer = wl_resource_get_user_data(resource); - wld_buffer_unreference(buffer); + wld_buffer_unreference(buffer); } -struct wl_resource * wayland_buffer_create_resource - (struct wl_client * client, uint32_t version, uint32_t id, - struct wld_buffer * buffer) +struct wl_resource * +wayland_buffer_create_resource(struct wl_client *client, uint32_t version, uint32_t id, + struct wld_buffer *buffer) { - struct wl_resource * resource; + struct wl_resource *resource; - resource = wl_resource_create(client, &wl_buffer_interface, version, id); + resource = wl_resource_create(client, &wl_buffer_interface, version, id); - if (!resource) - { - wl_client_post_no_memory(client); - return NULL; - } + if (!resource) { + wl_client_post_no_memory(client); + return NULL; + } - wl_resource_set_implementation(resource, &buffer_implementation, - buffer, &destroy_buffer); + wl_resource_set_implementation(resource, &buffer_implementation, + buffer, &destroy_buffer); - return resource; + return resource; } - diff --git a/libswc/wayland_buffer.h b/libswc/wayland_buffer.h @@ -29,11 +29,9 @@ struct wl_client; struct wl_resource; -struct wld_buffer * wayland_buffer_get(struct wl_resource * resource); +struct wld_buffer *wayland_buffer_get(struct wl_resource *resource); -struct wl_resource * wayland_buffer_create_resource - (struct wl_client * client, uint32_t version, uint32_t id, - struct wld_buffer * buffer); +struct wl_resource *wayland_buffer_create_resource(struct wl_client *client, uint32_t version, uint32_t id, + struct wld_buffer *buffer); #endif - diff --git a/libswc/window.c b/libswc/window.c @@ -34,500 +34,519 @@ #include <stdlib.h> #include <string.h> -#define INTERNAL(w) ((struct window *) (w)) +#define INTERNAL(w) ((struct window *)(w)) static const struct swc_window_handler null_handler; -static void handle_window_enter(struct wl_listener * listener, void * data) +static void +handle_window_enter(struct wl_listener *listener, void *data) { - struct event * event = data; - struct input_focus_event_data * event_data = event->data; - struct window * window; + struct event *event = data; + struct input_focus_event_data *event_data = event->data; + struct window *window; - if (event->type != INPUT_FOCUS_EVENT_CHANGED) - return; + if (event->type != INPUT_FOCUS_EVENT_CHANGED) + return; - if (!event_data->new || !(window = event_data->new->window)) - return; + if (!event_data->new || !(window = event_data->new->window)) + return; - if (window->handler->entered) - window->handler->entered(window->handler_data); + if (window->handler->entered) + window->handler->entered(window->handler_data); } struct wl_listener window_enter_listener = { - .notify = &handle_window_enter + .notify = &handle_window_enter }; -static void begin_interaction(struct window_pointer_interaction * interaction, - struct button * button) +static void +begin_interaction(struct window_pointer_interaction *interaction, + struct button *button) { - if (button) - { - /* Store the serial of the button press so we are able to cancel the + if (button) { + /* Store the serial of the button press so we are able to cancel the * interaction if the window changes from stacked mode. */ - interaction->serial = button->press.serial; - interaction->original_handler = button->handler; - button->handler = &interaction->handler; - } - else - interaction->original_handler = NULL; + interaction->serial = button->press.serial; + interaction->original_handler = button->handler; + button->handler = &interaction->handler; + } else + interaction->original_handler = NULL; - interaction->active = true; - wl_list_insert(&swc.seat->pointer->handlers, &interaction->handler.link); + interaction->active = true; + wl_list_insert(&swc.seat->pointer->handlers, &interaction->handler.link); } -static void end_interaction(struct window_pointer_interaction * interaction, - struct button * button) +static void +end_interaction(struct window_pointer_interaction *interaction, + struct button *button) { - if (!interaction->active) - return; + if (!interaction->active) + return; - if (interaction->original_handler) - { - if (!button) - { - button = pointer_get_button(swc.seat->pointer, interaction->serial); + if (interaction->original_handler) { + if (!button) { + button = pointer_get_button(swc.seat->pointer, interaction->serial); - if (!button) - { - WARNING("No button with serial %u\n", interaction->serial); - goto remove; - } - } + if (!button) { + WARNING("No button with serial %u\n", interaction->serial); + goto remove; + } + } - interaction->original_handler->button(interaction->original_handler, - get_time(), button, - WL_POINTER_BUTTON_STATE_RELEASED); - } + interaction->original_handler->button(interaction->original_handler, + get_time(), button, + WL_POINTER_BUTTON_STATE_RELEASED); + } - remove: - interaction->active = false; - wl_list_remove(&interaction->handler.link); +remove: + interaction->active = false; + wl_list_remove(&interaction->handler.link); } -static void flush(struct window * window) +static void +flush(struct window *window) { - if (window->move.pending) - { - if (window->impl->move) - window->impl->move(window, window->move.x, window->move.y); + if (window->move.pending) { + if (window->impl->move) + window->impl->move(window, window->move.x, window->move.y); - view_move(&window->view->base, window->move.x, window->move.y); - window->move.pending = false; - } + view_move(&window->view->base, window->move.x, window->move.y); + window->move.pending = false; + } } EXPORT -void swc_window_set_handler(struct swc_window * base, - const struct swc_window_handler * handler, - void * data) +void +swc_window_set_handler(struct swc_window *base, + const struct swc_window_handler *handler, + void *data) { - struct window * window = INTERNAL(base); + struct window *window = INTERNAL(base); - window->handler = handler; - window->handler_data = data; + window->handler = handler; + window->handler_data = data; } EXPORT -void swc_window_close(struct swc_window * base) +void +swc_window_close(struct swc_window *base) { - struct window * window = INTERNAL(base); + struct window *window = INTERNAL(base); - if (window->impl->close) - window->impl->close(window); + if (window->impl->close) + window->impl->close(window); } EXPORT -void swc_window_show(struct swc_window * window) +void +swc_window_show(struct swc_window *window) { - compositor_view_show(INTERNAL(window)->view); + compositor_view_show(INTERNAL(window)->view); } EXPORT -void swc_window_hide(struct swc_window * window) +void +swc_window_hide(struct swc_window *window) { - compositor_view_hide(INTERNAL(window)->view); + compositor_view_hide(INTERNAL(window)->view); } EXPORT -void swc_window_focus(struct swc_window * base) +void +swc_window_focus(struct swc_window *base) { - struct window * window = INTERNAL(base); - struct compositor_view * new_focus = window ? window->view : NULL, - * old_focus = swc.seat->keyboard->focus.view; + struct window *window = INTERNAL(base); + struct compositor_view *new_focus = window ? window->view : NULL, + *old_focus = swc.seat->keyboard->focus.view; - if (new_focus == old_focus) - return; + if (new_focus == old_focus) + return; - /* Focus the new window before unfocusing the old one in case both are X11 + /* Focus the new window before unfocusing the old one in case both are X11 * windows so the xwl_window implementation can handle this transition * correctly. */ - if (window && window->impl->focus) - window->impl->focus(window); - if (old_focus && old_focus->window && old_focus->window->impl->unfocus) - old_focus->window->impl->unfocus(old_focus->window); + if (window && window->impl->focus) + window->impl->focus(window); + if (old_focus && old_focus->window && old_focus->window->impl->unfocus) + old_focus->window->impl->unfocus(old_focus->window); - keyboard_set_focus(swc.seat->keyboard, new_focus); + keyboard_set_focus(swc.seat->keyboard, new_focus); } EXPORT -void swc_window_set_stacked(struct swc_window * base) +void +swc_window_set_stacked(struct swc_window *base) { - struct window * window = INTERNAL(base); + struct window *window = INTERNAL(base); - flush(window); - window->configure.pending = false; - window->configure.width = 0; - window->configure.height = 0; - if (window->impl->set_mode) - window->impl->set_mode(window, WINDOW_MODE_STACKED); - window->mode = WINDOW_MODE_STACKED; + flush(window); + window->configure.pending = false; + window->configure.width = 0; + window->configure.height = 0; + if (window->impl->set_mode) + window->impl->set_mode(window, WINDOW_MODE_STACKED); + window->mode = WINDOW_MODE_STACKED; } EXPORT -void swc_window_set_tiled(struct swc_window * base) +void +swc_window_set_tiled(struct swc_window *base) { - struct window * window = INTERNAL(base); + struct window *window = INTERNAL(base); - end_interaction(&window->move.interaction, NULL); - end_interaction(&window->resize.interaction, NULL); - if (window->impl->set_mode) - window->impl->set_mode(window, WINDOW_MODE_TILED); - window->mode = WINDOW_MODE_TILED; + end_interaction(&window->move.interaction, NULL); + end_interaction(&window->resize.interaction, NULL); + if (window->impl->set_mode) + window->impl->set_mode(window, WINDOW_MODE_TILED); + window->mode = WINDOW_MODE_TILED; } EXPORT -void swc_window_set_fullscreen(struct swc_window * base, - struct swc_screen * screen) +void +swc_window_set_fullscreen(struct swc_window *base, + struct swc_screen *screen) { - struct window * window = INTERNAL(base); + struct window *window = INTERNAL(base); - /* TODO: Implement fullscreen windows. */ + /* TODO: Implement fullscreen windows. */ - if (window->impl->set_mode) - window->impl->set_mode(window, WINDOW_MODE_FULLSCREEN); - window->mode = WINDOW_MODE_FULLSCREEN; + if (window->impl->set_mode) + window->impl->set_mode(window, WINDOW_MODE_FULLSCREEN); + window->mode = WINDOW_MODE_FULLSCREEN; } EXPORT -void swc_window_set_position(struct swc_window * base, int32_t x, int32_t y) +void +swc_window_set_position(struct swc_window *base, int32_t x, int32_t y) { - struct window * window = INTERNAL(base); - struct swc_rectangle * geometry = &window->view->base.geometry; + struct window *window = INTERNAL(base); + struct swc_rectangle *geometry = &window->view->base.geometry; - if (x == geometry->x && y == geometry->y) - { - window->move.pending = false; - return; - } + if (x == geometry->x && y == geometry->y) { + window->move.pending = false; + return; + } - window->move.x = x; - window->move.y = y; - window->move.pending = true; + window->move.x = x; + window->move.y = y; + window->move.pending = true; - /* If we don't have a configure pending, perform the move now. */ - if (!window->configure.pending) - flush(window); + /* If we don't have a configure pending, perform the move now. */ + if (!window->configure.pending) + flush(window); } EXPORT -void swc_window_set_size(struct swc_window * base, - uint32_t width, uint32_t height) +void +swc_window_set_size(struct swc_window *base, + uint32_t width, uint32_t height) { - struct window * window = INTERNAL(base); - struct swc_rectangle * geometry = &window->view->base.geometry; + struct window *window = INTERNAL(base); + struct swc_rectangle *geometry = &window->view->base.geometry; - if ((window->configure.pending - && width == window->configure.width - && height == window->configure.height) - || (!window->configure.pending - && width == geometry->width && height == geometry->height)) - { - return; - } + if ((window->configure.pending + && width == window->configure.width + && height == window->configure.height) + || (!window->configure.pending + && width == geometry->width && height == geometry->height)) { + return; + } - window->impl->configure(window, width, height); + window->impl->configure(window, width, height); - if (window->mode == WINDOW_MODE_TILED) - { - window->configure.width = width; - window->configure.height = height; - window->configure.pending = true; - } + if (window->mode == WINDOW_MODE_TILED) { + window->configure.width = width; + window->configure.height = height; + window->configure.pending = true; + } } EXPORT -void swc_window_set_geometry(struct swc_window * window, - const struct swc_rectangle * geometry) +void +swc_window_set_geometry(struct swc_window *window, + const struct swc_rectangle *geometry) { - swc_window_set_size(window, geometry->width, geometry->height); - swc_window_set_position(window, geometry->x, geometry->y); + swc_window_set_size(window, geometry->width, geometry->height); + swc_window_set_position(window, geometry->x, geometry->y); } EXPORT -void swc_window_set_border(struct swc_window * window, - uint32_t border_color, uint32_t border_width) +void +swc_window_set_border(struct swc_window *window, + uint32_t border_color, uint32_t border_width) { - struct compositor_view * view = INTERNAL(window)->view; + struct compositor_view *view = INTERNAL(window)->view; - compositor_view_set_border_color(view, border_color); - compositor_view_set_border_width(view, border_width); + compositor_view_set_border_color(view, border_color); + compositor_view_set_border_width(view, border_width); } EXPORT -void swc_window_begin_move(struct swc_window * window) +void +swc_window_begin_move(struct swc_window *window) { - window_begin_move(INTERNAL(window), NULL); + window_begin_move(INTERNAL(window), NULL); } EXPORT -void swc_window_end_move(struct swc_window * window) +void +swc_window_end_move(struct swc_window *window) { - end_interaction(&INTERNAL(window)->move.interaction, NULL); + end_interaction(&INTERNAL(window)->move.interaction, NULL); } EXPORT -void swc_window_begin_resize(struct swc_window * window, uint32_t edges) +void +swc_window_begin_resize(struct swc_window *window, uint32_t edges) { - window_begin_resize(INTERNAL(window), edges, NULL); + window_begin_resize(INTERNAL(window), edges, NULL); } EXPORT -void swc_window_end_resize(struct swc_window * window) +void +swc_window_end_resize(struct swc_window *window) { - end_interaction(&INTERNAL(window)->resize.interaction, NULL); + end_interaction(&INTERNAL(window)->resize.interaction, NULL); } -static bool move_motion(struct pointer_handler * handler, uint32_t time, - wl_fixed_t fx, wl_fixed_t fy) +static bool +move_motion(struct pointer_handler *handler, uint32_t time, + wl_fixed_t fx, wl_fixed_t fy) { - struct window * window - = wl_container_of(handler, window, move.interaction.handler); + struct window *window = 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); + view_move(&window->view->base, wl_fixed_to_int(fx) + window->move.offset.x, + wl_fixed_to_int(fy) + window->move.offset.y); - return true; + return true; } -static bool resize_motion(struct pointer_handler * handler, uint32_t time, - wl_fixed_t fx, wl_fixed_t fy) +static bool +resize_motion(struct pointer_handler *handler, uint32_t time, + wl_fixed_t fx, wl_fixed_t fy) { - struct window * window - = wl_container_of(handler, window, resize.interaction.handler); - const struct swc_rectangle * geometry = &window->view->base.geometry; - uint32_t width = geometry->width, height = geometry->height; + struct window *window = wl_container_of(handler, window, resize.interaction.handler); + const struct swc_rectangle *geometry = &window->view->base.geometry; + uint32_t width = geometry->width, height = geometry->height; - if (window->resize.edges & SWC_WINDOW_EDGE_LEFT) - width -= wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; - else if (window->resize.edges & SWC_WINDOW_EDGE_RIGHT) - width = wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; + if (window->resize.edges & SWC_WINDOW_EDGE_LEFT) + width -= wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; + else if (window->resize.edges & SWC_WINDOW_EDGE_RIGHT) + width = wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; - if (window->resize.edges & SWC_WINDOW_EDGE_TOP) - height -= wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; - else if (window->resize.edges & SWC_WINDOW_EDGE_BOTTOM) - height = wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; + if (window->resize.edges & SWC_WINDOW_EDGE_TOP) + height -= wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; + else if (window->resize.edges & SWC_WINDOW_EDGE_BOTTOM) + height = wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; - window->impl->configure(window, width, height); + window->impl->configure(window, width, height); - return true; + return true; } -static bool handle_button(struct pointer_handler * handler, uint32_t time, - struct button * button, uint32_t state) +static bool +handle_button(struct pointer_handler *handler, uint32_t time, + struct button *button, uint32_t state) { - struct window_pointer_interaction * interaction - = wl_container_of(handler, interaction, handler); + struct window_pointer_interaction *interaction = wl_container_of(handler, interaction, handler); - if (state != WL_POINTER_BUTTON_STATE_RELEASED - || !interaction->original_handler) - { - return false; - } + if (state != WL_POINTER_BUTTON_STATE_RELEASED + || !interaction->original_handler) { + return false; + } - end_interaction(interaction, button); - return true; + end_interaction(interaction, button); + return true; } -static void handle_attach(struct view_handler * handler) +static void +handle_attach(struct view_handler *handler) { - struct window * window = wl_container_of(handler, window, view_handler); + struct window *window = wl_container_of(handler, window, view_handler); - if (window->configure.acknowledged) - flush(window); - window->configure.pending = false; + if (window->configure.acknowledged) + flush(window);