Fix the label int he sidebar
[gmpc.git] / src / Tools / mpd-easy-commands.c
blobd90f6f0329444d0e82811235149fd43cf425e5a9
1 #include "main.h"
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;
8 /**
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);
15 if (split)
17 int output = -1;
18 if (split[0])
20 output = (int)strtol(split[0], NULL, 10);
22 if (output >= 0)
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);
28 MpdData *d = devices;
29 gboolean found = FALSE;
31 while (d != NULL && d->output_dev->id != -10)
33 if (d->output_dev->id == output)
35 found = TRUE;
36 break;
38 d = mpd_data_get_next(d);
41 if (found)
43 g_debug("select output %i\n", output);
44 d = devices;
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);
51 } else
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);
55 g_debug("%s\n", str);
56 playlist3_message_show(pl3_messages, str, ERROR_WARNING);
57 g_free(str);
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);
69 g_free(split);
71 g_free(str_param);
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);
80 } else
82 int i = (int)strtol(param, NULL, 10);
83 if (i >= 0 && i < 60)
85 mpd_status_set_crossfade(connection, i);
88 g_free(key_c);
89 g_free(param_c);
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)
97 int volume = 0;
98 if (param[0] == '-' || param[0] == '+')
100 volume = cur_volume;
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);
114 } else
116 random_toggle();
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,
128 mesg,
129 ERROR_CRITICAL);
130 g_free(mesg);
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);
137 g_free(mesg);
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,
152 mesg,
153 ERROR_CRITICAL);
154 g_free(mesg);
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);
161 g_free(mesg);
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);
178 g_free(mesg);
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);
185 g_free(mesg);
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)
195 int i = 0, j = 0;
196 gchar **fields;
197 if (!param)
198 return;
199 if (!mpd_check_connected(connection))
200 return;
201 printf("seek: '%s'\n", param);
202 fields = g_strsplit(param, ":", -1);
203 /* Calculate time */
204 for (j = 0; fields && fields[j]; j++)
206 i = atoi(fields[j]) + i * 60;
208 if (param[0] == '+' || param[0] == '-')
210 /* seek relative */
211 mpd_player_seek(connection, mpd_status_get_elapsed_song_time(connection) + i);
212 } else
214 /* seek absolute */
215 mpd_player_seek(connection, i);
217 g_strfreev(fields);
219 static void play_command(gpointer user_data, const char *param)
221 MpdData *data = advanced_search(param, TRUE);
222 if (data)
224 play_path(data->song->file);
225 mpd_data_free(data);
228 static void add_command(gpointer user_data, const char *param)
230 gulong songs = 0;
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);
242 songs++;
245 mpd_playlist_queue_commit(connection);
247 static void replace_command(gpointer user_data, const char *param)
249 gulong songs = 0;
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);
262 songs++;
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",
273 _("Consume"),
274 _("The used MPD server is too old and does not support this."));
275 playlist3_message_show(pl3_messages,mesg,ERROR_CRITICAL);
276 g_free(mesg);
277 } else if (val == MPD_SERVER_COMMAND_NOT_ALLOWED)
279 char * mesg = g_strdup_printf("%s: %s",
280 _("Consume"),
281 _("You have insufficient permission to use this option."));
282 playlist3_message_show(pl3_messages,mesg,ERROR_WARNING);
283 g_free(mesg);
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);
292 } else
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",
304 _("Single"),
305 _("The used MPD server is too old and does not support this."));
306 playlist3_message_show(pl3_messages,mesg,ERROR_CRITICAL);
307 g_free(mesg);
308 } else if (val == MPD_SERVER_COMMAND_NOT_ALLOWED)
310 char * mesg = g_strdup_printf("%s: %s",
311 _("Single"),
312 _("You have insufficient permission to use this option."));
313 playlist3_message_show(pl3_messages, mesg, ERROR_WARNING);
314 g_free(mesg);
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);
323 } else
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);
337 } else
339 repeat_toggle();
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') {
354 connect_to_mpd();
355 }else{
356 gmpc_profiles_set_profile_from_name(gmpc_profiles,
357 param);
358 connect_to_mpd();
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)
372 /* Player control */
373 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command, _("play"), "", _("start playback"),
374 (GmpcEasyCommandCallback *) play_song, connection,
375 "gtk-media-play");
376 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command, _("pause"), "", _("pause playback"),
377 (GmpcEasyCommandCallback *) pause_song, connection,
378 "gtk-media-pause");
379 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command, _("next"), "", _("next song"), (GmpcEasyCommandCallback *) next_song,
380 NULL,
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,
387 "gtk-media-stop");
389 gmpc_easy_command_add_entry_stock_id(gmpc_easy_command,
390 _("reset playmode"),
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,
417 NULL);
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,
449 _("disconnect"),
451 _("disconnect from MPD"), (GmpcEasyCommandCallback *) disconnect, NULL);
453 gmpc_easy_command_add_entry(gmpc_easy_command,
454 _("connect"),
456 _("connect to MPD"), (GmpcEasyCommandCallback *) connect, NULL);
457 gmpc_easy_command_add_entry(gmpc_easy_command,
458 _("connect"),
459 ".*",
460 _("connect to MPD using profile"), (GmpcEasyCommandCallback *) connect, NULL);