From 7a5f5091f6c8603bba0944e4b52eecf41133189c Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 13 Dec 2011 14:10:01 +0200 Subject: [PATCH] tests: add test for the input subsystem It should: - Capture your keypresses - Announce when a device is added or removed (with --enable-debug) - stop/start capturing on SIGQUIT (Ctrl-\) Signed-off-by: Ran Benita Signed-off-by: David Herrmann --- Makefile.am | 6 ++- tests/test_input.c | 127 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tests/test_input.c diff --git a/Makefile.am b/Makefile.am index 81ea168..7f96f27 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,8 @@ ACLOCAL_AMFLAGS = -I m4 bin_PROGRAMS = kmscon -check_PROGRAMS = test_console test_output test_vt test_buffer test_terminal +check_PROGRAMS = test_console test_output test_vt test_buffer test_terminal \ + test_input noinst_LTLIBRARIES = libkmscon-core.la AM_CFLAGS = \ @@ -76,4 +77,7 @@ test_buffer_LDADD = libkmscon-core.la test_terminal_SOURCES = tests/test_terminal.c test_terminal_LDADD = libkmscon-core.la +test_input_SOURCES = tests/test_input.c +test_input_LDADD = libkmscon-core.la + dist_doc_DATA = README TODO diff --git a/tests/test_input.c b/tests/test_input.c new file mode 100644 index 0000000..265db3b --- /dev/null +++ b/tests/test_input.c @@ -0,0 +1,127 @@ +/* + * test_input - Test the input system - hotplug and keypresses + * + * Copyright (c) 2011 Ran Benita + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "eloop.h" +#include "input.h" +#include "log.h" + +static bool terminate; + +static void sig_term(struct kmscon_signal *sig, int signum, void *data) +{ + terminate = true; +} + +/* Pressing Ctrl-\ should toggle the capturing. */ +static void sig_quit(struct kmscon_signal *sig, int signum, void *data) +{ + struct kmscon_input *input = data; + + if (kmscon_input_is_asleep(input)) { + kmscon_input_wake_up(input); + log_info("Woke Up\n"); + } else { + kmscon_input_sleep(input); + log_info("Went to sleep\n"); + } +} + +static void input_arrived(struct kmscon_input *input, uint16_t type, + uint16_t code, int32_t value, void *data) +{ + printf("got (type %x) (code %x) (value %x)\n", type, code, value); +} + +int main(int argc, char **argv) +{ + int ret; + struct kmscon_eloop *loop; + struct kmscon_input *input; + struct kmscon_signal *sigint, *sigquit; + + ret = kmscon_eloop_new(&loop); + if (ret) { + log_err("Cannot create eloop\n"); + goto err_out; + } + + ret = kmscon_input_new(&input); + if (ret) { + log_err("Cannot create input\n"); + goto err_loop; + } + + ret = kmscon_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL); + if (ret) { + log_err("Cannot add INT signal\n"); + goto err_input; + } + + ret = kmscon_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input); + if (ret) { + log_err("Cannot add quit signal\n"); + goto err_sigint; + } + + ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL); + if (ret) { + log_err("Cannot connect input\n"); + goto err_sigquit; + } + + kmscon_input_wake_up(input); + + system("stty -echo"); + + while (!terminate) { + ret = kmscon_eloop_dispatch(loop, -1); + if (ret) { + log_err("Dispatcher failed\n"); + break; + } + } + + system("stty echo"); + +err_sigquit: + kmscon_eloop_rm_signal(sigquit); +err_sigint: + kmscon_eloop_rm_signal(sigint); +err_input: + kmscon_input_unref(input); +err_loop: + kmscon_eloop_unref(loop); +err_out: + return abs(ret); +}