Translation system changes part 2: replace macros by strings
[mplayer/glamo.git] / libmenu / menu_console.c
blob85aa7a923443c24e518e28f99c9b304708966dca
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
20 #include "mp_msg.h"
21 #include "help_mp.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #ifndef __MINGW32__
30 #include <sys/wait.h>
31 #endif
32 #include <unistd.h>
33 #include <errno.h>
35 #include "libmpcodecs/img_format.h"
36 #include "libmpcodecs/mp_image.h"
38 #include "m_struct.h"
39 #include "m_option.h"
40 #include "menu.h"
42 #include "libvo/font_load.h"
43 #include "osdep/keycodes.h"
44 #include "input/input.h"
45 #include "osdep/timer.h"
47 typedef struct history_st history_t;
49 struct history_st {
50 char* buffer;
51 int size;
52 history_t* next;
53 history_t* prev;
56 struct menu_priv_s {
57 char** lines; // Our buffer
58 int last_line;
59 int num_lines;
60 int add_line;
61 unsigned int hide_ts;
62 unsigned int show_ts;
63 pid_t child; // Child process if we are running a shell cmd
64 int child_fd[3]; // The 3 default fd
65 char* prompt;
66 //int max_lines; // Max number of lines with the last mpi
67 history_t* history;
68 history_t* cur_history;
69 int history_size;
71 char* mp_prompt;
72 char* child_prompt;
73 int buf_lines; // Buffer size (in line)
74 int height; // Display size in %
75 int minb;
76 int vspace;
77 int bg,bg_alpha;
78 unsigned int hide_time;
79 unsigned int show_time;
80 int history_max;
81 int raw_child;
84 static struct menu_priv_s cfg_dflt = {
85 NULL,
92 { 0 , 0, 0 },
93 NULL,
94 NULL,
95 NULL,
98 "# ",
99 "$ ",
100 50, // lines
101 33, // %
104 0x80,0x40,
105 500,
106 500,
111 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
113 static m_option_t cfg_fields[] = {
114 { "prompt", ST_OFF(mp_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL },
115 { "child-prompt", ST_OFF(child_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL },
116 { "buffer-lines", ST_OFF(buf_lines), CONF_TYPE_INT, M_OPT_MIN, 5, 0, NULL },
117 { "height", ST_OFF(height), CONF_TYPE_INT, M_OPT_RANGE, 1, 100, NULL },
118 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
119 { "vspace", ST_OFF(vspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
120 { "bg", ST_OFF(bg), CONF_TYPE_INT, M_OPT_RANGE, -1, 255, NULL },
121 { "bg-alpha", ST_OFF(bg_alpha), CONF_TYPE_INT, M_OPT_RANGE, 0, 255, NULL },
122 { "show-time",ST_OFF(show_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
123 { "hide-time",ST_OFF(hide_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
124 { "history-size",ST_OFF(history_max), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
125 { "raw-child", ST_OFF(raw_child), CONF_TYPE_FLAG, 0, 0, 1, NULL },
126 { NULL, NULL, NULL, 0,0,0,NULL }
129 #define mpriv (menu->priv)
131 static void check_child(menu_t* menu);
133 static void add_line(struct menu_priv_s* priv, char* l) {
134 char* eol = strchr(l,'\n');
136 if(eol) {
137 if(eol != l) {
138 eol[0] = '\0';
139 add_line(priv,l);
141 if(eol[1]) add_line(priv,eol+1);
142 return;
145 if(priv->num_lines >= priv->buf_lines && priv->lines[priv->last_line])
146 free(priv->lines[priv->last_line]);
147 else
148 priv->num_lines++;
150 priv->lines[priv->last_line] = strdup(l);
151 priv->last_line = (priv->last_line + 1) % priv->buf_lines;
152 priv->add_line = 1;
155 static void add_string(struct menu_priv_s* priv, char* l) {
156 char* eol = strchr(l,'\n');
157 int ll = priv->last_line > 0 ? priv->last_line - 1 : priv->buf_lines-1;
159 if(priv->num_lines <= 0 || priv->add_line || eol == l) {
160 add_line(priv,l);
161 priv->add_line = 0;
162 return;
165 if(eol) {
166 eol[0] = '\0';
167 add_string(priv,l);
168 if(eol[1]) {
169 add_line(priv,eol+1);
170 priv->add_line = 0;
171 } else
172 priv->add_line = 1;
173 return;
175 priv->lines[ll] = realloc(priv->lines[ll],strlen(priv->lines[ll]) + strlen(l) + 1);
176 if ( priv->lines[ll] != NULL )
178 strcat(priv->lines[ll],l);
182 static void draw(menu_t* menu, mp_image_t* mpi) {
183 int h = mpi->h*mpriv->height/100;
184 int w = mpi->w - 2* mpriv->minb;
185 int x = mpriv->minb, y;
186 int lw,lh,i, ll;
188 if(mpriv->child) check_child(menu);
190 ll = mpriv->last_line - 1;
192 if(mpriv->hide_ts) {
193 unsigned int t = GetTimerMS() - mpriv->hide_ts;
194 if(t >= mpriv->hide_time) {
195 mpriv->hide_ts = 0;
196 menu->show = 0;
197 return;
199 h = mpi->h*(mpriv->height - (mpriv->height * t /mpriv->hide_time))/100;
200 } else if(mpriv->show_time && mpriv->show_ts == 0) {
201 mpriv->show_ts = GetTimerMS();
202 return;
203 } else if(mpriv->show_ts > 0) {
204 unsigned int t = GetTimerMS() - mpriv->show_ts;
205 if(t > mpriv->show_time)
206 mpriv->show_ts = -1;
207 else
208 h = mpi->h*(mpriv->height * t /mpriv->hide_time)/100;
211 y = h - mpriv->vspace;
213 if(x < 0 || y < 0 || w <= 0 || h <= 0 )
214 return;
216 if(mpriv->bg >= 0)
217 menu_draw_box(mpi,mpriv->bg,mpriv->bg_alpha,0,0,mpi->w,h);
219 if(!mpriv->child || !mpriv->raw_child){
220 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
221 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
222 menu_text_size(input,w,mpriv->vspace,1,&lw,&lh);
223 menu_draw_text_full(mpi,input,x,y,w,h,mpriv->vspace,1,
224 MENU_TEXT_BOT|MENU_TEXT_LEFT,
225 MENU_TEXT_BOT|MENU_TEXT_LEFT);
226 y -= lh + mpriv->vspace;
230 for( i = 0 ; y > mpriv->minb && i < mpriv->num_lines ; i++){
231 int c = (ll - i) >= 0 ? ll - i : mpriv->buf_lines + ll - i;
232 menu_text_size(mpriv->lines[c],w,mpriv->vspace,1,&lw,&lh);
233 menu_draw_text_full(mpi,mpriv->lines[c],x,y,w,h,mpriv->vspace,1,
234 MENU_TEXT_BOT|MENU_TEXT_LEFT,
235 MENU_TEXT_BOT|MENU_TEXT_LEFT);
236 y -= lh + mpriv->vspace;
238 return;
241 static void check_child(menu_t* menu) {
242 #ifndef __MINGW32__
243 fd_set rfd;
244 struct timeval tv;
245 int max_fd = mpriv->child_fd[2] > mpriv->child_fd[1] ? mpriv->child_fd[2] :
246 mpriv->child_fd[1];
247 int i,r,child_status,w;
248 char buffer[256];
250 if(!mpriv->child) return;
252 memset(&tv,0,sizeof(struct timeval));
253 FD_ZERO(&rfd);
254 FD_SET(mpriv->child_fd[1],&rfd);
255 FD_SET(mpriv->child_fd[2],&rfd);
257 r = select(max_fd+1,&rfd, NULL, NULL, &tv);
258 if(r == 0) {
259 r = waitpid(mpriv->child,&child_status,WNOHANG);
260 if(r < 0){
261 if(errno==ECHILD){ ///exiting children get handled in mplayer.c
262 for(i = 0 ; i < 3 ; i++)
263 close(mpriv->child_fd[i]);
264 mpriv->child = 0;
265 mpriv->prompt = mpriv->mp_prompt;
266 //add_line(mpriv,"Child process exited");
268 else mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] Waitpid error: %s.\n",strerror(errno));
270 } else if(r < 0) {
271 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] Select error.\n");
272 return;
275 w = 0;
276 for(i = 1 ; i < 3 ; i++) {
277 if(FD_ISSET(mpriv->child_fd[i],&rfd)){
278 if(w) mpriv->add_line = 1;
279 r = read(mpriv->child_fd[i],buffer,255);
280 if(r < 0)
281 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] Read error on child's file descriptor: %s.\n", i == 1 ? "stdout":"stderr");
282 else if(r>0) {
283 buffer[r] = '\0';
284 add_string(mpriv,buffer);
286 w = 1;
289 #endif
293 #define close_pipe(pipe) close(pipe[0]); close(pipe[1])
295 static int run_shell_cmd(menu_t* menu, char* cmd) {
296 #ifndef __MINGW32__
297 int in[2],out[2],err[2];
299 mp_tmsg(MSGT_GLOBAL,MSGL_INFO,"[MENU] Console run: %s ...\n",cmd);
300 if(mpriv->child) {
301 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] A child is already running.\n");
302 return 0;
305 pipe(in);
306 pipe(out);
307 pipe(err);
309 mpriv->child = fork();
310 if(mpriv->child < 0) {
311 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] Fork failed !!!\n");
312 close_pipe(in);
313 close_pipe(out);
314 close_pipe(err);
315 return 0;
317 if(!mpriv->child) { // Chlid process
318 int err_fd = dup(2);
319 FILE* errf = fdopen(err_fd,"w");
320 // Bind the std fd to our pipes
321 dup2(in[0],0);
322 dup2(out[1],1);
323 dup2(err[1],2);
324 execl("/bin/sh","sh","-c",cmd,(void*)NULL);
325 fprintf(errf,"exec failed : %s\n",strerror(errno));
326 exit(1);
328 // MPlayer
329 mpriv->child_fd[0] = in[1];
330 mpriv->child_fd[1] = out[0];
331 mpriv->child_fd[2] = err[0];
332 mpriv->prompt = mpriv->child_prompt;
333 //add_line(mpriv,"Child process started");
334 #endif
335 return 1;
338 static void enter_cmd(menu_t* menu) {
339 history_t* h;
340 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
342 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
343 add_line(mpriv,input);
345 if(mpriv->history == mpriv->cur_history) {
346 if(mpriv->history_size >= mpriv->history_max) {
347 history_t* i;
348 for(i = mpriv->history ; i->prev ; i = i->prev)
349 /**/;
350 i->next->prev = NULL;
351 free(i->buffer);
352 free(i);
353 } else
354 mpriv->history_size++;
355 h = calloc(1,sizeof(history_t));
356 h->size = 255;
357 h->buffer = calloc(h->size,1);
358 h->prev = mpriv->history;
359 mpriv->history->next = h;
360 mpriv->history = h;
361 } else
362 mpriv->history->buffer[0] = '\0';
364 mpriv->cur_history = mpriv->history;
365 //mpriv->input = mpriv->cur_history->buffer;
368 static void read_cmd(menu_t* menu,int cmd) {
369 switch(cmd) {
370 case MENU_CMD_CANCEL:
371 if(mpriv->hide_time)
372 mpriv->hide_ts = GetTimerMS();
373 else
374 menu->show = 0;
375 mpriv->show_ts = 0;
376 return;
377 case MENU_CMD_OK: {
378 mp_cmd_t* c;
379 if(mpriv->child) {
380 char *str = mpriv->cur_history->buffer;
381 int l = strlen(str);
382 while(l > 0) {
383 int w = write(mpriv->child_fd[0],str,l);
384 if(w < 0) {
385 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] write error\n");
386 break;
388 l -= w;
389 str += w;
391 if(write(mpriv->child_fd[0],"\n",1) < 0)
392 mp_tmsg(MSGT_GLOBAL,MSGL_ERR,"[MENU] write error\n");
393 enter_cmd(menu);
394 return;
396 c = mp_input_parse_cmd(mpriv->cur_history->buffer);
397 enter_cmd(menu);
398 if(!c)
399 add_line(mpriv,"Invalid command try help");
400 else {
401 switch(c->id) {
402 case MP_CMD_CHELP:
403 add_line(mpriv,"MPlayer console 0.01");
404 add_line(mpriv,"TODO: meaningful help message ;)");
405 add_line(mpriv,"Enter any slave command");
406 add_line(mpriv,"exit close this console");
407 break;
408 case MP_CMD_CEXIT:
409 menu->show = 0;
410 menu->cl = 1;
411 break;
412 case MP_CMD_CHIDE:
413 if(mpriv->hide_time)
414 mpriv->hide_ts = GetTimerMS();
415 else
416 menu->show = 0;
417 mpriv->show_ts = 0;
418 break;
419 case MP_CMD_RUN:
420 run_shell_cmd(menu,c->args[0].v.s);
421 break;
422 default: // Send the other commands to mplayer
423 mp_input_queue_cmd(menu->input_ctx, c);
426 return;
428 case MENU_CMD_UP:
429 if(mpriv->cur_history->prev)
430 mpriv->cur_history = mpriv->cur_history->prev;
431 break;
432 case MENU_CMD_DOWN:
433 if(mpriv->cur_history->next)
434 mpriv->cur_history = mpriv->cur_history->next;
435 break;
439 static int read_key(menu_t* menu,int c) {
440 if(mpriv->child && mpriv->raw_child) {
441 write(mpriv->child_fd[0],&c,sizeof(int));
442 return 1;
445 if (c == KEY_DELETE || c == KEY_BS) {
446 unsigned int i = strlen(mpriv->cur_history->buffer);
447 if(i > 0)
448 mpriv->cur_history->buffer[i-1] = '\0';
449 return 1;
451 if (menu_dflt_read_key(menu, c))
452 return 1;
454 if(isascii(c)) {
455 int l = strlen(mpriv->cur_history->buffer);
456 if(l >= mpriv->cur_history->size) {
457 mpriv->cur_history->size += 255;
458 mpriv->cur_history->buffer = realloc(mpriv->cur_history,mpriv->cur_history->size);
460 mpriv->cur_history->buffer[l] = (char)c;
461 mpriv->cur_history->buffer[l+1] = '\0';
462 return 1;
464 return 0;
468 static int openMenu(menu_t* menu, char* args) {
471 menu->draw = draw;
472 menu->read_cmd = read_cmd;
473 menu->read_key = read_key;
475 mpriv->lines = calloc(mpriv->buf_lines,sizeof(char*));
476 mpriv->prompt = mpriv->mp_prompt;
477 mpriv->cur_history = mpriv->history = calloc(1,sizeof(history_t));
478 mpriv->cur_history->buffer = calloc(255,1);
479 mpriv->cur_history->size = 255;
481 if(args)
482 add_line(mpriv,args);
484 return 1;
487 const menu_info_t menu_info_console = {
488 "MPlayer console",
489 "console",
490 "Albeu",
493 "console_cfg",
494 sizeof(struct menu_priv_s),
495 &cfg_dflt,
496 cfg_fields
498 openMenu,