From e6d3d3f5b17d7337432c710453e5126a8e1f6809 Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sat, 24 Mar 2012 15:44:06 +0100 Subject: [PATCH] ui: add user interface subsystem The UI subsystem starts the terminals, switches between them and draws the screen. Signed-off-by: David Herrmann --- src/ui.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui.h | 49 ++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 src/ui.c create mode 100644 src/ui.h diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..e81b75a --- /dev/null +++ b/src/ui.c @@ -0,0 +1,168 @@ +/* + * User Interface + * + * Copyright (c) 2012 David Herrmann + * + * 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. + */ + +/* + * User Interface + * Implementation of the user interface. + */ + +#include +#include +#include +#include "conf.h" +#include "eloop.h" +#include "font.h" +#include "input.h" +#include "log.h" +#include "terminal.h" +#include "ui.h" +#include "unicode.h" +#include "uterm.h" + +#define LOG_SUBSYSTEM "config" + +struct kmscon_ui { + struct ev_eloop *eloop; + struct uterm_video *video; + struct kmscon_input *input; + struct kmscon_symbol_table *st; + struct kmscon_font_factory *ff; + struct kmscon_terminal *term; +}; + +static void video_event(struct uterm_video *video, + struct uterm_video_hotplug *ev, + void *data) +{ + struct kmscon_ui *ui = data; + struct uterm_display *iter; + int ret; + + kmscon_terminal_rm_all_outputs(ui->term); + iter = uterm_video_get_displays(ui->video); + for ( ; iter; iter = uterm_display_next(iter)) { + if (uterm_display_get_state(iter) == UTERM_DISPLAY_INACTIVE) { + ret = uterm_display_activate(iter, NULL); + if (ret) + continue; + } + + kmscon_terminal_add_output(ui->term, iter); + } +} + +static void input_event(struct kmscon_input *input, + struct kmscon_input_event *ev, + void *data) +{ + struct kmscon_ui *ui = data; + int ret; + + ret = kmscon_terminal_input(ui->term, ev); + if (ret) { + kmscon_terminal_close(ui->term); + ev_eloop_exit(ui->eloop); + } +} + +int kmscon_ui_new(struct kmscon_ui **out, + struct ev_eloop *eloop, + struct uterm_video *video, + struct kmscon_input *input) +{ + struct kmscon_ui *ui; + int ret; + + if (!out || !eloop || !video || !input) + return -EINVAL; + + ui = malloc(sizeof(*ui)); + if (!ui) + return -ENOMEM; + memset(ui, 0, sizeof(*ui)); + ui->eloop = eloop; + ui->video = video; + ui->input = input; + + ret = kmscon_symbol_table_new(&ui->st); + if (ret) + goto err_free; + + ret = kmscon_font_factory_new(&ui->ff, ui->st); + if (ret) + goto err_st; + + ret = kmscon_terminal_new(&ui->term, eloop, ui->ff, ui->video, ui->st); + if (ret) + goto err_ff; + + ret = uterm_video_register_cb(ui->video, video_event, ui); + if (ret) + goto err_term; + + ret = kmscon_input_register_cb(ui->input, input_event, ui); + if (ret) + goto err_video; + + ret = kmscon_terminal_open(ui->term, NULL, NULL); + if (ret) + goto err_input; + + ev_eloop_ref(ui->eloop); + uterm_video_ref(ui->video); + kmscon_input_ref(ui->input); + *out = ui; + return 0; + +err_input: + kmscon_input_unregister_cb(ui->input, input_event); +err_video: + uterm_video_unregister_cb(ui->video, video_event); +err_term: + kmscon_terminal_unref(ui->term); +err_ff: + kmscon_font_factory_unref(ui->ff); +err_st: + kmscon_symbol_table_unref(ui->st); +err_free: + free(ui); + return ret; +} + +void kmscon_ui_free(struct kmscon_ui *ui) +{ + if (!ui) + return; + + kmscon_input_unregister_cb(ui->input, input_event); + uterm_video_unregister_cb(ui->video, video_event); + kmscon_terminal_unref(ui->term); + kmscon_font_factory_unref(ui->ff); + kmscon_symbol_table_unref(ui->st); + kmscon_input_unref(ui->input); + uterm_video_unref(ui->video); + ev_eloop_unref(ui->eloop); + free(ui); +} diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..d03b4f6 --- /dev/null +++ b/src/ui.h @@ -0,0 +1,49 @@ +/* + * User Interface + * + * Copyright (c) 2012 David Herrmann + * + * 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. + */ + +/* + * User Interface + * This is the main UI object that handles all currently opened terminals and + * draws the screen. + */ + +#ifndef KMSCON_UI_H +#define KMSCON_UI_H + +#include +#include +#include "eloop.h" +#include "input.h" +#include "uterm.h" + +struct kmscon_ui; + +int kmscon_ui_new(struct kmscon_ui **out, + struct ev_eloop *eloop, + struct uterm_video *video, + struct kmscon_input *input); +void kmscon_ui_free(struct kmscon_ui *ui); + +#endif /* KMSCON_UI_H */