12 #include "libvo/osd.h"
13 #include "libvo/font_load.h"
14 #include "libvo/sub.h"
15 #include "osdep/keycodes.h"
16 #include "asxparser.h"
17 #include "stream/stream.h"
18 #include "input/input.h"
20 #include "libmpcodecs/img_format.h"
21 #include "libmpcodecs/mp_image.h"
26 extern menu_info_t menu_info_cmdlist
;
27 extern menu_info_t menu_info_chapsel
;
28 extern menu_info_t menu_info_pt
;
29 extern menu_info_t menu_info_filesel
;
30 extern menu_info_t menu_info_txt
;
31 extern menu_info_t menu_info_console
;
32 extern menu_info_t menu_info_pref
;
33 #ifdef HAS_DVBIN_SUPPORT
34 extern menu_info_t menu_info_dvbsel
;
38 menu_info_t
* menu_info_list
[] = {
45 #ifdef HAS_DVBIN_SUPPORT
52 typedef struct key_cmd_s
{
57 typedef struct menu_cmd_bindings_s
{
61 struct menu_cmd_bindings_s
*parent
;
62 } menu_cmd_bindings_t
;
71 static struct MPContext
*menu_ctx
= NULL
;
72 static menu_def_t
* menu_list
= NULL
;
73 static int menu_count
= 0;
74 static menu_cmd_bindings_t
*cmd_bindings
= NULL
;
75 static int cmd_bindings_num
= 0;
78 menu_cmd_bindings_t
*get_cmd_bindings(const char *name
) {
80 for (i
= 0; i
< cmd_bindings_num
; ++i
)
81 if (!strcasecmp(cmd_bindings
[i
].name
, name
))
82 return &cmd_bindings
[i
];
86 static int menu_parse_config(char* buffer
) {
87 char *element
,*body
, **attribs
, *name
;
88 menu_info_t
* minfo
= NULL
;
90 ASX_Parser_t
* parser
= asx_parser_new();
93 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
95 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
96 asx_parser_free(parser
);
99 asx_parser_free(parser
);
103 name
= asx_get_attrib("name",attribs
);
105 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib
,parser
->line
);
108 asx_free_attribs(attribs
);
112 if (!strcasecmp(element
, "keybindings")) {
113 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
114 char *parent_bindings
;
115 cmd_bindings
= realloc(cmd_bindings
,
116 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
117 for (i
= 0; i
< cmd_bindings_num
; ++i
)
118 if (cmd_bindings
[i
].parent
)
119 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
120 bindings
= &cmd_bindings
[cmd_bindings_num
];
121 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
122 bindings
->name
= name
;
123 parent_bindings
= asx_get_attrib("parent",attribs
);
124 if (parent_bindings
) {
125 bindings
->parent
= get_cmd_bindings(parent_bindings
);
126 free(parent_bindings
);
129 asx_free_attribs(attribs
);
135 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
137 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,
140 asx_parser_free(parser
);
145 if (!strcasecmp(element
, "binding")) {
146 key
= asx_get_attrib("key",attribs
);
147 cmd
= asx_get_attrib("cmd",attribs
);
148 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
149 keycode
&= ~MP_NO_REPEAT_KEY
;
150 mp_msg(MSGT_GLOBAL
,MSGL_V
,
151 "[libmenu] got keybinding element %d %s=>[%s].\n",
152 keycode
, key
, cmd
? cmd
: "");
153 bindings
->bindings
= realloc(bindings
->bindings
,
154 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
155 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
156 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
157 ++bindings
->binding_num
;
164 asx_free_attribs(attribs
);
172 // Try to find this menu type in our list
173 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
174 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
175 minfo
= menu_info_list
[i
];
179 // Got it : add this to our list
181 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
182 menu_list
[menu_count
].name
= name
;
183 menu_list
[menu_count
].type
= minfo
;
184 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
185 menu_list
[menu_count
].args
= body
;
187 for(i
= 0 ; attribs
[2*i
] ; i
++) {
188 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
189 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
190 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_BadAttrib
,attribs
[2*i
],attribs
[2*i
+1],
194 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
196 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnknownMenuType
,element
,parser
->line
);
202 asx_free_attribs(attribs
);
208 /// This will build the menu_defs list from the cfg file
209 #define BUF_STEP 1024
211 #define BUF_MAX BUF_STEP*1024
212 int menu_init(struct MPContext
*mpctx
, char* cfg_file
) {
214 int bl
= BUF_STEP
, br
= 0;
216 #ifndef HAVE_FREETYPE
220 fd
= open(cfg_file
, O_RDONLY
);
222 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_CantOpenConfigFile
,cfg_file
);
228 if(bl
- br
< BUF_MIN
) {
230 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsTooBig
,BUF_MAX
/1024);
236 buffer
= realloc(buffer
,bl
);
238 r
= read(fd
,buffer
+br
,bl
-br
);
243 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsEmpty
);
251 f
= menu_parse_config(buffer
);
256 // Destroy all this stuff
257 void menu_uninit(void) {
259 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
260 free(menu_list
[i
].name
);
261 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
262 if(menu_list
[i
].args
) free(menu_list
[i
].args
);
266 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
267 free(cmd_bindings
[i
].name
);
268 while(cmd_bindings
[i
].binding_num
> 0)
269 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
270 free(cmd_bindings
[i
].bindings
);
275 /// Default read_key function
276 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
278 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
280 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
282 bindings
= get_cmd_bindings("default");
284 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
285 if (bindings
->bindings
[i
].key
== cmd
) {
286 if (bindings
->bindings
[i
].cmd
)
287 mp_input_parse_and_queue_cmds(bindings
->bindings
[i
].cmd
);
291 bindings
= bindings
->parent
;
296 menu_t
* menu_open(char *name
) {
300 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
301 if(strcmp(name
,menu_list
[i
].name
) == 0)
304 if(menu_list
[i
].name
== NULL
) {
305 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuNotFound
,name
);
308 m
= calloc(1,sizeof(menu_t
));
309 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
310 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
312 m
->type
= &menu_list
[i
];
313 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
316 m_struct_free(m
->priv_st
,m
->priv
);
318 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuInitFailed
,name
);
322 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
323 if(menu
->show
&& menu
->draw
)
324 menu
->draw(menu
,mpi
);
327 void menu_read_cmd(menu_t
* menu
,int cmd
) {
329 menu
->read_cmd(menu
,cmd
);
332 void menu_close(menu_t
* menu
) {
336 m_struct_free(menu
->priv_st
,menu
->priv
);
340 void menu_read_key(menu_t
* menu
,int cmd
) {
342 menu
->read_key(menu
,cmd
);
344 menu_dflt_read_key(menu
,cmd
);
347 ///////////////////////////// Helpers ////////////////////////////////////
349 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
351 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
355 return vo_draw_alpha_rgb15
;
358 return vo_draw_alpha_rgb16
;
361 return vo_draw_alpha_rgb24
;
364 return vo_draw_alpha_rgb32
;
372 return vo_draw_alpha_yv12
;
374 return vo_draw_alpha_yuy2
;
376 return vo_draw_alpha_uyvy
;
382 // return the real height of a char:
383 static inline int get_height(int c
,int h
){
385 if ((font
=vo_font
->font
[c
])>=0)
386 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
390 static void render_txt(char *txt
)
393 int c
= utf8_get_char((const char**)&txt
);
394 render_one_glyph(vo_font
, c
);
399 #include <fribidi/fribidi.h>
400 #include "libavutil/common.h"
401 char *menu_fribidi_charset
= NULL
;
402 int menu_flip_hebrew
= 0;
403 int menu_fribidi_flip_commas
= 0;
405 static char *menu_fribidi(char *txt
)
407 static int char_set_num
= -1;
408 static FriBidiChar
*logical
, *visual
;
409 static size_t buffer_size
= 1024;
410 static char *outputstr
;
412 FriBidiCharType base
;
413 fribidi_boolean log2vis
;
416 if (menu_flip_hebrew
) {
418 if (char_set_num
== -1) {
419 fribidi_set_mirroring (1);
420 fribidi_set_reorder_nsm (0);
421 char_set_num
= fribidi_parse_charset("UTF-8");
422 buffer_size
= FFMAX(1024,len
+1);
423 logical
= malloc(buffer_size
);
424 visual
= malloc(buffer_size
);
425 outputstr
= malloc(buffer_size
);
426 } else if (len
+1 > buffer_size
) {
428 logical
= realloc(logical
, buffer_size
);
429 visual
= realloc(visual
, buffer_size
);
430 outputstr
= realloc(outputstr
, buffer_size
);
432 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
433 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
434 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
436 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
437 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
445 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
446 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
450 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
455 txt
= menu_fribidi(txt
);
460 int c
=utf8_get_char((const char**)&txt
);
461 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
462 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
463 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
464 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
465 vo_font
->pic_a
[font
]->w
,
466 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
468 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
473 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
474 int x
, int y
,int w
, int h
,
475 int vspace
, int warp
, int align
, int anchor
) {
478 int sx
, xmin
, xmax
, xmid
, xrmin
;
481 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
484 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
489 txt
= menu_fribidi(txt
);
493 if(x
> mpi
->w
|| y
> mpi
->h
)
496 if(anchor
& MENU_TEXT_VCENTER
) {
497 if(h
<= 0) h
= mpi
->h
;
500 } else if(anchor
& MENU_TEXT_BOT
) {
501 if(h
<= 0) h
= mpi
->h
- y
;
505 if(h
<= 0) h
= mpi
->h
- y
;
510 if(anchor
& MENU_TEXT_HCENTER
) {
511 if(w
<= 0) w
= mpi
->w
;
514 } else if(anchor
& MENU_TEXT_RIGHT
) {
515 if(w
<= 0) w
= mpi
->w
-x
;
519 if(w
<= 0) w
= mpi
->w
-x
;
524 // How many space do we need to draw this ?
525 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
527 // Find the first line
528 if(align
& MENU_TEXT_VCENTER
)
529 sy
= ymin
+ ((h
- need_h
)/2);
530 else if(align
& MENU_TEXT_BOT
)
531 sy
= ymax
- need_h
- 1;
536 // Find the first col
537 if(align
& MENU_TEXT_HCENTER
)
538 sx
= xmin
+ ((w
- need_w
)/2);
539 else if(align
& MENU_TEXT_RIGHT
)
543 xmid
= xmin
+ (xmax
- xmin
) / 2;
545 // Clamp the bb to the mpi size
546 if(ymin
< 0) ymin
= 0;
547 if(xmin
< 0) xmin
= 0;
548 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
549 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
551 // Jump some the beginnig text if needed
552 while(sy
< ymin
&& *txt
) {
553 int c
=utf8_get_char((const char**)&txt
);
554 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
556 sy
+= vo_font
->height
+ vspace
;
557 if(c
== '\n') continue;
559 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
561 if(*txt
== '\0') // Nothing left to draw
564 while(sy
< ymax
&& *txt
) {
565 char* line_end
= NULL
;
568 if(txt
[0] == '\n') { // New line
569 sy
+= vo_font
->height
+ vspace
;
574 // Get the length and end of this line
575 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
576 unsigned char c
= txt
[n
];
577 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
578 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
581 ll
-= vo_font
->charspace
;
584 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
587 if(align
& MENU_TEXT_HCENTER
) {
589 // Find the middle point
590 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
591 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
592 if(ll
- vo_font
->charspace
> mid
) break;
594 ll
-= vo_font
->charspace
;
595 sx
= xmid
+ mid
- ll
;
596 } else// MENU_TEXT_RIGHT)
597 sx
= xmax
+ vo_font
->charspace
;
599 // We are after the start point -> go back
601 for(n
-- ; n
<= 0 ; n
--) {
602 unsigned char c
= txt
[n
];
603 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
604 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
606 } else { // We are before the start point -> go forward
607 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
608 unsigned char c
= txt
[n
];
609 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
612 txt
= &txt
[n
]; // Jump to the new start char
614 if(align
& MENU_TEXT_HCENTER
)
620 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
621 unsigned char c
= txt
[n
];
622 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
626 while(sx
< xmax
&& txt
!= line_end
) {
627 int c
=utf8_get_char((const char**)&txt
);
628 font
= vo_font
->font
[c
];
630 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
631 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
632 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
633 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
634 cs
* vo_font
->pic_a
[font
]->w
,
635 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
636 cs
* vo_font
->pic_a
[font
]->w
,
637 vo_font
->pic_a
[font
]->w
,
638 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
641 //printf("Can't draw '%c'\n",c);
643 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
646 if(txt
[0] == '\0') break;
647 sy
+= vo_font
->height
+ vspace
;
651 int menu_text_length(char* txt
) {
655 int c
=utf8_get_char((const char**)&txt
);
656 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
658 return l
- vo_font
->charspace
;
661 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
667 int c
=utf8_get_char((const char**)&txt
);
668 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
669 i
-= vo_font
->charspace
;
674 if(c
== '\n') continue;
676 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
679 i
-= vo_font
->charspace
;
684 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
688 int menu_text_num_lines(char* txt
, int max_width
) {
692 int c
=utf8_get_char((const char**)&txt
);
693 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
696 if(c
== '\n') continue;
698 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
703 char* menu_text_get_next_line(char* txt
, int max_width
) {
707 int c
=utf8_get_char((const char**)&txt
);
712 i
+= vo_font
->width
[c
];
715 i
+= vo_font
->charspace
;
721 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
722 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
726 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
730 if(x
> mpi
->w
|| y
> mpi
->h
) return;
732 if(x
< 0) w
+= x
, x
= 0;
733 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
734 if(y
< 0) h
+= y
, y
= 0;
735 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
737 g
= ((256-alpha
)*grey
)>>8;
741 int stride
= (w
+7)&(~7); // round to 8
742 char pic
[stride
*h
],pic_alpha
[stride
*h
];
743 memset(pic
,g
,stride
*h
);
744 memset(pic_alpha
,alpha
,stride
*h
);
745 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
746 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),