hvmain.cpp 7.94 KB
Newer Older
1 2
//
// Copyright (c) 2004 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 
// {{{EMULAB-LICENSE
// 
// This file is part of the Emulab network testbed software.
// 
// This file is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
// 
// This file is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
// License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with this file.  If not, see <http://www.gnu.org/licenses/>.
// 
// }}}
Russ Fish's avatar
Russ Fish committed
22
//
Mike Hibler's avatar
Mike Hibler committed
23 24 25

// hvmain.cpp - hypview for SWIG into _hv.so, based on hypviewer/examples/glut/main.cpp .

26 27
#include <stdio.h>
#include <memory>
28
#include <string>
Russ Fish's avatar
Russ Fish committed
29
#include <iostream>
30 31
NAMESPACEHACK

32
#ifndef WIN32
Russ Fish's avatar
Russ Fish committed
33 34 35 36
#include "wx/setup.h"
#include "wx/glcanvas.h"
#endif

37 38
extern "C" {
#include <stdlib.h>
Russ Fish's avatar
Russ Fish committed
39

40 41 42
#if 0
#include <GL/glut.h>
#endif
Russ Fish's avatar
Russ Fish committed
43

44 45 46
#ifndef WIN32
#include <ieeefp.h>
#endif
Russ Fish's avatar
Russ Fish committed
47

48 49 50 51 52
#ifdef HYPFREE
#include <floatingpoint.h>
#endif
#include <errno.h>
}
Russ Fish's avatar
Russ Fish committed
53

54 55 56 57 58 59 60 61 62 63
#ifdef HYPIRIX
#include <fstream.h>
#else
#include <fstream>
#endif

#include "HypView.h"
#include "HypData.h"
#include "HypNode.h"

64 65
//char prevsel[1024];
string prevsel;
66
#ifndef WIN32
Russ Fish's avatar
Russ Fish committed
67 68
auto_ptr<HypView> hv;  // We own this; make sure the pointed-to HypView will be deleted.
wxGLCanvas *glcanvas;  // We don't own this, so don't wrap it in an auto_ptr.
69 70 71
#else
HypView *hv = NULL;    // VC++6 doesn't support the reset operation to change an auto_ptr.
#endif
72

73 74
char const *getSelected(){
  return prevsel.c_str();
75 76
}

77
// I guess I don't know how to handle a "string" returned by the SWIG Python wrapper...
78 79
char * getGraphCenter(){
  return const_cast<char *>(hv->getGraphCenter().c_str());
80 81
}

82
void selectCB(const string & id, int shift, int control) {
83
  //printf("selectCB id=%s, prevsel=%s\n", id.c_str(), prevsel.c_str());
84
  //cerr << "selectCB id=" << id.c_str() << ", prevsel=" << prevsel << endl;
85 86 87 88 89
  hv->setSelected(id, 1);
  hv->setSelected(prevsel, 0);
  hv->gotoNode(id, HV_ANIMATE);
  hv->drawFrame();
  hv->idle(1);
90
  prevsel = id;
91 92
}

