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