HelenOS sources
This source file includes following definitions.
- client_data_create
- client_data_destroy
- kbd_push_data
- mouse_push_data
- kbd_push_event
- mouse_push_event_move
- mouse_push_event_abs_move
- mouse_push_event_button
- mouse_push_event_dclick
- client_arbitration
- client_connection
- kconsole_event_handler
- kbd_dev_new
- mouse_dev_new
- serial_dev_new
- kbd_add_dev
- kbd_add_kbdev
- mouse_add_mousedev
- serial_consumer
- serial_add_srldev
- kbd_add_legacy_devs
- dev_check_new_kbdevs
- dev_check_new_mousedevs
- dev_check_new_serialdevs
- dev_check_new
- cat_change_cb
- input_start_dev_discovery
- usage
- main
#include <adt/fifo.h>
#include <adt/list.h>
#include <async.h>
#include <config.h>
#include <errno.h>
#include <fibril.h>
#include <fibril_synch.h>
#include <io/chardev.h>
#include <io/console.h>
#include <io/keycode.h>
#include <ipc/services.h>
#include <ipc/input.h>
#include <loc.h>
#include <ns.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include <str_error.h>
#include "input.h"
#include "kbd.h"
#include "kbd_port.h"
#include "kbd_ctl.h"
#include "layout.h"
#include "mouse.h"
#include "mouse_proto.h"
#include "serial.h"
#define NUM_LAYOUTS 5
static layout_ops_t *layout[NUM_LAYOUTS] = {
&us_qwerty_ops,
&us_dvorak_ops,
&cz_ops,
&ar_ops,
&fr_azerty_ops
};
typedef struct {
link_t link;
bool active;
async_sess_t *sess;
} client_t;
static list_t clients;
static client_t *active_client = NULL;
static bool active = true;
static char *serial_console;
static list_t kbd_devs;
static list_t mouse_devs;
static list_t serial_devs;
static FIBRIL_MUTEX_INITIALIZE(discovery_lock);
static void *client_data_create(void)
{
client_t *client = (client_t *) calloc(1, sizeof(client_t));
if (client == NULL)
return NULL;
link_initialize(&client->link);
client->active = false;
client->sess = NULL;
list_append(&client->link, &clients);
return client;
}
static void client_data_destroy(void *data)
{
client_t *client = (client_t *) data;
list_remove(&client->link);
free(client);
}
void kbd_push_data(kbd_dev_t *kdev, sysarg_t data)
{
(*kdev->ctl_ops->parse)(data);
}
void mouse_push_data(mouse_dev_t *mdev, sysarg_t data)
{
(*mdev->proto_ops->parse)(data);
}
void kbd_push_event(kbd_dev_t *kdev, int type, unsigned int key)
{
kbd_event_t ev;
unsigned int mod_mask;
switch (key) {
case KC_LCTRL:
mod_mask = KM_LCTRL;
break;
case KC_RCTRL:
mod_mask = KM_RCTRL;
break;
case KC_LSHIFT:
mod_mask = KM_LSHIFT;
break;
case KC_RSHIFT:
mod_mask = KM_RSHIFT;
break;
case KC_LALT:
mod_mask = KM_LALT;
break;
case KC_RALT:
mod_mask = KM_RALT;
break;
default:
mod_mask = 0;
}
if (mod_mask != 0) {
if (type == KEY_PRESS)
kdev->mods = kdev->mods | mod_mask;
else
kdev->mods = kdev->mods & ~mod_mask;
}
switch (key) {
case KC_CAPS_LOCK:
mod_mask = KM_CAPS_LOCK;
break;
case KC_NUM_LOCK:
mod_mask = KM_NUM_LOCK;
break;
case KC_SCROLL_LOCK:
mod_mask = KM_SCROLL_LOCK;
break;
default:
mod_mask = 0;
}
if (mod_mask != 0) {
if (type == KEY_PRESS) {
kdev->mods = kdev->mods ^ (mod_mask & ~kdev->lock_keys);
kdev->lock_keys = kdev->lock_keys | mod_mask;
(*kdev->ctl_ops->set_ind)(kdev, kdev->mods);
} else {
kdev->lock_keys = kdev->lock_keys & ~mod_mask;
}
}
if ((type == KEY_PRESS) && (kdev->mods & KM_LCTRL)) {
switch (key) {
case KC_F1:
layout_destroy(kdev->active_layout);
kdev->active_layout = layout_create(layout[0]);
break;
case KC_F2:
layout_destroy(kdev->active_layout);
kdev->active_layout = layout_create(layout[1]);
break;
case KC_F3:
layout_destroy(kdev->active_layout);
kdev->active_layout = layout_create(layout[2]);
break;
case KC_F4:
layout_destroy(kdev->active_layout);
kdev->active_layout = layout_create(layout[3]);
break;
case KC_F5:
layout_destroy(kdev->active_layout);
kdev->active_layout = layout_create(layout[4]);
break;
default:
break;
}
}
if (type == KEY_PRESS) {
switch (key) {
case KC_F12:
console_kcon();
break;
}
}
ev.type = type;
ev.key = key;
ev.mods = kdev->mods;
ev.c = layout_parse_ev(kdev->active_layout, &ev);
list_foreach(clients, link, client_t, client) {
if (client->active) {
async_exch_t *exch = async_exchange_begin(client->sess);
async_msg_5(exch, INPUT_EVENT_KEY, kdev->svc_id,
ev.type, ev.key, ev.mods, ev.c);
async_exchange_end(exch);
}
}
}
void mouse_push_event_move(mouse_dev_t *mdev, int dx, int dy, int dz)
{
list_foreach(clients, link, client_t, client) {
if (client->active) {
async_exch_t *exch = async_exchange_begin(client->sess);
if ((dx) || (dy))
async_msg_3(exch, INPUT_EVENT_MOVE,
mdev->svc_id, dx, dy);
if (dz) {
keycode_t code = dz > 0 ? KC_UP : KC_DOWN;
for (unsigned int i = 0; i < 3; i++)
async_msg_5(exch, INPUT_EVENT_KEY,
0 ,
KEY_PRESS, code, 0, 0);
async_msg_5(exch, INPUT_EVENT_KEY, KEY_RELEASE,
0 , code, 0, 0);
}
async_exchange_end(exch);
}
}
}
void mouse_push_event_abs_move(mouse_dev_t *mdev, unsigned int x, unsigned int y,
unsigned int max_x, unsigned int max_y)
{
list_foreach(clients, link, client_t, client) {
if (client->active) {
if ((max_x) && (max_y)) {
async_exch_t *exch = async_exchange_begin(client->sess);
async_msg_5(exch, INPUT_EVENT_ABS_MOVE,
mdev->svc_id, x, y, max_x, max_y);
async_exchange_end(exch);
}
}
}
}
void mouse_push_event_button(mouse_dev_t *mdev, int bnum, int press)
{
list_foreach(clients, link, client_t, client) {
if (client->active) {
async_exch_t *exch = async_exchange_begin(client->sess);
async_msg_3(exch, INPUT_EVENT_BUTTON, mdev->svc_id,
bnum, press);
async_exchange_end(exch);
}
}
}
void mouse_push_event_dclick(mouse_dev_t *mdev, int bnum)
{
list_foreach(clients, link, client_t, client) {
if (client->active) {
async_exch_t *exch = async_exchange_begin(client->sess);
async_msg_2(exch, INPUT_EVENT_DCLICK, mdev->svc_id,
bnum);
async_exchange_end(exch);
}
}
}
static void client_arbitration(void)
{
list_foreach(clients, link, client_t, client)
client->active = ((active) && (client == active_client));
list_foreach(clients, link, client_t, client) {
async_exch_t *exch = async_exchange_begin(client->sess);
async_msg_0(exch, client->active ?
INPUT_EVENT_ACTIVE : INPUT_EVENT_DEACTIVE);
async_exchange_end(exch);
}
}
static void client_connection(ipc_call_t *icall, void *arg)
{
client_t *client = (client_t *) async_get_client_data();
if (client == NULL) {
async_answer_0(icall, ENOMEM);
return;
}
async_accept_0(icall);
while (true) {
ipc_call_t call;
async_get_call(&call);
if (!ipc_get_imethod(&call)) {
if (client->sess != NULL) {
async_hangup(client->sess);
client->sess = NULL;
}
async_answer_0(&call, EOK);
return;
}
async_sess_t *sess =
async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
if (sess != NULL) {
if (client->sess == NULL) {
client->sess = sess;
async_answer_0(&call, EOK);
} else
async_answer_0(&call, ELIMIT);
} else {
switch (ipc_get_imethod(&call)) {
case INPUT_ACTIVATE:
active_client = client;
client_arbitration();
async_answer_0(&call, EOK);
break;
default:
async_answer_0(&call, EINVAL);
}
}
}
}
static void kconsole_event_handler(ipc_call_t *call, void *arg)
{
if (ipc_get_arg1(call)) {
active = false;
} else {
active = true;
}
client_arbitration();
}
static kbd_dev_t *kbd_dev_new(void)
{
kbd_dev_t *kdev = calloc(1, sizeof(kbd_dev_t));
if (kdev == NULL) {
printf("%s: Error allocating keyboard device. "
"Out of memory.\n", NAME);
return NULL;
}
link_initialize(&kdev->link);
kdev->mods = KM_NUM_LOCK;
kdev->lock_keys = 0;
kdev->active_layout = layout_create(layout[0]);
return kdev;
}
static mouse_dev_t *mouse_dev_new(void)
{
mouse_dev_t *mdev = calloc(1, sizeof(mouse_dev_t));
if (mdev == NULL) {
printf("%s: Error allocating mouse device. "
"Out of memory.\n", NAME);
return NULL;
}
link_initialize(&mdev->link);
return mdev;
}
static serial_dev_t *serial_dev_new(void)
{
serial_dev_t *sdev = calloc(1, sizeof(serial_dev_t));
if (sdev == NULL) {
printf("%s: Error allocating serial device. "
"Out of memory.\n", NAME);
return NULL;
}
sdev->kdev = kbd_dev_new();
if (sdev->kdev == NULL) {
free(sdev);
return NULL;
}
link_initialize(&sdev->link);
return sdev;
}
static void kbd_add_dev(kbd_port_ops_t *port, kbd_ctl_ops_t *ctl)
{
kbd_dev_t *kdev = kbd_dev_new();
if (kdev == NULL)
return;
kdev->port_ops = port;
kdev->ctl_ops = ctl;
kdev->svc_id = 0;
if ((*kdev->port_ops->init)(kdev) != 0)
goto fail;
if ((*kdev->ctl_ops->init)(kdev) != 0) {
goto fail;
}
list_append(&kdev->link, &kbd_devs);
return;
fail:
free(kdev);
}
static int kbd_add_kbdev(service_id_t service_id, kbd_dev_t **kdevp)
{
kbd_dev_t *kdev = kbd_dev_new();
if (kdev == NULL)
return -1;
kdev->svc_id = service_id;
kdev->port_ops = NULL;
kdev->ctl_ops = &kbdev_ctl;
errno_t rc = loc_service_get_name(service_id, &kdev->svc_name);
if (rc != EOK) {
kdev->svc_name = NULL;
goto fail;
}
if ((*kdev->ctl_ops->init)(kdev) != 0) {
goto fail;
}
list_append(&kdev->link, &kbd_devs);
*kdevp = kdev;
return 0;
fail:
if (kdev->svc_name != NULL)
free(kdev->svc_name);
free(kdev);
return -1;
}
static int mouse_add_mousedev(service_id_t service_id, mouse_dev_t **mdevp)
{
mouse_dev_t *mdev = mouse_dev_new();
if (mdev == NULL)
return -1;
mdev->svc_id = service_id;
mdev->port_ops = NULL;
mdev->proto_ops = &mousedev_proto;
errno_t rc = loc_service_get_name(service_id, &mdev->svc_name);
if (rc != EOK) {
mdev->svc_name = NULL;
goto fail;
}
if ((*mdev->proto_ops->init)(mdev) != 0) {
goto fail;
}
list_append(&mdev->link, &mouse_devs);
*mdevp = mdev;
return 0;
fail:
free(mdev);
return -1;
}
static errno_t serial_consumer(void *arg)
{
serial_dev_t *sdev = (serial_dev_t *) arg;
while (true) {
uint8_t data;
size_t nread;
chardev_read(sdev->chardev, &data, sizeof(data), &nread,
chardev_f_none);
kbd_push_data(sdev->kdev, data);
}
return EOK;
}
static int serial_add_srldev(service_id_t service_id, serial_dev_t **sdevp)
{
bool match = false;
errno_t rc;
serial_dev_t *sdev = serial_dev_new();
if (sdev == NULL)
return -1;
sdev->kdev->svc_id = service_id;
rc = loc_service_get_name(service_id, &sdev->kdev->svc_name);
if (rc != EOK)
goto fail;
list_append(&sdev->link, &serial_devs);
match = (serial_console != NULL) &&
!str_cmp(serial_console, sdev->kdev->svc_name);
if (match) {
sdev->kdev->ctl_ops = &stty_ctl;
if ((*sdev->kdev->ctl_ops->init)(sdev->kdev) != 0) {
list_remove(&sdev->link);
goto fail;
}
sdev->sess = loc_service_connect(service_id, INTERFACE_DDF,
IPC_FLAG_BLOCKING);
rc = chardev_open(sdev->sess, &sdev->chardev);
if (rc != EOK) {
async_hangup(sdev->sess);
sdev->sess = NULL;
list_remove(&sdev->link);
goto fail;
}
fid_t fid = fibril_create(serial_consumer, sdev);
fibril_add_ready(fid);
}
*sdevp = sdev;
return 0;
fail:
if (sdev->kdev->svc_name != NULL)
free(sdev->kdev->svc_name);
free(sdev->kdev);
free(sdev);
return -1;
}
static void kbd_add_legacy_devs(void)
{
#if defined(UARCH_arm32) && defined(MACHINE_gta02)
kbd_add_dev(&chardev_port, &stty_ctl);
#endif
#if defined(UARCH_ia64) && defined(MACHINE_ski)
kbd_add_dev(&chardev_port, &stty_ctl);
#endif
#if defined(MACHINE_msim)
kbd_add_dev(&chardev_port, &stty_ctl);
#endif
#if defined(UARCH_sparc64) && defined(PROCESSOR_sun4v)
kbd_add_dev(&chardev_port, &stty_ctl);
#endif
#if defined(UARCH_arm64) && defined(MACHINE_virt)
kbd_add_dev(&chardev_port, &stty_ctl);
#endif
#if defined(UARCH_arm64) && defined(MACHINE_hikey960)
kbd_add_dev(&chardev_port, &stty_ctl);
#endif
(void) kbd_add_dev;
}
static errno_t dev_check_new_kbdevs(void)
{
category_id_t keyboard_cat;
service_id_t *svcs;
size_t count, i;
bool already_known;
errno_t rc;
rc = loc_category_get_id("keyboard", &keyboard_cat, IPC_FLAG_BLOCKING);
if (rc != EOK) {
printf("%s: Failed resolving category 'keyboard'.\n", NAME);
return ENOENT;
}
rc = loc_category_get_svcs(keyboard_cat, &svcs, &count);
if (rc != EOK) {
printf("%s: Failed getting list of keyboard devices.\n",
NAME);
return EIO;
}
for (i = 0; i < count; i++) {
already_known = false;
list_foreach(kbd_devs, link, kbd_dev_t, kdev) {
if (kdev->svc_id == svcs[i]) {
already_known = true;
break;
}
}
if (!already_known) {
kbd_dev_t *kdev;
if (kbd_add_kbdev(svcs[i], &kdev) == 0) {
printf("%s: Connected keyboard device '%s'\n",
NAME, kdev->svc_name);
}
}
}
free(svcs);
return EOK;
}
static errno_t dev_check_new_mousedevs(void)
{
category_id_t mouse_cat;
service_id_t *svcs;
size_t count, i;
bool already_known;
errno_t rc;
rc = loc_category_get_id("mouse", &mouse_cat, IPC_FLAG_BLOCKING);
if (rc != EOK) {
printf("%s: Failed resolving category 'mouse'.\n", NAME);
return ENOENT;
}
rc = loc_category_get_svcs(mouse_cat, &svcs, &count);
if (rc != EOK) {
printf("%s: Failed getting list of mouse devices.\n",
NAME);
return EIO;
}
for (i = 0; i < count; i++) {
already_known = false;
list_foreach(mouse_devs, link, mouse_dev_t, mdev) {
if (mdev->svc_id == svcs[i]) {
already_known = true;
break;
}
}
if (!already_known) {
mouse_dev_t *mdev;
if (mouse_add_mousedev(svcs[i], &mdev) == 0) {
printf("%s: Connected mouse device '%s'\n",
NAME, mdev->svc_name);
}
}
}
free(svcs);
return EOK;
}
static errno_t dev_check_new_serialdevs(void)
{
category_id_t serial_cat;
service_id_t *svcs;
size_t count, i;
bool already_known;
errno_t rc;
rc = loc_category_get_id("serial", &serial_cat, IPC_FLAG_BLOCKING);
if (rc != EOK) {
printf("%s: Failed resolving category 'serial'.\n", NAME);
return ENOENT;
}
rc = loc_category_get_svcs(serial_cat, &svcs, &count);
if (rc != EOK) {
printf("%s: Failed getting list of serial devices.\n",
NAME);
return EIO;
}
for (i = 0; i < count; i++) {
already_known = false;
list_foreach(serial_devs, link, serial_dev_t, sdev) {
if (sdev->kdev->svc_id == svcs[i]) {
already_known = true;
break;
}
}
if (!already_known) {
serial_dev_t *sdev;
if (serial_add_srldev(svcs[i], &sdev) == 0) {
printf("%s: Connected serial device '%s'\n",
NAME, sdev->kdev->svc_name);
}
}
}
free(svcs);
return EOK;
}
static errno_t dev_check_new(void)
{
errno_t rc;
fibril_mutex_lock(&discovery_lock);
if (!serial_console) {
rc = dev_check_new_kbdevs();
if (rc != EOK) {
fibril_mutex_unlock(&discovery_lock);
return rc;
}
rc = dev_check_new_mousedevs();
if (rc != EOK) {
fibril_mutex_unlock(&discovery_lock);
return rc;
}
} else {
rc = dev_check_new_serialdevs();
if (rc != EOK) {
fibril_mutex_unlock(&discovery_lock);
return rc;
}
}
fibril_mutex_unlock(&discovery_lock);
return EOK;
}
static void cat_change_cb(void *arg)
{
dev_check_new();
}
static errno_t input_start_dev_discovery(void)
{
errno_t rc = loc_register_cat_change_cb(cat_change_cb, NULL);
if (rc != EOK) {
printf("%s: Failed registering callback for device discovery: "
"%s\n", NAME, str_error(rc));
return rc;
}
return dev_check_new();
}
static void usage(char *name)
{
printf("Usage: %s <service_name>\n", name);
}
int main(int argc, char **argv)
{
errno_t rc;
loc_srv_t *srv;
if (argc < 2) {
usage(argv[0]);
return 1;
}
printf("%s: HelenOS input service\n", NAME);
list_initialize(&clients);
list_initialize(&kbd_devs);
list_initialize(&mouse_devs);
list_initialize(&serial_devs);
serial_console = config_get_value("console");
kbd_add_legacy_devs();
async_set_client_data_constructor(client_data_create);
async_set_client_data_destructor(client_data_destroy);
async_set_fallback_port_handler(client_connection, NULL);
rc = loc_server_register(NAME, &srv);
if (rc != EOK) {
printf("%s: Unable to register server\n", NAME);
return rc;
}
service_id_t service_id;
rc = loc_service_register(srv, argv[1], &service_id);
if (rc != EOK) {
printf("%s: Unable to register service %s\n", NAME, argv[1]);
return rc;
}
rc = async_event_subscribe(EVENT_KCONSOLE, kconsole_event_handler, NULL);
if (rc != EOK)
printf("%s: Failed to register kconsole notifications (%s)\n",
NAME, str_error(rc));
input_start_dev_discovery();
printf("%s: Accepting connections\n", NAME);
task_retval(0);
async_manager();
return 0;
}
HelenOS homepage, sources at GitHub