"inline" is used elsewhere and more portable than "__inline"
[mplayer/glamo.git] / libmenu / menu_console.c
bloba6788dd682b1e94bc2d46f3eba20f99d235f5cb9
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 read_cmd(menu_t* menu,int cmd) {
225 switch(cmd) {
226 case MENU_CMD_UP:
227 break;
228 case MENU_CMD_DOWN:
229 case MENU_CMD_OK:
230 break;
231 case MENU_CMD_CANCEL:
232 menu->show = 0;
233 menu->cl = 1;
234 break;
238 static void check_child(menu_t* menu) {
239 #ifndef __MINGW32__
240 fd_set rfd;
241 struct timeval tv;
242 int max_fd = mpriv->child_fd[2] > mpriv->child_fd[1] ? mpriv->child_fd[2] :
243 mpriv->child_fd[1];
244 int i,r,child_status,w;
245 char buffer[256];
247 if(!mpriv->child) return;
249 memset(&tv,0,sizeof(struct timeval));
250 FD_ZERO(&rfd);
251 FD_SET(mpriv->child_fd[1],&rfd);
252 FD_SET(mpriv->child_fd[2],&rfd);
254 r = select(max_fd+1,&rfd, NULL, NULL, &tv);
255 if(r == 0) {
256 r = waitpid(mpriv->child,&child_status,WNOHANG);
257 if(r < 0){
258 if(errno==ECHILD){ ///exiting childs get handled in mplayer.c
259 for(i = 0 ; i < 3 ; i++)
260 close(mpriv->child_fd[i]);
261 mpriv->child = 0;
262 mpriv->prompt = mpriv->mp_prompt;
263 //add_line(mpriv,"Child process exited");
265 else mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_WaitPidError,strerror(errno));
267 } else if(r < 0) {
268 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_SelectError);
269 return;
272 w = 0;
273 for(i = 1 ; i < 3 ; i++) {
274 if(FD_ISSET(mpriv->child_fd[i],&rfd)){
275 if(w) mpriv->add_line = 1;
276 r = read(mpriv->child_fd[i],buffer,255);
277 if(r < 0)
278 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReadErrorOnChilds, i == 1 ? "stdout":"stderr");
279 else if(r>0) {
280 buffer[r] = '\0';
281 add_string(mpriv,buffer);
283 w = 1;
286 #endif
290 #define close_pipe(pipe) close(pipe[0]); close(pipe[1])
292 static int run_shell_cmd(menu_t* menu, char* cmd) {
293 #ifndef __MINGW32__
294 int in[2],out[2],err[2];
296 mp_msg(MSGT_GLOBAL,MSGL_INFO,MSGTR_LIBMENU_ConsoleRun,cmd);
297 if(mpriv->child) {
298 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_AChildIsAlreadyRunning);
299 return 0;
302 pipe(in);
303 pipe(out);
304 pipe(err);
306 mpriv->child = fork();
307 if(mpriv->child < 0) {
308 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ForkFailed);
309 close_pipe(in);
310 close_pipe(out);
311 close_pipe(err);
312 return 0;
314 if(!mpriv->child) { // Chlid process
315 int err_fd = dup(2);
316 FILE* errf = fdopen(err_fd,"w");
317 // Bind the std fd to our pipes
318 dup2(in[0],0);
319 dup2(out[1],1);
320 dup2(err[1],2);
321 execl("/bin/sh","sh","-c",cmd,(void*)NULL);
322 fprintf(errf,"exec failed : %s\n",strerror(errno));
323 exit(1);
325 // MPlayer
326 mpriv->child_fd[0] = in[1];
327 mpriv->child_fd[1] = out[0];
328 mpriv->child_fd[2] = err[0];
329 mpriv->prompt = mpriv->child_prompt;
330 //add_line(mpriv,"Child process started");
331 #endif
332 return 1;
335 static void enter_cmd(menu_t* menu) {
336 history_t* h;
337 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
339 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
340 add_line(mpriv,input);
342 if(mpriv->history == mpriv->cur_history) {
343 if(mpriv->history_size >= mpriv->history_max) {
344 history_t* i;
345 for(i = mpriv->history ; i->prev ; i = i->prev)
346 /**/;
347 i->next->prev = NULL;
348 free(i->buffer);
349 free(i);
350 } else
351 mpriv->history_size++;
352 h = calloc(1,sizeof(history_t));
353 h->size = 255;
354 h->buffer = calloc(h->size,1);
355 h->prev = mpriv->history;
356 mpriv->history->next = h;
357 mpriv->history = h;
358 } else
359 mpriv->history->buffer[0] = '\0';
361 mpriv->cur_history = mpriv->history;
362 //mpriv->input = mpriv->cur_history->buffer;
365 static void read_key(menu_t* menu,int c) {
366 if(!mpriv->child || !mpriv->raw_child) switch(c) {
367 case KEY_ESC:
368 if(mpriv->hide_time)
369 mpriv->hide_ts = GetTimerMS();
370 else
371 menu->show = 0;
372 mpriv->show_ts = 0;
373 return;
374 case KEY_ENTER: {
375 mp_cmd_t* c;
376 if(mpriv->child) {
377 char *str = mpriv->cur_history->buffer;
378 int l = strlen(str);
379 while(l > 0) {
380 int w = write(mpriv->child_fd[0],str,l);
381 if(w < 0) {
382 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_WriteError);
383 break;
385 l -= w;
386 str += w;
388 if(write(mpriv->child_fd[0],"\n",1) < 0)
389 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_WriteError);
390 enter_cmd(menu);
391 return;
393 c = mp_input_parse_cmd(mpriv->cur_history->buffer);
394 enter_cmd(menu);
395 if(!c)
396 add_line(mpriv,"Invalid command try help");
397 else {
398 switch(c->id) {
399 case MP_CMD_CHELP:
400 add_line(mpriv,"MPlayer console 0.01");
401 add_line(mpriv,"TODO: meaningful help message ;)");
402 add_line(mpriv,"Enter any slave command");
403 add_line(mpriv,"exit close this console");
404 break;
405 case MP_CMD_CEXIT:
406 menu->show = 0;
407 menu->cl = 1;
408 break;
409 case MP_CMD_CHIDE:
410 if(mpriv->hide_time)
411 mpriv->hide_ts = GetTimerMS();
412 else
413 menu->show = 0;
414 mpriv->show_ts = 0;
415 break;
416 case MP_CMD_RUN:
417 run_shell_cmd(menu,c->args[0].v.s);
418 break;
419 default: // Send the other commands to mplayer
420 mp_input_queue_cmd(c);
423 return;
425 case KEY_DELETE:
426 case KEY_BS: {
427 unsigned int i = strlen(mpriv->cur_history->buffer);
428 if(i > 0)
429 mpriv->cur_history->buffer[i-1] = '\0';
430 return;
432 case KEY_UP:
433 if(mpriv->cur_history->prev)
434 mpriv->cur_history = mpriv->cur_history->prev;
435 break;
436 case KEY_DOWN:
437 if(mpriv->cur_history->next)
438 mpriv->cur_history = mpriv->cur_history->next;
439 break;
442 if(mpriv->child && mpriv->raw_child) {
443 write(mpriv->child_fd[0],&c,sizeof(int));
444 return;
447 if(isascii(c)) {
448 int l = strlen(mpriv->cur_history->buffer);
449 if(l >= mpriv->cur_history->size) {
450 mpriv->cur_history->size += 255;
451 mpriv->cur_history->buffer = realloc(mpriv->cur_history,mpriv->cur_history->size);
453 mpriv->cur_history->buffer[l] = (char)c;
454 mpriv->cur_history->buffer[l+1] = '\0';
460 static int openMenu(menu_t* menu, char* args) {
463 menu->draw = draw;
464 menu->read_cmd = read_cmd;
465 menu->read_key = read_key;
467 mpriv->lines = calloc(mpriv->buf_lines,sizeof(char*));
468 mpriv->prompt = mpriv->mp_prompt;
469 mpriv->cur_history = mpriv->history = calloc(1,sizeof(history_t));
470 mpriv->cur_history->buffer = calloc(255,1);
471 mpriv->cur_history->size = 255;
473 if(args)
474 add_line(mpriv,args);
476 return 1;
479 const menu_info_t menu_info_console = {
480 "MPlayer console",
481 "console",
482 "Albeu",
485 "console_cfg",
486 sizeof(struct menu_priv_s),
487 &cfg_dflt,
488 cfg_fields
490 openMenu,