summaryrefslogtreecommitdiff
path: root/src/audtool/wrappers.c
blob: 35afa35596d15d5620865f1fa261f9be565087ee (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * wrappers.c
 * Copyright 2013 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <stdlib.h>

#include "audtool.h"
#include "wrappers.h"

void generic_on_off (int argc, char * * argv, OnOffFunc func)
{
    gboolean show;

    if (argc == 1)
        show = TRUE;
    else if (argc == 2 && ! g_ascii_strcasecmp (argv[1], "on"))
        show = TRUE;
    else if (argc == 2 && ! g_ascii_strcasecmp (argv[1], "off"))
        show = FALSE;
    else
    {
        audtool_whine_args (argv[0], "<on/off>");
        exit (1);
    }

    func (dbus_proxy, show, NULL, NULL);
}

int get_playlist_length (void)
{
    int length = -1;
    obj_audacious_call_length_sync (dbus_proxy, & length, NULL, NULL);

    if (length < 0)
        exit (1);

    return length;
}

int get_queue_length (void)
{
    int length = -1;
    obj_audacious_call_get_playqueue_length_sync (dbus_proxy, & length, NULL, NULL);

    if (length < 0)
        exit (1);

    return length;
}

int get_queue_entry (int qpos)
{
    unsigned entry = -1;
    obj_audacious_call_queue_get_list_pos_sync (dbus_proxy, qpos, & entry, NULL, NULL);

    if (entry == (unsigned) -1)
        exit (1);

    return entry;
}

int find_in_queue (int entry)
{
    unsigned qpos = -1;
    obj_audacious_call_queue_get_queue_pos_sync (dbus_proxy, entry, & qpos, NULL, NULL);

    if (qpos == (unsigned) -1)
        exit (1);

    return qpos;
}

int get_current_entry (void)
{
    unsigned entry = -1;
    obj_audacious_call_position_sync (dbus_proxy, & entry, NULL, NULL);

    if (entry == (unsigned) -1)
        exit (1);

    return entry;
}

char * get_entry_filename (int entry)
{
    char * uri = NULL;
    obj_audacious_call_song_filename_sync (dbus_proxy, entry, & uri, NULL, NULL);

    if (! uri)
        exit (1);

    char * filename = g_filename_from_uri (uri, NULL, NULL);

    if (filename)
    {
        g_free (uri);
        return filename;
    }

    return uri;
}

char * get_entry_title (int entry)
{
    char * title = NULL;
    obj_audacious_call_song_title_sync (dbus_proxy, entry, & title, NULL, NULL);

    if (! title)
        exit (1);

    return title;
}

int get_entry_length (int entry)
{
    int length = -1;
    obj_audacious_call_song_frames_sync (dbus_proxy, entry, & length, NULL, NULL);

    if (length < 0)
        exit (1);

    return length;
}

char * get_entry_field (int entry, const char * field)
{
    GVariant * var = NULL;
    obj_audacious_call_song_tuple_sync (dbus_proxy, entry, field, & var, NULL, NULL);

    if (! var || ! g_variant_is_of_type (var, G_VARIANT_TYPE_VARIANT))
        exit (1);

    GVariant * var2 = g_variant_get_variant (var);

    if (! var2)
        exit (1);

    char * str;

    if (g_variant_is_of_type (var2, G_VARIANT_TYPE_STRING))
        str = g_strdup (g_variant_get_string (var2, NULL));
    else if (g_variant_is_of_type (var2, G_VARIANT_TYPE_INT32))
        str = g_strdup_printf ("%d", (int) g_variant_get_int32 (var2));
    else
        exit (1);

    g_variant_unref (var);
    g_variant_unref (var2);

    return str;
}

int get_current_time (void)
{
    unsigned time = -1;
    obj_audacious_call_time_sync (dbus_proxy, & time, NULL, NULL);

    if (time == (unsigned) -1)
        exit (1);

    return time;
}

void get_current_info (int * bitrate_p, int * samplerate_p, int * channels_p)
{
    int bitrate = -1, samplerate = -1, channels = -1;
    obj_audacious_call_get_info_sync (dbus_proxy, & bitrate, & samplerate, & channels, NULL, NULL);

    if (bitrate < 0 || samplerate < 0 || channels < 0)
        exit (1);

    if (bitrate_p)
        * bitrate_p = bitrate;
    if (samplerate_p)
        * samplerate_p = samplerate;
    if (channels_p)
        * channels_p = channels;
}