Subversion Repositories HelenOS-historic

Rev

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

Rev 1476 Rev 1481
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
 
41
 
41
static void sysput(char c)
42
static void sysput(char c)
42
{
43
{
43
    __SYSCALL3(SYS_IO, 1, &c, 1);
44
    __SYSCALL3(SYS_IO, 1, &c, 1);
44
 
45
 
45
}
46
}
46
//#define CONSOLE_COUNT VFB_CONNECTIONS
47
//#define CONSOLE_COUNT VFB_CONNECTIONS
47
#define CONSOLE_COUNT 8
48
#define CONSOLE_COUNT 8
-
 
49
#define MAX_KEYREQUESTS_BUFFERED 32
48
 
50
 
49
#define NAME "CONSOLE"
51
#define NAME "CONSOLE"
50
 
52
 
51
int active_console = 1;
53
int active_console = 1;
52
 
54
 
53
 
-
 
54
typedef struct {
55
typedef struct {
55
    keybuffer_t keybuffer;
56
    keybuffer_t keybuffer;
-
 
57
    FIFO_CREATE_STATIC(keyrequests, ipc_callid_t , MAX_KEYREQUESTS_BUFFERED);
-
 
58
    int keyrequest_counter;
56
    int client_phone;
59
    int client_phone;
57
    int vfb_number; /* Not used */
-
 
58
    int vfb_phone;
60
    int vfb_phone;
59
    int used;
61
    int used;
60
} connection_t;
62
} connection_t;
61
 
63
 
62
connection_t connections[CONSOLE_COUNT];
64
connection_t connections[CONSOLE_COUNT];
63
 
65
 
64
static int find_free_connection()
66
static int find_free_connection()
65
{
67
{
66
    int i = 0;
68
    int i = 0;
67
   
69
   
68
    while (i < CONSOLE_COUNT) {
70
    while (i < CONSOLE_COUNT) {
69
        if (connections[i].used == 0)
71
        if (connections[i].used == 0)
70
            return i;
72
            return i;
71
        ++i;
73
        ++i;
72
    }
74
    }
73
    return CONSOLE_COUNT;
75
    return CONSOLE_COUNT;
74
}
76
}
75
 
77
 
76
 
78
 
77
static int find_connection(int client_phone)
79
static int find_connection(int client_phone)
78
{
80
{
79
    int i = 0;
81
    int i = 0;
80
   
82
   
81
    while (i < CONSOLE_COUNT) {
83
    while (i < CONSOLE_COUNT) {
82
        if (connections[i].client_phone == client_phone)
84
        if (connections[i].client_phone == client_phone)
83
            return i;
85
            return i;
84
        ++i;
86
        ++i;
85
    }
87
    }
86
    return  CONSOLE_COUNT;
88
    return  CONSOLE_COUNT;
87
}
89
}
88
 
90
 
