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

Last change on this file was ca48672, checked in by Jiri Svoboda <jiri@…>, 5 weeks ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[6d5e378]1/*
[ca48672]2 * Copyright (c) 2025 Jiri Svoboda
[6d5e378]3 * Copyright (c) 2011 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[4122410]30/** @addtogroup output
31 * @{
32 */
33
[6d5e378]34#include <errno.h>
[38d150e]35#include <stddef.h>
36#include <stdlib.h>
[6d5e378]37#include <macros.h>
38#include <as.h>
[2f6ad06]39#include <task.h>
[6d5e378]40#include <ipc/output.h>
[73d8600]41#include <config.h>
[d3109ff]42#include <vt/vt100.h>
[6d5e378]43#include "port/ega.h"
[e53794c]44#include "port/chardev.h"
[1382446]45#include "port/ddev.h"
[6d5e378]46#include "output.h"
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{
[7fc81da]116 sysarg_t cols = 0;
117 sysarg_t rows = 0;
118 bool first;
[a35b458]119
[7fc81da]120 first = true;
[feeac0d]121 list_foreach(outdevs, link, outdev_t, dev) {
[7fc81da]122 if (first) {
123 cols = dev->cols;
124 rows = dev->rows;
125 first = false;
126 } else {
127 cols = min(cols, dev->cols);
128 rows = min(rows, dev->rows);
129 }
[6d5e378]130 }
[a35b458]131
[984a9ba]132 async_answer_2(icall, EOK, cols, rows);
[6d5e378]133}
134
[984a9ba]135static void srv_get_caps(ipc_call_t *icall)
[6d5e378]136{
137 console_caps_t caps = 0;
[a35b458]138
[feeac0d]139 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]140 assert(dev->ops.get_caps);
[a35b458]141
[6d5e378]142 caps |= dev->ops.get_caps(dev);
143 }
[a35b458]144
[984a9ba]145 async_answer_1(icall, EOK, caps);
[6d5e378]146}
147
[984a9ba]148static frontbuf_t *resolve_frontbuf(sysarg_t handle, ipc_call_t *icall)
[6d5e378]149{
150 frontbuf_t *frontbuf = NULL;
[feeac0d]151 list_foreach(frontbufs, link, frontbuf_t, cur) {
[6d5e378]152 if (cur == (frontbuf_t *) handle) {
153 frontbuf = cur;
154 break;
155 }
156 }
[a35b458]157
[6d5e378]158 if (frontbuf == NULL) {
[984a9ba]159 async_answer_0(icall, ENOENT);
[6d5e378]160 return NULL;
161 }
[a35b458]162
[6d5e378]163 return frontbuf;
164}
165
[984a9ba]166static void srv_frontbuf_create(ipc_call_t *icall)
[6d5e378]167{
168 frontbuf_t *frontbuf = (frontbuf_t *) malloc(sizeof(frontbuf_t));
169 if (frontbuf == NULL) {
[984a9ba]170 async_answer_0(icall, ENOMEM);
[6d5e378]171 return;
172 }
[a35b458]173
[6d5e378]174 link_initialize(&frontbuf->link);
[a35b458]175
[984a9ba]176 ipc_call_t call;
177 if (!async_share_out_receive(&call, &frontbuf->size,
[6d5e378]178 &frontbuf->flags)) {
179 free(frontbuf);
[984a9ba]180 async_answer_0(icall, EINVAL);
[6d5e378]181 return;
182 }
[a35b458]183
[984a9ba]184 errno_t rc = async_share_out_finalize(&call, &frontbuf->data);
[6d5e378]185 if ((rc != EOK) || (frontbuf->data == AS_MAP_FAILED)) {
186 free(frontbuf);
[984a9ba]187 async_answer_0(icall, ENOMEM);
[6d5e378]188 return;
189 }
[a35b458]190
[6d5e378]191 list_append(&frontbuf->link, &frontbufs);
[984a9ba]192 async_answer_1(icall, EOK, (sysarg_t) frontbuf);
[6d5e378]193}
194
[984a9ba]195static void srv_frontbuf_destroy(ipc_call_t *icall)
[6d5e378]196{
[fafb8e5]197 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]198 if (frontbuf == NULL)
199 return;
[a35b458]200
[6d5e378]201 list_remove(&frontbuf->link);
202 as_area_destroy(frontbuf->data);
203 free(frontbuf);
[a35b458]204
[984a9ba]205 async_answer_0(icall, EOK);
[6d5e378]206}
207
[984a9ba]208static void srv_cursor_update(ipc_call_t *icall)
[6d5e378]209{
[fafb8e5]210 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]211 if (frontbuf == NULL)
212 return;
[a35b458]213
[6d5e378]214 chargrid_t *buf = (chargrid_t *) frontbuf->data;
215 bool visible = chargrid_get_cursor_visibility(buf);
[a35b458]216
[6d5e378]217 sysarg_t col;
218 sysarg_t row;
219 chargrid_get_cursor(buf, &col, &row);
[a35b458]220
[feeac0d]221 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]222 assert(dev->ops.cursor_update);
[a35b458]223
[6d5e378]224 sysarg_t prev_col;
225 sysarg_t prev_row;
226 chargrid_get_cursor(dev->backbuf, &prev_col, &prev_row);
[a35b458]227
[6d5e378]228 chargrid_set_cursor(dev->backbuf, col, row);
229 chargrid_set_cursor_visibility(dev->backbuf, visible);
[a35b458]230
[6d5e378]231 dev->ops.cursor_update(dev, prev_col, prev_row, col, row,
232 visible);
[bbc6277]233 dev->ops.flush(dev);
234
[6d5e378]235 }
[a35b458]236
[984a9ba]237 async_answer_0(icall, EOK);
[6d5e378]238}
239
[984a9ba]240static void srv_set_style(ipc_call_t *icall)
[6d5e378]241{
[feeac0d]242 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]243 dev->attrs.type = CHAR_ATTR_STYLE;
244 dev->attrs.val.style =
[fafb8e5]245 (console_style_t) ipc_get_arg1(icall);
[6d5e378]246 }
[a35b458]247
[984a9ba]248 async_answer_0(icall, EOK);
[6d5e378]249}
250
[984a9ba]251static void srv_set_color(ipc_call_t *icall)
[6d5e378]252{
[feeac0d]253 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]254 dev->attrs.type = CHAR_ATTR_INDEX;
255 dev->attrs.val.index.bgcolor =
[fafb8e5]256 (console_color_t) ipc_get_arg1(icall);
[6d5e378]257 dev->attrs.val.index.fgcolor =
[fafb8e5]258 (console_color_t) ipc_get_arg2(icall);
[6d5e378]259 dev->attrs.val.index.attr =
[fafb8e5]260 (console_color_attr_t) ipc_get_arg3(icall);
[6d5e378]261 }
[a35b458]262
[984a9ba]263 async_answer_0(icall, EOK);
[6d5e378]264}
265
[984a9ba]266static void srv_set_rgb_color(ipc_call_t *icall)
[6d5e378]267{
[feeac0d]268 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]269 dev->attrs.type = CHAR_ATTR_RGB;
[fafb8e5]270 dev->attrs.val.rgb.bgcolor = ipc_get_arg1(icall);
271 dev->attrs.val.rgb.fgcolor = ipc_get_arg2(icall);
[6d5e378]272 }
[a35b458]273
[984a9ba]274 async_answer_0(icall, EOK);
[6d5e378]275}
276
277static bool srv_update_scroll(outdev_t *dev, chargrid_t *buf)
278{
279 assert(dev->ops.char_update);
[a35b458]280
[6d5e378]281 sysarg_t top_row = chargrid_get_top_row(buf);
[a35b458]282
[6d5e378]283 if (dev->top_row == top_row)
284 return false;
[a35b458]285
[6d5e378]286 dev->top_row = top_row;
[a35b458]287
[6d5e378]288 for (sysarg_t y = 0; y < dev->rows; y++) {
289 for (sysarg_t x = 0; x < dev->cols; x++) {
290 charfield_t *front_field =
291 chargrid_charfield_at(buf, x, y);
292 charfield_t *back_field =
293 chargrid_charfield_at(dev->backbuf, x, y);
294 bool update = false;
[a35b458]295
[6d5e378]296 if (front_field->ch != back_field->ch) {
297 back_field->ch = front_field->ch;
298 update = true;
299 }
[a35b458]300
[6d5e378]301 if (!attrs_same(front_field->attrs, back_field->attrs)) {
302 back_field->attrs = front_field->attrs;
303 update = true;
304 }
[a35b458]305
[6d5e378]306 front_field->flags &= ~CHAR_FLAG_DIRTY;
[a35b458]307
[6d5e378]308 if (update)
309 dev->ops.char_update(dev, x, y);
310 }
311 }
[a35b458]312
[6d5e378]313 return true;
314}
315
[984a9ba]316static void srv_update(ipc_call_t *icall)
[6d5e378]317{
[fafb8e5]318 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]319 if (frontbuf == NULL)
320 return;
[a35b458]321
[6d5e378]322 chargrid_t *buf = (chargrid_t *) frontbuf->data;
[a35b458]323
[feeac0d]324 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]325 assert(dev->ops.char_update);
[a35b458]326
[6d5e378]327 if (srv_update_scroll(dev, buf))
328 continue;
[a35b458]329
[6d5e378]330 for (sysarg_t y = 0; y < dev->rows; y++) {
331 for (sysarg_t x = 0; x < dev->cols; x++) {
332 charfield_t *front_field =
333 chargrid_charfield_at(buf, x, y);
334 charfield_t *back_field =
335 chargrid_charfield_at(dev->backbuf, x, y);
336 bool update = false;
[a35b458]337
[6d5e378]338 if ((front_field->flags & CHAR_FLAG_DIRTY) ==
339 CHAR_FLAG_DIRTY) {
340 if (front_field->ch != back_field->ch) {
341 back_field->ch = front_field->ch;
342 update = true;
343 }
[a35b458]344
[6d5e378]345 if (!attrs_same(front_field->attrs,
346 back_field->attrs)) {
347 back_field->attrs = front_field->attrs;
348 update = true;
349 }
[a35b458]350
[6d5e378]351 front_field->flags &= ~CHAR_FLAG_DIRTY;
352 }
[a35b458]353
[6d5e378]354 if (update)
355 dev->ops.char_update(dev, x, y);
356 }
357 }
[a35b458]358
[bbc6277]359 dev->ops.flush(dev);
[6d5e378]360 }
[a35b458]361
[984a9ba]362 async_answer_0(icall, EOK);
[6d5e378]363}
364
[984a9ba]365static void srv_damage(ipc_call_t *icall)
[6d5e378]366{
[fafb8e5]367 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]368 if (frontbuf == NULL)
369 return;
[a35b458]370
[6d5e378]371 chargrid_t *buf = (chargrid_t *) frontbuf->data;
[a35b458]372
[feeac0d]373 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]374 assert(dev->ops.char_update);
[a35b458]375
[6d5e378]376 if (srv_update_scroll(dev, buf))
377 continue;
[a35b458]378
[fafb8e5]379 sysarg_t col = ipc_get_arg2(icall);
380 sysarg_t row = ipc_get_arg3(icall);
[a35b458]381
[fafb8e5]382 sysarg_t cols = ipc_get_arg4(icall);
383 sysarg_t rows = ipc_get_arg5(icall);
[a35b458]384
[6d5e378]385 for (sysarg_t y = 0; y < rows; y++) {
386 for (sysarg_t x = 0; x < cols; x++) {
387 charfield_t *front_field =
388 chargrid_charfield_at(buf, col + x, row + y);
389 charfield_t *back_field =
390 chargrid_charfield_at(dev->backbuf, col + x, row + y);
[a35b458]391
[6d5e378]392 back_field->ch = front_field->ch;
393 back_field->attrs = front_field->attrs;
394 front_field->flags &= ~CHAR_FLAG_DIRTY;
395 dev->ops.char_update(dev, col + x, row + y);
396 }
397 }
[bbc6277]398 dev->ops.flush(dev);
399
[6d5e378]400 }
[984a9ba]401
402 async_answer_0(icall, EOK);
[6d5e378]403}
404
[984a9ba]405static void client_connection(ipc_call_t *icall, void *arg)
[6d5e378]406{
407 /* Accept the connection */
[beb83c1]408 async_accept_0(icall);
[a35b458]409
[6d5e378]410 while (true) {
411 ipc_call_t call;
[984a9ba]412 async_get_call(&call);
[a35b458]413
[fafb8e5]414 if (!ipc_get_imethod(&call)) {
[984a9ba]415 async_answer_0(&call, EOK);
[6d5e378]416 break;
417 }
[a35b458]418
[fafb8e5]419 switch (ipc_get_imethod(&call)) {
[6feb444]420 case OUTPUT_YIELD:
[984a9ba]421 srv_yield(&call);
[6feb444]422 break;
423 case OUTPUT_CLAIM:
[984a9ba]424 srv_claim(&call);
[6feb444]425 break;
426 case OUTPUT_GET_DIMENSIONS:
[984a9ba]427 srv_get_dimensions(&call);
[6feb444]428 break;
429 case OUTPUT_GET_CAPS:
[984a9ba]430 srv_get_caps(&call);
[6feb444]431 break;
[a35b458]432
[6feb444]433 case OUTPUT_FRONTBUF_CREATE:
[984a9ba]434 srv_frontbuf_create(&call);
[6feb444]435 break;
436 case OUTPUT_FRONTBUF_DESTROY:
[984a9ba]437 srv_frontbuf_destroy(&call);
[6feb444]438 break;
[a35b458]439
[6feb444]440 case OUTPUT_CURSOR_UPDATE:
[984a9ba]441 srv_cursor_update(&call);
[6feb444]442 break;
443 case OUTPUT_SET_STYLE:
[984a9ba]444 srv_set_style(&call);
[6feb444]445 break;
446 case OUTPUT_SET_COLOR:
[984a9ba]447 srv_set_color(&call);
[6feb444]448 break;
449 case OUTPUT_SET_RGB_COLOR:
[984a9ba]450 srv_set_rgb_color(&call);
[6feb444]451 break;
452 case OUTPUT_UPDATE:
[984a9ba]453 srv_update(&call);
[6feb444]454 break;
455 case OUTPUT_DAMAGE:
[984a9ba]456 srv_damage(&call);
[6feb444]457 break;
[a35b458]458
[6feb444]459 default:
[984a9ba]460 async_answer_0(&call, EINVAL);
[6d5e378]461 }
462 }
463}
464
465static void usage(char *name)
466{
467 printf("Usage: %s <service_name>\n", name);
468}
469
470int main(int argc, char *argv[])
471{
[4c6fd56]472 loc_srv_t *srv;
473
[6d5e378]474 if (argc < 2) {
475 usage(argv[0]);
476 return 1;
477 }
[a35b458]478
[6d5e378]479 printf("%s: HelenOS output service\n", NAME);
[a35b458]480
[6d5e378]481 /* Register server */
[b688fd8]482 async_set_fallback_port_handler(client_connection, NULL);
[4c6fd56]483 errno_t rc = loc_server_register(NAME, &srv);
[6d5e378]484 if (rc != EOK) {
485 printf("%s: Unable to register driver\n", NAME);
486 return rc;
487 }
[a35b458]488
[6d5e378]489 service_id_t service_id;
[ca48672]490 rc = loc_service_register(srv, argv[1], fallback_port_id, &service_id);
[6d5e378]491 if (rc != EOK) {
[4c6fd56]492 loc_server_unregister(srv);
493
[6d5e378]494 printf("%s: Unable to register service %s\n", NAME, argv[1]);
495 return rc;
496 }
[a35b458]497
[73d8600]498 if (!config_key_exists("console")) {
499 ega_init();
[1382446]500#if defined(CONFIG_FB) && !defined(CONFIG_WINSYS)
501 /*
502 * NOTE: We don't have a good way of detecting the absence
503 * of a KFB display device at run time because of the
504 * asunchronous nature of device discovery
505 */
506 output_ddev_init();
507#endif
[73d8600]508 }
[a35b458]509
[9940ce0]510 chardev_init();
[a35b458]511
[6d5e378]512 printf("%s: Accepting connections\n", NAME);
513 task_retval(0);
514 async_manager();
[a35b458]515
[6d5e378]516 /* Never reached */
517 return 0;
518}
[4122410]519
520/** @}
521 */
Note: See TracBrowser for help on using the repository browser.