paul@3 | 1 | /* |
paul@3 | 2 | * Provide access to keypad events on the configured device. |
paul@3 | 3 | * |
paul@3 | 4 | * (c) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@3 | 5 | * |
paul@3 | 6 | * This program is free software; you can redistribute it and/or |
paul@3 | 7 | * modify it under the terms of the GNU General Public License as |
paul@3 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@3 | 9 | * the License, or (at your option) any later version. |
paul@3 | 10 | * |
paul@3 | 11 | * This program is distributed in the hope that it will be useful, |
paul@3 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@3 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@3 | 14 | * GNU General Public License for more details. |
paul@3 | 15 | * |
paul@3 | 16 | * You should have received a copy of the GNU General Public License |
paul@3 | 17 | * along with this program; if not, write to the Free Software |
paul@3 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@3 | 19 | * Boston, MA 02110-1301, USA |
paul@3 | 20 | */ |
paul@3 | 21 | |
paul@3 | 22 | #include <l4/devices/dataspace.h> |
paul@3 | 23 | #include "input-event-client.h" |
paul@3 | 24 | #include "input-event-server.h" |
paul@3 | 25 | #include "input-event-ops.h" |
paul@3 | 26 | #include "input-keypad-client.h" |
paul@3 | 27 | |
paul@3 | 28 | #include <l4/cxx/ipc_stream> |
paul@3 | 29 | #include <l4/re/dataspace> |
paul@3 | 30 | #include <l4/re/env> |
paul@3 | 31 | #include <l4/re/event> |
paul@3 | 32 | #include <l4/re/util/cap_alloc> |
paul@3 | 33 | #include <l4/re/util/object_registry> |
paul@3 | 34 | #include <l4/sys/capability> |
paul@3 | 35 | #include <l4/sys/icu> |
paul@3 | 36 | #include <l4/sys/kip.h> |
paul@3 | 37 | #include <l4/sys/types.h> |
paul@3 | 38 | #include <l4/util/util.h> |
paul@3 | 39 | |
paul@3 | 40 | /* Handle invocations. */ |
paul@3 | 41 | |
paul@3 | 42 | int |
paul@3 | 43 | Input_event_server::dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios) |
paul@3 | 44 | { |
paul@3 | 45 | l4_msgtag_t tag; |
paul@3 | 46 | |
paul@3 | 47 | (void) obj; |
paul@3 | 48 | ios >> tag; |
paul@3 | 49 | |
paul@3 | 50 | switch (tag.label()) |
paul@3 | 51 | { |
paul@3 | 52 | case L4::Meta::Protocol: |
paul@3 | 53 | return L4::Util::handle_meta_request<Input_event_interface>(ios); |
paul@3 | 54 | |
paul@3 | 55 | /* Reset the buffer state and return the buffer memory capability. */ |
paul@3 | 56 | |
paul@3 | 57 | case Input_event_op_get_buffer: |
paul@3 | 58 | _events.reset(); |
paul@3 | 59 | ios << _mem; |
paul@3 | 60 | return L4_EOK; |
paul@3 | 61 | |
paul@3 | 62 | /* Just return the interrupt capability. */ |
paul@3 | 63 | |
paul@3 | 64 | case Input_event_op_bind: |
paul@3 | 65 | ios << _irq; |
paul@3 | 66 | return L4_EOK; |
paul@3 | 67 | |
paul@3 | 68 | default: |
paul@3 | 69 | return -L4_EBADPROTO; |
paul@3 | 70 | } |
paul@3 | 71 | } |
paul@3 | 72 | |
paul@3 | 73 | |
paul@3 | 74 | |
paul@3 | 75 | /* Event sending methods. */ |
paul@3 | 76 | |
paul@3 | 77 | void |
paul@3 | 78 | Input_event_server::send_event(int type, int code, int value) |
paul@3 | 79 | { |
paul@3 | 80 | L4Re::Event_buffer::Event event; |
paul@3 | 81 | |
paul@3 | 82 | event.time = l4_kip_clock(l4re_kip()); |
paul@3 | 83 | event.payload.stream_id = 0; |
paul@3 | 84 | event.payload.type = type; |
paul@3 | 85 | event.payload.code = code; |
paul@3 | 86 | event.payload.value = value; |
paul@3 | 87 | |
paul@3 | 88 | /* Queue the event and trigger the interrupt. */ |
paul@3 | 89 | |
paul@3 | 90 | _events.put(event); |
paul@3 | 91 | _irq->trigger(); |
paul@3 | 92 | } |
paul@3 | 93 | |
paul@3 | 94 | |
paul@3 | 95 | |
paul@3 | 96 | static void handler(Input_event event, void *priv) |
paul@3 | 97 | { |
paul@3 | 98 | Input_event_server *server = (Input_event_server *) priv; |
paul@3 | 99 | |
paul@3 | 100 | server->send_event(event.type, event.code, event.value); |
paul@3 | 101 | } |
paul@3 | 102 | |
paul@3 | 103 | |
paul@3 | 104 | |
paul@3 | 105 | static L4Re::Util::Registry_server<> server; |
paul@3 | 106 | |
paul@3 | 107 | |
paul@3 | 108 | |
paul@3 | 109 | int run(void) |
paul@3 | 110 | { |
paul@3 | 111 | Keypad *keypad = Keypad::get_keypad(); |
paul@3 | 112 | Input_keypad_client client(keypad); |
paul@3 | 113 | void *buffer; |
paul@3 | 114 | |
paul@3 | 115 | // Memory allocation capability for the event data. |
paul@3 | 116 | |
paul@3 | 117 | L4::Cap<L4Re::Dataspace> mem = allocate_data(L4_PAGESIZE, &buffer); |
paul@3 | 118 | if (!mem.is_valid()) return 1; |
paul@3 | 119 | |
paul@3 | 120 | /* Obtain a capability for the interrupt. */ |
paul@3 | 121 | |
paul@3 | 122 | L4::Cap<L4::Irq> irq = L4Re::Util::cap_alloc.alloc<L4::Irq>(); |
paul@3 | 123 | if (!irq.is_valid()) return 1; |
paul@3 | 124 | |
paul@3 | 125 | /* Create an interrupt object. */ |
paul@3 | 126 | |
paul@3 | 127 | if (l4_error(L4Re::Env::env()->factory()->create(irq))) return 1; |
paul@3 | 128 | |
paul@3 | 129 | // Event buffer for the data. |
paul@3 | 130 | |
paul@3 | 131 | L4Re::Event_buffer events(buffer, L4_PAGESIZE); |
paul@3 | 132 | |
paul@3 | 133 | // Initialise and register a server object. |
paul@3 | 134 | |
paul@3 | 135 | Input_event_server server_obj(mem, irq, events); |
paul@3 | 136 | server.registry()->register_obj(&server_obj, "ev"); |
paul@3 | 137 | |
paul@3 | 138 | // Attach the event handler and wait for events. |
paul@3 | 139 | |
paul@3 | 140 | client.attach(handler, (void *) &server_obj); |
paul@3 | 141 | |
paul@3 | 142 | // Enter the IPC server loop. |
paul@3 | 143 | |
paul@3 | 144 | server.loop(); |
paul@3 | 145 | return 0; |
paul@3 | 146 | } |