Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/globals.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/globals.h	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/globals.h	(revision 13344)
@@ -15,5 +15,5 @@
 
 /* file descriptor for socket connection to mana */
-int sock; 
+// int sock; 
 
 /* each layout / section defines one of the active portions of the graphics window
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/include/prototypes.h	(revision 13344)
@@ -39,5 +39,5 @@
 
 /* EventLoop */
-int           PScommand           PROTO(());
+int           PScommand           PROTO((int sock));
 int           CheckPipe           PROTO(());
 int           Reconfig            PROTO((XEvent *event));
@@ -45,21 +45,22 @@
 
 /* CheckPipe */
-int           PNGit               PROTO(());
-int           PPMit               PROTO(());
-int           LoadFrame           PROTO(());
-int           LoadObject          PROTO(());
-int           LoadLabels          PROTO(());
-int           LoadTextlines       PROTO(());
-int           LoadVectorData      PROTO(());
-int           Resize              PROTO(());
-int           GetLimits           PROTO(());
-int           SetLimits           PROTO(());
-int           SetSection          PROTO(());
-int           ListSection         PROTO(());
-int           DefineSection       PROTO(());
-int           SetFont             PROTO(());
-int           EraseCurrentPlot    PROTO(());
-int           ErasePlots          PROTO(());
-int           EraseSections       PROTO(());
+int           PNGit               PROTO((int sock));
+int           PPMit               PROTO((int sock));
+int           LoadFrame           PROTO((int sock));
+int           LoadObject          PROTO((int sock));
+int           LoadLabels          PROTO((int sock));
+int           LoadTextlines       PROTO((int sock));
+int           Resize              PROTO((int sock));
+int           GetLimits           PROTO((int sock));
+int           SetLimits           PROTO((int sock));
+int           SetSection          PROTO((int sock));
+int           ListSection         PROTO((int sock));
+int           DefineSection       PROTO((int sock));
+int           SetFont             PROTO((int sock));
+int           EraseCurrentPlot    PROTO((void));
+int           ErasePlots          PROTO((void));
+int           EraseSections       PROTO((void));
+
+int           LoadVectorData      PROTO((int sock, KapaGraphWidget *graph, int N, char *type));
 
 /* Section Utilities */
@@ -74,9 +75,10 @@
 Section      *GetActiveSection	  PROTO(());
 int           SetActiveSectionByNumber PROTO((int N));
-int           ListSection         PROTO(());
+int           ListSection         PROTO((int sock));
 
 KapaGraphWidget *InitGraph        PROTO(());
 void          DrawGraph           PROTO((KapaGraphWidget *graph));
 void          SetGraphSize        PROTO((Section *section));
+void          FreeGraph           PROTO((KapaGraphWidget *graph));
 
 void          InitLayout          PROTO((int argc, char **argv));
@@ -124,4 +126,5 @@
 void          QuitX               PROTO((Display *display, char *message));
 Graphic      *GetGraphic          PROTO(());
+int           GetPixelCount       PROTO((int sock));
 
 int           Center              PROTO(());
@@ -131,8 +134,10 @@
 void          Remap24             PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
 void          Remap32             PROTO((Graphic *graphic, KapaImageWidget *image, Matrix *matrix));
-int           LoadPicture         PROTO(());
+int           LoadPicture         PROTO((int sock));
+
 KapaImageWidget *InitImage        PROTO(());
+void          FreeImage           PROTO((KapaImageWidget *image));
 void          SetImageSize        PROTO((Section *section));
