/* Function to scan the system for joysticks. * Joystick 0 should be the system default joystick. * This function should return the number of available joysticks, or -1 * on an unrecoverable fatal error. */ int SDL_SYS_JoystickInit(void) { static ISpDeviceClass classes[4] = { kISpDeviceClass_Joystick, kISpDeviceClass_Gamepad, kISpDeviceClass_Wheel, 0 }; OSErr err; int i; UInt32 count, numJoysticks; if ( (Ptr)0 == (Ptr)ISpStartup ) { SDL_SetError("InputSprocket not installed"); return -1; // InputSprocket not installed } if( (Ptr)0 == (Ptr)ISpGetVersion ) { SDL_SetError("InputSprocket not version 1.1 or newer"); return -1; // old version of ISp (not at least 1.1) } ISpStartup(); /* Get all the joysticks */ numJoysticks = 0; for ( i=0; classes[i]; ++i ) { count = 0; err = ISpDevices_ExtractByClass( classes[i], MAX_JOYSTICKS-numJoysticks, &count, &SYS_Joysticks[numJoysticks]); numJoysticks += count; } for(i = 0; i < numJoysticks; i++) { ISpDevice_GetDefinition( SYS_Joysticks[i], sizeof(ISpDeviceDefinition), &SYS_DevDef[i]); err = ISpElementList_New( 0, NULL, &SYS_Elements[i], 0); if (err) { SDL_OutOfMemory(); return -1; } ISpDevice_GetElementList( SYS_Joysticks[i], &SYS_Elements[i]); } ISpDevices_Deactivate(numJoysticks, SYS_Joysticks); return numJoysticks; }
Word BURGERCALL JoystickInit(void) { Word32 TempLong; Word i; MacInput_t *LocalPtr; LocalPtr = &MacInputLocals; if (!MacInputInit(LocalPtr,MACINITINPUTJOYSTICK)) { /* 1.3 or better? */ /* Discard any previous data from a previous call */ /* This will allow simple rescanning */ DeallocAPointer(LocalPtr->JoystickDescriptionsArray); DeallocAPointer(LocalPtr->JoystickDeviceArray); LocalPtr->JoystickDescriptionsArray = 0; LocalPtr->JoystickDeviceArray = 0; { Word *Boundaries; i = 0; Boundaries = &JoystickBoundaries[0][0]; /* Init pointer to struct */ do { Word j; j = 0; do { Boundaries[AXISMIN] = 0x100000; /* Init center point */ Boundaries[AXISMAX] = 0xF00000; Boundaries[AXISCENTER] = 0x800000; Boundaries+=AXISENTRIES; JoystickSetDigital(j,20,i); /* Create the digital bounds */ } while (++j<AXISCOUNT); } while (++i<MAXJOYNUM); /* All of them checked? */ } MacInputLockInputSprocket(); /* Don't allow mouse/keyboard IRQ's */ if (ISpDevices_Extract(0,&TempLong,0)) { /* How many devices are present? */ goto Abort; } i = TempLong; /* Save in register */ LocalPtr->JoystickDeviceArray = (ISpDeviceReference *)AllocAPointer(sizeof(ISpDeviceReference)*i); if (!LocalPtr->JoystickDeviceArray) { /* Memory allocation error? */ goto Abort; } if (ISpDevices_Extract(i,&TempLong,LocalPtr->JoystickDeviceArray)) { /* Get the devices */ goto Abort; } /* Now remove all devices that are not mice or keyboards */ JoystickPresent = i; /* Save the device count */ if (i) { ISpDeviceReference *ArrayPtr; ISpDeviceReference *ArrayPtr2; ArrayPtr = LocalPtr->JoystickDeviceArray; ArrayPtr2 = ArrayPtr; do { ISpDeviceDefinition ResultBuf; /* Get the device definition */ if (!ISpDevice_GetDefinition(ArrayPtr[0],sizeof(ISpDeviceDefinition),&ResultBuf)) { if (ResultBuf.theDeviceClass != kISpElementLabel_None) { if ((ResultBuf.theDeviceClass != kISpDeviceClass_Mouse) && (ResultBuf.theDeviceClass != kISpDeviceClass_Keyboard)) { ArrayPtr2[0] = ArrayPtr[0]; ++ArrayPtr2; } } } ++ArrayPtr; } while (--i); JoystickPresent = ArrayPtr2-LocalPtr->JoystickDeviceArray; } /* At this point I have all the devices I am going to use. Now */ /* I will get the actual input methods and create a map for each one */ i = JoystickPresent; if (i) { Word j; JoyDesc_t *JoyDevPtr; if (i>MAXJOYNUM) { /* I can only have 4 input devices */ i = MAXJOYNUM; JoystickPresent = MAXJOYNUM; } JoyDevPtr = (JoyDesc_t *)AllocAPointerClear(sizeof(JoyDesc_t)*i); if (!JoyDevPtr) { goto Abort; } j = 0; LocalPtr->JoystickDescriptionsArray = JoyDevPtr; do { /* For each device, scan the input elements and find a burgerlib */ /* match, assign it a button, pad or axis id */ ISpElementListReference MyElementList; if (!ISpDevice_GetElementList(LocalPtr->JoystickDeviceArray[j],&MyElementList)) { Word32 ElementCount; ISpElementReference ElementBuf[500]; if (!ISpElementList_Extract(MyElementList,500,&ElementCount,ElementBuf)) { if (ElementCount) { Word TempIndex; Word FooIndex; Word AxisIndex; Word ButtonIndex; TempIndex = 0; AxisIndex = 0; /* No axis' found */ ButtonIndex = 0; /* No buttons found */ do { ISpElementInfo ElementInfo; ISpElementReference CurrentRef; CurrentRef = ElementBuf[TempIndex]; if (!ISpElement_GetInfo(CurrentRef,&ElementInfo)) { /* I've got input, what kind of input is it? */ switch (ElementInfo.theKind) { case kISpElementKind_Button: /* Simple button */ if (ButtonIndex<20) { JoyDevPtr->Elements[ButtonIndex+ELEMENTBUTTON] = CurrentRef; ++ButtonIndex; } break; /* Hat or pad */ case kISpElementKind_DPad: /* Hat/Pad */ /* If the ID is a hat, place it in the hat slot first */ if (ElementInfo.theLabel==kISpElementLabel_Pad_POV || ElementInfo.theLabel==kISpElementLabel_Pad_POV_Horiz) { if (!JoyDevPtr->Elements[ELEMENTPAD+1]) { JoyDevPtr->Elements[ELEMENTPAD+1] = CurrentRef; break; } } FooIndex = 0; do { if (!JoyDevPtr->Elements[FooIndex+ELEMENTPAD]) { JoyDevPtr->Elements[FooIndex+ELEMENTPAD] = CurrentRef; break; } } while (++FooIndex<2); break; /* Handle joystick axis' */ case kISpElementKind_Axis: /* Joystick axis */ case kISpElementKind_Delta: if (ElementInfo.theLabel==kISpElementLabel_Axis_XAxis) { if (!JoyDevPtr->Elements[ELEMENTAXIS]) { JoyDevPtr->Elements[ELEMENTAXIS] = CurrentRef; JoyDevPtr->ElementData[ELEMENTAXIS] = 0x7FFFFFFF; break; } if (!JoyDevPtr->Elements[ELEMENTAXIS+4]) { JoyDevPtr->Elements[ELEMENTAXIS+4] = CurrentRef; JoyDevPtr->ElementData[ELEMENTAXIS+4] = 0x7FFFFFFF; break; } } else if (ElementInfo.theLabel==kISpElementLabel_Axis_YAxis) { if (!JoyDevPtr->Elements[ELEMENTAXIS+1]) { JoyDevPtr->Elements[ELEMENTAXIS+1] = CurrentRef; JoyDevPtr->ElementData[ELEMENTAXIS+1] = 0x7FFFFFFF; break; } if (!JoyDevPtr->Elements[ELEMENTAXIS+5]) { JoyDevPtr->Elements[ELEMENTAXIS+5] = CurrentRef; JoyDevPtr->ElementData[ELEMENTAXIS+5] = 0x7FFFFFFF; break; } } else if (ElementInfo.theLabel==kISpElementLabel_Axis_ZAxis) { if (!JoyDevPtr->Elements[ELEMENTAXIS+2]) { JoyDevPtr->Elements[ELEMENTAXIS+2] = CurrentRef; JoyDevPtr->ElementData[ELEMENTAXIS+2] = 0x7FFFFFFF; break; } } /* Insert in the generic list */ if (AxisIndex<AXISCOUNT) { do { if (!JoyDevPtr->Elements[AxisIndex+ELEMENTAXIS]) { JoyDevPtr->Elements[AxisIndex+ELEMENTAXIS] = CurrentRef; JoyDevPtr->ElementData[AxisIndex+ELEMENTAXIS] = 0x7FFFFFFF; break; } } while (++AxisIndex<AXISCOUNT); } break; } } } while (++TempIndex<ElementCount); } } } ++JoyDevPtr; } while (++j<i); /* All devices scanned */ } MacInputUnlockInputSprocket(); return JoystickPresent; } Abort:; MacInputUnlockInputSprocket(); JoystickDestroy(); return 0; }