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