summaryrefslogtreecommitdiffstats
path: root/src/host/layer23/src/ui/ui.c
blob: 3e0258051112e0b6d322e55c410ab2c1a0e427c8 (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
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
725
726
727
728
729
730
731
732
733
734
735
736
/* (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <osmocom/core/select.h>
#include <osmocom/bb/ui/ui.h>
#include <osmocom/bb/ui/telnet_interface.h>
#include <osmocom/bb/common/l1ctl.h>

static char *ui_center(const char *text)
{
	static char line[UI_COLS + 1];
	int len, shift;

	strncpy(line, text, UI_COLS);
	line[UI_COLS] = '\0';
	len = strlen(line);
	if (len + 1 < UI_COLS) {
		shift = (UI_COLS - len) / 2;
		memcpy(line + shift, line, len + 1);
		memset(line, ' ', shift);
	}

	return line;
}

/*
 * io functions
 */

int ui_clearhome(struct ui_inst *ui)
{
	int i;

	/* initialize with spaces */
	memset(ui->buffer, ' ', sizeof(ui->buffer));
	/* terminate with EOL */
	for (i = 0; i < UI_ROWS; i++)
		ui->buffer[(UI_COLS + 1) * (i + 1) - 1] = '\0';

	ui->cursor_x = ui->cursor_y = 0;
	ui->cursor_on = 0;

	return 0;
}

int ui_puts(struct ui_inst *ui, int ln, const char *text)
{
	int len = strlen(text);

	/* out of range */
	if (ln < 0 || ln >= UI_ROWS)
		return -EINVAL;

	/* clip */
	if (len > UI_COLS)
		len = UI_COLS;

	/* copy */
	if (len)
		memcpy(ui->buffer + (UI_COLS + 1) * ln, text, len);

	return 0;
}

int ui_flush(struct ui_inst *ui)
{
	int i;
	char frame[UI_COLS + 5];
	char line[UI_COLS + 5];
	char cursor[16];

	/* clear */
	ui_telnet_puts(ui, "\033c");

	/* display */
	memset(frame + 1, '-', UI_COLS);
	frame[0] = frame[UI_COLS + 1] = '+';
	frame[UI_COLS + 2] = '\r';
	frame[UI_COLS + 3] = '\n';
	frame[UI_COLS + 4] = '\0';
	ui_telnet_puts(ui, frame);
	for (i = 0; i < UI_ROWS; i++) {
		sprintf(line, "|%s|\r\n", ui->buffer + (UI_COLS + 1) * i);
		{
			// HACK
			struct gsm_ui *gui = container_of(ui, struct gsm_ui, ui);
			struct osmocom_ms *ms = container_of(gui, struct osmocom_ms, gui);
			l1ctl_tx_display_req(ms, 0, 8 + i * 8, 0, 0, 0, i == 0, i == UI_ROWS - 1, ui->buffer + (UI_COLS + 1) * i);
			printf("%s\n", ui->buffer + (UI_COLS + 1) * i);
		}
		ui_telnet_puts(ui, line);
	}
	ui_telnet_puts(ui, frame);

	ui_telnet_puts(ui, "\r\n"
		"1 2 3 4 5 6 7 8 9 * 0 # = digits\r\n"
		"Pos1 = pickup, End = hangup\r\n"
		"F1 = left button, F2 = right button\r\n"
		"arrow keys = navigation buttons\r\n"); 

	/* set cursor */
	if (ui->cursor_on) {
		sprintf(cursor, "\033[%d;%dH", ui->cursor_y + 2,
			ui->cursor_x + 2);
		ui_telnet_puts(ui, cursor);
	}

	return 0;
}

static int bottom_puts(struct ui_inst *ui, const char *text)
{
	char bottom_line[UI_COLS + 1], *p;
	int space;

	strncpy(bottom_line, text, UI_COLS);
	bottom_line[UI_COLS] = '\0';
	if ((p = strchr(bottom_line, ' '))
	 && (space = UI_COLS - strlen(bottom_line))) {
	 	p++;
	 	memcpy(p + space, p, strlen(p));
		memset(p, ' ', space);
	}

	return ui_puts(ui, UI_ROWS - 1, bottom_line);
}

/*
 * listview
 */

static int init_listview(struct ui_view *uv, union ui_view_data *ud)
{
	ud->listview.vpos = 0;
	ud->listview.lines = 0;

	return 0;
}

static int keypad_listview(struct ui_inst *ui, struct ui_view *uv,
	union ui_view_data *ud, enum ui_key kp)
{
	int rows = UI_ROWS;

	if (ui->title)
		rows--;
	if (ui->bottom_line)
		rows--;

	switch (kp) {
	case UI_KEY_UP:
		if (ud->listview.vpos == 0)
			return -1;
		ud->listview.vpos--;
		break;
	case UI_KEY_DOWN:
		if (rows + ud->listview.vpos >= ud->listview.lines)
			return -1;
		ud->listview.vpos++;
		break;
	default:
		return 0;
	}
	/* refresh display */
	uv->display(ui, ud);

	return 0;
}

static int display_listview(struct ui_inst *ui, union ui_view_data *ud)
{
	const char **text = ud->listview.text;
	int lines = ud->listview.lines;
	int i, j = 0;
	int rows = UI_ROWS;
	
	if (ui->bottom_line)
		rows--;

	/* vpos will skip lines */
	for (i = 0; i < ud->listview.vpos; i++) {
		/* if we reached end of test, we leave the pointer there */
		if (*text == NULL)
			break;
		text++;
		j++;
	}

	ui_clearhome(ui);
	/* title */
	i = 0;
	if (ui->title)
		ui_puts(ui, i++, ui_center(ui->title));
	for (; i < rows; i++) {
		if (*text && j < lines) {
			ui_puts(ui, i, *text);
			text++;
			j++;
		} else
			break;
//			ui_puts(ui, i, "~");
	}
	if (ui->bottom_line)
		bottom_puts(ui, ui->bottom_line);
	ui_flush(ui);

	return 0;
}

/*
 * selectview
 */

static int init_selectview(struct ui_view *uv, union ui_view_data *ud)
{
	ud->selectview.vpos = 0;
	ud->selectview.cursor = 0;
	ud->selectview.lines = 0;

	return 0;
}

static int keypad_selectview(struct ui_inst *ui, struct ui_view *uv,
	union ui_view_data *ud, enum ui_key kp)
{
	int rows = UI_ROWS;
	
	if (ui->title)
		rows--;
	if (ui->bottom_line)
		rows--;

	switch (kp) {
	case UI_KEY_UP:
		if (ud->selectview.cursor == 0)
			return -1;
		ud->selectview.cursor--;
		/* follow cursor */
		if (ud->selectview.cursor < ud->selectview.vpos)
			ud->selectview.vpos = ud->selectview.cursor;
		break;
	case UI_KEY_DOWN:
		if (ud->selectview.cursor >= ud->selectview.lines - 1)
			return -1;
		ud->selectview.cursor++;
		/* follow cursor */
		if (ud->selectview.cursor > ud->selectview.vpos + rows - 1)
			ud->selectview.vpos = ud->selectview.cursor -
								(rows - 1);
		break;
	default:
		return 0;
	}
	/* refresh display */
	uv->display(ui, ud);

	return 0;
}

static int display_selectview(struct ui_inst *ui, union ui_view_data *ud)
{
	const char **text = ud->selectview.text;
	int lines = ud->selectview.lines;
	int i, j = 0, y = 0;
	int rows = UI_ROWS;
	char line[UI_COLS + 1];
	
	if (ui->bottom_line)
		rows--;

	/* vpos will skip lines */
	for (i = 0; i < ud->selectview.vpos; i++) {
		/* if we reached end of test, we leave the pointer there */
		if (*text == NULL)
			break;
		text++;
		j++;
	}

	ui_clearhome(ui);
	/* title */
	i = 0;
	if (ui->title)
		ui_puts(ui, i++, ui_center(ui->title));
	for (; i < rows; i++) {
		if (*text && j < lines) {
			if (ud->selectview.cursor == j)
				y = i;
			strncpy(line, *text, UI_COLS);
			line[UI_COLS] = '\0';
			ui_puts(ui, i, line);
			text++;
			j++;
		} else
			break;
//			ui_puts(ui, i, "~");
	}
	if (ui->bottom_line)
		bottom_puts(ui, ui->bottom_line);
	ui->cursor_on = 1;
	ui->cursor_x = 0;
	ui->cursor_y = y;
	ui_flush(ui);

	return 0;
}

/*
 * stringview
 */

static int init_stringview(struct ui_view *uv, union ui_view_data *ud)
{
	ud->stringview.options = 0;
	ud->stringview.pos = 0;

	return 0;
}

static int keypad_stringview(struct ui_inst *ui, struct ui_view *uv,
	union ui_view_data *ud, enum ui_key kp);

static int keypad_stringview_options(struct ui_inst *ui, struct ui_view *uv,
	union ui_view_data *ud, enum ui_key kp)
{
	switch (kp) {
	case UI_KEY_F1: /* back */
		ud->stringview.options = 0;
		break;
	case UI_KEY_1:
		ud->stringview.options = 0;
		return keypad_stringview(ui, uv, ud, 'a');
	case UI_KEY_2:
		ud->stringview.options = 0;
		return keypad_stringview(ui, uv, ud, 'b');
	case UI_KEY_3:
		ud->stringview.options = 0;
		return keypad_stringview(ui, uv, ud, 'c');
	case UI_KEY_0:
		ud->stringview.options = 0;
		return keypad_stringview(ui, uv, ud, '+');
	default:
		return 0;
	}
	/* refresh display */
	uv->display(ui, ud);

	return 0;
}

static int keypad_stringview(struct ui_inst *ui, struct ui_view *uv,
	union ui_view_data *ud, enum ui_key kp)
{
	if (ud->stringview.options)
		return keypad_stringview_options(ui, uv, ud, kp);

	switch (kp) {
	case UI_KEY_STAR:
	case UI_KEY_HASH:
	case UI_KEY_1:
	case UI_KEY_2:
	case UI_KEY_3:
	case UI_KEY_4:
	case UI_KEY_5:
	case UI_KEY_6:
	case UI_KEY_7:
	case UI_KEY_8:
	case UI_KEY_9:
	case UI_KEY_0:
	case 'a':
	case 'b':
	case 'c':
	case '+':
		/* check if number is full */
		if (strlen(ud->stringview.number) + 1 == ud->stringview.num_len)
			return -1;
		/* add digit */
		if (ud->stringview.number[ud->stringview.pos] == '\0') {
			/* add to the end */
			ud->stringview.number[ud->stringview.pos] = kp;
			ud->stringview.pos++;
			ud->stringview.number[ud->stringview.pos] = '\0';
		} else {
			/* insert digit */
			memcpy(ud->stringview.number + ud->stringview.pos + 1,
				ud->stringview.number + ud->stringview.pos,
				strlen(ud->stringview.number +
							ud->stringview.pos)
					+ 1);
			ud->stringview.number[ud->stringview.pos] = kp;
			ud->stringview.pos++;
		}
		break;
	case UI_KEY_LEFT:
		if (ud->stringview.pos == 0)
			return -1;
		ud->stringview.pos--;
		break;
	case UI_KEY_RIGHT:
		if (ud->stringview.pos == strlen(ud->stringview.number))
			return -1;
		ud->stringview.pos++;
		break;
	case UI_KEY_UP: /* select options */
		ud->stringview.options = 1;
		ud->stringview.options_pos = 0;
		break;
	case UI_KEY_F1: /* clear */
		ud->stringview.pos = 0;
		ud->stringview.number[0] = '\0';
		break;
	case UI_KEY_F2: /* delete */
		if (ud->stringview.pos == 0)
			return -1;
		/* del digit */
		if (ud->stringview.number[ud->stringview.pos] == '\0') {
			/* del digit from the end */
			ud->stringview.pos--;
			ud->stringview.number[ud->stringview.pos] = '\0';
		} else {
			/* remove digit */
			memcpy(ud->stringview.number + ud->stringview.pos - 1,
				ud->stringview.number + ud->stringview.pos,
				strlen(ud->stringview.number +
							ud->stringview.pos)
					+ 1);
			ud->stringview.pos--;
		}
		break;
	default:
		return 0;
	}
	/* refresh display */
	uv->display(ui, ud);

	return 0;
}

static int display_stringview(struct ui_inst *ui, union ui_view_data *ud)
{
	char line[UI_COLS + 1];
	char *p = ud->stringview.number;
	int len = strlen(p);
	int i = 1, y;

	/* options screen */
	if (ud->stringview.options) {
		ui_clearhome(ui);
		ui_puts(ui, 0, ui_center("Extra Keys"));
		ui_puts(ui, 2, "1:a 2:b 3:c");
		ui_puts(ui, 3, "4:  5:  6: ");
		ui_puts(ui, 4, "7:  8:  9: ");
		ui_puts(ui, 5, "*:  0:+ #: ");
		bottom_puts(ui, "back ");
		ui_flush(ui);
		return 0;
	}

	/* if number shrunk */
	if (ud->stringview.pos > len)
		ud->stringview.pos = len;

	ui_clearhome(ui);
	/* title */
	if (ui->title) {
		ui_puts(ui, i++, ui_center(ui->title));
		i++;
	}
	y = i;
	/* if line exceeds display width */
	while (len > UI_COLS) {
		memcpy(line, p, UI_COLS);
		line[UI_COLS] = '\0';
		ui_puts(ui, i++, line);
		p += UI_COLS;
		len -= UI_COLS;
	}
	/* last line */
	if (len)
		ui_puts(ui, i, p);
	/* cursor */
	ui->cursor_on = 1;
	ui->cursor_x = ud->stringview.pos % UI_COLS;
	ui->cursor_y = y + (ud->stringview.pos / UI_COLS);
	/* F-keys info */
	bottom_puts(ui, "clear del");
	ui_flush(ui);

	return 0;
}

/*
 * integer view
 */

static int init_intview(struct ui_view *uv, union ui_view_data *ud)
{
	ud->intview.min = -128;
	ud->intview.max = 127;
	ud->intview.value = 0;

	return 0;
}

static int keypad_intview(struct ui_inst *ui, struct ui_view *uv,
	union ui_view_data *ud, enum ui_key kp)
{
	int value;

	switch (kp) {
	case UI_KEY_1:
	case UI_KEY_2:
	case UI_KEY_3:
	case UI_KEY_4:
	case UI_KEY_5:
	case UI_KEY_6:
	case UI_KEY_7:
	case UI_KEY_8:
	case UI_KEY_9:
	case UI_KEY_0:
		value = ud->intview.value;
		value = value * 10 + kp - UI_KEY_0;
		/* if additional digit would cause overflow (or no change) */
		if (value <= ud->intview.value)
			return - 1;
		if (value > 0x7fffffff)
			return - 1;
		ud->intview.value = value;
		break;
	case UI_KEY_STAR:
		ud->intview.sign = 1 - ud->intview.sign;
		break;
	case UI_KEY_UP:
	case UI_KEY_DOWN:
		if (ud->intview.sign)
			value = 0 - ud->intview.value;
		else
			value = ud->intview.value;
		/* check if limit is already reached */
		if (kp == UI_KEY_UP && value == ud->intview.max)
			return -1;
		if (kp == UI_KEY_DOWN && value == ud->intview.min)
			return -1;
		/* if value out of range, put it in range */
		if (value > ud->intview.max) {
			value = ud->intview.max;
			goto store_value;
		}
		if (value < ud->intview.min) {
			value = ud->intview.min;
			goto store_value;
		}
		if (kp == UI_KEY_UP)
			value++;
		else
			value--;
		goto store_value;
	case UI_KEY_LEFT: /* delete */
		/* already 0 */
		if (ud->intview.value == 0)
			return -1;
		ud->intview.value /= 10;
		break;
	default:
		/* if other key is pressed, make value fit in range */
		if (ud->intview.sign)
			value = 0 - ud->intview.value;
		else
			value = ud->intview.value;
		if (value < ud->intview.min) {
			value = ud->intview.min;
			goto store_value;
		}
		if (value > ud->intview.max) {
			value = ud->intview.max;
			goto store_value;
		}
		return 0;
	}
	/* refresh display */
	uv->display(ui, ud);

	return 0;

store_value:
	/* store new value */
	if (value < 0) {
		ud->intview.value = 0 - value;
		ud->intview.sign = 1;
	} else {
		ud->intview.value = value;
		ud->intview.sign = 0;
	}
	/* refresh display */
	uv->display(ui, ud);

	return 0;
}

static int display_intview(struct ui_inst *ui, union ui_view_data *ud)
{
	char line[UI_COLS + 2];
	int i = 1, y, x = 1;
	int value;

	ui_clearhome(ui);
	/* title */
	if (ui->title) {
		ui_puts(ui, i++, ui_center(ui->title));
		i++;
	}
	y = i;
	/* value */
	if (ud->intview.sign)
		line[0] = '-';
	else
		line[0] = ' ';
	sprintf(line + 1, "%d", ud->intview.value);
	ui_puts(ui, i++, line);
	/* range */
	i++;
	snprintf(line, UI_COLS + 1, "(%d..%d)", ud->intview.min,
		ud->intview.max);
	line[UI_COLS] = '\0';
	ui_puts(ui, i++, line);
	/* cursor */
	value = ud->intview.value;
	while (value) {
		x++;
		value /= 10;
	}
	ui->cursor_on = 1;
	ui->cursor_x = x;
	ui->cursor_y = y;
	/* F-keys info */
	if (ui->bottom_line)
		bottom_puts(ui, ui->bottom_line);
	ui_flush(ui);

	return 0;
}

/*
 * structure of all views
 */

struct ui_view ui_listview = {
	.init = init_listview,
	.keypad = keypad_listview,
	.display = display_listview,
};

struct ui_view ui_selectview = {
	.init = init_selectview,
	.keypad = keypad_selectview,
	.display = display_selectview,
};

struct ui_view ui_stringview = {
	.init = init_stringview,
	.keypad = keypad_stringview,
	.display = display_stringview,
};

struct ui_view ui_intview = {
	.init = init_intview,
	.keypad = keypad_intview,
	.display = display_intview,
};

/*
 * instance handling
 */

int ui_inst_init(struct ui_inst *ui, struct ui_view *uv,
	int (*key_cb)(struct ui_inst *ui, enum ui_key kp),
	int (*beep_cb)(struct ui_inst *ui),
	int (*telnet_cb)(struct ui_inst *ui))
{
	ui->uv = uv;
	ui->key_cb = key_cb;
	ui->beep_cb = beep_cb;
	ui->telnet_cb = telnet_cb;

	ui_clearhome(ui);

	/* initialize view */
	uv->init(uv, &ui->ud);

	return 0;
}

int ui_inst_refresh(struct ui_inst *ui)
{
	/* refresh display */
	return ui->uv->display(ui, &ui->ud);
}

/* process keypress at user interface */
int ui_inst_keypad(struct ui_inst *ui, enum ui_key kp)
{
	int rc;

	/* first check if key is handled by callback */
	rc = ui->key_cb(ui, kp);
	if (rc)
		return rc; /* must exit, since key_cb() may reconfigure UI */

	rc = ui->uv->keypad(ui, ui->uv, &ui->ud, kp);
	if (rc < 0)
		ui->beep_cb(ui);

	return rc;
}