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(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; }
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; }
int main(int argc, char **argv) { mpd_context_t ctx; mpd_t *a, *b; mpd_t *q, *r; char *qs, *rs; char status_str[MPD_MAX_FLAG_STRING]; clock_t start_clock, end_clock; if (argc != 3) { fprintf(stderr, "divmod: usage: ./divmod x y\n"); exit(1); } mpd_init(&ctx, 38); ctx.traps = 0; q = mpd_new(&ctx); r = mpd_new(&ctx); a = mpd_new(&ctx); b = mpd_new(&ctx); mpd_set_string(a, argv[1], &ctx); mpd_set_string(b, argv[2], &ctx); start_clock = clock(); mpd_divmod(q, r, a, b, &ctx); end_clock = clock(); fprintf(stderr, "time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC); qs = mpd_to_sci(q, 1); rs = mpd_to_sci(r, 1); mpd_snprint_flags(status_str, MPD_MAX_FLAG_STRING, ctx.status); printf("%s %s %s\n", qs, rs, status_str); mpd_del(q); mpd_del(r); mpd_del(a); mpd_del(b); mpd_free(qs); mpd_free(rs); return 0; }
int main(int argc, char **argv) { mpd_context_t ctx; mpd_t *a, *b; mpd_t *result; char *rstring; char status_str[MPD_MAX_FLAG_STRING]; clock_t start_clock, end_clock; if (argc != 3) { fprintf(stderr, "shift: usage: ./shift x y\n"); exit(1); } mpd_init(&ctx, 38); ctx.traps = 0; result = mpd_new(&ctx); a = mpd_new(&ctx); b = mpd_new(&ctx); mpd_set_string(a, argv[1], &ctx); mpd_set_string(b, argv[2], &ctx); start_clock = clock(); mpd_shift(result, a, b, &ctx); end_clock = clock(); fprintf(stderr, "time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC); rstring = mpd_to_sci(result, 1); mpd_snprint_flags(status_str, MPD_MAX_FLAG_STRING, ctx.status); printf("%s %s\n", rstring, status_str); mpd_del(a); mpd_del(b); mpd_del(result); mpd_free(rstring); return 0; }
/* * Example from: http://en.wikipedia.org/wiki/Mandelbrot_set * * Escape time algorithm for drawing the set: * * Point x0, y0 is deemed to be in the Mandelbrot set if the return * value is maxiter. Lower return values indicate how quickly points * escaped and can be used for coloring. */ int color_point(mpd_t *x0, mpd_t *y0, int maxiter, mpd_context_t *ctx) { mpd_t *x, *y, *sq_x, *sq_y; mpd_t *two, *four, *c; int i; x = mpd_new(ctx); y = mpd_new(ctx); mpd_set_u32(x, 0, ctx); mpd_set_u32(y, 0, ctx); sq_x = mpd_new(ctx); sq_y = mpd_new(ctx); mpd_set_u32(sq_x, 0, ctx); mpd_set_u32(sq_y, 0, ctx); two = mpd_new(ctx); four = mpd_new(ctx); mpd_set_u32(two, 2, ctx); mpd_set_u32(four, 4, ctx); c = mpd_new(ctx); mpd_set_u32(c, 0, ctx); for (i = 0; i < maxiter && mpd_cmp(c, four, ctx) <= 0; i++) { mpd_mul(y, x, y, ctx); mpd_mul(y, y, two, ctx); mpd_add(y, y, y0, ctx); mpd_sub(x, sq_x, sq_y, ctx); mpd_add(x, x, x0, ctx); mpd_mul(sq_x, x, x, ctx); mpd_mul(sq_y, y, y, ctx); mpd_add(c, sq_x, sq_y, ctx); } mpd_del(x); mpd_del(y); mpd_del(sq_x); mpd_del(sq_y); mpd_del(two); mpd_del(four); mpd_del(c); return i; }
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; }
int update_user_balance(bool real, uint32_t user_id, const char *asset, const char *business, uint64_t business_id, mpd_t *change, json_t *detail) { struct update_key key; key.user_id = user_id; strncpy(key.asset, asset, sizeof(key.asset)); strncpy(key.business, business, sizeof(key.business)); key.business_id = business_id; dict_entry *entry = dict_find(dict_update, &key); if (entry) { return -1; } mpd_t *result; mpd_t *abs_change = mpd_new(&mpd_ctx); mpd_abs(abs_change, change, &mpd_ctx); if (mpd_cmp(change, mpd_zero, &mpd_ctx) >= 0) { result = balance_add(user_id, BALANCE_TYPE_AVAILABLE, asset, abs_change); } else { result = balance_sub(user_id, BALANCE_TYPE_AVAILABLE, asset, abs_change); } mpd_del(abs_change); if (result == NULL) return -2; struct update_val val = { .create_time = current_timestamp() }; dict_add(dict_update, &key, &val); if (real) { double now = current_timestamp(); json_object_set_new(detail, "id", json_integer(business_id)); char *detail_str = json_dumps(detail, 0); append_user_balance_history(now, user_id, asset, business, change, detail_str); free(detail_str); push_balance_message(now, user_id, asset, business, change); } return 0; }
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; }
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; }
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) { mpd_context_t ctx; mpd_t *x0, *y0; mpd_t *sqrt_2, *xstep, *ystep; uint32_t prec = 19; int iter = 1000; int points[40][80]; int i, j; clock_t start_clock, end_clock; if (argc != 3) { fprintf(stderr, "usage: ./bench prec iter\n"); exit(1); } prec = strtoul(argv[1], NULL, 10); iter = strtol(argv[2], NULL, 10); mpd_init(&ctx, prec); /* no more MPD_MINALLOC changes after here */ sqrt_2 = mpd_new(&ctx); xstep = mpd_new(&ctx); ystep = mpd_new(&ctx); x0 = mpd_new(&ctx); y0 = mpd_new(&ctx); mpd_set_u32(sqrt_2, 2, &ctx); mpd_sqrt(sqrt_2, sqrt_2, &ctx); mpd_div_u32(xstep, sqrt_2, 40, &ctx); mpd_div_u32(ystep, sqrt_2, 20, &ctx); start_clock = clock(); mpd_copy(y0, sqrt_2, &ctx); for (i = 0; i < 40; i++) { mpd_copy(x0, sqrt_2, &ctx); mpd_set_negative(x0); for (j = 0; j < 80; j++) { points[i][j] = color_point(x0, y0, iter, &ctx); mpd_add(x0, x0, xstep, &ctx); } mpd_sub(y0, y0, ystep, &ctx); } end_clock = clock(); #ifdef BENCH_VERBOSE for (i = 0; i < 40; i++) { for (j = 0; j < 80; j++) { if (points[i][j] == iter) { putchar('*'); } else if (points[i][j] >= 10) { putchar('+'); } else if (points[i][j] >= 5) { putchar('.'); } else { putchar(' '); } } putchar('\n'); } putchar('\n'); #endif printf("time: %f\n\n", (double)(end_clock-start_clock)/(double)CLOCKS_PER_SEC); mpd_del(x0); mpd_del(y0); mpd_del(sqrt_2); mpd_del(xstep); mpd_del(ystep); return 0; }