obs-properties.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "util/bmem.h"
  15. #include "util/darray.h"
  16. #include "obs-internal.h"
  17. #include "obs-properties.h"
  18. static inline void *get_property_data(struct obs_property *prop);
  19. /* ------------------------------------------------------------------------- */
  20. struct float_data {
  21. double min, max, step;
  22. enum obs_number_type type;
  23. char *suffix;
  24. };
  25. struct int_data {
  26. int min, max, step;
  27. enum obs_number_type type;
  28. char *suffix;
  29. };
  30. struct list_item {
  31. char *name;
  32. bool disabled;
  33. union {
  34. char *str;
  35. long long ll;
  36. double d;
  37. };
  38. };
  39. struct path_data {
  40. char *filter;
  41. char *default_path;
  42. enum obs_path_type type;
  43. };
  44. struct text_data {
  45. enum obs_text_type type;
  46. bool monospace;
  47. };
  48. struct list_data {
  49. DARRAY(struct list_item) items;
  50. enum obs_combo_type type;
  51. enum obs_combo_format format;
  52. };
  53. struct editable_list_data {
  54. enum obs_editable_list_type type;
  55. char *filter;
  56. char *default_path;
  57. };
  58. struct button_data {
  59. obs_property_clicked_t callback;
  60. };
  61. struct frame_rate_option {
  62. char *name;
  63. char *description;
  64. };
  65. struct frame_rate_range {
  66. struct media_frames_per_second min_time;
  67. struct media_frames_per_second max_time;
  68. };
  69. struct frame_rate_data {
  70. DARRAY(struct frame_rate_option) extra_options;
  71. DARRAY(struct frame_rate_range) ranges;
  72. };
  73. struct group_data {
  74. enum obs_group_type type;
  75. obs_properties_t *content;
  76. };
  77. static inline void path_data_free(struct path_data *data)
  78. {
  79. bfree(data->default_path);
  80. if (data->type == OBS_PATH_FILE)
  81. bfree(data->filter);
  82. }
  83. static inline void editable_list_data_free(struct editable_list_data *data)
  84. {
  85. bfree(data->default_path);
  86. bfree(data->filter);
  87. }
  88. static inline void list_item_free(struct list_data *data,
  89. struct list_item *item)
  90. {
  91. bfree(item->name);
  92. if (data->format == OBS_COMBO_FORMAT_STRING)
  93. bfree(item->str);
  94. }
  95. static inline void list_data_free(struct list_data *data)
  96. {
  97. for (size_t i = 0; i < data->items.num; i++)
  98. list_item_free(data, data->items.array + i);
  99. da_free(data->items);
  100. }
  101. static inline void frame_rate_data_options_free(struct frame_rate_data *data)
  102. {
  103. for (size_t i = 0; i < data->extra_options.num; i++) {
  104. struct frame_rate_option *opt = &data->extra_options.array[i];
  105. bfree(opt->name);
  106. bfree(opt->description);
  107. }
  108. da_resize(data->extra_options, 0);
  109. }
  110. static inline void frame_rate_data_ranges_free(struct frame_rate_data *data)
  111. {
  112. da_resize(data->ranges, 0);
  113. }
  114. static inline void frame_rate_data_free(struct frame_rate_data *data)
  115. {
  116. frame_rate_data_options_free(data);
  117. frame_rate_data_ranges_free(data);
  118. da_free(data->extra_options);
  119. da_free(data->ranges);
  120. }
  121. static inline void group_data_free(struct group_data *data)
  122. {
  123. obs_properties_destroy(data->content);
  124. }
  125. static inline void int_data_free(struct int_data *data)
  126. {
  127. if (data->suffix)
  128. bfree(data->suffix);
  129. }
  130. static inline void float_data_free(struct float_data *data)
  131. {
  132. if (data->suffix)
  133. bfree(data->suffix);
  134. }
  135. struct obs_properties;
  136. struct obs_property {
  137. char *name;
  138. char *desc;
  139. char *long_desc;
  140. void *priv;
  141. enum obs_property_type type;
  142. bool visible;
  143. bool enabled;
  144. struct obs_properties *parent;
  145. obs_property_modified_t modified;
  146. obs_property_modified2_t modified2;
  147. struct obs_property *next;
  148. };
  149. struct obs_properties {
  150. void *param;
  151. void (*destroy)(void *param);
  152. uint32_t flags;
  153. struct obs_property *first_property;
  154. struct obs_property **last;
  155. struct obs_property *parent;
  156. };
  157. obs_properties_t *obs_properties_create(void)
  158. {
  159. struct obs_properties *props;
  160. props = bzalloc(sizeof(struct obs_properties));
  161. props->last = &props->first_property;
  162. return props;
  163. }
  164. void obs_properties_set_param(obs_properties_t *props, void *param,
  165. void (*destroy)(void *param))
  166. {
  167. if (!props)
  168. return;
  169. if (props->param && props->destroy)
  170. props->destroy(props->param);
  171. props->param = param;
  172. props->destroy = destroy;
  173. }
  174. void obs_properties_set_flags(obs_properties_t *props, uint32_t flags)
  175. {
  176. if (props)
  177. props->flags = flags;
  178. }
  179. uint32_t obs_properties_get_flags(obs_properties_t *props)
  180. {
  181. return props ? props->flags : 0;
  182. }
  183. void *obs_properties_get_param(obs_properties_t *props)
  184. {
  185. return props ? props->param : NULL;
  186. }
  187. obs_properties_t *obs_properties_create_param(void *param,
  188. void (*destroy)(void *param))
  189. {
  190. struct obs_properties *props = obs_properties_create();
  191. obs_properties_set_param(props, param, destroy);
  192. return props;
  193. }
  194. static void obs_property_destroy(struct obs_property *property)
  195. {
  196. if (property->type == OBS_PROPERTY_LIST)
  197. list_data_free(get_property_data(property));
  198. else if (property->type == OBS_PROPERTY_PATH)
  199. path_data_free(get_property_data(property));
  200. else if (property->type == OBS_PROPERTY_EDITABLE_LIST)
  201. editable_list_data_free(get_property_data(property));
  202. else if (property->type == OBS_PROPERTY_FRAME_RATE)
  203. frame_rate_data_free(get_property_data(property));
  204. else if (property->type == OBS_PROPERTY_GROUP)
  205. group_data_free(get_property_data(property));
  206. else if (property->type == OBS_PROPERTY_INT)
  207. int_data_free(get_property_data(property));
  208. else if (property->type == OBS_PROPERTY_FLOAT)
  209. float_data_free(get_property_data(property));
  210. bfree(property->name);
  211. bfree(property->desc);
  212. bfree(property->long_desc);
  213. bfree(property);
  214. }
  215. void obs_properties_destroy(obs_properties_t *props)
  216. {
  217. if (props) {
  218. struct obs_property *p = props->first_property;
  219. if (props->destroy && props->param)
  220. props->destroy(props->param);
  221. while (p) {
  222. struct obs_property *next = p->next;
  223. obs_property_destroy(p);
  224. p = next;
  225. }
  226. bfree(props);
  227. }
  228. }
  229. obs_property_t *obs_properties_first(obs_properties_t *props)
  230. {
  231. return (props != NULL) ? props->first_property : NULL;
  232. }
  233. obs_property_t *obs_properties_get(obs_properties_t *props, const char *name)
  234. {
  235. struct obs_property *property;
  236. if (!props)
  237. return NULL;
  238. property = props->first_property;
  239. while (property) {
  240. if (strcmp(property->name, name) == 0)
  241. return property;
  242. if (property->type == OBS_PROPERTY_GROUP) {
  243. obs_properties_t *group =
  244. obs_property_group_content(property);
  245. obs_property_t *found = obs_properties_get(group, name);
  246. if (found != NULL) {
  247. return found;
  248. }
  249. }
  250. property = property->next;
  251. }
  252. return NULL;
  253. }
  254. obs_properties_t *obs_properties_get_parent(obs_properties_t *props)
  255. {
  256. return props->parent ? props->parent->parent : NULL;
  257. }
  258. void obs_properties_remove_by_name(obs_properties_t *props, const char *name)
  259. {
  260. if (!props)
  261. return;
  262. /* obs_properties_t is a forward-linked-list, so we need to keep both
  263. * previous and current pointers around. That way we can fix up the
  264. * pointers for the previous element if we find a match.
  265. */
  266. struct obs_property *cur = props->first_property;
  267. struct obs_property *prev = props->first_property;
  268. while (cur) {
  269. if (strcmp(cur->name, name) == 0) {
  270. // Fix props->last pointer.
  271. if (props->last == &cur->next) {
  272. if (cur == prev) {
  273. // If we are the last entry and there
  274. // is no previous entry, reset.
  275. props->last = &props->first_property;
  276. } else {
  277. // If we are the last entry and there
  278. // is a previous entry, update.
  279. props->last = &prev->next;
  280. }
  281. }
  282. // Fix props->first_property.
  283. if (props->first_property == cur)
  284. props->first_property = cur->next;
  285. // Update the previous element next pointer with our
  286. // next pointer. This is an automatic no-op if both
  287. // elements alias the same memory.
  288. prev->next = cur->next;
  289. // Finally clear our own next pointer and destroy.
  290. cur->next = NULL;
  291. obs_property_destroy(cur);
  292. break;
  293. }
  294. if (cur->type == OBS_PROPERTY_GROUP) {
  295. obs_properties_remove_by_name(
  296. obs_property_group_content(cur), name);
  297. }
  298. prev = cur;
  299. cur = cur->next;
  300. }
  301. }
  302. void obs_properties_apply_settings_internal(obs_properties_t *props,
  303. obs_data_t *settings,
  304. obs_properties_t *realprops)
  305. {
  306. struct obs_property *p;
  307. p = props->first_property;
  308. while (p) {
  309. if (p->type == OBS_PROPERTY_GROUP) {
  310. obs_properties_apply_settings_internal(
  311. obs_property_group_content(p), settings,
  312. realprops);
  313. }
  314. if (p->modified)
  315. p->modified(realprops, p, settings);
  316. else if (p->modified2)
  317. p->modified2(p->priv, realprops, p, settings);
  318. p = p->next;
  319. }
  320. }
  321. void obs_properties_apply_settings(obs_properties_t *props,
  322. obs_data_t *settings)
  323. {
  324. if (!props)
  325. return;
  326. obs_properties_apply_settings_internal(props, settings, props);
  327. }
  328. /* ------------------------------------------------------------------------- */
  329. static inline void propertes_add(struct obs_properties *props,
  330. struct obs_property *p)
  331. {
  332. *props->last = p;
  333. props->last = &p->next;
  334. }
  335. static inline size_t get_property_size(enum obs_property_type type)
  336. {
  337. switch (type) {
  338. case OBS_PROPERTY_INVALID:
  339. return 0;
  340. case OBS_PROPERTY_BOOL:
  341. return 0;
  342. case OBS_PROPERTY_INT:
  343. return sizeof(struct int_data);
  344. case OBS_PROPERTY_FLOAT:
  345. return sizeof(struct float_data);
  346. case OBS_PROPERTY_TEXT:
  347. return sizeof(struct text_data);
  348. case OBS_PROPERTY_PATH:
  349. return sizeof(struct path_data);
  350. case OBS_PROPERTY_LIST:
  351. return sizeof(struct list_data);
  352. case OBS_PROPERTY_COLOR:
  353. return 0;
  354. case OBS_PROPERTY_BUTTON:
  355. return sizeof(struct button_data);
  356. case OBS_PROPERTY_FONT:
  357. return 0;
  358. case OBS_PROPERTY_EDITABLE_LIST:
  359. return sizeof(struct editable_list_data);
  360. case OBS_PROPERTY_FRAME_RATE:
  361. return sizeof(struct frame_rate_data);
  362. case OBS_PROPERTY_GROUP:
  363. return sizeof(struct group_data);
  364. }
  365. return 0;
  366. }
  367. static inline struct obs_property *new_prop(struct obs_properties *props,
  368. const char *name, const char *desc,
  369. enum obs_property_type type)
  370. {
  371. size_t data_size = get_property_size(type);
  372. struct obs_property *p;
  373. p = bzalloc(sizeof(struct obs_property) + data_size);
  374. p->parent = props;
  375. p->enabled = true;
  376. p->visible = true;
  377. p->type = type;
  378. p->name = bstrdup(name);
  379. p->desc = bstrdup(desc);
  380. propertes_add(props, p);
  381. return p;
  382. }
  383. static inline obs_properties_t *get_topmost_parent(obs_properties_t *props)
  384. {
  385. obs_properties_t *parent = props;
  386. obs_properties_t *last_parent = parent;
  387. while (parent) {
  388. last_parent = parent;
  389. parent = obs_properties_get_parent(parent);
  390. }
  391. return last_parent;
  392. }
  393. static inline bool contains_prop(struct obs_properties *props, const char *name)
  394. {
  395. struct obs_property *p = props->first_property;
  396. while (p) {
  397. if (strcmp(p->name, name) == 0) {
  398. blog(LOG_WARNING, "Property '%s' exists", name);
  399. return true;
  400. }
  401. if (p->type == OBS_PROPERTY_GROUP) {
  402. if (contains_prop(obs_property_group_content(p),
  403. name)) {
  404. return true;
  405. }
  406. }
  407. p = p->next;
  408. }
  409. return false;
  410. }
  411. static inline bool has_prop(struct obs_properties *props, const char *name)
  412. {
  413. return contains_prop(get_topmost_parent(props), name);
  414. }
  415. static inline void *get_property_data(struct obs_property *prop)
  416. {
  417. return (uint8_t *)prop + sizeof(struct obs_property);
  418. }
  419. static inline void *get_type_data(struct obs_property *prop,
  420. enum obs_property_type type)
  421. {
  422. if (!prop || prop->type != type)
  423. return NULL;
  424. return get_property_data(prop);
  425. }
  426. obs_property_t *obs_properties_add_bool(obs_properties_t *props,
  427. const char *name, const char *desc)
  428. {
  429. if (!props || has_prop(props, name))
  430. return NULL;
  431. return new_prop(props, name, desc, OBS_PROPERTY_BOOL);
  432. }
  433. static obs_property_t *add_int(obs_properties_t *props, const char *name,
  434. const char *desc, int min, int max, int step,
  435. enum obs_number_type type)
  436. {
  437. if (!props || has_prop(props, name))
  438. return NULL;
  439. struct obs_property *p = new_prop(props, name, desc, OBS_PROPERTY_INT);
  440. struct int_data *data = get_property_data(p);
  441. data->min = min;
  442. data->max = max;
  443. data->step = step;
  444. data->type = type;
  445. return p;
  446. }
  447. static obs_property_t *add_flt(obs_properties_t *props, const char *name,
  448. const char *desc, double min, double max,
  449. double step, enum obs_number_type type)
  450. {
  451. if (!props || has_prop(props, name))
  452. return NULL;
  453. struct obs_property *p =
  454. new_prop(props, name, desc, OBS_PROPERTY_FLOAT);
  455. struct float_data *data = get_property_data(p);
  456. data->min = min;
  457. data->max = max;
  458. data->step = step;
  459. data->type = type;
  460. return p;
  461. }
  462. obs_property_t *obs_properties_add_int(obs_properties_t *props,
  463. const char *name, const char *desc,
  464. int min, int max, int step)
  465. {
  466. return add_int(props, name, desc, min, max, step, OBS_NUMBER_SCROLLER);
  467. }
  468. obs_property_t *obs_properties_add_float(obs_properties_t *props,
  469. const char *name, const char *desc,
  470. double min, double max, double step)
  471. {
  472. return add_flt(props, name, desc, min, max, step, OBS_NUMBER_SCROLLER);
  473. }
  474. obs_property_t *obs_properties_add_int_slider(obs_properties_t *props,
  475. const char *name,
  476. const char *desc, int min,
  477. int max, int step)
  478. {
  479. return add_int(props, name, desc, min, max, step, OBS_NUMBER_SLIDER);
  480. }
  481. obs_property_t *obs_properties_add_float_slider(obs_properties_t *props,
  482. const char *name,
  483. const char *desc, double min,
  484. double max, double step)
  485. {
  486. return add_flt(props, name, desc, min, max, step, OBS_NUMBER_SLIDER);
  487. }
  488. obs_property_t *obs_properties_add_text(obs_properties_t *props,
  489. const char *name, const char *desc,
  490. enum obs_text_type type)
  491. {
  492. if (!props || has_prop(props, name))
  493. return NULL;
  494. struct obs_property *p = new_prop(props, name, desc, OBS_PROPERTY_TEXT);
  495. struct text_data *data = get_property_data(p);
  496. data->type = type;
  497. return p;
  498. }
  499. obs_property_t *obs_properties_add_path(obs_properties_t *props,
  500. const char *name, const char *desc,
  501. enum obs_path_type type,
  502. const char *filter,
  503. const char *default_path)
  504. {
  505. if (!props || has_prop(props, name))
  506. return NULL;
  507. struct obs_property *p = new_prop(props, name, desc, OBS_PROPERTY_PATH);
  508. struct path_data *data = get_property_data(p);
  509. data->type = type;
  510. data->default_path = bstrdup(default_path);
  511. if (data->type == OBS_PATH_FILE)
  512. data->filter = bstrdup(filter);
  513. return p;
  514. }
  515. obs_property_t *obs_properties_add_list(obs_properties_t *props,
  516. const char *name, const char *desc,
  517. enum obs_combo_type type,
  518. enum obs_combo_format format)
  519. {
  520. if (!props || has_prop(props, name))
  521. return NULL;
  522. if (type == OBS_COMBO_TYPE_EDITABLE &&
  523. format != OBS_COMBO_FORMAT_STRING) {
  524. blog(LOG_WARNING,
  525. "List '%s', error: Editable combo boxes "
  526. "must be of the 'string' type",
  527. name);
  528. return NULL;
  529. }
  530. struct obs_property *p = new_prop(props, name, desc, OBS_PROPERTY_LIST);
  531. struct list_data *data = get_property_data(p);
  532. data->format = format;
  533. data->type = type;
  534. return p;
  535. }
  536. obs_property_t *obs_properties_add_color(obs_properties_t *props,
  537. const char *name, const char *desc)
  538. {
  539. if (!props || has_prop(props, name))
  540. return NULL;
  541. return new_prop(props, name, desc, OBS_PROPERTY_COLOR);
  542. }
  543. obs_property_t *obs_properties_add_button(obs_properties_t *props,
  544. const char *name, const char *text,
  545. obs_property_clicked_t callback)
  546. {
  547. if (!props || has_prop(props, name))
  548. return NULL;
  549. struct obs_property *p =
  550. new_prop(props, name, text, OBS_PROPERTY_BUTTON);
  551. struct button_data *data = get_property_data(p);
  552. data->callback = callback;
  553. return p;
  554. }
  555. obs_property_t *obs_properties_add_button2(obs_properties_t *props,
  556. const char *name, const char *text,
  557. obs_property_clicked_t callback,
  558. void *priv)
  559. {
  560. if (!props || has_prop(props, name))
  561. return NULL;
  562. struct obs_property *p =
  563. new_prop(props, name, text, OBS_PROPERTY_BUTTON);
  564. struct button_data *data = get_property_data(p);
  565. data->callback = callback;
  566. p->priv = priv;
  567. return p;
  568. }
  569. obs_property_t *obs_properties_add_font(obs_properties_t *props,
  570. const char *name, const char *desc)
  571. {
  572. if (!props || has_prop(props, name))
  573. return NULL;
  574. return new_prop(props, name, desc, OBS_PROPERTY_FONT);
  575. }
  576. obs_property_t *
  577. obs_properties_add_editable_list(obs_properties_t *props, const char *name,
  578. const char *desc,
  579. enum obs_editable_list_type type,
  580. const char *filter, const char *default_path)
  581. {
  582. if (!props || has_prop(props, name))
  583. return NULL;
  584. struct obs_property *p =
  585. new_prop(props, name, desc, OBS_PROPERTY_EDITABLE_LIST);
  586. struct editable_list_data *data = get_property_data(p);
  587. data->type = type;
  588. data->filter = bstrdup(filter);
  589. data->default_path = bstrdup(default_path);
  590. return p;
  591. }
  592. obs_property_t *obs_properties_add_frame_rate(obs_properties_t *props,
  593. const char *name,
  594. const char *desc)
  595. {
  596. if (!props || has_prop(props, name))
  597. return NULL;
  598. struct obs_property *p =
  599. new_prop(props, name, desc, OBS_PROPERTY_FRAME_RATE);
  600. struct frame_rate_data *data = get_property_data(p);
  601. da_init(data->extra_options);
  602. da_init(data->ranges);
  603. return p;
  604. }
  605. static bool check_property_group_recursion(obs_properties_t *parent,
  606. obs_properties_t *group)
  607. {
  608. /* Scan the group for the parent. */
  609. obs_property_t *current_property = group->first_property;
  610. while (current_property) {
  611. if (current_property->type == OBS_PROPERTY_GROUP) {
  612. obs_properties_t *cprops =
  613. obs_property_group_content(current_property);
  614. if (cprops == parent) {
  615. /* Contains find_props */
  616. return true;
  617. } else if (cprops == group) {
  618. /* Contains self, shouldn't be possible but
  619. * lets verify anyway. */
  620. return true;
  621. }
  622. check_property_group_recursion(cprops, group);
  623. }
  624. current_property = current_property->next;
  625. }
  626. return false;
  627. }
  628. static bool check_property_group_duplicates(obs_properties_t *parent,
  629. obs_properties_t *group)
  630. {
  631. obs_property_t *current_property = group->first_property;
  632. while (current_property) {
  633. if (has_prop(parent, current_property->name)) {
  634. return true;
  635. }
  636. current_property = current_property->next;
  637. }
  638. return false;
  639. }
  640. obs_property_t *obs_properties_add_group(obs_properties_t *props,
  641. const char *name, const char *desc,
  642. enum obs_group_type type,
  643. obs_properties_t *group)
  644. {
  645. if (!props || has_prop(props, name))
  646. return NULL;
  647. if (!group)
  648. return NULL;
  649. /* Prevent recursion. */
  650. if (props == group)
  651. return NULL;
  652. if (check_property_group_recursion(props, group))
  653. return NULL;
  654. /* Prevent duplicate properties */
  655. if (check_property_group_duplicates(props, group))
  656. return NULL;
  657. obs_property_t *p = new_prop(props, name, desc, OBS_PROPERTY_GROUP);
  658. group->parent = p;
  659. struct group_data *data = get_property_data(p);
  660. data->type = type;
  661. data->content = group;
  662. return p;
  663. }
  664. /* ------------------------------------------------------------------------- */
  665. static inline bool is_combo(struct obs_property *p)
  666. {
  667. return p->type == OBS_PROPERTY_LIST;
  668. }
  669. static inline struct list_data *get_list_data(struct obs_property *p)
  670. {
  671. if (!p || !is_combo(p))
  672. return NULL;
  673. return get_property_data(p);
  674. }
  675. static inline struct list_data *get_list_fmt_data(struct obs_property *p,
  676. enum obs_combo_format format)
  677. {
  678. struct list_data *data = get_list_data(p);
  679. return (data && data->format == format) ? data : NULL;
  680. }
  681. /* ------------------------------------------------------------------------- */
  682. bool obs_property_next(obs_property_t **p)
  683. {
  684. if (!p || !*p)
  685. return false;
  686. *p = (*p)->next;
  687. return *p != NULL;
  688. }
  689. void obs_property_set_modified_callback(obs_property_t *p,
  690. obs_property_modified_t modified)
  691. {
  692. if (p)
  693. p->modified = modified;
  694. }
  695. void obs_property_set_modified_callback2(obs_property_t *p,
  696. obs_property_modified2_t modified2,
  697. void *priv)
  698. {
  699. if (p) {
  700. p->modified2 = modified2;
  701. p->priv = priv;
  702. }
  703. }
  704. bool obs_property_modified(obs_property_t *p, obs_data_t *settings)
  705. {
  706. if (p) {
  707. if (p->modified) {
  708. obs_properties_t *top = get_topmost_parent(p->parent);
  709. return p->modified(top, p, settings);
  710. } else if (p->modified2) {
  711. obs_properties_t *top = get_topmost_parent(p->parent);
  712. return p->modified2(p->priv, top, p, settings);
  713. }
  714. }
  715. return false;
  716. }
  717. bool obs_property_button_clicked(obs_property_t *p, void *obj)
  718. {
  719. struct obs_context_data *context = obj;
  720. if (p) {
  721. struct button_data *data =
  722. get_type_data(p, OBS_PROPERTY_BUTTON);
  723. if (data && data->callback) {
  724. obs_properties_t *top = get_topmost_parent(p->parent);
  725. if (p->priv)
  726. return data->callback(top, p, p->priv);
  727. return data->callback(top, p,
  728. (context ? context->data : NULL));
  729. }
  730. }
  731. return false;
  732. }
  733. void obs_property_set_visible(obs_property_t *p, bool visible)
  734. {
  735. if (p)
  736. p->visible = visible;
  737. }
  738. void obs_property_set_enabled(obs_property_t *p, bool enabled)
  739. {
  740. if (p)
  741. p->enabled = enabled;
  742. }
  743. void obs_property_set_description(obs_property_t *p, const char *description)
  744. {
  745. if (p) {
  746. bfree(p->desc);
  747. p->desc = description && *description ? bstrdup(description)
  748. : NULL;
  749. }
  750. }
  751. void obs_property_set_long_description(obs_property_t *p, const char *long_desc)
  752. {
  753. if (p) {
  754. bfree(p->long_desc);
  755. p->long_desc = long_desc && *long_desc ? bstrdup(long_desc)
  756. : NULL;
  757. }
  758. }
  759. const char *obs_property_name(obs_property_t *p)
  760. {
  761. return p ? p->name : NULL;
  762. }
  763. const char *obs_property_description(obs_property_t *p)
  764. {
  765. return p ? p->desc : NULL;
  766. }
  767. const char *obs_property_long_description(obs_property_t *p)
  768. {
  769. return p ? p->long_desc : NULL;
  770. }
  771. enum obs_property_type obs_property_get_type(obs_property_t *p)
  772. {
  773. return p ? p->type : OBS_PROPERTY_INVALID;
  774. }
  775. bool obs_property_enabled(obs_property_t *p)
  776. {
  777. return p ? p->enabled : false;
  778. }
  779. bool obs_property_visible(obs_property_t *p)
  780. {
  781. return p ? p->visible : false;
  782. }
  783. int obs_property_int_min(obs_property_t *p)
  784. {
  785. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  786. return data ? data->min : 0;
  787. }
  788. int obs_property_int_max(obs_property_t *p)
  789. {
  790. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  791. return data ? data->max : 0;
  792. }
  793. int obs_property_int_step(obs_property_t *p)
  794. {
  795. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  796. return data ? data->step : 0;
  797. }
  798. enum obs_number_type obs_property_int_type(obs_property_t *p)
  799. {
  800. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  801. return data ? data->type : OBS_NUMBER_SCROLLER;
  802. }
  803. const char *obs_property_int_suffix(obs_property_t *p)
  804. {
  805. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  806. return data ? data->suffix : NULL;
  807. }
  808. double obs_property_float_min(obs_property_t *p)
  809. {
  810. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  811. return data ? data->min : 0;
  812. }
  813. double obs_property_float_max(obs_property_t *p)
  814. {
  815. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  816. return data ? data->max : 0;
  817. }
  818. double obs_property_float_step(obs_property_t *p)
  819. {
  820. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  821. return data ? data->step : 0;
  822. }
  823. const char *obs_property_float_suffix(obs_property_t *p)
  824. {
  825. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  826. return data ? data->suffix : NULL;
  827. }
  828. enum obs_number_type obs_property_float_type(obs_property_t *p)
  829. {
  830. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  831. return data ? data->type : OBS_NUMBER_SCROLLER;
  832. }
  833. enum obs_text_type obs_property_text_type(obs_property_t *p)
  834. {
  835. struct text_data *data = get_type_data(p, OBS_PROPERTY_TEXT);
  836. return data ? data->type : OBS_TEXT_DEFAULT;
  837. }
  838. enum obs_text_type obs_property_text_monospace(obs_property_t *p)
  839. {
  840. struct text_data *data = get_type_data(p, OBS_PROPERTY_TEXT);
  841. return data ? data->monospace : false;
  842. }
  843. enum obs_path_type obs_property_path_type(obs_property_t *p)
  844. {
  845. struct path_data *data = get_type_data(p, OBS_PROPERTY_PATH);
  846. return data ? data->type : OBS_PATH_DIRECTORY;
  847. }
  848. const char *obs_property_path_filter(obs_property_t *p)
  849. {
  850. struct path_data *data = get_type_data(p, OBS_PROPERTY_PATH);
  851. return data ? data->filter : NULL;
  852. }
  853. const char *obs_property_path_default_path(obs_property_t *p)
  854. {
  855. struct path_data *data = get_type_data(p, OBS_PROPERTY_PATH);
  856. return data ? data->default_path : NULL;
  857. }
  858. enum obs_combo_type obs_property_list_type(obs_property_t *p)
  859. {
  860. struct list_data *data = get_list_data(p);
  861. return data ? data->type : OBS_COMBO_TYPE_INVALID;
  862. }
  863. enum obs_combo_format obs_property_list_format(obs_property_t *p)
  864. {
  865. struct list_data *data = get_list_data(p);
  866. return data ? data->format : OBS_COMBO_FORMAT_INVALID;
  867. }
  868. void obs_property_int_set_limits(obs_property_t *p, int min, int max, int step)
  869. {
  870. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  871. if (!data)
  872. return;
  873. data->min = min;
  874. data->max = max;
  875. data->step = step;
  876. }
  877. void obs_property_float_set_limits(obs_property_t *p, double min, double max,
  878. double step)
  879. {
  880. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  881. if (!data)
  882. return;
  883. data->min = min;
  884. data->max = max;
  885. data->step = step;
  886. }
  887. void obs_property_int_set_suffix(obs_property_t *p, const char *suffix)
  888. {
  889. struct int_data *data = get_type_data(p, OBS_PROPERTY_INT);
  890. if (!data)
  891. return;
  892. bfree(data->suffix);
  893. data->suffix = bstrdup(suffix);
  894. }
  895. void obs_property_float_set_suffix(obs_property_t *p, const char *suffix)
  896. {
  897. struct float_data *data = get_type_data(p, OBS_PROPERTY_FLOAT);
  898. if (!data)
  899. return;
  900. bfree(data->suffix);
  901. data->suffix = bstrdup(suffix);
  902. }
  903. void obs_property_text_set_monospace(obs_property_t *p, bool monospace)
  904. {
  905. struct text_data *data = get_type_data(p, OBS_PROPERTY_TEXT);
  906. if (!data)
  907. return;
  908. data->monospace = monospace;
  909. }
  910. void obs_property_list_clear(obs_property_t *p)
  911. {
  912. struct list_data *data = get_list_data(p);
  913. if (data)
  914. list_data_free(data);
  915. }
  916. static size_t add_item(struct list_data *data, const char *name,
  917. const void *val)
  918. {
  919. struct list_item item = {NULL};
  920. item.name = bstrdup(name);
  921. if (data->format == OBS_COMBO_FORMAT_INT)
  922. item.ll = *(const long long *)val;
  923. else if (data->format == OBS_COMBO_FORMAT_FLOAT)
  924. item.d = *(const double *)val;
  925. else
  926. item.str = bstrdup(val);
  927. return da_push_back(data->items, &item);
  928. }
  929. static void insert_item(struct list_data *data, size_t idx, const char *name,
  930. const void *val)
  931. {
  932. struct list_item item = {NULL};
  933. item.name = bstrdup(name);
  934. if (data->format == OBS_COMBO_FORMAT_INT)
  935. item.ll = *(const long long *)val;
  936. else if (data->format == OBS_COMBO_FORMAT_FLOAT)
  937. item.d = *(const double *)val;
  938. else
  939. item.str = bstrdup(val);
  940. da_insert(data->items, idx, &item);
  941. }
  942. size_t obs_property_list_add_string(obs_property_t *p, const char *name,
  943. const char *val)
  944. {
  945. struct list_data *data = get_list_data(p);
  946. if (data && data->format == OBS_COMBO_FORMAT_STRING)
  947. return add_item(data, name, val);
  948. return 0;
  949. }
  950. size_t obs_property_list_add_int(obs_property_t *p, const char *name,
  951. long long val)
  952. {
  953. struct list_data *data = get_list_data(p);
  954. if (data && data->format == OBS_COMBO_FORMAT_INT)
  955. return add_item(data, name, &val);
  956. return 0;
  957. }
  958. size_t obs_property_list_add_float(obs_property_t *p, const char *name,
  959. double val)
  960. {
  961. struct list_data *data = get_list_data(p);
  962. if (data && data->format == OBS_COMBO_FORMAT_FLOAT)
  963. return add_item(data, name, &val);
  964. return 0;
  965. }
  966. void obs_property_list_insert_string(obs_property_t *p, size_t idx,
  967. const char *name, const char *val)
  968. {
  969. struct list_data *data = get_list_data(p);
  970. if (data && data->format == OBS_COMBO_FORMAT_STRING)
  971. insert_item(data, idx, name, val);
  972. }
  973. void obs_property_list_insert_int(obs_property_t *p, size_t idx,
  974. const char *name, long long val)
  975. {
  976. struct list_data *data = get_list_data(p);
  977. if (data && data->format == OBS_COMBO_FORMAT_INT)
  978. insert_item(data, idx, name, &val);
  979. }
  980. void obs_property_list_insert_float(obs_property_t *p, size_t idx,
  981. const char *name, double val)
  982. {
  983. struct list_data *data = get_list_data(p);
  984. if (data && data->format == OBS_COMBO_FORMAT_FLOAT)
  985. insert_item(data, idx, name, &val);
  986. }
  987. void obs_property_list_item_remove(obs_property_t *p, size_t idx)
  988. {
  989. struct list_data *data = get_list_data(p);
  990. if (data && idx < data->items.num) {
  991. list_item_free(data, data->items.array + idx);
  992. da_erase(data->items, idx);
  993. }
  994. }
  995. size_t obs_property_list_item_count(obs_property_t *p)
  996. {
  997. struct list_data *data = get_list_data(p);
  998. return data ? data->items.num : 0;
  999. }
  1000. bool obs_property_list_item_disabled(obs_property_t *p, size_t idx)
  1001. {
  1002. struct list_data *data = get_list_data(p);
  1003. return (data && idx < data->items.num) ? data->items.array[idx].disabled
  1004. : false;
  1005. }
  1006. void obs_property_list_item_disable(obs_property_t *p, size_t idx,
  1007. bool disabled)
  1008. {
  1009. struct list_data *data = get_list_data(p);
  1010. if (!data || idx >= data->items.num)
  1011. return;
  1012. data->items.array[idx].disabled = disabled;
  1013. }
  1014. const char *obs_property_list_item_name(obs_property_t *p, size_t idx)
  1015. {
  1016. struct list_data *data = get_list_data(p);
  1017. return (data && idx < data->items.num) ? data->items.array[idx].name
  1018. : NULL;
  1019. }
  1020. const char *obs_property_list_item_string(obs_property_t *p, size_t idx)
  1021. {
  1022. struct list_data *data = get_list_fmt_data(p, OBS_COMBO_FORMAT_STRING);
  1023. return (data && idx < data->items.num) ? data->items.array[idx].str
  1024. : NULL;
  1025. }
  1026. long long obs_property_list_item_int(obs_property_t *p, size_t idx)
  1027. {
  1028. struct list_data *data = get_list_fmt_data(p, OBS_COMBO_FORMAT_INT);
  1029. return (data && idx < data->items.num) ? data->items.array[idx].ll : 0;
  1030. }
  1031. double obs_property_list_item_float(obs_property_t *p, size_t idx)
  1032. {
  1033. struct list_data *data = get_list_fmt_data(p, OBS_COMBO_FORMAT_FLOAT);
  1034. return (data && idx < data->items.num) ? data->items.array[idx].d : 0.0;
  1035. }
  1036. enum obs_editable_list_type obs_property_editable_list_type(obs_property_t *p)
  1037. {
  1038. struct editable_list_data *data =
  1039. get_type_data(p, OBS_PROPERTY_EDITABLE_LIST);
  1040. return data ? data->type : OBS_EDITABLE_LIST_TYPE_STRINGS;
  1041. }
  1042. const char *obs_property_editable_list_filter(obs_property_t *p)
  1043. {
  1044. struct editable_list_data *data =
  1045. get_type_data(p, OBS_PROPERTY_EDITABLE_LIST);
  1046. return data ? data->filter : NULL;
  1047. }
  1048. const char *obs_property_editable_list_default_path(obs_property_t *p)
  1049. {
  1050. struct editable_list_data *data =
  1051. get_type_data(p, OBS_PROPERTY_EDITABLE_LIST);
  1052. return data ? data->default_path : NULL;
  1053. }
  1054. /* ------------------------------------------------------------------------- */
  1055. /* OBS_PROPERTY_FRAME_RATE */
  1056. void obs_property_frame_rate_clear(obs_property_t *p)
  1057. {
  1058. struct frame_rate_data *data =
  1059. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1060. if (!data)
  1061. return;
  1062. frame_rate_data_options_free(data);
  1063. frame_rate_data_ranges_free(data);
  1064. }
  1065. void obs_property_frame_rate_options_clear(obs_property_t *p)
  1066. {
  1067. struct frame_rate_data *data =
  1068. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1069. if (!data)
  1070. return;
  1071. frame_rate_data_options_free(data);
  1072. }
  1073. void obs_property_frame_rate_fps_ranges_clear(obs_property_t *p)
  1074. {
  1075. struct frame_rate_data *data =
  1076. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1077. if (!data)
  1078. return;
  1079. frame_rate_data_ranges_free(data);
  1080. }
  1081. size_t obs_property_frame_rate_option_add(obs_property_t *p, const char *name,
  1082. const char *description)
  1083. {
  1084. struct frame_rate_data *data =
  1085. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1086. if (!data)
  1087. return DARRAY_INVALID;
  1088. struct frame_rate_option *opt = da_push_back_new(data->extra_options);
  1089. opt->name = bstrdup(name);
  1090. opt->description = bstrdup(description);
  1091. return data->extra_options.num - 1;
  1092. }
  1093. size_t obs_property_frame_rate_fps_range_add(obs_property_t *p,
  1094. struct media_frames_per_second min,
  1095. struct media_frames_per_second max)
  1096. {
  1097. struct frame_rate_data *data =
  1098. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1099. if (!data)
  1100. return DARRAY_INVALID;
  1101. struct frame_rate_range *rng = da_push_back_new(data->ranges);
  1102. rng->min_time = min;
  1103. rng->max_time = max;
  1104. return data->ranges.num - 1;
  1105. }
  1106. void obs_property_frame_rate_option_insert(obs_property_t *p, size_t idx,
  1107. const char *name,
  1108. const char *description)
  1109. {
  1110. struct frame_rate_data *data =
  1111. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1112. if (!data)
  1113. return;
  1114. struct frame_rate_option *opt = da_insert_new(data->extra_options, idx);
  1115. opt->name = bstrdup(name);
  1116. opt->description = bstrdup(description);
  1117. }
  1118. void obs_property_frame_rate_fps_range_insert(
  1119. obs_property_t *p, size_t idx, struct media_frames_per_second min,
  1120. struct media_frames_per_second max)
  1121. {
  1122. struct frame_rate_data *data =
  1123. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1124. if (!data)
  1125. return;
  1126. struct frame_rate_range *rng = da_insert_new(data->ranges, idx);
  1127. rng->min_time = min;
  1128. rng->max_time = max;
  1129. }
  1130. size_t obs_property_frame_rate_options_count(obs_property_t *p)
  1131. {
  1132. struct frame_rate_data *data =
  1133. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1134. return data ? data->extra_options.num : 0;
  1135. }
  1136. const char *obs_property_frame_rate_option_name(obs_property_t *p, size_t idx)
  1137. {
  1138. struct frame_rate_data *data =
  1139. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1140. return data && data->extra_options.num > idx
  1141. ? data->extra_options.array[idx].name
  1142. : NULL;
  1143. }
  1144. const char *obs_property_frame_rate_option_description(obs_property_t *p,
  1145. size_t idx)
  1146. {
  1147. struct frame_rate_data *data =
  1148. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1149. return data && data->extra_options.num > idx
  1150. ? data->extra_options.array[idx].description
  1151. : NULL;
  1152. }
  1153. size_t obs_property_frame_rate_fps_ranges_count(obs_property_t *p)
  1154. {
  1155. struct frame_rate_data *data =
  1156. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1157. return data ? data->ranges.num : 0;
  1158. }
  1159. struct media_frames_per_second
  1160. obs_property_frame_rate_fps_range_min(obs_property_t *p, size_t idx)
  1161. {
  1162. struct frame_rate_data *data =
  1163. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1164. return data && data->ranges.num > idx
  1165. ? data->ranges.array[idx].min_time
  1166. : (struct media_frames_per_second){0};
  1167. }
  1168. struct media_frames_per_second
  1169. obs_property_frame_rate_fps_range_max(obs_property_t *p, size_t idx)
  1170. {
  1171. struct frame_rate_data *data =
  1172. get_type_data(p, OBS_PROPERTY_FRAME_RATE);
  1173. return data && data->ranges.num > idx
  1174. ? data->ranges.array[idx].max_time
  1175. : (struct media_frames_per_second){0};
  1176. }
  1177. enum obs_text_type obs_proprety_text_type(obs_property_t *p)
  1178. {
  1179. return obs_property_text_type(p);
  1180. }
  1181. enum obs_group_type obs_property_group_type(obs_property_t *p)
  1182. {
  1183. struct group_data *data = get_type_data(p, OBS_PROPERTY_GROUP);
  1184. return data ? data->type : OBS_COMBO_INVALID;
  1185. }
  1186. obs_properties_t *obs_property_group_content(obs_property_t *p)
  1187. {
  1188. struct group_data *data = get_type_data(p, OBS_PROPERTY_GROUP);
  1189. return data ? data->content : NULL;
  1190. }