89
/* Handler for keyboard */
91
/* Handler for keyboard */
90
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
92
static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall)
91
{
93
{
92
    ipc_callid_t callid;
94
    ipc_callid_t callid;
93
    ipc_call_t call;
95
    ipc_call_t call;
94
    int retval;
96
    int retval;
95
    int i;
97
    int i;
96
    char c;
98
    char c;
97
 
99
 
98
    /* Ignore parameters, the connection is alread opened */
100
    /* Ignore parameters, the connection is alread opened */
99
    while (1) {
101
    while (1) {
100
        callid = async_get_call(&call);
102
        callid = async_get_call(&call);
101
        switch (IPC_GET_METHOD(call)) {
103
        switch (IPC_GET_METHOD(call)) {
102
        case IPC_M_PHONE_HUNGUP:
104
        case IPC_M_PHONE_HUNGUP:
103
            ipc_answer_fast(callid,0,0,0);
105
            ipc_answer_fast(callid,0,0,0);
104
            /* TODO: Handle hangup */
106
            /* TODO: Handle hangup */
105
            return;
107
            return;
106
        case KBD_PUSHCHAR:
108
        case KBD_PUSHCHAR:
107
            /* got key from keyboard driver */
109
            /* got key from keyboard driver */
108
           
110
           
109
            /* find active console */
-
 
110
           
-
 
111
            /* if client is awaiting key, send it */
-
 
112
           
-
 
113
            /*FIXME: else store key to its buffer */
-
 
114
            retval = 0;
111
            retval = 0;
115
            c = IPC_GET_ARG1(call);
112
            c = IPC_GET_ARG1(call);
116
//          ipc_call_sync_2(connections[3].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
113
//          ipc_call_sync_2(connections[3].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
-
 
114
       
117
            /* switch to another virtual console */
115
            /* switch to another virtual console */
-
 
116
           
118
            if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
117
            if ((c >= KBD_KEY_F1) && (c < KBD_KEY_F1 + CONSOLE_COUNT)) {
119
                active_console = c - KBD_KEY_F1;
118
                active_console = c - KBD_KEY_F1;
120
                break;
119
                break;
121
            }
120
            }
-
 
121
           
-
 
122
            /* if client is awaiting key, send it */
-
 
123
            if (connections[active_console].keyrequest_counter > 0) {      
-
 
124
                connections[active_console].keyrequest_counter--;
-
 
125
                ipc_answer_fast(fifo_pop(connections[active_console].keyrequests), 0, c, 0);
-
 
126
                break;
-
 
127
            }
-
 
128
           
-
 
129
            /*FIXME: else store key to its buffer */
122
            keybuffer_push(&(connections[active_console].keybuffer), c);
130
            keybuffer_push(&(connections[active_console].keybuffer), c);
123
           
131
           
124
            /* Send it to first FB, DEBUG */
132
            /* Send it to first FB, DEBUG */
125
//          ipc_call_async_2(connections[0].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG1(call),NULL,NULL);
133
//          ipc_call_async_2(connections[0].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG1(call),NULL,NULL);
126
//          ipc_call_sync_2(connections[4].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
134
//          ipc_call_sync_2(connections[4].vfb_phone, FB_PUTCHAR, 0, c,NULL,NULL);
127
 
135
 
128
            break;
136
            break;
129
        default:
137
        default:
130
            retval = ENOENT;
138
            retval = ENOENT;
131
        }      
139
        }      
132
        ipc_answer_fast(callid, retval, 0, 0);
140
        ipc_answer_fast(callid, retval, 0, 0);
133
    }
141
    }
134
}
142
}
135
 
143
 
136
/** Default thread for new connections */
144
/** Default thread for new connections */
137
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
145
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
138
{
146
{
139
    ipc_callid_t callid;
147
    ipc_callid_t callid;
140
    ipc_call_t call;
148
    ipc_call_t call;
141
    int consnum;
149
    int consnum;
142
    ipcarg_t arg1;
150
    ipcarg_t arg1;
143
 
151
 
144
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
152
    if ((consnum = find_free_connection()) == CONSOLE_COUNT) {
145
        ipc_answer_fast(iid,ELIMIT,0,0);
153
        ipc_answer_fast(iid,ELIMIT,0,0);
146
        return;
154
        return;
147
    }
155
    }
148
    connections[consnum].used = 1;
156
    connections[consnum].used = 1;
149
    connections[consnum].client_phone = IPC_GET_ARG3(call);
157
    connections[consnum].client_phone = IPC_GET_ARG3(call);
150
 
158
 
151
    /* Accept the connection */
159
    /* Accept the connection */
152
    ipc_answer_fast(iid,0,0,0);
160
    ipc_answer_fast(iid,0,0,0);
153
   
161
   
154
    while (1) {
162
    while (1) {
155
        callid = async_get_call(&call);
163
        callid = async_get_call(&call);
156
        switch (IPC_GET_METHOD(call)) {
164
        switch (IPC_GET_METHOD(call)) {
157
        case IPC_M_PHONE_HUNGUP:
165
        case IPC_M_PHONE_HUNGUP:
158
            /* TODO */
166
            /* TODO */
159
            ipc_answer_fast(callid, 0,0,0);
167
            ipc_answer_fast(callid, 0,0,0);
160
            return;
168
            return;
161
        case CONSOLE_PUTCHAR:
169
        case CONSOLE_PUTCHAR:
162
            if (consnum != active_console) {
170
            if (consnum != active_console) {
163
            }
171
            }
164
            /* Send message to fb */
172
            /* Send message to fb */
165
            ipc_call_sync_2(connections[consnum].vfb_phone, FB_PUTCHAR, IPC_GET_ARG1(call), IPC_GET_ARG2(call), NULL, NULL);
173
            ipc_call_sync_2(connections[consnum].vfb_phone, FB_PUTCHAR, IPC_GET_ARG1(call), IPC_GET_ARG2(call), NULL, NULL);
166
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG2(call),NULL,NULL);
174
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, IPC_GET_ARG2(call),NULL,NULL);
167
            break;
175
            break;
168
        case CONSOLE_CLEAR:
176
        case CONSOLE_CLEAR:
169
            break;
177
            break;
170
        case CONSOLE_GOTO:
178
        case CONSOLE_GOTO:
171
            break;
179
            break;
172
 
180
 
173
        case CONSOLE_GETCHAR:
181
        case CONSOLE_GETCHAR:
174
            /* FIXME: Only temporary solution until request storage will be created  */
-
 
175
            while (keybuffer_empty(&(connections[consnum].keybuffer))) {
182
            if (keybuffer_empty(&(connections[consnum].keybuffer))) {
176
                /* FIXME: buffer empty -> store request */
183
                /* buffer is empty -> store request */
-
 
184
                if (connections[consnum].keyrequest_counter < MAX_KEYREQUESTS_BUFFERED) {      
-
 
185
                    fifo_push(connections[consnum].keyrequests, callid);
-
 
186
                    connections[consnum].keyrequest_counter++;
-
 
187
                } else {
-
 
188
                    /* no key available and too many requests => fail */
-
 
189
                    ipc_answer_fast(callid, ELIMIT, 0, 0);
-
 
190
                }
177
                async_usleep(1000);
191
                continue;
178
            };
192
            };
179
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
193
            keybuffer_pop(&(connections[consnum].keybuffer), (char *)&arg1);
180
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, arg1,NULL,NULL);
194
//          ipc_call_sync_2(connections[6].vfb_phone, FB_PUTCHAR, 0, arg1,NULL,NULL);
181
           
195
           
182
            break;
196
            break;
183
        }
197
        }
