summaryrefslogtreecommitdiffstats
path: root/libmenu/menu_console.c
blob: 01b0a0450d781cf20997678a96d4528faf27cd27 (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

#include "../config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

#include "img_format.h"
#include "mp_image.h"

#include "../m_struct.h"
#include "../m_option.h"
#include "menu.h"

#include "../libvo/font_load.h"
#include "../linux/keycodes.h"
#include "../input/input.h"
#include "../linux/timer.h"

typedef struct history_st history_t;

struct history_st {
  char* buffer;
  int size;
  history_t* next;
  history_t* prev;
};

struct menu_priv_s {
  char** lines; // Our buffer
  int last_line;
  int num_lines;
  int add_line;
  unsigned int hide_ts;
  unsigned int show_ts;
  pid_t child; // Child process if we are running a shell cmd
  int child_fd[3]; // The 3 default fd
  char* prompt;
  //int max_lines; // Max number of lines with the last mpi
  history_t* history;
  history_t* cur_history;
  int history_size;
  
  char* mp_prompt;
  char* child_prompt;
  int buf_lines; // Buffer size (in line)
  int height; // Display size in %
  int minb;
  int vspace;
  unsigned int hide_time;
  unsigned int show_time;
  int history_max;
  int raw_child;
};

static struct menu_priv_s cfg_dflt = {
  NULL,
  0,
  0,
  1,
  0,
  0,
  0,
  { 0 , 0, 0 },
  NULL,
  NULL,
  NULL,
  0,

  "# ",
  "$ ",
  50, // lines
  33, // %
  3,
  3,
  500,
  500,
  10,
  0
};

#define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)

static m_option_t cfg_fields[] = {
  { "prompt", ST_OFF(mp_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL },
  { "child-prompt", ST_OFF(child_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL },
  { "buffer-lines", ST_OFF(buf_lines), CONF_TYPE_INT, M_OPT_MIN, 5, 0, NULL },
  { "height", ST_OFF(height), CONF_TYPE_INT, M_OPT_RANGE, 1, 100, NULL },
  { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
  { "vspace", ST_OFF(vspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
  { "show-time",ST_OFF(show_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
  { "hide-time",ST_OFF(hide_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL },
  { "history-size",ST_OFF(history_max), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL },
  { "raw-child", ST_OFF(raw_child), CONF_TYPE_FLAG, 0, 0, 1, NULL },
  { NULL, NULL, NULL, 0,0,0,NULL }
};

#define mpriv (menu->priv)

static void check_child(menu_t* menu);

static void add_line(struct menu_priv_s* priv, char* l) {
  char* eol = strchr(l,'\n');

  if(eol) {
    if(eol != l) {
      eol[0] = '\0';
      add_line(priv,l);
    }
    if(eol[1]) add_line(priv,eol+1);
    return;
  }

  if(priv->num_lines >= priv->buf_lines && priv->lines[priv->last_line])
    free(priv->lines[priv->last_line]);
  else
    priv->num_lines++;

  priv->lines[priv->last_line] = strdup(l);
  priv->last_line = (priv->last_line + 1) % priv->buf_lines;
  priv->add_line = 1;
}

static void add_string(struct menu_priv_s* priv, char* l) {
  char* eol = strchr(l,'\n');
  int ll =  priv->last_line > 0 ? priv->last_line - 1 : priv->buf_lines-1;

  if(priv->num_lines <= 0 || priv->add_line || eol == l) {
    add_line(priv,l);
    priv->add_line = 0;
    return;
  }
  
  if(eol) {
    eol[0] = '\0';
    add_string(priv,l);
    if(eol[1]) { 
      add_line(priv,eol+1);
      priv->add_line = 0;
    } else
      priv->add_line = 1;
    return;
  }
  priv->lines[ll] = realloc(priv->lines[ll],strlen(priv->lines[ll]) + strlen(l) + 1);
  strcat(priv->lines[ll],l);
  
}

static void draw(menu_t* menu, mp_image_t* mpi) {
  int h = mpi->h*mpriv->height/100;
  int w = mpi->w - 2* mpriv->minb;
  int x = mpriv->minb, y;
  int lw,lh,i, ll;

  if(mpriv->child) check_child(menu);

  ll = mpriv->last_line - 1;

  if(mpriv->hide_ts) {
    unsigned int t = GetTimerMS() - mpriv->hide_ts;
    if(t >= mpriv->hide_time) {
      mpriv->hide_ts = 0;
      menu->show = 0;
      return;
    }
    h = mpi->h*(mpriv->height - (mpriv->height * t /mpriv->hide_time))/100;
  } else if(mpriv->show_time && mpriv->show_ts == 0) {
    mpriv->show_ts = GetTimerMS();
    return;
  } else if(mpriv->show_ts > 0) {
    unsigned int t = GetTimerMS() - mpriv->show_ts;
    if(t > mpriv->show_time)
      mpriv->show_ts = -1;
    else
      h = mpi->h*(mpriv->height * t /mpriv->hide_time)/100;
  }

  y = h -  mpriv->vspace;

  if(x < 0 || y < 0 || w <= 0 || h <= 0 )
    return;

  if(!mpriv->child || !mpriv->raw_child){
    char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
    sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
    menu_text_size(input,w,mpriv->vspace,1,&lw,&lh);
    menu_draw_text_full(mpi,input,x,y,w,h,mpriv->vspace,1,
			MENU_TEXT_BOT|MENU_TEXT_LEFT,
			MENU_TEXT_BOT|MENU_TEXT_LEFT);
    y -= lh + mpriv->vspace;
  }


  for( i = 0 ; y > mpriv->minb && i < mpriv->num_lines ; i++){
    int c = (ll - i) >= 0 ? ll - i : mpriv->buf_lines + ll - i;
    menu_text_size(mpriv->lines[c],w,mpriv->vspace,1,&lw,&lh);
    menu_draw_text_full(mpi,mpriv->lines[c],x,y,w,h,mpriv->vspace,1,
			MENU_TEXT_BOT|MENU_TEXT_LEFT,
			MENU_TEXT_BOT|MENU_TEXT_LEFT);
    y -= lh + mpriv->vspace;
  }
  return;
}

static void read_cmd(menu_t* menu,int cmd) {
  switch(cmd) {
  case MENU_CMD_UP:
    break;
  case MENU_CMD_DOWN:
  case MENU_CMD_OK:
    break;
  case MENU_CMD_CANCEL:
    menu->show = 0;
    menu->cl = 1;
    break;
  }
}

static void check_child(menu_t* menu) {
  fd_set rfd;
  struct timeval tv;
  int max_fd = mpriv->child_fd[2] > mpriv->child_fd[1] ? mpriv->child_fd[2] :
    mpriv->child_fd[1];
  int i,r,child_status,w;
  char buffer[256];

  if(!mpriv->child) return;

  memset(&tv,0,sizeof(struct timeval));
  FD_ZERO(&rfd);
  FD_SET(mpriv->child_fd[1],&rfd);
  FD_SET(mpriv->child_fd[2],&rfd);

  r = select(max_fd+1,&rfd, NULL, NULL, &tv);
  if(r == 0) {
    r = waitpid(mpriv->child,&child_status,WNOHANG);
    if(r > 0) {
      printf("child died\n");
    for(i = 0 ; i < 3 ; i++) 
      close(mpriv->child_fd[i]);
    mpriv->child = 0;
    mpriv->prompt = mpriv->mp_prompt;
    //add_line(mpriv,"Child process exited");
    } else if(r < 0)
      printf("waitpid error: %s\n",strerror(errno));
  } else if(r < 0) {
    printf("select error\n");
    return;
  }
  
  w = 0;
  for(i = 1 ; i < 3 ; i++) {
    if(FD_ISSET(mpriv->child_fd[i],&rfd)){
      if(w) mpriv->add_line = 1;
      r = read(mpriv->child_fd[i],buffer,255);
      if(r < 0)
	printf("Read error on child's %s \n", i == 1 ? "stdout":"stderr");
      else if(r>0) {
	buffer[r] = '\0';
	add_string(mpriv,buffer);
      }
      w = 1;
    }
  }

}

#define close_pipe(pipe) close(pipe[0]); close(pipe[1])

static int run_shell_cmd(menu_t* menu, char* cmd) {
  int in[2],out[2],err[2];

  printf("Console run %s ...\n",cmd);
  if(mpriv->child) {
    printf("A child is alredy running\n");
    return 0;
  }

  pipe(in);
  pipe(out);
  pipe(err);

  mpriv->child = fork();
  if(mpriv->child < 0) {
    printf("Fork failed !!!\n");
    close_pipe(in);
    close_pipe(out);
    close_pipe(err);
    return 0;
  }
  if(!mpriv->child) { // Chlid process
    int err_fd = dup(2);
    FILE* errf = fdopen(err_fd,"w");
    // Bind the std fd to our pipes
    dup2(in[0],0);
    dup2(out[1],1);
    dup2(err[1],2);
    execl("/bin/sh","sh","-c",cmd,NULL);
    fprintf(errf,"exec failed : %s\n",strerror(errno));
    exit(1);
  }
  // MPlayer
  mpriv->child_fd[0] = in[1];
  mpriv->child_fd[1] = out[0];
  mpriv->child_fd[2] = err[0];
  mpriv->prompt = mpriv->child_prompt;
  //add_line(mpriv,"Child process started");
  return 1;
}

static void enter_cmd(menu_t* menu) {
  history_t* h;
  char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1];
  
  sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer);
  add_line(mpriv,input);

  if(mpriv->history == mpriv->cur_history) {
    if(mpriv->history_size >= mpriv->history_max) {
      history_t* i;
      for(i = mpriv->history ; i->prev ; i = i->prev)
	/**/;
      i->next->prev = NULL;
      free(i->buffer);
      free(i);
    } else
      mpriv->history_size++;
    h = calloc(1,sizeof(history_t));
    h->size = 255;
    h->buffer = calloc(h->size,1);
    h->prev = mpriv->history;
    mpriv->history->next = h;
    mpriv->history = h;
  } else
    mpriv->history->buffer[0] = '\0';
    
  mpriv->cur_history = mpriv->history;
  //mpriv->input = mpriv->cur_history->buffer;
}

static void read_key(menu_t* menu,int c) {
  if(!mpriv->child || !mpriv->raw_child) switch(c) {
  case KEY_ESC:
    if(mpriv->hide_time)
      mpriv->hide_ts = GetTimerMS();
    else
      menu->show = 0;
    mpriv->show_ts = 0;
    return;
  case KEY_ENTER: {
    mp_cmd_t* c;
    if(mpriv->child) {
      char *str = mpriv->cur_history->buffer;
      int l = strlen(str);
      while(l > 0) {
	int w = write(mpriv->child_fd[0],str,l);
	if(w < 0) {
	  printf("Write error\n");
	  break;
	}
	l -= w;
	str += w;
      }
      if(write(mpriv->child_fd[0],"\n",1) < 0)
	printf("Write error\n");
      enter_cmd(menu);
      return;
    }
    c = mp_input_parse_cmd(mpriv->cur_history->buffer);
    enter_cmd(menu);
    if(!c)
      add_line(mpriv,"Invalid command try help");
    else {
      switch(c->id) {
      case MP_CMD_CHELP:
	add_line(mpriv,"Mplayer console 0.01");
	add_line(mpriv,"TODO: Write some mainful help msg ;)");
	add_line(mpriv,"Enter any mplayer command");
	add_line(mpriv,"exit close this console");
	break;
      case MP_CMD_CEXIT:
	menu->show = 0;
	menu->cl = 1;
	break;
      case MP_CMD_CHIDE:
	if(mpriv->hide_time)
	  mpriv->hide_ts = GetTimerMS();
	else
	  menu->show = 0;
	mpriv->show_ts = 0;
	break;
      case MP_CMD_CRUN:
	run_shell_cmd(menu,c->args[0].v.s);
	break;
      default: // Send the other commands to mplayer
	mp_input_queue_cmd(c);
      }
    }
    return;
  }
  case KEY_DELETE:
  case KEY_BS: {
    unsigned int i = strlen(mpriv->cur_history->buffer);
    if(i > 0)
      mpriv->cur_history->buffer[i-1] = '\0';
    return;
  }
  case KEY_UP:
    if(mpriv->cur_history->prev)
      mpriv->cur_history = mpriv->cur_history->prev;
    break;
  case KEY_DOWN:
    if(mpriv->cur_history->next)
      mpriv->cur_history = mpriv->cur_history->next;
    break;
  }

  if(mpriv->child && mpriv->raw_child) {
    write(mpriv->child_fd[0],&c,sizeof(int));
    return;
  }

  if(isascii(c)) {
    int l = strlen(mpriv->cur_history->buffer);
    if(l >= mpriv->cur_history->size) {
      mpriv->cur_history->size += 255;
      mpriv->cur_history->buffer = realloc(mpriv->cur_history,mpriv->cur_history->size);
    }
    mpriv->cur_history->buffer[l] = (char)c;
    mpriv->cur_history->buffer[l+1] = '\0';
  }

}


static int open(menu_t* menu, char* args) {


  menu->draw = draw;
  menu->read_cmd = read_cmd;
  menu->read_key = read_key;

  mpriv->lines = calloc(mpriv->buf_lines,sizeof(char*));
  mpriv->prompt = mpriv->mp_prompt;
  mpriv->cur_history = mpriv->history = calloc(1,sizeof(history_t));
  mpriv->cur_history->buffer = calloc(255,sizeof(char));
  mpriv->cur_history->size = 255;
  
  if(args)
    add_line(mpriv,args);

  return 1;
}

const menu_info_t menu_info_console = {
  "MPlayer console",
  "console",
  "Albeu",
  "",
  {
    "console_cfg",
    sizeof(struct menu_priv_s),
    &cfg_dflt,
    cfg_fields
  },
  open,
};