1 /*
2 * Copyright (c) 2020 Jabil Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <drivers/kscan.h>
8 #include <logging/log.h>
9
10 #include <SDL.h>
11
12 LOG_MODULE_REGISTER(kscan, CONFIG_KSCAN_LOG_LEVEL);
13
14 struct sdl_data {
15 const struct device *dev;
16 kscan_callback_t callback;
17 bool enabled;
18 };
19
sdl_filter(void * arg,SDL_Event * event)20 static int sdl_filter(void *arg, SDL_Event *event)
21 {
22 struct sdl_data *data = arg;
23 uint32_t row = 0;
24 uint32_t column = 0;
25 bool pressed = 0;
26
27 switch (event->type) {
28 case SDL_MOUSEBUTTONDOWN: {
29 pressed = 1;
30 column = event->button.x;
31 row = event->button.y;
32 } break;
33 case SDL_MOUSEBUTTONUP: {
34 pressed = 0;
35 column = event->button.x;
36 row = event->button.y;
37 } break;
38 case SDL_MOUSEMOTION: {
39 if (!event->motion.state)
40 break;
41 pressed = 1;
42 column = event->button.x;
43 row = event->button.y;
44 } break;
45 default:
46 return 1;
47 }
48
49 if (data->enabled && data->callback) {
50 data->callback(data->dev, row, column, pressed);
51 }
52 return 1;
53 }
54
sdl_configure(const struct device * dev,kscan_callback_t callback)55 static int sdl_configure(const struct device *dev, kscan_callback_t callback)
56 {
57 struct sdl_data *data = dev->data;
58
59 if (!callback) {
60 LOG_ERR("Callback is null");
61 return -EINVAL;
62 }
63 LOG_DBG("%s: set callback", dev->name);
64
65 data->callback = callback;
66
67 return 0;
68 }
69
sdl_enable_callback(const struct device * dev)70 static int sdl_enable_callback(const struct device *dev)
71 {
72 struct sdl_data *data = dev->data;
73
74 LOG_DBG("%s: enable cb", dev->name);
75 data->enabled = true;
76 return 0;
77 }
78
sdl_disable_callback(const struct device * dev)79 static int sdl_disable_callback(const struct device *dev)
80 {
81 struct sdl_data *data = dev->data;
82
83 LOG_DBG("%s: disable cb", dev->name);
84 data->enabled = false;
85 return 0;
86 }
87
sdl_init(const struct device * dev)88 static int sdl_init(const struct device *dev)
89 {
90 struct sdl_data *data = dev->data;
91
92 data->dev = dev;
93
94 LOG_INF("Init '%s' device", dev->name);
95 SDL_AddEventWatch(sdl_filter, data);
96
97 return 0;
98 }
99
100
101 static const struct kscan_driver_api sdl_driver_api = {
102 .config = sdl_configure,
103 .enable_callback = sdl_enable_callback,
104 .disable_callback = sdl_disable_callback,
105 };
106
107 static struct sdl_data sdl_data;
108
109 DEVICE_DEFINE(sdl, CONFIG_SDL_POINTER_KSCAN_DEV_NAME, sdl_init,
110 NULL, &sdl_data, NULL,
111 POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY,
112 &sdl_driver_api);
113