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