summaryrefslogtreecommitdiffstats
path: root/src/user/iface.c
blob: 75da415ea1287237ae2da95603b19484525d3a1a (plain) (blame)
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
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "vision-artificial/user/iface.h"

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#include "libalx/base/stdio/get.h"

#include "vision-artificial/image/iface.h"
#include "vision-artificial/proc/iface.h"
#include "vision-artificial/user/clui.h"
#include "vision-artificial/user/tui.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
#define TITLE_SIZE	(80)


/******************************************************************************
 ******* enums ****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* structs / unions *****************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
/* Global --------------------------------------------------------------------*/
	int			user_iface_mode;
	struct User_Iface_Log	user_iface_log;
/* Static --------------------------------------------------------------------*/


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/
static	void	user_iface_act		(int action);
static	void	user_iface_show_ocr	(void);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	user_iface_init		(void)
{

	user_iface_log.len	= 0;
	user_iface_log.pos	= -1;

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		break;
	case USER_IFACE_TUI:
		user_tui_init();
		break;
	}
}

void	user_iface_cleanup	(void)
{

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		break;
	case USER_IFACE_TUI:
		user_tui_cleanup();
		break;
	}
	fflush(stdout);
}

void	user_iface		(void)
{
	char	title[TITLE_SIZE];
	char	subtitle[TITLE_SIZE];
	int	user_action;

	snprintf(title, TITLE_SIZE, "Title");
	snprintf(subtitle, TITLE_SIZE, "Subtitle");

	do {
		img_iface_show_img();

		switch (user_iface_mode) {
		case USER_IFACE_CLUI:
			user_action	= user_clui(title, subtitle);
			break;
		case USER_IFACE_TUI:
			user_action	= user_tui(title, subtitle);
			break;
		default:
			user_action	= USER_IFACE_ACT_FOO;
		}
		user_iface_act(user_action);
	} while (user_action != USER_IFACE_ACT_QUIT);
}

void	user_iface_show_log	(const char *restrict title,
				const char *restrict subtitle)
{

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		user_clui_show_log(title, subtitle);
		break;
	case USER_IFACE_TUI:
		user_tui_show_log(title, subtitle);
		break;
	}
}

void	user_iface_fname	(const char *restrict filepath,
				char *restrict filename)
{

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		user_clui_fname(filepath, filename);
		break;
	case USER_IFACE_TUI:
		user_tui_fname(filepath, filename);
		break;
	}
}

double	user_iface_getdbl	(double m, double def, double M,
				const char *restrict title,
				const char *restrict help)
{

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		return	alx_get_int(m, def, M, title, help, 2);
	case USER_IFACE_TUI:
		return	user_tui_getint(m, def, M, title, help);
	default:
		return	1;
	}
}

int	user_iface_getint	(double m, int64_t def, double M,
				const char *restrict title,
				const char *restrict help)
{

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		return	alx_get_dbl(m, def, M, title, help, 2);
	case USER_IFACE_TUI:
		return	user_tui_getdbl(m, def, M, title, help);
	default:
		return	1.0;
	}
}

void	user_iface_log_write	(ptrdiff_t lvl, const char *restrict msg)
{

	snprintf(user_iface_log.line[user_iface_log.len], LOG_LINE_LEN, "%s",
									msg);
	user_iface_log.lvl[user_iface_log.len]	= lvl;
	user_iface_log.len++;
}

const char *user_iface_log_read	(void)
{

	user_iface_log.pos++;
	for (; user_iface_log.pos < user_iface_log.len; user_iface_log.pos++) {
		if (user_iface_log.lvl[user_iface_log.pos] <=
						user_iface_log.visible) {
			break;
		}
	}
	if (user_iface_log.pos == user_iface_log.len)
		return	NULL;
	return	&user_iface_log.line[user_iface_log.pos][0];
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/
static	void	user_iface_act		(int action)
{

	if (action & USER_IFACE_ACT_USRI) {
		switch (action) {
		case USER_IFACE_ACT_SHOW_OCR:
			user_iface_show_ocr();
			break;
		case USER_IFACE_ACT_QUIT:
			/* do nothing */
			break;
		}
	} else if (action & USER_IFACE_ACT_PROC) {
		proc_iface_single(action);
	} else {
		img_iface_act(action, NULL);
	}
}

static	void	user_iface_show_ocr	(void)
{

	switch (user_iface_mode) {
	case USER_IFACE_CLUI:
		user_clui_show_ocr();
		break;
	case USER_IFACE_TUI:
		user_tui_show_ocr();
		break;
	}
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/