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