source: mainline/uspace/srv/hid/output/output.c@ 0680854

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