summaryrefslogtreecommitdiff
path: root/src/audtool/main.c
blob: 2ed0fa9da57864d7534037c321ed66a711860f49 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * main.c
 * Copyright 2005-2013 George Averill, William Pitcock, Yoshiki Yazawa, and
 *                     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 <stdio.h>
#include <stdlib.h>
#include <locale.h>

#include "audtool.h"

const struct commandhandler handlers[] =
{
    {"<sep>", NULL, "Current song information", 0},
    {"current-song", get_current_song, "print formatted song title", 0},
    {"current-song-filename", get_current_song_filename, "print song file name", 0},
    {"current-song-length", get_current_song_length, "print song length", 0},
    {"current-song-length-seconds", get_current_song_length_seconds, "print song length in seconds", 0},
    {"current-song-length-frames", get_current_song_length_frames, "print song length in milliseconds", 0},
    {"current-song-output-length", get_current_song_output_length, "print playback time", 0},
    {"current-song-output-length-seconds", get_current_song_output_length_seconds, "print playback time in seconds", 0},
    {"current-song-output-length-frames", get_current_song_output_length_frames, "print playback time in milliseconds", 0},
    {"current-song-bitrate", get_current_song_bitrate, "print bitrate in bits per second", 0},
    {"current-song-bitrate-kbps", get_current_song_bitrate_kbps, "print bitrate in kilobits per second", 0},
    {"current-song-frequency", get_current_song_frequency, "print sampling rate in hertz", 0},
    {"current-song-frequency-khz", get_current_song_frequency_khz, "print sampling rate in kilohertz", 0},
    {"current-song-channels", get_current_song_channels, "print number of channels", 0},
    {"current-song-tuple-data", get_current_song_tuple_field_data, "print value of named field", 1},
    {"current-song-info", get_current_song_info, "print bitrate, sampling rate, and channels", 0},

    {"<sep>", NULL, "Playback commands", 0},
    {"playback-play", playback_play, "start/restart/unpause playback", 0},
    {"playback-pause", playback_pause, "pause/unpause playback", 0},
    {"playback-playpause", playback_playpause, "start/pause/unpause playback", 0},
    {"playback-stop", playback_stop, "stop playback", 0},
    {"playback-playing", playback_playing, "exit code = 0 if playing", 0},
    {"playback-paused", playback_paused, "exit code = 0 if paused", 0},
    {"playback-stopped", playback_stopped, "exit code = 0 if not playing", 0},
    {"playback-status", playback_status, "print status (playing/paused/stopped)", 0},
    {"playback-seek", playback_seek, "seek to given time", 1},
    {"playback-seek-relative", playback_seek_relative, "seek to relative time offset", 1},

    {"<sep>", NULL, "Playlist commands", 0},
    {"playlist-advance", playlist_advance, "skip to next song", 0},
    {"playlist-reverse", playlist_reverse, "skip to previous song", 0},
    {"playlist-addurl", playlist_add_url_string, "add URI at end of playlist", 1},
    {"playlist-insurl", playlist_ins_url_string, "insert URI at given position", 2},
    {"playlist-addurl-to-new-playlist", playlist_enqueue_to_temp, "open URI in \"Now Playing\" playlist", 1},
    {"playlist-delete", playlist_delete, "remove song at given position", 1},
    {"playlist-length", playlist_length, "print number of songs in playlist", 0},
    {"playlist-song", playlist_song, "print formatted title of given song", 1},
    {"playlist-song-filename", playlist_song_filename, "print file name of given song", 1},
    {"playlist-song-length", playlist_song_length, "print length of given song", 1},
    {"playlist-song-length-seconds", playlist_song_length_seconds, "print length of given song in seconds", 1},
    {"playlist-song-length-frames", playlist_song_length_frames, "print length of given song in milliseconds", 1},
    {"playlist-tuple-data", playlist_tuple_field_data, "print value of named field for given song", 2},
    {"playlist-display", playlist_display, "print all songs in playlist", 0},
    {"playlist-position", playlist_position, "print position of current song", 0},
    {"playlist-jump", playlist_jump, "skip to given song", 1},
    {"playlist-clear", playlist_clear, "clear playlist", 0},
    {"playlist-auto-advance-status", playlist_auto_advance_status, "query playlist auto-advance", 0},
    {"playlist-auto-advance-toggle", playlist_auto_advance_toggle, "toggle playlist auto-advance", 0},
    {"playlist-repeat-status", playlist_repeat_status, "query playlist repeat", 0},
    {"playlist-repeat-toggle", playlist_repeat_toggle, "toggle playlist repeat", 0},
    {"playlist-shuffle-status", playlist_shuffle_status, "query playlist shuffle", 0},
    {"playlist-shuffle-toggle", playlist_shuffle_toggle, "toggle playlist shuffle", 0},
    {"playlist-stop-after-status", playlist_stop_after_status, "query if stopping after current song", 0},
    {"playlist-stop-after-toggle", playlist_stop_after_toggle, "toggle if stopping after current song", 0},

    {"<sep>", NULL, "More playlist commands", 0},
    {"number-of-playlists", number_of_playlists, "print number of playlists", 0},
    {"current-playlist", current_playlist, "print number of current playlist", 0},
    {"play-current-playlist", play_current_playlist, "play/resume current playlist", 0},
    {"set-current-playlist", set_current_playlist, "make given playlist current", 1},
    {"current-playlist-name", playlist_title, "print current playlist title", 0},
    {"set-current-playlist-name", set_playlist_title, "set current playlist title", 1},
    {"new-playlist", new_playlist, "insert new playlist", 0},
    {"delete-current-playlist", delete_current_playlist, "remove current playlist", 0},

    {"<sep>", NULL, "Playlist queue commands", 0},
    {"playqueue-add", playqueue_add, "add given song to queue", 1},
    {"playqueue-remove", playqueue_remove, "remove given song from queue", 1},
    {"playqueue-is-queued", playqueue_is_queued, "exit code = 0 if given song is queued", 1},
    {"playqueue-get-queue-position", playqueue_get_queue_position, "print queue position of given song", 1},
    {"playqueue-get-list-position", playqueue_get_list_position, "print n-th queued song", 1},
    {"playqueue-length", playqueue_length, "print number of songs in queue", 0},
    {"playqueue-display", playqueue_display, "print all songs in queue", 0},
    {"playqueue-clear", playqueue_clear, "clear queue", 0},

    {"<sep>", NULL, "Volume control and equalizer", 0},
    {"get-volume", get_volume, "print current volume level in percent", 0},
    {"set-volume", set_volume, "set current volume level in percent", 1},
    {"equalizer-activate", equalizer_active, "activate/deactivate equalizer", 1},
    {"equalizer-get", equalizer_get_eq, "print equalizer settings", 0},
    {"equalizer-set", equalizer_set_eq, "set equalizer settings", 11},
    {"equalizer-get-preamp", equalizer_get_eq_preamp, "print equalizer pre-amplification", 0},
    {"equalizer-set-preamp", equalizer_set_eq_preamp, "set equalizer pre-amplification", 1},
    {"equalizer-get-band", equalizer_get_eq_band, "print gain of given equalizer band", 1},
    {"equalizer-set-band", equalizer_set_eq_band, "set gain of given equalizer band", 2},

    {"<sep>", NULL, "Miscellaneous", 0},
    {"mainwin-show", mainwin_show, "show/hide Audacious", 1},
    {"filebrowser-show", show_filebrowser, "show/hide Add Files window", 1},
    {"jumptofile-show", show_jtf_window, "show/hide Jump to Song window", 1},
    {"preferences-show", show_preferences_window, "show/hide Settings window", 1},
    {"about-show", show_about_window, "show/hide About window", 1},

    {"version", get_version, "print Audacious version", 0},
    {"shutdown", shutdown_audacious_server, "shut down Audacious", 0},

    {"help", get_handlers_list, "print this help", 0},

    {NULL, NULL, NULL, 0}
};

