1 /*
2  * wrappers.c
3  * Copyright 2013 John Lindgren
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions, and the following disclaimer in the documentation
13  *    provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <stdlib.h>
21 
22 #include "audtool.h"
23 #include "wrappers.h"
24 
generic_on_off(int argc,char ** argv,OnOffFunc func)25 void generic_on_off (int argc, char * * argv, OnOffFunc func)
26 {
27     gboolean show;
28 
29     if (argc == 1)
30         show = TRUE;
31     else if (argc == 2 && ! g_ascii_strcasecmp (argv[1], "on"))
32         show = TRUE;
33     else if (argc == 2 && ! g_ascii_strcasecmp (argv[1], "off"))
34         show = FALSE;
35     else
36     {
37         audtool_whine_args (argv[0], "<on/off>");
38         exit (1);
39     }
40 
41     func (dbus_proxy, show, NULL, NULL);
42 }
43 
get_playlist_length(void)44 int get_playlist_length (void)
45 {
46     int length = -1;
47     obj_audacious_call_length_sync (dbus_proxy, & length, NULL, NULL);
48 
49     if (length < 0)
50         exit (1);
51 
52     return length;
53 }
54 
get_queue_length(void)55 int get_queue_length (void)
56 {
57     int length = -1;
58     obj_audacious_call_get_playqueue_length_sync (dbus_proxy, & length, NULL, NULL);
59 
60     if (length < 0)
61         exit (1);
62 
63     return length;
64 }
65 
get_queue_entry(int qpos)66 int get_queue_entry (int qpos)
67 {
68     unsigned entry = -1;
69     obj_audacious_call_queue_get_list_pos_sync (dbus_proxy, qpos, & entry, NULL, NULL);
70 
71     if (entry == (unsigned) -1)
72         exit (1);
73 
74     return entry;
75 }
76 
find_in_queue(int entry)77 int find_in_queue (int entry)
78 {
79     unsigned qpos = -1;
80     obj_audacious_call_queue_get_queue_pos_sync (dbus_proxy, entry, & qpos, NULL, NULL);
81 
82     if (qpos == (unsigned) -1)
83         exit (1);
84 
85     return qpos;
86 }
87 
get_current_entry(void)88 int get_current_entry (void)
89 {
90     unsigned entry = -1;
91     obj_audacious_call_position_sync (dbus_proxy, & entry, NULL, NULL);
92 
93     if (entry == (unsigned) -1)
94         exit (1);
95 
96     return entry;
97 }
98 
get_entry_filename(int entry)99 char * get_entry_filename (int entry)
100 {
101     char * uri = NULL;
102     obj_audacious_call_song_filename_sync (dbus_proxy, entry, & uri, NULL, NULL);
103 
104     if (! uri)
105         exit (1);
106 
107     char * filename = g_filename_from_uri (uri, NULL, NULL);
108 
109     if (filename)
110     {
111         g_free (uri);
112         return filename;
113     }
114 
115     return uri;
116 }
117 
get_entry_title(int entry)118 char * get_entry_title (int entry)
119 {
120     char * title = NULL;
121     obj_audacious_call_song_title_sync (dbus_proxy, entry, & title, NULL, NULL);
122 
123     if (! title)
124         exit (1);
125 
126     return title;
127 }
128 
get_entry_length(int entry)129 int get_entry_length (int entry)
130 {
131     int length = -1;
132     obj_audacious_call_song_frames_sync (dbus_proxy, entry, & length, NULL, NULL);
133 
134     if (length < 0)
135         exit (1);
136 
137     return length;
138 }
139 
get_entry_field(int entry,const char * field)140 char * get_entry_field (int entry, const char * field)
141 {
142     GVariant * var = NULL;
143     obj_audacious_call_song_tuple_sync (dbus_proxy, entry, field, & var, NULL, NULL);
144 
145     if (! var || ! g_variant_is_of_type (var, G_VARIANT_TYPE_VARIANT))
146         exit (1);
147 
148     GVariant * var2 = g_variant_get_variant (var);
149 
150     if (! var2)
151         exit (1);
152 
153     char * str;
154 
155     if (g_variant_is_of_type (var2, G_VARIANT_TYPE_STRING))
156         str = g_strdup (g_variant_get_string (var2, NULL));
157     else if (g_variant_is_of_type (var2, G_VARIANT_TYPE_INT32))
158         str = g_strdup_printf ("%d", (int) g_variant_get_int32 (var2));
159     else
160         exit (1);
161 
162     g_variant_unref (var);
163     g_variant_unref (var2);
164 
165     return str;
166 }
167 
get_current_time(void)168 int get_current_time (void)
169 {
170     unsigned time = -1;
171     obj_audacious_call_time_sync (dbus_proxy, & time, NULL, NULL);
172 
173     if (time == (unsigned) -1)
174         exit (1);
175 
176     return time;
177 }
178 
get_current_info(int * bitrate_p,int * samplerate_p,int * channels_p)179 void get_current_info (int * bitrate_p, int * samplerate_p, int * channels_p)
180 {
181     int bitrate = -1, samplerate = -1, channels = -1;
182     obj_audacious_call_get_info_sync (dbus_proxy, & bitrate, & samplerate, & channels, NULL, NULL);
183 
184     if (bitrate < 0 || samplerate < 0 || channels < 0)
185         exit (1);
186 
187     if (bitrate_p)
188         * bitrate_p = bitrate;
189     if (samplerate_p)
190         * samplerate_p = samplerate;
191     if (channels_p)
192         * channels_p = channels;
193 }
194