xbmc
ApplicationMessenger.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "guilib/WindowIDs.h"
12 #include "messaging/ThreadMessage.h"
13 #include "threads/Thread.h"
14 
15 #include <map>
16 #include <memory>
17 #include <queue>
18 #include <string>
19 #include <vector>
20 
21 #define TMSG_MASK_MESSAGE 0xFFFF0000 // only keep the high bits to route messages
22 #define TMSG_MASK_APPLICATION (1<<30) //Don't use bit 31 as it'll fail to build, using unsigned variable to hold the message.
23 #define TMSG_MASK_PLAYLISTPLAYER (1<<29)
24 #define TMSG_MASK_GUIINFOMANAGER (1<<28)
25 #define TMSG_MASK_WINDOWMANAGER (1<<27)
26 #define TMSG_MASK_PERIPHERALS (1<<26)
27 
28 // defines here
29 #define TMSG_PLAYLISTPLAYER_PLAY TMSG_MASK_PLAYLISTPLAYER + 0
30 #define TMSG_PLAYLISTPLAYER_NEXT TMSG_MASK_PLAYLISTPLAYER + 1
31 #define TMSG_PLAYLISTPLAYER_PREV TMSG_MASK_PLAYLISTPLAYER + 2
32 #define TMSG_PLAYLISTPLAYER_ADD TMSG_MASK_PLAYLISTPLAYER + 3
33 #define TMSG_PLAYLISTPLAYER_CLEAR TMSG_MASK_PLAYLISTPLAYER + 4
34 #define TMSG_PLAYLISTPLAYER_SHUFFLE TMSG_MASK_PLAYLISTPLAYER + 5
35 #define TMSG_PLAYLISTPLAYER_GET_ITEMS TMSG_MASK_PLAYLISTPLAYER + 6
36 #define TMSG_PLAYLISTPLAYER_PLAY_SONG_ID TMSG_MASK_PLAYLISTPLAYER + 7
37 #define TMSG_PLAYLISTPLAYER_INSERT TMSG_MASK_PLAYLISTPLAYER + 8
38 #define TMSG_PLAYLISTPLAYER_REMOVE TMSG_MASK_PLAYLISTPLAYER + 9
39 #define TMSG_PLAYLISTPLAYER_SWAP TMSG_MASK_PLAYLISTPLAYER + 10
40 #define TMSG_PLAYLISTPLAYER_REPEAT TMSG_MASK_PLAYLISTPLAYER + 11
41 #define TMSG_MEDIA_PLAY TMSG_MASK_PLAYLISTPLAYER + 12
42 #define TMSG_MEDIA_STOP TMSG_MASK_PLAYLISTPLAYER + 13
43 // the PAUSE is indeed a PLAYPAUSE
44 #define TMSG_MEDIA_PAUSE TMSG_MASK_PLAYLISTPLAYER + 14
45 #define TMSG_MEDIA_RESTART TMSG_MASK_PLAYLISTPLAYER + 15
46 #define TMSG_MEDIA_UNPAUSE TMSG_MASK_PLAYLISTPLAYER + 16
47 #define TMSG_MEDIA_PAUSE_IF_PLAYING TMSG_MASK_PLAYLISTPLAYER + 17
48 #define TMSG_MEDIA_SEEK_TIME TMSG_MASK_PLAYLISTPLAYER + 18
49 
50 #define TMSG_SHUTDOWN TMSG_MASK_APPLICATION + 0
51 #define TMSG_POWERDOWN TMSG_MASK_APPLICATION + 1
52 #define TMSG_QUIT TMSG_MASK_APPLICATION + 2
53 #define TMSG_HIBERNATE TMSG_MASK_APPLICATION + 3
54 #define TMSG_SUSPEND TMSG_MASK_APPLICATION + 4
55 #define TMSG_RESTART TMSG_MASK_APPLICATION + 5
56 #define TMSG_RESET TMSG_MASK_APPLICATION + 6
57 #define TMSG_RESTARTAPP TMSG_MASK_APPLICATION + 7
58 #define TMSG_ACTIVATESCREENSAVER TMSG_MASK_APPLICATION + 8
59 #define TMSG_NETWORKMESSAGE TMSG_MASK_APPLICATION + 9
60 #define TMSG_RESETSCREENSAVER TMSG_MASK_APPLICATION + 10
61 #define TMSG_VOLUME_SHOW TMSG_MASK_APPLICATION + 11
62 #define TMSG_DISPLAY_SETUP TMSG_MASK_APPLICATION + 12
63 #define TMSG_DISPLAY_DESTROY TMSG_MASK_APPLICATION + 13
64 #define TMSG_SETVIDEORESOLUTION TMSG_MASK_APPLICATION + 14
65 #define TMSG_SWITCHTOFULLSCREEN TMSG_MASK_APPLICATION + 15
66 #define TMSG_MINIMIZE TMSG_MASK_APPLICATION + 16
67 #define TMSG_TOGGLEFULLSCREEN TMSG_MASK_APPLICATION + 17
68 #define TMSG_SETLANGUAGE TMSG_MASK_APPLICATION + 18
69 #define TMSG_RENDERER_FLUSH TMSG_MASK_APPLICATION + 19
70 #define TMSG_INHIBITIDLESHUTDOWN TMSG_MASK_APPLICATION + 20
71 #define TMSG_START_ANDROID_ACTIVITY TMSG_MASK_APPLICATION + 21
72 #define TMSG_EXECUTE_SCRIPT TMSG_MASK_APPLICATION + 22
73 #define TMSG_EXECUTE_BUILT_IN TMSG_MASK_APPLICATION + 23
74 #define TMSG_EXECUTE_OS TMSG_MASK_APPLICATION + 24
75 #define TMSG_PICTURE_SHOW TMSG_MASK_APPLICATION + 25
76 #define TMSG_PICTURE_SLIDESHOW TMSG_MASK_APPLICATION + 26
77 #define TMSG_LOADPROFILE TMSG_MASK_APPLICATION + 27
78 #define TMSG_VIDEORESIZE TMSG_MASK_APPLICATION + 28
79 #define TMSG_INHIBITSCREENSAVER TMSG_MASK_APPLICATION + 29
80 
81 #define TMSG_SYSTEM_POWERDOWN TMSG_MASK_APPLICATION + 30
82 #define TMSG_RENDERER_PREINIT TMSG_MASK_APPLICATION + 31
83 #define TMSG_RENDERER_UNINIT TMSG_MASK_APPLICATION + 32
84 #define TMSG_EVENT TMSG_MASK_APPLICATION + 33
85 
86 #define TMSG_GUI_INFOLABEL TMSG_MASK_GUIINFOMANAGER + 0
87 #define TMSG_GUI_INFOBOOL TMSG_MASK_GUIINFOMANAGER + 1
88 #define TMSG_UPDATE_CURRENT_ITEM TMSG_MASK_GUIINFOMANAGER + 2
89 
90 #define TMSG_CECTOGGLESTATE TMSG_MASK_PERIPHERALS + 1
91 #define TMSG_CECACTIVATESOURCE TMSG_MASK_PERIPHERALS + 2
92 #define TMSG_CECSTANDBY TMSG_MASK_PERIPHERALS + 3
93 
94 #define TMSG_GUI_DIALOG_OPEN TMSG_MASK_WINDOWMANAGER + 1
95 #define TMSG_GUI_ACTIVATE_WINDOW TMSG_MASK_WINDOWMANAGER + 2
96 #define TMSG_GUI_PYTHON_DIALOG TMSG_MASK_WINDOWMANAGER + 3
97 #define TMSG_GUI_WINDOW_CLOSE TMSG_MASK_WINDOWMANAGER + 4
98 #define TMSG_GUI_ACTION TMSG_MASK_WINDOWMANAGER + 5
99 #define TMSG_GUI_ADDON_DIALOG TMSG_MASK_WINDOWMANAGER + 6
100 #define TMSG_GUI_MESSAGE TMSG_MASK_WINDOWMANAGER + 7
101 
122 #define TMSG_GUI_DIALOG_YESNO TMSG_MASK_WINDOWMANAGER + 8
123 #define TMSG_GUI_DIALOG_OK TMSG_MASK_WINDOWMANAGER + 9
124 
132 #define TMSG_GUI_PREVIOUS_WINDOW TMSG_MASK_WINDOWMANAGER + 10
133 
134 
135 #define TMSG_CALLBACK 800
136 
137 
138 
139 class CGUIMessage;
140 
141 namespace KODI
142 {
143 namespace MESSAGING
144 {
145 class IMessageTarget;
146 
148 {
149  void (*callback)(void *userptr);
150  void *userptr;
151 };
152 
227 {
228 public:
231 
232  void Cleanup();
233  // if a message has to be send to the gui, use MSG_TYPE_WINDOW instead
246  int SendMsg(uint32_t messageId);
247 
264  int SendMsg(uint32_t messageId, int param1, int param2 = -1, void* payload = nullptr);
265 
283  int SendMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam);
284 
303  int SendMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam, std::vector<std::string> params);
304 
313  void PostMsg(uint32_t messageId);
314 
324  void PostMsg(uint32_t messageId, int64_t param3);
325 
338  void PostMsg(uint32_t messageId, int param1, int param2 = -1, void* payload = nullptr);
339 
353  void PostMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam);
368  void PostMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam, std::vector<std::string> params);
369 
373  void ProcessMessages();
374 
380  void ProcessWindowMessages();
381 
389  void SendGUIMessage(const CGUIMessage &msg, int windowID = WINDOW_INVALID, bool waitResult=false);
390 
395  void RegisterReceiver(IMessageTarget* target);
396 
402  void SetGUIThread(const std::thread::id thread) { m_guiThreadId = thread; }
403 
409  void SetProcessThread(const std::thread::id thread) { m_processThreadId = thread; }
410 
411  /*
412  * \brief Signals the shutdown of the application and message processing
413  */
414  void Stop() { m_bStop = true; }
415 
417  bool IsProcessThread() const;
418 
419 private:
421  CApplicationMessenger const& operator=(CApplicationMessenger const&) = delete;
422 
423  int SendMsg(ThreadMessage&& msg, bool wait);
424  void ProcessMessage(ThreadMessage *pMsg);
425 
426  std::queue<ThreadMessage*> m_vecMessages;
427  std::queue<ThreadMessage*> m_vecWindowMessages;
428  std::map<int, IMessageTarget*> m_mapTargets;
429  CCriticalSection m_critSection;
430  std::thread::id m_guiThreadId;
431  std::thread::id m_processThreadId;
432  bool m_bStop{ false };
433 };
434 }
435 }
A class wishing to receive messages should implement this and call.
Definition: IMessageTarget.h:23
void SetProcessThread(const std::thread::id thread)
Set the processing thread id to avoid messenger being dependent on CApplication to determine if marsh...
Definition: ApplicationMessenger.h:409
Definition: ThreadMessage.h:25
Definition: TestHelpers.h:60
void SetGUIThread(const std::thread::id thread)
Set the UI thread id to avoid messenger being dependent on CApplication to determine if marshaling is...
Definition: ApplicationMessenger.h:402
Controller configuration window.
Definition: AudioDecoder.h:18
Definition: GUIMessage.h:365
Definition: ApplicationMessenger.h:147
This implements a simple message dispatcher/router for Kodi.
Definition: ApplicationMessenger.h:226