1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2004, 2005 Dennis Smit <ds@nerds-incorporated.org>
4  *
5  * Authors: Dennis Smit <ds@nerds-incorporated.org>
6  *
7  * $Id:
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 
29 #include "lv_log.h"
30 #include "lv_param.h"
31 
32 static int paramcontainer_dtor (VisObject *object);
33 static int paramentry_dtor (VisObject *object);
34 
35 static int get_next_pcall_id (VisList *callbacks);
36 
paramcontainer_dtor(VisObject * object)37 static int paramcontainer_dtor (VisObject *object)
38 {
39 	VisParamContainer *paramcontainer = VISUAL_PARAMCONTAINER (object);
40 
41 	visual_list_destroy_elements (&paramcontainer->entries);
42 
43 	return VISUAL_OK;
44 }
45 
paramentry_dtor(VisObject * object)46 static int paramentry_dtor (VisObject *object)
47 {
48 	VisParamEntry *param = VISUAL_PARAMENTRY (object);
49 
50 	if (param->string != NULL)
51 		visual_mem_free (param->string);
52 
53 	if (param->name != NULL)
54 		visual_mem_free (param->name);
55 
56 	if (param->objdata != NULL)
57 		visual_object_unref (param->objdata);
58 
59 	visual_palette_free_colors (&param->pal);
60 
61 	visual_list_destroy_elements (&param->callbacks);
62 
63 	param->string = NULL;
64 	param->name = NULL;
65 	param->objdata = NULL;
66 
67 	return VISUAL_OK;
68 }
69 
get_next_pcall_id(VisList * callbacks)70 static int get_next_pcall_id (VisList *callbacks)
71 {
72 	VisListEntry *le = NULL;
73 	VisParamEntryCallback *pcall;
74 	int found = FALSE;
75 	int i;
76 
77 	/* Walk through all possible ids */
78 	for (i = 0; i < VISUAL_PARAM_CALLBACK_ID_MAX; i++) {
79 
80 		found = FALSE;
81 		/* Check all the callbacks if the id is used */
82 		while ((pcall = visual_list_next (callbacks, &le)) != NULL) {
83 
84 			/* Found the ID, break and get ready for the next iterate */
85 			if (pcall->id == i) {
86 				found = TRUE;
87 
88 				break;
89 			}
90 		}
91 
92 		/* The id has NOT been found, thus is an original, and we return this as the next id */
93 		if (found == FALSE)
94 			return i;
95 	}
96 
97 	/* This is virtually impossible, or something very wrong is going ok, but no id seems to be left */
98 	return -1;
99 }
100 
101 
102 /**
103  * @defgroup VisParam VisParam
104  * @{
105  */
106 
107 /**
108  * Creates a new VisParamContainer structure.
109  *
110  * @return A newly allocated VisParamContainer structure.
111  */
visual_param_container_new()112 VisParamContainer *visual_param_container_new ()
113 {
114 	VisParamContainer *paramcontainer;
115 
116 	paramcontainer = visual_mem_new0 (VisParamContainer, 1);
117 
118 	/* Do the VisObject initialization */
119 	visual_object_initialize (VISUAL_OBJECT (paramcontainer), TRUE, paramcontainer_dtor);
120 
121 	visual_list_set_destroyer (&paramcontainer->entries, visual_object_list_destroyer);
122 
123 	return paramcontainer;
124 }
125 
126 /**
127  * Sets the eventqueue in the VisParamContainer, so events can be emitted on param changes.
128  *
129  * @param paramcontainer A pointer to the VisParamContainer to which the VisEventQueue needs to be set.
130  * @param eventqueue A Pointer to the VisEventQueue that is used for the events the VisParamContainer can emit.
131  *
132  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_CONTAINER_NULL on failure.
133  */
visual_param_container_set_eventqueue(VisParamContainer * paramcontainer,VisEventQueue * eventqueue)134 int visual_param_container_set_eventqueue (VisParamContainer *paramcontainer, VisEventQueue *eventqueue)
135 {
136 	visual_log_return_val_if_fail (paramcontainer != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
137 
138 	paramcontainer->eventqueue = eventqueue;
139 
140 	return VISUAL_OK;
141 }
142 
143 /**
144  * Get the pointer to the VisEventQueue the VisParamContainer is emitting events to.
145  *
146  * @param paramcontainer A pointer to the VisParamContainer from which the VisEventQueue is requested.
147  *
148  * @return Pointer to the VisEventQueue possibly NULL, NULL on failure.
149  */
visual_param_container_get_eventqueue(VisParamContainer * paramcontainer)150 VisEventQueue *visual_param_container_get_eventqueue (VisParamContainer *paramcontainer)
151 {
152 	visual_log_return_val_if_fail (paramcontainer != NULL, NULL);
153 
154 	return paramcontainer->eventqueue;
155 }
156 
157 /**
158  * Adds a VisParamEntry to a VisParamContainer.
159  *
160  * @param paramcontainer A pointer to the VisParamContainer in which the VisParamEntry is added.
161  * @param param A pointer to the VisParamEntry that is added to the VisParamContainer.
162  *
163  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_CONTAINER_NULL, -VISUAL_ERROR_PARAM_NULL or
164  *	error values returned by visual_list_add () on failure.
165  */
visual_param_container_add(VisParamContainer * paramcontainer,VisParamEntry * param)166 int visual_param_container_add (VisParamContainer *paramcontainer, VisParamEntry *param)
167 {
168 	visual_log_return_val_if_fail (paramcontainer != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
169 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
170 
171 	param->parent = paramcontainer;
172 
173 	/* On container add, we always set changed once, so vars can be synchronised in the plugin
174 	 * it's event loop */
175 	visual_param_entry_changed (param);
176 
177 	return visual_list_add (&paramcontainer->entries, param);
178 }
179 
180 /**
181  * Adds a list of VisParamEntry elements, the list is terminated by an entry of type VISUAL_PARAM_ENTRY_TYPE_END.
182  * All the elements are reallocated, so this function can be used for static param lists.
183  *
184  * @param paramcontainer A pointer to the VisParamContainer in which the VisParamEntry elements are added.
185  * @param params A pointer to the VisParamEntry elements that are added to the VisParamContainer.
186  *
187  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_CONTAINER_NULL or -VISUAL_ERROR_PARAM_NULL on failure.
188  */
visual_param_container_add_many(VisParamContainer * paramcontainer,VisParamEntry * params)189 int visual_param_container_add_many (VisParamContainer *paramcontainer, VisParamEntry *params)
190 {
191 	VisParamEntry *pnew;
192 	int i = 0;
193 
194 	visual_log_return_val_if_fail (paramcontainer != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
195 	visual_log_return_val_if_fail (params != NULL, -VISUAL_ERROR_PARAM_NULL);
196 
197 	while (params[i].type != VISUAL_PARAM_ENTRY_TYPE_END) {
198 		pnew = visual_param_entry_new (visual_param_entry_get_name (&params[i]));
199 		visual_param_entry_set_from_param (pnew, &params[i]);
200 
201 		visual_param_container_add (paramcontainer, pnew);
202 
203 		i++;
204 	}
205 
206 	return VISUAL_OK;
207 }
208 
209 /**
210  * Removes a VisParamEntry from the VisParamContainer by giving the name of the VisParamEntry that needs
211  * to be removed.
212  *
213  * @param paramcontainer A pointer to the VisParamContainer from which a VisParamEntry needs to be removed.
214  * @param name The name of the VisParamEntry that needs to be removed from the VisParamContainer.
215  *
216  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_CONTAINER_NULL, -VISUAL_ERROR_NULL
217  *	or -VISUAL_ERROR_PARAM_NOT_FOUND on failure.
218  */
visual_param_container_remove(VisParamContainer * paramcontainer,const char * name)219 int visual_param_container_remove (VisParamContainer *paramcontainer, const char *name)
220 {
221 	VisListEntry *le = NULL;
222 	VisParamEntry *param;
223 
224 	visual_log_return_val_if_fail (paramcontainer != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
225 	visual_log_return_val_if_fail (name != NULL, -VISUAL_ERROR_NULL);
226 
227 	while ((param = visual_list_next (&paramcontainer->entries, &le)) != NULL) {
228 
229 		if (strcmp (param->name, name) == 0) {
230 			visual_list_delete (&paramcontainer->entries, &le);
231 
232 			return VISUAL_OK;
233 		}
234 	}
235 
236 	return -VISUAL_ERROR_PARAM_NOT_FOUND;
237 }
238 
239 /**
240  * Clones the source VisParamContainer into the destination VisParamContainer. When an entry with a certain name
241  * already exists in the destination container, it will be overwritten with a new value.
242  *
243  * @param destcont A pointer to the VisParamContainer in which the VisParamEntry values are copied.
244  * @param srccont A pointer to the VisParamContainer from which the VisParamEntry values are copied.
245  *
246  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_CONTAINER_NULL, on failure.
247  */
visual_param_container_copy(VisParamContainer * destcont,VisParamContainer * srccont)248 int visual_param_container_copy (VisParamContainer *destcont, VisParamContainer *srccont)
249 {
250 	VisListEntry *le = NULL;
251 	VisParamEntry *destparam;
252 	VisParamEntry *srcparam;
253 	VisParamEntry *tempparam;
254 
255 	visual_log_return_val_if_fail (destcont != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
256 	visual_log_return_val_if_fail (srccont != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
257 
258 	while ((srcparam = visual_list_next (&srccont->entries, &le)) != NULL) {
259 		tempparam = visual_param_container_get (destcont, visual_param_entry_get_name (srcparam));
260 
261 		/* Already exists, overwrite */
262 		if (tempparam != NULL) {
263 			visual_param_entry_set_from_param (tempparam, srcparam);
264 
265 			continue;
266 		}
267 
268 		/* Does not yet exist, create a new entry */
269 		destparam = visual_param_entry_new (visual_param_entry_get_name (srcparam));
270 		visual_param_entry_set_from_param (destparam, srcparam);
271 
272 		visual_param_container_add (destcont, destparam);
273 	}
274 
275 	return VISUAL_OK;
276 }
277 
278 /**
279  * Copies matching VisParamEntry elements from srccont into destcont, matching on the name.
280  *
281  * @param destcont A pointer to the VisParamContainer in which the VisParamEntry values are copied.
282  * @param srccont A pointer to the VisParamContainer from which the VisParamEntry values are copied.
283  *
284  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_CONTAINER_NULL, on failure.
285  */
visual_param_container_copy_match(VisParamContainer * destcont,VisParamContainer * srccont)286 int visual_param_container_copy_match (VisParamContainer *destcont, VisParamContainer *srccont)
287 {
288 	VisListEntry *le = NULL;
289 	VisParamEntry *destparam;
290 	VisParamEntry *srcparam;
291 
292 	visual_log_return_val_if_fail (destcont != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
293 	visual_log_return_val_if_fail (srccont != NULL, -VISUAL_ERROR_PARAM_CONTAINER_NULL);
294 
295 	while ((destparam = visual_list_next (&destcont->entries, &le)) != NULL) {
296 		srcparam = visual_param_container_get (srccont, visual_param_entry_get_name (destparam));
297 
298 		if (srcparam != NULL)
299 			visual_param_entry_set_from_param (destparam, srcparam);
300 	}
301 
302 	return VISUAL_OK;
303 }
304 
305 /**
306  * Retrieve a VisParamEntry from a VisParamContainer by giving the name of the VisParamEntry that is requested.
307  *
308  * @param paramcontainer A pointer to the VisParamContainer from which a VisParamEntry is requested.
309  * @param name The name of the VisParamEntry that is requested from the VisParamContainer.
310  *
311  * @return Pointer to the VisParamEntry, or NULL.
312  */
visual_param_container_get(VisParamContainer * paramcontainer,const char * name)313 VisParamEntry *visual_param_container_get (VisParamContainer *paramcontainer, const char *name)
314 {
315 	VisListEntry *le = NULL;
316 	VisParamEntry *param;
317 
318 	visual_log_return_val_if_fail (paramcontainer != NULL, NULL);
319 	visual_log_return_val_if_fail (name != NULL, NULL);
320 
321 	while ((param = visual_list_next (&paramcontainer->entries, &le)) != NULL) {
322 		param = le->data;
323 
324 		if (strcmp (param->name, name) == 0)
325 			return param;
326 	}
327 
328 	return NULL;
329 }
330 
331 /**
332  * Creates a new VisParamEntry structure.
333  *
334  * @param name The name that is assigned to the VisParamEntry.
335  *
336  * @return A newly allocated VisParamEntry structure.
337  */
visual_param_entry_new(char * name)338 VisParamEntry *visual_param_entry_new (char *name)
339 {
340 	VisParamEntry *param;
341 
342 	param = visual_mem_new0 (VisParamEntry, 1);
343 
344 	/* Do the VisObject initialization */
345 	visual_object_initialize (VISUAL_OBJECT (param), TRUE, paramentry_dtor);
346 
347 	visual_param_entry_set_name (param, name);
348 
349 	visual_list_set_destroyer (&param->callbacks, visual_object_list_destroyer);
350 
351 	return param;
352 }
353 
354 /**
355  * Adds a change notification callback, this shouldn't be used to get notificated within a plugin, but is for
356  * things like VisUI.
357  *
358  * @param param Pointer to the VisParamEntry to which a change notification callback is added.
359  * @param callback The notification callback, which is called on changes in the VisParamEntry.
360  * @param priv A private that can be used in the callback function.
361  *
362  * return callback id in the form of a positive value on succes,
363  * 	-VISUAL_ERROR_PARAM_NULL, -VISUAL_ERROR_PARAM_CALLBACK_NULL or
364  * 	-VISUAL_ERROR_PARAM_CALLBACK_TOO_MANY on failure.
365  */
visual_param_entry_add_callback(VisParamEntry * param,VisParamChangedCallbackFunc callback,void * priv)366 int visual_param_entry_add_callback (VisParamEntry *param, VisParamChangedCallbackFunc callback, void *priv)
367 {
368 	VisParamEntryCallback *pcall;
369 	int id;
370 
371 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
372 	visual_log_return_val_if_fail (callback != NULL, -VISUAL_ERROR_PARAM_CALLBACK_NULL);
373 
374 	id = get_next_pcall_id (&param->callbacks);
375 
376 	visual_log_return_val_if_fail (id >= 0, -VISUAL_ERROR_PARAM_CALLBACK_TOO_MANY);
377 
378 	pcall = visual_mem_new0 (VisParamEntryCallback, 1);
379 
380 	/* Do the VisObject initialization for the VisParamEntryCallback */
381 	visual_object_initialize (VISUAL_OBJECT (pcall), TRUE, NULL);
382 
383 	pcall->id = id;
384 	pcall->callback = callback;
385 	visual_object_set_private (VISUAL_OBJECT (pcall), priv);
386 
387 	visual_list_add (&param->callbacks, pcall);
388 
389 	return id;
390 }
391 
392 /**
393  * Removes a change notification callback from the list of callbacks.
394  *
395  * @param param Pointer to the VisParamEntry from which a change notification callback is removed.
396  * @param id The callback ID that was given by the visual_param_entry_add_callback method.
397  *
398  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
399  */
visual_param_entry_remove_callback(VisParamEntry * param,int id)400 int visual_param_entry_remove_callback (VisParamEntry *param, int id)
401 {
402 	VisListEntry *le = NULL;
403 	VisParamEntryCallback *pcall;
404 
405 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
406 
407 	while ((pcall = visual_list_next (&param->callbacks, &le)) != NULL) {
408 
409 		if (id == pcall->id) {
410 			visual_list_delete (&param->callbacks, &le);
411 
412 			visual_object_unref (VISUAL_OBJECT (pcall));
413 
414 			return VISUAL_OK;
415 		}
416 	}
417 
418 	return VISUAL_OK;
419 }
420 
421 /**
422  * Notifies all the callbacks for the given VisParamEntry parameter.
423  *
424  * @param param Pointer to the VisParamEntry of which all the change notification
425  * 	callbacks need to be called.
426  *
427  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
428  */
visual_param_entry_notify_callbacks(VisParamEntry * param)429 int visual_param_entry_notify_callbacks (VisParamEntry *param)
430 {
431 	VisListEntry *le = NULL;
432 	VisParamEntryCallback *pcall;
433 
434 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
435 
436 	while ((pcall = visual_list_next (&param->callbacks, &le)) != NULL)
437 		pcall->callback (param, visual_object_get_private (VISUAL_OBJECT (pcall)));
438 
439 	return VISUAL_OK;
440 }
441 
442 /**
443  * Checks if the VisParamEntry it's name is the given name.
444  *
445  * @param param Pointer to the VisParamEntry of which we want to check the name.
446  * @param name The name we want to check against.
447  *
448  * @return TRUE if the VisParamEntry is the one we requested, or FALSE if not.
449  */
visual_param_entry_is(VisParamEntry * param,const char * name)450 int visual_param_entry_is (VisParamEntry *param, const char *name)
451 {
452 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
453 
454 	if (strcmp (param->name, name) == 0)
455 		return TRUE;
456 
457 	return FALSE;
458 }
459 
460 /**
461  * When called, emits an event in the VisParamContainer it's VisEventQueue when the VisEventQueue
462  * is set.
463  *
464  * @param param Pointer to the VisParamEntry that is changed.
465  *
466  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
467  */
visual_param_entry_changed(VisParamEntry * param)468 int visual_param_entry_changed (VisParamEntry *param)
469 {
470 	VisEventQueue *eventqueue;
471 
472 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
473 
474 	if (param->parent == NULL)
475 		return VISUAL_OK;
476 
477 	eventqueue = param->parent->eventqueue;
478 
479 	if (eventqueue != NULL)
480 		visual_event_queue_add_param (eventqueue, param);
481 
482 	visual_param_entry_notify_callbacks (param);
483 
484 	return VISUAL_OK;
485 }
486 
487 /**
488  * Retrieves the VisParamEntryType from a VisParamEntry.
489  *
490  * @param param Pointer to the VisParamEntry from which the VisParamEntryType is requested.
491  *
492  * @return The VisParamEntryType on succes, -VISUAL_ERROR_PARAM_NULL on failure.
493  */
visual_param_entry_get_type(VisParamEntry * param)494 VisParamEntryType visual_param_entry_get_type (VisParamEntry *param)
495 {
496 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
497 
498 	return param->type;
499 }
500 
501 /**
502  * Compares two parameters with each other, When they are the same, TRUE is returned, if not FALSE.
503  * Keep in mind that FALSE is always returned for VISUAL_PARAM_ENTRY_TYPE_PALETTE and VISUAL_PARAM_ENTRY_TYPE_OBJECT.
504  *
505  * @param src1 Pointer to the first VisParamEntry for comparison.
506  * @param src2 Pointer to the second VisParamEntry for comparison.
507  *
508  * @return TRUE if the same, FALSE if not the same,
509  *	-VISUAL_ERROR_PARAM_NULL, -VISUAL_ERROR_PARAM_INVALID_TYPE or -VISUAL_ERROR_IMPOSSIBLE on failure.
510  */
visual_param_entry_compare(VisParamEntry * src1,VisParamEntry * src2)511 int visual_param_entry_compare (VisParamEntry *src1, VisParamEntry *src2)
512 {
513 	visual_log_return_val_if_fail (src1 != NULL, -VISUAL_ERROR_PARAM_NULL);
514 	visual_log_return_val_if_fail (src2 != NULL, -VISUAL_ERROR_PARAM_NULL);
515 
516 	if (src1->type != src2->type)
517 		return FALSE;
518 
519 	switch (src1->type) {
520 		case VISUAL_PARAM_ENTRY_TYPE_NULL:
521 			return TRUE;
522 
523 			break;
524 
525 		case VISUAL_PARAM_ENTRY_TYPE_STRING:
526 			if (!strcmp (src1->string, src2->string))
527 				return TRUE;
528 
529 			break;
530 
531 		case VISUAL_PARAM_ENTRY_TYPE_INTEGER:
532 			if (src1->numeric.integer == src2->numeric.integer)
533 				return TRUE;
534 
535 			break;
536 
537 		case VISUAL_PARAM_ENTRY_TYPE_FLOAT:
538 			if (src1->numeric.floating == src2->numeric.floating)
539 				return TRUE;
540 
541 			break;
542 
543 		case VISUAL_PARAM_ENTRY_TYPE_DOUBLE:
544 			if (src1->numeric.doubleflt == src2->numeric.doubleflt)
545 				return TRUE;
546 
547 			break;
548 
549 		case VISUAL_PARAM_ENTRY_TYPE_COLOR:
550 			return visual_color_compare (&src1->color, &src2->color);
551 
552 			break;
553 
554 		case VISUAL_PARAM_ENTRY_TYPE_PALETTE:
555 			return FALSE;
556 
557 			break;
558 
559 		case VISUAL_PARAM_ENTRY_TYPE_OBJECT:
560 			return FALSE;
561 
562 			break;
563 
564 		default:
565 			visual_log (VISUAL_LOG_CRITICAL, "param type is not valid");
566 
567 			return -VISUAL_ERROR_PARAM_INVALID_TYPE;
568 
569 			break;
570 	}
571 
572 	return -VISUAL_ERROR_IMPOSSIBLE;
573 }
574 
575 /**
576  * Copies the value of the src param into the param. Also sets the param to the type of which the
577  * source param is.
578  *
579  * @param param Pointer to the VisParamEntry to which a parameter is set.
580  * @param src Pointer to the VisParamEntry from which the value is retrieved.
581  *
582  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL, -VISUAL_ERROR_PARAM_INVALID_TYPE on failure.
583  */
visual_param_entry_set_from_param(VisParamEntry * param,VisParamEntry * src)584 int visual_param_entry_set_from_param (VisParamEntry *param, VisParamEntry *src)
585 {
586 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
587 	visual_log_return_val_if_fail (src != NULL, -VISUAL_ERROR_PARAM_NULL);
588 
589 	switch (src->type) {
590 		case VISUAL_PARAM_ENTRY_TYPE_NULL:
591 
592 			break;
593 
594 		case VISUAL_PARAM_ENTRY_TYPE_STRING:
595 			visual_param_entry_set_string (param, visual_param_entry_get_string (src));
596 			break;
597 
598 		case VISUAL_PARAM_ENTRY_TYPE_INTEGER:
599 			visual_param_entry_set_integer (param, visual_param_entry_get_integer (src));
600 
601 			break;
602 
603 		case VISUAL_PARAM_ENTRY_TYPE_FLOAT:
604 			visual_param_entry_set_float (param, visual_param_entry_get_float (src));
605 
606 			break;
607 
608 		case VISUAL_PARAM_ENTRY_TYPE_DOUBLE:
609 			visual_param_entry_set_double (param, visual_param_entry_get_double (src));
610 
611 			break;
612 
613 		case VISUAL_PARAM_ENTRY_TYPE_COLOR:
614 			visual_param_entry_set_color_by_color (param, visual_param_entry_get_color (src));
615 
616 			break;
617 
618 		case VISUAL_PARAM_ENTRY_TYPE_PALETTE:
619 			visual_param_entry_set_palette (param, visual_param_entry_get_palette (src));
620 
621 			break;
622 
623 		case VISUAL_PARAM_ENTRY_TYPE_OBJECT:
624 			visual_param_entry_set_object (param, visual_param_entry_get_object (src));
625 
626 			break;
627 
628 		default:
629 			visual_log (VISUAL_LOG_CRITICAL, "param type is not valid");
630 
631 			return -VISUAL_ERROR_PARAM_INVALID_TYPE;
632 
633 			break;
634 	}
635 
636 	return VISUAL_OK;
637 }
638 
639 /**
640  * Set the name for a VisParamEntry.
641  *
642  * @param param Pointer to the VisParamEntry to which the name is set.
643  * @param name The name that is set to the VisParamEntry.
644  *
645  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
646  */
visual_param_entry_set_name(VisParamEntry * param,char * name)647 int visual_param_entry_set_name (VisParamEntry *param, char *name)
648 {
649 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
650 
651 	if (param->name != NULL)
652 		visual_mem_free (param->name);
653 
654 	param->name = NULL;
655 
656 	if (name != NULL)
657 		param->name = strdup (name);
658 
659 	return VISUAL_OK;
660 }
661 
662 /**
663  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_STRING and assigns the string given as argument to it.
664  *
665  * @param param Pointer to the VisParamEntry to which a parameter is set.
666  * @param string The string for this parameter.
667  *
668  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
669  */
visual_param_entry_set_string(VisParamEntry * param,char * string)670 int visual_param_entry_set_string (VisParamEntry *param, char *string)
671 {
672 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
673 
674 	param->type = VISUAL_PARAM_ENTRY_TYPE_STRING;
675 
676 	if (string == NULL && param->string == NULL)
677 		return VISUAL_OK;
678 
679 	if (string == NULL && param->string != NULL) {
680 		visual_mem_free (param->string);
681 		param->string = NULL;
682 
683 		visual_param_entry_changed (param);
684 
685 	} else if (param->string == NULL && string != NULL) {
686 		param->string = strdup (string);
687 
688 		visual_param_entry_changed (param);
689 
690 	} else if (strcmp (string, param->string) != 0) {
691 		visual_mem_free (param->string);
692 
693 		param->string = strdup (string);
694 
695 		visual_param_entry_changed (param);
696 	}
697 
698 	return VISUAL_OK;
699 }
700 
701 /**
702  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_INTEGER and assigns the integer given as argument to it.
703  *
704  * @param param Pointer to the VisParamEntry to which a parameter is set.
705  * @param integer The integer value for this parameter.
706  *
707  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
708  */
visual_param_entry_set_integer(VisParamEntry * param,int integer)709 int visual_param_entry_set_integer (VisParamEntry *param, int integer)
710 {
711 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
712 
713 	param->type = VISUAL_PARAM_ENTRY_TYPE_INTEGER;
714 
715 	if (param->numeric.integer != integer) {
716 		param->numeric.integer = integer;
717 
718 		visual_param_entry_changed (param);
719 	}
720 
721 	return VISUAL_OK;
722 }
723 
724 /**
725  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_FLOAT and assigns the float given as argument to it.
726  *
727  * @param param Pointer to the VisParamEntry to which a parameter is set.
728  * @param floating The float value for this parameter.
729  *
730  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
731  */
visual_param_entry_set_float(VisParamEntry * param,float floating)732 int visual_param_entry_set_float (VisParamEntry *param, float floating)
733 {
734 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
735 
736 	param->type = VISUAL_PARAM_ENTRY_TYPE_FLOAT;
737 
738 	if (param->numeric.floating != floating) {
739 		param->numeric.floating = floating;
740 
741 		visual_param_entry_changed (param);
742 	}
743 
744 	return VISUAL_OK;
745 }
746 
747 /**
748  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_DOUBLE and assigns the double given as argument to it.
749  *
750  * @param param Pointer to the VisParamEntry to which a parameter is set.
751  * @param doubleflt The double value for this parameter.
752  *
753  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
754  */
visual_param_entry_set_double(VisParamEntry * param,double doubleflt)755 int visual_param_entry_set_double (VisParamEntry *param, double doubleflt)
756 {
757 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
758 
759 	param->type = VISUAL_PARAM_ENTRY_TYPE_DOUBLE;
760 
761 	if (param->numeric.doubleflt != doubleflt) {
762 		param->numeric.doubleflt = doubleflt;
763 
764 		visual_param_entry_changed (param);
765 	}
766 
767 	return VISUAL_OK;
768 }
769 
770 /**
771  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_COLOR and assigns the rgb values given as arguments to it.
772  *
773  * @param param Pointer to the VisParamEntry to which a parameter is set.
774  * @param r The red value for this color parameter.
775  * @param g The green value for this color parameter.
776  * @param b The blue value for this color parameter.
777  *
778  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
779  */
visual_param_entry_set_color(VisParamEntry * param,uint8_t r,uint8_t g,uint8_t b)780 int visual_param_entry_set_color (VisParamEntry *param, uint8_t r, uint8_t g, uint8_t b)
781 {
782 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
783 
784 	param->type = VISUAL_PARAM_ENTRY_TYPE_COLOR;
785 
786 	if (param->color.r != r || param->color.g != g || param->color.b != b) {
787 		param->color.r = r;
788 		param->color.g = g;
789 		param->color.b = b;
790 
791 		visual_param_entry_changed (param);
792 	}
793 
794 	return VISUAL_OK;
795 }
796 
797 /**
798  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_COLOR and assigns the rgb values from the given VisColor as argument to it.
799  *
800  * @param param Pointer to the VisParamEntry to which a parameter is set.
801  * @param color Pointer to the VisColor from which the rgb values are copied into the parameter.
802  *
803  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
804  */
visual_param_entry_set_color_by_color(VisParamEntry * param,VisColor * color)805 int visual_param_entry_set_color_by_color (VisParamEntry *param, VisColor *color)
806 {
807 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
808 
809 	param->type = VISUAL_PARAM_ENTRY_TYPE_COLOR;
810 
811 	if (visual_color_compare (&param->color, color) == FALSE) {
812 		visual_color_copy (&param->color, color);
813 
814 		visual_param_entry_changed (param);
815 	}
816 
817 	return VISUAL_OK;
818 }
819 
820 /**
821  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_PALETTE and assigns a VisPalette to the VisParamEntry.
822  * This function does not check if there is a difference between the prior set palette and the new one, and always
823  * emits the changed event. so watch out with usage.
824  *
825  * @param param Pointer to the VisParamEntry to which a parameter is set.
826  * @param pal Pointer to the VisPalette from which the palette data is retrieved for the VisParamEntry.
827  *
828  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
829  */
visual_param_entry_set_palette(VisParamEntry * param,VisPalette * pal)830 int visual_param_entry_set_palette (VisParamEntry *param, VisPalette *pal)
831 {
832 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
833 
834 	param->type = VISUAL_PARAM_ENTRY_TYPE_PALETTE;
835 
836 	visual_palette_free_colors (&param->pal);
837 
838 	if (pal != NULL) {
839 		visual_palette_allocate_colors (&param->pal, pal->ncolors);
840 
841 		visual_palette_copy (&param->pal, pal);
842 	}
843 
844 	visual_param_entry_changed (param);
845 
846 	return VISUAL_OK;
847 }
848 
849 /**
850  * Sets the VisParamEntry to VISUAL_PARAM_ENTRY_TYPE_OBJECT and assigns a VisObject to the VisParamEntry.
851  * With a VisObject VisParamEntry, the VisObject is referenced, not cloned.
852  *
853  * @param param Pointer to the VisParamEntry to which a parameter is set.
854  * @param object Pointer to the VisObject that is linked to the VisParamEntry.
855  *
856  * @return VISUAL_OK on succes, -VISUAL_ERROR_PARAM_NULL on failure.
857  */
visual_param_entry_set_object(VisParamEntry * param,VisObject * object)858 int visual_param_entry_set_object (VisParamEntry *param, VisObject *object)
859 {
860 	visual_log_return_val_if_fail (param != NULL, -VISUAL_ERROR_PARAM_NULL);
861 
862 	param->type = VISUAL_PARAM_ENTRY_TYPE_OBJECT;
863 
864 	if (param->objdata != NULL)
865 		visual_object_unref (param->objdata);
866 
867 	param->objdata = object;
868 
869 	if (param->objdata != NULL)
870 		visual_object_ref (param->objdata);
871 
872 	visual_param_entry_changed (param);
873 
874 	return VISUAL_OK;
875 }
876 
877 /**
878  * Get the name of the VisParamEntry.
879  *
880  * @param param Pointer to the VisParamEntry from which the name is requested.
881  *
882  * @return The name of the VisParamEntry or NULL.
883  */
visual_param_entry_get_name(VisParamEntry * param)884 char *visual_param_entry_get_name (VisParamEntry *param)
885 {
886 	visual_log_return_val_if_fail (param != NULL, NULL);
887 
888 	return param->name;
889 }
890 
891 /**
892  * Get the string parameter from a VisParamEntry.
893  *
894  * @param param Pointer to the VisParamEntry from which the string parameter is requested.
895  *
896  * @return The string parameter from the VisParamEntry or NULL.
897  */
visual_param_entry_get_string(VisParamEntry * param)898 char *visual_param_entry_get_string (VisParamEntry *param)
899 {
900 	visual_log_return_val_if_fail (param != NULL, NULL);
901 
902 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_STRING) {
903 		visual_log (VISUAL_LOG_WARNING, "Requesting string from a non string param");
904 
905 		return NULL;
906 	}
907 
908 	return param->string;
909 }
910 
911 /**
912  * Get the integer parameter from a VisParamEntry.
913  *
914  * @param param Pointer to the VisParamEntry from which the integer parameter is requested.
915  *
916  * @return The integer parameter from the VisParamEntry.
917  */
visual_param_entry_get_integer(VisParamEntry * param)918 int visual_param_entry_get_integer (VisParamEntry *param)
919 {
920 	visual_log_return_val_if_fail (param != NULL, 0);
921 
922 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_INTEGER)
923 		visual_log (VISUAL_LOG_WARNING, "Requesting integer from a non integer param");
924 
925 	return param->numeric.integer;
926 }
927 
928 /**
929  * Get the float parameter from a VisParamEntry.
930  *
931  * @param param Pointer to the VisParamEntry from which the float parameter is requested.
932  *
933  * @return The float parameter from the VisParamEntry.
934  */
visual_param_entry_get_float(VisParamEntry * param)935 float visual_param_entry_get_float (VisParamEntry *param)
936 {
937 	visual_log_return_val_if_fail (param != NULL, 0);
938 
939 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_FLOAT)
940 		visual_log (VISUAL_LOG_WARNING, "Requesting float from a non float param");
941 
942 	return param->numeric.floating;
943 }
944 
945 /**
946  * Get the double parameter from a VisParamEntry.
947  *
948  * @param param Pointer to the VisParamEntry from which the double parameter is requested.
949  *
950  * @return The double parameter from the VisParamEntry.
951  */
visual_param_entry_get_double(VisParamEntry * param)952 double visual_param_entry_get_double (VisParamEntry *param)
953 {
954 	visual_log_return_val_if_fail (param != NULL, 0);
955 
956 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_DOUBLE)
957 		visual_log (VISUAL_LOG_WARNING, "Requesting double from a non double param");
958 
959 	return param->numeric.doubleflt;
960 }
961 
962 /**
963  * Get the color parameter from a VisParamEntry.
964  *
965  * @param param Pointer to the VisParamEntry from which the color parameter is requested.
966  *
967  * @return Pointer to the VisColor parameter from the VisParamEntry. It's adviced to
968  *	use the VisColor that is returned as read only seen changing it directly won't emit events and
969  *	can cause synchronous problems between the plugin and the parameter system. Instead use the
970  *	visual_param_entry_set_color* methods to change the parameter value.
971  */
visual_param_entry_get_color(VisParamEntry * param)972 VisColor *visual_param_entry_get_color (VisParamEntry *param)
973 {
974 	visual_log_return_val_if_fail (param != NULL, NULL);
975 
976 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_COLOR) {
977 		visual_log (VISUAL_LOG_WARNING, "Requesting color from a non color param");
978 
979 		return NULL;
980 	}
981 
982 	return &param->color;
983 }
984 
985 /**
986  * Get the palette parameter from a VisParamEntry.
987  *
988  * @param param Pointer to the VisParamEntry from which the palette parameter is requested.
989  *
990  * @return Pointer to the VisPalette parameter from the VisParamEntry. The returned VisPalette
991  *	should be exclusively used as read only.
992  */
visual_param_entry_get_palette(VisParamEntry * param)993 VisPalette *visual_param_entry_get_palette (VisParamEntry *param)
994 {
995 	visual_log_return_val_if_fail (param != NULL, NULL);
996 
997 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_PALETTE) {
998 		visual_log (VISUAL_LOG_WARNING, "Requested palette from a non palette param\n");
999 
1000 		return NULL;
1001 	}
1002 
1003 	return &param->pal;
1004 }
1005 
1006 /**
1007  * Get the object parameter from a VisParamEntry.
1008  *
1009  * @param param Pointer to the VisParamEntry from which the object parameter is requested.
1010  *
1011  * @return Pointer to the VisObject parameter from the VisParamEntry.
1012  */
visual_param_entry_get_object(VisParamEntry * param)1013 VisObject *visual_param_entry_get_object (VisParamEntry *param)
1014 {
1015 	visual_log_return_val_if_fail (param != NULL, NULL);
1016 
1017 	if (param->type != VISUAL_PARAM_ENTRY_TYPE_OBJECT) {
1018 		visual_log (VISUAL_LOG_WARNING, "Requested object from a non object param\n");
1019 
1020 		return NULL;
1021 	}
1022 
1023 	return param->objdata;
1024 }
1025 
1026 /**
1027  * @}
1028  */
1029 
1030