Ejemplo n.º 1
0
void Context::connectToDaemon()
{
    Q_ASSERT(m_context == nullptr);

    // We require a glib event loop
    if (!QByteArray(QAbstractEventDispatcher::instance()->metaObject()->className()).contains("EventDispatcherGlib")) {
        qCWarning(PLASMAPA) << "Disabling PulseAudio integration for lack of GLib event loop";
        return;
    }

    qCDebug(PLASMAPA) <<  "Attempting connection to PulseAudio sound daemon";
    if (!m_mainloop) {
        m_mainloop = pa_glib_mainloop_new(nullptr);
        Q_ASSERT(m_mainloop);
    }

    pa_mainloop_api *api = pa_glib_mainloop_get_api(m_mainloop);
    Q_ASSERT(api);
    m_context = pa_context_new(api, "QPulse");
    Q_ASSERT(m_context);

    if (pa_context_connect(m_context, NULL, PA_CONTEXT_NOFAIL, nullptr) < 0) {
        pa_context_unref(m_context);
        pa_glib_mainloop_free(m_mainloop);
        m_context = nullptr;
        m_mainloop = nullptr;
        return;
    }
    pa_context_set_state_callback(m_context, &context_state_callback, this);
}
Ejemplo n.º 2
0
/**
 * This function does the context initialization.
 */
static gboolean
xvd_connect_to_pulse (XvdInstance *i)
{
  pa_context_flags_t flags = PA_CONTEXT_NOFAIL;

  if (i->pulse_context)
    {
      pa_context_unref (i->pulse_context);
      i->pulse_context = NULL;
    }

  i->pulse_context = pa_context_new (pa_glib_mainloop_get_api (i->pa_main_loop),
                                     XVD_APPNAME);
  g_assert(i->pulse_context);
  pa_context_set_state_callback (i->pulse_context,
                                 xvd_context_state_callback,
                                 i);

  if (pa_context_connect (i->pulse_context,
                          NULL,
                          flags,
                          NULL) < 0)
    {
      g_warning ("xvd_connect_to_pulse: failed to connect context: %s",
                 pa_strerror (pa_context_errno (i->pulse_context)));
      return FALSE;
    }
  return TRUE;
}
Ejemplo n.º 3
0
static void cancel_timer(struct ausrv *ausrv)
{
    pa_mainloop_api *api;
    
    if (ausrv->timer != NULL) {
        api = pa_glib_mainloop_get_api(ausrv->mainloop);
        api->time_free(ausrv->timer);
        ausrv->timer = NULL;
    }
}
Ejemplo n.º 4
0
int main(int argc, char *argv[]) {
    pa_mainloop_api *a;
    pa_io_event *ioe;
    pa_time_event *te;
    struct timeval tv;

#ifdef GLIB_MAIN_LOOP
    pa_glib_mainloop *g;

    glib_main_loop = g_main_loop_new(NULL, FALSE);
    assert(glib_main_loop);

    g = pa_glib_mainloop_new(NULL);
    assert(g);

    a = pa_glib_mainloop_get_api(g);
    assert(a);
#else /* GLIB_MAIN_LOOP */
    pa_mainloop *m;

    m = pa_mainloop_new();
    assert(m);

    a = pa_mainloop_get_api(m);
    assert(a);
#endif /* GLIB_MAIN_LOOP */

    ioe = a->io_new(a, 0, PA_IO_EVENT_INPUT, iocb, NULL);
    assert(ioe);

    de = a->defer_new(a, dcb, NULL);
    assert(de);

    te = a->time_new(a, pa_timeval_rtstore(&tv, pa_rtclock_now() + 2 * PA_USEC_PER_SEC, TRUE), tcb, NULL);

#if defined(GLIB_MAIN_LOOP)
    g_main_loop_run(glib_main_loop);
#else
    pa_mainloop_run(m, NULL);
#endif

    a->time_free(te);
    a->defer_free(de);
    a->io_free(ioe);

#ifdef GLIB_MAIN_LOOP
    pa_glib_mainloop_free(g);
    g_main_loop_unref(glib_main_loop);
#else
    pa_mainloop_free(m);
#endif

    return 0;
}
Ejemplo n.º 5
0
static void connect(void) {
    int r;

    ctxt = pa_context_new(pa_glib_mainloop_get_api(m), NULL);
    g_assert(ctxt);

    r = pa_context_connect(ctxt, NULL, PA_CONTEXT_NOAUTOSPAWN|PA_CONTEXT_NOFAIL, NULL);
    g_assert(r == 0);

    pa_context_set_state_callback(ctxt, context_state_callback, NULL);
}
Ejemplo n.º 6
0
static void restart_timer(struct ausrv *ausrv, int secs)
{
    pa_mainloop_api *api = pa_glib_mainloop_get_api(ausrv->mainloop);
    struct timeval   tv;

    gettimeofday(&tv, NULL);
    tv.tv_sec += secs;
    
    if (ausrv->timer != NULL)
        api->time_restart(ausrv->timer, &tv);
    else
        ausrv->timer = api->time_new(api, &tv, retry_connect, (void *)ausrv);
}
Ejemplo n.º 7
0
gboolean PulseSrc::async_get_pulse_devices(void* user_data) {
  PulseSrc* context = static_cast<PulseSrc*>(user_data);
  context->pa_glib_mainloop_ = pa_glib_mainloop_new(context->mainloop_->get_main_context());
  context->pa_mainloop_api_ = pa_glib_mainloop_get_api(context->pa_glib_mainloop_);
  context->pa_context_ = pa_context_new(context->pa_mainloop_api_, nullptr);
  if (nullptr == context->pa_context_) {
    g_debug("PulseSrc:: pa_context_new() failed.");
    return FALSE;
  }
  pa_context_set_state_callback(context->pa_context_, pa_context_state_callback, context);
  if (pa_context_connect(context->pa_context_, context->server_, (pa_context_flags_t)0, nullptr) <
      0) {
    g_debug("pa_context_connect() failed: %s", pa_strerror(pa_context_errno(context->pa_context_)));
    return FALSE;
  }
  context->connected_to_pulse_ = true;
  return FALSE;
}
Ejemplo n.º 8
0
struct ausrv *ausrv_create(struct tonegend *tonegend, char *server)
{
    pa_glib_mainloop   *mainloop = NULL;
    struct ausrv       *ausrv;
    pa_mainloop_api    *mainloop_api;