184
        ipc_answer_fast(callid, 0, arg1, 0);
198
        ipc_answer_fast(callid, 0, arg1, 0);
185
    }
199
    }
186
}
200
}
187
 
201
 
188
int main(int argc, char *argv[])
202
int main(int argc, char *argv[])
189
{
203
{
190
    ipcarg_t phonehash;
204
    ipcarg_t phonehash;
191
    int kbd_phone, fb_phone;
205
    int kbd_phone, fb_phone;
192
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
206
    ipcarg_t retval, arg1 = 0xdead, arg2 = 0xbeef;
193
    int i;
207
    int i;
194
   
208
   
195
    /* Connect to keyboard driver */
209
    /* Connect to keyboard driver */
196
 
210
 
197
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
211
    while ((kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0)) < 0) {
198
        usleep(10000);
212
        usleep(10000);
199
    };
213
    };
200
   
214
   
201
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
215
    if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, &phonehash) != 0) {
202
        return -1;
216
        return -1;
203
    };
217
    };
204
    async_new_connection(phonehash, 0, NULL, keyboard_events);
218
    async_new_connection(phonehash, 0, NULL, keyboard_events);
205
 
219
 
206
    /* Connect to framebuffer driver */
220
    /* Connect to framebuffer driver */
207
   
221
   
208
    for (i = 0; i < CONSOLE_COUNT; i++) {
222
    for (i = 0; i < CONSOLE_COUNT; i++) {
209
        connections[i].used = 0;
223
        connections[i].used = 0;
210
        keybuffer_init(&(connections[i].keybuffer));
224
        keybuffer_init(&(connections[i].keybuffer));
-
 
225
       
211
        /* TODO: init key_buffer */
226
        /* TODO: init key_buffer */
212
        while ((connections[i].vfb_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
227
        while ((connections[i].vfb_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0)) < 0) {
213
            usleep(10000);
228
            usleep(10000);
214
            //ipc_call_async_2(connections[i].vfb_phone, FB_PUTCHAR, 'a', 'b', NULL, (void *)NULL); 
-
 
215
        }
229
        }
-
 
230
        connections[i].keyrequests.head = connections[i].keyrequests.tail = 0;
-
 
231
        connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED;
-
 
232
        connections[i].keyrequest_counter = 0;
216
    }
233
    }
217
   
234
   
218
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
235
    if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, &phonehash) != 0) {
219
        return -1;
236
        return -1;
220
    };
237
    };
221
   
238
   
222
    async_manager();
239
    async_manager();
223
 
240
 
224
    return 0;  
241
    return 0;  
225
}
242
}
226
 
243