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
[] = {
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 menu_def_t
* menu_list
= NULL
;
75 static int menu_count
= 0;
76 static menu_cmd_bindings_t
*cmd_bindings
= NULL
;
77 static int cmd_bindings_num
= 0;
80 menu_cmd_bindings_t
*get_cmd_bindings(const char *name
) {
82 for (i
= 0; i
< cmd_bindings_num
; ++i
)
83 if (!strcasecmp(cmd_bindings
[i
].name
, name
))
84 return &cmd_bindings
[i
];
88 static int menu_parse_config(char* buffer
) {
89 char *element
,*body
, **attribs
, *name
;
90 menu_info_t
* minfo
= NULL
;
92 ASX_Parser_t
* parser
= asx_parser_new();
95 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
97 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
98 asx_parser_free(parser
);
101 asx_parser_free(parser
);
105 name
= asx_get_attrib("name",attribs
);
107 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib
,parser
->line
);
110 asx_free_attribs(attribs
);
114 if (!strcasecmp(element
, "keybindings")) {
115 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
116 char *parent_bindings
;
117 cmd_bindings
= realloc(cmd_bindings
,
118 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
119 for (i
= 0; i
< cmd_bindings_num
; ++i
)
120 if (cmd_bindings
[i
].parent
)
121 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
122 bindings
= &cmd_bindings
[cmd_bindings_num
];
123 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
124 bindings
->name
= name
;
125 parent_bindings
= asx_get_attrib("parent",attribs
);
126 if (parent_bindings
) {
127 bindings
->parent
= get_cmd_bindings(parent_bindings
);
128 free(parent_bindings
);
131 asx_free_attribs(attribs
);
137 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
139 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,
142 asx_parser_free(parser
);
147 if (!strcasecmp(element
, "binding")) {
148 key
= asx_get_attrib("key",attribs
);
149 cmd
= asx_get_attrib("cmd",attribs
);
150 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
151 keycode
&= ~MP_NO_REPEAT_KEY
;
152 mp_msg(MSGT_GLOBAL
,MSGL_V
,
153 "[libmenu] got keybinding element %d %s=>[%s].\n",
154 keycode
, key
, cmd
? cmd
: "");
155 bindings
->bindings
= realloc(bindings
->bindings
,
156 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
157 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
158 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
159 ++bindings
->binding_num
;
166 asx_free_attribs(attribs
);
174 // Try to find this menu type in our list
175 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
176 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
177 minfo
= menu_info_list
[i
];
181 // Got it : add this to our list
183 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
184 menu_list
[menu_count
].name
= name
;
185 menu_list
[menu_count
].type
= minfo
;
186 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
187 menu_list
[menu_count
].args
= body
;
189 for(i
= 0 ; attribs
[2*i
] ; i
++) {
190 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
191 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
192 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_BadAttrib
,attribs
[2*i
],attribs
[2*i
+1],
196 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
198 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnknownMenuType
,element
,parser
->line
);
204 asx_free_attribs(attribs
);
210 /// This will build the menu_defs list from the cfg file
211 #define BUF_STEP 1024
213 #define BUF_MAX BUF_STEP*1024
214 int menu_init(struct MPContext
*mpctx
, char* cfg_file
) {
216 int bl
= BUF_STEP
, br
= 0;
218 #ifndef CONFIG_FREETYPE
222 fd
= open(cfg_file
, O_RDONLY
);
224 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_CantOpenConfigFile
,cfg_file
);
230 if(bl
- br
< BUF_MIN
) {
232 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsTooBig
,BUF_MAX
/1024);
238 buffer
= realloc(buffer
,bl
);
240 r
= read(fd
,buffer
+br
,bl
-br
);
245 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsEmpty
);
253 f
= menu_parse_config(buffer
);
258 // Destroy all this stuff
259 void menu_uninit(void) {
261 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
262 free(menu_list
[i
].name
);
263 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
264 if(menu_list
[i
].args
) free(menu_list
[i
].args
);
268 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
269 free(cmd_bindings
[i
].name
);
270 while(cmd_bindings
[i
].binding_num
> 0)
271 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
272 free(cmd_bindings
[i
].bindings
);
277 /// Default read_key function
278 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
280 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
282 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
284 bindings
= get_cmd_bindings("default");
286 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
287 if (bindings
->bindings
[i
].key
== cmd
) {
288 if (bindings
->bindings
[i
].cmd
)
289 mp_input_parse_and_queue_cmds(bindings
->bindings
[i
].cmd
);
293 bindings
= bindings
->parent
;
298 menu_t
* menu_open(char *name
) {
302 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
303 if(strcmp(name
,menu_list
[i
].name
) == 0)
306 if(menu_list
[i
].name
== NULL
) {
307 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuNotFound
,name
);
310 m
= calloc(1,sizeof(menu_t
));
311 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
312 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
314 m
->type
= &menu_list
[i
];
315 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
318 m_struct_free(m
->priv_st
,m
->priv
);
320 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuInitFailed
,name
);
324 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
325 if(menu
->show
&& menu
->draw
)
326 menu
->draw(menu
,mpi
);
329 void menu_update_mouse_pos(double x
, double y
) {
332 menu_mouse_pos_updated
= 1;
335 void menu_read_cmd(menu_t
* menu
,int cmd
) {
337 menu
->read_cmd(menu
,cmd
);
340 void menu_close(menu_t
* menu
) {
344 m_struct_free(menu
->priv_st
,menu
->priv
);
348 int menu_read_key(menu_t
* menu
,int cmd
) {
350 return menu
->read_key(menu
,cmd
);
352 return menu_dflt_read_key(menu
,cmd
);
355 ///////////////////////////// Helpers ////////////////////////////////////
357 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
359 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
363 return vo_draw_alpha_rgb15
;
366 return vo_draw_alpha_rgb16
;
369 return vo_draw_alpha_rgb24
;
372 return vo_draw_alpha_rgb32
;
380 return vo_draw_alpha_yv12
;
382 return vo_draw_alpha_yuy2
;
384 return vo_draw_alpha_uyvy
;
390 // return the real height of a char:
391 static inline int get_height(int c
,int h
){
393 if ((font
=vo_font
->font
[c
])>=0)
394 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
398 static void render_txt(char *txt
)
401 int c
= utf8_get_char((const char**)&txt
);
402 render_one_glyph(vo_font
, c
);
406 #ifdef CONFIG_FRIBIDI
407 #include <fribidi/fribidi.h>
408 #include "libavutil/common.h"
409 char *menu_fribidi_charset
= NULL
;
410 int menu_flip_hebrew
= 0;
411 int menu_fribidi_flip_commas
= 0;
413 static char *menu_fribidi(char *txt
)
415 static int char_set_num
= -1;
416 static FriBidiChar
*logical
, *visual
;
417 static size_t buffer_size
= 1024;
418 static char *outputstr
;
420 FriBidiCharType base
;
421 fribidi_boolean log2vis
;
424 if (menu_flip_hebrew
) {
426 if (char_set_num
== -1) {
427 fribidi_set_mirroring (1);
428 fribidi_set_reorder_nsm (0);
429 char_set_num
= fribidi_parse_charset("UTF-8");
430 buffer_size
= FFMAX(1024,len
+1);
431 logical
= malloc(buffer_size
);
432 visual
= malloc(buffer_size
);
433 outputstr
= malloc(buffer_size
);
434 } else if (len
+1 > buffer_size
) {
436 logical
= realloc(logical
, buffer_size
);
437 visual
= realloc(visual
, buffer_size
);
438 outputstr
= realloc(outputstr
, buffer_size
);
440 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
441 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
442 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
444 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
445 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
453 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
454 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
458 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
462 #ifdef CONFIG_FRIBIDI
463 txt
= menu_fribidi(txt
);
468 int c
=utf8_get_char((const char**)&txt
);
469 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
470 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
471 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
472 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
473 vo_font
->pic_a
[font
]->w
,
474 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
476 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
481 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
482 int x
, int y
,int w
, int h
,
483 int vspace
, int warp
, int align
, int anchor
) {
486 int sx
, xmin
, xmax
, xmid
, xrmin
;
489 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
492 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
496 #ifdef CONFIG_FRIBIDI
497 txt
= menu_fribidi(txt
);
501 if(x
> mpi
->w
|| y
> mpi
->h
)
504 if(anchor
& MENU_TEXT_VCENTER
) {
505 if(h
<= 0) h
= mpi
->h
;
508 } else if(anchor
& MENU_TEXT_BOT
) {
509 if(h
<= 0) h
= mpi
->h
- y
;
513 if(h
<= 0) h
= mpi
->h
- y
;
518 if(anchor
& MENU_TEXT_HCENTER
) {
519 if(w
<= 0) w
= mpi
->w
;
522 } else if(anchor
& MENU_TEXT_RIGHT
) {
523 if(w
<= 0) w
= mpi
->w
-x
;
527 if(w
<= 0) w
= mpi
->w
-x
;
532 // How many space do we need to draw this ?
533 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
535 // Find the first line
536 if(align
& MENU_TEXT_VCENTER
)
537 sy
= ymin
+ ((h
- need_h
)/2);
538 else if(align
& MENU_TEXT_BOT
)
539 sy
= ymax
- need_h
- 1;
544 // Find the first col
545 if(align
& MENU_TEXT_HCENTER
)
546 sx
= xmin
+ ((w
- need_w
)/2);
547 else if(align
& MENU_TEXT_RIGHT
)
551 xmid
= xmin
+ (xmax
- xmin
) / 2;
553 // Clamp the bb to the mpi size
554 if(ymin
< 0) ymin
= 0;
555 if(xmin
< 0) xmin
= 0;
556 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
557 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
559 // Jump some the beginnig text if needed
560 while(sy
< ymin
&& *txt
) {
561 int c
=utf8_get_char((const char**)&txt
);
562 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
564 sy
+= vo_font
->height
+ vspace
;
565 if(c
== '\n') continue;
567 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
569 if(*txt
== '\0') // Nothing left to draw
572 while(sy
< ymax
&& *txt
) {
573 char* line_end
= NULL
;
576 if(txt
[0] == '\n') { // New line
577 sy
+= vo_font
->height
+ vspace
;
582 // Get the length and end of this line
583 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
584 unsigned char c
= txt
[n
];
585 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
586 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
589 ll
-= vo_font
->charspace
;
592 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
595 if(align
& MENU_TEXT_HCENTER
) {
597 // Find the middle point
598 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
599 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
600 if(ll
- vo_font
->charspace
> mid
) break;
602 ll
-= vo_font
->charspace
;
603 sx
= xmid
+ mid
- ll
;
604 } else// MENU_TEXT_RIGHT)
605 sx
= xmax
+ vo_font
->charspace
;
607 // We are after the start point -> go back
609 for(n
-- ; n
<= 0 ; n
--) {
610 unsigned char c
= txt
[n
];
611 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
612 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
614 } else { // We are before the start point -> go forward
615 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
616 unsigned char c
= txt
[n
];
617 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
620 txt
= &txt
[n
]; // Jump to the new start char
622 if(align
& MENU_TEXT_HCENTER
)
628 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
629 unsigned char c
= txt
[n
];
630 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
634 while(sx
< xmax
&& txt
!= line_end
) {
635 int c
=utf8_get_char((const char**)&txt
);
636 font
= vo_font
->font
[c
];
638 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
639 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
640 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
641 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
642 cs
* vo_font
->pic_a
[font
]->w
,
643 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
644 cs
* vo_font
->pic_a
[font
]->w
,
645 vo_font
->pic_a
[font
]->w
,
646 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
649 //printf("Can't draw '%c'\n",c);
651 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
654 if(txt
[0] == '\0') break;
655 sy
+= vo_font
->height
+ vspace
;
659 int menu_text_length(char* txt
) {
663 int c
=utf8_get_char((const char**)&txt
);
664 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
666 return l
- vo_font
->charspace
;
669 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
675 int c
=utf8_get_char((const char**)&txt
);
676 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
677 i
-= vo_font
->charspace
;
682 if(c
== '\n') continue;
684 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
687 i
-= vo_font
->charspace
;
692 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
696 int menu_text_num_lines(char* txt
, int max_width
) {
700 int c
=utf8_get_char((const char**)&txt
);
701 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
704 if(c
== '\n') continue;
706 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
711 char* menu_text_get_next_line(char* txt
, int max_width
) {
715 int c
=utf8_get_char((const char**)&txt
);
720 i
+= vo_font
->width
[c
];
723 i
+= vo_font
->charspace
;
729 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
730 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
734 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
738 if(x
> mpi
->w
|| y
> mpi
->h
) return;
740 if(x
< 0) w
+= x
, x
= 0;
741 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
742 if(y
< 0) h
+= y
, y
= 0;
743 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
745 g
= ((256-alpha
)*grey
)>>8;
749 int stride
= (w
+7)&(~7); // round to 8
750 char pic
[stride
*h
],pic_alpha
[stride
*h
];
751 memset(pic
,g
,stride
*h
);
752 memset(pic_alpha
,alpha
,stride
*h
);
753 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
754 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),