aboutsummaryrefslogtreecommitdiff
path: root/arch/casiowin/easy-input/src/main.c
blob: 24c408de871cbe799849dbbea14c634c73533432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* ****************************************************************************
 * main.c -- the easy input main file.
 * Copyright (C) 2015-2016 Louis "Dark Storm" Gatin <l.gatin@neuf.fr>
 * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of the 'casiowin/easy-input' module,
 * in libcarrot, an experimental modular libc project.
 *
 * This file is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This file 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 Lesser General Lesser Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include <EasyInput.h>

char *EI_input_string(int string_length, const char *chars_allowed)
{
	char *string = (void*)(0);
	EI_config *config = (void*)(0);
	EI_cursor_settings cursor_settings;

	unsigned int key = 0;

	int i;
	int char_deleted = 0;
	int text_displacement = 0;
	int cap = 0;

	char arrow[] = {0xE6, 0x9A, 0};


	/* Get actual configuration */
	config = (EI_config*)EI_manage_config(EI_GET_ALL, 0);

	/* Allocate RAM for string */
	string = (char*)malloc(sizeof(char) * string_length + 1);

	/* Initialize string */
	for(i = 0; i <= string_length; i++) string[i] = 0;

	/* Setup cursor flash */
	switch(config->align)
	{
		default:
		case EI_ALIGN_LEFT:
			EI_Cursor_SetPosition(config->column, config->row);
			break;

		case EI_ALIGN_CENTER:
			EI_Cursor_SetPosition(config->column + (config->box_length >> 1),
				config->row);
			break;

		case EI_ALIGN_RIGHT:
			EI_Cursor_SetPosition(config->column + config->box_length - 1,
				config->row);
			break;
	}

	if(config->start_mode == EI_ALPHA_LOCKED)
	{
		EI_KBD_PutKey(KEY_CTRL_SHIFT, 0);
		GetKey(&key);
	}

	if(config->start_mode >= EI_ALPHA)
	{
		EI_KBD_PutKey(KEY_CTRL_ALPHA, 0);
		GetKey(&key);
		EI_Cursor_SetFlashOn(3);
	}
	else
	{
		EI_Cursor_SetFlashOn(0);
	}


	/* Main loop */
	i = 0;
	while(key != KEY_CTRL_EXE && key != KEY_CTRL_AC)
	{
		EI_Cursor_GetSettings(&cursor_settings);
		
		if(cursor_settings.flashstyle == 3 && !cap)
			EI_Cursor_SetFlashOn(4);

		GetKey(&key); /* Getting key pressed */

		switch(key) /* Analyse key pressed */
		{
			case KEY_CTRL_F1:
				cap = !cap;
				break;

			case KEY_CTRL_DEL:
				if(i > 0)
				{
					i--;
					string[i] = 0;
					char_deleted = 1;
				}
				break;
				
			default:
				/* Give correct value to keys with a non-ascii return values */
				if(key == KEY_CHAR_PLUS) key = '+';
				if(key == KEY_CHAR_MINUS || key == KEY_CHAR_PMINUS) key = '-';
				if(key == KEY_CHAR_MULT) key = '*';
				if(key == KEY_CHAR_DIV) key = '/';
				if(key == KEY_CHAR_ANS) key = '_';
				if(key == KEY_CTRL_XTT) key = '#';

				if(key > 127) /* KEY_CTRL_***, etc. */
					break;

				/* Switch between caps */
				if(key >= 'A' && key <= 'Z') key += !cap * 32;

				if (EI_check_char(key, chars_allowed)
				 && EI_str_length(string) != string_length)
				{
					string[i] = key;
					i++;
				}
		}

		switch(config->align) /* Print string */
		{
			default:
			case EI_ALIGN_LEFT:
				if(EI_str_length(string) < config->box_length)
				{
					EI_Cursor_SetPosition(config->column, config->row);
					Print((unsigned char*)string);
					if(char_deleted && EI_str_length(string)
					 != config->box_length - 1)
					{
						Print((unsigned char*)" ");
						EI_Cursor_SetPosition(cursor_settings.column - 2,
						 config->row);
						char_deleted = 0;
					}
				}
				else
				{
					EI_Cursor_SetPosition(config->column, config->row);
					Print((unsigned char*)arrow);
					Print((unsigned char*)(string + EI_str_length(string)
					 - config->box_length + 2));
				}
				break;

			case EI_ALIGN_CENTER:
				if(EI_str_length(string) < config->box_length)
				{
					if(char_deleted && EI_str_length(string)
					 != config->box_length - 1 && (config->box_length - EI_str_length(string)) >> 1 != ((config->box_length - EI_str_length(string) - 1) >> 1))
					{
						EI_Cursor_SetPosition(config->column + ((config->box_length - EI_str_length(string)) >> 1) - 1, config->row);
						Print((unsigned char*)" ");
						Print((unsigned char*)string);
						char_deleted = 0;
					}
					else if(char_deleted && EI_str_length(string) != config->box_length - 1 && (config->box_length - EI_str_length(string)) >> 1 == ((config->box_length - EI_str_length(string) - 1) >> 1))
					{
						EI_Cursor_SetPosition(config->column + ((config->box_length - EI_str_length(string)) >> 1), config->row);
						Print((unsigned char*)string);
						Print((unsigned char*)" ");
						EI_Cursor_SetPosition(cursor_settings.column - 2, config->row);
						char_deleted = 0;
					}
					else
					{
						EI_Cursor_SetPosition(config->column + ((config->box_length - EI_str_length(string)) >> 1), config->row);
						Print((unsigned char*)string);
					}
				}
				else
				{
					EI_Cursor_SetPosition(config->column, config->row);
					Print((unsigned char*)arrow);
					Print((unsigned char*)(string + EI_str_length(string) - config->box_length + 2));
				}
				break;

			case EI_ALIGN_RIGHT:
				if(EI_str_length(string) < config->box_length)
				{
					EI_Cursor_SetPosition(config->column + config->box_length - EI_str_length(string) - (char_deleted && EI_str_length(string) != config->box_length - 1) - 1, config->row);
					if(char_deleted && EI_str_length(string) != config->box_length - 1)
					{
						Print((unsigned char*)" ");
						char_deleted = 0;
					}
					Print((unsigned char*)string);
				}
				else
				{
					EI_Cursor_SetPosition(config->column, config->row);
					Print((unsigned char*)arrow);
					Print((unsigned char*)(string + EI_str_length(string) - config->box_length + 2));
				}
				break;
		}
	}

	/* Set off cursor mode */
	EI_Cursor_SetFlashOff();


	/* Return NULL in case of an input break */
	if(key == KEY_CTRL_AC)
	{
		free(string);
		return (char*)(0);
	}


	/* Return the string */
	return string;
}

