Add a mechanism for collecting entropy, and displaying how much we've got,
authorben <ben@cda61777-01e9-0310-a592-d414129be87e>
Thu, 20 Feb 2003 22:55:09 +0000 (22:55 +0000)
committerben <ben@cda61777-01e9-0310-a592-d414129be87e>
Thu, 20 Feb 2003 22:55:09 +0000 (22:55 +0000)
based on the Windows version.  We don't _do_ anything with the entropy yet,
though.

git-svn-id: svn://svn.tartarus.org/sgt/putty@2875 cda61777-01e9-0310-a592-d414129be87e

mac/macpgen.c
mac/macpgen.r
mac/macpgkey.c
mac/macpgrid.h

index 2b1a3a7..40827d2 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macpgen.c,v 1.3 2003/02/16 14:27:37 ben Exp $ */
+/* $Id: macpgen.c,v 1.4 2003/02/20 22:55:09 ben Exp $ */
 /*
  * Copyright (c) 1999, 2003 Ben Harris
  * All rights reserved.
@@ -204,12 +204,33 @@ static void mac_eventloop(void) {
     Boolean gotevent;
     EventRecord event;
     RgnHandle cursrgn;
+    Point mousenow, mousethen;
+    KeyState *ks;
+    WindowPtr front;
 
     cursrgn = NewRgn();
+    GetMouse(&mousethen);
     for (;;) {
        mac_adjustcursor(cursrgn);
        gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
        mac_adjustcursor(cursrgn);
+       front = mac_frontwindow();
+       if (front != NULL) {
+           ks = mac_windowkey(front);
+           if (ks->collecting_entropy) {
+               GetMouse(&mousenow);
+               if (mousenow.h != mousethen.h || mousenow.v != mousethen.v) {
+                   ks->entropy[ks->entropy_got++] = *(unsigned *)&mousenow;
+                   ks->entropy[ks->entropy_got++] = TickCount();
+                   if (ks->entropy_got >= ks->entropy_required)
+                       ks->collecting_entropy = 0;
+                   SetControlValue(ks->progress, ks->entropy_got);
+                   mousethen = mousenow;
+               }
+               SetEmptyRgn(cursrgn);
+           }
+       }
+           
        if (gotevent)
            mac_event(&event);
        if (borednow)
index 036b14f..7bc8c29 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macpgen.r,v 1.3 2003/02/16 14:27:37 ben Exp $ */
+/* $Id: macpgen.r,v 1.4 2003/02/20 22:55:09 ben Exp $ */
 /*
  * Copyright (c) 1999, 2002 Ben Harris
  * All rights reserved.
@@ -460,10 +460,19 @@ resource 'dlgx' (wKey, "key", purgeable) {
     }
 };
 
+#define cProgress 129
+
 resource 'DITL' (wKey, "key", purgeable) {
     {
        { 13, 13, 33, 227 },
        Button { enabled, "Generate" },
+       { 46, 13, 12, 227 },
+       Control { enabled, cProgress },
     }
 };
 
+resource 'CNTL' (cProgress) {
+    { 46, 13, 12, 227 },
+    0, visible, 0, 0,
+    kControlProgressBarProc, 0, ""
+};
\ No newline at end of file
index c65d939..a174067 100644 (file)
@@ -1,6 +1,7 @@
-/* $Id: macpgkey.c,v 1.2 2003/02/16 14:27:37 ben Exp $ */
+/* $Id: macpgkey.c,v 1.3 2003/02/20 22:55:09 ben Exp $ */
 /*
  * Copyright (c) 2003 Ben Harris
+ * Copyright (c) 1997-2003 Simon Tatham
  * All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person
 /* Stuff to handle the key window in PuTTYgen */
 
 #include <MacTypes.h>
+#include <Controls.h>
 #include <Dialogs.h>
 #include <MacWindows.h>
 
 #include "putty.h"
 #include "mac.h"
 #include "macpgrid.h"
