Project

General

Profile

main.c

Jim Turner, November 21, 2015 18:54

 
1
/*
2
 * main.c
3
 * Copyright 2005-2013 George Averill, William Pitcock, Yoshiki Yazawa, and
4
 *                     John Lindgren
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice,
10
 *    this list of conditions, and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions, and the following disclaimer in the documentation
14
 *    provided with the distribution.
15
 *
16
 * This software is provided "as is" and without any warranty, express or
17
 * implied. In no event shall the authors be liable for any damages arising from
18
 * the use of this software.
19
 */
20

    
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <locale.h>
24

    
25
#include "audtool.h"
26

    
27
const struct commandhandler handlers[] =
28
{
29
    {"<sep>", NULL, "Current song information", 0},
30
    {"current-song", get_current_song, "print formatted song title", 0},
31
    {"current-song-filename", get_current_song_filename, "print song file name", 0},
32
    {"current-song-length", get_current_song_length, "print song length", 0},
33
    {"current-song-length-seconds", get_current_song_length_seconds, "print song length in seconds", 0},
34
    {"current-song-length-frames", get_current_song_length_frames, "print song length in milliseconds", 0},
35
    {"current-song-output-length", get_current_song_output_length, "print playback time", 0},
36
    {"current-song-output-length-seconds", get_current_song_output_length_seconds, "print playback time in seconds", 0},
37
    {"current-song-output-length-frames", get_current_song_output_length_frames, "print playback time in milliseconds", 0},
38
    {"current-song-bitrate", get_current_song_bitrate, "print bitrate in bits per second", 0},
39
    {"current-song-bitrate-kbps", get_current_song_bitrate_kbps, "print bitrate in kilobits per second", 0},
40
    {"current-song-frequency", get_current_song_frequency, "print sampling rate in hertz", 0},
41
    {"current-song-frequency-khz", get_current_song_frequency_khz, "print sampling rate in kilohertz", 0},
42
    {"current-song-channels", get_current_song_channels, "print number of channels", 0},
43
    {"current-song-tuple-data", get_current_song_tuple_field_data, "print value of named field", 1},
44
    {"current-song-info", get_current_song_info, "print bitrate, sampling rate, and channels", 0},
45

    
46
    {"<sep>", NULL, "Playback commands", 0},
47
    {"playback-play", playback_play, "start/restart/unpause playback", 0},
48
    {"playback-pause", playback_pause, "pause/unpause playback", 0},
49
    {"playback-playpause", playback_playpause, "start/pause/unpause playback", 0},
50
    {"playback-stop", playback_stop, "stop playback", 0},
51
    {"playback-playing", playback_playing, "exit code = 0 if playing", 0},
52
    {"playback-paused", playback_paused, "exit code = 0 if paused", 0},
53
    {"playback-stopped", playback_stopped, "exit code = 0 if not playing", 0},
54
    {"playback-status", playback_status, "print status (playing/paused/stopped)", 0},
55
    {"playback-seek", playback_seek, "seek to given time", 1},
56
    {"playback-seek-relative", playback_seek_relative, "seek to relative time offset", 1},
57
    {"playback-record", playback_record, "toggle recording (dubbing)", 1},
58

    
59
    {"<sep>", NULL, "Playlist commands", 0},
60
    {"playlist-advance", playlist_advance, "skip to next song", 0},
61
    {"playlist-reverse", playlist_reverse, "skip to previous song", 0},
62
    {"playlist-addurl", playlist_add_url_string, "add URI at end of playlist", 1},
63
    {"playlist-insurl", playlist_ins_url_string, "insert URI at given position", 2},
64
    {"playlist-addurl-to-new-playlist", playlist_enqueue_to_temp, "open URI in \"Now Playing\" playlist", 1},
65
    {"playlist-delete", playlist_delete, "remove song at given position", 1},
66
    {"playlist-length", playlist_length, "print number of songs in playlist", 0},
67
    {"playlist-song", playlist_song, "print formatted title of given song", 1},
68
    {"playlist-song-filename", playlist_song_filename, "print file name of given song", 1},
69
    {"playlist-song-length", playlist_song_length, "print length of given song", 1},
70
    {"playlist-song-length-seconds", playlist_song_length_seconds, "print length of given song in seconds", 1},
71
    {"playlist-song-length-frames", playlist_song_length_frames, "print length of given song in milliseconds", 1},
72
    {"playlist-tuple-data", playlist_tuple_field_data, "print value of named field for given song", 2},
73
    {"playlist-display", playlist_display, "print all songs in playlist", 0},
74
    {"playlist-position", playlist_position, "print position of current song", 0},
75
    {"playlist-jump", playlist_jump, "skip to given song", 1},
76
    {"playlist-clear", playlist_clear, "clear playlist", 0},
77
    {"playlist-auto-advance-status", playlist_auto_advance_status, "query playlist auto-advance", 0},
78
    {"playlist-auto-advance-toggle", playlist_auto_advance_toggle, "toggle playlist auto-advance", 0},
79
    {"playlist-repeat-status", playlist_repeat_status, "query playlist repeat", 0},
80
    {"playlist-repeat-toggle", playlist_repeat_toggle, "toggle playlist repeat", 0},
81
    {"playlist-shuffle-status", playlist_shuffle_status, "query playlist shuffle", 0},
82
    {"playlist-shuffle-toggle", playlist_shuffle_toggle, "toggle playlist shuffle", 0},
83
    {"playlist-stop-after-status", playlist_stop_after_status, "query if stopping after current song", 0},
84
    {"playlist-stop-after-toggle", playlist_stop_after_toggle, "toggle if stopping after current song", 0},
85

    
86
    {"<sep>", NULL, "More playlist commands", 0},
87
    {"number-of-playlists", number_of_playlists, "print number of playlists", 0},
88
    {"current-playlist", current_playlist, "print number of current playlist", 0},
89
    {"play-current-playlist", play_current_playlist, "play/resume current playlist", 0},
90
    {"set-current-playlist", set_current_playlist, "make given playlist current", 1},
91
    {"current-playlist-name", playlist_title, "print current playlist title", 0},
92
    {"set-current-playlist-name", set_playlist_title, "set current playlist title", 1},
93
    {"new-playlist", new_playlist, "insert new playlist", 0},
94
    {"delete-current-playlist", delete_current_playlist, "remove current playlist", 0},
95

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

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

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

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

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

    
129
    {NULL, NULL, NULL, 0}
130
};
131

    
132
ObjAudacious * dbus_proxy = NULL;
133
static GDBusConnection * connection = NULL;
134

    
135
static void audtool_disconnect (void)
136
{
137
    g_object_unref (dbus_proxy);
138
    dbus_proxy = NULL;
139

    
140
    g_dbus_connection_close_sync (connection, NULL, NULL);
141
    connection = NULL;
142
}
143

    
144
static void audtool_connect (void)
145
{
146
    GError * error = NULL;
147

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

    
150
    if (! connection)
151
    {
152
        fprintf (stderr, "D-Bus error: %s\n", error->message);
153
        g_error_free (error);
154
        exit (EXIT_FAILURE);
155
    }
156

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

    
160
    if (! dbus_proxy)
161
    {
162
        fprintf (stderr, "D-Bus error: %s\n", error->message);
163
        g_error_free (error);
164
        g_dbus_connection_close_sync (connection, NULL, NULL);
165
        exit (EXIT_FAILURE);
166
    }
167

    
168
    atexit (audtool_disconnect);
169
}
170

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

    
175
    setlocale (LC_CTYPE, "");
176

    
177
#if ! GLIB_CHECK_VERSION (2, 36, 0)
178
    g_type_init();
179
#endif
180

    
181
    audtool_connect ();
182

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

    
189
    for (j = 1; j < argc; j ++)
190
    {
191
        for (i = 0; handlers[i].name != NULL; i++)
192
        {
193
            if ((! g_ascii_strcasecmp (handlers[i].name, argv[j]) ||
194
             ! g_ascii_strcasecmp (g_strconcat ("--", handlers[i].name, NULL),
195
             argv[j])) && g_ascii_strcasecmp ("<sep>", handlers[i].name))
196
            {
197
                int numargs = handlers[i].args + 1 < argc - j ? handlers[i].args + 1 : argc - j;
198
                handlers[i].handler (numargs, & argv[j]);
199
                j += handlers[i].args;
200
                k ++;
201

    
202
                if (j >= argc)
203
                    break;
204
            }
205
        }
206
    }
207

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

    
214
    return 0;
215
}