vo_kva: Prevent a parent window from painting over ours in slave mode
[mplayer/greg.git] / m_property.c
blob363e042f1cc687c207848b77955da5c0be2028f5
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"
34 #define ROUND(x) ((int)((x)<0 ? (x)-0.5 : (x)+0.5))
36 static int do_action(const m_option_t* prop_list, const char* name,
37 int action, void* arg, void *ctx) {
38 const char* sep;
39 const m_option_t* prop;
40 m_property_action_t ka;
41 int r;
42 if((sep = strchr(name,'/')) && sep[1]) {
43 int len = sep-name;
44 char base[len+1];
45 memcpy(base,name,len);
46 base[len] = 0;
47 prop = m_option_list_find(prop_list, base);
48 ka.key = sep+1;
49 ka.action = action;
50 ka.arg = arg;
51 action = M_PROPERTY_KEY_ACTION;
52 arg = &ka;
53 } else
54 prop = m_option_list_find(prop_list, name);
55 if(!prop) return M_PROPERTY_UNKNOWN;
56 r = ((m_property_ctrl_f)prop->p)(prop,action,arg,ctx);
57 if(action == M_PROPERTY_GET_TYPE && r < 0) {
58 if(!arg) return M_PROPERTY_ERROR;
59 *(const m_option_t**)arg = prop;
60 return M_PROPERTY_OK;
62 return r;
65 int m_property_do(const m_option_t* prop_list, const char* name,
66 int action, void* arg, void *ctx) {
67 const m_option_t* opt;
68 void* val;
69 char* str;
70 int r;
72 switch(action) {
73 case M_PROPERTY_PRINT:
74 if((r = do_action(prop_list,name,M_PROPERTY_PRINT,arg,ctx)) >= 0)
75 return r;
76 // fallback on the default print for this type
77 case M_PROPERTY_TO_STRING:
78 if((r = do_action(prop_list,name,M_PROPERTY_TO_STRING,arg,ctx)) !=
79 M_PROPERTY_NOT_IMPLEMENTED)
80 return r;
81 // fallback on the options API. Get the type, value and print.
82 if((r = do_action(prop_list,name,M_PROPERTY_GET_TYPE,&opt,ctx)) <= 0)
83 return r;
84 val = calloc(1,opt->type->size);
85 if((r = do_action(prop_list,name,M_PROPERTY_GET,val,ctx)) <= 0) {
86 free(val);
87 return r;
89 if(!arg) return M_PROPERTY_ERROR;
90 str = m_option_print(opt,val);
91 free(val);
92 *(char**)arg = str == (char*)-1 ? NULL : str;
93 return str != (char*)-1;
94 case M_PROPERTY_PARSE:
95 // try the property own parsing func
96 if((r = do_action(prop_list,name,M_PROPERTY_PARSE,arg,ctx)) !=
97 M_PROPERTY_NOT_IMPLEMENTED)
98 return r;
99 // fallback on the options API, get the type and parse.
100 if((r = do_action(prop_list,name,M_PROPERTY_GET_TYPE,&opt,ctx)) <= 0)
101 return r;
102 if(!arg) return M_PROPERTY_ERROR;
103 val = calloc(1,opt->type->size);
104 if((r = m_option_parse(opt,opt->name,arg,val,M_CONFIG_FILE)) <= 0) {
105 free(val);
106 return r;
108 r = do_action(prop_list,name,M_PROPERTY_SET,val,ctx);
109 m_option_free(opt,val);
110 free(val);
111 return r;
113 return do_action(prop_list,name,action,arg,ctx);
116 char* m_properties_expand_string(const m_option_t* prop_list,char* str, void *ctx) {
117 int l,fr=0,pos=0,size=strlen(str)+512;
118 char *p = NULL,*e,*ret = malloc(size), num_val;
119 int skip = 0, lvl = 0, skip_lvl = 0;
121 while(str[0]) {
122 if(str[0] == '\\') {
123 int sl = 1;
124 switch(str[1]) {
125 case 'e':
126 p = "\x1b", l = 1; break;
127 case 'n':
128 p = "\n", l = 1; break;
129 case 'r':
130 p = "\r", l = 1; break;
131 case 't':
132 p = "\t", l = 1; break;
133 case 'x':
134 if(str[2]) {
135 char num[3] = { str[2], str[3], 0 };
136 char* end = num;
137 num_val = strtol(num,&end,16);
138 sl = end-num;
139 l = 1;
140 p = &num_val;
141 } else
142 l = 0;
143 break;
144 default:
145 p = str+1, l = 1;
147 str+=1+sl;
148 } else if(lvl > 0 && str[0] == ')') {
149 if(skip && lvl <= skip_lvl) skip = 0;
150 lvl--, str++, l = 0;
151 } else if(str[0] == '$' && str[1] == '{' && (e = strchr(str+2,'}'))) {
152 int pl = e-str-2;
153 char pname[pl+1];
154 memcpy(pname,str+2,pl);
155 pname[pl] = 0;
156 if(m_property_do(prop_list, pname,
157 M_PROPERTY_PRINT, &p, ctx) >= 0 && p)
158 l = strlen(p), fr = 1;
159 else
160 l = 0;
161 str = e+1;
162 } else if(str[0] == '?' && str[1] == '(' && (e = strchr(str+2,':'))) {
163 lvl++;
164 if(!skip) {
165 int is_not = str[2] == '!';
166 int pl = e - str - (is_not ? 3 : 2);
167 char pname[pl+1];
168 memcpy(pname, str + (is_not ? 3 : 2), pl);
169 pname[pl] = 0;
170 if(m_property_do(prop_list,pname,M_PROPERTY_GET,NULL,ctx) < 0) {
171 if (!is_not)
172 skip = 1, skip_lvl = lvl;
174 else if (is_not)
175 skip = 1, skip_lvl = lvl;
177 str = e+1, l = 0;
178 } else
179 p = str, l = 1, str++;
181 if(skip || l <= 0) continue;
183 if(pos+l+1 > size) {
184 size = pos+l+512;
185 ret = realloc(ret,size);
187 memcpy(ret+pos,p,l);
188 pos += l;
189 if(fr) free(p), fr = 0;
192 ret[pos] = 0;
193 return ret;
196 void m_properties_print_help_list(const m_option_t* list) {
197 char min[50],max[50];
198 int i,count = 0;
200 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\n Name Type Min Max\n\n");
201 for(i = 0 ; list[i].name ; i++) {
202 const m_option_t* opt = &list[i];
203 if(opt->flags & M_OPT_MIN)
204 sprintf(min,"%-8.0f",opt->min);
205 else
206 strcpy(min,"No");
207 if(opt->flags & M_OPT_MAX)
208 sprintf(max,"%-8.0f",opt->max);
209 else
210 strcpy(max,"No");
211 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %-20.20s %-15.15s %-10.10s %-10.10s\n",
212 opt->name,
213 opt->type->name,
214 min,
215 max);
216 count++;
218 mp_tmsg(MSGT_CFGPARSER, MSGL_INFO, "\nTotal: %d properties\n", count);
221 // Some generic property implementations
223 int m_property_int_ro(const m_option_t* prop,int action,
224 void* arg,int var) {
225 switch(action) {
226 case M_PROPERTY_GET:
227 if(!arg) return 0;
228 *(int*)arg = var;
229 return 1;
231 return M_PROPERTY_NOT_IMPLEMENTED;
234 int m_property_int_range(const m_option_t* prop,int action,
235 void* arg,int* var) {
236 switch(action) {
237 case M_PROPERTY_SET:
238 if(!arg) return 0;
239 M_PROPERTY_CLAMP(prop,*(int*)arg);
240 *var = *(int*)arg;
241 return 1;
242 case M_PROPERTY_STEP_UP:
243 case M_PROPERTY_STEP_DOWN:
244 *var += (arg ? *(int*)arg : 1) *
245 (action == M_PROPERTY_STEP_DOWN ? -1 : 1);
246 M_PROPERTY_CLAMP(prop,*var);
247 return 1;
249 return m_property_int_ro(prop,action,arg,*var);
252 int m_property_choice(const m_option_t* prop,int action,
253 void* arg,int* var) {
254 switch(action) {
255 case M_PROPERTY_STEP_UP:
256 case M_PROPERTY_STEP_DOWN:
257 *var += action == M_PROPERTY_STEP_UP ? 1 : prop->max;
258 *var %= (int)prop->max+1;
259 return 1;
261 return m_property_int_range(prop,action,arg,var);
264 int m_property_flag_ro(const m_option_t* prop,int action,
265 void* arg,int var) {
266 switch(action) {
267 case M_PROPERTY_PRINT:
268 if(!arg) return 0;
269 *(char**)arg = strdup((var > prop->min) ?
270 mp_gtext("enabled") : mp_gtext("disabled"));
271 return 1;
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) {
278 switch(action) {
279 case M_PROPERTY_STEP_UP:
280 case M_PROPERTY_STEP_DOWN:
281 *var = *var == prop->min ? prop->max : prop->min;
282 return 1;
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) {
291 switch(action) {
292 case M_PROPERTY_GET:
293 if(!arg) return 0;
294 *(float*)arg = var;
295 return 1;
296 case M_PROPERTY_PRINT:
297 if(!arg) return 0;
298 *(char**)arg = malloc(20);
299 sprintf(*(char**)arg,"%.2f",var);
300 return 1;
302 return M_PROPERTY_NOT_IMPLEMENTED;
305 int m_property_float_range(const m_option_t* prop,int action,
306 void* arg,float* var) {
307 switch(action) {
308 case M_PROPERTY_SET:
309 if(!arg) return 0;
310 M_PROPERTY_CLAMP(prop,*(float*)arg);
311 *var = *(float*)arg;
312 return 1;
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);
318 return 1;
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) {
325 switch(action) {
326 case M_PROPERTY_PRINT:
327 if(!arg) return 0;
328 *(char**)arg = malloc(20);
329 sprintf(*(char**)arg,"%d ms",ROUND((*var)*1000));
330 return 1;
331 default:
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) {
338 switch(action) {
339 case M_PROPERTY_GET:
340 if(!arg) return 0;
341 *(double*)arg = var;
342 return 1;
343 case M_PROPERTY_PRINT:
344 if(!arg) return 0;
345 *(char**)arg = malloc(20);
346 sprintf(*(char**)arg,"%.2f",var);
347 return 1;
349 return M_PROPERTY_NOT_IMPLEMENTED;
352 int m_property_time_ro(const m_option_t* prop,int action,
353 void* arg,double var) {
354 switch(action) {
355 case M_PROPERTY_PRINT:
356 if (!arg)
357 return M_PROPERTY_ERROR;
358 else {
359 int h, m, s = var;
360 h = s / 3600;
361 s -= h * 3600;
362 m = s / 60;
363 s -= m * 60;
364 *(char **) arg = malloc(20);
365 if (h > 0)
366 sprintf(*(char **) arg, "%d:%02d:%02d", h, m, s);
367 else if (m > 0)
368 sprintf(*(char **) arg, "%d:%02d", m, s);
369 else
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) {
378 switch(action) {
379 case M_PROPERTY_GET:
380 if(!arg) return 0;
381 *(char**)arg = str;
382 return 1;
383 case M_PROPERTY_PRINT:
384 if(!arg) return 0;
385 *(char**)arg = str ? strdup(str) : NULL;
386 return 1;
388 return M_PROPERTY_NOT_IMPLEMENTED;
391 int m_property_bitrate(const m_option_t* prop,int action,void* arg,int rate) {
392 switch(action) {
393 case M_PROPERTY_PRINT:
394 if (!arg)
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);