nux-1.14.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #include "Nux.h" 00024 #include "NuxGraphics/NuxGraphics.h" 00025 00026 namespace nux 00027 { 00028 00029 static NCriticalSection ThreadArrayLock; 00030 std::vector<NThread *> ThreadArray; 00031 00032 void NuxInitialize (const TCHAR *CommandLine) 00033 { 00034 nux::NuxCoreInitialize (0); 00035 nux::NuxGraphicsInitialize(); 00036 00037 // Register a thread local store for the WindowThreads. Each window thread will be able to access its own WindowThread pointer. 00038 inlRegisterThreadLocalIndex (0, ThreadLocal_InalogicAppImpl, NULL); 00039 } 00040 00041 static WindowThread *_CreateModalWindowThread (const TCHAR *WindowTitle, 00042 t_u32 width, 00043 t_u32 height, 00044 WindowThread *Parent, 00045 ThreadUserInitFunc UserInitFunc, 00046 void *InitData, 00047 bool Modal) 00048 { 00049 // check that Parent exist 00050 WindowThread *w = new WindowThread (WindowTitle, width, height, Parent, Modal); 00051 00052 if (w == 0) 00053 { 00054 nuxAssertMsg (0, TEXT ("[_CreateModalWindowThread] WindowThread creation failed.") ); 00055 return 0; 00056 } 00057 00058 return w; 00059 } 00060 00061 WindowThread *CreateGUIThread (const TCHAR *WindowTitle, 00062 t_u32 width, 00063 t_u32 height, 00064 WindowThread *Parent, 00065 ThreadUserInitFunc UserInitFunc, 00066 void *InitData) 00067 { 00068 if (GetWindowThread() ) 00069 { 00070 // An WindowThread already exist for this thread. 00071 nuxAssertMsg (0, "[CreateGUIThread] Only one WindowThread per thread is allowed"); 00072 return 0; 00073 } 00074 00075 inlSetThreadLocalStorage (ThreadLocal_InalogicAppImpl, 0); 00076 00077 WindowThread *w = new WindowThread (WindowTitle, width, height, 0, true); 00078 00079 if (w == 0) 00080 { 00081 nuxAssertMsg (0, TEXT ("[CreateGUIThread] WindowThread creation failed.") ); 00082 return 0; 00083 } 00084 00085 w->m_UserInitFunc = UserInitFunc; 00086 w->m_UserExitFunc = 0; 00087 w->m_InitData = InitData; 00088 w->m_ExitData = 0; 00089 w->SetWindowStyle (WINDOWSTYLE_NORMAL); 00090 w->ThreadCtor(); 00091 return w; 00092 } 00093 00094 #if defined(NUX_OS_WINDOWS) 00095 WindowThread *CreateFromForeignWindow (HWND WindowHandle, HDC WindowDCHandle, HGLRC OpenGLRenderingContext, 00096 ThreadUserInitFunc UserInitFunc, 00097 void *InitData 00098 ) 00099 { 00100 if (GetWindowThread() ) 00101 { 00102 // An WindowThread already exist for this thread. 00103 nuxAssertMsg (0, "[CreateGUIThread] Only one WindowThread per thread is allowed"); 00104 return 0; 00105 } 00106 00107 inlSetThreadLocalStorage (ThreadLocal_InalogicAppImpl, 0); 00108 00109 WindowThread *w = new WindowThread ("WindowTitle", 400, 300, 0, true); 00110 00111 if (w == 0) 00112 { 00113 nuxAssertMsg (0, TEXT ("[CreateGUIThread] WindowThread creation failed.") ); 00114 return 0; 00115 } 00116 00117 w->m_UserInitFunc = UserInitFunc; 00118 w->m_UserExitFunc = 0; 00119 w->m_InitData = InitData; 00120 w->m_ExitData = 0; 00121 w->SetWindowStyle (WINDOWSTYLE_NORMAL); 00122 w->ThreadCtor (WindowHandle, WindowDCHandle, OpenGLRenderingContext); 00123 return w; 00124 } 00125 00126 #elif defined(NUX_OS_LINUX) 00127 WindowThread *CreateFromForeignWindow (Window X11Window, GLXContext OpenGLContext, 00128 ThreadUserInitFunc UserInitFunc, 00129 void *InitData) 00130 { 00131 if (GetWindowThread() ) 00132 { 00133 // An WindowThread already exist for this thread. 00134 nuxAssertMsg (0, "[CreateGUIThread] Only one WindowThread per thread is allowed"); 00135 return 0; 00136 } 00137 00138 inlSetThreadLocalStorage (ThreadLocal_InalogicAppImpl, 0); 00139 00140 WindowThread *w = new WindowThread ("WindowTitle", 400, 300, 0, true); 00141 00142 if (w == 0) 00143 { 00144 nuxAssertMsg (0, TEXT ("[CreateGUIThread] WindowThread creation failed.") ); 00145 return 0; 00146 } 00147 00148 w->m_UserInitFunc = UserInitFunc; 00149 w->m_UserExitFunc = 0; 00150 w->m_InitData = InitData; 00151 w->m_ExitData = 0; 00152 w->SetWindowStyle (WINDOWSTYLE_NORMAL); 00153 w->m_embedded_window = true; 00154 w->ThreadCtor (NULL, X11Window, OpenGLContext); 00155 return w; 00156 } 00157 #endif 00158 00159 // Create a window thread that is a child of the Parent. This thread has a window. 00160 WindowThread *CreateWindowThread (WindowStyle WndStyle, 00161 const TCHAR *WindowTitle, 00162 t_u32 width, 00163 t_u32 height, 00164 WindowThread *Parent, 00165 ThreadUserInitFunc UserInitFunc, 00166 void *InitData) 00167 { 00168 WindowThread *w = _CreateModalWindowThread (WindowTitle, width, height, Parent, UserInitFunc, InitData, false); 00169 00170 if (w == 0) 00171 { 00172 nuxAssertMsg (0, TEXT ("[CreateWindowThread] WindowThread creation failed.") ); 00173 return 0; 00174 } 00175 00176 w->m_UserInitFunc = UserInitFunc; 00177 w->m_UserExitFunc = 0; 00178 w->m_InitData = InitData; 00179 w->m_ExitData = 0; 00180 w->SetWindowStyle (WndStyle); 00181 00182 return w; 00183 } 00184 00185 // Create modal graphics thread that is a child of the Parent. This thread has a window. 00186 WindowThread *CreateModalWindowThread (WindowStyle WndStyle, 00187 const TCHAR *WindowTitle, 00188 t_u32 width, 00189 t_u32 height, 00190 WindowThread *Parent, 00191 ThreadUserInitFunc UserInitFunc, 00192 void *InitData) 00193 { 00194 WindowThread *w = _CreateModalWindowThread (WindowTitle, width, height, Parent, UserInitFunc, InitData, true); 00195 00196 if (w == 0) 00197 { 00198 nuxAssertMsg (0, TEXT ("[CreateWindowThread] WindowThread creation failed.") ); 00199 return 0; 00200 } 00201 00202 w->m_UserInitFunc = UserInitFunc; 00203 w->m_UserExitFunc = 0; 00204 w->m_InitData = InitData; 00205 w->m_ExitData = 0; 00206 w->SetWindowStyle (WndStyle); 00207 00208 return w; 00209 } 00210 00211 SystemThread *CreateSystemThread (AbstractThread *Parent, ThreadUserInitFunc UserInitFunc, void *InitData) 00212 { 00213 SystemThread *system_thread = new SystemThread (Parent); 00214 00215 if (system_thread == 0) 00216 { 00217 nuxAssertMsg (0, TEXT ("[CreateSimpleThread] SystemThread creation failed.") ); 00218 return 0; 00219 } 00220 system_thread->m_UserInitFunc = UserInitFunc; 00221 system_thread->m_UserExitFunc = 0; 00222 system_thread->m_InitData = InitData; 00223 system_thread->m_ExitData = 0; 00224 return system_thread; 00225 } 00226 00227 bool RegisterNuxThread (NThread *ThreadPtr) 00228 { 00229 nuxAssert (ThreadPtr); 00230 NUX_RETURN_VALUE_IF_NULL (ThreadPtr, false); 00231 00232 NScopeLock Scope (&ThreadArrayLock); 00233 std::vector<NThread *>::iterator it = find (ThreadArray.begin(), ThreadArray.end(), ThreadPtr); 00234 00235 if (it == ThreadArray.end() ) 00236 { 00237 ThreadArray.push_back (ThreadPtr); 00238 } 00239 00240 return true; 00241 } 00242 00243 void UnregisterNuxThread (NThread *ThreadPtr) 00244 { 00245 nuxAssert (ThreadPtr); 00246 NUX_RETURN_IF_NULL (ThreadPtr); 00247 00248 NScopeLock Scope (&ThreadArrayLock); 00249 std::vector<NThread *>::iterator it = find (ThreadArray.begin(), ThreadArray.end(), ThreadPtr); 00250 00251 if (it != ThreadArray.end() ) 00252 { 00253 ThreadArray.erase (it); 00254 } 00255 } 00256 00257 ThreadState GetThreadState (unsigned int ThreadID) 00258 { 00259 NScopeLock Scope (&ThreadArrayLock); 00260 std::vector<NThread *>::iterator it; 00261 00262 for (it = ThreadArray.begin(); it != ThreadArray.end(); it++) 00263 { 00264 if ( (*it)->GetThreadId() == ThreadID) 00265 { 00266 return (*it)->GetThreadState(); 00267 break; 00268 } 00269 } 00270 00271 return THREADSTOP; 00272 } 00273 00274 00275 ObjectPtr<FontTexture> GetSysFont () 00276 { 00277 return GetGraphicsEngine ().GetFont (); 00278 } 00279 00280 ObjectPtr<FontTexture> GetSysBoldFont () 00281 { 00282 return GetGraphicsEngine ().GetBoldFont (); 00283 } 00284 00285 WindowCompositor &GetWindowCompositor () 00286 { 00287 NThread *thread = GetWindowThread (); 00288 00289 if (!thread->Type().IsObjectType (WindowThread::StaticObjectType) ) 00290 { 00291 nuxAssertMsg (0, TEXT ("[GetWindowCompositor] You can't call GetWindowCompositor on this type of thread: s"), thread->Type().name ); 00292 PrintOutputDebugString (TEXT ("[GetWindowCompositor] You can't call GetWindowCompositor on this type of thread: s"), thread->Type().name ); 00293 NUX_HARDWARE_BREAK; 00294 } 00295 00296 return (static_cast<WindowThread *> (thread) )->GetWindowCompositor(); 00297 } 00298 00299 NThread *GetThreadApplication () 00300 { 00301 NThread *thread = static_cast<NThread *> (inlGetThreadLocalStorage (ThreadLocal_InalogicAppImpl) ); 00302 return thread; 00303 } 00304 00305 WindowThread *GetGraphicsThread () 00306 { 00307 return NUX_STATIC_CAST (WindowThread *, GetWindowThread ()); 00308 } 00309 00310 WindowThread *GetWindowThread () 00311 { 00312 NThread *thread = static_cast<NThread *> (inlGetThreadLocalStorage (ThreadLocal_InalogicAppImpl) ); 00313 return NUX_STATIC_CAST (WindowThread *, thread); 00314 } 00315 00316 BasePainter& GetPainter () 00317 { 00318 NThread *thread = GetWindowThread (); 00319 return NUX_STATIC_CAST (WindowThread *, thread)->GetPainter (); 00320 } 00321 00322 UXTheme& GetTheme () 00323 { 00324 NThread *thread = GetWindowThread (); 00325 return NUX_STATIC_CAST (WindowThread *, thread)->GetTheme (); 00326 } 00327 00328 TimerHandler& GetTimer () 00329 { 00330 NThread *thread = GetWindowThread (); 00331 return NUX_STATIC_CAST (WindowThread *, thread)->GetTimerHandler (); 00332 } 00333 00334 GraphicsDisplay& GetWindow () 00335 { 00336 NThread *thread = GetWindowThread (); 00337 return NUX_STATIC_CAST (WindowThread *, thread)->GetWindow (); 00338 } 00339 00340 GraphicsEngine& GetGraphicsEngine () 00341 { 00342 NThread *thread = GetWindowThread (); 00343 return NUX_STATIC_CAST (WindowThread *, thread)->GetGraphicsEngine (); 00344 } 00345 00346 }