| [02f45748] | 1 | /*
|
|---|
| [60ebe63] | 2 | * Copyright (c) 2022 Jiri Svoboda
|
|---|
| [02f45748] | 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>
|
|---|
| [ddb844e] | 36 | #include <inttypes.h>
|
|---|
| [02f45748] | 37 | #include <io/input.h>
|
|---|
| [195b7b3] | 38 | #include <io/log.h>
|
|---|
| [02f45748] | 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 *);
|
|---|
| [60ebe63] | 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);
|
|---|
| [02f45748] | 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,
|
|---|
| [8edec53] | 61 | .button = ds_input_ev_button,
|
|---|
| 62 | .dclick = ds_input_ev_dclick
|
|---|
| [02f45748] | 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 |
|
|---|
| [60ebe63] | 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)
|
|---|
| [02f45748] | 77 | {
|
|---|
| 78 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 79 | kbd_event_t event;
|
|---|
| [c11ee605] | 80 | errno_t rc;
|
|---|
| [02f45748] | 81 |
|
|---|
| [88d828e] | 82 | event.kbd_id = kbd_id;
|
|---|
| [02f45748] | 83 | event.type = type;
|
|---|
| 84 | event.key = key;
|
|---|
| 85 | event.mods = mods;
|
|---|
| 86 | event.c = c;
|
|---|
| 87 |
|
|---|
| [c11ee605] | 88 | ds_display_lock(disp);
|
|---|
| 89 | rc = ds_display_post_kbd_event(disp, &event);
|
|---|
| 90 | ds_display_unlock(disp);
|
|---|
| 91 | return rc;
|
|---|
| [02f45748] | 92 | }
|
|---|
| 93 |
|
|---|
| [60ebe63] | 94 | static errno_t ds_input_ev_move(input_t *input, unsigned pos_id, int dx, int dy)
|
|---|
| [02f45748] | 95 | {
|
|---|
| [4fbdc3d] | 96 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 97 | ptd_event_t event;
|
|---|
| [c11ee605] | 98 | errno_t rc;
|
|---|
| [4fbdc3d] | 99 |
|
|---|
| [60ebe63] | 100 | event.pos_id = pos_id;
|
|---|
| [4fbdc3d] | 101 | event.type = PTD_MOVE;
|
|---|
| 102 | event.dmove.x = dx;
|
|---|
| 103 | event.dmove.y = dy;
|
|---|
| 104 |
|
|---|
| [c11ee605] | 105 | ds_display_lock(disp);
|
|---|
| 106 | rc = ds_display_post_ptd_event(disp, &event);
|
|---|
| 107 | ds_display_unlock(disp);
|
|---|
| 108 | return rc;
|
|---|
| [02f45748] | 109 | }
|
|---|
| 110 |
|
|---|
| [60ebe63] | 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)
|
|---|
| [02f45748] | 113 | {
|
|---|
| [1388f7f0] | 114 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 115 | ptd_event_t event;
|
|---|
| [c11ee605] | 116 | errno_t rc;
|
|---|
| [1388f7f0] | 117 |
|
|---|
| [60ebe63] | 118 | event.pos_id = pos_id;
|
|---|
| [1388f7f0] | 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 |
|
|---|
| [c11ee605] | 127 | ds_display_lock(disp);
|
|---|
| 128 | rc = ds_display_post_ptd_event(disp, &event);
|
|---|
| 129 | ds_display_unlock(disp);
|
|---|
| 130 | return rc;
|
|---|
| [02f45748] | 131 | }
|
|---|
| 132 |
|
|---|
| [60ebe63] | 133 | static errno_t ds_input_ev_button(input_t *input, unsigned pos_id, int bnum,
|
|---|
| 134 | int bpress)
|
|---|
| [02f45748] | 135 | {
|
|---|
| [4fbdc3d] | 136 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 137 | ptd_event_t event;
|
|---|
| [c11ee605] | 138 | errno_t rc;
|
|---|
| [4fbdc3d] | 139 |
|
|---|
| [60ebe63] | 140 | event.pos_id = pos_id;
|
|---|
| [4fbdc3d] | 141 | event.type = bpress ? PTD_PRESS : PTD_RELEASE;
|
|---|
| 142 | event.btn_num = bnum;
|
|---|
| 143 | event.dmove.x = 0;
|
|---|
| 144 | event.dmove.y = 0;
|
|---|
| 145 |
|
|---|
| [c11ee605] | 146 | ds_display_lock(disp);
|
|---|
| 147 | rc = ds_display_post_ptd_event(disp, &event);
|
|---|
| [8edec53] | 148 | ds_display_unlock(disp);
|
|---|
| 149 | return rc;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| [60ebe63] | 152 | static errno_t ds_input_ev_dclick(input_t *input, unsigned pos_id, int bnum)
|
|---|
| [8edec53] | 153 | {
|
|---|
| 154 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 155 | ptd_event_t event;
|
|---|
| 156 | errno_t rc;
|
|---|
| 157 |
|
|---|
| [60ebe63] | 158 | event.pos_id = pos_id;
|
|---|
| [8edec53] | 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);
|
|---|
| [c11ee605] | 166 | ds_display_unlock(disp);
|
|---|
| 167 | return rc;
|
|---|
| [02f45748] | 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) {
|
|---|
| [78445be8] | 183 | log_msg(LOG_DEFAULT, LVL_ERROR, "Input service %s not found",
|
|---|
| [195b7b3] | 184 | svc);
|
|---|
| [02f45748] | 185 | return rc;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
|
|---|
| 189 | if (sess == NULL) {
|
|---|
| [195b7b3] | 190 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| [78445be8] | 191 | "Unable to connect to input service %s", svc);
|
|---|
| [02f45748] | 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);
|
|---|
| [195b7b3] | 199 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| [78445be8] | 200 | "Unable to communicate with service %s (%s)",
|
|---|
| [195b7b3] | 201 | svc, str_error(rc));
|
|---|
| [02f45748] | 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 | */
|
|---|