| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #include <unistd.h> |
|---|
| 10 | #include <stdlib.h> |
|---|
| 11 | #include <sys/time.h> |
|---|
| 12 | #include "general.h" |
|---|
| 13 | #include "xosview.h" |
|---|
| 14 | #include "meter.h" |
|---|
| 15 | #include "MeterMaker.h" |
|---|
| 16 | #if (defined(XOSVIEW_NETBSD) || defined(XOSVIEW_FREEBSD) || defined(XOSVIEW_OPENBSD)) |
|---|
| 17 | #include "kernel.h" |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | static const char * const versionString = "gr_osview version: " XOSVIEW_VERSION; |
|---|
| 21 | |
|---|
| 22 | static const char NAME[] = "gr_osview@"; |
|---|
| 23 | |
|---|
| 24 | #if !defined(__GNUC__) |
|---|
| 25 | |
|---|
| 26 | #define MIN(x,y) \ |
|---|
| 27 | ( \ |
|---|
| 28 | x < y ? x : y \ |
|---|
| 29 | ) |
|---|
| 30 | |
|---|
| 31 | #define MAX(x,y) \ |
|---|
| 32 | ( \ |
|---|
| 33 | x > y ? x : y \ |
|---|
| 34 | ) |
|---|
| 35 | |
|---|
| 36 | #else |
|---|
| 37 | |
|---|
| 38 | #define MIN(x,y) \ |
|---|
| 39 | ({ \ |
|---|
| 40 | const typeof(x) _x = x; \ |
|---|
| 41 | const typeof(y) _y = y; \ |
|---|
| 42 | \ |
|---|
| 43 | (void) (&_x == &_y); \ |
|---|
| 44 | \ |
|---|
| 45 | _x < _y ? _x : _y; \ |
|---|
| 46 | }) |
|---|
| 47 | |
|---|
| 48 | #define MAX(x,y) \ |
|---|
| 49 | ({ \ |
|---|
| 50 | const typeof(x) _x = x; \ |
|---|
| 51 | const typeof(y) _y = y; \ |
|---|
| 52 | \ |
|---|
| 53 | (void) (&_x == &_y); \ |
|---|
| 54 | \ |
|---|
| 55 | _x > _y ? _x : _y; \ |
|---|
| 56 | }) |
|---|
| 57 | |
|---|
| 58 | #endif // sgi |
|---|
| 59 | |
|---|
| 60 | double MAX_SAMPLES_PER_SECOND = 10; |
|---|
| 61 | |
|---|
| 62 | CVSID("$Id: xosview.cc,v 1.1.1.1 2008/05/04 15:53:48 emasson Exp $"); |
|---|
| 63 | CVSID_DOT_H(XOSVIEW_H_CVSID); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | XOSView::XOSView( char * instName, int argc, char *argv[] ) : XWin(), |
|---|
| 67 | xrm(Xrm("xosview", instName)){ |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | checkVersion(argc, argv); |
|---|
| 72 | |
|---|
| 73 | setDisplayName (xrm.getDisplayName( argc, argv)); |
|---|
| 74 | openDisplay(); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | xrm.loadAndMergeResources (argc, argv, display_); |
|---|
| 79 | XWinInit (argc, argv, NULL, &xrm); |
|---|
| 80 | #if 1 // Don't enable this yet. |
|---|
| 81 | MAX_SAMPLES_PER_SECOND = atof(getResource("samplesPerSec")); |
|---|
| 82 | if (!MAX_SAMPLES_PER_SECOND) |
|---|
| 83 | MAX_SAMPLES_PER_SECOND = 10; |
|---|
| 84 | #endif |
|---|
| 85 | usleeptime_ = (unsigned long) (1000000/MAX_SAMPLES_PER_SECOND); |
|---|
| 86 | if (usleeptime_ >= 1000000) { |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | sleeptime_ = usleeptime_ / 1000000; |
|---|
| 90 | usleeptime_ = usleeptime_ % 1000000; |
|---|
| 91 | } else { sleeptime_ = 0; } |
|---|
| 92 | #if (defined(XOSVIEW_NETBSD) || defined(XOSVIEW_FREEBSD) || defined(XOSVIEW_OPENBSD)) |
|---|
| 93 | BSDInit(); |
|---|
| 94 | #endif |
|---|
| 95 | |
|---|
| 96 | hmargin_ = atoi(getResource("horizontalMargin")); |
|---|
| 97 | vmargin_ = atoi(getResource("verticalMargin")); |
|---|
| 98 | vspacing_ = atoi(getResource("verticalSpacing")); |
|---|
| 99 | hmargin_ = MAX(0, hmargin_); |
|---|
| 100 | vmargin_ = MAX(0, vmargin_); |
|---|
| 101 | vspacing_ = MAX(0, vspacing_); |
|---|
| 102 | |
|---|
| 103 | checkArgs (argc, argv); |
|---|
| 104 | xoff_ = hmargin_; |
|---|
| 105 | yoff_ = 0; |
|---|
| 106 | nummeters_ = 0; |
|---|
| 107 | meters_ = NULL; |
|---|
| 108 | name_ = "xosview"; |
|---|
| 109 | _isvisible = false; |
|---|
| 110 | _ispartiallyvisible = false; |
|---|
| 111 | exposed_once_flag_ = 0; |
|---|
| 112 | |
|---|
| 113 | expose_flag_ = 1; |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | addEvent( new Event( this, ConfigureNotify, |
|---|
| 117 | (EventCallBack)&XOSView::resizeEvent ) ); |
|---|
| 118 | addEvent( new Event( this, Expose, |
|---|
| 119 | (EventCallBack)&XOSView::exposeEvent ) ); |
|---|
| 120 | addEvent( new Event( this, KeyPress, |
|---|
| 121 | (EventCallBack)&XOSView::keyPressEvent ) ); |
|---|
| 122 | addEvent( new Event( this, VisibilityNotify, |
|---|
| 123 | (EventCallBack)&XOSView::visibilityEvent ) ); |
|---|
| 124 | addEvent( new Event( this, UnmapNotify, |
|---|
| 125 | (EventCallBack)&XOSView::unmapEvent ) ); |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | MeterMaker mm(this); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | checkOverallResources (); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | mm.makeMeters(); |
|---|
| 135 | for (int i = 1 ; i <= mm.n() ; i++) |
|---|
| 136 | addmeter(mm[i]); |
|---|
| 137 | |
|---|
| 138 | if (nummeters_ == 0) |
|---|
| 139 | { |
|---|
| 140 | fprintf (stderr, "No meters were enabled! Exiting...\n"); |
|---|
| 141 | exit (0); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | checkMeterResources(); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | figureSize(); |
|---|
| 149 | init( argc, argv ); |
|---|
| 150 | title( winname() ); |
|---|
| 151 | iconname( winname() ); |
|---|
| 152 | dolegends(); |
|---|
| 153 | resize(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | void XOSView::checkVersion(int argc, char *argv[]) const |
|---|
| 157 | { |
|---|
| 158 | for (int i = 0 ; i < argc ; i++) |
|---|
| 159 | if (!strncasecmp(argv[i], "-v", 2) |
|---|
| 160 | || !strncasecmp(argv[i], "--version", 10)) |
|---|
| 161 | { |
|---|
| 162 | std::cerr << versionString << std::endl; |
|---|
| 163 | exit(0); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void XOSView::figureSize ( void ) { |
|---|
| 168 | if ( legend_ ){ |
|---|
| 169 | if ( !usedlabels_ ) |
|---|
| 170 | xoff_ = textWidth( "XXXXX" ); |
|---|
| 171 | else |
|---|
| 172 | xoff_ = textWidth( "XXXXXXXXX" ); |
|---|
| 173 | |
|---|
| 174 | yoff_ = caption_ ? textHeight() + textHeight() / 4 : 0; |
|---|
| 175 | } |
|---|
| 176 | static int firsttime = 1; |
|---|
| 177 | if (firsttime) { |
|---|
| 178 | firsttime = 0; |
|---|
| 179 | width_ = findx(); |
|---|
| 180 | height_ = findy(); |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | } |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | void XOSView::checkMeterResources( void ){ |
|---|
| 188 | MeterNode *tmp = meters_; |
|---|
| 189 | |
|---|
| 190 | while ( tmp != NULL ){ |
|---|
| 191 | tmp->meter_->checkResources(); |
|---|
| 192 | tmp = tmp->next_; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | int XOSView::newypos( void ){ |
|---|
| 197 | return 15 + 25 * nummeters_; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | void XOSView::dolegends( void ){ |
|---|
| 201 | MeterNode *tmp = meters_; |
|---|
| 202 | while ( tmp != NULL ){ |
|---|
| 203 | tmp->meter_->docaptions( caption_ ); |
|---|
| 204 | tmp->meter_->dolegends( legend_ ); |
|---|
| 205 | tmp->meter_->dousedlegends( usedlabels_ ); |
|---|
| 206 | tmp = tmp->next_; |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | void XOSView::addmeter( Meter *fm ){ |
|---|
| 211 | MeterNode *tmp = meters_; |
|---|
| 212 | |
|---|
| 213 | if ( meters_ == NULL ) |
|---|
| 214 | meters_ = new MeterNode( fm ); |
|---|
| 215 | else { |
|---|
| 216 | while ( tmp->next_ != NULL ) |
|---|
| 217 | tmp = tmp->next_; |
|---|
| 218 | tmp->next_ = new MeterNode( fm ); |
|---|
| 219 | } |
|---|
| 220 | nummeters_++; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | int XOSView::findx( void ){ |
|---|
| 224 | if ( legend_ ){ |
|---|
| 225 | if ( !usedlabels_ ) |
|---|
| 226 | return textWidth( "XXXXXXXXXXXXXXXXXXXXXXXX" ); |
|---|
| 227 | else |
|---|
| 228 | return textWidth( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ); |
|---|
| 229 | } |
|---|
| 230 | return 80; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | int XOSView::findy( void ){ |
|---|
| 234 | if ( legend_ ) |
|---|
| 235 | return 10 + textHeight() * nummeters_ * ( caption_ ? 2 : 1 ); |
|---|
| 236 | |
|---|
| 237 | return 15 * nummeters_; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | void XOSView::checkOverallResources() { |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | usedlabels_ = legend_ = caption_ = 0; |
|---|
| 246 | |
|---|
| 247 | setFont(); |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | if ( isResourceTrue("captions") ) |
|---|
| 251 | caption_ = 1; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | if ( isResourceTrue("labels") ) |
|---|
| 255 | legend_ = 1; |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | if ( isResourceTrue("usedlabels") ) |
|---|
| 259 | usedlabels_ = 1; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | const char *XOSView::winname( void ){ |
|---|
| 263 | char host[100]; |
|---|
| 264 | gethostname( host, 99 ); |
|---|
| 265 | static char name[101]; |
|---|
| 266 | |
|---|
| 267 | snprintf( name, 100, "%s%s", NAME, host); |
|---|
| 268 | |
|---|
| 269 | return getResourceOrUseDefault ("title", name); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | void XOSView::resize( void ){ |
|---|
| 274 | int spacing = vspacing_+1; |
|---|
| 275 | int topmargin = vmargin_; |
|---|
| 276 | int rightmargin = hmargin_; |
|---|
| 277 | int newwidth = width_ - xoff_ - rightmargin; |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | int newheight = |
|---|
| 283 | (height_ - |
|---|
| 284 | (topmargin + topmargin + (nummeters_-1)*spacing + nummeters_*yoff_) |
|---|
| 285 | ) / nummeters_; |
|---|
| 286 | newheight = (newheight >= 2) ? newheight : 2; |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | int counter = 1; |
|---|
| 290 | MeterNode *tmp = meters_; |
|---|
| 291 | while ( tmp != NULL ) { |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | tmp->meter_->resize( xoff_, |
|---|
| 297 | topmargin + counter*yoff_ + (counter-1)*(newheight+spacing), |
|---|
| 298 | newwidth, newheight ); |
|---|
| 299 | tmp = tmp->next_; |
|---|
| 300 | |
|---|
| 301 | counter++; |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | XOSView::~XOSView( void ){ |
|---|
| 307 | MeterNode *tmp = meters_; |
|---|
| 308 | while ( tmp != NULL ){ |
|---|
| 309 | MeterNode *save = tmp->next_; |
|---|
| 310 | delete tmp->meter_; |
|---|
| 311 | delete tmp; |
|---|
| 312 | tmp = save; |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | void XOSView::reallydraw( void ){ |
|---|
| 317 | XOSDEBUG("Doing draw.\n"); |
|---|
| 318 | clear(); |
|---|
| 319 | MeterNode *tmp = meters_; |
|---|
| 320 | |
|---|
| 321 | while ( tmp != NULL ){ |
|---|
| 322 | tmp->meter_->draw(); |
|---|
| 323 | tmp = tmp->next_; |
|---|
| 324 | } |
|---|
| 325 | flush(); |
|---|
| 326 | |
|---|
| 327 | expose_flag_ = 0; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | void XOSView::draw ( void ) { |
|---|
| 331 | if (hasBeenExposedAtLeastOnce() && isAtLeastPartiallyVisible()) |
|---|
| 332 | reallydraw(); |
|---|
| 333 | else { |
|---|
| 334 | if (!hasBeenExposedAtLeastOnce()) { |
|---|
| 335 | XOSDEBUG("Skipping draw: not yet exposed.\n"); |
|---|
| 336 | } else if (!isAtLeastPartiallyVisible()) { |
|---|
| 337 | XOSDEBUG("Skipping draw: not visible.\n"); |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | void XOSView::keyrelease( char *ch ){ |
|---|
| 342 | |
|---|
| 343 | (void) ch; |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | void XOSView::run( void ){ |
|---|
| 347 | int counter = 0; |
|---|
| 348 | |
|---|
| 349 | while( !done_ ){ |
|---|
| 350 | checkevent(); |
|---|
| 351 | |
|---|
| 352 | if (_isvisible){ |
|---|
| 353 | MeterNode *tmp = meters_; |
|---|
| 354 | while ( tmp != NULL ){ |
|---|
| 355 | if ( tmp->meter_->requestevent() ) |
|---|
| 356 | tmp->meter_->checkevent(); |
|---|
| 357 | tmp = tmp->next_; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | flush(); |
|---|
| 361 | } |
|---|
| 362 | #ifdef HAVE_USLEEP |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | if (sleeptime_) sleep((unsigned int)sleeptime_); |
|---|
| 366 | if (usleeptime_) usleep( (unsigned int)usleeptime_); |
|---|
| 367 | #else |
|---|
| 368 | usleep_via_select ( usleeptime_ ); |
|---|
| 369 | #endif |
|---|
| 370 | counter = (counter + 1) % 5; |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | void XOSView::usleep_via_select( unsigned long usec ){ |
|---|
| 375 | struct timeval time; |
|---|
| 376 | |
|---|
| 377 | time.tv_sec = (int)(usec / 1000000); |
|---|
| 378 | time.tv_usec = usec - time.tv_sec * 1000000; |
|---|
| 379 | |
|---|
| 380 | select( 0, 0, 0, 0, &time ); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | void XOSView::keyPressEvent( XKeyEvent &event ){ |
|---|
| 384 | char c = 0; |
|---|
| 385 | KeySym key; |
|---|
| 386 | |
|---|
| 387 | XLookupString( &event, &c, 1, &key, NULL ); |
|---|
| 388 | |
|---|
| 389 | if ( (c == 'q') || (c == 'Q') ) |
|---|
| 390 | done_ = 1; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | void XOSView::checkArgs (int argc, char** argv) const |
|---|
| 394 | { |
|---|
| 395 | |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | if (argc == 1) return; |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | argc--; |
|---|
| 404 | argv++; |
|---|
| 405 | while (argc > 0 && argv && *argv) |
|---|
| 406 | { |
|---|
| 407 | switch (argv[0][1]) { |
|---|
| 408 | case 'n': |
|---|
| 409 | |
|---|
| 410 | if (!strncasecmp(*argv, "-name", 6)) |
|---|
| 411 | { |
|---|
| 412 | argv++; |
|---|
| 413 | argc--; |
|---|
| 414 | } |
|---|
| 415 | break; |
|---|
| 416 | #if (defined(XOSVIEW_NETBSD) || defined(XOSVIEW_FREEBSD) || defined(XOSVIEW_OPENBSD)) |
|---|
| 417 | case 'N': if (strlen(argv[0]) > 2) |
|---|
| 418 | SetKernelName(argv[0]+2); |
|---|
| 419 | else |
|---|
| 420 | { |
|---|
| 421 | SetKernelName(argv[1]); |
|---|
| 422 | argc--; |
|---|
| 423 | argv++; |
|---|
| 424 | } |
|---|
| 425 | break; |
|---|
| 426 | #endif |
|---|
| 427 | |
|---|
| 428 | default: |
|---|
| 429 | std::cerr << "Ignoring unknown option '" << argv[0] << "'.\n"; |
|---|
| 430 | break; |
|---|
| 431 | } |
|---|
| 432 | argc--; |
|---|
| 433 | argv++; |
|---|
| 434 | } |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | void XOSView::exposeEvent( XExposeEvent &event ) { |
|---|
| 438 | _isvisible = true; |
|---|
| 439 | if ( event.count == 0 ) |
|---|
| 440 | { |
|---|
| 441 | expose_flag_++; |
|---|
| 442 | draw(); |
|---|
| 443 | } |
|---|
| 444 | XOSDEBUG("Got expose event.\n"); |
|---|
| 445 | if (!exposed_once_flag_) { exposed_once_flag_ = 1; draw(); } |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | void XOSView::resizeEvent( XEvent & ) { |
|---|
| 449 | resize(); |
|---|
| 450 | expose_flag_++; |
|---|
| 451 | draw(); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | void XOSView::visibilityEvent( XVisibilityEvent &event ){ |
|---|
| 456 | _ispartiallyvisible = false; |
|---|
| 457 | if (event.state == VisibilityPartiallyObscured){ |
|---|
| 458 | _ispartiallyvisible = true; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | if (event.state == VisibilityFullyObscured){ |
|---|
| 462 | _isvisible = false; |
|---|
| 463 | } |
|---|
| 464 | else { |
|---|
| 465 | _isvisible = true; |
|---|
| 466 | } |
|---|
| 467 | XOSDEBUG("Got visibility event; %d and %d\n", |
|---|
| 468 | _ispartiallyvisible, _isvisible); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | void XOSView::unmapEvent( XUnmapEvent & ){ |
|---|
| 472 | _isvisible = false; |
|---|
| 473 | } |
|---|