1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
13 const CommandList music_commands
[] = {
15 {"Music Player Daemon", "sh -c \"mpc | grep -v '^volume: .* repeat: .* random: .*'\""},
16 {"Rhythmbox", "rhythmbox --print-playing"},
17 {"XMMS / Beep", "logjam-xmms-client"},
18 {"amaroK", "dcop amarok player nowPlaying"},
23 MusicSource
music_current_source (void) {
24 if (!conf
.music_command
) return MUSIC_SOURCE_NONE
;
25 for (int i
= 0; i
< MUSIC_SOURCE_COUNT
; ++i
) {
26 if (music_commands
[i
].label
&& music_commands
[i
].command
&& (strcmp(music_commands
[i
].command
, conf
.music_command
) == 0)) return i
;
28 return MUSIC_SOURCE_CUSTOM
;
32 GQuark
music_error_quark (void) {
33 static GQuark quark
= 0;
34 if (quark
== 0) quark
= g_quark_from_static_string("music-error-quark");
39 static gboolean
music_command_can_detect (GError
**err
) {
40 if (!conf
.music_command
|| !conf
.music_command
[0]) {
41 g_set_error(err
, MUSIC_ERROR
, MUSIC_COMMAND_LINE_ERROR
, "%s", _("No music command line is set."));
48 static char *music_command_detect (const char *command
, GError
**err
) {
49 gchar
*std_out
, *std_err
, *p
, *cnv
;
51 if (!g_spawn_command_line_sync(command
, &std_out
, &std_err
, &exit_status
, err
)) return NULL
;
52 if (exit_status
!= 0) {
53 g_set_error(err
, MUSIC_ERROR
, MUSIC_COMMAND_ERROR
, "%s", std_err
);
58 /* we take the first line of this output. */
59 for (p
= std_out
; *p
; ++p
) if (*p
== '\n') break;
60 /* remove trailing spaces */
61 while (p
> std_out
&& isspace(p
[-1])) --p
;
63 if (std_out
[0] && isspace(std_out
[0])) memmove(std_out
, std_out
+1, strlen(std_out
)+1);
65 g_set_error(err
, MUSIC_ERROR
, MUSIC_COMMAND_ERROR
, "%s", _("Command produced no output."));
69 /* Check if result is correct UTF-8 */
70 if (g_utf8_validate(std_out
, -1, NULL
)) return std_out
;
71 /* Try converting from the current locale to UTF-8 */
72 cnv
= g_locale_to_utf8(std_out
, -1, NULL
, NULL
, err
);
78 gboolean
music_can_detect (GError
**err
) {
79 return music_command_can_detect(err
);
83 char *music_detect (GError
**err
) {
84 if (conf
.music_command
) return music_command_detect(conf
.music_command
, err
);