annotate libmenu/menu.c @ 12963:89c88649a785

Ignore some more generated files.
author diego
date Thu, 05 Aug 2004 00:12:15 +0000
parents fd97f3727f15
children 931bdbc37ee0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
1
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
2 #include "../config.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
3
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
4 #include <stdlib.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
5 #include <stdio.h>
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
6 #include <string.h>
8604
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
7 #include <fcntl.h>
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
8 #include <unistd.h>
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
9
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
10 #include "../libvo/osd.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
11 #include "../libvo/font_load.h"
9380
edfe34c5405d linux->osdep
arpi
parents: 9212
diff changeset
12 #include "../osdep/keycodes.h"
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
13 #include "../asxparser.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
14 #include "../libmpdemux/stream.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
15
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
16 #include "img_format.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
17 #include "mp_image.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
18 #include "../m_option.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
19 #include "../m_struct.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
20 #include "menu.h"
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
21
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
22 extern menu_info_t menu_info_cmdlist;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
23 extern menu_info_t menu_info_pt;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
24 extern menu_info_t menu_info_filesel;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
25 extern menu_info_t menu_info_txt;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
26 extern menu_info_t menu_info_console;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
27 extern menu_info_t menu_info_pref;
10626
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
28 #ifdef HAS_DVBIN_SUPPORT
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
29 extern menu_info_t menu_info_dvbsel;
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
30 #endif
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
31
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
32
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
33 menu_info_t* menu_info_list[] = {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
34 &menu_info_pt,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
35 &menu_info_cmdlist,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
36 &menu_info_filesel,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
37 &menu_info_txt,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
38 &menu_info_console,
10626
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
39 #ifdef HAS_DVBIN_SUPPORT
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
40 &menu_info_dvbsel,
fd97f3727f15 Finnaly commit Nico's dvb menu. Sorry for committing this
albeu
parents: 10397
diff changeset
41 #endif
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
42 &menu_info_pref,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
43 NULL
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
44 };
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
45
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
46 typedef struct menu_def_st {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
47 char* name;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
48 menu_info_t* type;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
49 void* cfg;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
50 char* args;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
51 } menu_def_t;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
52
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
53 static menu_def_t* menu_list = NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
54 static int mcount = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
55
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
56
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
57 static int menu_parse_config(char* buffer) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
58 char *element,*body, **attribs, *name;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
59 menu_info_t* minfo = NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
60 int r,i;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
61 ASX_Parser_t* parser = asx_parser_new();
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
62
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
63 while(1) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
64 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
65 if(r < 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
66 printf("Syntax error at line %d\n",parser->line);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
67 asx_parser_free(parser);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
68 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
69 } else if(r == 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
70 asx_parser_free(parser);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
71 return 1;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
72 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
73 // Has it a name ?
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
74 name = asx_get_attrib("name",attribs);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
75 if(!name) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
76 printf("Menu definitions need a name attrib (line %d)\n",parser->line);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
77 free(element);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
78 if(body) free(body);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
79 asx_free_attribs(attribs);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
80 continue;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
81 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
82
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
83 // Try to find this menu type in our list
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
84 for(i = 0, minfo = NULL ; menu_info_list[i] ; i++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
85 if(strcasecmp(element,menu_info_list[i]->name) == 0) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
86 minfo = menu_info_list[i];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
87 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
88 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
89 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
90 // Got it : add this to our list
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
91 if(minfo) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
92 menu_list = realloc(menu_list,(mcount+2)*sizeof(menu_def_t));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
93 menu_list[mcount].name = name;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
94 menu_list[mcount].type = minfo;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
95 menu_list[mcount].cfg = m_struct_alloc(&minfo->priv_st);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
96 menu_list[mcount].args = body;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
97 // Setup the attribs
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
98 for(i = 0 ; attribs[2*i] ; i++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
99 if(strcasecmp(attribs[2*i],"name") == 0) continue;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
100 if(!m_struct_set(&minfo->priv_st,menu_list[mcount].cfg,attribs[2*i], attribs[2*i+1]))
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
101 printf("Bad attrib %s=%s in menu %s at line %d\n",attribs[2*i],attribs[2*i+1],
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
102 name,parser->line);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
103 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
104 mcount++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
105 memset(&menu_list[mcount],0,sizeof(menu_def_t));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
106 } else {
10397
6180d7558f75 output typo fixes: unknow --> unknown
diego
parents: 9380
diff changeset
107 printf("Unknown menu type %s at line %d\n",element,parser->line);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
108 free(name);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
109 if(body) free(body);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
110 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
111
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
112 free(element);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
113 asx_free_attribs(attribs);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
114 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
115
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
116 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
117
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
118
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
119 /// This will build the menu_defs list from the cfg file
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
120 #define BUF_STEP 1024
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
121 #define BUF_MIN 128
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
122 #define BUF_MAX BUF_STEP*1024
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
123 int menu_init(char* cfg_file) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
124 char* buffer = NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
125 int bl = BUF_STEP, br = 0;
9103
6c2c74adaebe mplayer crashes if one tries to use osd menu without having a font
arpi
parents: 8604
diff changeset
126 int f, fd;
9212
24b102dbd0fe Fixes a problem where the menu won't work, if you just use freetype fonts
arpi
parents: 9103
diff changeset
127 #ifndef HAVE_FREETYPE
9103
6c2c74adaebe mplayer crashes if one tries to use osd menu without having a font
arpi
parents: 8604
diff changeset
128 if(vo_font == NULL)
6c2c74adaebe mplayer crashes if one tries to use osd menu without having a font
arpi
parents: 8604
diff changeset
129 return 0;
9212
24b102dbd0fe Fixes a problem where the menu won't work, if you just use freetype fonts
arpi
parents: 9103
diff changeset
130 #endif
9103
6c2c74adaebe mplayer crashes if one tries to use osd menu without having a font
arpi
parents: 8604
diff changeset
131 fd = open(cfg_file, O_RDONLY);
8604
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
132 if(fd < 0) {
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
133 printf("Can't open menu config file: %s\n",cfg_file);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
134 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
135 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
136 buffer = malloc(bl);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
137 while(1) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
138 int r;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
139 if(bl - br < BUF_MIN) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
140 if(bl >= BUF_MAX) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
141 printf("Menu config file is too big (> %d KB)\n",BUF_MAX/1024);
8604
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
142 close(fd);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
143 free(buffer);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
144 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
145 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
146 bl += BUF_STEP;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
147 buffer = realloc(buffer,bl);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
148 }
8604
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
149 r = read(fd,buffer+br,bl-br);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
150 if(r == 0) break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
151 br += r;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
152 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
153 if(!br) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
154 printf("Menu config file is empty\n");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
155 return 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
156 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
157 buffer[br-1] = '\0';
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
158
8604
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
159 close(fd);
41a1e5dbb552 This patch fixes the reading of the menu.conf, because stream_open()
arpi
parents: 8251
diff changeset
160
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
161 f = menu_parse_config(buffer);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
162 free(buffer);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
163 return f;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
164 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
165
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
166 // Destroy all this stuff
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
167 void menu_unint(void) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
168 int i;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
169 for(i = 0 ; menu_list && menu_list[i].name ; i++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
170 free(menu_list[i].name);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
171 m_struct_free(&menu_list[i].type->priv_st,menu_list[i].cfg);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
172 if(menu_list[i].args) free(menu_list[i].args);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
173 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
174 free(menu_list);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
175 mcount = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
176 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
177
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
178 /// Default read_key function
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
179 void menu_dflt_read_key(menu_t* menu,int cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
180 switch(cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
181 case KEY_UP:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
182 menu->read_cmd(menu,MENU_CMD_UP);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
183 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
184 case KEY_DOWN:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
185 menu->read_cmd(menu,MENU_CMD_DOWN);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
186 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
187 case KEY_LEFT:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
188 case KEY_ESC:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
189 menu->read_cmd(menu,MENU_CMD_CANCEL);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
190 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
191 case KEY_RIGHT:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
192 case KEY_ENTER:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
193 menu->read_cmd(menu,MENU_CMD_OK);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
194 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
195 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
196 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
198 menu_t* menu_open(char *name) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
199 menu_t* m;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
200 int i;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
201
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
202 for(i = 0 ; menu_list[i].name != NULL ; i++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
203 if(strcmp(name,menu_list[i].name) == 0)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
204 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
205 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
206 if(menu_list[i].name == NULL) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
207 printf("Menu %s not found\n",name);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
208 return NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
209 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
210 m = calloc(1,sizeof(menu_t));
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
211 m->priv_st = &(menu_list[i].type->priv_st);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
212 m->priv = m_struct_copy(m->priv_st,menu_list[i].cfg);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
213 if(menu_list[i].type->open(m,menu_list[i].args))
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
214 return m;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
215 if(m->priv)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
216 m_struct_free(m->priv_st,m->priv);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
217 free(m);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
218 printf("Menu %s: init failed\n",name);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
219 return NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
220 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
221
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
222 void menu_draw(menu_t* menu,mp_image_t* mpi) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
223 if(menu->show && menu->draw)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
224 menu->draw(menu,mpi);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
225 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
226
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
227 void menu_read_cmd(menu_t* menu,int cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
228 if(menu->read_cmd)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
229 menu->read_cmd(menu,cmd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
230 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
231
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
232 void menu_close(menu_t* menu) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
233 if(menu->close)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
234 menu->close(menu);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
235 if(menu->priv)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
236 m_struct_free(menu->priv_st,menu->priv);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
237 free(menu);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
238 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
239
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
240 void menu_read_key(menu_t* menu,int cmd) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
241 if(menu->read_key)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
242 menu->read_key(menu,cmd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
243 else
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
244 menu_dflt_read_key(menu,cmd);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
245 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
246
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
247 ///////////////////////////// Helpers ////////////////////////////////////
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
248
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
249 typedef void (*draw_alpha_f)(int w,int h, unsigned char* src, unsigned char *srca, int srcstride, unsigned char* dstbase,int dststride);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
250
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
251 inline static draw_alpha_f get_draw_alpha(uint32_t fmt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
252 switch(fmt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
253 case IMGFMT_BGR15:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
254 case IMGFMT_RGB15:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
255 return vo_draw_alpha_rgb15;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
256 case IMGFMT_BGR16:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
257 case IMGFMT_RGB16:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
258 return vo_draw_alpha_rgb16;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
259 case IMGFMT_BGR24:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
260 case IMGFMT_RGB24:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
261 return vo_draw_alpha_rgb24;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
262 case IMGFMT_BGR32:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
263 case IMGFMT_RGB32:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
264 return vo_draw_alpha_rgb32;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
265 case IMGFMT_YV12:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
266 case IMGFMT_I420:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
267 case IMGFMT_IYUV:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
268 case IMGFMT_YVU9:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
269 case IMGFMT_IF09:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
270 case IMGFMT_Y800:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
271 case IMGFMT_Y8:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
272 return vo_draw_alpha_yv12;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
273 case IMGFMT_YUY2:
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
274 return vo_draw_alpha_yuy2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
275 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
276
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
277 return NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
278 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
279
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
280 // return the real height of a char:
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
281 static inline int get_height(int c,int h){
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
282 int font;
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
283 if ((font=vo_font->font[c])>=0)
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
284 if(h<vo_font->pic_a[font]->h) h=vo_font->pic_a[font]->h;
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
285 return h;
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
286 }
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
287
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
288 #ifdef HAVE_FREETYPE
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
289 #define render_txt(t) { char* p = t; while(*p) render_one_glyph(vo_font,*p++); }
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
290 #else
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
291 #define render_txt(t)
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
292 #endif
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
293
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
294
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
295 void menu_draw_text(mp_image_t* mpi,char* txt, int x, int y) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
296 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
297 int font;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
298
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
299 if(!draw_alpha) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
300 printf("Unsupported outformat !!!!\n");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
301 return;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
302 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
303
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
304 render_txt(txt);
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
305
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
306 while (*txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
307 unsigned char c=*txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
308 if ((font=vo_font->font[c])>=0 && (x + vo_font->width[c] <= mpi->w) && (y + vo_font->pic_a[font]->h <= mpi->h))
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
309 draw_alpha(vo_font->width[c], vo_font->pic_a[font]->h,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
310 vo_font->pic_b[font]->bmp+vo_font->start[c],
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
311 vo_font->pic_a[font]->bmp+vo_font->start[c],
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
312 vo_font->pic_a[font]->w,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
313 mpi->planes[0] + y * mpi->stride[0] + x * (mpi->bpp>>3),
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
314 mpi->stride[0]);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
315 x+=vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
316 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
317
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
318 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
319
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
320 void menu_draw_text_full(mp_image_t* mpi,char* txt,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
321 int x, int y,int w, int h,
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
322 int vspace, int warp, int align, int anchor) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
323 int need_w,need_h;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
324 int sy, ymin, ymax;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
325 int sx, xmin, xmax, xmid, xrmin;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
326 int ll = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
327 int font;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
328 draw_alpha_f draw_alpha = get_draw_alpha(mpi->imgfmt);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
329
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
330 if(!draw_alpha) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
331 printf("Unsupported outformat !!!!\n");
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
332 return;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
333 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
334
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
335 render_txt(txt);
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
336
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
337 if(x > mpi->w || y > mpi->h)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
338 return;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
339
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
340 if(anchor & MENU_TEXT_VCENTER) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
341 if(h <= 0) h = mpi->h;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
342 ymin = y - h/2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
343 ymax = y + h/2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
344 } else if(anchor & MENU_TEXT_BOT) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
345 if(h <= 0) h = mpi->h - y;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
346 ymin = y - h;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
347 ymax = y;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
348 } else {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
349 if(h <= 0) h = mpi->h - y;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
350 ymin = y;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
351 ymax = y + h;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
352 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
353
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
354 if(anchor & MENU_TEXT_HCENTER) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
355 if(w <= 0) w = mpi->w;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
356 xmin = x - w/2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
357 xmax = x + w/2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
358 } else if(anchor & MENU_TEXT_RIGHT) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
359 if(w <= 0) w = mpi->w -x;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
360 xmin = x - w;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
361 xmax = x;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
362 } else {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
363 if(w <= 0) w = mpi->w -x;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
364 xmin = x;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
365 xmax = x + w;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
366 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
367
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
368 // How many space do we need to draw this ?
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
369 menu_text_size(txt,w,vspace,warp,&need_w,&need_h);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
370
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
371 // Find the first line
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
372 if(align & MENU_TEXT_VCENTER)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
373 sy = ymin + ((h - need_h)/2);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
374 else if(align & MENU_TEXT_BOT)
8232
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
375 sy = ymax - need_h - 1;
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
376 else
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
377 sy = y;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
378
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
379 #if 0
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
380 // Find the first col
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
381 if(align & MENU_TEXT_HCENTER)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
382 sx = xmin + ((w - need_w)/2);
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
383 else if(align & MENU_TEXT_RIGHT)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
384 sx = xmax - need_w;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
385 #endif
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
386
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
387 xmid = xmin + (xmax - xmin) / 2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
388 xrmin = xmin;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
389 // Clamp the bb to the mpi size
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
390 if(ymin < 0) ymin = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
391 if(xmin < 0) xmin = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
392 if(ymax > mpi->h) ymax = mpi->h;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
393 if(xmax > mpi->w) xmax = mpi->w;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
394
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
395 // Jump some the beginnig text if needed
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
396 while(sy < ymin && *txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
397 unsigned char c=*txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
398 if(c == '\n' || (warp && ll + vo_font->width[c] > w)) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
399 ll = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
400 sy += vo_font->height + vspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
401 if(c == '\n') continue;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
402 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
403 ll += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
404 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
405 if(*txt == '\0') // Nothing left to draw
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
406 return;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
407
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
408 while(sy < ymax && *txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
409 char* line_end = NULL;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
410 int n;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
411
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
412 if(txt[0] == '\n') { // New line
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
413 sy += vo_font->height + vspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
414 txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
415 continue;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
416 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
417
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
418 // Get the length and end of this line
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
419 for(n = 0, ll = 0 ; txt[n] != '\0' && txt[n] != '\n' ; n++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
420 unsigned char c = txt[n];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
421 if(warp && ll + vo_font->width[c] > w) break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
422 ll += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
423 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
424 line_end = &txt[n];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
425 ll -= vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
426
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
427
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
428 if(align & (MENU_TEXT_HCENTER|MENU_TEXT_RIGHT)) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
429 // Too long line
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
430 if(ll > xmax-xmin) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
431 if(align & MENU_TEXT_HCENTER) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
432 int mid = ll/2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
433 // Find the middle point
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
434 for(n--, ll = 0 ; n <= 0 ; n--) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
435 ll += vo_font->width[(int)txt[n]]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
436 if(ll - vo_font->charspace > mid) break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
437 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
438 ll -= vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
439 sx = xmid + mid - ll;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
440 } else// MENU_TEXT_RIGHT)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
441 sx = xmax + vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
442
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
443 // We are after the start point -> go back
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
444 if(sx > xmin) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
445 for(n-- ; n <= 0 ; n--) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
446 unsigned char c = txt[n];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
447 if(sx - vo_font->width[c] - vo_font->charspace < xmin) break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
448 sx -= vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
449 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
450 } else { // We are before the start point -> go forward
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
451 for( ; sx < xmin && (&txt[n]) != line_end ; n++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
452 unsigned char c = txt[n];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
453 sx += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
454 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
455 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
456 txt = &txt[n]; // Jump to the new start char
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
457 } else {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
458 if(align & MENU_TEXT_HCENTER)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
459 sx = xmid - ll/2;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
460 else
8232
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
461 sx = xmax - 1 - ll;
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
462 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
463 } else {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
464 for(sx = xrmin ; sx < xmin && txt != line_end ; txt++) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
465 unsigned char c = txt[n];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
466 sx += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
467 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
468 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
469
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
470 while(sx < xmax && txt != line_end) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
471 unsigned char c = *txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
472 font = vo_font->font[c];
8232
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
473 if(font >= 0) {
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
474 int cs = (vo_font->pic_a[font]->h - vo_font->height) / 2;
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
475 if ((sx + vo_font->width[c] < xmax) && (sy + vo_font->height < ymax) )
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
476 draw_alpha(vo_font->width[c], vo_font->height,
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
477 vo_font->pic_b[font]->bmp+vo_font->start[c] +
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
478 cs * vo_font->pic_a[font]->w,
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
479 vo_font->pic_a[font]->bmp+vo_font->start[c] +
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
480 cs * vo_font->pic_a[font]->w,
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
481 vo_font->pic_a[font]->w,
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
482 mpi->planes[0] + sy * mpi->stride[0] + sx * (mpi->bpp>>3),
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
483 mpi->stride[0]);
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
484 // else
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
485 //printf("Can't draw '%c'\n",c);
5bbbccba0ad5 Fix buffer overflow in a drawing func
albeu
parents: 8224
diff changeset
486 }
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
487 sx+=vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
488 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
489 txt = line_end;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
490 if(txt[0] == '\0') break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
491 sy += vo_font->height + vspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
492 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
493 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
494
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
495 int menu_text_length(char* txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
496 int l = 0;
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
497 render_txt(txt);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
498 while (*txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
499 unsigned char c=*txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
500 l += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
501 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
502 return l - vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
503 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
504
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
505 void menu_text_size(char* txt,int max_width, int vspace, int warp, int* _w, int* _h) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
506 int l = 1, i = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
507 int w = 0;
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
508
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
509 render_txt(txt);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
510 while (*txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
511 unsigned char c=*txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
512 if(c == '\n' || (warp && i + vo_font->width[c] >= max_width)) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
513 if(*txt)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
514 l++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
515 i = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
516 if(c == '\n') continue;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
517 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
518 i += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
519 if(i > w) w = i;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
520 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
521
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
522 *_w = w;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
523 *_h = (l-1) * (vo_font->height + vspace) + vo_font->height;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
524 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
525
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
526
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
527 int menu_text_num_lines(char* txt, int max_width) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
528 int l = 1, i = 0;
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
529 render_txt(txt);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
530 while (*txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
531 unsigned char c=*txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
532 if(c == '\n' || i + vo_font->width[c] > max_width) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
533 l++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
534 i = 0;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
535 if(c == '\n') continue;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
536 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
537 i += vo_font->width[c]+vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
538 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
539 return l;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
540 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
541
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
542 char* menu_text_get_next_line(char* txt, int max_width) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
543 int i = 0;
8224
fefc56153615 Fix freetype. Freetype is highly recommended for a nice output ;)
albeu
parents: 8197
diff changeset
544 render_txt(txt);
8197
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
545 while (*txt) {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
546 unsigned char c=*txt;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
547 if(c == '\n') {
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
548 txt++;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
549 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
550 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
551 i += vo_font->width[c];
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
552 if(i >= max_width)
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
553 break;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
554 i += vo_font->charspace;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
555 }
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
556 return txt;
b31caec933e9 OSD menus initial version
albeu
parents:
diff changeset
557 }