Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / AHI / AddAudioModes / AddAudioModes.c
blobba0b1977e6c2fc47c26ad18b771bd7e8627accc1
1 /*
2 AddAudioModes - Manipulates AHI's audio mode database
3 Copyright (C) 1996-2005 Martin Blom <martin@blom.org>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include <config.h>
22 #include <devices/ahi.h>
23 #include <graphics/modeid.h>
24 #include <graphics/gfxbase.h>
25 #include <intuition/screens.h>
26 #include <proto/ahi.h>
27 #include <proto/dos.h>
28 #include <proto/exec.h>
29 #include <proto/graphics.h>
30 #include <proto/intuition.h>
31 #include <stdlib.h>
32 #include <limits.h>
34 #include "version.h"
36 struct GfxBase *GfxBase = NULL;
37 struct IntuitionBase *IntuitionBase = NULL;
38 struct Library *AHIBase = NULL;
39 struct MsgPort *AHImp = NULL;
40 struct AHIRequest *AHIio = NULL;
41 BYTE AHIDevice = -1;
43 #ifdef __AMIGAOS4__
44 struct AHIIFace *IAHI = NULL;
45 #endif
47 const char version[] = "$VER: AddAudioModes " VERS "\n\r";
49 #define AHIVERSION 4
51 #define TEMPLATE "FILES/M,QUIET/S,REFRESH/S,REMOVE/S,DBLSCAN/S"
53 struct {
54 STRPTR *files;
55 ULONG quiet;
56 ULONG refresh;
57 ULONG remove;
58 ULONG dblscan;
59 } args = {NULL, FALSE, FALSE, FALSE, FALSE};
61 #ifdef __MORPHOS__
62 #define IS_MORPHOS 1
63 #else
64 #define IS_MORPHOS 0
65 #endif
67 void
68 cleanup( void )
70 #ifdef __AMIGAOS4__
71 DropInterface((struct Interface*) IAHI);
72 #endif
74 if( AHIDevice == 0 )
76 CloseDevice( (struct IORequest *) AHIio );
79 DeleteIORequest( (struct IORequest *) AHIio);
80 DeleteMsgPort( AHImp );
82 CloseLibrary( (struct Library*) IntuitionBase );
83 CloseLibrary( (struct Library*) GfxBase );
87 void
88 OpenAHI( void )
90 if( AHIDevice != 0 )
92 AHImp = CreateMsgPort();
94 if( AHImp != NULL )
96 AHIio = (struct AHIRequest *)
97 CreateIORequest( AHImp, sizeof( struct AHIRequest ) );
99 if( AHIio != NULL )
101 AHIio->ahir_Version = AHIVERSION;
103 AHIDevice = OpenDevice( AHINAME,
104 AHI_NO_UNIT,
105 (struct IORequest *) AHIio,
106 AHIDF_NOMODESCAN );
110 if( AHIDevice != 0 )
112 Printf( "Unable to open %s version %ld\n", AHINAME, AHIVERSION );
113 cleanup();
114 exit( RETURN_FAIL );
117 AHIBase = (struct Library *) AHIio->ahir_Std.io_Device;
119 #ifdef __AMIGAOS4__
120 IAHI = (struct AHIIFace *) GetInterface(AHIBase, "main", 1, NULL);
121 #endif
125 // Disable command line processing
126 const long __nocommandline=1;
129 main( void )
131 struct RDArgs *rdargs;
132 int rc = RETURN_OK;
134 GfxBase = (struct GfxBase *) OpenLibrary( GRAPHICSNAME, 37 );
135 IntuitionBase = (struct IntuitionBase *) OpenLibrary( "intuition.library", 37 );
137 if( GfxBase == NULL )
139 Printf( "Unable to open %s version %ld\n", GRAPHICSNAME, 37 );
140 cleanup();
141 return RETURN_FAIL;
144 if( IntuitionBase == NULL )
146 Printf( "Unable to open %s version %ld\n", "intuition.library", 37 );
147 cleanup();
148 return RETURN_FAIL;
151 rdargs = ReadArgs( TEMPLATE , (SIPTR *) &args, NULL );
153 if( rdargs != NULL )
155 /* Refresh database */
157 if( args.refresh && !args.remove )
159 ULONG id;
161 OpenAHI();
163 /* First, empty the database */
165 for( id = AHI_NextAudioID( AHI_INVALID_ID );
166 id != (ULONG) AHI_INVALID_ID;
167 id = AHI_NextAudioID( AHI_INVALID_ID ) )
169 AHI_RemoveAudioMode( id );
172 /* Now add all modes */
174 if( !AHI_LoadModeFile( "DEVS:AudioModes" ) )
176 if( IS_MORPHOS )
178 ULONG res;
180 /* Be quiet here. - Piru */
181 APTR *windowptr = &((struct Process *) FindTask(NULL))->pr_WindowPtr;
182 APTR oldwindowptr = *windowptr;
183 *windowptr = (APTR) -1;
184 res = AHI_LoadModeFile( "MOSSYS:DEVS/AudioModes" );
185 *windowptr = oldwindowptr;
187 if( !res )
189 if( !args.quiet )
191 PrintFault( IoErr(), "AudioModes" );
194 rc = RETURN_ERROR;
197 else
199 if ( !args.quiet )
201 PrintFault( IoErr(), "DEVS:AudioModes" );
204 rc = RETURN_ERROR;
209 /* Load mode files */
211 if( args.files != NULL && !args.remove )
213 int i = 0;
215 OpenAHI();
217 while( args.files[i] )
219 if( !AHI_LoadModeFile( args.files[i] ) && !args.quiet )
221 PrintFault( IoErr(), args.files[i] );
222 rc = RETURN_ERROR;
224 i++;
228 /* Remove database */
230 if( args.remove )
232 if( args.files || args.refresh )
234 PutStr( "The REMOVE switch cannot be used together with FILES or REFRESH.\n" );
235 rc = RETURN_FAIL;
237 else
239 ULONG id;
241 OpenAHI();
243 for( id = AHI_NextAudioID( AHI_INVALID_ID );
244 id != (ULONG) AHI_INVALID_ID;
245 id = AHI_NextAudioID( AHI_INVALID_ID ) )
247 AHI_RemoveAudioMode( id );
252 /* Make display mode doublescan (allowing > 28 kHz sample rates) */
254 if( args.dblscan )
256 ULONG id;
257 ULONG bestid = INVALID_ID;
258 int minper = INT_MAX;
259 struct Screen *screen = NULL;
261 static const struct ColorSpec colorspecs[] =
263 { 0, 0, 0, 0 },
264 { 1, 0, 0, 0 },
265 {-1, 0, 0, 0 }
268 union {
269 struct MonitorInfo mon;
270 struct DisplayInfo dis;
271 } buffer;
273 for( id = NextDisplayInfo( INVALID_ID );
274 id != (ULONG) INVALID_ID;
275 id = NextDisplayInfo( id ) )
277 int period;
279 if( GetDisplayInfoData( NULL,
280 (UBYTE*) &buffer.dis, sizeof(buffer.dis),
281 DTAG_DISP, id ) )
283 if( !(buffer.dis.PropertyFlags & (DIPF_IS_ECS | DIPF_IS_AA ) ) )
285 continue;
289 if( GetDisplayInfoData( NULL,
290 (UBYTE*) &buffer.mon, sizeof(buffer.mon),
291 DTAG_MNTR, id ) )
293 period = buffer.mon.TotalColorClocks * buffer.mon.TotalRows
294 / ( 2 * ( buffer.mon.TotalRows - buffer.mon.MinRow + 1 ) );
296 if( period < minper )
298 minper = period;
299 bestid = id;
305 if( bestid != (ULONG) INVALID_ID && minper < 100 )
307 screen = OpenScreenTags( NULL,
308 SA_DisplayID, bestid,
309 SA_Colors, &colorspecs,
310 TAG_DONE );
312 else if( ( GfxBase->ChipRevBits0 & (GFXF_HR_DENISE | GFXF_AA_LISA ) ) != 0 )
314 /* No suitable screen mode found, let's bang the hardware...
315 Using code from Sebastiano Vigna <vigna@eolo.usr.dsi.unimi.it>. */
317 struct Custom *custom = (struct Custom *) 0xdff000;
319 custom->bplcon0 = 0x8211;
320 custom->ddfstrt = 0x0018;
321 custom->ddfstop = 0x0058;
322 custom->hbstrt = 0x0009;
323 custom->hsstop = 0x0017;
324 custom->hbstop = 0x0021;
325 custom->htotal = 0x0071;
326 custom->vbstrt = 0x0000;
327 custom->vsstrt = 0x0003;
328 custom->vsstop = 0x0005;
329 custom->vbstop = 0x001D;
330 custom->vtotal = 0x020E;
331 custom->beamcon0 = 0x0B88;
332 custom->bplcon1 = 0x0000;
333 custom->bplcon2 = 0x027F;
334 custom->bplcon3 = 0x00A3;
335 custom->bplcon4 = 0x0011;
338 if( screen != NULL )
340 CloseScreen( screen );
344 FreeArgs( rdargs );
347 cleanup();
348 return rc;
351 #if defined(__mc68000__) && defined(__libnix__)
352 void __main(void) {}
353 #endif