summaryrefslogtreecommitdiffstats
path: root/parecord/device.c
blob: 24f94bae512b5bc10c3bb77f5426e52ff8ae2818 (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
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
#include <string.h>
#include <pulse/pulseaudio.h>
#include <stdio.h>

#include "device.h"

static struct list_devices *g_input; // struct array
static int input_idx;
static int input_num; // size of struct array

static void context_state_callback(pa_context *c, void *userdata)
{
	int *ready = userdata;

	switch(pa_context_get_state(c)) {
		case PA_CONTEXT_CONNECTING:
		case PA_CONTEXT_AUTHORIZING:
		case PA_CONTEXT_SETTING_NAME:
			break;

		case PA_CONTEXT_READY: {
			*ready = 1;
			break;
		}

		case PA_CONTEXT_TERMINATED:
		case PA_CONTEXT_FAILED:
		default: {
			*ready = 2;
			// error
			break;
		}
	}
}

static void context_sourcelist_callback(pa_context *c,
	const pa_source_info *i, int eol, void *userdata)
{
	// end of list
	if(eol > 0)
		return;

	if(input_idx >= input_num)
	{
		int i;
		struct list_devices *tmp = malloc(
			sizeof(struct list_devices) * (input_num+1));

		for(i = 0; i < input_num; i++) {
			tmp[i].name = g_input[i].name;
			tmp[i].description = g_input[i].description;
		}
		input_num++;
		free(g_input);
		g_input = tmp; 
	}
	g_input[input_idx].name
		= malloc((strlen(i->name)+1)*sizeof(char));
	strcpy(g_input[input_idx].name, i->name);

	g_input[input_idx].description
		= malloc((strlen(i->description)+1)*sizeof(char));
	strcpy(g_input[input_idx].description, i->description);
	
	input_idx++;
}

struct list_devices *getInputDeviceList(int *len)
{
	pa_mainloop *ml;
	pa_mainloop_api *ml_api;
	pa_context *context;
	pa_operation *operation;
	int state = 0;
	int ready = 0;

	if(!(ml = pa_mainloop_new()))
		return NULL;
	ml_api = pa_mainloop_get_api(ml);
	context = pa_context_new(ml_api, NULL);

	// Connect the context
	if(pa_context_connect(context, NULL, 0, NULL) < 0)
		return NULL;
	pa_context_set_state_callback(context, context_state_callback, &ready);

	input_idx = 0;
	input_num = 3;
	g_input = malloc(
		sizeof(struct list_devices) * input_num);

	for(;;) {
		if(ready == 0) {
			pa_mainloop_iterate(ml, 1, NULL);
			continue;
		}
		if(ready == 2) {
			pa_context_disconnect(context);
			pa_context_unref(context);
			pa_mainloop_free(ml);
			return NULL;
		}

		switch(state) {
			case 0: {
				operation = pa_context_get_source_info_list(
					context, context_sourcelist_callback, NULL);

				state++;
				break;
			}
			case 1: {
				if(pa_operation_get_state(operation) == PA_OPERATION_DONE) {
					pa_operation_unref(operation);
					pa_context_disconnect(context);
					pa_context_unref(context);
					pa_mainloop_free(ml);
					*len = input_num;
					return g_input;
				}
				break;
			}
			default: {
				pa_mainloop_free(ml);
				return NULL;
			}
		}
		pa_mainloop_iterate(ml, 1, NULL);
	}
}

void freeDeviceList(struct list_devices *list, int len)
{
	int i;
	for(i = 0; i < len; i++)
	{
		free(list[i].name);
		free(list[i].description);
	}
	free(list);
}