From ad6d40c7c6943b53285ba3f1ee58c4a0d2c00dcb Mon Sep 17 00:00:00 2001 From: Timidger Date: Mon, 16 Apr 2018 18:42:10 -0400 Subject: [PATCH 01/15] Moved cat out of support, removed support --- examples/{support => }/cat.c | 0 examples/{support => }/cat.h | 0 examples/meson.build | 32 +-- examples/multi-pointer.c | 1 - examples/pointer.c | 1 - examples/rotation.c | 2 +- examples/support/README | 2 - examples/support/config.c | 351 --------------------------- examples/support/config.h | 60 ----- examples/support/ini.c | 195 --------------- examples/support/ini.h | 93 ------- examples/support/shared.c | 458 ----------------------------------- examples/support/shared.h | 146 ----------- examples/tablet.c | 1 - examples/touch.c | 2 +- 15 files changed, 12 insertions(+), 1332 deletions(-) rename examples/{support => }/cat.c (100%) rename examples/{support => }/cat.h (100%) delete mode 100644 examples/support/README delete mode 100644 examples/support/config.c delete mode 100644 examples/support/config.h delete mode 100644 examples/support/ini.c delete mode 100644 examples/support/ini.h delete mode 100644 examples/support/shared.c delete mode 100644 examples/support/shared.h diff --git a/examples/support/cat.c b/examples/cat.c similarity index 100% rename from examples/support/cat.c rename to examples/cat.c diff --git a/examples/support/cat.h b/examples/cat.h similarity index 100% rename from examples/support/cat.h rename to examples/cat.h diff --git a/examples/meson.build b/examples/meson.build index 6b9a1f46..896f2bd0 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,53 +1,41 @@ -lib_shared = static_library( - 'shared', - ['support/shared.c', 'support/cat.c', 'support/ini.c', 'support/config.c'], - dependencies: wlroots, - include_directories: include_directories('support') -) - threads = dependency('threads') wayland_cursor = dependency('wayland-cursor') -executable('simple', 'simple.c', dependencies: wlroots, link_with: lib_shared) -executable('pointer', 'pointer.c', dependencies: wlroots, link_with: lib_shared) -executable('touch', 'touch.c', dependencies: wlroots, link_with: lib_shared) -executable('tablet', 'tablet.c', dependencies: wlroots, link_with: lib_shared) -executable('multi-pointer', 'multi-pointer.c', dependencies: wlroots, - link_with: lib_shared) +executable('simple', 'simple.c', dependencies: wlroots) +executable('pointer', 'pointer.c', dependencies: wlroots) +executable('touch', 'touch.c', dependencies: wlroots) +executable('tablet', 'tablet.c', dependencies: wlroots) +executable('multi-pointer', 'multi-pointer.c', dependencies: wlroots) executable( 'rotation', 'rotation.c', + 'cat.c', dependencies: wlroots, - link_with: lib_shared, ) executable( 'output-layout', 'output-layout.c', - dependencies: wlroots, - link_with: lib_shared, + dependencies: wlroots ) executable( 'screenshot', 'screenshot.c', - dependencies: [wayland_client, wlr_protos, wlroots], - link_with: lib_shared, + dependencies: [wayland_client, wlr_protos, wlroots] ) executable( 'idle', 'idle.c', - dependencies: [wayland_client, wlr_protos, wlroots, threads], - link_with: lib_shared, + dependencies: [wayland_client, wlr_protos, wlroots, threads] ) executable( 'idle-inhibit', 'idle-inhibit.c', - dependencies: [wayland_client, wlr_protos, wlroots, threads], - link_with: lib_shared, + dependencies: [wayland_client, wlr_protos, wlroots, threads] ) executable( diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index 46f0e78f..ff60e5bf 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -22,7 +22,6 @@ #include #include #include -#include "support/cat.h" #include "support/config.h" #include "support/shared.h" diff --git a/examples/pointer.c b/examples/pointer.c index aaaad841..491937c8 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -22,7 +22,6 @@ #include #include #include -#include "support/cat.h" #include "support/config.h" #include "support/shared.h" diff --git a/examples/rotation.c b/examples/rotation.c index c7bbc99a..37def808 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -19,7 +19,7 @@ #include #include "support/shared.h" #include "support/config.h" -#include "support/cat.h" +#include "cat.h" struct sample_state { struct example_config *config; diff --git a/examples/support/README b/examples/support/README deleted file mode 100644 index c32c873e..00000000 --- a/examples/support/README +++ /dev/null @@ -1,2 +0,0 @@ -Support code for the examples. Code that's not relevant to the principle each -example demonstrates is largely offloaded to this directory. diff --git a/examples/support/config.c b/examples/support/config.c deleted file mode 100644 index 319be31a..00000000 --- a/examples/support/config.c +++ /dev/null @@ -1,351 +0,0 @@ -#ifndef _POSIX_C_SOURCE -#define _POSIX_C_SOURCE 200809L -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "support/config.h" -#include "shared.h" -#include "ini.h" - -static void usage(const char *name, int ret) { - fprintf(stderr, - "usage: %s [-C ]\n" - "\n" - " -C Path to the configuration file\n" - " (default: wlr-example.ini).\n" - " See `rootston/rootston.ini.example` for config\n" - " file documentation.\n", name); - - exit(ret); -} - -static struct wlr_box *parse_geometry(const char *str) { - // format: {width}x{height}+{x}+{y} - if (strlen(str) > 255) { - wlr_log(L_ERROR, "cannot parse geometry string, too long"); - return NULL; - } - - char *buf = strdup(str); - struct wlr_box *box = calloc(1, sizeof(struct wlr_box)); - - bool has_width = false; - bool has_height = false; - bool has_x = false; - bool has_y = false; - - char *pch = strtok(buf, "x+"); - while (pch != NULL) { - errno = 0; - char *endptr; - long val = strtol(pch, &endptr, 0); - - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || - (errno != 0 && val == 0)) { - goto invalid_input; - } - - if (endptr == pch) { - goto invalid_input; - } - - if (!has_width) { - box->width = val; - has_width = true; - } else if (!has_height) { - box->height = val; - has_height = true; - } else if (!has_x) { - box->x = val; - has_x = true; - } else if (!has_y) { - box->y = val; - has_y = true; - } else { - break; - } - pch = strtok(NULL, "x+"); - } - - if (!has_width || !has_height) { - goto invalid_input; - } - - free(buf); - return box; - -invalid_input: - wlr_log(L_ERROR, "could not parse geometry string: %s", str); - free(buf); - free(box); - return NULL; -} - -static const char *output_prefix = "output:"; -static const char *device_prefix = "device:"; - -static int config_ini_handler(void *user, const char *section, const char *name, - const char *value) { - struct example_config *config = user; - if (strncmp(output_prefix, section, strlen(output_prefix)) == 0) { - const char *output_name = section + strlen(output_prefix); - struct output_config *oc; - bool found = false; - - wl_list_for_each(oc, &config->outputs, link) { - if (strcmp(oc->name, output_name) == 0) { - found = true; - break; - } - } - - if (!found) { - oc = calloc(1, sizeof(struct output_config)); - oc->name = strdup(output_name); - oc->transform = WL_OUTPUT_TRANSFORM_NORMAL; - wl_list_insert(&config->outputs, &oc->link); - } - - if (strcmp(name, "x") == 0) { - oc->x = strtol(value, NULL, 10); - } else if (strcmp(name, "y") == 0) { - oc->y = strtol(value, NULL, 10); - } else if (strcmp(name, "rotate") == 0) { - if (strcmp(value, "90") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_90; - } else if (strcmp(value, "180") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_180; - } else if (strcmp(value, "270") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_270; - } else if (strcmp(value, "flipped") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED; - } else if (strcmp(value, "flipped-90") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; - } else if (strcmp(value, "flipped-180") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; - } else if (strcmp(value, "flipped-270") == 0) { - oc->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; - } else { - wlr_log(L_ERROR, "got unknown transform value: %s", value); - } - } - } else if (strcmp(section, "cursor") == 0) { - if (strcmp(name, "map-to-output") == 0) { - free(config->cursor.mapped_output); - config->cursor.mapped_output = strdup(value); - } else if (strcmp(name, "geometry") == 0) { - free(config->cursor.mapped_box); - config->cursor.mapped_box = parse_geometry(value); - } else { - wlr_log(L_ERROR, "got unknown cursor config: %s", name); - } - } else if (strncmp(device_prefix, section, strlen(device_prefix)) == 0) { - const char *device_name = section + strlen(device_prefix); - struct device_config *dc; - bool found = false; - - wl_list_for_each(dc, &config->devices, link) { - if (strcmp(dc->name, device_name) == 0) { - found = true; - break; - } - } - - if (!found) { - dc = calloc(1, sizeof(struct device_config)); - dc->name = strdup(device_name); - wl_list_insert(&config->devices, &dc->link); - } - - if (strcmp(name, "map-to-output") == 0) { - free(dc->mapped_output); - dc->mapped_output = strdup(value); - } else if (strcmp(name, "geometry") == 0) { - free(dc->mapped_box); - dc->mapped_box = parse_geometry(value); - } else { - wlr_log(L_ERROR, "got unknown device config: %s", name); - } - } else { - wlr_log(L_ERROR, "got unknown config section: %s", section); - } - - return 1; -} - -struct example_config *parse_args(int argc, char *argv[]) { - struct example_config *config = calloc(1, sizeof(struct example_config)); - wl_list_init(&config->outputs); - wl_list_init(&config->devices); - - int c; - while ((c = getopt(argc, argv, "C:h")) != -1) { - switch (c) { - case 'C': - config->config_path = strdup(optarg); - break; - case 'h': - case '?': - usage(argv[0], c != 'h'); - } - } - - if (!config->config_path) { - // get the config path from the current directory - char cwd[MAXPATHLEN]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - char buf[MAXPATHLEN]; - if (snprintf(buf, MAXPATHLEN, "%s/%s", cwd, "wlr-example.ini") >= MAXPATHLEN) { - wlr_log(L_ERROR, "config path too long"); - exit(1); - } - config->config_path = strdup(buf); - } else { - wlr_log(L_ERROR, "could not get cwd"); - exit(1); - } - } - - int result = ini_parse(config->config_path, config_ini_handler, config); - - if (result == -1) { - wlr_log(L_DEBUG, "No config file found. Using empty config."); - } else if (result == -2) { - wlr_log(L_ERROR, "Could not allocate memory to parse config file"); - exit(1); - } else if (result != 0) { - wlr_log(L_ERROR, "Could not parse config file"); - exit(1); - } - - return config; -} - -void example_config_destroy(struct example_config *config) { - struct output_config *oc, *otmp = NULL; - wl_list_for_each_safe(oc, otmp, &config->outputs, link) { - free(oc->name); - free(oc); - } - - struct device_config *dc, *dtmp = NULL; - wl_list_for_each_safe(dc, dtmp, &config->devices, link) { - free(dc->name); - if (dc->mapped_output) { - free(dc->mapped_output); - } - if (dc->mapped_box) { - free(dc->mapped_box); - } - free(dc); - } - - if (config->config_path) { - free(config->config_path); - } - if (config->cursor.mapped_output) { - free(config->cursor.mapped_output); - } - if (config->cursor.mapped_box) { - free(config->cursor.mapped_box); - } - free(config); -} - -struct output_config *example_config_get_output(struct example_config *config, - struct wlr_output *output) { - struct output_config *o_config; - wl_list_for_each(o_config, &config->outputs, link) { - if (strcmp(o_config->name, output->name) == 0) { - return o_config; - } - } - - return NULL; -} - -struct device_config *example_config_get_device(struct example_config *config, - struct wlr_input_device *device) { - struct device_config *d_config; - wl_list_for_each(d_config, &config->devices, link) { - if (strcmp(d_config->name, device->name) == 0) { - return d_config; - } - } - - return NULL; -} - -/** - * Set device output mappings to NULL and configure region mappings. - */ -static void reset_device_mappings(struct example_config *config, - struct wlr_cursor *cursor, struct wlr_input_device *device) { - struct device_config *d_config; - wlr_cursor_map_input_to_output(cursor, device, NULL); - d_config = example_config_get_device(config, device); - if (d_config) { - wlr_cursor_map_input_to_region(cursor, device, - d_config->mapped_box); - } -} - -static void set_device_output_mappings(struct example_config *config, - struct wlr_cursor *cursor, struct wlr_output *output, - struct wlr_input_device *device) { - struct device_config *d_config; - d_config = example_config_get_device(config, device); - if (d_config && - d_config->mapped_output && - strcmp(d_config->mapped_output, output->name) == 0) { - wlr_cursor_map_input_to_output(cursor, device, output); - } -} - -void example_config_configure_cursor(struct example_config *config, - struct wlr_cursor *cursor, struct compositor_state *compositor) { - struct pointer_state *p_state; - struct tablet_tool_state *tt_state; - struct touch_state *tch_state; - struct output_state *o_state; - - // reset mappings - wlr_cursor_map_to_output(cursor, NULL); - wl_list_for_each(p_state, &compositor->pointers, link) { - reset_device_mappings(config, cursor, p_state->device); - } - wl_list_for_each(tt_state, &compositor->tablet_tools, link) { - reset_device_mappings(config, cursor, tt_state->device); - } - wl_list_for_each(tch_state, &compositor->touch, link) { - reset_device_mappings(config, cursor, tch_state->device); - } - - // configure device to output mappings - char *mapped_output = config->cursor.mapped_output; - wl_list_for_each(o_state, &compositor->outputs, link) { - if (mapped_output && strcmp(mapped_output, o_state->output->name) == 0) { - wlr_cursor_map_to_output(cursor, o_state->output); - } - - wl_list_for_each(p_state, &compositor->pointers, link) { - set_device_output_mappings(config, cursor, o_state->output, - p_state->device); - } - wl_list_for_each(tt_state, &compositor->tablet_tools, link) { - set_device_output_mappings(config, cursor, o_state->output, - tt_state->device); - } - wl_list_for_each(tch_state, &compositor->touch, link) { - set_device_output_mappings(config, cursor, o_state->output, - tch_state->device); - } - } -} diff --git a/examples/support/config.h b/examples/support/config.h deleted file mode 100644 index e26531c2..00000000 --- a/examples/support/config.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef _EXAMPLE_CONFIG_H -#define _EXAMPLE_CONFIG_H -#ifndef _POSIX_C_SOURCE -#define _POSIX_C_SOURCE 200112L -#endif -#include -#include -#include -#include "shared.h" - -struct output_config { - char *name; - enum wl_output_transform transform; - int x, y; - struct wl_list link; -}; - -struct device_config { - char *name; - char *mapped_output; - struct wlr_box *mapped_box; - struct wl_list link; -}; - -struct example_config { - struct { - char *mapped_output; - struct wlr_box *mapped_box; - } cursor; - - struct wl_list outputs; - struct wl_list devices; - char *config_path; -}; - -struct example_config *parse_args(int argc, char *argv[]); - -void example_config_destroy(struct example_config *config); - -/** - * Get configuration for the output. If the output is not configured, returns - * NULL. - */ -struct output_config *example_config_get_output(struct example_config *config, - struct wlr_output *output); - -/** - * Get configuration for the device. If the device is not configured, returns - * NULL. - */ -struct device_config *example_config_get_device(struct example_config *config, - struct wlr_input_device *device); - -/** - * Configure cursor device mappings. - */ -void example_config_configure_cursor(struct example_config *config, - struct wlr_cursor *cursor, struct compositor_state *state); - -#endif diff --git a/examples/support/ini.c b/examples/support/ini.c deleted file mode 100644 index 6bc1eae6..00000000 --- a/examples/support/ini.c +++ /dev/null @@ -1,195 +0,0 @@ -/* inih -- simple .INI file parser - -inih is released under the New BSD license (see LICENSE.txt). Go to the project -home page for more info: - -https://github.com/benhoyt/inih - -*/ - -#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) -#define _CRT_SECURE_NO_WARNINGS -#endif - -#include -#include -#include - -#include "ini.h" - -#if !INI_USE_STACK -#include -#endif - -#define MAX_SECTION 50 -#define MAX_NAME 50 - -/* Strip whitespace chars off end of given string, in place. Return s. */ -static char* rstrip(char* s) -{ - char* p = s + strlen(s); - while (p > s && isspace((unsigned char)(*--p))) - *p = '\0'; - return s; -} - -/* Return pointer to first non-whitespace char in given string. */ -static char* lskip(const char* s) -{ - while (*s && isspace((unsigned char)(*s))) - s++; - return (char*)s; -} - -/* Return pointer to first char (of chars) or inline comment in given string, - or pointer to null at end of string if neither found. Inline comment must - be prefixed by a whitespace character to register as a comment. */ -static char* find_chars_or_comment(const char* s, const char* chars) -{ -#if INI_ALLOW_INLINE_COMMENTS - int was_space = 0; - while (*s && (!chars || !strchr(chars, *s)) && - !(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s))) { - was_space = isspace((unsigned char)(*s)); - s++; - } -#else - while (*s && (!chars || !strchr(chars, *s))) { - s++; - } -#endif - return (char*)s; -} - -/* Version of strncpy that ensures dest (size bytes) is null-terminated. */ -static char* strncpy0(char* dest, const char* src, size_t size) -{ - strncpy(dest, src, size-1); - dest[size - 1] = '\0'; - return dest; -} - -/* See documentation in header file. */ -int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, - void* user) -{ - /* Uses a fair bit of stack (use heap instead if you need to) */ -#if INI_USE_STACK - char line[INI_MAX_LINE]; -#else - char* line; -#endif - char section[MAX_SECTION] = ""; - char prev_name[MAX_NAME] = ""; - - char* start; - char* end; - char* name; - char* value; - int lineno = 0; - int error = 0; - -#if !INI_USE_STACK - line = (char*)malloc(INI_MAX_LINE); - if (!line) { - return -2; - } -#endif - - /* Scan through stream line by line */ - while (reader(line, INI_MAX_LINE, stream) != NULL) { - lineno++; - - start = line; -#if INI_ALLOW_BOM - if (lineno == 1 && (unsigned char)start[0] == 0xEF && - (unsigned char)start[1] == 0xBB && - (unsigned char)start[2] == 0xBF) { - start += 3; - } -#endif - start = lskip(rstrip(start)); - - if (*start == ';' || *start == '#') { - /* Per Python configparser, allow both ; and # comments at the - start of a line */ - } -#if INI_ALLOW_MULTILINE - else if (*prev_name && *start && start > line) { - /* Non-blank line with leading whitespace, treat as continuation - of previous name's value (as per Python configparser). */ - if (!handler(user, section, prev_name, start) && !error) - error = lineno; - } -#endif - else if (*start == '[') { - /* A "[section]" line */ - end = find_chars_or_comment(start + 1, "]"); - if (*end == ']') { - *end = '\0'; - strncpy0(section, start + 1, sizeof(section)); - *prev_name = '\0'; - } - else if (!error) { - /* No ']' found on section line */ - error = lineno; - } - } - else if (*start) { - /* Not a comment, must be a name[=:]value pair */ - end = find_chars_or_comment(start, "=:"); - if (*end == '=' || *end == ':') { - *end = '\0'; - name = rstrip(start); - value = lskip(end + 1); -#if INI_ALLOW_INLINE_COMMENTS - end = find_chars_or_comment(value, NULL); - if (*end) - *end = '\0'; -#endif - rstrip(value); - - /* Valid name[=:]value pair found, call handler */ - strncpy0(prev_name, name, sizeof(prev_name)); - if (!handler(user, section, name, value) && !error) - error = lineno; - memset(value, 0, strlen(value)); - } - else if (!error) { - /* No '=' or ':' found on name[=:]value line */ - error = lineno; - } - } - -#if INI_STOP_ON_FIRST_ERROR - if (error) - break; -#endif - } - -#if !INI_USE_STACK - free(line); -#endif - - return error; -} - -/* See documentation in header file. */ -int ini_parse_file(FILE* file, ini_handler handler, void* user) -{ - return ini_parse_stream((ini_reader)fgets, file, handler, user); -} - -/* See documentation in header file. */ -int ini_parse(const char* filename, ini_handler handler, void* user) -{ - FILE* file; - int error; - - file = fopen(filename, "r"); - if (!file) - return -1; - error = ini_parse_file(file, handler, user); - fclose(file); - return error; -} diff --git a/examples/support/ini.h b/examples/support/ini.h deleted file mode 100644 index 2804255b..00000000 --- a/examples/support/ini.h +++ /dev/null @@ -1,93 +0,0 @@ -/* inih -- simple .INI file parser - -inih is released under the New BSD license (see LICENSE.txt). Go to the project -home page for more info: - -https://github.com/benhoyt/inih - -*/ - -#ifndef __INI_H__ -#define __INI_H__ - -/* Make this header file easier to include in C++ code */ -#ifdef __cplusplus -extern "C" { -#endif - -#include - -/* Typedef for prototype of handler function. */ -typedef int (*ini_handler)(void* user, const char* section, - const char* name, const char* value); - -/* Typedef for prototype of fgets-style reader function. */ -typedef char* (*ini_reader)(char* str, int num, void* stream); - -/* Parse given INI-style file. May have [section]s, name=value pairs - (whitespace stripped), and comments starting with ';' (semicolon). Section - is "" if name=value pair parsed before any section heading. name:value - pairs are also supported as a concession to Python's configparser. - - For each name=value pair parsed, call handler function with given user - pointer as well as section, name, and value (data only valid for duration - of handler call). Handler should return nonzero on success, zero on error. - - Returns 0 on success, line number of first error on parse error (doesn't - stop on first error), -1 on file open error, or -2 on memory allocation - error (only when INI_USE_STACK is zero). -*/ -int ini_parse(const char* filename, ini_handler handler, void* user); - -/* Same as ini_parse(), but takes a FILE* instead of filename. This doesn't - close the file when it's finished -- the caller must do that. */ -int ini_parse_file(FILE* file, ini_handler handler, void* user); - -/* Same as ini_parse(), but takes an ini_reader function pointer instead of - filename. Used for implementing custom or string-based I/O. */ -int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler, - void* user); - -/* Nonzero to allow multi-line value parsing, in the style of Python's - configparser. If allowed, ini_parse() will call the handler with the same - name for each subsequent line parsed. */ -#ifndef INI_ALLOW_MULTILINE -#define INI_ALLOW_MULTILINE 1 -#endif - -/* Nonzero to allow a UTF-8 BOM sequence (0xEF 0xBB 0xBF) at the start of - the file. See http://code.google.com/p/inih/issues/detail?id=21 */ -#ifndef INI_ALLOW_BOM -#define INI_ALLOW_BOM 1 -#endif - -/* Nonzero to allow inline comments (with valid inline comment characters - specified by INI_INLINE_COMMENT_PREFIXES). Set to 0 to turn off and match - Python 3.2+ configparser behaviour. */ -#ifndef INI_ALLOW_INLINE_COMMENTS -#define INI_ALLOW_INLINE_COMMENTS 1 -#endif -#ifndef INI_INLINE_COMMENT_PREFIXES -#define INI_INLINE_COMMENT_PREFIXES ";" -#endif - -/* Nonzero to use stack, zero to use heap (malloc/free). */ -#ifndef INI_USE_STACK -#define INI_USE_STACK 1 -#endif - -/* Stop parsing on first error (default is to keep parsing). */ -#ifndef INI_STOP_ON_FIRST_ERROR -#define INI_STOP_ON_FIRST_ERROR 0 -#endif - -/* Maximum line length for any line in INI file. */ -#ifndef INI_MAX_LINE -#define INI_MAX_LINE 2000 -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* __INI_H__ */ diff --git a/examples/support/shared.c b/examples/support/shared.c deleted file mode 100644 index 02b324f5..00000000 --- a/examples/support/shared.c +++ /dev/null @@ -1,458 +0,0 @@ -#define _POSIX_C_SOURCE 200112L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "shared.h" - -static void keyboard_key_notify(struct wl_listener *listener, void *data) { - struct wlr_event_keyboard_key *event = data; - struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key); - uint32_t keycode = event->keycode + 8; - enum wlr_key_state key_state = event->state; - const xkb_keysym_t *syms; - int nsyms = xkb_state_key_get_syms(kbstate->device->keyboard->xkb_state, - keycode, &syms); - - for (int i = 0; i < nsyms; ++i) { - xkb_keysym_t sym = syms[i]; - char name[64]; - int l = xkb_keysym_get_name(sym, name, sizeof(name)); - if (l != -1 && l != sizeof(name)) { - wlr_log(L_DEBUG, "Key event: %s %s", name, - key_state == WLR_KEY_PRESSED ? "pressed" : "released"); - } - if (kbstate->compositor->keyboard_key_cb) { - kbstate->compositor->keyboard_key_cb(kbstate, event->keycode, sym, - key_state, event->time_msec * 1000); - } - if (sym == XKB_KEY_Escape) { - wl_display_terminate(kbstate->compositor->display); - } else if (key_state == WLR_KEY_PRESSED && - sym >= XKB_KEY_XF86Switch_VT_1 && - sym <= XKB_KEY_XF86Switch_VT_12) { - if (wlr_backend_is_multi(kbstate->compositor->backend)) { - struct wlr_session *session = - wlr_multi_get_session(kbstate->compositor->backend); - if (session) { - wlr_session_change_vt(session, sym - XKB_KEY_XF86Switch_VT_1 + 1); - } - } - } - } -} - -static void keyboard_destroy_notify(struct wl_listener *listener, void *data) { - struct keyboard_state *kbstate = wl_container_of(listener, kbstate, destroy); - struct compositor_state *state = kbstate->compositor; - if (state->input_remove_cb) { - state->input_remove_cb(state, kbstate->device); - } - wl_list_remove(&kbstate->link); - wl_list_remove(&kbstate->destroy.link); - wl_list_remove(&kbstate->key.link); - free(kbstate); -} - -static void keyboard_add(struct wlr_input_device *device, struct compositor_state *state) { - struct keyboard_state *kbstate = calloc(sizeof(struct keyboard_state), 1); - kbstate->device = device; - kbstate->compositor = state; - kbstate->destroy.notify = keyboard_destroy_notify; - wl_signal_add(&device->events.destroy, &kbstate->destroy); - kbstate->key.notify = keyboard_key_notify; - wl_signal_add(&device->keyboard->events.key, &kbstate->key); - wl_list_insert(&state->keyboards, &kbstate->link); - - struct xkb_rule_names rules; - memset(&rules, 0, sizeof(rules)); - rules.rules = getenv("XKB_DEFAULT_RULES"); - rules.model = getenv("XKB_DEFAULT_MODEL"); - rules.layout = getenv("XKB_DEFAULT_LAYOUT"); - rules.variant = getenv("XKB_DEFAULT_VARIANT"); - rules.options = getenv("XKB_DEFAULT_OPTIONS"); - struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - if (!context) { - wlr_log(L_ERROR, "Failed to create XKB context"); - exit(1); - } - wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, - &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); - xkb_context_unref(context); -} - -static void pointer_motion_notify(struct wl_listener *listener, void *data) { - struct wlr_event_pointer_motion *event = data; - struct pointer_state *pstate = wl_container_of(listener, pstate, motion); - if (pstate->compositor->pointer_motion_cb) { - pstate->compositor->pointer_motion_cb(pstate, - event->delta_x, event->delta_y); - } -} - -static void pointer_motion_absolute_notify(struct wl_listener *listener, void *data) { - struct wlr_event_pointer_motion_absolute *event = data; - struct pointer_state *pstate = wl_container_of(listener, pstate, motion_absolute); - if (pstate->compositor->pointer_motion_absolute_cb) { - pstate->compositor->pointer_motion_absolute_cb( - pstate, event->x, event->y); - } -} - -static void pointer_button_notify(struct wl_listener *listener, void *data) { - struct wlr_event_pointer_button *event = data; - struct pointer_state *pstate = wl_container_of(listener, pstate, button); - if (pstate->compositor->pointer_button_cb) { - pstate->compositor->pointer_button_cb(pstate, - event->button, event->state); - } -} - -static void pointer_axis_notify(struct wl_listener *listener, void *data) { - struct wlr_event_pointer_axis *event = data; - struct pointer_state *pstate = wl_container_of(listener, pstate, axis); - if (pstate->compositor->pointer_axis_cb) { - pstate->compositor->pointer_axis_cb(pstate, - event->source, event->orientation, event->delta); - } -} - -static void pointer_destroy_notify(struct wl_listener *listener, void *data) { - struct pointer_state *pstate = wl_container_of(listener, pstate, destroy); - struct compositor_state *state = pstate->compositor; - if (state->input_remove_cb) { - state->input_remove_cb(state, pstate->device); - } - wl_list_remove(&pstate->link); - wl_list_remove(&pstate->destroy.link); - wl_list_remove(&pstate->motion.link); - wl_list_remove(&pstate->motion_absolute.link); - wl_list_remove(&pstate->button.link); - wl_list_remove(&pstate->axis.link); - free(pstate); -} - -static void pointer_add(struct wlr_input_device *device, struct compositor_state *state) { - struct pointer_state *pstate = calloc(sizeof(struct pointer_state), 1); - pstate->device = device; - pstate->compositor = state; - pstate->destroy.notify = pointer_destroy_notify; - wl_signal_add(&device->events.destroy, &pstate->destroy); - pstate->motion.notify = pointer_motion_notify; - wl_signal_add(&device->pointer->events.motion, &pstate->motion); - pstate->motion_absolute.notify = pointer_motion_absolute_notify; - wl_signal_add(&device->pointer->events.motion_absolute, &pstate->motion_absolute); - pstate->button.notify = pointer_button_notify; - wl_signal_add(&device->pointer->events.button, &pstate->button); - pstate->axis.notify = pointer_axis_notify; - wl_signal_add(&device->pointer->events.axis, &pstate->axis); - wl_list_insert(&state->pointers, &pstate->link); -} - -static void touch_down_notify(struct wl_listener *listener, void *data) { - struct wlr_event_touch_down *event = data; - struct touch_state *tstate = wl_container_of(listener, tstate, down); - if (tstate->compositor->touch_down_cb) { - tstate->compositor->touch_down_cb(tstate, - event->touch_id, event->x, event->y); - } -} - -static void touch_motion_notify(struct wl_listener *listener, void *data) { - struct wlr_event_touch_motion *event = data; - struct touch_state *tstate = wl_container_of(listener, tstate, motion); - if (tstate->compositor->touch_motion_cb) { - tstate->compositor->touch_motion_cb(tstate, - event->touch_id, event->x, event->y); - } -} - -static void touch_up_notify(struct wl_listener *listener, void *data) { - struct wlr_event_touch_up *event = data; - struct touch_state *tstate = wl_container_of(listener, tstate, up); - if (tstate->compositor->touch_up_cb) { - tstate->compositor->touch_up_cb(tstate, event->touch_id); - } -} - -static void touch_cancel_notify(struct wl_listener *listener, void *data) { - struct wlr_event_touch_cancel *event = data; - struct touch_state *tstate = wl_container_of(listener, tstate, cancel); - if (tstate->compositor->touch_cancel_cb) { - tstate->compositor->touch_cancel_cb(tstate, event->touch_id); - } -} - -static void touch_destroy_notify(struct wl_listener *listener, void *data) { - struct touch_state *tstate = wl_container_of(listener, tstate, destroy); - struct compositor_state *state = tstate->compositor; - if (state->input_remove_cb) { - state->input_remove_cb(state, tstate->device); - } - wl_list_remove(&tstate->link); - wl_list_remove(&tstate->destroy.link); - wl_list_remove(&tstate->down.link); - wl_list_remove(&tstate->motion.link); - wl_list_remove(&tstate->up.link); - wl_list_remove(&tstate->cancel.link); - free(tstate); -} - -static void touch_add(struct wlr_input_device *device, struct compositor_state *state) { - struct touch_state *tstate = calloc(sizeof(struct touch_state), 1); - tstate->device = device; - tstate->compositor = state; - tstate->destroy.notify = touch_destroy_notify; - wl_signal_add(&device->events.destroy, &tstate->destroy); - tstate->down.notify = touch_down_notify; - wl_signal_add(&device->touch->events.down, &tstate->down); - tstate->motion.notify = touch_motion_notify; - wl_signal_add(&device->touch->events.motion, &tstate->motion); - tstate->up.notify = touch_up_notify; - wl_signal_add(&device->touch->events.up, &tstate->up); - tstate->cancel.notify = touch_cancel_notify; - wl_signal_add(&device->touch->events.cancel, &tstate->cancel); - wl_list_insert(&state->touch, &tstate->link); -} - -static void tablet_tool_axis_notify(struct wl_listener *listener, void *data) { - struct wlr_event_tablet_tool_axis *event = data; - struct tablet_tool_state *tstate = wl_container_of(listener, tstate, axis); - if (tstate->compositor->tool_axis_cb) { - tstate->compositor->tool_axis_cb(tstate, event); - } -} - -static void tablet_tool_proximity_notify(struct wl_listener *listener, void *data) { - struct wlr_event_tablet_tool_proximity *event = data; - struct tablet_tool_state *tstate = wl_container_of(listener, tstate, proximity); - if (tstate->compositor->tool_proximity_cb) { - tstate->compositor->tool_proximity_cb(tstate, event->state); - } -} - -static void tablet_tool_button_notify(struct wl_listener *listener, void *data) { - struct wlr_event_tablet_tool_button *event = data; - struct tablet_tool_state *tstate = wl_container_of(listener, tstate, button); - if (tstate->compositor->tool_button_cb) { - tstate->compositor->tool_button_cb(tstate, event->button, event->state); - } -} - -static void tablet_tool_destroy_notify(struct wl_listener *listener, void *data) { - struct tablet_tool_state *tstate = wl_container_of(listener, tstate, destroy); - struct compositor_state *state = tstate->compositor; - if (state->input_remove_cb) { - state->input_remove_cb(state, tstate->device); - } - wl_list_remove(&tstate->link); - wl_list_remove(&tstate->destroy.link); - wl_list_remove(&tstate->axis.link); - wl_list_remove(&tstate->proximity.link); - //wl_list_remove(&tstate->tip.link); - wl_list_remove(&tstate->button.link); - free(tstate); -} - -static void tablet_tool_add(struct wlr_input_device *device, - struct compositor_state *state) { - struct tablet_tool_state *tstate = calloc(sizeof(struct tablet_tool_state), 1); - tstate->device = device; - tstate->compositor = state; - tstate->destroy.notify = tablet_tool_destroy_notify; - wl_signal_add(&device->events.destroy, &tstate->destroy); - tstate->axis.notify = tablet_tool_axis_notify; - wl_signal_add(&device->tablet_tool->events.axis, &tstate->axis); - tstate->proximity.notify = tablet_tool_proximity_notify; - wl_signal_add(&device->tablet_tool->events.proximity, &tstate->proximity); - //tstate->tip.notify = tablet_tool_tip_notify; - //wl_signal_add(&device->tablet_tool->events.tip, &tstate->tip); - tstate->button.notify = tablet_tool_button_notify; - wl_signal_add(&device->tablet_tool->events.button, &tstate->button); - wl_list_insert(&state->tablet_tools, &tstate->link); -} - -static void tablet_pad_button_notify(struct wl_listener *listener, void *data) { - struct wlr_event_tablet_pad_button *event = data; - struct tablet_pad_state *pstate = wl_container_of(listener, pstate, button); - if (pstate->compositor->pad_button_cb) { - pstate->compositor->pad_button_cb(pstate, event->button, event->state); - } -} - -static void tablet_pad_ring_notify(struct wl_listener *listener, void *data) { - struct wlr_event_tablet_pad_ring *event = data; - struct tablet_pad_state *pstate = wl_container_of(listener, pstate, ring); - if (pstate->compositor->pad_ring_cb) { - pstate->compositor->pad_ring_cb(pstate, event->ring, event->position); - } -} - -static void tablet_pad_destroy_notify(struct wl_listener *listener, void *data) { - struct tablet_pad_state *pstate = wl_container_of(listener, pstate, destroy); - struct compositor_state *state = pstate->compositor; - if (state->input_remove_cb) { - state->input_remove_cb(state, pstate->device); - } - wl_list_remove(&pstate->link); - wl_list_remove(&pstate->destroy.link); - wl_list_remove(&pstate->button.link); - free(pstate); -} - -static void tablet_pad_add(struct wlr_input_device *device, - struct compositor_state *state) { - struct tablet_pad_state *pstate = calloc(sizeof(struct tablet_pad_state), 1); - pstate->device = device; - pstate->compositor = state; - pstate->destroy.notify = tablet_pad_destroy_notify; - wl_signal_add(&device->events.destroy, &pstate->destroy); - pstate->button.notify = tablet_pad_button_notify; - wl_signal_add(&device->tablet_pad->events.button, &pstate->button); - pstate->ring.notify = tablet_pad_ring_notify; - wl_signal_add(&device->tablet_pad->events.ring, &pstate->ring); - wl_list_insert(&state->tablet_pads, &pstate->link); -} - -static void new_input_notify(struct wl_listener *listener, void *data) { - struct wlr_input_device *device = data; - struct compositor_state *state = wl_container_of(listener, state, new_input); - switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard_add(device, state); - break; - case WLR_INPUT_DEVICE_POINTER: - pointer_add(device, state); - break; - case WLR_INPUT_DEVICE_TOUCH: - touch_add(device, state); - break; - case WLR_INPUT_DEVICE_TABLET_TOOL: - tablet_tool_add(device, state); - break; - case WLR_INPUT_DEVICE_TABLET_PAD: - tablet_pad_add(device, state); - break; - default: - break; - } - - if (state->input_add_cb) { - state->input_add_cb(state, device); - } -} - -static void output_frame_notify(struct wl_listener *listener, void *data) { - struct output_state *output = wl_container_of(listener, output, frame); - struct compositor_state *compositor = output->compositor; - - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - if (compositor->output_frame_cb) { - compositor->output_frame_cb(output, &now); - } - - output->last_frame = now; - compositor->last_frame = now; -} - -static void output_resolution_notify(struct wl_listener *listener, void *data) { - struct output_state *output = wl_container_of(listener, output, resolution); - struct compositor_state *compositor = output->compositor; - - if (compositor->output_resolution_cb) { - compositor->output_resolution_cb(compositor, output); - } -} - -static void output_destroy_notify(struct wl_listener *listener, void *data) { - struct output_state *ostate = wl_container_of(listener, ostate, destroy); - struct compositor_state *state = ostate->compositor; - if (state->output_remove_cb) { - state->output_remove_cb(ostate); - } - wl_list_remove(&ostate->link); - wl_list_remove(&ostate->frame.link); - wl_list_remove(&ostate->resolution.link); - free(ostate); -} - -static void new_output_notify(struct wl_listener *listener, void *data) { - struct wlr_output *output = data; - struct compositor_state *state = wl_container_of(listener, state, new_output); - wlr_log(L_DEBUG, "Output '%s' added", output->name); - wlr_log(L_DEBUG, "%s %s %"PRId32"mm x %"PRId32"mm", output->make, output->model, - output->phys_width, output->phys_height); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode; - mode = wl_container_of((&output->modes)->prev, mode, link); - wlr_output_set_mode(output, mode); - } - struct output_state *ostate = calloc(1, sizeof(struct output_state)); - clock_gettime(CLOCK_MONOTONIC, &ostate->last_frame); - ostate->output = output; - ostate->compositor = state; - ostate->destroy.notify = output_destroy_notify; - wl_signal_add(&output->events.destroy, &ostate->destroy); - ostate->frame.notify = output_frame_notify; - wl_signal_add(&output->events.frame, &ostate->frame); - ostate->resolution.notify = output_resolution_notify; - wl_signal_add(&output->events.mode, &ostate->resolution); - wl_list_insert(&state->outputs, &ostate->link); - if (state->output_add_cb) { - state->output_add_cb(ostate); - } -} - -void compositor_init(struct compositor_state *state) { - state->display = wl_display_create(); - state->event_loop = wl_display_get_event_loop(state->display); - - wl_list_init(&state->keyboards); - wl_list_init(&state->pointers); - wl_list_init(&state->touch); - wl_list_init(&state->tablet_tools); - wl_list_init(&state->tablet_pads); - wl_list_init(&state->outputs); - - struct wlr_backend *wlr = wlr_backend_autocreate(state->display); - if (!wlr) { - exit(1); - } - state->backend = wlr; - - wl_signal_add(&wlr->events.new_input, &state->new_input); - state->new_input.notify = new_input_notify; - wl_signal_add(&wlr->events.new_output, &state->new_output); - state->new_output.notify = new_output_notify; - - clock_gettime(CLOCK_MONOTONIC, &state->last_frame); - - const char *socket = wl_display_add_socket_auto(state->display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - - wlr_log(L_INFO, "Running compositor on wayland display '%s'", socket); - setenv("_WAYLAND_DISPLAY", socket, true); -} - -void compositor_fini(struct compositor_state *state) { - wl_display_destroy(state->display); -} diff --git a/examples/support/shared.h b/examples/support/shared.h deleted file mode 100644 index 2079a9ef..00000000 --- a/examples/support/shared.h +++ /dev/null @@ -1,146 +0,0 @@ -#ifndef _EXAMPLE_SHARED_H -#define _EXAMPLE_SHARED_H -#ifndef _POSIX_C_SOURCE -#define _POSIX_C_SOURCE 200112L -#endif -#include -#include -#include -#include -#include -#include -#include -#include - -struct output_state { - struct compositor_state *compositor; - struct wlr_output *output; - struct wl_listener destroy; - struct wl_listener frame; - struct wl_listener resolution; - struct timespec last_frame; - struct wl_list link; - void *data; -}; - -struct keyboard_state { - struct compositor_state *compositor; - struct wlr_input_device *device; - struct wl_listener destroy; - struct wl_listener key; - struct wl_list link; - void *data; -}; - -struct pointer_state { - struct compositor_state *compositor; - struct wlr_input_device *device; - struct wl_listener destroy; - struct wl_listener motion; - struct wl_listener motion_absolute; - struct wl_listener button; - struct wl_listener axis; - struct wl_list link; - void *data; -}; - -struct touch_state { - struct compositor_state *compositor; - struct wlr_input_device *device; - struct wl_listener destroy; - struct wl_listener down; - struct wl_listener up; - struct wl_listener motion; - struct wl_listener cancel; - struct wl_list link; - void *data; -}; - -struct tablet_tool_state { - struct compositor_state *compositor; - struct wlr_input_device *device; - struct wl_listener destroy; - struct wl_listener axis; - struct wl_listener proximity; - struct wl_listener tip; - struct wl_listener button; - struct wl_list link; - void *data; -}; - -struct tablet_pad_state { - struct compositor_state *compositor; - struct wlr_input_device *device; - struct wl_listener destroy; - struct wl_listener button; - struct wl_listener ring; - struct wl_list link; - void *data; -}; - -struct compositor_state { - void (*input_add_cb)(struct compositor_state *compositor, - struct wlr_input_device *device); - void (*input_remove_cb)(struct compositor_state *compositor, - struct wlr_input_device *device); - void (*output_add_cb)(struct output_state *s); - void (*keyboard_add_cb)(struct keyboard_state *s); - void (*output_frame_cb)(struct output_state *s, struct timespec *ts); - void (*output_remove_cb)(struct output_state *s); - void (*output_resolution_cb)(struct compositor_state *compositor, - struct output_state *s); - void (*keyboard_remove_cb)(struct keyboard_state *s); - void (*keyboard_key_cb)(struct keyboard_state *s, uint32_t keycode, - xkb_keysym_t sym, enum wlr_key_state key_state, uint64_t time_usec); - void (*pointer_motion_cb)(struct pointer_state *s, - double d_x, double d_y); - void (*pointer_motion_absolute_cb)(struct pointer_state *s, - double x, double y); - void (*pointer_button_cb)(struct pointer_state *s, - uint32_t button, enum wlr_button_state state); - void (*pointer_axis_cb)(struct pointer_state *s, - enum wlr_axis_source source, - enum wlr_axis_orientation orientation, - double delta); - void (*touch_down_cb)(struct touch_state *s, - int32_t touch_id, double x, double y); - void (*touch_motion_cb)(struct touch_state *s, - int32_t touch_id, double x, double y); - void (*touch_up_cb)(struct touch_state *s, int32_t touch_id); - void (*touch_cancel_cb)(struct touch_state *s, int32_t touch_id); - void (*tool_axis_cb)(struct tablet_tool_state *s, - struct wlr_event_tablet_tool_axis *event); - void (*tool_proximity_cb)(struct tablet_tool_state *s, - enum wlr_tablet_tool_proximity_state proximity); - void (*tool_tip_cb)(struct tablet_tool_state *s, - enum wlr_tablet_tool_tip_state state); - void (*tool_button_cb)(struct tablet_tool_state *s, - uint32_t button, enum wlr_button_state state); - void (*pad_button_cb)(struct tablet_pad_state *s, - uint32_t button, enum wlr_button_state state); - void (*pad_ring_cb)(struct tablet_pad_state *s, - uint32_t ring, double position); - - struct wl_display *display; - struct wl_event_loop *event_loop; - struct wlr_backend *backend; - struct wlr_session *session; - - struct wl_list keyboards; - struct wl_list pointers; - struct wl_list touch; - struct wl_list tablet_tools; - struct wl_list tablet_pads; - struct wl_listener new_input; - - struct timespec last_frame; - struct wl_listener new_output; - struct wl_list outputs; - - void *data; -}; - -void compositor_init(struct compositor_state *state); -void compositor_fini(struct compositor_state *state); - -#endif diff --git a/examples/tablet.c b/examples/tablet.c index ca9d782c..e8f90071 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -19,7 +19,6 @@ #include #include #include -#include "support/cat.h" #include "support/shared.h" struct sample_state { diff --git a/examples/touch.c b/examples/touch.c index 949d209d..472ad41c 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -17,7 +17,7 @@ #include #include #include -#include "support/cat.h" +#include "cat.h" #include "support/shared.h" struct sample_state { From b98779a6747e55d7997ab29f26113785421dd817 Mon Sep 17 00:00:00 2001 From: Timidger Date: Mon, 16 Apr 2018 19:55:44 -0400 Subject: [PATCH 02/15] Got simple working without keyboard input --- examples/simple.c | 79 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/examples/simple.c b/examples/simple.c index 79db4a0c..8303e2c7 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #include #include #include @@ -11,19 +11,30 @@ #include #include #include -#include "support/shared.h" struct sample_state { + struct wl_listener new_output; + struct timespec last_frame; float color[3]; int dec; }; -void handle_output_frame(struct output_state *output, struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; +}; - long ms = (ts->tv_sec - state->last_frame.tv_sec) * 1000 + - (ts->tv_nsec - state->last_frame.tv_nsec) / 1000000; + +void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); + struct sample_state *sample = sample_output->sample; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + long ms = (now.tv_sec - sample->last_frame.tv_sec) * 1000 + + (now.tv_nsec - sample->last_frame.tv_nsec) / 1000000; int inc = (sample->dec + 1) % 3; sample->color[inc] += ms / 2000.0f; @@ -35,12 +46,31 @@ void handle_output_frame(struct output_state *output, struct timespec *ts) { sample->dec = inc; } - wlr_output_make_current(output->output, NULL); + wlr_output_make_current(sample_output->output, NULL); glClearColor(sample->color[0], sample->color[1], sample->color[2], 1.0); glClear(GL_COLOR_BUFFER_BIT); - wlr_output_swap_buffers(output->output, NULL, NULL); + wlr_output_swap_buffers(sample_output->output, NULL, NULL); + sample->last_frame = now; +} + +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + wl_list_remove(&sample_output->frame.link); + free(sample_output); +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; } int main() { @@ -48,17 +78,28 @@ int main() { struct sample_state state = { .color = { 1.0, 0.0, 0.0 }, .dec = 0, + .last_frame = { 0 } }; - struct compositor_state compositor = { 0, - .data = &state, - .output_frame_cb = handle_output_frame, - }; - compositor_init(&compositor); - if (!wlr_backend_start(compositor.backend)) { - wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + struct wl_display *display = wl_display_create(); + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { exit(1); } - wl_display_run(compositor.display); - compositor_fini(&compositor); + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + clock_gettime(CLOCK_MONOTONIC, &state.last_frame); + const char *socket = wl_display_add_socket_auto(display); + if (!socket) { + wlr_log_errno(L_ERROR, "Unable to open wayland socket"); + wlr_backend_destroy(wlr); + exit(1); + } + setenv("_WAYLAND_DISPLAY", socket, true); + if (!wlr_backend_start(wlr)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(wlr); + exit(1); + } + wl_display_run(display); + wl_display_destroy(display); } From 44b2bf18f0918bb44032daee0b42765d4bd2581f Mon Sep 17 00:00:00 2001 From: Timidger Date: Mon, 16 Apr 2018 20:13:02 -0400 Subject: [PATCH 03/15] Can now quit the simple example --- examples/simple.c | 77 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/examples/simple.c b/examples/simple.c index 8303e2c7..801dd9d7 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -9,11 +9,14 @@ #include #include #include +#include #include #include struct sample_state { + struct wl_display *display; struct wl_listener new_output; + struct wl_listener new_input; struct timespec last_frame; float color[3]; int dec; @@ -26,6 +29,13 @@ struct sample_output { struct wl_listener destroy; }; +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + void output_frame_notify(struct wl_listener *listener, void *data) { struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); @@ -58,6 +68,7 @@ void output_frame_notify(struct wl_listener *listener, void *data) { void output_remove_notify(struct wl_listener *listener, void *data) { struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); free(sample_output); } @@ -73,20 +84,82 @@ void new_output_notify(struct wl_listener *listener, void *data) { sample_output->destroy.notify = output_remove_notify; } +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } + } + // TODO keyboard input +} + +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + default: + break; + } +} + int main() { wlr_log_init(L_DEBUG, NULL); + struct wl_display *display = wl_display_create(); struct sample_state state = { .color = { 1.0, 0.0, 0.0 }, .dec = 0, - .last_frame = { 0 } + .last_frame = { 0 }, + .display = display }; - struct wl_display *display = wl_display_create(); struct wlr_backend *wlr = wlr_backend_autocreate(display); if (!wlr) { exit(1); } wl_signal_add(&wlr->events.new_output, &state.new_output); state.new_output.notify = new_output_notify; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; clock_gettime(CLOCK_MONOTONIC, &state.last_frame); const char *socket = wl_display_add_socket_auto(display); if (!socket) { From 72847093706fc4ca7689870e07b6098dddc8afa3 Mon Sep 17 00:00:00 2001 From: Timidger Date: Tue, 17 Apr 2018 20:11:24 -0400 Subject: [PATCH 04/15] simple now works on DRM --- examples/simple.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/simple.c b/examples/simple.c index 801dd9d7..506f582e 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -38,6 +38,7 @@ struct sample_keyboard { void output_frame_notify(struct wl_listener *listener, void *data) { + wlr_log(L_DEBUG, "Output removed"); struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); struct sample_state *sample = sample_output->sample; struct timespec now; @@ -76,6 +77,10 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } sample_output->output = output; sample_output->sample = sample; wl_signal_add(&output->events.frame, &sample_output->frame); From 4c1d74ef9129e74cf8cb2fd753b3c01f43bc4287 Mon Sep 17 00:00:00 2001 From: Timidger Date: Fri, 20 Apr 2018 10:11:45 -0400 Subject: [PATCH 05/15] Made examples/pointer standalone --- examples/pointer.c | 220 ++++++++++++++++++++++++++++++--------------- 1 file changed, 147 insertions(+), 73 deletions(-) diff --git a/examples/pointer.c b/examples/pointer.c index 491937c8..34f6c896 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #define _XOPEN_SOURCE 500 #include #include @@ -22,12 +22,10 @@ #include #include #include -#include "support/config.h" -#include "support/shared.h" struct sample_state { + struct wl_display *display; struct compositor_state *compositor; - struct example_config *config; struct wlr_xcursor *xcursor; struct wlr_cursor *cursor; double cur_x, cur_y; @@ -35,7 +33,10 @@ struct sample_state { float clear_color[4]; struct wlr_output_layout *layout; struct wl_list devices; + struct timespec last_frame; + struct wl_listener new_output; + struct wl_listener new_input; struct wl_listener cursor_motion; struct wl_listener cursor_motion_absolute; struct wl_listener cursor_button; @@ -59,6 +60,21 @@ struct touch_point { struct wl_list link; }; +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; +}; + +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + + static void warp_to_touch(struct sample_state *sample, struct wlr_input_device *dev) { if (wl_list_empty(&sample->touch_points)) { @@ -78,11 +94,10 @@ static void warp_to_touch(struct sample_state *sample, wlr_cursor_warp_absolute(sample->cursor, dev, x, y); } -static void handle_output_frame(struct output_state *output, - struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; - struct wlr_output *wlr_output = output->output; +void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); + struct sample_state *sample = sample_output->sample; + struct wlr_output *wlr_output = sample_output->output; wlr_output_make_current(wlr_output, NULL); @@ -93,52 +108,6 @@ static void handle_output_frame(struct output_state *output, wlr_output_swap_buffers(wlr_output, NULL, NULL); } -static void handle_output_add(struct output_state *ostate) { - struct sample_state *sample = ostate->compositor->data; - - struct output_config *o_config = - example_config_get_output(sample->config, ostate->output); - - if (o_config) { - wlr_output_set_transform(ostate->output, o_config->transform); - wlr_output_layout_add(sample->layout, ostate->output, o_config->x, - o_config->y); - } else { - wlr_output_layout_add_auto(sample->layout, ostate->output); - } - - example_config_configure_cursor(sample->config, sample->cursor, - sample->compositor); - - struct wlr_xcursor_image *image = sample->xcursor->images[0]; - wlr_cursor_set_image(sample->cursor, image->buffer, image->width * 4, - image->width, image->height, image->hotspot_x, image->hotspot_y, 0); - - wlr_cursor_warp(sample->cursor, NULL, sample->cursor->x, sample->cursor->y); -} - -static void handle_output_remove(struct output_state *ostate) { - struct sample_state *sample = ostate->compositor->data; - - wlr_output_layout_remove(sample->layout, ostate->output); - - example_config_configure_cursor(sample->config, sample->cursor, - sample->compositor); -} - -static void handle_input_add(struct compositor_state *state, - struct wlr_input_device *device) { - struct sample_state *sample = state->data; - - if (device->type == WLR_INPUT_DEVICE_POINTER || - device->type == WLR_INPUT_DEVICE_TOUCH || - device->type == WLR_INPUT_DEVICE_TABLET_TOOL) { - wlr_cursor_attach_input_device(sample->cursor, device); - example_config_configure_cursor(sample->config, sample->cursor, - sample->compositor); - } -} - static void handle_cursor_motion(struct wl_listener *listener, void *data) { struct sample_state *sample = wl_container_of(listener, sample, cursor_motion); @@ -255,18 +224,120 @@ static void handle_tablet_tool_axis(struct wl_listener *listener, void *data) { } } +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } + } +} + +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + struct sample_state *sample = sample_output->sample; + wlr_output_layout_remove(sample->layout, sample_output->output); + wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); + free(sample_output); +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; + wlr_output_layout_add_auto(sample->layout, sample_output->output); + + struct wlr_xcursor_image *image = sample->xcursor->images[0]; + wlr_cursor_set_image(sample->cursor, image->buffer, image->width * 4, + image->width, image->height, image->hotspot_x, image->hotspot_y, 0); + +} + + +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + switch (device->type) { + case WLR_INPUT_DEVICE_POINTER: + case WLR_INPUT_DEVICE_TOUCH: + case WLR_INPUT_DEVICE_TABLET_TOOL: + wlr_cursor_attach_input_device(sample->cursor, device); + break; + + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + default: + break; + } +} + + int main(int argc, char *argv[]) { wlr_log_init(L_DEBUG, NULL); + struct wl_display *display = wl_display_create(); struct sample_state state = { .default_color = { 0.25f, 0.25f, 0.25f, 1 }, .clear_color = { 0.25f, 0.25f, 0.25f, 1 }, + .display = display }; - state.config = parse_args(argc, argv); + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { + exit(1); + } state.cursor = wlr_cursor_create(); state.layout = wlr_output_layout_create(); wlr_cursor_attach_output_layout(state.cursor, state.layout); - wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_box); + //wlr_cursor_map_to_region(state.cursor, state.config->cursor.mapped_box); wl_list_init(&state.devices); wl_list_init(&state.touch_points); @@ -297,20 +368,17 @@ int main(int argc, char *argv[]) { wl_signal_add(&state.cursor->events.touch_cancel, &state.touch_cancel); state.touch_cancel.notify = handle_touch_cancel; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; + + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + // tool events wl_signal_add(&state.cursor->events.tablet_tool_axis, &state.tablet_tool_axis); state.tablet_tool_axis.notify = handle_tablet_tool_axis; - struct compositor_state compositor = { 0 }; - compositor.data = &state; - compositor.output_add_cb = handle_output_add; - compositor.output_remove_cb = handle_output_remove; - compositor.output_frame_cb = handle_output_frame; - compositor.input_add_cb = handle_input_add; - - state.compositor = &compositor; - struct wlr_xcursor_theme *theme = wlr_xcursor_theme_load("default", 16); if (!theme) { wlr_log(L_ERROR, "Failed to load cursor theme"); @@ -326,17 +394,23 @@ int main(int argc, char *argv[]) { wlr_cursor_set_image(state.cursor, image->buffer, image->width * 4, image->width, image->height, image->hotspot_x, image->hotspot_y, 0); - compositor_init(&compositor); - if (!wlr_backend_start(compositor.backend)) { - wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + clock_gettime(CLOCK_MONOTONIC, &state.last_frame); + const char *socket = wl_display_add_socket_auto(display); + if (!socket) { + wlr_log_errno(L_ERROR, "Unable to open wayland socket"); + wlr_backend_destroy(wlr); exit(1); } - wl_display_run(compositor.display); - compositor_fini(&compositor); + setenv("_WAYLAND_DISPLAY", socket, true); + if (!wlr_backend_start(wlr)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(wlr); + exit(1); + } + wl_display_run(display); + wl_display_destroy(display); wlr_xcursor_theme_destroy(theme); - example_config_destroy(state.config); wlr_cursor_destroy(state.cursor); wlr_output_layout_destroy(state.layout); } From c622a0c7fbcecc1541be7f540b89bba9cff39c07 Mon Sep 17 00:00:00 2001 From: Timidger Date: Fri, 20 Apr 2018 10:37:33 -0400 Subject: [PATCH 06/15] Made touch standalone TODO test --- examples/simple.c | 1 - examples/touch.c | 235 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 198 insertions(+), 38 deletions(-) diff --git a/examples/simple.c b/examples/simple.c index 506f582e..7bf95601 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -103,7 +103,6 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { wl_display_terminate(sample->display); } } - // TODO keyboard input } void keyboard_destroy_notify(struct wl_listener *listener, void *data) { diff --git a/examples/touch.c b/examples/touch.c index 472ad41c..93905ffd 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #define _XOPEN_SOURCE 500 #include #include @@ -12,18 +12,24 @@ #include #include #include +#include #include #include +#include #include #include #include #include "cat.h" -#include "support/shared.h" struct sample_state { + struct wl_display *display; struct wlr_renderer *renderer; struct wlr_texture *cat_texture; struct wl_list touch_points; + struct timespec last_frame; + struct wl_listener new_output; + struct wl_listener new_input; + struct wl_list touch; }; struct touch_point { @@ -32,10 +38,38 @@ struct touch_point { struct wl_list link; }; -static void handle_output_frame(struct output_state *output, struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; - struct wlr_output *wlr_output = output->output; +struct touch_state { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener destroy; + struct wl_listener down; + struct wl_listener up; + struct wl_listener motion; + struct wl_list link; + void *data; +}; + +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; +}; + +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + +static void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); + struct sample_state *sample = sample_output->sample; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + struct wlr_output *wlr_output = sample_output->output; int32_t width, height; wlr_output_effective_resolution(wlr_output, &width, &height); @@ -57,57 +91,176 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_end(sample->renderer); wlr_output_swap_buffers(wlr_output, NULL, NULL); + sample->last_frame = now; } -static void handle_touch_down(struct touch_state *tstate, - int32_t touch_id, double x, double y) { - struct sample_state *sample = tstate->compositor->data; +static void touch_down_notify(struct wl_listener *listener, void *data) { + struct wlr_event_touch_motion *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, down); + struct sample_state *sample = tstate->sample; struct touch_point *point = calloc(1, sizeof(struct touch_point)); - point->touch_id = touch_id; - point->x = x; - point->y = y; + point->touch_id = event->touch_id; + point->x = event->x; + point->y = event->y; wl_list_insert(&sample->touch_points, &point->link); } -static void handle_touch_up(struct touch_state *tstate, int32_t touch_id) { - struct sample_state *sample = tstate->compositor->data; +static void touch_up_notify(struct wl_listener *listener, void *data ) { + struct wlr_event_touch_up *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, up); + struct sample_state *sample = tstate->sample; struct touch_point *point, *tmp; wl_list_for_each_safe(point, tmp, &sample->touch_points, link) { - if (point->touch_id == touch_id) { + if (point->touch_id == event->touch_id) { wl_list_remove(&point->link); break; } } } -static void handle_touch_motion(struct touch_state *tstate, - int32_t touch_id, double x, double y) { - struct sample_state *sample = tstate->compositor->data; +static void touch_motion_notify(struct wl_listener *listener, void *data) { + struct wlr_event_touch_motion *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, motion); + struct sample_state *sample = tstate->sample; struct touch_point *point; wl_list_for_each(point, &sample->touch_points, link) { - if (point->touch_id == touch_id) { - point->x = x; - point->y = y; + if (point->touch_id == event->touch_id) { + point->x = event->x; + point->y = event->y; break; } } } +static void touch_destroy_notify(struct wl_listener *listener, void *data) { + struct touch_state *tstate = wl_container_of(listener, tstate, destroy); + wl_list_remove(&tstate->link); + wl_list_remove(&tstate->destroy.link); + wl_list_remove(&tstate->down.link); + wl_list_remove(&tstate->up.link); + wl_list_remove(&tstate->motion.link); + free(tstate); +} + +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); + free(sample_output); +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; +} + +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } + } +} + +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + struct touch_state *tstate; + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + case WLR_INPUT_DEVICE_TOUCH: + tstate = calloc(sizeof(struct touch_state), 1); + tstate->device = device; + tstate->sample = sample; + tstate->destroy.notify = touch_destroy_notify; + wl_signal_add(&device->events.destroy, &tstate->destroy); + tstate->down.notify = touch_down_notify; + wl_signal_add(&device->touch->events.down, &tstate->down); + tstate->motion.notify = touch_motion_notify; + wl_signal_add(&device->touch->events.motion, &tstate->motion); + tstate->up.notify = touch_up_notify; + wl_signal_add(&device->touch->events.up, &tstate->up); + wl_list_insert(&sample->touch, &tstate->link); + break; + default: + break; + } +} + + int main(int argc, char *argv[]) { wlr_log_init(L_DEBUG, NULL); - struct sample_state state; - wl_list_init(&state.touch_points); - - struct compositor_state compositor = { 0, - .data = &state, - .output_frame_cb = handle_output_frame, - .touch_down_cb = handle_touch_down, - .touch_up_cb = handle_touch_up, - .touch_motion_cb = handle_touch_motion, + struct wl_display *display = wl_display_create(); + struct sample_state state = { + .display = display }; - compositor_init(&compositor); + wl_list_init(&state.touch_points); + wl_list_init(&state.touch); - state.renderer = wlr_backend_get_renderer(compositor.backend); + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { + exit(1); + } + + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; + clock_gettime(CLOCK_MONOTONIC, &state.last_frame); + + + state.renderer = wlr_backend_get_renderer(wlr); if (!state.renderer) { wlr_log(L_ERROR, "Could not start compositor, OOM"); exit(EXIT_FAILURE); @@ -120,14 +273,22 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - if (!wlr_backend_start(compositor.backend)) { - wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + const char *socket = wl_display_add_socket_auto(display); + if (!socket) { + wlr_log_errno(L_ERROR, "Unable to open wayland socket"); + wlr_backend_destroy(wlr); exit(1); } - wl_display_run(compositor.display); + setenv("_WAYLAND_DISPLAY", socket, true); + + if (!wlr_backend_start(wlr)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(wlr); + exit(1); + } + wl_display_run(display); wlr_texture_destroy(state.cat_texture); wlr_renderer_destroy(state.renderer); - compositor_fini(&compositor); + wl_display_destroy(display); } From 7be5dfecc0ce35c1481b2f0047de3dff0410900a Mon Sep 17 00:00:00 2001 From: Timidger Date: Fri, 20 Apr 2018 11:36:12 -0400 Subject: [PATCH 07/15] Made tablet standalone --- examples/tablet.c | 281 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 228 insertions(+), 53 deletions(-) diff --git a/examples/tablet.c b/examples/tablet.c index e8f90071..7b8abeb4 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #define _XOPEN_SOURCE 500 #include #include @@ -15,13 +15,14 @@ #include #include #include +#include #include #include #include #include -#include "support/shared.h" struct sample_state { + struct wl_display *display; struct wlr_renderer *renderer; bool proximity, tap, button; double distance; @@ -33,12 +34,55 @@ struct sample_state { struct wl_list link; float tool_color[4]; float pad_color[4]; + struct timespec last_frame; + struct wl_listener new_output; + struct wl_listener new_input; + struct wl_list tablet_tools; + struct wl_list tablet_pads; }; -static void handle_output_frame(struct output_state *output, struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; - struct wlr_output *wlr_output = output->output; +struct tablet_tool_state { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener destroy; + struct wl_listener axis; + struct wl_listener proximity; + struct wl_listener tip; + struct wl_listener button; + struct wl_list link; + void *data; +}; + +struct tablet_pad_state { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener destroy; + struct wl_listener button; + struct wl_listener ring; + struct wl_list link; + void *data; +}; + +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; +}; + +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + +static void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); + struct sample_state *sample = sample_output->sample; + struct wlr_output *wlr_output = sample_output->output; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); int32_t width, height; wlr_output_effective_resolution(wlr_output, &width, &height); @@ -87,11 +131,13 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_end(sample->renderer); wlr_output_swap_buffers(wlr_output, NULL, NULL); + sample->last_frame = now; } -static void handle_tool_axis(struct tablet_tool_state *tstate, - struct wlr_event_tablet_tool_axis *event) { - struct sample_state *sample = tstate->compositor->data; +static void tablet_tool_axis_notify(struct wl_listener *listener, void *data) { + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, axis); + struct wlr_event_tablet_tool_axis *event = data; + struct sample_state *sample = tstate->sample; if ((event->updated_axes & WLR_TABLET_TOOL_AXIS_X)) { sample->x = event->x; } @@ -112,21 +158,23 @@ static void handle_tool_axis(struct tablet_tool_state *tstate, } } -static void handle_tool_proximity(struct tablet_tool_state *tstate, - enum wlr_tablet_tool_proximity_state state) { - struct sample_state *sample = tstate->compositor->data; - sample->proximity = state == WLR_TABLET_TOOL_PROXIMITY_IN; +static void tablet_tool_proximity_notify(struct wl_listener *listener, void *data) { + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, proximity); + struct wlr_event_tablet_tool_proximity *event = data; + struct sample_state *sample = tstate->sample; + sample->proximity = event->state == WLR_TABLET_TOOL_PROXIMITY_IN; } -static void handle_tool_button(struct tablet_tool_state *tstate, - uint32_t button, enum wlr_button_state state) { - struct sample_state *sample = tstate->compositor->data; - if (state == WLR_BUTTON_RELEASED) { +static void tablet_tool_button_notify(struct wl_listener *listener, void *data) { + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, button); + struct wlr_event_tablet_tool_button *event = data; + struct sample_state *sample = tstate->sample; + if (event->state == WLR_BUTTON_RELEASED) { sample->button = false; } else { sample->button = true; for (size_t i = 0; i < 3; ++i) { - if (button % 3 == i) { + if (event->button % 3 == i) { sample->tool_color[i] = 0; } else { sample->tool_color[i] = 1; @@ -135,15 +183,16 @@ static void handle_tool_button(struct tablet_tool_state *tstate, } } -static void handle_pad_button(struct tablet_pad_state *pstate, - uint32_t button, enum wlr_button_state state) { - struct sample_state *sample = pstate->compositor->data; +static void tablet_pad_button_notify(struct wl_listener *listener, void *data) { + struct tablet_pad_state *pstate = wl_container_of(listener, pstate, button); + struct wlr_event_tablet_pad_button *event = data; + struct sample_state *sample = pstate->sample; float default_color[4] = { 0.5, 0.5, 0.5, 1.0 }; - if (state == WLR_BUTTON_RELEASED) { + if (event->state == WLR_BUTTON_RELEASED) { memcpy(sample->pad_color, default_color, sizeof(default_color)); } else { for (size_t i = 0; i < 3; ++i) { - if (button % 3 == i) { + if (event->button % 3 == i) { sample->pad_color[i] = 0; } else { sample->pad_color[i] = 1; @@ -152,56 +201,182 @@ static void handle_pad_button(struct tablet_pad_state *pstate, } } -static void handle_pad_ring(struct tablet_pad_state *pstate, - uint32_t ring, double position) { - struct sample_state *sample = pstate->compositor->data; - if (position != -1) { - sample->ring = -(position * (M_PI / 180.0)); +static void tablet_pad_ring_notify(struct wl_listener *listener, void *data) { + struct tablet_pad_state *pstate = wl_container_of(listener, pstate, ring); + struct wlr_event_tablet_pad_ring *event = data; + struct sample_state *sample = pstate->sample; + if (event->position != -1) { + sample->ring = -(event->position * (M_PI / 180.0)); } } -static void handle_input_add(struct compositor_state *cstate, - struct wlr_input_device *inputdev) { - struct sample_state *sample = cstate->data; - if (inputdev->type == WLR_INPUT_DEVICE_TABLET_TOOL) { - sample->width_mm = inputdev->width_mm == 0 ? - 20 : inputdev->width_mm; - sample->height_mm = inputdev->height_mm == 0 ? - 10 : inputdev->height_mm; +static void tablet_tool_destroy_notify(struct wl_listener *listener, void *data) { + struct tablet_tool_state *tstate = wl_container_of(listener, tstate, destroy); + wl_list_remove(&tstate->link); + wl_list_remove(&tstate->destroy.link); + wl_list_remove(&tstate->axis.link); + wl_list_remove(&tstate->proximity.link); + wl_list_remove(&tstate->button.link); + free(tstate); +} + +static void tablet_pad_destroy_notify(struct wl_listener *listener, void *data) { + struct tablet_pad_state *pstate = wl_container_of(listener, pstate, destroy); + wl_list_remove(&pstate->link); + wl_list_remove(&pstate->destroy.link); + wl_list_remove(&pstate->ring.link); + wl_list_remove(&pstate->button.link); + free(pstate); +} + +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); + free(sample_output); +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; +} + +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } } } +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + struct tablet_tool_state *tstate; + struct tablet_pad_state *pstate; + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + case WLR_INPUT_DEVICE_TABLET_PAD: + pstate = calloc(sizeof(struct tablet_pad_state), 1); + pstate->device = device; + pstate->sample = sample; + pstate->destroy.notify = tablet_pad_destroy_notify; + wl_signal_add(&device->events.destroy, &pstate->destroy); + pstate->button.notify = tablet_pad_button_notify; + wl_signal_add(&device->tablet_pad->events.button, &pstate->button); + pstate->ring.notify = tablet_pad_ring_notify; + wl_signal_add(&device->tablet_pad->events.ring, &pstate->ring); + wl_list_insert(&sample->tablet_pads, &pstate->link); + break; + case WLR_INPUT_DEVICE_TABLET_TOOL: + sample->width_mm = device->width_mm == 0 ? + 20 : device->width_mm; + sample->height_mm = device->height_mm == 0 ? + 10 : device->height_mm; + + tstate = calloc(sizeof(struct tablet_tool_state), 1); + tstate->device = device; + tstate->sample = sample; + tstate->destroy.notify = tablet_tool_destroy_notify; + wl_signal_add(&device->events.destroy, &tstate->destroy); + tstate->axis.notify = tablet_tool_axis_notify; + wl_signal_add(&device->tablet_tool->events.axis, &tstate->axis); + tstate->proximity.notify = tablet_tool_proximity_notify; + wl_signal_add(&device->tablet_tool->events.proximity, &tstate->proximity); + tstate->button.notify = tablet_tool_button_notify; + wl_signal_add(&device->tablet_tool->events.button, &tstate->button); + wl_list_insert(&sample->tablet_tools, &tstate->link); + break; + default: + break; + } +} + + int main(int argc, char *argv[]) { wlr_log_init(L_DEBUG, NULL); + struct wl_display *display = wl_display_create(); struct sample_state state = { + .display = display, .tool_color = { 1, 1, 1, 1 }, .pad_color = { 0.5, 0.5, 0.5, 1.0 } }; - struct compositor_state compositor = { - .data = &state, - .output_frame_cb = handle_output_frame, - .tool_axis_cb = handle_tool_axis, - .tool_proximity_cb = handle_tool_proximity, - .tool_button_cb = handle_tool_button, - .pad_button_cb = handle_pad_button, - .pad_ring_cb = handle_pad_ring, - .input_add_cb = handle_input_add, - 0 - }; - compositor_init(&compositor); + wl_list_init(&state.tablet_pads); + wl_list_init(&state.tablet_tools); + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { + exit(1); + } - state.renderer = wlr_backend_get_renderer(compositor.backend); + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; + clock_gettime(CLOCK_MONOTONIC, &state.last_frame); + + state.renderer = wlr_backend_get_renderer(wlr); if (!state.renderer) { wlr_log(L_ERROR, "Could not start compositor, OOM"); exit(EXIT_FAILURE); } - if (!wlr_backend_start(compositor.backend)) { + if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + wlr_backend_destroy(wlr); exit(1); } - wl_display_run(compositor.display); + wl_display_run(display); wlr_renderer_destroy(state.renderer); - compositor_fini(&compositor); + wl_display_destroy(display); } From b510c0f6c2b394de312879e3e45c63f60f06ae87 Mon Sep 17 00:00:00 2001 From: Timidger Date: Fri, 20 Apr 2018 12:11:51 -0400 Subject: [PATCH 08/15] Made rotation example standalone --- examples/rotation.c | 283 +++++++++++++++++++++++++++++++------------- 1 file changed, 201 insertions(+), 82 deletions(-) diff --git a/examples/rotation.c b/examples/rotation.c index 37def808..a7bbbf32 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -1,6 +1,7 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #define _XOPEN_SOURCE 500 #include +#include #include #include #include @@ -14,29 +15,48 @@ #include #include #include +#include +#include +#include #include #include #include -#include "support/shared.h" -#include "support/config.h" #include "cat.h" struct sample_state { - struct example_config *config; + struct wl_display *display; + struct wl_listener new_output; + struct wl_listener new_input; + struct timespec last_frame; struct wlr_renderer *renderer; struct wlr_texture *cat_texture; + struct wl_list outputs; + enum wl_output_transform transform; }; -struct output_data { +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; float x_offs, y_offs; float x_vel, y_vel; + struct wl_list link; }; -static void handle_output_frame(struct output_state *output, struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; - struct output_data *odata = output->data; - struct wlr_output *wlr_output = output->output; +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + +static void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); + struct sample_state *sample = sample_output->sample; + struct wlr_output *wlr_output = sample_output->output; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); int32_t width, height; wlr_output_effective_resolution(wlr_output, &width, &height); @@ -45,8 +65,8 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); - for (int y = -128 + (int)odata->y_offs; y < height; y += 128) { - for (int x = -128 + (int)odata->x_offs; x < width; x += 128) { + for (int y = -128 + (int)sample_output->y_offs; y < height; y += 128) { + for (int x = -128 + (int)sample_output->x_offs; x < width; x += 128) { wlr_render_texture(sample->renderer, sample->cat_texture, wlr_output->transform_matrix, x, y, 1.0f); } @@ -55,90 +75,191 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_end(sample->renderer); wlr_output_swap_buffers(wlr_output, NULL, NULL); - long ms = (ts->tv_sec - output->last_frame.tv_sec) * 1000 + - (ts->tv_nsec - output->last_frame.tv_nsec) / 1000000; + long ms = (now.tv_sec - sample->last_frame.tv_sec) * 1000 + + (now.tv_nsec - sample->last_frame.tv_nsec) / 1000000; float seconds = ms / 1000.0f; - odata->x_offs += odata->x_vel * seconds; - odata->y_offs += odata->y_vel * seconds; - if (odata->x_offs > 128) { - odata->x_offs = 0; + sample_output->x_offs += sample_output->x_vel * seconds; + sample_output->y_offs += sample_output->y_vel * seconds; + if (sample_output->x_offs > 128) { + sample_output->x_offs = 0; } - if (odata->y_offs > 128) { - odata->y_offs = 0; + if (sample_output->y_offs > 128) { + sample_output->y_offs = 0; } + sample->last_frame = now; } -static void handle_output_add(struct output_state *output) { - struct output_data *odata = calloc(1, sizeof(struct output_data)); - odata->x_offs = odata->y_offs = 0; - odata->x_vel = odata->y_vel = 128; - output->data = odata; - struct sample_state *state = output->compositor->data; - - struct output_config *conf; - wl_list_for_each(conf, &state->config->outputs, link) { - if (strcmp(conf->name, output->output->name) == 0) { - wlr_output_set_transform(output->output, conf->transform); - break; - } - } -} - -static void handle_output_remove(struct output_state *output) { - free(output->data); -} - -static void update_velocities(struct compositor_state *state, +static void update_velocities(struct sample_state *sample, float x_diff, float y_diff) { - struct output_state *output; - wl_list_for_each(output, &state->outputs, link) { - struct output_data *odata = output->data; - odata->x_vel += x_diff; - odata->y_vel += y_diff; + struct sample_output *sample_output; + wl_list_for_each(sample_output, &sample->outputs, link) { + sample_output->x_vel += x_diff; + sample_output->y_vel += y_diff; } } -static void handle_keyboard_key(struct keyboard_state *kbstate, - uint32_t keycode, xkb_keysym_t sym, enum wlr_key_state key_state, - uint64_t time_usec) { - // NOTE: It may be better to simply refer to our key state during each frame - // and make this change in pixels/sec^2 - // Also, key repeat - if (key_state == WLR_KEY_PRESSED) { - switch (sym) { - case XKB_KEY_Left: - update_velocities(kbstate->compositor, -16, 0); - break; - case XKB_KEY_Right: - update_velocities(kbstate->compositor, 16, 0); - break; - case XKB_KEY_Up: - update_velocities(kbstate->compositor, 0, -16); - break; - case XKB_KEY_Down: - update_velocities(kbstate->compositor, 0, 16); - break; +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); + free(sample_output); +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } + sample_output->x_offs = sample_output->y_offs = 0; + sample_output->x_vel = sample_output->y_vel = 128; + + wlr_output_set_transform(output, sample->transform); + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; + wl_list_insert(&sample->outputs, &sample_output->link); +} + +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } + if (event->state == WLR_KEY_PRESSED) { + switch (sym) { + case XKB_KEY_Left: + update_velocities(sample, -16, 0); + break; + case XKB_KEY_Right: + update_velocities(sample, 16, 0); + break; + case XKB_KEY_Up: + update_velocities(sample, 0, -16); + break; + case XKB_KEY_Down: + update_velocities(sample, 0, 16); + break; + } } } } +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + default: + break; + } +} + + int main(int argc, char *argv[]) { + int c; + enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL; + while ((c = getopt(argc, argv, "r:")) != -1) { + switch (c) { + case 'r': + if (strcmp(optarg, "90") == 0) { + transform = WL_OUTPUT_TRANSFORM_90; + } else if (strcmp(optarg, "180") == 0) { + transform = WL_OUTPUT_TRANSFORM_180; + } else if (strcmp(optarg, "270") == 0) { + transform = WL_OUTPUT_TRANSFORM_270; + } else if (strcmp(optarg, "flipped") == 0) { + transform = WL_OUTPUT_TRANSFORM_FLIPPED; + } else if (strcmp(optarg, "flipped-90") == 0) { + transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; + } else if (strcmp(optarg, "flipped-180") == 0) { + transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; + } else if (strcmp(optarg, "flipped-270") == 0) { + transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; + } else { + wlr_log(L_ERROR, "got unknown transform value: %s", optarg); + } + break; + default: + break; + } + } wlr_log_init(L_DEBUG, NULL); - struct sample_state state = {0}; - state.config = parse_args(argc, argv); + struct wl_display *display = wl_display_create(); + struct sample_state state = { + .display = display, + .transform = transform + }; + wl_list_init(&state.outputs); - struct compositor_state compositor = { 0 }; - compositor.data = &state; - compositor.output_add_cb = handle_output_add; - compositor.output_remove_cb = handle_output_remove; - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; - compositor_init(&compositor); + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { + exit(1); + } - state.renderer = wlr_backend_get_renderer(compositor.backend); + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; + clock_gettime(CLOCK_MONOTONIC, &state.last_frame); + const char *socket = wl_display_add_socket_auto(display); + if (!socket) { + wlr_log_errno(L_ERROR, "Unable to open wayland socket"); + wlr_backend_destroy(wlr); + exit(1); + } + setenv("_WAYLAND_DISPLAY", socket, true); + + state.renderer = wlr_backend_get_renderer(wlr); if (!state.renderer) { wlr_log(L_ERROR, "Could not start compositor, OOM"); + wlr_backend_destroy(wlr); exit(EXIT_FAILURE); } state.cat_texture = wlr_texture_from_pixels(state.renderer, @@ -149,16 +270,14 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - if (!wlr_backend_start(compositor.backend)) { + if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + wlr_backend_destroy(wlr); exit(1); } - wl_display_run(compositor.display); + wl_display_run(display); wlr_texture_destroy(state.cat_texture); wlr_renderer_destroy(state.renderer); - compositor_fini(&compositor); - - example_config_destroy(state.config); + wl_display_destroy(display); } From b3ca73c0acc2c38fe533a1ddd4ac674ad9ee93ba Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Apr 2018 17:18:56 -0400 Subject: [PATCH 09/15] Multi-pointer ported over --- examples/multi-pointer.c | 336 ++++++++++++++++++++++++++------------- 1 file changed, 227 insertions(+), 109 deletions(-) diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index ff60e5bf..aaa71a14 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #define _XOPEN_SOURCE 500 #include #include @@ -22,13 +22,23 @@ #include #include #include -#include "support/config.h" -#include "support/shared.h" -struct sample_state; +struct sample_state { + struct wl_display *display; + struct wlr_xcursor *xcursor; + float default_color[4]; + float clear_color[4]; + struct wlr_output_layout *layout; + struct wl_list cursors; // sample_cursor::link + struct wl_list pointers; // sample_pointer::link + struct wl_list outputs; // sample_output::link + struct timespec last_frame; + struct wl_listener new_output; + struct wl_listener new_input; +}; struct sample_cursor { - struct sample_state *state; + struct sample_state *sample; struct wlr_input_device *device; struct wlr_cursor *cursor; struct wl_list link; @@ -37,22 +47,52 @@ struct sample_cursor { struct wl_listener cursor_motion_absolute; struct wl_listener cursor_button; struct wl_listener cursor_axis; + struct wl_listener destroy; }; -struct sample_state { - struct compositor_state *compositor; - struct example_config *config; - struct wlr_xcursor *xcursor; - float default_color[4]; - float clear_color[4]; - struct wlr_output_layout *layout; - struct wl_list cursors; // sample_cursor::link +struct sample_pointer { + struct wlr_input_device *device; + struct wl_list link; }; -static void handle_output_frame(struct output_state *output, - struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; + struct wl_list link; +}; + +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + +void configure_cursor(struct wlr_cursor *cursor, struct wlr_input_device *device, + struct sample_state *sample) { + struct sample_output *output; + wlr_log(L_ERROR, "Configuring cursor %p for device %p", cursor, device); + + // reset mappings + wlr_cursor_map_to_output(cursor, NULL); + wlr_cursor_detach_input_device(cursor, device); + wlr_cursor_map_input_to_output(cursor, device, NULL); + + wlr_cursor_attach_input_device(cursor, device); + + // configure device to output mappings + wl_list_for_each(output, &sample->outputs, link) { + wlr_cursor_map_to_output(cursor, output->output); + + wlr_cursor_map_input_to_output(cursor, device, output->output); + } +} + +void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *output = wl_container_of(listener, output, frame); + struct sample_state *sample = output->sample; struct wlr_output *wlr_output = output->output; wlr_output_make_current(wlr_output, NULL); @@ -64,46 +104,6 @@ static void handle_output_frame(struct output_state *output, wlr_output_swap_buffers(wlr_output, NULL, NULL); } -static void handle_output_add(struct output_state *ostate) { - struct sample_state *sample = ostate->compositor->data; - - struct output_config *o_config = - example_config_get_output(sample->config, ostate->output); - - if (o_config) { - wlr_output_set_transform(ostate->output, o_config->transform); - wlr_output_layout_add(sample->layout, ostate->output, o_config->x, - o_config->y); - } else { - wlr_output_layout_add_auto(sample->layout, ostate->output); - } - - struct sample_cursor *cursor; - wl_list_for_each(cursor, &sample->cursors, link) { - example_config_configure_cursor(sample->config, cursor->cursor, - sample->compositor); - - struct wlr_xcursor_image *image = sample->xcursor->images[0]; - wlr_cursor_set_image(cursor->cursor, image->buffer, image->width, - image->width, image->height, image->hotspot_x, image->hotspot_y, 0); - - wlr_cursor_warp(cursor->cursor, NULL, cursor->cursor->x, - cursor->cursor->y); - } -} - -static void handle_output_remove(struct output_state *ostate) { - struct sample_state *sample = ostate->compositor->data; - - wlr_output_layout_remove(sample->layout, ostate->output); - - struct sample_cursor *cursor; - wl_list_for_each(cursor, &sample->cursors, link) { - example_config_configure_cursor(sample->config, cursor->cursor, - sample->compositor); - } -} - static void handle_cursor_motion(struct wl_listener *listener, void *data) { struct sample_cursor *cursor = wl_container_of(listener, cursor, cursor_motion); @@ -120,39 +120,6 @@ static void handle_cursor_motion_absolute(struct wl_listener *listener, wlr_cursor_warp_absolute(cursor->cursor, event->device, event->x, event->y); } -static void handle_input_add(struct compositor_state *state, - struct wlr_input_device *device) { - struct sample_state *sample = state->data; - - if (device->type != WLR_INPUT_DEVICE_POINTER) { - return; - } - - struct sample_cursor *cursor = calloc(1, sizeof(struct sample_cursor)); - cursor->state = sample; - cursor->device = device; - - cursor->cursor = wlr_cursor_create(); - wlr_cursor_attach_output_layout(cursor->cursor, sample->layout); - wlr_cursor_map_to_region(cursor->cursor, sample->config->cursor.mapped_box); - - wl_signal_add(&cursor->cursor->events.motion, &cursor->cursor_motion); - cursor->cursor_motion.notify = handle_cursor_motion; - wl_signal_add(&cursor->cursor->events.motion_absolute, - &cursor->cursor_motion_absolute); - cursor->cursor_motion_absolute.notify = handle_cursor_motion_absolute; - - wlr_cursor_attach_input_device(cursor->cursor, device); - example_config_configure_cursor(sample->config, cursor->cursor, - sample->compositor); - - struct wlr_xcursor_image *image = sample->xcursor->images[0]; - wlr_cursor_set_image(cursor->cursor, image->buffer, image->width * 4, - image->width, image->height, image->hotspot_x, image->hotspot_y, 0); - - wl_list_insert(&sample->cursors, &cursor->link); -} - static void cursor_destroy(struct sample_cursor *cursor) { wl_list_remove(&cursor->link); wl_list_remove(&cursor->cursor_motion.link); @@ -161,9 +128,10 @@ static void cursor_destroy(struct sample_cursor *cursor) { free(cursor); } -static void handle_input_remove(struct compositor_state *state, - struct wlr_input_device *device) { - struct sample_state *sample = state->data; +void input_remove_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_cursor *sample_cursor = wl_container_of(listener, sample_cursor, destroy); + struct sample_state *sample = sample_cursor->sample; struct sample_cursor *cursor; wl_list_for_each(cursor, &sample->cursors, link) { if (cursor->device == device) { @@ -171,29 +139,181 @@ static void handle_input_remove(struct compositor_state *state, break; } } + struct sample_pointer *pointer; + wl_list_for_each(pointer, &sample->pointers, link) { + if (pointer->device == device) { + free(pointer); + break; + } + } +} + +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + struct sample_state *sample = sample_output->sample; + wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); + wl_list_remove(&sample_output->link); + free(sample_output); + + struct sample_cursor *cursor; + wl_list_for_each(cursor, &sample->cursors, link) { + configure_cursor(cursor->cursor, cursor->device, sample); + } +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; + + wlr_output_layout_add_auto(sample->layout, output); + + + struct sample_cursor *cursor; + wl_list_for_each(cursor, &sample->cursors, link) { + configure_cursor(cursor->cursor, cursor->device, sample); + + struct wlr_xcursor_image *image = sample->xcursor->images[0]; + wlr_cursor_set_image(cursor->cursor, image->buffer, image->width * 4, + image->width, image->height, image->hotspot_x, image->hotspot_y, 0); + + wlr_cursor_warp(cursor->cursor, NULL, cursor->cursor->x, + cursor->cursor->y); + } + wl_list_insert(&sample->outputs, &sample_output->link); +} + +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } + } +} + +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + struct sample_cursor *cursor = 0; + struct sample_pointer *pointer = 0; + struct wlr_xcursor_image *image; + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + case WLR_INPUT_DEVICE_POINTER: + cursor = calloc(1, sizeof(struct sample_cursor)); + pointer = calloc(1, sizeof(struct sample_pointer)); + pointer->device = device; + cursor->sample = sample; + cursor->device = device; + + cursor->cursor = wlr_cursor_create(); + + wlr_cursor_attach_output_layout(cursor->cursor, sample->layout); + + wl_signal_add(&cursor->cursor->events.motion, &cursor->cursor_motion); + cursor->cursor_motion.notify = handle_cursor_motion; + wl_signal_add(&cursor->cursor->events.motion_absolute, + &cursor->cursor_motion_absolute); + cursor->cursor_motion_absolute.notify = handle_cursor_motion_absolute; + + wlr_cursor_attach_input_device(cursor->cursor, device); + configure_cursor(cursor->cursor, device, sample); + + image = sample->xcursor->images[0]; + wlr_cursor_set_image(cursor->cursor, image->buffer, image->width * 4, + image->width, image->height, image->hotspot_x, image->hotspot_y, 0); + + wl_list_insert(&sample->cursors, &cursor->link); + wl_list_insert(&sample->pointers, &pointer->link); + break; + default: + break; + } } int main(int argc, char *argv[]) { wlr_log_init(L_DEBUG, NULL); + struct wl_display *display = wl_display_create(); struct sample_state state = { .default_color = { 0.25f, 0.25f, 0.25f, 1 }, .clear_color = { 0.25f, 0.25f, 0.25f, 1 }, + .display = display, }; - + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { + exit(1); + } wl_list_init(&state.cursors); + wl_list_init(&state.pointers); + wl_list_init(&state.outputs); - state.config = parse_args(argc, argv); state.layout = wlr_output_layout_create(); - struct compositor_state compositor = { 0 }; - compositor.data = &state; - compositor.output_add_cb = handle_output_add; - compositor.output_remove_cb = handle_output_remove; - compositor.output_frame_cb = handle_output_frame; - compositor.input_add_cb = handle_input_add; - compositor.input_remove_cb = handle_input_remove; + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; - state.compositor = &compositor; + clock_gettime(CLOCK_MONOTONIC, &state.last_frame); + + const char *socket = wl_display_add_socket_auto(display); + if (!socket) { + wlr_log_errno(L_ERROR, "Unable to open wayland socket"); + wlr_backend_destroy(wlr); + exit(1); + } + setenv("_WAYLAND_DISPLAY", socket, true); struct wlr_xcursor_theme *theme = wlr_xcursor_theme_load("default", 16); if (!theme) { @@ -206,14 +326,13 @@ int main(int argc, char *argv[]) { return 1; } - compositor_init(&compositor); - if (!wlr_backend_start(compositor.backend)) { + if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + wlr_backend_destroy(wlr); exit(1); } - wl_display_run(compositor.display); - compositor_fini(&compositor); + wl_display_run(display); + wl_display_destroy(display); struct sample_cursor *cursor, *tmp_cursor; wl_list_for_each_safe(cursor, tmp_cursor, &state.cursors, link) { @@ -221,6 +340,5 @@ int main(int argc, char *argv[]) { } wlr_xcursor_theme_destroy(theme); - example_config_destroy(state.config); wlr_output_layout_destroy(state.layout); } From 8fd25cbc5bf50a2016cb8c726dbfc1b9509c56b0 Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Apr 2018 17:28:04 -0400 Subject: [PATCH 10/15] Fixed indentation --- examples/multi-pointer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index aaa71a14..236fd99e 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -71,7 +71,7 @@ struct sample_keyboard { }; void configure_cursor(struct wlr_cursor *cursor, struct wlr_input_device *device, - struct sample_state *sample) { + struct sample_state *sample) { struct sample_output *output; wlr_log(L_ERROR, "Configuring cursor %p for device %p", cursor, device); From 583654405fc48c334de523197c583e53199fb929 Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Apr 2018 17:32:00 -0400 Subject: [PATCH 11/15] Made output layout example standalone --- examples/output-layout.c | 224 +++++++++++++++++++++++++++------------ 1 file changed, 154 insertions(+), 70 deletions(-) diff --git a/examples/output-layout.c b/examples/output-layout.c index b04e86b7..9c8db60a 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #define _XOPEN_SOURCE 700 #include #include @@ -16,16 +16,17 @@ #include #include #include +#include #include #include #include #include -#include "support/cat.h" -#include "support/config.h" -#include "support/shared.h" +#include "cat.h" struct sample_state { - struct example_config *config; + struct wl_display *display; + struct wl_listener new_output; + struct wl_listener new_input; struct wlr_renderer *renderer; struct wlr_texture *cat_texture; struct wlr_output_layout *layout; @@ -34,6 +35,20 @@ struct sample_state { struct timespec ts_last; }; +struct sample_output { + struct sample_state *sample; + struct wlr_output *output; + struct wl_listener frame; + struct wl_listener destroy; +}; + +struct sample_keyboard { + struct sample_state *sample; + struct wlr_input_device *device; + struct wl_listener key; + struct wl_listener destroy; +}; + static void animate_cat(struct sample_state *sample, struct wlr_output *output) { struct timespec ts; @@ -93,10 +108,12 @@ static void animate_cat(struct sample_state *sample, sample->ts_last = ts; } -static void handle_output_frame(struct output_state *output, - struct timespec *ts) { - struct compositor_state *state = output->compositor; - struct sample_state *sample = state->data; +void output_frame_notify(struct wl_listener *listener, void *data) { + struct sample_output *output = wl_container_of(listener, output, frame); + struct sample_state *sample = output->sample; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + struct wlr_output *wlr_output = output->output; wlr_output_make_current(wlr_output, NULL); @@ -124,94 +141,161 @@ static void handle_output_frame(struct output_state *output, wlr_output_swap_buffers(wlr_output, NULL, NULL); } -static void handle_output_add(struct output_state *ostate) { - struct sample_state *sample = ostate->compositor->data; - - struct output_config *o_config = - example_config_get_output(sample->config, ostate->output); - - if (o_config) { - wlr_output_set_transform(ostate->output, o_config->transform); - wlr_output_layout_add(sample->layout, ostate->output, o_config->x, - o_config->y); - } else { - wlr_output_layout_add_auto(sample->layout, ostate->output); - } -} - -static void handle_output_remove(struct output_state *ostate) { - struct sample_state *sample = ostate->compositor->data; - wlr_output_layout_remove(sample->layout, ostate->output); -} - -static void update_velocities(struct compositor_state *state, +static void update_velocities(struct sample_state *sample, float x_diff, float y_diff) { - struct sample_state *sample = state->data; sample->x_vel += x_diff; sample->y_vel += y_diff; } -static void handle_keyboard_key(struct keyboard_state *kbstate, - uint32_t keycode, xkb_keysym_t sym, enum wlr_key_state key_state, - uint64_t time_usec) { - // NOTE: It may be better to simply refer to our key state during each frame - // and make this change in pixels/sec^2 - // Also, key repeat - int delta = 75; - if (key_state == WLR_KEY_PRESSED) { - switch (sym) { - case XKB_KEY_Left: - update_velocities(kbstate->compositor, -delta, 0); - break; - case XKB_KEY_Right: - update_velocities(kbstate->compositor, delta, 0); - break; - case XKB_KEY_Up: - update_velocities(kbstate->compositor, 0, -delta); - break; - case XKB_KEY_Down: - update_velocities(kbstate->compositor, 0, delta); - break; +void output_remove_notify(struct wl_listener *listener, void *data) { + struct sample_output *sample_output = wl_container_of(listener, sample_output, destroy); + struct sample_state *sample = sample_output->sample; + wlr_output_layout_remove(sample->layout, sample_output->output); + wl_list_remove(&sample_output->frame.link); + wl_list_remove(&sample_output->destroy.link); + free(sample_output); +} + +void new_output_notify(struct wl_listener *listener, void *data) { + struct wlr_output *output = data; + struct sample_state *sample = wl_container_of(listener, sample, new_output); + struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); + if (wl_list_length(&output->modes) > 0) { + struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + wlr_output_set_mode(output, mode); + } + wlr_output_layout_add_auto(sample->layout, output); + sample_output->output = output; + sample_output->sample = sample; + wl_signal_add(&output->events.frame, &sample_output->frame); + sample_output->frame.notify = output_frame_notify; + wl_signal_add(&output->events.destroy, &sample_output->destroy); + sample_output->destroy.notify = output_remove_notify; +} + +void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, key); + struct sample_state *sample = keyboard->sample; + struct wlr_event_keyboard_key *event = data; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(keyboard->device->keyboard->xkb_state, + keycode, &syms); + for (int i = 0; i < nsyms; i++) { + xkb_keysym_t sym = syms[i]; + if (sym == XKB_KEY_Escape) { + wl_display_terminate(sample->display); + } + // NOTE: It may be better to simply refer to our key state during each frame + // and make this change in pixels/sec^2 + // Also, key repeat + int delta = 75; + if (event->state == WLR_KEY_PRESSED) { + switch (sym) { + case XKB_KEY_Left: + update_velocities(sample, -delta, 0); + break; + case XKB_KEY_Right: + update_velocities(sample, delta, 0); + break; + case XKB_KEY_Up: + update_velocities(sample, 0, -delta); + break; + case XKB_KEY_Down: + update_velocities(sample, 0, delta); + break; + } } } } +void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct sample_keyboard *keyboard = wl_container_of(listener, keyboard, destroy); + wl_list_remove(&keyboard->destroy.link); + wl_list_remove(&keyboard->key.link); + free(keyboard); +} + +void new_input_notify(struct wl_listener *listener, void *data) { + struct wlr_input_device *device = data; + struct sample_state *sample = wl_container_of(listener, sample, new_input); + struct sample_keyboard *keyboard; + struct xkb_rule_names rules; + struct xkb_context *context; + switch (device->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + keyboard = calloc(1, sizeof(struct sample_keyboard)); + keyboard->device = device; + keyboard->sample = sample; + wl_signal_add(&device->events.destroy, &keyboard->destroy); + keyboard->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&device->keyboard->events.key, &keyboard->key); + keyboard->key.notify = keyboard_key_notify; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names(context, + &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + break; + default: + break; + } +} + + int main(int argc, char *argv[]) { wlr_log_init(L_DEBUG, NULL); - struct sample_state state = {0}; + struct wl_display *display = wl_display_create(); + struct sample_state state = { + .x_vel = 500, + .y_vel = 500, + .display = display, + }; - state.x_vel = 500; - state.y_vel = 500; state.layout = wlr_output_layout_create(); clock_gettime(CLOCK_MONOTONIC, &state.ts_last); - state.config = parse_args(argc, argv); + struct wlr_backend *wlr = wlr_backend_autocreate(display); + if (!wlr) { + exit(1); + } - struct compositor_state compositor = { 0 }; - compositor.data = &state; - compositor.output_add_cb = handle_output_add; - compositor.output_remove_cb = handle_output_remove; - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; - compositor_init(&compositor); + wl_signal_add(&wlr->events.new_output, &state.new_output); + state.new_output.notify = new_output_notify; + wl_signal_add(&wlr->events.new_input, &state.new_input); + state.new_input.notify = new_input_notify; - state.renderer = wlr_backend_get_renderer(compositor.backend); + const char *socket = wl_display_add_socket_auto(display); + if (!socket) { + wlr_log_errno(L_ERROR, "Unable to open wayland socket"); + wlr_backend_destroy(wlr); + exit(1); + } + setenv("_WAYLAND_DISPLAY", socket, true); + + state.renderer = wlr_backend_get_renderer(wlr); state.cat_texture = wlr_texture_from_pixels(state.renderer, WL_SHM_FORMAT_ABGR8888, cat_tex.width * 4, cat_tex.width, cat_tex.height, cat_tex.pixel_data); - if (!wlr_backend_start(compositor.backend)) { + if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(compositor.backend); + wlr_backend_destroy(wlr); exit(1); } - wl_display_run(compositor.display); + wl_display_run(display); wlr_texture_destroy(state.cat_texture); wlr_renderer_destroy(state.renderer); - compositor_fini(&compositor); wlr_output_layout_destroy(state.layout); - - example_config_destroy(state.config); } From 167ca57b18f4777d716b3f37dd1b378695e11290 Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Apr 2018 17:32:07 -0400 Subject: [PATCH 12/15] Fixed up building examples --- examples/meson.build | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/examples/meson.build b/examples/meson.build index 896f2bd0..4725b989 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -3,22 +3,11 @@ wayland_cursor = dependency('wayland-cursor') executable('simple', 'simple.c', dependencies: wlroots) executable('pointer', 'pointer.c', dependencies: wlroots) -executable('touch', 'touch.c', dependencies: wlroots) +executable('touch', 'touch.c', 'cat.c', dependencies: wlroots) executable('tablet', 'tablet.c', dependencies: wlroots) +executable('rotation', 'rotation.c', 'cat.c', dependencies: wlroots) executable('multi-pointer', 'multi-pointer.c', dependencies: wlroots) - -executable( - 'rotation', - 'rotation.c', - 'cat.c', - dependencies: wlroots, -) - -executable( - 'output-layout', - 'output-layout.c', - dependencies: wlroots -) +executable('output-layout', 'output-layout.c', 'cat.c', dependencies: wlroots) executable( 'screenshot', From 7f1230fe7881d343e83da30affcbae80e494da08 Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Apr 2018 19:47:11 -0400 Subject: [PATCH 13/15] Remvoed unnecessary wayland socket in examples --- examples/pointer.c | 8 +------- examples/rotation.c | 7 ------- examples/simple.c | 8 +------- examples/touch.c | 8 -------- 4 files changed, 2 insertions(+), 29 deletions(-) diff --git a/examples/pointer.c b/examples/pointer.c index 34f6c896..00002921 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -395,13 +395,7 @@ int main(int argc, char *argv[]) { image->width, image->height, image->hotspot_x, image->hotspot_y, 0); clock_gettime(CLOCK_MONOTONIC, &state.last_frame); - const char *socket = wl_display_add_socket_auto(display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - setenv("_WAYLAND_DISPLAY", socket, true); + if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); wlr_backend_destroy(wlr); diff --git a/examples/rotation.c b/examples/rotation.c index a7bbbf32..618e91af 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -248,13 +248,6 @@ int main(int argc, char *argv[]) { wl_signal_add(&wlr->events.new_input, &state.new_input); state.new_input.notify = new_input_notify; clock_gettime(CLOCK_MONOTONIC, &state.last_frame); - const char *socket = wl_display_add_socket_auto(display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - setenv("_WAYLAND_DISPLAY", socket, true); state.renderer = wlr_backend_get_renderer(wlr); if (!state.renderer) { diff --git a/examples/simple.c b/examples/simple.c index 7bf95601..0c58f933 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -165,13 +165,7 @@ int main() { wl_signal_add(&wlr->events.new_input, &state.new_input); state.new_input.notify = new_input_notify; clock_gettime(CLOCK_MONOTONIC, &state.last_frame); - const char *socket = wl_display_add_socket_auto(display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - setenv("_WAYLAND_DISPLAY", socket, true); + if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); wlr_backend_destroy(wlr); diff --git a/examples/touch.c b/examples/touch.c index 93905ffd..1d56a9ff 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -273,14 +273,6 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - const char *socket = wl_display_add_socket_auto(display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - setenv("_WAYLAND_DISPLAY", socket, true); - if (!wlr_backend_start(wlr)) { wlr_log(L_ERROR, "Failed to start backend"); wlr_backend_destroy(wlr); From 7da50d065b298b74e16c86065c19cfe22e157c6f Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Apr 2018 20:16:29 -0400 Subject: [PATCH 14/15] Fixed style issues --- examples/multi-pointer.c | 26 ++++++++++---------------- examples/output-layout.c | 15 ++++++--------- examples/pointer.c | 16 ++++++---------- examples/rotation.c | 15 ++++++--------- examples/simple.c | 16 ++++++---------- examples/tablet.c | 23 +++++++++-------------- examples/touch.c | 20 ++++++++------------ 7 files changed, 51 insertions(+), 80 deletions(-) diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index 236fd99e..6b67e8eb 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -205,8 +205,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } } } @@ -220,28 +220,22 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; - struct sample_cursor *cursor = 0; - struct sample_pointer *pointer = 0; - struct wlr_xcursor_image *image; switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); @@ -250,9 +244,9 @@ void new_input_notify(struct wl_listener *listener, void *data) { &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); xkb_context_unref(context); break; - case WLR_INPUT_DEVICE_POINTER: - cursor = calloc(1, sizeof(struct sample_cursor)); - pointer = calloc(1, sizeof(struct sample_pointer)); + case WLR_INPUT_DEVICE_POINTER:; + struct sample_cursor *cursor = calloc(1, sizeof(struct sample_cursor)); + struct sample_pointer *pointer = calloc(1, sizeof(struct sample_pointer)); pointer->device = device; cursor->sample = sample; cursor->device = device; @@ -270,7 +264,7 @@ void new_input_notify(struct wl_listener *listener, void *data) { wlr_cursor_attach_input_device(cursor->cursor, device); configure_cursor(cursor->cursor, device, sample); - image = sample->xcursor->images[0]; + struct wlr_xcursor_image *image = sample->xcursor->images[0]; wlr_cursor_set_image(cursor->cursor, image->buffer, image->width * 4, image->width, image->height, image->hotspot_x, image->hotspot_y, 0); diff --git a/examples/output-layout.c b/examples/output-layout.c index 9c8db60a..382e170e 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -184,8 +184,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } // NOTE: It may be better to simply refer to our key state during each frame // and make this change in pixels/sec^2 // Also, key repeat @@ -219,25 +219,22 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); diff --git a/examples/pointer.c b/examples/pointer.c index 00002921..96c07667 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -74,7 +74,6 @@ struct sample_keyboard { struct wl_listener destroy; }; - static void warp_to_touch(struct sample_state *sample, struct wlr_input_device *dev) { if (wl_list_empty(&sample->touch_points)) { @@ -235,8 +234,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } } } @@ -282,9 +281,6 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; switch (device->type) { case WLR_INPUT_DEVICE_POINTER: case WLR_INPUT_DEVICE_TOUCH: @@ -292,21 +288,21 @@ void new_input_notify(struct wl_listener *listener, void *data) { wlr_cursor_attach_input_device(sample->cursor, device); break; - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); diff --git a/examples/rotation.c b/examples/rotation.c index 618e91af..4a30cd96 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -138,8 +138,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } if (event->state == WLR_KEY_PRESSED) { switch (sym) { case XKB_KEY_Left: @@ -169,25 +169,22 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); diff --git a/examples/simple.c b/examples/simple.c index 0c58f933..d3424036 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -36,7 +36,6 @@ struct sample_keyboard { struct wl_listener destroy; }; - void output_frame_notify(struct wl_listener *listener, void *data) { wlr_log(L_DEBUG, "Output removed"); struct sample_output *sample_output = wl_container_of(listener, sample_output, frame); @@ -100,8 +99,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } } } @@ -115,25 +114,22 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); diff --git a/examples/tablet.c b/examples/tablet.c index 7b8abeb4..bf67ab93 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -263,8 +263,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } } } @@ -278,27 +278,22 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; - struct tablet_tool_state *tstate; - struct tablet_pad_state *pstate; switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); @@ -307,8 +302,8 @@ void new_input_notify(struct wl_listener *listener, void *data) { &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); xkb_context_unref(context); break; - case WLR_INPUT_DEVICE_TABLET_PAD: - pstate = calloc(sizeof(struct tablet_pad_state), 1); + case WLR_INPUT_DEVICE_TABLET_PAD:; + struct tablet_pad_state *pstate = calloc(sizeof(struct tablet_pad_state), 1); pstate->device = device; pstate->sample = sample; pstate->destroy.notify = tablet_pad_destroy_notify; @@ -325,7 +320,7 @@ void new_input_notify(struct wl_listener *listener, void *data) { sample->height_mm = device->height_mm == 0 ? 10 : device->height_mm; - tstate = calloc(sizeof(struct tablet_tool_state), 1); + struct tablet_tool_state *tstate = calloc(sizeof(struct tablet_tool_state), 1); tstate->device = device; tstate->sample = sample; tstate->destroy.notify = tablet_tool_destroy_notify; diff --git a/examples/touch.c b/examples/touch.c index 1d56a9ff..47f5793c 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -176,8 +176,8 @@ void keyboard_key_notify(struct wl_listener *listener, void *data) { for (int i = 0; i < nsyms; i++) { xkb_keysym_t sym = syms[i]; if (sym == XKB_KEY_Escape) { - wl_display_terminate(sample->display); - } + wl_display_terminate(sample->display); + } } } @@ -191,26 +191,22 @@ void keyboard_destroy_notify(struct wl_listener *listener, void *data) { void new_input_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct sample_state *sample = wl_container_of(listener, sample, new_input); - struct sample_keyboard *keyboard; - struct xkb_rule_names rules; - struct xkb_context *context; - struct touch_state *tstate; switch (device->type) { - case WLR_INPUT_DEVICE_KEYBOARD: - keyboard = calloc(1, sizeof(struct sample_keyboard)); + case WLR_INPUT_DEVICE_KEYBOARD:; + struct sample_keyboard *keyboard = calloc(1, sizeof(struct sample_keyboard)); keyboard->device = device; keyboard->sample = sample; wl_signal_add(&device->events.destroy, &keyboard->destroy); keyboard->destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->keyboard->events.key, &keyboard->key); keyboard->key.notify = keyboard_key_notify; - memset(&rules, 0, sizeof(rules)); + struct xkb_rule_names rules = { 0 }; rules.rules = getenv("XKB_DEFAULT_RULES"); rules.model = getenv("XKB_DEFAULT_MODEL"); rules.layout = getenv("XKB_DEFAULT_LAYOUT"); rules.variant = getenv("XKB_DEFAULT_VARIANT"); rules.options = getenv("XKB_DEFAULT_OPTIONS"); - context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { wlr_log(L_ERROR, "Failed to create XKB context"); exit(1); @@ -219,8 +215,8 @@ void new_input_notify(struct wl_listener *listener, void *data) { &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); xkb_context_unref(context); break; - case WLR_INPUT_DEVICE_TOUCH: - tstate = calloc(sizeof(struct touch_state), 1); + case WLR_INPUT_DEVICE_TOUCH:; + struct touch_state *tstate = calloc(sizeof(struct touch_state), 1); tstate->device = device; tstate->sample = sample; tstate->destroy.notify = touch_destroy_notify; From 9a1d0e42d2c8eda68bba1c1b4dfecc4bf6485baa Mon Sep 17 00:00:00 2001 From: Timidger Date: Thu, 10 May 2018 19:03:36 -0700 Subject: [PATCH 15/15] Fixed emersion's issues --- examples/multi-pointer.c | 12 ++---------- examples/output-layout.c | 12 ++---------- examples/pointer.c | 4 ++-- examples/rotation.c | 4 ++-- examples/simple.c | 4 ++-- examples/tablet.c | 4 ++-- examples/touch.c | 4 ++-- 7 files changed, 14 insertions(+), 30 deletions(-) diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index 6b67e8eb..83ee253d 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -166,8 +166,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } sample_output->output = output; @@ -301,14 +301,6 @@ int main(int argc, char *argv[]) { clock_gettime(CLOCK_MONOTONIC, &state.last_frame); - const char *socket = wl_display_add_socket_auto(display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - setenv("_WAYLAND_DISPLAY", socket, true); - struct wlr_xcursor_theme *theme = wlr_xcursor_theme_load("default", 16); if (!theme) { wlr_log(L_ERROR, "Failed to load cursor theme"); diff --git a/examples/output-layout.c b/examples/output-layout.c index 382e170e..d45100b6 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -160,8 +160,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } wlr_output_layout_add_auto(sample->layout, output); @@ -271,14 +271,6 @@ int main(int argc, char *argv[]) { wl_signal_add(&wlr->events.new_input, &state.new_input); state.new_input.notify = new_input_notify; - const char *socket = wl_display_add_socket_auto(display); - if (!socket) { - wlr_log_errno(L_ERROR, "Unable to open wayland socket"); - wlr_backend_destroy(wlr); - exit(1); - } - setenv("_WAYLAND_DISPLAY", socket, true); - state.renderer = wlr_backend_get_renderer(wlr); state.cat_texture = wlr_texture_from_pixels(state.renderer, WL_SHM_FORMAT_ABGR8888, cat_tex.width * 4, cat_tex.width, cat_tex.height, diff --git a/examples/pointer.c b/examples/pointer.c index 96c07667..399b1de6 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -252,8 +252,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } sample_output->output = output; diff --git a/examples/rotation.c b/examples/rotation.c index 4a30cd96..0ee9964c 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -110,8 +110,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } sample_output->x_offs = sample_output->y_offs = 0; diff --git a/examples/simple.c b/examples/simple.c index d3424036..e64289fb 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -76,8 +76,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } sample_output->output = output; diff --git a/examples/tablet.c b/examples/tablet.c index bf67ab93..d178f2ef 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -240,8 +240,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } sample_output->output = output; diff --git a/examples/touch.c b/examples/touch.c index 47f5793c..069fda74 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -153,8 +153,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *output = data; struct sample_state *sample = wl_container_of(listener, sample, new_output); struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); - if (wl_list_length(&output->modes) > 0) { - struct wlr_output_mode *mode = wl_container_of((&output->modes)->prev, mode, link); + if (!wl_list_empty(&output->modes)) { + struct wlr_output_mode *mode = wl_container_of(output->modes.prev, mode, link); wlr_output_set_mode(output, mode); } sample_output->output = output;