revert between 56095 -> 55830 in arch
[AROS.git] / arch / all-hosted / hidd / sdl / startup.c
blob24abce53d6979e4f0049567269608b94e3cb0f02
1 /*
2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
8 #include <aros/debug.h>
9 #include <dos/dosextens.h>
10 #include <hidd/gfx.h>
11 #include <hidd/keyboard.h>
12 #include <hidd/mouse.h>
13 #include <hidd/hidd.h>
14 #include <graphics/gfxbase.h>
15 #include <workbench/startup.h>
16 #include <workbench/workbench.h>
17 #include <proto/dos.h>
18 #include <proto/exec.h>
19 #include <proto/graphics.h>
20 #include <proto/icon.h>
21 #include <proto/oop.h>
23 #include <string.h>
25 #include "sdl_intern.h"
27 /* SDL includes define main to SDL_main, bring it back */
28 #undef main
30 struct Library *OOPBase;
31 struct Library *UtilityBase;
33 OOP_AttrBase MetaAttrBase;
34 OOP_AttrBase HiddAttrBase;
35 OOP_AttrBase HiddPixFmtAttrBase;
36 OOP_AttrBase HiddBitMapAttrBase;
37 OOP_AttrBase HiddColorMapAttrBase;
38 OOP_AttrBase HiddSyncAttrBase;
39 OOP_AttrBase HiddGfxAttrBase;
40 OOP_AttrBase HiddSDLBitMapAttrBase;
41 OOP_AttrBase HiddMouseAB;
42 OOP_AttrBase HiddKbdAB;
44 static struct OOP_ABDescr attrbases[] = {
45 { IID_Meta, &MetaAttrBase },
46 { IID_Hidd, &HiddAttrBase },
47 { IID_Hidd_PixFmt, &HiddPixFmtAttrBase },
48 { IID_Hidd_BitMap, &HiddBitMapAttrBase },
49 { IID_Hidd_ColorMap, &HiddColorMapAttrBase },
50 { IID_Hidd_Sync, &HiddSyncAttrBase },
51 { IID_Hidd_Gfx, &HiddGfxAttrBase },
52 { IID_Hidd_BitMap_SDL, &HiddSDLBitMapAttrBase },
53 { IID_Hidd_Mouse, &HiddMouseAB },
54 { IID_Hidd_Kbd, &HiddKbdAB },
55 { NULL, NULL }
58 /* Class static data is really static now. :)
59 If the driver would be compiled as a ROM resident, this structure could
60 be allocated either on stack or using AllocMem() */
61 struct sdlhidd xsd = {NULL};
63 static int sdl_Startup(struct sdlhidd *xsd)
65 struct TagItem kbd_tags[] =
67 {aHidd_Name , (IPTR)"SDLKbd" },
68 {aHidd_HardwareName, (IPTR)"SDL keyboard input"},
69 {aHidd_ProducerName, (IPTR)"libsdl.org" },
70 {TAG_DONE , 0 }
72 struct TagItem mouse_tags[] =
74 {aHidd_Name , (IPTR)"SDLMouse" },
75 {aHidd_HardwareName, (IPTR)"SDL pointer input"},
76 {aHidd_ProducerName, (IPTR)"libsdl.org" },
77 {TAG_DONE , 0 }
79 struct GfxBase *GfxBase;
80 OOP_Object *kbd, *ms = NULL;
81 OOP_Object *kbdriver = NULL;
82 OOP_Object *msdriver = NULL;
83 ULONG err;
85 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 41);
86 if (!GfxBase)
87 return FALSE;
89 xsd->basebm = OOP_FindClass(CLID_Hidd_BitMap);
90 xsd->mousehidd = NULL;
91 D(bug("[SDL] Class initialization OK, creating objects\n"));
93 /* Add keyboard and mouse driver to the system */
94 kbd = OOP_NewObject(NULL, CLID_Hidd_Kbd, NULL);
95 if (kbd) {
96 ms = OOP_NewObject(NULL, CLID_Hidd_Mouse, NULL);
97 if (ms) {
98 kbdriver = HIDD_Kbd_AddHardwareDriver(kbd, xsd->kbdclass, kbd_tags);
99 D(bug("[SDL] Keyboard driver object 0x%p\n", kbdriver));
100 if (kbdriver) {
101 msdriver = HIDD_Mouse_AddHardwareDriver(ms, xsd->mouseclass, mouse_tags);
102 D(bug("[SDL] Mouse driver object 0x%p\n", msdriver));
108 /* If we got no input, we can't work, fail */
109 if (!msdriver)
111 err = -1;
113 if (kbdriver)
114 HIDD_Kbd_RemHardwareDriver(kbd, kbdriver);
116 if (ms)
117 OOP_DisposeObject(ms);
119 if (kbd)
120 OOP_DisposeObject(kbd);
122 return FALSE;
124 else
127 * Now proceed to adding display modes ..
128 * SDL is currently a singletone by design,
129 * So only one object!
131 err = AddDisplayDriverA(xsd->gfxclass, NULL, NULL);
134 CloseLibrary(&GfxBase->LibNode);
136 return err ? FALSE : TRUE;
139 extern struct WBStartup *WBenchMsg;
141 int __nocommandline = 1;
143 int main(void)
145 BPTR olddir = NULL;
146 STRPTR myname;
147 struct DiskObject *icon;
148 struct RDArgs *rdargs = NULL;
149 IPTR fullscreen = FALSE;
150 int ret = RETURN_FAIL;
152 /* Open libraries manually, otherwise they will be closed when this subroutine
153 exits. Driver needs them. */
154 OOPBase = OpenLibrary("oop.library", 42);
155 if (!OOPBase)
156 return RETURN_FAIL;
158 /* If SDLGfx class is already registered, the user attempts to run us twice.
159 Just ignore this. */
160 if (OOP_FindClass(CLID_Hidd_Gfx_SDL)) {
161 D(bug("[SDL] Driver already registered\n"));
162 CloseLibrary(OOPBase);
163 return RETURN_OK;
166 UtilityBase = OpenLibrary("utility.library", 36);
167 if (!UtilityBase) {
168 CloseLibrary(OOPBase);
169 return RETURN_FAIL;
172 /* We don't open dos.library and icon.library manually because only startup code
173 needs them and these libraries can be closed even upon successful exit */
174 if (WBenchMsg) {
175 olddir = CurrentDir(WBenchMsg->sm_ArgList[0].wa_Lock);
176 myname = WBenchMsg->sm_ArgList[0].wa_Name;
177 } else {
178 struct Process *me = (struct Process *)FindTask(NULL);
180 if (me->pr_CLI) {
181 struct CommandLineInterface *cli = BADDR(me->pr_CLI);
183 myname = cli->cli_CommandName;
184 } else
185 myname = me->pr_Task.tc_Node.ln_Name;
188 icon = GetDiskObject(myname);
189 D(bug("[X11] Icon 0x%p\n", icon));
191 if (icon) {
192 STRPTR str;
194 str = FindToolType(icon->do_ToolTypes, "FULLSCREEN");
195 fullscreen = str ? TRUE : FALSE;
198 if (!WBenchMsg)
199 rdargs = ReadArgs("FULLSCREEN/S", &fullscreen, NULL);
201 xsd.use_fullscreen = fullscreen;
202 if (rdargs)
203 FreeArgs(rdargs);
204 if (icon)
205 FreeDiskObject(icon);
206 if (olddir)
207 CurrentDir(olddir);
209 /* Obtain attribute bases first */
210 if (OOP_ObtainAttrBases(attrbases)) {
211 /* Load host libraries */
212 if (sdl_hostlib_init(&xsd)) {
213 /* Create classes */
214 struct TagItem SDLGfx_tags[] = {
215 {aMeta_SuperID , (IPTR)CLID_Hidd_Gfx },
216 {aMeta_InterfaceDescr, (IPTR)SDLGfx_ifdescr },
217 {aMeta_InstSize , sizeof(struct gfxdata)},
218 {aMeta_ID , (IPTR)CLID_Hidd_Gfx_SDL},
219 {TAG_DONE , 0 }
222 xsd.gfxclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLGfx_tags);
223 if (xsd.gfxclass) {
224 struct TagItem SDLBitMap_tags[] = {
225 {aMeta_SuperID , (IPTR)CLID_Hidd_BitMap },
226 {aMeta_InterfaceDescr, (IPTR)SDLBitMap_ifdescr},
227 {aMeta_InstSize , sizeof(struct bmdata) },
228 {TAG_DONE , 0 }
231 xsd.gfxclass->UserData = &xsd;
232 xsd.bmclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLBitMap_tags);
233 if (xsd.bmclass) {
234 struct TagItem SDLMouse_tags[] = {
235 {aMeta_SuperID , (IPTR)CLID_Hidd },
236 {aMeta_InterfaceDescr, (IPTR)SDLMouse_ifdescr },
237 {aMeta_InstSize , sizeof(struct mousedata)},
238 {TAG_DONE , 0 }
241 xsd.bmclass->UserData = &xsd;
242 xsd.mouseclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLMouse_tags);
243 if (xsd.mouseclass) {
244 struct TagItem SDLKbd_tags[] = {
245 {aMeta_SuperID , (IPTR)CLID_Hidd },
246 {aMeta_InterfaceDescr, (IPTR)SDLKbd_ifdescr },
247 {aMeta_InstSize , sizeof(struct kbddata)},
248 {TAG_DONE , 0 }
251 xsd.mouseclass->UserData = &xsd;
252 xsd.kbdclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLKbd_tags);
253 if (xsd.kbdclass) {
254 xsd.kbdclass->UserData = &xsd;
256 /* Init internal stuff */
257 sdl_keymap_init(&xsd);
258 if (sdl_event_init(&xsd)) {
259 if (sdl_hidd_init(&xsd)) {
260 if (sdl_Startup(&xsd)) {
261 /* Register our gfx class as public, we use it as a
262 protection against double start */
263 OOP_AddClass(xsd.gfxclass);
264 /* Everything is okay, stay resident and exit */
265 struct Process *me = (struct Process *)FindTask(NULL);
267 D(bug("[SDL] Staying resident, process 0x%p\n", me));
268 if (me->pr_CLI) {
269 struct CommandLineInterface *cli = BADDR(me->pr_CLI);
271 D(bug("[SDL] CLI 0x%p\n", cli));
272 cli->cli_Module = NULL;
273 } else
274 me->pr_SegList = NULL;
276 /* Note also that we don't close needed libraries and
277 don't free attribute bases */
278 return RETURN_OK;
280 SV(SDL_Quit);
282 sdl_event_expunge(&xsd);
284 OOP_DisposeObject((OOP_Object *)xsd.kbdclass);
286 OOP_DisposeObject((OOP_Object *)xsd.mouseclass);
288 OOP_DisposeObject((OOP_Object *)xsd.bmclass);
290 OOP_DisposeObject((OOP_Object *)xsd.gfxclass);
292 sdl_hostlib_expunge(&xsd);
293 } else
294 /* Perhaps some stupid user attempts to run this driver on
295 native system. Well, let's forgive him :) */
296 ret = RETURN_OK;
297 OOP_ReleaseAttrBases(attrbases);
300 CloseLibrary(UtilityBase);
301 CloseLibrary(OOPBase);
302 return ret;