1 #include "xinemacontrol.h"
3 XinemaControl::XinemaControl():
4 playback_state(STOPPED),
7 current_audio_channel(-1),
8 current_spu_channel(-1)
10 QObject::connect(&socket, &QAbstractSocket::connected, this, &XinemaControl::connected);
11 QObject::connect(&socket, &QAbstractSocket::disconnected, this, &XinemaControl::disconnected);
12 QObject::connect(&socket, &QIODevice::readyRead, this, &XinemaControl::data_available);
15 void XinemaControl::connect(const QHostAddress &addr)
17 socket.connectToHost(addr, 34588);
20 bool XinemaControl::is_connected() const
22 return socket.state()==QAbstractSocket::ConnectedState;
25 void XinemaControl::list_directory(const QString &dir)
30 send_request("list_directory "+dir);
33 void XinemaControl::play_file(const QString &fn)
38 send_request("play_file "+fn);
41 void XinemaControl::play()
46 void XinemaControl::seek(float time)
48 send_request(QString("seek %1").arg(time));
51 void XinemaControl::pause()
53 send_request("pause");
56 void XinemaControl::stop()
61 void XinemaControl::select_audio_channel(int chan)
64 send_request("select_audio off");
66 send_request(QString("select_audio %1").arg(chan));
69 void XinemaControl::select_spu_channel(int chan)
72 send_request("select_spu off");
74 send_request(QString("select_spu %1").arg(chan));
77 void XinemaControl::send_request(const QString &req)
79 socket.write(req.toUtf8());
80 socket.write("\n", 1);
83 void XinemaControl::data_available()
85 while(socket.bytesAvailable())
88 int len = socket.read(rbuf, sizeof(rbuf));
92 buffer.append(rbuf, len);
98 int newline = buffer.indexOf('\n', start);
102 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
103 process_reply(reply);
108 buffer.remove(0, start);
111 void XinemaControl::process_reply(const QString &reply)
113 int space = reply.indexOf(' ');
114 QString keyword = reply.mid(0, space);
117 args = reply.mid(space+1);
119 if(keyword=="directory")
120 emit directory_started(args);
121 else if(keyword=="subdir")
122 emit subdirectory_added(args);
123 else if(keyword=="file")
124 emit file_added(args);
125 else if(keyword=="state")
128 playback_state = STOPPED;
129 else if(args=="PAUSED")
130 playback_state = PAUSED;
131 else if(args=="PLAYING")
132 playback_state = PLAYING;
133 emit playback_state_changed(playback_state);
135 else if(keyword=="title")
138 emit title_changed(title);
140 else if(keyword=="duration")
142 duration = args.toFloat();
143 emit duration_changed(duration);
145 else if(keyword=="position")
147 position = args.toFloat();
148 emit position_changed(position);
150 else if(keyword=="audio_count")
151 resize_list(audio_channels, args.toInt());
152 else if(keyword=="audio")
154 space = args.indexOf(' ');
155 audio_channels[args.mid(0, space).toInt()] = args.mid(space+1);
157 else if(keyword=="spu_count")
158 resize_list(spu_channels, args.toInt());
159 else if(keyword=="spu")
161 space = args.indexOf(' ');
162 spu_channels[args.mid(0, space).toInt()] = args.mid(space+1);
164 else if(keyword=="channels_end")
165 emit channels_changed();
166 else if(keyword=="current_audio")
168 current_audio_channel = convert_channel(args);
169 emit current_audio_channel_changed(current_audio_channel);
171 else if(keyword=="current_spu")
173 current_spu_channel = convert_channel(args);
174 emit current_spu_channel_changed(current_spu_channel);
178 int XinemaControl::convert_channel(const QString &arg)
186 void XinemaControl::resize_list(QStringList &list, int size)
188 while(list.size()>size)
190 while(list.size()<size)
191 list.append(QString());