3 /// \ingroup Properties
14 #include "m_property.h"
18 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
20 static int do_action(const m_option_t
* prop_list
, const char* name
,
21 int action
, void* arg
, void *ctx
) {
23 const m_option_t
* prop
;
24 m_property_action_t ka
;
26 if((sep
= strchr(name
,'/')) && sep
[1]) {
29 memcpy(base
,name
,len
);
31 prop
= m_option_list_find(prop_list
, base
);
35 action
= M_PROPERTY_KEY_ACTION
;
38 prop
= m_option_list_find(prop_list
, name
);
39 if(!prop
) return M_PROPERTY_UNKNOWN
;
40 r
= ((m_property_ctrl_f
)prop
->p
)(prop
,action
,arg
,ctx
);
41 if(action
== M_PROPERTY_GET_TYPE
&& r
< 0) {
42 if(!arg
) return M_PROPERTY_ERROR
;
43 *(const m_option_t
**)arg
= prop
;
49 int m_property_do(const m_option_t
* prop_list
, const char* name
,
50 int action
, void* arg
, void *ctx
) {
51 const m_option_t
* opt
;
57 case M_PROPERTY_PRINT
:
58 if((r
= do_action(prop_list
,name
,M_PROPERTY_PRINT
,arg
,ctx
)) >= 0)
60 // fallback on the default print for this type
61 case M_PROPERTY_TO_STRING
:
62 if((r
= do_action(prop_list
,name
,M_PROPERTY_TO_STRING
,arg
,ctx
)) !=
63 M_PROPERTY_NOT_IMPLEMENTED
)
65 // fallback on the options API. Get the type, value and print.
66 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET_TYPE
,&opt
,ctx
)) <= 0)
68 val
= calloc(1,opt
->type
->size
);
69 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET
,val
,ctx
)) <= 0) {
73 if(!arg
) return M_PROPERTY_ERROR
;
74 str
= m_option_print(opt
,val
);
76 *(char**)arg
= str
== (char*)-1 ? NULL
: str
;
77 return str
!= (char*)-1;
78 case M_PROPERTY_PARSE
:
79 // try the property own parsing func
80 if((r
= do_action(prop_list
,name
,M_PROPERTY_PARSE
,arg
,ctx
)) !=
81 M_PROPERTY_NOT_IMPLEMENTED
)
83 // fallback on the options API, get the type and parse.
84 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET_TYPE
,&opt
,ctx
)) <= 0)
86 if(!arg
) return M_PROPERTY_ERROR
;
87 val
= calloc(1,opt
->type
->size
);
88 if((r
= m_option_parse(opt
,opt
->name
,arg
,val
,M_CONFIG_FILE
)) <= 0) {
92 r
= do_action(prop_list
,name
,M_PROPERTY_SET
,val
,ctx
);
93 m_option_free(opt
,val
);
97 return do_action(prop_list
,name
,action
,arg
,ctx
);
100 char* m_properties_expand_string(const m_option_t
* prop_list
,char* str
, void *ctx
) {
101 int l
,fr
=0,pos
=0,size
=strlen(str
)+512;
102 char *p
= NULL
,*e
,*ret
= malloc(size
), num_val
;
103 int skip
= 0, lvl
= 0, skip_lvl
= 0;
110 p
= "\x1b", l
= 1; break;
112 p
= "\n", l
= 1; break;
114 p
= "\r", l
= 1; break;
116 p
= "\t", l
= 1; break;
119 char num
[3] = { str
[2], str
[3], 0 };
121 num_val
= strtol(num
,&end
,16);
132 } else if(lvl
> 0 && str
[0] == ')') {
133 if(skip
&& lvl
<= skip_lvl
) skip
= 0;
135 } else if(str
[0] == '$' && str
[1] == '{' && (e
= strchr(str
+2,'}'))) {
138 memcpy(pname
,str
+2,pl
);
140 if(m_property_do(prop_list
, pname
,
141 M_PROPERTY_PRINT
, &p
, ctx
) >= 0 && p
)
142 l
= strlen(p
), fr
= 1;
146 } else if(str
[0] == '?' && str
[1] == '(' && (e
= strchr(str
+2,':'))) {
149 int is_not
= str
[2] == '!';
150 int pl
= e
- str
- (is_not
? 3 : 2);
152 memcpy(pname
, str
+ (is_not
? 3 : 2), pl
);
154 if(m_property_do(prop_list
,pname
,M_PROPERTY_GET
,NULL
,ctx
) < 0) {
156 skip
= 1, skip_lvl
= lvl
;
159 skip
= 1, skip_lvl
= lvl
;
163 p
= str
, l
= 1, str
++;
165 if(skip
|| l
<= 0) continue;
169 ret
= realloc(ret
,size
);
173 if(fr
) free(p
), fr
= 0;
180 void m_properties_print_help_list(const m_option_t
* list
) {
181 char min
[50],max
[50];
184 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\n Name Type Min Max\n\n");
185 for(i
= 0 ; list
[i
].name
; i
++) {
186 const m_option_t
* opt
= &list
[i
];
187 if(opt
->flags
& M_OPT_MIN
)
188 sprintf(min
,"%-8.0f",opt
->min
);
191 if(opt
->flags
& M_OPT_MAX
)
192 sprintf(max
,"%-8.0f",opt
->max
);
195 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s\n",
202 mp_tmsg(MSGT_CFGPARSER
, MSGL_INFO
, "\nTotal: %d properties\n", count
);
205 // Some generic property implementations
207 int m_property_int_ro(const m_option_t
* prop
,int action
,
215 return M_PROPERTY_NOT_IMPLEMENTED
;
218 int m_property_int_range(const m_option_t
* prop
,int action
,
219 void* arg
,int* var
) {
223 M_PROPERTY_CLAMP(prop
,*(int*)arg
);
226 case M_PROPERTY_STEP_UP
:
227 case M_PROPERTY_STEP_DOWN
:
228 *var
+= (arg
? *(int*)arg
: 1) *
229 (action
== M_PROPERTY_STEP_DOWN
? -1 : 1);
230 M_PROPERTY_CLAMP(prop
,*var
);
233 return m_property_int_ro(prop
,action
,arg
,*var
);
236 int m_property_choice(const m_option_t
* prop
,int action
,
237 void* arg
,int* var
) {
239 case M_PROPERTY_STEP_UP
:
240 case M_PROPERTY_STEP_DOWN
:
241 *var
+= action
== M_PROPERTY_STEP_UP
? 1 : prop
->max
;
242 *var
%= (int)prop
->max
+1;
245 return m_property_int_range(prop
,action
,arg
,var
);
248 int m_property_flag_ro(const m_option_t
* prop
,int action
,
251 case M_PROPERTY_PRINT
:
253 *(char**)arg
= strdup((var
> prop
->min
) ? _("enabled") : _("disabled"));
256 return m_property_int_ro(prop
,action
,arg
,var
);
259 int m_property_flag(const m_option_t
* prop
,int action
,
260 void* arg
,int* var
) {
262 case M_PROPERTY_STEP_UP
:
263 case M_PROPERTY_STEP_DOWN
:
264 *var
= *var
== prop
->min
? prop
->max
: prop
->min
;
266 case M_PROPERTY_PRINT
:
267 return m_property_flag_ro(prop
, action
, arg
, *var
);
269 return m_property_int_range(prop
,action
,arg
,var
);
272 int m_property_float_ro(const m_option_t
* prop
,int action
,
273 void* arg
,float var
) {
279 case M_PROPERTY_PRINT
:
281 *(char**)arg
= malloc(20);
282 sprintf(*(char**)arg
,"%.2f",var
);
285 return M_PROPERTY_NOT_IMPLEMENTED
;
288 int m_property_float_range(const m_option_t
* prop
,int action
,
289 void* arg
,float* var
) {
293 M_PROPERTY_CLAMP(prop
,*(float*)arg
);
296 case M_PROPERTY_STEP_UP
:
297 case M_PROPERTY_STEP_DOWN
:
298 *var
+= (arg
? *(float*)arg
: 0.1) *
299 (action
== M_PROPERTY_STEP_DOWN
? -1 : 1);
300 M_PROPERTY_CLAMP(prop
,*var
);
303 return m_property_float_ro(prop
,action
,arg
,*var
);
306 int m_property_delay(const m_option_t
* prop
,int action
,
307 void* arg
,float* var
) {
309 case M_PROPERTY_PRINT
:
311 *(char**)arg
= malloc(20);
312 sprintf(*(char**)arg
,"%d ms",ROUND((*var
)*1000));
315 return m_property_float_range(prop
,action
,arg
,var
);
319 int m_property_double_ro(const m_option_t
* prop
,int action
,
320 void* arg
,double var
) {
326 case M_PROPERTY_PRINT
:
328 *(char**)arg
= malloc(20);
329 sprintf(*(char**)arg
,"%.2f",var
);
332 return M_PROPERTY_NOT_IMPLEMENTED
;
335 int m_property_time_ro(const m_option_t
* prop
,int action
,
336 void* arg
,double var
) {
338 case M_PROPERTY_PRINT
:
340 return M_PROPERTY_ERROR
;
347 *(char **) arg
= malloc(20);
349 sprintf(*(char **) arg
, "%d:%02d:%02d", h
, m
, s
);
351 sprintf(*(char **) arg
, "%d:%02d", m
, s
);
353 sprintf(*(char **) arg
, "%d", s
);
354 return M_PROPERTY_OK
;
357 return m_property_double_ro(prop
,action
,arg
,var
);
360 int m_property_string_ro(const m_option_t
* prop
,int action
,void* arg
,char* str
) {
366 case M_PROPERTY_PRINT
:
368 *(char**)arg
= str
? strdup(str
) : NULL
;
371 return M_PROPERTY_NOT_IMPLEMENTED
;
374 int m_property_bitrate(const m_option_t
* prop
,int action
,void* arg
,int rate
) {
376 case M_PROPERTY_PRINT
:
378 return M_PROPERTY_ERROR
;
379 *(char**)arg
= malloc (16);
380 sprintf(*(char**)arg
, "%d kbps", rate
*8/1000);
381 return M_PROPERTY_OK
;
383 return m_property_int_ro(prop
, action
, arg
, rate
);