summaryrefslogtreecommitdiffstats
path: root/src/paman.cc
blob: bc54e9dde38c5246783dd860065b4e83e90989bc (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* $Id$ */

/***
  This file is part of paman.
 
  paman is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  paman is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public License
  along with paman; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#include <signal.h>

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

#include <polyp/polypaudio.h>
#include <polyp/glib-mainloop.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;

#define WINDOW_TITLE "Polypaudio Manager"

extern "C" static void context_state_callback(struct pa_context *c, void *) {
    g_assert(c && mainWindow);

    switch (pa_context_get_state(c)) {
        case PA_CONTEXT_CONNECTING: {
            char t[256];
            snprintf(t, sizeof(t), "Connection to <b>%s</b> ...", pa_context_get_server(c));
            mainWindow->showSuccess(t);
            mainWindow->connectButton->set_sensitive(false);
            return;
        }
            
        case PA_CONTEXT_AUTHORIZING:
            mainWindow->showSuccess("Authorizing ...");
            return;
            
        case PA_CONTEXT_SETTING_NAME:
            mainWindow->showSuccess("Setting name ...");
            return;
            
        case PA_CONTEXT_READY: {
            char t[256];
            snprintf(t, sizeof(t), WINDOW_TITLE" [%s]", pa_context_get_server(c));
            mainWindow->set_title(t);
            mainWindow->showSuccess("Ready");
            g_assert(!serverInfoManager);
            serverInfoManager = new ServerInfoManager(*c);
            mainWindow->statButton->set_sensitive(true);
            mainWindow->disconnectButton->set_sensitive(true);
            return;
        }
            
        case PA_CONTEXT_TERMINATED:
            mainWindow->set_title(WINDOW_TITLE);
            mainWindow->showSuccess("Disconnected");
            break;

        case PA_CONTEXT_FAILED:
        default:
            mainWindow->set_title(WINDOW_TITLE);
            mainWindow->showFailure(pa_strerror(pa_context_errno(c)));
            break;
            
    }
            
    killConnection();
}

void createConnection() {
    if (serverInfoManager) {
        delete serverInfoManager;
        serverInfoManager = NULL;
    }
    
    if (context) {
        pa_context_unref(context);
        context = NULL;
    }

    context = pa_context_new(mainloop_api, "Polypaudio Manager");
    g_assert(context);
    pa_context_set_state_callback(context, context_state_callback, NULL);
    pa_context_connect(context, NULL, (pa_context_flags_t) 0, NULL);
}

void killConnection() {
    if (serverInfoManager) {
        delete serverInfoManager;
        serverInfoManager = NULL;
    }
    
    if (context) {
        pa_context_unref(context);
        context = NULL;
    }

    mainWindow->connectButton->set_sensitive(true);
    mainWindow->disconnectButton->set_sensitive(false);
    mainWindow->statButton->set_sensitive(false);
    mainWindow->clearAllData();
    mainWindow->set_title(WINDOW_TITLE);
}

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);

    createConnection();

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

    mainloop_api = NULL;
    if (mainWindow)
        delete mainWindow;

    pa_glib_mainloop_free(m);

    return 0;
}