home *** CD-ROM | disk | FTP | other *** search
- /*
- ***********************************************************************
- *
- * PRAM guardian
- * Reading Parameter RAM (PRAM) and setting/resetting it
- *
- * The program is intended to display the contents of the parameter RAM,
- * a non-volatile RAM that stores some system parameters like the system font
- * number and the speaker volume. The program also has some "standard" PRAM
- * settings stored as a 'HEXA' resource named "Standard PRAM".
- * The program displays both actual and standard PRAM settings side-by-side,
- * shows difference(s) in a noticeable form, and can copy one setting onto
- * another and save it.
- *
- ***********************************************************************
- */
-
- /* MacHeaders Included */
-
- #include <string.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include "myenv.h"
-
-
- /*
- *-----------------------------------------------------------------------------------
- * Handling the dialog
- */
-
- class ADialog // Handle A Mac modal dialog
- {
- protected:
- DialogPtr the_dialog;
- public:
- ADialog(const short dialog_id);
- ~ADialog(void);
- void printf(const int item_no, const char * fmt_str,...); // Put a formatted text into a dialog item
- void set_onoff(const int item_no,const Boolean val); // Set an on/off item
- void hilight(const int item_no, const Boolean onoff); // Hilight an item
- void enable_item(const int item_no, const Boolean onoff); // Enable/Disable the control (button)
- };
-
-
- // Creating a dialog with a given resource ID
- ADialog::ADialog(const short dialog_id)
- {
- the_dialog = GetNewDialog(dialog_id,nil,(WindowPtr)(-1));
- assert( the_dialog != 0 );
- }
-
-
- // Dispose of the dialog
- ADialog::~ADialog(void)
- {
- assert( the_dialog != 0 );
- DisposDialog(the_dialog);
- the_dialog = 0;
- }
-
- // Put a formatted text into a dialog item
- // Note that the function is smart enough to
- // tell a static text item from a button
- // and set up the text or button title
- // appropriately
- void ADialog::printf(const int item_no, const char * fmt_str,...)
- {
- OSErr syserr;
-
- char buffer [400];
- va_list args;
- va_start(args,fmt_str); // Init 'args' to the beginning of
- // the variable length list of args
- vsprintf(buffer,fmt_str,args);
- CtoPstr(buffer);
-
- short item_type;
- Handle item;
- Rect boundary_box;
-
- GetDItem(the_dialog,item_no,&item_type,&item,&boundary_box);
- assert( item != nil );
- if( (item_type & (~itemDisable)) == statText )
- SetIText(item,(unsigned char const *)buffer);
- else if( (item_type & ctrlItem) == ctrlItem ) // If an item is a button, set its title
- SetCTitle((ControlHandle)item,(unsigned char const *)buffer);
- else
- _error("Can't handle an item of type %d",item_type);
- }
-
- // Set an on/off item (it has to be a button)
- void ADialog::set_onoff(const int item_no,const Boolean val)
- {
- short item_type;
- Handle item;
- Rect boundary_box;
-
- GetDItem(the_dialog,item_no,&item_type,&item,&boundary_box);
- assert( item != nil );
- assert( (item_type & ctrlItem) == ctrlItem );
- SetCtlValue((ControlHandle)item, val ? 1 : 0 );
- }
-
- // Hilight a dialog item
- // Again, if the item turns out to be a text item,
- // the entire text is highlighted.
- // If the item is a button, it's highlighted
- void ADialog::hilight(const int item_no, const Boolean onoff)
- {
- short item_type;
- Handle item;
- Rect boundary_box;
-
- GetDItem(the_dialog,item_no,&item_type,&item,&boundary_box);
- assert( item != nil );
- if( (item_type & (~itemDisable)) == statText )
- {
- if( (**(TEHandle)item).selEnd > 0 ) // The text has been selected
- if( onoff )
- ; // Item is already selected
- else
- SelIText(the_dialog,item_no,0,0); // Unselect the text
- else if ( onoff )
- SelIText(the_dialog,item_no,0,32767); // Select the text
- }
- else if( (item_type & ctrlItem) == ctrlItem ) // If an item is a button, set its title
- HiliteControl((ControlHandle)item, onoff ? inButton : 0 );
- else
- _error("Can't handle an item of type %d",item_type);
- }
-
-
- // Enable/Disable the control (button)
- void ADialog::enable_item(const int item_no, const Boolean onoff)
- {
- short item_type;
- Handle item;
- Rect boundary_box;
-
- GetDItem(the_dialog,item_no,&item_type,&item,&boundary_box);
- assert( item != nil );
- assert( (item_type & ctrlItem) == ctrlItem );
- HiliteControl((ControlHandle)item, onoff ? 0 : 255 );
- }
-
-
- class MyDialog : ADialog // Handle THE dialog of the present program
- {
- Boolean this_machine_selected; // Item "this computer's PRAM" is selected
- public:
-
- enum Items { item_quit = 2, item_copy=3,
- item_this_computer=19, item_standard=35 }; // Selectable items
- enum { pi_atalkA=1, pi_atalkB, pi_eligible_dev, pi_modem_port_conf,
- pi_printer_port_conf, pi_alarm, pi_font, pi_auto_key,
- pi_printer_port, pi_speaker_vol, pi_click_times, pi_mouse_scaling,
- pi_external_startup_disk, pi_menu_blink_rate, pi_save };
-
- MyDialog(void);
- ~MyDialog(void) {}
- Items handle(void); // Handle the dialog, return the clicked item that can't handle
- Boolean is_this_machine_selected(void) const { return this_machine_selected; }
- };
-
- MyDialog::MyDialog(void)
- : ADialog(128)
- {
- this_machine_selected = TRUE;
- hilight(item_this_computer,TRUE);
- printf(item_copy,">>> copy >>>");
- }
-
- MyDialog::Items MyDialog::handle(void)
- {
- short item_hit;
- while(1)
- {
- ModalDialog(nil,&item_hit);
- switch(item_hit)
- {
- case item_this_computer:
- this_machine_selected = TRUE;
- hilight(item_this_computer,TRUE);
- hilight(item_standard,FALSE);
- printf(item_copy,">>> copy >>>");
- continue;
-
- case item_standard:
- this_machine_selected = FALSE;
- hilight(item_this_computer,FALSE);
- hilight(item_standard,TRUE);
- printf(item_copy,"<<< copy <<<");
- continue;
- }
- break;
- }
- return (Items)item_hit;
- }
-
- /*
- *-----------------------------------------------------------------------------------
- * PRAM classes
- */
-
-
- class PRAM
- {
- typedef unsigned short ushort;
- typedef unsigned char byte;
-
- protected: // This is a layout of the Parameter RAM.
- byte valid; // Check agains struct SysParmType in <OsUtils.h>
- byte aTalkA;
- byte aTalkB;
- byte config;
- ushort portA;
- ushort portB;
- unsigned long alarm; // Alarm clock setting
- short font; // System font number minus 1
- struct {
- ushort auto_key_threshold : 4; // in 4-tick units
- ushort auto_key_rate : 4; // in two-tick units
- ushort reserved : 7;
- ushort printer_connection : 1; // 1 - the printer is connected to the modem port
- } kbdPrint;
- struct {
- ushort reserved : 5;
- ushort speaker_volume : 3; // 0 (silent) through 7 (loud)
- ushort double_click_time : 4; // in 4-tick units
- ushort caret_blink_time : 4; // in 4-tick units
- } volClik;
- struct {
- ushort reserved : 9;
- ushort mouse_scaling : 1;
- ushort reserved2 : 1;
- ushort startup_disk_ext : 1; // 1 means use an external disk to boot up
- ushort menu_blink_times : 2;
- ushort reserved3 : 2;
- } misc;
-
- Boolean modified; // Modification flag
- void save(const SysPPtr pram_ptr); // Saved the modified PRAM back to where it
- // was found in the first place
-
- public:
-
- PRAM (const SysPPtr pram_ptr); // Load up the data to the structure
- ~PRAM(void) {}
- };
-
- // Load up (i.e., copy) the data to this class
- // using the supplied ptr
- PRAM::PRAM(const SysPPtr pram_ptr)
- {
- assert( pram_ptr != 0 );
- memcpy(&valid,pram_ptr,sizeof(SysParmType));
- assert( valid == 0xA8 );
- modified = FALSE;
- }
-
- // Save (i.e., copy) the data from this class
- // to the supplied ptr
- // Clear the modification bit after the operation
- void PRAM::save(const SysPPtr pram_ptr)
- {
- assert( pram_ptr != 0 );
- assert( valid == 0xA8 );
- assert( modified );
- memcpy(pram_ptr,&valid,sizeof(SysParmType));
- modified = FALSE;
- }
-
- // A class to display the PRAM
- class PRAM_Display : protected PRAM
- {
- ADialog& dialog; // A dialog to display the PRAM
- const short starting_item_no; // where the contents of PRAM is to be displayed
-
- public:
- PRAM_Display(const SysPPtr pram_ptr, ADialog& _dialog, const int _starting_item_no);
- ~PRAM_Display(void) {}
- void display(void);
- };
-
- PRAM_Display::PRAM_Display(const SysPPtr pram_ptr, ADialog& _dialog, const int _starting_item_no)
- : PRAM(pram_ptr), starting_item_no(_starting_item_no), dialog(_dialog)
- {
- assert( starting_item_no > 0 );
- }
-
- // Display the PRAM settings in the consecutive
- // dialog boxes starting with starting_item_no+1
- void PRAM_Display::display(void)
- {
- //message("kbdPrint %04x",*(ushort *)&kbdPrint);
- //message("volClick %04x",*(ushort *)&volClik);
- //message("misc %04x",*(ushort *)&misc);
- dialog.printf(starting_item_no+MyDialog::pi_atalkA,"%X",aTalkA);
- dialog.printf(starting_item_no+MyDialog::pi_atalkB,"%X",aTalkB);
-
- dialog.printf(starting_item_no+MyDialog::pi_eligible_dev,"%X",config);
- dialog.printf(starting_item_no+MyDialog::pi_modem_port_conf,"%04X",portA);
- dialog.printf(starting_item_no+MyDialog::pi_printer_port_conf,"%04X",portB);
- dialog.printf(starting_item_no+MyDialog::pi_alarm,"%6u",alarm);
- dialog.printf(starting_item_no+MyDialog::pi_font,"%2d",font+1);
-
- dialog.printf(starting_item_no+MyDialog::pi_auto_key,"%d/%d",4*kbdPrint.auto_key_threshold,
- 4*kbdPrint.auto_key_rate);
- dialog.printf(starting_item_no+MyDialog::pi_printer_port,"%s",kbdPrint.printer_connection ?
- "Printer" : "Modem");
- dialog.printf(starting_item_no+MyDialog::pi_speaker_vol,"%1u",volClik.speaker_volume);
- dialog.printf(starting_item_no+MyDialog::pi_click_times,"%d/%d",4*volClik.double_click_time,
- 4*volClik.caret_blink_time);
- dialog.set_onoff(starting_item_no+MyDialog::pi_mouse_scaling,misc.mouse_scaling);
- dialog.set_onoff(starting_item_no+MyDialog::pi_external_startup_disk,misc.startup_disk_ext);
- dialog.printf(starting_item_no+MyDialog::pi_menu_blink_rate,"%1u",misc.menu_blink_times);
-
- dialog.enable_item(starting_item_no+MyDialog::pi_save,modified);
- }
-
- // PRAM that is gotten from the system
- class ActualPRAM : public PRAM_Display
- {
- public:
- ActualPRAM(ADialog& dialog, const int starting_item_no);
- ~ActualPRAM(void) {}
- void save(void); // Save changes made within the structure to PRAM
- void operator = (const PRAM& pram) { *(PRAM *)this = pram; modified = TRUE; }
- };
-
-
- ActualPRAM::ActualPRAM(ADialog& dialog, const int starting_item_no)
- : PRAM_Display(GetSysPPtr(),dialog,starting_item_no)
- {
- }
-
- // Save changes made within the structure to PRAM
- void ActualPRAM::save(void)
- {
- PRAM::save(GetSysPPtr());
- do_well( WriteParam() );
- }
-
-
- // PRAM that is gotten from the "Standard PRAM"
- // resource
- class StandardPRAM : public PRAM_Display
- {
- Handle handle;
- public:
- StandardPRAM(ADialog& dialog, const int starting_item_no);
- ~StandardPRAM(void) {}
- void save(void); // Save changes made within the structure to PRAM
- void operator = (const PRAM& pram) { *(PRAM *)this = pram; modified = TRUE; }
- };
-
-
- StandardPRAM::StandardPRAM(ADialog& dialog, const int starting_item_no)
- : PRAM_Display((SysPPtr)*(handle=Get1NamedResource('HEXA',"\pStandard PRAM")),
- dialog,starting_item_no)
- {
- assert( SizeResource(handle) == sizeof(SysParmType) );
- }
-
- // Save changes made within the structure to PRAM
- void StandardPRAM::save(void)
- {
- PRAM::save((SysPPtr)*handle);
- ChangedResource(handle);
- do_well( ResError() );
- WriteResource(handle);
- do_well( ResError() );
- }
-
-
- /*
- *-----------------------------------------------------------------------------------
- * Handling the dialog
- */
-
- /*
- *-----------------------------------------------------------------------------------
- * Routing module
- */
-
- extern "C" char * ExtPRAM(void);
-
- void main(void)
- {
- Initialize_MAC();
-
- MyDialog dialog;
- ActualPRAM this_computer_pram(dialog,MyDialog::item_this_computer);
- StandardPRAM standard_PRAM(dialog,MyDialog::item_standard);
-
- this_computer_pram.display();
- standard_PRAM.display();
-
- #if 1
- {
- char * buf = ExtPRAM();
- message("Another extparm %0x %0x %0x",*((unsigned long *)buf),
- *((unsigned long *)buf+1),
- *((unsigned long *)buf+2));
- //message("Another extparm %0x",*((unsigned long *)buf));
- message("Another volclick %0x",*((unsigned short *)buf+16));
- message("Another beep %0x",*(unsigned short *)(buf+0x7c));
- }
- #endif
- for(;;)
- {
- switch(dialog.handle())
- {
- case MyDialog::item_copy:
- if( dialog.is_this_machine_selected() ) // Copy to standard
- {
- standard_PRAM = this_computer_pram;
- standard_PRAM.display();
- }
- else
- {
- this_computer_pram = standard_PRAM;
- this_computer_pram.display();
- }
- continue;
-
- case MyDialog::item_this_computer+MyDialog::pi_save:
- this_computer_pram.save();
- this_computer_pram.display();
- continue;
-
- case MyDialog::item_standard+MyDialog::pi_save:
- standard_PRAM.save();
- standard_PRAM.display();
- continue;
-
- case MyDialog::item_quit:
- break;
-
- default:
- continue;
- }
- break;
- }
-
- }
-