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 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
37 static int do_action(const m_option_t
* prop_list
, const char* name
,
38 int action
, void* arg
, void *ctx
) {
40 const m_option_t
* prop
;
41 m_property_action_t ka
;
43 if((sep
= strchr(name
,'/')) && sep
[1]) {
46 memcpy(base
,name
,len
);
48 prop
= m_option_list_find(prop_list
, base
);
52 action
= M_PROPERTY_KEY_ACTION
;
55 prop
= m_option_list_find(prop_list
, name
);
56 if(!prop
) return M_PROPERTY_UNKNOWN
;
57 r
= ((m_property_ctrl_f
)prop
->p
)(prop
,action
,arg
,ctx
);
58 if(action
== M_PROPERTY_GET_TYPE
&& r
< 0) {
59 if(!arg
) return M_PROPERTY_ERROR
;
60 *(const m_option_t
**)arg
= prop
;
66 int m_property_do(const m_option_t
* prop_list
, const char* name
,
67 int action
, void* arg
, void *ctx
) {
68 const m_option_t
* opt
;
74 case M_PROPERTY_PRINT
:
75 if((r
= do_action(prop_list
,name
,M_PROPERTY_PRINT
,arg
,ctx
)) >= 0)
77 // fallback on the default print for this type
78 case M_PROPERTY_TO_STRING
:
79 if((r
= do_action(prop_list
,name
,M_PROPERTY_TO_STRING
,arg
,ctx
)) !=
80 M_PROPERTY_NOT_IMPLEMENTED
)
82 // fallback on the options API. Get the type, value and print.
83 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET_TYPE
,&opt
,ctx
)) <= 0)
85 val
= calloc(1,opt
->type
->size
);
86 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET
,val
,ctx
)) <= 0) {
90 if(!arg
) return M_PROPERTY_ERROR
;
91 str
= m_option_print(opt
,val
);
93 *(char**)arg
= str
== (char*)-1 ? NULL
: str
;
94 return str
!= (char*)-1;
95 case M_PROPERTY_PARSE
:
96 // try the property own parsing func
97 if((r
= do_action(prop_list
,name
,M_PROPERTY_PARSE
,arg
,ctx
)) !=
98 M_PROPERTY_NOT_IMPLEMENTED
)
100 // fallback on the options API, get the type and parse.
101 if((r
= do_action(prop_list
,name
,M_PROPERTY_GET_TYPE
,&opt
,ctx
)) <= 0)
103 if(!arg
) return M_PROPERTY_ERROR
;
104 val
= calloc(1,opt
->type
->size
);
105 if((r
= m_option_parse(opt
,opt
->name
,arg
,val
,M_CONFIG_FILE
)) <= 0) {
109 r
= do_action(prop_list
,name
,M_PROPERTY_SET
,val
,ctx
);
110 m_option_free(opt
,val
);
114 return do_action(prop_list
,name
,action
,arg
,ctx
);
117 char* m_properties_expand_string(const m_option_t
* prop_list
,char* str
, void *ctx
) {
118 int l
,fr
=0,pos
=0,size
=strlen(str
)+512;
119 char *p
= NULL
,*e
,*ret
= malloc(size
), num_val
;
120 int skip
= 0, lvl
= 0, skip_lvl
= 0;
127 p
= "\x1b", l
= 1; break;
129 p
= "\n", l
= 1; break;
131 p
= "\r", l
= 1; break;
133 p
= "\t", l
= 1; break;
136 char num
[3] = { str
[2], str
[3], 0 };
138 num_val
= strtol(num
,&end
,16);
149 } else if(lvl
> 0 && str
[0] == ')') {
150 if(skip
&& lvl
<= skip_lvl
) skip
= 0;
152 } else if(str
[0] == '$' && str
[1] == '{' && (e
= strchr(str
+2,'}'))) {
155 memcpy(pname
,str
+2,pl
);
157 if(m_property_do(prop_list
, pname
,
158 M_PROPERTY_PRINT
, &p
, ctx
) >= 0 && p
)
159 l
= strlen(p
), fr
= 1;
163 } else if(str
[0] == '?' && str
[1] == '(' && (e
= strchr(str
+2,':'))) {
166 int is_not
= str
[2] == '!';
167 int pl
= e
- str
- (is_not
? 3 : 2);
169 memcpy(pname
, str
+ (is_not
? 3 : 2), pl
);
171 if(m_property_do(prop_list
,pname
,M_PROPERTY_GET
,NULL
,ctx
) < 0) {
173 skip
= 1, skip_lvl
= lvl
;
176 skip
= 1, skip_lvl
= lvl
;
180 p
= str
, l
= 1, str
++;
182 if(skip
|| l
<= 0) continue;
186 ret
= realloc(ret
,size
);
190 if(fr
) free(p
), fr
= 0;
197 void m_properties_print_help_list(const m_option_t
* list
) {
198 char min
[50],max
[50];
201 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_PropertyListHeader
);
202 for(i
= 0 ; list
[i
].name
; i
++) {
203 const m_option_t
* opt
= &list
[i
];
204 if(opt
->flags
& M_OPT_MIN
)
205 sprintf(min
,"%-8.0f",opt
->min
);
208 if(opt
->flags
& M_OPT_MAX
)
209 sprintf(max
,"%-8.0f",opt
->max
);
212 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, " %-20.20s %-15.15s %-10.10s %-10.10s\n",
219 mp_msg(MSGT_CFGPARSER
, MSGL_INFO
, MSGTR_TotalProperties
, count
);
222 // Some generic property implementations
224 int m_property_int_ro(const m_option_t
* prop
,int action
,
232 return M_PROPERTY_NOT_IMPLEMENTED
;
235 int m_property_int_range(const m_option_t
* prop
,int action
,
236 void* arg
,int* var
) {
240 M_PROPERTY_CLAMP(prop
,*(int*)arg
);
243 case M_PROPERTY_STEP_UP
:
244 case M_PROPERTY_STEP_DOWN
:
245 *var
+= (arg
? *(int*)arg
: 1) *
246 (action
== M_PROPERTY_STEP_DOWN
? -1 : 1);
247 M_PROPERTY_CLAMP(prop
,*var
);
250 return m_property_int_ro(prop
,action
,arg
,*var
);
253 int m_property_choice(const m_option_t
* prop
,int action
,
254 void* arg
,int* var
) {
256 case M_PROPERTY_STEP_UP
:
257 case M_PROPERTY_STEP_DOWN
:
258 *var
+= action
== M_PROPERTY_STEP_UP
? 1 : prop
->max
;
259 *var
%= (int)prop
->max
+1;
262 return m_property_int_range(prop
,action
,arg
,var
);
265 int m_property_flag_ro(const m_option_t
* prop
,int action
,
268 case M_PROPERTY_PRINT
:
270 *(char**)arg
= strdup((var
> prop
->min
) ? MSGTR_Enabled
: MSGTR_Disabled
);
273 return m_property_int_ro(prop
,action
,arg
,var
);
276 int m_property_flag(const m_option_t
* prop
,int action
,
277 void* arg
,int* var
) {
279 case M_PROPERTY_STEP_UP
:
280 case M_PROPERTY_STEP_DOWN
:
281 *var
= *var
== prop
->min
? prop
->max
: prop
->min
;
283 case M_PROPERTY_PRINT
:
284 return m_property_flag_ro(prop
, action
, arg
, *var
);
286 return m_property_int_range(prop
,action
,arg
,var
);
289 int m_property_float_ro(const m_option_t
* prop
,int action
,
290 void* arg
,float var
) {
296 case M_PROPERTY_PRINT
:
298 *(char**)arg
= malloc(20);
299 sprintf(*(char**)arg
,"%.2f",var
);
302 return M_PROPERTY_NOT_IMPLEMENTED
;
305 int m_property_float_range(const m_option_t
* prop
,int action
,
306 void* arg
,float* var
) {
310 M_PROPERTY_CLAMP(prop
,*(float*)arg
);
313 case M_PROPERTY_STEP_UP
:
314 case M_PROPERTY_STEP_DOWN
:
315 *var
+= (arg
? *(float*)arg
: 0.1) *
316 (action
== M_PROPERTY_STEP_DOWN
? -1 : 1);
317 M_PROPERTY_CLAMP(prop
,*var
);
320 return m_property_float_ro(prop
,action
,arg
,*var
);
323 int m_property_delay(const m_option_t
* prop
,int action
,
324 void* arg
,float* var
) {
326 case M_PROPERTY_PRINT
:
328 *(char**)arg
= malloc(20);
329 sprintf(*(char**)arg
,"%d ms",ROUND((*var
)*1000));
332 return m_property_float_range(prop
,action
,arg
,var
);
336 int m_property_double_ro(const m_option_t
* prop
,int action
,
337 void* arg
,double var
) {
343 case M_PROPERTY_PRINT
:
345 *(char**)arg
= malloc(20);
346 sprintf(*(char**)arg
,"%.2f",var
);
349 return M_PROPERTY_NOT_IMPLEMENTED
;
352 int m_property_time_ro(const m_option_t
* prop
,int action
,
353 void* arg
,double var
) {
355 case M_PROPERTY_PRINT
:
357 return M_PROPERTY_ERROR
;
364 *(char **) arg
= malloc(20);
366 sprintf(*(char **) arg
, "%d:%02d:%02d", h
, m
, s
);
368 sprintf(*(char **) arg
, "%d:%02d", m
, s
);
370 sprintf(*(char **) arg
, "%d", s
);
371 return M_PROPERTY_OK
;
374 return m_property_double_ro(prop
,action
,arg
,var
);
377 int m_property_string_ro(const m_option_t
* prop
,int action
,void* arg
,char* str
) {
383 case M_PROPERTY_PRINT
:
385 *(char**)arg
= str
? strdup(str
) : NULL
;
388 return M_PROPERTY_NOT_IMPLEMENTED
;
391 int m_property_bitrate(const m_option_t
* prop
,int action
,void* arg
,int rate
) {
393 case M_PROPERTY_PRINT
:
395 return M_PROPERTY_ERROR
;
396 *(char**)arg
= malloc (16);
397 sprintf(*(char**)arg
, "%d kbps", rate
*8/1000);
398 return M_PROPERTY_OK
;
400 return m_property_int_ro(prop
, action
, arg
, rate
);