source: mainline/uspace/srv/hid/output/output.c@ 7348c4b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7348c4b was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
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 output
30 * @{
31 */
32
33#include <errno.h>
34#include <stddef.h>
35#include <stdlib.h>
36#include <macros.h>
37#include <as.h>
38#include <task.h>
39#include <ipc/output.h>
40#include <config.h>
41#include "port/ega.h"
42#include "port/chardev.h"
43#include "output.h"
44
45#define MAX_COLS 128
46#define MAX_ROWS 128
47
48typedef struct {
49 link_t link;
50
51 size_t size;
52 unsigned int flags;
53 void *data;
54} frontbuf_t;
55
56static LIST_INITIALIZE(outdevs);
57static LIST_INITIALIZE(frontbufs);
58
59outdev_t *outdev_register(outdev_ops_t *ops, void *data)
60{
61 assert(ops->get_dimensions);
62
63 outdev_t *dev = (outdev_t *) malloc(sizeof(outdev_t));
64 if (dev == NULL)
65 return NULL;
66
67 link_initialize(&dev->link);
68
69 dev->ops = *ops;
70 dev->data = data;
71
72 ops->get_dimensions(dev, &dev->cols, &dev->rows);
73 dev->backbuf = chargrid_create(dev->cols, dev->rows,
74 CHARGRID_FLAG_NONE);
75 if (dev->backbuf == NULL) {
76 free(dev);
77 return NULL;
78 }
79
80 list_append(&dev->link, &outdevs);
81 return dev;
82}
83
84static void srv_yield(ipc_call_t *icall)
85{
86 errno_t ret = EOK;
87
88 list_foreach(outdevs, link, outdev_t, dev) {
89 assert(dev->ops.yield);
90
91 errno_t rc = dev->ops.yield(dev);
92 if (rc != EOK)
93 ret = rc;
94 }
95
96 async_answer_0(icall, ret);
97}
98
99static void srv_claim(ipc_call_t *icall)
100{
101 errno_t ret = EOK;
102
103 list_foreach(outdevs, link, outdev_t, dev) {
104 assert(dev->ops.claim);
105
106 errno_t rc = dev->ops.claim(dev);
107 if (rc != EOK)
108 ret = rc;
109 }
110
111 async_answer_0(icall, ret);
112}
113
114static void srv_get_dimensions(ipc_call_t *icall)
115{
116 sysarg_t cols = MAX_COLS;
117 sysarg_t rows = MAX_ROWS;
118
119 list_foreach(outdevs, link, outdev_t, dev) {
120 cols = min(cols, dev->cols);
121 rows = min(rows, dev->rows);
122 }
123
124 async_answer_2(icall, EOK, cols, rows);
125}
126
127static void srv_get_caps(ipc_call_t *icall)
128{
129 console_caps_t caps = 0;
130
131 list_foreach(outdevs, link, outdev_t, dev) {
132 assert(dev->ops.get_caps);
133
134 caps |= dev->ops.get_caps(dev);
135 }
136
137 async_answer_1(icall, EOK, caps);
138}
139
140static frontbuf_t *resolve_frontbuf(sysarg_t handle, ipc_call_t *icall)
141{
142 frontbuf_t *frontbuf = NULL;
143 list_foreach(frontbufs, link, frontbuf_t, cur) {
144 if (cur == (frontbuf_t *) handle) {
145 frontbuf = cur;
146 break;
147 }
148 }
149
150 if (frontbuf == NULL) {
151 async_answer_0(icall, ENOENT);
152 return NULL;
153 }
154
155 return frontbuf;
156}
157
158static void srv_frontbuf_create(ipc_call_t *icall)
159{
160 frontbuf_t *frontbuf = (frontbuf_t *) malloc(sizeof(frontbuf_t));
161 if (frontbuf == NULL) {
162 async_answer_0(icall, ENOMEM);
163 return;
164 }
165
166 link_initialize(&frontbuf->link);
167
168 ipc_call_t call;
169 if (!async_share_out_receive(&call, &frontbuf->size,
170 &frontbuf->flags)) {
171 free(frontbuf);
172 async_answer_0(icall, EINVAL);
173 return;
174 }
175
176 errno_t rc = async_share_out_finalize(&call, &frontbuf->data);
177 if ((rc != EOK) || (frontbuf->data == AS_MAP_FAILED)) {
178 free(frontbuf);
179 async_answer_0(icall, ENOMEM);
180 return;
181 }
182
183 list_append(&frontbuf->link, &frontbufs);
184 async_answer_1(icall, EOK, (sysarg_t) frontbuf);
185}
186
187static void srv_frontbuf_destroy(ipc_call_t *icall)
188{
189 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
190 if (frontbuf == NULL)
191 return;
192
193 list_remove(&frontbuf->link);
194 as_area_destroy(frontbuf->data);
195 free(frontbuf);
196
197 async_answer_0(icall, EOK);
198}
199
200static void srv_cursor_update(ipc_call_t *icall)
201{
202 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
203 if (frontbuf == NULL)
204 return;
205
206 chargrid_t *buf = (chargrid_t *) frontbuf->data;
207 bool visible = chargrid_get_cursor_visibility(buf);
208
209 sysarg_t col;
210 sysarg_t row;
211 chargrid_get_cursor(buf, &col, &row);
212
213 list_foreach(outdevs, link, outdev_t, dev) {
214 assert(dev->ops.cursor_update);
215
216 sysarg_t prev_col;
217 sysarg_t prev_row;
218 chargrid_get_cursor(dev->backbuf, &prev_col, &prev_row);
219
220 chargrid_set_cursor(dev->backbuf, col, row);
221 chargrid_set_cursor_visibility(dev->backbuf, visible);
222
223 dev->ops.cursor_update(dev, prev_col, prev_row, col, row,
224 visible);
225 dev->ops.flush(dev);
226
227 }
228
229 async_answer_0(icall, EOK);
230}
231
232static void srv_set_style(ipc_call_t *icall)
233{
234 list_foreach(outdevs, link, outdev_t, dev) {
235 dev->attrs.type = CHAR_ATTR_STYLE;
236 dev->attrs.val.style =
237 (console_style_t) ipc_get_arg1(icall);
238 }
239
240 async_answer_0(icall, EOK);
241}
242
243static void srv_set_color(ipc_call_t *icall)
244{
245 list_foreach(outdevs, link, outdev_t, dev) {
246 dev->attrs.type = CHAR_ATTR_INDEX;
247 dev->attrs.val.index.bgcolor =
248 (console_color_t) ipc_get_arg1(icall);
249 dev->attrs.val.index.fgcolor =
250 (console_color_t) ipc_get_arg2(icall);
251 dev->attrs.val.index.attr =
252 (console_color_attr_t) ipc_get_arg3(icall);
253 }
254
255 async_answer_0(icall, EOK);
256}
257
258static void srv_set_rgb_color(ipc_call_t *icall)
259{
260 list_foreach(outdevs, link, outdev_t, dev) {
261 dev->attrs.type = CHAR_ATTR_RGB;
262 dev->attrs.val.rgb.bgcolor = ipc_get_arg1(icall);
263 dev->attrs.val.rgb.fgcolor = ipc_get_arg2(icall);
264 }
265
266 async_answer_0(icall, EOK);
267}
268
269static bool srv_update_scroll(outdev_t *dev, chargrid_t *buf)
270{
271 assert(dev->ops.char_update);
272
273 sysarg_t top_row = chargrid_get_top_row(buf);
274
275 if (dev->top_row == top_row)
276 return false;
277
278 dev->top_row = top_row;
279
280 for (sysarg_t y = 0; y < dev->rows; y++) {
281 for (sysarg_t x = 0; x < dev->cols; x++) {
282 charfield_t *front_field =
283 chargrid_charfield_at(buf, x, y);
284 charfield_t *back_field =
285 chargrid_charfield_at(dev->backbuf, x, y);
286 bool update = false;
287
288 if (front_field->ch != back_field->ch) {
289 back_field->ch = front_field->ch;
290 update = true;
291 }
292
293 if (!attrs_same(front_field->attrs, back_field->attrs)) {
294 back_field->attrs = front_field->attrs;
295 update = true;
296 }
297
298 front_field->flags &= ~CHAR_FLAG_DIRTY;
299
300 if (update)
301 dev->ops.char_update(dev, x, y);
302 }
303 }
304
305 return true;
306}
307
308static void srv_update(ipc_call_t *icall)
309{
310 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
311 if (frontbuf == NULL)
312 return;
313
314 chargrid_t *buf = (chargrid_t *) frontbuf->data;
315
316 list_foreach(outdevs, link, outdev_t, dev) {
317 assert(dev->ops.char_update);
318
319 if (srv_update_scroll(dev, buf))
320 continue;
321
322 for (sysarg_t y = 0; y < dev->rows; y++) {
323 for (sysarg_t x = 0; x < dev->cols; x++) {
324 charfield_t *front_field =
325 chargrid_charfield_at(buf, x, y);
326 charfield_t *back_field =
327 chargrid_charfield_at(dev->backbuf, x, y);
328 bool update = false;
329
330 if ((front_field->flags & CHAR_FLAG_DIRTY) ==
331 CHAR_FLAG_DIRTY) {
332 if (front_field->ch != back_field->ch) {
333 back_field->ch = front_field->ch;
334 update = true;
335 }
336
337 if (!attrs_same(front_field->attrs,
338 back_field->attrs)) {
339 back_field->attrs = front_field->attrs;
340 update = true;
341 }
342
343 front_field->flags &= ~CHAR_FLAG_DIRTY;
344 }
345
346 if (update)
347 dev->ops.char_update(dev, x, y);
348 }
349 }
350
351 dev->ops.flush(dev);
352 }
353
354 async_answer_0(icall, EOK);
355}
356
357static void srv_damage(ipc_call_t *icall)
358{
359 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
360 if (frontbuf == NULL)
361 return;
362
363 chargrid_t *buf = (chargrid_t *) frontbuf->data;
364
365 list_foreach(outdevs, link, outdev_t, dev) {
366 assert(dev->ops.char_update);
367
368 if (srv_update_scroll(dev, buf))
369 continue;
370
371 sysarg_t col = ipc_get_arg2(icall);
372 sysarg_t row = ipc_get_arg3(icall);
373
374 sysarg_t cols = ipc_get_arg4(icall);
375 sysarg_t rows = ipc_get_arg5(icall);
376
377 for (sysarg_t y = 0; y < rows; y++) {
378 for (sysarg_t x = 0; x < cols; x++) {
379 charfield_t *front_field =
380 chargrid_charfield_at(buf, col + x, row + y);
381 charfield_t *back_field =
382 chargrid_charfield_at(dev->backbuf, col + x, row + y);
383
384 back_field->ch = front_field->ch;
385 back_field->attrs = front_field->attrs;
386 front_field->flags &= ~CHAR_FLAG_DIRTY;
387 dev->ops.char_update(dev, col + x, row + y);
388 }
389 }
390 dev->ops.flush(dev);
391
392 }
393
394 async_answer_0(icall, EOK);
395}
396
397static void client_connection(ipc_call_t *icall, void *arg)
398{
399 /* Accept the connection */
400 async_accept_0(icall);
401
402 while (true) {
403 ipc_call_t call;
404 async_get_call(&call);
405
406 if (!ipc_get_imethod(&call)) {
407 async_answer_0(&call, EOK);
408 break;
409 }
410
411 switch (ipc_get_imethod(&call)) {
412 case OUTPUT_YIELD:
413 srv_yield(&call);
414 break;
415 case OUTPUT_CLAIM:
416 srv_claim(&call);
417 break;
418 case OUTPUT_GET_DIMENSIONS:
419 srv_get_dimensions(&call);
420 break;
421 case OUTPUT_GET_CAPS:
422 srv_get_caps(&call);
423 break;
424
425 case OUTPUT_FRONTBUF_CREATE:
426 srv_frontbuf_create(&call);
427 break;
428 case OUTPUT_FRONTBUF_DESTROY:
429 srv_frontbuf_destroy(&call);
430 break;
431
432 case OUTPUT_CURSOR_UPDATE:
433 srv_cursor_update(&call);
434 break;
435 case OUTPUT_SET_STYLE:
436 srv_set_style(&call);
437 break;
438 case OUTPUT_SET_COLOR:
439 srv_set_color(&call);
440 break;
441 case OUTPUT_SET_RGB_COLOR:
442 srv_set_rgb_color(&call);
443 break;
444 case OUTPUT_UPDATE:
445 srv_update(&call);
446 break;
447 case OUTPUT_DAMAGE:
448 srv_damage(&call);
449 break;
450
451 default:
452 async_answer_0(&call, EINVAL);
453 }
454 }
455}
456
457static void usage(char *name)
458{
459 printf("Usage: %s <service_name>\n", name);
460}
461
462int main(int argc, char *argv[])
463{
464 if (argc < 2) {
465 usage(argv[0]);
466 return 1;
467 }
468
469 printf("%s: HelenOS output service\n", NAME);
470
471 /* Register server */
472 async_set_fallback_port_handler(client_connection, NULL);
473 errno_t rc = loc_server_register(NAME);
474 if (rc != EOK) {
475 printf("%s: Unable to register driver\n", NAME);
476 return rc;
477 }
478
479 service_id_t service_id;
480 rc = loc_service_register(argv[1], &service_id);
481 if (rc != EOK) {
482 printf("%s: Unable to register service %s\n", NAME, argv[1]);
483 return rc;
484 }
485
486 if (!config_key_exists("console")) {
487 ega_init();
488 }
489
490 chardev_init();
491
492 printf("%s: Accepting connections\n", NAME);
493 task_retval(0);
494 async_manager();
495
496 /* Never reached */
497 return 0;
498}
499
500/** @}
501 */
Note: See TracBrowser for help on using the repository browser.