1.1 --- a/pkg/devices/input/include/input-event-loop.h Fri May 18 22:34:51 2018 +0200
1.2 +++ b/pkg/devices/input/include/input-event-loop.h Fri May 18 23:41:51 2018 +0200
1.3 @@ -29,20 +29,10 @@
1.4 #include <l4/sys/capability>
1.5 #include <l4/sys/icu>
1.6
1.7 -typedef L4Re::Event_buffer::Event Event_type;
1.8 -
1.9 /* Input event loop abstraction. */
1.10
1.11 -class Input_event_loop : public Event_loop
1.12 +class Input_event_loop : public Event_handler_loop<L4Re::Event_buffer::Event>
1.13 {
1.14 - /* Handler function type. */
1.15 -
1.16 - typedef void (*Event_handler)(Event_type &, void *);
1.17 -
1.18 - /* External handler function. */
1.19 -
1.20 - Event_handler _handler;
1.21 -
1.22 /* Event buffer and notification interrupt capability. */
1.23
1.24 L4Re::Event_buffer _event_buffer;
1.25 @@ -52,22 +42,21 @@
1.26 L4::Cap<L4::Irq> _irq;
1.27
1.28 public:
1.29 - /* Initialise the event loop with an event handler function and private data,
1.30 - an event buffer, a notification interrupt, and a thread priority. */
1.31 + /* Initialise the event loop with an event buffer, a notification interrupt,
1.32 + and a thread priority. */
1.33
1.34 - explicit Input_event_loop(Event_handler handler, void *priv,
1.35 - L4Re::Event_buffer event_buffer,
1.36 + explicit Input_event_loop(L4Re::Event_buffer event_buffer,
1.37 L4::Cap<L4::Irq> irq,
1.38 int priority=0x20)
1.39 - : _handler(handler), Event_loop(priv, priority), _event_buffer(event_buffer), _irq(irq)
1.40 + : Event_handler_loop(priority), _event_buffer(event_buffer), _irq(irq)
1.41 {
1.42 }
1.43
1.44 - /* Event handler method. */
1.45 + /* Event handler method, dispatching to the provided handler. */
1.46
1.47 virtual void handle();
1.48
1.49 - /* Initiation function. */
1.50 + /* Start the loop, dispatching to the handle method. */
1.51
1.52 virtual void start(l4_umword_t label=0xDF00);
1.53 };
3.1 --- a/pkg/devices/util/include/event-loop.h Fri May 18 22:34:51 2018 +0200
3.2 +++ b/pkg/devices/util/include/event-loop.h Fri May 18 23:41:51 2018 +0200
3.3 @@ -31,21 +31,16 @@
3.4 class Event_loop
3.5 {
3.6 protected:
3.7 - /* Private data for the handler. */
3.8 -
3.9 - void *_priv;
3.10 -
3.11 /* Thread properties. */
3.12
3.13 int _priority;
3.14 pthread_t _pthread;
3.15
3.16 public:
3.17 - /* Initialise the event loop with an event handler function and private data,
3.18 - plus thread priority. */
3.19 + /* Initialise the event loop with a thread priority. */
3.20
3.21 - explicit Event_loop(void *priv, int priority=0x20)
3.22 - : _priv(priv), _priority(priority)
3.23 + explicit Event_loop(int priority=0x20)
3.24 + : _priority(priority)
3.25 {
3.26 }
3.27
3.28 @@ -62,4 +57,40 @@
3.29 virtual void start();
3.30 };
3.31
3.32 +
3.33 +
3.34 +/* Event loop with handler. */
3.35 +
3.36 +template <class T>
3.37 +class Event_handler_loop : public Event_loop
3.38 +{
3.39 +protected:
3.40 + /* Event type. */
3.41 +
3.42 + typedef T Event_type;
3.43 +
3.44 + /* Handler function type. */
3.45 +
3.46 + typedef void (*Event_handler)(Event_type &);
3.47 +
3.48 + /* External handler function. */
3.49 +
3.50 + Event_handler _handler;
3.51 +
3.52 +public:
3.53 + /* Initialise the event loop with a thread priority. */
3.54 +
3.55 + explicit Event_handler_loop(int priority=0x20)
3.56 + : Event_loop(priority)
3.57 + {
3.58 + }
3.59 +
3.60 + /* Attach a handler to the loop. */
3.61 +
3.62 + virtual void attach(Event_handler handler)
3.63 + {
3.64 + _handler = handler;
3.65 + }
3.66 +};
3.67 +
3.68 #endif
4.1 --- a/pkg/landfall-examples/input_event_client/input_event_client.cc Fri May 18 22:34:51 2018 +0200
4.2 +++ b/pkg/landfall-examples/input_event_client/input_event_client.cc Fri May 18 23:41:51 2018 +0200
4.3 @@ -51,6 +51,7 @@
4.4
4.5 #include <l4/re/c/util/video/goos_fb.h>
4.6 #include <l4/re/c/video/view.h>
4.7 +
4.8 #include <l4/re/env>
4.9 #include <l4/re/event>
4.10 #include <l4/re/event_enums.h>
4.11 @@ -141,7 +142,7 @@
4.12 static uint8_t row = 0;
4.13 static uint32_t text_x = 0, text_y = 0, next_y = 0;
4.14
4.15 -static void handler(L4Re::Event_buffer::Event &event, void *priv)
4.16 +static void show_key_code(L4Re::Event_buffer::Event &event)
4.17 {
4.18 uint32_t colsize = view_info.width / 10,
4.19 rowsize = view_info.height / 20;
4.20 @@ -150,26 +151,28 @@
4.21
4.22 /* Convert the key code into a bit pattern. */
4.23
4.24 - if (!priv)
4.25 - {
4.26 - for (column = 0, mask = (1 << 9); column < 10; column++, mask >>= 1)
4.27 - _screen->draw_box(Rect(Point(column * colsize, row * rowsize),
4.28 - Area(colsize, rowsize)),
4.29 - event.payload.code & mask ? (
4.30 - event.payload.value ? Rgb32::Color(0, 255, 0)
4.31 - : Rgb32::Color(255, 0, 0))
4.32 - : Rgb32::Color(0, 0, 0));
4.33 + for (column = 0, mask = (1 << 9); column < 10; column++, mask >>= 1)
4.34 + _screen->draw_box(Rect(Point(column * colsize, row * rowsize),
4.35 + Area(colsize, rowsize)),
4.36 + event.payload.code & mask ? (
4.37 + event.payload.value ? Rgb32::Color(0, 255, 0)
4.38 + : Rgb32::Color(255, 0, 0))
4.39 + : Rgb32::Color(0, 0, 0));
4.40 +
4.41 + /* Advance to the next row, wrapping around. */
4.42
4.43 - /* Advance to the next row, wrapping around. */
4.44 + row = (row + 1) % 20;
4.45
4.46 - row = (row + 1) % 20;
4.47 - }
4.48 + /* Refresh the display. */
4.49
4.50 - /* Or produce a string. */
4.51 + l4re_util_video_goos_fb_refresh(&gfb, 0, 0, view_info.width, view_info.height);
4.52 +}
4.53
4.54 - else if (event.payload.value)
4.55 +static void show_key_label(L4Re::Event_buffer::Event &event)
4.56 +{
4.57 + if (event.payload.value)
4.58 {
4.59 - const char *s = ((const char *(*)(int)) priv)(event.payload.code);
4.60 + const char *s = key_to_string(event.payload.code);
4.61 Rgba32::Color col;
4.62
4.63 if (!s)
4.64 @@ -226,12 +229,6 @@
4.65
4.66
4.67
4.68 -/* Event buffer memory. */
4.69 -
4.70 -static void *evmem = 0;
4.71 -
4.72 -
4.73 -
4.74 /* Arguments: [ chars ] */
4.75
4.76 int main(int argc, char *argv[])
4.77 @@ -272,11 +269,12 @@
4.78
4.79 L4::Cap<L4Re::Dataspace> mem = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>();
4.80 if (!mem.is_valid()) return 1;
4.81 -
4.82 if (event_server->get_buffer(mem)) return 1;
4.83
4.84 /* Attach the event buffer to this task. */
4.85
4.86 + void *evmem = 0;
4.87 +
4.88 if (L4Re::Env::env()->rm()->attach(&evmem, mem->size(), L4Re::Rm::Search_addr,
4.89 L4::Ipc::make_cap_rw(mem)))
4.90 return 1;
4.91 @@ -292,16 +290,18 @@
4.92
4.93 if (event_server->bind(0, irq)) return 1;
4.94
4.95 - /* Private data for the handler function. */
4.96 + /* Create an event handler. */
4.97
4.98 - void *priv = 0;
4.99 + Input_event_loop loop(event_buffer, irq);
4.100 +
4.101 + /* Attach the handler function. */
4.102
4.103 if ((argc > 1) && (!strcmp(argv[1], "chars")))
4.104 - priv = (void *) key_to_string;
4.105 + loop.attach(show_key_label);
4.106 + else
4.107 + loop.attach(show_key_code);
4.108
4.109 - /* Create an event handler and wait for events. */
4.110 -
4.111 - Input_event_loop loop(handler, priv, event_buffer, irq);
4.112 + /* Wait for events. */
4.113
4.114 loop.start();
4.115 l4_sleep_forever();