Mercurial > mplayer.hg
annotate libmenu/menu_console.c @ 17993:98eb966a4024
Add a function to draw flat boxes and use it to make the list
menu and console look much cooler. Idea take from Otvos Atilla's
patches (oattila_At_chello--.--hu).
author | albeu |
---|---|
date | Thu, 30 Mar 2006 02:52:54 +0000 |
parents | 931bdbc37ee0 |
children | 6927fabaef92 |
rev | line source |
---|---|
8197 | 1 |
16862 | 2 #include "config.h" |
8197 | 3 |
4 #include <stdlib.h> | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 #include <ctype.h> | |
8229 | 8 #include <sys/time.h> |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
9 #include <sys/types.h> |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
10 #ifndef __MINGW32__ |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
11 #include <sys/wait.h> |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
12 #endif |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
13 #include <unistd.h> |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
14 #include <errno.h> |
8197 | 15 |
16 #include "img_format.h" | |
17 #include "mp_image.h" | |
18 | |
16862 | 19 #include "m_struct.h" |
20 #include "m_option.h" | |
8197 | 21 #include "menu.h" |
22 | |
16862 | 23 #include "libvo/font_load.h" |
24 #include "osdep/keycodes.h" | |
25 #include "input/input.h" | |
26 #include "osdep/timer.h" | |
8197 | 27 |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
28 typedef struct history_st history_t; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
29 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
30 struct history_st { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
31 char* buffer; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
32 int size; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
33 history_t* next; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
34 history_t* prev; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
35 }; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
36 |
8197 | 37 struct menu_priv_s { |
38 char** lines; // Our buffer | |
39 int last_line; | |
40 int num_lines; | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
41 int add_line; |
8197 | 42 unsigned int hide_ts; |
43 unsigned int show_ts; | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
44 pid_t child; // Child process if we are running a shell cmd |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
45 int child_fd[3]; // The 3 default fd |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
46 char* prompt; |
8197 | 47 //int max_lines; // Max number of lines with the last mpi |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
48 history_t* history; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
49 history_t* cur_history; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
50 int history_size; |
8197 | 51 |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
52 char* mp_prompt; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
53 char* child_prompt; |
8197 | 54 int buf_lines; // Buffer size (in line) |
55 int height; // Display size in % | |
56 int minb; | |
57 int vspace; | |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
58 int bg,bg_alpha; |
8197 | 59 unsigned int hide_time; |
60 unsigned int show_time; | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
61 int history_max; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
62 int raw_child; |
8197 | 63 }; |
64 | |
65 static struct menu_priv_s cfg_dflt = { | |
66 NULL, | |
67 0, | |
68 0, | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
69 1, |
8197 | 70 0, |
71 0, | |
72 0, | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
73 { 0 , 0, 0 }, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
74 NULL, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
75 NULL, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
76 NULL, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
77 0, |
8197 | 78 |
79 "# ", | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
80 "$ ", |
8197 | 81 50, // lines |
82 33, // % | |
83 3, | |
84 3, | |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
85 0x80,0x40, |
8197 | 86 500, |
87 500, | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
88 10, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
89 0 |
8197 | 90 }; |
91 | |
92 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m) | |
93 | |
94 static m_option_t cfg_fields[] = { | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
95 { "prompt", ST_OFF(mp_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL }, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
96 { "child-prompt", ST_OFF(child_prompt), CONF_TYPE_STRING, M_OPT_MIN, 1, 0, NULL }, |
8197 | 97 { "buffer-lines", ST_OFF(buf_lines), CONF_TYPE_INT, M_OPT_MIN, 5, 0, NULL }, |
98 { "height", ST_OFF(height), CONF_TYPE_INT, M_OPT_RANGE, 1, 100, NULL }, | |
99 { "minbor", ST_OFF(minb), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL }, | |
100 { "vspace", ST_OFF(vspace), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL }, | |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
101 { "bg", ST_OFF(bg), CONF_TYPE_INT, M_OPT_RANGE, -1, 255, NULL }, |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
102 { "bg-alpha", ST_OFF(bg_alpha), CONF_TYPE_INT, M_OPT_RANGE, 0, 255, NULL }, |
8197 | 103 { "show-time",ST_OFF(show_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL }, |
104 { "hide-time",ST_OFF(hide_time), CONF_TYPE_INT, M_OPT_MIN, 0, 0, NULL }, | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
105 { "history-size",ST_OFF(history_max), CONF_TYPE_INT, M_OPT_MIN, 1, 0, NULL }, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
106 { "raw-child", ST_OFF(raw_child), CONF_TYPE_FLAG, 0, 0, 1, NULL }, |
8197 | 107 { NULL, NULL, NULL, 0,0,0,NULL } |
108 }; | |
109 | |
110 #define mpriv (menu->priv) | |
111 | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
112 static void check_child(menu_t* menu); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
113 |
8197 | 114 static void add_line(struct menu_priv_s* priv, char* l) { |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
115 char* eol = strchr(l,'\n'); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
116 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
117 if(eol) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
118 if(eol != l) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
119 eol[0] = '\0'; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
120 add_line(priv,l); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
121 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
122 if(eol[1]) add_line(priv,eol+1); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
123 return; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
124 } |
8197 | 125 |
126 if(priv->num_lines >= priv->buf_lines && priv->lines[priv->last_line]) | |
127 free(priv->lines[priv->last_line]); | |
128 else | |
129 priv->num_lines++; | |
130 | |
131 priv->lines[priv->last_line] = strdup(l); | |
132 priv->last_line = (priv->last_line + 1) % priv->buf_lines; | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
133 priv->add_line = 1; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
134 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
135 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
136 static void add_string(struct menu_priv_s* priv, char* l) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
137 char* eol = strchr(l,'\n'); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
138 int ll = priv->last_line > 0 ? priv->last_line - 1 : priv->buf_lines-1; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
139 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
140 if(priv->num_lines <= 0 || priv->add_line || eol == l) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
141 add_line(priv,l); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
142 priv->add_line = 0; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
143 return; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
144 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
145 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
146 if(eol) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
147 eol[0] = '\0'; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
148 add_string(priv,l); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
149 if(eol[1]) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
150 add_line(priv,eol+1); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
151 priv->add_line = 0; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
152 } else |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
153 priv->add_line = 1; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
154 return; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
155 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
156 priv->lines[ll] = realloc(priv->lines[ll],strlen(priv->lines[ll]) + strlen(l) + 1); |
12646 | 157 if ( priv->lines[ll] != NULL ) |
158 { | |
159 strcat(priv->lines[ll],l); | |
160 } | |
8197 | 161 } |
162 | |
163 static void draw(menu_t* menu, mp_image_t* mpi) { | |
164 int h = mpi->h*mpriv->height/100; | |
165 int w = mpi->w - 2* mpriv->minb; | |
166 int x = mpriv->minb, y; | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
167 int lw,lh,i, ll; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
168 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
169 if(mpriv->child) check_child(menu); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
170 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
171 ll = mpriv->last_line - 1; |
8197 | 172 |
173 if(mpriv->hide_ts) { | |
174 unsigned int t = GetTimerMS() - mpriv->hide_ts; | |
175 if(t >= mpriv->hide_time) { | |
176 mpriv->hide_ts = 0; | |
177 menu->show = 0; | |
178 return; | |
179 } | |
180 h = mpi->h*(mpriv->height - (mpriv->height * t /mpriv->hide_time))/100; | |
181 } else if(mpriv->show_time && mpriv->show_ts == 0) { | |
182 mpriv->show_ts = GetTimerMS(); | |
183 return; | |
184 } else if(mpriv->show_ts > 0) { | |
185 unsigned int t = GetTimerMS() - mpriv->show_ts; | |
186 if(t > mpriv->show_time) | |
187 mpriv->show_ts = -1; | |
188 else | |
189 h = mpi->h*(mpriv->height * t /mpriv->hide_time)/100; | |
190 } | |
191 | |
192 y = h - mpriv->vspace; | |
193 | |
194 if(x < 0 || y < 0 || w <= 0 || h <= 0 ) | |
195 return; | |
196 | |
17993
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
197 if(mpriv->bg >= 0) |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
198 menu_draw_box(mpi,mpriv->bg,mpriv->bg_alpha,0,0,mpi->w,h); |
98eb966a4024
Add a function to draw flat boxes and use it to make the list
albeu
parents:
16862
diff
changeset
|
199 |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
200 if(!mpriv->child || !mpriv->raw_child){ |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
201 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
202 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
203 menu_text_size(input,w,mpriv->vspace,1,&lw,&lh); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
204 menu_draw_text_full(mpi,input,x,y,w,h,mpriv->vspace,1, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
205 MENU_TEXT_BOT|MENU_TEXT_LEFT, |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
206 MENU_TEXT_BOT|MENU_TEXT_LEFT); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
207 y -= lh + mpriv->vspace; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
208 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
209 |
8197 | 210 |
211 for( i = 0 ; y > mpriv->minb && i < mpriv->num_lines ; i++){ | |
212 int c = (ll - i) >= 0 ? ll - i : mpriv->buf_lines + ll - i; | |
213 menu_text_size(mpriv->lines[c],w,mpriv->vspace,1,&lw,&lh); | |
214 menu_draw_text_full(mpi,mpriv->lines[c],x,y,w,h,mpriv->vspace,1, | |
215 MENU_TEXT_BOT|MENU_TEXT_LEFT, | |
216 MENU_TEXT_BOT|MENU_TEXT_LEFT); | |
217 y -= lh + mpriv->vspace; | |
218 } | |
219 return; | |
220 } | |
221 | |
222 static void read_cmd(menu_t* menu,int cmd) { | |
223 switch(cmd) { | |
224 case MENU_CMD_UP: | |
225 break; | |
226 case MENU_CMD_DOWN: | |
227 case MENU_CMD_OK: | |
228 break; | |
229 case MENU_CMD_CANCEL: | |
230 menu->show = 0; | |
231 menu->cl = 1; | |
232 break; | |
233 } | |
234 } | |
235 | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
236 static void check_child(menu_t* menu) { |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
237 #ifndef __MINGW32__ |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
238 fd_set rfd; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
239 struct timeval tv; |
8297 | 240 int max_fd = mpriv->child_fd[2] > mpriv->child_fd[1] ? mpriv->child_fd[2] : |
241 mpriv->child_fd[1]; | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
242 int i,r,child_status,w; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
243 char buffer[256]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
244 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
245 if(!mpriv->child) return; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
246 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
247 memset(&tv,0,sizeof(struct timeval)); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
248 FD_ZERO(&rfd); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
249 FD_SET(mpriv->child_fd[1],&rfd); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
250 FD_SET(mpriv->child_fd[2],&rfd); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
251 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
252 r = select(max_fd+1,&rfd, NULL, NULL, &tv); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
253 if(r == 0) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
254 r = waitpid(mpriv->child,&child_status,WNOHANG); |
13391 | 255 if(r < 0){ |
256 if(errno==ECHILD){ ///exiting childs get handled in mplayer.c | |
257 for(i = 0 ; i < 3 ; i++) | |
258 close(mpriv->child_fd[i]); | |
259 mpriv->child = 0; | |
260 mpriv->prompt = mpriv->mp_prompt; | |
261 //add_line(mpriv,"Child process exited"); | |
262 } | |
263 else printf("waitpid error: %s\n",strerror(errno)); | |
264 } | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
265 } else if(r < 0) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
266 printf("select error\n"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
267 return; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
268 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
269 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
270 w = 0; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
271 for(i = 1 ; i < 3 ; i++) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
272 if(FD_ISSET(mpriv->child_fd[i],&rfd)){ |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
273 if(w) mpriv->add_line = 1; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
274 r = read(mpriv->child_fd[i],buffer,255); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
275 if(r < 0) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
276 printf("Read error on child's %s \n", i == 1 ? "stdout":"stderr"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
277 else if(r>0) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
278 buffer[r] = '\0'; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
279 add_string(mpriv,buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
280 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
281 w = 1; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
282 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
283 } |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
284 #endif |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
285 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
286 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
287 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
288 #define close_pipe(pipe) close(pipe[0]); close(pipe[1]) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
289 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
290 static int run_shell_cmd(menu_t* menu, char* cmd) { |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
291 #ifndef __MINGW32__ |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
292 int in[2],out[2],err[2]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
293 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
294 printf("Console run %s ...\n",cmd); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
295 if(mpriv->child) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
296 printf("A child is alredy running\n"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
297 return 0; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
298 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
299 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
300 pipe(in); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
301 pipe(out); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
302 pipe(err); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
303 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
304 mpriv->child = fork(); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
305 if(mpriv->child < 0) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
306 printf("Fork failed !!!\n"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
307 close_pipe(in); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
308 close_pipe(out); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
309 close_pipe(err); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
310 return 0; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
311 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
312 if(!mpriv->child) { // Chlid process |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
313 int err_fd = dup(2); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
314 FILE* errf = fdopen(err_fd,"w"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
315 // Bind the std fd to our pipes |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
316 dup2(in[0],0); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
317 dup2(out[1],1); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
318 dup2(err[1],2); |
10333 | 319 execl("/bin/sh","sh","-c",cmd,(void*)NULL); |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
320 fprintf(errf,"exec failed : %s\n",strerror(errno)); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
321 exit(1); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
322 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
323 // MPlayer |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
324 mpriv->child_fd[0] = in[1]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
325 mpriv->child_fd[1] = out[0]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
326 mpriv->child_fd[2] = err[0]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
327 mpriv->prompt = mpriv->child_prompt; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
328 //add_line(mpriv,"Child process started"); |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
329 #endif |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
330 return 1; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
331 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
332 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
333 static void enter_cmd(menu_t* menu) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
334 history_t* h; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
335 char input[strlen(mpriv->cur_history->buffer) + strlen(mpriv->prompt) + 1]; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
336 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
337 sprintf(input,"%s%s",mpriv->prompt,mpriv->cur_history->buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
338 add_line(mpriv,input); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
339 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
340 if(mpriv->history == mpriv->cur_history) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
341 if(mpriv->history_size >= mpriv->history_max) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
342 history_t* i; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
343 for(i = mpriv->history ; i->prev ; i = i->prev) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
344 /**/; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
345 i->next->prev = NULL; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
346 free(i->buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
347 free(i); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
348 } else |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
349 mpriv->history_size++; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
350 h = calloc(1,sizeof(history_t)); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
351 h->size = 255; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
352 h->buffer = calloc(h->size,1); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
353 h->prev = mpriv->history; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
354 mpriv->history->next = h; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
355 mpriv->history = h; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
356 } else |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
357 mpriv->history->buffer[0] = '\0'; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
358 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
359 mpriv->cur_history = mpriv->history; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
360 //mpriv->input = mpriv->cur_history->buffer; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
361 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
362 |
8197 | 363 static void read_key(menu_t* menu,int c) { |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
364 if(!mpriv->child || !mpriv->raw_child) switch(c) { |
8197 | 365 case KEY_ESC: |
366 if(mpriv->hide_time) | |
367 mpriv->hide_ts = GetTimerMS(); | |
368 else | |
369 menu->show = 0; | |
370 mpriv->show_ts = 0; | |
371 return; | |
372 case KEY_ENTER: { | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
373 mp_cmd_t* c; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
374 if(mpriv->child) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
375 char *str = mpriv->cur_history->buffer; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
376 int l = strlen(str); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
377 while(l > 0) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
378 int w = write(mpriv->child_fd[0],str,l); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
379 if(w < 0) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
380 printf("Write error\n"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
381 break; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
382 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
383 l -= w; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
384 str += w; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
385 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
386 if(write(mpriv->child_fd[0],"\n",1) < 0) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
387 printf("Write error\n"); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
388 enter_cmd(menu); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
389 return; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
390 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
391 c = mp_input_parse_cmd(mpriv->cur_history->buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
392 enter_cmd(menu); |
8197 | 393 if(!c) |
394 add_line(mpriv,"Invalid command try help"); | |
395 else { | |
396 switch(c->id) { | |
397 case MP_CMD_CHELP: | |
12874 | 398 add_line(mpriv,"MPlayer console 0.01"); |
399 add_line(mpriv,"TODO: meaningful help message ;)"); | |
400 add_line(mpriv,"Enter any slave command"); | |
8197 | 401 add_line(mpriv,"exit close this console"); |
402 break; | |
403 case MP_CMD_CEXIT: | |
404 menu->show = 0; | |
405 menu->cl = 1; | |
406 break; | |
407 case MP_CMD_CHIDE: | |
408 if(mpriv->hide_time) | |
409 mpriv->hide_ts = GetTimerMS(); | |
410 else | |
411 menu->show = 0; | |
412 mpriv->show_ts = 0; | |
413 break; | |
14087 | 414 case MP_CMD_RUN: |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
415 run_shell_cmd(menu,c->args[0].v.s); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
416 break; |
8197 | 417 default: // Send the other commands to mplayer |
418 mp_input_queue_cmd(c); | |
419 } | |
420 } | |
421 return; | |
422 } | |
423 case KEY_DELETE: | |
424 case KEY_BS: { | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
425 unsigned int i = strlen(mpriv->cur_history->buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
426 if(i > 0) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
427 mpriv->cur_history->buffer[i-1] = '\0'; |
8197 | 428 return; |
429 } | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
430 case KEY_UP: |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
431 if(mpriv->cur_history->prev) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
432 mpriv->cur_history = mpriv->cur_history->prev; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
433 break; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
434 case KEY_DOWN: |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
435 if(mpriv->cur_history->next) |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
436 mpriv->cur_history = mpriv->cur_history->next; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
437 break; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
438 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
439 |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
440 if(mpriv->child && mpriv->raw_child) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
441 write(mpriv->child_fd[0],&c,sizeof(int)); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
442 return; |
8197 | 443 } |
444 | |
445 if(isascii(c)) { | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
446 int l = strlen(mpriv->cur_history->buffer); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
447 if(l >= mpriv->cur_history->size) { |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
448 mpriv->cur_history->size += 255; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
449 mpriv->cur_history->buffer = realloc(mpriv->cur_history,mpriv->cur_history->size); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
450 } |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
451 mpriv->cur_history->buffer[l] = (char)c; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
452 mpriv->cur_history->buffer[l+1] = '\0'; |
8197 | 453 } |
454 | |
455 } | |
456 | |
457 | |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
458 static int openMenu(menu_t* menu, char* args) { |
8197 | 459 |
460 | |
461 menu->draw = draw; | |
462 menu->read_cmd = read_cmd; | |
463 menu->read_key = read_key; | |
464 | |
465 mpriv->lines = calloc(mpriv->buf_lines,sizeof(char*)); | |
8227
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
466 mpriv->prompt = mpriv->mp_prompt; |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
467 mpriv->cur_history = mpriv->history = calloc(1,sizeof(history_t)); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
468 mpriv->cur_history->buffer = calloc(255,sizeof(char)); |
3050cfda3c61
A new command to run shell process and a command history.
albeu
parents:
8197
diff
changeset
|
469 mpriv->cur_history->size = 255; |
8197 | 470 |
471 if(args) | |
472 add_line(mpriv,args); | |
473 | |
474 return 1; | |
475 } | |
476 | |
477 const menu_info_t menu_info_console = { | |
478 "MPlayer console", | |
479 "console", | |
480 "Albeu", | |
481 "", | |
482 { | |
483 "console_cfg", | |
484 sizeof(struct menu_priv_s), | |
485 &cfg_dflt, | |
486 cfg_fields | |
487 }, | |
10864
a2537e7d2d76
make menu work on mingw (run command is still disabled), based on patch by Christophe Perinaud
faust3
parents:
10333
diff
changeset
|
488 openMenu, |
8197 | 489 }; |