chiark / gitweb /
minimisation works but initial solution is bust
[moebius2.git] / view.c
diff --git a/view.c b/view.c
index 1ce6db66d492867a869ed833b2a9e9c6931067a8..abecc7ca4d5fc206532f00d3f7c33dfe41281b78 100644 (file)
--- a/view.c
+++ b/view.c
@@ -5,6 +5,10 @@
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/poll.h>
+
 #include "mgraph.h"
 
 #define MAXTRIS (N*2)
@@ -18,16 +22,21 @@ static Vertices conformation;
 static double transform[D3][D3]= {{1,0,0}, {0,1,0}, {0,0,1}};
 static GSL_MATRIX(transform);
 
-const char *input_filename;
+static FILE *input_f;
+static struct stat input_stab;
+static const char *input_filename;
 
 static void read_input(void) {
-  FILE *f;
   int r;
   
-  f= fopen(input_filename, "rb");  if (!f) diee("input file");
+  if (input_f) fclose(input_f);
+  input_f= fopen(input_filename, "rb");  if (!input_f) diee("input file");
+
+  if (fstat(fileno(input_f), &input_stab)) diee("fstat input file");
+
   errno= 0;
-  r= fread(&conformation,sizeof(conformation),1,f);  if (r!=1) diee("fread");
-  fclose(f);
+  r= fread(&conformation,sizeof(conformation),1,input_f);
+  if (r!=1) diee("fread");
 }
 
 static void transform_coordinates(void) {
@@ -468,9 +477,31 @@ static void event_config(XConfigureEvent *e) {
   show();
 }
 
+static void check_input(void) {
+  struct stat newstab;
+  int r;
+
+  r= stat(input_filename, &newstab);
+  if (r<0) diee("could not check input");
+
+#define CI(x) if (newstab.st_##x == input_stab.st_##x) ; else goto changed
+  CI(dev);
+  CI(ino);
+  CI(size);
+  CI(mtime);
+#undef CI
+  return;
+
+ changed:
+  show();
+}
+
 int main(int argc, const char *const *argv) {
+  static const int wantedevents= POLLIN|POLLPRI|POLLERR|POLLHUP;
+
   XEvent event;
-  int k;
+  int k, i, r, *xfds, nxfds, polls_alloc=0;
+  struct pollfd *polls=0;
   int motion_deferred=0, motion_x=-1, motion_y=-1;
   
   if (argc != 2 || argv[1][0]=='-') {
@@ -485,16 +516,40 @@ int main(int argc, const char *const *argv) {
 
   XMapWindow(display,window);
   for (;;) {
-    if (motion_deferred) {
-      int r= XCheckMaskEvent(display,~0UL,&event);
-      if (!r) {
+
+    XA( XInternalConnectionNumbers(display, &xfds, &nxfds) );
+    if (polls_alloc <= nxfds) {
+      polls_alloc= nxfds + polls_alloc + 1;
+      polls= realloc(polls, sizeof(*polls) * polls_alloc);
+      if (!polls) diee("realloc for pollfds");
+    }
+    for (i=0; i<nxfds; i++) {
+      polls[i].fd= xfds[i];
+      polls[i].events= wantedevents;
+      polls[i].revents= 0;
+    }
+    XFree(xfds);
+
+    polls[i].fd= ConnectionNumber(display);
+    polls[i].events= wantedevents;
+
+    r= poll(polls, nxfds+1, motion_deferred ? 0 : 200);
+    if (r<0) diee("poll");
+
+    for (i=0; i<nxfds; i++)
+      if (polls[i].revents)
+       XProcessInternalConnection(display, polls[i].fd);
+
+    r= XCheckMaskEvent(display,~0UL,&event);
+    if (!r) {
+      if (motion_deferred) {
        event_motion(motion_x, motion_y);
        motion_deferred=0;
-       continue;
       }
-    } else {
-      XNextEvent(display,&event);
+      check_input();
+      continue;
     }
+    
     switch (event.type) {
 
     case ButtonPress: