summaryrefslogtreecommitdiffstats
path: root/libalx/src/alx_ncur.c
blob: 116a3bbc668fec79621ee296f8abdb77e2933e30 (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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/******************************************************************************
 *	Copyright (C) 2017	Alejandro Colomar Andrés		      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
/*	*	*	*	*	*	*	*	*	*
 *	*	* Standard	*	*	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
	#include <ncurses.h>
	#include <inttypes.h>
	#include <stdarg.h>
	#include <stdbool.h>
	#include <stdio.h>
	#include <string.h>
		/* wchar_t */
	#include <wchar.h>

/*	*	*	*	*	*	*	*	*	*
 *	*	* Other	*	*	*	*	*	*	*
 *	*	*	*	*	*	*	*	*	*/
	#include "alx_ncur.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
	# define	BUFF_SIZE	(1024)

	# define	MAX_TRIES	(2)

	# define	ERR_OK		(0)
	# define	ERR_RANGE	(1)
	# define	ERR_SSCANF	(2)
	# define	ERR_GETSTR	(3)
	# define	ERR_FPTR	(4)

	# define	ERR_RANGE_MSG	"¡ Number is out of range !"
	# define	ERR_SSCANF_MSG	"¡ sscanf() error !"
	# define	ERR_GETSTR_MSG	"¡ wgetstr() error !"
	# define	ERR_FPTR_MSG	"¡ FILE error !"


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	alx_ncur_prn_menu	(WINDOW *win,
					int N, struct Alx_Menu mnu[N]);
static	int	alx_ncur_usr_sel	(WINDOW *win,
					int N, struct Alx_Menu mnu[N]);
static	double	loop_w_getdbl		(WINDOW *win,
					double m, double def, double M);
static	int64_t	loop_w_getint		(WINDOW *win,
					double m, int64_t def, double M);
static	void	loop_w_getstr		(char *str, WINDOW *win);

static	void	loop_w_getfname		(const char *fpath, char *fname, bool exist,
					WINDOW *win);
static	void	manage_w_error		(WINDOW *win, int err);


/******************************************************************************
 ******* main *****************************************************************
 ******************************************************************************/
void	alx_start_curses	(void)
{
	initscr();
	nonl();
	cbreak();
	noecho();
	keypad(stdscr, true);

	if (has_colors()) {
		start_color();
		use_default_colors();
	}
//	mousemask(BUTTON4_PRESSED | BUTTON2_PRESSED, NULL);
}

void	alx_pause_curses	(void)
{
	def_prog_mode();
	endwin();
}

void	alx_resume_curses	(void)
{
	fflush(stdout);
	reset_prog_mode();
}

void	alx_end_curses		(void)
{
	clear();
	refresh();
	endwin();
}

void	alx_win_del		(WINDOW *win)
{
	/* Delete window */
	wbkgd(win, 0);
	wclear(win);
	wrefresh(win);
	delwin(win);
}

int	alx_menu		(int h, int w,
				int N, struct Alx_Menu mnu[N], const char *title)
{
	/* Dimensions */
	WINDOW	*win;
	int	r;
	int	c;
	r =	1;
	c =	(80 - w) / 2;
	win =	newwin(h, w, r, c);

	/* Input */
	int	i;
	i =	alx_menu_2(win, N, mnu, title);

	/* Delete window */
	alx_win_del(win);

	return	i;
}

int	alx_menu_2		(WINDOW *win,
				int N, struct Alx_Menu mnu[N], const char *title)
{
	/* Activate keypad, and don't echo input */
	keypad(win, true);
	noecho();

	/* Print box, title and menu items */
	box(win, 0, 0);
	alx_ncur_prn_title(win, title);
	alx_ncur_prn_menu(win, N, mnu);

	/* Input */
	int	i;
	i =	alx_ncur_usr_sel(win, N, mnu);

	return	i;
}

double	alx_w_getdbl		(int w, int r, const char *title,
				double m, double def, double M,
				const char *format, ...)
{
	/* Varargs */
	va_list	args;
	va_start(args, format);

	/* Dimensions */
	WINDOW	*win1;
	int	h1;
	int	w1;
	int	r1;
	int	c1;
	WINDOW	*win2;
	int	h2;
	int	w2;
	int	r2;
	int	c2;
	WINDOW	*win3;
	int	h3;
	int	w3;
	int	r3;
	int	c3;
	h1 =	3;
	w1 =	w;
	r1 =	r;
	c1 =	(80 - w) / 2;
	h2 =	1;
	w2 =	w1 - 4;
	r2 =	r1 + 3;
	c2 =	c1 + 2;
	h3 =	1;
	w3 =	w1 - 2;
	r3 =	r1 + 1;
	c3 =	c1 + 1;

	/* Box & title */
	win1 =	newwin(h1, w1, r1, c1);
	wbkgd(win1, A_REVERSE);
	box(win1, 0, 0);
	alx_ncur_prn_title(win1, title);
	wrefresh(win1);

	/* Help */
	win2 =	newwin(h2, w2, r2, c2);
	if (format == NULL) {
		wprintw(win2, "Introduce a real number [%lf U %lf] (default %lf)", m, M, def);
	} else {
		vw_printw(win2, format, args);
	}
	wrefresh(win2);

	/* Input */
	double	R;
	win3 =	newwin(h3, w3, r3, c3);
	wbkgd(win3, A_REVERSE);
	wrefresh(win3);
	R =	loop_w_getdbl(win3, m, def, M);

	/* Delete window */
	alx_win_del(win3);
	alx_win_del(win2);
	alx_win_del(win1);

	/* End varargs */
	va_end(args);

	return	R;
}

int64_t	alx_w_getint		(int w, int r, const char *title,
				double m, int64_t def, double M,
				const char *format, ...)
{
	/* Varargs */
	va_list	args;
	va_start(args, format);

	/* Dimensions */
	WINDOW	*win1;
	int	h1;
	int	w1;
	int	r1;
	int	c1;
	WINDOW	*win2;
	int	h2;
	int	w2;
	int	r2;
	int	c2;
	WINDOW	*win3;
	int	h3;
	int	w3;
	int	r3;
	int	c3;
	h1 =	3;
	w1 =	w;
	r1 =	r;
	c1 =	(80 - w) / 2;
	h2 =	1;
	w2 =	w1 - 4;
	r2 =	r1 + 3;
	c2 =	c1 + 2;
	h3 =	1;
	w3 =	w1 - 2;
	r3 =	r1 + 1;
	c3 =	c1 + 1;

	/* Box & title */
	win1 =	newwin(h1, w1, r1, c1);
	wbkgd(win1, A_REVERSE);
	box(win1, 0, 0);
	alx_ncur_prn_title(win1, title);
	wrefresh(win1);

	/* Help */
	win2 =	newwin(h2, w2, r2, c2);
	if (format == NULL) {
		wprintw(win2, "Introduce an integer number [%lf U %lf] (default %"PRIi64")", m, M, def);
	} else {
		vw_printw(win2, format, args);
	}
	wrefresh(win2);

	/* Input */
	int	Z;
	win3 =	newwin(h3, w3, r3, c3);
	wbkgd(win3, A_REVERSE);
	wrefresh(win3);
	Z =	loop_w_getint(win3, m, def, M);

	/* Delete window */
	alx_win_del(win3);
	alx_win_del(win2);
	alx_win_del(win1);

	/* End varargs */
	va_end(args);

	return	Z;
}

void	alx_w_getstr		(char *str,
				int w, int r, const char *title,
				const char *format, ...)
{
	/* Varargs */
	va_list	args;
	va_start(args, format);

	/* Dimensions */
	WINDOW	*win1;
	int	h1;
	int	w1;
	int	r1;
	int	c1;
	h1 =	3;
	w1 =	w;
	r1 =	r;
	c1 =	(80 - w) / 2;
	WINDOW	*win2;
	int	h2;
	int	w2;
	int	r2;
	int	c2;
	h2 =	1;
	w2 =	w1 - 4;
	r2 =	r1 + 3;
	c2 =	c1 + 2;
	WINDOW	*win3;
	int	h3;
	int	w3;
	int	r3;
	int	c3;
	h3 =	1;
	w3 =	w1 - 2;
	r3 =	r1 + 1;
	c3 =	c1 + 1;

	/* Box & title */
	win1 =	newwin(h1, w1, r1, c1);
	wbkgd(win1, A_REVERSE);
	box(win1, 0, 0);
	alx_ncur_prn_title(win1, title);
	wrefresh(win1);

	/* Help */
	win2 =	newwin(h2, w2, r2, c2);
	if (format == NULL) {
		waddstr(win2, "Introduce a string");
	} else {
		vw_printw(win2, format, args);
	}
	wrefresh(win2);

	/* Input */
	win3 =	newwin(h3, w3, r3, c3);
	wbkgd(win3, A_REVERSE);
	wrefresh(win3);
	loop_w_getstr(str, win3);

	/* Delete window */
	alx_win_del(win3);
	alx_win_del(win2);
	alx_win_del(win1);

	/* End varargs */
	va_end(args);
}

void	alx_w_getfname		(const char *fpath, char *fname, bool exist,
				int w, int r, const char *title,
				const char *format, ...)
{
	/* Varargs */
	va_list	args;
	va_start(args, format);

	/* Dimensions */
	WINDOW	*win1;
	int	h1;
	int	w1;
	int	r1;
	int	c1;
	WINDOW	*win2;
	int	h2;
	int	w2;
	int	r2;
	int	c2;
	WINDOW	*win3;
	int	h3;
	int	w3;
	int	r3;
	int	c3;
	h1 =	3;
	w1 =	w;
	r1 =	r;
	c1 =	(80 - w) / 2;
	h2 =	1;
	w2 =	w1 - 4;
	r2 =	r1 + 3;
	c2 =	c1 + 2;
	h3 =	1;
	w3 =	w1 - 2;
	r3 =	r1 + 1;
	c3 =	c1 + 1;

	/* Box & title */
	win1 =	newwin(h1, w1, r1, c1);
	wbkgd(win1, A_REVERSE);
	box(win1, 0, 0);
	alx_ncur_prn_title(win1, title);
	wrefresh(win1);

	/* Help */
	win2 =	newwin(h2, w2, r2, c2);
	if (format == NULL) {
		waddstr(win2, "Introduce a file name");
	} else {
		vw_printw(win2, format, args);
	}
	wrefresh(win2);

	/* Input */
	win3 =	newwin(h3, w3, r3, c3);
	wbkgd(win3, A_REVERSE);
	wrefresh(win3);
	loop_w_getfname(fpath, fname, exist, win3);

	/* Delete window */
	alx_win_del(win3);
	alx_win_del(win2);
	alx_win_del(win1);

	/* End varargs */
	va_end(args);
}

void	alx_ncur_prn_title	(WINDOW *win, const char *title)
{
	/* Find size of window */
	int	h;
	int	w;
	getmaxyx(win, h, w);

	/* Find length of title */
	int	len;
	len =	strlen(title);

	/* Print title centered */
	mvwaddch(win, 0, (w - (len + 2))/2 - 1, ACS_RTEE);
	wprintw(win, " %s ", title);
	waddch(win, ACS_LTEE);
}

void	alx_ncur_prn_subtitle	(WINDOW *win, const char *subtitle)
{
	/* Find size of window */
	int	h;
	int	w;
	getmaxyx(win, h, w);

	/* Find length of title */
	int	len;
	len =	strlen(subtitle);

	/* Print subtitle centered */
	mvwaddch(win, h - 1, (w - (len + 2))/2 - 1, ACS_RTEE);
	wprintw(win, " %s ", subtitle);
	waddch(win, ACS_LTEE);
}


/******************************************************************************
 ******* static functions *****************************************************
 ******************************************************************************/
static	void	alx_ncur_prn_menu	(WINDOW *win,
					int N, struct Alx_Menu mnu[N])
{
	/* Find size of window */
	int	h;
	int	w;
	getmaxyx(win, h, w);

	/* Print all menu items */
	int	i;
	for (i = 0; i < N; i++) {
		mvwaddstr(win, mnu[i].r, mnu[i].c, mnu[i].t);
	}

	wrefresh(win);
}

static	int	alx_ncur_usr_sel	(WINDOW *win,
					int N, struct Alx_Menu mnu[N])
{
	/* default item */
	int	i =	1;
	wmove(win, mnu[i].r, mnu[i].c + 1);

	/* Receive input until ENTER key */
	bool	wh;
	wchar_t	ch;
	wh = true;
	while (wh) {
		/* Input */
		ch = wgetch(win);

		switch (ch) {
		case KEY_UP:
		case 'w':
		case 'k':
			/* KEY_UP, move one item up */
			if (i) {
				i--;
			} else {
				i = N - 1;
			}
			wmove(win, mnu[i].r, mnu[i].c + 1);
			break;

		case KEY_DOWN:
		case 's':
		case 'j':
			/* KEY_DOWN, move one item down */
			if (i != N - 1) {
				i++;
			} else {
				i = 0;
			}
			wmove(win, mnu[i].r, mnu[i].c + 1);
			break;

		/* '\r' is Enter key in Windows */
		case '\r':
		case '\n':
		case ' ':
			/* ENTER / SPACE, end menu */
			wh = false;
			break;

		default:
			if ((ch >= '0') && (ch < N + '0')) {
				/* Input is a number, move to that item & end menu */
				i = ch - '0';
				wmove(win, mnu[i].r, mnu[i].c + 1);
				wh = false;
			}
			break;

		}

	}

	return	i;
}

static	double	loop_w_getdbl		(WINDOW *win,
					double m, double def, double M)
{
	int	i;
	char	buff [BUFF_SIZE];
	int	x;
	double	R;
	int	err;

	for (i = 0; i < MAX_TRIES; i++) {
		echo();
		x =	mvwgetnstr(win, 0, 0, buff, BUFF_SIZE);
		noecho();
		wclear(win);
		wrefresh(win);

		err	= ERR_OK;
		if (x == ERR) {
			err =	ERR_GETSTR;
		} else if (sscanf(buff, "%lf", &R) != 1) {
			err =	ERR_SSCANF;
		} else if (R < m || R > M) {
			err =	ERR_RANGE;
		} else {
			break;
		}

		manage_w_error(win, err);
		R =	def;
	}

	return	R;
}

static	int64_t	loop_w_getint		(WINDOW *win,
					double m, int64_t def, double M)
{
	int	i;
	char	buff [BUFF_SIZE];
	int	x;
	int64_t	Z;
	int	err;

	for (i = 0; i < MAX_TRIES; i++) {
		echo();
		x	= mvwgetnstr(win, 0, 0, buff, BUFF_SIZE);
		noecho();
		wclear(win);
		wrefresh(win);

		err	= ERR_OK;
		if (x == ERR) {
			err	= ERR_GETSTR;
		} else if (sscanf(buff, "%"SCNi64, &Z) != 1) {
			err	= ERR_SSCANF;
		} else if (Z < m || Z > M) {
			err	= ERR_RANGE;
		} else {
			break;
		}

		manage_w_error(win, err);
		Z =	def;
	}

	return	Z;
}

static	void	loop_w_getstr		(char *str, WINDOW *win)
{
	int	i;
	char	buff [BUFF_SIZE];
	int	x;
	int	err;

	for (i = 0; i < MAX_TRIES; i++) {
		echo();
		x	= mvwgetnstr(win, 0, 0, buff, BUFF_SIZE);
		noecho();
		wclear(win);
		wrefresh(win);

		err	= ERR_OK;
		if (x == ERR) {
			err	= ERR_GETSTR;
		} else {
			break;
		}

		manage_w_error(win, err);
	}

	if (!err) {
		strcpy(str, buff);
	}
}

static	void	loop_w_getfname		(const char *fpath, char *fname, bool exist,
					WINDOW *win)
{
	int	i;
	char	buff [FILENAME_MAX];
	char	buff2 [FILENAME_MAX];
	char	file_path [FILENAME_MAX];
	int	x;
	int	err;
	FILE	*fp;

	for (i = 0; i < MAX_TRIES; i++) {
		echo();
		x	= mvwgetnstr(win, 0, 0, buff, FILENAME_MAX);
		noecho();
		wclear(win);
		wrefresh(win);

		err	= ERR_OK;
		if (x == ERR) {
			err =	ERR_GETSTR;
		} else if (sscanf(buff, " %s ", buff2) != 1) {
			err	= ERR_SSCANF;
		} else {
			strcpy(file_path, fpath);
			strcat(file_path, buff2);

			fp =	fopen(file_path, "r");

			if (exist) {
				if (fp == NULL) {
					err =	ERR_FPTR;
				} else {
					fclose(fp);
					break;
				}
			} else {
				if (fp != NULL) {
					err =	ERR_FPTR;
					fclose(fp);
				} else {
					break;
				}
			}
		}

		manage_w_error(win, err);
	}

	if (!err) {
		strcpy(fname, buff2);
	}
}

static	void	manage_w_error		(WINDOW *win, int err)
{
	switch (err) {
	case ERR_RANGE:
		mvwaddstr(win, 0, 0, ERR_RANGE_MSG);
		break;
	case ERR_SSCANF:
		mvwaddstr(win, 0, 0, ERR_SSCANF_MSG);
		break;
	case ERR_GETSTR:
		mvwaddstr(win, 0, 0, ERR_GETSTR_MSG);
		break;
	case ERR_FPTR:
		mvwaddstr(win, 0, 0, ERR_FPTR_MSG);
		break;
	}
	wrefresh(win);
	/* Wait for any key */
	wgetch(win);
	wclear(win);
	wrefresh(win);
}


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