// 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 #import // 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; // Clear to the background color if all rings are to be // redrawn. stipple = [[NSImage alloc] initWithSize: NSMakeSize(48,48)]; r = [[NSCustomImageRep alloc] initWithDrawSelector: @selector(drawImageRep) delegate: self]; [r autorelease]; [r setSize: NSMakeSize(48,48)]; [stipple addRepresentation: r]; [NSApp setApplicationIconImage:stipple]; [stipple release]; stipple = nil; } - (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