Mercurial > mplayer.hg
annotate libmenu/menu.c @ 8310:171c99ff2649
.depend should be ignored.
author | diego |
---|---|
date | Thu, 28 Nov 2002 23:14:48 +0000 |
parents | 3d74366d947e |
children | 41a1e5dbb552 |
rev | line source |
---|---|
8197 | 1 |
2 #include "../config.h" | |
3 | |
4 #include <stdlib.h> | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 | |
8 #include "../libvo/osd.h" | |
9 #include "../libvo/font_load.h" | |
10 #include "../linux/keycodes.h" | |
11 #include "../asxparser.h" | |
12 #include "../libmpdemux/stream.h" | |
13 | |
14 #include "img_format.h" | |
15 #include "mp_image.h" | |
16 #include "../m_option.h" | |
17 #include "../m_struct.h" | |
18 #include "menu.h" | |
19 | |
20 extern menu_info_t menu_info_cmdlist; | |
21 extern menu_info_t menu_info_pt; | |
22 extern menu_info_t menu_info_filesel; | |
23 extern menu_info_t menu_info_txt; | |
24 extern menu_info_t menu_info_console; | |
25 extern menu_info_t menu_info_pref; | |
26 | |
27 menu_info_t* menu_info_list[] = { | |
28 &menu_info_pt, | |
29 &menu_info_cmdlist, | |
30 &menu_info_filesel, | |
31 &menu_info_txt, | |
32 &menu_info_console, | |
33 &menu_info_pref, | |
34 NULL | |
35 }; | |
36 | |
37 typedef struct menu_def_st { | |
38 char* name; | |
39 menu_info_t* type; | |
40 void* cfg; | |
41 char* args; | |
42 } menu_def_t; | |
43 | |
44 static menu_def_t* menu_list = NULL; | |
45 static int mcount = 0; | |
46 | |
47 | |
48 static int menu_parse_config(char* buffer) { | |
49 char *element,*body, **attribs, *name; | |
50 menu_info_t* minfo = NULL; | |
51 int r,i; | |
52 ASX_Parser_t* parser = asx_parser_new(); | |
53 | |
54 while(1) { | |
55 r = asx_get_element(parser,&buffer,&element,&body,&attribs); | |
56 if(r < 0) { | |
57 printf("Syntax error at line %d\n",parser->line); | |
58 asx_parser_free(parser); | |
59 return 0; | |
60 } else if(r == 0) { | |
61 asx_parser_free(parser); | |
62 return 1; | |
63 } | |
64 // Has it a name ? | |
65 name = asx_get_attrib("name",attribs); | |
66 if(!name) { | |
67 printf("Menu definitions need a name attrib (line %d)\n",parser->line); | |
68 free(element); | |
69 if(body) free(body); | |
70 asx_free_attribs(attribs); | |
71 continue; | |
72 } | |
73 | |
74 // Try to find this menu type in our list | |
75 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) { | |
76 if(strcasecmp(element,menu_info_list[i]->name) == 0) { | |
77 minfo = menu_info_list[i]; | |
78 break; | |
79 } | |
80 } | |
81 // Got it : add this to our list | |
82 if(minfo) { | |
83 menu_list = realloc(menu_list,(mcount+2)*sizeof(menu_def_t)); | |
84 menu_list[mcount].name = name; | |
85 menu_list[mcount].type = minfo; | |
86 menu_list[mcount].cfg = m_struct_alloc(&minfo->priv_st); | |
87 menu_list[mcount].args = body; | |
88 // Setup the attribs | |
89 for(i = 0 ; attribs[2*i] ; i++) { | |
90 if(strcasecmp(attribs[2*i],"name") == 0) continue; | |
91 if(!m_struct_set(&minfo->priv_st,menu_list[mcount].cfg,attribs[2*i], attribs[2*i+1])) | |
92 printf("Bad attrib %s=%s in menu %s at line %d\n",attribs[2*i],attribs[2*i+1], | |
93 name,parser->line); | |
94 } | |
95 mcount++; | |
96 memset(&menu_list[mcount],0,sizeof(menu_def_t)); | |
97 } else { | |
98 printf("Unknow menu type %s at line %d\n",element,parser->line); | |
99 free(name); | |
100 if(body) free(body); | |
101 } | |
102 | |
103 free(element); | |
104 asx_free_attribs(attribs); | |
105 } | |
106 | |
107 } | |
108 | |
109 | |
110 /// This will build the menu_defs list from the cfg file | |
111 #define BUF_STEP 1024 | |
112 #define BUF_MIN 128 | |
113 #define BUF_MAX BUF_STEP*1024 | |
114 int menu_init(char* cfg_file) { | |
115 char* buffer = NULL; | |
116 int bl = BUF_STEP, br = 0; | |
117 int f; | |
118 stream_t* stream = open_stream(cfg_file,0,&f); | |
119 if(!stream) { | |
120 printf("Can't open menu config file: %s\n",cfg_file); | |
121 return 0; | |
122 } | |
123 buffer = malloc(bl); | |
124 while(1) { | |
125 int r; | |
126 if(bl - br < BUF_MIN) { | |
127 if(bl >= BUF_MAX) { | |
128 printf("Menu config file is too big (> %d KB)\n",BUF_MAX/1024); | |
129 free_stream(stream); | |
130 free(buffer); | |
131 return 0; | |
132 } | |
133 bl += BUF_STEP; | |
134 buffer = realloc(buffer,bl); | |
135 } | |
136 r = stream_read(stream,buffer+br,bl-br); | |
137 if(r == 0) break; | |
138 br += r; | |
139 } | |
140 if(!br) { | |
141 printf("Menu config file is empty\n"); | |
142 return 0; | |
143 } | |
144 buffer[br-1] = '\0'; | |
145 | |
146 f = menu_parse_config(buffer); | |
147 free(buffer); | |
148 return f; | |
149 } | |
150 | |
151 // Destroy all this stuff | |
152 void menu_unint(void) { | |
153 int i; | |
154 for(i = 0 ; menu_list && menu_list[i].name ; i++) { | |
155 free(menu_list[i].name); | |
156 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg); | |
157 if(menu_list[i].args) free(menu_list[i].args); | |
158 } | |
159 free(menu_list); | |
160 mcount = 0; | |
161 } | |
162 | |
163 /// Default read_key function | |
164 void menu_dflt_read_key(menu_t* menu,int cmd) { | |
165 switch(cmd) { | |
166 case KEY_UP: | |
167 menu->read_cmd(menu,MENU_CMD_UP); | |
168 break; | |
169 case KEY_DOWN: | |
170 menu->read_cmd(menu,MENU_CMD_DOWN); | |
171 break; | |
172 case KEY_LEFT: | |
173 case KEY_ESC: | |
174 menu->read_cmd(menu,MENU_CMD_CANCEL); | |
175 break; | |
176 case KEY_RIGHT: | |
177 case KEY_ENTER: | |
178 menu->read_cmd(menu,MENU_CMD_OK); | |
179 break; | |
180 } | |
181 } | |
182 | |
183 menu_t* menu_open(char *name) { | |
184 menu_t* m; | |
185 int i; | |
186 | |
187 for(i = 0 ; menu_list[i].name != NULL ; i++) { | |
188 if(strcmp(name,menu_list[i].name) == 0) | |
189 break; | |
190 } | |
191 if(menu_list[i].name == NULL) { | |
192 printf("Menu %s not found\n",name); | |
193 return NULL; | |
194 } | |
195 m = calloc(1,sizeof(menu_t)); | |
196 m->priv_st = &(menu_list[i].type->priv_st); | |
197 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg); | |
198 if(menu_list[i].type->open(m,menu_list[i].args)) | |
199 return m; | |
200 if(m->priv) | |
201 m_struct_free(m->priv_st,m->priv); | |
202 free(m); | |
203 printf("Menu %s: init failed\n",name); | |
204 return NULL; | |
205 } | |
206 | |
207 void menu_draw(menu_t* menu,mp_image_t* mpi) { | |
208 if(menu->show && menu->draw) | |
209 menu->draw(menu,mpi); | |
210 } | |
211 | |
212 void menu_read_cmd(menu_t* menu,int cmd) { | |
213 if(menu->read_cmd) | |
214 menu->read_cmd(menu,cmd); | |
215 } | |
216 | |
217 void menu_close(menu_t* menu) { | |
218 if(menu->close) | |
219 menu->close(menu); | |
220 if(menu->priv) | |
221 m_struct_free(menu->priv_st,menu->priv); | |
222 free(menu); | |
223 } | |
224 | |
225 void menu_read_key(menu_t* menu,int cmd) { | |
226 if(menu->read_key) | |
227 menu->read_key(menu,cmd); | |
228 else | |
229 menu_dflt_read_key(menu,cmd); | |
230 } | |
231 | |
232 ///////////////////////////// Helpers //////////////////////////////////// | |
233 | |
234 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride); | |
235 | |
236 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) { | |
237 switch(fmt) { | |
238 case IMGFMT_BGR15: | |
239 case IMGFMT_RGB15: | |
240 return vo_draw_alpha_rgb15; | |
241 case IMGFMT_BGR16: | |
242 case IMGFMT_RGB16: | |
243 return vo_draw_alpha_rgb16; | |
244 case IMGFMT_BGR24: | |
245 case IMGFMT_RGB24: | |
246 return vo_draw_alpha_rgb24; | |
247 case IMGFMT_BGR32: | |
248 case IMGFMT_RGB32: | |
249 return vo_draw_alpha_rgb32; | |
250 case IMGFMT_YV12: | |
251 case IMGFMT_I420: | |
252 case IMGFMT_IYUV: | |
253 case IMGFMT_YVU9: | |
254 case IMGFMT_IF09: | |
255 case IMGFMT_Y800: | |
256 case IMGFMT_Y8: | |
257 return vo_draw_alpha_yv12; | |
258 case IMGFMT_YUY2: | |
259 return vo_draw_alpha_yuy2; | |
260 } | |
261 | |
262 return NULL; | |
263 } | |
264 | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
265 // return the real height of a char: |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
266 static inline int get_height(int c,int h){ |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
267 int font; |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
268 if ((font=vo_font->font[c])>=0) |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
269 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h; |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
270 return h; |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
271 } |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
272 |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
273 #ifdef HAVE_FREETYPE |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
274 #define render_txt(t) { char* p = t; while(*p) render_one_glyph(vo_font,*p++); } |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
275 #else |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
276 #define render_txt(t) |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
277 #endif |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
278 |
8197 | 279 |
280 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) { | |
281 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt); | |
282 int font; | |
283 | |
284 if(!draw_alpha) { | |
285 printf("Unsupported outformat !!!!\n"); | |
286 return; | |
287 } | |
288 | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
289 render_txt(txt); |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
290 |
8197 | 291 while (*txt) { |
292 unsigned char c=*txt++; | |
293 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h)) | |
294 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h, | |
295 vo_font->pic_b[font]->bmp+vo_font->start[c], | |
296 vo_font->pic_a[font]->bmp+vo_font->start[c], | |
297 vo_font->pic_a[font]->w, | |
298 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3), | |
299 mpi->stride[0]); | |
300 x+=vo_font->width[c]+vo_font->charspace; | |
301 } | |
302 | |
303 } | |
304 | |
305 void menu_draw_text_full(mp_image_t* mpi,char* txt, | |
306 int x, int y,int w, int h, | |
307 int vspace, int warp, int align, int anchor) { | |
308 int need_w,need_h; | |
309 int sy, ymin, ymax; | |
310 int sx, xmin, xmax, xmid, xrmin; | |
311 int ll = 0; | |
312 int font; | |
313 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt); | |
314 | |
315 if(!draw_alpha) { | |
316 printf("Unsupported outformat !!!!\n"); | |
317 return; | |
318 } | |
319 | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
320 render_txt(txt); |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
321 |
8197 | 322 if(x > mpi->w || y > mpi->h) |
323 return; | |
324 | |
325 if(anchor & MENU_TEXT_VCENTER) { | |
326 if(h <= 0) h = mpi->h; | |
327 ymin = y - h/2; | |
328 ymax = y + h/2; | |
329 } else if(anchor & MENU_TEXT_BOT) { | |
330 if(h <= 0) h = mpi->h - y; | |
331 ymin = y - h; | |
332 ymax = y; | |
333 } else { | |
334 if(h <= 0) h = mpi->h - y; | |
335 ymin = y; | |
336 ymax = y + h; | |
337 } | |
338 | |
339 if(anchor & MENU_TEXT_HCENTER) { | |
340 if(w <= 0) w = mpi->w; | |
341 xmin = x - w/2; | |
342 xmax = x + w/2; | |
343 } else if(anchor & MENU_TEXT_RIGHT) { | |
344 if(w <= 0) w = mpi->w -x; | |
345 xmin = x - w; | |
346 xmax = x; | |
347 } else { | |
348 if(w <= 0) w = mpi->w -x; | |
349 xmin = x; | |
350 xmax = x + w; | |
351 } | |
352 | |
353 // How many space do we need to draw this ? | |
354 menu_text_size(txt,w,vspace,warp,&need_w,&need_h); | |
355 | |
356 // Find the first line | |
357 if(align & MENU_TEXT_VCENTER) | |
358 sy = ymin + ((h - need_h)/2); | |
359 else if(align & MENU_TEXT_BOT) | |
8232 | 360 sy = ymax - need_h - 1; |
8197 | 361 else |
362 sy = y; | |
363 | |
364 #if 0 | |
365 // Find the first col | |
366 if(align & MENU_TEXT_HCENTER) | |
367 sx = xmin + ((w - need_w)/2); | |
368 else if(align & MENU_TEXT_RIGHT) | |
369 sx = xmax - need_w; | |
370 #endif | |
371 | |
372 xmid = xmin + (xmax - xmin) / 2; | |
373 xrmin = xmin; | |
374 // Clamp the bb to the mpi size | |
375 if(ymin < 0) ymin = 0; | |
376 if(xmin < 0) xmin = 0; | |
377 if(ymax > mpi->h) ymax = mpi->h; | |
378 if(xmax > mpi->w) xmax = mpi->w; | |
379 | |
380 // Jump some the beginnig text if needed | |
381 while(sy < ymin && *txt) { | |
382 unsigned char c=*txt++; | |
383 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) { | |
384 ll = 0; | |
385 sy += vo_font->height + vspace; | |
386 if(c == '\n') continue; | |
387 } | |
388 ll += vo_font->width[c]+vo_font->charspace; | |
389 } | |
390 if(*txt == '\0') // Nothing left to draw | |
391 return; | |
392 | |
393 while(sy < ymax && *txt) { | |
394 char* line_end = NULL; | |
395 int n; | |
396 | |
397 if(txt[0] == '\n') { // New line | |
398 sy += vo_font->height + vspace; | |
399 txt++; | |
400 continue; | |
401 } | |
402 | |
403 // Get the length and end of this line | |
404 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) { | |
405 unsigned char c = txt[n]; | |
406 if(warp && ll + vo_font->width[c] > w) break; | |
407 ll += vo_font->width[c]+vo_font->charspace; | |
408 } | |
409 line_end = &txt[n]; | |
410 ll -= vo_font->charspace; | |
411 | |
412 | |
413 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) { | |
414 // Too long line | |
415 if(ll > xmax-xmin) { | |
416 if(align & MENU_TEXT_HCENTER) { | |
417 int mid = ll/2; | |
418 // Find the middle point | |
419 for(n--, ll = 0 ; n <= 0 ; n--) { | |
420 ll += vo_font->width[(int)txt[n]]+vo_font->charspace; | |
421 if(ll - vo_font->charspace > mid) break; | |
422 } | |
423 ll -= vo_font->charspace; | |
424 sx = xmid + mid - ll; | |
425 } else// MENU_TEXT_RIGHT) | |
426 sx = xmax + vo_font->charspace; | |
427 | |
428 // We are after the start point -> go back | |
429 if(sx > xmin) { | |
430 for(n-- ; n <= 0 ; n--) { | |
431 unsigned char c = txt[n]; | |
432 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break; | |
433 sx -= vo_font->width[c]+vo_font->charspace; | |
434 } | |
435 } else { // We are before the start point -> go forward | |
436 for( ; sx < xmin && (&txt[n]) != line_end ; n++) { | |
437 unsigned char c = txt[n]; | |
438 sx += vo_font->width[c]+vo_font->charspace; | |
439 } | |
440 } | |
441 txt = &txt[n]; // Jump to the new start char | |
442 } else { | |
443 if(align & MENU_TEXT_HCENTER) | |
444 sx = xmid - ll/2; | |
445 else | |
8232 | 446 sx = xmax - 1 - ll; |
8197 | 447 } |
448 } else { | |
449 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) { | |
450 unsigned char c = txt[n]; | |
451 sx += vo_font->width[c]+vo_font->charspace; | |
452 } | |
453 } | |
454 | |
455 while(sx < xmax && txt != line_end) { | |
456 unsigned char c = *txt++; | |
457 font = vo_font->font[c]; | |
8232 | 458 if(font >= 0) { |
459 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2; | |
460 if ((sx + vo_font->width[c] < xmax) && (sy + vo_font->height < ymax) ) | |
461 draw_alpha(vo_font->width[c], vo_font->height, | |
462 vo_font->pic_b[font]->bmp+vo_font->start[c] + | |
463 cs * vo_font->pic_a[font]->w, | |
464 vo_font->pic_a[font]->bmp+vo_font->start[c] + | |
465 cs * vo_font->pic_a[font]->w, | |
466 vo_font->pic_a[font]->w, | |
467 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3), | |
468 mpi->stride[0]); | |
469 // else | |
470 //printf("Can't draw '%c'\n",c); | |
471 } | |
8197 | 472 sx+=vo_font->width[c]+vo_font->charspace; |
473 } | |
474 txt = line_end; | |
475 if(txt[0] == '\0') break; | |
476 sy += vo_font->height + vspace; | |
477 } | |
478 } | |
479 | |
480 int menu_text_length(char* txt) { | |
481 int l = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
482 render_txt(txt); |
8197 | 483 while (*txt) { |
484 unsigned char c=*txt++; | |
485 l += vo_font->width[c]+vo_font->charspace; | |
486 } | |
487 return l - vo_font->charspace; | |
488 } | |
489 | |
490 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) { | |
491 int l = 1, i = 0; | |
492 int w = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
493 |
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
494 render_txt(txt); |
8197 | 495 while (*txt) { |
496 unsigned char c=*txt++; | |
497 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) { | |
498 if(*txt) | |
499 l++; | |
500 i = 0; | |
501 if(c == '\n') continue; | |
502 } | |
503 i += vo_font->width[c]+vo_font->charspace; | |
504 if(i > w) w = i; | |
505 } | |
506 | |
507 *_w = w; | |
508 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height; | |
509 } | |
510 | |
511 | |
512 int menu_text_num_lines(char* txt, int max_width) { | |
513 int l = 1, i = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
514 render_txt(txt); |
8197 | 515 while (*txt) { |
516 unsigned char c=*txt++; | |
517 if(c == '\n' || i + vo_font->width[c] > max_width) { | |
518 l++; | |
519 i = 0; | |
520 if(c == '\n') continue; | |
521 } | |
522 i += vo_font->width[c]+vo_font->charspace; | |
523 } | |
524 return l; | |
525 } | |
526 | |
527 char* menu_text_get_next_line(char* txt, int max_width) { | |
528 int i = 0; | |
8224
fefc56153615
Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents:
8197
diff
changeset
|
529 render_txt(txt); |
8197 | 530 while (*txt) { |
531 unsigned char c=*txt; | |
532 if(c == '\n') { | |
533 txt++; | |
534 break; | |
535 } | |
536 i += vo_font->width[c]; | |
537 if(i >= max_width) | |
538 break; | |
539 i += vo_font->charspace; | |
540 } | |
541 return txt; | |
542 } |