Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/open_kapa.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/open_kapa.c	(revision 13353)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/open_kapa.c	(revision 13354)
@@ -9,53 +9,75 @@
 
 /* list of available socket connections */
-static int        Active;        // currently active socket (not device)
+static int        Active;        // currently active socket entry (index value, not socket value)
 static int       *Socket = NULL; // list of available sockets
-static int       *Device = NULL; // list of device numbers for each socked
-static int       Nsocket = 0;    // number of available sockets / devices
+static char     **Device = NULL; // list of device names for each socket
+static int       Ndevice = 0;    // number of available sockets / devices
 
 void InitKapa () {
 
-  Active  = -1;		      // -1 is the INVALID entry
-  Nsocket = 0;		      // number of defined sockets
-  ALLOCATE (Device, int, 1);
-  ALLOCATE (Socket, int, 1);
-  Device[Active] = 0;  
-  Socket[Active] = 0;  
-}
-
-// returns the number of the requested device, or -1 if not found
-int FindKapaDevice (int thisDevice) {
+  Active  = -1;					  // -1 is the INVALID entry
+  Ndevice = 0;					  // number of defined sockets
+  ALLOCATE (Device, char *, 1);			  // for future REALLOCATE calls
+  ALLOCATE (Socket, int, 1);			  // for future REALLOCATE calls
+}
+
+// add new device name if not found
+int AddKapaDevice (char *name) {
+
+  int N;
+
+  N = FindKapaDevice (name);
+  if (N != -1) return (N);
+  N = Ndevice;
+  Ndevice ++;
+  REALLOCATE (Device, char *, Ndevice);
+  REALLOCATE (Socket, int, Ndevice);
+  Device[N] = strcreate (name);
+  Socket[N] = -1;
+  return (N);
+}
+
+// delete device by name, close if not closed
+int DelKapaDevice (char *name) {
+
+  int N;
+
+  N = FindKapaDevice (name);
+  if (N == -1) return (FALSE);
+  if (Socket[N] != -1) close (Socket[N]);
+  free (Device[N]);
+  for (i = N; i < Ndevice - 1; i++) {
+    Device[N] = Device[N+1];
+    Socket[N] = Socket[N+1];
+  }
+  Ndevice --;
+  REALLOCATE (Device, char *, Ndevice);
+  REALLOCATE (Socket, int, Ndevice);
+  return (TRUE);
+}
+
+// returns the entry of the requested device, or -1 if not found
+int FindKapaDevice (char *name) {
 
   int i;
 
-  for (i = 0; i < Nsocket; i++) {
-    if (Device[i] == thisDevice) {
-      return (i);
-    }
+  for (i = 0; i < Ndevice; i++) {
+    if (!strcmp(Device[i], name)) return (i);
   }
   return (-1);
 }
 
-// set the active device to the given device number, open if needed
-int open_kapa (int thisDevice) {
-
-  int fd, entry;
-  char *kapa_exec, name[16];
+// set the active device to the given device, open if needed
+int open_kapa (int entry) {
+
+  int fd;
+  char *kapa_exec, *kapa_name;
 
   // find the given device number, or create. set this to active
-  entry = FindKapaDevice (thisDevice);
-  if (entry < 0) {
-    Active = Nsocket;
-    Nsocket ++;
-    REALLOCATE (Device, int, Nsocket);
-    REALLOCATE (Socket, int, Nsocket);
-    Device[Active] = -1;
-    Socket[Active] = -1;
-  } else {
-    Active = entry;
-  }
+  assert (entry > 0);
+  assert (entry < Ndevice);
 
   // if the (now) active socket is not open, open it
-  if (Socket[Active] < 0) {
+  if (Socket[entry] < 0) {
     kapa_exec = get_variable ("KAPA");
     if (kapa_exec == (char *) NULL) {
@@ -65,22 +87,21 @@
 
     // KAPA may be either kapa://host or /path/to/program
-
-    snprintf (name, 16, "[%d]", thisDevice);
-    fd = KapaOpen (kapa_exec, name);
+    ALLOCATE (kapa_name, char, strlen(Device[entry]) + 5);
+    snprintf (kapa_name, strlen(Device[entry]) + 5, "[%s]", Device[entry]);
+    fd = KapaOpen (kapa_exec, kapa_name);
     free (kapa_exec);
+    free (kapa_name);
 
     if (fd < 0) {
-      gprint (GP_ERR, "error starting kapa\n");
-      // delete the active entry
+      gprint (GP_ERR, "error starting kapa device %s\n", Device[entry]);
       return (FALSE);
     } 
-    Device[Active] = thisDevice;
-    Socket[Active] = fd;
+    Socket[entry] = fd;
   } 
+  Active = entry;
   return (TRUE);
 }
 
 /**************** graph ops */
-
 // XXX for now, use a local variable.  later: get from kapa
 static Graphdata graphdata;
@@ -117,21 +138,23 @@
 # endif
 
-/* return pointers for current Xgraph, set if desired, test, open if needed */
-int GetGraph (Graphdata *data, int *fd, int *dev) {
-
-  int thisDevice;
+/* return pointers for named device or current; open if needed */
+// if fd == NULL, don't return the value
+// if name == NULL, use the currently active device
+int GetGraph (Graphdata *data, int *fd, char *name) {
+
+  int entry;
 
   SetImageDevice (FALSE);
-  if (dev == NULL) {
+  if (name == NULL) {
     if (Active < 0) {
-      thisDevice = 0;
+      entry = AddKapaDevice ("0");
     } else {
-      thisDevice = Device[Active];
+      entry = Active;
     }
   } else {
-    thisDevice = *dev;
+    entry = AddKapaDevice (name);
   }
   
-  if (!open_kapa (thisDevice)) {
+  if (!open_kapa (entry)) {
     return (FALSE);
   }
@@ -144,28 +167,24 @@
 
 /* return pointers for given Xgraph, don't set or open */
-int GetGraphData (Graphdata *data, int *fd, int *dev) {
-
-  int n;
-
-  if (dev == NULL) {
+int GetGraphData (Graphdata *data, int *fd, char *name) {
+
+  int entry;
+
+  if (name == NULL) {
     if (Active < 0) {
-      gprint (GP_ERR, "invalid Xgraph window %d\n", *dev); 
-      return (FALSE);
-    }
-    *fd = Socket[Active];
-    return (TRUE);
-  } 
-
-  n = FindKapaDevice (*dev);
-  if (n < 0) {
-    gprint (GP_ERR, "invalid Xgraph window %d\n", *dev); 
-    return (FALSE);
-  }
-
-  Active = n;
-
-  if (fd != NULL) *fd = Socket[Active];
+      gprint (GP_ERR, "no active kapa window\n"); 
+      return (FALSE);
+    }
+    entry = Active
+  } else {
+    entry = FindKapaDevice (name);
+    if (entry < 0) {
+      gprint (GP_ERR, "invalid kapa window %s\n", name); 
+      return (FALSE);
+    }
+  }
+
+  if (fd != NULL) *fd = Socket[entry];
   if (data != NULL) *data = graphdata;
-
   return (TRUE);
 }
@@ -202,7 +221,7 @@
 
 /* return pointers for current Ximage, set if desired, test, open if needed */
-int GetImage (int *fd, int *N) {
+int GetImage (int *fd, char *name) {
   int status;
-  status = GetGraph (NULL, fd, N);
+  status = GetGraph (NULL, fd, name);
   return (status);
 }
