merged: Platinum from linuxport branches
[xbmc:xbmc-antiquated.git] / xbmc / lib / libUPnP / Platinum / ThirdParty / Neptune / Source / System / Win32 / NptWin32MessageQueue.cpp
1 /*****************************************************************\r
2 |\r
3 |      Neptune - Win32 Message Queue\r
4 |\r
5 |      (c) 2001-2008 Gilles Boccon-Gibod\r
6 |      Author: Gilles Boccon-Gibod (bok@bok.net)\r
7 |\r
8 ****************************************************************/\r
9 \r
10 /*----------------------------------------------------------------------\r
11 |       includes\r
12 +---------------------------------------------------------------------*/\r
13 #include "NptWin32MessageQueue.h"\r
14 \r
15 /*----------------------------------------------------------------------\r
16 |       platform adaptation\r
17 +---------------------------------------------------------------------*/\r
18 #if defined(_WIN32_WCE)\r
19 #define GetWindowLongPtr GetWindowLong\r
20 #define SetWindowLongPtr SetWindowLong\r
21 #endif\r
22 \r
23 /*----------------------------------------------------------------------\r
24 |       constants\r
25 +---------------------------------------------------------------------*/\r
26 const int NPT_WIN32_MESSAGE_ID_BASE = WM_USER + 9200;\r
27 \r
28 /*----------------------------------------------------------------------\r
29 |       NPT_Win32WindowMessageQueue\r
30 +---------------------------------------------------------------------*/\r
31 NPT_Win32WindowMessageQueue::NPT_Win32WindowMessageQueue()\r
32 {\r
33     // create a hidden window to process our incoming messages\r
34     WNDCLASS wclass;\r
35 \r
36     // compute a unique class name\r
37     m_ClassName[0] = 'N';\r
38     m_ClassName[1] = 'P';\r
39     m_ClassName[2] = 'T';\r
40     m_ClassName[3] = 'H';\r
41     m_ClassName[4] = 'W';\r
42     NPT_String tid = NPT_String::FromInteger(GetCurrentThreadId());\r
43     for (unsigned int i=0; i<=tid.GetLength(); i++) {\r
44         m_ClassName[5+i] = tid.GetChars()[i];        \r
45     }\r
46 \r
47     // register a window class\r
48     wclass.style         = 0;\r
49     wclass.lpfnWndProc   = NPT_Win32WindowMessageQueue::WindowProcedure;\r
50     wclass.cbClsExtra    = 0;\r
51     wclass.cbWndExtra    = 0;\r
52     wclass.hInstance     = GetModuleHandle(NULL);\r
53     wclass.hIcon         = NULL;\r
54     wclass.hCursor       = NULL;\r
55     wclass.hbrBackground = NULL;\r
56     wclass.lpszMenuName  = NULL;\r
57     wclass.lpszClassName = m_ClassName;\r
58 \r
59     // register the class and ignore any error because we might\r
60     // be registering the class more than once                  \r
61     RegisterClass(&wclass);\r
62 \r
63     // create the hidden window\r
64     m_WindowHandle = CreateWindow(\r
65         wclass.lpszClassName, // pointer to registered class name \r
66         TEXT(""),             // pointer to window name \r
67         0,                    // window style \r
68         0,                    // horizontal position of window \r
69         0,                    // vertical position of window \r
70         0,                    // window width \r
71         0,                    // window height \r
72         NULL,                 // handle to parent or owner window \r
73         NULL,                 // handle to menu or child-window identifier \r
74         wclass.hInstance,     // handle to application instance \r
75         NULL);\r
76 \r
77     // set a pointer to ourself as user data */\r
78 #if defined(_MSC_VER)\r
79 #pragma warning( push )\r
80 #pragma warning( disable: 4244) // we have to test for this because SetWindowLongPtr\r
81                                 // is incorrectly defined, so we'll get a C4244 warning\r
82 #endif // _MSC_VER\r
83     if (m_WindowHandle) {\r
84         SetWindowLongPtr(m_WindowHandle, GWL_USERDATA, NPT_POINTER_TO_LONG(this));\r
85     }\r
86 #if defined(_MSC_VER)\r
87 #pragma warning( pop )\r
88 #endif // _MSC_VER\r
89     m_hInstance = wclass.hInstance;\r
90 }\r
91 \r
92 /*----------------------------------------------------------------------\r
93 |       NPT_Win32WindowMessageQueue\r
94 +---------------------------------------------------------------------*/\r
95 NPT_Win32WindowMessageQueue::~NPT_Win32WindowMessageQueue() \r
96 {\r
97     // remove ourself as user data to ensure we're not called anymore\r
98     SetWindowLongPtr(m_WindowHandle, GWL_USERDATA, 0);\r
99 \r
100     // destroy the hidden window\r
101     DestroyWindow(m_WindowHandle);\r
102 \r
103     // unregister the window class\r
104     UnregisterClass(m_ClassName, m_hInstance);\r
105 }\r
106 \r
107 /*----------------------------------------------------------------------\r
108 |       NPT_Win32WindowMessageQueue::WindowProcedure\r
109 +---------------------------------------------------------------------*/\r
110 LRESULT CALLBACK \r
111 NPT_Win32WindowMessageQueue::WindowProcedure(HWND   window, \r
112                                              UINT   message,\r
113                                              WPARAM wparam, \r
114                                              LPARAM lparam)\r
115 {\r
116     // if it is a windows message, just pass it along\r
117     if (message != (UINT) NPT_WIN32_MESSAGE_ID_BASE) {\r
118         return DefWindowProc(window, message, wparam, lparam);\r
119     }\r
120 \r
121     // dispatch the message to the handler\r
122 #if defined(_MSC_VER)\r
123 #pragma warning( push )\r
124 #pragma warning( disable: 4312) // we have to test for this because GetWindowLongPtr\r
125                                 // is incorrectly defined, so we'll get a C4244 warning\r
126 #endif // _MSC_VER\r
127     NPT_Win32WindowMessageQueue* queue = reinterpret_cast<NPT_Win32WindowMessageQueue *>(GetWindowLongPtr(window, GWL_USERDATA));\r
128 #if defined(_MSC_VER)\r
129 #pragma warning( pop )  \r
130 #endif // _MSC_VER\r
131     if (queue == NULL) {\r
132         return 0; \r
133     }\r
134     queue->HandleMessage(reinterpret_cast<NPT_Message*>(lparam),\r
135         reinterpret_cast<NPT_MessageHandler*>(wparam));\r
136     return 0;\r
137 }\r
138 \r
139 /*----------------------------------------------------------------------\r
140 |       NPT_Win32WindowMessageQueue::PumpMessage\r
141 +---------------------------------------------------------------------*/\r
142 NPT_Result\r
143 NPT_Win32WindowMessageQueue::PumpMessage(bool)\r
144 {\r
145     // you cannot pump messages on this type of queue, since they will\r
146     // be pumped by the main windows message loop \r
147     return NPT_FAILURE; \r
148 }\r
149 \r
150 /*----------------------------------------------------------------------\r
151 |       NPT_Win32WindowMessageQueue::QueueMessage\r
152 +---------------------------------------------------------------------*/\r
153 NPT_Result\r
154 NPT_Win32WindowMessageQueue::QueueMessage(NPT_Message*        message,\r
155                                           NPT_MessageHandler* handler)\r
156 {\r
157     int result;\r
158 \r
159     result = ::PostMessage(m_WindowHandle, \r
160         NPT_WIN32_MESSAGE_ID_BASE, \r
161         (WPARAM)handler, \r
162         (LPARAM)message);\r
163 \r
164     if (result == 0) return NPT_FAILURE;\r
165     return NPT_SUCCESS;\r
166 }\r
167 \r
168 /*----------------------------------------------------------------------\r
169 |       NPT_Win32WindowMessageQueue::HandleMessage\r
170 +---------------------------------------------------------------------*/\r
171 NPT_Result\r
172 NPT_Win32WindowMessageQueue::HandleMessage(NPT_Message*        message,\r
173                                            NPT_MessageHandler* handler)\r
174 {\r
175     // this printf causes windows CE to slow down !!!\r
176     //NPT_Debug(":: NPT_Win32WindowMessageQueue::HandleMessage - got message\n");\r
177     NPT_Result result = NPT_FAILURE;\r
178 \r
179     if (message && handler) {\r
180         result = handler->HandleMessage(message);\r
181     }\r
182     delete message;\r
183     return result;\r
184 }\r
185 \r