[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>
|
---|
| 44 |
|
---|
| 45 | static void input_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
| 46 |
|
---|
| 47 | int input_open(async_sess_t *sess, input_ev_ops_t *ev_ops,
|
---|
| 48 | void *arg, input_t **rinput)
|
---|
| 49 | {
|
---|
| 50 | input_t *input = calloc(1, sizeof(input_t));
|
---|
| 51 | if (input == NULL)
|
---|
| 52 | return ENOMEM;
|
---|
| 53 |
|
---|
| 54 | input->sess = sess;
|
---|
| 55 | input->ev_ops = ev_ops;
|
---|
| 56 | input->user = arg;
|
---|
| 57 |
|
---|
| 58 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 59 |
|
---|
| 60 | int rc = async_connect_to_me(exch, 0, 0, 0, input_cb_conn, input);
|
---|
| 61 | async_exchange_end(exch);
|
---|
| 62 |
|
---|
| 63 | if (rc != EOK)
|
---|
| 64 | goto error;
|
---|
| 65 |
|
---|
| 66 | *rinput = input;
|
---|
| 67 | return EOK;
|
---|
| 68 |
|
---|
| 69 | error:
|
---|
| 70 | if (input != NULL)
|
---|
| 71 | free(input);
|
---|
| 72 |
|
---|
| 73 | return rc;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void input_close(input_t *input)
|
---|
| 77 | {
|
---|
| 78 | /* XXX Synchronize with input_cb_conn */
|
---|
| 79 | free(input);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | int input_yield(input_t *input)
|
---|
| 83 | {
|
---|
| 84 | async_exch_t *exch = async_exchange_begin(input->sess);
|
---|
| 85 |
|
---|
| 86 | int rc = async_req_0_0(exch, INPUT_YIELD);
|
---|
| 87 | async_exchange_end(exch);
|
---|
| 88 |
|
---|
| 89 | return rc;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | int input_reclaim(input_t *input)
|
---|
| 93 | {
|
---|
| 94 | async_exch_t *exch = async_exchange_begin(input->sess);
|
---|
| 95 |
|
---|
| 96 | int rc = async_req_0_0(exch, INPUT_RECLAIM);
|
---|
| 97 | async_exchange_end(exch);
|
---|
| 98 |
|
---|
| 99 | return rc;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | static void input_ev_key(input_t *input, ipc_callid_t callid,
|
---|
| 103 | ipc_call_t *call)
|
---|
| 104 | {
|
---|
| 105 | kbd_event_type_t type;
|
---|
| 106 | keycode_t key;
|
---|
| 107 | keymod_t mods;
|
---|
| 108 | wchar_t c;
|
---|
| 109 | int rc;
|
---|
| 110 |
|
---|
| 111 | type = IPC_GET_ARG1(*call);
|
---|
| 112 | key = IPC_GET_ARG2(*call);
|
---|
| 113 | mods = IPC_GET_ARG3(*call);
|
---|
| 114 | c = IPC_GET_ARG4(*call);
|
---|
| 115 |
|
---|
| 116 | rc = input->ev_ops->key(input, type, key, mods, c);
|
---|
| 117 | async_answer_0(callid, rc);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | static void input_ev_move(input_t *input, ipc_callid_t callid,
|
---|
| 121 | ipc_call_t *call)
|
---|
| 122 | {
|
---|
| 123 | int dx;
|
---|
| 124 | int dy;
|
---|
| 125 | int rc;
|
---|
| 126 |
|
---|
| 127 | dx = IPC_GET_ARG1(*call);
|
---|
| 128 | dy = IPC_GET_ARG2(*call);
|
---|
| 129 |
|
---|
| 130 | rc = input->ev_ops->move(input, dx, dy);
|
---|
| 131 | async_answer_0(callid, rc);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | static void input_ev_abs_move(input_t *input, ipc_callid_t callid,
|
---|
| 135 | ipc_call_t *call)
|
---|
| 136 | {
|
---|
| 137 | unsigned x;
|
---|
| 138 | unsigned y;
|
---|
| 139 | unsigned max_x;
|
---|
| 140 | unsigned max_y;
|
---|
| 141 | int rc;
|
---|
| 142 |
|
---|
| 143 | x = IPC_GET_ARG1(*call);
|
---|
| 144 | y = IPC_GET_ARG2(*call);
|
---|
[6af521c] | 145 | max_x = IPC_GET_ARG3(*call);
|
---|
| 146 | max_y = IPC_GET_ARG4(*call);
|
---|
[111d2d6] | 147 |
|
---|
| 148 | rc = input->ev_ops->abs_move(input, x, y, max_x, max_y);
|
---|
| 149 | async_answer_0(callid, rc);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | static void input_ev_button(input_t *input, ipc_callid_t callid,
|
---|
| 153 | ipc_call_t *call)
|
---|
| 154 | {
|
---|
| 155 | int bnum;
|
---|
| 156 | int press;
|
---|
| 157 | int rc;
|
---|
| 158 |
|
---|
| 159 | bnum = IPC_GET_ARG1(*call);
|
---|
| 160 | press = IPC_GET_ARG2(*call);
|
---|
| 161 |
|
---|
| 162 | rc = input->ev_ops->button(input, bnum, press);
|
---|
| 163 | async_answer_0(callid, rc);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static void input_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 167 | {
|
---|
| 168 | input_t *input = (input_t *)arg;
|
---|
| 169 |
|
---|
| 170 | while (true) {
|
---|
| 171 | ipc_call_t call;
|
---|
| 172 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 173 |
|
---|
| 174 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 175 | /* TODO: Handle hangup */
|
---|
| 176 | return;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 180 | case INPUT_EVENT_KEY:
|
---|
| 181 | input_ev_key(input, callid, &call);
|
---|
| 182 | break;
|
---|
| 183 | case INPUT_EVENT_MOVE:
|
---|
| 184 | input_ev_move(input, callid, &call);
|
---|
| 185 | break;
|
---|
| 186 | case INPUT_EVENT_ABS_MOVE:
|
---|
| 187 | input_ev_abs_move(input, callid, &call);
|
---|
| 188 | break;
|
---|
| 189 | case INPUT_EVENT_BUTTON:
|
---|
| 190 | input_ev_button(input, callid, &call);
|
---|
| 191 | break;
|
---|
| 192 | default:
|
---|
| 193 | async_answer_0(callid, ENOTSUP);
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /** @}
|
---|
| 199 | */
|
---|