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.
20 /// \ingroup Properties
31 #include "m_property.h"
35 static int do_action(const m_option_t
* prop_list
, const char* name
,
36 int action
, void* arg
, void *ctx
) {
38 const m_option_t
* prop
;
39 m_property_action_t ka
;
41 if((sep
= strchr(name
,'/')) && sep
[1]) {
44 memcpy(base
,name
,len
);
46 prop
= m_option_list_find(prop_list
, base
);
50 action
= M_PROPERTY_KEY_ACTION
;
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
;
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
;
72 case M_PROPERTY_PRINT
:
73 if((r
= do_action(prop_list
,name
,M_PROPERTY_PRINT
,arg
,ctx
)) >= 0)
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
)
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)
83 val
= calloc(1,opt
->type
->size
);
84 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET
,val
,ctx
)) <= 0) {
88 if(!arg
) return M_PROPERTY_ERROR
;
89 str
= m_option_print(opt
,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
)
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)
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) {
107 r
= do_action(prop_list
,name
,M_PROPERTY_SET
,val
,ctx
);
108 m_option_free(opt
,val
);
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;
125 p
= "\x1b", l
= 1; break;
127 p
= "\n", l
= 1; break;
129 p
= "\r", l
= 1; break;
131 p
= "\t", l
= 1; break;
134 char num
[3] = { str
[2], str
[3], 0 };
136 num_val
= strtol(num
,&end
,16);
147 } else if(lvl
> 0 && str
[0] == ')') {
148 if(skip
&& lvl
<= skip_lvl
) skip
= 0;
150 } else if(str
[0] == '$' && str
[1] == '{' && (e
= strchr(str
+2,'}'))) {
153 memcpy(pname
,str
+2,pl
);
155 if(m_property_do(prop_list
, pname
,
156 M_PROPERTY_PRINT
, &p
, ctx
) >= 0 && p
)
157 l
= strlen(p
), fr
= 1;
161 } else if(str
[0] == '?' && str
[1] == '(' && (e
= strchr(str
+2,':'))) {
164 int is_not
= str
[2] == '!';
165 int pl
= e
- str
- (is_not
? 3 : 2);
167 memcpy(pname
, str
+ (is_not
? 3 : 2), pl
);
169 if(m_property_do(prop_list
,pname
,M_PROPERTY_GET
,NULL
,ctx
) < 0) {
171 skip
= 1, skip_lvl
= lvl
;
174 skip
= 1, skip_lvl
= lvl
;
178 p
= str
, l
= 1, str
++;
180 if(skip
|| l
<= 0) continue;
184 ret
= realloc(ret
,size
);
188 if(fr
) free(p
), fr
= 0;
195 void m_properties_print_help_list(const m_option_t
* list
) {
196 char min
[50],max
[50];
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
);
206 if(opt
->flags
& M_OPT_MAX
)
207 sprintf(max
,"%-8.0f",opt
->max
);
210 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s\n",
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
,
230 return M_PROPERTY_NOT_IMPLEMENTED
;
233 int m_property_int_range(const m_option_t
* prop
,int action
,
234 void* arg
,int* var
) {
238 M_PROPERTY_CLAMP(prop
,*(int*)arg
);
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
);
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
) {
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;
260 return m_property_int_range(prop
,action
,arg
,var
);
263 int m_property_flag_ro(const m_option_t
* prop
,int action
,
266 case M_PROPERTY_PRINT
:
268 *(char**)arg
= strdup((var
> prop
->min
) ?
269 mp_gtext("enabled") : mp_gtext("disabled"));
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
) {
278 case M_PROPERTY_STEP_UP
:
279 case M_PROPERTY_STEP_DOWN
:
280 *var
= *var
== prop
->min
? prop
->max
: prop
->min
;
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
) {
295 case M_PROPERTY_PRINT
:
297 *(char**)arg
= malloc(20);
298 sprintf(*(char**)arg
,"%.2f",var
);
301 return M_PROPERTY_NOT_IMPLEMENTED
;
304 int m_property_float_range(const m_option_t
* prop
,int action
,
305 void* arg
,float* var
) {
309 M_PROPERTY_CLAMP(prop
,*(float*)arg
);
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
);
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
) {
325 case M_PROPERTY_PRINT
:
327 *(char**)arg
= malloc(20);
328 sprintf(*(char**)arg
,"%d ms",ROUND((*var
)*1000));
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
) {
342 case M_PROPERTY_PRINT
:
344 *(char**)arg
= malloc(20);
345 sprintf(*(char**)arg
,"%.2f",var
);
348 return M_PROPERTY_NOT_IMPLEMENTED
;
351 int m_property_time_ro(const m_option_t
* prop
,int action
,
352 void* arg
,double var
) {
354 case M_PROPERTY_PRINT
:
356 return M_PROPERTY_ERROR
;
363 *(char **) arg
= malloc(20);
365 sprintf(*(char **) arg
, "%d:%02d:%02d", h
, m
, s
);
367 sprintf(*(char **) arg
, "%d:%02d", m
, s
);
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
) {
382 case M_PROPERTY_PRINT
:
384 *(char**)arg
= str
? strdup(str
) : NULL
;
387 return M_PROPERTY_NOT_IMPLEMENTED
;
390 int m_property_bitrate(const m_option_t
* prop
,int action
,void* arg
,int rate
) {
392 case M_PROPERTY_PRINT
:
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
);