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
|
// Copyright 1991, 1994, 1997 Scott Hess. Permission to use, copy,
// modify, and distribute this software and its documentation for
// any purpose and without fee is hereby granted, provided that
// this copyright notice appear in all copies.
//
// Scott Hess makes no representations about the suitability of
// this software for any purpose. It is provided "as is" without
// express or implied warranty.
//
// Modifications to run on Cocoa made by Gregory Casamento and
// Alex Malmberg
//
#import "Percentages.h"
#import "TimeMonWraps.h"
#import "loadave.h"
#import "TimeMonColors.h"
#import <syslog.h>
#import <math.h>
// Determines how much movement is needed for a display/redisplay.
#define MINSHOWN 0.01
// Minimum values for the defaults.
#define MINPERIOD 0.1
#define MINFACTOR 4
#define MINLAGFACTOR 1
@implementation Percentages
- (id)init
{
self = [super init];
if (self)
{
// stipple = [NSImage imageNamed:@"NSApplicationIcon"];
defaults = [NSUserDefaults standardUserDefaults];
[NSApp setDelegate:self];
}
return self;
}
-(void) drawImageRep
{
int i;
static float radii[3]={ 23.0, 17.0, 11.0};
NSPoint point = NSMakePoint(24,24);
NSBezierPath *bp = [NSBezierPath bezierPath];
NSPoint outer = NSMakePoint(47.5, 24.0);
NSPoint middle = NSMakePoint(41.5, 24.0);
NSPoint inner = NSMakePoint(35.5, 24.0);
NSPoint lineEnd = NSMakePoint(24.0, 48.0);
for(i = 0; i < 3; i++)
{
// Store away the values we redraw.
bcopy(pcents[i], lpcents[i], sizeof(lpcents[i]));
drawArc2(radii[i],
90 - (pcents[i][0]) * 360,
90 - (pcents[i][0] + pcents[i][1]) * 360,
90 - (pcents[i][0] + pcents[i][1] + pcents[i][2]) * 360,
90 - (pcents[i][0] + pcents[i][1] + pcents[i][2] + pcents[i][3]) * 360);
}
[[NSColor blackColor] set];
[bp moveToPoint: outer];
[bp appendBezierPathWithArcWithCenter: point radius: 23.5 startAngle: 0 endAngle: 360 clockwise: NO];
[bp moveToPoint: middle];
[bp appendBezierPathWithArcWithCenter: point radius: 17.5 startAngle: 0 endAngle: 360 clockwise: NO];
[bp moveToPoint: inner];
[bp appendBezierPathWithArcWithCenter: point radius: 11.5 startAngle: 0 endAngle: 360 clockwise: NO];
[bp moveToPoint: point];
[bp lineToPoint: lineEnd];
[bp stroke];
}
- (void) awakeFromNib
{
NSString *path = [[NSBundle mainBundle] pathForResource: @"README"
ofType: @"rtf"];
// load the readme if it exists.
if (path != nil)
{
NSData *data = [NSData dataWithContentsOfFile: path];
if (data != nil)
{
NSDictionary *dict = nil;
NSTextStorage *ts = [[NSTextStorage alloc] initWithRTF: data
documentAttributes: &dict];
[[(NSTextView *)readmeText layoutManager] replaceTextStorage: ts];
}
}
}
- (void)update
{
NSImageRep *r;
NSImage *stipple;
stipple = [[NSImage alloc] initWithSize: NSMakeSize(48,48)];
r = [[NSCustomImageRep alloc]
initWithDrawSelector: @selector(drawImageRep)
delegate: self];
[r setSize: NSMakeSize(48,48)];
[stipple addRepresentation: r];
[NSApp setApplicationIconImage:stipple];
[r release];
[stipple release]; /* setApplicationIconImage does a retain, so we release */
}
- (void)step
{
int i, j, oIndex;
float total;
// Read the new CPU times.
la_read(oldTimes[laIndex]);
// The general idea for calculating the ring values is to
// first find the earliest valid index into the oldTimes
// table for that ring. Once in a "steady state", this is
// determined by the lagFactor and/or layerFactor values.
// Prior to steady state, things are restricted by how many
// steps have been performed. Note that the index must
// also be wrapped around to remain within the table.
//
// The values are all then easily calculated by subtracting
// the info at the old index from the info at the current
// index.
// Calculate values for the innermost "lag" ring.
oIndex=(laIndex-MIN(lagFactor, steps)+laSize)%laSize;
for(total=0, i=0; i<CPUSTATES; i++)
{
total+=oldTimes[laIndex][i]-oldTimes[oIndex][i];
}
if (total)
{
for (i = 0; i < CPUSTATES; i++)
pcents[2][i] = (oldTimes[laIndex][i] - oldTimes[oIndex][i]) / total;
}
// Calculate the middle ring.
oIndex=(laIndex-MIN(lagFactor+layerFactor, steps)+laSize)%laSize;
for(total=0, i=0; i<CPUSTATES; i++)
{
total+=oldTimes[laIndex][i]-oldTimes[oIndex][i];
}
if (total)
{
for (i = 0; i < CPUSTATES; i++)
pcents[1][i] = (oldTimes[laIndex][i] - oldTimes[oIndex][i]) / total;
}
// Calculate the outer ring.
oIndex=(laIndex-MIN(lagFactor+layerFactor*layerFactor, steps)+laSize)%laSize;
for(total=0, i=0; i<CPUSTATES; i++)
{
total+=oldTimes[laIndex][i]-oldTimes[oIndex][i];
}
if (total)
{
for (i = 0; i < CPUSTATES; i++)
pcents[0][i] = (oldTimes[laIndex][i] - oldTimes[oIndex][i]) / total;
}
// Move the index forward for the next cycle.
laIndex = (laIndex + 1) % laSize;
steps++;
// Look through the rings and see if any values changed by
// one percent or more, and if so mark that and inner rings
// for update.
for (i = 0; i < CPUSTATES; i++)
{
for (j = 0; j < CPUSTATES; j++)
{
if (rint(pcents[i][j] * 100) != rint(lpcents[i][j] * 100))
{
for ( ; i < 3; i++)
{
updateFlags[i] = YES;
}
break;
}
}
}
// If there's a need for updating of any rings, call update.
if (updateFlags[2])
{
[self update];
}
}
// This was for debugging, no longer needed. I used it to hook
// up a menu item to manually call step.
- (void)cycle:(id)sender
{
[self step];
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
}
// Resize the oldTimes array and rearrange the values within
// so that as many as possible are retained, but no bad values
// are introduced.
- (void)__reallocOldTimes
{
CPUTime *newTimes;
// Get the new size for the array.
unsigned newSize = layerFactor * layerFactor + lagFactor + 1;
// Allocate info for the array.
newTimes = NSZoneMalloc([self zone], sizeof(CPUTime) * newSize);
bzero(newTimes, sizeof(CPUTime) * newSize);
// If there was a previous array, copy over values. First,
// an index is found for the first valid time. Then enough
// times to fill the rings are copied, if available.
if (oldTimes)
{
unsigned ii, jj, elts;
elts = MIN(lagFactor + layerFactor * layerFactor + 1, steps);
ii = (laIndex + laSize - elts) % laSize;
jj = MIN(laSize - ii, elts);
if (jj)
{
bcopy(oldTimes + ii, newTimes + 0, jj * sizeof(oldTimes[0]));
}
if (jj < elts)
{
bcopy(oldTimes + 0, newTimes + jj, (elts - jj) * sizeof(oldTimes[0]));
}
// Free the old times.
NSZoneFree([self zone], oldTimes);
}
// Reset everything so that we only access valid data.
oldTimes = newTimes;
laIndex = MIN(steps, laSize);
laIndex = MIN(laIndex, newSize)%newSize;
steps = MIN(steps, laSize);
steps = MIN(steps, newSize);
laSize = newSize;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
float f;
int ret;
CPUTime cp_time;
SEL stepSel = @selector(step);
NSMethodSignature *sig = [self methodSignatureForSelector:stepSel];
NSDictionary *defs = [NSDictionary dictionaryWithObjectsAndKeys:
@"0.5", @"UpdatePeriod",
@"4", @"LagFactor",
@"16", @"LayerFactor",
@"YES", @"HideOnAutolaunch",
@"NO", @"NXAutoLaunch",
// For color systems.
@"1.000 1.000 1.000 1.000", @"IdleColor", // White
@"0.333 0.667 0.867 1.000", @"NiceColor", // A light blue-green
@"0.200 0.467 0.800 1.000", @"UserColor", // A darker blue-green
@"0.000 0.000 1.000 1.000", @"SystemColor", // Blue
@"1.000 0.800 0.900 1.000", @"IOWaitColor", // Light purple
// For monochrome systems.
@"1.000", @"IdleGray", // White
@"0.667", @"NiceGray", // Light gray
@"0.333", @"UserGray", // Dark gray
@"0.000", @"SystemGray", // Black
nil];
[defaults registerDefaults:defs];
[defaults synchronize];
// Shoot out error codes if there was an error.
if ((ret = la_init(cp_time)) )
{
const id syslogs[] = {
NULL, // LA_NOERR
@"Cannot read or parse /proc/stat." // LA_ERROR
};
NSLog(@"TimeMon: %@", syslogs[ret]);
NSLog(@"TimeMon: Exiting!");
NSRunAlertPanel(@"TimeMon", syslogs[ret], @"OK", nil, nil, nil);
[NSApp terminate:[notification object]];
}
// Move ourselves over to the appIcon window.
// [NSApp setApplicationIconImage:stipple];
// Get us registered for periodic exec.
f = [defaults floatForKey:@"UpdatePeriod"];
f = MAX(f, MINPERIOD);
[periodText setFloatValue:f];
selfStep = [[NSInvocation invocationWithMethodSignature:sig] retain];
[selfStep setSelector:stepSel];
[selfStep setTarget:self];
[selfStep retainArguments];
te = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)f invocation:selfStep repeats:YES];
// Get the lag factor.
lagFactor = [defaults integerForKey:@"LagFactor"];
lagFactor = MAX(lagFactor, MINLAGFACTOR);
[lagText setIntValue:lagFactor];
// Get the layer factor.
layerFactor = [defaults integerForKey:@"LayerFactor"];
layerFactor = MAX(layerFactor, MINFACTOR);
[factorText setIntValue:layerFactor];
[self __reallocOldTimes];
bcopy(cp_time, oldTimes[0], sizeof(CPUTime));
laIndex = 1;
steps = 1;
[colorFields readColors];
if ([defaults boolForKey:@"HideOnAutolaunch"]
&& [defaults boolForKey:@"NXAutoLaunch"]) {
[NSApp hide:self];
}
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(id)sender
{
// If te is installed, remove it.
if (te)
{
[te invalidate];
te = nil;
}
// [stipple release];
la_finish();
return NSTerminateNow;
}
- (void)display
{
updateFlags[0] = updateFlags[1] = updateFlags[2] = YES;
[self update];
}
- (void)togglePause:(id)sender
{
if (te)
{
NSImage *pausedImage = [NSImage imageNamed:@"TimeMonP"];
NSImage *pausedStipple = [NSApp applicationIconImage];
[pauseMenuCell setTitle:@"Continue"];
[te invalidate];
te = nil;
[pausedStipple lockFocus];
[pausedImage compositeToPoint:NSZeroPoint
operation:NSCompositeSourceOver];
[pausedStipple unlockFocus];
[NSApp setApplicationIconImage: pausedStipple];
}
else
{
float f;
[pauseMenuCell setTitle:@"Pause"];
f = [defaults floatForKey:@"UpdatePeriod"];
f = MAX(f, MINPERIOD);
[periodText setFloatValue:f];
te = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)f invocation:selfStep repeats:YES];
[self display];
}
}
- (void)setPeriod:(id)sender
{
[defaults setObject:[periodText stringValue] forKey:@"UpdatePeriod"];
[defaults synchronize];
if (te) {
float f;
[te invalidate];
f = [defaults floatForKey:@"UpdatePeriod"];
f = MAX(f, MINPERIOD);
[periodText setFloatValue:f];
te = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)f invocation:selfStep repeats:YES];
}
}
- (void)setLag:(id)sender
{
[defaults setObject:[lagText stringValue] forKey:@"LagFactor"];
[defaults synchronize];
lagFactor = [defaults integerForKey:@"LagFactor"];
lagFactor = MAX(lagFactor, MINLAGFACTOR);
[lagText setIntValue:lagFactor];
[self __reallocOldTimes];
}
- (void)setFactor:(id)sender
{
[defaults setObject:[factorText stringValue] forKey:@"LayerFactor"];
[defaults synchronize];
layerFactor = [defaults integerForKey:@"LayerFactor"];
layerFactor = MAX(layerFactor, MINFACTOR);
[factorText setIntValue:layerFactor];
[self __reallocOldTimes];
}
- (BOOL)textShouldEndEditing:(NSText *)sender
{
id delegate = [sender delegate];
if (delegate == factorText)
{
[self setFactor:factorText];
}
else if (delegate == periodText)
{
[self setPeriod:periodText];
}
else if (delegate == lagText)
{
[self setLag:lagText];
}
return YES;
}
@end
|