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 extern menu_info_t menu_info_dvbsel
;
36 menu_info_t
* menu_info_list
[] = {
43 #ifdef HAS_DVBIN_SUPPORT
50 typedef struct key_cmd_s
{
55 typedef struct menu_cmd_bindings_s
{
59 struct menu_cmd_bindings_s
*parent
;
60 } menu_cmd_bindings_t
;
69 double menu_mouse_x
= -1.0;
70 double menu_mouse_y
= -1.0;
71 int menu_mouse_pos_updated
= 0;
73 static struct MPContext
*menu_ctx
= NULL
;
74 static struct m_config
*menu_mconfig
= NULL
;
75 static struct input_ctx
*menu_input
= NULL
;
76 static menu_def_t
* menu_list
= NULL
;
77 static int menu_count
= 0;
78 static menu_cmd_bindings_t
*cmd_bindings
= NULL
;
79 static int cmd_bindings_num
= 0;
82 menu_cmd_bindings_t
*get_cmd_bindings(const char *name
) {
84 for (i
= 0; i
< cmd_bindings_num
; ++i
)
85 if (!strcasecmp(cmd_bindings
[i
].name
, name
))
86 return &cmd_bindings
[i
];
90 static int menu_parse_config(char* buffer
, struct m_config
*mconfig
)
92 char *element
,*body
, **attribs
, *name
;
93 menu_info_t
* minfo
= NULL
;
95 ASX_Parser_t
* parser
= asx_parser_new(mconfig
);
98 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
100 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
101 asx_parser_free(parser
);
104 asx_parser_free(parser
);
108 name
= asx_get_attrib("name",attribs
);
110 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib
,parser
->line
);
113 asx_free_attribs(attribs
);
117 if (!strcasecmp(element
, "keybindings")) {
118 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
119 char *parent_bindings
;
120 cmd_bindings
= realloc(cmd_bindings
,
121 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
122 for (i
= 0; i
< cmd_bindings_num
; ++i
)
123 if (cmd_bindings
[i
].parent
)
124 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
125 bindings
= &cmd_bindings
[cmd_bindings_num
];
126 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
127 bindings
->name
= name
;
128 parent_bindings
= asx_get_attrib("parent",attribs
);
129 if (parent_bindings
) {
130 bindings
->parent
= get_cmd_bindings(parent_bindings
);
131 free(parent_bindings
);
134 asx_free_attribs(attribs
);
140 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
142 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,
145 asx_parser_free(parser
);
150 if (!strcasecmp(element
, "binding")) {
151 key
= asx_get_attrib("key",attribs
);
152 cmd
= asx_get_attrib("cmd",attribs
);
153 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
154 keycode
&= ~MP_NO_REPEAT_KEY
;
155 mp_msg(MSGT_GLOBAL
,MSGL_V
,
156 "[libmenu] got keybinding element %d %s=>[%s].\n",
157 keycode
, key
, cmd
? cmd
: "");
158 bindings
->bindings
= realloc(bindings
->bindings
,
159 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
160 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
161 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
162 ++bindings
->binding_num
;
169 asx_free_attribs(attribs
);
177 // Try to find this menu type in our list
178 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
179 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
180 minfo
= menu_info_list
[i
];
184 // Got it : add this to our list
186 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
187 menu_list
[menu_count
].name
= name
;
188 menu_list
[menu_count
].type
= minfo
;
189 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
190 menu_list
[menu_count
].args
= body
;
192 for(i
= 0 ; attribs
[2*i
] ; i
++) {
193 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
194 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
195 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_BadAttrib
,attribs
[2*i
],attribs
[2*i
+1],
199 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
201 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnknownMenuType
,element
,parser
->line
);
207 asx_free_attribs(attribs
);
213 /// This will build the menu_defs list from the cfg file
214 #define BUF_STEP 1024
216 #define BUF_MAX BUF_STEP*1024
217 int menu_init(struct MPContext
*mpctx
, struct m_config
*mconfig
,
218 struct input_ctx
*input_ctx
, char* cfg_file
)
221 int bl
= BUF_STEP
, br
= 0;
223 #ifndef HAVE_FREETYPE
227 fd
= open(cfg_file
, O_RDONLY
);
229 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_CantOpenConfigFile
,cfg_file
);
235 if(bl
- br
< BUF_MIN
) {
237 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsTooBig
,BUF_MAX
/1024);
243 buffer
= realloc(buffer
,bl
);
245 r
= read(fd
,buffer
+br
,bl
-br
);
250 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsEmpty
);
258 menu_mconfig
= mconfig
;
259 menu_input
= input_ctx
;
260 f
= menu_parse_config(buffer
, mconfig
);
265 // Destroy all this stuff
266 void menu_uninit(void) {
268 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
269 free(menu_list
[i
].name
);
270 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
271 if(menu_list
[i
].args
) free(menu_list
[i
].args
);
275 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
276 free(cmd_bindings
[i
].name
);
277 while(cmd_bindings
[i
].binding_num
> 0)
278 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
279 free(cmd_bindings
[i
].bindings
);
284 /// Default read_key function
285 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
287 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
289 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
291 bindings
= get_cmd_bindings("default");
293 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
294 if (bindings
->bindings
[i
].key
== cmd
) {
295 if (bindings
->bindings
[i
].cmd
)
296 mp_input_parse_and_queue_cmds(menu
->input_ctx
,
297 bindings
->bindings
[i
].cmd
);
301 bindings
= bindings
->parent
;
306 menu_t
* menu_open(char *name
) {
310 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
311 if(strcmp(name
,menu_list
[i
].name
) == 0)
314 if(menu_list
[i
].name
== NULL
) {
315 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuNotFound
,name
);
318 m
= calloc(1,sizeof(menu_t
));
319 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
320 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
322 m
->mconfig
= menu_mconfig
;
323 m
->input_ctx
= menu_input
;
324 m
->type
= &menu_list
[i
];
325 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
328 m_struct_free(m
->priv_st
,m
->priv
);
330 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuInitFailed
,name
);
334 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
335 if(menu
->show
&& menu
->draw
)
336 menu
->draw(menu
,mpi
);
339 void menu_update_mouse_pos(double x
, double y
) {
342 menu_mouse_pos_updated
= 1;
345 void menu_read_cmd(menu_t
* menu
,int cmd
) {
347 menu
->read_cmd(menu
,cmd
);
350 void menu_close(menu_t
* menu
) {
354 m_struct_free(menu
->priv_st
,menu
->priv
);
358 int menu_read_key(menu_t
* menu
,int cmd
) {
360 return menu
->read_key(menu
,cmd
);
362 return menu_dflt_read_key(menu
,cmd
);
365 ///////////////////////////// Helpers ////////////////////////////////////
367 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
369 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
373 return vo_draw_alpha_rgb15
;
376 return vo_draw_alpha_rgb16
;
379 return vo_draw_alpha_rgb24
;
382 return vo_draw_alpha_rgb32
;
390 return vo_draw_alpha_yv12
;
392 return vo_draw_alpha_yuy2
;
394 return vo_draw_alpha_uyvy
;
400 // return the real height of a char:
401 static inline int get_height(int c
,int h
){
403 if ((font
=vo_font
->font
[c
])>=0)
404 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
408 static void render_txt(char *txt
)
411 int c
= utf8_get_char((const char**)&txt
);
412 render_one_glyph(vo_font
, c
);
416 #ifdef CONFIG_FRIBIDI
417 #include <fribidi/fribidi.h>
418 #include "libavutil/common.h"
419 char *menu_fribidi_charset
= NULL
;
420 int menu_flip_hebrew
= 0;
421 int menu_fribidi_flip_commas
= 0;
423 static char *menu_fribidi(char *txt
)
425 static int char_set_num
= -1;
426 static FriBidiChar
*logical
, *visual
;
427 static size_t buffer_size
= 1024;
428 static char *outputstr
;
430 FriBidiCharType base
;
431 fribidi_boolean log2vis
;
434 if (menu_flip_hebrew
) {
436 if (char_set_num
== -1) {
437 fribidi_set_mirroring (1);
438 fribidi_set_reorder_nsm (0);
439 char_set_num
= fribidi_parse_charset("UTF-8");
440 buffer_size
= FFMAX(1024,len
+1);
441 logical
= malloc(buffer_size
);
442 visual
= malloc(buffer_size
);
443 outputstr
= malloc(buffer_size
);
444 } else if (len
+1 > buffer_size
) {
446 logical
= realloc(logical
, buffer_size
);
447 visual
= realloc(visual
, buffer_size
);
448 outputstr
= realloc(outputstr
, buffer_size
);
450 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
451 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
452 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
454 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
455 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
463 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
464 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
468 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
472 #ifdef CONFIG_FRIBIDI
473 txt
= menu_fribidi(txt
);
478 int c
=utf8_get_char((const char**)&txt
);
479 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
480 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
481 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
482 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
483 vo_font
->pic_a
[font
]->w
,
484 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
486 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
491 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
492 int x
, int y
,int w
, int h
,
493 int vspace
, int warp
, int align
, int anchor
) {
496 int sx
, xmin
, xmax
, xmid
, xrmin
;
499 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
502 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
506 #ifdef CONFIG_FRIBIDI
507 txt
= menu_fribidi(txt
);
511 if(x
> mpi
->w
|| y
> mpi
->h
)
514 if(anchor
& MENU_TEXT_VCENTER
) {
515 if(h
<= 0) h
= mpi
->h
;
518 } else if(anchor
& MENU_TEXT_BOT
) {
519 if(h
<= 0) h
= mpi
->h
- y
;
523 if(h
<= 0) h
= mpi
->h
- y
;
528 if(anchor
& MENU_TEXT_HCENTER
) {
529 if(w
<= 0) w
= mpi
->w
;
532 } else if(anchor
& MENU_TEXT_RIGHT
) {
533 if(w
<= 0) w
= mpi
->w
-x
;
537 if(w
<= 0) w
= mpi
->w
-x
;
542 // How many space do we need to draw this ?
543 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
545 // Find the first line
546 if(align
& MENU_TEXT_VCENTER
)
547 sy
= ymin
+ ((h
- need_h
)/2);
548 else if(align
& MENU_TEXT_BOT
)
549 sy
= ymax
- need_h
- 1;
554 // Find the first col
555 if(align
& MENU_TEXT_HCENTER
)
556 sx
= xmin
+ ((w
- need_w
)/2);
557 else if(align
& MENU_TEXT_RIGHT
)
561 xmid
= xmin
+ (xmax
- xmin
) / 2;
563 // Clamp the bb to the mpi size
564 if(ymin
< 0) ymin
= 0;
565 if(xmin
< 0) xmin
= 0;
566 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
567 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
569 // Jump some the beginnig text if needed
570 while(sy
< ymin
&& *txt
) {
571 int c
=utf8_get_char((const char**)&txt
);
572 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
574 sy
+= vo_font
->height
+ vspace
;
575 if(c
== '\n') continue;
577 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
579 if(*txt
== '\0') // Nothing left to draw
582 while(sy
< ymax
&& *txt
) {
583 char* line_end
= NULL
;
586 if(txt
[0] == '\n') { // New line
587 sy
+= vo_font
->height
+ vspace
;
592 // Get the length and end of this line
593 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
594 unsigned char c
= txt
[n
];
595 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
596 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
599 ll
-= vo_font
->charspace
;
602 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
605 if(align
& MENU_TEXT_HCENTER
) {
607 // Find the middle point
608 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
609 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
610 if(ll
- vo_font
->charspace
> mid
) break;
612 ll
-= vo_font
->charspace
;
613 sx
= xmid
+ mid
- ll
;
614 } else// MENU_TEXT_RIGHT)
615 sx
= xmax
+ vo_font
->charspace
;
617 // We are after the start point -> go back
619 for(n
-- ; n
<= 0 ; n
--) {
620 unsigned char c
= txt
[n
];
621 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
622 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
624 } else { // We are before the start point -> go forward
625 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
626 unsigned char c
= txt
[n
];
627 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
630 txt
= &txt
[n
]; // Jump to the new start char
632 if(align
& MENU_TEXT_HCENTER
)
638 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
639 unsigned char c
= txt
[n
];
640 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
644 while(sx
< xmax
&& txt
!= line_end
) {
645 int c
=utf8_get_char((const char**)&txt
);
646 font
= vo_font
->font
[c
];
648 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
649 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
650 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
651 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
652 cs
* vo_font
->pic_a
[font
]->w
,
653 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
654 cs
* vo_font
->pic_a
[font
]->w
,
655 vo_font
->pic_a
[font
]->w
,
656 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
659 //printf("Can't draw '%c'\n",c);
661 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
664 if(txt
[0] == '\0') break;
665 sy
+= vo_font
->height
+ vspace
;
669 int menu_text_length(char* txt
) {
673 int c
=utf8_get_char((const char**)&txt
);
674 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
676 return l
- vo_font
->charspace
;
679 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
685 int c
=utf8_get_char((const char**)&txt
);
686 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
687 i
-= vo_font
->charspace
;
692 if(c
== '\n') continue;
694 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
697 i
-= vo_font
->charspace
;
702 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
706 int menu_text_num_lines(char* txt
, int max_width
) {
710 int c
=utf8_get_char((const char**)&txt
);
711 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
714 if(c
== '\n') continue;
716 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
721 char* menu_text_get_next_line(char* txt
, int max_width
) {
725 int c
=utf8_get_char((const char**)&txt
);
730 i
+= vo_font
->width
[c
];
733 i
+= vo_font
->charspace
;
739 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
740 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
744 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
748 if(x
> mpi
->w
|| y
> mpi
->h
) return;
750 if(x
< 0) w
+= x
, x
= 0;
751 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
752 if(y
< 0) h
+= y
, y
= 0;
753 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
755 g
= ((256-alpha
)*grey
)>>8;
759 int stride
= (w
+7)&(~7); // round to 8
760 char pic
[stride
*h
],pic_alpha
[stride
*h
];
761 memset(pic
,g
,stride
*h
);
762 memset(pic_alpha
,alpha
,stride
*h
);
763 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
764 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),