Changeset d2cc7e1 in mainline for uspace/lib/libc/generic/console.c


Ignore:
Timestamp:
2009-03-21T11:26:31Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0a5116db
Parents:
5b8c75a
Message:

Buffer console output with line granularity. Makes esp. msim/ski console faster. EGA-fb needs fixing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/console.c

    r5b8c75a rd2cc7e1  
    3939#include <ipc/console.h>
    4040#include <ipc/services.h>
     41#include <errno.h>
     42#include <string.h>
    4143#include <console.h>
    4244
    4345static int console_phone = -1;
     46
     47/** Size of cbuffer. */
     48#define CBUFFER_SIZE 256
     49
     50/** Buffer for writing characters to the console. */
     51static char cbuffer[CBUFFER_SIZE];
     52
     53/** Pointer to end of cbuffer. */
     54static char *cbuffer_end = cbuffer + CBUFFER_SIZE;
     55
     56/** Pointer to first available field in cbuffer. */
     57static char *cbp = cbuffer;
     58
     59static ssize_t cons_write(const char *buf, size_t nbyte);
     60static void cons_putchar(int c);
     61
     62static void cbuffer_flush(void);
     63static void cbuffer_drain(void);
     64static void cbuffer_putc(int c);
     65
    4466
    4567void console_open(bool blocking)
     
    85107{
    86108        int cons_phone = console_phone_get(true);
     109
     110        cbuffer_drain();
    87111        async_msg_0(cons_phone, CONSOLE_CLEAR);
    88112}
     
    91115{
    92116        int cons_phone = console_phone_get(true);
     117
     118        cbuffer_flush();
    93119        async_msg_2(cons_phone, CONSOLE_GOTO, row, col);
    94120}
     
    96122void console_putchar(int c)
    97123{
     124        cbuffer_putc(c);
     125}
     126
     127/** Write all data from output buffer to the console. */
     128static void cbuffer_flush(void)
     129{
     130        int rc;
     131        int len;
     132
     133        len = cbp - cbuffer;
     134
     135        while (len > 0) {
     136                rc = cons_write(cbuffer, cbp - cbuffer);
     137                if (rc < 0)
     138                        return;
     139
     140                len -= rc;
     141        }
     142
     143        cbp = cbuffer;
     144}
     145
     146/** Drop all data in console output buffer. */
     147static void cbuffer_drain(void)
     148{
     149        cbp = cbuffer;
     150}
     151
     152/** Write one character to the output buffer. */
     153static inline void cbuffer_putc(int c)
     154{
     155        if (cbp == cbuffer_end)
     156                cbuffer_flush();
     157
     158        *cbp++ = c;
     159
     160        if (c == '\n')
     161                cbuffer_flush();
     162}
     163
     164/** Write one character to the console via IPC. */
     165static void cons_putchar(int c)
     166{
    98167        int cons_phone = console_phone_get(true);
    99168        async_msg_1(cons_phone, CONSOLE_PUTCHAR, c);
    100169}
    101170
     171/** Write characters to the console via IPC. */
     172static ssize_t cons_write(const char *buf, size_t nbyte)
     173{
     174        int cons_phone = console_phone_get(true);
     175        ipcarg_t rc;
     176        ipc_call_t answer;
     177        aid_t req;
     178
     179        async_serialize_start();
     180       
     181        req = async_send_0(cons_phone, CONSOLE_WRITE, &answer);
     182        rc = ipc_data_write_start(cons_phone, (void *) buf, nbyte);
     183
     184        if (rc != EOK) {
     185                async_wait_for(req, NULL);
     186                async_serialize_end();
     187                return (ssize_t) rc;
     188        }
     189
     190        async_wait_for(req, &rc);
     191        async_serialize_end();
     192
     193        if (rc == EOK)
     194                return (ssize_t) IPC_GET_ARG1(answer);
     195        else
     196                return -1;
     197}
     198
     199/** Write characters to the console. */
     200ssize_t console_write(const char *buf, size_t nbyte)
     201{
     202        size_t left;
     203
     204        left = nbyte;
     205
     206        while (left > 0) {
     207                cbuffer_putc(*buf++);
     208                --left;
     209        }
     210
     211        return nbyte;
     212}
     213
     214/** Write a NULL-terminated string to the console. */
     215void console_putstr(const char *s)
     216{
     217        size_t len;
     218        ssize_t rc;
     219
     220        len = strlen(s);
     221        while (len > 0) {
     222                rc = console_write(s, len);
     223                if (rc < 0)
     224                        return; /* Error */
     225                s += rc;
     226                len -= rc;
     227        }
     228}
     229
     230/** Flush all output to the console. */
    102231void console_flush(void)
    103232{
    104         int cons_phone = console_phone_get(true);
     233        int cons_phone = console_phone_get(false);
     234
     235        cbuffer_flush();
    105236        async_msg_0(cons_phone, CONSOLE_FLUSH);
    106237}
     
    123254{
    124255        int cons_phone = console_phone_get(true);
     256
     257        cbuffer_flush();
    125258        async_msg_1(cons_phone, CONSOLE_SET_STYLE, style);
    126259}
     
    129262{
    130263        int cons_phone = console_phone_get(true);
     264
     265        cbuffer_flush();
    131266        async_msg_3(cons_phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags);
    132267}
     
    135270{
    136271        int cons_phone = console_phone_get(true);
     272
     273        cbuffer_flush();
    137274        async_msg_2(cons_phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color);
    138275}
     
    141278{
    142279        int cons_phone = console_phone_get(true);
     280
     281        cbuffer_flush();
    143282        async_msg_1(cons_phone, CONSOLE_CURSOR_VISIBILITY, show != 0);
    144283}
Note: See TracChangeset for help on using the changeset viewer.