diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-08-18 14:32:08 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-08-18 14:32:08 +0200 |
commit | 82dfd76ff534e362b7bfb3c12b870286a354f80b (patch) | |
tree | 815f344cf92433d2e47242aaa65069bc6213f3d7 | |
parent | 774d4ea84ac0fd216821b6a058555576ab6db6af (diff) |
Normalized text input via macro and added lobby-entering dialog
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | src/ui/account_create_dialog.c | 53 | ||||
-rw-r--r-- | src/ui/chat_open_dialog.c | 53 | ||||
-rw-r--r-- | src/ui/chats.c | 14 | ||||
-rw-r--r-- | src/ui/chats.h | 4 | ||||
-rw-r--r-- | src/ui/lobby_enter_dialog.c | 104 | ||||
-rw-r--r-- | src/ui/lobby_enter_dialog.h | 53 | ||||
-rw-r--r-- | src/ui/messages.c | 62 | ||||
-rw-r--r-- | src/ui/text_input.h | 85 |
9 files changed, 270 insertions, 163 deletions
@@ -11,6 +11,7 @@ SOURCES = messenger_cli.c\ ui/accounts.c\ ui/chat_open_dialog.c\ ui/chats.c\ + ui/lobby_enter_dialog.c\ ui/members.c\ ui/messages.c HEADERS = application.h\ @@ -21,8 +22,10 @@ HEADERS = application.h\ ui/chat.h\ ui/chat_open_dialog.h\ ui/chats.h\ + ui/lobby_enter_dialog.h\ ui/members.h\ - ui/messages.h + ui/messages.h\ + ui/text_input.h LIBRARIES = gnunetchat gnunetutil ncurses diff --git a/src/ui/account_create_dialog.c b/src/ui/account_create_dialog.c index 5472bc7..f3c9394 100644 --- a/src/ui/account_create_dialog.c +++ b/src/ui/account_create_dialog.c @@ -24,12 +24,11 @@ #include "account_create_dialog.h" -#include <ctype.h> - #include <gnunet/platform.h> #include <gnunet/gnunet_chat_lib.h> #include <gnunet/gnunet_util_lib.h> +#include "text_input.h" #include "../application.h" #include "../util.h" @@ -42,69 +41,21 @@ account_create_dialog_event(UI_ACCOUNT_CREATE_DIALOG_Handle *create_dialog, { case 27: case KEY_EXIT: - { create_dialog->window = NULL; break; - } - case KEY_LEFT: - { - create_dialog->name_pos--; - break; - } - case KEY_RIGHT: - { - create_dialog->name_pos++; - break; - } case '\n': case KEY_ENTER: - { if (create_dialog->name_len > 0) GNUNET_CHAT_account_create(app->chat.handle, create_dialog->name); create_dialog->name_len = 0; create_dialog->window = NULL; break; - } - case KEY_BACKSPACE: - { - if ((create_dialog->name_pos < create_dialog->name_len) && - (create_dialog->name_pos > 0)) - for (int i = create_dialog->name_pos; i < create_dialog->name_len; i++) - create_dialog->name[i - 1] = create_dialog->name[i]; - - if ((create_dialog->name_pos > 0) && (create_dialog->name_len > 0)) - { - create_dialog->name_pos--; - create_dialog->name_len--; - } - - break; - } default: - { - if (!isprint(key)) - break; - - for (int i = create_dialog->name_len - 1; i >= create_dialog->name_pos; i--) - create_dialog->name[i + 1] = create_dialog->name[i]; - - create_dialog->name[create_dialog->name_pos++] = (char) key; - create_dialog->name_len++; break; - } } - if (create_dialog->name_len > 255) - create_dialog->name_len = 255; - - create_dialog->name[create_dialog->name_len] = '\0'; - - if (create_dialog->name_pos < 0) - create_dialog->name_pos = 0; - - if (create_dialog->name_pos > create_dialog->name_len) - create_dialog->name_pos = create_dialog->name_len; + text_input_event(create_dialog->name, key); } void diff --git a/src/ui/chat_open_dialog.c b/src/ui/chat_open_dialog.c index 701b8e5..5b641d6 100644 --- a/src/ui/chat_open_dialog.c +++ b/src/ui/chat_open_dialog.c @@ -24,12 +24,11 @@ #include "chat_open_dialog.h" -#include <ctype.h> - #include <gnunet/platform.h> #include <gnunet/gnunet_chat_lib.h> #include <gnunet/gnunet_util_lib.h> +#include "text_input.h" #include "../application.h" #include "../util.h" @@ -42,69 +41,21 @@ chat_open_dialog_event(UI_CHAT_OPEN_DIALOG_Handle *open_dialog, { case 27: case KEY_EXIT: - { open_dialog->window = NULL; break; - } - case KEY_LEFT: - { - open_dialog->topic_pos--; - break; - } - case KEY_RIGHT: - { - open_dialog->topic_pos++; - break; - } case '\n': case KEY_ENTER: - { if (open_dialog->topic_len > 0) GNUNET_CHAT_group_create(app->chat.handle, open_dialog->topic); open_dialog->topic_len = 0; open_dialog->window = NULL; break; - } - case KEY_BACKSPACE: - { - if ((open_dialog->topic_pos < open_dialog->topic_len) && - (open_dialog->topic_pos > 0)) - for (int i = open_dialog->topic_pos; i < open_dialog->topic_len; i++) - open_dialog->topic[i - 1] = open_dialog->topic[i]; - - if ((open_dialog->topic_pos > 0) && (open_dialog->topic_len > 0)) - { - open_dialog->topic_pos--; - open_dialog->topic_len--; - } - - break; - } default: - { - if (!isprint(key)) break; - - for (int i = open_dialog->topic_len - 1; i >= open_dialog->topic_pos; i--) - open_dialog->topic[i + 1] = open_dialog->topic[i]; - - open_dialog->topic[open_dialog->topic_pos++] = (char) key; - open_dialog->topic_len++; - break; - } } - if (open_dialog->topic_len > 255) - open_dialog->topic_len = 255; - - open_dialog->topic[open_dialog->topic_len] = '\0'; - - if (open_dialog->topic_pos < 0) - open_dialog->topic_pos = 0; - - if (open_dialog->topic_pos > open_dialog->topic_len) - open_dialog->topic_pos = open_dialog->topic_len; + text_input_event(open_dialog->topic, key); } void diff --git a/src/ui/chats.c b/src/ui/chats.c index 1b472a8..1a25927 100644 --- a/src/ui/chats.c +++ b/src/ui/chats.c @@ -81,6 +81,11 @@ chats_event(UI_CHATS_Handle *chats, chat_open_dialog_event(&(chats->open_dialog), app, key); return; } + else if (chats->enter_dialog.window) + { + lobby_enter_dialog_event(&(chats->enter_dialog), app, key); + return; + } chats->line_index = 0; chats->selected = NULL; @@ -140,8 +145,10 @@ chats_event(UI_CHATS_Handle *chats, app->chat.context = chats->selected; } - else + else if (chats->line_selected == count - 3) chats->open_dialog.window = &(chats->window); + else if (chats->line_selected == count - 1) + chats->enter_dialog.window = &(chats->window); break; } default: @@ -238,6 +245,11 @@ chats_print(UI_CHATS_Handle *chats, chat_open_dialog_print(&(chats->open_dialog), app); return; } + else if (chats->enter_dialog.window) + { + lobby_enter_dialog_print(&(chats->enter_dialog), app); + return; + } if (!(chats->window)) return; diff --git a/src/ui/chats.h b/src/ui/chats.h index 339cb9c..b355dc7 100644 --- a/src/ui/chats.h +++ b/src/ui/chats.h @@ -34,6 +34,8 @@ #include "chat_open_dialog.h" +#include "lobby_enter_dialog.h" + struct MESSENGER_Application; typedef struct UI_CHATS_Handle @@ -47,6 +49,8 @@ typedef struct UI_CHATS_Handle struct GNUNET_CHAT_Context *selected; UI_CHAT_OPEN_DIALOG_Handle open_dialog; + + UI_LOBBY_ENTER_DIALOG_Handle enter_dialog; } UI_CHATS_Handle; #define UI_CHATS_ROWS_MIN 8 diff --git a/src/ui/lobby_enter_dialog.c b/src/ui/lobby_enter_dialog.c new file mode 100644 index 0000000..6de4d1c --- /dev/null +++ b/src/ui/lobby_enter_dialog.c @@ -0,0 +1,104 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/lobby_enter_dialog.c + */ + +#include "lobby_enter_dialog.h" + +#include <gnunet/platform.h> +#include <gnunet/gnunet_chat_lib.h> +#include <gnunet/gnunet_util_lib.h> + +#include "text_input.h" +#include "../application.h" +#include "../util.h" + +void +lobby_enter_dialog_event(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog, + struct MESSENGER_Application *app, + int key) +{ + struct GNUNET_CHAT_Uri *uri; + + switch (key) + { + case 27: + case KEY_EXIT: + if (enter_dialog->error) + GNUNET_free(enter_dialog->error); + + enter_dialog->error = NULL; + enter_dialog->window = NULL; + break; + case '\n': + case KEY_ENTER: + if (enter_dialog->uri_len > 0) + { + if (enter_dialog->error) + GNUNET_free(enter_dialog->error); + + enter_dialog->error = NULL; + uri = GNUNET_CHAT_uri_parse(enter_dialog->uri, &(enter_dialog->error)); + + if (uri) + { + GNUNET_CHAT_lobby_join(app->chat.handle, uri); + GNUNET_CHAT_uri_destroy(uri); + + enter_dialog->uri_len = 0; + enter_dialog->window = NULL; + } + } + + break; + default: + break; + } + + text_input_event(enter_dialog->uri, key); +} + +void +lobby_enter_dialog_print(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog, + UNUSED struct MESSENGER_Application *app) +{ + if (!(enter_dialog->window)) + return; + + WINDOW *window = *(enter_dialog->window); + + werase(window); + wmove(window, 0, 0); + + wprintw(window, "%s", enter_dialog->uri); + + if (enter_dialog->error) + { + wmove(window, 1, 0); + wprintw(window, "ERROR: %s", enter_dialog->error); + } + + wmove(window, 0, enter_dialog->uri_pos); + + wcursyncup(window); + curs_set(1); +} diff --git a/src/ui/lobby_enter_dialog.h b/src/ui/lobby_enter_dialog.h new file mode 100644 index 0000000..81ddeac --- /dev/null +++ b/src/ui/lobby_enter_dialog.h @@ -0,0 +1,53 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/lobby_enter_dialog.h + */ + +#ifndef UI_LOBBY_ENTER_DIALOG_H_ +#define UI_LOBBY_ENTER_DIALOG_H_ + +#include <stdlib.h> +#include <curses.h> + +struct MESSENGER_Application; + +typedef struct UI_LOBBY_ENTER_DIALOG_Handle +{ + WINDOW **window; + + char *error; + + char uri [2048]; + int uri_len; + int uri_pos; +} UI_LOBBY_ENTER_DIALOG_Handle; + +void +lobby_enter_dialog_event(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog, + struct MESSENGER_Application *app, + int key); + +void +lobby_enter_dialog_print(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog, + struct MESSENGER_Application *app); + +#endif /* UI_LOBBY_ENTER_DIALOG_H_ */ diff --git a/src/ui/messages.c b/src/ui/messages.c index 1194f5b..211fe94 100644 --- a/src/ui/messages.c +++ b/src/ui/messages.c @@ -24,6 +24,7 @@ #include "messages.h" +#include "text_input.h" #include "../application.h" #include "../util.h" @@ -39,8 +40,6 @@ _messages_iterate(UI_MESSAGES_Handle *messages, messages->selected = message; } - - void _messages_handle_message(UI_MESSAGES_Handle *messages) { @@ -94,38 +93,19 @@ messages_event(UI_MESSAGES_Handle *messages, { case 27: case KEY_EXIT: - { app->chat.context = NULL; break; - } - case KEY_LEFT: - { - messages->text_pos--; - break; - } - case KEY_RIGHT: - { - messages->text_pos++; - break; - } case KEY_UP: - { messages->line_selected--; break; - } case KEY_DOWN: - { messages->line_selected++; break; - } case '\t': - { app->chat.show_members = TRUE; break; - } case '\n': case KEY_ENTER: - { if (messages->selected) _messages_handle_message(messages); else if (messages->text_len > 0) @@ -134,55 +114,19 @@ messages_event(UI_MESSAGES_Handle *messages, messages->text_len = 0; } break; - } case KEY_BACKSPACE: - { if (messages->selected) - { GNUNET_CHAT_message_delete( messages->selected, GNUNET_TIME_relative_get_zero_() ); - break; - } - - if ((messages->text_pos < messages->text_len) && - (messages->text_pos > 0)) - for (int i = messages->text_pos; i < messages->text_len; i++) - messages->text[i - 1] = messages->text[i]; - - if ((messages->text_pos > 0) && (messages->text_len > 0)) - { - messages->text_pos--; - messages->text_len--; - } - break; - } default: - { - if ((messages->selected) || (!isprint(key))) - break; - - for (int i = messages->text_len - 1; i >= messages->text_pos; i--) - messages->text[i + 1] = messages->text[i]; - - messages->text[messages->text_pos++] = (char) key; - messages->text_len++; break; - } } - if (messages->text_len >= TEXT_LEN_MAX) - messages->text_len = TEXT_LEN_MAX - 1; - - messages->text[messages->text_len] = '\0'; - - if (messages->text_pos < 0) - messages->text_pos = 0; - - if (messages->text_pos > messages->text_len) - messages->text_pos = messages->text_len; + if (!(messages->selected)) + text_input_event(messages->text, key); if (messages->line_selected < 0) messages->line_selected = 0; diff --git a/src/ui/text_input.h b/src/ui/text_input.h new file mode 100644 index 0000000..8dc416f --- /dev/null +++ b/src/ui/text_input.h @@ -0,0 +1,85 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/text_input.h + */ + +#ifndef UI_TEXT_INPUT_H_ +#define UI_TEXT_INPUT_H_ + +#include <ctype.h> + +#define text_input_event(text, key) { \ + switch (key) \ + { \ + case KEY_LEFT: \ + { \ + text##_pos--; \ + break; \ + } \ + case KEY_RIGHT: \ + { \ + text##_pos++; \ + break; \ + } \ + case KEY_BACKSPACE: \ + { \ + if ((text##_pos < text##_len) && (text##_pos > 0)) \ + for (int i = text##_pos; i < text##_len; i++) \ + text[i - 1] = text[i]; \ + \ + if ((text##_pos > 0) && (text##_len > 0)) \ + { \ + text##_pos--; \ + text##_len--; \ + } \ + \ + break; \ + } \ + default: \ + { \ + if (!isprint(key)) \ + break; \ + \ + for (int i = text##_len - 1; i >= text##_pos; i--) \ + text[i + 1] = text[i]; \ + \ + text[text##_pos++] = (char) key; \ + text##_len++; \ + break; \ + } \ + } \ + \ + int max_len = (int) sizeof(text) - 1; \ + \ + if (text##_len > max_len) \ + text##_len = max_len; \ + \ + text[text##_len] = '\0'; \ + \ + if (text##_pos < 0) \ + text##_pos = 0; \ + \ + if (text##_pos > text##_len) \ + text##_pos = text##_len; \ +} + +#endif /* UI_TEXT_INPUT_H_ */ |