Rev 1339 | Rev 1344 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1339 | Rev 1343 | ||
---|---|---|---|
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 <stdio.h> |
31 | #include <stdio.h> |
31 | #include <unistd.h> |
32 | #include <unistd.h> |
32 | #include <stdlib.h> |
33 | #include <stdlib.h> |
33 | #include <ns.h> |
34 | #include <ns.h> |
34 | #include <errno.h> |
35 | #include <errno.h> |
35 | #include <arch/kbd.h> |
36 | #include <arch/kbd.h> |
36 | #include <kbd.h> |
37 | #include <kbd.h> |
37 | #include <key_buffer.h> |
38 | #include <key_buffer.h> |
38 | #include <fifo.h> |
39 | #include <fifo.h> |
39 | 40 | ||
40 | #define NAME "KBD" |
41 | #define NAME "KBD" |
41 | 42 | ||
42 | #define KBD_REQUEST_MAX 32 /**< Maximum requests buffered until keypress */ |
43 | #define KBD_REQUEST_MAX 32 /**< Maximum requests buffered until keypress */ |
43 | 44 | ||
44 | int main(int argc, char **argv) |
45 | int main(int argc, char **argv) |
45 | { |
46 | { |
46 | ipc_call_t call; |
47 | ipc_call_t call; |
47 | ipc_callid_t callid; |
48 | ipc_callid_t callid; |
48 | char connected = 0; |
49 | char connected = 0; |
49 | int res; |
50 | int res; |
50 | int c; |
51 | int c; |
51 | ipcarg_t phonead; |
52 | ipcarg_t phonead; |
52 | 53 | ||
53 | ipcarg_t retval, arg1, arg2; |
54 | ipcarg_t retval, arg1, arg2; |
54 | 55 | ||
55 | fifo_count_t callers_counter = 0; |
56 | fifo_count_t callers_counter = 0; |
56 | 57 | ||
57 | FIFO_INITIALIZE_STATIC(callers_buffer, ipc_callid_t, KBD_REQUEST_MAX); |
58 | FIFO_INITIALIZE_STATIC(callers_buffer, ipc_callid_t, KBD_REQUEST_MAX); |
58 | 59 | ||
59 | printf("Uspace kbd service started.\n"); |
60 | printf("Uspace kbd service started.\n"); |
60 | 61 | ||
61 | /* Initialize arch dependent parts */ |
62 | /* Initialize arch dependent parts */ |
62 | if (!(res = kbd_arch_init())) { |
63 | if (!(res = kbd_arch_init())) { |
63 | printf("Kbd registration failed with retval %d.\n", res); |
64 | printf("Kbd registration failed with retval %d.\n", res); |
64 | return -1; |
65 | return -1; |
65 | }; |
66 | }; |
66 | 67 | ||
67 | /* Initialize key buffer */ |
68 | /* Initialize key buffer */ |
68 | key_buffer_init(); |
69 | key_buffer_init(); |
69 | 70 | ||
70 | /* Register service at nameserver */ |
71 | /* Register service at nameserver */ |
71 | printf("%s: Registering at naming service.\n", NAME); |
72 | printf("%s: Registering at naming service.\n", NAME); |
72 | 73 | ||
73 | if ((res = ipc_connect_to_me(PHONE_NS, 30, 60, &phonead)) != 0) { |
74 | if ((res = ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, &phonead)) != 0) { |
74 | printf("%s: Error: Registering at naming service failed.\n", NAME); |
75 | printf("%s: Error: Registering at naming service failed.\n", NAME); |
75 | return -1; |
76 | return -1; |
76 | }; |
77 | }; |
77 | 78 | ||
78 | while (1) { |
79 | while (1) { |
79 | callid = ipc_wait_for_call(&call, 0); |
80 | callid = ipc_wait_for_call(&call, 0); |
80 | // printf("%s:Call phone=%lX..", NAME, call.in_phone_hash); |
81 | // printf("%s:Call phone=%lX..", NAME, call.in_phone_hash); |
81 | switch (IPC_GET_METHOD(call)) { |
82 | switch (IPC_GET_METHOD(call)) { |
82 | case IPC_M_PHONE_HUNGUP: |
83 | case IPC_M_PHONE_HUNGUP: |
83 | if (connected) { |
84 | if (connected) { |
84 | if (--connected == 0) { |
85 | if (--connected == 0) { |
85 | callers_counter = 0; |
86 | callers_counter = 0; |
86 | callers_buffer.head = callers_buffer.tail = 0; |
87 | callers_buffer.head = callers_buffer.tail = 0; |
87 | key_buffer_free(); |
88 | key_buffer_free(); |
88 | } |
89 | } |
89 | } |
90 | } |
90 | 91 | ||
91 | printf("%s: Phone hung up.\n", NAME); |
92 | printf("%s: Phone hung up.\n", NAME); |
92 | retval = 0; |
93 | retval = 0; |
93 | break; |
94 | break; |
94 | case IPC_M_CONNECT_TO_ME: |
95 | case IPC_M_CONNECT_TO_ME: |
95 | printf("%s: Somebody connecting phid=%zd.\n", NAME, IPC_GET_ARG3(call)); |
96 | printf("%s: Somebody connecting phid=%zd.\n", NAME, IPC_GET_ARG3(call)); |
96 | retval = 0; |
97 | retval = 0; |
97 | break; |
98 | break; |
98 | case IPC_M_CONNECT_ME_TO: |
99 | case IPC_M_CONNECT_ME_TO: |
99 | // printf("%s: Connect me (%P) to: %zd\n",NAME, IPC_GET_ARG3(call), IPC_GET_ARG1(call)); |
100 | // printf("%s: Connect me (%P) to: %zd\n",NAME, IPC_GET_ARG3(call), IPC_GET_ARG1(call)); |
100 | if (connected) { |
101 | if (connected) { |
101 | retval = ELIMIT; |
102 | retval = ELIMIT; |
102 | } else { |
103 | } else { |
103 | retval = 0; |
104 | retval = 0; |
104 | connected = 1; |
105 | connected = 1; |
105 | } |
106 | } |
106 | break; |
107 | break; |
107 | case IPC_M_INTERRUPT: |
108 | case IPC_M_INTERRUPT: |
108 | if (connected) { |
109 | if (connected) { |
109 | kbd_arch_process(IPC_GET_ARG2(call)); |
110 | kbd_arch_process(IPC_GET_ARG2(call)); |
110 | } |
111 | } |
111 | //printf("%s: GOT INTERRUPT: %c\n", NAME, IPC_GET_ARG2(call)); |
112 | //printf("%s: GOT INTERRUPT: %c\n", NAME, IPC_GET_ARG2(call)); |
112 | if (!callers_counter) |
113 | if (!callers_counter) |
113 | break; |
114 | break; |
114 | /* Small trick - interrupt does not need answer so we can change callid to caller awaiting key */ |
115 | /* Small trick - interrupt does not need answer so we can change callid to caller awaiting key */ |
115 | callers_counter--; |
116 | callers_counter--; |
116 | callid = fifo_pop(callers_buffer); |
117 | callid = fifo_pop(callers_buffer); |
117 | case KBD_GETCHAR: |
118 | case KBD_GETCHAR: |
118 | // printf("%s: Getchar: ", NAME); |
119 | // printf("%s: Getchar: ", NAME); |
119 | retval = 0; |
120 | retval = 0; |
120 | arg1 = 0; |
121 | arg1 = 0; |
121 | if (!key_buffer_pop((char *)&arg1)) { |
122 | if (!key_buffer_pop((char *)&arg1)) { |
122 | if (callers_counter < KBD_REQUEST_MAX) { |
123 | if (callers_counter < KBD_REQUEST_MAX) { |
123 | callers_counter++; |
124 | callers_counter++; |
124 | fifo_push(callers_buffer, callid); |
125 | fifo_push(callers_buffer, callid); |
125 | } else { |
126 | } else { |
126 | retval = ELIMIT; |
127 | retval = ELIMIT; |
127 | } |
128 | } |
128 | continue; |
129 | continue; |
129 | }; |
130 | }; |
130 | arg2 = 0xbeef; |
131 | arg2 = 0xbeef; |
131 | // printf("GetChar return %c\n", arg1); |
132 | // printf("GetChar return %c\n", arg1); |
132 | 133 | ||
133 | break; |
134 | break; |
134 | default: |
135 | default: |
135 | printf("%s: Unknown method: %zd\n", NAME, IPC_GET_METHOD(call)); |
136 | printf("%s: Unknown method: %zd\n", NAME, IPC_GET_METHOD(call)); |
136 | retval = ENOENT; |
137 | retval = ENOENT; |
137 | break; |
138 | break; |
138 | } |
139 | } |
139 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
140 | if (! (callid & IPC_CALLID_NOTIFICATION)) { |
140 | // printf("%s: Answering\n", NAME); |
141 | // printf("%s: Answering\n", NAME); |
141 | ipc_answer(callid, retval, arg1, arg2); |
142 | ipc_answer_fast(callid, retval, arg1, arg2); |
142 | } |
143 | } |
143 | } |
144 | } |
144 | } |
145 | } |
145 | 146 |