Subversion Repositories HelenOS

Rev

Rev 4153 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4153 Rev 4263
Line 1... Line 1...
1
/*
1
/*
-
 
2
 * Copyright (c) 2006 Josef Cejka
-
 
3
 * Copyright (c) 2006 Jakub Vana
2
 * Copyright (c) 2008 Jiri Svoboda
4
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
5
 * All rights reserved.
4
 *
6
 *
5
 * Redistribution and use in source and binary forms, with or without
7
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
8
 * modification, are permitted provided that the following conditions
Line 33... Line 35...
33
 */
35
 */
34
 
36
 
35
#include <async.h>
37
#include <async.h>
36
#include <io/stream.h>
38
#include <io/stream.h>
37
#include <ipc/console.h>
39
#include <ipc/console.h>
-
 
40
#include <ipc/services.h>
-
 
41
#include <errno.h>
-
 
42
#include <string.h>
38
#include <console.h>
43
#include <console.h>
39
 
44
 
-
 
45
static int console_phone = -1;
-
 
46
 
-
 
47
/** Size of cbuffer. */
-
 
48
#define CBUFFER_SIZE 256
-
 
49
 
-
 
50
/** Buffer for writing characters to the console. */
-
 
51
static char cbuffer[CBUFFER_SIZE];
-
 
52
 
-
 
53
/** Pointer to end of cbuffer. */
-
 
54
static char *cbuffer_end = cbuffer + CBUFFER_SIZE;
-
 
55
 
-
 
56
/** Pointer to first available field in cbuffer. */
-
 
57
static char *cbp = cbuffer;
-
 
58
 
-
 
59
static ssize_t cons_write(const char *buf, size_t nbyte);
-
 
60
static void cons_putchar(wchar_t c);
-
 
61
 
-
 
62
static void cbuffer_flush(void);
-
 
63
static void cbuffer_drain(void);
-
 
64
static inline void cbuffer_putc(int c);
-
 
65
 
-
 
66
 
-
 
67
void console_open(bool blocking)
-
 
68
{
-
 
69
    if (console_phone < 0) {
-
 
70
        int phone;
-
 
71
        if (blocking) {
-
 
72
            phone = ipc_connect_me_to_blocking(PHONE_NS,
-
 
73
                SERVICE_CONSOLE, 0, 0);
-
 
74
        } else {
-
 
75
            phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0,
-
 
76
                0);
-
 
77
        }
-
 
78
        if (phone >= 0)
-
 
79
            console_phone = phone;
-
 
80
    }
-
 
81
}
-
 
82
 
-
 
83
void console_close(void)
-
 
84
{
-
 
85
    if (console_phone >= 0) {
-
 
86
        if (ipc_hangup(console_phone) == 0) {
-
 
87
            console_phone = -1;
-
 
88
        }
-
 
89
    }
-
 
90
}
-
 
91
 
-
 
92
int console_phone_get(bool blocking)
-
 
93
{
-
 
94
    if (console_phone < 0)
-
 
95
        console_open(blocking);
-
 
96
   
-
 
97
    return console_phone;
-
 
98
}
-
 
99
 
-
 
100
void console_wait(void)
-
 
101
{
-
 
102
    while (console_phone < 0)
-
 
103
        console_open(true);
-
 
104
}
-
 
105
 
40
void console_clear(void)
106
void console_clear(void)
41
{
107
{
42
    int cons_phone = get_console_phone();
108
    int cons_phone = console_phone_get(true);
-
 
109
 
-
 
110
    cbuffer_drain();
43
    async_msg_0(cons_phone, CONSOLE_CLEAR);
111
    async_msg_0(cons_phone, CONSOLE_CLEAR);
44
}
112
}
45
 
113
 