-int           GetPixelCount       PROTO(());
+
 void          InitButtonSize      PROTO((Button *button, int width, int height, char *bitmap));
 void          InitButtonFunc      PROTO((Button *button, int (*function)()));
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CSaveOverlay.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CSaveOverlay.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CSaveOverlay.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int CSaveOverlay () {
+int CSaveOverlay (int sock) {
 
   char filename[256];
@@ -10,9 +10,10 @@
   KapaImageWidget *image;
 
+  KiiScanMessage (sock, "%*s %d %s", &N, filename);
+  
   section = GetActiveSection();
   image   = section->image;
+  if (image == NULL) return (TRUE);
 
-  KiiScanMessage (sock, "%*s %d %s", &N, filename);
-  
   f = fopen (filename, "w");
   if (f == NULL) {
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Center.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Center.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Center.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int Center () {
+int Center (int sock) {
 
   int zoom;
@@ -9,9 +9,10 @@
   KapaImageWidget *image;
 
+  KiiScanMessage (sock, "%lf %lf %d", &X,  &Y, &zoom);
+
   graphic = GetGraphic();
   section = GetActiveSection();
   image = section->image;
-
-  KiiScanMessage (sock, "%lf %lf %d", &X,  &Y, &zoom);
+  if (image == NULL) return (TRUE);
 
   image[0].X = 0.5*image[0].matrix.Naxis[0] - X;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/CheckPipe.c	(revision 13344)
@@ -2,8 +2,24 @@
 # define STRCONST(A) ((int)(0x1000000*A[0] + 0x10000*A[1] + 0x100*A[2] + 0x1*A[3]))
 
+static KapaSockAddress Address;
+static int InitSocket = -1;
+static int sock = -1;
+
+// we can supply a port here, with only small changes
+void InitPipe () {
+  InitSocket = KapaServerInit (&Address);
+  return;
+}
+
 int CheckPipe () {
 
   int status;
   char buffer[32];
+
+  // check if we have a valid connection. if not, see if we can get one
+  if (sock == -1) {
+    sock = KapaServerWait (InitSocket, &Address);
+    if (sock == -1) return (TRUE);
+  }
 
   /***** read (4 byte) message word from socket ****/
@@ -47,5 +63,5 @@
   
   if (!strcmp (buffer, "PSIT")) {
-    status = PScommand ();
+    status = PScommand (sock);
     write (sock, "DONE", 4);
     return (status);
@@ -53,5 +69,5 @@
   
   if (!strcmp (buffer, "PNGF")) {
-    status = PNGit ();
+    status = PNGit (sock);
     write (sock, "DONE", 4);
     return (status);
@@ -59,5 +75,5 @@
   
   if (!strcmp (buffer, "PPMF")) {
-    status = PPMit ();
+    status = PPMit (sock);
     write (sock, "DONE", 4);
     return (status);
@@ -65,55 +81,55 @@
   
   if (!strcmp (buffer, "DBOX")) {
-    status = LoadFrame ();
+    status = LoadFrame (sock);
     return (status);
   }
   
   if (!strcmp (buffer, "PLOT")) {
-    status = LoadObject ();
+    status = LoadObject (sock);
     return (status);
   }
   
   if (!strcmp (buffer, "LABL")) {
-    status = LoadLabels ();
+    status = LoadLabels (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "PTXT")) {
-    status = LoadTextlines ();
+    status = LoadTextlines (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "RSIZ")) {
-    status = Resize ();
+    status = Resize (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "LIMS")) {
-    GetLimits ();
+    GetLimits (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "SLIM")) {
-    status = SetLimits ();
+    status = SetLimits (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "SSEC")) {
-    status = SetSection ();
+    status = SetSection (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "LSEC")) {
-    status = ListSection ();
+    status = ListSection (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "DSEC")) {
-    status = DefineSection ();
+    status = DefineSection (sock);
     return (TRUE);
   }
   
   if (!strcmp (buffer, "FONT")) {
-    status = SetFont ();
+    status = SetFont (sock);
     return (TRUE);
   }
@@ -121,5 +137,5 @@
   /* Erase Section */
   if (!strcmp (buffer, "ERSC")) {
-    status = EraseCurrentPlot (TRUE);
+    status = EraseCurrentPlot ();
     return (status);
   }
@@ -127,5 +143,5 @@
   /* Erase Section */
   if (!strcmp (buffer, "ERAS")) {
-    status = ErasePlots (TRUE);
+    status = ErasePlots ();
     return (status);
   }
@@ -133,5 +149,5 @@
   /* Don't Erase Section */
   if (!strcmp (buffer, "ERSS")) {
-    status = EraseSections (FALSE);
+    status = EraseSections ();
     return (status);
   }
@@ -139,5 +155,5 @@
 
   if (!strcmp (buffer, "READ")) {
-    status = LoadPicture ();
+    status = LoadPicture (sock);
     return (status);
   }
@@ -145,30 +161,30 @@
   // XXX duplicate Command word
   if (!strcmp (buffer, "ERAS")) {
-    status = EraseOverlay ();
+    status = EraseOverlay (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "LOAD")) {
-    status = LoadOverlay ();
+    status = LoadOverlay (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "TICK")) {
-    status = LoadTickmarks ();
+    status = LoadTickmarks (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "SAVE")) {
-    status = SaveOverlay ();
+    status = SaveOverlay (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "CSVE")) {
-    status = CSaveOverlay ();
+    status = CSaveOverlay (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "JPEG")) {
-    status = JPEGit24 ();
+    status = JPEGit24 (sock);
     write (sock, "DONE", 4);
     return (status);
@@ -176,10 +192,10 @@
 
   if (!strcmp (buffer, "CENT")) {
-    status = Center ();
+    status = Center (sock);
     return (status);
   }
 
   if (!strcmp (buffer, "NPIX")) {
-    GetPixelCount ();
+    GetPixelCount (sock);
     return (TRUE);
   }
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/DefineSection.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/DefineSection.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/DefineSection.c	(revision 13344)
@@ -1,5 +1,6 @@
 # include "Ximage.h"
 
-int DefineSection () {
+// define the section, but do not create a graph or image 
+int DefineSection (int sock) {
   
   int i, N, MoveSection;
@@ -15,5 +16,4 @@
   if (N < 0) {
     section = AddSection (name, x, y, dx, dy);
-    section->graph = InitGraph ();
     MoveSection = TRUE;
   } else {
@@ -34,4 +34,5 @@
   if (MoveSection) {
     SetGraphSize (section);
+    SetImageSize (section);
     Refresh (1);
   }
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseCurrentPlot.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseCurrentPlot.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseCurrentPlot.c	(revision 13344)
@@ -11,4 +11,5 @@
   section = GetActiveSection();
   if (section->graph == NULL) return (TRUE);
+
   EraseGraph (section->graph);
   
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseOverlay.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseOverlay.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/EraseOverlay.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int EraseOverlay () {
+int EraseOverlay (int sock) {
 
   char buffer[256];
@@ -12,4 +12,5 @@
   section = GetActiveSection();
   image = section->image;
+  if (image == NULL) return (TRUE);
 
   KiiScanCommand (sock, 16, "%*s %d", &N);
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/GetPixelCount.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/GetPixelCount.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/GetPixelCount.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int GetPixelCount () {
+int GetPixelCount (int sock) {
   
   int i;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Graphs.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Graphs.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Graphs.c	(revision 13344)
@@ -100,4 +100,5 @@
   FREE (graph[0].objects);
   FREE (graph[0].textline);
+  free (graph);
   return;
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Image.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Image.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Image.c	(revision 13344)
@@ -145,2 +145,19 @@
   FlushDisplay (graphic[0].display);
 }
+
+void FreeImage (KapaImageWidget *image) {
+
+  int i;
+
+  if (image == NULL) return;
+
+  for (i = 0; i < NOVERLAYS; i++) {
+    free (image[0].overlay[i].objects);
+  }
+  free (image[0].matrix.buffer);
+  free (image[0].picture.data);
+  free (image[0].cmapbar.data);
+  free (image[0].zoom.data);
+
+  free (image);
+}
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/InterpretKeys.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/InterpretKeys.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/InterpretKeys.c	(revision 13344)
@@ -55,8 +55,11 @@
     }
     snprintf (line, 40, "%12s %12.6f %12.6f ", name, X, Y);
-    write (sock, line, 40);
+    // XXX need to do something different here
+    // xxx write (sock, line, 40);
   }
 
 skip_cursor:
+  if (image == NULL) return (TRUE);
+
   // offset is in image pixels: 
   // 0.5 image pixels is 1 screen pixel for expand == +2
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/InterpretPresses.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/InterpretPresses.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/InterpretPresses.c	(revision 13344)
@@ -34,5 +34,6 @@
     }
     snprintf (line, 40, "%12s %12.6f %12.6f ", name, X, Y);
-    write (sock, line, 40);
+    // need to do something different here...
+    // xxx write (sock, line, 40);
   }
 
@@ -40,4 +41,7 @@
   status = TRUE;
   this_button = event[0].button;
+
+  // XXX add graph buttons here
+  if (image == NULL) return (TRUE);
   
   if ((event[0].type == ButtonPress) && InPicture (event, &image[0].picture)) {
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/JPEGit24.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/JPEGit24.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/JPEGit24.c	(revision 13344)
@@ -6,6 +6,6 @@
 # define WHITE_B 255
 
-// XXX this currently writes out the jpeg for the active image
-int JPEGit24 () {
+// XXX this currently writes out the jpeg for just the active image
+int JPEGit24 (int sock) {
 
   struct jpeg_compress_struct cinfo;
@@ -30,10 +30,11 @@
   FILE *f;
 
+  /* expect a line telling the number of bytes and a filename */
+  KiiScanMessage (sock, "%s", filename);
+
   graphic = GetGraphic();
   section = GetActiveSection();
   image   = section->image;
-
-  /* expect a line telling the number of bytes and a filename */
-  KiiScanMessage (sock, "%s", filename);
+  if (image == NULL) return (TRUE);
 
   /***** JPEG init calls */
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Layout.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Layout.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Layout.c	(revision 13344)
@@ -4,21 +4,8 @@
 void InitLayout (int argc, char **argv) {
 
-  int N, UseGraph;
+  int N;
   Section *section;
 
-  UseGraph = FALSE;
-  if ((N = get_argument (argc, argv, "-image"))) {
-    remove_argument(N, &argc, argv);
-    UseGraph = FALSE;
-  }
-
-  /** initiate connection with server **/
-  if (!FOREGROUND) {	
-    if (argc < 2) {
-      fprintf (stderr, "socket path not specified\n");
-      exit (0);
-    }
-    sock = KiiWait (argv[1]);
-  }
+  InitPipe();
 
   ACTIVE_CURSOR = FALSE;
@@ -37,14 +24,13 @@
   }
 
+  /* move this out of here... */
+  if (argc != 1) {
+    fprintf (stderr, "USAGE: kapa\n");
+    exit (0);
+  }
+
   InitRotFonts ();
 
-  /* create basic section */
+  /* create basic section, empty of image or graph */
   section = AddSection ("default", 0.0, 0.0, 1.0, 1.0);
-  if (UseGraph) {
-    section->graph = InitGraph ();
-    SetGraphSize (section);
-  } else {
-    section->image = InitImage ();
-    SetImageSize (section);
-  }
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadFrame.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadFrame.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadFrame.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int LoadFrame () {
+int LoadFrame (int sock) {
   
   int i, status;
@@ -9,8 +9,10 @@
 
   section = GetActiveSection();
+  if (section->graph == NULL) {
+    section->graph = InitGraph ();
+    SetGraphSize (section);
+  }
   graph = section->graph;
 
-  // XXX what to do if graph is NULL?
-  
   KiiScanMessage (sock, "%lf %lf %lf %lf", 
 		  &graph[0].axis[0].min, &graph[0].axis[0].max, 
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadLabels.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadLabels.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadLabels.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int LoadLabels () {
+int LoadLabels (int sock) {
   
   char *c, *label;
@@ -9,4 +9,8 @@
 
   section = GetActiveSection();
+  if (section->graph == NULL) {
+    section->graph = InitGraph ();
+    SetGraphSize (section);
+  }
   graph = section->graph;
   
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadObject.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadObject.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadObject.c	(revision 13344)
@@ -2,5 +2,5 @@
 # include <errno.h>
 
-int LoadObject () {
+int LoadObject (int sock) {
   
   int N;
@@ -9,4 +9,8 @@
 
   section = GetActiveSection();
+  if (section->graph == NULL) {
+    section->graph = InitGraph ();
+    SetGraphSize (section);
+  }
   graph = section->graph;
   
@@ -44,5 +48,5 @@
   fcntl (sock, F_SETFL, O_NONBLOCK);  
   
-  if (!LoadVectorData (graph, N, "x")) {
+  if (!LoadVectorData (sock, graph, N, "x")) {
     FreeObjectData (&graph[0].objects[N]);
     graph[0].Nobjects --;
@@ -50,5 +54,5 @@
   }
     
-  if (!LoadVectorData (graph, N, "y")) {
+  if (!LoadVectorData (sock, graph, N, "y")) {
     FreeObjectData (&graph[0].objects[N]);
     graph[0].Nobjects --;
@@ -56,5 +60,5 @@
   }
   if (graph[0].objects[N].size < 0.0) {
-    if (!LoadVectorData (graph, N, "z")) {
+    if (!LoadVectorData (sock, graph, N, "z")) {
       FreeObjectData (&graph[0].objects[N]);
       graph[0].Nobjects --;
@@ -63,10 +67,10 @@
   }
   if (graph[0].objects[N].etype & 0x01) {
-    if (!LoadVectorData (graph, N, "dym")) {
+    if (!LoadVectorData (sock, graph, N, "dym")) {
       FreeObjectData (&graph[0].objects[N]);
       graph[0].Nobjects --;
       REALLOCATE (graph[0].objects, Gobjects, MAX (1, graph[0].Nobjects));
     }
-    if (!LoadVectorData (graph, N, "dyp")) {
+    if (!LoadVectorData (sock, graph, N, "dyp")) {
       FreeObjectData (&graph[0].objects[N]);
       graph[0].Nobjects --;
@@ -75,10 +79,10 @@
   }
   if (graph[0].objects[N].etype & 0x02) {
-    if (!LoadVectorData (graph, N, "dxm")) {
+    if (!LoadVectorData (sock, graph, N, "dxm")) {
       FreeObjectData (&graph[0].objects[N]);
       graph[0].Nobjects --;
       REALLOCATE (graph[0].objects, Gobjects, MAX (1, graph[0].Nobjects));
     }
-    if (!LoadVectorData (graph, N, "dxp")) {
+    if (!LoadVectorData (sock, graph, N, "dxp")) {
       FreeObjectData (&graph[0].objects[N]);
       graph[0].Nobjects --;
@@ -97,5 +101,5 @@
 
 /* load data for the named component */
-int LoadVectorData (KapaGraphWidget *graph, int N, char *type) {
+int LoadVectorData (int sock, KapaGraphWidget *graph, int N, char *type) {
   
   int Npts, Ninpts, status, Ntry;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadOverlay.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadOverlay.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadOverlay.c	(revision 13344)
@@ -2,5 +2,5 @@
 
 // XXX this is a very inefficient method to send the data: binary would be better
-int LoadOverlay () {
+int LoadOverlay (int sock) {
   
   char word[17], type[16], string[128];
@@ -15,4 +15,8 @@
   graphic = GetGraphic ();
   section = GetActiveSection();
+  if (section->image == NULL) {
+    section->image = InitImage ();
+    SetImageSize (section);
+  }
   image   = section->image;
 
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadPicture.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadPicture.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadPicture.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int LoadPicture () {
+int LoadPicture (int sock) {
 
   Header header;
@@ -12,5 +12,9 @@
   graphic = GetGraphic ();
   section = GetActiveSection();
-  image   = section->image;
+  if (section->image == NULL) {
+    section->image = InitImage ();
+    SetImageSize (section);
+  }
+  image = section->image;
   
   KiiSendMessage (sock, "%d", graphic->Npixels);
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadTextlines.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadTextlines.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadTextlines.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int LoadTextlines () {
+int LoadTextlines (int sock) {
   
   char *string;
@@ -11,5 +11,9 @@
   section = GetActiveSection();
   graph = section->graph;
-  
+    if (section->graph == NULL) {
+    section->graph = InitGraph ();
+    SetGraphSize (section);
+  }
+
   fcntl (sock, F_SETFL, !O_NONBLOCK);  
   
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadTickmarks.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadTickmarks.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/LoadTickmarks.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int LoadTickmarks () {
+int LoadTickmarks (int sock) {
   
   char line[129], type[16];
@@ -10,5 +10,10 @@
 
   section = GetActiveSection();
+  if (section->image == NULL) {
+    section->image = InitImage ();
+    SetImageSize (section);
+  }
   image   = section->image;
+
   fcntl (sock, F_SETFL, !O_NONBLOCK);  
 
@@ -63,4 +68,3 @@
   fcntl (sock, F_SETFL, O_NONBLOCK);  
   return (TRUE);
-  
 }
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PNGit.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PNGit.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PNGit.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int PNGit () {
+int PNGit (int sock) {
 
   FILE *f;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PPMit.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PPMit.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PPMit.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int PPMit () {
+int PPMit (int sock) {
 
   FILE *f;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PSit.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PSit.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/PSit.c	(revision 13344)
@@ -6,5 +6,5 @@
 static Graphic *graphic;
 
-int PScommand () {
+int PScommand (int sock) {
 
   int status, scaleMode, pageMode;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Resize.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Resize.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Resize.c	(revision 13344)
@@ -2,5 +2,5 @@
 
 // XXX Should there be a base command + KiiMessage command?
-int Resize () {
+int Resize (int sock) {
  
   int i, Nsection;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SaveOverlay.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SaveOverlay.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SaveOverlay.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int SaveOverlay () {
+int SaveOverlay (int sock) {
 
   int i, N;
@@ -11,4 +11,5 @@
   section = GetActiveSection();
   image   = section->image;
+  if (image == NULL) return (TRUE);
 
   KiiScanMessage (sock, "%*s %d %s", &N, filename);
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Sections.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Sections.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/Sections.c	(revision 13344)
@@ -24,7 +24,7 @@
 
   if (section == NULL) return;
-  if (section[0].graph != NULL) FreeGraph (section[0].graph);
-  // if (section[0].image != NULL) FreeImage (section[0].image);
-  if (section[0].name  != NULL) free (section[0].name);
+  FreeGraph (section[0].graph);
+  FreeImage (section[0].image);
+  FREE (section[0].name);
   free (section);
 }
@@ -42,6 +42,4 @@
 }
 
-// XXX how to set the section type?
-// XXX set active section on Add?
 Section *AddSection (char *name, float x, float y, float dx, float dy) {
 
@@ -116,5 +114,5 @@
 }
 
-int ListSection () {
+int ListSection (int sock) {
   
   int i, ThisSection;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetFont.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetFont.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetFont.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int SetFont () {
+int SetFont (int sock) {
   
   char name[64];
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetLimits.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetLimits.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetLimits.c	(revision 13344)
@@ -1,5 +1,5 @@
 # include "Ximage.h"
 
-int SetLimits () {
+int SetLimits (int sock) {
   
   int i;
@@ -14,7 +14,10 @@
 
   section = GetActiveSection();
-  if (section->graph == NULL) return (TRUE);
+  if (section->graph == NULL) {
+    section->graph = InitGraph ();
+    SetGraphSize (section);
+  }
   graph = section->graph;
-
+  
   graph[0].axis[2].min = graph[0].axis[0].min = xmin;
   graph[0].axis[2].max = graph[0].axis[0].max = xmax;
@@ -32,9 +35,8 @@
   Refresh (1);
 
-  return (TRUE);
-  
+  return (TRUE);  
 }
 
-int GetLimits () {
+int GetLimits (int sock) {
   
   double dX, dY;
@@ -57,2 +59,8 @@
   return (TRUE);
 }
+
+/* example for image:
+   
+    section->image = InitImage ();
+    SetImageSize (section);
+*/
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetSection.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetSection.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/SetSection.c	(revision 13344)
@@ -2,5 +2,5 @@
 
 // set active section
-int SetSection () {
+int SetSection (int sock) {
   
   int N;
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/UpdatePointer.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/UpdatePointer.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/UpdatePointer.c	(revision 13344)
@@ -13,4 +13,5 @@
   section = GetActiveSection();
   image   = section->image;
+  if (image == NULL) return (TRUE);
 
   if (image[0].MovePointer && InPicture ((XButtonEvent *)event, &image[0].picture)) {
Index: /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/kapa.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/kapa.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/kapa2/src/kapa.c	(revision 13344)
@@ -7,6 +7,4 @@
   SetUpGraphic (&argc, argv);
 
-  // XXX get socket connection here?
-
   InitLayout (argc, argv);
   EventLoop ();
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/Makefile
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/Makefile	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/Makefile	(revision 13344)
@@ -29,5 +29,6 @@
 $(SRC)/KiiConvert.$(ARCH).o \
 $(SRC)/KapaWindow.$(ARCH).o \
-$(SRC)/KapaColors.$(ARCH).o
+$(SRC)/KapaColors.$(ARCH).o \
+$(SRC)/KapaOpen.$(ARCH).o
 
 DRAW = \
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/include/kapa.h	(revision 13344)
@@ -2,7 +2,16 @@
 # define KAPA_H
 
+/* linux is happy with this, not solaris */
+# include <netinet/ip.h>
+# include <netdb.h>
+# include <arpa/inet.h>
+
+# include <sys/types.h>
+# include <sys/socket.h>
 # include <X11/Xlib.h>
 # include <png.h>
 # include <dvo.h>
+
+typedef struct sockaddr_in KapaSockAddress;
 
 typedef struct {
@@ -196,6 +205,13 @@
 int bDrawRotBitmap (int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale);
 
+/* Kapa Socket functions */
+int KapaOpen (char *kapa_exec, char *kapa_name);
+int KapaServerInit (KapaSockAddress *Address);
+int KapaServerWait (int InitSocket, KapaSockAddress *Address);
+int KapaDefineValidIP (char *ipstring);
+int KapaClientSocket (char *hostname);
+
 /* define Kapa names for shared functions */
-# define KapaOpen(p,n) KiiOpen(p,n)
+// # define KapaOpen(p,n) KiiOpen(p,n)
 # define KapaResize(fd,Nx,Ny) KiiResize(fd,Nx,Ny)
 # define KapaCenter(fd,x,y,z) KiiResize(fd,x,y,z)
Index: /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/IOfuncs.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/IOfuncs.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/libkapa/src/IOfuncs.c	(revision 13344)
@@ -111,6 +111,6 @@
 int KiiScanCommand (int device, int length, char *format, ...) {
 
-  int Nbytes, status;
-  char *buffer, *message;
+  int status;
+  char *message;
   va_list argp;  
 
@@ -118,6 +118,6 @@
 
   /* read Nbytes from the device */
-  status = read (device, message, Nbytes);
-  if (status != Nbytes) fprintf (stderr, "Kii/Kapa comm error\n");
+  status = read (device, message, length);
+  if (status != length) fprintf (stderr, "Kii/Kapa comm error\n");
   message[status] = 0;
   /* make the string easy to parse */
@@ -125,5 +125,5 @@
   /* scan the incoming message */
   va_start (argp, format);
-  Nbytes = vsscanf (message, format, argp);
+  status = vsscanf (message, format, argp);
   va_end (argp);
 
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/close.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/close.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/cmd.data/close.c	(revision 13344)
@@ -24,8 +24,8 @@
   if (IsImage) {
     if (!GetImage (&Source, &Nsource)) return (FALSE);
-    close_image (Nsource); 
+    // close_image (Nsource); 
   } else {
     if (!GetGraph (NULL, &Source, &Nsource)) return (FALSE);
-    close_graph (Nsource); 
+    // close_graph (Nsource); 
   }
 
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/include/display.h
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/include/display.h	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/include/display.h	(revision 13344)
@@ -4,15 +4,4 @@
 # ifndef DISPLAY_H
 # define DISPLAY_H
-
-# if (0)
-typedef struct {
-  double xmin, xmax, ymin, ymax;
-  int style, ptype, ltype, etype, ebar, color;
-  double lweight, size;
-  Coords coords;
-  int flipeast, flipnorth;
-  char axis[8], labels[8], ticks[8];
-} Graphdata;
-# endif
 
 /*** plotting functions ***/
@@ -45,4 +34,8 @@
 int           SelectOverlay         PROTO((char *name, int *number));
 
+void InitKapa ();
+int FindKapaDevice (int thisDevice);
+int open_kapa (int thisDevice);
+
 /* calling program need to define a function 'get_variable' which
  * returns the name of the executable for each of KAPA and KII
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/Makefile
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/Makefile	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/Makefile	(revision 13344)
@@ -29,9 +29,11 @@
 $(SDIR)/gaussian.$(ARCH).o		\
 $(SDIR)/graphtools.$(ARCH).o            \
-$(SDIR)/open_graph.$(ARCH).o            \
-$(SDIR)/open_image.$(ARCH).o            \
 $(SDIR)/queues.$(ARCH).o		\
 $(SDIR)/PlotVectors.$(ARCH).o		\
+$(SDIR)/open_kapa.$(ARCH).o             \
 $(SDIR)/style_args.$(ARCH).o
+
+#$(SDIR)/open_graph.$(ARCH).o            \
+#$(SDIR)/open_image.$(ARCH).o            \
 
 # dependancy rules for include files ########################
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/open_image.c
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/open_image.c	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/lib.data/open_image.c	(revision 13344)
@@ -122,5 +122,6 @@
   return (Ximbuffer[Active]);
 }
-  
+ 
+// leave this information on kapa, request when needed? 
 void SetImageScale (double zero, double range) {
   Xzero[Active] = zero;
Index: /branches/kapa-mods-2007-05/Ohana/src/opihi/mana/mana.c.in
===================================================================
--- /branches/kapa-mods-2007-05/Ohana/src/opihi/mana/mana.c.in	(revision 13343)
+++ /branches/kapa-mods-2007-05/Ohana/src/opihi/mana/mana.c.in	(revision 13344)
@@ -51,6 +51,6 @@
 /* add program-dependent exit functions here */
 void cleanup () {
-  QuitImage ();
-  QuitGraph ();
+	//   QuitImage ();
+	//   QuitGraph ();
   return;
 }
Index: /trunk/Ohana/src/libkapa/src/KapaOpen.c
===================================================================
--- /trunk/Ohana/src/libkapa/src/KapaOpen.c	(revision 13344)
+++ /trunk/Ohana/src/libkapa/src/KapaOpen.c	(revision 13344)
@@ -0,0 +1,244 @@
+# include "kapa_internal.h"
+
+# define MY_PORT 2500
+# define MY_WAIT 100000
+# define DEBUG 0
+
+static int Nvalid = 0;
+static int *VALID = NULL;
+
+int KapaServerInit (KapaSockAddress *Address) {
+
+  int status, InitSocket, length;
+
+  Address[0].sin_family = AF_INET;
+  Address[0].sin_port   = MY_PORT;
+  Address[0].sin_addr.s_addr = INADDR_ANY; // use this line to bind any address / port?
+
+retry_server:
+
+  length = sizeof(Address[0]);
+
+  InitSocket = socket (PF_INET, SOCK_STREAM, 0);
+  if (InitSocket == -1) {
+    perror ("socket: ");
+    exit (2);
+  }
+
+  if (DEBUG) fprintf (stderr, "init sock: %d, len: %d\n", InitSocket, length);
+  status = bind (InitSocket, (struct sockaddr *) Address, length);
+  if (status == -1) {
+    if (errno == EADDRINUSE) {
+	Address[0].sin_port ++;
+	if (Address[0].sin_port > MY_PORT + 10) exit (2);
+	goto retry_server;
+    }
+    perror ("bind: ");
+    exit (2);
+  }
+
+  /* repeated starts of the server are limited by xinetd or something:
+     requires 60sec timeout of the selected socket */
+
+  if (DEBUG) fprintf (stderr, "bound to port: %d\n", Address[0].sin_port);
+  status = listen (InitSocket, 10);
+  if (status == -1) {
+    perror ("listen: ");
+    exit (2);
+  }
+  return (InitSocket);
+}
+
+int KapaServerWait (int InitSocket, KapaSockAddress *Address) {
+
+  int i, status, BindSocket;
+  KapaSockAddress Address_in;
+  socklen_t length;
+  u_int32_t addr;
+  fd_set rfds;
+  struct timeval wait;
+
+  Address_in = Address[0];
+
+  length = sizeof(Address_in);
+
+  wait.tv_sec = 0;
+  wait.tv_usec = MY_WAIT;
+
+  /* do I need to clear rfds on each pass? */
+  FD_SET (InitSocket, &rfds);
+  status = select (InitSocket + 1, &rfds, NULL, NULL, &wait);
+  if (status == -1) {
+    perror ("select");
+    abort ();
+  }
+  if (!status) return (-1);
+
+  /* this is a blocking wait; use in a separate thread */
+  // fcntl (InitSocket, F_SETFL, !O_NONBLOCK); 
+
+  if (DEBUG) fprintf (stderr, "init sock: %d, len: %d\n", InitSocket, length);
+  BindSocket = accept (InitSocket, (struct sockaddr *) &Address_in, &length);
+  if (DEBUG) fprintf (stderr, "bind sock: %d\n", BindSocket);
+  if (BindSocket == -1) {
+    perror ("accept: ");
+    exit (2);
+  }
+
+  addr = Address_in.sin_addr.s_addr;
+  if (DEBUG) {
+    fprintf (stderr, "incoming connection from: ");
+    fprintf (stderr, " %u", (0xff & (addr >>  0)));
+    fprintf (stderr, ".%u", (0xff & (addr >>  8)));
+    fprintf (stderr, ".%u", (0xff & (addr >> 16)));
+    fprintf (stderr, ".%u", (0xff & (addr >> 24)));
+    fprintf (stderr, "\n");
+  }
+
+  if (Nvalid == 0) goto accepted;
+
+  for (i = 0; i < Nvalid; i++) {
+    /* valid IP addresses may be machines (120.90.121.142) or 
+       class C networks (120.90.121.0) */
+       
+    /* for machine, address must match */
+    if ((0xff & (VALID[i] >> 24)) != 0) {
+      if (addr == VALID[i]) goto accepted;
+    }
+
+    /* for network, lower three bytes of address must match */
+    if ((0xff & (VALID[i] >> 24)) == 0) {
+      if ((0x00ffffff & addr) == VALID[i]) goto accepted;
+    }
+  }
+
+  if (DEBUG) fprintf (stderr, "connection rejected\n");
+  close (BindSocket);
+  return (-1);
+
+accepted:
+  if (DEBUG) fprintf (stderr, "connection accepted\n");
+  // fcntl (BindSocket, F_SETFL, O_NONBLOCK); 
+  return (BindSocket);
+}
+
+/* load valid ip list */
+int KapaDefineValidIP (char *ipstring) {
+
+  int i, NVALID, ip1, ip2, ip3, ip4, test, status;
+  char string[80];
+
+  if (Nvalid == 0) {
+    Nvalid = 1;
+    ALLOCATE (VALID, int, Nvalid);
+  } else {
+    Nvalid ++;
+    REALLOCATE (VALID, int, Nvalid);
+  }
+
+  status = sscanf (ipstring, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
+  test = TRUE;
+  test &= (status == 4);
+  test &= ((ip1 > 0) && (ip1 < 256)); 
+  test &= ((ip2 > 0) && (ip2 < 256)); 
+  test &= ((ip3 > 0) && (ip3 < 256)); 
+  test &= ((ip4 >=0) && (ip4 < 256)); 
+  if (!test) {
+    fprintf (stderr, "invalid IP address %s\n", string);
+    exit (2);
+  }
+  VALID[Nvalid-1] = ip1 | (ip2 << 8) | (ip3 << 16) | (ip4 << 24);
+  return (TRUE);
+}
+
+/* connect to a running server on the specified host */
+int KapaClientSocket (char *hostname) {
+
+  int i, status, InitSocket, length;
+  KapaSockAddress Address;
+  struct hostent  *host;
+  char tmpline[80], hostip[80];
+
+  host = gethostbyname (hostname);
+  bzero (hostip, 80);
+  for (i = 0; i < host[0].h_length; i++) {
+    sprintf (tmpline, "%u", (0xff & host[0].h_addr[i]));
+    strcat (hostip, tmpline);
+    if (i < host[0].h_length - 1) strcat (hostip, ".");
+  }
+
+  if (DEBUG) {
+    fprintf (stderr, "trying %s (%s:%d)...", host[0].h_name, hostip, MY_PORT);
+  }
+
+  Address.sin_family = AF_INET;
+  Address.sin_port   = MY_PORT;
+
+retry_client:
+  status = inet_aton (hostip, &Address.sin_addr);
+  if (!status) {
+    fprintf (stderr, "invalid address\n");
+    exit (2);
+  }
+
+  length = sizeof(Address);
+
+  InitSocket = socket (PF_INET, SOCK_STREAM, 0);
+  if (InitSocket == -1) {
+    perror ("socket: ");
+    exit (2);
+  }
+
+  status = connect (InitSocket, (struct sockaddr *) &Address, length);
+  if (status == -1) {
+    if (DEBUG) fprintf (stderr, "error connecting: %d\n", errno);
+    if (errno == ECONNREFUSED) {
+      Address.sin_port ++;
+      if (Address.sin_port > MY_PORT + 10) return (-1);
+      goto retry_client;
+    }
+    perror ("connect: ");
+    exit (2);
+  }
+
+  if (DEBUG) fprintf (stderr, "connected on port: %d\n", Address.sin_port);
+  if (DEBUG) fprintf (stderr, "connected\n");
+  fcntl (InitSocket, F_SETFL, O_NONBLOCK); 
+  return (InitSocket);
+}
+
+int KapaOpen (char *kapa_exec, char *kapa_name) {
+
+  // kapa_exec may be kapa://host, in which case we attempt to connect to an 
+  // already running kapa, or the program path, in which case we are supposed
+  // to launch it locally, then connect to it.
+
+  int sock, Ntry;
+  char line[128];
+
+  if (!strncmp (kapa_exec, "kapa://", 7)) {
+    sock = KapaClientSocket (&kapa_exec[7]);
+    return (sock);
+  }
+
+  if (kapa_name == NULL) {
+    sprintf (line, "%s &", kapa_exec);
+  } else {
+    sprintf (line, "%s -name %s &", kapa_exec, kapa_name);
+  }
+  system (line);
+
+# define NTRY 500
+  Ntry = 0;
+  while (Ntry < NTRY) {
+    sock = KapaClientSocket ("localhost");
+    if (sock != -1) break;
+    // if (errno != EAGAIN) break;
+    if (errno != ECONNREFUSED) break;
+    usleep (10000);
+    Ntry ++;
+  }
+
+  if (sock < 0) return (-1);
+  return (sock);
+}
Index: /trunk/Ohana/src/opihi/lib.data/open_kapa.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.data/open_kapa.c	(revision 13344)
+++ /trunk/Ohana/src/opihi/lib.data/open_kapa.c	(revision 13344)
@@ -0,0 +1,226 @@
+# include "display.h"
+# include "shell.h"
+
+/* kapa support for the new version of kapa (v2.0), which has both graph and image
+ * elements merged into a single display device.  The user may now open an arbitrary
+ * number of kapa windows, and the display information is retrieved from kapa across the
+ * socket when it is needed.  Communication is now via an INET socket (not a UNIX socket).
+ */
+
+/* list of available socket connections */
+static int        Active;        // currently active socket (not device)
+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
+
+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) {
+
+  int i;
+
+  for (i = 0; i < Nsocket; i++) {
+    if (Device[i] == thisDevice) {
+      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];
+
+  // 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;
+  }
+
+  // if the (now) active socket is not open, open it
+  if (Socket[Active] < 0) {
+    kapa_exec = get_variable ("KAPA");
+    if (kapa_exec == (char *) NULL) {
+      gprint (GP_ERR, "variable KAPA not found\n");
+      return (FALSE);
+    }
+
+    // KAPA may be either kapa://host or /path/to/program
+
+    snprintf (name, 16, "[%d]", thisDevice);
+    fd = KapaOpen (kapa_exec, name);
+    free (kapa_exec);
+
+    if (fd < 0) {
+      gprint (GP_ERR, "error starting kapa\n");
+      // delete the active entry
+      return (FALSE);
+    } 
+    Device[Active] = thisDevice;
+    Socket[Active] = fd;
+  } 
+  return (TRUE);
+}
+
+/**************** graph ops */
+
+// XXX for now, use a local variable.  later: get from kapa
+static Graphdata graphdata;
+
+void InitGraph () {
+  graphdata.xmin = graphdata.ymin = 0.0;
+  graphdata.xmax = graphdata.ymax = 1.0;
+  graphdata.style = graphdata.ptype = 0;
+  graphdata.ltype = graphdata.color = 0;
+  graphdata.etype = graphdata.ebar = 0;
+  graphdata.lweight = graphdata.size = 1.0;
+    
+  graphdata.coords.pc1_1 = graphdata.coords.pc2_2 = 1.0;
+  graphdata.coords.pc1_2 = graphdata.coords.pc2_1 = 0.0;
+  strcpy (graphdata.coords.ctype, "RA---LIN");
+  graphdata.coords.crval1 = 0.0;
+  graphdata.coords.crval2 = 0.0;
+  graphdata.coords.crpix1 = 0.0;
+  graphdata.coords.crpix2 = 0.0;
+  graphdata.coords.cdelt1 = graphdata.coords.cdelt2 = 1.0;
+  graphdata.flipeast = TRUE;
+  graphdata.flipnorth = FALSE;
+  strcpy (graphdata.axis, "2222");
+  strcpy (graphdata.ticks, "2222");
+  strcpy (graphdata.labels, "2222");
+}
+
+# if (0)
+  /* test Xgraph[0], flush junk from pipe */
+  signal (SIGPIPE, XGraphDead);
+  fcntl (Socket[n], F_SETFL,  O_NONBLOCK); 
+  for (i = 0; (read (Socket[n], buffer, 64) > 0) && (i < 20); i++);
+  fcntl (Socket[n], F_SETFL, !O_NONBLOCK); 
+# endif
+
+/* return pointers for current Xgraph, set if desired, test, open if needed */
+int GetGraph (Graphdata *data, int *fd, int *dev) {
+
+  int thisDevice;
+
+  SetImageDevice (FALSE);
+  if (dev == NULL) {
+    if (Active < 0) {
+      thisDevice = 0;
+    } else {
+      thisDevice = Device[Active];
+    }
+  } else {
+    thisDevice = *dev;
+  }
+  
+  if (!open_kapa (thisDevice)) {
+    return (FALSE);
+  }
+  
+  if (data != NULL) *data = graphdata;
+  if (fd != NULL) *fd = Socket[Active];
+
+  return (TRUE);
+}
+
+/* return pointers for given Xgraph, don't set or open */
+int GetGraphData (Graphdata *data, int *fd, int *dev) {
+
+  int n;
+
+  if (dev == 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];
+  if (data != NULL) *data = graphdata;
+
+  return (TRUE);
+}
+
+/* assign given values to current Xgraph */
+void SetGraph (Graphdata data) {
+  graphdata = data;
+}
+
+/** internal tracking of current active device type **/
+/* drop this stuff */
+static int       IsImage = FALSE;
+
+int GetCurrentDevice () {
+  return (IsImage);
+}
+
+void SetImageDevice (int state) {
+  IsImage = state;
+}
+
+/************* image ops ***********/
+// XXX for now, use local static:
+
+static char      Ximbuffer[512];
+static double    Xzero;
+static double    Xrange;
+
+void InitImage () {
+  Xzero = 0;
+  Xrange = 1024;
+  strcpy (Ximbuffer, "none");
+}
+
+/* return pointers for current Ximage, set if desired, test, open if needed */
+int GetImage (int *fd, int *N) {
+  int status;
+  status = GetGraph (NULL, fd, N);
+  return (status);
+}
+
+void SetImageName (char *name) {
+  strcpy (Ximbuffer, name);
+}
+
+char *GetImageName () {
+  return (Ximbuffer);
+}
+ 
+// leave this information on kapa, request when needed? 
+void SetImageScale (double zero, double range) {
+  Xzero = zero;
+  Xrange = range;
+}
+void GetImageScale (double *zero, double *range) {
+  *zero = Xzero;
+  *range = Xrange;
+}
