1 | /*
|
---|
2 | * Copyright (c) 2022 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup display
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Input events
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <inttypes.h>
|
---|
37 | #include <io/input.h>
|
---|
38 | #include <io/log.h>
|
---|
39 | #include <loc.h>
|
---|
40 | #include <str_error.h>
|
---|
41 | #include "display.h"
|
---|
42 | #include "input.h"
|
---|
43 | #include "main.h"
|
---|
44 |
|
---|
45 | static errno_t ds_input_ev_active(input_t *);
|
---|
46 | static errno_t ds_input_ev_deactive(input_t *);
|
---|
47 | static errno_t ds_input_ev_key(input_t *, unsigned, kbd_event_type_t, keycode_t,
|
---|
48 | keymod_t, char32_t);
|
---|
49 | static errno_t ds_input_ev_move(input_t *, unsigned, int, int);
|
---|
50 | static errno_t ds_input_ev_abs_move(input_t *, unsigned, unsigned, unsigned,
|
---|
51 | unsigned, unsigned);
|
---|
52 | static errno_t ds_input_ev_button(input_t *, unsigned, int, int);
|
---|
53 | static errno_t ds_input_ev_dclick(input_t *, unsigned, int);
|
---|
54 |
|
---|
55 | static input_ev_ops_t ds_input_ev_ops = {
|
---|
56 | .active = ds_input_ev_active,
|
---|
57 | .deactive = ds_input_ev_deactive,
|
---|
58 | .key = ds_input_ev_key,
|
---|
59 | .move = ds_input_ev_move,
|
---|
60 | .abs_move = ds_input_ev_abs_move,
|
---|
61 | .button = ds_input_ev_button,
|
---|
62 | .dclick = ds_input_ev_dclick
|
---|
63 | };
|
---|
64 |
|
---|
65 | static errno_t ds_input_ev_active(input_t *input)
|
---|
66 | {
|
---|
67 | return EOK;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static errno_t ds_input_ev_deactive(input_t *input)
|
---|
71 | {
|
---|
72 | return EOK;
|
---|
73 | }
|
---|
74 |
|
---|
75 | static errno_t ds_input_ev_key(input_t *input, unsigned kbd_id,
|
---|
76 | kbd_event_type_t type, keycode_t key, keymod_t mods, char32_t c)
|
---|
77 | {
|
---|
78 | ds_display_t *disp = (ds_display_t *) input->user;
|
---|
79 | kbd_event_t event;
|
---|
80 | errno_t rc;
|
---|
81 |
|
---|
82 | event.kbd_id = kbd_id;
|
---|
83 | event.type = type;
|
---|
84 | event.key = key;
|
---|
85 | event.mods = mods;
|
---|
86 | event.c = c;
|
---|
87 |
|
---|
88 | ds_display_lock(disp);
|
---|
89 | rc = ds_display_post_kbd_event(disp, &event);
|
---|
90 | ds_display_unlock(disp);
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static errno_t ds_input_ev_move(input_t *input, unsigned pos_id, int dx, int dy)
|
---|
95 | {
|
---|
96 | ds_display_t *disp = (ds_display_t *) input->user;
|
---|
97 | ptd_event_t event;
|
---|
98 | errno_t rc;
|
---|
99 |
|
---|
100 | event.pos_id = pos_id;
|
---|
101 | event.type = PTD_MOVE;
|
---|
102 | event.dmove.x = dx;
|
---|
103 | event.dmove.y = dy;
|
---|
104 |
|
---|
105 | ds_display_lock(disp);
|
---|
106 | rc = ds_display_post_ptd_event(disp, &event);
|
---|
107 | ds_display_unlock(disp);
|
---|
108 | return rc;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static errno_t ds_input_ev_abs_move(input_t *input, unsigned pos_id, unsigned x,
|
---|
112 | unsigned y, unsigned max_x, unsigned max_y)
|
---|
113 | {
|
---|
114 | ds_display_t *disp = (ds_display_t *) input->user;
|
---|
115 | ptd_event_t event;
|
---|
116 | errno_t rc;
|
---|
117 |
|
---|
118 | event.pos_id = pos_id;
|
---|
119 | event.type = PTD_ABS_MOVE;
|
---|
120 | event.apos.x = x;
|
---|
121 | event.apos.y = y;
|
---|
122 | event.abounds.p0.x = 0;
|
---|
123 | event.abounds.p0.y = 0;
|
---|
124 | event.abounds.p1.x = max_x + 1;
|
---|
125 | event.abounds.p1.y = max_y + 1;
|
---|
126 |
|
---|
127 | ds_display_lock(disp);
|
---|
128 | rc = ds_display_post_ptd_event(disp, &event);
|
---|
129 | ds_display_unlock(disp);
|
---|
130 | return rc;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static errno_t ds_input_ev_button(input_t *input, unsigned pos_id, int bnum,
|
---|
134 | int bpress)
|
---|
135 | {
|
---|
136 | ds_display_t *disp = (ds_display_t *) input->user;
|
---|
137 | ptd_event_t event;
|
---|
138 | errno_t rc;
|
---|
139 |
|
---|
140 | event.pos_id = pos_id;
|
---|
141 | event.type = bpress ? PTD_PRESS : PTD_RELEASE;
|
---|
142 | event.btn_num = bnum;
|
---|
143 | event.dmove.x = 0;
|
---|
144 | event.dmove.y = 0;
|
---|
145 |
|
---|
146 | ds_display_lock(disp);
|
---|
147 | rc = ds_display_post_ptd_event(disp, &event);
|
---|
148 | ds_display_unlock(disp);
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static errno_t ds_input_ev_dclick(input_t *input, unsigned pos_id, int bnum)
|
---|
153 | {
|
---|
154 | ds_display_t *disp = (ds_display_t *) input->user;
|
---|
155 | ptd_event_t event;
|
---|
156 | errno_t rc;
|
---|
157 |
|
---|
158 | event.pos_id = pos_id;
|
---|
159 | event.type = PTD_DCLICK;
|
---|
160 | event.btn_num = bnum;
|
---|
161 | event.dmove.x = 0;
|
---|
162 | event.dmove.y = 0;
|
---|
163 |
|
---|
164 | ds_display_lock(disp);
|
---|
165 | rc = ds_display_post_ptd_event(disp, &event);
|
---|
166 | ds_display_unlock(disp);
|
---|
167 | return rc;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** Open input service.
|
---|
171 | *
|
---|
172 | * @param display Display
|
---|
173 | * @return EOK on success or an error code
|
---|
174 | */
|
---|
175 | errno_t ds_input_open(ds_display_t *display)
|
---|
176 | {
|
---|
177 | async_sess_t *sess;
|
---|
178 | service_id_t dsid;
|
---|
179 | const char *svc = "hid/input";
|
---|
180 |
|
---|
181 | errno_t rc = loc_service_get_id(svc, &dsid, 0);
|
---|
182 | if (rc != EOK) {
|
---|
183 | log_msg(LOG_DEFAULT, LVL_ERROR, "Input service %s not found",
|
---|
184 | svc);
|
---|
185 | return rc;
|
---|
186 | }
|
---|
187 |
|
---|
188 | sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
|
---|
189 | if (sess == NULL) {
|
---|
190 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
191 | "Unable to connect to input service %s", svc);
|
---|
192 | return EIO;
|
---|
193 | }
|
---|
194 |
|
---|
195 | rc = input_open(sess, &ds_input_ev_ops, (void *) display,
|
---|
196 | &display->input);
|
---|
197 | if (rc != EOK) {
|
---|
198 | async_hangup(sess);
|
---|
199 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
200 | "Unable to communicate with service %s (%s)",
|
---|
201 | svc, str_error(rc));
|
---|
202 | return rc;
|
---|
203 | }
|
---|
204 |
|
---|
205 | input_activate(display->input);
|
---|
206 | return EOK;
|
---|
207 | }
|
---|
208 |
|
---|
209 | void ds_input_close(ds_display_t *display)
|
---|
210 | {
|
---|
211 | input_close(display->input);
|
---|
212 | display->input = NULL;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** @}
|
---|
216 | */
|
---|