source: mainline/uspace/lib/c/generic/io/input.c@ 8fb674b

Last change on this file since 8fb674b was 8fb674b, checked in by Matthieu Riolo <matthieu.riolo@…>, 5 years ago

correcting usage of async_data_read_receive() in srv/input

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[111d2d6]1/*
2 * Copyright (c) 2012 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 libc
30 * @{
31 */
32/**
33 * @file
34 * @brief Input protocol client stub
35 */
36
37#include <async.h>
38#include <assert.h>
39#include <errno.h>
40#include <io/kbd_event.h>
41#include <io/input.h>
42#include <ipc/input.h>
43#include <stdlib.h>
[a76ba5f3]44#include <str.h>
[111d2d6]45
[984a9ba]46static void input_cb_conn(ipc_call_t *icall, void *arg);
[111d2d6]47
[b7fd2a0]48errno_t input_open(async_sess_t *sess, input_ev_ops_t *ev_ops,
[111d2d6]49 void *arg, input_t **rinput)
50{
51 input_t *input = calloc(1, sizeof(input_t));
52 if (input == NULL)
53 return ENOMEM;
54
55 input->sess = sess;
56 input->ev_ops = ev_ops;
57 input->user = arg;
58
59 async_exch_t *exch = async_exchange_begin(sess);
60
[f9b2cb4c]61 port_id_t port;
[b7fd2a0]62 errno_t rc = async_create_callback_port(exch, INTERFACE_INPUT_CB, 0, 0,
[f9b2cb4c]63 input_cb_conn, input, &port);
[a35b458]64
[111d2d6]65 async_exchange_end(exch);
66
67 if (rc != EOK)
68 goto error;
69
70 *rinput = input;
71 return EOK;
72
73error:
74 if (input != NULL)
75 free(input);
76
77 return rc;
78}
79
80void input_close(input_t *input)
81{
82 /* XXX Synchronize with input_cb_conn */
83 free(input);
84}
85
[b7fd2a0]86errno_t input_activate(input_t *input)
[111d2d6]87{
88 async_exch_t *exch = async_exchange_begin(input->sess);
[b7fd2a0]89 errno_t rc = async_req_0_0(exch, INPUT_ACTIVATE);
[111d2d6]90 async_exchange_end(exch);
[a35b458]91
[111d2d6]92 return rc;
93}
94
[984a9ba]95static void input_ev_active(input_t *input, ipc_call_t *call)
[111d2d6]96{
[b7fd2a0]97 errno_t rc = input->ev_ops->active(input);
[984a9ba]98 async_answer_0(call, rc);
[593e023]99}
[111d2d6]100
[984a9ba]101static void input_ev_deactive(input_t *input, ipc_call_t *call)
[593e023]102{
[b7fd2a0]103 errno_t rc = input->ev_ops->deactive(input);
[984a9ba]104 async_answer_0(call, rc);
[111d2d6]105}
106
[984a9ba]107static void input_ev_key(input_t *input, ipc_call_t *call)
[111d2d6]108{
109 kbd_event_type_t type;
110 keycode_t key;
111 keymod_t mods;
[28a5ebd]112 char32_t c;
[b7fd2a0]113 errno_t rc;
[111d2d6]114
[fafb8e5]115 type = ipc_get_arg1(call);
116 key = ipc_get_arg2(call);
117 mods = ipc_get_arg3(call);
118 c = ipc_get_arg4(call);
[111d2d6]119
120 rc = input->ev_ops->key(input, type, key, mods, c);
[984a9ba]121 async_answer_0(call, rc);
[111d2d6]122}
123
[984a9ba]124static void input_ev_move(input_t *input, ipc_call_t *call)
[111d2d6]125{
126 int dx;
127 int dy;
[b7fd2a0]128 errno_t rc;
[111d2d6]129
[fafb8e5]130 dx = ipc_get_arg1(call);
131 dy = ipc_get_arg2(call);
[111d2d6]132
133 rc = input->ev_ops->move(input, dx, dy);
[984a9ba]134 async_answer_0(call, rc);
[111d2d6]135}
136
[984a9ba]137static void input_ev_abs_move(input_t *input, ipc_call_t *call)
[111d2d6]138{
139 unsigned x;
140 unsigned y;
141 unsigned max_x;
142 unsigned max_y;
[b7fd2a0]143 errno_t rc;
[111d2d6]144
[fafb8e5]145 x = ipc_get_arg1(call);
146 y = ipc_get_arg2(call);
147 max_x = ipc_get_arg3(call);
148 max_y = ipc_get_arg4(call);
[111d2d6]149
150 rc = input->ev_ops->abs_move(input, x, y, max_x, max_y);
[984a9ba]151 async_answer_0(call, rc);
[111d2d6]152}
153
[984a9ba]154static void input_ev_button(input_t *input, ipc_call_t *call)
[111d2d6]155{
156 int bnum;
157 int press;
[b7fd2a0]158 errno_t rc;
[111d2d6]159
[fafb8e5]160 bnum = ipc_get_arg1(call);
161 press = ipc_get_arg2(call);
[111d2d6]162
163 rc = input->ev_ops->button(input, bnum, press);
[984a9ba]164 async_answer_0(call, rc);
[111d2d6]165}
166
[984a9ba]167static void input_cb_conn(ipc_call_t *icall, void *arg)
[111d2d6]168{
[984a9ba]169 input_t *input = (input_t *) arg;
[111d2d6]170
171 while (true) {
172 ipc_call_t call;
[984a9ba]173 async_get_call(&call);
[111d2d6]174
[fafb8e5]175 if (!ipc_get_imethod(&call)) {
[889cdb1]176 async_answer_0(&call, EOK);
[111d2d6]177 return;
178 }
179
[fafb8e5]180 switch (ipc_get_imethod(&call)) {
[593e023]181 case INPUT_EVENT_ACTIVE:
[984a9ba]182 input_ev_active(input, &call);
[593e023]183 break;
184 case INPUT_EVENT_DEACTIVE:
[984a9ba]185 input_ev_deactive(input, &call);
[593e023]186 break;
[111d2d6]187 case INPUT_EVENT_KEY:
[984a9ba]188 input_ev_key(input, &call);
[111d2d6]189 break;
190 case INPUT_EVENT_MOVE:
[984a9ba]191 input_ev_move(input, &call);
[111d2d6]192 break;
193 case INPUT_EVENT_ABS_MOVE:
[984a9ba]194 input_ev_abs_move(input, &call);
[111d2d6]195 break;
196 case INPUT_EVENT_BUTTON:
[984a9ba]197 input_ev_button(input, &call);
[111d2d6]198 break;
199 default:
[984a9ba]200 async_answer_0(&call, ENOTSUP);
[111d2d6]201 }
202 }
203}
204
[a76ba5f3]205/**
206 * Retrieves the active keyboard layout
207 * @param sess Active session to the input server
208 * @param layout The name of the currently active layout,
209 * needs to be freed by the caller
210 * @return EOK if sucessful or the corresponding error code.
211 * If a failure occurs the param layout is already freed
212 */
213errno_t input_layout_get(async_sess_t *sess, char **layout)
214{
[8fb674b]215 *layout = NULL;
216
[a76ba5f3]217 async_exch_t *exch = async_exchange_begin(sess);
218
[8fb674b]219 ipc_call_t answer;
220 aid_t req = async_send_0(exch, INPUT_GET_LAYOUT, &answer);
[a76ba5f3]221
[8fb674b]222 char layout_buf[INPUT_LAYOUT_NAME_MAXLEN + 1];
223 ipc_call_t dreply;
224 aid_t dreq = async_data_read(exch, layout_buf, INPUT_LAYOUT_NAME_MAXLEN,
225 &dreply);
[a76ba5f3]226
[8fb674b]227 errno_t dretval;
228 async_wait_for(dreq, &dretval);
229
230 async_exchange_end(exch);
231
232 if (dretval != EOK) {
233 async_forget(req);
234 return dretval;
[a76ba5f3]235 }
236
[8fb674b]237 errno_t retval;
238 async_wait_for(req, &retval);
[a76ba5f3]239
[8fb674b]240 if (retval != EOK)
241 return retval;
[a76ba5f3]242
[8fb674b]243 size_t length = ipc_get_arg2(&dreply);
244 assert(length <= INPUT_LAYOUT_NAME_MAXLEN);
245 layout_buf[length] = '\0';
246
247 *layout = str_dup(layout_buf);
248 if (*layout == NULL)
249 return ENOMEM;
250
251 return EOK;
[a76ba5f3]252}
253
254/**
255 * Changes the keyboard layout
256 * @param sess Active session to the input server
257 * @param layout The name of the layout which should be activated
258 * @return EOK if sucessful or the corresponding error code.
259 */
260errno_t input_layout_set(async_sess_t *sess, const char *layout)
261{
262 errno_t rc;
263 ipc_call_t call;
264 async_exch_t *exch = async_exchange_begin(sess);
265
[6a208fc]266 aid_t mid = async_send_0(exch, INPUT_SET_LAYOUT, &call);
[a76ba5f3]267 rc = async_data_write_start(exch, layout, str_size(layout));
268
269 if (rc == EOK)
270 async_wait_for(mid, &rc);
271
272 async_exchange_end(exch);
273 return rc;
274}
275
[111d2d6]276/** @}
277 */
Note: See TracBrowser for help on using the repository browser.