aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 188f31d6b71e282eba353684537b305d7d6d82f4 (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
/*
 * Copyright 2015 Canonical Ltd.
 * Copyright 2023-2024 Robert Tari <robert@tari.in>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program 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 General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib/gi18n.h>
#include <glib-unix.h>
#include <locale.h>
#include <libnotify/notify.h>

#include "indicator-sound-service.h"
#include "config.h"

static IndicatorSoundService * service = NULL;
static pa_glib_mainloop * pgloop = NULL;

static gboolean
sigterm_handler (gpointer data)
{
    g_debug("Got SIGTERM");
    g_main_loop_quit((GMainLoop *)data);
    return G_SOURCE_REMOVE;
}

static void
on_name_lost(GDBusConnection * connection,
             const gchar * name,
             gpointer user_data)
{
    g_warning("Name lost or unable to acquire bus: %s", name);
    g_main_loop_quit((GMainLoop *)user_data);
}

static void
on_bus_acquired(GDBusConnection *connection,
                const gchar *name,
                gpointer user_data)
{
    MediaPlayerList * playerlist = NULL;
    IndicatorSoundOptions * options = NULL;
    VolumeControlPulse * volume = NULL;
    AccountsServiceUser * accounts = NULL;
    VolumeWarning * warning = NULL;
    AccountsServiceAccess * accounts_service_access = NULL;


    if (g_strcmp0("lightdm", g_get_user_name()) == 0) {
        playerlist = MEDIA_PLAYER_LIST(media_player_list_greeter_new());
    } else {
        playerlist = MEDIA_PLAYER_LIST(media_player_list_mpris_new());
        accounts = accounts_service_user_new();
    }

    pgloop = pa_glib_mainloop_new(NULL);
    options = INDICATOR_SOUND_OPTIONS (indicator_sound_options_gsettings_new ());
    accounts_service_access = accounts_service_access_new();
    volume = volume_control_pulse_new(options, pgloop, accounts_service_access);
    warning = VOLUME_WARNING (volume_warning_pulse_new (options, pgloop));

    service = indicator_sound_service_new (playerlist, VOLUME_CONTROL (volume), accounts, options, warning, accounts_service_access);

    g_clear_object(&playerlist);
    g_clear_object(&options);
    g_clear_object(&volume);
    g_clear_object(&accounts);
    g_clear_object(&warning);
}

int
main (int argc, char ** argv)
{
    GMainLoop * loop = NULL;
    bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    setlocale (LC_ALL, "");
    bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);

    /* Build Mainloop */
    loop = g_main_loop_new(NULL, FALSE);

    g_unix_signal_add(SIGTERM, sigterm_handler, loop);

    /* Initialize libnotify */
    notify_init ("ayatana-indicator-sound");

    g_bus_own_name(G_BUS_TYPE_SESSION,
        "org.ayatana.indicator.sound",
        G_BUS_NAME_OWNER_FLAGS_NONE,
        on_bus_acquired,
        NULL, /* name acquired */
        on_name_lost,
        loop,
        NULL);

    g_main_loop_run(loop);

    g_clear_object(&service);
    g_clear_pointer(&pgloop, pa_glib_mainloop_free);

    notify_uninit();

    return 0;
}