~ubuntu-branches/ubuntu/precise/xfce4-mpc-plugin/precise

« back to all changes in this revision

Viewing changes to panel-plugin/simple-libmpd.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2007-06-02 15:17:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070602151746-ea7jev5y5fu8wfwx
Tags: 0.3.2-1ubuntu1
* Merge with Debian Unstable, remaining Ubuntu changes:
  - Uses xfce.mk
  - Build Depends on libmpd-dev. This fixes a crash when no running mpd can
    be found
  - Adhere to DebianMaintainerField.

Show diffs side-by-side

added added

removed removed

Lines of Context:
125
125
   FD_SET(mo->socket,&fds);
126
126
   if((err = select(mo->socket+1,&fds,NULL,NULL,&tv)) == 1)
127
127
   {
128
 
      if ((nbread = recv(mo->socket, mo->recv_buffer, MAXBUFLEN, 0)) < 0)
 
128
      if ((nbread = recv(mo->socket, mo->buffer, MAXBUFLEN, 0)) < 0)
129
129
      {
130
130
         mo->error = MPD_ERROR_NORESPONSE;
131
131
         DBG("ERROR @recv(), err=%s",strerror(errno));
138
138
          return;
139
139
      }
140
140
      mo->buflen = nbread;
141
 
      strncpy(mo->buffer, mo->recv_buffer, mo->buflen);
142
141
      mo->buffer[mo->buflen] = '\0';
143
142
   }
144
143
   else if(err < 0)
196
195
   FD_SET(mo->socket,&fds);
197
196
   if((err = select(mo->socket+1,&fds,NULL,NULL,&tv)) == 1)
198
197
   {
199
 
      if ((nbread = recv(mo->socket, mo->recv_buffer, MAXBUFLEN, 0)) < 0)
 
198
      if ((nbread = recv(mo->socket, mo->buffer, MAXBUFLEN, 0)) < 0)
200
199
      {
201
200
         mo->error = MPD_ERROR_NORESPONSE;
202
201
         DBG("ERROR @recv(), err=%s",strerror(errno));
209
208
         return -1;
210
209
      }
211
210
 
212
 
      DBG("Read %d bytes, buff=\"%s\"", nbread, mo->recv_buffer);
213
211
      mo->buflen = nbread;
214
 
      strncpy(mo->buffer, mo->recv_buffer, mo->buflen);
215
212
      mo->buffer[mo->buflen] = '\0';
 
213
      DBG("Read %d bytes, buff=\"%s\"", nbread, mo->buffer);
216
214
   }
217
215
   else if(err < 0)