46
void console_goto(int row, int col)
114
void console_goto(int row, int col)
47
{
115
{
48
    int cons_phone = get_console_phone();
116
    int cons_phone = console_phone_get(true);
-
 
117
 
-
 
118
    cbuffer_flush();
49
    async_msg_2(cons_phone, CONSOLE_GOTO, row, col);
119
    async_msg_2(cons_phone, CONSOLE_GOTO, row, col);
50
}
120
}
51
 
121
 
-
 
122
void console_putchar(wchar_t c)
-
 
123
{
-
 
124
//  cbuffer_putc(c);
-
 
125
    cbuffer_flush();
-
 
126
    cons_putchar(c);
-
 
127
}
-
 
128
 
-
 
129
/** Write all data from output buffer to the console. */
-
 
130
static void cbuffer_flush(void)
-
 
131
{
-
 
132
    int rc;
-
 
133
    int len;
-
 
134
 
-
 
135
    len = cbp - cbuffer;
-
 
136
 
-
 
137
    while (len > 0) {
-
 
138
        rc = cons_write(cbuffer, cbp - cbuffer);
-
 
139
        if (rc < 0)
-
 
140
            return;
-
 
141
 
-
 
142
        len -= rc;
-
 
143
    }
-
 
144
 
-
 
145
    cbp = cbuffer;
-
 
146
}
-
 
147
 
-
 
148
/** Drop all data in console output buffer. */
-
 
149
static void cbuffer_drain(void)
-
 
150
{
-
 
151
    cbp = cbuffer;
-
 
152
}
-
 
153
 
-
 
154
/** Write one character to the output buffer. */
-
 
155
static inline void cbuffer_putc(int c)
-
 
156
{
-
 
157
    if (cbp == cbuffer_end)
-
 
158
        cbuffer_flush();
-
 
159
 
-
 
160
    *cbp++ = c;
-
 
161
 
-
 
162
    if (c == '\n')
-
 
163
        cbuffer_flush();
-
 
164
}
-
 
165
 
-
 
166
/** Write one character to the console via IPC. */
-
 
167
static void cons_putchar(wchar_t c)
-
 
168
{
-
 
169
    int cons_phone = console_phone_get(true);
-
 
170
    async_msg_1(cons_phone, CONSOLE_PUTCHAR, c);
-
 
171
}
-
 
172
 
-
 
173
/** Write characters to the console via IPC. */
-
 
174
static ssize_t cons_write(const char *buf, size_t nbyte)
-
 
175
{
-
 
176
    int cons_phone = console_phone_get(true);
-
 
177
    ipcarg_t rc;
-
 
178
    ipc_call_t answer;
-
 
179
    aid_t req;
-
 
180
 
-
 
181
    async_serialize_start();
-
 
182
   
-
 
183
    req = async_send_0(cons_phone, CONSOLE_WRITE, &answer);
-
 
184
    rc = ipc_data_write_start(cons_phone, (void *) buf, nbyte);
-
 
185
 
-
 
186
    if (rc != EOK) {
-
 
187
        async_wait_for(req, NULL);
-
 
188
        async_serialize_end();
-
 
189
        return (ssize_t) rc;
-
 
190
    }
-
 
191
 
-
 
192
    async_wait_for(req, &rc);
-
 
193
    async_serialize_end();
-
 
194
 
-
 
195
    if (rc == EOK)
-
 
196
        return (ssize_t) IPC_GET_ARG1(answer);
-
 
197
    else
-
 
198
        return -1;
-
 
199
}
-
 
200
 
-
 
201
/** Write characters to the console. */
-
 
202
ssize_t console_write(const char *buf, size_t nbyte)
-
 
203
{
-
 
204
    size_t left;
-
 
205
 
-
 
206
    left = nbyte;
-
 
207
 
-
 
208
    while (left > 0) {
-
 
209
        cbuffer_putc(*buf++);
-
 
210
        --left;
-
 
211
    }
-
 
212
 
-
 
213
    return nbyte;
-
 
214
}
-
 
215
 
-
 
216
/** Write a NULL-terminated string to the console. */
-
 
