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 double menu_mouse_x
= -1.0;
72 double menu_mouse_y
= -1.0;
73 int menu_mouse_pos_updated
= 0;
75 static struct MPContext
*menu_ctx
= 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
) {
91 char *element
,*body
, **attribs
, *name
;
92 menu_info_t
* minfo
= NULL
;
94 ASX_Parser_t
* parser
= asx_parser_new();
97 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
99 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
100 asx_parser_free(parser
);
103 asx_parser_free(parser
);
107 name
= asx_get_attrib("name",attribs
);
109 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib
,parser
->line
);
112 asx_free_attribs(attribs
);
116 if (!strcasecmp(element
, "keybindings")) {
117 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
118 char *parent_bindings
;
119 cmd_bindings
= realloc(cmd_bindings
,
120 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
121 for (i
= 0; i
< cmd_bindings_num
; ++i
)
122 if (cmd_bindings
[i
].parent
)
123 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
124 bindings
= &cmd_bindings
[cmd_bindings_num
];
125 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
126 bindings
->name
= name
;
127 parent_bindings
= asx_get_attrib("parent",attribs
);
128 if (parent_bindings
) {
129 bindings
->parent
= get_cmd_bindings(parent_bindings
);
130 free(parent_bindings
);
133 asx_free_attribs(attribs
);
139 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
141 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,
144 asx_parser_free(parser
);
149 if (!strcasecmp(element
, "binding")) {
150 key
= asx_get_attrib("key",attribs
);
151 cmd
= asx_get_attrib("cmd",attribs
);
152 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
153 keycode
&= ~MP_NO_REPEAT_KEY
;
154 mp_msg(MSGT_GLOBAL
,MSGL_V
,
155 "[libmenu] got keybinding element %d %s=>[%s].\n",
156 keycode
, key
, cmd
? cmd
: "");
157 bindings
->bindings
= realloc(bindings
->bindings
,
158 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
159 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
160 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
161 ++bindings
->binding_num
;
168 asx_free_attribs(attribs
);
176 // Try to find this menu type in our list
177 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
178 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
179 minfo
= menu_info_list
[i
];
183 // Got it : add this to our list
185 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
186 menu_list
[menu_count
].name
= name
;
187 menu_list
[menu_count
].type
= minfo
;
188 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
189 menu_list
[menu_count
].args
= body
;
191 for(i
= 0 ; attribs
[2*i
] ; i
++) {
192 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
193 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
194 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_BadAttrib
,attribs
[2*i
],attribs
[2*i
+1],
198 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
200 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnknownMenuType
,element
,parser
->line
);
206 asx_free_attribs(attribs
);
212 /// This will build the menu_defs list from the cfg file
213 #define BUF_STEP 1024
215 #define BUF_MAX BUF_STEP*1024
216 int menu_init(struct MPContext
*mpctx
, char* cfg_file
) {
218 int bl
= BUF_STEP
, br
= 0;
220 #ifndef HAVE_FREETYPE
224 fd
= open(cfg_file
, O_RDONLY
);
226 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_CantOpenConfigFile
,cfg_file
);
232 if(bl
- br
< BUF_MIN
) {
234 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsTooBig
,BUF_MAX
/1024);
240 buffer
= realloc(buffer
,bl
);
242 r
= read(fd
,buffer
+br
,bl
-br
);
247 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsEmpty
);
255 f
= menu_parse_config(buffer
);
260 // Destroy all this stuff
261 void menu_uninit(void) {
263 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
264 free(menu_list
[i
].name
);
265 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
266 if(menu_list
[i
].args
) free(menu_list
[i
].args
);
270 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
271 free(cmd_bindings
[i
].name
);
272 while(cmd_bindings
[i
].binding_num
> 0)
273 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
274 free(cmd_bindings
[i
].bindings
);
279 /// Default read_key function
280 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
282 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
284 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
286 bindings
= get_cmd_bindings("default");
288 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
289 if (bindings
->bindings
[i
].key
== cmd
) {
290 if (bindings
->bindings
[i
].cmd
)
291 mp_input_parse_and_queue_cmds(bindings
->bindings
[i
].cmd
);
295 bindings
= bindings
->parent
;
300 menu_t
* menu_open(char *name
) {
304 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
305 if(strcmp(name
,menu_list
[i
].name
) == 0)
308 if(menu_list
[i
].name
== NULL
) {
309 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuNotFound
,name
);
312 m
= calloc(1,sizeof(menu_t
));
313 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
314 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
316 m
->type
= &menu_list
[i
];
317 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
320 m_struct_free(m
->priv_st
,m
->priv
);
322 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuInitFailed
,name
);
326 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
327 if(menu
->show
&& menu
->draw
)
328 menu
->draw(menu
,mpi
);
331 void menu_update_mouse_pos(double x
, double y
) {
334 menu_mouse_pos_updated
= 1;
337 void menu_read_cmd(menu_t
* menu
,int cmd
) {
339 menu
->read_cmd(menu
,cmd
);
342 void menu_close(menu_t
* menu
) {
346 m_struct_free(menu
->priv_st
,menu
->priv
);
350 int menu_read_key(menu_t
* menu
,int cmd
) {
352 return menu
->read_key(menu
,cmd
);
354 return menu_dflt_read_key(menu
,cmd
);
357 ///////////////////////////// Helpers ////////////////////////////////////
359 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
361 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
365 return vo_draw_alpha_rgb15
;
368 return vo_draw_alpha_rgb16
;
371 return vo_draw_alpha_rgb24
;
374 return vo_draw_alpha_rgb32
;
382 return vo_draw_alpha_yv12
;
384 return vo_draw_alpha_yuy2
;
386 return vo_draw_alpha_uyvy
;
392 // return the real height of a char:
393 static inline int get_height(int c
,int h
){
395 if ((font
=vo_font
->font
[c
])>=0)
396 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
400 static void render_txt(char *txt
)
403 int c
= utf8_get_char((const char**)&txt
);
404 render_one_glyph(vo_font
, c
);
409 #include <fribidi/fribidi.h>
410 #include "libavutil/common.h"
411 char *menu_fribidi_charset
= NULL
;
412 int menu_flip_hebrew
= 0;
413 int menu_fribidi_flip_commas
= 0;
415 static char *menu_fribidi(char *txt
)
417 static int char_set_num
= -1;
418 static FriBidiChar
*logical
, *visual
;
419 static size_t buffer_size
= 1024;
420 static char *outputstr
;
422 FriBidiCharType base
;
423 fribidi_boolean log2vis
;
426 if (menu_flip_hebrew
) {
428 if (char_set_num
== -1) {
429 fribidi_set_mirroring (1);
430 fribidi_set_reorder_nsm (0);
431 char_set_num
= fribidi_parse_charset("UTF-8");
432 buffer_size
= FFMAX(1024,len
+1);
433 logical
= malloc(buffer_size
);
434 visual
= malloc(buffer_size
);
435 outputstr
= malloc(buffer_size
);
436 } else if (len
+1 > buffer_size
) {
438 logical
= realloc(logical
, buffer_size
);
439 visual
= realloc(visual
, buffer_size
);
440 outputstr
= realloc(outputstr
, buffer_size
);
442 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
443 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
444 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
446 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
447 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
455 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
456 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
460 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
465 txt
= menu_fribidi(txt
);
470 int c
=utf8_get_char((const char**)&txt
);
471 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
472 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
473 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
474 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
475 vo_font
->pic_a
[font
]->w
,
476 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
478 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
483 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
484 int x
, int y
,int w
, int h
,
485 int vspace
, int warp
, int align
, int anchor
) {
488 int sx
, xmin
, xmax
, xmid
, xrmin
;
491 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
494 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
499 txt
= menu_fribidi(txt
);
503 if(x
> mpi
->w
|| y
> mpi
->h
)
506 if(anchor
& MENU_TEXT_VCENTER
) {
507 if(h
<= 0) h
= mpi
->h
;
510 } else if(anchor
& MENU_TEXT_BOT
) {
511 if(h
<= 0) h
= mpi
->h
- y
;
515 if(h
<= 0) h
= mpi
->h
- y
;
520 if(anchor
& MENU_TEXT_HCENTER
) {
521 if(w
<= 0) w
= mpi
->w
;
524 } else if(anchor
& MENU_TEXT_RIGHT
) {
525 if(w
<= 0) w
= mpi
->w
-x
;
529 if(w
<= 0) w
= mpi
->w
-x
;
534 // How many space do we need to draw this ?
535 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
537 // Find the first line
538 if(align
& MENU_TEXT_VCENTER
)
539 sy
= ymin
+ ((h
- need_h
)/2);
540 else if(align
& MENU_TEXT_BOT
)
541 sy
= ymax
- need_h
- 1;
546 // Find the first col
547 if(align
& MENU_TEXT_HCENTER
)
548 sx
= xmin
+ ((w
- need_w
)/2);
549 else if(align
& MENU_TEXT_RIGHT
)
553 xmid
= xmin
+ (xmax
- xmin
) / 2;
555 // Clamp the bb to the mpi size
556 if(ymin
< 0) ymin
= 0;
557 if(xmin
< 0) xmin
= 0;
558 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
559 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
561 // Jump some the beginnig text if needed
562 while(sy
< ymin
&& *txt
) {
563 int c
=utf8_get_char((const char**)&txt
);
564 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
566 sy
+= vo_font
->height
+ vspace
;
567 if(c
== '\n') continue;
569 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
571 if(*txt
== '\0') // Nothing left to draw
574 while(sy
< ymax
&& *txt
) {
575 char* line_end
= NULL
;
578 if(txt
[0] == '\n') { // New line
579 sy
+= vo_font
->height
+ vspace
;
584 // Get the length and end of this line
585 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
586 unsigned char c
= txt
[n
];
587 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
588 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
591 ll
-= vo_font
->charspace
;
594 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
597 if(align
& MENU_TEXT_HCENTER
) {
599 // Find the middle point
600 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
601 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
602 if(ll
- vo_font
->charspace
> mid
) break;
604 ll
-= vo_font
->charspace
;
605 sx
= xmid
+ mid
- ll
;
606 } else// MENU_TEXT_RIGHT)
607 sx
= xmax
+ vo_font
->charspace
;
609 // We are after the start point -> go back
611 for(n
-- ; n
<= 0 ; n
--) {
612 unsigned char c
= txt
[n
];
613 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
614 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
616 } else { // We are before the start point -> go forward
617 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
618 unsigned char c
= txt
[n
];
619 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
622 txt
= &txt
[n
]; // Jump to the new start char
624 if(align
& MENU_TEXT_HCENTER
)
630 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
631 unsigned char c
= txt
[n
];
632 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
636 while(sx
< xmax
&& txt
!= line_end
) {
637 int c
=utf8_get_char((const char**)&txt
);
638 font
= vo_font
->font
[c
];
640 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
641 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
642 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
643 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
644 cs
* vo_font
->pic_a
[font
]->w
,
645 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
646 cs
* vo_font
->pic_a
[font
]->w
,
647 vo_font
->pic_a
[font
]->w
,
648 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
651 //printf("Can't draw '%c'\n",c);
653 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
656 if(txt
[0] == '\0') break;
657 sy
+= vo_font
->height
+ vspace
;
661 int menu_text_length(char* txt
) {
665 int c
=utf8_get_char((const char**)&txt
);
666 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
668 return l
- vo_font
->charspace
;
671 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
677 int c
=utf8_get_char((const char**)&txt
);
678 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
679 i
-= vo_font
->charspace
;
684 if(c
== '\n') continue;
686 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
689 i
-= vo_font
->charspace
;
694 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
698 int menu_text_num_lines(char* txt
, int max_width
) {
702 int c
=utf8_get_char((const char**)&txt
);
703 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
706 if(c
== '\n') continue;
708 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
713 char* menu_text_get_next_line(char* txt
, int max_width
) {
717 int c
=utf8_get_char((const char**)&txt
);
722 i
+= vo_font
->width
[c
];
725 i
+= vo_font
->charspace
;
731 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
732 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
736 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
740 if(x
> mpi
->w
|| y
> mpi
->h
) return;
742 if(x
< 0) w
+= x
, x
= 0;
743 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
744 if(y
< 0) h
+= y
, y
= 0;
745 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
747 g
= ((256-alpha
)*grey
)>>8;
751 int stride
= (w
+7)&(~7); // round to 8
752 char pic
[stride
*h
],pic_alpha
[stride
*h
];
753 memset(pic
,g
,stride
*h
);
754 memset(pic_alpha
,alpha
,stride
*h
);
755 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
756 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),