added esc, context menu and gui switch buttons. resampled icons to 84x84.
[simple-xmbc-rem] / src / xbmc.cpp
1 #include "xbmc.h"
2 #include "constants.h"
3 #include "genericnotify.h"
4
5 #include <QSettings>
6 #include <QTextStream>
7 #include <QNetworkReply>
8
9 // XBMC constants -- from https://github.com/xbmc/xbmc/blob/master/xbmc/guilib/Key.h
10 #define ACTION_MOVE_LEFT                1
11 #define ACTION_MOVE_RIGHT               2
12 #define ACTION_MOVE_UP                  3
13 #define ACTION_MOVE_DOWN                4
14 #define ACTION_SELECT_ITEM              7
15 #define ACTION_HIGHLIGHT_ITEM           8
16 #define ACTION_PARENT_DIR               9
17 #define ACTION_PAUSE                    12
18 #define ACTION_STOP                     13
19 #define ACTION_NEXT_ITEM                14
20 #define ACTION_PREV_ITEM                15
21 #define ACTION_FORWARD                  16 // Can be used to specify specific action in a window, Playback control is handled in ACTION_PLAYER_*
22 #define ACTION_REWIND                   17 // Can be used to specify specific action in a window, Playback control is handled in ACTION_PLAYER_*
23 #define ACTION_SHOW_GUI                 18 // toggle between GUI and movie or GUI and visualisation.
24 #define ACTION_STEP_FORWARD             20 // seek +1% in the movie. Can b used in videoFullScreen.xml window id=2005
25 #define ACTION_STEP_BACK                21 // seek -1% in the movie. Can b used in videoFullScreen.xml window id=2005
26 #define ACTION_BIG_STEP_FORWARD         22 // seek +10% in the movie. Can b used in videoFullScreen.xml window id=2005
27 #define ACTION_BIG_STEP_BACK            23 // seek -10% in the movie. Can b used in videoFullScreen.xml window id=2005
28 #define ACTION_NEXT_SUBTITLE            26 // switch to next subtitle of movie. Can b used in videoFullScreen.xml window id=2005
29 #define ACTION_SUBTITLE_DELAY_MIN       52 // Decrease subtitle/movie Delay. Can b used in videoFullScreen.xml window id=2005
30 #define ACTION_SUBTITLE_DELAY_PLUS      53 // Increase subtitle/movie Delay. Can b used in videoFullScreen.xml window id=2005
31 #define ACTION_AUDIO_DELAY_MIN          54 // Increase avsync delay. Can b used in videoFullScreen.xml window id=2005
32 #define ACTION_AUDIO_DELAY_PLUS         55 // Decrease avsync delay. Can b used in videoFullScreen.xml window id=2005
33 #define ACTION_AUDIO_NEXT_LANGUAGE      56 // Select next language in movie. Can b used in videoFullScreen.xml window id=2005
34 #define ACTION_PLAYER_PLAY              79 // Play current song. Unpauses song and sets playspeed to 1x. global action, can be used anywhere
35 #define ACTION_VOLUME_UP                88
36 #define ACTION_VOLUME_DOWN              89
37 #define ACTION_MUTE                     91
38 #define ACTION_CONTEXT_MENU             117 // pops up the context menu
39 #define ACTION_ENTER                    135
40
41 // 0xF000 -> 0xF200 is reserved for the keyboard; a keyboard press is either
42 #define KEY_VKEY 0xF000 // a virtual key/functional key e.g. cursor left
43 #define KEY_ASCII 0xF100 // a printable character in the range of TRUE ASCII (from 0 to 127) // FIXME make it clean and pure unicode! remove the need for KEY_ASCII
44 #define KEY_UNICODE 0xF200 // another printable character whose range is not included in this KEY code
45
46 Xbmc::Xbmc(QObject *parent) : QObject(parent)
47 {
48     m_manager = new QNetworkAccessManager(this);
49 }
50
51 Xbmc::~Xbmc()
52 {
53     delete m_manager;
54 }
55
56 void Xbmc::commandActionFinished()
57 {
58     QNetworkReply* reply = qobject_cast<QNetworkReply *>(sender());
59     if (reply) {
60         if (reply->error() == QNetworkReply::NoError) {
61             QTextStream stream(reply);
62             QString msg = stream.readAll();
63             qDebug("Xbmc::commandActionFinished: %s", qPrintable(msg));
64         } else {
65             notify::notify(reply->errorString());
66         }
67         reply->deleteLater();
68     }
69 }
70
71 void Xbmc::do_command_action(int action)
72 {
73     QSettings settings;
74     QString server = settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString();
75     QString port = settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toString();
76
77     QUrl url = QUrl(QString("http://%1:%2/xbmcCmds/xbmcHttp?command=Action(%3)").arg(server).arg(port).arg(action));
78
79     QNetworkRequest request;
80     request.setUrl(url);
81
82     QNetworkReply *reply = m_manager->get(request);
83     connect(reply, SIGNAL(finished()), this, SLOT(commandActionFinished()));
84 }
85
86 void Xbmc::actionRight()
87 {
88     do_command_action(ACTION_MOVE_RIGHT);
89     do_command_action(ACTION_STEP_FORWARD);
90 }
91
92 void Xbmc::actionLeft()
93 {
94     do_command_action(ACTION_MOVE_LEFT);
95     do_command_action(ACTION_STEP_BACK);
96 }
97
98 void Xbmc::actionUp()
99 {
100     do_command_action(ACTION_MOVE_UP);
101     do_command_action(ACTION_BIG_STEP_FORWARD);
102 }
103
104 void Xbmc::actionDown()
105 {
106     do_command_action(ACTION_MOVE_DOWN);
107     do_command_action(ACTION_BIG_STEP_BACK);
108 }
109
110 void Xbmc::actionMute()
111 {
112     do_command_action(ACTION_MUTE);
113 }
114
115 void Xbmc::actionVolumeUp()
116 {
117     do_command_action(ACTION_VOLUME_UP);
118 }
119
120 void Xbmc::actionVolumeDown()
121 {
122     do_command_action(ACTION_VOLUME_DOWN);
123 }
124
125 void Xbmc::actionNextSubtitle()
126 {
127     do_command_action(ACTION_NEXT_SUBTITLE);
128 }
129
130 void Xbmc::actionNextLanguage()
131 {
132     do_command_action(ACTION_AUDIO_NEXT_LANGUAGE);
133 }
134
135 void Xbmc::actionPlay()
136 {
137     do_command_action(ACTION_PLAYER_PLAY);
138 }
139
140 void Xbmc::actionSelect()
141 {
142     do_command_action(ACTION_SELECT_ITEM);
143 }
144
145 void Xbmc::actionStop()
146 {
147     do_command_action(ACTION_STOP);
148 }
149
150 void Xbmc::actionShowGui()
151 {
152     do_command_action(ACTION_SHOW_GUI);
153 }
154
155 void Xbmc::actionSendKeyEsc()
156 {
157     do_send_key(KEY_ASCII + 0x1B); // ESC
158 }
159
160 void Xbmc::actionContextMenu()
161 {
162     do_command_action(ACTION_CONTEXT_MENU);
163 }
164
165 void Xbmc::do_send_key(int key)
166 {
167     QSettings settings;
168     QString server = settings.value(SETUP_XBMC_SERVER, SETUP_XBMC_SERVER_DEFAULT).toString();
169     QString port = settings.value(SETUP_XBMC_PORT, SETUP_XBMC_PORT_DEFAULT).toString();
170
171     QUrl url = QUrl(QString("http://%1:%2/xbmcCmds/xbmcHttp?command=SendKey(%3)").arg(server).arg(port).arg(key));
172
173     QNetworkRequest request;
174     request.setUrl(url);
175
176     QNetworkReply *reply = m_manager->get(request);
177     connect(reply, SIGNAL(finished()), this, SLOT(commandActionFinished()));
178 }