summaryrefslogtreecommitdiff
path: root/src/sink.c
blob: 8f6b13b1536d2f65fe5a2f76ff76981efb4223a6 (plain)
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
#include <stdio.h>
#include <pulse/pulseaudio.h>
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>

#include "sink_input.h"
#include "sink.h"

/*
 * return an initilized sink
 */
sink_info* sink_init(void) {
	
	sink_info* sink = (sink_info*) calloc(1, sizeof(sink_info));
	
	sink->name = NULL;
	sink->device = NULL;
	sink->input_counter = 0;
	sink->input_max = 1;
	sink->input_list = NULL;

	sink->input_list = sink_input_list_init(sink->input_max);

	return sink;
}

/*
 * frees all components of a sink 
 */
void sink_clear(sink_info* sink) {

	if (sink->name != NULL)
		free(sink->name);
	
	if (sink->device != NULL)
		free(sink->device);

	sink_input_list_clear(sink->input_list, &sink->input_max);

	free(sink);
	//sink = NULL;
}

void sink_check(sink_info** sink) {

	if (sink == NULL)
		printf("Error: sink = NULL\n");

	if ((*sink) == NULL)
		(*sink) = sink_init();
}

/*
 * check the list length and resize the list, if current position = max
 */
void sink_list_check(sink_info*** sink_list, uint32_t* max, uint32_t counter) {
	if (counter < *max)
		return;

	*max *= 2;
	*sink_list = (sink_info**) realloc(*sink_list, (*max) * sizeof(sink_info*));

	
	for (int i = counter; i < *max; ++i)
		(*sink_list)[i] = NULL;

}

void sink_check_input_list(sink_info* sink) {
	
	if (sink->input_counter >= sink->input_max)
		sink_input_list_enlarge(&sink->input_list, &sink->input_max, sink->input_counter);
}

sink_info** sink_list_init(uint32_t max) {

	sink_info** sink_list = (sink_info**) calloc(max, sizeof(sink_info*));

	for (int i = 0; i < max; ++i)
		sink_list[i] = NULL;

	return sink_list;
}

void sink_list_reset(sink_info** sink_list, uint32_t* counter) {

	for (int i = 0; i < (*counter); ++i)
		sink_list[i]->input_counter = 0;

	(*counter) = 0;
}

void sink_list_clear(sink_info** sink_list, uint32_t* max, uint32_t* counter) {

	for (int i = 0; i < (*max); ++i)
		if (sink_list[i] != NULL)
			sink_clear(sink_list[i]);

	(*max) = 0;
	(*counter) = 0;
	
	free(sink_list);
	// sink_list = NULL; 

	/* TODO: for all *_clear:
	 * setting local parameter to NULL doesnt do want is wanted
	 * pointer to sink_list would be needed here...
	 */
}