2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "libvo/osd.h"
30 #include "libvo/font_load.h"
31 #include "libvo/sub.h"
32 #include "osdep/keycodes.h"
33 #include "asxparser.h"
34 #include "stream/stream.h"
35 #include "input/input.h"
37 #include "libmpcodecs/img_format.h"
38 #include "libmpcodecs/mp_image.h"
43 extern menu_info_t menu_info_cmdlist
;
44 extern menu_info_t menu_info_chapsel
;
45 extern menu_info_t menu_info_pt
;
46 extern menu_info_t menu_info_filesel
;
47 extern menu_info_t menu_info_txt
;
48 extern menu_info_t menu_info_console
;
49 extern menu_info_t menu_info_pref
;
50 extern menu_info_t menu_info_dvbsel
;
53 menu_info_t
* menu_info_list
[] = {
67 typedef struct key_cmd_s
{
72 typedef struct menu_cmd_bindings_s
{
76 struct menu_cmd_bindings_s
*parent
;
77 } menu_cmd_bindings_t
;
86 double menu_mouse_x
= -1.0;
87 double menu_mouse_y
= -1.0;
88 int menu_mouse_pos_updated
= 0;
90 static struct MPContext
*menu_ctx
= NULL
;
91 static menu_def_t
* menu_list
= NULL
;
92 static int menu_count
= 0;
93 static menu_cmd_bindings_t
*cmd_bindings
= NULL
;
94 static int cmd_bindings_num
= 0;
97 menu_cmd_bindings_t
*get_cmd_bindings(const char *name
) {
99 for (i
= 0; i
< cmd_bindings_num
; ++i
)
100 if (!strcasecmp(cmd_bindings
[i
].name
, name
))
101 return &cmd_bindings
[i
];
105 static int menu_parse_config(char* buffer
) {
106 char *element
,*body
, **attribs
, *name
;
107 menu_info_t
* minfo
= NULL
;
109 ASX_Parser_t
* parser
= asx_parser_new();
112 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
114 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
115 asx_parser_free(parser
);
118 asx_parser_free(parser
);
122 name
= asx_get_attrib("name",attribs
);
124 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib
,parser
->line
);
127 asx_free_attribs(attribs
);
131 if (!strcasecmp(element
, "keybindings")) {
132 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
133 char *parent_bindings
;
134 cmd_bindings
= realloc(cmd_bindings
,
135 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
136 for (i
= 0; i
< cmd_bindings_num
; ++i
)
137 if (cmd_bindings
[i
].parent
)
138 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
139 bindings
= &cmd_bindings
[cmd_bindings_num
];
140 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
141 bindings
->name
= name
;
142 parent_bindings
= asx_get_attrib("parent",attribs
);
143 if (parent_bindings
) {
144 bindings
->parent
= get_cmd_bindings(parent_bindings
);
145 free(parent_bindings
);
148 asx_free_attribs(attribs
);
154 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
156 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,
159 asx_parser_free(parser
);
164 if (!strcasecmp(element
, "binding")) {
165 key
= asx_get_attrib("key",attribs
);
166 cmd
= asx_get_attrib("cmd",attribs
);
167 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
168 keycode
&= ~MP_NO_REPEAT_KEY
;
169 mp_msg(MSGT_GLOBAL
,MSGL_V
,
170 "[libmenu] got keybinding element %d %s=>[%s].\n",
171 keycode
, key
, cmd
? cmd
: "");
172 bindings
->bindings
= realloc(bindings
->bindings
,
173 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
174 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
175 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
176 ++bindings
->binding_num
;
183 asx_free_attribs(attribs
);
191 // Try to find this menu type in our list
192 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
193 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
194 minfo
= menu_info_list
[i
];
198 // Got it : add this to our list
200 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
201 menu_list
[menu_count
].name
= name
;
202 menu_list
[menu_count
].type
= minfo
;
203 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
204 menu_list
[menu_count
].args
= body
;
206 for(i
= 0 ; attribs
[2*i
] ; i
++) {
207 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
208 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
209 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_BadAttrib
,attribs
[2*i
],attribs
[2*i
+1],
213 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
215 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnknownMenuType
,element
,parser
->line
);
221 asx_free_attribs(attribs
);
227 /// This will build the menu_defs list from the cfg file
228 #define BUF_STEP 1024
230 #define BUF_MAX BUF_STEP*1024
231 int menu_init(struct MPContext
*mpctx
, char* cfg_file
) {
233 int bl
= BUF_STEP
, br
= 0;
235 #ifndef CONFIG_FREETYPE
239 fd
= open(cfg_file
, O_RDONLY
);
241 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_CantOpenConfigFile
,cfg_file
);
247 if(bl
- br
< BUF_MIN
) {
249 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsTooBig
,BUF_MAX
/1024);
255 buffer
= realloc(buffer
,bl
);
257 r
= read(fd
,buffer
+br
,bl
-br
);
262 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsEmpty
);
270 f
= menu_parse_config(buffer
);
275 // Destroy all this stuff
276 void menu_uninit(void) {
278 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
279 free(menu_list
[i
].name
);
280 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
281 if(menu_list
[i
].args
) free(menu_list
[i
].args
);
285 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
286 free(cmd_bindings
[i
].name
);
287 while(cmd_bindings
[i
].binding_num
> 0)
288 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
289 free(cmd_bindings
[i
].bindings
);
294 /// Default read_key function
295 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
297 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
299 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
301 bindings
= get_cmd_bindings("default");
303 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
304 if (bindings
->bindings
[i
].key
== cmd
) {
305 if (bindings
->bindings
[i
].cmd
)
306 mp_input_parse_and_queue_cmds(bindings
->bindings
[i
].cmd
);
310 bindings
= bindings
->parent
;
315 menu_t
* menu_open(char *name
) {
319 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
320 if(strcmp(name
,menu_list
[i
].name
) == 0)
323 if(menu_list
[i
].name
== NULL
) {
324 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuNotFound
,name
);
327 m
= calloc(1,sizeof(menu_t
));
328 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
329 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
331 m
->type
= &menu_list
[i
];
332 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
335 m_struct_free(m
->priv_st
,m
->priv
);
337 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuInitFailed
,name
);
341 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
342 if(menu
->show
&& menu
->draw
)
343 menu
->draw(menu
,mpi
);
346 void menu_update_mouse_pos(double x
, double y
) {
349 menu_mouse_pos_updated
= 1;
352 void menu_read_cmd(menu_t
* menu
,int cmd
) {
354 menu
->read_cmd(menu
,cmd
);
357 void menu_close(menu_t
* menu
) {
361 m_struct_free(menu
->priv_st
,menu
->priv
);
365 int menu_read_key(menu_t
* menu
,int cmd
) {
367 return menu
->read_key(menu
,cmd
);
369 return menu_dflt_read_key(menu
,cmd
);
372 ///////////////////////////// Helpers ////////////////////////////////////
374 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
376 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
380 return vo_draw_alpha_rgb15
;
383 return vo_draw_alpha_rgb16
;
386 return vo_draw_alpha_rgb24
;
389 return vo_draw_alpha_rgb32
;
397 return vo_draw_alpha_yv12
;
399 return vo_draw_alpha_yuy2
;
401 return vo_draw_alpha_uyvy
;
407 // return the real height of a char:
408 static inline int get_height(int c
,int h
){
410 if ((font
=vo_font
->font
[c
])>=0)
411 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
415 static void render_txt(char *txt
)
418 int c
= utf8_get_char((const char**)&txt
);
419 render_one_glyph(vo_font
, c
);
423 #ifdef CONFIG_FRIBIDI
424 #include <fribidi/fribidi.h>
425 #include "libavutil/common.h"
426 char *menu_fribidi_charset
= NULL
;
427 int menu_flip_hebrew
= 0;
428 int menu_fribidi_flip_commas
= 0;
430 static char *menu_fribidi(char *txt
)
432 static int char_set_num
= -1;
433 static FriBidiChar
*logical
, *visual
;
434 static size_t buffer_size
= 1024;
435 static char *outputstr
;
437 FriBidiCharType base
;
438 fribidi_boolean log2vis
;
441 if (menu_flip_hebrew
) {
443 if (char_set_num
== -1) {
444 fribidi_set_mirroring (1);
445 fribidi_set_reorder_nsm (0);
446 char_set_num
= fribidi_parse_charset("UTF-8");
447 buffer_size
= FFMAX(1024,len
+1);
448 logical
= malloc(buffer_size
);
449 visual
= malloc(buffer_size
);
450 outputstr
= malloc(buffer_size
);
451 } else if (len
+1 > buffer_size
) {
453 logical
= realloc(logical
, buffer_size
);
454 visual
= realloc(visual
, buffer_size
);
455 outputstr
= realloc(outputstr
, buffer_size
);
457 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
458 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
459 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
461 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
462 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
470 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
471 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
475 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
479 #ifdef CONFIG_FRIBIDI
480 txt
= menu_fribidi(txt
);
485 int c
=utf8_get_char((const char**)&txt
);
486 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
487 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
488 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
489 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
490 vo_font
->pic_a
[font
]->w
,
491 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
493 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
498 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
499 int x
, int y
,int w
, int h
,
500 int vspace
, int warp
, int align
, int anchor
) {
503 int sx
, xmin
, xmax
, xmid
, xrmin
;
506 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
509 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
513 #ifdef CONFIG_FRIBIDI
514 txt
= menu_fribidi(txt
);
518 if(x
> mpi
->w
|| y
> mpi
->h
)
521 if(anchor
& MENU_TEXT_VCENTER
) {
522 if(h
<= 0) h
= mpi
->h
;
525 } else if(anchor
& MENU_TEXT_BOT
) {
526 if(h
<= 0) h
= mpi
->h
- y
;
530 if(h
<= 0) h
= mpi
->h
- y
;
535 if(anchor
& MENU_TEXT_HCENTER
) {
536 if(w
<= 0) w
= mpi
->w
;
539 } else if(anchor
& MENU_TEXT_RIGHT
) {
540 if(w
<= 0) w
= mpi
->w
-x
;
544 if(w
<= 0) w
= mpi
->w
-x
;
549 // How many space do we need to draw this ?
550 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
552 // Find the first line
553 if(align
& MENU_TEXT_VCENTER
)
554 sy
= ymin
+ ((h
- need_h
)/2);
555 else if(align
& MENU_TEXT_BOT
)
556 sy
= ymax
- need_h
- 1;
561 // Find the first col
562 if(align
& MENU_TEXT_HCENTER
)
563 sx
= xmin
+ ((w
- need_w
)/2);
564 else if(align
& MENU_TEXT_RIGHT
)
568 xmid
= xmin
+ (xmax
- xmin
) / 2;
570 // Clamp the bb to the mpi size
571 if(ymin
< 0) ymin
= 0;
572 if(xmin
< 0) xmin
= 0;
573 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
574 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
576 // Jump some the beginnig text if needed
577 while(sy
< ymin
&& *txt
) {
578 int c
=utf8_get_char((const char**)&txt
);
579 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
581 sy
+= vo_font
->height
+ vspace
;
582 if(c
== '\n') continue;
584 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
586 if(*txt
== '\0') // Nothing left to draw
589 while(sy
< ymax
&& *txt
) {
590 char* line_end
= NULL
;
593 if(txt
[0] == '\n') { // New line
594 sy
+= vo_font
->height
+ vspace
;
599 // Get the length and end of this line
600 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
601 unsigned char c
= txt
[n
];
602 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
603 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
606 ll
-= vo_font
->charspace
;
609 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
612 if(align
& MENU_TEXT_HCENTER
) {
614 // Find the middle point
615 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
616 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
617 if(ll
- vo_font
->charspace
> mid
) break;
619 ll
-= vo_font
->charspace
;
620 sx
= xmid
+ mid
- ll
;
621 } else// MENU_TEXT_RIGHT)
622 sx
= xmax
+ vo_font
->charspace
;
624 // We are after the start point -> go back
626 for(n
-- ; n
<= 0 ; n
--) {
627 unsigned char c
= txt
[n
];
628 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
629 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
631 } else { // We are before the start point -> go forward
632 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
633 unsigned char c
= txt
[n
];
634 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
637 txt
= &txt
[n
]; // Jump to the new start char
639 if(align
& MENU_TEXT_HCENTER
)
645 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
646 unsigned char c
= txt
[n
];
647 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
651 while(sx
< xmax
&& txt
!= line_end
) {
652 int c
=utf8_get_char((const char**)&txt
);
653 font
= vo_font
->font
[c
];
655 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
656 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
657 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
658 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
659 cs
* vo_font
->pic_a
[font
]->w
,
660 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
661 cs
* vo_font
->pic_a
[font
]->w
,
662 vo_font
->pic_a
[font
]->w
,
663 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
666 //printf("Can't draw '%c'\n",c);
668 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
671 if(txt
[0] == '\0') break;
672 sy
+= vo_font
->height
+ vspace
;
676 int menu_text_length(char* txt
) {
680 int c
=utf8_get_char((const char**)&txt
);
681 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
683 return l
- vo_font
->charspace
;
686 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
692 int c
=utf8_get_char((const char**)&txt
);
693 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
694 i
-= vo_font
->charspace
;
699 if(c
== '\n') continue;
701 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
704 i
-= vo_font
->charspace
;
709 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
713 int menu_text_num_lines(char* txt
, int max_width
) {
717 int c
=utf8_get_char((const char**)&txt
);
718 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
721 if(c
== '\n') continue;
723 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
728 char* menu_text_get_next_line(char* txt
, int max_width
) {
732 int c
=utf8_get_char((const char**)&txt
);
737 i
+= vo_font
->width
[c
];
740 i
+= vo_font
->charspace
;
746 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
747 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
751 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
755 if(x
> mpi
->w
|| y
> mpi
->h
) return;
757 if(x
< 0) w
+= x
, x
= 0;
758 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
759 if(y
< 0) h
+= y
, y
= 0;
760 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
762 g
= ((256-alpha
)*grey
)>>8;
766 int stride
= (w
+7)&(~7); // round to 8
767 char pic
[stride
*h
],pic_alpha
[stride
*h
];
768 memset(pic
,g
,stride
*h
);
769 memset(pic_alpha
,alpha
,stride
*h
);
770 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
771 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),