const void *EI_manage_config(enum __EI_PARAM_TYPE parameter, int value)
{
	static EI_config config;

	if(value)
	{
		switch(parameter)
		{
			case EI_SET_COLUMN: config.column = value - 1;
				break;
			case EI_SET_ROW: config.row = value - 1;
				break;
			case EI_SET_BOX_LENGTH: config.box_length = (value < 3 ? 3 : value); /* Minimal size is 3 */
				break;
			case EI_SET_ALIGN: config.align = value;
				break;
			case EI_SET_START_MODE: config.start_mode = value;
				break;
			case EI_SET_Aa_KEY: config.Aa_key = value;
				break;
		}
	}
	else
	{
		switch(parameter)
		{
			case EI_GET_ALL: return (const void*)(&config);
			case EI_SET_COLUMN: return (const void*)&config.column;
			case EI_SET_ROW: return (const void*)&config.row;
			case EI_SET_BOX_LENGTH: return (const void*)&config.box_length;
			case EI_SET_ALIGN: return (const void*)&config.align;
			case EI_SET_START_MODE: return (const void*)&config.start_mode;
			case EI_SET_Aa_KEY: return (const void*)&config.Aa_key;
		}
	}

	return (void*)(0);
}

void EI_init(void)
{
	EI_manage_config(EI_SET_COLUMN, 1);
	EI_manage_config(EI_SET_ROW, 1);
	EI_manage_config(EI_SET_ALIGN, EI_ALIGN_LEFT);
	EI_manage_config(EI_SET_START_MODE, EI_ALPHA_LOCKED);
	EI_manage_config(EI_SET_BOX_LENGTH, 21);
	EI_manage_config(EI_SET_Aa_KEY, KEY_CTRL_F1);
}

int EI_str_length(const char *str)
{
	int i = 0;

	while(str[i]) i++;
	
	return i;
}

int EI_check_char(char c, const char *chars_allowed)
{
	int i = 0;

	while(chars_allowed[i])
	{
		if(c == chars_allowed[i])
			return 1;
		i++;
	}

	return 0;
}