1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #if defined(__HAIKU__)
24 
25 /* Handle the BeApp specific portions of the application */
26 
27 #include <AppKit.h>
28 #include <storage/Path.h>
29 #include <storage/Entry.h>
30 #include <unistd.h>
31 
32 #include "SDL_BApp.h"	/* SDL_BApp class definition */
33 #include "SDL_BeApp.h"
34 #include "SDL_timer.h"
35 #include "SDL_error.h"
36 
37 #include "../../video/haiku/SDL_BWin.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #include "../../thread/SDL_systhread.h"
44 
45 /* Flag to tell whether or not the Be application is active or not */
46 int SDL_BeAppActive = 0;
47 static SDL_Thread *SDL_AppThread = NULL;
48 
49 static int
StartBeApp(void * unused)50 StartBeApp(void *unused)
51 {
52     BApplication *App;
53 
54     App = new SDL_BApp("application/x-SDL-executable");
55 
56     App->Run();
57     delete App;
58     return (0);
59 }
60 
61 /* Initialize the Be Application, if it's not already started */
62 int
SDL_InitBeApp(void)63 SDL_InitBeApp(void)
64 {
65     /* Create the BApplication that handles appserver interaction */
66     if (SDL_BeAppActive <= 0) {
67         SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
68         if (SDL_AppThread == NULL) {
69             return SDL_SetError("Couldn't create BApplication thread");
70         }
71 
72         /* Change working directory to that of executable */
73         app_info info;
74         if (B_OK == be_app->GetAppInfo(&info)) {
75             entry_ref ref = info.ref;
76             BEntry entry;
77             if (B_OK == entry.SetTo(&ref)) {
78                 BPath path;
79                 if (B_OK == path.SetTo(&entry)) {
80                     if (B_OK == path.GetParent(&path)) {
81                         chdir(path.Path());
82                     }
83                 }
84             }
85         }
86 
87         do {
88             SDL_Delay(10);
89         } while ((be_app == NULL) || be_app->IsLaunching());
90 
91         /* Mark the application active */
92         SDL_BeAppActive = 0;
93     }
94 
95     /* Increment the application reference count */
96     ++SDL_BeAppActive;
97 
98     /* The app is running, and we're ready to go */
99     return (0);
100 }
101 
102 /* Quit the Be Application, if there's nothing left to do */
103 void
SDL_QuitBeApp(void)104 SDL_QuitBeApp(void)
105 {
106     /* Decrement the application reference count */
107     --SDL_BeAppActive;
108 
109     /* If the reference count reached zero, clean up the app */
110     if (SDL_BeAppActive == 0) {
111         if (SDL_AppThread != NULL) {
112             if (be_app != NULL) {       /* Not tested */
113                 be_app->PostMessage(B_QUIT_REQUESTED);
114             }
115             SDL_WaitThread(SDL_AppThread, NULL);
116             SDL_AppThread = NULL;
117         }
118         /* be_app should now be NULL since be_app has quit */
119     }
120 }
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 /* SDL_BApp functions */
ClearID(SDL_BWin * bwin)127 void SDL_BApp::ClearID(SDL_BWin *bwin) {
128 	_SetSDLWindow(NULL, bwin->GetID());
129 	int32 i = _GetNumWindowSlots() - 1;
130 	while(i >= 0 && GetSDLWindow(i) == NULL) {
131 		_PopBackWindow();
132 		--i;
133 	}
134 }
135 
136 #endif /* __HAIKU__ */
137 
138 /* vi: set ts=4 sw=4 expandtab: */
139