JNIEXPORT jlongArray JNICALL Java_jdrlib_JDRDevice_nativeGetDevices(JNIEnv *env, jclass cls) { CFArrayRef devs = DRCopyDeviceArray(); CFIndex i, max = CFArrayGetCount(devs); jlongArray res; jlong *devsArr; devsArr = calloc(max, 8); if (devsArr == NULL) { return NULL; /* out of memory error thrown */ } res = (*env)->NewLongArray(env, max); if (res == NULL) { return NULL; /* out of memory error thrown */ } for (i = 0; i < max; i++) { long dev = (long)CFArrayGetValueAtIndex(devs, i); devsArr[i] = dev; //printf("NDEBUG: %ld\n", dev); } (*env)->SetLongArrayRegion(env, res, 0, max, devsArr); free(devsArr); CFRelease(devs); return res; }
/* Get the number of devices capable of burning audio CDs. If the result is N, then calls to GetDeviceName and OpenDevice are guaranteed to be valid for indices from 0 up to N-1, until the next time you call GetNumDevices. At that point, the list of devices will be rescanned, and may be different. */ int PortBurn_GetNumDevices(void *handle) { PBHandle *h = (PBHandle *)handle; if (!h) return 0; if (h->deviceList) { CFRelease(h->deviceList); h->deviceList = NULL; } h->deviceList = DRCopyDeviceArray(); if (!h->deviceList) return 0; return (int)CFArrayGetCount(h->deviceList); }
/* druPromptForDevice Interactively asks the user to select a device from the devices which are currently attached. If only one device is connected, the device is automatically chosen and nothing is printed. The optional filter function is called to filter devices. If you wish to suppress a device, the filter function should return 0. The returned device is retained by this routine. */ DRDeviceRef druPromptForDevice(char *promptString, druDeviceFilterProc filter) { CFArrayRef deviceList = DRCopyDeviceArray(); CFIndex deviceCount = CFArrayGetCount(deviceList); DRDeviceRef device; CFIndex selection; char userInput[10]; /* Can't proceed without at least one drive. */ if (deviceCount == 0) { printf("Sorry, no CD/DVD drives were found.\n"); exit(1); } /* Filter the list. */ if (filter != NULL) { CFMutableArrayRef filteredList = CFArrayCreateMutableCopy(NULL,0,deviceList); for (selection=deviceCount-1; selection>=0; --selection) if ((*filter)((DRDeviceRef)CFArrayGetValueAtIndex(filteredList,selection)) == 0) CFArrayRemoveValueAtIndex(filteredList,selection); CFRelease(deviceList); deviceList = filteredList; deviceCount = CFArrayGetCount(deviceList); } /* Can't proceed without at least one drive. */ if (deviceCount == 0) { printf("Sorry, no eligible drives were found.\n"); exit(1); } /* If there's only one device, which is actually true for many machines (those with an internal CD burner and no external burners attached) then the choice is obvious, and we don't need to display a menu. */ if (deviceCount == 1) { device = (DRDeviceRef)CFArrayGetValueAtIndex(deviceList,0); CFRetain(device); CFRelease(deviceList); return device; } /* Display a menu of devices. */ printf("Available devices:\n"); druDisplayDeviceList(deviceList); /* Display the prompt. */ if (promptString == NULL) promptString = "Please select a device:"; printf("%s ", promptString); fflush(stdout); /* Get user input. */ userInput[0] = 0; selection = atoi(fgets(userInput,sizeof(userInput),stdin)) - 1; if (selection < 0 || selection >= deviceCount) { printf("Aborted.\n"); exit(1); } /* Return the selected device. */ device = (DRDeviceRef)CFArrayGetValueAtIndex(deviceList,selection); CFRetain(device); CFRelease(deviceList); return device; }