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 static menu_cmd_bindings_t
*get_cmd_bindings(const char *name
)
100 for (i
= 0; i
< cmd_bindings_num
; ++i
)
101 if (!strcasecmp(cmd_bindings
[i
].name
, name
))
102 return &cmd_bindings
[i
];
106 static int menu_parse_config(char* buffer
) {
107 char *element
,*body
, **attribs
, *name
;
108 menu_info_t
* minfo
= NULL
;
110 ASX_Parser_t
* parser
= asx_parser_new();
113 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
115 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,parser
->line
);
116 asx_parser_free(parser
);
119 asx_parser_free(parser
);
123 name
= asx_get_attrib("name",attribs
);
125 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuDefinitionsNeedANameAttrib
,parser
->line
);
128 asx_free_attribs(attribs
);
132 if (!strcasecmp(element
, "keybindings")) {
133 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
134 char *parent_bindings
;
135 cmd_bindings
= realloc(cmd_bindings
,
136 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
137 for (i
= 0; i
< cmd_bindings_num
; ++i
)
138 if (cmd_bindings
[i
].parent
)
139 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
140 bindings
= &cmd_bindings
[cmd_bindings_num
];
141 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
142 bindings
->name
= name
;
143 parent_bindings
= asx_get_attrib("parent",attribs
);
144 if (parent_bindings
) {
145 bindings
->parent
= get_cmd_bindings(parent_bindings
);
146 free(parent_bindings
);
149 asx_free_attribs(attribs
);
155 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
157 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_SyntaxErrorAtLine
,
160 asx_parser_free(parser
);
165 if (!strcasecmp(element
, "binding")) {
166 key
= asx_get_attrib("key",attribs
);
167 cmd
= asx_get_attrib("cmd",attribs
);
168 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
169 keycode
&= ~MP_NO_REPEAT_KEY
;
170 mp_msg(MSGT_GLOBAL
,MSGL_V
,
171 "[libmenu] got keybinding element %d %s=>[%s].\n",
172 keycode
, key
, cmd
? cmd
: "");
173 bindings
->bindings
= realloc(bindings
->bindings
,
174 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
175 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
176 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
177 ++bindings
->binding_num
;
184 asx_free_attribs(attribs
);
192 // Try to find this menu type in our list
193 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
194 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
195 minfo
= menu_info_list
[i
];
199 // Got it : add this to our list
201 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
202 menu_list
[menu_count
].name
= name
;
203 menu_list
[menu_count
].type
= minfo
;
204 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
205 menu_list
[menu_count
].args
= body
;
207 for(i
= 0 ; attribs
[2*i
] ; i
++) {
208 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
209 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
210 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_BadAttrib
,attribs
[2*i
],attribs
[2*i
+1],
214 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
216 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnknownMenuType
,element
,parser
->line
);
222 asx_free_attribs(attribs
);
228 /// This will build the menu_defs list from the cfg file
229 #define BUF_STEP 1024
231 #define BUF_MAX BUF_STEP*1024
232 int menu_init(struct MPContext
*mpctx
, char* cfg_file
) {
234 int bl
= BUF_STEP
, br
= 0;
236 #ifndef CONFIG_FREETYPE
240 fd
= open(cfg_file
, O_RDONLY
);
242 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_CantOpenConfigFile
,cfg_file
);
248 if(bl
- br
< BUF_MIN
) {
250 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsTooBig
,BUF_MAX
/1024);
256 buffer
= realloc(buffer
,bl
);
258 r
= read(fd
,buffer
+br
,bl
-br
);
263 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_ConfigFileIsEmpty
);
271 f
= menu_parse_config(buffer
);
276 // Destroy all this stuff
277 void menu_uninit(void) {
279 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
280 free(menu_list
[i
].name
);
281 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
282 if(menu_list
[i
].args
) free(menu_list
[i
].args
);
286 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
287 free(cmd_bindings
[i
].name
);
288 while(cmd_bindings
[i
].binding_num
> 0)
289 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
290 free(cmd_bindings
[i
].bindings
);
295 /// Default read_key function
296 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
298 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
300 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
302 bindings
= get_cmd_bindings("default");
304 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
305 if (bindings
->bindings
[i
].key
== cmd
) {
306 if (bindings
->bindings
[i
].cmd
)
307 mp_input_parse_and_queue_cmds(bindings
->bindings
[i
].cmd
);
311 bindings
= bindings
->parent
;
316 menu_t
* menu_open(char *name
) {
320 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
321 if(strcmp(name
,menu_list
[i
].name
) == 0)
324 if(menu_list
[i
].name
== NULL
) {
325 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuNotFound
,name
);
328 m
= calloc(1,sizeof(menu_t
));
329 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
330 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
332 m
->type
= &menu_list
[i
];
333 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
336 m_struct_free(m
->priv_st
,m
->priv
);
338 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_MenuInitFailed
,name
);
342 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
343 if(menu
->show
&& menu
->draw
)
344 menu
->draw(menu
,mpi
);
347 void menu_update_mouse_pos(double x
, double y
) {
350 menu_mouse_pos_updated
= 1;
353 void menu_read_cmd(menu_t
* menu
,int cmd
) {
355 menu
->read_cmd(menu
,cmd
);
358 void menu_close(menu_t
* menu
) {
362 m_struct_free(menu
->priv_st
,menu
->priv
);
366 int menu_read_key(menu_t
* menu
,int cmd
) {
368 return menu
->read_key(menu
,cmd
);
370 return menu_dflt_read_key(menu
,cmd
);
373 ///////////////////////////// Helpers ////////////////////////////////////
375 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
377 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
381 return vo_draw_alpha_rgb15
;
384 return vo_draw_alpha_rgb16
;
387 return vo_draw_alpha_rgb24
;
390 return vo_draw_alpha_rgb32
;
398 return vo_draw_alpha_yv12
;
400 return vo_draw_alpha_yuy2
;
402 return vo_draw_alpha_uyvy
;
408 // return the real height of a char:
409 static inline int get_height(int c
,int h
){
411 if ((font
=vo_font
->font
[c
])>=0)
412 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
416 static void render_txt(char *txt
)
419 int c
= utf8_get_char((const char**)&txt
);
420 render_one_glyph(vo_font
, c
);
424 #ifdef CONFIG_FRIBIDI
425 #include <fribidi/fribidi.h>
426 #include "libavutil/common.h"
427 char *menu_fribidi_charset
= NULL
;
428 int menu_flip_hebrew
= 0;
429 int menu_fribidi_flip_commas
= 0;
431 static char *menu_fribidi(char *txt
)
433 static int char_set_num
= -1;
434 static FriBidiChar
*logical
, *visual
;
435 static size_t buffer_size
= 1024;
436 static char *outputstr
;
438 FriBidiCharType base
;
439 fribidi_boolean log2vis
;
442 if (menu_flip_hebrew
) {
444 if (char_set_num
== -1) {
445 fribidi_set_mirroring (1);
446 fribidi_set_reorder_nsm (0);
447 char_set_num
= fribidi_parse_charset("UTF-8");
448 buffer_size
= FFMAX(1024,len
+1);
449 logical
= malloc(buffer_size
);
450 visual
= malloc(buffer_size
);
451 outputstr
= malloc(buffer_size
);
452 } else if (len
+1 > buffer_size
) {
454 logical
= realloc(logical
, buffer_size
);
455 visual
= realloc(visual
, buffer_size
);
456 outputstr
= realloc(outputstr
, buffer_size
);
458 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
459 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
460 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
462 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
463 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
471 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
472 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
476 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
480 #ifdef CONFIG_FRIBIDI
481 txt
= menu_fribidi(txt
);
486 int c
=utf8_get_char((const char**)&txt
);
487 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
488 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
489 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
490 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
491 vo_font
->pic_a
[font
]->w
,
492 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
494 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
499 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
500 int x
, int y
,int w
, int h
,
501 int vspace
, int warp
, int align
, int anchor
) {
504 int sx
, xmin
, xmax
, xmid
, xrmin
;
507 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
510 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
514 #ifdef CONFIG_FRIBIDI
515 txt
= menu_fribidi(txt
);
519 if(x
> mpi
->w
|| y
> mpi
->h
)
522 if(anchor
& MENU_TEXT_VCENTER
) {
523 if(h
<= 0) h
= mpi
->h
;
526 } else if(anchor
& MENU_TEXT_BOT
) {
527 if(h
<= 0) h
= mpi
->h
- y
;
531 if(h
<= 0) h
= mpi
->h
- y
;
536 if(anchor
& MENU_TEXT_HCENTER
) {
537 if(w
<= 0) w
= mpi
->w
;
540 } else if(anchor
& MENU_TEXT_RIGHT
) {
541 if(w
<= 0) w
= mpi
->w
-x
;
545 if(w
<= 0) w
= mpi
->w
-x
;
550 // How many space do we need to draw this ?
551 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
553 // Find the first line
554 if(align
& MENU_TEXT_VCENTER
)
555 sy
= ymin
+ ((h
- need_h
)/2);
556 else if(align
& MENU_TEXT_BOT
)
557 sy
= ymax
- need_h
- 1;
562 // Find the first col
563 if(align
& MENU_TEXT_HCENTER
)
564 sx
= xmin
+ ((w
- need_w
)/2);
565 else if(align
& MENU_TEXT_RIGHT
)
569 xmid
= xmin
+ (xmax
- xmin
) / 2;
571 // Clamp the bb to the mpi size
572 if(ymin
< 0) ymin
= 0;
573 if(xmin
< 0) xmin
= 0;
574 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
575 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
577 // Jump some the beginnig text if needed
578 while(sy
< ymin
&& *txt
) {
579 int c
=utf8_get_char((const char**)&txt
);
580 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
582 sy
+= vo_font
->height
+ vspace
;
583 if(c
== '\n') continue;
585 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
587 if(*txt
== '\0') // Nothing left to draw
590 while(sy
< ymax
&& *txt
) {
591 char* line_end
= NULL
;
594 if(txt
[0] == '\n') { // New line
595 sy
+= vo_font
->height
+ vspace
;
600 // Get the length and end of this line
601 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
602 unsigned char c
= txt
[n
];
603 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
604 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
607 ll
-= vo_font
->charspace
;
610 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
613 if(align
& MENU_TEXT_HCENTER
) {
615 // Find the middle point
616 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
617 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
618 if(ll
- vo_font
->charspace
> mid
) break;
620 ll
-= vo_font
->charspace
;
621 sx
= xmid
+ mid
- ll
;
622 } else// MENU_TEXT_RIGHT)
623 sx
= xmax
+ vo_font
->charspace
;
625 // We are after the start point -> go back
627 for(n
-- ; n
<= 0 ; n
--) {
628 unsigned char c
= txt
[n
];
629 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
630 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
632 } else { // We are before the start point -> go forward
633 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
634 unsigned char c
= txt
[n
];
635 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
638 txt
= &txt
[n
]; // Jump to the new start char
640 if(align
& MENU_TEXT_HCENTER
)
646 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
647 unsigned char c
= txt
[n
];
648 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
652 while(sx
< xmax
&& txt
!= line_end
) {
653 int c
=utf8_get_char((const char**)&txt
);
654 font
= vo_font
->font
[c
];
656 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
657 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
658 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
659 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
660 cs
* vo_font
->pic_a
[font
]->w
,
661 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
662 cs
* vo_font
->pic_a
[font
]->w
,
663 vo_font
->pic_a
[font
]->w
,
664 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
667 //printf("Can't draw '%c'\n",c);
669 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
672 if(txt
[0] == '\0') break;
673 sy
+= vo_font
->height
+ vspace
;
677 int menu_text_length(char* txt
) {
681 int c
=utf8_get_char((const char**)&txt
);
682 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
684 return l
- vo_font
->charspace
;
687 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
693 int c
=utf8_get_char((const char**)&txt
);
694 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
695 i
-= vo_font
->charspace
;
700 if(c
== '\n') continue;
702 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
705 i
-= vo_font
->charspace
;
710 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
714 int menu_text_num_lines(char* txt
, int max_width
) {
718 int c
=utf8_get_char((const char**)&txt
);
719 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
722 if(c
== '\n') continue;
724 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
729 static char* menu_text_get_next_line(char* txt
, int max_width
)
734 int c
=utf8_get_char((const char**)&txt
);
739 i
+= vo_font
->width
[c
];
742 i
+= vo_font
->charspace
;
748 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
749 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
753 mp_msg(MSGT_GLOBAL
,MSGL_WARN
,MSGTR_LIBMENU_UnsupportedOutformat
);
757 if(x
> mpi
->w
|| y
> mpi
->h
) return;
759 if(x
< 0) w
+= x
, x
= 0;
760 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
761 if(y
< 0) h
+= y
, y
= 0;
762 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
764 g
= ((256-alpha
)*grey
)>>8;
768 int stride
= (w
+7)&(~7); // round to 8
769 char pic
[stride
*h
],pic_alpha
[stride
*h
];
770 memset(pic
,g
,stride
*h
);
771 memset(pic_alpha
,alpha
,stride
*h
);
772 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
773 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),