]> code.delx.au - pulseaudio/blob - polyp/core.c
e2bebec25e8941c772cdbf84d7b14dd7be297330
[pulseaudio] / polyp / core.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <stdio.h>
29 #include <signal.h>
30
31 #include "core.h"
32 #include "module.h"
33 #include "sink.h"
34 #include "source.h"
35 #include "namereg.h"
36 #include "util.h"
37 #include "scache.h"
38 #include "autoload.h"
39 #include "xmalloc.h"
40 #include "subscribe.h"
41 #include "props.h"
42 #include "random.h"
43
44 struct pa_core* pa_core_new(struct pa_mainloop_api *m) {
45 struct pa_core* c;
46 c = pa_xmalloc(sizeof(struct pa_core));
47
48 c->mainloop = m;
49 c->clients = pa_idxset_new(NULL, NULL);
50 c->sinks = pa_idxset_new(NULL, NULL);
51 c->sources = pa_idxset_new(NULL, NULL);
52 c->source_outputs = pa_idxset_new(NULL, NULL);
53 c->sink_inputs = pa_idxset_new(NULL, NULL);
54
55 c->default_source_name = c->default_sink_name = NULL;
56
57 c->modules = NULL;
58 c->namereg = NULL;
59 c->scache = NULL;
60 c->autoload_idxset = NULL;
61 c->autoload_hashmap = NULL;
62 c->running_as_daemon = 0;
63
64 c->default_sample_spec.format = PA_SAMPLE_S16NE;
65 c->default_sample_spec.rate = 44100;
66 c->default_sample_spec.channels = 2;
67
68 c->module_auto_unload_event = NULL;
69 c->module_defer_unload_event = NULL;
70 c->scache_auto_unload_event = NULL;
71
72 c->subscription_defer_event = NULL;
73 c->subscription_event_queue = NULL;
74 c->subscriptions = NULL;
75
76 c->memblock_stat = pa_memblock_stat_new();
77
78 c->disallow_module_loading = 0;
79
80 c->quit_event = NULL;
81
82 c->exit_idle_time = -1;
83 c->module_idle_time = 20;
84 c->scache_idle_time = 20;
85
86 c->resample_method = PA_RESAMPLER_SRC_SINC_FASTEST;
87
88 pa_property_init(c);
89
90 pa_random(&c->cookie, sizeof(c->cookie));
91
92 pa_check_signal_is_blocked(SIGPIPE);
93 return c;
94 }
95
96 void pa_core_free(struct pa_core *c) {
97 assert(c);
98
99 pa_module_unload_all(c);
100 assert(!c->modules);
101
102 assert(pa_idxset_isempty(c->clients));
103 pa_idxset_free(c->clients, NULL, NULL);
104
105 assert(pa_idxset_isempty(c->sinks));
106 pa_idxset_free(c->sinks, NULL, NULL);
107
108 assert(pa_idxset_isempty(c->sources));
109 pa_idxset_free(c->sources, NULL, NULL);
110
111 assert(pa_idxset_isempty(c->source_outputs));
112 pa_idxset_free(c->source_outputs, NULL, NULL);
113
114 assert(pa_idxset_isempty(c->sink_inputs));
115 pa_idxset_free(c->sink_inputs, NULL, NULL);
116
117 pa_scache_free(c);
118 pa_namereg_free(c);
119 pa_autoload_free(c);
120 pa_subscription_free_all(c);
121
122 if (c->quit_event)
123 c->mainloop->time_free(c->quit_event);
124
125 pa_xfree(c->default_source_name);
126 pa_xfree(c->default_sink_name);
127
128 pa_memblock_stat_unref(c->memblock_stat);
129
130 pa_property_cleanup(c);
131
132 pa_xfree(c);
133 }
134
135 static void quit_callback(struct pa_mainloop_api*m, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
136 struct pa_core *c = userdata;
137 assert(c->quit_event = e);
138
139 m->quit(m, 0);
140 }
141
142 void pa_core_check_quit(struct pa_core *c) {
143 assert(c);
144
145 if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_ncontents(c->clients) == 0) {
146 struct timeval tv;
147 gettimeofday(&tv, NULL);
148 tv.tv_sec+= c->exit_idle_time;
149 c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
150 } else if (c->quit_event && pa_idxset_ncontents(c->clients) > 0) {
151 c->mainloop->time_free(c->quit_event);
152 c->quit_event = NULL;
153 }
154 }
155