Subversion Repositories HelenOS

Rev

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

Rev 1344 Rev 1345
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
#include <ipc.h>
29
#include <ipc.h>
30
#include <services.h>
30
#include <services.h>
31
#include <stdio.h>
31
#include <stdio.h>
32
#include <unistd.h>
32
#include <unistd.h>
33
#include <stdlib.h>
33
#include <stdlib.h>
34
#include <ns.h>
34
#include <ns.h>
35
#include <errno.h>
35
#include <errno.h>
36
#include <arch/kbd.h>
36
#include <arch/kbd.h>
37
#include <kbd.h>
37
#include <kbd.h>
38
#include <key_buffer.h>
38
#include <key_buffer.h>
39
#include <fifo.h>
39
#include <fifo.h>
40
 
40
 
41
#define NAME "KBD"
41
#define NAME "KBD"
42
 
42
 
43
#define KBD_REQUEST_MAX 32 /**< Maximum requests buffered until keypress */
43
#define KBD_REQUEST_MAX 32 /**< Maximum requests buffered until keypress */
44
 
44
 
45
int main(int argc, char **argv)
45
int main(int argc, char **argv)
46
{
46
{
47
    ipc_call_t call;
47
    ipc_call_t call;
48
    ipc_callid_t callid;
48
    ipc_callid_t callid;
49
    char connected = 0;
49
    char connected = 0;
50
    int res;
50
    int res;
51
    int c;
51
    int c;
52
    ipcarg_t phonead;
52
    ipcarg_t phonead;
53
   
53
   
54
    ipcarg_t retval, arg1, arg2;
54
    ipcarg_t retval, arg1, arg2;
55
 
55
 
56
    /* Counter of unsatisfied calls */
56
    /* Counter of unsatisfied calls */
57
    fifo_count_t callers_counter = 0;
57
    fifo_count_t callers_counter = 0;
58
    /* Fifo with callid's of unsatisfied calls requred for answer */
58
    /* Fifo with callid's of unsatisfied calls requred for answer */
59
    FIFO_INITIALIZE_STATIC(callers_buffer, ipc_callid_t, KBD_REQUEST_MAX);
59
    FIFO_INITIALIZE_STATIC(callers_buffer, ipc_callid_t, KBD_REQUEST_MAX);
60
 
60
 
61
    printf("Uspace kbd service started.\n");
61
    printf("Uspace kbd service started.\n");
62
 
62
 
63
    /* Initialize arch dependent parts */
63
    /* Initialize arch dependent parts */
64
    if (!(res = kbd_arch_init())) {
64
    if (!(res = kbd_arch_init())) {
65
            printf("Kbd registration failed with retval %d.\n", res);
65
            printf("Kbd registration failed with retval %d.\n", res);
66
            return -1;
66
            return -1;
67
            };
67
            };
68
   
68
   
69
    /* Initialize key buffer */
69
    /* Initialize key buffer */
70
    key_buffer_init();
70
    key_buffer_init();
71
   
71
   
72
    /* Register service at nameserver */
72
    /* Register service at nameserver */
73
    printf("%s: Registering at naming service.\n", NAME);
73
    printf("%s: Registering at naming service.\n", NAME);
74
 
74
 
75
    if ((res = ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, &phonead)) != 0) {
75
    if ((res = ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, &phonead)) != 0) {
76
        printf("%s: Error: Registering at naming service failed.\n", NAME);
76
        printf("%s: Error: Registering at naming service failed.\n", NAME);
77
        return -1;
77
        return -1;
78
    };
78
    };
79
   
79
   
