mirror of
				https://gitlab.freedesktop.org/wlroots/wlroots.git
				synced 2025-10-29 05:40:12 -04:00 
			
		
		
		
	Move shared example code
This commit is contained in:
		
							parent
							
								
									19f24e5530
								
							
						
					
					
						commit
						4f73498b78
					
				
					 17 changed files with 21 additions and 18 deletions
				
			
		
							
								
								
									
										2
									
								
								examples/support/README
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								examples/support/README
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,2 @@ | |||
| Support code for the examples. Code that's not relevant to the principle each | ||||
| example demonstrates is largely offloaded to this directory. | ||||
							
								
								
									
										2641
									
								
								examples/support/cat.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2641
									
								
								examples/support/cat.c
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										13
									
								
								examples/support/cat.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								examples/support/cat.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | |||
| #ifndef _CAT_H | ||||
| #define _CAT_H | ||||
| 
 | ||||
| struct gimp_texture { | ||||
|   unsigned int 	 width; | ||||
|   unsigned int 	 height; | ||||
|   unsigned int 	 bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */  | ||||
|   unsigned char	 pixel_data[128 * 128 * 4 + 1]; | ||||
| }; | ||||
| 
 | ||||
| extern const struct gimp_texture cat_tex; | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										347
									
								
								examples/support/config.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										347
									
								
								examples/support/config.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,347 @@ | |||
| #ifndef _POSIX_C_SOURCE | ||||
| #define _POSIX_C_SOURCE 200809L | ||||
| #endif | ||||
| #include <stdlib.h> | ||||
| #include <limits.h> | ||||
| #include <getopt.h> | ||||
| #include <string.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/param.h> | ||||
| #include <wlr/util/log.h> | ||||
| #include <wlr/types/wlr_box.h> | ||||
| #include "shared.h" | ||||
| #include "config.h" | ||||
| #include "ini.h" | ||||
| 
 | ||||
