1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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/AppFileInfo.h>
29 #include <storage/Path.h>
30 #include <storage/Entry.h>
31 #include <storage/File.h>
32 #include <unistd.h>
33 
34 #include "SDL_BApp.h"   /* SDL_BApp class definition */
35 #include "SDL_BeApp.h"
36 #include "SDL_timer.h"
37 #include "SDL_error.h"
38 
39 #include "../../video/haiku/SDL_BWin.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #include "../../thread/SDL_systhread.h"
46 
47 /* Flag to tell whether or not the Be application is active or not */
48 static int SDL_BeAppActive = 0;
49 static SDL_Thread *SDL_AppThread = NULL;
50 
51 /* Default application signature */
52 const char *signature = "application/x-SDL-executable";
53 
54 static int
StartBeApp(void * unused)55 StartBeApp(void *unused)
56 {
57     BApplication *App;
58 
59     // dig resources for correct signature
60     image_info info;
61     int32 cookie = 0;
62     if (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
63         BFile f(info.name, O_RDONLY);
64         if (f.InitCheck() == B_OK) {
65             BAppFileInfo app_info(&f);
66             if (app_info.InitCheck() == B_OK) {
67                 char sig[B_MIME_TYPE_LENGTH];
68                 if (app_info.GetSignature(sig) == B_OK)
69                     signature = strndup(sig, B_MIME_TYPE_LENGTH);
70             }
71         }
72     }
73 
74     App = new SDL_BApp(signature);
75 
76     App->Run();
77     delete App;
78     return (0);
79 }
80 
81 /* Initialize the Be Application, if it's not already started */
82 int
SDL_InitBeApp(void)83 SDL_InitBeApp(void)
84 {
85     /* Create the BApplication that handles appserver interaction */
86     if (SDL_BeAppActive <= 0) {
87         SDL_AppThread = SDL_CreateThreadInternal(StartBeApp, "SDLApplication", 0, NULL);
88         if (SDL_AppThread == NULL) {
89             return SDL_SetError("Couldn't create BApplication thread");
90         }
91 
92         /* Change working directory to that of executable */
93         app_info info;
94         if (B_OK == be_app->GetAppInfo(&info)) {
95             entry_ref ref = info.ref;
96             BEntry entry;
97             if (B_OK == entry.SetTo(&ref)) {
98                 BPath path;
99                 if (B_OK == path.SetTo(&entry)) {
100                     if (B_OK == path.GetParent(&path)) {
101                         chdir(path.Path());
102                     }
103                 }
104             }
105         }
106 
107         do {
108             SDL_Delay(10);
109         } while ((be_app == NULL) || be_app->IsLaunching());
110 
111         /* Mark the application active */
112         SDL_BeAppActive = 0;
113     }
114 
115     /* Increment the application reference count */
116     ++SDL_BeAppActive;
117 
118     /* The app is running, and we're ready to go */
119     return (0);
120 }
121 
122 /* Quit the Be Application, if there's nothing left to do */
123 void
SDL_QuitBeApp(void)124 SDL_QuitBeApp(void)
125 {
126     /* Decrement the application reference count */
127     --SDL_BeAppActive;
128 
129     /* If the reference count reached zero, clean up the app */
130     if (SDL_BeAppActive == 0) {
131         if (SDL_AppThread != NULL) {
132             if (be_app != NULL) {       /* Not tested */
133                 be_app->PostMessage(B_QUIT_REQUESTED);
134             }
135             SDL_WaitThread(SDL_AppThread, NULL);
136             SDL_AppThread = NULL;
137         }
138         /* be_app should now be NULL since be_app has quit */
139     }
140 }
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 /* SDL_BApp functions */
ClearID(SDL_BWin * bwin)147 void SDL_BApp::ClearID(SDL_BWin *bwin) {
148     _SetSDLWindow(NULL, bwin->GetID());
149     int32 i = _GetNumWindowSlots() - 1;
150     while(i >= 0 && GetSDLWindow(i) == NULL) {
151         _PopBackWindow();
152         --i;
153     }
154 }
155 
156 #endif /* __HAIKU__ */
157 
158 /* vi: set ts=4 sw=4 expandtab: */
159