80
    while (1) {
80
    while (1) {
81
        callid = ipc_wait_for_call(&call, 0);
81
        callid = ipc_wait_for_call(&call, 0);
82
    //  printf("%s:Call phone=%lX..", NAME, call.in_phone_hash);
82
    //  printf("%s:Call phone=%lX..", NAME, call.in_phone_hash);
83
        switch (IPC_GET_METHOD(call)) {
83
        switch (IPC_GET_METHOD(call)) {
84
            case IPC_M_PHONE_HUNGUP:
84
            case IPC_M_PHONE_HUNGUP:
85
                if (connected) {
85
                if (connected) {
86
                    /* If nobody's connected, clear keybuffer and dont store new keys */
86
                    /* If nobody's connected, clear keybuffer and dont store new keys */
87
                    if (--connected == 0) {
87
                    if (--connected == 0) {
88
                        callers_counter = 0;
88
                        callers_counter = 0;
89
                        callers_buffer.head = callers_buffer.tail = 0;
89
                        callers_buffer.head = callers_buffer.tail = 0;
90
                        key_buffer_free(); 
90
                        key_buffer_free(); 
91
                    }
91
                    }
92
                   
92
                   
93
                    printf("%s: Phone hung up.\n", NAME);
93
                    printf("%s: Phone hung up.\n", NAME);
94
                } else {
94
                } else {
95
                    printf("%s: Oops, got phone hung up, but nobody connected.\n", NAME);
95
                    printf("%s: Oops, got phone hung up, but nobody connected.\n", NAME);
96
                }
96
                }
97
               
97
               
98
                retval = 0;
98
                retval = 0;
99
                break;
99
                break;
100
            case IPC_M_CONNECT_TO_ME:
-
 
101
                printf("%s: Somebody connecting phid=%zd.\n", NAME, IPC_GET_ARG3(call));
-
 
102
                retval = 0;
-
 
103
                break;
-
 
104
            case IPC_M_CONNECT_ME_TO:
100
            case IPC_M_CONNECT_ME_TO:
105
            //  printf("%s: Connect me (%P) to: %zd\n",NAME, IPC_GET_ARG3(call), IPC_GET_ARG1(call));
101
            //  printf("%s: Connect me (%P) to: %zd\n",NAME, IPC_GET_ARG3(call), IPC_GET_ARG1(call));
106
                /* Only one connected client allowed */
102
                /* Only one connected client allowed */
107
                if (connected) {
103
                if (connected) {
108
                    retval = ELIMIT;
104
                    retval = ELIMIT;
109
                } else {
105
                } else {
110
                    retval = 0;
106
                    retval = 0;
111
                    connected = 1;
107
                    connected = 1;
112
                }
108
                }
113
                break;
109
                break;
114
            case IPC_M_INTERRUPT:
110
            case IPC_M_INTERRUPT:
115
                if (connected) {
111
                if (connected) {
116
                    /* recode scancode and store it into key buffer */
112
                    /* recode scancode and store it into key buffer */
117
                    kbd_arch_process(IPC_GET_ARG2(call));
113
                    kbd_arch_process(IPC_GET_ARG2(call));
118
                    //printf("%s: GOT INTERRUPT: %c\n", NAME, IPC_GET_ARG2(call));
114
                    //printf("%s: GOT INTERRUPT: %c\n", NAME, IPC_GET_ARG2(call));
119
 
115
 
120
                    /* Some callers could awaiting keypress - if its true, we have to send keys to them.
116
                    /* Some callers could awaiting keypress - if its true, we have to send keys to them.
121
                     * One interrupt can store more than one key into buffer. */
117
                     * One interrupt can store more than one key into buffer. */
122
                    retval = 0;
118
                    retval = 0;
123
                    arg2 = 0xbeef;
119
                    arg2 = 0xbeef;
124
                    while ((callers_counter) && (!key_buffer_empty())) {
120
                    while ((callers_counter) && (!key_buffer_empty())) {
125
                        callers_counter--;
121
                        callers_counter--;
126
                        if (!key_buffer_pop((char *)&arg1)) {
122
                        if (!key_buffer_pop((char *)&arg1)) {
127
                            printf("%s: KeyBuffer empty but it should not be.\n");
123
                            printf("%s: KeyBuffer empty but it should not be.\n");
128
                            break;
124
                            break;
129
                        }
125
                        }
130
                        ipc_answer_fast(fifo_pop(callers_buffer), retval, arg1, arg2);
126
                        ipc_answer_fast(fifo_pop(callers_buffer), retval, arg1, arg2);
131
                    }
127
                    }
132
                }
128
                }
133
                break;
129
                break;
134
            case KBD_GETCHAR:
130
            case KBD_GETCHAR:
135
    //          printf("%s: Getchar: ", NAME);
131
    //          printf("%s: Getchar: ", NAME);
136
                retval = 0;
132
                retval = 0;
137
                arg1 = 0;  
133
                arg1 = 0;  
138
                if (!key_buffer_pop((char *)&arg1)) {
134
                if (!key_buffer_pop((char *)&arg1)) {
139
                    if (callers_counter < KBD_REQUEST_MAX) {
135
                    if (callers_counter < KBD_REQUEST_MAX) {
140
                        callers_counter++;
136
                        callers_counter++;
141
                        fifo_push(callers_buffer, callid);
137
                        fifo_push(callers_buffer, callid);
142
                    } else {
138
                    } else {
143
                        retval = ELIMIT;
139
                        retval = ELIMIT;
144
                    }
140
                    }
145
                    continue;
141
                    continue;
146
                };
142
                };
147
                arg2 = 0xbeef;
143
                arg2 = 0xbeef;
148
            //  printf("GetChar return %c\n", arg1);
144
            //  printf("GetChar return %c\n", arg1);
149
               
145
               
150
                break;
146
                break;
151
            default:
147
            default:
152
                printf("%s: Unknown method: %zd\n", NAME, IPC_GET_METHOD(call));
148
                printf("%s: Unknown method: %zd\n", NAME, IPC_GET_METHOD(call));
153
                retval = ENOENT;
149
                retval = ENOENT;
154
                break;
150
                break;
155
        }
151
        }
156
 
152
 
157
        if (! (callid & IPC_CALLID_NOTIFICATION)) {
153
        if (! (callid & IPC_CALLID_NOTIFICATION)) {
158
            ipc_answer_fast(callid, retval, arg1, arg2);
154
            ipc_answer_fast(callid, retval, arg1, arg2);
159
        }
155
        }
160
    }
156
    }
161
}
157
}
162
 
158
 
163
 
159