ObjAudacious * dbus_proxy = NULL;
static GDBusConnection * connection = NULL;

static void audtool_disconnect (void)
{
    g_object_unref (dbus_proxy);
    dbus_proxy = NULL;

    g_dbus_connection_close_sync (connection, NULL, NULL);
    connection = NULL;
}

static void audtool_connect (void)
{
    GError * error = NULL;

    connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, & error);

    if (! connection)
    {
        fprintf (stderr, "D-Bus error: %s\n", error->message);
        g_error_free (error);
        exit (EXIT_FAILURE);
    }

    dbus_proxy = obj_audacious_proxy_new_sync (connection, 0,
     "org.atheme.audacious", "/org/atheme/audacious", NULL, & error);

    if (! dbus_proxy)
    {
        fprintf (stderr, "D-Bus error: %s\n", error->message);
        g_error_free (error);
        g_dbus_connection_close_sync (connection, NULL, NULL);
        exit (EXIT_FAILURE);
    }

    atexit (audtool_disconnect);
}

int main (int argc, char * * argv)
{
    int i, j = 0, k = 0;

    setlocale (LC_CTYPE, "");

#if ! GLIB_CHECK_VERSION (2, 36, 0)
    g_type_init();
#endif

    audtool_connect ();

    if (argc < 2)
    {
        fprintf (stderr, "Not enough parameters.  Try \"audtool help\".\n");
        exit (EXIT_FAILURE);
    }

    for (j = 1; j < argc; j ++)
    {
        for (i = 0; handlers[i].name != NULL; i++)
        {
            if ((! g_ascii_strcasecmp (handlers[i].name, argv[j]) ||
             ! g_ascii_strcasecmp (g_strconcat ("--", handlers[i].name, NULL),
             argv[j])) && g_ascii_strcasecmp ("<sep>", handlers[i].name))
            {
                int numargs = handlers[i].args + 1 < argc - j ? handlers[i].args + 1 : argc - j;
                handlers[i].handler (numargs, & argv[j]);
                j += handlers[i].args;
                k ++;

                if (j >= argc)
                    break;
            }
        }
    }

    if (k == 0)
    {
        fprintf (stderr, "Unknown command \"%s\".  Try \"audtool help\".\n", argv[1]);
        exit (EXIT_FAILURE);
    }

    return 0;
}