diff options
author | Tony Crisci <tony@dubstepdish.com> | 2017-12-12 08:29:37 -0500 |
---|---|---|
committer | Tony Crisci <tony@dubstepdish.com> | 2017-12-12 08:29:37 -0500 |
commit | 163edc5a900fda58e006ed30e14ae10cc4aa13b3 (patch) | |
tree | a43e355091da4545bf9f16c63accb7d853170195 /sway/input/input-manager.c | |
parent | input config (diff) | |
download | sway-163edc5a900fda58e006ed30e14ae10cc4aa13b3.tar.gz sway-163edc5a900fda58e006ed30e14ae10cc4aa13b3.tar.zst sway-163edc5a900fda58e006ed30e14ae10cc4aa13b3.zip |
sway input device
Diffstat (limited to 'sway/input/input-manager.c')
-rw-r--r-- | sway/input/input-manager.c | 185 |
1 files changed, 120 insertions, 65 deletions
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index ca80f267..b7f5615c 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c | |||
@@ -14,23 +14,70 @@ | |||
14 | 14 | ||
15 | static const char *default_seat = "seat0"; | 15 | static const char *default_seat = "seat0"; |
16 | 16 | ||
17 | // TODO make me not global | ||
18 | struct sway_input_manager *input_manager; | ||
19 | |||
17 | struct input_config *current_input_config = NULL; | 20 | struct input_config *current_input_config = NULL; |
18 | 21 | ||
19 | static struct sway_seat *input_manager_get_seat( | 22 | static struct sway_seat *input_manager_get_seat( |
20 | struct sway_input_manager *input, const char *seat_name) { | 23 | struct sway_input_manager *input, const char *seat_name) { |
21 | struct sway_seat *seat = NULL; | 24 | struct sway_seat *seat = NULL; |
22 | 25 | wl_list_for_each(seat, &input->seats, link) { | |
23 | for (int i = 0; i < input->seats->length; ++i) { | ||
24 | seat = input->seats->items[i]; | ||
25 | if (strcmp(seat->seat->name, seat_name) == 0) { | 26 | if (strcmp(seat->seat->name, seat_name) == 0) { |
26 | return seat; | 27 | return seat; |
27 | } | 28 | } |
28 | } | 29 | } |
29 | 30 | ||
30 | seat = sway_seat_create(input, seat_name); | 31 | return sway_seat_create(input, seat_name); |
31 | list_add(input->seats, seat); | 32 | } |
33 | |||
34 | static char *get_device_identifier(struct wlr_input_device *device) { | ||
35 | int vendor = device->vendor; | ||
36 | int product = device->product; | ||
37 | char *name = strdup(device->name); | ||
38 | |||
39 | char *p = name; | ||
40 | for (; *p; ++p) { | ||
41 | if (*p == ' ') { | ||
42 | *p = '_'; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | sway_log(L_DEBUG, "rewritten name %s", name); | ||
47 | |||
48 | int len = strlen(name) + sizeof(char) * 6; | ||
49 | char *identifier = malloc(len); | ||
50 | if (!identifier) { | ||
51 | sway_log(L_ERROR, "Unable to allocate unique input device name"); | ||
52 | return NULL; | ||
53 | } | ||
54 | |||
55 | const char *fmt = "%d:%d:%s"; | ||
56 | snprintf(identifier, len, fmt, vendor, product, name); | ||
57 | free(name); | ||
58 | return identifier; | ||
59 | } | ||
32 | 60 | ||
33 | return seat; | 61 | static struct sway_input_device *input_sway_device_from_wlr(struct sway_input_manager *input, |
62 | struct wlr_input_device *device) { | ||
63 | struct sway_input_device *sway_device = NULL; | ||
64 | wl_list_for_each(sway_device, &input->devices, link) { | ||
65 | if (sway_device->wlr_device == device) { | ||
66 | return sway_device; | ||
67 | } | ||
68 | } | ||
69 | return NULL; | ||
70 | } | ||
71 | |||
72 | static struct sway_input_device *input_sway_device_from_config(struct sway_input_manager *input, | ||
73 | struct input_config *config) { | ||
74 | struct sway_input_device *sway_device = NULL; | ||
75 | wl_list_for_each(sway_device, &input->devices, link) { | ||
76 | if (strcmp(sway_device->identifier, config->identifier) == 0) { | ||
77 | return sway_device; | ||
78 | } | ||
79 | } | ||
80 | return NULL; | ||
34 | } | 81 | } |
35 | 82 | ||
36 | static void input_add_notify(struct wl_listener *listener, void *data) { | 83 | static void input_add_notify(struct wl_listener *listener, void *data) { |
@@ -38,9 +85,27 @@ static void input_add_notify(struct wl_listener *listener, void *data) { | |||
38 | wl_container_of(listener, input, input_add); | 85 | wl_container_of(listener, input, input_add); |
39 | struct wlr_input_device *device = data; | 86 | struct wlr_input_device *device = data; |
40 | 87 | ||
41 | // TODO device configuration | 88 | struct sway_input_device *sway_device = |
89 | calloc(1, sizeof(struct sway_input_device)); | ||
90 | if (!sway_assert(sway_device, "could not allocate input device")) { | ||
91 | return; | ||
92 | } | ||
93 | |||
94 | sway_device->wlr_device = device; | ||
95 | sway_device->identifier = get_device_identifier(device); | ||
96 | wl_list_insert(&input->devices, &sway_device->link); | ||
97 | |||
98 | // find config | ||
99 | for (int i = 0; i < config->input_configs->length; ++i) { | ||
100 | struct input_config *input_config = config->input_configs->items[i]; | ||
101 | if (strcmp(input_config->identifier, sway_device->identifier) == 0) { | ||
102 | sway_device->config = input_config; | ||
103 | break; | ||
104 | } | ||
105 | } | ||
106 | |||
42 | struct sway_seat *seat = input_manager_get_seat(input, default_seat); | 107 | struct sway_seat *seat = input_manager_get_seat(input, default_seat); |
43 | sway_seat_add_device(seat, device); | 108 | sway_seat_add_device(seat, sway_device); |
44 | } | 109 | } |
45 | 110 | ||
46 | static void input_remove_notify(struct wl_listener *listener, void *data) { | 111 | static void input_remove_notify(struct wl_listener *listener, void *data) { |
@@ -48,9 +113,21 @@ static void input_remove_notify(struct wl_listener *listener, void *data) { | |||
48 | wl_container_of(listener, input, input_remove); | 113 | wl_container_of(listener, input, input_remove); |
49 | struct wlr_input_device *device = data; | 114 | struct wlr_input_device *device = data; |
50 | 115 | ||
51 | // TODO device configuration | 116 | struct sway_input_device *sway_device = |
52 | struct sway_seat *seat = input_manager_get_seat(input, default_seat); | 117 | input_sway_device_from_wlr(input, device); |
53 | sway_seat_remove_device(seat, device); | 118 | |
119 | if (!sway_assert(sway_device, "could not find sway device")) { | ||
120 | return; | ||
121 | } | ||
122 | |||
123 | struct sway_seat *seat = NULL; | ||
124 | wl_list_for_each(seat, &input->seats, link) { | ||
125 | sway_seat_remove_device(seat, sway_device); | ||
126 | } | ||
127 | |||
128 | wl_list_remove(&sway_device->link); | ||
129 | free(sway_device->identifier); | ||
130 | free(sway_device); | ||
54 | } | 131 | } |
55 | 132 | ||
56 | struct sway_input_manager *sway_input_manager_create( | 133 | struct sway_input_manager *sway_input_manager_create( |
@@ -63,7 +140,8 @@ struct sway_input_manager *sway_input_manager_create( | |||
63 | // XXX probably don't need the full server | 140 | // XXX probably don't need the full server |
64 | input->server = server; | 141 | input->server = server; |
65 | 142 | ||
66 | input->seats = create_list(); | 143 | wl_list_init(&input->devices); |
144 | wl_list_init(&input->seats); | ||
67 | 145 | ||
68 | // create the default seat | 146 | // create the default seat |
69 | input_manager_get_seat(input, default_seat); | 147 | input_manager_get_seat(input, default_seat); |
@@ -77,69 +155,46 @@ struct sway_input_manager *sway_input_manager_create( | |||
77 | return input; | 155 | return input; |
78 | } | 156 | } |
79 | 157 | ||
80 | struct input_config *new_input_config(const char* identifier) { | 158 | bool sway_input_manager_has_focus(struct sway_input_manager *input, |
81 | struct input_config *input = calloc(1, sizeof(struct input_config)); | 159 | swayc_t *container) { |
82 | if (!input) { | 160 | struct sway_seat *seat = NULL; |
83 | sway_log(L_DEBUG, "Unable to allocate input config"); | 161 | wl_list_for_each(seat, &input->seats, link) { |
84 | return NULL; | 162 | if (seat->focus == container) { |
85 | } | 163 | return true; |
86 | sway_log(L_DEBUG, "new_input_config(%s)", identifier); | 164 | } |
87 | if (!(input->identifier = strdup(identifier))) { | ||
88 | free(input); | ||
89 | sway_log(L_DEBUG, "Unable to allocate input config"); | ||
90 | return NULL; | ||
91 | } | 165 | } |
92 | 166 | ||
93 | input->tap = INT_MIN; | 167 | return false; |
94 | input->drag_lock = INT_MIN; | ||
95 | input->dwt = INT_MIN; | ||
96 | input->send_events = INT_MIN; | ||
97 | input->click_method = INT_MIN; | ||
98 | input->middle_emulation = INT_MIN; | ||
99 | input->natural_scroll = INT_MIN; | ||
100 | input->accel_profile = INT_MIN; | ||
101 | input->pointer_accel = FLT_MIN; | ||
102 | input->scroll_method = INT_MIN; | ||
103 | input->left_handed = INT_MIN; | ||
104 | |||
105 | return input; | ||
106 | } | 168 | } |
107 | 169 | ||
108 | char *libinput_dev_unique_id(struct libinput_device *device) { | 170 | void sway_input_manager_set_focus(struct sway_input_manager *input, |
109 | int vendor = libinput_device_get_id_vendor(device); | 171 | swayc_t *container) { |
110 | int product = libinput_device_get_id_product(device); | 172 | struct sway_seat *seat ; |
111 | char *name = strdup(libinput_device_get_name(device)); | 173 | wl_list_for_each(seat, &input->seats, link) { |
112 | 174 | sway_seat_set_focus(seat, container); | |
113 | char *p = name; | ||
114 | for (; *p; ++p) { | ||
115 | if (*p == ' ') { | ||
116 | *p = '_'; | ||
117 | } | ||
118 | } | 175 | } |
176 | } | ||
119 | 177 | ||
120 | sway_log(L_DEBUG, "rewritten name %s", name); | 178 | void sway_input_manager_apply_config(struct sway_input_manager *input, |
179 | struct input_config *config) { | ||
180 | struct sway_input_device *sway_device = | ||
181 | input_sway_device_from_config(input, config); | ||
182 | if (!sway_device) { | ||
183 | return; | ||
184 | } | ||
121 | 185 | ||
122 | int len = strlen(name) + sizeof(char) * 6; | 186 | struct sway_seat *seat = NULL; |
123 | char *identifier = malloc(len); | 187 | wl_list_for_each(seat, &input->seats, link) { |
124 | if (!identifier) { | 188 | sway_seat_remove_device(seat, sway_device); |
125 | sway_log(L_ERROR, "Unable to allocate unique input device name"); | ||
126 | return NULL; | ||
127 | } | 189 | } |
128 | 190 | ||
129 | const char *fmt = "%d:%d:%s"; | 191 | seat = input_manager_get_seat(input, default_seat); |
130 | snprintf(identifier, len, fmt, vendor, product, name); | 192 | sway_seat_add_device(seat, sway_device); |
131 | free(name); | ||
132 | return identifier; | ||
133 | } | 193 | } |
134 | 194 | ||
135 | bool sway_input_manager_swayc_has_focus(struct sway_input_manager *input, | 195 | void sway_input_manager_configure_xcursor(struct sway_input_manager *input) { |
136 | swayc_t *container) { | 196 | struct sway_seat *seat = NULL; |
137 | for (int i = 0; i < input->seats->length; ++i) { | 197 | wl_list_for_each(seat, &input->seats, link) { |
138 | struct sway_seat *seat = input->seats->items[i]; | 198 | sway_seat_configure_xcursor(seat); |
139 | if (seat->focus == container) { | ||
140 | return true; | ||
141 | } | ||
142 | } | 199 | } |
143 | |||
144 | return false; | ||
145 | } | 200 | } |