chiark / gitweb /
merge
[moebius2.git] / view.c
diff --git a/view.c b/view.c
index d2650113e0d00f98d703c67928f2aa072a6ea0b6..a395c8c513eef6da2e4a861fd03fc95f492aeb5e 100644 (file)
--- a/view.c
+++ b/view.c
@@ -5,8 +5,6 @@
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/poll.h>
 
 #include "mgraph.h"
@@ -25,6 +23,7 @@ static GSL_MATRIX(transform);
 static FILE *input_f;
 static struct stat input_stab;
 static const char *input_filename;
+static int pause_updates;
 
 static void read_input(void) {
   int r;
@@ -46,7 +45,7 @@ static void transform_coordinates(void) {
 
   int v, k;
   
-  FOR_VERTEX(v) {
+  FOR_VERTEX(v,INNER) {
     input_gsl.data= &conformation[v][0];
     GA( gsl_blas_dgemv(CblasNoTrans, 1.0,&transform_gsl, &input_gsl,
                       0.0, &result_gsl) );
@@ -78,7 +77,7 @@ static void generate_display_list(void) {
   int vb, ve[V6], e;
 
   ntris= 0;
-  FOR_VERTEX(vb) {
+  FOR_VERTEX(vb,INNER) {
     /* We use the two triangles in the parallelogram vb, vb+e5, vb+e0, vb+e1.
      * We go round each triangle clockwise (although our surface is non-
      * orientable so it shouldn't matter).  Picking the parallelogram
@@ -95,7 +94,7 @@ static void generate_display_list(void) {
 
   if (!vertex_in_triangles_checked) {
     int v, expd;
-    FOR_VERTEX(v) {
+    FOR_VERTEX(v,INNER) {
       expd= RIM_VERTEX_P(v) ? 3 : 6;
       if (vertex_in_triangles[v] != expd) {
        fprintf(stderr,"vertex %02x used for %d triangles, expected %d\n",
@@ -173,7 +172,7 @@ static void drawtriangle(const Triangle *t) {
 
 static const unsigned long core_event_mask=
   ButtonPressMask|ButtonReleaseMask|StructureNotifyMask|ButtonMotionMask|
-  KeyPressMask;
+  KeyPressMask|SubstructureNotifyMask;
 
 static void mkpixmaps(void) {
   for (currentbuffer=0; currentbuffer<2; currentbuffer++) {
@@ -338,7 +337,7 @@ static void make_z_rotation(double rotz[D3][D3], double cz, double sz) {
   rotz[0][0]=  cz;    rotz[0][1]=  sz;     rotz[0][2]=  0;
   rotz[1][0]= -sz;    rotz[1][1]=  cz;     rotz[1][2]=  0;
   rotz[2][0]=   0;    rotz[2][1]=   0;     rotz[2][2]=  1;
-}  
+}
 
 static void drag_rotate_delta(double dx, double dy) {
   /* We multiple our transformation matrix by a matrix:
@@ -541,10 +540,38 @@ static void event_motion(int x, int y) {
   drag_position(x,y);
 }
 
+static void transform_preset_record(const char *fn, const char *fn_new) {
+  FILE *f;
+  f= fopen(fn_new,"wb");
+  if (!f) diee("open new transform");
+  if (fwrite(transform,sizeof(transform),1,f) != 1) diee("write transform");
+  if (fclose(f)) diee("fclose new transform");
+  if (rename(fn_new,fn)) diee("install transform");
+}
+
+static void transform_preset_playback(const char *fn) {
+  FILE *f;
+  f= fopen(fn,"rb");
+  if (!f && errno==ENOENT) {
+    fprintf(stderr,"no preset %s\n",fn);
+    XBell(display,100);
+    return;
+  }
+  errno= 0;
+  if (fread(transform,sizeof(transform),1,f) != 1) {
+    perror("read preset!");
+    XBell(display,100);
+    return;
+  }
+  fclose(f);
+  show();
+}
+
 static void event_key(XKeyEvent *e) {
   KeySym ks;
-  char buf[10];
-  int r;
+  XKeyEvent e_nomod;
+  char buf[10], buf_nomod[10];
+  int r, r_nomod;
 
   r= XLookupString(e,buf,sizeof(buf)-1,&ks,0);
   if (!r) {
@@ -553,17 +580,43 @@ static void event_key(XKeyEvent *e) {
     return;
   }
 
-  if (!strcmp(buf,"q")) exit(0);
-  if (!strcmp(buf,"w")) {
+  if (!strcmp(buf,"q"))
+    exit(0);
+  else if (!strcmp(buf,"p"))
+    pause_updates= !pause_updates;
+  else if (!strcmp(buf,"w")) {
     wireframe= !wireframe;
     show();
     return;
-  }
-  if (!strcmp(buf,"d")) {
+  } else if (!strcmp(buf,"d")) {
     eyes_apart= eyes_apart>0 ? eyes_apart_min : eyes_apart_preferred;
     show();
     return;
   }
+
+  e_nomod= *e;
+  e_nomod.state= 0;
+  buf_nomod[0]= 0;
+  r_nomod= XLookupString(&e_nomod,buf_nomod,sizeof(buf_nomod)-1,&ks,0);
+  if (r_nomod && !buf_nomod[1] && buf_nomod[0]>='0' && buf_nomod[0]<='9') {
+    char filename[20], filename_new[25];
+    snprintf(filename,sizeof(filename)-1,".view-preset-%s",buf_nomod);
+    snprintf(filename_new,sizeof(filename_new)-1,"%s.new",filename);
+    printf("transform preset %d %s\n", e->state, filename);
+    if (e->state) transform_preset_record(filename,filename_new);
+    else transform_preset_playback(filename);
+    return;
+  }
+
+  printf("unknown key keycode=%d state=0x%x char=%c 0x%02x "
+        "[rnm=%d bnm[0,1]=0x%02x,%02x]\n",
+        e->keycode, e->state, buf[0]>' ' && buf[0]<127 ? buf[0] : '?',
+        buf[0], r_nomod, buf_nomod[0], buf_nomod[1]);
+  printf("%d %d %d %d\n",
+        r_nomod,
+        !buf_nomod[1],
+        buf_nomod[0]>='0',
+        buf_nomod[0]<='9');
 }
 
 static void event_config(XConfigureEvent *e) {
@@ -603,7 +656,7 @@ static void check_input(void) {
 
 static void topocheck(void) {
   int v1,e,v2,eprime,v1prime, count;
-  FOR_EDGE(v1,e,v2) {
+  FOR_EDGE(v1,e,v2, INNER) {
     count= 0;
     FOR_VEDGE(v2,eprime,v1prime)
       if (v1prime==v1) count++;
@@ -626,6 +679,7 @@ int main(int argc, const char *const *argv) {
   struct pollfd *polls=0;
   int motion_deferred=0, motion_x=-1, motion_y=-1;
 
+  mgraph_prepare();
   topocheck();
   if (argc==1) { printf("topology self-consistent, ok\n"); exit(0); }
   
@@ -658,8 +712,11 @@ int main(int argc, const char *const *argv) {
     polls[i].fd= ConnectionNumber(display);
     polls[i].events= wantedevents;
 
-    r= poll(polls, nxfds+1, motion_deferred ? 0 : 200);
-    if (r<0) diee("poll");
+    r= poll(polls, nxfds+1, motion_deferred ? 0 : pause_updates ? -1 : 200);
+    if (r<0) {
+      if (errno==EINTR) continue;
+      diee("poll");
+    }
 
     for (i=0; i<nxfds; i++)
       if (polls[i].revents)
@@ -671,7 +728,8 @@ int main(int argc, const char *const *argv) {
        event_motion(motion_x, motion_y);
        motion_deferred=0;
       }
-      check_input();
+      if (!pause_updates)
+       check_input();
       continue;
     }