217
void console_putstr(const char *s)
-
 
218
{
-
 
219
    size_t len;
-
 
220
    ssize_t rc;
-
 
221
 
-
 
222
    len = strlen(s);
-
 
223
    while (len > 0) {
-
 
224
        rc = console_write(s, len);
-
 
225
        if (rc < 0)
-
 
226
            return; /* Error */
-
 
227
        s += rc;
-
 
228
        len -= rc;
-
 
229
    }
-
 
230
}
-
 
231
 
-
 
232
/** Flush all output to the console. */
52
void console_flush(void)
233
void console_flush(void)
53
{
234
{
54
    int cons_phone = get_console_phone();
235
    int cons_phone = console_phone_get(false);
-
 
236
 
-
 
237
    cbuffer_flush();
55
    async_msg_0(cons_phone, CONSOLE_FLUSH);
238
    async_msg_0(cons_phone, CONSOLE_FLUSH);
56
}
239
}
57
 
240
 
58
int console_get_size(int *rows, int *cols)
241
int console_get_size(int *rows, int *cols)
59
{
242
{
60
    int cons_phone = get_console_phone();
243
    int cons_phone = console_phone_get(true);
61
    ipcarg_t r, c;
244
    ipcarg_t r, c;
62
    int rc;
245
    int rc;
63
 
246
 
64
    rc = async_req_0_2(cons_phone, CONSOLE_GETSIZE, &r, &c);
247
    rc = async_req_0_2(cons_phone, CONSOLE_GETSIZE, &r, &c);
65
 
248
 
Line 69... Line 252...
69
    return rc;
252
    return rc;
70
}
253
}
71
 
254
 
72
void console_set_style(int style)
255
void console_set_style(int style)
73
{
256
{
74
    int cons_phone = get_console_phone();
257
    int cons_phone = console_phone_get(true);
-
 
258
 
-
 
259
    cbuffer_flush();
75
    async_msg_1(cons_phone, CONSOLE_SET_STYLE, style);
260
    async_msg_1(cons_phone, CONSOLE_SET_STYLE, style);
76
}
261
}
77
 
262
 
78
void console_set_color(int fg_color, int bg_color, int flags)
263
void console_set_color(int fg_color, int bg_color, int flags)
79
{
264
{
80
    int cons_phone = get_console_phone();
265
    int cons_phone = console_phone_get(true);
-
 
266
 
-
 
267
    cbuffer_flush();
81
    async_msg_3(cons_phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags);
268
    async_msg_3(cons_phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags);
82
}
269
}
83
 
270
 
84
void console_set_rgb_color(int fg_color, int bg_color)
271
void console_set_rgb_color(int fg_color, int bg_color)
85
{
272
{
86
    int cons_phone = get_console_phone();
273
    int cons_phone = console_phone_get(true);
-
 
274
 
-
 
275
    cbuffer_flush();
87
    async_msg_2(cons_phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color);
276
    async_msg_2(cons_phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color);
88
}
277
}
89
 
278
 
90
void console_cursor_visibility(int show)
279
void console_cursor_visibility(int show)
91
{
280
{
92
    int cons_phone = get_console_phone();
281
    int cons_phone = console_phone_get(true);
-
 
282
 
-
 
283
    cbuffer_flush();
93
    async_msg_1(cons_phone, CONSOLE_CURSOR_VISIBILITY, show != 0);
284
    async_msg_1(cons_phone, CONSOLE_CURSOR_VISIBILITY, show != 0);
94
}
285
}
95
 
286
 
-
 
287
void console_kcon_enable(void)
-
 
288
{
-
 
289
    int cons_phone = console_phone_get(true);
-
 
290
 
-
 
291
    cbuffer_flush();
-
 
292
    async_msg_0(cons_phone, CONSOLE_KCON_ENABLE);
-
 
293
}
-
 
294
 
96
/** @}
295
/** @}
97
 */
296
 */