Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / ApplicationMessenger.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
6  *
7  *  This Program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  This Program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with XBMC; see the file COPYING.  If not, see
19  *  <http://www.gnu.org/licenses/>.
20  *
21  */
22
23 #include "threads/CriticalSection.h"
24 #include "utils/StdString.h"
25 #include "guilib/WindowIDs.h"
26 #include "threads/Thread.h"
27 #include "threads/Event.h"
28 #include <boost/shared_ptr.hpp>
29
30 #include <queue>
31 #include "utils/GlobalsHandling.h"
32
33 class CFileItem;
34 class CFileItemList;
35 class CGUIDialog;
36 class CGUIWindow;
37 class CGUIMessage;
38 class CVideoInfoTag;
39 class CAction;
40
41 namespace MUSIC_INFO
42 {
43   class CMusicInfoTag;
44 }
45
46 // defines here
47 #define TMSG_DIALOG_DOMODAL       100
48 #define TMSG_EXECUTE_SCRIPT       102
49 #define TMSG_EXECUTE_BUILT_IN     103
50 #define TMSG_EXECUTE_OS           104
51
52 #define TMSG_MEDIA_PLAY           200
53 #define TMSG_MEDIA_STOP           201
54 // the PAUSE is indeed a PLAYPAUSE
55 #define TMSG_MEDIA_PAUSE          202
56 #define TMSG_MEDIA_RESTART        203
57 #define TMSG_MEDIA_UNPAUSE        204
58 #define TMSG_MEDIA_PAUSE_IF_PLAYING   205
59
60 #define TMSG_PLAYLISTPLAYER_PLAY  210
61 #define TMSG_PLAYLISTPLAYER_NEXT  211
62 #define TMSG_PLAYLISTPLAYER_PREV  212
63 #define TMSG_PLAYLISTPLAYER_ADD   213
64 #define TMSG_PLAYLISTPLAYER_CLEAR 214
65 #define TMSG_PLAYLISTPLAYER_SHUFFLE   215
66 #define TMSG_PLAYLISTPLAYER_GET_ITEMS 216
67 #define TMSG_PLAYLISTPLAYER_PLAY_SONG_ID 217
68 #define TMSG_PLAYLISTPLAYER_INSERT 218
69 #define TMSG_PLAYLISTPLAYER_REMOVE 219
70 #define TMSG_PLAYLISTPLAYER_SWAP 223
71 #define TMSG_PLAYLISTPLAYER_REPEAT 224
72 #define TMSG_UPDATE_CURRENT_ITEM 225
73
74 #define TMSG_PICTURE_SHOW         220
75 #define TMSG_PICTURE_SLIDESHOW    221
76
77 #define TMSG_SHUTDOWN             300
78 #define TMSG_POWERDOWN            301
79 #define TMSG_QUIT                 302
80 #define TMSG_HIBERNATE            303
81 #define TMSG_SUSPEND              304
82 #define TMSG_RESTART              305
83 #define TMSG_RESET                306
84 #define TMSG_RESTARTAPP           307
85 #define TMSG_SWITCHTOFULLSCREEN   308
86 #define TMSG_MINIMIZE             309
87 #define TMSG_TOGGLEFULLSCREEN     310
88 #define TMSG_SETLANGUAGE          311
89 #define TMSG_RENDERER_FLUSH       312
90 #define TMSG_INHIBITIDLESHUTDOWN  313
91 #define TMSG_LOADPROFILE          314
92 #define TMSG_ACTIVATESCREENSAVER  315
93 #define TMSG_CECTOGGLESTATE       316
94 #define TMSG_CECACTIVATESOURCE    317
95 #define TMSG_CECSTANDBY           318
96
97 #define TMSG_NETWORKMESSAGE         500
98
99 #define TMSG_GUI_DO_MODAL             600
100 #define TMSG_GUI_SHOW                 601
101 #define TMSG_GUI_ACTIVATE_WINDOW      604
102 #define TMSG_GUI_PYTHON_DIALOG        605
103 #define TMSG_GUI_WINDOW_CLOSE         606
104 #define TMSG_GUI_ACTION               607
105 #define TMSG_GUI_INFOLABEL            608
106 #define TMSG_GUI_INFOBOOL             609
107 #define TMSG_GUI_ADDON_DIALOG         610
108 #define TMSG_GUI_MESSAGE              611
109 #define TMSG_START_ANDROID_ACTIVITY   612
110
111 #define TMSG_CALLBACK             800
112
113 #define TMSG_VOLUME_SHOW          900
114 #define TMSG_SPLASH_MESSAGE       901
115
116 #define TMSG_DISPLAY_SETUP      1000
117 #define TMSG_DISPLAY_DESTROY    1001
118
119 typedef struct
120 {
121   unsigned int dwMessage;
122   unsigned int dwParam1;
123   unsigned int dwParam2;
124   CStdString strParam;
125   std::vector<CStdString> params;
126   boost::shared_ptr<CEvent> waitEvent;
127   void* lpVoid;
128 }
129 ThreadMessage;
130
131 class CDelayedMessage : public CThread
132 {
133   public:
134     CDelayedMessage(ThreadMessage& msg, unsigned int delay);
135     virtual void Process();
136
137   private:
138     unsigned int   m_delay;
139     ThreadMessage  m_msg;
140 };
141
142 struct ThreadMessageCallback
143 {
144   void (*callback)(void *userptr);
145   void *userptr;
146 };
147
148 class CApplicationMessenger;
149 namespace xbmcutil
150 {
151    template<class T> class GlobalsSingleton;
152 }
153
154 class CApplicationMessenger
155 {
156 public:
157   /*!
158    \brief The only way through which the global instance of the CApplicationMessenger should be accessed.
159    \return the global instance.
160    */
161   static CApplicationMessenger& Get();
162
163   void Cleanup();
164   // if a message has to be send to the gui, use MSG_TYPE_WINDOW instead
165   void SendMessage(ThreadMessage& msg, bool wait = false);
166   void ProcessMessages(); // only call from main thread.
167   void ProcessWindowMessages();
168
169
170   void MediaPlay(std::string filename);
171   void MediaPlay(const CFileItem &item);
172   void MediaPlay(const CFileItemList &item, int song = 0);
173   void MediaPlay(int playlistid, int song = -1);
174   void MediaStop(bool bWait = true, int playlistid = -1);
175   void MediaPause();
176   void MediaUnPause();
177   void MediaPauseIfPlaying();
178   void MediaRestart(bool bWait);
179
180   void PlayListPlayerPlay();
181   void PlayListPlayerPlay(int iSong);
182   bool PlayListPlayerPlaySongId(int songId);
183   void PlayListPlayerNext();
184   void PlayListPlayerPrevious();
185   void PlayListPlayerAdd(int playlist, const CFileItem &item);
186   void PlayListPlayerAdd(int playlist, const CFileItemList &list);
187   void PlayListPlayerClear(int playlist);
188   void PlayListPlayerShuffle(int playlist, bool shuffle);
189   void PlayListPlayerGetItems(int playlist, CFileItemList &list);
190   void PlayListPlayerInsert(int playlist, const CFileItem &item, int position); 
191   void PlayListPlayerInsert(int playlist, const CFileItemList &list, int position);
192   void PlayListPlayerRemove(int playlist, int position);
193   void PlayListPlayerSwap(int playlist, int indexItem1, int indexItem2);
194   void PlayListPlayerRepeat(int playlist, int repeatState);
195
196   void PlayFile(const CFileItem &item, bool bRestart = false); // thread safe version of g_application.PlayFile()
197   void PictureShow(std::string filename);
198   void PictureSlideShow(std::string pathname, bool addTBN = false);
199   void SetGUILanguage(const std::string &strLanguage);
200   void Shutdown();
201   void Powerdown();
202   void Quit();
203   void Hibernate();
204   void Suspend();
205   void Restart();
206   void RestartApp();
207   void Reset();
208   void InhibitIdleShutdown(bool inhibit);
209   void ActivateScreensaver();
210   void SwitchToFullscreen(); //
211   void Minimize(bool wait = false);
212   void ExecOS(const CStdString &command, bool waitExit = false);
213   void UserEvent(int code);
214   //! \brief Set the tag for the currently playing song
215   void SetCurrentSongTag(const MUSIC_INFO::CMusicInfoTag& tag);
216   //! \brief Set the tag for the currently playing video
217   void SetCurrentVideoTag(const CVideoInfoTag& tag);
218   //! \brief Set the currently currently item
219   void SetCurrentItem(const CFileItem& item);
220
221   void LoadProfile(unsigned int idx);
222   bool CECToggleState();
223   bool CECActivateSource();
224   bool CECStandby();
225
226   CStdString GetResponse();
227   int SetResponse(CStdString response);
228   void ExecBuiltIn(const CStdString &command, bool wait = false);
229
230   void NetworkMessage(unsigned int dwMessage, unsigned int dwParam = 0);
231
232   void DoModal(CGUIDialog *pDialog, int iWindowID, const CStdString &param = "");
233   void Show(CGUIDialog *pDialog);
234   void Close(CGUIWindow *window, bool forceClose, bool waitResult = true, int nextWindowID = 0, bool enableSound = true);
235   void ActivateWindow(int windowID, const std::vector<CStdString> &params, bool swappingWindows);
236   void SendAction(const CAction &action, int windowID = WINDOW_INVALID, bool waitResult=true);
237
238   //! \brief Send text to currently focused window / keyboard.
239   void SendText(const std::string &aTextString, bool closeKeyboard = false);
240
241   /*! \brief Send a GUIMessage, optionally waiting before it's processed to return.
242    Should be used to send messages to the GUI from other threads.
243    \param msg the GUIMessage to send.
244    \param windowID optional window to send the message to (defaults to no specified window).
245    \param waitResult whether to wait for the result (defaults to false).
246    */
247   void SendGUIMessage(const CGUIMessage &msg, int windowID = WINDOW_INVALID, bool waitResult=false);
248
249   std::vector<CStdString> GetInfoLabels(const std::vector<CStdString> &properties);
250   std::vector<bool> GetInfoBooleans(const std::vector<CStdString> &properties);
251
252   void ShowVolumeBar(bool up);
253
254   void SetSplashMessage(const CStdString& message);
255   void SetSplashMessage(int stringID);
256   
257   bool SetupDisplay();
258   bool DestroyDisplay();
259   void StartAndroidActivity(const std::vector<CStdString> &params);
260
261   virtual ~CApplicationMessenger();
262 private:
263   // private construction, and no assignements; use the provided singleton methods
264    friend class xbmcutil::GlobalsSingleton<CApplicationMessenger>;
265   CApplicationMessenger();
266   CApplicationMessenger(const CApplicationMessenger&);
267   CApplicationMessenger const& operator=(CApplicationMessenger const&);
268   void ProcessMessage(ThreadMessage *pMsg);
269
270   std::queue<ThreadMessage*> m_vecMessages;
271   std::queue<ThreadMessage*> m_vecWindowMessages;
272   CCriticalSection m_critSection;
273   CCriticalSection m_critBuffer;
274   CStdString bufferResponse;
275 };
276
277 XBMC_GLOBAL_REF(CApplicationMessenger,s_messenger);
278 #define s_messenger XBMC_GLOBAL_USE(CApplicationMessenger)
279
280