Port the SB128 code to AROS.
[AROS.git] / workbench / c / AROSMonDrvs.c
blob5a3d4640e8f4ba3252c6430646a5bf7db9ee337c
1 /*
2 Copyright 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Code that loads and initializes necessary HIDDs.
6 Lang: english
7 */
9 #define DEBUG 0
11 #include <aros/debug.h>
12 #include <dos/dosextens.h>
13 #include <workbench/icon.h>
14 #include <proto/alib.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
17 #include <proto/icon.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #define MONITORS_DIR "DEVS:Monitors"
24 /************************************************************************/
26 /* This code does almost the same thing as C:LoadMonDrvs does on other systems.
27 However, additionally we support priority-based sorting for display drivers.
28 This is needed in order to make monitor ID assignment more predictable */
30 struct MonitorNode
32 struct Node n;
33 char Name[1];
36 static BYTE checkIcon(STRPTR name, struct Library *IconBase)
38 LONG pri = 0;
39 struct DiskObject *dobj = GetDiskObject(name);
41 if (dobj == NULL)
42 return 0;
44 if ((dobj->do_Type == WBTOOL) || (dobj->do_Type == WBPROJECT))
46 const STRPTR *toolarray = (const STRPTR *)dobj->do_ToolTypes;
47 STRPTR s;
49 if ((s = FindToolType(toolarray, "STARTPRI")))
51 pri = atol(s);
52 if (pri < -128)
53 pri = -128;
54 else if (pri > 127)
55 pri = 127;
57 FreeDiskObject(dobj);
59 return pri;
62 static BOOL findMonitors(struct List *monitorsList, struct DosLibrary *DOSBase, struct Library *IconBase, APTR poolmem)
64 BOOL retvalue = TRUE;
65 LONG error;
66 struct AnchorPath *ap = AllocPooled(poolmem, sizeof(struct AnchorPath));
68 DB2(bug("[LoadMonDrvs] AnchorPath 0x%p\n", ap));
69 if (ap)
71 /* Initialize important fields in AnchorPath, especially
72 ap_Strlen (prevents memory trashing) */
73 ap->ap_Flags = 0;
74 ap->ap_Strlen = 0;
75 ap->ap_BreakBits = 0;
77 error = MatchFirst("~(#?.info)", ap);
78 while (!error)
80 struct MonitorNode *newnode;
82 DB2(bug("[LoadMonDrvs] Found monitor name %s\n", ap->ap_Info.fib_FileName));
84 newnode = AllocPooled(poolmem, sizeof(struct MonitorNode) + strlen(ap->ap_Info.fib_FileName));
85 DB2(bug("[LoadMonDrvs] Monitor node 0x%p\n", newnode));
86 if (newnode == NULL) {
87 retvalue = FALSE;
88 goto exit;
91 strcpy(newnode->Name, ap->ap_Info.fib_FileName);
92 if (IconBase)
93 newnode->n.ln_Pri = checkIcon(ap->ap_Info.fib_FileName, IconBase);
94 else
95 newnode->n.ln_Pri = 0;
96 Enqueue(monitorsList, &newnode->n);
98 error = MatchNext(ap);
100 if (error != ERROR_NO_MORE_ENTRIES)
102 retvalue = FALSE;
103 goto exit;
105 MatchEnd(ap);
107 else
108 retvalue = FALSE;
109 exit:
110 return retvalue;
113 static void loadMonitors(struct List *monitorsList, struct DosLibrary *DOSBase)
115 struct MonitorNode *node;
117 D(bug("[LoadMonDrvs] Loading monitor drivers...\n"));
118 D(bug(" Pri Name\n"));
120 ForeachNode(monitorsList, node)
122 D(bug("%4d %s\n", node->n.ln_Pri, node->Name));
123 Execute(node->Name, BNULL, BNULL);
126 D(bug("--------------------------\n"));
129 __startup BOOL _main(void)
131 APTR pool;
132 struct Library *IconBase;
133 APTR DOSBase;
134 BPTR dir, olddir;
135 BOOL res = TRUE;
137 DOSBase = OpenLibrary("dos.library", 0);
138 if (DOSBase == NULL)
139 return ERROR_INVALID_RESIDENT_LIBRARY;
142 dir = Lock(MONITORS_DIR, SHARED_LOCK);
143 D(bug("[LoadMonDrvs] Monitors directory 0x%p\n", dir));
144 if (dir) {
145 olddir = CurrentDir(dir);
147 pool = CreatePool(MEMF_ANY, sizeof(struct MonitorNode) * 10, sizeof(struct MonitorNode) * 5);
148 DB2(bug("[LoadMonDrvs] Created pool 0x%p\n", pool));
149 if (pool) {
150 struct List MonitorsList;
152 NewList(&MonitorsList);
153 IconBase = OpenLibrary("icon.library", 0);
154 if (IconBase) {
155 findMonitors(&MonitorsList, DOSBase, IconBase, pool);
156 loadMonitors(&MonitorsList, DOSBase);
157 CloseLibrary(IconBase);
159 DeletePool(pool);
160 } else
161 res = FALSE;
163 CurrentDir(olddir);
164 UnLock(dir);
167 CloseLibrary(DOSBase);
169 return res;