Index: trunk/Ohana/src/kapa/Makefile
===================================================================
--- trunk/Ohana/src/kapa/Makefile	(revision 5851)
+++ trunk/Ohana/src/kapa/Makefile	(revision 5852)
@@ -23,5 +23,5 @@
 INCS	= 	-I$(INC) -I$(LINC) -I$(XINC)
 #LIBS	= 	-L$(LLIB) $(XLIB) -lFITS -lohana -lX11 -lm
-LIBS	= 	-L$(LLIB) $(XLIB) -lFITS -lohana -lX11 -lsocket -lnsl -lpng -lz -lm
+LIBS	= 	-L$(LLIB) $(XLIB) -lFITS -lkapa -lohana -lX11 -lsocket -lnsl -lpng -lz -lm
 CFLAGS  =	-o $*.$(ARCH).o $(INCS) 
 CCFLAGS	=	$(INCS) $(LIBS) 
@@ -52,12 +52,10 @@
 $(GDIR)/LoadPtext.$(ARCH).o		  $(GDIR)/DrawPtext.$(ARCH).o		\
 $(GDIR)/PSLabels.$(ARCH).o		  $(GDIR)/PSPtext.$(ARCH).o		\
-$(GDIR)/PNGit.$(ARCH).o                   $(GDIR)/PPMit2.$(ARCH).o	        \
-$(GDIR)/DrawRotString.$(ARCH).o        	  $(GDIR)/RotFont.$(ARCH).o             \
-$(GDIR)/PSRotFont.$(ARCH).o
+$(GDIR)/PPMit2.$(ARCH).o	          \
 
 BDRAW = \
-$(GDIR)/bDrawIt.$(ARCH).o	  	  $(GDIR)/bDrawFuncs.$(ARCH).o          \
+$(GDIR)/bDrawIt.$(ARCH).o	  	  \
 $(GDIR)/bDrawFrame.$(ARCH).o              $(GDIR)/bDrawLabels.$(ARCH).o         \
-$(GDIR)/bDrawObjects.$(ARCH).o            $(GDIR)/bDrawRotFont.$(ARCH).o        \
+$(GDIR)/bDrawObjects.$(ARCH).o            \
 $(GDIR)/PNGit2.$(ARCH).o \
 
@@ -75,5 +73,5 @@
 $(SDIR)/GetColor.$(ARCH).o          	  $(SDIR)/SetWMHints.$(ARCH).o         \
 $(SDIR)/LoadFont.$(ARCH).o          	  $(SDIR)/TopWindow.$(ARCH).o          \
-$(SDIR)/MakeColormap.$(ARCH).o      	  $(SDIR)/Ximage.$(ARCH).o             \
+$(SDIR)/Ximage.$(ARCH).o             \
 $(SDIR)/MakeCursor.$(ARCH).o
 
Index: trunk/Ohana/src/kapa/event/CheckPipe.c
===================================================================
--- trunk/Ohana/src/kapa/event/CheckPipe.c	(revision 5851)
+++ trunk/Ohana/src/kapa/event/CheckPipe.c	(revision 5852)
@@ -73,7 +73,7 @@
   
   if (!strcmp (buffer, "LIMS")) {
-    sprintf (buffer, "LIMS: %8.1f %8.1f ", 
-	     section[TheSection].axis[0].dfx, section[TheSection].axis[1].dfy);
-    write (sock, buffer, 30);
+    KiiSendMessage (sock, "%8.1f %8.1f ", 
+		    section[TheSection].axis[0].dfx, 
+		    section[TheSection].axis[1].dfy);
     return (TRUE);
   }
Index: trunk/Ohana/src/kapa/event/Resize.c
===================================================================
--- trunk/Ohana/src/kapa/event/Resize.c	(revision 5851)
+++ trunk/Ohana/src/kapa/event/Resize.c	(revision 5852)
@@ -1,20 +1,10 @@
 # include "Ximage.h"
