main.c

/*
    File:       main.c
 
    Contains:   it downs the sound level at shutdown time and set it back to its original 
                level at startup time, once the startup sound is executed. 
 
    Written by: Faustino ForcŽn 
 
    Copyright:  Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
#include <Finder.h>
#include <shutdown.h>
#include <script.h>
#include <Files.h>
#include <Folders.h>
#include <Errors.h>
#include <Sound.h>
#include <Processes.h>
#include <LowMem.h>
 
int lee();
pascal void shut_down();
 
int lee()
{   
int     elValor;
FSSpec  spec;
long    elCount;
long    foundDir;
short   vRef;
OSErr   elError;
Str255  nombredisco="\pNoSound.prefs";
short   refNum;
 
    elError = FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder,&vRef, &foundDir);
            // another bendition from sys 7 or "How to find your way home in a drunky night"
    if (elError == noErr)
    {
        FSMakeFSSpec(vRef, foundDir, nombredisco, &spec);   // the brand new calls from sys 7
        elError = FSpOpenDF(&spec, fsRdWrPerm, &refNum);
        if (elError != noErr)                               // we failedÉ
            return(3);                                      // default value
        elCount = 2L;
        FSRead(refNum, &elCount,&elValor);
        FSClose(refNum);
        if(elCount==0)              // file is empty
            elValor=3;              // default value
    }
    else
        elValor=3;
    return(elValor);
}
 
pascal void shut_down()
{
    short   refNum;
    int     valor;  
    SysPPtr elPuntero;
    FSSpec  spec;
    long    elCount;
    long    foundDir;
    OSErr   elError;
    short   vRef;
    Str255  nombredisco;
    
    
    // we haven«t globals at this time, soÉ
    nombredisco[0]=13;
    nombredisco[1]='N';
    nombredisco[2]='o';
    nombredisco[3]='S';
    nombredisco[4]='o';
    nombredisco[5]='u';
    nombredisco[6]='n';
    nombredisco[7]='d';
    nombredisco[8]='.';
    nombredisco[9]='p';
    nombredisco[10]='r';
    nombredisco[11]='e';
    nombredisco[12]='f';
    nombredisco[13]='s';
    // yes, is really dirty, but it works
 
    InitUtil();                 // from the PRAM calls
    elPuntero=GetSysPPtr();
    valor = ((elPuntero->volClik & 0x0700) >> 8);
    elError = FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &vRef, &foundDir);
    if (elError == noErr)
    {
        FSMakeFSSpec(vRef, foundDir, nombredisco, &spec);
        elError = FSpOpenDF(&spec, fsRdWrPerm, &refNum);
        if (elError == fnfErr)          // this is also your first time, baby?
        {
            FSpCreate(&spec, 'SARA', 'VOL ', smSystemScript);   
                // yes, my daughter calls SARA, how you guess it?
            FSpOpenDF(&spec, fsRdWrPerm, &refNum);
        }
        elCount = 2L;
        FSWrite(refNum, &elCount, &valor);
        FSClose(refNum);
    }
        // here«s the real stuff, when we change the vol
    elPuntero->volClik = (elPuntero->volClik & 0xf8ff) + (1 << 8);
    WriteParam();
}
 
void main(void)
{
    long size;
    Ptr puntero,otro, temp;
    char buffer[4];
    int volumen;
    SysPPtr elPuntero;
    
    volumen=lee();  // we want to get the original volume, stored somewhere
    InitUtil();
    elPuntero=GetSysPPtr();
        // we put the vol to the original value
        // in the worst case (no file, some error) it will be 3 (default value)
    elPuntero->volClik = (elPuntero->volClik & 0xf8ff) + (volumen << 8);
    WriteParam();
        // now tell the Mac that we changed its mind
    SetDefaultOutputVolume(volumen);
    
        // now began the install process
    size = (long) ((Ptr) GetApplLimit() - (Ptr) LMGetApplZone());
    puntero = NewPtrSysClear(size);
    if (puntero) 
    {
        temp = (Ptr) shut_down;
        buffer[0] = *(temp+2);
        buffer[1] = *(temp+3);
        buffer[2] = *(temp+4);
        buffer[3] = *(temp+5);
        BlockMove(LMGetApplZone(),puntero,size);
        otro = (Ptr) (long) puntero + (*(long *)buffer - (long) LMGetApplZone());
        ShutDwnInstall(NewShutDwnProc(otro), sdRestartOrPower + sdOnUnmount + sdOnDrivers);
    }
    ExitToShell();
    // we need to assure that our proc is linked with the app
    shut_down();
}
 
// eof