-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyvonne-remote-ui.c
399 lines (361 loc) · 9.21 KB
/
yvonne-remote-ui.c
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
/*****************************************************
* This file is part of yvonne-remote.
* ( ncurses terminal interface )
*
* 2018, Jérôme Blanchi aka d.j.a.y
* http://github.com/d-j-a-y/yvonne-remote
*
* yvonne-remote 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*
*****************************************************/
#include "yvonne-remote.h"
#include "yvonne-remote-ui.h"
char *yrc_menuEntry[] = { "Start - 's'",
"Pause - 'p'",
"Video - 'v'",
"Quit - 'q'",
}; // Help ?
char yrc_menuEntryCode[] = { 's',
'p',
'v',
'q',
};
int yrc_menuEntries = sizeof(yrc_menuEntry) / sizeof(char *);
char *yrc_msgTypePrefix[] = { "E: ",
"W: ",
"I: ",
"", //UI
"", //Video banner
};
static WINDOW *menu_win = NULL;
static WINDOW *msg_win = NULL;
/**
* yrc_uiX
* @param ...
*
* @return
*
* Do x
*/
/**
* yrc_uiSetup
* @param
*
* @return Error Code
*
* Setup the ncurse user interface
*/
int yrc_uiSetup (void)
{
initscr();
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
start_color(); /* Start color*/
init_pair(1, COLOR_BLUE, COLOR_BLACK);
//~ int row, col;
//~ getmaxyx(stdscr,row,col); /* FIXME get the number of rows and columns */
return ERROR_NO;
}
/**
* yrc_uiRestore
* @param ...
*
* @return Error Code
*
* Restore the initial terminal state
*/
int yrc_uiRestore (void)
{
clrtoeol();
refresh();
endwin();
return ERROR_NO;
}
/**
* yrc_menuOpen
*
* @return Error Code
*
* Setup the menu window, caller is responsible of cleaning the memory (see yrc_menuClose)
*/
int yrc_menuOpen (void)
{
int startx = (COLS - YRC_MENU_WIDTH) / 2;
int starty = (LINES - YRC_MENU_HEIGHT) / 2;
menu_win = newwin(YRC_MENU_HEIGHT, YRC_MENU_WIDTH, starty, startx);
if(menu_win !=NULL){
wtimeout (menu_win, 300);
keypad(menu_win, TRUE);
return ERROR_NO;
}
return ERROR_GENERIC;
}
/**
* yrc_menuClose
*
* @return Error Code
*
* Clean menu memory
*/
int yrc_menuClose (void)
{
if (menu_win) delwin(menu_win);
return ERROR_NO;
}
/**
* yrc_errorOpen
*
* @return Error Code
*
* Setup the error window, caller is responsible of cleaning the memory (see yrc_errorClose)
*/
int yrc_errorOpen (void)
{
msg_win = newwin(YRC_UI_FOOTER_H, YRC_UI_INDEX_X-1, LINES-YRC_UI_FOOTER_H, 0);
if (msg_win != NULL) {
scrollok(msg_win, TRUE);
return ERROR_NO;
}
return ERROR_GENERIC;
}
/**
* yrc_errorClose
*
* @return Error Code
*
* Clean menu memory
*/
int yrc_errorClose (void)
{
if(msg_win) delwin(msg_win);
return ERROR_NO;
}
/**
* yrc_menuPrint
* @param A one based index for current highlighed menu entry
*
* Build and display the menu with current highlighed menu entry
*/
void yrc_menuPrint (int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < yrc_menuEntries; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", yrc_menuEntry[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", yrc_menuEntry[i]);
++y;
}
wrefresh(menu_win);
}
/**
* yrc_menuCheckEntry
* @param A pointer to a ncurses WINDOW pointer
* @param A pointer to the current highlighted menu entry
*
* @return Menu entry selection has letter or YRC_MENU_ENTRY_NO
*
* Check (no block has set @see yrc_menuOpen) for key pressed.
* Adjust menu selection and if new event occurs, return user choice.
*/
int yrc_menuCheckEntry (WINDOW *menu_win, int *highlight)
{
int c;
int choice = YRC_MENU_ENTRY_NO;
c = wgetch(menu_win);
switch(c) {
case KEY_UP:
if((*highlight) == 1)
(*highlight) = yrc_menuEntries;
else
--(*highlight);
break;
case KEY_DOWN:
if((*highlight) == yrc_menuEntries)
(*highlight) = 1;
else
++(*highlight);
break;
case 10: //RETURN
choice = (int)yrc_menuEntryCode[(*highlight)-1];
break;
case 's':
case 'S':
choice = 's';
break;
case 'p':
case 'P':
choice = 'p';
break;
case 'v':
case 'V':
choice = 'v';
break;
case 'q':
case 'Q':
choice = 'q';
break;
case EOF:
break;
default:
mvprintw(24, 0, "Unexpected character pressed '%c'.", c);
refresh();
break;
}
yrc_menuPrint(*highlight);
return choice;
}
/**
* yrc_stateMachineLocal
* @param pointer to int for bit field
*
* Update the state from local stupid UI. @see yrc_menuCheckEntry
*/
void yrc_stateMachineLocal (volatile sig_atomic_t *yrc_stateField)
{
static int highlight = 1;
static int yy = -1;
static char tictac = '*';
if(yy == -1) yy = YRC_UI_INDEX_X ;
mvaddch(LINES-YRC_UI_FOOTER_H-1,yy++,tictac);
refresh();
if (yy >= COLS-1 ){ yy = YRC_UI_INDEX_X; tictac = ~tictac; }
int menu_choice;
if ((menu_choice = yrc_menuCheckEntry (menu_win, &highlight)) != YRC_MENU_ENTRY_NO) {
switch(menu_choice) {
case 'p':
(*yrc_stateField) &= ~YRC_STATE_PHOTO;
break;
case 's':
(*yrc_stateField) |= YRC_STATE_PHOTO;
break;
case 'v':
(*yrc_stateField) |= YRC_STATE_VIDEO;
break;
case 'q':
(*yrc_stateField) |= YRC_STATE_QUIT;
break;
}
}
}
/**
* yrc_coloredPrintf
* @param msgType Type of message has YvonneMsgType
* @param char* message
* @param ...
*
* Basic message printing
* @warning Do not use with ncurses support aka failback
*/
void yrc_coloredPrintf (YvonneMsgType msgType, char* message, ...)
{
char buf[1024];
va_list args;
// parse arguments
va_start(args, message);
vsnprintf(buf, sizeof(buf) - 1, message, args);
char *errorColor;
switch(msgType) {
case YVONNE_MSG_ERROR:
errorColor = ANSI_COLOR_RED;
break;
case YVONNE_MSG_WARNING:
errorColor = ANSI_COLOR_YELLOW;
break;
case YVONNE_MSG_INFO:
errorColor = ANSI_COLOR_CYAN;
break;
case YVONNE_MSG_UI:
errorColor = ANSI_COLOR_MAGENTA;
break;
case YVONNE_MSG_VIDEO_BANNER:
errorColor = ANSI_COLOR_GREEN;
break;
default:
errorColor = ANSI_COLOR_RESET;
}
fprintf( stdout, "%s%s%s%s\n",yrc_msgTypePrefix[msgType], errorColor, buf, ANSI_COLOR_RESET );
va_end(args);
}
/**
* yrc_uiPrintHelp
*
* Display the help message in the upper left window corner
*/
void yrc_uiPrintHelp(void)
{
mvprintw(0, 0, "Use shortcuts or arrow keys up and down and Enter to select a choice.");
refresh();
}
/**
* yrc_uiPrintLayout
*
* Print the immobile stuf
*/
void yrc_uiPrintLayout(void)
{
mvprintw(LINES-YRC_UI_FOOTER_H, YRC_UI_INDEX_X, "photo : ");
mvprintw(LINES-YRC_UI_FOOTER_H+1, YRC_UI_INDEX_X, "video : ");
refresh();
}
/**
* yrc_uiPrintMediaIndex
*
* Refresh the current photo and video index
*/
void yrc_uiPrintMediaIndex(int currentPhoto, int currentVideo)
{
mvprintw(LINES-YRC_UI_FOOTER_H, YRC_UI_INDEX_X+YRC_UI_INDEX_TEXT_L, "%d", currentPhoto); //strlen current photo
mvprintw(LINES-YRC_UI_FOOTER_H+1, YRC_UI_INDEX_X+YRC_UI_INDEX_TEXT_L, "%d", currentVideo);
refresh();
}
/**
* yrc_uiPrintMessage
* @param msgType Type of message has YvonneMsgType
* @param char* message
* @param ...
*
*
* Type based (error/warning/info...) messages printing.
*/
void yrc_uiPrintMessage(YvonneMsgType msgType, char* errorMessage, ...)
{
va_list args;
int textAttrib;
// parse arguments
va_start(args, errorMessage);
switch(msgType) {
case YVONNE_MSG_ERROR:
textAttrib = A_STANDOUT;
break;
case YVONNE_MSG_VIDEO_BANNER:
textAttrib = COLOR_PAIR(1);
break;
default:
textAttrib = A_NORMAL;
break;
}
wattron(msg_win,textAttrib);
vwprintw(msg_win, errorMessage, args); //strlen current photo
wattroff(msg_win,textAttrib);
wrefresh(msg_win);
va_end(args);
}