chiark / gitweb /
another rune
[moebius2.git] / view.c
diff --git a/view.c b/view.c
index cc86803594841154686a48adb5c77aff1bb5b316..32ae7d7df8a8a39ecd9ad0785f0d2870bed54c89 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"
@@ -339,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:
@@ -542,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) {
@@ -566,11 +592,31 @@ static void event_key(XKeyEvent *e) {
     eyes_apart= eyes_apart>0 ? eyes_apart_min : eyes_apart_preferred;
     show();
     return;
-  } else {
-    printf("unknown key keycode=%d state=0x%x char=%c 0x%02x\n",
-          e->keycode, e->state, buf[0]>' ' && buf[0]<127 ? buf[0] : '?',
-          buf[0]);
   }
+
+  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) {