HelenOS sources
This source file includes following definitions.
- default_connection_handler
- usb_multimedia_push_ev
- usb_multimedia_init
- usb_multimedia_deinit
- usb_multimedia_polling_callback
#include "multimedia.h"
#include "../usbhid.h"
#include "keymap.h"
#include <usb/hid/hidparser.h>
#include <usb/debug.h>
#include <usb/hid/usages/core.h>
#include <usb/hid/usages/consumer.h>
#include <errno.h>
#include <async.h>
#include <str_error.h>
#include <ipc/kbdev.h>
#include <io/kbd_event.h>
#define NAME "multimedia-keys"
typedef struct usb_multimedia_t {
async_sess_t *console_sess;
} usb_multimedia_t;
static void default_connection_handler(ddf_fun_t *fun, ipc_call_t *icall)
{
usb_log_debug(NAME " default_connection_handler()");
usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
async_sess_t *sess =
async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
if (sess != NULL) {
if (multim_dev->console_sess == NULL) {
multim_dev->console_sess = sess;
usb_log_debug(NAME " Saved session to console: %p",
sess);
async_answer_0(icall, EOK);
} else
async_answer_0(icall, ELIMIT);
} else
async_answer_0(icall, EINVAL);
}
static ddf_dev_ops_t multimedia_ops = {
.default_handler = default_connection_handler
};
static void usb_multimedia_push_ev(
usb_multimedia_t *multim_dev, int type, unsigned int key)
{
assert(multim_dev != NULL);
const kbd_event_t ev = {
.type = type,
.key = key,
.mods = 0,
.c = 0,
};
usb_log_debug2(NAME " Sending key %d to the console", ev.key);
if (multim_dev->console_sess == NULL) {
usb_log_warning(
"Connection to console not ready, key discarded.\n");
return;
}
async_exch_t *exch = async_exchange_begin(multim_dev->console_sess);
if (exch != NULL) {
async_msg_4(exch, KBDEV_EVENT, ev.type, ev.key, ev.mods, ev.c);
async_exchange_end(exch);
} else {
usb_log_warning("Failed to send multimedia key.");
}
}
errno_t usb_multimedia_init(struct usb_hid_dev *hid_dev, void **data)
{
if (hid_dev == NULL || hid_dev->usb_dev == NULL) {
return EINVAL;
}
usb_log_debug(NAME " Initializing HID/multimedia structure...");
ddf_fun_t *fun = usb_device_ddf_fun_create(
hid_dev->usb_dev, fun_exposed, NAME);
if (fun == NULL) {
usb_log_error("Could not create DDF function node.");
return ENOMEM;
}
ddf_fun_set_ops(fun, &multimedia_ops);
usb_multimedia_t *multim_dev =
ddf_fun_data_alloc(fun, sizeof(usb_multimedia_t));
if (multim_dev == NULL) {
ddf_fun_destroy(fun);
return ENOMEM;
}
multim_dev->console_sess = NULL;
errno_t rc = ddf_fun_bind(fun);
if (rc != EOK) {
usb_log_error("Could not bind DDF function: %s.",
str_error(rc));
ddf_fun_destroy(fun);
return rc;
}
usb_log_debug(NAME " function created (handle: %" PRIun ").",
ddf_fun_get_handle(fun));
rc = ddf_fun_add_to_category(fun, "keyboard");
if (rc != EOK) {
usb_log_error(
"Could not add DDF function to category 'keyboard': %s.\n",
str_error(rc));
if (ddf_fun_unbind(fun) != EOK) {
usb_log_error("Failed to unbind %s, won't destroy.",
ddf_fun_get_name(fun));
} else {
ddf_fun_destroy(fun);
}
return rc;
}
*data = fun;
usb_log_debug(NAME " HID/multimedia structure initialized.");
return EOK;
}
void usb_multimedia_deinit(struct usb_hid_dev *hid_dev, void *data)
{
ddf_fun_t *fun = data;
usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
if (multim_dev->console_sess)
async_hangup(multim_dev->console_sess);
if (ddf_fun_unbind(fun) != EOK) {
usb_log_error("Failed to unbind %s, won't destroy.",
ddf_fun_get_name(fun));
} else {
usb_log_debug2("%s unbound.", ddf_fun_get_name(fun));
ddf_fun_destroy(fun);
}
}
bool usb_multimedia_polling_callback(struct usb_hid_dev *hid_dev, void *data)
{
ddf_fun_t *fun = data;
if (hid_dev == NULL) {
return false;
}
usb_multimedia_t *multim_dev = ddf_fun_data_get(fun);
usb_hid_report_path_t *path = usb_hid_report_path();
if (path == NULL)
return true;
errno_t ret =
usb_hid_report_path_append_item(path, USB_HIDUT_PAGE_CONSUMER, 0);
if (ret != EOK) {
usb_hid_report_path_free(path);
return true;
}
usb_hid_report_path_set_report_id(path, hid_dev->report_id);
usb_hid_report_field_t *field = usb_hid_report_get_sibling(
&hid_dev->report, NULL, path, USB_HID_PATH_COMPARE_END |
USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
USB_HID_REPORT_TYPE_INPUT);
while (field != NULL) {
if (field->value != 0) {
usb_log_debug(NAME " KEY VALUE(%X) USAGE(%X)",
field->value, field->usage);
const unsigned key =
usb_multimedia_map_usage(field->usage);
const char *key_str =
usbhid_multimedia_usage_to_str(field->usage);
usb_log_info("Pressed key: %s", key_str);
usb_multimedia_push_ev(multim_dev, KEY_PRESS, key);
}
field = usb_hid_report_get_sibling(
&hid_dev->report, field, path, USB_HID_PATH_COMPARE_END |
USB_HID_PATH_COMPARE_USAGE_PAGE_ONLY,
USB_HID_REPORT_TYPE_INPUT);
}
usb_hid_report_path_free(path);
return true;
}
HelenOS homepage, sources at GitHub