clean up hal patch to use pa_xnew and timeval compatibility wrappers

git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1190 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Shahms E. King 2006-08-07 20:29:31 +00:00
parent 5d8ccfd839
commit b382df5868
2 changed files with 28 additions and 74 deletions

View file

@ -27,6 +27,7 @@
#include <pulsecore/log.h> #include <pulsecore/log.h>
#include <pulsecore/props.h> #include <pulsecore/props.h>
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulse/timeval.h>
#include "dbus-util.h" #include "dbus-util.h"
@ -82,14 +83,6 @@ get_watch_flags(DBusWatch *watch)
return events; return events;
} }
static void timeval_next(struct timeval *tv, int millint)
{
/* number of seconds in the milli-second interval */
tv->tv_sec += (millint / 1000);
/* milliseconds minus the seconds portion, converted to microseconds */
tv->tv_usec += (millint - tv->tv_sec * 1000) * 1000;
}
/* pa_io_event_cb_t IO event handler */ /* pa_io_event_cb_t IO event handler */
static void handle_io_event(PA_GCC_UNUSED pa_mainloop_api *ea, pa_io_event *e, static void handle_io_event(PA_GCC_UNUSED pa_mainloop_api *ea, pa_io_event *e,
int fd, pa_io_event_flags_t events, void *userdata) int fd, pa_io_event_flags_t events, void *userdata)
@ -128,7 +121,7 @@ static void handle_time_event(pa_mainloop_api *ea, pa_time_event* e,
dbus_timeout_handle(timeout); dbus_timeout_handle(timeout);
/* restart it for the next scheduled time */ /* restart it for the next scheduled time */
timeval_next(&next, dbus_timeout_get_interval(timeout)); pa_timeval_add(&next, dbus_timeout_get_interval(timeout) * 1000);
ea->time_restart(e, &next); ea->time_restart(e, &next);
} }
} }
@ -182,10 +175,10 @@ static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
if (!dbus_timeout_get_enabled(timeout)) if (!dbus_timeout_get_enabled(timeout))
return FALSE; return FALSE;
if (gettimeofday(&tv, NULL) < 0) if (pa_gettimeofday(&tv) < 0)
return -1; return -1;
timeval_next(&tv, dbus_timeout_get_interval(timeout)); pa_timeval_add(&tv, dbus_timeout_get_interval(timeout) * 1000);
ev = c->mainloop->time_new(c->mainloop, &tv, handle_time_event, ev = c->mainloop->time_new(c->mainloop, &tv, handle_time_event,
(void*) timeout); (void*) timeout);
@ -216,14 +209,13 @@ static void toggle_timeout(DBusTimeout *timeout, void *data)
pa_core *c = (pa_core*) data; pa_core *c = (pa_core*) data;
pa_time_event *ev = (pa_time_event*) dbus_timeout_get_data(timeout); pa_time_event *ev = (pa_time_event*) dbus_timeout_get_data(timeout);
gettimeofday(&tv, NULL);
if (dbus_timeout_get_enabled(timeout)) { if (dbus_timeout_get_enabled(timeout)) {
timeval_next(&tv, dbus_timeout_get_interval(timeout)); pa_gettimeofday(&tv);
pa_timeval_add(&tv, dbus_timeout_get_interval(timeout) * 1000);
c->mainloop->time_restart(ev, &tv); c->mainloop->time_restart(ev, &tv);
} else { } else {
/* set it to expire one second ago */ /* disable the timeout */
tv.tv_sec -= 1; c->mainloop->time_restart(ev, NULL);
c->mainloop->time_restart(ev, &tv);
} }
} }
@ -251,7 +243,7 @@ wakeup_main(void *userdata)
static pa_dbus_connection* pa_dbus_connection_new(pa_core* c, DBusConnection *conn, const char* name) static pa_dbus_connection* pa_dbus_connection_new(pa_core* c, DBusConnection *conn, const char* name)
{ {
pa_dbus_connection *pconn = pa_xmalloc(sizeof(pa_dbus_connection)); pa_dbus_connection *pconn = pa_xnew(pa_dbus_connection, 1);
pconn->refcount = 1; pconn->refcount = 1;
pconn->core = c; pconn->core = c;
@ -305,19 +297,16 @@ pa_dbus_connection* pa_dbus_bus_get(pa_core *c, DBusBusType type,
const char* name; const char* name;
DBusConnection *conn; DBusConnection *conn;
pa_dbus_connection *pconn; pa_dbus_connection *pconn;
static const char sysname[] = "dbus-connection-system";
static const char sessname[] = "dbus-connection-session";
static const char startname[] = "dbus-connection-starter";
switch (type) { switch (type) {
case DBUS_BUS_SYSTEM: case DBUS_BUS_SYSTEM:
name = sysname; name = "dbus-connection-system";
break; break;
case DBUS_BUS_SESSION: case DBUS_BUS_SESSION:
name = sessname; name = "dbus-connection-session";
break; break;
case DBUS_BUS_STARTER: case DBUS_BUS_STARTER:
name = startname; name = "dbus-connection-starter";
break; break;
default: default:
assert(0); /* never reached */ assert(0); /* never reached */

View file

@ -34,11 +34,11 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulse/timeval.h>
#include <pulsecore/core-error.h> #include <pulsecore/core-error.h>
#include <pulsecore/module.h> #include <pulsecore/module.h>
#include <pulsecore/log.h> #include <pulsecore/log.h>
#include <pulsecore/core-subscribe.h>
#include <pulsecore/hashmap.h> #include <pulsecore/hashmap.h>
#include <pulsecore/idxset.h> #include <pulsecore/idxset.h>
#include <pulsecore/core-util.h> #include <pulsecore/core-util.h>
@ -68,18 +68,16 @@ typedef enum {
} alsa_type_t; } alsa_type_t;
struct device { struct device {
uint32_t index;
char *udi; char *udi;
pa_module *module;
}; };
struct userdata { struct userdata {
pa_core *core; pa_core *core;
pa_subscription *sub;
LibHalContext *ctx; LibHalContext *ctx;
capability_t capability; capability_t capability;
pa_dbus_connection *conn; pa_dbus_connection *conn;
pa_hashmap *by_udi; pa_hashmap *devices;
pa_hashmap *by_module;
}; };
struct timerdata { struct timerdata {
@ -159,12 +157,11 @@ static dbus_bool_t hal_device_add_alsa(struct userdata *u, const char *udi,
if (!(m = pa_module_load(u->core, module_name, args))) if (!(m = pa_module_load(u->core, module_name, args)))
return FALSE; return FALSE;
d = pa_xmalloc(sizeof(struct device)); d = pa_xnew(struct device, 1);
d->udi = pa_xstrdup(udi); d->udi = pa_xstrdup(udi);
d->module = m; d->index = m->index;
pa_hashmap_put(u->by_module, m, d); pa_hashmap_put(u->devices, udi, d);
pa_hashmap_put(u->by_udi, udi, d);
return TRUE; return TRUE;
} }
@ -291,13 +288,13 @@ static void device_added_cb(LibHalContext *ctx, const char *udi)
if (!has_cap) if (!has_cap)
return; return;
/* actually add the device one second later */ /* actually add the device 1/2 second later */
t = pa_xmalloc(sizeof(struct timerdata)); t = pa_xnew(struct timerdata, 1);
t->u = u; t->u = u;
t->udi = pa_xstrdup(udi); t->udi = pa_xstrdup(udi);
gettimeofday(&tv, NULL); pa_gettimeofday(&tv);
tv.tv_sec += 1; pa_timeval_add(&tv, 500000);
u->core->mainloop->time_new(u->core->mainloop, &tv, u->core->mainloop->time_new(u->core->mainloop, &tv,
device_added_time_cb, t); device_added_time_cb, t);
} }
@ -308,10 +305,8 @@ static void device_removed_cb(LibHalContext* ctx, const char *udi)
struct userdata *u = (struct userdata*) libhal_ctx_get_user_data(ctx); struct userdata *u = (struct userdata*) libhal_ctx_get_user_data(ctx);
pa_log_debug(__FILE__": Device removed: %s", udi); pa_log_debug(__FILE__": Device removed: %s", udi);
if ((d = pa_hashmap_remove(u->by_udi, udi))) { if ((d = pa_hashmap_remove(u->devices, udi))) {
d = pa_hashmap_remove(u->by_module, d->module); pa_module_unload_by_index(u->core, d->index);
pa_log_debug(__FILE__": Unloading: %s <%s>", d->module->name, d->module->argument);
pa_module_unload_request(d->module);
hal_device_free(d); hal_device_free(d);
} }
} }
@ -335,30 +330,6 @@ static void property_modified_cb(LibHalContext *ctx, const char *udi,
} }
#endif #endif
static void subscribe_notify_cb(pa_core *c, pa_subscription_event_type_t type,
uint32_t idx, void *userdata)
{
pa_module *m;
struct device *d;
struct userdata *u = (struct userdata*) userdata;
/* only listen for module remove events */
if (type != (PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE))
return;
if (!(m = pa_idxset_get_by_index(c->modules, idx)))
return;
/* we found the module, see if it's one we care about */
if ((d = pa_hashmap_remove(u->by_module, m))) {
pa_log_debug(__FILE__": Removing module #%u %s: %s",
m->index, m->name, d->udi);
d = pa_hashmap_remove(u->by_udi, d->udi);
hal_device_free(d);
}
}
static void pa_hal_context_free(LibHalContext* hal_ctx) static void pa_hal_context_free(LibHalContext* hal_ctx)
{ {
DBusError error; DBusError error;
@ -374,11 +345,8 @@ static void pa_hal_context_free(LibHalContext* hal_ctx)
static void userdata_free(struct userdata *u) { static void userdata_free(struct userdata *u) {
pa_hal_context_free(u->ctx); pa_hal_context_free(u->ctx);
pa_subscription_free(u->sub);
/* free the hashmap */
pa_hashmap_free(u->by_module, NULL, NULL);
/* free the devices with the hashmap */ /* free the devices with the hashmap */
pa_hashmap_free(u->by_udi, hal_device_free_cb, NULL); pa_hashmap_free(u->devices, hal_device_free_cb, NULL);
pa_dbus_connection_unref(u->conn); pa_dbus_connection_unref(u->conn);
pa_xfree(u); pa_xfree(u);
} }
@ -441,15 +409,12 @@ int pa__init(pa_core *c, pa_module*m) {
return -1; return -1;
} }
u = pa_xmalloc(sizeof(struct userdata)); u = pa_xnew(struct userdata, 1);
u->core = c; u->core = c;
u->ctx = hal_ctx; u->ctx = hal_ctx;
u->conn = conn; u->conn = conn;
u->by_module = pa_hashmap_new(NULL, NULL); u->devices = pa_hashmap_new(pa_idxset_string_hash_func,
u->by_udi = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
pa_idxset_string_compare_func);
u->sub = pa_subscription_new(c, PA_SUBSCRIPTION_MASK_MODULE,
subscribe_notify_cb, (void*) u);
m->userdata = (void*) u; m->userdata = (void*) u;
#if HAVE_ALSA #if HAVE_ALSA