kodi
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 // clang-format off
22 #define TMSG_MASK_MESSAGE 0xFFFF0000 // only keep the high bits to route messages
23 #define TMSG_MASK_APPLICATION (1<<30) //Don't use bit 31 as it'll fail to build, using unsigned variable to hold the message.
24 #define TMSG_MASK_PLAYLISTPLAYER (1<<29)
25 #define TMSG_MASK_GUIINFOMANAGER (1<<28)
26 #define TMSG_MASK_WINDOWMANAGER (1<<27)
27 #define TMSG_MASK_PERIPHERALS (1<<26)
28 
29 // defines here
30 #define TMSG_PLAYLISTPLAYER_PLAY TMSG_MASK_PLAYLISTPLAYER + 0
31 #define TMSG_PLAYLISTPLAYER_NEXT TMSG_MASK_PLAYLISTPLAYER + 1
32 #define TMSG_PLAYLISTPLAYER_PREV TMSG_MASK_PLAYLISTPLAYER + 2
33 #define TMSG_PLAYLISTPLAYER_ADD TMSG_MASK_PLAYLISTPLAYER + 3
34 #define TMSG_PLAYLISTPLAYER_CLEAR TMSG_MASK_PLAYLISTPLAYER + 4
35 #define TMSG_PLAYLISTPLAYER_SHUFFLE TMSG_MASK_PLAYLISTPLAYER + 5
36 #define TMSG_PLAYLISTPLAYER_GET_ITEMS TMSG_MASK_PLAYLISTPLAYER + 6
37 #define TMSG_PLAYLISTPLAYER_PLAY_ITEM_ID TMSG_MASK_PLAYLISTPLAYER + 7
38 #define TMSG_PLAYLISTPLAYER_INSERT TMSG_MASK_PLAYLISTPLAYER + 8
39 #define TMSG_PLAYLISTPLAYER_REMOVE TMSG_MASK_PLAYLISTPLAYER + 9
40 #define TMSG_PLAYLISTPLAYER_SWAP TMSG_MASK_PLAYLISTPLAYER + 10
41 #define TMSG_PLAYLISTPLAYER_REPEAT TMSG_MASK_PLAYLISTPLAYER + 11
42 #define TMSG_MEDIA_PLAY TMSG_MASK_PLAYLISTPLAYER + 12
43 #define TMSG_MEDIA_STOP TMSG_MASK_PLAYLISTPLAYER + 13
44 // the PAUSE is indeed a PLAYPAUSE
45 #define TMSG_MEDIA_PAUSE TMSG_MASK_PLAYLISTPLAYER + 14
46 #define TMSG_MEDIA_RESTART TMSG_MASK_PLAYLISTPLAYER + 15
47 #define TMSG_MEDIA_UNPAUSE TMSG_MASK_PLAYLISTPLAYER + 16
48 #define TMSG_MEDIA_PAUSE_IF_PLAYING TMSG_MASK_PLAYLISTPLAYER + 17
49 #define TMSG_MEDIA_SEEK_TIME TMSG_MASK_PLAYLISTPLAYER + 18
50 
51 #define TMSG_SHUTDOWN TMSG_MASK_APPLICATION + 0
52 #define TMSG_POWERDOWN TMSG_MASK_APPLICATION + 1
53 #define TMSG_QUIT TMSG_MASK_APPLICATION + 2
54 #define TMSG_HIBERNATE TMSG_MASK_APPLICATION + 3
55 #define TMSG_SUSPEND TMSG_MASK_APPLICATION + 4
56 #define TMSG_RESTART TMSG_MASK_APPLICATION + 5
57 #define TMSG_RESET TMSG_MASK_APPLICATION + 6
58 #define TMSG_RESTARTAPP TMSG_MASK_APPLICATION + 7
59 #define TMSG_ACTIVATESCREENSAVER TMSG_MASK_APPLICATION + 8
60 #define TMSG_NETWORKMESSAGE TMSG_MASK_APPLICATION + 9
61 #define TMSG_RESETSCREENSAVER TMSG_MASK_APPLICATION + 10
62 #define TMSG_VOLUME_SHOW TMSG_MASK_APPLICATION + 11
63 #define TMSG_DISPLAY_SETUP TMSG_MASK_APPLICATION + 12
64 #define TMSG_DISPLAY_DESTROY TMSG_MASK_APPLICATION + 13
65 #define TMSG_SETVIDEORESOLUTION TMSG_MASK_APPLICATION + 14
66 #define TMSG_SWITCHTOFULLSCREEN TMSG_MASK_APPLICATION + 15
67 #define TMSG_MINIMIZE TMSG_MASK_APPLICATION + 16
68 #define TMSG_TOGGLEFULLSCREEN TMSG_MASK_APPLICATION + 17
69 #define TMSG_SETLANGUAGE TMSG_MASK_APPLICATION + 18
70 #define TMSG_RENDERER_FLUSH TMSG_MASK_APPLICATION + 19
71 #define TMSG_INHIBITIDLESHUTDOWN TMSG_MASK_APPLICATION + 20
72 #define TMSG_START_ANDROID_ACTIVITY TMSG_MASK_APPLICATION + 21
73 #define TMSG_EXECUTE_SCRIPT TMSG_MASK_APPLICATION + 22
74 #define TMSG_EXECUTE_BUILT_IN TMSG_MASK_APPLICATION + 23
75 #define TMSG_EXECUTE_OS TMSG_MASK_APPLICATION + 24
76 #define TMSG_PICTURE_SHOW TMSG_MASK_APPLICATION + 25
77 #define TMSG_PICTURE_SLIDESHOW TMSG_MASK_APPLICATION + 26
78 #define TMSG_LOADPROFILE TMSG_MASK_APPLICATION + 27
79 #define TMSG_VIDEORESIZE TMSG_MASK_APPLICATION + 28
80 #define TMSG_INHIBITSCREENSAVER TMSG_MASK_APPLICATION + 29
81 
82 #define TMSG_SYSTEM_POWERDOWN TMSG_MASK_APPLICATION + 30
83 #define TMSG_RENDERER_PREINIT TMSG_MASK_APPLICATION + 31
84 #define TMSG_RENDERER_UNINIT TMSG_MASK_APPLICATION + 32
85 #define TMSG_EVENT TMSG_MASK_APPLICATION + 33
86 #define TMSG_MOVETOSCREEN TMSG_MASK_APPLICATION + 34
87 
89 #define TMSG_UPDATE_PLAYER_ITEM TMSG_MASK_APPLICATION + 35
90 
91 #define TMSG_SET_VOLUME TMSG_MASK_APPLICATION + 36
92 #define TMSG_SET_MUTE TMSG_MASK_APPLICATION + 37
93 
94 #define TMSG_GUI_INFOLABEL TMSG_MASK_GUIINFOMANAGER + 0
95 #define TMSG_GUI_INFOBOOL TMSG_MASK_GUIINFOMANAGER + 1
96 #define TMSG_UPDATE_CURRENT_ITEM TMSG_MASK_GUIINFOMANAGER + 2
97 
98 #define TMSG_CECTOGGLESTATE TMSG_MASK_PERIPHERALS + 1
99 #define TMSG_CECACTIVATESOURCE TMSG_MASK_PERIPHERALS + 2
100 #define TMSG_CECSTANDBY TMSG_MASK_PERIPHERALS + 3
101 
102 #define TMSG_GUI_DIALOG_OPEN TMSG_MASK_WINDOWMANAGER + 1
103 #define TMSG_GUI_ACTIVATE_WINDOW TMSG_MASK_WINDOWMANAGER + 2
104 #define TMSG_GUI_PYTHON_DIALOG TMSG_MASK_WINDOWMANAGER + 3
105 #define TMSG_GUI_WINDOW_CLOSE TMSG_MASK_WINDOWMANAGER + 4
106 #define TMSG_GUI_ACTION TMSG_MASK_WINDOWMANAGER + 5
107 #define TMSG_GUI_ADDON_DIALOG TMSG_MASK_WINDOWMANAGER + 6
108 #define TMSG_GUI_MESSAGE TMSG_MASK_WINDOWMANAGER + 7
109 
130 #define TMSG_GUI_DIALOG_YESNO TMSG_MASK_WINDOWMANAGER + 8
131 #define TMSG_GUI_DIALOG_OK TMSG_MASK_WINDOWMANAGER + 9
132 
140 #define TMSG_GUI_PREVIOUS_WINDOW TMSG_MASK_WINDOWMANAGER + 10
141 
142 
143 #define TMSG_CALLBACK 800
144 // clang-format on
145 
146 class CGUIMessage;
147 
148 namespace KODI
149 {
150 namespace MESSAGING
151 {
152 class IMessageTarget;
153 
155 {
156  void (*callback)(void *userptr);
157  void *userptr;
158 };
159 
234 {
235 public:
238 
239  void Cleanup();
240  // if a message has to be send to the gui, use MSG_TYPE_WINDOW instead
253  int SendMsg(uint32_t messageId);
254 
271  int SendMsg(uint32_t messageId, int param1, int param2 = -1, void* payload = nullptr);
272 
290  int SendMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam);
291 
310  int SendMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam, std::vector<std::string> params);
311 
320  void PostMsg(uint32_t messageId);
321 
331  void PostMsg(uint32_t messageId, int64_t param3);
332 
345  void PostMsg(uint32_t messageId, int param1, int param2 = -1, void* payload = nullptr);
346 
360  void PostMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam);
375  void PostMsg(uint32_t messageId, int param1, int param2, void* payload, std::string strParam, std::vector<std::string> params);
376 
380  void ProcessMessages();
381 
387  void ProcessWindowMessages();
388 
396  void SendGUIMessage(const CGUIMessage &msg, int windowID = WINDOW_INVALID, bool waitResult=false);
397 
402  void RegisterReceiver(IMessageTarget* target);
403 
409  void SetGUIThread(const std::thread::id thread) { m_guiThreadId = thread; }
410 
416  void SetProcessThread(const std::thread::id thread) { m_processThreadId = thread; }
417 
418  /*
419  * \brief Signals the shutdown of the application and message processing
420  */
421  void Stop() { m_bStop = true; }
422 
424  bool IsProcessThread() const;
425 
426 private:
428  CApplicationMessenger const& operator=(CApplicationMessenger const&) = delete;
429 
430  int SendMsg(ThreadMessage&& msg, bool wait);
431  void ProcessMessage(ThreadMessage *pMsg);
432 
433  std::queue<ThreadMessage*> m_vecMessages;
434  std::queue<ThreadMessage*> m_vecWindowMessages;
435  std::map<int, IMessageTarget*> m_mapTargets;
436  CCriticalSection m_critSection;
437  std::thread::id m_guiThreadId;
438  std::thread::id m_processThreadId;
439  bool m_bStop{ false };
440 };
441 }
442 }
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:416
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:409
Definition: AudioDecoder.h:18
Definition: GUIMessage.h:365
Definition: ApplicationMessenger.h:154
This implements a simple message dispatcher/router for Kodi.
Definition: ApplicationMessenger.h:233