+#include "ssh.h"
+
+/* ----------------------------------------------------------------------
+ * Progress report code. This is really horrible :-)
+ */
+#define PROGRESSRANGE 65535
+#define MAXPHASE 5
+struct progress {
+    int nphases;
+    struct {
+       int exponential;
+       unsigned startpoint, total;
+       unsigned param, current, n;    /* if exponential */
+       unsigned mult;                 /* if linear */
+    } phases[MAXPHASE];
+    unsigned total, divisor, range;
+    ControlHandle progbar;
+};
+
+static void progress_update(void *param, int action, int phase, int iprogress)
+{
+    struct progress *p = (struct progress *) param;
+    unsigned progress = iprogress;
+    int position;
+
+    if (action < PROGFN_READY && p->nphases < phase)
+       p->nphases = phase;
+    switch (action) {
+      case PROGFN_INITIALISE:
+       p->nphases = 0;
+       break;
+      case PROGFN_LIN_PHASE:
+       p->phases[phase-1].exponential = 0;
+       p->phases[phase-1].mult = p->phases[phase].total / progress;
+       break;
+      case PROGFN_EXP_PHASE:
+       p->phases[phase-1].exponential = 1;
+       p->phases[phase-1].param = 0x10000 + progress;
+       p->phases[phase-1].current = p->phases[phase-1].total;
+       p->phases[phase-1].n = 0;
+       break;
+      case PROGFN_PHASE_EXTENT:
+       p->phases[phase-1].total = progress;
+       break;
+      case PROGFN_READY:
+       {
+           unsigned total = 0;
+           int i;
+           for (i = 0; i < p->nphases; i++) {
+               p->phases[i].startpoint = total;
+               total += p->phases[i].total;
+           }
+           p->total = total;
+           p->divisor = ((p->total + PROGRESSRANGE - 1) / PROGRESSRANGE);
+           p->range = p->total / p->divisor;
+           SetControlMaximum(p->progbar, p->range);
+       }
+       break;
+      case PROGFN_PROGRESS:
+       if (p->phases[phase-1].exponential) {
+           while (p->phases[phase-1].n < progress) {
+               p->phases[phase-1].n++;
+               p->phases[phase-1].current *= p->phases[phase-1].param;
+               p->phases[phase-1].current /= 0x10000;
+           }
+           position = (p->phases[phase-1].startpoint +
+                       p->phases[phase-1].total - p->phases[phase-1].current);
+       } else {
+           position = (p->phases[phase-1].startpoint +
+                       progress * p->phases[phase-1].mult);
+       }
+       SetControlValue(p->progbar, position / p->divisor);
+       break;
+    }
+}
 
 static void mac_clickkey(WindowPtr window, EventRecord *event)
 {
     short item;
     DialogRef dialog;
+    KeyState *ks = mac_windowkey(window);
 
     dialog = GetDialogFromWindow(window);
     if (DialogSelect(event, &dialog, &item))
        switch (item) {
          case wiKeyGenerate:
+           SetControlMaximum(ks->progress, 1024);
+           ks->entropy = smalloc(1024 * sizeof(*ks->entropy));
+           ks->entropy_required = 1024;
+           ks->entropy_got = 0;
+           ks->collecting_entropy = TRUE;
            /* Do something */
            break;
        }
@@ -87,9 +170,14 @@ void mac_newkey(void)
 {
     KeyState *ks;
     WinInfo *wi;
+    Handle h;
+    short type;
+    Rect rect;
 
     ks = smalloc(sizeof(*ks));
     ks->box = GetNewDialog(wKey, NULL, (WindowPtr)-1);
+    GetDialogItem(ks->box, wiKeyProgress, &type, &h, &rect);
+    ks->progress = (ControlHandle)h;
     wi = smalloc(sizeof(*wi));
     memset(wi, 0, sizeof(*wi));
     wi->ks = ks;
index bc44def..438f3cb 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macpgrid.h,v 1.3 2003/02/16 14:27:37 ben Exp $ */
+/* $Id: macpgrid.h,v 1.4 2003/02/20 22:55:09 ben Exp $ */
 
 /*
  * macpgrid.h -- Mac resource IDs for PuTTYgen
@@ -44,3 +44,5 @@
 #define wLicence       131
 #define wKey           134
 #define wiKeyGenerate          1
+#define wiKeyProgress          2
+