ad_ffmpeg: return failure from init() if initial decode fails
[mplayer.git] / m_property.c
blob93fa8863fd0184004b20494573d6c6d75c0ad557
1 /*
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.
19 /// \file
20 /// \ingroup Properties
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <inttypes.h>
28 #include <unistd.h>
30 #include "m_option.h"
31 #include "m_property.h"
32 #include "mp_msg.h"
33 #include "mpcommon.h"
35 static int do_action(const m_option_t* prop_list, const char* name,
36 int action, void* arg, void *ctx) {
37 const char* sep;
38 const m_option_t* prop;
39 m_property_action_t ka;
40 int r;
41 if((sep = strchr(name,'/')) && sep[1]) {
42 int len = sep-name;
43 char base[len+1];
44 memcpy(base,name,len);
45 base[len] = 0;
46 prop = m_option_list_find(prop_list, base);
47 ka.key = sep+1;
48 ka.action = action;
49 ka.arg = arg;
50 action = M_PROPERTY_KEY_ACTION;
51 arg = &ka;
52 } else
53 prop = m_option_list_find(prop_list, name);
54 if(!prop) return M_PROPERTY_UNKNOWN;
55 r = ((m_property_ctrl_f)prop->p)(prop,action,arg,ctx);
56 if(action == M_PROPERTY_GET_TYPE && r < 0) {
57 if(!arg) return M_PROPERTY_ERROR;
58 *(const m_option_t**)arg = prop;
59 return M_PROPERTY_OK;
61 return r;
64 int m_property_do(const m_option_t* prop_list, const char* name,
65 int action, void* arg, void *ctx) {
66 const m_option_t* opt;
67 void* val;
68 char* str;
69 int r;
71 switch(action) {
72 case M_PROPERTY_PRINT:
73 if((r = do_action(prop_list,name,M_PROPERTY_PRINT,arg,ctx)) >= 0)
74 return r;
75 // fallback on the default print for this type
76 case M_PROPERTY_TO_STRING:
77 if((r = do_action(prop_list,name,M_PROPERTY_TO_STRING,arg,ctx)) !=
78 M_PROPERTY_NOT_IMPLEMENTED)
79 return r;
80 // fallback on the options API. Get the type, value and print.
81 if((r = do_action(prop_list,name,M_PROPERTY_GET_TYPE,&opt,ctx)) <= 0)
82 return r;
83 val = calloc(1,opt->type->size);
84 if((r = do_action(prop_list,name,M_PROPERTY_GET,val,ctx)) <= 0) {
85 free(val);
86 return r;
88 if(!arg) return M_PROPERTY_ERROR;
89 str = m_option_print(opt,val);
90 free(val);
91 *(char**)arg = str == (char*)-1 ? NULL : str;
92 return str != (char*)-1;
93 case M_PROPERTY_PARSE:
94 // try the property own parsing func
95 if((r = do_action(prop_list,name,M_PROPERTY_PARSE,arg,ctx)) !=
96 M_PROPERTY_NOT_IMPLEMENTED)
97 return r;
98 // fallback on the options API, get the type and parse.
99 if((r = do_action(prop_list,name,M_PROPERTY_GET_TYPE,&opt,ctx)) <= 0)
100 return r;
101 if(!arg) return M_PROPERTY_ERROR;
102 val = calloc(1,opt->type->size);
103 if((r = m_option_parse(opt,opt->name,arg,val,M_CONFIG_FILE)) <= 0) {
104 free(val);
105 return r;
107 r = do_action(prop_list,name,M_PROPERTY_SET,val,ctx);
108 m_option_free(opt,val);
109 free(val);
110 return r;
112 return do_action(prop_list,name,action,arg,ctx);
115 char* m_properties_expand_string(const m_option_t* prop_list,char* str, void *ctx) {
116 int l,fr=0,pos=0,size=strlen(str)+512;
117 char *p = NULL,*e,*ret = malloc(size), num_val;
118 int skip = 0, lvl = 0, skip_lvl = 0;
120 while(str[0]) {
121 if(str[0] == '\\') {
122 int sl = 1;
123 switch(str[1]) {
124 case 'e':
125 p = "\x1b", l = 1; break;
126 case 'n':
127 p = "\n", l = 1; break;
128 case 'r':
129 p = "\r", l = 1; break;
130 case 't':
131 p = "\t", l = 1; break;
132 case 'x':
133 if(str[2]) {
134 char num[3] = { str[2], str[3], 0 };
135 char* end = num;
136 num_val = strtol(num,&end,16);
137 sl = end-num;
138 l = 1;
139 p = &num_val;
140 } else
141 l = 0;
142 break;
143 default:
144 p = str+1, l = 1;
146 str+=1+sl;
147 } else if(lvl > 0 && str[0] == ')') {
148 if(skip && lvl <= skip_lvl) skip = 0;
149 lvl--, str++, l = 0;
150 } else if(str[0] == '$' && str[1] == '{' && (e = strchr(str+2,'}'))) {
151 int pl = e-str-2;
152 char pname[pl+1];
153 memcpy(pname,str+2,pl);
154 pname[pl] = 0;
155 if(m_property_do(prop_list, pname,
156 M_PROPERTY_PRINT, &p, ctx) >= 0 && p)
157 l = strlen(p), fr = 1;
158 else
159 l = 0;
160 str = e+1;
161 } else if(str[0] == '?' && str[1] == '(' && (e = strchr(str+2,':'))) {
162 lvl++;
163 if(!skip) {
164 int is_not = str[2] == '!';
165 int pl = e - str - (is_not ? 3 : 2);
166 char pname[pl+1];
167 memcpy(pname, str + (is_not ? 3 : 2), pl);
168 pname[pl] = 0;
169 if(m_property_do(prop_list,pname,M_PROPERTY_GET,NULL,ctx) < 0) {
170 if (!is_not)
171 skip = 1, skip_lvl = lvl;
173 else if (is_not)
174 skip = 1, skip_lvl = lvl;
176 str = e+1, l = 0;
177 } else
178 p = str, l = 1, str++;
180 if(skip || l <= 0) continue;
182 if(pos+l+1 > size) {
183 size = pos+l+512;
184 ret = realloc(ret,size);
186 memcpy(ret+pos,p,l);
187 pos += l;
188 if(fr) free(p), fr = 0;
191 ret[pos] = 0;
192 return ret;
195 void m_properties_print_help_list(const m_option_t* list) {
196 char min[50],max[50];
197 int i,count = 0;
199 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\n Name Type Min Max\n\n");
200 for(i = 0 ; list[i].name ; i++) {
201 const m_option_t* opt = &list[i];
202 if(opt->flags & M_OPT_MIN)
203 sprintf(min,"%-8.0f",opt->min);
204 else
205 strcpy(min,"No");
206 if(opt->flags & M_OPT_MAX)
207 sprintf(max,"%-8.0f",opt->max);
208 else
209 strcpy(max,"No");
210 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %-20.20s %-15.15s %-10.10s %-10.10s\n",
211 opt->name,
212 opt->type->name,
213 min,
214 max);
215 count++;
217 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\nTotal: %d properties\n", count);
220 // Some generic property implementations
222 int m_property_int_ro(const m_option_t* prop,int action,
223 void* arg,int var) {
224 switch(action) {
225 case M_PROPERTY_GET:
226 if(!arg) return 0;
227 *(int*)arg = var;
228 return 1;
230 return M_PROPERTY_NOT_IMPLEMENTED;
233 int m_property_int_range(const m_option_t* prop,int action,
234 void* arg,int* var) {
235 switch(action) {
236 case M_PROPERTY_SET:
237 if(!arg) return 0;
238 M_PROPERTY_CLAMP(prop,*(int*)arg);
239 *var = *(int*)arg;
240 return 1;
241 case M_PROPERTY_STEP_UP:
242 case M_PROPERTY_STEP_DOWN:
243 *var += (arg ? *(int*)arg : 1) *
244 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
245 M_PROPERTY_CLAMP(prop,*var);
246 return 1;
248 return m_property_int_ro(prop,action,arg,*var);
251 int m_property_choice(const m_option_t* prop,int action,
252 void* arg,int* var) {
253 switch(action) {
254 case M_PROPERTY_STEP_UP:
255 case M_PROPERTY_STEP_DOWN:
256 *var += action == M_PROPERTY_STEP_UP ? 1 : prop->max;
257 *var %= (int)prop->max+1;
258 return 1;
260 return m_property_int_range(prop,action,arg,var);
263 int m_property_flag_ro(const m_option_t* prop,int action,
264 void* arg,int var) {
265 switch(action) {
266 case M_PROPERTY_PRINT:
267 if(!arg) return 0;
268 *(char**)arg = strdup((var > prop->min) ?
269 mp_gtext("enabled") : mp_gtext("disabled"));
270 return 1;
272 return m_property_int_ro(prop,action,arg,var);
275 int m_property_flag(const m_option_t* prop,int action,
276 void* arg,int* var) {
277 switch(action) {
278 case M_PROPERTY_STEP_UP:
279 case M_PROPERTY_STEP_DOWN:
280 *var = *var == prop->min ? prop->max : prop->min;
281 return 1;
282 case M_PROPERTY_PRINT:
283 return m_property_flag_ro(prop, action, arg, *var);
285 return m_property_int_range(prop,action,arg,var);
288 int m_property_float_ro(const m_option_t* prop,int action,
289 void* arg,float var) {
290 switch(action) {
291 case M_PROPERTY_GET:
292 if(!arg) return 0;
293 *(float*)arg = var;
294 return 1;
295 case M_PROPERTY_PRINT:
296 if(!arg) return 0;
297 *(char**)arg = malloc(20);
298 sprintf(*(char**)arg,"%.2f",var);
299 return 1;
301 return M_PROPERTY_NOT_IMPLEMENTED;
304 int m_property_float_range(const m_option_t* prop,int action,
305 void* arg,float* var) {
306 switch(action) {
307 case M_PROPERTY_SET:
308 if(!arg) return 0;
309 M_PROPERTY_CLAMP(prop,*(float*)arg);
310 *var = *(float*)arg;
311 return 1;
312 case M_PROPERTY_STEP_UP:
313 case M_PROPERTY_STEP_DOWN:
314 *var += (arg ? *(float*)arg : 0.1) *
315 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
316 M_PROPERTY_CLAMP(prop,*var);
317 return 1;
319 return m_property_float_ro(prop,action,arg,*var);
322 int m_property_delay(const m_option_t* prop,int action,
323 void* arg,float* var) {
324 switch(action) {
325 case M_PROPERTY_PRINT:
326 if(!arg) return 0;
327 *(char**)arg = malloc(20);
328 sprintf(*(char**)arg,"%d ms",ROUND((*var)*1000));
329 return 1;
330 default:
331 return m_property_float_range(prop,action,arg,var);
335 int m_property_double_ro(const m_option_t* prop,int action,
336 void* arg,double var) {
337 switch(action) {
338 case M_PROPERTY_GET:
339 if(!arg) return 0;
340 *(double*)arg = var;
341 return 1;
342 case M_PROPERTY_PRINT:
343 if(!arg) return 0;
344 *(char**)arg = malloc(20);
345 sprintf(*(char**)arg,"%.2f",var);
346 return 1;
348 return M_PROPERTY_NOT_IMPLEMENTED;
351 int m_property_time_ro(const m_option_t* prop,int action,
352 void* arg,double var) {
353 switch(action) {
354 case M_PROPERTY_PRINT:
355 if (!arg)
356 return M_PROPERTY_ERROR;
357 else {
358 int h, m, s = var;
359 h = s / 3600;
360 s -= h * 3600;
361 m = s / 60;
362 s -= m * 60;
363 *(char **) arg = malloc(20);
364 if (h > 0)
365 sprintf(*(char **) arg, "%d:%02d:%02d", h, m, s);
366 else if (m > 0)
367 sprintf(*(char **) arg, "%d:%02d", m, s);
368 else
369 sprintf(*(char **) arg, "%d", s);
370 return M_PROPERTY_OK;
373 return m_property_double_ro(prop,action,arg,var);
376 int m_property_string_ro(const m_option_t* prop,int action,void* arg,char* str) {
377 switch(action) {
378 case M_PROPERTY_GET:
379 if(!arg) return 0;
380 *(char**)arg = str;
381 return 1;
382 case M_PROPERTY_PRINT:
383 if(!arg) return 0;
384 *(char**)arg = str ? strdup(str) : NULL;
385 return 1;
387 return M_PROPERTY_NOT_IMPLEMENTED;
390 int m_property_bitrate(const m_option_t* prop,int action,void* arg,int rate) {
391 switch(action) {
392 case M_PROPERTY_PRINT:
393 if (!arg)
394 return M_PROPERTY_ERROR;
395 *(char**)arg = malloc (16);
396 sprintf(*(char**)arg, "%d kbps", rate*8/1000);
397 return M_PROPERTY_OK;
399 return m_property_int_ro(prop, action, arg, rate);