Subversion Repositories HelenOS

Rev

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

Rev 1481 Rev 1487
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
 
42
 
42
static void sysput(char c)
43
static void sysput(char c)
43
{
44
{
44
    __SYSCALL3(SYS_IO, 1, &c, 1);
45
    __SYSCALL3(SYS_IO, 1, &c, 1);
45
 
-
 
46
}
46
}
-
 
47
 
47
//#define CONSOLE_COUNT VFB_CONNECTIONS
48
//#define CONSOLE_COUNT VFB_CONNECTIONS
48
#define CONSOLE_COUNT 8
49
#define CONSOLE_COUNT 8
49
#define MAX_KEYREQUESTS_BUFFERED 32
50
#define MAX_KEYREQUESTS_BUFFERED 32
50
 
51
 
51
#define NAME "CONSOLE"
52
#define NAME "CONSOLE"
52
 
53
 
53
int active_console = 1;
54
int active_console = 1;
54
 
55
 
-
 
56
struct {
-
 
57
    int phone;      /**< Framebuffer phone */
-
 
58
    int rows;       /**< Framebuffer rows */
-
 
59
    int cols;       /**< Framebuffer columns */
-
 
60
} fb_info;
-
 
61
 
55
typedef struct {
62
typedef struct {
56
    keybuffer_t keybuffer;
63
    keybuffer_t keybuffer;
57
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
64
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
58
    int keyrequest_counter;
65
    int keyrequest_counter;
59
    int client_phone;
66
    int client_phone;
60
    int vfb_phone;
-
 
61
    int used;
67
    int used;
-
 
68
    screenbuffer_t screenbuffer;
62
} connection_t;
69
} connection_t;
63
 
70
 
64
connection_t connections[CONSOLE_COUNT];
71
connection_t connections[CONSOLE_COUNT];
65
 
72
 
66
static int find_free_connection()
73
static int find_free_connection()
67
{
74
{
68
    int i = 0;
75
    int i = 0;
69
   
76
   
70
    while (i < CONSOLE_COUNT) {
77
    while (i < CONSOLE_COUNT) {
71
        if (connections[i].used == 0)
78
        if (connections[i].used == 0)
72
            return i;
79
            return i;
73
        ++i;
80
        ++i;
74
    }
81
    }
75
    return CONSOLE_COUNT;
82
    return CONSOLE_COUNT;
76
}
83
}
77
 
84
 
78
 
85
 
79
static int find_connection(int client_phone)
86
static int find_connection(int client_phone)
80
{
87
{
81
    int i = 0;
88
    int i = 0;
82
   
89
   
83
    while (i < CONSOLE_COUNT) {
90
    while (i < CONSOLE_COUNT) {
84
        if (connections[i].client_phone == client_phone)
91
        if (connections[i].client_phone == client_phone)
85
            return i;
92
            return i;
86
        ++i;
93
        ++i;
87
    }
94
    }
88
    return  CONSOLE_COUNT;
95
    return  CONSOLE_COUNT;
89
}
96
}
90
 
97
 
