2 #include "gmpc-extras.h"
3 #include "mpd-easy-commands.h"
4 #include "mpdinteraction.h"
5 #include "advanced-search.h"
7 extern int current_volume
;
9 * Easy command interface
11 static void output_set(gpointer data
, const char *param
)
13 gchar
*str_param
= g_strstrip(g_strdup(param
));
14 gchar
**split
= g_strsplit(str_param
, " ", 2);
20 output
= (int)strtol(split
[0], NULL
, 10);
24 /* Default to 'select' if no specific action given */
25 if (!split
[1] || g_utf8_collate(split
[1], _("select")) == 0)
27 MpdData
*devices
= mpd_server_get_output_devices(connection
);
29 gboolean found
= FALSE
;
31 while (d
!= NULL
&& d
->output_dev
->id
!= -10)
33 if (d
->output_dev
->id
== output
)
38 d
= mpd_data_get_next(d
);
43 g_debug("select output %i\n", output
);
45 while (d
!= NULL
&& d
->output_dev
->id
!= -10)
47 mpd_server_set_output_device(connection
, d
->output_dev
->id
,
48 d
->output_dev
->id
== output
? 1 : 0);
49 d
= mpd_data_get_next(d
);
53 /* Let user know their request failed, otherwise they will likely be mystified */
54 gchar
*str
= g_strdup_printf("Can't select output %i because it does not exist", output
);
56 playlist3_message_show(pl3_messages
, str
, ERROR_WARNING
);
59 } else if (g_utf8_collate(split
[1], _("enable")) == 0)
61 g_debug("enable output %i\n", output
);
62 mpd_server_set_output_device(connection
, output
, 1);
63 } else if (g_utf8_collate(split
[1], _("disable")) == 0)
65 mpd_server_set_output_device(connection
, output
, 0);
66 g_debug("disable output %i\n", output
);
73 static void crossfade_set(gpointer data
, const char *param
)
75 gchar
*param_c
= g_utf8_casefold(param
, -1);
76 gchar
*key_c
= g_utf8_casefold(_("Off"), -1);
77 if (g_utf8_collate(param_c
, key_c
) == 0)
79 mpd_status_set_crossfade(connection
, 0);
82 int i
= (int)strtol(param
, NULL
, 10);
85 mpd_status_set_crossfade(connection
, i
);
91 static void volume_set(gpointer data
, const char *param
)
93 int cur_volume
= mpd_status_get_volume(connection
);
94 /* if volume is disabled (current_volume < 0) ignore this command */
95 if (strlen(param
) > 0 && current_volume
>= 0)
98 if (param
[0] == '-' || param
[0] == '+')
102 volume
+= atoi(param
);
103 mpd_status_set_volume(connection
, volume
);
106 static void set_random(gpointer data
, const char *param
)
108 if (strncmp(param
, "on", 2) == 0)
110 mpd_player_set_random(connection
, TRUE
);
111 } else if (strncmp(param
, "off", 3) == 0)
113 mpd_player_set_random(connection
, FALSE
);
119 static void update_database_command(gpointer user_data
, const char *param
)
121 int val
= mpd_server_check_command_allowed(connection
, "update");
122 if (val
== MPD_SERVER_COMMAND_NOT_SUPPORTED
)
124 char * mesg
= g_strdup_printf("%s: %s",
125 _("Update database"),
126 _("The used MPD server is too old and does not support this."));
127 playlist3_message_show(pl3_messages
,
131 } else if (val
== MPD_SERVER_COMMAND_NOT_ALLOWED
)
133 char * mesg
= g_strdup_printf("%s: %s",
134 _("Update database"),
135 _("You have insufficient permission to use this option."));
136 playlist3_message_show(pl3_messages
,mesg
, ERROR_WARNING
);
138 } else if (val
== MPD_SERVER_COMMAND_ALLOWED
)
140 mpd_database_update_dir(connection
, "/");
143 static void repeat_current_song_command(gpointer user_data
, const char *param
)
145 int val
= mpd_server_check_command_allowed(connection
, "single");
146 if (val
== MPD_SERVER_COMMAND_NOT_SUPPORTED
)
148 char * mesg
= g_strdup_printf("%s: %s",
149 _("Repeat current song"),
150 _("The used MPD server is too old and does not support this."));
151 playlist3_message_show(pl3_messages
,
155 } else if (val
== MPD_SERVER_COMMAND_NOT_ALLOWED
)
157 char * mesg
= g_strdup_printf("%s: %s",
158 _("Repeat current song"),
159 _("You have insufficient permission to use this option."));
160 playlist3_message_show(pl3_messages
,mesg
, ERROR_WARNING
);
162 } else if (val
== MPD_SERVER_COMMAND_ALLOWED
)
164 playlist3_message_show(pl3_messages
, _("The current song will be forever repeated."), ERROR_INFO
);
165 mpd_player_set_repeat(connection
, TRUE
);
166 mpd_player_set_single(connection
, TRUE
);
169 static void stop_after_current_song_command(gpointer user_data
, const char *param
)
171 int val
= mpd_server_check_command_allowed(connection
, "single");
172 if (val
== MPD_SERVER_COMMAND_NOT_SUPPORTED
)
174 char * mesg
= g_strdup_printf("%s: %s",
175 _("Stop after current song"),
176 _("The used MPD server is too old and does not support this."));
177 playlist3_message_show(pl3_messages
,mesg
,ERROR_CRITICAL
);
179 } else if (val
== MPD_SERVER_COMMAND_NOT_ALLOWED
)
181 char * mesg
= g_strdup_printf("%s: %s",
182 _("Stop after current song"),
183 _("You have insufficient permission to use this option."));
184 playlist3_message_show(pl3_messages
,mesg
,ERROR_WARNING
);
186 } else if (val
== MPD_SERVER_COMMAND_ALLOWED
)
188 playlist3_message_show(pl3_messages
, _("Playback will be stopped after the current playing song."), ERROR_INFO
);
189 mpd_player_set_repeat(connection
, FALSE
);
190 mpd_player_set_single(connection
, TRUE
);
193 static void seek_command(gpointer user_data
, const char *param
)
199 if (!mpd_check_connected(connection
))
201 printf("seek: '%s'\n", param
);
202 fields
= g_strsplit(param
, ":", -1);
204 for (j
= 0; fields
&& fields
[j
]; j
++)
206 i
= atoi(fields
[j
]) + i
* 60;
208 if (param
[0] == '+' || param
[0] == '-')
211 mpd_player_seek(connection
, mpd_status_get_elapsed_song_time(connection
) + i
);
215 mpd_player_seek(connection
, i
);
219 static void play_command(gpointer user_data
, const char *param
)
221 MpdData
*data
= advanced_search(param
, TRUE
);
224 play_path(data
->song
->file
);
228 static void add_command(gpointer user_data
, const char *param
)
231 MpdData
*data
= advanced_search(param
, FALSE
);
232 for (; data
; data
= mpd_data_get_next(data
))
235 if ((songs
& 16383) == 16383)
237 mpd_playlist_queue_commit(connection
);
239 if (data
->type
== MPD_DATA_TYPE_SONG
)
241 mpd_playlist_queue_add(connection
, data
->song
->file
);
245 mpd_playlist_queue_commit(connection
);
247 static void replace_command(gpointer user_data
, const char *param
)
250 MpdData
*data
= advanced_search(param
, FALSE
);
251 mpd_playlist_clear(connection
);
252 for (; data
; data
= mpd_data_get_next(data
))
255 if ((songs
& 16383) == 16383)
257 mpd_playlist_queue_commit(connection
);
259 if (data
->type
== MPD_DATA_TYPE_SONG
)
261 mpd_playlist_queue_add(connection
, data
->song
->file
);
265 mpd_playlist_queue_commit(connection
);
267 static void set_consume(gpointer data
, const char *param
)
269 int val
= mpd_server_check_command_allowed(connection
, "consume");
270 if (val
== MPD_SERVER_COMMAND_NOT_SUPPORTED
)
272 char * mesg
= g_strdup_printf("%s: %s",
274 _("The used MPD server is too old and does not support this."));
275 playlist3_message_show(pl3_messages
,mesg
,ERROR_CRITICAL
);
277 } else if (val
== MPD_SERVER_COMMAND_NOT_ALLOWED
)
279 char * mesg
= g_strdup_printf("%s: %s",
281 _("You have insufficient permission to use this option."));
282 playlist3_message_show(pl3_messages
,mesg
,ERROR_WARNING
);
284 } else if (val
== MPD_SERVER_COMMAND_ALLOWED
)
286 if (g_utf8_collate(param
, "on") == 0)
288 mpd_player_set_consume(connection
, TRUE
);
289 } else if (g_utf8_collate(param
, "off") == 0)
291 mpd_player_set_consume(connection
, FALSE
);
294 mpd_player_set_consume(connection
, !mpd_player_get_consume(connection
));
298 static void set_single(gpointer data
, const char *param
)
300 int val
= mpd_server_check_command_allowed(connection
, "single");
301 if (val
== MPD_SERVER_COMMAND_NOT_SUPPORTED
)
303 char * mesg
= g_strdup_printf("%s: %s",
305 _("The used MPD server is too old and does not support this."));
306 playlist3_message_show(pl3_messages
,mesg
,ERROR_CRITICAL
);
308 } else if (val
== MPD_SERVER_COMMAND_NOT_ALLOWED
)
310 char * mesg
= g_strdup_printf("%s: %s",
312 _("You have insufficient permission to use this option."));
313 playlist3_message_show(pl3_messages
, mesg
, ERROR_WARNING
);
315 } else if (val
== MPD_SERVER_COMMAND_ALLOWED
)
317 if (g_utf8_collate(param
, "on") == 0)
319 mpd_player_set_single(connection
, TRUE
);
320 } else if (g_utf8_collate(param
, "off") == 0)
322 mpd_player_set_single(connection
, FALSE
);
325 mpd_player_set_single(connection
, !mpd_player_get_single(connection
));
329 static void set_repeat(gpointer data
, const char *param
)
331 if (strncmp(param
, "on", 2) == 0)
333 mpd_player_set_repeat(connection
, TRUE
);
334 } else if (strncmp(param
, "off", 3) == 0)
336 mpd_player_set_repeat(connection
, FALSE
);
342 static void disconnect(gpointer data
, const char *param
)
344 if(mpd_check_connected(connection
)) {
345 disconnect_from_mpd();
348 static void connect(gpointer data
, const char *param
)
350 if(mpd_check_connected(connection
)) {
351 disconnect_from_mpd();
353 if(param
== NULL
|| param
[0] == '\0') {
356 gmpc_profiles_set_profile_from_name(gmpc_profiles
,
362 static void reset_playmode(gpointer data
, const char *param
)
364 mpd_player_set_repeat(connection
, FALSE
);
365 mpd_player_set_random(connection
, FALSE
);
366 mpd_player_set_single(connection
, FALSE
);
367 mpd_player_set_consume(connection
, FALSE
);
370 void mpd_easy_commands_init(void)
373 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("play"), "", _("start playback"),
374 (GmpcEasyCommandCallback
*) play_song
, connection
,
376 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("pause"), "", _("pause playback"),
377 (GmpcEasyCommandCallback
*) pause_song
, connection
,
379 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("next"), "", _("next song"), (GmpcEasyCommandCallback
*) next_song
,
381 GTK_STOCK_MEDIA_NEXT
);
382 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("prev"), "", _("previous song"),
383 (GmpcEasyCommandCallback
*) prev_song
, NULL
,
384 GTK_STOCK_MEDIA_PREVIOUS
);
385 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("stop"), "", _("stop playback"),
386 (GmpcEasyCommandCallback
*) stop_song
, NULL
,
389 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
,
392 _("Reset the play mode."), (GmpcEasyCommandCallback
*) reset_playmode
, NULL
, GTK_STOCK_CLEAR
);
394 gmpc_easy_command_add_entry_icon_name(gmpc_easy_command
, _("random"), "(on|off|)", _("Random (on|off)"),
395 (GmpcEasyCommandCallback
*) set_random
, NULL
, "stock_shuffle");
396 gmpc_easy_command_add_entry_icon_name(gmpc_easy_command
, _("repeat"), "(on|off|)", _("Repeat (on|off)"),
397 (GmpcEasyCommandCallback
*) set_repeat
, NULL
, "stock_repeat");
399 gmpc_easy_command_add_entry_icon_name(gmpc_easy_command
, _("single"), "(on|off|)", _("Single (on|off)"),
400 (GmpcEasyCommandCallback
*) set_single
, NULL
, "media-repeat-single");
401 gmpc_easy_command_add_entry_icon_name(gmpc_easy_command
, _("consume"), "(on|off|)", _("Consume (on|off)"),
402 (GmpcEasyCommandCallback
*) set_consume
, NULL
,"media-consume");
404 /* volume commands */
405 gmpc_easy_command_add_entry_icon_name(gmpc_easy_command
, _("volume"), "[+-]?[0-9]+", _("Volume (+-)<level>"),
406 (GmpcEasyCommandCallback
*) volume_set
, NULL
, "audio-volume-high");
407 gmpc_easy_command_add_entry_icon_name(gmpc_easy_command
, _("mute"), "", _("Mute"),
408 (GmpcEasyCommandCallback
*) volume_toggle_mute
, NULL
, "audio-volume-muted");
410 gmpc_easy_command_add_entry(gmpc_easy_command
, _("crossfade"),
411 C_("Regex for matching crossfade, translate off", "([0-9]+|Off)"),
412 _("Set Crossfade <seconds>"), (GmpcEasyCommandCallback
*) crossfade_set
, NULL
);
414 gmpc_easy_command_add_entry(gmpc_easy_command
, _("output"),
415 C_("Regex for matching output", "[0-9]+[ ]*(Enable|Disable|Select|)"),
416 _("output X enable or disable or select"), (GmpcEasyCommandCallback
*) output_set
,
418 /* basic playlist commands */
419 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("play"), ".*", _("Play <query>"),
420 (GmpcEasyCommandCallback
*) play_command
, NULL
, "gtk-media-play");
421 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("add"), ".*", _("Add <query>"),
422 (GmpcEasyCommandCallback
*) add_command
, NULL
, GTK_STOCK_ADD
);
423 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command
, _("replace"), ".*", _("Replace <query>"),
424 (GmpcEasyCommandCallback
*) replace_command
, NULL
, GTK_STOCK_REDO
);
426 /* Basic seek commands */
427 gmpc_easy_command_add_entry(gmpc_easy_command
, _("seek"), "[+-]?[0-9:]+", _("Seek within the current song"),
428 (GmpcEasyCommandCallback
*) seek_command
, NULL
);
429 /* Advanced commands */
430 gmpc_easy_command_add_entry(gmpc_easy_command
,
431 _("stop after current song"),
433 _("Stop playback after the current song"),
434 (GmpcEasyCommandCallback
*) stop_after_current_song_command
, NULL
);
436 gmpc_easy_command_add_entry(gmpc_easy_command
,
437 _("repeat current song"),
439 _("Repeat the current song"),
440 (GmpcEasyCommandCallback
*) repeat_current_song_command
, NULL
);
442 gmpc_easy_command_add_entry(gmpc_easy_command
,
443 _("update database"),
445 _("Update the database"), (GmpcEasyCommandCallback
*) update_database_command
, NULL
);
448 gmpc_easy_command_add_entry(gmpc_easy_command
,
451 _("disconnect from MPD"), (GmpcEasyCommandCallback
*) disconnect
, NULL
);
453 gmpc_easy_command_add_entry(gmpc_easy_command
,
456 _("connect to MPD"), (GmpcEasyCommandCallback
*) connect
, NULL
);
457 gmpc_easy_command_add_entry(gmpc_easy_command
,
460 _("connect to MPD using profile"), (GmpcEasyCommandCallback
*) connect
, NULL
);