Ignore svn changes up to r30255
[mplayer/glamo.git] / m_property.c
blobb296aa1b879168036bfb851347087e1a9b5d108c
2 /// \file
3 /// \ingroup Properties
5 #include "config.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <inttypes.h>
11 #include <unistd.h>
13 #include "m_option.h"
14 #include "m_property.h"
15 #include "mp_msg.h"
16 #include "help_mp.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) {
22 const char* sep;
23 const m_option_t* prop;
24 m_property_action_t ka;
25 int r;
26 if((sep = strchr(name,'/')) && sep[1]) {
27 int len = sep-name;
28 char base[len+1];
29 memcpy(base,name,len);
30 base[len] = 0;
31 prop = m_option_list_find(prop_list, base);
32 ka.key = sep+1;
33 ka.action = action;
34 ka.arg = arg;
35 action = M_PROPERTY_KEY_ACTION;
36 arg = &ka;
37 } else
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;
44 return M_PROPERTY_OK;
46 return r;
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;
52 void* val;
53 char* str;
54 int r;
56 switch(action) {
57 case M_PROPERTY_PRINT:
58 if((r = do_action(prop_list,name,M_PROPERTY_PRINT,arg,ctx)) >= 0)
59 return r;
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)
64 return r;
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)
67 return r;
68 val = calloc(1,opt->type->size);
69 if((r = do_action(prop_list,name,M_PROPERTY_GET,val,ctx)) <= 0) {
70 free(val);
71 return r;
73 if(!arg) return M_PROPERTY_ERROR;
74 str = m_option_print(opt,val);
75 free(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)
82 return r;
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)
85 return r;
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) {
89 free(val);
90 return r;
92 r = do_action(prop_list,name,M_PROPERTY_SET,val,ctx);
93 m_option_free(opt,val);
94 free(val);
95 return r;
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;
105 while(str[0]) {
106 if(str[0] == '\\') {
107 int sl = 1;
108 switch(str[1]) {
109 case 'e':
110 p = "\x1b", l = 1; break;
111 case 'n':
112 p = "\n", l = 1; break;
113 case 'r':
114 p = "\r", l = 1; break;
115 case 't':
116 p = "\t", l = 1; break;
117 case 'x':
118 if(str[2]) {
119 char num[3] = { str[2], str[3], 0 };
120 char* end = num;
121 num_val = strtol(num,&end,16);
122 sl = end-num;
123 l = 1;
124 p = &num_val;
125 } else
126 l = 0;
127 break;
128 default:
129 p = str+1, l = 1;
131 str+=1+sl;
132 } else if(lvl > 0 && str[0] == ')') {
133 if(skip && lvl <= skip_lvl) skip = 0;
134 lvl--, str++, l = 0;
135 } else if(str[0] == '$' && str[1] == '{' && (e = strchr(str+2,'}'))) {
136 int pl = e-str-2;
137 char pname[pl+1];
138 memcpy(pname,str+2,pl);
139 pname[pl] = 0;
140 if(m_property_do(prop_list, pname,
141 M_PROPERTY_PRINT, &p, ctx) >= 0 && p)
142 l = strlen(p), fr = 1;
143 else
144 l = 0;
145 str = e+1;
146 } else if(str[0] == '?' && str[1] == '(' && (e = strchr(str+2,':'))) {
147 lvl++;
148 if(!skip) {
149 int is_not = str[2] == '!';
150 int pl = e - str - (is_not ? 3 : 2);
151 char pname[pl+1];
152 memcpy(pname, str + (is_not ? 3 : 2), pl);
153 pname[pl] = 0;
154 if(m_property_do(prop_list,pname,M_PROPERTY_GET,NULL,ctx) < 0) {
155 if (!is_not)
156 skip = 1, skip_lvl = lvl;
158 else if (is_not)
159 skip = 1, skip_lvl = lvl;
161 str = e+1, l = 0;
162 } else
163 p = str, l = 1, str++;
165 if(skip || l <= 0) continue;
167 if(pos+l+1 > size) {
168 size = pos+l+512;
169 ret = realloc(ret,size);
171 memcpy(ret+pos,p,l);
172 pos += l;
173 if(fr) free(p), fr = 0;
176 ret[pos] = 0;
177 return ret;
180 void m_properties_print_help_list(const m_option_t* list) {
181 char min[50],max[50];
182 int i,count = 0;
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);
189 else
190 strcpy(min,"No");
191 if(opt->flags & M_OPT_MAX)
192 sprintf(max,"%-8.0f",opt->max);
193 else
194 strcpy(max,"No");
195 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %-20.20s %-15.15s %-10.10s %-10.10s\n",
196 opt->name,
197 opt->type->name,
198 min,
199 max);
200 count++;
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,
208 void* arg,int var) {
209 switch(action) {
210 case M_PROPERTY_GET:
211 if(!arg) return 0;
212 *(int*)arg = var;
213 return 1;
215 return M_PROPERTY_NOT_IMPLEMENTED;
218 int m_property_int_range(const m_option_t* prop,int action,
219 void* arg,int* var) {
220 switch(action) {
221 case M_PROPERTY_SET:
222 if(!arg) return 0;
223 M_PROPERTY_CLAMP(prop,*(int*)arg);
224 *var = *(int*)arg;
225 return 1;
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);
231 return 1;
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) {
238 switch(action) {
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;
243 return 1;
245 return m_property_int_range(prop,action,arg,var);
248 int m_property_flag_ro(const m_option_t* prop,int action,
249 void* arg,int var) {
250 switch(action) {
251 case M_PROPERTY_PRINT:
252 if(!arg) return 0;
253 *(char**)arg = strdup((var > prop->min) ?
254 mp_gtext("enabled") : mp_gtext("disabled"));
255 return 1;
257 return m_property_int_ro(prop,action,arg,var);
260 int m_property_flag(const m_option_t* prop,int action,
261 void* arg,int* var) {
262 switch(action) {
263 case M_PROPERTY_STEP_UP:
264 case M_PROPERTY_STEP_DOWN:
265 *var = *var == prop->min ? prop->max : prop->min;
266 return 1;
267 case M_PROPERTY_PRINT:
268 return m_property_flag_ro(prop, action, arg, *var);
270 return m_property_int_range(prop,action,arg,var);
273 int m_property_float_ro(const m_option_t* prop,int action,
274 void* arg,float var) {
275 switch(action) {
276 case M_PROPERTY_GET:
277 if(!arg) return 0;
278 *(float*)arg = var;
279 return 1;
280 case M_PROPERTY_PRINT:
281 if(!arg) return 0;
282 *(char**)arg = malloc(20);
283 sprintf(*(char**)arg,"%.2f",var);
284 return 1;
286 return M_PROPERTY_NOT_IMPLEMENTED;
289 int m_property_float_range(const m_option_t* prop,int action,
290 void* arg,float* var) {
291 switch(action) {
292 case M_PROPERTY_SET:
293 if(!arg) return 0;
294 M_PROPERTY_CLAMP(prop,*(float*)arg);
295 *var = *(float*)arg;
296 return 1;
297 case M_PROPERTY_STEP_UP:
298 case M_PROPERTY_STEP_DOWN:
299 *var += (arg ? *(float*)arg : 0.1) *
300 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
301 M_PROPERTY_CLAMP(prop,*var);
302 return 1;
304 return m_property_float_ro(prop,action,arg,*var);
307 int m_property_delay(const m_option_t* prop,int action,
308 void* arg,float* var) {
309 switch(action) {
310 case M_PROPERTY_PRINT:
311 if(!arg) return 0;
312 *(char**)arg = malloc(20);
313 sprintf(*(char**)arg,"%d ms",ROUND((*var)*1000));
314 return 1;
315 default:
316 return m_property_float_range(prop,action,arg,var);
320 int m_property_double_ro(const m_option_t* prop,int action,
321 void* arg,double var) {
322 switch(action) {
323 case M_PROPERTY_GET:
324 if(!arg) return 0;
325 *(double*)arg = var;
326 return 1;
327 case M_PROPERTY_PRINT:
328 if(!arg) return 0;
329 *(char**)arg = malloc(20);
330 sprintf(*(char**)arg,"%.2f",var);
331 return 1;
333 return M_PROPERTY_NOT_IMPLEMENTED;
336 int m_property_time_ro(const m_option_t* prop,int action,
337 void* arg,double var) {
338 switch(action) {
339 case M_PROPERTY_PRINT:
340 if (!arg)
341 return M_PROPERTY_ERROR;
342 else {
343 int h, m, s = var;
344 h = s / 3600;
345 s -= h * 3600;
346 m = s / 60;
347 s -= m * 60;
348 *(char **) arg = malloc(20);
349 if (h > 0)
350 sprintf(*(char **) arg, "%d:%02d:%02d", h, m, s);
351 else if (m > 0)
352 sprintf(*(char **) arg, "%d:%02d", m, s);
353 else
354 sprintf(*(char **) arg, "%d", s);
355 return M_PROPERTY_OK;
358 return m_property_double_ro(prop,action,arg,var);
361 int m_property_string_ro(const m_option_t* prop,int action,void* arg,char* str) {
362 switch(action) {
363 case M_PROPERTY_GET:
364 if(!arg) return 0;
365 *(char**)arg = str;
366 return 1;
367 case M_PROPERTY_PRINT:
368 if(!arg) return 0;
369 *(char**)arg = str ? strdup(str) : NULL;
370 return 1;
372 return M_PROPERTY_NOT_IMPLEMENTED;
375 int m_property_bitrate(const m_option_t* prop,int action,void* arg,int rate) {
376 switch(action) {
377 case M_PROPERTY_PRINT:
378 if (!arg)
379 return M_PROPERTY_ERROR;
380 *(char**)arg = malloc (16);
381 sprintf(*(char**)arg, "%d kbps", rate*8/1000);
382 return M_PROPERTY_OK;
384 return m_property_int_ro(prop, action, arg, rate);