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
|
#include <glib.h>
#include <signal.h>
#include <iostream>
#include <sstream>
#include <errno.h>
#include "imms.h"
#include "strmanip.h"
#include "utils.h"
#include "serverstub.h"
#include "socketserver.h"
#define INTERFACE_VERSION "2.0"
using std::cerr;
using std::cout;
using std::endl;
using std::stringstream;
Imms *imms;
gboolean do_events(void *unused)
{
if (imms)
imms->do_events();
return TRUE;
}
class ImmsDFilter : public SocketServer, public IMMSServer
{
public:
ImmsDFilter(const string &sockname) : SocketServer(sockname) {}
void write_command(const string &command)
{
SocketServer::write(command + "\n");
}
void connection_established()
{
if (!imms)
imms = new Imms(this);
}
void connection_lost()
{
delete imms;
imms = 0;
exit(0);
}
void check_playlist_item(int pos, const string &path)
{
string oldpath = imms->get_item_from_playlist(pos);
if (oldpath != "")
{
if (oldpath != path)
{
cerr << "IMMSd: playlist triggered refresh: "
<< oldpath << " != " << path << endl;
write_command("PlaylistChanged");
}
}
else
imms->playlist_insert_item(pos, path);
}
void process_line(const string &line)
{
stringstream sstr;
sstr << line;
string command = "";
sstr >> command;
#if defined(DEBUG) && 1
if (command != "Playlist")
std::cout << "> " << line << endl;
#endif
if (command == "Setup")
{
bool use_xidle;
sstr >> use_xidle;
imms->setup(use_xidle);
return;
}
if (command == "StartSong")
{
int pos;
sstr >> pos;
string path;
getline(sstr, path);
path = path_normalize(path);
check_playlist_item(pos, path);
imms->start_song(pos, path);
return;
}
if (command == "EndSong")
{
bool end, jumped, bad;
sstr >> end >> jumped >> bad;
imms->end_song(end, jumped, bad);
return;
}
if (command == "PlaylistItem")
{
int pos;
sstr >> pos;
string path;
getline(sstr, path);
path = path_normalize(path);
check_playlist_item(pos, path);
return;
}
if (command == "Playlist")
{
int pos;
sstr >> pos;
string path;
getline(sstr, path);
path = path_normalize(path);
imms->playlist_insert_item(pos, path);
return;
}
if (command == "PlaylistEnd")
{
imms->playlist_ready();
return;
}
if (command == "PlaylistChanged")
{
int length;
sstr >> length;
#ifdef DEBUG
cerr << "got playlist length = " << length << endl;
#endif
imms->playlist_changed(length);
write_command("GetEntirePlaylist");
return;
}
if (command == "SelectNext")
{
int pos = imms->select_next();
if (pos == -1)
{
write_command("TryAgain");
return;
}
write_command("EnqueueNext " + itos(pos));
return;
}
if (command == "Version")
{
write_command("Version " INTERFACE_VERSION);
return;
}
cerr << "IMMSd: Unknown command: " << command << endl;
}
};
GMainLoop *loop = 0;
void quit(int signum)
{
if (loop)
g_main_quit(loop);
loop = 0;
signal(signum, SIG_DFL);
}
int main(int argc, char **argv)
{
int r = mkdir(get_imms_root().c_str(), 0700);
if (r < 0 && errno != EEXIST)
{
cerr << "IMMSd: could not create directory "
<< get_imms_root() << ": " << strerror(errno) << endl;
exit(r);
}
StackLockFile lock(get_imms_root(".immsd_lock"));
if (!lock.isok())
{
cerr << "IMMSd: another instance already active - exiting." << endl;
exit(1);
}
loop = g_main_loop_new(NULL, FALSE);
signal(SIGINT, quit);
signal(SIGTERM, quit);
signal(SIGPIPE, SIG_IGN);
GSource* ts = g_timeout_source_new(500);
g_source_attach(ts, NULL);
g_source_set_callback(ts, (GSourceFunc)do_events, NULL, NULL);
ImmsDFilter filter(get_imms_root("socket"));
cout << "IMMSd version " << PACKAGE_VERSION << " ready..." << endl;
g_main_loop_run(loop);
return 0;
}
|