Subversion Repositories HelenOS-historic

Rev

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

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