Russ Fish's avatar
Russ Fish committed
93
void frameEndCB(int) {
94
#ifndef WIN32
Russ Fish's avatar
Russ Fish committed
95 96
  //printf("frameEndCB glcanvas=0x%x\n", glcanvas);
  glcanvas->SwapBuffers();
97
#endif
Russ Fish's avatar
Russ Fish committed
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void display() { hv->drawFrame(); }
void mouse(int b, int s, int x, int y) { hv->mouse(b,s,x,y,0,0); }
void motion(int x, int y) { hv->motion(x,y,0,0); }
void passive(int x, int y) { hv->passive(x,y,0,0); }
void idle() { hv->idle(); }
void reshape(int w, int h) { hv->reshape(w,h); }
#if 0
void visibility(int status) {
  if (status == GLUT_VISIBLE) {
    glutIdleFunc(NULL);
    display();
    glutIdleFunc(idle);
  } else {
    glutIdleFunc(NULL);
  }
}
#endif
#if 0
void keyboard(unsigned char key, int /*x*/, int /*y*/) { 
  static int labels = HV_LABELSHORT;
  static bool sphere = false;
  char buf[128];
  
  switch (key) {
    case 'f':
      cin.getline(buf,128);
      hv->gotoNode(buf, HV_ANIMATE);
      break;
    case 'l':
      if (labels == HV_LABELNONE) labels = HV_LABELSHORT;
      else if (labels == HV_LABELSHORT) labels = HV_LABELLONG;
      else if (labels == HV_LABELLONG) labels = HV_LABELNONE;
      hv->setLabels(labels);
      break;
    case 'r':
      hv->gotoCenterNode(HV_ANIMATE);
      break;
    case 's':
      sphere = !sphere;
      hv->setSphere(sphere);
      break;
    default:
      break;
  }
  display();
  glutIdleFunc(idle);
}
#endif

void PrintAllocations()
{
151 152
//  if (hv)
//    delete(hv);
153
#ifndef WIN32
154
  hv.reset(NULL);
155 156 157
#else
  hv = NULL;
#endif
158 159 160 161 162 163 164 165
  cerr << "HypNode objects not destroyed: " << HypNode::NumObjects() << endl;
  cerr << "HypLink objects not destroyed: " << HypLink::NumObjects() << endl;
  return;
}

#if 0
int main(int argc, char *argv[]) {
#else
166 167

#ifndef WIN32
Russ Fish's avatar
Russ Fish committed
168
HypView *hvMain(int argc, char *argv[], void * window, int width, int height) {
169 170 171 172
#else
HypView *hvMain(int argc, char *argv[], int window, int width, int height) {
#endif

173 174 175 176 177 178 179
#endif
  char *fname;
  if (argc > 1) 
    fname = strdup(argv[1]);
  else 
    fname = strdup("test.lvhist");

Russ Fish's avatar
Russ Fish committed
180
#ifndef WIN32
181
  fpsetmask(0);
Russ Fish's avatar
Russ Fish committed
182
#endif
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

#if 0
  atexit(PrintAllocations);
  
  glutInit(&argc,argv);
  //  glutInitWindowSize(hd->winx, hd->winy);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  ifstream inFile(fname);
  if (!inFile) {
    cerr << "Could not open '" << fname << "': " << strerror(errno) << endl;
    exit(1);
  }
  glutCreateWindow(fname);
#else
  ifstream inFile(fname);
  if (!inFile) {
    cerr << "Could not open '" << fname << "': " << strerror(errno) << endl;
    return 0;
  }
#endif

Russ Fish's avatar
Russ Fish committed
204
#ifndef WIN32
205
  hv.reset(new HypView());
Russ Fish's avatar
Russ Fish committed
206 207 208
#else
  HWND hWnd = (HWND)window;
  HDC hdc = ::GetDC(hWnd);
209
  hv = new HypView(hdc, hWnd);
Russ Fish's avatar
Russ Fish committed
210 211 212 213 214 215
  HGLRC ctx = wglCreateContext(hdc);
  if (!ctx) {
    printf("wglCreateContext Failed\n");
    exit(1);
  }
#endif
216 217 218 219 220 221 222 223

  // hv->clearSpanPolicy();
  // hv->addSpanPolicy(HV_SPANBFS);
  
#if 1
  hv->setGraph(inFile);
  inFile.close();
#endif
Russ Fish's avatar
Russ Fish committed
224
#ifndef WIN32
Russ Fish's avatar
Russ Fish committed
225 226 227 228 229 230 231
  // OK, this is nuts...  The canvas pointer comes through byte-swapped and shifted 4 bits!
  int canvas; char *w = (char *)&window, *c = (char *)&canvas; int *ic = (int *)&canvas;
  c[0] = w[3]; c[3] = w[0]; c[1] = w[2]; c[2] = w[1]; *ic = (*ic >> 4) & 0xfffffff;
  glcanvas = (wxGLCanvas *)canvas;
  //printf("hvmain window=0x%x, canvas=0x%x, width=%d, height=%d\n", window, canvas, width, height);

  hv->setFrameEndCallback(frameEndCB);
232
  hv->afterRealize();
Russ Fish's avatar
Russ Fish committed
233
#else
234
  //printf("hvmain ctx=0x%x, width=%d, height=%d\n", width, height);
Russ Fish's avatar
Russ Fish committed
235 236
  hv->afterRealize(ctx, width, height);
#endif
237
  hv->setSphere(1);
Russ Fish's avatar
Russ Fish committed
238
  hv->setColorSphere(.3f,.3f,.3f);
239
  hv->setColorBackground(1,1,1);
Russ Fish's avatar
Russ Fish committed
240
#ifndef WIN32
241
  hv->setLabelFont("-adobe-helvetica-medium-r-normal--12-120-75-75-p-67-iso8859-1");
Russ Fish's avatar
Russ Fish committed
242
#endif
243 244 245 246
  hv->setColorLabel(0,0,0);
  hv->setLabels(HV_LABELLONG);
  hv->setMotionCull(0);
  hv->setPassiveCull(2);
Russ Fish's avatar
Russ Fish committed
247
  hv->setGotoStepSize(.0833f);
248 249 250 251 252 253 254 255 256
  hv->setKeepAspect(1);
  hv->setCenterShow(0);

  // mime types

  hv->setGroupKey(0); // start out coloring by mime types

  hv->setColorGroup(0, "image", 1.0, 0.0, 1.0);
  hv->setColorGroup(0, "html", 0, 1, 1);
Russ Fish's avatar
Russ Fish committed
257 258 259 260 261 262 263 264
  hv->setColorGroup(0, "text", .90f, .35f, .05f);
  hv->setColorGroup(0, "image", .42f, 0, .48f);
  hv->setColorGroup(0, "application", .99f, .64f, .25f);
  hv->setColorGroup(0, "audio", .91f, .36f, .57f);
  hv->setColorGroup(0, "host", .375f, .75f, .375f);
  hv->setColorGroup(0, "lan", .375f, .375f, .75f);
  hv->setColorGroup(0, "source", .9f, .9f, .9f);
  hv->setColorGroup(0, "vrml", .09f, 0, 1);
265 266 267 268 269 270 271
  hv->setColorGroup(0, "invisible", 0, 0, 0);

  // tree position
  hv->setColorGroup(1, "host", 1.0, 1.0, 1.0);
  hv->setColorGroup(1, "mainbranch", 0, 1, 1);
  hv->setColorGroup(1, "orphan", 1.0, 0.0, 1.0);

Russ Fish's avatar
Russ Fish committed
272 273 274 275 276 277
  //  hv->gotoCenterNode(HV_JUMP);
  struct timeval idletime;
  idletime.tv_sec = 5;
  idletime.tv_usec = 0;
  hv->setIdleFrameTime(idletime);

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#if 0
  glutDisplayFunc(display);
#endif
  hv->bindCallback(HV_LEFT_CLICK, HV_PICK);
  hv->bindCallback(HV_PASSIVE, HV_HILITE);
  hv->bindCallback(HV_LEFT_DRAG, HV_TRANS);
  hv->bindCallback(HV_MIDDLE_DRAG, HV_ROT);
  hv->setPickCallback(selectCB);
#if 0
  glutKeyboardFunc(keyboard);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutPassiveMotionFunc(passive);
  glutVisibilityFunc(visibility);
  glutReshapeFunc(reshape);
  glutMainLoop();

295
//  delete(hv);
296
  PrintAllocations();
297 298

  return 0;
299 300 301
  
  // free(fname);
#else
302 303

#ifndef WIN32
304
  return hv.get();
305 306 307 308
#else
  return hv;
#endif

309 310 311
#endif
}

Russ Fish's avatar
Russ Fish committed
312
void hvKill(HypView * /*hv*/) {
313
#ifndef WIN32
Russ Fish's avatar
Russ Fish committed
314
  delete hv.get();
315 316 317
#else
  delete hv;
#endif
Russ Fish's avatar
Russ Fish committed
318
}
319

Russ Fish's avatar
Russ Fish committed
320
int hvReadFile(char *fname, int width, int height) {
321 322 323 324 325 326 327 328
  ifstream inFile(fname);
  if (!inFile) {
    cerr << "Could not open '" << fname << "': " << strerror(errno) << endl;
    return 1;
  }

  hv->setGraph(inFile);
  inFile.close();
Russ Fish's avatar
Russ Fish committed
329
#ifndef WIN32
330
  hv->afterRealize();
Russ Fish's avatar
Russ Fish committed
331 332 333 334 335
#else
  HDC hdc = hv->getWidget();
  HGLRC ctx = wglCreateContext(hdc);
  hv->afterRealize(ctx, width, height);
#endif
336 337
  return 0;
}