-# define SCAN \
-  status = read (sock, buffer, 16); \
-  buffer[16] = 0; \
-  sscanf (buffer, "%*s %d", &Nbytes); \
-  status = read (sock, buffer, Nbytes); \
-  buffer[status] = 0; /* make the string easy to parse */ \
-  (DEBUG) && fprintf (stderr, "buffer: <%s>\n", buffer); \
-
 
 int Resize () {
  
-  int i, status, Nbytes;
+  int i;
   unsigned int NX, NY;
-  char buffer[128];
 
-  SCAN;
-  sscanf (buffer, "%d %d", &NX, &NY);
+  KiiScanMessage (sock, "%d %d", &NX, &NY);
 
   if ((graphic.dx == NX) && (graphic.dy == NY)) 
Index: trunk/Ohana/src/kapa/event/SetSection.c
===================================================================
--- trunk/Ohana/src/kapa/event/SetSection.c	(revision 5851)
+++ trunk/Ohana/src/kapa/event/SetSection.c	(revision 5852)
@@ -1,23 +1,14 @@
 # include "Ximage.h"
-# define SCAN \
-status = read (sock, buffer, 16); \
-buffer[16] = 0; \
-sscanf (buffer, "%*s %d", &Nbytes); \
-status = read (sock, buffer, Nbytes); \
-buffer[status] = 0;
 
 int SetSection (int SwitchSection) {
   
-  int i, status, Nbytes, NewSection, ThisSection, MoveSection;
-  char buffer[128], name[128];
+  int i, NewSection, ThisSection, MoveSection;
+  char name[128];
   double x, y, dx, dy;
 
-  SCAN;
-
   if (SwitchSection) {
-    sscanf (buffer, "%s", name);
+    KiiScanMessage (sock, "%s", name);
   } else {
-    sscanf (buffer, "%s %lf %lf %lf %lf", 
-	    name, &x, &y, &dx, &dy);
+    KiiScanMessage (sock, "%s %lf %lf %lf %lf", name, &x, &y, &dx, &dy);
   }    
 
@@ -92,10 +83,8 @@
 int ListSection () {
   
-  int i, status, Nbytes, ThisSection;
-  char buffer[128], name[128];
+  int i, ThisSection;
+  char name[128];
 
-  SCAN;
-
-  sscanf (buffer, "%s", name);
+  KiiScanMessage (sock, "%s", name);
 
   if (!strcmp (name, "*")) {
Index: trunk/Ohana/src/kapa/graph/DrawFrame.c
===================================================================
--- trunk/Ohana/src/kapa/graph/DrawFrame.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/DrawFrame.c	(revision 5852)
@@ -6,4 +6,6 @@
   int i, fx, fy, dfx, dfy, P, IsLabel;
   double range, major, minor, first, next;
+
+  DrawRotTextInit (graphic.display, graphic.window, graphic.gc, graphic.fore, graphic.back);
 
   /* each axis is drawn independently, but ticks and labels are placed according to
Index: trunk/Ohana/src/kapa/graph/DrawLabels.c
===================================================================
--- trunk/Ohana/src/kapa/graph/DrawLabels.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/DrawLabels.c	(revision 5852)
@@ -10,4 +10,5 @@
   fontname = GetRotFont (&size);
   XSetForeground (graphic.display, graphic.gc, graphic.fore);
+  DrawRotTextInit (graphic.display, graphic.window, graphic.gc, graphic.fore, graphic.back);
 
   /* each label is drawn independently */
Index: trunk/Ohana/src/kapa/graph/DrawPtext.c
===================================================================
--- trunk/Ohana/src/kapa/graph/DrawPtext.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/DrawPtext.c	(revision 5852)
@@ -9,4 +9,5 @@
   fontname = GetRotFont (&size);
   XSetForeground (graphic.display, graphic.gc, graphic.fore);
+  DrawRotTextInit (graphic.display, graphic.window, graphic.gc, graphic.fore, graphic.back);
 
   for (i = 0; i < layout[0].Nptext; i++) {
Index: trunk/Ohana/src/kapa/graph/LoadBox.c
===================================================================
--- trunk/Ohana/src/kapa/graph/LoadBox.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/LoadBox.c	(revision 5852)
@@ -1,22 +1,15 @@
 # include "Ximage.h"
-# define SCAN \
-status = read (sock, buffer, 16); \
-buffer[16] = 0; \
-sscanf (buffer, "%*s %d", &Nbytes); \
-status = read (sock, buffer, Nbytes); \
-buffer[status] = 0;
 
 int LoadBox () {
   
-  int i, status, Nbytes;
-  char buffer[128], Axis[16], Labels[16], Ticks[16];
+  int i, status;
+  char Axis[16], Labels[16], Ticks[16];
   Layout *layout;
   
   layout = &section[TheSection];
 
-  SCAN;
-  sscanf (buffer, "%lf %lf %lf %lf", 
-	  &layout[0].axis[0].min, &layout[0].axis[0].max, 
-	  &layout[0].axis[1].min, &layout[0].axis[1].max);
+  KiiScanMessage (sock, "%lf %lf %lf %lf", 
+		  &layout[0].axis[0].min, &layout[0].axis[0].max, 
+		  &layout[0].axis[1].min, &layout[0].axis[1].max);
 
   layout[0].axis[3].min = layout[0].axis[1].min;
@@ -25,6 +18,5 @@
   layout[0].axis[2].max = layout[0].axis[0].max;
   
-  SCAN;
-  sscanf (buffer, "%s %s %s", Axis, Labels, Ticks);
+  KiiScanMessage (sock, "%s %s %s", Axis, Labels, Ticks);
 
   for (i = 0; i < 4; i++) {
@@ -83,5 +75,5 @@
   layout[0].X = 0;
   layout[0].Y = 0;
-   layout[0].expand = 1;
+  layout[0].expand = 1;
 */
   
Index: trunk/Ohana/src/kapa/graph/LoadLabels.c
===================================================================
--- trunk/Ohana/src/kapa/graph/LoadLabels.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/LoadLabels.c	(revision 5852)
@@ -3,6 +3,6 @@
 int LoadLabels () {
   
-  char buffer[256], *c;
-  int status, Nbytes, mode, size;
+  char *c, *label;
+  int mode, size, Nbytes;
   Layout *layout;
   
@@ -11,13 +11,10 @@
   fcntl (sock, F_SETFL, !O_NONBLOCK);  
   
-  status = read (sock, buffer, 16); 
-  buffer[16] = 0; 
-  sscanf (buffer, "%d %d", &Nbytes, &mode); 
+  KiiScanMessage (sock, "%d", &mode);
+  label = KiiRecvData (sock);
 
-  status = read (sock, buffer, Nbytes); 
-  buffer[status] = 0; 
-
-  strncpy (layout[0].label[mode].text, buffer, Nbytes);
-  layout[0].label[mode].text[Nbytes] = 0;
+  Nbytes = MIN (strlen(label), 127);
+  strncpy (layout[0].label[mode].text, label, Nbytes);
+  label[Nbytes] = 0;
 
   fcntl (sock, F_SETFL, O_NONBLOCK);  
Index: trunk/Ohana/src/kapa/graph/LoadObject.c
===================================================================
--- trunk/Ohana/src/kapa/graph/LoadObject.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/LoadObject.c	(revision 5852)
@@ -1,17 +1,8 @@
 # include "Ximage.h"
-# include <unistd.h>
 # include <errno.h>
-
-# define SCAN \
-status = read (sock, buffer, 16); \
-buffer[16] = 0; \
-sscanf (buffer, "%*s %d", &Nbytes); \
-status = read (sock, buffer, Nbytes); \
-buffer[status] = 0;
 
 int LoadObject () {
   
-  char buffer[1024];
-  int status, N, Nbytes;
+  int N;
   Layout *layout;
 
@@ -27,27 +18,25 @@
   layout[0].objects[N].dym = layout[0].objects[N].dyp = (float *) NULL;
   
-  SCAN;
-  sscanf (buffer, "%d %d %d %d %d %d %d %lf %lf",
-	  &layout[0].objects[N].Npts, &layout[0].objects[N].style, 
-	  &layout[0].objects[N].ptype, &layout[0].objects[N].ltype, 
-	  &layout[0].objects[N].etype, &layout[0].objects[N].ebar, 
-	  &layout[0].objects[N].color, 
-	  &layout[0].objects[N].lweight, &layout[0].objects[N].size);
+  KiiScanMessage (sock, "%d %d %d %d %d %d %d %lf %lf",
+		  &layout[0].objects[N].Npts, &layout[0].objects[N].style, 
+		  &layout[0].objects[N].ptype, &layout[0].objects[N].ltype, 
+		  &layout[0].objects[N].etype, &layout[0].objects[N].ebar, 
+		  &layout[0].objects[N].color, 
+		  &layout[0].objects[N].lweight, &layout[0].objects[N].size);
   
   if (DEBUG) fprintf (stderr, "%d %d %d %d %d %d %d %lf %lf\n",
-	  layout[0].objects[N].Npts, layout[0].objects[N].style, 
-	  layout[0].objects[N].ptype, layout[0].objects[N].ltype, 
-	  layout[0].objects[N].etype, layout[0].objects[N].ebar, 
-	  layout[0].objects[N].color, 
-	  layout[0].objects[N].lweight, layout[0].objects[N].size);
+		      layout[0].objects[N].Npts, layout[0].objects[N].style, 
+		      layout[0].objects[N].ptype, layout[0].objects[N].ltype, 
+		      layout[0].objects[N].etype, layout[0].objects[N].ebar, 
+		      layout[0].objects[N].color, 
+		      layout[0].objects[N].lweight, layout[0].objects[N].size);
   
   /* force valid ranges */
-  if ((layout[0].objects[N].color > GetColormapSize()) || (layout[0].objects[N].color < 0))
+  if ((layout[0].objects[N].color > KapaColormapSize()) || (layout[0].objects[N].color < 0))
     layout[0].objects[N].color = 0;
 
-  SCAN;
-  sscanf (buffer, "%lf %lf %lf %lf",
-	  &layout[0].objects[N].x0, &layout[0].objects[N].x1, 
-	  &layout[0].objects[N].y0, &layout[0].objects[N].y1);
+  KiiScanMessage (sock, "%lf %lf %lf %lf",
+		  &layout[0].objects[N].x0, &layout[0].objects[N].x1, 
+		  &layout[0].objects[N].y0, &layout[0].objects[N].y1);
   
   fcntl (sock, F_SETFL, O_NONBLOCK);  
Index: trunk/Ohana/src/kapa/graph/PNGit2.c
===================================================================
--- trunk/Ohana/src/kapa/graph/PNGit2.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/PNGit2.c	(revision 5852)
@@ -1,5 +1,3 @@
 # include "Ximage.h"
-# include <png.h>
-# include "bDraw.h"
 
 int PNGit () {
@@ -58,5 +56,5 @@
   /* png_set_IHDR (png_ptr, info_ptr, graphic.dx, graphic.dy, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); */
 
-  palette = MakePNGPalette (&Npalette);
+  palette = KapaPNGPalette (&Npalette);
   png_set_PLTE (png_ptr, info_ptr, palette, Npalette); 
  
Index: trunk/Ohana/src/kapa/graph/PPMit2.c
===================================================================
--- trunk/Ohana/src/kapa/graph/PPMit2.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/PPMit2.c	(revision 5852)
@@ -1,5 +1,3 @@
 # include "Ximage.h"
-# include <png.h>
-# include "bDraw.h"
 
 int PPMit () {
@@ -25,5 +23,5 @@
   }
   
-  palette = MakePNGPalette (&Npalette);
+  palette = KapaPNGPalette (&Npalette);
 
   dx = graphic.dx;
Index: trunk/Ohana/src/kapa/graph/PSObjects.c
===================================================================
--- trunk/Ohana/src/kapa/graph/PSObjects.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/PSObjects.c	(revision 5852)
@@ -111,5 +111,5 @@
     lweight = MAX (0, MIN (10, layout[0].objects[i].lweight));
     fprintf (f, "%.1f setlinewidth\n", lweight);
-    fprintf (f, "%s setrgbcolor\n", GetRGBString(layout[0].objects[i].color));
+    fprintf (f, "%s setrgbcolor\n", KapaColorRGBString(layout[0].objects[i].color));
 
     switch (layout[0].objects[i].style) {
Index: trunk/Ohana/src/kapa/graph/SetLimits.c
===================================================================
--- trunk/Ohana/src/kapa/graph/SetLimits.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/SetLimits.c	(revision 5852)
@@ -1,14 +1,7 @@
 # include "Ximage.h"
-# define SCAN \
-status = read (sock, buffer, 16); \
-buffer[16] = 0; \
-sscanf (buffer, "%*s %d", &Nbytes); \
-status = read (sock, buffer, Nbytes); \
-buffer[status] = 0;
 
 int SetLimits () {
   
-  int i, status, Nbytes;
-  char buffer[128];
+  int i;
   double xmin, xmax, ymin, ymax;
   Layout *layout;
@@ -16,6 +9,5 @@
   layout = &section[TheSection];
 
-  SCAN;
-  sscanf (buffer, "%lf %lf %lf %lf", &xmin, &xmax, &ymin, &ymax);
+  KiiScanMessage (sock, "%lf %lf %lf %lf", &xmin, &xmax, &ymin, &ymax);
 
   layout[0].axis[2].min = layout[0].axis[0].min = xmin;
Index: trunk/Ohana/src/kapa/graph/bDrawFrame.c
===================================================================
--- trunk/Ohana/src/kapa/graph/bDrawFrame.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/bDrawFrame.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 int bDrawFrame (Layout *layout) {
Index: trunk/Ohana/src/kapa/graph/bDrawFuncs.c
===================================================================
--- trunk/Ohana/src/kapa/graph/bDrawFuncs.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/bDrawFuncs.c	(revision 5852)
@@ -13,5 +13,5 @@
   bDrawBuffer *buffer;
 
-  white = GetColorByName ("white");
+  white = KapaColorByName ("white");
 
   ALLOCATE (buffer, bDrawBuffer, 1);
Index: trunk/Ohana/src/kapa/graph/bDrawIt.c
===================================================================
--- trunk/Ohana/src/kapa/graph/bDrawIt.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/bDrawIt.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 bDrawBuffer *bDrawIt () {
@@ -8,5 +7,5 @@
   bDrawColor black;
 
-  black = GetColorByName ("black");
+  black = KapaColorByName ("black");
 
   buffer = bDrawBufferCreate (graphic.dx, graphic.dy);
Index: trunk/Ohana/src/kapa/graph/bDrawLabels.c
===================================================================
--- trunk/Ohana/src/kapa/graph/bDrawLabels.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/bDrawLabels.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
   
 void bDrawLabels (Layout *layout) {
Index: trunk/Ohana/src/kapa/graph/bDrawObjects.c
===================================================================
--- trunk/Ohana/src/kapa/graph/bDrawObjects.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/bDrawObjects.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 # define DrawLine(X1,Y1,X2,Y2) (bDrawLine ((X1), (Y1), (X2), (Y2)))
@@ -20,5 +19,5 @@
   bDrawColor black;
   
-  black = GetColorByName ("black");
+  black = KapaColorByName ("black");
   for (i = 0; i < layout[0].Nobjects; i++) {
 
Index: trunk/Ohana/src/kapa/graph/bDrawRotFont.c
===================================================================
--- trunk/Ohana/src/kapa/graph/bDrawRotFont.c	(revision 5851)
+++ trunk/Ohana/src/kapa/graph/bDrawRotFont.c	(revision 5852)
@@ -20,6 +20,6 @@
   RotFont *currentfont;
 
-  white = GetColorByName ("white");
-  black = GetColorByName ("black");
+  white = KapaColorByName ("white");
+  black = KapaColorByName ("black");
 
   currentname = GetRotFont (&currentsize);
Index: trunk/Ohana/src/kapa/include/Ximage.h
===================================================================
--- trunk/Ohana/src/kapa/include/Ximage.h	(revision 5851)
+++ trunk/Ohana/src/kapa/include/Ximage.h	(revision 5852)
@@ -1,4 +1,5 @@
 # include <Xohana.h>
-# include <fitsio.h>
+# include <dvo.h>
+# include <kapa.h>
 # include "constants.h"
 # include "structures.h"
Index: trunk/Ohana/src/kapa/include/bDraw.h
===================================================================
--- trunk/Ohana/src/kapa/include/bDraw.h	(revision 5851)
+++ trunk/Ohana/src/kapa/include/bDraw.h	(revision 5852)
@@ -1,2 +1,5 @@
+# ifndef BDRAW_H
+# define BDRAW_H
+
 # include <png.h>
 
@@ -49,2 +52,3 @@
 int bDrawRotBitmap (int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale);
 
+# endif
Index: trunk/Ohana/src/kapa/include/globals.h
===================================================================
--- trunk/Ohana/src/kapa/include/globals.h	(revision 5851)
+++ trunk/Ohana/src/kapa/include/globals.h	(revision 5852)
@@ -5,4 +5,5 @@
 int MAP_WINDOW;
 int FOREGROUND;
+char *NAME_WINDOW;
 
 /* file descriptor for socket connection to mana */
Index: trunk/Ohana/src/kapa/include/prototypes.h
===================================================================
--- trunk/Ohana/src/kapa/include/prototypes.h	(revision 5851)
+++ trunk/Ohana/src/kapa/include/prototypes.h	(revision 5852)
@@ -64,18 +64,9 @@
 int           remove_argument     PROTO((int, int *, char **));
 
-RotFont *GetRotFontData (double *scale);
-int SetRotFont PROTO((char *name, int size));
-void InitRotFonts PROTO(());
-int DrawRotText PROTO((int x, int y, char *string, int pos, double angle));
-int DrawRotBitmap PROTO((int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale));
-char *GetRotFont PROTO((int *size));
-int RotStrlen PROTO((char *c));
-void PSRotText PROTO((FILE *f, int x, int y, char *string, int pos, double angle));
-void PSDumpRotSegment PROTO((FILE *f, char *segment, int *Nseg));
-void PSSetFont PROTO((FILE *f, char *name, int size));
 void MakeGC ();
 
 int EventLoop ();
 void CloseDisplay ();
+int args (int *argc, char **argv);
 
 void DrawPtext (Layout *layout);
@@ -87,4 +78,20 @@
 void PSPtext (Layout *layout, FILE *f);
 void PSTick (FILE *f, double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int mode, int naxis);
+
+/* kapa bDraw Functions */
+int bDrawObjects (Layout *layout);
+void bDrawConnect (Layout *layout, Gobjects *object);
+void bDrawClipLine (double x0, double y0, double x1, double y1, double X0, double Y1, double X1, double Y0);
+void bDrawHistogram (Layout *layout, Gobjects *object);
+void bDrawPoints (Layout *layout, Gobjects *object);
+void bDrawXErrors (Layout *layout, Gobjects *object);
+void bDrawYErrors (Layout *layout, Gobjects *object);
+
+int bDrawFrame (Layout *layout);
+void bDrawTick (double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int mode, int naxis);
+void bDrawLabels (Layout *layout);
+void bDrawPtext (Layout *layout);
+
+bDrawBuffer *bDrawIt ();
 
 /******************* XGRAPH **
Index: trunk/Ohana/src/kapa/include/structures.h
===================================================================
--- trunk/Ohana/src/kapa/include/structures.h	(revision 5851)
+++ trunk/Ohana/src/kapa/include/structures.h	(revision 5852)
@@ -88,4 +88,5 @@
 } Overlay;
   
+# if (0)
 typedef struct {
   char      ctype[16];
@@ -95,4 +96,5 @@
   double    pc2_1, pc2_2;
 } Coords;
+# endif
 
 typedef struct {
@@ -110,15 +112,4 @@
   char label[64];
 } Axis;
-
-typedef struct {
-  int dx, dy, ascent;
-  unsigned char *bits;
-} RotFont;
-
-typedef struct {
-  RotFont *font;
-  char name[64];
-  int size;
-} FontSet;
 
 /******** Here we define the Layout struct specific to this program  *******/
Index: trunk/Ohana/src/kapa/setup/DefineLayout.c
===================================================================
--- trunk/Ohana/src/kapa/setup/DefineLayout.c	(revision 5851)
+++ trunk/Ohana/src/kapa/setup/DefineLayout.c	(revision 5852)
@@ -5,7 +5,5 @@
 void DefineLayout (int argc, char **argv) {
 
-  int i, status, N;
-  struct sockaddr_un Address;
-  char temp[100];
+  int i;
   
   /** initiate connection with server **/
@@ -15,15 +13,5 @@
       exit (0);
     }
-    strcpy (Address.sun_path, argv[1]);
-    Address.sun_family = AF_UNIX;
-    sock = socket (AF_UNIX, SOCK_STREAM, 0);
-    status = connect (sock, (struct sockaddr *) &Address, sizeof (Address));
-    if (status < 0) {
-      fprintf (stderr, "kapa - unsuccessful connection: %d\n", status);
-      exit (0);
-    }
-    fcntl (sock, F_SETFL, O_NONBLOCK);  
-    sprintf (temp, "rm -f %s", argv[1]);
-    system (temp);
+    sock = KiiWait (argv[1]);
   }
 
Index: trunk/Ohana/src/kapa/setup/SetUpWindow.c
===================================================================
--- trunk/Ohana/src/kapa/setup/SetUpWindow.c	(revision 5851)
+++ trunk/Ohana/src/kapa/setup/SetUpWindow.c	(revision 5852)
@@ -5,6 +5,7 @@
 void SetUpWindow (int *argc, char **argv) {
 
-  int N;
   Icon icon;
+  char *name;
+  int Ncolors;
 
   icon.width = icon_width;
@@ -21,8 +22,15 @@
   SetNormalHints ();
   SetWMHints (&icon);
-  NameWindow ("Kapa");
 
-  MakeColormap (*argc, argv);  /* move to SetUpWindow? */
+  if (NAME_WINDOW == NULL) {
+    NameWindow ("Kapa");
+  } else {
+    ALLOCATE (name, char, strlen(NAME_WINDOW) + 10);
+    sprintf (name, "Kapa %s", NAME_WINDOW);
+    NameWindow (name);
+    free (name);
+  }
 
+  graphic.color = KapaX11colors (graphic.display, graphic.colormap, graphic.fore, &Ncolors);
   if (MAP_WINDOW) MapWindow (graphic);
   return;
Index: trunk/Ohana/src/kapa/setup/args.c
===================================================================
--- trunk/Ohana/src/kapa/setup/args.c	(revision 5851)
+++ trunk/Ohana/src/kapa/setup/args.c	(revision 5852)
@@ -9,4 +9,11 @@
     remove_argument(N, argc, argv);
     MAP_WINDOW = FALSE;
+  }
+
+  NAME_WINDOW = NULL;
+  if ((N = get_argument (*argc, argv, "-name"))) {
+    remove_argument(N, argc, argv);
+    NAME_WINDOW = strcreate (argv[N]);
+    remove_argument(N, argc, argv);
   }
 
Index: trunk/Ohana/src/kii/Makefile
===================================================================
--- trunk/Ohana/src/kii/Makefile	(revision 5851)
+++ trunk/Ohana/src/kii/Makefile	(revision 5852)
@@ -21,5 +21,5 @@
 #
 INCS	= 	-I$(INC) -I$(LINC) -I$(XINC)
-LIBS	= 	-L$(LLIB) $(XLIB) -ldvo -lFITS -lohana -ljpeg -lX11 -lsocket -lnsl -lm
+LIBS	= 	-L$(LLIB) $(XLIB) -ldvo -lFITS -lkapa -lohana -ljpeg -lX11 -lsocket -lnsl -lm
 # LIBS	= 	-L$(LLIB) $(XLIB) -lohana -lFITS -ljpeg -lX11 -lsocket -lnsl -lm -static
 CFLAGS  =	-o $*.$(ARCH).o $(INCS) 
@@ -52,8 +52,5 @@
 $(ODIR)/DrawOverlay.$(ARCH).o		  $(ODIR)/CSaveOverlay.$(ARCH).o       \
 $(ODIR)/PaintTickmarks.$(ARCH).o          $(ODIR)/LoadTickmarks.$(ARCH).o      \
-$(ODIR)/bDrawColormap.$(ARCH).o           $(ODIR)/bDrawOverlay.$(ARCH).o       \
-$(ODIR)/bDrawFuncs.$(ARCH).o 		  $(ODIR)/bDrawRotFont.$(ARCH).o       \
-$(ODIR)/RotFont.$(ARCH).o
-
+$(ODIR)/bDrawOverlay.$(ARCH).o
 
 POBJ = \
Index: trunk/Ohana/src/kii/button/PSit.c
===================================================================
--- trunk/Ohana/src/kii/button/PSit.c	(revision 5851)
+++ trunk/Ohana/src/kii/button/PSit.c	(revision 5852)
@@ -246,5 +246,5 @@
   int i, k, m, val;
   double Nchar, Npix, start, slope, frac;
-  char *buff;
+  unsigned char *buff;
 
   Nchar = 255.0;
@@ -318,5 +318,5 @@
   extra = 4 - (dx * 3) % 4;
   /* start at the last line, print lines in decending order */
-  buff = &layout[0].picture.data[(dy - 1)*(3*dx + extra)];
+  buff = (unsigned char *)&layout[0].picture.data[(dy - 1)*(3*dx + extra)];
   slope = layout[0].slope;
   start = layout[0].start;
Index: trunk/Ohana/src/kii/colorbar/CreateColorbar.c
===================================================================
--- trunk/Ohana/src/kii/colorbar/CreateColorbar.c	(revision 5851)
+++ trunk/Ohana/src/kii/colorbar/CreateColorbar.c	(revision 5852)
@@ -28,5 +28,5 @@
   case 16:
     REALLOCATE (layout[0].cmapbar.data, char, 2*dy*dx);
-    out8 = layout[0].cmapbar.data;
+    out8 = (unsigned char *) layout[0].cmapbar.data;
     for (i = 0; i < dx; i++) {
       pixvalue = layout[0].cmap[(int)(i*layout[0].Npixels/dx)].pixel;
@@ -44,5 +44,5 @@
     extra = 4 - (dx * 3) % 4;
     REALLOCATE (layout[0].cmapbar.data, char, dy*(3*dx + extra));
-    out8 = layout[0].cmapbar.data;
+    out8 = (unsigned char *) layout[0].cmapbar.data;
     for (i = 0; i < dx; i++) {
       pixvalue = layout[0].cmap[(int)(i*layout[0].Npixels/dx)].pixel;
Index: trunk/Ohana/src/kii/include/Ximage.h
===================================================================
--- trunk/Ohana/src/kii/include/Ximage.h	(revision 5851)
+++ trunk/Ohana/src/kii/include/Ximage.h	(revision 5852)
@@ -1,4 +1,5 @@
 # include <Xohana.h>
 # include <dvo.h>
+# include <kapa.h>
 # include "constants.h"
 # include "structures.h"
Index: trunk/Ohana/src/kii/include/bDraw.h
===================================================================
--- trunk/Ohana/src/kii/include/bDraw.h	(revision 5851)
+++ trunk/Ohana/src/kii/include/bDraw.h	(revision 5852)
@@ -50,2 +50,3 @@
 int bDrawRotBitmap (int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale);
 
+void bDrawOverlay (Layout *layout, int N);
Index: trunk/Ohana/src/kii/include/constants.h
===================================================================
--- trunk/Ohana/src/kii/include/constants.h	(revision 5851)
+++ trunk/Ohana/src/kii/include/constants.h	(revision 5852)
@@ -25,4 +25,5 @@
 int DECIMAL_DEG;
 int DEBUG;
+int FOREGROUND;
 int USE_XWINDOW;
 int MAP_WINDOW;
Index: trunk/Ohana/src/kii/include/prototypes.h
===================================================================
--- trunk/Ohana/src/kii/include/prototypes.h	(revision 5851)
+++ trunk/Ohana/src/kii/include/prototypes.h	(revision 5852)
@@ -1,12 +1,6 @@
-RotFont *GetRotFontData (double *scale);
-int SetRotFont PROTO((char *name, int size));
-void InitRotFonts PROTO(());
-int DrawRotText PROTO((int x, int y, char *string, int pos, double angle));
-int DrawRotBitmap PROTO((int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale));
-char *GetRotFont PROTO((int *size));
-int RotStrlen PROTO((char *c));
-void PSRotText PROTO((FILE *f, int x, int y, char *string, int pos, double angle));
-void PSDumpRotSegment PROTO((FILE *f, char *segment, int *Nseg));
-void PSSetFont PROTO((FILE *f, char *name, int size));
+
+int args (int *argc, char **argv);
+
+void bDrawOverlay (Layout *layout, int N);
 
 int SetColormap (Graphic *graphic, Layout *layout, char *name);
Index: trunk/Ohana/src/kii/include/structures.h
===================================================================
--- trunk/Ohana/src/kii/include/structures.h	(revision 5851)
+++ trunk/Ohana/src/kii/include/structures.h	(revision 5852)
@@ -67,5 +67,5 @@
   int      dx, dy, x, y;
   XImage  *pix;
-  char    *data;
+  char *data;
 } Picture;
 
@@ -124,15 +124,4 @@
 } Layout;
 
-typedef struct {
-  int dx, dy, ascent;
-  unsigned char *bits;
-} RotFont;
-
-typedef struct {
-  RotFont *font;
-  char name[64];
-  int size;
-} FontSet;
-
 /* this routine is independent of the number of overlays */
 
Index: trunk/Ohana/src/kii/overlay/bDrawColormap.c
===================================================================
--- trunk/Ohana/src/kii/overlay/bDrawColormap.c	(revision 5851)
+++ trunk/Ohana/src/kii/overlay/bDrawColormap.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 /* this list must match the one in mana/include/package.h, and equiv
Index: trunk/Ohana/src/kii/overlay/bDrawFrame.c
===================================================================
--- trunk/Ohana/src/kii/overlay/bDrawFrame.c	(revision 5851)
+++ trunk/Ohana/src/kii/overlay/bDrawFrame.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 int bDrawFrame (Layout *layout) {
Index: trunk/Ohana/src/kii/overlay/bDrawFuncs.c
===================================================================
--- trunk/Ohana/src/kii/overlay/bDrawFuncs.c	(revision 5851)
+++ trunk/Ohana/src/kii/overlay/bDrawFuncs.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 static int bWeight;
Index: trunk/Ohana/src/kii/overlay/bDrawOverlay.c
===================================================================
--- trunk/Ohana/src/kii/overlay/bDrawOverlay.c	(revision 5851)
+++ trunk/Ohana/src/kii/overlay/bDrawOverlay.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 # define INFRONT 4
 
@@ -14,5 +13,5 @@
  
   /* translate color to bDrawColors : layout[0].overlay[N].color */
-  color = GetColorByName (name[N]);
+  color = KapaColorByName (name[N]);
   bDrawSetStyle (color, 0, 0);
   
Index: trunk/Ohana/src/kii/overlay/bDrawRotFont.c
===================================================================
--- trunk/Ohana/src/kii/overlay/bDrawRotFont.c	(revision 5851)
+++ trunk/Ohana/src/kii/overlay/bDrawRotFont.c	(revision 5852)
@@ -1,4 +1,3 @@
 # include "Ximage.h"
-# include "bDraw.h"
 
 # define NROTCHARS 256
Index: trunk/Ohana/src/kii/picture/CreatePicture.c
===================================================================
--- trunk/Ohana/src/kii/picture/CreatePicture.c	(revision 5851)
+++ trunk/Ohana/src/kii/picture/CreatePicture.c	(revision 5852)
@@ -4,5 +4,5 @@
 
   int i, j, extra;
-  char *c;
+  unsigned char *c;
   unsigned int *l;
   unsigned int start, start1, start2, start3;
@@ -13,5 +13,5 @@
   case 8:
     REALLOCATE (layout[0].picture.data, char, layout[0].picture.dx*layout[0].picture.dy);
-    c = layout[0].picture.data;
+    c = (unsigned char *) layout[0].picture.data;
     for (i = 0; i < (layout[0].picture.dx*layout[0].picture.dy); i++, c++)
       *c = start;
@@ -22,5 +22,5 @@
   case 16:
     REALLOCATE (layout[0].picture.data, char, 2*layout[0].picture.dy*layout[0].picture.dx);
-    c = layout[0].picture.data;
+    c = (unsigned char *) layout[0].picture.data;
     start1 = 0x0000ff & (start);
     start2 = 0x0000ff & (start >> 8);
Index: trunk/Ohana/src/kii/picture/JPEGit24.c
===================================================================
--- trunk/Ohana/src/kii/picture/JPEGit24.c	(revision 5851)
+++ trunk/Ohana/src/kii/picture/JPEGit24.c	(revision 5852)
@@ -1,6 +1,4 @@
 # include "Ximage.h"
 # include "jpeglib.h"
-# include <png.h>
-# include "bDraw.h"
 
 # define WHITE_R 255
@@ -18,5 +16,5 @@
   int ii, i, j;
   int i_start, i_end, j_start, j_end;
-  int dropback, extra;  /* this is a bit of a kludge... */
+  int dropback;  /* this is a bit of a kludge... */
   int dx, dy, DX, DY;
   int status, Nbytes, quality;
@@ -194,5 +192,5 @@
     bDrawBuffer *buffer;
 
-    palette = MakePNGPalette (&Npalette);
+    palette = KapaPNGPalette (&Npalette);
 
     buffer = bDrawBufferCreate (dx, dy);
@@ -202,5 +200,5 @@
     }
 
-    white = GetColorByName ("white");
+    white = KapaColorByName ("white");
     for (j = 0; j < dy; j++) {
       for (i = 0; i < dx; i++) {
Index: trunk/Ohana/src/kii/picture/NewPicture.c
===================================================================
--- trunk/Ohana/src/kii/picture/NewPicture.c	(revision 5851)
+++ trunk/Ohana/src/kii/picture/NewPicture.c	(revision 5852)
@@ -1,44 +1,31 @@
 # include "Ximage.h"
-# define SCAN \
-  status = read (layout[0].Ximage, buffer, 16); \
-  buffer[16] = 0; \
-  sscanf (buffer, "%*s %d", &Nbytes); \
-  status = read (layout[0].Ximage, buffer, Nbytes); \
-  buffer[status] = 0; /* make the string easy to parse */ \
-  (DEBUG) && fprintf (stderr, "buffer: <%s>\n", buffer); \
 
 int NewPicture (Graphic *graphic, Layout *layout) {
 
   Header header;
-  char buffer[1024], *buff;
-  int status, bytes_left, Nbytes;
+  char *buff;
+  int status, bytes_left, socket;
  
-  fits_free_matrix (&layout[0].matrix);
-  sprintf (buffer, "NCOLOR: %6d ", layout[0].Npixels);
-  write (layout[0].Ximage, buffer, 16);
+  socket = layout[0].Ximage;
 
-  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  
+  KiiSendMessage (socket, "%d", layout[0].Npixels);
+
+  fcntl (socket, F_SETFL, !O_NONBLOCK);  
 
   header.Naxes = 2;
-  SCAN;
-  sscanf (buffer, "%d %d %d %d %lf %lf",
-	  &header.Naxis[0], &header.Naxis[1], &header.bitpix, 
-	  &header.unsign,   &header.bzero,    &header.bscale);
 
-  SCAN;
-  sscanf (buffer, "%lf %lf %lf %lf %d", &layout[0].zero, &layout[0].range, &layout[0].min,  &layout[0].max, &header.size);
+  KiiScanMessage (socket, "%d %d %d %d %lf %lf", &header.Naxis[0], &header.Naxis[1]);
+  KiiScanMessage (socket, "%d %d %d %d %lf %lf", &header.bitpix, &header.unsign, &header.bzero, &header.bscale);
+  KiiScanMessage (socket, "%lf %lf",  &layout[0].zero, &layout[0].range);
+  KiiScanMessage (socket, "%lf %lf %d", &layout[0].min,  &layout[0].max, &header.size);
+  KiiScanMessage (socket, "%lf %f %f %f %f", &layout[0].coords.crval1, &layout[0].coords.crpix1, &layout[0].coords.cdelt1, &layout[0].coords.pc1_1, &layout[0].coords.pc1_2);
+  KiiScanMessage (socket, "%lf %f %f %f %f", &layout[0].coords.crval2, &layout[0].coords.crpix2, &layout[0].coords.cdelt2, &layout[0].coords.pc2_1, &layout[0].coords.pc2_2);
+	  
+  KiiScanMessage (socket, "%s %s %s", layout[0].coords.ctype, layout[0].file, layout[0].buffer_name);
 
-  SCAN;
-  sscanf (buffer, "%lf %f %f %f %f", &layout[0].coords.crval1, &layout[0].coords.crpix1, &layout[0].coords.cdelt1, &layout[0].coords.pc1_1, &layout[0].coords.pc1_2);
-	  
-  SCAN;
-  sscanf (buffer, "%lf %f %f %f %f", &layout[0].coords.crval2, &layout[0].coords.crpix2, &layout[0].coords.cdelt2, &layout[0].coords.pc2_1, &layout[0].coords.pc2_2);
-	  
-  SCAN;
-  sscanf (buffer, "%s %s %s", layout[0].coords.ctype, layout[0].file, layout[0].buffer_name);
-
+  fits_free_matrix (&layout[0].matrix);
   fits_create_matrix (&header, &layout[0].matrix);
 
-  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
+  fcntl (socket, F_SETFL, O_NONBLOCK);  
 
   status = 1;
@@ -47,5 +34,5 @@
   layout[0].matrix.size = 0;
   while (bytes_left > 0) {
-    status = read (layout[0].Ximage, buff, bytes_left);
+    status = read (socket, buff, bytes_left);
     if (status == 0) {  /* No more pipe */
       fprintf (stderr, "error: pipe closed\n");
@@ -59,5 +46,5 @@
   }
 
-  (DEBUG) && fprintf (stderr, "read %d bytes\n", layout[0].matrix.size);
+  if (DEBUG) fprintf (stderr, "read %d bytes\n", layout[0].matrix.size);
   /* it it not obvious this condition should kill kii, but ... */
   if (layout[0].matrix.size != header.size) {  
@@ -69,7 +56,7 @@
 
   Remap (graphic, layout, &layout[0].matrix);
-  (DEBUG) && fprintf (stderr, "remapped image\n");
+  if (DEBUG) fprintf (stderr, "remapped image\n");
   Refresh (graphic, layout, 0);
-  (DEBUG) && fprintf (stderr, "refreshed\n");
+  if (DEBUG) fprintf (stderr, "refreshed\n");
   XFlush (graphic[0].display);
 
Index: trunk/Ohana/src/kii/picture/Remap8.c
===================================================================
--- trunk/Ohana/src/kii/picture/Remap8.c	(revision 5851)
+++ trunk/Ohana/src/kii/picture/Remap8.c	(revision 5852)
@@ -68,6 +68,6 @@
   if ((i_end - i_start) % expand_out == 0) dropback = 0;
 
-  out_pix = layout[0].picture.data;
-  in_pix  = (unsigned char *) (matrix[0].buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
+  out_pix = (unsigned char *) layout[0].picture.data;
+  in_pix  = (unsigned char *) matrix[0].buffer + DX*(int)MAX(Y,0) + (int)MAX(X,0);
 
   /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
Index: trunk/Ohana/src/kii/picture/Resize.c
===================================================================
--- trunk/Ohana/src/kii/picture/Resize.c	(revision 5851)
+++ trunk/Ohana/src/kii/picture/Resize.c	(revision 5852)
@@ -1,20 +1,9 @@
 # include "Ximage.h"
-# define SCAN \
-  status = read (layout[0].Ximage, buffer, 16); \
-  buffer[16] = 0; \
-  sscanf (buffer, "%*s %d", &Nbytes); \
-  status = read (layout[0].Ximage, buffer, Nbytes); \
-  buffer[status] = 0; /* make the string easy to parse */ \
-  (DEBUG) && fprintf (stderr, "buffer: <%s>\n", buffer); \
-
 
 int Resize (Graphic *graphic, Layout *layout) {
  
-  int status, Nbytes;
   unsigned int NX, NY;
-  char buffer[128];
 
-  SCAN;
-  sscanf (buffer, "%d %d", &NX, &NY);
+  KiiScanMessage (layout[0].Ximage, "%d %d", &NX, &NY);
 
   NX += ZOOM_X + 3*PAD1 + 25;
Index: trunk/Ohana/src/kii/setup/CheckVisual.c
===================================================================
--- trunk/Ohana/src/kii/setup/CheckVisual.c	(revision 5851)
+++ trunk/Ohana/src/kii/setup/CheckVisual.c	(revision 5852)
@@ -1,5 +1,4 @@
 # include "Ximage.h"
 # define NPIXELS 64
-# define DEBUG 1
 
 /* DirectColor doesn't seem to work, even though it is available:
@@ -195,6 +194,3 @@
     }
   }
-  
-
 }
-
Index: trunk/Ohana/src/kii/setup/DefineLayout.c
===================================================================
--- trunk/Ohana/src/kii/setup/DefineLayout.c	(revision 5851)
+++ trunk/Ohana/src/kii/setup/DefineLayout.c	(revision 5852)
@@ -5,25 +5,14 @@
 void DefineLayout (Layout *layout, Graphic *graphic, int argc, char **argv) {
 
-  int i, status, N;
-  struct sockaddr_un Address;
-  char temp[100], *temp_name;
+  int i, N;
+  char *colormap;
 
   /** initiate connection with mana **/
-  if (!DEBUG) {	
+  if (!FOREGROUND) {
     if (argc < 2) {
       fprintf (stderr, "socket path not specified\n");
       exit (0);
     }
-    strcpy (Address.sun_path, argv[1]);
-    Address.sun_family = AF_UNIX;
-    layout[0].Ximage = socket (AF_UNIX, SOCK_STREAM, 0);
-    status = connect (layout[0].Ximage, (struct sockaddr *) &Address, (socklen_t) sizeof (Address));
-    if (status < 0) {
-      fprintf (stderr, "kii - unsuccessful connection: %d\n", status);
-      exit (0);
-    }
-    fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
-    sprintf (temp, "rm -f %s", argv[1]);
-    system (temp);
+    layout[0].Ximage = KiiWait (argv[1]);
   }
 
@@ -34,11 +23,11 @@
     MakeColormap (graphic, layout, argc, argv);
   } else {
-    temp_name = default_colormap;
+    colormap = default_colormap;
     if ((N = get_argument (argc, argv, "-cm"))) {
       remove_argument (N, &argc, argv);
-      temp_name = argv[N];
+      colormap = argv[N];
     }
     layout[0].Npixels = 256;
-    SetColormap (graphic, layout, temp_name);
+    SetColormap (graphic, layout, colormap);
   }
 
@@ -70,8 +59,6 @@
 }
 
-
 /* this routine is independent of the number of overlays */
 
   /* cmap = the mapping of value to color in kii
      colormap = the set of pixel -> color representation in X */
-
Index: trunk/Ohana/src/kii/setup/SetUpWindow.c
===================================================================
--- trunk/Ohana/src/kii/setup/SetUpWindow.c	(revision 5851)
+++ trunk/Ohana/src/kii/setup/SetUpWindow.c	(revision 5852)
@@ -5,5 +5,4 @@
 void SetUpWindow (Graphic *graphic, int *argc, char **argv) {
 
-  int N;
   Icon icon;
   char *name;
Index: trunk/Ohana/src/kii/setup/args.c
===================================================================
--- trunk/Ohana/src/kii/setup/args.c	(revision 5851)
+++ trunk/Ohana/src/kii/setup/args.c	(revision 5852)
@@ -24,9 +24,14 @@
   }
 
+  DEBUG = FALSE;
   if ((N = get_argument (*argc, argv, "-debug"))) {
     remove_argument(N, argc, argv);
     DEBUG = TRUE;
-  } else {
-    DEBUG = FALSE;
+  }
+
+  FOREGROUND = FALSE;
+  if ((N = get_argument (*argc, argv, "-fg"))) {
+    remove_argument(N, argc, argv);
+    FOREGROUND = TRUE;
   }
 
Index: trunk/Ohana/src/libkapa/Makefile
===================================================================
--- trunk/Ohana/src/libkapa/Makefile	(revision 5851)
+++ trunk/Ohana/src/libkapa/Makefile	(revision 5852)
@@ -27,9 +27,17 @@
 $(SRC)/KiiOverlay.$(ARCH).o \
 $(SRC)/KiiCursor.$(ARCH).o \
-$(SRC)/KiiConvert.$(ARCH).o
+$(SRC)/KiiConvert.$(ARCH).o \
+$(SRC)/KapaWindow.$(ARCH).o \
+$(SRC)/KapaColors.$(ARCH).o
 
-# $(SRC)/KiiTickmarks.$(ARCH).o
+DRAW = \
+$(SRC)/bDrawFuncs.$(ARCH).o     \
+$(SRC)/bDrawRotFont.$(ARCH).o   \
+$(SRC)/RotFont.$(ARCH).o        \
+$(SRC)/DrawRotString.$(ARCH).o  \
+$(SRC)/PSRotFont.$(ARCH).o
 
 $(OBJS): $(INCS)
+$(DRAW): $(INCS)
 
 $(DESTLIB)/libkapa.a: $(LIB)/libkapa.$(ARCH).a
@@ -38,8 +46,8 @@
 	cp $(LIB)/libkapa.$(ARCH).a $(DESTLIB)/libkapa.a
 
-$(LIB)/libkapa.$(ARCH).a: $(OBJS)
+$(LIB)/libkapa.$(ARCH).a: $(OBJS) $(DRAW)
 	@if [ ! -d $(LIB) ]; then mkdir -p $(LIB); fi
 	rm -f $(LIB)/libkapa.$(ARCH).a
-	ar rcv $(LIB)/libkapa.$(ARCH).a $(OBJS)
+	ar rcv $(LIB)/libkapa.$(ARCH).a $(OBJS) $(DRAW)
 	$(RANLIB) $(LIB)/libkapa.$(ARCH).a
 
Index: trunk/Ohana/src/libkapa/include/kapa.h
===================================================================
--- trunk/Ohana/src/libkapa/include/kapa.h	(revision 5851)
+++ trunk/Ohana/src/libkapa/include/kapa.h	(revision 5852)
@@ -1,4 +1,7 @@
 # ifndef KAPA_H
 # define KAPA_H
+
+# include <X11/Xlib.h>
+# include <png.h>
 
 typedef struct {
@@ -34,18 +37,55 @@
 } KiiOverlayType;
 
+typedef struct {
+  char *name;
+  float x;
+  float y;
+  float dx;
+  float dy;
+} KapaSection;
+
+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;
+
+typedef struct {
+  int dx, dy, ascent;
+  unsigned char *bits;
+} RotFont;
+
+typedef struct {
+  RotFont *font;
+  char name[64];
+  int size;
+} FontSet;
+
+typedef png_byte bDrawColor;
+
+typedef struct {
+  int Nx, Ny;
+  bDrawColor **pixels;
+} bDrawBuffer;
+
 /* IOfuncs.c */
 int KiiSendMessage (int device, char *format, ...);
 int KiiSendCommand (int device, int length, char *format, ...);
 int KiiSendCommandV (int device, int length, char *format, va_list argp);
+int KiiScanMessage (int device, char *format, ...);
+int KiiSendData (int device, char *data, int Nbytes);
+char *KiiRecvData (int device);
 
 /* KiiOpen.c */
 int KiiOpen (char *kii_exec, char *name);
 int KiiClose (int socket);
+int KiiWait (char *sockpath);
 
 /* KiiPicture.c */
 int KiiNewPicture1D (int fd, KiiImage *image, KiiDisplayMode *mode, Coords *coords);
 int KiiNewPicture2D (int fd, KiiImage *image, KiiDisplayMode *mode, Coords *coords);
-int KiiResize (int fd, int Nx, int Ny);
-int KiiCenter (int fd, double x, double y, int zoom);
 
 /* KiiOverlay.c */
@@ -58,6 +98,8 @@
 
 /* KiiConvert.c */
-int KiiJpeg (int fd, char *filename);
 int KiiPS (int fd, int scale, int raw, char *filename);
+int KiiJPEG (int fd, char *filename);
+int KapaPNG (int fd, char *filename);
+int KapaPPM (int fd, char *filename);
 
 /* KiiCursor.c */
@@ -66,3 +108,68 @@
 int KiiCursorRead (int fd, double *x, double *y, char *key);
 
+/* KapaWindow.c */
+int KiiResize (int fd, int Nx, int Ny);
+int KiiCenter (int fd, double x, double y, int zoom);
+int KapaBox (int fd, Graphdata *graphdata);
+int KapaClear (int fd, int ClearSection);
+int KapaSetFont (int fd, char *name, int size);
+int KapaSendLabel (int fd, char *string, int mode);
+int KapaSendTextline (int fd, char *string, float x, float y, float angle);
+int KapaSetLimits (int fd, Graphdata *graphmode);
+int KapaSetSection (int fd, KapaSection *section);
+int KapaSelectSection (int fd, char *name);
+int KapaGetSection (int fd, char *name);
+
+/* KapaColors */
+int KapaColorByName (char *name);
+int KapaColormapSize ();
+char *KapaColorRGBString (int N);
+png_color *KapaPNGPalette (int *Npalette);
+unsigned long *KapaX11colors (Display *display, Colormap colormap, unsigned long default_color, int *Ncolors);
+
+/* RotFont.c */
+void InitRotFonts PROTO(());
+int SetRotFont PROTO((char *name, int size));
+char *GetRotFont PROTO((int *size));
+RotFont *GetRotFontData (double *scale);
+int RotStrlen PROTO((char *c));
+
+/* DrawRotString.c */
+int DrawRotText PROTO((int x, int y, char *string, int pos, double angle));
+int DrawRotBitmap PROTO((int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale));
+int DrawRotTextInit (Display *display, Window window, GC gc, unsigned long fore, unsigned long back);
+
+
+/* PSRotFont.c */
+void PSRotText PROTO((FILE *f, int x, int y, char *string, int pos, double angle));
+void PSDumpRotSegment PROTO((FILE *f, char *segment, int *Nseg));
+void PSSetFont PROTO((FILE *f, char *name, int size));
+
+/* bDrawFuncs.c */
+bDrawBuffer *bDrawBufferCreate (int Nx, int Ny);
+void bDrawBufferFree (bDrawBuffer *buffer);
+void bDrawSetBuffer (bDrawBuffer *buffer);
+void bDrawSetStyle (bDrawColor color, int lw, int lt);
+void bDrawPoint (int x, int y);
+void bDrawPointf (float x, float y);
+
+void bDrawArc (double Xc, double Yc, double Xr, double Yr, double Ts, double Te);
+void bDrawCircle (double Xc, double Yc, double radius);
+void bDrawCircleFill (double xc, double yc, double radius);
+
+void bDrawLine (double x1, double y1, double x2, double y2);
+void bDrawLineWeight (int X1, int Y1, int X2, int Y2, int swapcoords);
+void bDrawLineBresen (int X1, int Y1, int X2, int Y2, int swapcoords);
+void bDrawLineHorizontal (int X1, int X2, int Y);
+void bDrawLineVertical (int X, int Y1, int Y2);
+
+void bDrawRectOpen (double x1, double y1, double x2, double y2);
+void bDrawRectFill (double x1, double y1, double x2, double y2);
+void bDrawTriOpen (double x1, double y1, double x2, double y2, double x3, double y3);
+void bDrawTriFill (double x1, double y1, double x2, double y2, double x3, double y3);
+
+/* bDrawRotFont.c */
+int bDrawRotText (int x, int y, char *string, int pos, double angle);
+int bDrawRotBitmap (int x, int y, int dx, int dy, unsigned char *bitmap, int mode, double angle, double scale);
+
 # endif
Index: trunk/Ohana/src/libkapa/include/kapa_internal.h
===================================================================
--- trunk/Ohana/src/libkapa/include/kapa_internal.h	(revision 5851)
+++ trunk/Ohana/src/libkapa/include/kapa_internal.h	(revision 5852)
@@ -10,4 +10,5 @@
 # include <time.h>
 # include <errno.h>
+
 # include <ohana.h>
 # include <dvo.h>
Index: trunk/Ohana/src/libkapa/src/IOfuncs.c
===================================================================
--- trunk/Ohana/src/libkapa/src/IOfuncs.c	(revision 5851)
+++ trunk/Ohana/src/libkapa/src/IOfuncs.c	(revision 5852)
@@ -1,3 +1,31 @@
 # include <kapa_internal.h>
+
+/* why is this not defined in stdarg.h for linux/x64? */
+int vsscanf(const char *str, const char *format, va_list ap);
+
+int KiiSendData (int device, char *data, int Nbytes) {
+
+  KiiSendCommand (device, 16, "LEN: %11d", Nbytes);
+  write (device, data, Nbytes);
+  return (TRUE);
+} 
+
+char *KiiRecvData (int device) {
+
+  int status, Nbytes;
+  char *data, buffer[20];
+
+  /* read 16 bytes: LEN (length) */
+  status = read (device, buffer, 16);
+  buffer[16] = 0;
+
+  /* find the message length, allocate space */
+  sscanf (buffer, "%*s %d", &Nbytes);
+  ALLOCATE (data, char, Nbytes + 1);
+  read (device, data, Nbytes);
+  data[Nbytes] = 0;
+
+  return (data);
+} 
 
 /* send a message of arbitrary size, sending the size first */
@@ -14,8 +42,10 @@
   if (!Nbyte) return (FALSE);
 
+  /* the message may contain up to 99,999,999,999 bytes (100MB) */
   va_start (argp, format);
-  KiiSendCommand (device, 16, "NBYTES: %6d", Nbyte);
+  KiiSendCommand (device, 16, "LEN: %11d", Nbyte);
   status = KiiSendCommandV (device, Nbyte, format, argp);
   va_end (argp);
+
   return (status);
 }
@@ -29,4 +59,5 @@
   status = KiiSendCommandV (device, length, format, argp);
   va_end (argp);
+
   return (status);
 }
@@ -36,12 +67,41 @@
   char *string;
 
-  /* I allocated and zero 1 extra byte */
+  /* string is sent WITHOUT ending NULL char */
+  /* allocate and zero length + 1 extra byte */
   ALLOCATE (string, char, length + 1);
   memset (string, 0, length + 1);
   vsnprintf (string, length + 1, format, argp);
 
-  /* fprintf (stderr, "msg: %s\n", string); */
   write (device, string, length);
   free (string);
   return (TRUE);
 }
+
+/* scan a message of arbitrary size, accepting the size first */
+int KiiScanMessage (int device, char *format, ...) {
+
+  int Nbytes, status;
+  char buffer[20], *message;
+  va_list argp;  
+
+  /* read 16 bytes: LEN (length) */
+  status = read (device, buffer, 16);
+  buffer[16] = 0;
+
+  /* find the message length, allocate space */
+  sscanf (buffer, "%*s %d", &Nbytes);
+  ALLOCATE (message, char, Nbytes + 1);
+
+  /* read Nbytes from the device */
+  status = read (device, message, Nbytes);
+  if (status != Nbytes) fprintf (stderr, "Kii/Kapa comm error\n");
+  message[status] = 0;
+  /* make the string easy to parse */
+
+  /* scan the incoming message */
+  va_start (argp, format);
+  Nbytes = vsscanf (message, format, argp);
+  va_end (argp);
+
+  return (status);
+}
Index: trunk/Ohana/src/libkapa/src/KapaWindow.c
===================================================================
--- trunk/Ohana/src/libkapa/src/KapaWindow.c	(revision 5851)
+++ trunk/Ohana/src/libkapa/src/KapaWindow.c	(revision 5852)
@@ -1,20 +1,95 @@
-# include <kapa_internals.h>
+# include <kapa_internal.h>
 
-typedef struct {
-  double xmin, xmax, ymin, ymax;
-  int style, ptype, ltype, etype, ebar, color;
-  double lweight, size;
-  Coords coords;
-  int flipeast, flipnorth;
-} Graphdata;
+int KiiResize (int fd, int Nx, int Ny) {
 
-int KapaBox (int fd, Graphdata *graphdata, char *Axis, char *Labels, char *Ticks) {
-
-  
-
-  SendGraphCommand (fd, 4, "DBOX");
-  SendGraphMessage (fd, "%12.6g %12.6g %12.6g %12.6g", 
-		    graphdata[0].xmin, graphdata[0].xmax, graphdata[0].ymin, graphdata[0].ymax);
-  SendGraphMessage (fd, "%s %s %s", Axis, Labels, Ticks);
+  KiiSendCommand (fd, 4, "RSIZ");
+  KiiSendMessage (fd, "%d %d", Nx, Ny); 
   return (TRUE);
 }
+
+int KiiCenter (int fd, double x, double y, int zoom) {
+
+  KiiSendCommand (fd, 4, "CENT");
+  KiiSendMessage (fd, "%8.3f %8.3f %8d ", x, y, zoom);
+  return (TRUE);
+}
+
+int KapaBox (int fd, Graphdata *graphdata) {
+
+  KiiSendCommand (fd, 4, "DBOX");
+
+  KiiSendMessage (fd, "%12.6g %12.6g %12.6g %12.6g", 
+		    graphdata[0].xmin, graphdata[0].xmax, graphdata[0].ymin, graphdata[0].ymax);
+
+  KiiSendMessage (fd, "%s %s %s", graphdata[0].axis, graphdata[0].labels, graphdata[0].ticks);
+  return (TRUE);
+}
+
+int KapaClear (int fd, int ClearSection) {
+
+  if (ClearSection) {
+    KiiSendCommand (fd, 4, "ERSS");
+  } else {
+    KiiSendCommand (fd, 4, "ERAS");
+  }
+  return (TRUE);
+}
+
+int KapaSetFont (int fd, char *name, int size) {
+
+  KiiSendCommand (fd, 4, "FONT");
+  KiiSendCommand (fd, 16, "%s", name);
+  KiiSendCommand (fd, 16, "%d", size);
+  return (TRUE);
+}
+
+int KapaSendLabel (int fd, char *string, int mode) {
+  
+  KiiSendCommand (fd, 4, "LABL");
+  KiiSendMessage (fd, "%6d", mode);
+  KiiSendData (fd, string, strlen(string));
+  return (TRUE);
+}
+
+int KapaSendTextline (int fd, char *string, float x, float y, float angle) {
+  
+  KiiSendCommand (fd, 4, "PTXT");
+  KiiSendMessage (fd, "%f", x);
+  KiiSendMessage (fd, "%f", y);
+  KiiSendMessage (fd, "%f", angle);
+  KiiSendData (fd, string, strlen(string));
+  return (TRUE);
+}
+
+int KapaSetLimits (int fd, Graphdata *graphmode) {
+
+  KiiSendCommand (fd, 4, "SLIM");
+  KiiSendMessage (fd, "%g %g %g %g ", graphmode[0].xmin, graphmode[0].xmax, graphmode[0].ymin, graphmode[0].ymax);
+  return (TRUE);
+}
+
+int KapaSetSection (int fd, KapaSection *section) {
+
+  KiiSendCommand (fd, 4, "DSEC");
+  KiiSendMessage (fd, "%s %6.3f %6.3f %6.3f %6.3f", 
+		  section[0].name, 
+		  section[0].x,
+		  section[0].y,
+		  section[0].dx,
+		  section[0].dy);
+  return (TRUE);
+}
+
+int KapaSelectSection (int fd, char *name) {
+
+  KiiSendCommand (fd, 4, "SSEC");
+  KiiSendMessage (fd, "%s", name);
+  return (TRUE);
+}
+
+int KapaGetSection (int fd, char *name) {
+
+  KiiSendCommand (fd, 4, "LSEC");
+  KiiSendMessage (fd, "%s", name);
+  return (TRUE);
+}
Index: trunk/Ohana/src/libkapa/src/KiiConvert.c
===================================================================
--- trunk/Ohana/src/libkapa/src/KiiConvert.c	(revision 5851)
+++ trunk/Ohana/src/libkapa/src/KiiConvert.c	(revision 5852)
@@ -1,11 +1,34 @@
 # include <kapa_internal.h>
 
-int KiiJpeg (int fd, char *filename) {
+int KiiJPEG (int fd, char *filename) {
 
   char buffer[20];
 
   KiiSendCommand (fd, 4, "JPEG");
-  KiiSendCommand (fd, 16, "LEN: %11d", strlen(filename));
-  write (fd, filename, strlen(filename));
+  KiiSendMessage (fd, "%s", filename);
+
+  /* block for the response, which says the operation completed */
+  read (fd, buffer, 4);
+  return (TRUE);
+}
+
+int KapaPNG (int fd, char *filename) {
+
+  char buffer[20];
+
+  KiiSendCommand (fd, 4, "PNGF");
+  KiiSendMessage (fd, "%s", filename);
+
+  /* block for the response, which says the operation completed */
+  read (fd, buffer, 4);
+  return (TRUE);
+}
+
+int KapaPPM (int fd, char *filename) {
+
+  char buffer[20];
+
+  KiiSendCommand (fd, 4, "PPMF");
+  KiiSendMessage (fd, "%s", filename);
 
   /* block for the response, which says the operation completed */
@@ -32,6 +55,5 @@
 
 name:
-  KiiSendCommand (fd, 16, "LEN: %11d", strlen(filename));
-  write (fd, filename, strlen(filename));
+  KiiSendMessage (fd, "%s", filename);
 
   /* block for the response, which says the operation completed */
Index: trunk/Ohana/src/libkapa/src/KiiOpen.c
===================================================================
--- trunk/Ohana/src/libkapa/src/KiiOpen.c	(revision 5851)
+++ trunk/Ohana/src/libkapa/src/KiiOpen.c	(revision 5852)
@@ -4,6 +4,7 @@
 int KiiOpen (char *kii_exec, char *name) {
 
-  int InitSocket, status, addreslen;
+  int InitSocket, status;
   struct sockaddr_un Address;
+  socklen_t AddressLength;
   char temp[128], socket_name[64];
   int Ntry, fd;
@@ -30,5 +31,5 @@
   system (temp);
 
-  addreslen =  sizeof (Address);
+  AddressLength =  sizeof (Address);
   fcntl (InitSocket, F_SETFL, O_NONBLOCK); 
 
@@ -36,5 +37,5 @@
   Ntry = 0;
   while (Ntry < NTRY) {
-    fd = accept (InitSocket, (struct sockaddr *)&Address, &addreslen);
+    fd = accept (InitSocket, (struct sockaddr *)&Address, &AddressLength);
     if (fd != -1) break;
     if (errno != EAGAIN) break;
@@ -49,4 +50,22 @@
 }
 
+int KiiWait (char *sockpath) {
+
+  int sock, status;
+  struct sockaddr_un Address;
+
+  strcpy (Address.sun_path, sockpath);
+  Address.sun_family = AF_UNIX;
+  sock = socket (AF_UNIX, SOCK_STREAM, 0);
+  status = connect (sock, (struct sockaddr *) &Address, sizeof (Address));
+  if (status < 0) {
+    fprintf (stderr, "unsuccessful connection: %d\n", status);
+    exit (0);
+  }
+  fcntl (sock, F_SETFL, O_NONBLOCK);  
+  unlink (sockpath);
+  return (sock);
+}
+
 int KiiClose (int fd) {
 
Index: trunk/Ohana/src/libkapa/src/KiiPicture.c
===================================================================
--- trunk/Ohana/src/libkapa/src/KiiPicture.c	(revision 5851)
+++ trunk/Ohana/src/libkapa/src/KiiPicture.c	(revision 5852)
@@ -7,5 +7,4 @@
   float *in, min, max;
   char *out, *outbuffer, *root;
-  char buffer[1024];
   double a1, a2;
 
@@ -13,9 +12,5 @@
 
   KiiSendCommand (fd, 4, "READ"); /* tell Ximage to look for the incoming image */
-
-  /* convert from (float *) to 1 byte chars -- depends on how many colors Kii owns */
-  /* use this response as a handshake test */
-  read (fd, buffer, 16); 
-  sscanf (buffer, "%*s %d", &Ncolors); 
+  KiiScanMessage (fd, "%d", &Ncolors);
 
   ALLOCATE (outbuffer, char, Npix);
@@ -47,6 +42,8 @@
 
   /* done with the conversion, now send Ximage the converted picture */
-  KiiSendMessage (fd, "%8d %8d 8 1 %f %f", image[0].Nx, image[0].Ny, 0.0, 1.0);
-  KiiSendMessage (fd, "%f %f %f %f %d ", mode[0].zero, mode[0].range, min, max, size);
+  KiiSendMessage (fd, "%8d %8d", image[0].Nx, image[0].Ny);
+  KiiSendMessage (fd, "8 1 0.0 1.0");
+  KiiSendMessage (fd, "%f %f ", mode[0].zero, mode[0].range);
+  KiiSendMessage (fd, "%f %f %d ", min, max, size);
   KiiSendMessage (fd, "%f %f %g %g %g ", coords[0].crval1, coords[0].crpix1, coords[0].cdelt1, coords[0].pc1_1, coords[0].pc1_2);
   KiiSendMessage (fd, "%f %f %g %g %g ", coords[0].crval2, coords[0].crpix2, coords[0].cdelt2, coords[0].pc2_1, coords[0].pc2_2);
@@ -55,4 +52,5 @@
   root = filerootname (image[0].file);
   KiiSendMessage (fd, "%s %s %s ", coords[0].ctype, root, image[0].name);
+  free (root);
 
   /* send the image data */
@@ -135,16 +133,2 @@
    KiiSendFilename
 */
-
-int KiiResize (int fd, int Nx, int Ny) {
-
-  KiiSendCommand (fd, 4, "RSIZ");
-  KiiSendMessage (fd, "%d %d", Nx, Ny); 
-  return (TRUE);
-}
-
-int KiiCenter (int fd, double x, double y, int zoom) {
-
-  KiiSendCommand (fd, 4, "CENT");
-  KiiSendMessage (fd, "%8.3f %8.3f %8d ", x, y, zoom);
-  return (TRUE);
-}
