1 #include "xinemacontrol.h"
3 XinemaControl::XinemaControl():
4 playback_state(STOPPED),
8 current_audio_channel(-1),
9 current_spu_channel(-1)
11 QObject::connect(&socket, &QAbstractSocket::connected, this, &XinemaControl::connected);
12 QObject::connect(&socket, &QAbstractSocket::disconnected, this, &XinemaControl::disconnected);
13 QObject::connect(&socket, &QIODevice::readyRead, this, &XinemaControl::data_available);
16 void XinemaControl::connect(const QHostAddress &addr)
18 socket.connectToHost(addr, 34588);
21 bool XinemaControl::is_connected() const
23 return socket.state()==QAbstractSocket::ConnectedState;
26 void XinemaControl::list_directory(const QString &dir)
31 send_request("list_directory "+dir);
34 void XinemaControl::play_file(const QString &fn)
39 send_request("play_file "+fn);
42 void XinemaControl::play()
47 void XinemaControl::seek(float time)
49 send_request(QString("seek %1").arg(time));
52 void XinemaControl::pause()
54 send_request("pause");
57 void XinemaControl::stop()
62 void XinemaControl::select_audio_channel(int chan)
65 send_request("select_audio off");
67 send_request(QString("select_audio %1").arg(chan));
70 void XinemaControl::select_spu_channel(int chan)
73 send_request("select_spu off");
75 send_request(QString("select_spu %1").arg(chan));
78 void XinemaControl::send_request(const QString &req)
80 socket.write(req.toUtf8());
81 socket.write("\n", 1);
84 void XinemaControl::data_available()
86 while(socket.bytesAvailable())
89 int len = socket.read(rbuf, sizeof(rbuf));
93 buffer.append(rbuf, len);
99 int newline = buffer.indexOf('\n', start);
103 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
104 process_reply(reply);
109 buffer.remove(0, start);
112 void XinemaControl::process_reply(const QString &reply)
114 int space = reply.indexOf(' ');
115 QString keyword = reply.mid(0, space);
118 args = reply.mid(space+1);
120 if(keyword=="directory")
121 emit directory_started(args);
122 else if(keyword=="subdir")
123 emit subdirectory_added(args);
124 else if(keyword=="file")
125 emit file_added(args);
126 else if(keyword=="state")
129 playback_state = STOPPED;
130 else if(args=="PAUSED")
131 playback_state = PAUSED;
132 else if(args=="PLAYING")
133 playback_state = PLAYING;
134 emit playback_state_changed(playback_state);
136 else if(keyword=="title")
139 emit title_changed(title);
141 else if(keyword=="duration")
143 duration = args.toFloat();
144 emit duration_changed(duration);
146 else if(keyword=="position")
148 position = args.toFloat();
149 emit position_changed(position);
151 else if(keyword=="video_size")
153 space = args.indexOf(' ');
154 video_size = QSize(args.mid(0, space).toInt(), args.mid(space+1).toInt());
155 emit size_changed(video_size);
157 else if(keyword=="framerate")
159 framerate = args.toFloat();
160 emit framerate_changed(framerate);
162 else if(keyword=="video_codec")
165 emit codecs_changed(video_codec, audio_codec);
167 else if(keyword=="audio_codec")
170 emit codecs_changed(video_codec, audio_codec);
172 else if(keyword=="audio_count")
173 resize_list(audio_channels, args.toInt());
174 else if(keyword=="audio")
176 space = args.indexOf(' ');
177 audio_channels[args.mid(0, space).toInt()] = args.mid(space+1);
179 else if(keyword=="spu_count")
180 resize_list(spu_channels, args.toInt());
181 else if(keyword=="spu")
183 space = args.indexOf(' ');
184 spu_channels[args.mid(0, space).toInt()] = args.mid(space+1);
186 else if(keyword=="channels_end")
187 emit channels_changed();
188 else if(keyword=="current_audio")
190 current_audio_channel = convert_channel(args);
191 emit current_audio_channel_changed(current_audio_channel);
193 else if(keyword=="current_spu")
195 current_spu_channel = convert_channel(args);
196 emit current_spu_channel_changed(current_spu_channel);
200 int XinemaControl::convert_channel(const QString &arg)
208 void XinemaControl::resize_list(QStringList &list, int size)
210 while(list.size()>size)
212 while(list.size()<size)
213 list.append(QString());