summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Endecott <spam_from_libdlo@chezphil.org>2009-05-28 15:56:23 -0700
committerlibdlo <libdlo@displaylink.com>2009-05-28 15:56:23 -0700
commit1c47e4dbe05ea5b2a153b1c584a6ac23b50d7218 (patch)
treec96e752ec5d4863d123d9bb5d111b6493ed5c74c
parentd50f082699787c3c6531df431b35c9ad52a82667 (diff)
Equivalent for $DISPLAY
index 12e6f33..1810cb5 100644
-rw-r--r--src/libdlo.c71
-rw-r--r--src/libdlo.h53
2 files changed, 87 insertions, 37 deletions
diff --git a/src/libdlo.c b/src/libdlo.c
index 12e6f33..eef7719 100644
--- a/src/libdlo.c
+++ b/src/libdlo.c
@@ -464,27 +464,68 @@ error:
DPRINTF("dlo: claim: error %u '%s'\n", (int)err, dlo_strerror(err));
return (dlo_dev_t)0;
-}
-
-
-dlo_dev_t dlo_claim_first_device(const dlo_claim_t flags, const uint32_t timeout)
-{
- dlo_devlist_t *node;
- dlo_devlist_t *next;
- dlo_dev_t uid = 0;
+}
+
+
+static const char* parse_cmdline(int *argc_p, char *argv[])
+{
+ /* Scan the command line looking for --dlo:display. Return the value found, if
+ * any, else return NULL. Remove all --dlo:* options from argv.
+ */
+
+ const char *display = NULL;
+ int n;
+ int m = 1;
+
+ if (!argc_p) {
+ return NULL;
+ }
+
+ for (n=1; n<*argc_p; ++n) {
+ const char* arg = argv[n];
+ if (strncmp(arg,"--dlo:",6)==0) {
+ const char* dlo_arg = arg + 6;
+ if (strncmp(dlo_arg,"display=",8)==0) {
+ display = dlo_arg + 8;
+ }
+ } else {
+ argv[m] = arg;
+ ++m;
+ }
+ }
+ *argc_p = m;
+ return display;
+}
+
+dlo_dev_t dlo_claim_default_device(int *argc_p, char *argv[],
+ const dlo_claim_t flags, const uint32_t timeout)
+{
+ const char *display = parse_cmdline(argc_p, argv);
+ if (!display) {
+ display = getenv("DLODISPLAY");
+ }
+
+ // For now, assume that display is simply the serial number. Some thought about
+ // exactly what its format should be is needed.
+ const char *serial = display;
+
+ dlo_devlist_t *node;
+ dlo_devlist_t *next;
+ dlo_dev_t uid = 0;
/* Look for a DisplayLink device to connect to - note the first one which is unclaimed */
node = dlo_enumerate_devices();
// dlo_enumerate_devices allocates memory for each node, which we must free
while (node)
- {
- dlo_device_t *dev = (dlo_device_t *)node->dev.uid;
-
- if (!uid && !dev->claimed)
- {
- uid = dlo_claim_device(node->dev.uid, flags, timeout);
- }
+ {
+ dlo_device_t *dev = (dlo_device_t *)node->dev.uid;
+
+ if (!uid && !dev->claimed
+ && (!serial || strcmp(serial,dev->serial)==0))
+ {
+ uid = dlo_claim_device(node->dev.uid, flags, timeout);
+ }
/* If we haven't claimed a device, move on to the next one */
next = node->next;
diff --git a/src/libdlo.h b/src/libdlo.h
index f6b5f0a..286cc88 100644
--- a/src/libdlo.h
+++ b/src/libdlo.h
@@ -601,28 +601,37 @@ extern dlo_devlist_t *dlo_enumerate_devices(void);
* Devices should be released with a call to @c release_device() when they are no
* longer required.
*/
-extern dlo_dev_t dlo_claim_device(const dlo_dev_t uid, const dlo_claim_t flags, const uint32_t timeout);
-
-
-/** Claim the first available (unclaimed) device.
- *
- * @param flags Flags word describing how the device is to be accessed.
- * @param timeout Timeout in milliseconds (zero for infinite).
- *
- * @return Unique ID of the claimed device (or NULL if failed).
- *
- * This call performs a very similar function to @c dlo_claim_device() except
- * that it performs the enumeration of connected devices on behalf of the caller
- * and returns the unique ID of the first available (unclaimed device). This
- * device is claimed automatically.
- *
- * If no unclaimed devices are found, or if the claim operation itself fails in
- * some way, the function will return a device handle of zero.
- */
-extern dlo_dev_t dlo_claim_first_device(const dlo_claim_t flags, const uint32_t timeout);
-
-
-/** Release the specified device.
+extern dlo_dev_t dlo_claim_device(const dlo_dev_t uid, const dlo_claim_t flags, const uint32_t timeout);
+
+
+/** Claim the default device.
+ *
+ * @param argc_p Pointer to main program's argc.
+ * @param argv Main program's argv.
+ * @param flags Flags word describing how the device is to be accessed.
+ * @param timeout Timeout in milliseconds (zero for infinite).
+ *
+ * @return Unique ID of the claimed device (or NULL if failed).
+ *
+ * This call enumerates the connected devices and returns the unique ID of the default
+ * device. The default device is selected by the DLODISPLAY environment variable or the
+ * --dlo:display command-line option; if neither is specified the first device found is
+ * selected. This selected device is claimed.
+ *
+ * If no default device is found, or if the claim operation fails, the function will
+ * return a device handle of zero.
+ *
+ * The main program should pass a pointer to argc and argv to this function so that any
+ * --dlo:display option can be found. This function will update argc and argv to remove
+ * any --dlo:* options that it finds, so the main program need not worry about the
+ * presence of these options in the command line if it calls this function first.
+ * Pass NULL as argc_p to disable command-line parsing.
+ */
+extern dlo_dev_t dlo_claim_default_device(int *argc_p, char *argv[],
+ const dlo_claim_t flags, const uint32_t timeout);
+
+
+/** Release the specified device.
*
* @param uid Unique ID of the device to release.
*