TweetFollow Us on Twitter

ADB Count
Volume Number:6
Issue Number:5
Column Tag:XCMD Corner

Related Info: ADB Manager

CountADBs and Reality

By Donald Koscheka, , Mark Armstrong

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

This column strives to present interesting insights into enhancing Hypercard through XCMDs. When I started writing this column almost two years ago, I was apprehensive; how would I come up with a new idea each month year in and year out. The answer to this question was something of a revelation to me -- once I decided to keep coming up with new ideas, I began looking in new places for those ideas. No part of Macintosh programming is off-limits, I explore everything. Of course there are limits to what one person can do and I find myself very often writing a column in response to some question asked by a colleague or a reader.

I’ve decided that it’s time to go one step further. Beginning this month, intermittently offer this space for “guest XCMDs” such as Mark Armstrong’s following ADB externals. The guest columnist idea appeals to me for two reasons: (1) it broadens the column’s “reach” by discussing areas that I may have not explored myself and (2) it provides you with different perspectives and coding styles so that you can see how others have solved specific problems in an XCMD. This is an open invitation. If you would like to have your XCMD published, send it to me via AppleLink (N0735) or America OnLine (AFC Donald) or to the Editor. Use Macwrite or TEXT only. I will respond as quickly as possible to inform you as to whether I can use the article or not. A few rules: If I can use the material, I will edit it and return it to you for approval. I edit for style and grammar, expect to have some work done to your prose. Don’t submit anything that you don’t want in the public domain and don’t submit any article without source code. You may use whatever language you like. Give yourself a “short plug” at the beginning of the column so that other readers get to know you better.

This month, I would like to introduce Mark Armstrong, Vice President of Research and Development at Pharos Technologies, Inc, a software development and systems integration company in Cincinnati, Ohio. Mark has written one commercial engineering application on the Macintosh called “UNITize” and has contributed to the arcade-style game “Marble Madness”. With his physics degree firmly in hand, he still wonders how he got mixed up in all this.

CountADBs and Reality

In some circumstances (such as on the factory floor), there is a need to monitor the Macintosh to determine whether the mouse or keyboard devices become detached from ADB. Some software may modify their behavior if certain input devices are available, hiding or showing functionality based on that result. Below are two simple XFCN’s, developed in Think C 4.0, that tell you if the mouse and keyboard are connected.

According to Inside Macintosh Volume 5, the function CountADBs() “returns a value representing the number of devices connected to the Apple Desktop Bus (ADB) by counting the number of entries in the device table....” The important word is representing. It does not really tell you what is connected out there on the bus. Consequently, even if you have no devices hooked up to ADB, CountADBs() will return a value of 2. This happens because the system always installs the keyboard and mouse drivers regardless of whether they are physically resident. To find out what is really connected to ADB you can poll devices that you believe are connected to the ADB. Connected devices will respond to the poll.

Before going too far, you must determine whether the host machine is equipped for ADB. The following call to SysEnvirons() determines whether ADB is installed.

/* 1 */

Boolean ADBExists()
 {
 SysEnvRectheWorld;
 OSErr  err;
 
 err = SysEnvirons(1,&theWorld);
 if (err) return (FALSE);
 else
 {
if ((theWorld.machineType >= 0) && (theWorld.machineType < 3)) 
 return (FALSE);
 else return (TRUE);
 }
 }

Once you have determined that ADB exists on your machine, you check to see what devices are actually there by looping from 1 to the maximum allowable number of ADB devices (16). Inside the loop, you call ADBIndAvail() which returns TRUE if the specified device is physically connected and functional or FALSE otherwise. ADBIndAvail() takes a device specified by the index and sends a command via ADBOp() to that device to see if it is responding. If the device receives the operation, it will signal that it has done so by executing a completion routine. If the completion routine ADBOp() doesn’t execute, then the device did not receive the message, and we can assume that the device is dysfunctional or disconnected. The completion routine ADBComplete() sets a global variable to TRUE. If completion does not occur, then the operation times out and the global variable remains FALSE. In the following code, we use the first byte of Scratch8 as the global variable. We chose to use Scratch8 because this code was developed for a Think C 4.0 XCMD where A4 had already been pushed using SetUpA4(). In most cases, a regular global variable will do the trick.

/* 2 */

#define ADB_TIMEOUT10000

extern char Scratch8[]  : 0x9FA;

ADBComplete(){
 *(char *)Scratch8 = TRUE;
}

ADBIndAvail(index)
 short  index;
 {
 ADBAddress addrs;
 ADBDataBlock  devBlock;
 OSErr  err;
 short  cmdNum;
 Str255 ADBData;
 
 addrs = GetIndADB(&devBlock,index);
 cmdNum = ((addrs*16)+0xF);
 ADBData[0] = 0;
 ADBData[1] = 0;
 ADBData[2] = 0;
 
 *(char *)Scratch8 = FALSE;
 err = ADBOp(NIL,ADBComplete,&ADBData,cmdNum);
 if (!err){
 short I= 0;
 do
 if (++I > ADB_TIMEOUT) *(char *)Scratch8 = TRUE; 
 while (!*(char *)Scratch8);
 if (ADBData[0] != 0) return (TRUE);
 }
 return (FALSE);
}

