Backport
[AROS.git] / workbench / c / AROSMonDrvs.c
blob6b5ad6074e8adda07c6fb4d831dae7d91e2795f7
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Code that loads and initializes necessary HIDDs.
6 Lang: english
7 */
9 /******************************************************************************
12 NAME
14 AROSMonDrvs
16 SYNOPSIS
18 NOCOMPOSITION/S,ONLYCOMPOSITION/S
20 LOCATION
24 FUNCTION
26 This command does almost the same thing as C:LoadMonDrvs does on
27 other systems. However, additionally we support priority-based
28 sorting for display drivers. This is needed in order to make monitor
29 ID assignment more predictable.
31 INPUTS
33 NOCOMPOSITION -- Only load Monitors
34 ONLYCOMPOSITION -- Only load Compositor
36 RESULT
38 NOTES
40 EXAMPLE
42 BUGS
44 SEE ALSO
46 INTERNALS
48 HISTORY
50 ******************************************************************************/
52 #define DEBUG 0
54 #include <aros/debug.h>
55 #include <dos/dosextens.h>
56 #include <workbench/icon.h>
57 #include <proto/alib.h>
58 #include <proto/exec.h>
59 #include <proto/dos.h>
60 #include <proto/icon.h>
62 #include <aros/shcommands.h>
64 #include <stdlib.h>
65 #include <string.h>
67 #define MONITORS_DIR "DEVS:Monitors"
68 #define COMPOSITING_NAME "Compositor"
70 /************************************************************************/
72 struct MonitorNode
74 struct Node n;
75 char Name[1];
78 static BYTE checkIcon(STRPTR name, struct Library *IconBase)
80 LONG pri = 0;
81 struct DiskObject *dobj = GetDiskObject(name);
83 if (dobj == NULL)
84 return 0;
86 if ((dobj->do_Type == WBTOOL) || (dobj->do_Type == WBPROJECT))
88 const STRPTR *toolarray = (const STRPTR *)dobj->do_ToolTypes;
89 STRPTR s;
91 if ((s = FindToolType(toolarray, "STARTPRI")))
93 pri = atol(s);
94 if (pri < -128)
95 pri = -128;
96 else if (pri > 127)
97 pri = 127;
99 FreeDiskObject(dobj);
101 return pri;
104 static BOOL findMonitors(struct List *monitorsList, struct DosLibrary *DOSBase, struct Library *IconBase, struct ExecBase *SysBase, APTR poolmem)
106 BOOL retvalue = TRUE;
107 LONG error;
108 struct AnchorPath *ap = AllocPooled(poolmem, sizeof(struct AnchorPath));
110 DB2(bug("[LoadMonDrvs] AnchorPath 0x%p\n", ap));
111 if (ap)
113 /* Initialize important fields in AnchorPath, especially
114 ap_Strlen (prevents memory trashing) */
115 ap->ap_Flags = 0;
116 ap->ap_Strlen = 0;
117 ap->ap_BreakBits = 0;
119 error = MatchFirst("~(#?.info)", ap);
120 while (!error)
122 struct MonitorNode *newnode;
124 DB2(bug("[LoadMonDrvs] Found monitor name %s\n", ap->ap_Info.fib_FileName));
126 /* Software composition driver was loaded before */
127 if (strcmp(ap->ap_Info.fib_FileName, COMPOSITING_NAME))
129 newnode = AllocPooled(poolmem, sizeof(struct MonitorNode) + strlen(ap->ap_Info.fib_FileName));
130 DB2(bug("[LoadMonDrvs] Monitor node 0x%p\n", newnode));
131 if (newnode == NULL)
133 retvalue = FALSE;
134 break;
137 strcpy(newnode->Name, ap->ap_Info.fib_FileName);
138 if (IconBase)
139 newnode->n.ln_Pri = checkIcon(ap->ap_Info.fib_FileName, IconBase);
140 else
141 newnode->n.ln_Pri = 0;
142 Enqueue(monitorsList, &newnode->n);
145 error = MatchNext(ap);
148 if (error != ERROR_NO_MORE_ENTRIES)
150 retvalue = FALSE;
151 /* FIXME: Why no MatchEnd() in this case?
152 goto exit; */
154 MatchEnd(ap);
156 else
157 retvalue = FALSE;
159 return retvalue;
162 static void loadMonitors(struct List *monitorsList, struct DosLibrary *DOSBase)
164 struct MonitorNode *node;
166 D(bug("[LoadMonDrvs] Loading monitor drivers...\n"));
167 D(bug(" Pri Name\n"));
169 ForeachNode(monitorsList, node)
171 D(bug("%4d %s\n", node->n.ln_Pri, node->Name));
172 Execute(node->Name, BNULL, BNULL);
175 D(bug("--------------------------\n"));
178 AROS_SH2H(AROSMonDrvs, 1.0, "Load AROS Monitor and Compositor drivers",
179 AROS_SHAH(BOOL, , NOCOMPOSITION,/S,FALSE, "Only load Monitors"),
180 AROS_SHAH(BOOL, , ONLYCOMPOSITION,/S,FALSE, "Only load Compositor"))
182 AROS_SHCOMMAND_INIT
184 APTR pool;
185 struct Library *IconBase;
186 BPTR dir, olddir;
187 BOOL res = TRUE;
189 dir = Lock(MONITORS_DIR, SHARED_LOCK);
190 D(bug("[LoadMonDrvs] Monitors directory 0x%p\n", dir));
191 if (dir)
193 olddir = CurrentDir(dir);
195 if (!SHArg(NOCOMPOSITION))
197 /* Software composition driver is ran first */
198 D(bug("[LoadMonDrvs] Loading composition driver...\n"));
199 Execute(COMPOSITING_NAME, BNULL, BNULL);
202 if (!SHArg(ONLYCOMPOSITION))
204 pool = CreatePool(MEMF_ANY, sizeof(struct MonitorNode) * 10, sizeof(struct MonitorNode) * 5);
205 DB2(bug("[LoadMonDrvs] Created pool 0x%p\n", pool));
206 if (pool)
208 struct List MonitorsList;
210 NewList(&MonitorsList);
211 IconBase = OpenLibrary("icon.library", 0);
212 if (IconBase)
214 findMonitors(&MonitorsList, DOSBase, IconBase, SysBase, pool);
215 loadMonitors(&MonitorsList, DOSBase);
216 CloseLibrary(IconBase);
218 DeletePool(pool);
220 else
221 res = FALSE;
224 CurrentDir(olddir);
225 UnLock(dir);
228 return res;
230 AROS_SHCOMMAND_EXIT