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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* Copyright (C) 2009 Jonathan Matthew <jonathan@d14n.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <glib-object.h>
#include "mediaplayerid.h"
#include "mpid-private.h"
static gboolean debug_enabled = FALSE;
#define ENUM_ENTRY(name, nick) { name, "" #name "", nick }
/**
* MPIDError:
* @MPID_ERROR_NONE: Indicates no error has occurred
* @MPID_ERROR_NO_DEVICE_PATH: Unable to find the device path
* @MPID_ERROR_MECHANISM_FAILED: The device detection mechanism (e.g. udev) failed
* @MPID_ERROR_NOT_MEDIA_PLAYER: The device is not a media player
* @MPID_ERROR_DEVICE_INFO_MISSING: The device detection mechanism identified the device
* but was unable to locate its device information
*/
GType
mpid_error_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
ENUM_ENTRY(MPID_ERROR_NONE, "OK"),
ENUM_ENTRY(MPID_ERROR_NO_DEVICE_PATH, "no-such-device"),
ENUM_ENTRY(MPID_ERROR_MECHANISM_FAILED, "device-db-failed"),
ENUM_ENTRY(MPID_ERROR_NOT_MEDIA_PLAYER, "not-media-player"),
ENUM_ENTRY(MPID_ERROR_DEVICE_INFO_MISSING, "device-info-missing"),
{ 0, 0, 0 }
};
etype = g_enum_register_static ("MPIDError", values);
}
return etype;
}
/**
* MPIDSource:
* @MPID_SOURCE_NONE: No device information is available
* @MPID_SOURCE_SYSTEM: Device information provided by the operating system (e.g. udev)
* @MPID_SOURCE_OVERRIDE: Device information provided by an override file on the device itself.
*/
GType
mpid_source_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
ENUM_ENTRY(MPID_SOURCE_NONE, "no-device-info"),
ENUM_ENTRY(MPID_SOURCE_SYSTEM, "system-device-info"),
ENUM_ENTRY(MPID_SOURCE_OVERRIDE, "override-device-info"),
{ 0, 0, 0 }
};
etype = g_enum_register_static ("MPIDSourceType", values);
}
return etype;
}
/**
* mpid_enable_debug:
* @debug: whether to enable debug output
*
* Enables or disables debug output from the MPID library
*/
void
mpid_enable_debug (gboolean debug)
{
debug_enabled = debug;
}
void
mpid_debug (const char *format, ...)
{
va_list args;
va_start (args, format);
if (debug_enabled)
g_vprintf (format, args);
va_end (args);
}
void
mpid_debug_strv (const char *what, char **strv)
{
int i;
if (strv != NULL) {
mpid_debug ("%s:\n", what);
for (i = 0; strv[i] != NULL; i++) {
mpid_debug ("\t%s\n", strv[i]);
}
} else {
mpid_debug ("%s: (none)\n", what);
}
}
void
mpid_debug_str (const char *what, const char *str)
{
if (str != NULL) {
mpid_debug ("%s: %s\n", what, str);
} else {
mpid_debug ("%s: (none)\n", what);
}
}
static GKeyFile *
read_fake_keyfile (const char *path)
{
const char *fake_group = "[mpid-data]\n";
char *data;
char *munged;
gsize data_size;
gsize munged_data_size;
GKeyFile *keyfile;
GError *error = NULL;
if (g_file_get_contents (path, &data, &data_size, &error) == FALSE) {
mpid_debug ("unable to read contents of file %s: %s\n", path, error->message);
g_clear_error (&error);
return NULL;
}
/* prepend a group name to the file contents */
munged_data_size = data_size + strlen (fake_group);
munged = g_malloc0 (munged_data_size + 1);
strcpy (munged, fake_group);
memcpy (munged + strlen (fake_group), data, data_size);
keyfile = g_key_file_new ();
if (g_key_file_load_from_data (keyfile, munged, munged_data_size, G_KEY_FILE_NONE, &error) == FALSE) {
mpid_debug ("unable to parse contents of file %s: %s\n", path, error->message);
g_key_file_free (keyfile);
keyfile = NULL;
/* probably do something with this error too */
g_clear_error (&error);
}
g_free (munged);
return keyfile;
}
void
mpid_override_string_from_keyfile (char **str, GKeyFile *keyfile, const char *group, const char *key)
{
char *v;
v = g_key_file_get_string (keyfile, group, key, NULL);
if (v != NULL) {
g_free (*str);
*str = v;
}
}
void
mpid_override_strv_from_keyfile (char ***strv, GKeyFile *keyfile, const char *group, const char *key)
{
char **v;
v = g_key_file_get_string_list (keyfile, group, key, NULL, NULL);
if (v != NULL) {
g_strfreev (*strv);
*strv = v;
}
}
void
mpid_device_read_override_file (MPIDDevice *device)
{
GKeyFile *keyfile;
GError *error = NULL;
char *mountpoint;
char *override_path;
char *start_group;
char *str;
int val;
mountpoint = mpid_device_get_mount_point (device);
if (mountpoint == NULL) {
/* maybe set an error if not already set? */
return;
}
override_path = g_build_filename (mountpoint, ".audio_player.mpi", NULL);
if (g_file_test (override_path, G_FILE_TEST_EXISTS)) {
mpid_debug ("found override file %s on mount %s\n", override_path, mountpoint);
device->error = MPID_ERROR_NONE;
mpid_read_device_file (device, override_path);
device->source = MPID_SOURCE_OVERRIDE;
g_free (override_path);
g_free (mountpoint);
return;
}
override_path = g_build_filename (mountpoint, ".is_audio_player", NULL);
if (g_file_test (override_path, G_FILE_TEST_EXISTS) == FALSE) {
mpid_debug ("override file %s not found on mount %s\n", override_path, mountpoint);
g_free (override_path);
g_free (mountpoint);
return;
}
keyfile = read_fake_keyfile (override_path);
g_free (override_path);
g_free (mountpoint);
if (keyfile == NULL) {
/* maybe set an error? */
return;
}
/* forget any previous error */
device->error = MPID_ERROR_NONE;
device->source = MPID_SOURCE_OVERRIDE;
/* ensure we at least have 'storage' protocol and mp3 output.
* for mp3-only devices with no playlists, an empty override file should suffice.
*/
if (device->access_protocols == NULL) {
char *p[] = { MPID_PROTOCOL_GENERIC, NULL };
device->access_protocols = g_strdupv (p);
}
if (device->output_formats == NULL) {
char *f[] = { "audio/mpeg", NULL };
device->output_formats = g_strdupv (f);
}
/* now apply information from the override file */
start_group = g_key_file_get_start_group (keyfile);
g_key_file_set_list_separator (keyfile, ',');
mpid_override_strv_from_keyfile (&device->output_formats, keyfile, start_group, "output_formats");
mpid_override_strv_from_keyfile (&device->input_formats, keyfile, start_group, "input_formats");
mpid_override_strv_from_keyfile (&device->playlist_formats, keyfile, start_group, "playlist_formats");
mpid_override_strv_from_keyfile (&device->audio_folders, keyfile, start_group, "audio_folders");
str = g_key_file_get_string (keyfile, start_group, "playlist_path", NULL);
if (str != NULL) {
g_free (device->playlist_path);
device->playlist_path = str;
}
val = g_key_file_get_integer (keyfile, start_group, "folder_depth", &error);
if (error == NULL) {
device->folder_depth = val;
} else {
g_clear_error (&error);
}
g_key_file_free (keyfile);
}
|