91
/* Handler for keyboard */
98
/* Handler for keyboard */
92
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
99
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
93
{
100
{
94
    ipc_callid_t callid;
101
    ipc_callid_t callid;
95
    ipc_call_t call;
102
    ipc_call_t call;
96
    int retval;
103
    int retval;
97
    int i;
104
    int i, j;
98
    char c;
105
    char c;
99
 
106
 
100
    /* Ignore parameters, the connection is alread opened */
107
    /* Ignore parameters, the connection is alread opened */
101
    while (1) {
108
    while (1) {
102
        callid = async_get_call(&call);
109
        callid = async_get_call(&call);
103
        switch (IPC_GET_METHOD(call)) {
110
        switch (IPC_GET_METHOD(call)) {
104
        case IPC_M_PHONE_HUNGUP:
111
        case IPC_M_PHONE_HUNGUP:
105
            ipc_answer_fast(callid,0,0,0);
112
            ipc_answer_fast(callid,0,0,0);
106
            /* TODO: Handle hangup */
113
            /* TODO: Handle hangup */
107
            return;
114
            return;
108
        case KBD_PUSHCHAR:
115
        case KBD_PUSHCHAR:
109
            /* got key from keyboard driver */
116
            /* got key from keyboard driver */
110
           
117
           
111
            retval = 0;
118
            retval = 0;
112
            c = IPC_GET_ARG1(call);
119
            c = IPC_GET_ARG1(call);
113
//          ipc_call_sync_2(connections[3].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
120
//          ipc_call_sync_2(connections[3].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
114
       
121
       
115
            /* switch to another virtual console */
122
            /* switch to another virtual console */
116
           
123
           
117
            if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
124
            if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
-
 
125
                /*FIXME: draw another console content from buffer */
-
 
126
 
118
                active_console = c - KBD_KEY_F1;
127
                active_console = c - KBD_KEY_F1;
-
 
128
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
-
 
129
                ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, 'a', 0, 0,NULL,NULL, NULL);
-
 
130
               
-
 
131
                for (i = 0; i < connections[active_console].screenbuffer.size_x; i++)
-
 
132
                    for (j = 0; j < connections[active_console].screenbuffer.size_y; j++) {
-
 
133
                   
-
 
134
                    ipc_call_async_3(fb_info.phone, FB_PUTCHAR, get_field_at(&(connections[active_console].screenbuffer),\
-
 
135
                                i, j)->character, j, i, NULL, NULL, NULL);
-
 
136
                }
-
 
137
 
119
                break;
138
                break;
120
            }
139
            }
121
           
140
           
122
            /* if client is awaiting key, send it */
141
            /* if client is awaiting key, send it */
123
            if (connections[active_console].keyrequest_counter > 0) {      
142
            if (connections[active_console].keyrequest_counter > 0) {      
124
                connections[active_console].keyrequest_counter--;
143
                connections[active_console].keyrequest_counter--;
125
                ipc_answer_fast(fifo_pop(connections[active_console].keyrequests), 0, c, 0);
144
                ipc_answer_fast(fifo_pop(connections[active_console].keyrequests), 0, c, 0);
126
                break;
145
                break;
127
            }
146
            }
128
           
147
           
129
            /*FIXME: else store key to its buffer */
148
            /*FIXME: else store key to its buffer */
130
            keybuffer_push(&(connections[active_console].keybuffer), c);
149
            keybuffer_push(&(connections[active_console].keybuffer), c);
131
           
150
           
132
            /* Send it to first FB, DEBUG */
151
            /* Send it to first FB, DEBUG */
133
//          ipc_call_async_2(connections[0].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG1(call),NULL,NULL);
152
//          ipc_call_async_2(connections[0].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG1(call),NULL,NULL);
134
//          ipc_call_sync_2(connections[4].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
153
//          ipc_call_sync_2(connections[4].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
135
 
154
 
136
            break;
155
            break;
137
        default:
156
        default:
138
            retval = ENOENT;
157
            retval = ENOENT;
139
        }      
158
        }      
140
        ipc_answer_fast(callid, retval, 0, 0);
159
        ipc_answer_fast(callid, retval, 0, 0);
141
    }
160
    }
142
}
161
}
143
 
162
 
144
/** Default thread for new connections */
163
/** Default thread for new connections */
145
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
164
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
146
{
165
{
147
    ipc_callid_t callid;
166
    ipc_callid_t callid;
148
    ipc_call_t call;
167
    ipc_call_t call;
149
    int consnum;
168
    int consnum;
150
    ipcarg_t arg1;
169
    ipcarg_t arg1;
151
 
170
 
152
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
171
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
153
        ipc_answer_fast(iid,ELIMIT,0,0);
172
        ipc_answer_fast(iid,ELIMIT,0,0);
154
        return;
173
        return;
155
    }
174
    }
-
 
175
   
156
    connections[consnum].used = 1;
176
    connections[consnum].used = 1;
157
    connections[consnum].client_phone = IPC_GET_ARG3(call);
177
    connections[consnum].client_phone = IPC_GET_ARG3(call);
-
 
178
    screenbuffer_clear(&(connections[consnum].screenbuffer));
158
 
179
 
159
    /* Accept the connection */
180
    /* Accept the connection */
160
    ipc_answer_fast(iid,0,0,0);
181
    ipc_answer_fast(iid,0,0,0);
161
   
182
   
