]> code.delx.au - pulseaudio/blob - polyp/core.c
0b33c1070af7c8152a260f991b87b4f50ae6be57
[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 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 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
30 #include "core.h"
31 #include "module.h"
32 #include "sink.h"
33 #include "source.h"
34 #include "namereg.h"
35 #include "util.h"
36 #include "scache.h"
37 #include "autoload.h"
38 #include "xmalloc.h"
39 #include "subscribe.h"
40
41 struct pa_core* pa_core_new(struct pa_mainloop_api *m) {
42 struct pa_core* c;
43 c = pa_xmalloc(sizeof(struct pa_core));
44
45 c->mainloop = m;
46 c->clients = pa_idxset_new(NULL, NULL);
47 c->sinks = pa_idxset_new(NULL, NULL);
48 c->sources = pa_idxset_new(NULL, NULL);
49 c->source_outputs = pa_idxset_new(NULL, NULL);
50 c->sink_inputs = pa_idxset_new(NULL, NULL);
51
52 c->default_source_name = c->default_sink_name = NULL;
53
54 c->modules = NULL;
55 c->namereg = NULL;
56 c->scache = NULL;
57
58 c->autoload_hashmap = NULL;
59
60 c->default_sample_spec.format = PA_SAMPLE_S16NE;
61 c->default_sample_spec.rate = 44100;
62 c->default_sample_spec.channels = 2;
63
64 c->auto_unload_event = NULL;
65
66 c->subscription_defer_event = NULL;
67 c->subscription_event_queue = NULL;
68 c->subscriptions = NULL;
69
70 c->memblock_stat = pa_memblock_stat_new();
71
72 c->disallow_module_loading = 0;
73
74 c->quit_event = NULL;
75
76 c->exit_idle_time = -1;
77 c->module_idle_time = 20;
78
79 pa_check_for_sigpipe();
80
81 return c;
82 }
83
84 void pa_core_free(struct pa_core *c) {
85 assert(c);
86
87 pa_module_unload_all(c);
88 assert(!c->modules);
89
90 assert(pa_idxset_isempty(c->clients));
91 pa_idxset_free(c->clients, NULL, NULL);
92
93 assert(pa_idxset_isempty(c->sinks));
94 pa_idxset_free(c->sinks, NULL, NULL);
95
96 assert(pa_idxset_isempty(c->sources));
97 pa_idxset_free(c->sources, NULL, NULL);
98
99 assert(pa_idxset_isempty(c->source_outputs));
100 pa_idxset_free(c->source_outputs, NULL, NULL);
101
102 assert(pa_idxset_isempty(c->sink_inputs));
103 pa_idxset_free(c->sink_inputs, NULL, NULL);
104
105 pa_scache_free(c);
106 pa_namereg_free(c);
107 pa_autoload_free(c);
108 pa_subscription_free_all(c);
109
110 if (c->quit_event) {
111 c->mainloop->time_free(c->quit_event);
112 c->quit_event = NULL;
113 }
114
115 pa_xfree(c->default_source_name);
116 pa_xfree(c->default_sink_name);
117
118 pa_memblock_stat_unref(c->memblock_stat);
119
120 pa_xfree(c);
121 }
122
123 static void quit_callback(struct pa_mainloop_api*m, struct pa_time_event *e, const struct timeval *tv, void *userdata) {
124 struct pa_core *c = userdata;
125 assert(c->quit_event = e);
126
127 m->quit(m, 0);
128 }
129
130 void pa_core_check_quit(struct pa_core *c) {
131 assert(c);
132
133 if (!c->quit_event && c->exit_idle_time >= 0 && pa_idxset_ncontents(c->clients) == 0) {
134 struct timeval tv;
135 gettimeofday(&tv, NULL);
136 tv.tv_sec+= c->exit_idle_time;
137 c->quit_event = c->mainloop->time_new(c->mainloop, &tv, quit_callback, c);
138 } else if (c->quit_event && pa_idxset_ncontents(c->clients) > 0) {
139 c->mainloop->time_free(c->quit_event);
140 c->quit_event = NULL;
141 }
142 }
143