aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-08-18 14:32:08 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2022-08-18 14:32:08 +0200
commit82dfd76ff534e362b7bfb3c12b870286a354f80b (patch)
tree815f344cf92433d2e47242aaa65069bc6213f3d7
parent774d4ea84ac0fd216821b6a058555576ab6db6af (diff)
downloadmessenger-cli-82dfd76ff534e362b7bfb3c12b870286a354f80b.tar.gz
messenger-cli-82dfd76ff534e362b7bfb3c12b870286a354f80b.zip
Normalized text input via macro and added lobby-entering dialog
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--Makefile5
-rw-r--r--src/ui/account_create_dialog.c53
-rw-r--r--src/ui/chat_open_dialog.c53
-rw-r--r--src/ui/chats.c14
-rw-r--r--src/ui/chats.h4
-rw-r--r--src/ui/lobby_enter_dialog.c104
-rw-r--r--src/ui/lobby_enter_dialog.h53
-rw-r--r--src/ui/messages.c62
-rw-r--r--src/ui/text_input.h85
9 files changed, 270 insertions, 163 deletions
diff --git a/Makefile b/Makefile
index ee7f64c..1713bff 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,7 @@ SOURCES = messenger_cli.c\
11 ui/accounts.c\ 11 ui/accounts.c\
12 ui/chat_open_dialog.c\ 12 ui/chat_open_dialog.c\
13 ui/chats.c\ 13 ui/chats.c\
14 ui/lobby_enter_dialog.c\
14 ui/members.c\ 15 ui/members.c\
15 ui/messages.c 16 ui/messages.c
16HEADERS = application.h\ 17HEADERS = application.h\
@@ -21,8 +22,10 @@ HEADERS = application.h\
21 ui/chat.h\ 22 ui/chat.h\
22 ui/chat_open_dialog.h\ 23 ui/chat_open_dialog.h\
23 ui/chats.h\ 24 ui/chats.h\
25 ui/lobby_enter_dialog.h\
24 ui/members.h\ 26 ui/members.h\
25 ui/messages.h 27 ui/messages.h\
28 ui/text_input.h
26 29
27LIBRARIES = gnunetchat gnunetutil ncurses 30LIBRARIES = gnunetchat gnunetutil ncurses
28 31
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 @@
24 24
25#include "account_create_dialog.h" 25#include "account_create_dialog.h"
26 26
27#include <ctype.h>
28
29#include <gnunet/platform.h> 27#include <gnunet/platform.h>
30#include <gnunet/gnunet_chat_lib.h> 28#include <gnunet/gnunet_chat_lib.h>
31#include <gnunet/gnunet_util_lib.h> 29#include <gnunet/gnunet_util_lib.h>
32 30
31#include "text_input.h"
33#include "../application.h" 32#include "../application.h"
34#include "../util.h" 33#include "../util.h"
35 34
@@ -42,69 +41,21 @@ account_create_dialog_event(UI_ACCOUNT_CREATE_DIALOG_Handle *create_dialog,
42 { 41 {
43 case 27: 42 case 27:
44 case KEY_EXIT: 43 case KEY_EXIT:
45 {
46 create_dialog->window = NULL; 44 create_dialog->window = NULL;
47 break; 45 break;
48 }
49 case KEY_LEFT:
50 {
51 create_dialog->name_pos--;
52 break;
53 }
54 case KEY_RIGHT:
55 {
56 create_dialog->name_pos++;
57 break;
58 }
59 case '\n': 46 case '\n':
60 case KEY_ENTER: 47 case KEY_ENTER:
61 {
62 if (create_dialog->name_len > 0) 48 if (create_dialog->name_len > 0)
63 GNUNET_CHAT_account_create(app->chat.handle, create_dialog->name); 49 GNUNET_CHAT_account_create(app->chat.handle, create_dialog->name);
64 50
65 create_dialog->name_len = 0; 51 create_dialog->name_len = 0;
66 create_dialog->window = NULL; 52 create_dialog->window = NULL;
67 break; 53 break;
68 }
69 case KEY_BACKSPACE:
70 {
71 if ((create_dialog->name_pos < create_dialog->name_len) &&
72 (create_dialog->name_pos > 0))
73 for (int i = create_dialog->name_pos; i < create_dialog->name_len; i++)
74 create_dialog->name[i - 1] = create_dialog->name[i];
75
76 if ((create_dialog->name_pos > 0) && (create_dialog->name_len > 0))
77 {
78 create_dialog->name_pos--;
79 create_dialog->name_len--;
80 }
81
82 break;
83 }
84 default: 54 default:
85 {
86 if (!isprint(key))
87 break;
88
89 for (int i = create_dialog->name_len - 1; i >= create_dialog->name_pos; i--)
90 create_dialog->name[i + 1] = create_dialog->name[i];
91
92 create_dialog->name[create_dialog->name_pos++] = (char) key;
93 create_dialog->name_len++;
94 break; 55 break;
95 }
96 } 56 }
97 57
98 if (create_dialog->name_len > 255) 58 text_input_event(create_dialog->name, key);
99 create_dialog->name_len = 255;
100
101 create_dialog->name[create_dialog->name_len] = '\0';
102
103 if (create_dialog->name_pos < 0)
104 create_dialog->name_pos = 0;
105
106 if (create_dialog->name_pos > create_dialog->name_len)
107 create_dialog->name_pos = create_dialog->name_len;
108} 59}
109 60
110void 61void
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 @@
24 24
25#include "chat_open_dialog.h" 25#include "chat_open_dialog.h"
26 26
27#include <ctype.h>
28
29#include <gnunet/platform.h> 27#include <gnunet/platform.h>
30#include <gnunet/gnunet_chat_lib.h> 28#include <gnunet/gnunet_chat_lib.h>
31#include <gnunet/gnunet_util_lib.h> 29#include <gnunet/gnunet_util_lib.h>
32 30
31#include "text_input.h"
33#include "../application.h" 32#include "../application.h"
34#include "../util.h" 33#include "../util.h"
35 34
@@ -42,69 +41,21 @@ chat_open_dialog_event(UI_CHAT_OPEN_DIALOG_Handle *open_dialog,
42 { 41 {
43 case 27: 42 case 27:
44 case KEY_EXIT: 43 case KEY_EXIT:
45 {
46 open_dialog->window = NULL; 44 open_dialog->window = NULL;
47 break; 45 break;
48 }
49 case KEY_LEFT:
50 {
51 open_dialog->topic_pos--;
52 break;
53 }
54 case KEY_RIGHT:
55 {
56 open_dialog->topic_pos++;
57 break;
58 }
59 case '\n': 46 case '\n':
60 case KEY_ENTER: 47 case KEY_ENTER:
61 {
62 if (open_dialog->topic_len > 0) 48 if (open_dialog->topic_len > 0)
63 GNUNET_CHAT_group_create(app->chat.handle, open_dialog->topic); 49 GNUNET_CHAT_group_create(app->chat.handle, open_dialog->topic);
64 50
65 open_dialog->topic_len = 0; 51 open_dialog->topic_len = 0;
66 open_dialog->window = NULL; 52 open_dialog->window = NULL;
67 break; 53 break;
68 }
69 case KEY_BACKSPACE:
70 {
71 if ((open_dialog->topic_pos < open_dialog->topic_len) &&
72 (open_dialog->topic_pos > 0))
73 for (int i = open_dialog->topic_pos; i < open_dialog->topic_len; i++)
74 open_dialog->topic[i - 1] = open_dialog->topic[i];
75
76 if ((open_dialog->topic_pos > 0) && (open_dialog->topic_len > 0))
77 {
78 open_dialog->topic_pos--;
79 open_dialog->topic_len--;
80 }
81
82 break;
83 }
84 default: 54 default:
85 {
86 if (!isprint(key))
87 break; 55 break;
88
89 for (int i = open_dialog->topic_len - 1; i >= open_dialog->topic_pos; i--)
90 open_dialog->topic[i + 1] = open_dialog->topic[i];
91
92 open_dialog->topic[open_dialog->topic_pos++] = (char) key;
93 open_dialog->topic_len++;
94 break;
95 }
96 } 56 }
97 57
98 if (open_dialog->topic_len > 255) 58 text_input_event(open_dialog->topic, key);
99 open_dialog->topic_len = 255;
100
101 open_dialog->topic[open_dialog->topic_len] = '\0';
102
103 if (open_dialog->topic_pos < 0)
104 open_dialog->topic_pos = 0;
105
106 if (open_dialog->topic_pos > open_dialog->topic_len)
107 open_dialog->topic_pos = open_dialog->topic_len;
108} 59}
109 60
110void 61void
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,
81 chat_open_dialog_event(&(chats->open_dialog), app, key); 81 chat_open_dialog_event(&(chats->open_dialog), app, key);
82 return; 82 return;
83 } 83 }
84 else if (chats->enter_dialog.window)
85 {
86 lobby_enter_dialog_event(&(chats->enter_dialog), app, key);
87 return;
88 }
84 89
85 chats->line_index = 0; 90 chats->line_index = 0;
86 chats->selected = NULL; 91 chats->selected = NULL;
@@ -140,8 +145,10 @@ chats_event(UI_CHATS_Handle *chats,
140 145
141 app->chat.context = chats->selected; 146 app->chat.context = chats->selected;
142 } 147 }
143 else 148 else if (chats->line_selected == count - 3)
144 chats->open_dialog.window = &(chats->window); 149 chats->open_dialog.window = &(chats->window);
150 else if (chats->line_selected == count - 1)
151 chats->enter_dialog.window = &(chats->window);
145 break; 152 break;
146 } 153 }
147 default: 154 default:
@@ -238,6 +245,11 @@ chats_print(UI_CHATS_Handle *chats,
238 chat_open_dialog_print(&(chats->open_dialog), app); 245 chat_open_dialog_print(&(chats->open_dialog), app);
239 return; 246 return;
240 } 247 }
248 else if (chats->enter_dialog.window)
249 {
250 lobby_enter_dialog_print(&(chats->enter_dialog), app);
251 return;
252 }
241 253
242 if (!(chats->window)) 254 if (!(chats->window))
243 return; 255 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 @@
34 34
35#include "chat_open_dialog.h" 35#include "chat_open_dialog.h"
36 36
37#include "lobby_enter_dialog.h"
38
37struct MESSENGER_Application; 39struct MESSENGER_Application;
38 40
39typedef struct UI_CHATS_Handle 41typedef struct UI_CHATS_Handle
@@ -47,6 +49,8 @@ typedef struct UI_CHATS_Handle
47 struct GNUNET_CHAT_Context *selected; 49 struct GNUNET_CHAT_Context *selected;
48 50
49 UI_CHAT_OPEN_DIALOG_Handle open_dialog; 51 UI_CHAT_OPEN_DIALOG_Handle open_dialog;
52
53 UI_LOBBY_ENTER_DIALOG_Handle enter_dialog;
50} UI_CHATS_Handle; 54} UI_CHATS_Handle;
51 55
52#define UI_CHATS_ROWS_MIN 8 56#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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/lobby_enter_dialog.c
23 */
24
25#include "lobby_enter_dialog.h"
26
27#include <gnunet/platform.h>
28#include <gnunet/gnunet_chat_lib.h>
29#include <gnunet/gnunet_util_lib.h>
30
31#include "text_input.h"
32#include "../application.h"
33#include "../util.h"
34
35void
36lobby_enter_dialog_event(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog,
37 struct MESSENGER_Application *app,
38 int key)
39{
40 struct GNUNET_CHAT_Uri *uri;
41
42 switch (key)
43 {
44 case 27:
45 case KEY_EXIT:
46 if (enter_dialog->error)
47 GNUNET_free(enter_dialog->error);
48
49 enter_dialog->error = NULL;
50 enter_dialog->window = NULL;
51 break;
52 case '\n':
53 case KEY_ENTER:
54 if (enter_dialog->uri_len > 0)
55 {
56 if (enter_dialog->error)
57 GNUNET_free(enter_dialog->error);
58
59 enter_dialog->error = NULL;
60 uri = GNUNET_CHAT_uri_parse(enter_dialog->uri, &(enter_dialog->error));
61
62 if (uri)
63 {
64 GNUNET_CHAT_lobby_join(app->chat.handle, uri);
65 GNUNET_CHAT_uri_destroy(uri);
66
67 enter_dialog->uri_len = 0;
68 enter_dialog->window = NULL;
69 }
70 }
71
72 break;
73 default:
74 break;
75 }
76
77 text_input_event(enter_dialog->uri, key);
78}
79
80void
81lobby_enter_dialog_print(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog,
82 UNUSED struct MESSENGER_Application *app)
83{
84 if (!(enter_dialog->window))
85 return;
86
87 WINDOW *window = *(enter_dialog->window);
88
89 werase(window);
90 wmove(window, 0, 0);
91
92 wprintw(window, "%s", enter_dialog->uri);
93
94 if (enter_dialog->error)
95 {
96 wmove(window, 1, 0);
97 wprintw(window, "ERROR: %s", enter_dialog->error);
98 }
99
100 wmove(window, 0, enter_dialog->uri_pos);
101
102 wcursyncup(window);
103 curs_set(1);
104}
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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/lobby_enter_dialog.h
23 */
24
25#ifndef UI_LOBBY_ENTER_DIALOG_H_
26#define UI_LOBBY_ENTER_DIALOG_H_
27
28#include <stdlib.h>
29#include <curses.h>
30
31struct MESSENGER_Application;
32
33typedef struct UI_LOBBY_ENTER_DIALOG_Handle
34{
35 WINDOW **window;
36
37 char *error;
38
39 char uri [2048];
40 int uri_len;
41 int uri_pos;
42} UI_LOBBY_ENTER_DIALOG_Handle;
43
44void
45lobby_enter_dialog_event(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog,
46 struct MESSENGER_Application *app,
47 int key);
48
49void
50lobby_enter_dialog_print(UI_LOBBY_ENTER_DIALOG_Handle *enter_dialog,
51 struct MESSENGER_Application *app);
52
53#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 @@
24 24
25#include "messages.h" 25#include "messages.h"
26 26
27#include "text_input.h"
27#include "../application.h" 28#include "../application.h"
28#include "../util.h" 29#include "../util.h"
29 30
@@ -39,8 +40,6 @@ _messages_iterate(UI_MESSAGES_Handle *messages,
39 messages->selected = message; 40 messages->selected = message;
40} 41}
41 42
42
43
44void 43void
45_messages_handle_message(UI_MESSAGES_Handle *messages) 44_messages_handle_message(UI_MESSAGES_Handle *messages)
46{ 45{
@@ -94,38 +93,19 @@ messages_event(UI_MESSAGES_Handle *messages,
94 { 93 {
95 case 27: 94 case 27:
96 case KEY_EXIT: 95 case KEY_EXIT:
97 {
98 app->chat.context = NULL; 96 app->chat.context = NULL;
99 break; 97 break;
100 }
101 case KEY_LEFT:
102 {
103 messages->text_pos--;
104 break;
105 }
106 case KEY_RIGHT:
107 {
108 messages->text_pos++;
109 break;
110 }
111 case KEY_UP: 98 case KEY_UP:
112 {
113 messages->line_selected--; 99 messages->line_selected--;
114 break; 100 break;
115 }
116 case KEY_DOWN: 101 case KEY_DOWN:
117 {
118 messages->line_selected++; 102 messages->line_selected++;
119 break; 103 break;
120 }
121 case '\t': 104 case '\t':
122 {
123 app->chat.show_members = TRUE; 105 app->chat.show_members = TRUE;
124 break; 106 break;
125 }
126 case '\n': 107 case '\n':
127 case KEY_ENTER: 108 case KEY_ENTER:
128 {
129 if (messages->selected) 109 if (messages->selected)
130 _messages_handle_message(messages); 110 _messages_handle_message(messages);
131 else if (messages->text_len > 0) 111 else if (messages->text_len > 0)
@@ -134,55 +114,19 @@ messages_event(UI_MESSAGES_Handle *messages,
134 messages->text_len = 0; 114 messages->text_len = 0;
135 } 115 }
136 break; 116 break;
137 }
138 case KEY_BACKSPACE: 117 case KEY_BACKSPACE:
139 {
140 if (messages->selected) 118 if (messages->selected)
141 {
142 GNUNET_CHAT_message_delete( 119 GNUNET_CHAT_message_delete(
143 messages->selected, 120 messages->selected,
144 GNUNET_TIME_relative_get_zero_() 121 GNUNET_TIME_relative_get_zero_()
145 ); 122 );
146 break;
147 }
148
149 if ((messages->text_pos < messages->text_len) &&
150 (messages->text_pos > 0))
151 for (int i = messages->text_pos; i < messages->text_len; i++)
152 messages->text[i - 1] = messages->text[i];
153
154 if ((messages->text_pos > 0) && (messages->text_len > 0))
155 {
156 messages->text_pos--;
157 messages->text_len--;
158 }
159
160 break; 123 break;
161 }
162 default: 124 default:
163 {
164 if ((messages->selected) || (!isprint(key)))
165 break;
166
167 for (int i = messages->text_len - 1; i >= messages->text_pos; i--)
168 messages->text[i + 1] = messages->text[i];
169
170 messages->text[messages->text_pos++] = (char) key;
171 messages->text_len++;
172 break; 125 break;
173 }
174 } 126 }
175 127
176 if (messages->text_len >= TEXT_LEN_MAX) 128 if (!(messages->selected))
177 messages->text_len = TEXT_LEN_MAX - 1; 129 text_input_event(messages->text, key);
178
179 messages->text[messages->text_len] = '\0';
180
181 if (messages->text_pos < 0)
182 messages->text_pos = 0;
183
184 if (messages->text_pos > messages->text_len)
185 messages->text_pos = messages->text_len;
186 130
187 if (messages->line_selected < 0) 131 if (messages->line_selected < 0)
188 messages->line_selected = 0; 132 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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/text_input.h
23 */
24
25#ifndef UI_TEXT_INPUT_H_
26#define UI_TEXT_INPUT_H_
27
28#include <ctype.h>
29
30#define text_input_event(text, key) { \
31 switch (key) \
32 { \
33 case KEY_LEFT: \
34 { \
35 text##_pos--; \
36 break; \
37 } \
38 case KEY_RIGHT: \
39 { \
40 text##_pos++; \
41 break; \
42 } \
43 case KEY_BACKSPACE: \
44 { \
45 if ((text##_pos < text##_len) && (text##_pos > 0)) \
46 for (int i = text##_pos; i < text##_len; i++) \
47 text[i - 1] = text[i]; \
48 \
49 if ((text##_pos > 0) && (text##_len > 0)) \
50 { \
51 text##_pos--; \
52 text##_len--; \
53 } \
54 \
55 break; \
56 } \
57 default: \
58 { \
59 if (!isprint(key)) \
60 break; \
61 \
62 for (int i = text##_len - 1; i >= text##_pos; i--) \
63 text[i + 1] = text[i]; \
64 \
65 text[text##_pos++] = (char) key; \
66 text##_len++; \
67 break; \
68 } \
69 } \
70 \
71 int max_len = (int) sizeof(text) - 1; \
72 \
73 if (text##_len > max_len) \
74 text##_len = max_len; \
75 \
76 text[text##_len] = '\0'; \
77 \
78 if (text##_pos < 0) \
79 text##_pos = 0; \
80 \
81 if (text##_pos > text##_len) \
82 text##_pos = text##_len; \
83}
84
85#endif /* UI_TEXT_INPUT_H_ */