| 1 | /*
|
|---|
| 2 | * Copyright (c) 2024 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 "ievent.h"
|
|---|
| 43 | #include "input.h"
|
|---|
| 44 | #include "main.h"
|
|---|
| 45 |
|
|---|
| 46 | static errno_t ds_input_ev_active(input_t *);
|
|---|
| 47 | static errno_t ds_input_ev_deactive(input_t *);
|
|---|
| 48 | static errno_t ds_input_ev_key(input_t *, unsigned, kbd_event_type_t, keycode_t,
|
|---|
| 49 | keymod_t, char32_t);
|
|---|
| 50 | static errno_t ds_input_ev_move(input_t *, unsigned, int, int);
|
|---|
| 51 | static errno_t ds_input_ev_abs_move(input_t *, unsigned, unsigned, unsigned,
|
|---|
| 52 | unsigned, unsigned);
|
|---|
| 53 | static errno_t ds_input_ev_button(input_t *, unsigned, int, int);
|
|---|
| 54 | static errno_t ds_input_ev_dclick(input_t *, unsigned, int);
|
|---|
| 55 |
|
|---|
| 56 | static input_ev_ops_t ds_input_ev_ops = {
|
|---|
| 57 | .active = ds_input_ev_active,
|
|---|
| 58 | .deactive = ds_input_ev_deactive,
|
|---|
| 59 | .key = ds_input_ev_key,
|
|---|
| 60 | .move = ds_input_ev_move,
|
|---|
| 61 | .abs_move = ds_input_ev_abs_move,
|
|---|
| 62 | .button = ds_input_ev_button,
|
|---|
| 63 | .dclick = ds_input_ev_dclick
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | static errno_t ds_input_ev_active(input_t *input)
|
|---|
| 67 | {
|
|---|
| 68 | return EOK;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static errno_t ds_input_ev_deactive(input_t *input)
|
|---|
| 72 | {
|
|---|
| 73 | return EOK;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static errno_t ds_input_ev_key(input_t *input, unsigned kbd_id,
|
|---|
| 77 | kbd_event_type_t type, keycode_t key, keymod_t mods, char32_t c)
|
|---|
| 78 | {
|
|---|
| 79 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 80 | kbd_event_t event;
|
|---|
| 81 | errno_t rc;
|
|---|
| 82 |
|
|---|
| 83 | event.kbd_id = kbd_id;
|
|---|
| 84 | event.type = type;
|
|---|
| 85 | event.key = key;
|
|---|
| 86 | event.mods = mods;
|
|---|
| 87 | event.c = c;
|
|---|
| 88 |
|
|---|
| 89 | ds_display_lock(disp);
|
|---|
| 90 | rc = ds_ievent_post_kbd(disp, &event);
|
|---|
| 91 | ds_display_unlock(disp);
|
|---|
| 92 | return rc;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | static errno_t ds_input_ev_move(input_t *input, unsigned pos_id, int dx, int dy)
|
|---|
| 96 | {
|
|---|
| 97 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 98 | ptd_event_t event;
|
|---|
| 99 | errno_t rc;
|
|---|
| 100 |
|
|---|
| 101 | event.pos_id = pos_id;
|
|---|
| 102 | event.type = PTD_MOVE;
|
|---|
| 103 | event.dmove.x = dx;
|
|---|
| 104 | event.dmove.y = dy;
|
|---|
| 105 |
|
|---|
| 106 | ds_display_lock(disp);
|
|---|
| 107 | rc = ds_ievent_post_ptd(disp, &event);
|
|---|
| 108 | ds_display_unlock(disp);
|
|---|
| 109 | return rc;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | static errno_t ds_input_ev_abs_move(input_t *input, unsigned pos_id, unsigned x,
|
|---|
| 113 | unsigned y, unsigned max_x, unsigned max_y)
|
|---|
| 114 | {
|
|---|
| 115 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 116 | ptd_event_t event;
|
|---|
| 117 | errno_t rc;
|
|---|
| 118 |
|
|---|
| 119 | event.pos_id = pos_id;
|
|---|
| 120 | event.type = PTD_ABS_MOVE;
|
|---|
| 121 | event.apos.x = x;
|
|---|
| 122 | event.apos.y = y;
|
|---|
| 123 | event.abounds.p0.x = 0;
|
|---|
| 124 | event.abounds.p0.y = 0;
|
|---|
| 125 | event.abounds.p1.x = max_x + 1;
|
|---|
| 126 | event.abounds.p1.y = max_y + 1;
|
|---|
| 127 |
|
|---|
| 128 | ds_display_lock(disp);
|
|---|
| 129 | rc = ds_ievent_post_ptd(disp, &event);
|
|---|
| 130 | ds_display_unlock(disp);
|
|---|
| 131 | return rc;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static errno_t ds_input_ev_button(input_t *input, unsigned pos_id, int bnum,
|
|---|
| 135 | int bpress)
|
|---|
| 136 | {
|
|---|
| 137 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 138 | ptd_event_t event;
|
|---|
| 139 | errno_t rc;
|
|---|
| 140 |
|
|---|
| 141 | event.pos_id = pos_id;
|
|---|
| 142 | event.type = bpress ? PTD_PRESS : PTD_RELEASE;
|
|---|
| 143 | event.btn_num = bnum;
|
|---|
| 144 | event.dmove.x = 0;
|
|---|
| 145 | event.dmove.y = 0;
|
|---|
| 146 |
|
|---|
| 147 | ds_display_lock(disp);
|
|---|
| 148 | rc = ds_ievent_post_ptd(disp, &event);
|
|---|
| 149 | ds_display_unlock(disp);
|
|---|
| 150 | return rc;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | static errno_t ds_input_ev_dclick(input_t *input, unsigned pos_id, int bnum)
|
|---|
| 154 | {
|
|---|
| 155 | ds_display_t *disp = (ds_display_t *) input->user;
|
|---|
| 156 | ptd_event_t event;
|
|---|
| 157 | errno_t rc;
|
|---|
| 158 |
|
|---|
| 159 | event.pos_id = pos_id;
|
|---|
| 160 | event.type = PTD_DCLICK;
|
|---|
| 161 | event.btn_num = bnum;
|
|---|
| 162 | event.dmove.x = 0;
|
|---|
| 163 | event.dmove.y = 0;
|
|---|
| 164 |
|
|---|
| 165 | ds_display_lock(disp);
|
|---|
| 166 | rc = ds_ievent_post_ptd(disp, &event);
|
|---|
| 167 | ds_display_unlock(disp);
|
|---|
| 168 | return rc;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** Open input service.
|
|---|
| 172 | *
|
|---|
| 173 | * @param display Display
|
|---|
| 174 | * @return EOK on success or an error code
|
|---|
| 175 | */
|
|---|
| 176 | errno_t ds_input_open(ds_display_t *display)
|
|---|
| 177 | {
|
|---|
| 178 | async_sess_t *sess;
|
|---|
| 179 | service_id_t dsid;
|
|---|
| 180 | const char *svc = "hid/input";
|
|---|
| 181 |
|
|---|
| 182 | errno_t rc = loc_service_get_id(svc, &dsid, 0);
|
|---|
| 183 | if (rc != EOK) {
|
|---|
| 184 | log_msg(LOG_DEFAULT, LVL_ERROR, "Input service %s not found",
|
|---|
| 185 | svc);
|
|---|
| 186 | return rc;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | sess = loc_service_connect(dsid, INTERFACE_INPUT, 0);
|
|---|
| 190 | if (sess == NULL) {
|
|---|
| 191 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 192 | "Unable to connect to input service %s", svc);
|
|---|
| 193 | return EIO;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | rc = input_open(sess, &ds_input_ev_ops, (void *) display,
|
|---|
| 197 | &display->input);
|
|---|
| 198 | if (rc != EOK) {
|
|---|
| 199 | async_hangup(sess);
|
|---|
| 200 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 201 | "Unable to communicate with service %s (%s)",
|
|---|
| 202 | svc, str_error(rc));
|
|---|
| 203 | return rc;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | input_activate(display->input);
|
|---|
| 207 | return EOK;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void ds_input_close(ds_display_t *display)
|
|---|
| 211 | {
|
|---|
| 212 | input_close(display->input);
|
|---|
| 213 | display->input = NULL;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /** @}
|
|---|
| 217 | */
|
|---|