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