source: mainline/uspace/srv/hid/display/input.c@ 6301a24f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6301a24f was c11ee605, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Display server needs some locking

Sometimes destroying a window would race with event delivery. We add
one big lock for the display object that is taken before servicing
any client request or input event.

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