int main(int argc, char *argv[]) { Irc freenode; struct pollfd pfd[4]; int i, ready, murm_listenfd = -1; initialize(argc, argv); for (i = 0; i < SIZE(pfd); i++) { pfd[i].fd = -1; pfd[i].events = POLLIN; } if (add_murmur_callbacks(cfg.murmur_port)) pfd[MURM_LISTEN].fd = murm_listenfd = sock_listen(LOCALHOST, CB_LISTEN_PORT_S); else fprintf(stderr, "Could not connect to Murmur\n"); if ((pfd[MPD].fd = mpdfd = mpd_connect(cfg.mpd_port)) < 0) fprintf(stderr, "Could not connect to MPD\n"); // Connect to server and set IRC details if (!(freenode = irc_connect(cfg.server, cfg.port))) exit_msg("Irc connection failed"); pfd[IRC].fd = get_socket(freenode); set_nick(freenode, cfg.nick); set_user(freenode, cfg.user); for (i = 0; i < cfg.channels_set; i++) join_channel(freenode, cfg.channels[i]); while ((ready = poll(pfd, SIZE(pfd), TIMEOUT)) > 0) { // Keep reading & parsing lines as long the connection is active and act on any registered actions found if (pfd[IRC].revents & POLLIN) while (parse_irc_line(freenode) > 0); if (pfd[MURM_LISTEN].revents & POLLIN) if ((pfd[MURM_ACCEPT].fd = accept_murmur_connection(murm_listenfd)) > 0) pfd[MURM_LISTEN].fd = -1; // Stop listening for connections if (pfd[MURM_ACCEPT].revents & POLLIN) { if (!listen_murmur_callbacks(freenode, pfd[MURM_ACCEPT].fd)) { pfd[MURM_ACCEPT].fd = -1; pfd[MURM_LISTEN].fd = murm_listenfd; // Start listening again for Murmur connections } } if (pfd[MPD].revents & POLLIN) if (!print_song(freenode, default_channel(freenode))) pfd[MPD].fd = mpdfd = mpd_connect(cfg.mpd_port); } // If we reach here, it means we got disconnected from server. Exit with error (1) if (ready == -1) perror("poll"); else fprintf(stderr, "%d minutes passed without getting a message, exiting...\n", TIMEOUT / 1000 / 60); quit_server(freenode, cfg.quit_msg); cleanup(); return 1; }
/* callback if a row of the playlist is double-clicked */ static void pl_row_activated(GtkTreeView *tv, GtkTreePath *path, GtkTreeViewColumn *column, gpointer data) { GtkTreeIter iter; GtkTreeModel *model; gint pl_id; pl_id = -32; model = gtk_tree_view_get_model(tv); gtk_tree_model_get_iter(model, &iter, path); gtk_tree_model_get(model, &iter, COLUMN_PL_ID, &pl_id, -1); if(pl_id > -32) { // if auto connect is set, try to reconnect if(mpd_info.msi.connected) { if(!mpd_check_connected(mpd_info.obj)) { if(mpd_connect(mpd_info.obj) != MPD_OK) { msi_clear(&mpd_info); mpd_info.msi.connected = FALSE; return; } msi_fill(&mpd_info); mpd_info.msi.connected = TRUE; } mpd_player_play_id(mpd_info.obj, pl_id); } } fprintf(log_file, "[%s:%3i] %s(): pl_id = %i\n", __FILE__, __LINE__, __FUNCTION__, pl_id); fflush(log_file); return; }
int main(int argc, char **argv) { int iport = 6600; char *hostname = getenv("MPD_HOST"); char *port = getenv("MPD_PORT"); char *password = getenv("MPD_PASSWORD"); MpdObj *obj = NULL; if(!hostname) { hostname = "localhost"; } if(port){ iport = atoi(port); } /* Create mpd object */ obj = mpd_new(hostname, iport,password); /* Connect signals */ mpd_signal_connect_error(obj,(ErrorCallback)error_callback, NULL); mpd_signal_connect_status_changed(obj,(StatusChangedCallback)status_changed, NULL); /* Set timeout */ mpd_set_connection_timeout(obj, 10); if(!mpd_connect(obj)) { mpd_send_password(obj); do{ mpd_status_update(obj); } while(!usleep(300000)); } mpd_free(obj); return 0; }
bool gimmix_connect (void) { char *host = NULL; char *pass = NULL; int port; host = cfg_get_key_value (conf, "mpd_hostname"); pass = cfg_get_key_value (conf, "mpd_password"); port = atoi (cfg_get_key_value (conf, "mpd_port")); gmo = mpd_new (host, port, pass); mpd_signal_connect_error (gmo, (ErrorCallback)gimmix_mpd_connection_error_callback, NULL); if (mpd_connect(gmo) == MPD_OK) { mpd_send_password (gmo); printf ("connected to mpd\n"); mpd_signal_connect_connection_changed (gmo, (ConnectionChangedCallback)gimmix_mpd_connection_changed_callback, NULL); return true; } else { mpd_free (gmo); gmo = NULL; } return false; }
int main() { if(daemon(0, 0) < 0) { exit(1); } signal(SIGHUP, handler); signal(SIGTERM, handler); signal(SIGINT, handler); signal(SIGQUIT, handler); char *home = getenv("HOME"); int size = snprintf(NULL, 0, "%s/.mpdnp.pipe", home) + 1; path = malloc(size); snprintf(path, size, "%s/.mpdnp.pipe", home); mkfifo(path, 0666); fd = open(path, O_WRONLY | O_NONBLOCK); if(path != NULL) { free(path); } write(fd, " ", 2); char *host = getenv("MPD_HOST"); char *port = getenv("MPD_PORT"); if(host == NULL) { host = "localhost"; } if(port == NULL) { port = "6600"; } // New object obj = mpd_new(host, atoi(port), NULL); // Connect the signal mpd_signal_connect_status_changed(obj, (StatusChangedCallback) status_changed, NULL); // Timeout mpd_set_connection_timeout(obj, 10); if(!mpd_connect(obj)) { while(!usleep(100000)) { if(obj == NULL) { break; } else { mpd_status_update(obj); } } } tidyUp(); return 0; }
static gboolean mpd_reconnect(G_GNUC_UNUSED gpointer data) { if (!mpd_connect()) { mpd_disconnect(); return TRUE; // try again } mpd.reconnect_source = 0; return FALSE; // remove event }
static void __async_handler_function(GSimpleAsyncResult * simple, GObject * object, GCancellable * cancel) { MpdAsyncData *data = g_simple_async_result_get_op_res_gpointer(G_SIMPLE_ASYNC_RESULT(simple)); g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Start async function\n"); if (mpd_connect(data->mi) == MPD_OK) { g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "call function\n"); data->result = (data->function(data->mi, data->function_data)); g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Got result: %p\n", data->result); } mpd_free(data->mi); }
/* callback wrapper for mpd_mrl_add */ static void mpd_mrl_add_cb(GtkWidget *widget, gpointer data) { GtkEntry *e = GTK_ENTRY(widget); const gchar *etext = gtk_entry_get_text(e); if(mpd_info.msi.connected) { if(!mpd_check_connected(mpd_info.obj)) { if(mpd_connect(mpd_info.obj) != MPD_OK) { mpd_info.msi.connected = FALSE; return; } else { mpd_info.msi.connected = TRUE; } } mpd_mrl_add(mpd_info.obj, etext); } gtk_widget_destroy(GTK_WIDGET(data)); return; }
/* delete songs from playlist */ void pl_del_songs(GtkWidget *widget, gpointer data) { GtkTreeIter iter; int i, selection_length; gint *pl_id; GList *selection; GtkTreeModel *tree_model; pl_id = (gint *)-32; selection = gtk_tree_selection_get_selected_rows(GTK_TREE_SELECTION(data), &tree_model); selection_length = g_list_length(selection); if(debug) { fprintf(log_file, "[%s:%3i] %s(): selection length = %i\n", __FILE__, __LINE__, __FUNCTION__, selection_length); fflush(log_file); } if(mpd_info.msi.connected) { if(!mpd_check_connected(mpd_info.obj)) if(mpd_connect(mpd_info.obj) != MPD_OK) { msi_clear(&mpd_info); mpd_info.msi.connected = FALSE; return; } msi_fill(&mpd_info); mpd_info.msi.connected = TRUE; } for(i=0; i<selection_length; i++) { gtk_tree_model_get_iter(tree_model, &iter, selection->data); gtk_tree_model_get(tree_model, &iter, COLUMN_PL_ID, &pl_id, -1); if(debug) { fprintf(log_file, "[%s:%3i] %s(): song id = %i\n", __FILE__, __LINE__, __FUNCTION__, (int)pl_id); fflush(log_file); } mpd_playlist_queue_delete_id(mpd_info.obj, (int)pl_id); selection = g_list_next(selection); } mpd_playlist_queue_commit(mpd_info.obj); fprintf(log_file, "[%s:%3i] %s(): finished deleting songs\n", __FILE__, __LINE__, __FUNCTION__); g_list_foreach (selection, (GFunc)gtk_tree_path_free, NULL); g_list_free (selection); return; }
MpdObj * gimmix_mpd_connect (void) { MpdObj *mo; char *host; char *pass; int port; host = cfg_get_key_value (conf, "mpd_hostname"); pass = cfg_get_key_value (conf, "mpd_password"); port = atoi (cfg_get_key_value (conf, "mpd_port")); mo = mpd_new (host, port, pass); if (mpd_connect (mo) == MPD_OK) { mpd_signal_connect_status_changed (mo, (StatusChangedCallback)gimmix_status_changed, NULL); return mo; } return NULL; }
static int process_value(struct volume_handle *vh, struct mpd_handle *mh, uint32_t val, uint8_t adc_chan) { int8_t ret = 0, count = 0; /* mpd connection is sometimes invalid (especially while loading first * time after start up) at this point, though the first mpd_connect * reports sucess. Appears to be a bug in libmpd. Hence we check the * connection again here and do reconnecting if necessary. */ while (!mpd_check_connected(mh->mpd_obj)) { mpd_connect(mh->mpd_obj); usleep(500000); count++; if (count > 10) { printf("time out trying reconnecting to mpd\n"); ret = 1; goto out; } } if (adc_chan == volume_chan) { if (! vol_control_enabled) goto out; vh->vol_raw_val = val; set_volume(vh); } else if (adc_chan == tuner_chan) { if (! tuner_control_enabled) goto out; mh->tuner_raw_val = val; /* check result of set_tuner. If set_tuner failed, we have NULL * pointers to mpd_song and hence might have segfaults. */ ret = set_tuner(mh); } out: return ret; }
MpdObj * gimmix_mpd_connect (Conf *conf) { MpdObj *mo; if (conf) { if ((conf->hostname!="") && (conf->port!=-1)) { mo = mpd_new (conf->hostname, conf->port, conf->password); mpd_connect (mo); if (mpd_check_connected (mo)) { mpd_signal_connect_status_changed (mo, (StatusChangedCallback)song_changed, NULL); return mo; } } else return NULL; } return NULL; }
/* -------------------------------------------------------------------------- */ static bool mpd_start_connection(struct lcd_stuff_mpd *mpd) { int err; mpd->error = false; /* create the current song */ mpd_song_delete(mpd->current_song); mpd->current_song = mpd_song_new("", ""); /* create the object */ if (mpd->mpd) { mpd_free(mpd->mpd); mpd->mpd = NULL; } mpd->mpd = mpd_new(mpd->connection->host, mpd->connection->port, mpd->connection->password); /* connect signal handlers */ mpd_signal_connect_error(mpd->mpd, mpd_error_handler, mpd); mpd_signal_connect_status_changed(mpd->mpd, mpd_state_changed_handler, mpd); mpd_signal_connect_connection_changed(mpd->mpd, mpd_connection_changed_handler, mpd); /* set timeout */ mpd_set_connection_timeout(mpd->mpd, mpd->timeout); if (mpd->error) { mpd_disconnect(mpd->mpd); return false; } /* connect */ err = mpd_connect(mpd->mpd); if (err != MPD_OK || mpd->error) { report(RPT_ERR, "Failed to connect: %d", err); return false; } return true; }
static void mpd_connect_cb(GtkWidget *widget, gpointer data) { GList *tmp; if((mpd_connect(mpd_info.obj))==MPD_OK) { msi_fill(&mpd_info); mpd_info.msi.connected = TRUE; tmp = mpd_info.period_funcs; for(tmp=g_list_first(tmp);tmp!=NULL;tmp=g_list_next(tmp)) { g_timeout_add(500, (GSourceFunc)tmp->data, (gpointer)mpd_info.update_interval); } tmp=mpd_info.update_routines; for(tmp=g_list_first(tmp);tmp!=NULL;tmp=g_list_next(tmp)) { g_timeout_add(mpd_info.update_interval, (GSourceFunc)tmp->data, (gpointer)mpd_info.update_interval); } mpd_gui_show_mpd_elements(); } else { msi_clear(&mpd_info); mpd_info.msi.connected = FALSE; mpd_gui_hide_mpd_elements(); } return; }
struct mpd_connection * mpd_connection_new(const char *host, unsigned port, unsigned timeout_ms) { struct mpd_connection *connection = malloc(sizeof(*connection)); bool success; int fd; const char *line; char *password = NULL; if (connection == NULL) return NULL; mpd_error_init(&connection->error); connection->async = NULL; connection->parser = NULL; connection->receiving = false; connection->sending_command_list = false; connection->pair_state = PAIR_STATE_NONE; connection->request = NULL; if (!mpd_socket_global_init(&connection->error)) return connection; if (timeout_ms == 0) /* 30s is the default */ timeout_ms = 30000; mpd_connection_set_timeout(connection, timeout_ms); host = mpd_check_host(host, &password); port = mpd_check_port(port); fd = mpd_connect(host, port, &connection->timeout, &connection->error); if (fd < 0) { free(password); return connection; } connection->async = mpd_async_new(fd); if (connection->async == NULL) { free(password); mpd_socket_close(fd); mpd_error_code(&connection->error, MPD_ERROR_OOM); return connection; } connection->parser = mpd_parser_new(); if (connection->parser == NULL) { free(password); mpd_error_code(&connection->error, MPD_ERROR_OOM); return connection; } line = mpd_sync_recv_line(connection->async, &connection->timeout); if (line == NULL) { free(password); mpd_connection_sync_error(connection); return connection; } success = mpd_parse_welcome(connection, line); if (password != NULL) { if (success) mpd_run_password(connection, password); free(password); } return connection; }
int main(int argc, char *argv[]) { pid_t pid; struct sigaction sa; if (init_preferences(argc, argv) == FALSE) g_error("Config file parsing failed"); /* Open the log file before forking, so that if there is an error, the * user will get some idea what is going on */ open_log(prefs.log_file); g_log_set_default_handler(scmpc_log, NULL); /* Check if scmpc is already running */ if ((pid = scmpc_is_running()) > 0) { clear_preferences(); g_error("Daemon is already running with PID: %ld", (long)pid); } /* Daemonise if wanted */ if (prefs.fork) daemonise(); /* Signal handler */ open_signal_pipe(); sa.sa_handler = sighandler; sigfillset(&sa.sa_mask); sa.sa_flags = SA_RESTART; sigaction(SIGINT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); if (as_connection_init() == FALSE) { scmpc_cleanup(); exit(EXIT_FAILURE); } as_authenticate(); queue_init(); queue_load(); // submit the loaded queue as_check_submit(); mpd.song_pos = g_timer_new(); mpd.idle_source = 0; if (!mpd_connect()) { mpd_disconnect(); mpd_schedule_reconnect(); } // set up main loop events loop = g_main_loop_new(NULL, FALSE); // save queue if (prefs.cache_interval > 0) { cache_save_source = g_timeout_add_seconds(prefs.cache_interval * 60, queue_save, NULL); } g_main_loop_run(loop); scmpc_cleanup(); }
/* searches in the mpd database for a given string and outputs the result (data contains a GtkEntry and a GtkWindow)*/ static void mpd_search(GtkWidget *widget, gpointer data) { GList *args,*vbox_content,*argument_list; gpointer list_data; GtkWidget *vbox, *entry, *label, *result_tree_view, *add_button, *scrolled_window, *window; GtkBox *box; GtkBoxChild *child; const gchar *entry_string; gchar *search_string; GtkListStore *list_store; GtkTreeSelection *tree_selection; GtkObject *h_adjustment, *v_adjustment; MpdData *result, *res_ptr; int result_count = 0; int i = 0; GList *song_handle_list; MpdPlContainer *result_list; args = (GList *)data; /* remove old results, if any */ vbox = g_list_nth_data(args, 1); box = GTK_BOX(vbox); vbox_content = box->children; list_data = g_list_nth_data(vbox_content, 4); if(list_data != NULL) { child = list_data; gtk_widget_destroy(child->widget); } list_data = g_list_nth_data(vbox_content, 3); if(list_data != NULL) { child = list_data; gtk_widget_destroy(child->widget); } if(debug) { fprintf(log_file, "[%s:%3i] %s(): Type = %i\n", __FILE__, __LINE__, __FUNCTION__, search_type); fflush(log_file); } /* search for the typed string */ entry = g_list_nth_data(args, 0); entry_string = gtk_entry_get_text(GTK_ENTRY(entry)); search_string = g_strdup(entry_string); /* table ^= search_type */ if(!mpd_check_connected(mpd_info.obj)) { if(mpd_connect(mpd_info.obj)!=MPD_OK) { msi_clear(&mpd_info); mpd_info.msi.connected = FALSE; return; } msi_fill(&mpd_info); mpd_info.msi.connected = TRUE; } /* * Last argument: TRUE -> only exact matches * FALSE -> more matches */ result = mpd_database_find(mpd_info.obj, search_type, search_string, FALSE); /* Iterate through the found songs, using * the function mpd_data_get_next_keep(md), which * calls mpd_data_get_next_real(md,FALSE) which * prevents the list being killed when reaching * the end. */ for( res_ptr = mpd_data_get_first(result); res_ptr != NULL; res_ptr = mpd_data_get_next_keep(res_ptr) ) { /* count only songs ... */ if(res_ptr->type == MPD_DATA_TYPE_SONG) ++result_count; } /* no matches */ if(result_count == 0) { label = gtk_label_new("No songs found!"); gtk_box_pack_end_defaults(GTK_BOX(vbox), label); gtk_widget_show(label); } /* output found songs */ else { song_handle_list = NULL; result_list = mpd_pl_container_new_alloc(result_count); //mpd_Song *result_list[result_count]; /* as above: the song info is still needed, as * the mpd_Song objects are *not* copied into the * result list */ for( res_ptr = mpd_data_get_first(result); res_ptr != NULL; res_ptr = mpd_data_get_next_keep(res_ptr) ) { /* only songs */ if(res_ptr->type == MPD_DATA_TYPE_SONG) { result_list->list[i++] = mpd_songDup(res_ptr->song); song_handle_list = g_list_append(song_handle_list, strdup(res_ptr->song->file)); } } list_store = pl_create_list_store(result_list); mpd_pl_container_free(result_list); result_tree_view = pl_create_tree_view(GTK_TREE_MODEL(list_store)); /* create a facility to select only some of the found songs for adding...*/ tree_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(result_tree_view)); gtk_tree_selection_set_mode(tree_selection, GTK_SELECTION_MULTIPLE); /**********/ argument_list = NULL; argument_list = g_list_append(argument_list, song_handle_list); argument_list = g_list_append(argument_list, tree_selection); add_button = gtk_button_new_with_label("add selected songs"); g_signal_connect(GTK_BUTTON(add_button), "clicked", G_CALLBACK(mpd_add_song_list), argument_list); /* g_signal_connect_swapped(GTK_BUTTON(add_button), "clicked", G_CALLBACK(gtk_widget_destroy), g_list_nth_data(data, 2)); */ gtk_box_pack_end(GTK_BOX(vbox), add_button,FALSE,FALSE,0); /* put a scrolled window in between the treeview and the window... */ /* I just don't know how this works exactly, but the result is acceptable, so why bother */ h_adjustment = gtk_adjustment_new(0, 0, 4, 1, 4, 4); v_adjustment = gtk_adjustment_new(0, 0, 20, 1, 20, 20); scrolled_window = gtk_scrolled_window_new(GTK_ADJUSTMENT(h_adjustment), GTK_ADJUSTMENT(v_adjustment)); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_NONE); /* display scrollbars only when needed */ gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_container_add(GTK_CONTAINER(scrolled_window), result_tree_view); gtk_box_pack_end_defaults(GTK_BOX(vbox), scrolled_window); gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE); gtk_widget_show_all(scrolled_window); gtk_widget_show_all(add_button); window = g_list_nth_data(args, 2); gtk_window_resize(GTK_WINDOW(window), 700, 500); } /* delete search result, have to do this ourselves * because we needed the song data until now */ mpd_data_free(result); return; }
/* updates title_text, sb_text and tt strings */ gboolean update_info(gpointer data) { char *state_string; int current_time, song_time; int play_state; if(mpd_info.msi.connected) { // connected play_state = mpd_play_state(); current_time = mpd_get_current_play_time(); song_time = mpd_get_current_song_time(); if(!mpd_info.mps.update) { switch(play_state) { /* playing */ case 1: state_string = "playing"; do_string(FMT_TITLE, state_string, current_time, song_time); do_string(FMT_STATUSBAR, state_string, current_time, song_time); #ifdef APPLET do_string(FMT_TOOLTIP, state_string, current_time, song_time); if(musicus_applet.show_text) do_string(FMT_APPLET, state_string, current_time, song_time); else snprintf(ap_text, 255, "[MPD]"); #endif break; case 2: state_string = "paused"; do_string(FMT_TITLE, state_string, current_time, song_time); do_string(FMT_STATUSBAR, state_string, current_time, song_time); #ifdef APPLET do_string(FMT_TOOLTIP, state_string, current_time, song_time); if(musicus_applet.show_text) do_string(FMT_APPLET, state_string, current_time, song_time); else snprintf(ap_text, 255, "[MPD]"); #endif break; default: snprintf(title_text, 255, "MPD [stopped]"); snprintf(sb_text, 255, "[stopped]"); #ifdef APPLET snprintf(tt_text, 255, "MPD [stopped]"); snprintf(ap_text, 255, "MPD"); #endif } } /* update MpdObj status */ if(!mpd_check_connected(mpd_info.obj)) { if(mpd_connect(mpd_info.obj)!=MPD_OK) { msi_clear(&mpd_info); mpd_info.msi.connected = FALSE; return FALSE; } msi_fill(&mpd_info); mpd_info.msi.connected = TRUE; } mpd_status_update(mpd_info.obj); return ((mpd_info.update_interval != *(int*)data) ? FALSE : TRUE); } #ifdef APPLET snprintf(ap_text, 255, "MPD"); #endif return FALSE; }
void *main_mpd_cli(void *data ) { blockQueue char_queue; // kolejka polecen thread_data *my_data; my_data = (thread_data*)data; ////////////////////////////// LCD PART /////////////////////// my_data->mainLCD->set_print_song_state(0); /////////////////////////////////////////////////////////////////// int iport = 6600, button_counter =0; char *hostname = getenv("MPD_HOST"); char *port = getenv("MPD_PORT"); char *password = getenv("MPD_PASSWORD"); MpdObj *obj = NULL; std::cout << " adres hosta to " << hostname << std::endl; if(!hostname) { std::cout << " ip mpd to " << my_data->server_settings->MPD_IP << " ! \n"; hostname = (char*)my_data->server_settings->MPD_IP.c_str(); } if(port){ iport = std::stoi(port); } /* Create mpd object */ obj = mpd_new(hostname, iport,password); /* Connect signals */ mpd_signal_connect_error(obj,(ErrorCallback)error_callback, NULL); mpd_signal_connect_status_changed(obj,(StatusChangedCallback)status_changed , my_data ); /* Set timeout */ mpd_set_connection_timeout(obj, 10); int work; work = mpd_connect(obj); while (work){ log_file_mutex.mutex_lock(); log_file_cout << ERROR << "nie udalo sie polaczyc z MPD "<< std::endl; log_file_mutex.mutex_unlock(); system("service mpd stop"); sleep(1); system("service mpd start"); log_file_mutex.mutex_lock(); log_file_cout << INFO << "restart MPD "<< std::endl; log_file_cout << INFO << "nawiazuje nowe polaczenie z MPD "<< std::endl; log_file_mutex.mutex_unlock(); my_data->myEventHandler.run("mpd")->addEvent("restart MPD"); work = mpd_connect(obj); } std::cout << " stop "<<std::endl; if(!work) { char buffer; do{ if(char_queue._size() > 0) { //digitalWrite(LED7,1); buffer = char_queue._get(); switch(buffer) { case '\n': break; case 'D': mpd_player_next(obj); break; case 'U': mpd_player_prev(obj); break; case 't': mpd_player_play(obj); break; case 'A': mpd_player_pause(obj); break; case 'P': mpd_player_pause(obj); mpd_player_stop(obj); break; case 'q': printf("Quitting....\n"); break; case 'R': mpd_player_set_repeat(obj, !mpd_player_get_repeat(obj)); break; case 's': mpd_player_set_random(obj, !mpd_player_get_random(obj)); break; case 'p': /*if(char_queue._size() > 0) { buffer = char_queue._get(); int id = atoi(buffer); printf(GREEN"Playing:"RESET" %i\n", id); mpd_player_play_id(obj,id); } break;*/ case '+': mpd_status_set_volume(obj, mpd_status_get_volume(obj)+1); break; case '-': mpd_status_set_volume(obj, mpd_status_get_volume(obj)-1); break; case '%': mpd_status_set_volume(obj, my_data->ptr_MPD_info->volume); break; case '2': debug_level = (debug_level > 0)?0:3; printf( "Debug:"" %s\n", (debug_level >0)? "Enabled":"Disabled"); break; case 'I': mpd_player_play_id(obj,my_data->currentSongID); break; case 'h': printf("\th:\t\tHelp\n"\ "\td:\t\tToggle debug on/off\n"\ "\t+:\t\tIncrease volume\n"\ "\t-:\t\tDecrease volume\n"\ "\ta <pass>:\t Authentificate with pass\n"\ "\tp <id>:\t Play song with id\n"\ "\tl:\t\tList the playlist\n"\ "\ts:\t\tToggle shuffle mode\n"\ "\tr:\t\tToggle repeat\n"\ "\tq:\t\tQuit\n"\ "\tv:\t\tStop\n"\ "\tc:\t\tPause\n"\ "\tx:\t\tPlay\n"\ "\tz:\t\tPrevious\n"\ "\tb:\t\tNext\n"); break; default: printf("buffer: %c\n", buffer); } } if (!mpd_check_connected(obj)) { log_file_mutex.mutex_lock(); log_file_cout << WARNING << "utracono polacznie z MPD "<< std::endl; log_file_cout << INFO << "restart MPD" << std::endl; log_file_mutex.mutex_unlock(); system ("service mpd restart"); mpd_connect(obj); } mpd_status_update(obj); my_data->mainLCD->checkState(); if ( digitalRead(BUTTON_PIN) == HIGH ) { std::cout << " wcisnieta pin !" << std::endl; if (check_title_song_to == true && button_counter ==4) { char_queue._add('P'); std::cout << " \n\ngasze !" << std::endl; } else if (check_title_song_to == false && button_counter ==4) { char_queue._add('t'); std::cout << " \n\n\n zapalam !" << std::endl; } else if (check_title_song_to == false && button_counter ==10) { std::cout << " \n\n\n koniec programu z przyciska !" << std::endl; go_while = false; } std::cout << " \n\n\n licznik guzika wskazuje "<< button_counter << std::endl; ++button_counter; } else { button_counter =0; } } while(!usleep(500000) && go_while); mpd_player_stop(obj); sleep (3); } else{ std::cout << " NIE UDALO SIE POłączyć "<<std::endl; } mpd_free(obj); log_file_mutex.mutex_lock(); log_file_cout << INFO << " koniec watku klient MPD "<< std::endl; log_file_mutex.mutex_unlock(); return 0; }
int main(int argc, char **argv) { int fdstdin = 0; pthread_t Lkeys; pthread_t g15display; pthread_t EKeys; int volume; int volume_new; char devname[256] = "Unknown"; int iport = 6600; char *hostname = getenv("MPD_HOST"); char *port = getenv("MPD_PORT"); char *password = getenv("MPD_PASSWORD"); int eventdev; char evdev_name[128]; pthread_mutex_init(&lockit,NULL); pthread_mutex_init(&daemon_mutex,NULL); int i; for (i=0;i<argc;i++) { char argument[20]; memset(argument,0,20); strncpy(argument,argv[i],19); if (!strncmp(argument, "-q",2) || !strncmp(argument, "--quickscroll",13)) { quickscroll=1; } if (!strncmp(argument, "-h",2) || !strncmp(argument, "--help",6)) { printf(" %s version %s\n (c)2006-2007 Mike Lampard\n\n",argv[0],VERSION); printf("%s -q or --quickscroll Use volume control to scroll through the playlist\n",argv[0]); printf("%s -v or --version Show program version\n",argv[0]); printf("%s -h or --help This help text\n\n",argv[0]); exit(0); } if (!strncmp(argument, "-v",2) || !strncmp(argument, "--version",9)) { printf("%s version %s\n",argv[0],VERSION); exit(0); } } for(eventdev=0;eventdev<127;eventdev++) { snprintf(evdev_name,127,"/dev/input/event%i",eventdev); if ((mmedia_fd = open(evdev_name, O_NONBLOCK|O_RDONLY)) < 0) { // ignore errors when opening devices //printf("error opening interface %i\n",eventdev); } else { ioctl(mmedia_fd, EVIOCGNAME(sizeof(devname)), devname); printf("Device Name %s on %s\n", devname, evdev_name); if(0==strncmp(devname,"Logitech Logitech Gaming Keyboard",256)){ printf("Found device: \"%s\" on %s ", devname,evdev_name); break; } else if(0==strncmp(devname,"G15 Gaming Keyboard",256)){ printf("Found device: \"%s\" on %s ", devname,evdev_name); break; }else close(mmedia_fd); } } if (mmedia_fd) { // we assume that the next event device is the multimedia keys close(mmedia_fd); snprintf(evdev_name,127,"/dev/input/event%i",++eventdev); printf("and %s\n",evdev_name); if ((mmedia_fd = open(evdev_name, O_NONBLOCK|O_RDONLY)) < 0) { printf("error opening interface %i",eventdev); } }else { printf("Unable to find Keyboard via EVENT interface... is /dev/input/event[0-9] readable??\n"); } /* set correct hostname */ if(!hostname) { hostname = "localhost"; } if(port){ iport = atoi(port); } if((g15screen_fd = new_g15_screen(G15_G15RBUF))<0){ printf("Sorry, cant connect to the G15daemon\n"); return 1; } canvas = (g15canvas *) malloc (sizeof (g15canvas)); if (canvas != NULL) { memset(canvas->buffer, 0, G15_BUFFER_LEN); canvas->mode_cache = 0; canvas->mode_reverse = 0; canvas->mode_xor = 0; } /* Create mpd object */ obj = mpd_new(hostname, iport,password); /* Connect signals */ mpd_signal_connect_error(obj,(ErrorCallback)error_callback, NULL); mpd_signal_connect_status_changed(obj,(StatusChangedCallback)status_changed, NULL); /* Set timeout */ mpd_set_connection_timeout(obj, 10); if(0==mpd_connect(obj)) { char buffer[20]; pthread_attr_t attr; mpd_send_password(obj); memset(buffer, '\0', 20); pthread_attr_init(&attr); pthread_attr_setstacksize(&attr,32*1024); /* set stack to 64k - dont need 8Mb !! */ pthread_create(&Lkeys, &attr, Lkeys_thread, NULL); pthread_create(&EKeys, &attr,event_key_thread, NULL); pthread_create(&g15display, &attr, g15display_thread, NULL); do{ pthread_mutex_lock(&daemon_mutex); if(voltimeout) --voltimeout; if(mute){ volume_adjust = 0; mute = 0; if (muted_volume == 0) { //printf("mute\n"); muted_volume = mpd_status_get_volume(obj); mpd_status_set_volume (obj,0); } else { //printf("unmute\n"); if (mpd_status_get_volume(obj) == 0) { /* if no other client has set volume up */ mpd_status_set_volume (obj,muted_volume); } muted_volume = 0; } } if(volume_adjust != 0){ if (muted_volume != 0) { volume=muted_volume; } else { volume=mpd_status_get_volume(obj); } volume_new = volume + volume_adjust; volume_adjust = 0; if(volume_new < 0) volume_new = 0; if(volume_new > 100) volume_new = 100; if(volume != volume_new || muted_volume){ //printf("volume %d -> %d\n", volume, volume_new); mpd_status_set_volume (obj,volume_new); } voltimeout=100; muted_volume=0; } mpd_status_update(obj); pthread_mutex_unlock(&daemon_mutex); }while(!usleep(5000) && !leaving); leaving = 1; pthread_join(Lkeys,NULL); pthread_join(g15display,NULL); }else printf("Unable to connect to MPD server. Exiting\n"); if(obj) mpd_free(obj); close(fdstdin); if(canvas!=NULL) free(canvas); close(g15screen_fd); close(mmedia_fd); pthread_mutex_destroy(&lockit); return 1; }