int main() { char fullpath[100], text[100]; char *buf = NULL, *sub = NULL, *state = NULL; conn = mpd_connection_new(NULL, 0, 10000); notify = notify_notification_new(NULL, NULL, NULL); notify_init("mpdnotify"); while(mpd_run_idle_mask(conn, MPD_IDLE_PLAYER)) { mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); status = mpd_recv_status(conn); if(mpd_status_get_state(status) == MPD_STATE_PLAY) state = "Playing:"; else if(mpd_status_get_state(status) == MPD_STATE_PAUSE) state = "Paused:"; else state = "Stopped:"; mpd_response_next(conn); song = mpd_recv_song(conn); if(song) { artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); path = mpd_song_get_uri(song); sprintf(text, "%s - %s", artist, title); buf = fullpath; sub = strrchr(path, '/'); buf = strcpy(buf, mpdroot); buf = strncat(buf, path, strlen(path) - strlen(sub) + 1); buf = strcat(buf, albumart); icon = gdk_pixbuf_new_from_file(fullpath, NULL); if(icon) notify_notification_update(notify, state, text, NULL); else notify_notification_update(notify, state, text, "sound"); notify_notification_set_icon_from_pixbuf(notify, icon); notify_notification_show(notify, &gerror); mpd_song_free(song); } mpd_response_finish(conn); } mpd_connection_free(conn); notify_uninit(); return 0; }
int main(int argc, char **argv) { int t; bool firsttime = true; pid=getpid(); _debug = (bool) getenv("MADAUDIO_DEBUG"); if(!_debug) daemon(0, 0); int flags = LOG_NDELAY | LOG_PID; if(_debug) flags |= LOG_PERROR; openlog("madaudio-unsuspend", flags, LOG_DAEMON); struct mpd_connection *conn; for(t=1000; t > 0; t++) { conn = mpd_connection_new(MADAUDIO_SOCKET, 0, 0); if(conn && mpd_connection_get_error(conn)==MPD_ERROR_SUCCESS) break; debug("can't connect to mpd, retry", pid); usleep(1000); }; debug("connected..."); while(true) { struct mpd_status * status = mpd_run_status(conn); check(conn); if(!status) err(1, "madaudio-unsuspend[%d]: Can't get status\n"); enum mpd_state state = mpd_status_get_state(status); if(state != oldstate) { if(state == MPD_STATE_PLAY) lock_autosuspend(); else { if(!firsttime) unlock_autosuspend(); } }; oldstate = state; firsttime = false; mpd_status_free(status); mpd_run_idle_mask(conn, MPD_IDLE_PLAYER); check(conn); } }
/** * Wait until the song changes or until playback is started/stopped. */ static void wait_current(struct mpd_connection *c) { struct mpd_status *status = getStatus(c); const int old_song = get_active_song(status); mpd_status_free(status); int new_song; do { enum mpd_idle idle = mpd_run_idle_mask(c, MPD_IDLE_PLAYER); if (idle == 0) printErrorAndExit(c); status = getStatus(c); new_song = get_active_song(status); mpd_status_free(status); } while (new_song == old_song); }
/** * Wait until the song changes or until playback is started/stopped. */ static void wait_current(struct mpd_connection *c) { if (mpd_connection_cmp_server_version(c, 0, 14, 0) < 0) fprintf(stderr, "warning: MPD 0.14 required for this command\n"); struct mpd_status *status = getStatus(c); const int old_song = get_active_song(status); mpd_status_free(status); int new_song; do { enum mpd_idle idle = mpd_run_idle_mask(c, MPD_IDLE_PLAYER); if (idle == 0) printErrorAndExit(c); status = getStatus(c); new_song = get_active_song(status); mpd_status_free(status); } while (new_song == old_song); }
int main(int argc, char *argv[]) { int opts; const char *short_opts = "t:r:b:a:p:s:h"; struct option long_opts[] = { { "title", required_argument, 0, 't' }, { "artist", required_argument, 0, 'r' }, { "album", required_argument, 0, 'b' }, { "addr", required_argument, 0, 'a' }, { "port", required_argument, 0, 'p' }, { "secret", required_argument, 0, 's' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } }; unsigned int id = 0; unsigned int current = 0; struct mpd_song *song = NULL; struct mpd_connection *mpd = NULL; char *mpd_addr = getenv("MPD_HOST"); int mpd_port = getenv("MPD_PORT") ? atoi(getenv("MPD_PORT")) : 0; char *mpd_pass = NULL; char *title = NULL; char *artist = NULL; char *album = NULL; if (argc == 1) { help(); return -1; } while ((opts = getopt_long(argc, argv, short_opts, long_opts, 0)) != -1) { switch (opts) { case 't': { title = optarg; break; } case 'r': { artist = optarg; break; } case 'b': { album = optarg; break; } case 'a': { mpd_addr = optarg; break; } case 'p': { mpd_port = atoi(optarg); break; } case 's': { mpd_pass = optarg; break; } default : case 'h': { help(); return -1; } } } mpd = mpd_connection_new(mpd_addr, mpd_port, 30000); CHECK_MPD_CONN(mpd); if (mpd_pass != NULL) { mpd_run_password(mpd, mpd_pass); CHECK_MPD_CONN(mpd); } mpd_search_queue_songs(mpd, false); if (title) { mpd_search_add_tag_constraint( mpd, MPD_OPERATOR_DEFAULT, MPD_TAG_TITLE, title ); } if (artist) { mpd_search_add_tag_constraint( mpd, MPD_OPERATOR_DEFAULT, MPD_TAG_ARTIST, artist ); } if (album) { mpd_search_add_tag_constraint( mpd, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, album ); } mpd_search_commit(mpd); song = mpd_recv_song(mpd); mpd_response_finish(mpd); if (song) { id = mpd_song_get_id(song); mpd_song_free(song); while (true) { unsigned int got = 0; mpd_run_idle_mask(mpd, MPD_IDLE_PLAYER); CHECK_MPD_CONN(mpd); song = mpd_run_current_song(mpd); CHECK_MPD_CONN(mpd); got = mpd_song_get_id(song); mpd_song_free(song); if (current != got) { mpd_run_play_id(mpd, id); CHECK_MPD_CONN(mpd); mpd_connection_free(mpd); return 0; } } } mpd_connection_free(mpd); return 0; }
/*** main ***/ int main(int argc, char ** argv) { char * album = NULL, * artist = NULL, * icon = NULL, * notifystr = NULL, * title = NULL; GError * error = NULL; unsigned short int errcount = 0, state = MPD_STATE_UNKNOWN; const char * mpd_host = MPD_HOST, * music_dir = NULL, * uri = NULL;; unsigned mpd_port = MPD_PORT, mpd_timeout = MPD_TIMEOUT; struct mpd_song * song = NULL; unsigned int i; program = argv[0]; music_dir = getenv("XDG_MUSIC_DIR"); /* get the verbose status */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'v': verbose++; break; } } /* reinitialize getopt() by resetting optind to 0 */ optind = 0; /* say hello */ if (verbose > 0) printf("%s: %s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", program, PROGNAME, VERSION); /* get command line options */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'h': fprintf(stderr, "usage: %s [-h] [-H HOST] [-p PORT] [-m MUSIC-DIR] [-v]\n", program); return EXIT_SUCCESS; case 'p': mpd_port = atoi(optarg); if (verbose > 0) printf("%s: using port %d\n", program, mpd_port); break; case 'm': music_dir = optarg; if (verbose > 0) printf("%s: using music-dir %s\n", program, music_dir); break; case 'H': mpd_host = optarg; if (verbose > 0) printf("%s: using host %s\n", program, mpd_host); break; } } /* disable artwork stuff if we are connected to a foreign host */ if (mpd_host != NULL) music_dir = NULL; /* change directory to music base directory */ if (music_dir != NULL) { if (chdir(music_dir) == -1) { fprintf(stderr, "%s: Can not change directory to '%s'.\n", program, music_dir); music_dir = NULL; } } /* libav */ av_register_all(); conn = mpd_connection_new(mpd_host, mpd_port, mpd_timeout); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { fprintf(stderr,"%s: %s\n", program, mpd_connection_get_error_message(conn)); mpd_connection_free(conn); exit(EXIT_FAILURE); } if(notify_init(PROGNAME) == FALSE) { fprintf(stderr, "%s: Can't create notify.\n", program); exit(EXIT_FAILURE); } notification = # if NOTIFY_CHECK_VERSION(0, 7, 0) notify_notification_new(TEXT_TOPIC, TEXT_NONE, ICON_SOUND); # else notify_notification_new(TEXT_TOPIC, TEXT_NONE, ICON_SOUND, NULL); # endif notify_notification_set_category(notification, PROGNAME); notify_notification_set_urgency (notification, NOTIFY_URGENCY_NORMAL); signal(SIGHUP, received_signal); signal(SIGINT, received_signal); signal(SIGTERM, received_signal); signal(SIGUSR1, received_signal); while(doexit == 0 && mpd_run_idle_mask(conn, MPD_IDLE_PLAYER)) { mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); state = mpd_status_get_state(mpd_recv_status(conn)); if (state == MPD_STATE_PLAY) { mpd_response_next(conn); song = mpd_recv_song(conn); uri = mpd_song_get_uri(song); if (music_dir != NULL && uri != NULL) icon = get_icon(music_dir, uri); if (verbose > 0 && icon != NULL) printf("%s: found icon: %s\n", program, icon); if ((title = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_TITLE, 0), -1)) == NULL) title = strdup(TEXT_UNKNOWN); if ((artist = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), -1)) == NULL) artist = strdup(TEXT_UNKNOWN); if ((album = g_markup_escape_text(mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), -1)) == NULL) album = strdup(TEXT_UNKNOWN); notifystr = malloc(sizeof(TEXT_PLAY) + strlen(title) + strlen(artist) + strlen(album)); sprintf(notifystr, TEXT_PLAY, title, artist, album); free(title); free(artist); free(album); mpd_song_free(song); } else if (state == MPD_STATE_PAUSE) notifystr = TEXT_PAUSE; else if (state == MPD_STATE_STOP) notifystr = TEXT_STOP; else notifystr = TEXT_UNKNOWN; if (verbose > 0) printf("%s: %s\n", program, notifystr); notify_notification_update(notification, TEXT_TOPIC, notifystr, icon ? icon : ICON_SOUND); notify_notification_set_timeout(notification, NOTIFICATION_TIMEOUT); while(notify_notification_show(notification, &error) == FALSE) { if (errcount > 1) { fprintf(stderr, "%s: Looks like we can not reconnect to notification daemon... Exiting.\n", program); exit(EXIT_FAILURE); } else { g_printerr("%s: Error \"%s\" while trying to show notification. Trying to reconnect.\n", program, error->message); errcount++; g_error_free(error); error = NULL; notify_uninit(); usleep(500 * 1000); if(notify_init(PROGNAME) == FALSE) { fprintf(stderr, "%s: Can't create notify.\n", program); exit(EXIT_FAILURE); } } } errcount = 0; if (state == MPD_STATE_PLAY) free(notifystr); if (icon != NULL) { free(icon); icon = NULL; } mpd_response_finish(conn); } if (verbose > 0) printf("Exiting...\n"); mpd_connection_free(conn); g_object_unref(G_OBJECT(notification)); notify_uninit(); return EXIT_SUCCESS; }
int cmd_update(int argc, char **argv, struct mpd_connection *conn) { if (contains_absolute_path(argc, argv) && !path_prepare(conn)) printErrorAndExit(conn); if (!mpd_command_list_begin(conn, false)) printErrorAndExit(conn); int i = 0; const char *update = ""; if (argc > 0) update = charset_to_utf8(argv[i]); do { char *tmp = strdup(update); strip_trailing_slash(tmp); const char *path = tmp; const char *relative_path = to_relative_path(path); if (relative_path != NULL) path = relative_path; mpd_send_update(conn, path); free(tmp); } while (++i < argc && (update = charset_to_utf8(argv[i])) != NULL); if (!mpd_command_list_end(conn)) printErrorAndExit(conn); /* obtain the last "update id" response */ unsigned id = 0; while (true) { unsigned next_id = mpd_recv_update_id(conn); if (next_id == 0) break; id = next_id; } my_finishCommand(conn); while (options.wait) { /* idle until an update finishes */ enum mpd_idle idle = mpd_run_idle_mask(conn, MPD_IDLE_UPDATE); struct mpd_status *status; unsigned current_id; if (idle == 0) printErrorAndExit(conn); /* determine the current "update id" */ status = getStatus(conn); current_id = mpd_status_get_update_id(status); mpd_status_free(status); /* is our last queued update finished now? */ if (current_id == 0 || current_id > id || (id > 1 << 30 && id < 1000)) /* wraparound */ break; } return 1; }
int main(int argc, char ** argv) { struct mpd_connection *conn; conn = mpd_connection_new(NULL, 0, 30000); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); { int i; for(i=0;i<3;i++) { printf("version[%i]: %i\n",i, mpd_connection_get_server_version(conn)[i]); } } if(argc==1) { struct mpd_status * status; struct mpd_song *song; const struct mpd_audio_format *audio_format; mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); status = mpd_recv_status(conn); if (status == NULL) return handle_error(conn); printf("volume: %i\n", mpd_status_get_volume(status)); printf("repeat: %i\n", mpd_status_get_repeat(status)); printf("queue version: %u\n", mpd_status_get_queue_version(status)); printf("queue length: %i\n", mpd_status_get_queue_length(status)); if (mpd_status_get_error(status) != NULL) printf("error: %s\n", mpd_status_get_error(status)); if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) { printf("song: %i\n", mpd_status_get_song_pos(status)); printf("elaspedTime: %i\n",mpd_status_get_elapsed_time(status)); printf("elasped_ms: %u\n", mpd_status_get_elapsed_ms(status)); printf("totalTime: %i\n", mpd_status_get_total_time(status)); printf("bitRate: %i\n", mpd_status_get_kbit_rate(status)); } audio_format = mpd_status_get_audio_format(status); if (audio_format != NULL) { printf("sampleRate: %i\n", audio_format->sample_rate); printf("bits: %i\n", audio_format->bits); printf("channels: %i\n", audio_format->channels); } mpd_status_free(status); if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); mpd_response_next(conn); while ((song = mpd_recv_song(conn)) != NULL) { printf("uri: %s\n", mpd_song_get_uri(song)); print_tag(song, MPD_TAG_ARTIST, "artist"); print_tag(song, MPD_TAG_ALBUM, "album"); print_tag(song, MPD_TAG_TITLE, "title"); print_tag(song, MPD_TAG_TRACK, "track"); print_tag(song, MPD_TAG_NAME, "name"); print_tag(song, MPD_TAG_DATE, "date"); if (mpd_song_get_duration(song) > 0) { printf("time: %u\n", mpd_song_get_duration(song)); } printf("pos: %u\n", mpd_song_get_pos(song)); mpd_song_free(song); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if(argc==3 && strcmp(argv[1],"lsinfo")==0) { struct mpd_entity * entity; if (!mpd_send_list_meta(conn, argv[2])) return handle_error(conn); while ((entity = mpd_recv_entity(conn)) != NULL) { const struct mpd_song *song; const struct mpd_directory *dir; const struct mpd_playlist *pl; switch (mpd_entity_get_type(entity)) { case MPD_ENTITY_TYPE_UNKNOWN: break; case MPD_ENTITY_TYPE_SONG: song = mpd_entity_get_song(entity); printf("uri: %s\n", mpd_song_get_uri(song)); print_tag(song, MPD_TAG_ARTIST, "artist"); print_tag(song, MPD_TAG_ALBUM, "album"); print_tag(song, MPD_TAG_TITLE, "title"); print_tag(song, MPD_TAG_TRACK, "track"); break; case MPD_ENTITY_TYPE_DIRECTORY: dir = mpd_entity_get_directory(entity); printf("directory: %s\n", mpd_directory_get_path(dir)); break; case MPD_ENTITY_TYPE_PLAYLIST: pl = mpd_entity_get_playlist(entity); printf("playlist: %s\n", mpd_playlist_get_path(pl)); break; } mpd_entity_free(entity); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if(argc==2 && strcmp(argv[1],"artists")==0) { struct mpd_pair *pair; if (!mpd_search_db_tags(conn, MPD_TAG_ARTIST) || !mpd_search_commit(conn)) return handle_error(conn); while ((pair = mpd_recv_pair_tag(conn, MPD_TAG_ARTIST)) != NULL) { printf("%s\n", pair->value); mpd_return_pair(conn, pair); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "playlists") == 0) { if (!mpd_send_list_playlists(conn)) return handle_error(conn); struct mpd_playlist *playlist; while ((playlist = mpd_recv_playlist(conn)) != NULL) { printf("%s\n", mpd_playlist_get_path(playlist)); mpd_playlist_free(playlist); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "idle") == 0) { enum mpd_idle idle = mpd_run_idle(conn); if (idle == 0 && mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) return handle_error(conn); for (unsigned j = 0;; ++j) { enum mpd_idle i = 1 << j; const char *name = mpd_idle_name(i); if (name == NULL) break; if (idle & i) printf("%s\n", name); } } else if (argc == 3 && strcmp(argv[1], "subscribe") == 0) { /* subscribe to a channel and print all messages */ if (!mpd_run_subscribe(conn, argv[2])) return handle_error(conn); while (mpd_run_idle_mask(conn, MPD_IDLE_MESSAGE) != 0) { if (!mpd_send_read_messages(conn)) return handle_error(conn); struct mpd_message *msg; while ((msg = mpd_recv_message(conn)) != NULL) { printf("%s\n", mpd_message_get_text(msg)); mpd_message_free(msg); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } return handle_error(conn); } else if (argc == 2 && strcmp(argv[1], "channels") == 0) { /* print a list of channels */ if (!mpd_send_channels(conn)) return handle_error(conn); struct mpd_pair *pair; while ((pair = mpd_recv_channel_pair(conn)) != NULL) { printf("%s\n", pair->value); mpd_return_pair(conn, pair); } if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(conn)) return handle_error(conn); } else if (argc == 4 && strcmp(argv[1], "message") == 0) { /* send a message to a channel */ if (!mpd_run_send_message(conn, argv[2], argv[3])) return handle_error(conn); } mpd_connection_free(conn); return 0; }
void infinite_loop(struct mpd_connection *conn) { char *notification; struct mpd_status *status; NotifyNotification *netlink = NULL; time_t start_time = time(NULL); while (mpd_run_idle_mask(conn, 0xff)) { mpd_command_list_begin(conn, true); mpd_send_status(conn); mpd_send_current_song(conn); mpd_command_list_end(conn); if ((status = mpd_recv_status(conn)) == NULL) { fprintf(stderr, "Cannot connect to MPD. Retrying..."); continue; } if (mpd_status_get_state(status) == MPD_STATE_STOP) notification = strdup("Stopped playback"); else if (mpd_status_get_state(status) == MPD_STATE_PLAY) { struct mpd_song *song; char *title, *album, *artist; mpd_response_next(conn); song = mpd_recv_song(conn); title = (char *)mpd_song_get_tag(song, MPD_TAG_TITLE, 0); album = (char *)mpd_song_get_tag(song, MPD_TAG_ALBUM, 0); artist = (char *)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); notification = format_notification(title, album, artist); mpd_song_free(song); } else if (mpd_status_get_state(status) == MPD_STATE_PAUSE) notification = strdup("Paused playback"); else notification = strdup("tagadatugrut"); if (netlink != NULL && time(NULL) - start_time >= SLEEP_TIME) { notify_notification_close(netlink, NULL); netlink = NULL; } while (netlink == NULL) { netlink = notify_notification_new("MPD Notification", NULL, "sound"); start_time = time(NULL); } notify_notification_update(netlink, "MPD", notification, "sound"); notify_notification_show(netlink, NULL); mpd_response_finish(conn); free(notification); } }