gboolean poll_mpd ( gpointer data ) { struct MAIN_DATA* main_data = data; int status = mpd_poll( main_data->mpd ); if ( status != 0 ) { log_message_warn( main_data->logger, "We lost our connection to MPD. Trying again shortly." ); (void)g_timeout_add_seconds( 3, reconnect_mpd, data ); return FALSE; } if ( mpd_changed( main_data->mpd, MPD_CHANGED_ANY ) ) { display_update( main_data->display ); // \bug maybe check for error... } return TRUE; }
int main(int argc, char **argv) { int n, option_index = 0; struct mg_server *server = mg_create_server(NULL, server_callback); unsigned int current_timer = 0, last_timer = 0; char *run_as_user = NULL; char const *error_msg = NULL; char *webport = "8080"; atexit(bye); #ifdef WITH_DYNAMIC_ASSETS mg_set_option(server, "document_root", SRC_PATH); #endif mpd.port = 6600; strcpy(mpd.host, "127.0.0.1"); static struct option long_options[] = { {"host", required_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {"webport", required_argument, 0, 'w'}, {"dirbletoken", required_argument, 0, 'd'}, {"user", required_argument, 0, 'u'}, {"version", no_argument, 0, 'v'}, {"help", no_argument, 0, 0 }, {"mpdpass", required_argument, 0, 'm'}, {0, 0, 0, 0 } }; while((n = getopt_long(argc, argv, "h:p:w:u:vm:", long_options, &option_index)) != -1) { switch (n) { case 'h': strncpy(mpd.host, optarg, sizeof(mpd.host)); break; case 'p': mpd.port = atoi(optarg); break; case 'w': webport = strdup(optarg); break; case 'd': strncpy(dirble_api_token, optarg, sizeof(dirble_api_token)); break; case 'u': run_as_user = strdup(optarg); break; case 'm': if (strlen(optarg) > 0) mpd.password = strdup(optarg); break; case 'v': fprintf(stdout, "ympd %d.%d.%d\n" "Copyright (C) 2014 Andrew Karpow <*****@*****.**>\n" "built " __DATE__ " "__TIME__ " ("__VERSION__")\n", YMPD_VERSION_MAJOR, YMPD_VERSION_MINOR, YMPD_VERSION_PATCH); return EXIT_SUCCESS; break; default: fprintf(stderr, "Usage: %s [OPTION]...\n\n" " -h, --host <host>\t\tconnect to mpd at host [localhost]\n" " -p, --port <port>\t\tconnect to mpd at port [6600]\n" " -w, --webport [ip:]<port>\tlisten interface/port for webserver [8080]\n" " -u, --user <username>\t\tdrop priviliges to user after socket bind\n" " -d, --dirbletoken <apitoken>\tDirble API token\n" " -V, --version\t\t\tget version\n" " -m, --mpdpass <password>\tspecifies the password to use when connecting to mpd\n" " --help\t\t\t\tthis help\n" , argv[0]); return EXIT_FAILURE; } if(error_msg) { fprintf(stderr, "Mongoose error: %s\n", error_msg); return EXIT_FAILURE; } } error_msg = mg_set_option(server, "listening_port", webport); if(error_msg) { fprintf(stderr, "Mongoose error: %s\n", error_msg); return EXIT_FAILURE; } /* drop privilges at last to ensure proper port binding */ if(run_as_user != NULL) { error_msg = mg_set_option(server, "run_as_user", run_as_user); free(run_as_user); if(error_msg) { fprintf(stderr, "Mongoose error: %s\n", error_msg); return EXIT_FAILURE; } } while (!force_exit) { mg_poll_server(server, 200); current_timer = time(NULL); if(current_timer - last_timer) { last_timer = current_timer; mpd_poll(server); } } mpd_disconnect(); mg_destroy_server(&server); return EXIT_SUCCESS; }
void mpd_disconnect() { mpd.conn_state = MPD_DISCONNECT; mpd_poll(NULL); }
int main(int argc, char **argv) { int n, option_index = 0; struct mg_server *server = mg_create_server(NULL); unsigned int current_timer = 0, last_timer = 0; char *run_as_user = NULL; char radio_song_name[512]; const char *radio_path = NULL; char *radio_url = NULL; char radio_added_song[512]; config_t cfg; config_setting_t *settings; char config_file_name[512]; struct passwd *pw = getpwuid(getuid()); char *homedir = pw->pw_dir; sprintf(config_file_name, "%s/%s/%s", homedir, project_name, ympd_conf_file); printf("conf = %s\n", config_file_name); atexit(bye); mg_set_option(server, "listening_port", "8080"); mpd.port = 6600; strcpy(mpd.host, "127.0.0.1"); config_init(&cfg); if(! config_read_file(&cfg, config_file_name)) { printf("config file error %s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return(EXIT_FAILURE); } if (!config_lookup_string(&cfg, "application.radio_path", &radio_path)) { fprintf(stderr, "No 'radio_path' setting in configuration file.\n"); } /* drop privilges at last to ensure proper port binding */ if(run_as_user != NULL) { mg_set_option(server, "run_as_user", run_as_user); free(run_as_user); } if (!config_lookup_string(&cfg, "streamripper.url", &radio_url)) { fprintf(stderr, "No 'radio_url' setting in configuration file.\n"); } printf("url = %s\n", radio_url); start_streamripper(); printf("start_streamripper\n"); /* init_watch_radio(); printf("init_watch_radio\n"); add_watch_radio(radio_path); printf("add_watch_radio\n");*/ mg_set_http_close_handler(server, mpd_close_handler); mg_set_request_handler(server, server_callback); while (!force_exit) { current_timer = mg_poll_server(server, 200); if(current_timer - last_timer) { last_timer = current_timer; mpd_poll(server); /* if (radio_poll(radio_song_name)) { sprintf(radio_added_song, "%s%s", "radio/", radio_song_name); printf("%s\n", radio_added_song); mpd_run_update(mpd.conn, radio_added_song); sleep(1); mpd_run_add(mpd.conn, radio_added_song); }*/ } } stop_streamripper(); close_watch_radio(); mpd_disconnect(); mg_destroy_server(&server); config_destroy(&cfg); return EXIT_SUCCESS; }
void mpd_idle(gpointer data) { mpd_poll(); g_main_context_wakeup(NULL); }