Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
Arquivo: mpdnp.c Projeto: Zariel/mpdnp
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;
}
Exemplo n.º 3
0
void mpd_async_request(MpdAsyncCallback * callback, gpointer callback_data, MpdAsyncFunction * function,
					   gpointer function_data)
{
	gchar *string = NULL;
	GSimpleAsyncResult *res = NULL;
	MpdAsyncData *data = g_new0(MpdAsyncData, 1);
	data->mi = mpd_new_default();
	data->function = function;
	data->function_data = function_data;
	data->callback = callback;
	data->callback_data = callback_data;
	/**
     * Set Hostname
     */
	string = connection_get_hostname();
	mpd_set_hostname(data->mi, string);
	/**
	 * Set port
	 */
	mpd_set_port(data->mi, connection_get_port());
	/**
	 * Timeout
	 */
	mpd_set_connection_timeout(data->mi,
							   cfg_get_single_value_as_float_with_default(config, "connection", "timeout",
																		  DEFAULT_TIMEOUT));

	if (connection_use_auth())
	{
		string = connection_get_password();
		mpd_set_password(data->mi, string);
	} else
	{
		mpd_set_password(data->mi, "");
	}

	res = g_simple_async_result_new(NULL, __mpd_async_simple_callback, NULL, mpd_async_request);
	g_simple_async_result_set_op_res_gpointer(G_SIMPLE_ASYNC_RESULT(res), data, __mpd_async_result_free_data);
	g_log(LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Start async thread %p\n", data);
	g_simple_async_result_run_in_thread(res, __async_handler_function, G_PRIORITY_LOW, NULL);
}
Exemplo n.º 4
0
/* -------------------------------------------------------------------------- */
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;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
0
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;
}