162
    while (1) {
183
    while (1) {
163
        callid = async_get_call(&call);
184
        callid = async_get_call(&call);
164
        switch (IPC_GET_METHOD(call)) {
185
        switch (IPC_GET_METHOD(call)) {
165
        case IPC_M_PHONE_HUNGUP:
186
        case IPC_M_PHONE_HUNGUP:
166
            /* TODO */
187
            /* TODO */
167
            ipc_answer_fast(callid, 0,0,0);
188
            ipc_answer_fast(callid, 0,0,0);
168
            return;
189
            return;
169
        case CONSOLE_PUTCHAR:
190
        case CONSOLE_PUTCHAR:
170
            if (consnum != active_console) {
-
 
171
            }
191
           
172
            /* Send message to fb */
192
            /* Send message to fb */
-
 
193
            if (consnum == active_console) {
173
            ipc_call_sync_2(connections[consnum].vfb_phone, FB_PUTCHAR, IPC_GET_ARG1(call), IPC_GET_ARG2(call), NULL, NULL);
194
                ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, IPC_GET_ARG2(call), connections[consnum].screenbuffer.position_y, \
-
 
195
                        connections[consnum].screenbuffer.position_x, NULL, NULL, NULL);
-
 
196
            }
-
 
197
           
174
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG2(call),NULL,NULL);
198
            screenbuffer_putchar(&(connections[consnum].screenbuffer), IPC_GET_ARG2(call));
175
            break;
199
            break;
176
        case CONSOLE_CLEAR:
200
        case CONSOLE_CLEAR:
-
 
201
            /* Send message to fb */
-
 
202
            if (consnum == active_console) {
-
 
203
                ipc_call_async_2(fb_info.phone, FB_CLEAR, 0, 0, NULL, NULL);
-
 
204
            }
-
 
205
           
-
 
206
            screenbuffer_clear(&(connections[consnum].screenbuffer));
-
 
207
           
177
            break;
208
            break;
178
        case CONSOLE_GOTO:
209
        case CONSOLE_GOTO:
-
 
210
           
-
 
211
            screenbuffer_goto(&(connections[consnum].screenbuffer), IPC_GET_ARG1(call), IPC_GET_ARG2(call));
-
 
212
           
179
            break;
213
            break;
180
 
214
 
181
        case CONSOLE_GETCHAR:
215
        case CONSOLE_GETCHAR:
182
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
216
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
183
                /* buffer is empty -> store request */
217
                /* buffer is empty -> store request */
184
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
218
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
185
                    fifo_push(connections[consnum].keyrequests, callid);
219
                    fifo_push(connections[consnum].keyrequests, callid);
186
                    connections[consnum].keyrequest_counter++;
220
                    connections[consnum].keyrequest_counter++;
187
                } else {
221
                } else {
188
                    /* no key available and too many requests => fail */
222
                    /* no key available and too many requests => fail */
189
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
223
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
190
                }
224
                }
191
                continue;
225
                continue;
192
            };
226
            };
193
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
227
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
194
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, arg1,NULL,NULL);
228
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, arg1,NULL,NULL);
195
           
229
           
196
            break;
230
            break;
197
        }
231
        }
198
        ipc_answer_fast(callid, 0, arg1, 0);
232
        ipc_answer_fast(callid, 0, arg1, 0);
199
    }
233
    }
200
}
234
}
201
 
235
 
202
int main(int argc, char *argv[])
236
int main(int argc, char *argv[])
203
{
237
{
204
    ipcarg_t phonehash;
238
    ipcarg_t phonehash;
205
    int kbd_phone, fb_phone;
239
    int kbd_phone, fb_phone;
206
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
240
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
207
    int i;
241
    int i;
208
   
242
   
209
    /* Connect to keyboard driver */
243
    /* Connect to keyboard driver */
210
 
244
 
211
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
245
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
212
        usleep(10000);
246
        usleep(10000);
213
    };
247
    };
214
   
248
   
215
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
249
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
216
        return -1;
250
        return -1;
217
    };
251
    };
218
    async_new_connection(phonehash, 0, NULL, keyboard_events);
252
    async_new_connection(phonehash, 0, NULL, keyboard_events);
219
 
253
 
220
    /* Connect to framebuffer driver */
254
    /* Connect to framebuffer driver */
221
   
255
   
-
 
256
    while ((fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
-
 
257
        usleep(10000);
-
 
258
    }
-
 
259
   
-
 
260
    ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, '1', 0, 0,NULL,NULL, NULL);
-
 
261
    ipc_call_sync_2(fb_info.phone, FB_GET_CSIZE, 0, 0, &(fb_info.rows), &(fb_info.cols));
-
 
262
   
-
 
263
    /* Init virtual consoles */
222
    for (i = 0; i < CONSOLE_COUNT; i++) {
264
    for (i = 0; i < CONSOLE_COUNT; i++) {
-
 
265
        ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, '$', 2*i, 1,NULL,NULL, NULL);
223
        connections[i].used = 0;
266
        connections[i].used = 0;
224
        keybuffer_init(&(connections[i].keybuffer));
267
        keybuffer_init(&(connections[i].keybuffer));
-
 
268
        ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, '>', 2*i+1, 1,NULL,NULL, NULL);
225
       
269
       
226
        /* TODO: init key_buffer */
-
 
227
        while ((connections[i].vfb_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
-
 
228
            usleep(10000);
-
 
229
        }
-
 
230
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
270
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
231
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
271
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
232
        connections[i].keyrequest_counter = 0;
272
        connections[i].keyrequest_counter = 0;
-
 
273
       
-
 
274
        ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, '?', 2*i+1, 1,NULL,NULL, NULL);
-
 
275
        if (screenbuffer_init(&(connections[i].screenbuffer), fb_info.cols, fb_info.rows ) == NULL) {
-
 
276
            /*FIXME: handle error */
-
 
277
            return -1;
-
 
278
        }
233
    }
279
    }
234
   
280
   
235
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
281
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
236
        return -1;
282
        return -1;
237
    };
283
    };
238
   
284
   
-
 
285
    ipc_call_sync_3(fb_info.phone, FB_PUTCHAR, 'M', 3, 3,NULL,NULL, NULL);
239
    async_manager();
286
    async_manager();
240
 
287
 
241
    return 0;  
288
    return 0;  
242
}
289
}
243
 
290