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.
28 #include "libvo/osd.h"
29 #include "sub/font_load.h"
31 #include "osdep/keycodes.h"
32 #include "asxparser.h"
33 #include "stream/stream.h"
34 #include "input/input.h"
36 #include "libmpcodecs/img_format.h"
37 #include "libmpcodecs/mp_image.h"
42 extern menu_info_t menu_info_cmdlist
;
43 extern menu_info_t menu_info_chapsel
;
44 extern menu_info_t menu_info_pt
;
45 extern menu_info_t menu_info_filesel
;
46 extern menu_info_t menu_info_txt
;
47 extern menu_info_t menu_info_console
;
48 extern menu_info_t menu_info_pref
;
49 extern menu_info_t menu_info_dvbsel
;
52 menu_info_t
* menu_info_list
[] = {
66 typedef struct key_cmd_s
{
71 typedef struct menu_cmd_bindings_s
{
75 struct menu_cmd_bindings_s
*parent
;
76 } menu_cmd_bindings_t
;
85 double menu_mouse_x
= -1.0;
86 double menu_mouse_y
= -1.0;
87 int menu_mouse_pos_updated
= 0;
89 static struct MPContext
*menu_ctx
= NULL
;
90 static struct m_config
*menu_mconfig
= NULL
;
91 static struct input_ctx
*menu_input
= NULL
;
92 static menu_def_t
* menu_list
= NULL
;
93 static int menu_count
= 0;
94 static menu_cmd_bindings_t
*cmd_bindings
= NULL
;
95 static int cmd_bindings_num
= 0;
98 static menu_cmd_bindings_t
*get_cmd_bindings(const char *name
)
101 for (i
= 0; i
< cmd_bindings_num
; ++i
)
102 if (!strcasecmp(cmd_bindings
[i
].name
, name
))
103 return &cmd_bindings
[i
];
107 static int menu_parse_config(char* buffer
, struct m_config
*mconfig
)
109 char *element
,*body
, **attribs
, *name
;
110 menu_info_t
* minfo
= NULL
;
112 ASX_Parser_t
* parser
= asx_parser_new(mconfig
);
115 r
= asx_get_element(parser
,&buffer
,&element
,&body
,&attribs
);
117 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] syntax error at line: %d\n",parser
->line
);
118 asx_parser_free(parser
);
121 asx_parser_free(parser
);
125 name
= asx_get_attrib("name",attribs
);
127 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Menu definitions need a name attribute (line %d).\n",parser
->line
);
130 asx_free_attribs(attribs
);
134 if (!strcasecmp(element
, "keybindings")) {
135 menu_cmd_bindings_t
*bindings
= cmd_bindings
;
136 char *parent_bindings
;
137 cmd_bindings
= realloc(cmd_bindings
,
138 (cmd_bindings_num
+1)*sizeof(menu_cmd_bindings_t
));
139 for (i
= 0; i
< cmd_bindings_num
; ++i
)
140 if (cmd_bindings
[i
].parent
)
141 cmd_bindings
[i
].parent
= cmd_bindings
[i
].parent
-bindings
+cmd_bindings
;
142 bindings
= &cmd_bindings
[cmd_bindings_num
];
143 memset(bindings
, 0, sizeof(menu_cmd_bindings_t
));
144 bindings
->name
= name
;
145 parent_bindings
= asx_get_attrib("parent",attribs
);
146 if (parent_bindings
) {
147 bindings
->parent
= get_cmd_bindings(parent_bindings
);
148 free(parent_bindings
);
151 asx_free_attribs(attribs
);
157 r
= asx_get_element(parser
,&bd
,&element
,&b
,&attribs
);
159 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] syntax error at line: %d\n",
162 asx_parser_free(parser
);
167 if (!strcasecmp(element
, "binding")) {
168 key
= asx_get_attrib("key",attribs
);
169 cmd
= asx_get_attrib("cmd",attribs
);
170 if (key
&& (keycode
= mp_input_get_key_from_name(key
)) >= 0) {
171 keycode
&= ~MP_NO_REPEAT_KEY
;
172 mp_msg(MSGT_GLOBAL
,MSGL_V
,
173 "[libmenu] got keybinding element %d %s=>[%s].\n",
174 keycode
, key
, cmd
? cmd
: "");
175 bindings
->bindings
= realloc(bindings
->bindings
,
176 (bindings
->binding_num
+1)*sizeof(key_cmd_t
));
177 bindings
->bindings
[bindings
->binding_num
].key
= keycode
;
178 bindings
->bindings
[bindings
->binding_num
].cmd
= cmd
;
179 ++bindings
->binding_num
;
186 asx_free_attribs(attribs
);
194 // Try to find this menu type in our list
195 for(i
= 0, minfo
= NULL
; menu_info_list
[i
] ; i
++) {
196 if(strcasecmp(element
,menu_info_list
[i
]->name
) == 0) {
197 minfo
= menu_info_list
[i
];
201 // Got it : add this to our list
203 menu_list
= realloc(menu_list
,(menu_count
+2)*sizeof(menu_def_t
));
204 menu_list
[menu_count
].name
= name
;
205 menu_list
[menu_count
].type
= minfo
;
206 menu_list
[menu_count
].cfg
= m_struct_alloc(&minfo
->priv_st
);
207 menu_list
[menu_count
].args
= body
;
209 for(i
= 0 ; attribs
[2*i
] ; i
++) {
210 if(strcasecmp(attribs
[2*i
],"name") == 0) continue;
211 if(!m_struct_set(&minfo
->priv_st
,menu_list
[menu_count
].cfg
,attribs
[2*i
], attribs
[2*i
+1]))
212 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] bad attribute %s=%s in menu '%s' at line %d\n",attribs
[2*i
],attribs
[2*i
+1],
216 memset(&menu_list
[menu_count
],0,sizeof(menu_def_t
));
218 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] unknown menu type '%s' at line %d\n",element
,parser
->line
);
224 asx_free_attribs(attribs
);
230 /// This will build the menu_defs list from the cfg file
231 #define BUF_STEP 1024
233 #define BUF_MAX BUF_STEP*1024
234 int menu_init(struct MPContext
*mpctx
, struct m_config
*mconfig
,
235 struct input_ctx
*input_ctx
, char* cfg_file
)
238 int bl
= BUF_STEP
, br
= 0;
240 #ifndef CONFIG_FREETYPE
244 fd
= open(cfg_file
, O_RDONLY
);
246 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Can't open menu config file: %s\n",cfg_file
);
252 if(bl
- br
< BUF_MIN
) {
254 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Config file is too big (> %d KB)\n",BUF_MAX
/1024);
260 buffer
= realloc(buffer
,bl
);
262 r
= read(fd
,buffer
+br
,bl
-br
);
267 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Config file is empty.\n");
275 menu_mconfig
= mconfig
;
276 menu_input
= input_ctx
;
277 f
= menu_parse_config(buffer
, mconfig
);
282 // Destroy all this stuff
283 void menu_uninit(void) {
285 for(i
= 0 ; menu_list
&& menu_list
[i
].name
; i
++) {
286 free(menu_list
[i
].name
);
287 m_struct_free(&menu_list
[i
].type
->priv_st
,menu_list
[i
].cfg
);
288 free(menu_list
[i
].args
);
292 for (i
= 0; i
< cmd_bindings_num
; ++i
) {
293 free(cmd_bindings
[i
].name
);
294 while(cmd_bindings
[i
].binding_num
> 0)
295 free(cmd_bindings
[i
].bindings
[--cmd_bindings
[i
].binding_num
].cmd
);
296 free(cmd_bindings
[i
].bindings
);
301 /// Default read_key function
302 int menu_dflt_read_key(menu_t
* menu
,int cmd
) {
304 menu_cmd_bindings_t
*bindings
= get_cmd_bindings(menu
->type
->name
);
306 bindings
= get_cmd_bindings(menu
->type
->type
->name
);
308 bindings
= get_cmd_bindings("default");
310 for (i
= 0; i
< bindings
->binding_num
; ++i
) {
311 if (bindings
->bindings
[i
].key
== cmd
) {
312 if (bindings
->bindings
[i
].cmd
)
313 mp_input_parse_and_queue_cmds(menu
->input_ctx
,
314 bindings
->bindings
[i
].cmd
);
318 bindings
= bindings
->parent
;
323 menu_t
* menu_open(char *name
) {
327 for(i
= 0 ; menu_list
[i
].name
!= NULL
; i
++) {
328 if(strcmp(name
,menu_list
[i
].name
) == 0)
331 if(menu_list
[i
].name
== NULL
) {
332 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Menu %s not found.\n",name
);
335 m
= calloc(1,sizeof(menu_t
));
336 m
->priv_st
= &(menu_list
[i
].type
->priv_st
);
337 m
->priv
= m_struct_copy(m
->priv_st
,menu_list
[i
].cfg
);
339 m
->mconfig
= menu_mconfig
;
340 m
->input_ctx
= menu_input
;
341 m
->type
= &menu_list
[i
];
342 if(menu_list
[i
].type
->open(m
,menu_list
[i
].args
))
345 m_struct_free(m
->priv_st
,m
->priv
);
347 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Menu '%s': Init failed.\n",name
);
351 void menu_draw(menu_t
* menu
,mp_image_t
* mpi
) {
352 if(menu
->show
&& menu
->draw
)
353 menu
->draw(menu
,mpi
);
356 void menu_update_mouse_pos(double x
, double y
) {
359 menu_mouse_pos_updated
= 1;
362 void menu_read_cmd(menu_t
* menu
,int cmd
) {
364 menu
->read_cmd(menu
,cmd
);
367 void menu_close(menu_t
* menu
) {
371 m_struct_free(menu
->priv_st
,menu
->priv
);
375 int menu_read_key(menu_t
* menu
,int cmd
) {
377 return menu
->read_key(menu
,cmd
);
379 return menu_dflt_read_key(menu
,cmd
);
382 ///////////////////////////// Helpers ////////////////////////////////////
384 typedef void (*draw_alpha_f
)(int w
,int h
, unsigned char* src
, unsigned char *srca
, int srcstride
, unsigned char* dstbase
,int dststride
);
386 inline static draw_alpha_f
get_draw_alpha(uint32_t fmt
) {
390 return vo_draw_alpha_rgb12
;
393 return vo_draw_alpha_rgb15
;
396 return vo_draw_alpha_rgb16
;
399 return vo_draw_alpha_rgb24
;
402 return vo_draw_alpha_rgb32
;
410 return vo_draw_alpha_yv12
;
412 return vo_draw_alpha_yuy2
;
414 return vo_draw_alpha_uyvy
;
420 // return the real height of a char:
421 static inline int get_height(int c
,int h
){
423 if ((font
=vo_font
->font
[c
])>=0)
424 if(h
<vo_font
->pic_a
[font
]->h
) h
=vo_font
->pic_a
[font
]->h
;
428 static void render_txt(char *txt
)
431 int c
= utf8_get_char((const char**)&txt
);
432 render_one_glyph(vo_font
, c
);
436 #ifdef CONFIG_FRIBIDI
437 #include <fribidi/fribidi.h>
438 #include "libavutil/common.h"
439 char *menu_fribidi_charset
= NULL
;
440 int menu_flip_hebrew
= 0;
441 int menu_fribidi_flip_commas
= 0;
443 static char *menu_fribidi(char *txt
)
445 static int char_set_num
= -1;
446 static FriBidiChar
*logical
, *visual
;
447 static size_t buffer_size
= 1024;
448 static char *outputstr
;
450 FriBidiCharType base
;
451 fribidi_boolean log2vis
;
454 if (menu_flip_hebrew
) {
456 if (char_set_num
== -1) {
457 fribidi_set_mirroring (1);
458 fribidi_set_reorder_nsm (0);
459 char_set_num
= fribidi_parse_charset("UTF-8");
460 buffer_size
= FFMAX(1024,len
+1);
461 logical
= malloc(buffer_size
);
462 visual
= malloc(buffer_size
);
463 outputstr
= malloc(buffer_size
);
464 } else if (len
+1 > buffer_size
) {
466 logical
= realloc(logical
, buffer_size
);
467 visual
= realloc(visual
, buffer_size
);
468 outputstr
= realloc(outputstr
, buffer_size
);
470 len
= fribidi_charset_to_unicode (char_set_num
, txt
, len
, logical
);
471 base
= menu_fribidi_flip_commas
?FRIBIDI_TYPE_ON
:FRIBIDI_TYPE_L
;
472 log2vis
= fribidi_log2vis (logical
, len
, &base
, visual
, NULL
, NULL
, NULL
);
474 len
= fribidi_remove_bidi_marks (visual
, len
, NULL
, NULL
, NULL
);
475 fribidi_unicode_to_charset (char_set_num
, visual
, len
, outputstr
);
483 void menu_draw_text(mp_image_t
* mpi
,char* txt
, int x
, int y
) {
484 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
488 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Unsupported output format!!!!\n");
492 #ifdef CONFIG_FRIBIDI
493 txt
= menu_fribidi(txt
);
498 int c
=utf8_get_char((const char**)&txt
);
499 if ((font
=vo_font
->font
[c
])>=0 && (x
+ vo_font
->width
[c
] <= mpi
->w
) && (y
+ vo_font
->pic_a
[font
]->h
<= mpi
->h
))
500 draw_alpha(vo_font
->width
[c
], vo_font
->pic_a
[font
]->h
,
501 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
],
502 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
],
503 vo_font
->pic_a
[font
]->w
,
504 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),
506 x
+=vo_font
->width
[c
]+vo_font
->charspace
;
511 void menu_draw_text_full(mp_image_t
* mpi
,char* txt
,
512 int x
, int y
,int w
, int h
,
513 int vspace
, int warp
, int align
, int anchor
) {
516 int sx
, xmin
, xmax
, xmid
, xrmin
;
519 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
522 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Unsupported output format!!!!\n");
526 #ifdef CONFIG_FRIBIDI
527 txt
= menu_fribidi(txt
);
531 if(x
> mpi
->w
|| y
> mpi
->h
)
534 if(anchor
& MENU_TEXT_VCENTER
) {
535 if(h
<= 0) h
= mpi
->h
;
538 } else if(anchor
& MENU_TEXT_BOT
) {
539 if(h
<= 0) h
= mpi
->h
- y
;
543 if(h
<= 0) h
= mpi
->h
- y
;
548 if(anchor
& MENU_TEXT_HCENTER
) {
549 if(w
<= 0) w
= mpi
->w
;
552 } else if(anchor
& MENU_TEXT_RIGHT
) {
553 if(w
<= 0) w
= mpi
->w
-x
;
557 if(w
<= 0) w
= mpi
->w
-x
;
562 // How many space do we need to draw this ?
563 menu_text_size(txt
,w
,vspace
,warp
,&need_w
,&need_h
);
565 // Find the first line
566 if(align
& MENU_TEXT_VCENTER
)
567 sy
= ymin
+ ((h
- need_h
)/2);
568 else if(align
& MENU_TEXT_BOT
)
569 sy
= ymax
- need_h
- 1;
574 // Find the first col
575 if(align
& MENU_TEXT_HCENTER
)
576 sx
= xmin
+ ((w
- need_w
)/2);
577 else if(align
& MENU_TEXT_RIGHT
)
581 xmid
= xmin
+ (xmax
- xmin
) / 2;
583 // Clamp the bb to the mpi size
584 if(ymin
< 0) ymin
= 0;
585 if(xmin
< 0) xmin
= 0;
586 if(ymax
> mpi
->h
) ymax
= mpi
->h
;
587 if(xmax
> mpi
->w
) xmax
= mpi
->w
;
589 // Jump some the beginnig text if needed
590 while(sy
< ymin
&& *txt
) {
591 int c
=utf8_get_char((const char**)&txt
);
592 if(c
== '\n' || (warp
&& ll
+ vo_font
->width
[c
] > w
)) {
594 sy
+= vo_font
->height
+ vspace
;
595 if(c
== '\n') continue;
597 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
599 if(*txt
== '\0') // Nothing left to draw
602 while(sy
< ymax
&& *txt
) {
603 char* line_end
= NULL
;
606 if(txt
[0] == '\n') { // New line
607 sy
+= vo_font
->height
+ vspace
;
612 // Get the length and end of this line
613 for(n
= 0, ll
= 0 ; txt
[n
] != '\0' && txt
[n
] != '\n' ; n
++) {
614 unsigned char c
= txt
[n
];
615 if(warp
&& ll
+ vo_font
->width
[c
] > w
) break;
616 ll
+= vo_font
->width
[c
]+vo_font
->charspace
;
619 ll
-= vo_font
->charspace
;
622 if(align
& (MENU_TEXT_HCENTER
|MENU_TEXT_RIGHT
)) {
625 if(align
& MENU_TEXT_HCENTER
) {
627 // Find the middle point
628 for(n
--, ll
= 0 ; n
<= 0 ; n
--) {
629 ll
+= vo_font
->width
[(int)txt
[n
]]+vo_font
->charspace
;
630 if(ll
- vo_font
->charspace
> mid
) break;
632 ll
-= vo_font
->charspace
;
633 sx
= xmid
+ mid
- ll
;
634 } else// MENU_TEXT_RIGHT)
635 sx
= xmax
+ vo_font
->charspace
;
637 // We are after the start point -> go back
639 for(n
-- ; n
<= 0 ; n
--) {
640 unsigned char c
= txt
[n
];
641 if(sx
- vo_font
->width
[c
] - vo_font
->charspace
< xmin
) break;
642 sx
-= vo_font
->width
[c
]+vo_font
->charspace
;
644 } else { // We are before the start point -> go forward
645 for( ; sx
< xmin
&& (&txt
[n
]) != line_end
; n
++) {
646 unsigned char c
= txt
[n
];
647 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
650 txt
= &txt
[n
]; // Jump to the new start char
652 if(align
& MENU_TEXT_HCENTER
)
658 for(sx
= xrmin
; sx
< xmin
&& txt
!= line_end
; txt
++) {
659 unsigned char c
= txt
[n
];
660 sx
+= vo_font
->width
[c
]+vo_font
->charspace
;
664 while(sx
< xmax
&& txt
!= line_end
) {
665 int c
=utf8_get_char((const char**)&txt
);
666 font
= vo_font
->font
[c
];
668 int cs
= (vo_font
->pic_a
[font
]->h
- vo_font
->height
) / 2;
669 if ((sx
+ vo_font
->width
[c
] <= xmax
) && (sy
+ vo_font
->height
<= ymax
) )
670 draw_alpha(vo_font
->width
[c
], vo_font
->height
,
671 vo_font
->pic_b
[font
]->bmp
+vo_font
->start
[c
] +
672 cs
* vo_font
->pic_a
[font
]->w
,
673 vo_font
->pic_a
[font
]->bmp
+vo_font
->start
[c
] +
674 cs
* vo_font
->pic_a
[font
]->w
,
675 vo_font
->pic_a
[font
]->w
,
676 mpi
->planes
[0] + sy
* mpi
->stride
[0] + sx
* (mpi
->bpp
>>3),
679 //printf("Can't draw '%c'\n",c);
681 sx
+=vo_font
->width
[c
]+vo_font
->charspace
;
684 if(txt
[0] == '\0') break;
685 sy
+= vo_font
->height
+ vspace
;
689 int menu_text_length(char* txt
) {
693 int c
=utf8_get_char((const char**)&txt
);
694 l
+= vo_font
->width
[c
]+vo_font
->charspace
;
696 return l
- vo_font
->charspace
;
699 void menu_text_size(char* txt
,int max_width
, int vspace
, int warp
, int* _w
, int* _h
) {
705 int c
=utf8_get_char((const char**)&txt
);
706 if(c
== '\n' || (warp
&& i
+ vo_font
->width
[c
] >= max_width
)) {
707 i
-= vo_font
->charspace
;
712 if(c
== '\n') continue;
714 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
717 i
-= vo_font
->charspace
;
722 *_h
= (l
-1) * (vo_font
->height
+ vspace
) + vo_font
->height
;
726 int menu_text_num_lines(char* txt
, int max_width
) {
730 int c
=utf8_get_char((const char**)&txt
);
731 if(c
== '\n' || i
+ vo_font
->width
[c
] > max_width
) {
734 if(c
== '\n') continue;
736 i
+= vo_font
->width
[c
]+vo_font
->charspace
;
742 void menu_draw_box(mp_image_t
* mpi
,unsigned char grey
,unsigned char alpha
, int x
, int y
, int w
, int h
) {
743 draw_alpha_f draw_alpha
= get_draw_alpha(mpi
->imgfmt
);
747 mp_tmsg(MSGT_GLOBAL
,MSGL_WARN
,"[MENU] Unsupported output format!!!!\n");
751 if(x
> mpi
->w
|| y
> mpi
->h
) return;
753 if(x
< 0) w
+= x
, x
= 0;
754 if(x
+w
> mpi
->w
) w
= mpi
->w
-x
;
755 if(y
< 0) h
+= y
, y
= 0;
756 if(y
+h
> mpi
->h
) h
= mpi
->h
-y
;
758 g
= ((256-alpha
)*grey
)>>8;
762 int stride
= (w
+7)&(~7); // round to 8
763 char pic
[stride
*h
],pic_alpha
[stride
*h
];
764 memset(pic
,g
,stride
*h
);
765 memset(pic_alpha
,alpha
,stride
*h
);
766 draw_alpha(w
,h
,pic
,pic_alpha
,stride
,
767 mpi
->planes
[0] + y
* mpi
->stride
[0] + x
* (mpi
->bpp
>>3),