]> git.tdb.fi Git - xinema.git/blob - remote/source/xinemacontrol.cpp
eb2ec47e5706b0a67016491cedde811468c31ede
[xinema.git] / remote / source / xinemacontrol.cpp
1 #include "xinemacontrol.h"
2
3 XinemaControl::XinemaControl():
4         playback_state(STOPPED),
5         duration(0.0f),
6         position(0.0f),
7         current_audio_channel(-1),
8         current_spu_channel(-1)
9 {
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);
13 }
14
15 void XinemaControl::connect(const QHostAddress &addr)
16 {
17         socket.connectToHost(addr, 34588);
18 }
19
20 bool XinemaControl::is_connected() const
21 {
22         return socket.state()==QAbstractSocket::ConnectedState;
23 }
24
25 void XinemaControl::list_directory(const QString &dir)
26 {
27         if(!is_connected())
28                 return;
29
30         send_request("list_directory "+dir);
31 }
32
33 void XinemaControl::play_file(const QString &fn)
34 {
35         if(!is_connected())
36                 return;
37
38         send_request("play_file "+fn);
39 }
40
41 void XinemaControl::play()
42 {
43         send_request("play");
44 }
45
46 void XinemaControl::seek(float time)
47 {
48         send_request(QString("seek %1").arg(time));
49 }
50
51 void XinemaControl::pause()
52 {
53         send_request("pause");
54 }
55
56 void XinemaControl::stop()
57 {
58         send_request("stop");
59 }
60
61 void XinemaControl::select_audio_channel(int chan)
62 {
63         if(chan<0)
64                 send_request("select_audio off");
65         else
66                 send_request(QString("select_audio %1").arg(chan));
67 }
68
69 void XinemaControl::select_spu_channel(int chan)
70 {
71         if(chan<0)
72                 send_request("select_spu off");
73         else
74                 send_request(QString("select_spu %1").arg(chan));
75 }
76
77 void XinemaControl::send_request(const QString &req)
78 {
79         socket.write(req.toUtf8());
80         socket.write("\n", 1);
81 }
82
83 void XinemaControl::data_available()
84 {
85         while(socket.bytesAvailable())
86         {
87                 char rbuf[1024];
88                 int len = socket.read(rbuf, sizeof(rbuf));
89                 if(len<0)
90                         break;
91
92                 buffer.append(rbuf, len);
93         }
94
95         unsigned start = 0;
96         while(1)
97         {
98                 int newline = buffer.indexOf('\n', start);
99                 if(newline<0)
100                         break;
101
102                 QString reply = QString::fromUtf8(buffer.mid(start, newline-start));
103                 process_reply(reply);
104
105                 start = newline+1;
106         }
107
108         buffer.remove(0, start);
109 }
110
111 void XinemaControl::process_reply(const QString &reply)
112 {
113         int space = reply.indexOf(' ');
114         QString keyword = reply.mid(0, space);
115         QString args;
116         if(space>=0)
117                 args = reply.mid(space+1);
118
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")
126         {
127                 if(args=="STOPPED")
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);
134         }
135         else if(keyword=="title")
136         {
137                 title = args;
138                 emit title_changed(title);
139         }
140         else if(keyword=="duration")
141         {
142                 duration = args.toFloat();
143                 emit duration_changed(duration);
144         }
145         else if(keyword=="position")
146         {
147                 position = args.toFloat();
148                 emit position_changed(position);
149         }
150         else if(keyword=="audio_count")
151                 resize_list(audio_channels, args.toInt());
152         else if(keyword=="audio")
153         {
154                 space = args.indexOf(' ');
155                 audio_channels[args.mid(0, space).toInt()] = args.mid(space+1);
156         }
157         else if(keyword=="spu_count")
158                 resize_list(spu_channels, args.toInt());
159         else if(keyword=="spu")
160         {
161                 space = args.indexOf(' ');
162                 spu_channels[args.mid(0, space).toInt()] = args.mid(space+1);
163         }
164         else if(keyword=="channels_end")
165                 emit channels_changed();
166         else if(keyword=="current_audio")
167         {
168                 current_audio_channel = convert_channel(args);
169                 emit current_audio_channel_changed(current_audio_channel);
170         }
171         else if(keyword=="current_spu")
172         {
173                 current_spu_channel = convert_channel(args);
174                 emit current_spu_channel_changed(current_spu_channel);
175         }
176 }
177
178 int XinemaControl::convert_channel(const QString &arg)
179 {
180         if(arg=="off")
181                 return OFF;
182         else
183                 return arg.toInt();
184 }
185
186 void XinemaControl::resize_list(QStringList &list, int size)
187 {
188         while(list.size()>size)
189                 list.removeLast();
190         while(list.size()<size)
191                 list.append(QString());
192 }