| static void usage(const char *name, int ret) { | ||||
| 	fprintf(stderr, | ||||
| 		"usage: %s [-C <FILE>]\n" | ||||
| 		"\n" | ||||
| 		" -C <FILE>      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]; | ||||
| 			snprintf(buf, MAXPATHLEN, "%s/%s", cwd, "wlr-example.ini"); | ||||
| 			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); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										60
									
								
								examples/support/config.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								examples/support/config.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,60 @@ | |||
| #ifndef _EXAMPLE_CONFIG_H | ||||
| #define _EXAMPLE_CONFIG_H | ||||
| #ifndef _POSIX_C_SOURCE | ||||
| #define _POSIX_C_SOURCE 200112L | ||||
| #endif | ||||
| #include <wlr/types/wlr_output_layout.h> | ||||
| #include <wlr/types/wlr_input_device.h> | ||||
| #include <wlr/types/wlr_cursor.h> | ||||
| #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 | ||||
							
								
								
									
										195
									
								
								examples/support/ini.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										195
									
								
								examples/support/ini.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,195 @@ | |||
| /* 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 <stdio.h> | ||||
| #include <ctype.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| #include "ini.h" | ||||
| 
 | ||||
| #if !INI_USE_STACK | ||||
| #include <stdlib.h> | ||||
| #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); | ||||
|     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; | ||||
| } | ||||
							
								
								
									
										93
									
								
								examples/support/ini.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								examples/support/ini.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,93 @@ | |||
| /* 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 <stdio.h> | ||||
| 
 | ||||
| /* 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__ */ | ||||
							
								
								
									
										499
									
								
								examples/support/shared.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										499
									
								
								examples/support/shared.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,499 @@ | |||
| #define _POSIX_C_SOURCE 200112L | ||||
| #include <assert.h> | ||||
| #include <string.h> | ||||
| #include <stdlib.h> | ||||
| #include <time.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <stdbool.h> | ||||
| #include <unistd.h> | ||||
| #include <limits.h> | ||||
| #include <xkbcommon/xkbcommon.h> | ||||
| #include <wayland-server-protocol.h> | ||||
| #include <wlr/backend.h> | ||||
| #include <wlr/backend/session.h> | ||||
| #include <wlr/backend/multi.h> | ||||
| #include <wlr/types/wlr_output.h> | ||||
| #include <wlr/types/wlr_output_layout.h> | ||||
| #include <wlr/types/wlr_input_device.h> | ||||
| #include <wlr/util/log.h> | ||||
| #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_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->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_mm, event->y_mm); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 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_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->motion.notify = pointer_motion_notify; | ||||
| 	pstate->motion_absolute.notify = pointer_motion_absolute_notify; | ||||
| 	pstate->button.notify = pointer_button_notify; | ||||
| 	pstate->axis.notify = pointer_axis_notify; | ||||
| 	wl_signal_add(&device->pointer->events.motion, &pstate->motion); | ||||
| 	wl_signal_add(&device->pointer->events.motion_absolute, &pstate->motion_absolute); | ||||
| 	wl_signal_add(&device->pointer->events.button, &pstate->button); | ||||
| 	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->slot, | ||||
| 			event->x_mm, event->y_mm, event->width_mm, event->height_mm); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 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->slot, | ||||
| 			event->x_mm, event->y_mm, event->width_mm, event->height_mm); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 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->slot); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 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->slot); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 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->down.notify = touch_down_notify; | ||||
| 	tstate->motion.notify = touch_motion_notify; | ||||
| 	tstate->up.notify = touch_up_notify; | ||||
| 	tstate->cancel.notify = touch_cancel_notify; | ||||
| 	wl_signal_add(&device->touch->events.down, &tstate->down); | ||||
| 	wl_signal_add(&device->touch->events.motion, &tstate->motion); | ||||
| 	wl_signal_add(&device->touch->events.up, &tstate->up); | ||||
| 	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_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->axis.notify = tablet_tool_axis_notify; | ||||
| 	tstate->proximity.notify = tablet_tool_proximity_notify; | ||||
| 	//tstate->tip.notify = tablet_tool_tip_notify;
 | ||||
| 	tstate->button.notify = tablet_tool_button_notify; | ||||
| 	wl_signal_add(&device->tablet_tool->events.axis, &tstate->axis); | ||||
| 	wl_signal_add(&device->tablet_tool->events.proximity, &tstate->proximity); | ||||
| 	//wl_signal_add(&device->tablet_tool->events.tip, &tstate->tip);
 | ||||
| 	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_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->button.notify = tablet_pad_button_notify; | ||||
| 	wl_signal_add(&device->tablet_pad->events.button, &pstate->button); | ||||
| 	wl_list_insert(&state->tablet_pads, &pstate->link); | ||||
| } | ||||
| 
 | ||||
