File: lmc.c

package info (click to toggle)
mpdscribble 0.2.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 716 kB
  • ctags: 500
  • sloc: ansic: 3,904; sh: 3,603; makefile: 70
file content (205 lines) | stat: -rw-r--r-- 4,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
    This file is part of mpdscribble,
    another audioscrobbler plugin for music player daemon.
    Copyright © 2005 Kuno Woudt <kuno@frob.nl>

    mpdscribble is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    mpdscribble is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with mpdscribble; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lmc.h"
#include "misc.h"

static mpd_Connection *g_mpd = NULL;
mpd_InfoEntity *g_entity = NULL;

char *g_host;
int g_port;

static void
lmc_failure ()
{
  warning ("mpd error (%i): %s", g_mpd->errorCode, g_mpd->errorStr);
  mpd_closeConnection (g_mpd);
  g_mpd = 0;
}

static int
lmc_reconnect (void)
{
  char *at = strchr (g_host, '@');
  char *host = g_host;
  char *password = NULL;

  if (at)
    {
      host = at+1;
      password = strndup2 (g_host, at-g_host);
    }

  g_mpd = mpd_newConnection (host, g_port, 10);
  if (g_mpd->error)
    {
      lmc_failure ();
      return 0;
    }

  if (password)
    {
      notice ("sending password ... ");

      mpd_sendPasswordCommand(g_mpd, password);
      mpd_finishCommand(g_mpd);
      free (password);
    }

  if (g_mpd->error)
    {
      lmc_failure ();
      return 0;
    }

  notice ("connected to mpd %i.%i.%i at %s:%i.",
          g_mpd->version[0], g_mpd->version[1], g_mpd->version[2],
          host, g_port);

  return 1;
}

void
lmc_connect (char *host, int port)
{
  g_host = host;
  g_port = port;
  lmc_reconnect ();
}

void
lmc_disconnect (void)
{
  if (g_entity)
    mpd_freeInfoEntity (g_entity);

  mpd_closeConnection (g_mpd);
}

int
lmc_current (mpd_Song *songptr)
{
  mpd_Song *song;
  mpd_Status *status;
  int elapsed;

  if (!g_mpd)
    {
      warning ("waiting 15 seconds before reconnecting.");
      sleep (15);
      warning ("attempting to reconnect to mpd... ");
      lmc_reconnect ();
      return LMC_NOTPLAYING;
    }

  mpd_sendCommandListOkBegin(g_mpd);
  mpd_sendStatusCommand(g_mpd);
  mpd_sendCurrentSongCommand(g_mpd);
  mpd_sendCommandListEnd(g_mpd);

  status = mpd_getStatus (g_mpd);
  if (!status)
    {
      lmc_failure ();
      warning ("waiting 15 seconds before reconnecting.");
      sleep (15);
      warning ("attempting to reconnect to mpd... ");
      lmc_reconnect ();
      return LMC_NOTPLAYING;
    }

  if (status->error)
    {
      /* FIXME: clearing stuff doesn't seem to help, it keeps printing the
         same error over and over, so just let's just ignore these errors
         for now. */
      //      warning ("mpd status error: %s\n", status->error);
      //      mpd_executeCommand(g_mpd, "clearerror");
      mpd_clearError (g_mpd);
    }

  if (status->state == MPD_STATUS_STATE_PAUSE)
    {
      elapsed = LMC_PAUSED;
    }
  else if (status->state != MPD_STATUS_STATE_PLAY)
    {
      elapsed = LMC_NOTPLAYING;
    }
  else
    {
      elapsed = status->elapsedTime;

      if (g_mpd->error)
        {
          lmc_failure ();
          return 0;
        }

      mpd_nextListOkCommand(g_mpd);

      if (g_entity)
        {
          mpd_freeInfoEntity (g_entity);
          g_entity = NULL;
        }

      while ((g_entity = mpd_getNextInfoEntity (g_mpd))
             && (g_entity->type != MPD_INFO_ENTITY_TYPE_SONG))
        {
          mpd_freeInfoEntity (g_entity);
          g_entity = NULL;
        }

      if (!g_entity)
        {
          elapsed = -1;
        }
      else
        {
          song = g_entity->info.song;
          memcpy (songptr, song, sizeof (mpd_Song));

          if (g_mpd->error)
            {
              lmc_failure ();
              return 0;
            }
        }
    }

  mpd_finishCommand(g_mpd);
  if (g_mpd->error)
    {
      lmc_failure ();
      return 0;
    }

  mpd_freeStatus(status);

  return elapsed;
}