1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*****************************************************************
* gmerlin - a general purpose multimedia framework and applications
*
* Copyright (c) 2001 - 2024 Members of the Gmerlin project
* http://github.com/bplaum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* *****************************************************************/
#include <string.h>
#include <uuid/uuid.h>
#include <config.h>
#include <gavl/gavl.h>
#include <gavl/utils.h>
#include <gmerlin/utils.h>
#include <gmerlin/parameter.h>
#include <gmerlin/bgmsg.h>
#include <gmerlin/bggavl.h>
#include <gmerlin/state.h>
#include <gmerlin/translation.h>
#include <gmerlin/log.h>
#define LOG_DOMAIN "state"
#define DESC_MIN "min"
#define DESC_MAX "max"
#define DESC_DICT "$desc"
typedef struct
{
int id;
bg_msg_sink_t * sink;
const char * ctx;
const gavl_dictionary_t * child;
} apply_t;
static const gavl_dictionary_t * state_var_get_desc(const gavl_dictionary_t * dict, const char * ctx, const char * var)
{
if(!(dict = gavl_dictionary_get_dictionary(dict, DESC_DICT)) ||
!(dict = gavl_dictionary_get_recursive(dict, ctx)) ||
!(dict = gavl_dictionary_get_dictionary(dict, var)))
return NULL;
return dict;
}
static gavl_dictionary_t * state_var_get_desc_create(gavl_dictionary_t * dict, const char * ctx, const char * var)
{
if(!(dict = gavl_dictionary_get_dictionary_create(dict, DESC_DICT)) ||
!(dict = gavl_dictionary_get_recursive_create(dict, ctx)) ||
!(dict = gavl_dictionary_get_dictionary_create(dict, var)))
return NULL;
return dict;
}
static void apply_func_child(void * priv, const char * name, const gavl_value_t * val)
{
gavl_msg_t * msg;
apply_t * a = priv;
msg = bg_msg_sink_get(a->sink);
gavl_msg_set_state(msg, a->id, gavl_dictionary_is_last(a->child, name), a->ctx, name, val);
bg_msg_sink_put(a->sink);
}
void bg_state_apply_ctx(gavl_dictionary_t * state, const char * ctx, bg_msg_sink_t * sink, int id)
{
apply_t a;
memset(&a, 0, sizeof(a));
a.id = id;
a.sink = sink;
a.ctx = ctx;
a.child = gavl_dictionary_get_recursive_create(state, ctx);
gavl_dictionary_foreach(a.child, apply_func_child, &a);
}
static void apply_func(void * priv, const char * name, const gavl_value_t * val)
{
apply_t * a = priv;
// if(gavl_string_starts_with(name
if(gavl_string_starts_with(name, DESC_DICT))
{
/* NOP */
}
else
{
a->ctx = name;
a->child = val->v.dictionary;
gavl_dictionary_foreach(a->child, apply_func_child, a);
}
}
void bg_state_apply(gavl_dictionary_t * state, bg_msg_sink_t * sink, int id)
{
apply_t a;
a.id = id;
a.sink = sink;
gavl_dictionary_foreach(state, apply_func, &a);
}
int bg_state_set(gavl_dictionary_t * state,
int last,
const char * ctx,
const char * var,
const gavl_value_t * val,
bg_msg_sink_t * sink, int id)
{
int changed = 0;
gavl_dictionary_t * child;
if(!ctx)
ctx = "/";
if(state)
{
child = gavl_dictionary_get_recursive_create(state, ctx);
changed = gavl_dictionary_set(child, var, val);
}
else
changed = 1;
if(sink && (last || changed))
{
gavl_msg_t * msg = bg_msg_sink_get(sink);
gavl_msg_set_state(msg, id, last, ctx, var, val);
bg_msg_sink_put(sink);
// fprintf(stderr, "State changed %s %s\n", ctx, var);
}
return changed;
}
const gavl_value_t * bg_state_get(const gavl_dictionary_t * state,
const char * ctx,
const char * var)
{
const gavl_dictionary_t * child;
if(!strcmp(ctx, "/"))
child = state;
else if(!(child = gavl_dictionary_get_recursive(state, ctx)))
{
// fprintf(stderr, "No such ctx %s\n", ctx);
return NULL;
}
return gavl_dictionary_get(child, var);
}
void bg_state_set_range(gavl_dictionary_t * state,
const char * ctx, const char * var,
const gavl_value_t * min,
const gavl_value_t * max)
{
gavl_dictionary_t * desc;
if(!min || (min->type == GAVL_TYPE_UNDEFINED) ||
!max || (max->type == GAVL_TYPE_UNDEFINED))
{
gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Invalid range passed to bg_state_set_range");
return;
}
desc = state_var_get_desc_create(state, ctx, var);
gavl_dictionary_set(desc, DESC_MIN, min);
gavl_dictionary_set(desc, DESC_MAX, max);
}
int bg_state_get_range(gavl_dictionary_t * state,
const char * ctx, const char * var,
gavl_value_t * min,
gavl_value_t * max)
{
const gavl_dictionary_t * desc;
const gavl_value_t * val;
if(!(desc = state_var_get_desc(state, ctx, var)))
return 0;
if(!(val = gavl_dictionary_get(desc, DESC_MIN)))
return 0;
gavl_value_copy(min, val);
if(!(val = gavl_dictionary_get(desc, DESC_MAX)))
return 0;
gavl_value_copy(max, val);
return 1;
}
void bg_state_set_range_int(gavl_dictionary_t * state,
const char * ctx, const char * var,
int min, int max)
{
gavl_value_t val_min;
gavl_value_t val_max;
gavl_value_init(&val_min);
gavl_value_init(&val_max);
gavl_value_set_int(&val_min, min);
gavl_value_set_int(&val_max, max);
bg_state_set_range(state, ctx, var, &val_min, &val_max);
}
void bg_state_set_range_long(gavl_dictionary_t * state,
const char * ctx, const char * var,
int64_t min, int64_t max)
{
gavl_value_t val_min;
gavl_value_t val_max;
gavl_value_init(&val_min);
gavl_value_init(&val_max);
gavl_value_set_long(&val_min, min);
gavl_value_set_long(&val_max, max);
bg_state_set_range(state, ctx, var, &val_min, &val_max);
}
void bg_state_set_range_float(gavl_dictionary_t * state,
const char * ctx, const char * var,
double min, double max)
{
gavl_value_t val_min;
gavl_value_t val_max;
gavl_value_init(&val_min);
gavl_value_init(&val_max);
gavl_value_set_float(&val_min, min);
gavl_value_set_float(&val_max, max);
bg_state_set_range(state, ctx, var, &val_min, &val_max);
}
void bg_state_init_ctx(gavl_dictionary_t * state,
const char * ctx,
const bg_state_var_desc_t * desc)
{
int i = 0;
while(desc[i].name)
{
if((desc[i].val_min.type != GAVL_TYPE_UNDEFINED) &&
(desc[i].val_max.type != GAVL_TYPE_UNDEFINED))
{
bg_state_set_range(state, ctx, desc[i].name,
&desc[i].val_min, &desc[i].val_max);
}
// fprintf(stderr, "State init ctx: %p %s %s\n", state, ctx, desc[i].name);
if(desc[i].val_default.type != GAVL_TYPE_UNDEFINED)
bg_state_set(state,
1,
ctx,
desc[i].name,
&desc[i].val_default,
NULL, 0);
else
{
gavl_value_t val;
gavl_value_init(&val);
gavl_value_set_type(&val, desc[i].type);
bg_state_set(state,
1,
ctx,
desc[i].name,
&val, NULL, 0);
gavl_value_free(&val);
}
i++;
}
}
static void check_max(gavl_value_t * val1, const gavl_value_t * max)
{
switch(val1->type)
{
case GAVL_TYPE_INT:
{
int max_i;
if(gavl_value_get_int(max, &max_i) &&
(val1->v.i > max_i))
val1->v.i = max_i;
}
break;
case GAVL_TYPE_LONG:
{
int64_t max_l;
if(gavl_value_get_long(max, &max_l) &&
(val1->v.l > max_l))
val1->v.l = max_l;
}
break;
case GAVL_TYPE_FLOAT:
{
double max_d;
if(gavl_value_get_float(max, &max_d) &&
(val1->v.d > max_d))
val1->v.d = max_d;
}
break;
default:
break;
}
}
static void check_min(gavl_value_t * val1, const gavl_value_t * min)
{
switch(val1->type)
{
case GAVL_TYPE_INT:
{
int min_i;
if(gavl_value_get_int(min, &min_i) &&
(val1->v.i < min_i))
val1->v.i = min_i;
}
break;
case GAVL_TYPE_LONG:
{
int64_t min_l;
if(gavl_value_get_long(min, &min_l) &&
(val1->v.l < min_l))
val1->v.l = min_l;
}
break;
case GAVL_TYPE_FLOAT:
{
double min_d;
if(gavl_value_get_float(min, &min_d) &&
(val1->v.d < min_d))
val1->v.d = min_d;
}
break;
default:
break;
}
}
int bg_state_clamp_value(gavl_dictionary_t * state,
const char * ctx, const char * var,
gavl_value_t * val)
{
int min_i, max_i, val_i;
gavl_value_t val_min;
gavl_value_t val_max;
gavl_value_init(&val_min);
gavl_value_init(&val_max);
if(!bg_state_get_range(state, ctx, var,
&val_min,
&val_max))
return 1;
/* Toggle */
if(gavl_value_get_int(val, &val_i) &&
gavl_value_get_int(&val_min, &min_i) &&
gavl_value_get_int(&val_max, &max_i) &&
(min_i == 0) && (max_i == 1) && (val_i == 2))
{
gavl_value_set_int(val, 0);
return 1;
}
/* min must have the same type and be smaller than max */
if(gavl_value_compare(&val_min, &val_max) >= 0)
return 0;
check_max(val, &val_max);
check_min(val, &val_min);
return 1;
}
int bg_state_toggle_value(gavl_dictionary_t * state,
const char * ctx, const char * var,
gavl_value_t * ret)
{
const gavl_value_t * val;
if(!(val = bg_state_get(state, ctx, var)))
return 0;
gavl_value_copy(ret, val);
switch(val->type)
{
case GAVL_TYPE_INT:
{
if(ret->v.i)
ret->v.i = 0;
else
ret->v.i = 1;
}
break;
case GAVL_TYPE_LONG:
{
if(ret->v.l)
ret->v.l = 0;
else
ret->v.l = 1;
}
break;
default:
return 0;
}
return 1;
}
int bg_state_add_value(gavl_dictionary_t * state,
const char * ctx, const char * var,
const gavl_value_t * add, gavl_value_t * ret)
{
const gavl_value_t * val;
if(!(val = bg_state_get(state, ctx, var)))
return 0;
gavl_value_copy(ret, val);
switch(val->type)
{
case GAVL_TYPE_INT:
{
int add_i;
if(gavl_value_get_int(add, &add_i))
ret->v.i += add_i;
else
return 0;
}
break;
case GAVL_TYPE_LONG:
{
int64_t add_l;
if(gavl_value_get_long(add, &add_l))
ret->v.l += add_l;
else
return 0;
}
break;
case GAVL_TYPE_FLOAT:
{
double add_d;
if(gavl_value_get_float(add, &add_d))
ret->v.d += add_d;
else
return 0;
}
break;
default:
return 0;
}
return bg_state_clamp_value(state, ctx, var, ret);
}
int bg_state_handle_set_rel(gavl_dictionary_t * state, gavl_msg_t * msg)
{
gavl_value_t val;
gavl_value_t add;
const char * ctx;
const char * var;
int last = 0;
gavl_msg_t new_msg;
if((msg->NS != BG_MSG_NS_STATE) ||
(msg->ID != BG_CMD_SET_STATE_REL))
return 0;
gavl_value_init(&val);
gavl_value_init(&add);
gavl_msg_get_state(msg, &last, &ctx, &var, &add, NULL);
/* Add (and clamp) value */
bg_state_add_value(state, ctx, var, &add, &val);
gavl_msg_init(&new_msg);
gavl_msg_set_state(&new_msg, BG_CMD_SET_STATE, last, ctx, var, &val);
gavl_msg_free(msg);
gavl_msg_copy(msg, &new_msg);
gavl_value_free(&val);
gavl_value_free(&add);
return 1;
}
/* Merge */
static void merge_func_child(void * priv, const char * name, const gavl_value_t * val)
{
gavl_dictionary_set(priv, name, val);
}
static void merge_func(void * priv, const char * name, const gavl_value_t * val)
{
gavl_dictionary_t * dst;
if(val->type != GAVL_TYPE_DICTIONARY)
return;
dst = gavl_dictionary_get_dictionary_create(priv, name);
gavl_dictionary_foreach(val->v.dictionary, merge_func_child, dst);
}
void bg_state_merge(gavl_dictionary_t * dst, const gavl_dictionary_t * src)
{
gavl_dictionary_foreach(src, merge_func, dst);
}
|