In both cases below, the first portion of the code checks to see if the first and only argument is a question mark (?) or an exclamation point (!). If so, the XFCN responds appropriately as suggested by MacDTS.

The XFCN checks first to see if ADB exists on this machine and returns an error code if it does not. Next, it loops through all the devices to see if any one of them is a responding mouse. If it finds one, it returns TRUE, otherwise it completes the loop and returns FALSE. Each time through the loop, we call GetIndADB(), as directed in Inside Mac, to determine if the device in question is, indeed, a mouse.

/* 3 */

#include “MacTypes.h”
#include “HyperXCmd.h”
#include “DeskBus.h”
#include “SetUpA4.h”

#define ADB_KEYBOARD 2
#define ADB_MOUSE3

#define STANDARD_KBD 1
#define EXTENDED_KBD 2

#define MAX_ADB_DEVICES 16

pascal main(paramPtr)
   XCmdBlockPtr  paramPtr;
   {
   Str255 str;
   shortix;
   
   RememberA0();
   SetUpA4();
   
   if(paramPtr->paramCount == 1){
   ZeroToPas(paramPtr,*((unsigned char **)paramPtr->params[0]),str);
      if (str[0] == 1){
        if (str[1] == ‘?’) 
 pstrcpy(str,”\pKBDAvail()”);
        else if (str[1] == ‘!’) 
 pstrcpy(str,”\pv1.0; © Pharos Technologies, Inc. 1989");
      }
      goto Done;
   }
   
   if (!ADBExists()){ 
 pstrcpy(str,”\p0\rnoADB”); 
 goto Done; 
 }
   
 for ( ix  =1; ix <=MAX_ADB_DEVICES; ix ++){
 ADBDataBlock    ADBinfo;
 ADBAddress ADBaddr;
 
 if (!ADBIndAvail( ix )) continue;
 ADBaddr = GetIndADB(&ADBinfo,I);
 if (ADBaddr <= 0) continue;
 if (ADBinfo.origADBAddr == ADB_MOUSE){ 
 pstrcpy(str,”\ptrue”); 
 goto Done; 
 }
 }
 pstrcpy(str,”\pfalse”);
Done:
   paramPtr->returnValue = PasToZero(paramPtr,(StringPtr)str);
   RestoreA4();
}

A similar routine checks for keyboard devices. This routine not only checks to see if it is a keyboard, but also returns the type of keyboard by examining the devType field of ADBInfo.

/* 4 */

#include “MacTypes.h”
#include “HyperXCmd.h”
#include “DeskBus.h”
#include “SetUpA4.h”

#define ADB_KEYBOARD 2
#define ADB_MOUSE3
#define STANDARD_KBD 1
#define EXTENDED_KBD 2
#define MAX_ADB_DEVICES 16

pascal main(paramPtr)
   XCmdBlockPtr  paramPtr;
   {
   Str255 str;
   shortI;
   
   RememberA0();
   SetUpA4();
   
   if (paramPtr->paramCount == 1){
   ZeroToPas(paramPtr,*((unsigned char **)paramPtr->params[0]),str);
      if (str[0] == 1){
        if (str[1] == ‘?’) 
 pstrcpy(str,”\pKBDAvail()”);
        else if (str[1] == ‘!’) 
 pstrcpy(str,”\pv1.0; © Pharos Technologies, Inc. 1989");
      }
      goto Done;
    }
   
   if (!ADBExists()){ 
 pstrcpy(str,”\p0\rnoADB”); 
 goto Done; 
 }
   
 for (I=1;I<=MAX_ADB_DEVICES;I++){
 ADBDataBlock    ADBinfo;
 ADBAddress ADBaddr;
 
 if (!ADBIndAvail(I)) continue;
 ADBaddr = GetIndADB(&ADBinfo,I);
 if (ADBaddr <= 0) continue;

 if (ADBinfo.origADBAddr == ADB_KEYBOARD){
 if (ADBinfo.devType == STANDARD_KBD){ 
 pstrcpy(str,”\ptrue,standard”); 
 goto Done; 
 }
 else if (ADBinfo.devType == EXTENDED_KBD){ 
 pstrcpy(str,”\ptrue,extended”); 
 goto Done; 
 }
 else{ 
 pstrcpy(str,”\ptrue,unknown”); 
 goto Done; 
 }
 }
 }
 pstrcpy(str,”\pfalse”);
Done:
   paramPtr->returnValue = PasToZero(paramPtr,(StringPtr)str);
   RestoreA4();
   }

Once you know how to identify a particular device, it is easy to check to see if it is present on ADB. If you don’t know how to identify a given device, then the code above can be easily modified to return all devices found on ADB. By the way, I would like to give special thanks and credit to Cameron Birse. If you don’t recognize the name from any number of “sources”, then never mind.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »

Price Scanner via MacPrices.net

Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.