From d0cf8d0d01b12d359be5c4216ef5f0bdbdfee622 Mon Sep 17 00:00:00 2001
From: Tony Crisci <tony@dubstepdish.com>
Date: Fri, 25 Aug 2017 09:40:01 -0400
Subject: add device configuration

---
 examples/config.c                | 42 ++++++++++++++++++++++++++++--
 examples/config.h                |  7 +++++
 examples/pointer.c               | 55 +++++++++++++++++++++++++---------------
 examples/wlr-example.ini.example |  9 +++++++
 4 files changed, 91 insertions(+), 22 deletions(-)

diff --git a/examples/config.c b/examples/config.c
index 909a2463..31aaa6d7 100644
--- a/examples/config.c
+++ b/examples/config.c
@@ -22,6 +22,7 @@ static void usage(const char *name, int ret) {
 }
 
 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;
@@ -73,6 +74,32 @@ static int config_ini_handler(void *user, const char *section, const char *name,
 		} 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) {
+			if (dc->mapped_output) {
+				free(dc->mapped_output);
+			}
+			dc->mapped_output = strdup(value);
+		} else {
+			wlr_log(L_ERROR, "got unknown device config: %s", name);
+		}
 	} else {
 		wlr_log(L_ERROR, "got unknown config section: %s", section);
 	}
@@ -83,6 +110,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
 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) {
@@ -126,11 +154,21 @@ struct example_config *parse_args(int argc, char *argv[]) {
 }
 
 void example_config_destroy(struct example_config *config) {
-	struct output_config *oc, *tmp = NULL;
-	wl_list_for_each_safe(oc, tmp, &config->outputs, link) {
+	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);
+		}
+		free(dc);
+	}
+
 	if (config->config_path) {
 		free(config->config_path);
 	}
diff --git a/examples/config.h b/examples/config.h
index c5f0c8dd..e1765c57 100644
--- a/examples/config.h
+++ b/examples/config.h
@@ -12,12 +12,19 @@ struct output_config {
 	struct wl_list link;
 };
 
+struct device_config {
+	char *name;
+	char *mapped_output;
+	struct wl_list link;
+};
+
 struct example_config {
 	struct {
 		char *mapped_output;
 	} cursor;
 
 	struct wl_list outputs;
+	struct wl_list devices;
 	char *config_path;
 };
 
diff --git a/examples/pointer.c b/examples/pointer.c
index 88fdaef7..bec71dff 100644
--- a/examples/pointer.c
+++ b/examples/pointer.c
@@ -61,30 +61,50 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts
 	wlr_output_swap_buffers(wlr_output);
 }
 
+static void configure_devices(struct sample_state *sample) {
+	struct sample_input_device *dev;
+	// reset device to output mappings
+	wl_list_for_each(dev, &sample->devices, link) {
+		wlr_cursor_map_input_to_output(sample->cursor, dev->device, NULL);
+	}
+
+	struct output_state *ostate;
+	wl_list_for_each(ostate, &sample->compositor->outputs, link) {
+		struct device_config *dc;
+		wl_list_for_each(dc, &sample->config->devices, link) {
+			// configure device to output mappings
+			if (dc->mapped_output &&
+					strcmp(dc->mapped_output, ostate->output->name) == 0) {
+				wl_list_for_each(dev, &sample->devices, link) {
+					if (strcmp(dev->device->name, dc->name) == 0) {
+						wlr_cursor_map_input_to_output(sample->cursor,
+							dev->device, ostate->output);
+					}
+				}
+			}
+		}
+	}
+}
+
 static void handle_output_add(struct output_state *ostate) {
 	struct sample_state *sample = ostate->compositor->data;
 	struct wlr_output *wlr_output = ostate->output;
 	struct wlr_xcursor_image *image = sample->xcursor->images[0];
 
+	// reset layout
 	wlr_output_layout_destroy(sample->layout);
 	sample->layout = configure_layout(sample->config, &ostate->compositor->outputs);
 	wlr_cursor_attach_output_layout(sample->cursor, sample->layout);
 
+	// cursor configuration
 	char *mapped_output = sample->config->cursor.mapped_output;
 	if (mapped_output && strcmp(mapped_output, wlr_output->name) == 0) {
 		wlr_cursor_map_to_output(sample->cursor, wlr_output);
 	}
 
-	/*
-	// TODO configuration
-	if (strcmp("DP-1", ostate->output->name) == 0) {
-		struct sample_input_device *dev;
-		wl_list_for_each(dev, &sample->devices, link) {
-			wlr_cursor_map_input_to_output(sample->cursor, dev->device, ostate->output);
-		}
-	}
-	*/
+	configure_devices(sample);
 
+	// TODO move to wlr_cursor
 	if (!wlr_output_set_cursor(wlr_output, image->buffer,
 			image->width, image->width, image->height)) {
 		wlr_log(L_DEBUG, "Failed to set hardware cursor");
@@ -101,6 +121,8 @@ static void handle_output_remove(struct output_state *ostate) {
 	sample->layout = configure_layout(sample->config, &ostate->compositor->outputs);
 	wlr_cursor_attach_output_layout(sample->cursor, sample->layout);
 
+	configure_devices(sample);
+
 	if (strcmp(sample->config->cursor.mapped_output, ostate->output->name) == 0) {
 		wlr_cursor_map_to_output(sample->cursor, NULL);
 	}
@@ -122,19 +144,10 @@ static void handle_input_add(struct compositor_state *state, struct
 	if (device->type == WLR_INPUT_DEVICE_POINTER) {
 		struct sample_input_device *s_device = calloc(1, sizeof(struct sample_input_device));
 		s_device->device = device;
-		wl_list_insert(&sample->devices, &s_device->link);
-
-		/*
-		// TODO configuration
-		struct output_state *ostate;
-		wl_list_for_each(ostate, &sample->compositor->outputs, link) {
-			if (strcmp(ostate->output->name, "DP-1") == 0) {
-				wlr_cursor_map_input_to_output(sample->cursor, device, ostate->output);
-			}
-		}
-		*/
 
+		wl_list_insert(&sample->devices, &s_device->link);
 		wlr_cursor_attach_input_device(sample->cursor, device);
+		configure_devices(sample);
 	}
 }
 
@@ -224,6 +237,8 @@ int main(int argc, char *argv[]) {
 	compositor.output_resolution_cb = handle_output_resolution;
 	compositor.output_frame_cb = handle_output_frame;
 	compositor.input_add_cb = handle_input_add;
+	// TODO input_remove_cb
+	//compositor.input_remove_cb = handle_input_add;
 
 	state.compositor = &compositor;
 
diff --git a/examples/wlr-example.ini.example b/examples/wlr-example.ini.example
index a9dff591..ffb6229e 100644
--- a/examples/wlr-example.ini.example
+++ b/examples/wlr-example.ini.example
@@ -37,4 +37,13 @@ y=232
 [cursor]
 map-to-output=HDMI-A-1
 
+# 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.
+[device:Razer Razer DeathAdder 2013]
+map-to-output=DP-1
+
 # vim:filetype=dosini
-- 
cgit v1.2.3