-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathxosview.cc
465 lines (392 loc) · 11.2 KB
/
xosview.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//
// Copyright (c) 1994, 1995, 2002, 2006 by Mike Romberg ( [email protected] )
//
// This file may be distributed under terms of the GPL
//
#include "xosview.h"
#include "meter.h"
#include "MeterMaker.h"
#if ( defined(XOSVIEW_NETBSD) || defined(XOSVIEW_FREEBSD) || \
defined(XOSVIEW_OPENBSD) || defined(XOSVIEW_DFBSD) )
# include "kernel.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
static const char * const versionString = "xosview version: Git";
static const char NAME[] = "xosview@";
#if !defined(__GNUC__)
#define MIN(x,y) \
( \
x < y ? x : y \
)
#define MAX(x,y) \
( \
x > y ? x : y \
)
#else
#define MIN(x,y) \
({ \
const typeof(x) _x = x; \
const typeof(y) _y = y; \
\
(void) (&_x == &_y); \
\
_x < _y ? _x : _y; \
})
#define MAX(x,y) \
({ \
const typeof(x) _x = x; \
const typeof(y) _y = y; \
\
(void) (&_x == &_y); \
\
_x > _y ? _x : _y; \
})
#endif // sgi
double MAX_SAMPLES_PER_SECOND = 10;
XOSView::XOSView( const char * instName, int argc, char *argv[] ) : XWin(),
xrm(Xrm("xosview", instName)){
// Check for version arguments first. This allows
// them to work without the need for a connection
// to the X server
checkVersion(argc, argv);
setDisplayName (xrm.getDisplayName( argc, argv));
openDisplay(); // So that the Xrm class can contact the display for its
// default values.
// The resources need to be initialized before calling XWinInit, because
// XWinInit looks at the geometry resource for its geometry. BCG
xrm.loadAndMergeResources (argc, argv, display_);
XWinInit (argc, argv, NULL, &xrm);
#if 1 // Don't enable this yet.
MAX_SAMPLES_PER_SECOND = atof(getResource("samplesPerSec"));
if (!MAX_SAMPLES_PER_SECOND)
MAX_SAMPLES_PER_SECOND = 10;
#endif
usleeptime_ = (unsigned long) (1000000/MAX_SAMPLES_PER_SECOND);
if (usleeptime_ >= 1000000) {
/* The syscall usleep() only takes times less than 1 sec, so
* split into a sleep time and a usleep time if needed. */
sleeptime_ = usleeptime_ / 1000000;
usleeptime_ = usleeptime_ % 1000000;
} else { sleeptime_ = 0; }
#if ( defined(XOSVIEW_NETBSD) || defined(XOSVIEW_FREEBSD) || \
defined(XOSVIEW_OPENBSD) || defined(XOSVIEW_DFBSD) )
BSDInit(); /* Needs to be done before processing of -N option. */
#endif
hmargin_ = atoi(getResource("horizontalMargin"));
vmargin_ = atoi(getResource("verticalMargin"));
vspacing_ = atoi(getResource("verticalSpacing"));
hmargin_ = MAX(0, hmargin_);
vmargin_ = MAX(0, vmargin_);
vspacing_ = MAX(0, vspacing_);
checkArgs (argc, argv); // Check for any other unhandled args.
xoff_ = hmargin_;
yoff_ = 0;
nummeters_ = 0;
meters_ = NULL;
name_ = const_cast<char *>("xosview");
_deferred_resize = true;
_deferred_redraw = true;
windowVisibility = OBSCURED;
// set up the X events
addEvent( new Event( this, ConfigureNotify,
(EventCallBack)&XOSView::resizeEvent ) );
addEvent( new Event( this, Expose,
(EventCallBack)&XOSView::exposeEvent ) );
addEvent( new Event( this, KeyPress,
(EventCallBack)&XOSView::keyPressEvent ) );
addEvent( new Event( this, VisibilityNotify,
(EventCallBack)&XOSView::visibilityEvent ) );
addEvent( new Event( this, UnmapNotify,
(EventCallBack)&XOSView::unmapEvent ) );
// add or change the Resources
MeterMaker mm(this);
// see if legends are to be used
checkOverallResources ();
// add in the meters
mm.makeMeters();
for (int i = 1 ; i <= mm.n() ; i++)
addmeter(mm[i]);
if (nummeters_ == 0) {
std::cerr << "No meters were enabled! Exiting..." << std::endl;
exit (0);
}
// Have the meters re-check the resources.
checkMeterResources();
// determine the width and height of the window then create it
figureSize();
init( argc, argv );
title( winname() );
iconname( winname() );
dolegends();
}
void XOSView::checkVersion(int argc, char *argv[]) const
{
for (int i = 0 ; i < argc ; i++)
if (!strncasecmp(argv[i], "-v", 2)
|| !strncasecmp(argv[i], "--version", 10))
{
std::cerr << versionString << std::endl;
exit(0);
}
}
void XOSView::figureSize ( void ) {
if ( legend_ ){
if ( !usedlabels_ )
xoff_ = textWidth( "XXXXXX" );
else
xoff_ = textWidth( "XXXXXXXXXX" );
yoff_ = caption_ ? textHeight() + textHeight() / 4 : 0;
}
static int firsttime = 1;
if (firsttime) {
firsttime = 0;
width_ = findx();
height_ = findy();
}
else
{
}
}
void XOSView::checkMeterResources( void ){
MeterNode *tmp = meters_;
while ( tmp != NULL ){
tmp->meter_->checkResources();
tmp = tmp->next_;
}
}
int XOSView::newypos( void ){
return 15 + 25 * nummeters_;
}
void XOSView::dolegends( void ){
MeterNode *tmp = meters_;
while ( tmp != NULL ){
tmp->meter_->docaptions( caption_ );
tmp->meter_->dolegends( legend_ );
tmp->meter_->dousedlegends( usedlabels_ );
tmp = tmp->next_;
}
}
void XOSView::addmeter( Meter *fm ){
MeterNode *tmp = meters_;
if ( meters_ == NULL )
meters_ = new MeterNode( fm );
else {
while ( tmp->next_ != NULL )
tmp = tmp->next_;
tmp->next_ = new MeterNode( fm );
}
nummeters_++;
}
int XOSView::findx( void ){
if ( legend_ ){
if ( !usedlabels_ )
return textWidth( "XXXXXXXXXXXXXXXXXXXXXXXX" );
else
return textWidth( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" );
}
return 80;
}
int XOSView::findy( void ){
if ( legend_ )
return 10 + textHeight() * nummeters_ * ( caption_ ? 2 : 1 );
return 15 * nummeters_;
}
void XOSView::checkOverallResources() {
// Check various resource values.
// Set 'off' value. This is not necessarily a default value --
// the value in the defaultXResourceString is the default value.
usedlabels_ = legend_ = caption_ = 0;
setFont();
// use captions
if ( isResourceTrue("captions") )
caption_ = 1;
// use labels
if ( isResourceTrue("labels") )
legend_ = 1;
// use "free" labels
if ( isResourceTrue("usedlabels") )
usedlabels_ = 1;
}
const char *XOSView::winname( void ){
char host[100];
gethostname( host, 99 );
host[99] = 0;
static char name[120]; /* We return a pointer to this,
so it can't be local. */
snprintf( name, 120, "%s%s", NAME, host);
// Allow overriding of this name through the -title option.
return getResourceOrUseDefault ("title", name);
}
void XOSView::resize( void ){
int spacing = vspacing_+1;
int topmargin = vmargin_;
int rightmargin = hmargin_;
int newwidth = width_ - xoff_ - rightmargin;
int newheight =
(height_ -
(topmargin + topmargin + (nummeters_-1)*spacing + nummeters_*yoff_)
) / nummeters_;
newheight = (newheight >= 2) ? newheight : 2;
int counter = 1;
MeterNode *tmp = meters_;
while ( tmp != NULL ) {
tmp->meter_->resize( xoff_,
topmargin + counter*yoff_ + (counter-1)*(newheight+spacing),
newwidth, newheight );
tmp = tmp->next_;
counter++;
}
}
XOSView::~XOSView( void ){
MeterNode *tmp = meters_;
while ( tmp != NULL ){
MeterNode *save = tmp->next_;
delete tmp->meter_;
delete tmp;
tmp = save;
}
}
void XOSView::draw(void) {
if (windowVisibility != OBSCURED) {
MeterNode *tmp = meters_;
XOSDEBUG("Doing draw.\n");
clear();
while (tmp != NULL) {
tmp->meter_->draw();
tmp = tmp->next_;
}
}
else {
XOSDEBUG("Skipping draw: not visible.\n");
}
}
void XOSView::run( void ){
while(!done_) {
// Check for X11 events
checkevent();
// Check if the window has been resized (at least once)
if (_deferred_resize) {
resize();
_deferred_resize = false;
_deferred_redraw = true;
}
// redraw everything if needed
if (_deferred_redraw) {
draw();
_deferred_redraw = false;
}
// Update the metrics & meters
MeterNode *tmp = meters_;
while ( tmp != NULL ){
if ( tmp->meter_->requestevent() )
tmp->meter_->checkevent();
tmp = tmp->next_;
}
flush();
/* First, sleep for the proper integral number of seconds --
* usleep only deals with times less than 1 sec. */
if (sleeptime_) sleep((unsigned int)sleeptime_);
if (usleeptime_) usleep( (unsigned int)usleeptime_);
}
}
void XOSView::keyPressEvent( XKeyEvent &event ){
char c = 0;
KeySym key;
XLookupString( &event, &c, 1, &key, NULL );
if ( (c == 'q') || (c == 'Q') )
done_ = 1;
}
void XOSView::checkArgs (int argc, char** argv) const
{
// The XWin constructor call in the XOSView constructor above
// modifies argc and argv, so by this
// point, all XResource arguments should be removed. Since we currently
// don't have any other command-line arguments, perform a check here
// to make sure we don't get any more.
if (argc == 1) return; // No arguments besides X resources.
// Skip to the first real argument.
argc--;
argv++;
while (argc > 0 && argv && *argv)
{
switch (argv[0][1]) {
case 'n': // Check for -name option that was already parsed
// and acted upon by main().
if (!strncasecmp(*argv, "-name", 6))
{
argv++; // Skip arg to -name.
argc--;
}
break;
#if ( defined(XOSVIEW_NETBSD) || defined(XOSVIEW_FREEBSD) || \
defined(XOSVIEW_OPENBSD) || defined(XOSVIEW_DFBSD) )
case 'N': if (strlen(argv[0]) > 2)
SetKernelName(argv[0]+2);
else
{
SetKernelName(argv[1]);
argc--;
argv++;
}
break;
#endif
/* Fall through to default/error case. */
default:
std::cerr << "Ignoring unknown option '" << argv[0] << "'.\n";
break;
}
argc--;
argv++;
}
}
void XOSView::exposeEvent(XExposeEvent &event) {
_deferred_redraw = true;
XOSDEBUG("Got expose event.\n");
}
/*
* All window changes come in via XConfigureEvent (not
* XResizeRequestEvent)
*/
void XOSView::resizeEvent( XConfigureEvent &event ) {
XOSDEBUG("Got configure event.\n");
if (event.width == width_ && event.height == height_)
return;
XOSDEBUG("Window has resized\n");
width(event.width);
height(event.height);
_deferred_resize = true;
}
void XOSView::visibilityEvent(XVisibilityEvent &event) {
if (event.state == VisibilityPartiallyObscured) {
if (windowVisibility != FULLY_VISIBLE)
_deferred_redraw = true;
windowVisibility = PARTIALLY_VISIBILE;
}
else if (event.state == VisibilityFullyObscured) {
windowVisibility = OBSCURED;
_deferred_redraw = false;
}
else {
if (windowVisibility != FULLY_VISIBLE)
_deferred_redraw = true;
windowVisibility = FULLY_VISIBLE;
}
XOSDEBUG("Got visibility event: %s\n",
(windowVisibility == FULLY_VISIBLE)
? "Full"
: (windowVisibility == PARTIALLY_VISIBILE)
? "Partial"
: "Obscured"
);
}
void XOSView::unmapEvent( XUnmapEvent & ev ){
/* unclutter creates a subwindow of our window if it hides the cursor,
we get the unmap event if the cursor is moved again. Don't treat it
as main window unmap */
if(ev.window == window_)
windowVisibility = OBSCURED;
}