    if ((ausrv = malloc(sizeof(*ausrv))) == NULL) {
        LOG_ERROR("%s(): Can't allocate memory", __FUNCTION__);
        goto failed;
    }

    if ((mainloop = pa_glib_mainloop_new(NULL)) == NULL) {
        LOG_ERROR("%s(): pa_glib_mainloop_new() failed", __FUNCTION__);
        goto failed;
    }

    mainloop_api = pa_glib_mainloop_get_api(mainloop);

    if (pa_signal_init(mainloop_api) < 0) {
        LOG_ERROR("%s(): pa_signal_init() failed", __FUNCTION__);
        goto failed;
    }
    
    memset(ausrv, 0, sizeof(*ausrv));
    ausrv->tonegend = tonegend;
    ausrv->server   = strdup(server ? server : DEFAULT_SERVER);
    ausrv->mainloop = mainloop;

    connect_server(ausrv);

    return ausrv;

 failed:
    if (mainloop != NULL)
        pa_glib_mainloop_free(mainloop);

    if (ausrv != NULL)
        free(ausrv);

    return NULL;

}
Ejemplo n.º 9
0
EventdSoundPulseaudioContext *
eventd_sound_pulseaudio_init()
{
    EventdSoundPulseaudioContext *context;

    context = g_new0(EventdSoundPulseaudioContext, 1);

    context->pa_loop = pa_glib_mainloop_new(NULL);

    context->context = pa_context_new(pa_glib_mainloop_get_api(context->pa_loop), PACKAGE_NAME " sndfile plugin");
    if ( context->context == NULL )
    {
        g_warning("Couldn't open sound system");
        pa_glib_mainloop_free(context->pa_loop);
        g_free(context);
        return NULL;
    }

    pa_context_set_state_callback(context->context, _eventd_sound_pulseaudio_context_state_callback, NULL);

    return context;
}
Ejemplo n.º 10
0
double get_db()
{
    pa_glib_mainloop *pa_ml = NULL;
    pa_mainloop_api *pa_mlapi = NULL;
    //pa_operation *pa_op = NULL;
    pa_context *pa_ctx = NULL;

    GMainContext *mctx = NULL;

    pa_ml = pa_glib_mainloop_new(g_main_context_default());
    pa_mlapi = pa_glib_mainloop_get_api(pa_ml);
    pa_ctx = pa_context_new(pa_mlapi, "deepin");

    pa_context_connect(pa_ctx, NULL, 0, NULL);
    pa_context_set_state_callback(pa_ctx, pa_state_cb, NULL);

    mctx = g_main_context_default();
    mainloop = g_main_loop_new(mctx, FALSE);
    g_main_loop_run(mainloop);
    
    return ret;
}
Ejemplo n.º 11
0
SwfdecPlayback *
swfdec_playback_open (SwfdecPlayer *player, GMainContext *context)
{
  SwfdecPlayback *sound;
  const GList *walk;
  pa_mainloop_api *pa_api;

  g_return_val_if_fail (SWFDEC_IS_PLAYER (player), NULL);
  g_return_val_if_fail (context != NULL, NULL);

  sound = g_new0 (SwfdecPlayback, 1);
  sound->player = player;
  g_signal_connect (player, "advance", G_CALLBACK (advance_before), sound);
  g_signal_connect (player, "audio-added", G_CALLBACK (audio_added), sound);
  g_signal_connect (player, "audio-removed", G_CALLBACK (audio_removed), sound);

  /* Create our mainloop attachment to glib.  XXX: I hope this means we don't
   * have to run the main loop using pa functions.
   */
  sound->pa_mainloop = pa_glib_mainloop_new (context);
  pa_api = pa_glib_mainloop_get_api (sound->pa_mainloop);

  sound->pa = pa_context_new (pa_api, "swfdec");

  pa_context_set_state_callback (sound->pa, context_state_callback, sound);
  pa_context_connect (sound->pa,
		      NULL, /* default server */
		      0, /* default flags */
		      NULL /* spawning api */
		      );

  for (walk = swfdec_player_get_audio (player); walk; walk = walk->next) {
    swfdec_playback_stream_open (sound, walk->data);
  }
  g_main_context_ref (context);
  sound->context = context;
  return sound;
}
Ejemplo n.º 12
0
static gboolean enumerate_audio_source_devices(GClosure *callback)
{
    AudioListContext *context;

    context = g_slice_new0(AudioListContext);

    context->callback = callback;

    context->mainloop = pa_glib_mainloop_new(_owr_get_main_context());

    if (!context->mainloop) {
        g_warning("PulseAudio: failed to create glib mainloop");
        goto cleanup;
    }

    context->pa_context = pa_context_new(pa_glib_mainloop_get_api(context->mainloop), "Owr");

    if (!context->pa_context) {
        g_warning("PulseAudio: failed to create context");
        goto cleanup_mainloop;
    }

    pa_context_set_state_callback(context->pa_context,
        (pa_context_notify_cb_t) on_pulse_context_state_change, context);
    pa_context_connect(context->pa_context, NULL, 0, NULL);

done:
    return FALSE;

cleanup_mainloop:
    pa_glib_mainloop_free(context->mainloop);

cleanup:
    finish_pa_list(context);

    goto done;
}
Ejemplo n.º 13
0
void pulse_initialize_audio_step_1()
{
    pa_mainloop_api *vtable = NULL;
    pa_proplist *main_proplist = NULL;

    pulse.loop = xpa_glib_mainloop_new (g_main_context_default());
#ifdef USE_GLIB_MAINLOOP
    vtable = pa_glib_mainloop_get_api (pulse.loop);
#else
    vtable = xpa_mainloop_get_api(pulse.loop);
#endif

    /* PROPLIST: Only the PA_PROP_MEDIA_ROLE is important.  */
    main_proplist = xpa_proplist_new();
    xpa_proplist_sets(main_proplist, PA_PROP_MEDIA_ROLE,
                      BURRO_PROP_MEDIA_ROLE);
    xpa_proplist_sets(main_proplist, PA_PROP_APPLICATION_ID,
                      BURRO_PROP_APPLICATION_ID);
    xpa_proplist_sets(main_proplist, PA_PROP_APPLICATION_NAME,
                      BURRO_PROP_APPLICATION_NAME);

    /* A context is the basic object for a connection to a PulseAudio
       server. It multiplexes commands, data streams and events
       through a single channel.  */
    pulse.context = xpa_context_new_with_proplist(vtable,
                                                  BURRO_PROP_APPLICATION_NAME,
                                                  main_proplist);
    xpa_proplist_free (main_proplist);
    xpa_context_set_state_callback(pulse.context,
                                   cb_audio_context_state, NULL);

    pulse.state = PA_CONTEXT_UNCONNECTED;

    /* Connect the context */
    xpa_context_connect_to_default_server(pulse.context);

}
Ejemplo n.º 14
0
static void connect_server(struct ausrv *ausrv)
{
    pa_mainloop_api *api    = pa_glib_mainloop_get_api(ausrv->mainloop);
    char            *server = ausrv->server;

    cancel_timer(ausrv);


    if (server != NULL && !strcmp(ausrv->server, DEFAULT_SERVER))
        server = NULL;
    
    
    /*
     * Note: It is not possible to reconnect a context if it ever gets
     *     disconnected. If we have a context here, get rid of it and
     *     allocate a new one.
     */
    if (ausrv->context != NULL) {
        pa_context_set_state_callback(ausrv->context, NULL, NULL);
        pa_context_set_subscribe_callback(ausrv->context, NULL, NULL);
        pa_context_unref(ausrv->context);
        ausrv->context = NULL;
    }
    
    if ((ausrv->context = pa_context_new(api, pa_client_name)) == NULL) {
        LOG_ERROR("%s(): pa_context_new() failed, exiting", __FUNCTION__);
        exit(1);
    }
    
    pa_context_set_state_callback(ausrv->context, context_callback, ausrv);
    pa_context_set_subscribe_callback(ausrv->context, event_callback, ausrv);


    LOG_INFO("Trying to connect to %s...", server ? server : DEFAULT_SERVER);
    pa_context_connect(ausrv->context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
}
Ejemplo n.º 15
0
int main(int argc, char **argv)
{
	pa_glib_mainloop *mainloop;
	pa_mainloop_api *mainloop_api;
	pa_context *context;
	GdkWindow *root;
	int status = EXIT_SUCCESS;
	int i;

	gtk_init(&argc, &argv);

	mainloop = pa_glib_mainloop_new(NULL);
	if (!mainloop) {
		fprintf(stderr, "pa_glib_mainloop_new() failed\n");
		status = EXIT_FAILURE;
		goto out;
	}

	mainloop_api = pa_glib_mainloop_get_api(mainloop);

	if (pa_signal_init(mainloop_api) < 0) {
		fprintf(stderr, "pa_signal_init() failed\n");
		status = EXIT_FAILURE;
		goto mainloop_free;
	}
	pa_signal_new(SIGINT, exit_signal_callback, NULL);
	pa_signal_new(SIGTERM, exit_signal_callback, NULL);

	context = pa_context_new(mainloop_api, NULL);
	if (!context) {
		fprintf(stderr, "pa_context_new() failed\n");
		status = EXIT_FAILURE;
		goto mainloop_free;
	}

	pa_context_set_state_callback(context, context_state_callback, NULL);
	if (pa_context_connect(context, NULL, 0, NULL) < 0) {
		fprintf(stderr, "pa_context_connect() failed: %s\n",
			pa_strerror(pa_context_errno(context)));
		status = EXIT_FAILURE;
		goto context_unref;
	}

	if (!notify_init(APP_NAME)) {
		fprintf(stderr, "Could not initialize libnotify\n");
		status = EXIT_FAILURE;
		goto context_unref;
	}

	notification = notify_notification_new(APP_NAME, NULL, NULL);
	if (!notification) {
		fprintf(stderr, "notify_notification_new() failed\n");
		status = EXIT_FAILURE;
		goto notify_uninit;
	}

	notify_notification_set_timeout(notification, NOTIFY_EXPIRES_DEFAULT);
	notify_notification_set_hint_string(notification, "synchronous", "volume");

	root = gdk_get_default_root_window();

	gdk_window_set_events(root, GDK_KEY_PRESS_MASK);
	gdk_window_add_filter(root, filter, context);

	for (i = 0; i < sizeof(keysyms) / sizeof(keysyms[0]); i++) {
		keycodes[i] = XKeysymToKeycode(GDK_WINDOW_XDISPLAY(root), keysyms[i]);
		if (!keycodes[i]) {
			fprintf(stderr, "%s is not mapped on this keyboard\n",
				XKeysymToString(keysyms[i]));
			continue;
		}
		XGrabKey(GDK_WINDOW_XDISPLAY(root), keycodes[i], AnyModifier,
			 GDK_WINDOW_XID(root), False, GrabModeAsync,
			 GrabModeAsync);
	}

	gtk_main();

	g_object_unref(G_OBJECT(notification));
notify_uninit:
	notify_uninit();
context_unref:
	pa_context_unref(context);
mainloop_free:
	pa_glib_mainloop_free(mainloop);
out:
	return status;
}
Ejemplo n.º 16
0
struct audio_service* audio_service_create()
{
	struct audio_service *service;
	LSError error;
	pa_mainloop_api *mainloop_api;
	char name[100];

	service = g_try_new0(struct audio_service, 1);
	if (!service)
		return NULL;

	LSErrorInit(&error);

	if (!LSRegisterPubPriv("org.webosports.audio", &service->handle, false, &error)) {
		g_warning("Failed to register the luna service: %s", error.message);
		LSErrorFree(&error);
		goto error;
	}

	if (!LSRegisterCategory(service->handle, "/", audio_service_methods,
			NULL, NULL, &error)) {
		g_warning("Could not register service category: %s", error.message);
		LSErrorFree(&error);
		goto error;
	}

	if (!LSCategorySetData(service->handle, "/", service, &error)) {
		g_warning("Could not set daa for service category: %s", error.message);
		LSErrorFree(&error);
		goto error;
	}

	if (!LSGmainAttach(service->handle, event_loop, &error)) {
		g_warning("Could not attach service handle to mainloop: %s", error.message);
		LSErrorFree(&error);
		goto error;
	}

	service->pa_mainloop = pa_glib_mainloop_new(g_main_context_default());
	mainloop_api = pa_glib_mainloop_get_api(service->pa_mainloop);

	snprintf(name, 100, "AudioServiceContext:%i", getpid());
	service->context = pa_context_new(mainloop_api, name);
	service->context_initialized = false;
	pa_context_set_state_callback(service->context, context_state_cb, service);

	if (pa_context_connect(service->context, NULL, 0, NULL) < 0) {
		g_warning("Failed to connect to PulseAudio");
		pa_context_unref(service->context);
		pa_glib_mainloop_free(service->pa_mainloop);
		goto error;
	}

	sample_list = g_slist_alloc();

	return service;

error:
	if (service->handle != NULL) {
		LSUnregister(service->handle, &error);
		LSErrorFree(&error);
	}

	g_free(service);

	return NULL;
}