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