summaryrefslogtreecommitdiffstats
path: root/paman.cc
blob: 6fb0d88ef244ad8ef22dd3aa9fdd86f7fb92f7ff (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
#include <signal.h>

#include <gtkmm.h>
#include <libglademm.h>

#include <polyp/polyplib.h>
#include <polyp/glib-mainloop.h>
#include <polyp/polyplib-error.h>

#include "paman.hh"
#include "SinkWindow.hh"
#include "MainWindow.hh"

MainWindow *mainWindow = NULL;
ServerInfoManager *serverInfoManager = NULL;
struct pa_context *context = NULL;
struct pa_mainloop_api *mainloop_api = NULL;

static void context_complete_callback(struct pa_context *c, int success, void *userdata) {
    g_assert(c && mainWindow && !serverInfoManager);

    if (!success) {
        mainWindow->showFailure(pa_strerror(pa_context_errno(c)));
        //pa_context_free(context);  /* Mrprmfmfl! */
        context = NULL;
        mainWindow->connectButton->set_sensitive(true);
        return;
    }

    mainWindow->showSuccess("Connected");
    mainWindow->connectButton->set_sensitive(false);
    serverInfoManager = new ServerInfoManager(*c);
}

static void die_callback(struct pa_context *c, void *userdata) {
    mainWindow->clearAllData();
    mainWindow->showFailure(pa_strerror(pa_context_errno(c)));
    delete serverInfoManager;
    serverInfoManager = NULL;
    //pa_context_free(contetx);  /* Mrprmfmfl! */
    context = NULL;
    mainWindow->connectButton->set_sensitive(true);
}

void create_connection() {
    if (serverInfoManager) {
        delete serverInfoManager;
        serverInfoManager = NULL;
    }
    if (context) {
        pa_context_free(context);
        context = NULL;
    }

    mainWindow->showSuccess("Connecting ...");
    context = pa_context_new(mainloop_api, "Polypaudio Manager");
    g_assert(context);
    pa_context_set_die_callback(context, die_callback, NULL);
    
    if (pa_context_connect(context, NULL, context_complete_callback, NULL) < 0) {
        context_complete_callback(context, 0, NULL);
        return;
    }

    mainWindow->connectButton->set_sensitive(false);
}

int main(int argc, char *argv[]) {
    struct pa_glib_mainloop *m;

    signal(SIGPIPE, SIG_IGN);
    
    Gtk::Main kit(argc, argv);

    mainWindow = MainWindow::create();
    g_assert(mainWindow);

    m = pa_glib_mainloop_new(g_main_context_default());
    g_assert(m);
    mainloop_api = pa_glib_mainloop_get_api(m);
    g_assert(mainloop_api);

    create_connection();

    Gtk::Main::run(*mainWindow);
    
quit:
    if (serverInfoManager)
        delete serverInfoManager;
    
    if (context)
        pa_context_free(context);

    mainloop_api = NULL;
    if (mainWindow)
        delete mainWindow;

    pa_glib_mainloop_free(m);

    return 0;
}