Subversion Repositories HelenOS-historic

Rev

Rev 1489 | Rev 1497 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1489 Rev 1490
1
/*
1
/*
2
 * Copyright (C) 2006 Josef Cejka
2
 * Copyright (C) 2006 Josef Cejka
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
 
29
 
30
#include <kbd.h>
30
#include <kbd.h>
31
#include <fb.h>
31
#include <fb.h>
32
#include <ipc/ipc.h>
32
#include <ipc/ipc.h>
33
#include <ipc/fb.h>
33
#include <ipc/fb.h>
34
#include <ipc/services.h>
34
#include <ipc/services.h>
35
#include <errno.h>
35
#include <errno.h>
36
#include <key_buffer.h>
36
#include <key_buffer.h>
37
#include <console.h>
37
#include <console.h>
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <async.h>
39
#include <async.h>
40
#include <libadt/fifo.h>
40
#include <libadt/fifo.h>
41
#include <screenbuffer.h>
41
#include <screenbuffer.h>
42
 
42
 
43
#define CONSOLE_COUNT 8
43
#define CONSOLE_COUNT 8
44
#define MAX_KEYREQUESTS_BUFFERED 32
44
#define MAX_KEYREQUESTS_BUFFERED 32
45
 
45
 
46
#define NAME "CONSOLE"
46
#define NAME "CONSOLE"
47
 
47
 
48
int active_console = 1;
48
int active_console = 1;
49
 
49
 
50
struct {
50
struct {
51
    int phone;      /**< Framebuffer phone */
51
    int phone;      /**< Framebuffer phone */
52
    int rows;       /**< Framebuffer rows */
52
    int rows;       /**< Framebuffer rows */
53
    int cols;       /**< Framebuffer columns */
53
    int cols;       /**< Framebuffer columns */
54
} fb_info;
54
} fb_info;
55
 
55
 
56
typedef struct {
56
typedef struct {
57
    keybuffer_t keybuffer;
57
    keybuffer_t keybuffer;
58
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
58
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
59
    int keyrequest_counter;
59
    int keyrequest_counter;
60
    int client_phone;
60
    int client_phone;
61
    int used;
61
    int used;
62
    screenbuffer_t screenbuffer;
62
    screenbuffer_t screenbuffer;
63
} connection_t;
63
} connection_t;
64
 
64
 
65
connection_t connections[CONSOLE_COUNT];
65
connection_t connections[CONSOLE_COUNT];
66
 
66
 
67
static int find_free_connection()
67
static int find_free_connection()
68
{
68
{
69
    int i = 0;
69
    int i = 0;
70
   
70
   
71
    while (i < CONSOLE_COUNT) {
71
    while (i < CONSOLE_COUNT) {
72
        if (connections[i].used == 0)
72
        if (connections[i].used == 0)
73
            return i;
73
            return i;
74
        ++i;
74
        ++i;
75
    }
75
    }
76
    return CONSOLE_COUNT;
76
    return CONSOLE_COUNT;
77
}
77
}
78
 
78
 
79
 
79
 
80
static int find_connection(int client_phone)
80
static int find_connection(int client_phone)
81
{
81
{
82
    int i = 0;
82
    int i = 0;
83
   
83
   
84
    while (i < CONSOLE_COUNT) {
84
    while (i < CONSOLE_COUNT) {
85
        if (connections[i].client_phone == client_phone)
85
        if (connections[i].client_phone == client_phone)
86
            return i;
86
            return i;
87
        ++i;
87
        ++i;
88
    }
88
    }
89
    return  CONSOLE_COUNT;
89
    return  CONSOLE_COUNT;
90
}
90
}
91
 
91
 
