commit
6daf9e9ab2
@ -0,0 +1,303 @@
|
||||
#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 `examples/wlr-example.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 wlr_output_layout *configure_layout(struct example_config *config,
|
||||
struct wl_list *outputs) {
|
||||
struct wlr_output_layout *layout = wlr_output_layout_init();
|
||||
int max_x = INT_MIN;
|
||||
int max_x_y = INT_MIN; // y value for the max_x output
|
||||
|
||||
// first add all the configured outputs
|
||||
struct output_state *output;
|
||||
wl_list_for_each(output, outputs, link) {
|
||||
struct output_config *conf;
|
||||
wl_list_for_each(conf, &config->outputs, link) {
|
||||
if (strcmp(conf->name, output->output->name) == 0) {
|
||||
wlr_output_layout_add(layout, output->output,
|
||||
conf->x, conf->y);
|
||||
wlr_output_transform(output->output, conf->transform);
|
||||
int width, height;
|
||||
wlr_output_effective_resolution(output->output, &width,
|
||||
&height);
|
||||
if (conf->x + width > max_x) {
|
||||
max_x = conf->x + width;
|
||||
max_x_y = conf->y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (max_x == INT_MIN) {
|
||||
// couldn't find a configured output
|
||||
max_x = 0;
|
||||
max_x_y = 0;
|
||||
}
|
||||
|
||||
// now add all the other configured outputs in a sensible position
|
||||
wl_list_for_each(output, outputs, link) {
|
||||
if (wlr_output_layout_get(layout, output->output)) {
|
||||
continue;
|
||||
}
|
||||
wlr_output_layout_add(layout, output->output, max_x, max_x_y);
|
||||
int width, height;
|
||||
wlr_output_effective_resolution(output->output, &width, &height);
|
||||
max_x += width;
|
||||
}
|
||||
|
||||
return layout;
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
#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>
|
||||
|
||||
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);
|
||||
|
||||
struct wlr_output_layout *configure_layout(struct example_config *config,
|
||||
struct wl_list *outputs);
|
||||
#endif
|
@ -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;
|
||||
}
|
@ -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__ */
|
@ -0,0 +1,57 @@
|
||||
# Configuration
|
||||
# -------------
|
||||
# Some examples will read a configuration file. Not all examples will use all of
|
||||
# the configuration options. The configuration file will be loaded from
|
||||
# `wlr-example.ini` from the current directory or the path can be specified by the
|
||||
# `-C` option given on the command line.
|
||||
#
|
||||
# Output configuration
|
||||
# ~~~~~~~~~~~~~~~~~~~~
|
||||
# Each output is specified in a section named [output:{NAME}] where NAME is the
|
||||
# drm name for this output.
|
||||
#
|
||||
# Value "x" specifies the x-coordinate in the output layout.
|
||||
#
|
||||
# Value "y" specifies the y-coordinate in the output layout.
|
||||
#
|
||||
# Value "rotate" specifies output rotation and can be 90, 180, 270, flipped,
|
||||
# flipped-90, flipped-180, or flipped-270
|
||||
[output:HDMI-A-1]
|
||||
x=3000
|
||||
y=0
|
||||
rotate=90
|
||||
|
||||
[output:DP-1]
|
||||
x=0
|
||||
y=0
|
||||
rotate=270
|
||||
|
||||
[output:DVI-D-1]
|
||||
x=1080
|
||||
y=232
|
||||
|
||||
# Cursor Configuration
|
||||
# ~~~~~~~~~~~~~~~~~~~~
|
||||
# Value "map-to-output" specifies the output to which the cursor is
|
||||
# constrained.
|
||||
#
|
||||
# Value "geometry" specifies the geometry (widthxheight+x+y) to which the cursor
|
||||
# is constrained.
|
||||
[cursor]
|
||||
map-to-output=HDMI-A-1
|
||||
geometry=500x700+50+50
|
||||
|
||||
# Device Configuration
|
||||
# ~~~~~~~~~~~~~~~~~~~~
|
||||
# Each device is specified in a section named [device:{NAME}] where NAME is the
|
||||
# name given to this device. See a log file for device names.
|
||||
#
|
||||
# Value "map-to-output" specifies the output to which the device is constrained.
|
||||
#
|
||||
# Value "geometry" specifies the geometry (widthxheight+x+y) to which the device
|
||||
# is constrained.
|
||||
[device:Razer Razer DeathAdder 2013]
|
||||
map-to-output=DP-1
|
||||
geometry=500x700+50+50
|
||||
|
||||
# vim:filetype=dosini
|
@ -0,0 +1,20 @@
|
||||
#ifndef _WLR_TYPES_GEOMETRY_H
|
||||
#define _WLR_TYPES_GEOMETRY_H
|
||||
#include <stdbool.h>
|
||||
|
||||
struct wlr_box {
|
||||
int x, y;
|
||||
int width, height;
|
||||
};
|
||||
|
||||
void wlr_box_closest_point(struct wlr_box *box, double x, double y,
|
||||
double *dest_x, double *dest_y);
|
||||
|
||||
bool wlr_box_intersection(struct wlr_box *box_a,
|
||||
struct wlr_box *box_b, struct wlr_box **dest);
|
||||
|
||||
bool wlr_box_contains_point(struct wlr_box *box, double x, double y);
|
||||
|
||||
bool wlr_box_empty(struct wlr_box *box);
|
||||
|
||||
#endif
|
@ -0,0 +1,112 @@
|
||||
#ifndef _WLR_TYPES_CURSOR_H
|
||||
#define _WLR_TYPES_CURSOR_H
|
||||
#include <wayland-server.h>
|
||||
#include <wlr/types/wlr_output.h>
|
||||
#include <wlr/types/wlr_output_layout.h>
|
||||
#include <wlr/types/wlr_input_device.h>
|
||||
#include <wlr/types/wlr_box.h>
|
||||
#include <wlr/xcursor.h>
|
||||
|
||||
struct wlr_cursor_state;
|
||||
|
||||
struct wlr_cursor {
|
||||
struct wlr_cursor_state *state;
|
||||
int x, y;
|
||||
|
||||
struct {
|
||||
struct wl_signal motion;
|
||||
struct wl_signal motion_absolute;
|
||||
struct wl_signal button;
|
||||
struct wl_signal axis;
|
||||
|
||||
struct wl_signal touch_up;
|
||||
struct wl_signal touch_down;
|
||||
struct wl_signal touch_motion;
|
||||
struct wl_signal touch_cancel;
|
||||
|
||||
struct wl_signal tablet_tool_axis;
|
||||
struct wl_signal tablet_tool_proximity;
|
||||
struct wl_signal tablet_tool_tip;
|
||||
struct wl_signal tablet_tool_button;
|
||||
} events;
|
||||
};
|
||||
|
||||
struct wlr_cursor *wlr_cursor_create();
|
||||
|
||||
void wlr_cursor_destroy(struct wlr_cursor *cur);
|
||||
|
||||
void wlr_cursor_set_xcursor(struct wlr_cursor *cur, struct wlr_xcursor *xcur);
|
||||
|
||||
/**
|
||||
* Warp the cursor to the given x and y in layout coordinates. If x and y are
|
||||
* out of the layout boundaries or constraints, no warp will happen.
|
||||
*
|
||||
* `dev` may be passed to respect device mapping constraints. If `dev` is NULL,
|
||||
* device mapping constraints will be ignored.
|
||||
*
|
||||
* Returns true when the mouse warp was successful.
|
||||
*/
|
||||
bool wlr_cursor_warp(struct wlr_cursor *cur, struct wlr_input_device *dev,
|
||||
double x, double y);
|
||||
|
||||
void wlr_cursor_warp_absolute(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev, double x_mm, double y_mm);
|
||||
|
||||
/**
|
||||
* Move the cursor in the direction of the given x and y coordinates.
|
||||
*
|
||||
* `dev` may be passed to respect device mapping constraints. If `dev` is NULL,
|
||||
* device mapping constraints will be ignored.
|
||||
*/
|
||||
void wlr_cursor_move(struct wlr_cursor *cur, struct wlr_input_device *dev,
|
||||
double delta_x, double delta_y);
|
||||
|
||||
/**
|
||||
* Attaches this input device to this cursor. The input device must be one of:
|
||||
*
|
||||
* - WLR_INPUT_DEVICE_POINTER
|
||||
* - WLR_INPUT_DEVICE_TOUCH
|
||||
* - WLR_INPUT_DEVICE_TABLET_TOOL
|
||||
*/
|
||||
void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev);
|
||||
|
||||
void wlr_cursor_detach_input_device(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev);
|
||||
/**
|
||||
* Uses the given layout to establish the boundaries and movement semantics of
|
||||
* this cursor. Cursors without an output layout allow infinite movement in any
|
||||
* direction and do not support absolute input events.
|
||||
*/
|
||||
void wlr_cursor_attach_output_layout(struct wlr_cursor *cur,
|
||||
struct wlr_output_layout *l);
|
||||
|
||||
/**
|
||||
* Attaches this cursor to the given output, which must be among the outputs in
|
||||
* the current output_layout for this cursor. This call is invalid for a cursor
|
||||
* without an associated output layout.
|
||||
*/
|
||||
void wlr_cursor_map_to_output(struct wlr_cursor *cur,
|
||||
struct wlr_output *output);
|
||||
|
||||
/**
|
||||
* Maps all input from a specific input device to a given output. The input
|
||||
* device must be attached to this cursor and the output must be among the
|
||||
* outputs in the attached output layout.
|
||||
*/
|
||||
void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev, struct wlr_output *output);
|
||||
|
||||
/**
|
||||
* Maps this cursor to an arbitrary region on the associated wlr_output_layout.
|
||||
*/
|
||||
void wlr_cursor_map_to_region(struct wlr_cursor *cur, struct wlr_box *box);
|
||||
|
||||
/**
|
||||
* Maps inputs from this input device to an arbitrary region on the associated
|
||||
* wlr_output_layout.
|
||||
*/
|
||||
void wlr_cursor_map_input_to_region(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev, struct wlr_box *box);
|
||||
|
||||
#endif
|
@ -0,0 +1,67 @@
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <wlr/types/wlr_box.h>
|
||||
#include <wlr/util/log.h>
|
||||
|
||||
void wlr_box_closest_point(struct wlr_box *box, double x, double y,
|
||||
double *dest_x, double *dest_y) {
|
||||
// find the closest x point
|
||||
if (x < box->x) {
|
||||
*dest_x = box->x;
|
||||
} else if (x > box->x + box->width) {
|
||||
*dest_x = box->x + box->width;
|
||||
} else {
|
||||
*dest_x = x;
|
||||
}
|
||||
|
||||
// find closest y point
|
||||
if (y < box->y) {
|
||||
*dest_y = box->y;
|
||||
} else if (y > box->y + box->height) {
|
||||
*dest_y = box->y + box->height;
|
||||
} else {
|
||||
*dest_y = y;
|
||||
}
|
||||
}
|
||||
|
||||
bool wlr_box_empty(struct wlr_box *box) {
|
||||
return box == NULL || box->width <= 0 || box->height <= 0;
|
||||
}
|
||||
|
||||
bool wlr_box_intersection(struct wlr_box *box_a,
|
||||
struct wlr_box *box_b, struct wlr_box **box_dest) {
|
||||
struct wlr_box *dest = *box_dest;
|
||||
bool a_empty = wlr_box_empty(box_a);
|
||||
bool b_empty = wlr_box_empty(box_b);
|
||||
|
||||
if (a_empty || b_empty) {
|
||||
dest->x = 0;
|
||||
dest->y = 0;
|
||||
dest->width = -100;
|
||||
dest->height = -100;
|
||||
return false;
|
||||
}
|
||||
|
||||
int x1 = fmax(box_a->x, box_b->x);
|
||||
int y1 = fmax(box_a->y, box_b->y);
|
||||
int x2 = fmin(box_a->x + box_a->width, box_b->x + box_b->width);
|
||||
int y2 = fmin(box_a->y + box_a->height, box_b->y + box_b->height);
|
||||
|
||||
dest->x = x1;
|
||||
dest->y = y1;
|
||||
dest->width = x2 - x1;
|
||||
dest->height = y2 - y1;
|
||||
|
||||
return !wlr_box_empty(dest);
|
||||
}
|
||||
|
||||
bool wlr_box_contains_point(struct wlr_box *box, double x, double y) {
|
||||
if (wlr_box_empty(box)) {
|
||||
return false;
|
||||
} else {
|
||||
return x >= box->x && x <= box->x + box->width &&
|
||||
y >= box->y && y <= box->y + box->height;
|
||||
}
|
||||
}
|
@ -0,0 +1,481 @@
|
||||
#include <wlr/types/wlr_cursor.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <wlr/util/log.h>
|
||||
#include <wayland-server.h>
|
||||
#include <wlr/types/wlr_output.h>
|
||||
#include <wlr/types/wlr_output_layout.h>
|
||||
#include <wlr/types/wlr_input_device.h>
|
||||
|
||||
struct wlr_cursor_device {
|
||||
struct wlr_cursor *cursor;
|
||||
struct wlr_input_device *device;
|
||||
struct wl_list link;
|
||||
struct wlr_output *mapped_output;
|
||||
struct wlr_box *mapped_box;
|
||||
|
||||
struct wl_listener motion;
|
||||
struct wl_listener motion_absolute;
|
||||
struct wl_listener button;
|
||||
struct wl_listener axis;
|
||||
|
||||
struct wl_listener touch_down;
|
||||
struct wl_listener touch_up;
|
||||
struct wl_listener touch_motion;
|
||||
struct wl_listener touch_cancel;
|
||||
|
||||
struct wl_listener tablet_tool_axis;
|
||||
struct wl_listener tablet_tool_proximity;
|
||||
struct wl_listener tablet_tool_tip;
|
||||
struct wl_listener tablet_tool_button;
|
||||
|
||||
struct wl_listener destroy;
|
||||
};
|
||||
|
||||
struct wlr_cursor_state {
|
||||
struct wl_list devices;
|
||||
struct wlr_output_layout *layout;
|
||||
struct wlr_xcursor *xcursor;
|
||||
struct wlr_output *mapped_output;
|
||||
struct wlr_box *mapped_box;
|
||||
};
|
||||
|
||||
struct wlr_cursor *wlr_cursor_create() {
|
||||
struct wlr_cursor *cur = calloc(1, sizeof(struct wlr_cursor));
|
||||
if (!cur) {
|
||||
wlr_log(L_ERROR, "Failed to allocate wlr_cursor");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cur->state = calloc(1, sizeof(struct wlr_cursor_state));
|
||||
if (!cur->state) {
|
||||
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_state");
|
||||
free(cur);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cur->state->mapped_output = NULL;
|
||||
|
||||
wl_list_init(&cur->state->devices);
|
||||
|
||||
// pointer signals
|
||||
wl_signal_init(&cur->events.motion);
|
||||
wl_signal_init(&cur->events.motion_absolute);
|
||||
wl_signal_init(&cur->events.button);
|
||||
wl_signal_init(&cur->events.axis);
|
||||
|
||||
// touch signals
|
||||
wl_signal_init(&cur->events.touch_up);
|
||||
wl_signal_init(&cur->events.touch_down);
|
||||
wl_signal_init(&cur->events.touch_motion);
|
||||
wl_signal_init(&cur->events.touch_cancel);
|
||||
|
||||
// tablet tool signals
|
||||
wl_signal_init(&cur->events.tablet_tool_tip);
|
||||
wl_signal_init(&cur->events.tablet_tool_axis);
|
||||
wl_signal_init(&cur->events.tablet_tool_button);
|
||||
wl_signal_init(&cur->events.tablet_tool_proximity);
|
||||
|
||||
cur->x = 100;
|
||||
cur->y = 100;
|
||||
|
||||
return cur;
|
||||
}
|
||||
|
||||
void wlr_cursor_destroy(struct wlr_cursor *cur) {
|
||||
struct wlr_cursor_device *device, *device_tmp = NULL;
|
||||
wl_list_for_each_safe(device, device_tmp, &cur->state->devices, link) {
|
||||
wl_list_remove(&device->link);
|
||||
free(device);
|
||||
}
|
||||
|
||||
free(cur);
|
||||
}
|
||||
|
||||
void wlr_cursor_set_xcursor(struct wlr_cursor *cur, struct wlr_xcursor *xcur) {
|
||||
cur->state->xcursor = xcur;
|
||||
}
|
||||
|
||||
static struct wlr_cursor_device *get_cursor_device(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *device) {
|
||||
struct wlr_cursor_device *c_device, *ret = NULL;
|
||||
wl_list_for_each(c_device, &cur->state->devices, link) {
|
||||
if (c_device->device == device) {
|
||||
ret = c_device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void wlr_cursor_warp_unchecked(struct wlr_cursor *cur,
|
||||
double x, double y) {
|
||||
assert(cur->state->layout);
|
||||
int hotspot_x = 0;
|
||||
int hotspot_y = 0;
|
||||
|
||||
if (cur->state->xcursor && cur->state->xcursor->image_count > 0) {
|
||||
struct wlr_xcursor_image *image = cur->state->xcursor->images[0];
|
||||
hotspot_x = image->hotspot_x;
|
||||
hotspot_y = image->hotspot_y;
|
||||
}
|
||||
|
||||
|
||||
struct wlr_output_layout_output *l_output;
|
||||
wl_list_for_each(l_output, &cur->state->layout->outputs, link) {
|
||||
double output_x = x;
|
||||
double output_y = y;
|
||||
|
||||
wlr_output_layout_output_coords(cur->state->layout,
|
||||
l_output->output, &output_x, &output_y);
|
||||
wlr_output_move_cursor(l_output->output, output_x - hotspot_x,
|
||||
output_y - hotspot_y);
|
||||
}
|
||||
|
||||
cur->x = x;
|
||||
cur->y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the most specific mapping box for the device in this order:
|
||||
*
|
||||
* 1. device geometry mapping
|
||||
* 2. device output mapping
|
||||
* 3. cursor geometry mapping
|
||||
* 4. cursor output mapping
|
||||
*
|
||||
* Absolute movement for touch and pen devices will be relative to this box and
|
||||
* pointer movement will be constrained to this box.
|
||||
*
|
||||
* If none of these are set, returns NULL and absolute movement should be
|
||||
* relative to the extents of the layout.
|
||||
*/
|
||||
static struct wlr_box *get_mapping(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev) {
|
||||
assert(cur->state->layout);
|
||||
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
|
||||
|
||||
if (c_device) {
|
||||
if (c_device->mapped_box) {
|
||||
return c_device->mapped_box;
|
||||
}
|
||||
if (c_device->mapped_output) {
|
||||
return wlr_output_layout_get_box(cur->state->layout,
|
||||
c_device->mapped_output);
|
||||
}
|
||||
}
|
||||
|
||||
if (cur->state->mapped_box) {
|
||||
return cur->state->mapped_box;
|
||||
}
|
||||
if (cur->state->mapped_output) {
|
||||
return wlr_output_layout_get_box(cur->state->layout,
|
||||
cur->state->mapped_output);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool wlr_cursor_warp(struct wlr_cursor *cur, struct wlr_input_device *dev,
|
||||
double x, double y) {
|
||||
assert(cur->state->layout);
|
||||
bool result = false;
|
||||
|
||||
struct wlr_box *mapping = get_mapping(cur, dev);
|
||||
|
||||
if (mapping) {
|
||||
if (wlr_box_contains_point(mapping, x, y)) {
|
||||
wlr_cursor_warp_unchecked(cur, x, y);
|
||||
result = true;
|
||||
}
|
||||
} else if (wlr_output_layout_contains_point(cur->state->layout, NULL,
|
||||
x, y)) {
|
||||
wlr_cursor_warp_unchecked(cur, x, y);
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wlr_cursor_warp_absolute(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev, double x_mm, double y_mm) {
|
||||
assert(cur->state->layout);
|
||||
|
||||
struct wlr_box *mapping = get_mapping(cur, dev);
|
||||
if (!mapping) {
|
||||
mapping = wlr_output_layout_get_box(cur->state->layout, NULL);
|
||||
}
|
||||
|
||||
double x = mapping->width * x_mm + mapping->x;
|
||||
double y = mapping->height * y_mm + mapping->y;
|
||||
|
||||
wlr_cursor_warp_unchecked(cur, x, y);
|
||||
}
|
||||
|
||||
void wlr_cursor_move(struct wlr_cursor *cur, struct wlr_input_device *dev,
|
||||
double delta_x, double delta_y) {
|
||||
assert(cur->state->layout);
|
||||
|
||||
double x = cur->x + delta_x;
|
||||
double y = cur->y + delta_y;
|
||||
|
||||
struct wlr_box *mapping = get_mapping(cur, dev);
|
||||
|
||||
if (mapping) {
|
||||
double closest_x, closest_y;
|
||||
if (!wlr_box_contains_point(mapping, x, y)) {
|
||||
wlr_box_closest_point(mapping, x, y, &closest_x,
|
||||
&closest_y);
|
||||
x = closest_x;
|
||||
y = closest_y;
|
||||
}
|
||||
} else {
|
||||
if (!wlr_output_layout_contains_point(cur->state->layout, NULL, x, y)) {
|
||||
double layout_x, layout_y;
|
||||
wlr_output_layout_closest_point(cur->state->layout, NULL, x, y,
|
||||
&layout_x, &layout_y);
|
||||
x = layout_x;
|
||||
y = layout_y;
|
||||
}
|
||||
}
|
||||
|
||||
wlr_cursor_warp_unchecked(cur, x, y);
|
||||
}
|
||||
|
||||
static void handle_pointer_motion(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_pointer_motion *event = data;
|
||||
struct wlr_cursor_device *device =
|
||||
wl_container_of(listener, device, motion);
|
||||
wl_signal_emit(&device->cursor->events.motion, event);
|
||||
}
|
||||
|
||||
static void handle_pointer_motion_absolute(struct wl_listener *listener,
|
||||
void *data) {
|
||||
struct wlr_event_pointer_motion_absolute *event = data;
|
||||
struct wlr_cursor_device *device =
|
||||
wl_container_of(listener, device, motion_absolute);
|
||||
wl_signal_emit(&device->cursor->events.motion_absolute, event);
|
||||
}
|
||||
|
||||
static void handle_pointer_button(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_pointer_button *event = data;
|
||||
struct wlr_cursor_device *device =
|
||||
wl_container_of(listener, device, button);
|
||||
wl_signal_emit(&device->cursor->events.button, event);
|
||||
}
|
||||
|
||||
static void handle_pointer_axis(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_pointer_axis *event = data;
|
||||
struct wlr_cursor_device *device = wl_container_of(listener, device, axis);
|
||||
wl_signal_emit(&device->cursor->events.axis, event);
|
||||
}
|
||||
|
||||
static void handle_touch_up(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_touch_up *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, touch_up);
|
||||
wl_signal_emit(&device->cursor->events.touch_up, event);
|
||||
}
|
||||
|
||||
static void handle_touch_down(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_touch_down *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, touch_down);
|
||||
wl_signal_emit(&device->cursor->events.touch_down, event);
|
||||
}
|
||||
|
||||
static void handle_touch_motion(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_touch_motion *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, touch_motion);
|
||||
wl_signal_emit(&device->cursor->events.touch_motion, event);
|
||||
}
|
||||
|
||||
static void handle_touch_cancel(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_touch_cancel *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, touch_cancel);
|
||||
wl_signal_emit(&device->cursor->events.touch_cancel, event);
|
||||
}
|
||||
|
||||
static void handle_tablet_tool_tip(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_tablet_tool_tip *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, tablet_tool_tip);
|
||||
wl_signal_emit(&device->cursor->events.tablet_tool_tip, event);
|
||||
}
|
||||
|
||||
static void handle_tablet_tool_axis(struct wl_listener *listener, void *data) {
|
||||
struct wlr_event_tablet_tool_axis *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, tablet_tool_axis);
|
||||
wl_signal_emit(&device->cursor->events.tablet_tool_axis, event);
|
||||
}
|
||||
|
||||
static void handle_tablet_tool_button(struct wl_listener *listener,
|
||||
void *data) {
|
||||
struct wlr_event_tablet_tool_button *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, tablet_tool_button);
|
||||
wl_signal_emit(&device->cursor->events.tablet_tool_button, event);
|
||||
}
|
||||
|
||||
static void handle_tablet_tool_proximity(struct wl_listener *listener,
|
||||
void *data) {
|
||||
struct wlr_event_tablet_tool_proximity *event = data;
|
||||
struct wlr_cursor_device *device;
|
||||
device = wl_container_of(listener, device, tablet_tool_proximity);
|
||||
wl_signal_emit(&device->cursor->events.tablet_tool_proximity, event);
|
||||
}
|
||||
|
||||
static void handle_device_destroy(struct wl_listener *listener, void *data) {
|
||||
struct wlr_cursor_device *c_device;
|
||||
c_device = wl_container_of(listener, c_device, destroy);
|
||||
wlr_cursor_detach_input_device(c_device->cursor, c_device->device);
|
||||
}
|
||||
|
||||
void wlr_cursor_attach_input_device(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev) {
|
||||
if (dev->type != WLR_INPUT_DEVICE_POINTER &&
|
||||
dev->type != WLR_INPUT_DEVICE_TOUCH &&
|
||||
dev->type != WLR_INPUT_DEVICE_TABLET_TOOL) {
|
||||
wlr_log(L_ERROR, "only device types of pointer, touch or tablet tool"
|
||||
"are supported");
|
||||
return;
|
||||
}
|
||||
|
||||
// make sure it is not already attached
|
||||
struct wlr_cursor_device *_dev;
|
||||
wl_list_for_each(_dev, &cur->state->devices, link) {
|
||||
if (_dev->device == dev) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
struct wlr_cursor_device *device;
|
||||
device = calloc(1, sizeof(struct wlr_cursor_device));
|
||||
if (!device) {
|
||||
wlr_log(L_ERROR, "Failed to allocate wlr_cursor_device");
|
||||
return;
|
||||
}
|
||||
|
||||
device->cursor = cur;
|
||||
device->device = dev;
|
||||
|
||||
// listen to events
|
||||
|
||||
wl_signal_add(&dev->events.destroy, &device->destroy);
|
||||
device->destroy.notify = handle_device_destroy;
|
||||
|
||||
if (dev->type == WLR_INPUT_DEVICE_POINTER) {
|
||||
wl_signal_add(&dev->pointer->events.motion, &device->motion);
|
||||
device->motion.notify = handle_pointer_motion;
|
||||
|
||||
wl_signal_add(&dev->pointer->events.motion_absolute,
|
||||
&device->motion_absolute);
|
||||
device->motion_absolute.notify = handle_pointer_motion_absolute;
|
||||
|
||||
wl_signal_add(&dev->pointer->events.button, &device->button);
|
||||
device->button.notify = handle_pointer_button;
|
||||
|
||||
wl_signal_add(&dev->pointer->events.axis, &device->axis);
|
||||
device->axis.notify = handle_pointer_axis;
|
||||
} else if (dev->type == WLR_INPUT_DEVICE_TOUCH) {
|
||||
wl_signal_add(&dev->touch->events.motion, &device->touch_motion);
|
||||
device->touch_motion.notify = handle_touch_motion;
|
||||
|
||||
wl_signal_add(&dev->touch->events.down, &device->touch_down);
|
||||
device->touch_down.notify = handle_touch_down;
|
||||
|
||||
wl_signal_add(&dev->touch->events.up, &device->touch_up);
|
||||
device->touch_up.notify = handle_touch_up;
|
||||
|
||||
wl_signal_add(&dev->touch->events.cancel, &device->touch_cancel);
|
||||
device->touch_cancel.notify = handle_touch_cancel;
|
||||
} else if (dev->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
|
||||
wl_signal_add(&dev->tablet_tool->events.tip, &device->tablet_tool_tip);
|
||||
device->tablet_tool_tip.notify = handle_tablet_tool_tip;
|
||||
|
||||
wl_signal_add(&dev->tablet_tool->events.proximity,
|
||||
&device->tablet_tool_proximity);
|
||||
device->tablet_tool_proximity.notify = handle_tablet_tool_proximity;
|
||||
|
||||
wl_signal_add(&dev->tablet_tool->events.axis,
|
||||
&device->tablet_tool_axis);
|
||||
device->tablet_tool_axis.notify = handle_tablet_tool_axis;
|
||||
|
||||
wl_signal_add(&dev->tablet_tool->events.button,
|
||||
&device->tablet_tool_button);
|
||||
device->tablet_tool_button.notify = handle_tablet_tool_button;
|
||||
}
|
||||
|
||||
wl_list_insert(&cur->state->devices, &device->link);
|
||||
}
|
||||
|
||||
void wlr_cursor_detach_input_device(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev) {
|
||||
struct wlr_cursor_device *target_device = NULL, *_device = NULL;
|
||||
wl_list_for_each(_device, &cur->state->devices, link) {
|
||||
if (_device->device == dev) {
|
||||
target_device = _device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (target_device) {
|
||||
wl_list_remove(&target_device->link);
|
||||
free(target_device);
|
||||
}
|
||||
}
|
||||
|
||||
void wlr_cursor_attach_output_layout(struct wlr_cursor *cur,
|
||||
struct wlr_output_layout *l) {
|
||||
cur->state->layout = l;
|
||||
}
|
||||
|
||||
void wlr_cursor_map_to_output(struct wlr_cursor *cur,
|
||||
struct wlr_output *output) {
|
||||
cur->state->mapped_output = output;
|
||||
}
|
||||
|
||||
void wlr_cursor_map_input_to_output(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev, struct wlr_output *output) {
|
||||
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
|
||||
if (!c_device) {
|
||||
wlr_log(L_ERROR, "Cannot map device \"%s\" to output"
|
||||
"(not found in this cursor)", dev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
c_device->mapped_output = output;
|
||||
}
|
||||
|
||||
void wlr_cursor_map_to_region(struct wlr_cursor *cur,
|
||||
struct wlr_box *box) {
|
||||
if (box && wlr_box_empty(box)) {
|
||||
wlr_log(L_ERROR, "cannot map cursor to an empty region");
|
||||
return;
|
||||
}
|
||||
|
||||
cur->state->mapped_box = box;
|
||||
}
|
||||
|
||||
void wlr_cursor_map_input_to_region(struct wlr_cursor *cur,
|
||||
struct wlr_input_device *dev, struct wlr_box *box) {
|
||||
if (box && wlr_box_empty(box)) {
|
||||
wlr_log(L_ERROR, "cannot map device \"%s\" input to an empty region",
|
||||
dev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
struct wlr_cursor_device *c_device = get_cursor_device(cur, dev);
|
||||
if (!c_device) {
|
||||
wlr_log(L_ERROR, "Cannot map device \"%s\" to geometry (not found in"
|
||||
"this cursor)", dev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
c_device->mapped_box = box;
|
||||
}
|
Loading…
Reference in new issue