| static void input_add_notify(struct wl_listener *listener, void *data) { | ||||
| 	struct wlr_input_device *device = data; | ||||
| 	struct compositor_state *state = wl_container_of(listener, state, input_add); | ||||
| 	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 keyboard_remove(struct wlr_input_device *device, struct compositor_state *state) { | ||||
| 	struct keyboard_state *kbstate = NULL, *_kbstate; | ||||
| 	wl_list_for_each(_kbstate, &state->keyboards, link) { | ||||
| 		if (_kbstate->device == device) { | ||||
| 			kbstate = _kbstate; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	if (!kbstate) { | ||||
| 		return; | ||||
| 	} | ||||
| 	wl_list_remove(&kbstate->link); | ||||
| 	wl_list_remove(&kbstate->key.link); | ||||
| 	free(kbstate); | ||||
| } | ||||
| 
 | ||||
| static void pointer_remove(struct wlr_input_device *device, struct compositor_state *state) { | ||||
| 	struct pointer_state *pstate = NULL, *_pstate; | ||||
| 	wl_list_for_each(_pstate, &state->pointers, link) { | ||||
| 		if (_pstate->device == device) { | ||||
| 			pstate = _pstate; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	if (!pstate) { | ||||
| 		return; | ||||
| 	} | ||||
| 	wl_list_remove(&pstate->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 touch_remove(struct wlr_input_device *device, struct compositor_state *state) { | ||||
| 	struct touch_state *tstate = NULL, *_tstate; | ||||
| 	wl_list_for_each(_tstate, &state->touch, link) { | ||||
| 		if (_tstate->device == device) { | ||||
| 			tstate = _tstate; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	if (!tstate) { | ||||
| 		return; | ||||
| 	} | ||||
| 	wl_list_remove(&tstate->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 tablet_tool_remove(struct wlr_input_device *device, struct compositor_state *state) { | ||||
| 	struct tablet_tool_state *tstate = NULL, *_tstate; | ||||
| 	wl_list_for_each(_tstate, &state->tablet_tools, link) { | ||||
| 		if (_tstate->device == device) { | ||||
| 			tstate = _tstate; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	if (!tstate) { | ||||
| 		return; | ||||
| 	} | ||||
| 	wl_list_remove(&tstate->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_pad_remove(struct wlr_input_device *device, struct compositor_state *state) { | ||||
| 	struct tablet_pad_state *pstate = NULL, *_pstate; | ||||
| 	wl_list_for_each(_pstate, &state->tablet_pads, link) { | ||||
| 		if (_pstate->device ==device) { | ||||
| 			pstate = _pstate; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	if (!pstate) { | ||||
| 		return; | ||||
| 	} | ||||
| 	wl_list_remove(&pstate->button.link); | ||||
| 	free(pstate); | ||||
| } | ||||
| 
 | ||||
| static void input_remove_notify(struct wl_listener *listener, void *data) { | ||||
| 	struct wlr_input_device *device = data; | ||||
| 	struct compositor_state *state = wl_container_of(listener, state, input_remove); | ||||
| 
 | ||||
| 	if (state->input_remove_cb) { | ||||
| 		state->input_remove_cb(state, device); | ||||
| 	} | ||||
| 
 | ||||
| 	switch (device->type) { | ||||
| 	case WLR_INPUT_DEVICE_KEYBOARD: | ||||
| 		keyboard_remove(device, state); | ||||
| 		break; | ||||
| 	case WLR_INPUT_DEVICE_POINTER: | ||||
| 		pointer_remove(device, state); | ||||
| 		break; | ||||
| 	case WLR_INPUT_DEVICE_TOUCH: | ||||
| 		touch_remove(device, state); | ||||
| 		break; | ||||
| 	case WLR_INPUT_DEVICE_TABLET_TOOL: | ||||
| 		tablet_tool_remove(device, state); | ||||
| 		break; | ||||
| 	case WLR_INPUT_DEVICE_TABLET_PAD: | ||||
| 		tablet_pad_remove(device, state); | ||||
| 		break; | ||||
| 	default: | ||||
| 		break; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 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_add_notify(struct wl_listener *listener, void *data) { | ||||
| 	struct wlr_output *output = data; | ||||
| 	struct compositor_state *state = wl_container_of(listener, state, output_add); | ||||
| 	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->frame.notify = output_frame_notify; | ||||
| 	ostate->resolution.notify = output_resolution_notify; | ||||
| 	wl_signal_add(&output->events.frame, &ostate->frame); | ||||
| 	wl_signal_add(&output->events.resolution, &ostate->resolution); | ||||
| 	wl_list_insert(&state->outputs, &ostate->link); | ||||
| 	if (state->output_add_cb) { | ||||
| 		state->output_add_cb(ostate); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| static void output_remove_notify(struct wl_listener *listener, void *data) { | ||||
| 	struct wlr_output *output = data; | ||||
| 	struct compositor_state *state = wl_container_of(listener, state, output_remove); | ||||
| 	struct output_state *ostate = NULL, *_ostate; | ||||
| 	wl_list_for_each(_ostate, &state->outputs, link) { | ||||
| 		if (_ostate->output == output) { | ||||
| 			ostate = _ostate; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 	if (!ostate) { | ||||
| 		return; // We are unfamiliar with this output
 | ||||
| 	} | ||||
| 	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); | ||||
| } | ||||
| 
 | ||||
| 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); | ||||
| 	state->input_add.notify = input_add_notify; | ||||
| 	state->input_remove.notify = input_remove_notify; | ||||
| 
 | ||||
| 	wl_list_init(&state->outputs); | ||||
| 	state->output_add.notify = output_add_notify; | ||||
| 	state->output_remove.notify = output_remove_notify; | ||||
| 
 | ||||
| 	struct wlr_backend *wlr = wlr_backend_autocreate(state->display); | ||||
| 	if (!wlr) { | ||||
| 		exit(1); | ||||
| 	} | ||||
| 	wl_signal_add(&wlr->events.input_add, &state->input_add); | ||||
| 	wl_signal_add(&wlr->events.input_remove, &state->input_remove); | ||||
| 	wl_signal_add(&wlr->events.output_add, &state->output_add); | ||||
| 	wl_signal_add(&wlr->events.output_remove, &state->output_remove); | ||||
| 	state->backend = wlr; | ||||
| 
 | ||||
| 	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) { | ||||
| 	wlr_backend_destroy(state->backend); | ||||
| 	wl_display_destroy(state->display); | ||||
| } | ||||
							
								
								
									
										139
									
								
								examples/support/shared.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										139
									
								
								examples/support/shared.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,139 @@ | |||
| #ifndef _EXAMPLE_SHARED_H | ||||
| #define _EXAMPLE_SHARED_H | ||||
| #ifndef _POSIX_C_SOURCE | ||||
| #define _POSIX_C_SOURCE 200112L | ||||
| #endif | ||||
| #include <time.h> | ||||
| #include <stdbool.h> | ||||
| #include <xkbcommon/xkbcommon.h> | ||||
| #include <wayland-server-protocol.h> | ||||
| #include <wlr/backend.h> | ||||
| #include <wlr/backend/session.h> | ||||
| #include <wlr/types/wlr_output.h> | ||||
| #include <wlr/types/wlr_input_device.h> | ||||
| 
 | ||||
| struct output_state { | ||||
| 	struct compositor_state *compositor; | ||||
| 	struct wlr_output *output; | ||||
| 	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 key; | ||||
| 	struct wl_list link; | ||||
| 	void *data; | ||||
| }; | ||||
| 
 | ||||
| struct pointer_state { | ||||
| 	struct compositor_state *compositor; | ||||
| 	struct wlr_input_device *device; | ||||
| 	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 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 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 button; | ||||
| 	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 slot, | ||||
| 		double x, double y, double width, double height); | ||||
| 	void (*touch_motion_cb)(struct touch_state *s, int32_t slot, | ||||
| 		double x, double y, double width, double height); | ||||
| 	void (*touch_up_cb)(struct touch_state *s, int32_t slot); | ||||
| 	void (*touch_cancel_cb)(struct touch_state *s, int32_t slot); | ||||
| 	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); | ||||
| 
 | ||||
| 	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 input_add; | ||||
| 	struct wl_listener input_remove; | ||||
| 
 | ||||
| 	struct timespec last_frame; | ||||
| 	struct wl_listener output_add; | ||||
| 	struct wl_listener output_remove; | ||||
| 	struct wl_list outputs; | ||||
| 
 | ||||
| 	void *data; | ||||
| }; | ||||
| 
 | ||||
| void compositor_init(struct compositor_state *state); | ||||
| void compositor_fini(struct compositor_state *state); | ||||
| 
 | ||||
| #endif | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Drew DeVault
						Drew DeVault