Offer to unmount a volume whenever a new disk is inserted while tip is running.

This commit is contained in:
Marcio T 2021-11-25 16:54:59 -07:00
parent 1e4ce62177
commit 85eb5bc52c
5 changed files with 225 additions and 3 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <Files.h>
#include "mac_vol.h"
#include "pstring.h"
void mac_list_volumes() {
HParamBlockRec paramBlock;
@ -18,6 +19,52 @@ void mac_list_volumes() {
}
}
OSErr mac_get_drive_volumes(int driveNum, Str255 str) {
HParamBlockRec paramBlock;
Str255 volName;
Boolean first = true;
paramBlock.volumeParam.ioCompletion = 0;
paramBlock.volumeParam.ioNamePtr = volName;
paramBlock.volumeParam.ioVRefNum = 0;
paramBlock.volumeParam.ioVolIndex = 0;
for (;;) {
OSErr err = PBHGetVInfo(&paramBlock, false);
if (err == nsvErr) return noErr;
if (err != noErr) return err;
if (paramBlock.volumeParam.ioVDrvInfo == driveNum) {
if (!first) pstrcat(str, "\p, ");
pstrcat(str, volName);
first = false;
}
paramBlock.volumeParam.ioVolIndex++;
}
}
OSErr mac_unmount_drive(int driveNum) {
HParamBlockRec paramBlock;
paramBlock.volumeParam.ioCompletion = 0;
paramBlock.volumeParam.ioNamePtr = 0;
paramBlock.volumeParam.ioVRefNum = 0;
paramBlock.volumeParam.ioVolIndex = 0;
for (;;) {
OSErr err = PBHGetVInfo(&paramBlock, false);
if (err == nsvErr) return noErr;
if (err != noErr) return err;
if (paramBlock.volumeParam.ioVDrvInfo == driveNum) {
// We found a drive to unmount
err = UnmountVol(0, paramBlock.volumeParam.ioVRefNum);
if (err != noErr) return err;
// Continue searching from the top of the list
paramBlock.volumeParam.ioVolIndex = 0;
continue;
}
paramBlock.volumeParam.ioVolIndex++;
}
}
void mac_unmount(int id) {
HParamBlockRec paramBlock;
paramBlock.volumeParam.ioCompletion = 0;

View File

@ -1,2 +1,4 @@
void mac_list_volumes();
void mac_unmount(int id);
OSErr mac_get_drive_volumes(int driveNum, Str255 str);
OSErr mac_unmount_drive(int driveNum);

View File

@ -7,6 +7,7 @@
#include <Windows.h>
#include <Quickdraw.h>
#include "mac_vol.h"
#include "tip.h"
WindowPtr tipWindow;
@ -23,6 +24,7 @@ void DoEvent(EventRecord &event, RgnHandle *cursorRgn);
void DoUpdate(WindowPtr window);
void DoMouseDown(EventRecord &event);
void DoMouseMove(EventRecord &event, RgnHandle *cursorRegion);
void DoDiskEvent(EventRecord &event);
void run_tip(int id) {
CurrentDevice = id;
@ -52,8 +54,8 @@ void NewTipWindow() {
AllowColor = theWorld.hasColorQD;
Rect rect = qd.screenBits.bounds;
rect.left = 7;
rect.top = 43;
rect.left = 8;
rect.top = GetMBarHeight() + rect.left + 16;
rect.bottom = rect.top + 336 - 35;
rect.right = rect.left + 467;
@ -105,7 +107,8 @@ void DoEvent(EventRecord &event, RgnHandle *cursorRgn) {
switch(event.what) {
case mouseDown: DoMouseDown(event); break;
case updateEvt: DoUpdate((WindowPtr)event.message); break;
case osEvt: DoMouseMove(event, cursorRgn); break;
case diskEvt: DoDiskEvent(event); break;
case osEvt: DoMouseMove(event, cursorRgn); break;
}
}
@ -182,6 +185,32 @@ void DoMouseMove(EventRecord &event, RgnHandle *cursorRgn) {
}
}
void DoDiskEvent(EventRecord &event) {
OSErr mountErr = HiWord(event.message);
short driveNum = LoWord(event.message);
if (mountErr == noErr) {
// Get the volume name of recently mounted drive
Str255 volumes;
mac_get_drive_volumes(driveNum, volumes);
// Ask the user whether they want to unmount the disk
ParamText(volumes, "\p", "\p", "\p");
if (CautionAlert(128, NULL) == 1) {
// The user wishes to unmount the disk
OSErr err = mac_unmount_drive(driveNum);
if(err != noErr) {
if(err == fBsyErr) {
ParamText("\pFailed to unmount. One or more files are open.", "\p", "\p", "\p");
} else {
ParamText("\pFailed to unmount.", "\p", "\p", "\p");
}
StopAlert(129, NULL);
}
}
}
}
void StrToPascal(Str255 pStr, const char *str) {
size_t len = strlen(str);
pStr[0] = (len > 255) ? 255 : len;

View File

@ -0,0 +1,121 @@
/************************************************************
pstring.c
AUTHOR: Marcio Luis Teixeira
CREATED: 9/17/94
LAST REVISION: 11/25/21
(c) 1994-1995 by Marcio Luis Teixeira.
All rights reserved.
*************************************************************/
#include "pstring.h"
#include <ctype.h>
short pstrlen( unsigned char *str ) {
return (short) *str;
}
void psetlen( unsigned char *str, short len ) {
*str = (unsigned char) len;
}
short pstrcmp( unsigned char *str1, unsigned char *str2 ) {
unsigned char i;
i = *str1;
while (i--)
if (*str1++ != *str2++)
return 1;
return 0;
}
short pstricmp( unsigned char *str1, unsigned char *str2 ) {
unsigned char i;
if (*str1 != *str2)
return 1;
i = *str1;
while (i--)
if (tolower( *str1++ ) != tolower( *str2++ ))
return 1;
return 0;
}
short pstrmemcmp( unsigned char *str, unsigned char *mem ) {
unsigned char i;
i = *str;
str++;
while (i--)
if (*str++ != *mem++)
return 1;
return 0;
}
short pstrmemicmp( unsigned char *str, unsigned char *mem ) {
unsigned char i;
i = *str;
str++;
while (i--)
if (tolower( *str++ ) != tolower( *mem++ ))
return 1;
return 0;
}
unsigned char * pstrcat( unsigned char *str1, unsigned char *str2 ) {
short len1;
short len2;
len2 = pstrlen( str2 );
len1 = pstrlen( str1 );
if (len1 + len2 > 255) return str1;
BlockMove( str2 + 1, str1 + len1 + 1, len2 );
psetlen( str1, len1 + len2 );
return str1;
}
unsigned char * pstrcpy( unsigned char *str1, unsigned char *str2 ) {
*str1 = '\0';
str1 = pstrcat( str1, str2 );
return str1;
}
unsigned char *pstrparams( unsigned char *str,
unsigned char *p0, unsigned char *p1,
unsigned char *p2, unsigned char *p3 ) {
short len;
unsigned char *c;
len = pstrlen( str );
c = str;
while( (len--) > 0 ) {
c++;
if( *c == '^' ) {
short pLen;
unsigned char *pStr;
switch( *(c+1) ) {
case '0': pStr = p0; break;
case '1': pStr = p1; break;
case '2': pStr = p2; break;
case '3': pStr = p3; break;
default: continue;
}
if( pStr == NULL ) continue;
pLen = pstrlen( pStr );
BlockMove( c + 2, c + pLen, len - 1 );
BlockMove( pStr + 1, c, pLen );
c += pLen;
psetlen( str, pstrlen( str ) + pLen - 2 );
len -= 2;
}
}
return str;
}

View File

@ -0,0 +1,23 @@
/************************************************************
pstring.h
AUTHOR: Marcio Luis Teixeira
CREATED: 9/17/94
LAST REVISION: 11/25/21
(c) 1994-1995 by Marcio Luis Teixeira.
All rights reserved.
*************************************************************/
short pstrlen( unsigned char *str );
void psetlen( unsigned char *str, short len );
short pstrcmp( unsigned char *str1, unsigned char *str2 );
short pstricmp( unsigned char *str1, unsigned char *str2 );
unsigned char * pstrcat( unsigned char *str1, unsigned char *str2 );
unsigned char * pstrcpy( unsigned char *str1, unsigned char *str2 );
unsigned char * pstrparams( unsigned char *str, unsigned char *p0, unsigned char *p1, unsigned char *p2, unsigned char *p3 );
short pstrmemcmp( unsigned char *str, unsigned char *mem );
short pstrmemicmp( unsigned char *str, unsigned char *mem );