92
/* Handler for keyboard */
92
/* Handler for keyboard */
93
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
93
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
94
{
94
{
95
    ipc_callid_t callid;
95
    ipc_callid_t callid;
96
    ipc_call_t call;
96
    ipc_call_t call;
97
    int retval;
97
    int retval;
98
    int i, j;
98
    int i, j;
99
    char c,d;
99
    char c,d;
100
    connection_t *conn;
100
    connection_t *conn;
101
 
101
 
102
    /* Ignore parameters, the connection is alread opened */
102
    /* Ignore parameters, the connection is alread opened */
103
    while (1) {
103
    while (1) {
104
        callid = async_get_call(&call);
104
        callid = async_get_call(&call);
105
        switch (IPC_GET_METHOD(call)) {
105
        switch (IPC_GET_METHOD(call)) {
106
        case IPC_M_PHONE_HUNGUP:
106
        case IPC_M_PHONE_HUNGUP:
107
            ipc_answer_fast(callid,0,0,0);
107
            ipc_answer_fast(callid,0,0,0);
108
            /* TODO: Handle hangup */
108
            /* TODO: Handle hangup */
109
            return;
109
            return;
110
        case KBD_PUSHCHAR:
110
        case KBD_PUSHCHAR:
111
            /* got key from keyboard driver */
111
            /* got key from keyboard driver */
112
           
112
           
113
            retval = 0;
113
            retval = 0;
114
            c = IPC_GET_ARG1(call);
114
            c = IPC_GET_ARG1(call);
115
            /* switch to another virtual console */
115
            /* switch to another virtual console */
116
           
116
           
117
            conn = &connections[active_console];
117
            conn = &connections[active_console];
118
            if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
118
//          if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
-
 
119
            if ((c >= '1') && (c < '1' + CONSOLE_COUNT)) {
119
                /*FIXME: draw another console content from buffer */
120
                /*FIXME: draw another console content from buffer */
120
 
121
 
121
                active_console = c - KBD_KEY_F1;
122
                active_console = c - '1';
122
                conn = &connections[active_console];
123
                conn = &connections[active_console];
123
 
124
 
124
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
125
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 0, NULL, NULL);
125
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
126
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
126
                for (i = 0; i < conn->screenbuffer.size_x; i++)
127
                for (i = 0; i < conn->screenbuffer.size_x; i++)
127
                    for (j = 0; j < conn->screenbuffer.size_y; j++) {
128
                    for (j = 0; j < conn->screenbuffer.size_y; j++) {
128
                        d = get_field_at(&(conn->screenbuffer),i, j)->character;
129
                        d = get_field_at(&(conn->screenbuffer),i, j)->character;
129
                        if (d && d != ' ')
130
                        if (d && d != ' ')
130
                            ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
131
                            ipc_call_async_3(fb_info.phone, FB_PUTCHAR, d, j, i, NULL, NULL);
131
                    }
132
                    }
132
                ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
133
                ipc_call_async_2(fb_info.phone, FB_CURSOR_GOTO, conn->screenbuffer.position_y, conn->screenbuffer.position_x, NULL, NULL);
133
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
134
                ipc_call_async(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL, NULL);
134
 
135
 
135
                break;
136
                break;
136
            }
137
            }
137
           
138
           
138
            /* if client is awaiting key, send it */
139
            /* if client is awaiting key, send it */
139
            if (conn->keyrequest_counter > 0) {    
140
            if (conn->keyrequest_counter > 0) {    
140
                conn->keyrequest_counter--;
141
                conn->keyrequest_counter--;
141
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
142
                ipc_answer_fast(fifo_pop(conn->keyrequests), 0, c, 0);
142
                break;
143
                break;
143
            }
144
            }
144
           
145
           
145
            /*FIXME: else store key to its buffer */
146
            /*FIXME: else store key to its buffer */
146
            keybuffer_push(&conn->keybuffer, c);
147
            keybuffer_push(&conn->keybuffer, c);
147
           
148
           
148
            break;
149
            break;
149
        default:
150
        default:
150
            retval = ENOENT;
151
            retval = ENOENT;
151
        }      
152
        }      
152
        ipc_answer_fast(callid, retval, 0, 0);
153
        ipc_answer_fast(callid, retval, 0, 0);
153
    }
154
    }
154
}
155
}
155
 
156
 
156
/** Default thread for new connections */
157
/** Default thread for new connections */
157
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
158
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
158
{
159
{
159
    ipc_callid_t callid;
160
    ipc_callid_t callid;
160
    ipc_call_t call;
161
    ipc_call_t call;
161
    int consnum;
162
    int consnum;
162
    ipcarg_t arg1;
163
    ipcarg_t arg1;
163
 
164
 
164
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
165
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
165
        ipc_answer_fast(iid,ELIMIT,0,0);
166
        ipc_answer_fast(iid,ELIMIT,0,0);
166
        return;
167
        return;
167
    }
168
    }
168
   
169
   
169
    connections[consnum].used = 1;
170
    connections[consnum].used = 1;
170
    connections[consnum].client_phone = IPC_GET_ARG3(call);
171
    connections[consnum].client_phone = IPC_GET_ARG3(call);
171
    screenbuffer_clear(&(connections[consnum].screenbuffer));
172
    screenbuffer_clear(&(connections[consnum].screenbuffer));
172
 
173
 
173
    /* Accept the connection */
174
    /* Accept the connection */
174
    ipc_answer_fast(iid,0,0,0);
175
    ipc_answer_fast(iid,0,0,0);
175
   
176
   
