Subversion Repositories HelenOS

Rev

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

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