218
216
   {
330
328
 
331
329
void parse_status_answer(MpdObj *mo, void* unused_param)
332
330
{
333
 
   char *eol,*ptr;
334
 
   char key[15], value[200];
335
 
 
336
 
   while (strcmp(mo->buffer,"OK\n"))
 
331
   gchar **lines, **tokens;
 
332
   int i;
 
333
   mo->songid = -1;
 
334
   lines = g_strsplit(mo->buffer, "\n", 0);
 
335
   for (i = 0 ; lines[i] && strcmp(lines[i], "OK") ; i++)
337
336
   {
338
 
      /*HACK @#!*/
339
 
      ptr = strstr(mo->buffer, ":");
340
 
      eol = strstr(mo->buffer, "\n");
341
 
      strncpy(key, mo->buffer, ptr - mo->buffer);
342
 
      key[ptr - mo->buffer]='\0';
343
 
      strncpy(value,ptr + 2 , eol - ptr - 2);
344
 
      value[eol - ptr - 2]='\0';
345
 
 
346
 
      DBG("key=\"%s\",value=\"%s\"", key, value);
347
 
      if      (0 == strcmp("volume",key)) mo->curvol = atoi(value);
348
 
      else if (0 == strcmp("repeat",key)) mo->repeat = atoi(value);
349
 
      else if (0 == strcmp("random",key)) mo->random = atoi(value);
350
 
      else if (0 == strcmp("playlistlength",key)) mo->playlistlength = atoi(value);
351
 
      else if (0 == strcmp("state", key))
 
337
      tokens = g_strsplit(lines[i], ":", 2);
 
338
      /* remove leading whitespace */
 
339
      tokens[1] = g_strchug(tokens[1]);
 
340
      DBG("key=\"%s\",value=\"%s\"", tokens[0], tokens[1]);
 
341
      if      (0 == strcmp("volume",tokens[0])) mo->curvol = atoi(tokens[1]);
 
342
      else if (0 == strcmp("repeat",tokens[0])) mo->repeat = atoi(tokens[1]);
 
343
      else if (0 == strcmp("random",tokens[0])) mo->random = atoi(tokens[1]);
 
344
      else if (0 == strcmp("playlistlength",tokens[0])) mo->playlistlength = atoi(tokens[1]);
 
345
      else if (0 == strcmp("state", tokens[0]))
352
346
      {
353
 
         if      (0 == strcmp("play", value)) mo->status = MPD_PLAYER_PLAY;
354
 
         else if (0 == strcmp("pause",value)) mo->status = MPD_PLAYER_PAUSE;
355
 
         else if (0 == strcmp("stop", value)) mo->status = MPD_PLAYER_STOP;
 
347
         if      (0 == strcmp("play", tokens[1])) mo->status = MPD_PLAYER_PLAY;
 
348
         else if (0 == strcmp("pause",tokens[1])) mo->status = MPD_PLAYER_PAUSE;
 
349
         else if (0 == strcmp("stop", tokens[1])) mo->status = MPD_PLAYER_STOP;
356
350
      }
357
 
      else if (0 == strcmp("song",key)) mo->song = atoi(value);
358
 
      else if (0 == strcmp("songid",key)) mo->songid = atoi(value);
359
 
 
360
 
      *eol = '\0';
361
 
      strcpy(mo->buffer, eol+1); /*overlap ! */
362
 
      mo->buflen = strlen(mo->buffer);
 
351
      else if (0 == strcmp("song",tokens[0])) mo->song = atoi(tokens[1]);
 
352
      else if (0 == strcmp("songid",tokens[0])) mo->songid = atoi(tokens[1]);
 
353
      g_strfreev(tokens);
363
354
   }
 
355
   g_strfreev(lines);
364
356
}
365
357
 
366
358
int mpd_status_update(MpdObj* mo)
375
367
void parse_one_song(MpdObj *mo, void* param)
376
368
{
377
369
   mpd_Song* ms = (mpd_Song*) param;
378
 
   char *eol,*ptr;
379
 
   char key[15], value[200];
 
370
   gchar **lines, **tokens;
 
371
   int i;
380
372
   ms->artist = ms->album = ms->title = ms->track = NULL;
381
 
   ms->id = ms->pos = 0;
 
373
   ms->id = ms->pos = -1;
382
374
 
383
 
   while (!ms->id)
 
375
   lines = g_strsplit(mo->buffer, "\n", 0);
 
376
   for (i = 0 ; lines[i] && strcmp(lines[i], "OK") ; i++)
384
377
   {
385
 
      /*HACK @#!*/
386
 
      ptr = strstr(mo->buffer, ":");
387
 
      eol = strstr(mo->buffer, "\n");
388
 
      strncpy(key, mo->buffer, ptr - mo->buffer);
389
 
      key[ptr - mo->buffer]='\0';
390
 
      strncpy(value,ptr + 2 , eol - ptr - 2);
391
 
      value[eol - ptr - 2]='\0';
392
 
 
393
 
      DBG("key=\"%s\",value=\"%s\"", key, value);
394
 
      if      (!ms->artist && 0 == strcmp("Artist",key)) ms->artist= strdup(value);
395
 
      else if (!ms->album  && 0 == strcmp("Album", key)) ms->album = strdup(value);
396
 
      else if (!ms->title  && 0 == strcmp("Title", key)) ms->title = strdup(value);
397
 
      else if (!ms->track  && 0 == strcmp("Track", key)) ms->track = strdup(value);
398
 
      else if (!ms->pos    && 0 == strcmp("Pos",   key)) ms->pos   = atoi(value);
399
 
      else if (!ms->id     && 0 == strcmp("Id",    key)) ms->id    = atoi(value);
400
 
      *eol = '\0';
401
 
      strcpy(mo->buffer, eol+1); /* overlap ! */
402
 
      mo->buflen = strlen(mo->buffer);
 
378
      tokens = g_strsplit(lines[i], ":", 2);
 
379
      /* remove leading whitespace */
 
380
      tokens[1] = g_strchug(tokens[1]);
 
381
      DBG("key=\"%s\",value=\"%s\"", tokens[0], tokens[1]);
 
382
      if      (!ms->artist && 0 == strcmp("Artist",tokens[0])) ms->artist= g_strdup(tokens[1]);
 
383
      else if (!ms->album  && 0 == strcmp("Album", tokens[0])) ms->album = g_strdup(tokens[1]);
 
384
      else if (!ms->title  && 0 == strcmp("Title", tokens[0])) ms->title = g_strdup(tokens[1]);
 
385
      else if (!ms->track  && 0 == strcmp("Track", tokens[0])) ms->track = g_strdup(tokens[1]);
 
386
      else if (ms->pos < 0 && 0 == strcmp("Pos",   tokens[0])) ms->pos   = atoi(tokens[1]);
 
387
      else if (ms->id < 0  && 0 == strcmp("Id",    tokens[0])) ms->id    = atoi(tokens[1]);
 
388
      g_strfreev(tokens);
403
389
   }
 
390
   if (ms->id < 0)
 
391
      mo->error = MPD_FAILED;
 
392
   g_strfreev(lines);
404
393
}
405
394
 
406
395
void parse_playlistinfo_answer(MpdObj *mo, void *param)
407
396
{
408
397
   MpdData* md = (MpdData*) param;
409
 
   while (strcmp(mo->buffer,"OK\n"))
 
398
   mpd_Song* ms;
 
399
   gchar **lines, **tokens;
 
400
   int i = 0;
 
401
 
 
402
   lines = g_strsplit(mo->buffer, "\n", 0);
 
403
   while(lines[i] && strcmp(lines[i],"OK"))
410
404
   {
 
405
      ms = &md->allsongs[md->nb];
 
406
      ms->artist = ms->album = ms->title = ms->track = NULL;
 
407
      ms->id = ms->pos = -1;
411
408
      DBG("Going to parse song #%d", md->nb);
412
 
      parse_one_song(mo, (void*) &(md->allsongs[md->nb]));
 
409
 
 
410
      while(lines[i] && ms->id < 0)
 
411
      {
 
412
         tokens = g_strsplit(lines[i], ":", 2);
 
413
         /* remove leading whitespace */
 
414
         tokens[1] = g_strchug(tokens[1]);
 
415
         DBG("key=\"%s\",value=\"%s\"", tokens[0], tokens[1]);
 
416
         if      (!ms->artist && 0 == strcmp("Artist",tokens[0])) ms->artist= g_strdup(tokens[1]);
 
417
         else if (!ms->album  && 0 == strcmp("Album", tokens[0])) ms->album = g_strdup(tokens[1]);
 
418
         else if (!ms->title  && 0 == strcmp("Title", tokens[0])) ms->title = g_strdup(tokens[1]);
 
419
         else if (!ms->track  && 0 == strcmp("Track", tokens[0])) ms->track = g_strdup(tokens[1]);
 
420
         else if (ms->pos < 0 && 0 == strcmp("Pos",   tokens[0])) ms->pos   = atoi(tokens[1]);
 
421
         else if (ms->id < 0  && 0 == strcmp("Id",    tokens[0])) ms->id    = atoi(tokens[1]);
 
422
         i++;
 
423
         g_strfreev(tokens);
 
424
      }
413
425
      md->nb++;
414
426
   }
 
427
   g_strfreev(lines);
415
428
   DBG("Got 'OK', md->nb = %d", md->nb);
416
429
}
417
430