From now on, libmenu does not steal all input keys from input modules.
[mplayer/greg.git] / libmenu / menu_console.c
blobabc941cdca48e3252ff4f232c9d4af66014d056c
2 #include "config.h"
3 #include "mp_msg.h"
4 #include "help_mp.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <ctype.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #ifndef __MINGW32__
13 #include <sys/wait.h>
14 #endif
15 #include <unistd.h>
16 #include <errno.h>
18 #include "libmpcodecs/img_format.h"
19 #include "libmpcodecs/mp_image.h"
21 #include "m_struct.h"
22 #include "m_option.h"
23 #include "menu.h"
25 #include "libvo/font_load.h"
26 #include "osdep/keycodes.h"
27 #include "input/input.h"
28 #include "osdep/timer.h"
30 typedef struct history_st history_t;
32 struct history_st {
33 char* buffer;
34 int size;
35 history_t* next;
36 history_t* prev;
39 struct menu_priv_s {
40 char** lines; // Our buffer
41 int last_line;
42 int num_lines;
43 int add_line;
44 unsigned int hide_ts;
45 unsigned int show_ts;
46 pid_t child; // Child process if we are running a shell cmd
47 int child_fd[3]; // The 3 default fd
48 char* prompt;
49 //int max_lines; // Max number of lines with the last mpi
50 history_t* history;
51 history_t* cur_history;
52 int history_size;
54 char* mp_prompt;
55 char* child_prompt;
56 int buf_lines; // Buffer size (in line)
57 int height; // Display size in %
58 int minb;
59 int vspace;
60 int bg,bg_alpha;
61 unsigned int hide_time;
62 unsigned int show_time;
63 int history_max;
64 int raw_child;
67 static struct menu_priv_s cfg_dflt = {
68 NULL,
75 { 0 , 0, 0 },
76 NULL,
77 NULL,
78 NULL,
81 "# ",
82 "$ ",
83 50, // lines
84 33, // %
87 0x80,0x40,
88 500,
89 500,
90 10,
94 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
96 static m_option_t cfg_fields[] = {
97 { "prompt", ST_OFF(mp_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL },
98 { "child-prompt", ST_OFF(child_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL },
99 { "buffer-lines", ST_OFF(buf_lines), CONF_TYPE_INT, M_OPT_MIN, 5, 0, NULL },
100 { "height", ST_OFF(height), CONF_TYPE_INT, M_OPT_RANGE, 1, 100, NULL },
101 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
102 { "vspace", ST_OFF(vspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
103 { "bg", ST_OFF(bg), CONF_TYPE_INT, M_OPT_RANGE, -1, 255, NULL },
104 { "bg-alpha", ST_OFF(bg_alpha), CONF_TYPE_INT, M_OPT_RANGE, 0, 255, NULL },
105 { "show-time",ST_OFF(show_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
106 { "hide-time",ST_OFF(hide_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
107 { "history-size",ST_OFF(history_max), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
108 { "raw-child", ST_OFF(raw_child), CONF_TYPE_FLAG, 0, 0, 1, NULL },
109 { NULL, NULL, NULL, 0,0,0,NULL }
112 #define mpriv (menu->priv)
114 static void check_child(menu_t* menu);
116 static void add_line(struct menu_priv_s* priv, char* l) {
117 char* eol = strchr(l,'\n');
119 if(eol) {
120 if(eol != l) {
121 eol[0] = '\0';
122 add_line(priv,l);
124 if(eol[1]) add_line(priv,eol+1);
125 return;
128 if(priv->num_lines >= priv->buf_lines && priv->lines[priv->last_line])
129 free(priv->lines[priv->last_line]);
130 else
131 priv->num_lines++;
133 priv->lines[priv->last_line] = strdup(l);
134 priv->last_line = (priv->last_line + 1) % priv->buf_lines;
135 priv->add_line = 1;
138 static void add_string(struct menu_priv_s* priv, char* l) {
139 char* eol = strchr(l,'\n');
140 int ll = priv->last_line > 0 ? priv->last_line - 1 : priv->buf_lines-1;
142 if(priv->num_lines <= 0 || priv->add_line || eol == l) {
143 add_line(priv,l);
144 priv->add_line = 0;
145 return;
148 if(eol) {
149 eol[0] = '\0';
150 add_string(priv,l);
151 if(eol[1]) {
152 add_line(priv,eol+1);
153 priv->add_line = 0;
154 } else
155 priv->add_line = 1;
156 return;
158 priv->lines[ll] = realloc(priv->lines[ll],strlen(priv->lines[ll]) + strlen(l) + 1);
159 if ( priv->lines[ll] != NULL )
161 strcat(priv->lines[ll],l);
165 static void draw(menu_t* menu, mp_image_t* mpi) {
166 int h = mpi->h*mpriv->height/100;
167 int w = mpi->w - 2* mpriv->minb;
168 int x = mpriv->minb, y;
169 int lw,lh,i, ll;
171 if(mpriv->child) check_child(menu);
173 ll = mpriv->last_line - 1;
175 if(mpriv->hide_ts) {
176 unsigned int t = GetTimerMS() - mpriv->hide_ts;
177 if(t >= mpriv->hide_time) {
178 mpriv->hide_ts = 0;
179 menu->show = 0;
180 return;
182 h = mpi->h*(mpriv->height - (mpriv->height * t /mpriv->hide_time))/100;
183 } else if(mpriv->show_time && mpriv->show_ts == 0) {
184 mpriv->show_ts = GetTimerMS();
185 return;
186 } else if(mpriv->show_ts > 0) {
187 unsigned int t = GetTimerMS() - mpriv->show_ts;
188 if(t > mpriv->show_time)
189 mpriv->show_ts = -1;
190 else
191 h = mpi->h*(mpriv->height * t /mpriv->hide_time)/100;
194 y = h - mpriv->vspace;
196 if(x < 0 || y < 0 || w <= 0 || h <= 0 )
197 return;
199 if(mpriv->bg >= 0)
200 menu_draw_box(mpi,mpriv->bg,mpriv->bg_alpha,0,0,mpi->w,h);
202 if(!mpriv->child || !mpriv->raw_child){
203 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
204 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
205 menu_text_size(input,w,mpriv->vspace,1,&lw,&lh);
206 menu_draw_text_full(mpi,input,x,y,w,h,mpriv->vspace,1,
207 MENU_TEXT_BOT|MENU_TEXT_LEFT,
208 MENU_TEXT_BOT|MENU_TEXT_LEFT);
209 y -= lh + mpriv->vspace;
213 for( i = 0 ; y > mpriv->minb && i < mpriv->num_lines ; i++){
214 int c = (ll - i) >= 0 ? ll - i : mpriv->buf_lines + ll - i;
215 menu_text_size(mpriv->lines[c],w,mpriv->vspace,1,&lw,&lh);
216 menu_draw_text_full(mpi,mpriv->lines[c],x,y,w,h,mpriv->vspace,1,
217 MENU_TEXT_BOT|MENU_TEXT_LEFT,
218 MENU_TEXT_BOT|MENU_TEXT_LEFT);
219 y -= lh + mpriv->vspace;
221 return;
224 static void check_child(menu_t* menu) {
225 #ifndef __MINGW32__
226 fd_set rfd;
227 struct timeval tv;
228 int max_fd = mpriv->child_fd[2] > mpriv->child_fd[1] ? mpriv->child_fd[2] :
229 mpriv->child_fd[1];
230 int i,r,child_status,w;
231 char buffer[256];
233 if(!mpriv->child) return;
235 memset(&tv,0,sizeof(struct timeval));
236 FD_ZERO(&rfd);
237 FD_SET(mpriv->child_fd[1],&rfd);
238 FD_SET(mpriv->child_fd[2],&rfd);
240 r = select(max_fd+1,&rfd, NULL, NULL, &tv);
241 if(r == 0) {
242 r = waitpid(mpriv->child,&child_status,WNOHANG);
243 if(r < 0){
244 if(errno==ECHILD){ ///exiting children get handled in mplayer.c
245 for(i = 0 ; i < 3 ; i++)
246 close(mpriv->child_fd[i]);
247 mpriv->child = 0;
248 mpriv->prompt = mpriv->mp_prompt;
249 //add_line(mpriv,"Child process exited");
251 else mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_WaitPidError,strerror(errno));
253 } else if(r < 0) {
254 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_SelectError);
255 return;
258 w = 0;
259 for(i = 1 ; i < 3 ; i++) {
260 if(FD_ISSET(mpriv->child_fd[i],&rfd)){
261 if(w) mpriv->add_line = 1;
262 r = read(mpriv->child_fd[i],buffer,255);
263 if(r < 0)
264 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReadErrorOnChildFD, i == 1 ? "stdout":"stderr");
265 else if(r>0) {
266 buffer[r] = '\0';
267 add_string(mpriv,buffer);
269 w = 1;
272 #endif
276 #define close_pipe(pipe) close(pipe[0]); close(pipe[1])
278 static int run_shell_cmd(menu_t* menu, char* cmd) {
279 #ifndef __MINGW32__
280 int in[2],out[2],err[2];
282 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_LIBMENU_ConsoleRun,cmd);
283 if(mpriv->child) {
284 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_AChildIsAlreadyRunning);
285 return 0;
288 pipe(in);
289 pipe(out);
290 pipe(err);
292 mpriv->child = fork();
293 if(mpriv->child < 0) {
294 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ForkFailed);
295 close_pipe(in);
296 close_pipe(out);
297 close_pipe(err);
298 return 0;
300 if(!mpriv->child) { // Chlid process
301 int err_fd = dup(2);
302 FILE* errf = fdopen(err_fd,"w");
303 // Bind the std fd to our pipes
304 dup2(in[0],0);
305 dup2(out[1],1);
306 dup2(err[1],2);
307 execl("/bin/sh","sh","-c",cmd,(void*)NULL);
308 fprintf(errf,"exec failed : %s\n",strerror(errno));
309 exit(1);
311 // MPlayer
312 mpriv->child_fd[0] = in[1];
313 mpriv->child_fd[1] = out[0];
314 mpriv->child_fd[2] = err[0];
315 mpriv->prompt = mpriv->child_prompt;
316 //add_line(mpriv,"Child process started");
317 #endif
318 return 1;
321 static void enter_cmd(menu_t* menu) {
322 history_t* h;
323 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
325 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
326 add_line(mpriv,input);
328 if(mpriv->history == mpriv->cur_history) {
329 if(mpriv->history_size >= mpriv->history_max) {
330 history_t* i;
331 for(i = mpriv->history ; i->prev ; i = i->prev)
332 /**/;
333 i->next->prev = NULL;
334 free(i->buffer);
335 free(i);
336 } else
337 mpriv->history_size++;
338 h = calloc(1,sizeof(history_t));
339 h->size = 255;
340 h->buffer = calloc(h->size,1);
341 h->prev = mpriv->history;
342 mpriv->history->next = h;
343 mpriv->history = h;
344 } else
345 mpriv->history->buffer[0] = '\0';
347 mpriv->cur_history = mpriv->history;
348 //mpriv->input = mpriv->cur_history->buffer;
351 static void read_cmd(menu_t* menu,int cmd) {
352 switch(cmd) {
353 case MENU_CMD_CANCEL:
354 if(mpriv->hide_time)
355 mpriv->hide_ts = GetTimerMS();
356 else
357 menu->show = 0;
358 mpriv->show_ts = 0;
359 return;
360 case MENU_CMD_OK: {
361 mp_cmd_t* c;
362 if(mpriv->child) {
363 char *str = mpriv->cur_history->buffer;
364 int l = strlen(str);
365 while(l > 0) {
366 int w = write(mpriv->child_fd[0],str,l);
367 if(w < 0) {
368 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_WriteError);
369 break;
371 l -= w;
372 str += w;
374 if(write(mpriv->child_fd[0],"\n",1) < 0)
375 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_WriteError);
376 enter_cmd(menu);
377 return;
379 c = mp_input_parse_cmd(mpriv->cur_history->buffer);
380 enter_cmd(menu);
381 if(!c)
382 add_line(mpriv,"Invalid command try help");
383 else {
384 switch(c->id) {
385 case MP_CMD_CHELP:
386 add_line(mpriv,"MPlayer console 0.01");
387 add_line(mpriv,"TODO: meaningful help message ;)");
388 add_line(mpriv,"Enter any slave command");
389 add_line(mpriv,"exit close this console");
390 break;
391 case MP_CMD_CEXIT:
392 menu->show = 0;
393 menu->cl = 1;
394 break;
395 case MP_CMD_CHIDE:
396 if(mpriv->hide_time)
397 mpriv->hide_ts = GetTimerMS();
398 else
399 menu->show = 0;
400 mpriv->show_ts = 0;
401 break;
402 case MP_CMD_RUN:
403 run_shell_cmd(menu,c->args[0].v.s);
404 break;
405 default: // Send the other commands to mplayer
406 mp_input_queue_cmd(c);
409 return;
411 case MENU_CMD_UP:
412 if(mpriv->cur_history->prev)
413 mpriv->cur_history = mpriv->cur_history->prev;
414 break;
415 case MENU_CMD_DOWN:
416 if(mpriv->cur_history->next)
417 mpriv->cur_history = mpriv->cur_history->next;
418 break;
422 static int read_key(menu_t* menu,int c) {
423 if(mpriv->child && mpriv->raw_child) {
424 write(mpriv->child_fd[0],&c,sizeof(int));
425 return 1;
428 if (c == KEY_DELETE || c == KEY_BS) {
429 unsigned int i = strlen(mpriv->cur_history->buffer);
430 if(i > 0)
431 mpriv->cur_history->buffer[i-1] = '\0';
432 return 1;
434 if (menu_dflt_read_key(menu, c))
435 return 1;
437 if(isascii(c)) {
438 int l = strlen(mpriv->cur_history->buffer);
439 if(l >= mpriv->cur_history->size) {
440 mpriv->cur_history->size += 255;
441 mpriv->cur_history->buffer = realloc(mpriv->cur_history,mpriv->cur_history->size);
443 mpriv->cur_history->buffer[l] = (char)c;
444 mpriv->cur_history->buffer[l+1] = '\0';
445 return 1;
447 return 0;
451 static int openMenu(menu_t* menu, char* args) {
454 menu->draw = draw;
455 menu->read_cmd = read_cmd;
456 menu->read_key = read_key;
458 mpriv->lines = calloc(mpriv->buf_lines,sizeof(char*));
459 mpriv->prompt = mpriv->mp_prompt;
460 mpriv->cur_history = mpriv->history = calloc(1,sizeof(history_t));
461 mpriv->cur_history->buffer = calloc(255,1);
462 mpriv->cur_history->size = 255;
464 if(args)
465 add_line(mpriv,args);
467 return 1;
470 const menu_info_t menu_info_console = {
471 "MPlayer console",
472 "console",
473 "Albeu",
476 "console_cfg",
477 sizeof(struct menu_priv_s),
478 &cfg_dflt,
479 cfg_fields
481 openMenu,