Copyright clean-up (part 1):
[AROS.git] / arch / all-hosted / hidd / sdl / startup.c
blobe62642cfa0ef6660a26711cd4efeac369d3c14a2
1 /*
2 Copyright © 1995-2014, 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/graphics.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_SDLBitMap, &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 GfxBase *GfxBase;
66 OOP_Object *kbd, *ms;
67 ULONG err;
69 xsd->mousehidd = NULL;
70 D(bug("[SDL] Class initialization OK, creating objects\n"));
72 /* Add keyboard and mouse driver to the system */
73 kbd = OOP_NewObject(NULL, CLID_Hidd_Kbd, NULL);
74 if (kbd) {
75 ms = OOP_NewObject(NULL, CLID_Hidd_Mouse, NULL);
76 if (ms) {
77 xsd->kbdhidd = HIDD_Kbd_AddHardwareDriver(kbd, xsd->kbdclass, NULL);
78 if (xsd->kbdhidd) {
79 xsd->mousehidd = HIDD_Mouse_AddHardwareDriver(ms, xsd->mouseclass, NULL);
80 D(bug("[SDL] Mouse driver object 0x%p\n", xsd->mousehidd));
81 if (!xsd->mousehidd)
82 HIDD_Kbd_RemHardwareDriver(kbd, xsd->mousehidd);
84 OOP_DisposeObject(ms);
86 OOP_DisposeObject(kbd);
89 /* If we got no input, we can't work, fail */
90 if (!xsd->mousehidd)
91 return FALSE;
93 /* Now proceed to adding display modes */
94 GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 41);
95 if (!GfxBase)
96 return FALSE;
99 * SDL is a singletone by design, so we can't create many SDL displays.
100 * So only one object!
102 err = AddDisplayDriverA(xsd->gfxclass, NULL, NULL);
104 CloseLibrary(&GfxBase->LibNode);
105 return err ? FALSE : TRUE;
108 extern struct WBStartup *WBenchMsg;
110 int __nocommandline = 1;
112 int main(void)
114 BPTR olddir = NULL;
115 STRPTR myname;
116 struct DiskObject *icon;
117 struct RDArgs *rdargs = NULL;
118 IPTR fullscreen = FALSE;
119 int ret = RETURN_FAIL;
121 /* Open libraries manually, otherwise they will be closed when this subroutine
122 exits. Driver needs them. */
123 OOPBase = OpenLibrary("oop.library", 42);
124 if (!OOPBase)
125 return RETURN_FAIL;
127 /* If SDLGfx class is already registered, the user attempts to run us twice.
128 Just ignore this. */
129 if (OOP_FindClass(CLID_Hidd_SDLGfx)) {
130 D(bug("[SDL] Driver already registered\n"));
131 CloseLibrary(OOPBase);
132 return RETURN_OK;
135 UtilityBase = OpenLibrary("utility.library", 36);
136 if (!UtilityBase) {
137 CloseLibrary(OOPBase);
138 return RETURN_FAIL;
141 /* We don't open dos.library and icon.library manually because only startup code
142 needs them and these libraries can be closed even upon successful exit */
143 if (WBenchMsg) {
144 olddir = CurrentDir(WBenchMsg->sm_ArgList[0].wa_Lock);
145 myname = WBenchMsg->sm_ArgList[0].wa_Name;
146 } else {
147 struct Process *me = (struct Process *)FindTask(NULL);
149 if (me->pr_CLI) {
150 struct CommandLineInterface *cli = BADDR(me->pr_CLI);
152 myname = cli->cli_CommandName;
153 } else
154 myname = me->pr_Task.tc_Node.ln_Name;
157 icon = GetDiskObject(myname);
158 D(bug("[X11] Icon 0x%p\n", icon));
160 if (icon) {
161 STRPTR str;
163 str = FindToolType(icon->do_ToolTypes, "FULLSCREEN");
164 fullscreen = str ? TRUE : FALSE;
167 if (!WBenchMsg)
168 rdargs = ReadArgs("FULLSCREEN/S", &fullscreen, NULL);
170 xsd.use_fullscreen = fullscreen;
171 if (rdargs)
172 FreeArgs(rdargs);
173 if (icon)
174 FreeDiskObject(icon);
175 if (olddir)
176 CurrentDir(olddir);
178 /* Obtain attribute bases first */
179 if (OOP_ObtainAttrBases(attrbases)) {
180 /* Load host libraries */
181 if (sdl_hostlib_init(&xsd)) {
182 /* Create classes */
183 struct TagItem SDLGfx_tags[] = {
184 {aMeta_SuperID , (IPTR)CLID_Hidd_Gfx },
185 {aMeta_InterfaceDescr, (IPTR)SDLGfx_ifdescr },
186 {aMeta_InstSize , sizeof(struct gfxdata)},
187 {aMeta_ID , (IPTR)CLID_Hidd_SDLGfx},
188 {TAG_DONE , 0 }
191 xsd.gfxclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLGfx_tags);
192 if (xsd.gfxclass) {
193 struct TagItem SDLBitMap_tags[] = {
194 {aMeta_SuperID , (IPTR)CLID_Hidd_BitMap },
195 {aMeta_InterfaceDescr, (IPTR)SDLBitMap_ifdescr},
196 {aMeta_InstSize , sizeof(struct bmdata) },
197 {TAG_DONE , 0 }
200 xsd.bmclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLBitMap_tags);
201 if (xsd.bmclass) {
202 struct TagItem SDLMouse_tags[] = {
203 {aMeta_SuperID , (IPTR)CLID_Hidd },
204 {aMeta_InterfaceDescr, (IPTR)SDLMouse_ifdescr },
205 {aMeta_InstSize , sizeof(struct mousedata)},
206 {TAG_DONE , 0 }
209 xsd.mouseclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLMouse_tags);
210 if (xsd.mouseclass) {
211 struct TagItem SDLKbd_tags[] = {
212 {aMeta_SuperID , (IPTR)CLID_Hidd },
213 {aMeta_InterfaceDescr, (IPTR)SDLKbd_ifdescr },
214 {aMeta_InstSize , sizeof(struct kbddata)},
215 {TAG_DONE , 0 }
218 xsd.kbdclass = OOP_NewObject(NULL, CLID_HiddMeta, SDLKbd_tags);
219 if (xsd.kbdclass) {
220 /* Init internal stuff */
221 sdl_keymap_init(&xsd);
222 if (sdl_event_init(&xsd)) {
223 if (sdl_hidd_init(&xsd)) {
224 if (sdl_Startup(&xsd)) {
225 /* Register our gfx class as public, we use it as a
226 protection against double start */
227 OOP_AddClass(xsd.gfxclass);
228 /* Everything is okay, stay resident and exit */
229 struct Process *me = (struct Process *)FindTask(NULL);
231 D(bug("[SDL] Staying resident, process 0x%p\n", me));
232 if (me->pr_CLI) {
233 struct CommandLineInterface *cli = BADDR(me->pr_CLI);
235 D(bug("[SDL] CLI 0x%p\n", cli));
236 cli->cli_Module = NULL;
237 } else
238 me->pr_SegList = NULL;
240 /* Note also that we don't close needed libraries and
241 don't free attribute bases */
242 return RETURN_OK;
244 SV(SDL_Quit);
246 sdl_event_expunge(&xsd);
248 OOP_DisposeObject((OOP_Object *)xsd.kbdclass);
250 OOP_DisposeObject((OOP_Object *)xsd.mouseclass);
252 OOP_DisposeObject((OOP_Object *)xsd.bmclass);
254 OOP_DisposeObject((OOP_Object *)xsd.gfxclass);
256 sdl_hostlib_expunge(&xsd);
257 } else
258 /* Perhaps some stupid user attempts to run this driver on
259 native system. Well, let's forgive him :) */
260 ret = RETURN_OK;
261 OOP_ReleaseAttrBases(attrbases);
264 CloseLibrary(UtilityBase);
265 CloseLibrary(OOPBase);
266 return ret;