176
    while (1) {
177
    while (1) {
177
        callid = async_get_call(&call);
178
        callid = async_get_call(&call);
178
        switch (IPC_GET_METHOD(call)) {
179
        switch (IPC_GET_METHOD(call)) {
179
        case IPC_M_PHONE_HUNGUP:
180
        case IPC_M_PHONE_HUNGUP:
180
            /* TODO */
181
            /* TODO */
181
            ipc_answer_fast(callid, 0,0,0);
182
            ipc_answer_fast(callid, 0,0,0);
182
            return;
183
            return;
183
        case CONSOLE_PUTCHAR:
184
        case CONSOLE_PUTCHAR:
184
           
185
           
185
            /* Send message to fb */
186
            /* Send message to fb */
186
            if (consnum == active_console) {
187
            if (consnum == active_console) {
187
                ipc_call_async_3(fb_info.phone, FB_PUTCHAR, IPC_GET_ARG2(call), connections[consnum].screenbuffer.position_y, \
188
                ipc_call_async_3(fb_info.phone, FB_PUTCHAR, IPC_GET_ARG2(call), connections[consnum].screenbuffer.position_y, \
188
                        connections[consnum].screenbuffer.position_x, NULL, NULL);
189
                        connections[consnum].screenbuffer.position_x, NULL, NULL);
189
            }
190
            }
190
           
191
           
191
            screenbuffer_putchar(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call));
192
            screenbuffer_putchar(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call));
192
            break;
193
            break;
193
        case CONSOLE_CLEAR:
194
        case CONSOLE_CLEAR:
194
            /* Send message to fb */
195
            /* Send message to fb */
195
            if (consnum == active_console) {
196
            if (consnum == active_console) {
196
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
197
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
197
            }
198
            }
198
           
199
           
199
            screenbuffer_clear(&(connections[consnum].screenbuffer));
200
            screenbuffer_clear(&(connections[consnum].screenbuffer));
200
           
201
           
201
            break;
202
            break;
202
        case CONSOLE_GOTO:
203
        case CONSOLE_GOTO:
203
           
204
           
204
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
205
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
205
           
206
           
206
            break;
207
            break;
207
 
208
 
208
        case CONSOLE_GETCHAR:
209
        case CONSOLE_GETCHAR:
209
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
210
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
210
                /* buffer is empty -> store request */
211
                /* buffer is empty -> store request */
211
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
212
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
212
                    fifo_push(connections[consnum].keyrequests, callid);
213
                    fifo_push(connections[consnum].keyrequests, callid);
213
                    connections[consnum].keyrequest_counter++;
214
                    connections[consnum].keyrequest_counter++;
214
                } else {
215
                } else {
215
                    /* no key available and too many requests => fail */
216
                    /* no key available and too many requests => fail */
216
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
217
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
217
                }
218
                }
218
                continue;
219
                continue;
219
            };
220
            };
220
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
221
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
221
           
222
           
222
            break;
223
            break;
223
        }
224
        }
224
        ipc_answer_fast(callid, 0, arg1, 0);
225
        ipc_answer_fast(callid, 0, arg1, 0);
225
    }
226
    }
226
}
227
}
227
 
228
 
228
int main(int argc, char *argv[])
229
int main(int argc, char *argv[])
229
{
230
{
230
    ipcarg_t phonehash;
231
    ipcarg_t phonehash;
231
    int kbd_phone, fb_phone;
232
    int kbd_phone, fb_phone;
232
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
233
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
233
    int i;
234
    int i;
-
 
235
 
-
 
236
    async_set_client_connection(client_connection);
234
   
237
   
235
    /* Connect to keyboard driver */
238
    /* Connect to keyboard driver */
236
 
239
 
237
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
240
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
238
        usleep(10000);
241
        usleep(10000);
239
    };
242
    };
240
   
243
   
241
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
244
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
242
        return -1;
245
        return -1;
243
    };
246
    };
244
    async_new_connection(phonehash, 0, NULL, keyboard_events);
247
    async_new_connection(phonehash, 0, NULL, keyboard_events);
245
 
248
 
246
    /* Connect to framebuffer driver */
249
    /* Connect to framebuffer driver */
247
   
250
   
248
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
251
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
249
        usleep(10000);
252
        usleep(10000);
250
    }
253
    }
251
   
254
   
252
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
255
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
253
    ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
256
    ipc_call_sync(fb_info.phone, FB_CURSOR_VISIBILITY, 1, NULL);
254
   
257
   
255
    /* Init virtual consoles */
258
    /* Init virtual consoles */
256
    for (i = 0; i < CONSOLE_COUNT; i++) {
259
    for (i = 0; i < CONSOLE_COUNT; i++) {
257
        connections[i].used = 0;
260
        connections[i].used = 0;
258
        keybuffer_init(&(connections[i].keybuffer));
261
        keybuffer_init(&(connections[i].keybuffer));
259
       
262
       
260
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
263
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
261
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
264
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
262
        connections[i].keyrequest_counter = 0;
265
        connections[i].keyrequest_counter = 0;
263
       
266
       
264
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
267
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
265
            /*FIXME: handle error */
268
            /*FIXME: handle error */
266
            return -1;
269
            return -1;
267
        }
270
        }
268
    }
271
    }
269
   
272
   
270
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
273
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
271
        return -1;
274
        return -1;
272
    };
275
    };
273
   
276
   
274
    async_manager();
277
    async_manager();
275
 
278
 
276
    return 0;  
279
    return 0;  
277
}
280
}
278
 
281