[325] | 1 | /*
|
---|
| 2 | author: Onne Gorter
|
---|
| 3 |
|
---|
| 4 | This file is part of AntiRSI.
|
---|
| 5 |
|
---|
| 6 | AntiRSI is free software; you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | AntiRSI is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with AntiRSI; if not, write to the Free Software
|
---|
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | #import "AntiRSI.h"
|
---|
| 22 |
|
---|
| 23 | #include <math.h>
|
---|
| 24 | #include <ApplicationServices/ApplicationServices.h>
|
---|
| 25 |
|
---|
[328] | 26 | extern CFTimeInterval CGSSecondsSinceLastInputEvent(unsigned long eventType);
|
---|
[325] | 27 |
|
---|
[333] | 28 | static int badge_session_minutes = -1;
|
---|
| 29 |
|
---|
[325] | 30 | @implementation AntiRSI
|
---|
| 31 |
|
---|
[333] | 32 | - (void)computeResetSessionDate;
|
---|
| 33 | {
|
---|
| 34 | NSCalendarDate *now = [NSCalendarDate calendarDate];
|
---|
| 35 | NSCalendarDate *reset =
|
---|
[346] | 36 | [NSCalendarDate dateWithYear:[now yearOfCommonEra]
|
---|
| 37 | month:[now monthOfYear]
|
---|
| 38 | day:[now dayOfMonth]
|
---|
| 39 | hour:[reset_session_timer_time hourOfDay]
|
---|
| 40 | minute:[reset_session_timer_time minuteOfHour]
|
---|
| 41 | second:0
|
---|
| 42 | timeZone:[NSTimeZone systemTimeZone]];
|
---|
[333] | 43 | if ([now compare:reset] != NSOrderedAscending)
|
---|
| 44 | reset = [reset dateByAddingYears:0 months:0 days:1 hours:0 minutes:0 seconds:0];
|
---|
| 45 | reset_session_date = [reset timeIntervalSinceReferenceDate];
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[325] | 48 | // bindings methods
|
---|
| 49 | - (void)setMicro_pause_duration:(float)f
|
---|
| 50 | {
|
---|
[346] | 51 | micro_pause_duration = round(f);
|
---|
| 52 | if (s_taking_micro_pause == state) {
|
---|
| 53 | [progress setMaxValue:micro_pause_duration];
|
---|
| 54 | [progress setDoubleValue:micro_pause_taking_t];
|
---|
| 55 | }
|
---|
[325] | 56 | }
|
---|
| 57 |
|
---|
| 58 | - (void)setMicro_pause_period:(float)f
|
---|
| 59 | { micro_pause_period = 60 * round(f); }
|
---|
| 60 |
|
---|
| 61 | - (void)setWork_break_duration:(float)f
|
---|
| 62 | {
|
---|
[346] | 63 | work_break_duration = 60 * round(f);
|
---|
| 64 | if (s_taking_work_break == state) {
|
---|
| 65 | [progress setMaxValue:work_break_duration / 60];
|
---|
| 66 | [progress setDoubleValue:work_break_taking_t / 60 - 0.5];
|
---|
| 67 | }
|
---|
[325] | 68 | }
|
---|
| 69 |
|
---|
| 70 | - (void)setWork_break_period:(float)f
|
---|
| 71 | { work_break_period = 60 * round(f); }
|
---|
| 72 |
|
---|
[333] | 73 | - (void)setReset_session_timer_time:(NSDate *)d;
|
---|
| 74 | {
|
---|
| 75 | if (reset_session_timer_time != nil)
|
---|
| 76 | [reset_session_timer_time autorelease];
|
---|
| 77 | reset_session_timer_time = [[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:[d timeIntervalSinceReferenceDate]];
|
---|
| 78 | [reset_session_timer_time setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
|
---|
| 79 | [self computeResetSessionDate];
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[325] | 82 | - (void)installTimer:(double)interval
|
---|
| 83 | {
|
---|
[346] | 84 | if (mtimer != nil) {
|
---|
| 85 | [mtimer invalidate];
|
---|
| 86 | [mtimer autorelease];
|
---|
| 87 | }
|
---|
| 88 | mtimer = [[NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(tick:)
|
---|
[325] | 89 | userInfo:nil repeats:YES] retain];
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | - (void)setSample_interval:(NSString *)s
|
---|
| 93 | {
|
---|
[346] | 94 | sample_interval = 1;
|
---|
| 95 | if ([s isEqualToString:@"Super Smooth"]) sample_interval = 0.1;
|
---|
| 96 | if ([s isEqualToString:@"Smooth"]) sample_interval = 0.33;
|
---|
| 97 | if ([s isEqualToString:@"Normal"]) sample_interval = 1;
|
---|
| 98 | if ([s isEqualToString:@"Low"]) sample_interval = 2;
|
---|
| 99 |
|
---|
| 100 | [self installTimer:sample_interval];
|
---|
[325] | 101 | }
|
---|
| 102 |
|
---|
| 103 | - (void)setDraw_dock_image:(BOOL)b
|
---|
| 104 | {
|
---|
[346] | 105 | draw_dock_image=b;
|
---|
| 106 | if (!b) {
|
---|
| 107 | [NSApp setApplicationIconImage:[NSImage imageNamed:@"AntiRSI"]];
|
---|
| 108 | } else {
|
---|
| 109 | [self drawDockImage];
|
---|
| 110 | }
|
---|
[325] | 111 | }
|
---|
| 112 |
|
---|
| 113 | - (void)setBackground:(NSColor *)c
|
---|
| 114 | {
|
---|
[346] | 115 | [background autorelease];
|
---|
| 116 | background=[c retain];
|
---|
| 117 |
|
---|
| 118 | // make new darkbackground color
|
---|
| 119 | float r,g,b,a;
|
---|
| 120 | [background getRed:&r green:&g blue:&b alpha:&a];
|
---|
| 121 | [darkbackground autorelease];
|
---|
| 122 | darkbackground=[[NSColor colorWithCalibratedRed:r*0.35 green:g*0.35 blue:b*0.35 alpha:a+0.2] retain];
|
---|
| 123 |
|
---|
| 124 | [self drawDockImage];
|
---|
[325] | 125 | }
|
---|
| 126 |
|
---|
| 127 | - (void)setElapsed:(NSColor *)c
|
---|
| 128 | {
|
---|
[346] | 129 | [elapsed autorelease];
|
---|
| 130 | elapsed=[c retain];
|
---|
[332] | 131 | [dock_badge setBadgeColor:elapsed];
|
---|
[333] | 132 | badge_session_minutes = -1;
|
---|
[346] | 133 | [self drawDockImage];
|
---|
[325] | 134 | }
|
---|
| 135 |
|
---|
| 136 | - (void)setTaking:(NSColor *)c
|
---|
| 137 | {
|
---|
[346] | 138 | [taking autorelease];
|
---|
| 139 | taking=[c retain];
|
---|
| 140 | [self drawDockImage];
|
---|
[325] | 141 | }
|
---|
| 142 |
|
---|
| 143 | // end of bindings
|
---|
| 144 |
|
---|
[331] | 145 | - (void)resetTimers;
|
---|
| 146 | {
|
---|
[346] | 147 | // set timers to 0
|
---|
| 148 | micro_pause_t = 0;
|
---|
| 149 | work_break_t = 0;
|
---|
| 150 | micro_pause_taking_t = 0;
|
---|
| 151 | work_break_taking_t = 0;
|
---|
| 152 | work_break_taking_cached_t = 0;
|
---|
| 153 | work_break_taking_cached_date = 0;
|
---|
[331] | 154 | session_t = 0;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[325] | 157 | - (void)awakeFromNib
|
---|
| 158 | {
|
---|
[346] | 159 | // want transparancy
|
---|
| 160 | [NSColor setIgnoresAlpha:NO];
|
---|
| 161 |
|
---|
| 162 | // initial colors
|
---|
| 163 | elapsed = [[NSColor colorWithCalibratedRed:0.3 green:0.3 blue:0.9 alpha:0.95] retain];
|
---|
| 164 | taking = [[NSColor colorWithCalibratedRed:0.3 green:0.9 blue:0.3 alpha:0.90] retain];
|
---|
| 165 | background = [NSColor colorWithCalibratedRed:0.9 green:0.9 blue:0.9 alpha:0.7];
|
---|
| 166 |
|
---|
| 167 | //initial values
|
---|
| 168 | micro_pause_period = 4*60;
|
---|
| 169 | micro_pause_duration = 13;
|
---|
| 170 | work_break_period = 50*60;
|
---|
| 171 | work_break_duration = 8*60;
|
---|
| 172 | sample_interval = 1;
|
---|
| 173 |
|
---|
| 174 | // set current state
|
---|
| 175 | state = s_normal;
|
---|
[331] | 176 | [self resetTimers];
|
---|
[346] | 177 |
|
---|
| 178 | // initialize dock image
|
---|
| 179 | dock_image = [[NSImage alloc] initWithSize:NSMakeSize(128,128)];
|
---|
| 180 | [dock_image setCacheMode:NSImageCacheNever];
|
---|
| 181 | original_dock_image = [NSImage imageNamed:@"AntiRSI"];
|
---|
| 182 | draw_dock_image_q = YES;
|
---|
[332] | 183 | dock_badge = [[CTBadge systemBadge] retain];
|
---|
| 184 | [dock_badge setBadgeColor:elapsed];
|
---|
[346] | 185 |
|
---|
| 186 | // setup main window that will show either micropause or workbreak
|
---|
| 187 | main_window = [[NSWindow alloc] initWithContentRect:[view frame]
|
---|
| 188 | styleMask:NSBorderlessWindowMask
|
---|
| 189 | backing:NSBackingStoreBuffered defer:YES];
|
---|
| 190 | [main_window setBackgroundColor:[NSColor clearColor]];
|
---|
| 191 | [main_window setLevel:NSStatusWindowLevel];
|
---|
| 192 | [main_window setAlphaValue:0.85];
|
---|
| 193 | [main_window setOpaque:NO];
|
---|
| 194 | [main_window setHasShadow:NO];
|
---|
| 195 | [main_window setMovableByWindowBackground:YES];
|
---|
[370] | 196 | if ([main_window respondsToSelector:@selector(setCollectionBehavior:)])
|
---|
| 197 | [main_window setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces];
|
---|
| 198 |
|
---|
[346] | 199 | [main_window center];
|
---|
| 200 | [main_window setContentView:view];
|
---|
[332] | 201 | NSTimeZone *utcZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
|
---|
| 202 | [reset_session_time setTimeZone:utcZone];
|
---|
[325] | 203 | [progress setEnabled:NO];
|
---|
[331] | 204 | NSFont *myriad = [NSFont fontWithName: @"Myriad" size: 40];
|
---|
| 205 | if (myriad) [status setFont: myriad];
|
---|
[346] | 206 |
|
---|
| 207 | // initialze history filter
|
---|
| 208 | h0 = 0;
|
---|
| 209 | h1 = 0;
|
---|
| 210 | h2 = 0;
|
---|
| 211 |
|
---|
| 212 | // initialize ticks
|
---|
| 213 | date = [NSDate timeIntervalSinceReferenceDate];
|
---|
| 214 |
|
---|
| 215 | // set background now
|
---|
| 216 | [self setBackground:background];
|
---|
| 217 |
|
---|
| 218 | // create initial values
|
---|
| 219 | NSUserDefaultsController *dc = [NSUserDefaultsController sharedUserDefaultsController];
|
---|
| 220 | NSMutableDictionary* initial = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
---|
| 221 | [NSNumber numberWithFloat:4], @"micro_pause_period",
|
---|
| 222 | [NSNumber numberWithFloat:13], @"micro_pause_duration",
|
---|
| 223 | [NSNumber numberWithFloat:50], @"work_break_period",
|
---|
| 224 | [NSNumber numberWithFloat:8], @"work_break_duration",
|
---|
| 225 | @"Smooth", @"sample_interval",
|
---|
| 226 | [NSNumber numberWithBool:YES], @"draw_dock_image",
|
---|
| 227 | [NSNumber numberWithBool:YES], @"draw_dock_badge",
|
---|
| 228 | [NSNumber numberWithBool:NO], @"lock_focus",
|
---|
| 229 | [NSNumber numberWithBool:NO], @"reset_session_timer_daily",
|
---|
| 230 | [NSNumber numberWithBool:NO], @"reset_session_timer_after",
|
---|
| 231 | [NSCalendarDate dateWithYear:2000 month:1 day:1 hour:6 minute:0 second:0 timeZone:utcZone], @"reset_session_timer_time",
|
---|
| 232 | [NSNumber numberWithInt:8], @"reset_session_timer_after_hours",
|
---|
| 233 | [NSArchiver archivedDataWithRootObject:elapsed], @"elapsed",
|
---|
| 234 | [NSArchiver archivedDataWithRootObject:taking], @"taking",
|
---|
| 235 | [NSArchiver archivedDataWithRootObject:background], @"background",
|
---|
| 236 | nil];
|
---|
[332] | 237 | [[NSUserDefaults standardUserDefaults] registerDefaults:initial];
|
---|
[346] | 238 | [dc setInitialValues:initial];
|
---|
| 239 |
|
---|
[370] | 240 | // bind to defaults controller
|
---|
[346] | 241 | [self bind:@"micro_pause_period" toObject:dc withKeyPath:@"values.micro_pause_period" options:nil];
|
---|
| 242 | [self bind:@"micro_pause_duration" toObject:dc withKeyPath:@"values.micro_pause_duration" options:nil];
|
---|
| 243 | [self bind:@"work_break_period" toObject:dc withKeyPath:@"values.work_break_period" options:nil];
|
---|
| 244 | [self bind:@"work_break_duration" toObject:dc withKeyPath:@"values.work_break_duration" options:nil];
|
---|
| 245 | [self bind:@"sample_interval" toObject:dc withKeyPath:@"values.sample_interval" options:nil];
|
---|
| 246 | [self bind:@"draw_dock_image" toObject:dc withKeyPath:@"values.draw_dock_image" options:nil];
|
---|
[332] | 247 | [self bind:@"draw_dock_badge" toObject:dc withKeyPath:@"values.draw_dock_badge" options:nil];
|
---|
[346] | 248 | [self bind:@"lock_focus" toObject:dc withKeyPath:@"values.lock_focus" options:nil];
|
---|
[332] | 249 | [self bind:@"reset_session_timer_daily" toObject:dc withKeyPath:@"values.reset_session_timer_daily" options:nil];
|
---|
| 250 | [self bind:@"reset_session_timer_after" toObject:dc withKeyPath:@"values.reset_session_timer_after" options:nil];
|
---|
| 251 | [self bind:@"reset_session_timer_time" toObject:dc withKeyPath:@"values.reset_session_timer_time" options:nil];
|
---|
| 252 | [self bind:@"reset_session_timer_after_hours" toObject:dc withKeyPath:@"values.reset_session_timer_after_hours" options:nil];
|
---|
[346] | 253 | NSDictionary* unarchive = [NSDictionary dictionaryWithObject:NSUnarchiveFromDataTransformerName forKey:@"NSValueTransformerName"];
|
---|
| 254 | [self bind:@"elapsed" toObject:dc withKeyPath:@"values.elapsed" options:unarchive];
|
---|
| 255 | [self bind:@"taking" toObject:dc withKeyPath:@"values.taking" options:unarchive];
|
---|
| 256 | [self bind:@"background" toObject:dc withKeyPath:@"values.background" options:unarchive];
|
---|
[332] | 257 |
|
---|
[346] | 258 | // alert every binding
|
---|
| 259 | [dc revert:self];
|
---|
| 260 |
|
---|
| 261 | // start the timer
|
---|
| 262 | [self installTimer:sample_interval];
|
---|
| 263 |
|
---|
| 264 | // about dialog
|
---|
| 265 | [version setStringValue:[NSString stringWithFormat:@"Version %@", sVersion]];
|
---|
[325] | 266 | }
|
---|
| 267 |
|
---|
| 268 | // tick every second and update status
|
---|
| 269 | - (void)tick:(NSTimer *)timer
|
---|
| 270 | {
|
---|
[346] | 271 | // calculate time since last tick
|
---|
| 272 | NSTimeInterval new_date = [NSDate timeIntervalSinceReferenceDate];
|
---|
| 273 | NSTimeInterval tick_time = new_date - date;
|
---|
| 274 | date = new_date;
|
---|
| 275 |
|
---|
[332] | 276 | if (reset_session_timer_daily && date >= reset_session_date) {
|
---|
| 277 | [self resetSession:nil];
|
---|
| 278 | [self computeResetSessionDate];
|
---|
| 279 | return;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[346] | 282 | // check if we are still on track of normal time, otherwise we might have slept or something
|
---|
| 283 | if (tick_time > work_break_duration) {
|
---|
| 284 | // set timers to 0
|
---|
| 285 | micro_pause_t = 0;
|
---|
| 286 | work_break_t = 0;
|
---|
| 287 | micro_pause_taking_t = micro_pause_duration;
|
---|
| 288 | work_break_taking_t = work_break_duration;
|
---|
| 289 | if (s_normal != state) {
|
---|
| 290 | [self endBreak];
|
---|
[325] | 291 | }
|
---|
[346] | 292 | // and do stuff on next tick
|
---|
| 293 | return;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | // just did a whole micropause beyond normal time
|
---|
| 297 | if (tick_time > micro_pause_duration && s_taking_work_break != state) {
|
---|
| 298 | // set micro_pause timers to 0
|
---|
| 299 | micro_pause_t = 0;
|
---|
| 300 | micro_pause_taking_t = micro_pause_duration;
|
---|
| 301 | if (s_normal != state) {
|
---|
| 302 | [self endBreak];
|
---|
[325] | 303 | }
|
---|
[346] | 304 | // and do stuff on next tick
|
---|
| 305 | return;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | // get idle time in seconds
|
---|
| 309 | CFTimeInterval idle_time = CGSSecondsSinceLastInputEvent(kCGAnyInputEventType);
|
---|
[328] | 310 | // CFTimeInterval cgs_idle_time = idle_time;
|
---|
[325] | 311 | // from other people's reverse engineering of this function, on MDD G4s this can return a large positive number when input is in progress
|
---|
| 312 | if (idle_time >= 18446744000.0) {
|
---|
[328] | 313 | idle_time = 0;
|
---|
[325] | 314 | } else if (CGEventSourceSecondsSinceLastEventType != NULL) {
|
---|
[346] | 315 | CGEventType eventTypes[] = { kCGEventLeftMouseDown, kCGEventLeftMouseUp, kCGEventRightMouseDown, kCGEventRightMouseUp, kCGEventMouseMoved, kCGEventLeftMouseDragged, kCGEventRightMouseDragged, kCGEventKeyDown, kCGEventKeyUp, kCGEventFlagsChanged, kCGEventScrollWheel, kCGEventTabletPointer, kCGEventTabletProximity, kCGEventOtherMouseDown, kCGEventOtherMouseUp, kCGEventOtherMouseDragged, kCGEventNull };
|
---|
[328] | 316 | CFTimeInterval event_idle_time;
|
---|
| 317 | idle_time = DBL_MAX;
|
---|
[325] | 318 | for (CGEventType *eventType = eventTypes ; *eventType != kCGEventNull ; eventType++) {
|
---|
[328] | 319 | event_idle_time = CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState, *eventType);
|
---|
| 320 | if (event_idle_time < idle_time) idle_time = event_idle_time;
|
---|
[325] | 321 | }
|
---|
| 322 | }
|
---|
| 323 | // NSLog(@"CGEventSource %.2f, CGS %.2f", idle_time, cgs_idle_time);
|
---|
| 324 |
|
---|
[332] | 325 | if (reset_session_timer_after && idle_time > reset_session_timer_after_hours * 3600) {
|
---|
| 326 | [self resetSession:nil];
|
---|
| 327 | return;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[346] | 330 | // calculate slack, this gives a sort of 3 history filtered idea.
|
---|
| 331 | BOOL slack = (h2 + h1 + h0 > 15);
|
---|
| 332 |
|
---|
| 333 | // if new event comes in history bumps up
|
---|
| 334 | if (h0 >= idle_time || idle_time < sample_interval) {
|
---|
| 335 | h2 = h1;
|
---|
| 336 | h1 = h0;
|
---|
| 337 | }
|
---|
| 338 | h0 = idle_time;
|
---|
| 339 |
|
---|
| 340 | switch (state) {
|
---|
| 341 | case s_normal:
|
---|
| 342 | // idle_time needs to be at least 0.3 * micro_pause_duration before kicking in
|
---|
| 343 | // but we cut the user some slack based on previous idle_times
|
---|
| 344 | if (idle_time <= micro_pause_duration * 0.3 && !slack) {
|
---|
| 345 | micro_pause_t += tick_time;
|
---|
| 346 | work_break_t += tick_time;
|
---|
[330] | 347 | if (idle_time < 1) {
|
---|
| 348 | session_t += tick_time;
|
---|
| 349 | }
|
---|
[346] | 350 | micro_pause_taking_t = 0;
|
---|
| 351 | if (work_break_taking_t > 0) {
|
---|
| 352 | work_break_taking_cached_t = work_break_taking_t;
|
---|
| 353 | work_break_taking_cached_date = date;
|
---|
| 354 | }
|
---|
| 355 | work_break_taking_t = 0;
|
---|
| 356 | } else if (micro_pause_t > 0) {
|
---|
| 357 | // oke, leaway is over, increase micro_pause_taking_t unless micro_pause is already over
|
---|
| 358 | //micro_pause_t stays put
|
---|
| 359 | work_break_t += tick_time;
|
---|
| 360 | micro_pause_taking_t += tick_time;
|
---|
| 361 | work_break_taking_t = 0;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | // if micro_pause_taking_t is above micro_pause_duration, then micro pause is over,
|
---|
| 365 | // if still idleing workbreak_taking_t kicks in unless it is already over
|
---|
| 366 | if (micro_pause_taking_t >= micro_pause_duration && work_break_t > 0) {
|
---|
| 367 | work_break_taking_t += tick_time;
|
---|
| 368 | micro_pause_t = 0;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | // if work_break_taking_t is above work_break_duration, then work break is over
|
---|
| 372 | if (work_break_taking_t >= work_break_duration) {
|
---|
| 373 | micro_pause_t = 0;
|
---|
| 374 | work_break_t = 0;
|
---|
| 375 | // micro_pause_taking_t stays put
|
---|
| 376 | // work_break_taking_t stays put
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | // if user needs to take a micro pause
|
---|
| 380 | if (micro_pause_t >= micro_pause_period) {
|
---|
| 381 | // anticipate next workbreak by not issuing this micro_pause ...
|
---|
| 382 | if (work_break_t > work_break_period - (micro_pause_period / 2)) {
|
---|
| 383 | work_break_t = work_break_period;
|
---|
| 384 | [self doWorkBreak];
|
---|
| 385 | } else {
|
---|
| 386 | [self doMicroPause];
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | // if user needs to take a work break
|
---|
| 391 | if (work_break_t >= work_break_period) {
|
---|
| 392 | // stop micro_pause stuff
|
---|
| 393 | micro_pause_t = 0;
|
---|
| 394 | micro_pause_taking_t = micro_pause_duration;
|
---|
| 395 | // and display window
|
---|
| 396 | [self doWorkBreak];
|
---|
| 397 | }
|
---|
| 398 | break;
|
---|
| 399 |
|
---|
| 400 | // taking a micro pause with window
|
---|
| 401 | case s_taking_micro_pause:
|
---|
| 402 | // continue updating timers
|
---|
| 403 | micro_pause_taking_t += tick_time;
|
---|
| 404 | work_break_t += tick_time;
|
---|
| 405 |
|
---|
| 406 | // if we don't break, or interrupt the break, reset it
|
---|
| 407 | if (idle_time < 1 && !slack) {
|
---|
| 408 | micro_pause_taking_t = 0;
|
---|
[330] | 409 | session_t += tick_time;
|
---|
[346] | 410 | }
|
---|
| 411 |
|
---|
| 412 | // update window
|
---|
[330] | 413 | [self updateBreakWindowDuration:micro_pause_duration progress:micro_pause_taking_t
|
---|
| 414 | nextBreak:work_break_period - work_break_t];
|
---|
[346] | 415 |
|
---|
| 416 | // check if we done enough
|
---|
| 417 | if (micro_pause_taking_t > micro_pause_duration) {
|
---|
| 418 | micro_pause_t = 0;
|
---|
| 419 | [self endBreak];
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | // if workbreak must be run ...
|
---|
| 423 | if (work_break_t >= work_break_period) {
|
---|
| 424 | // stop micro_pause stuff
|
---|
| 425 | micro_pause_t = 0;
|
---|
| 426 | micro_pause_taking_t = micro_pause_duration;
|
---|
| 427 | // and display window
|
---|
| 428 | [self doWorkBreak];
|
---|
| 429 | } else {
|
---|
[325] | 430 | double slip = (micro_pause_duration - micro_pause_taking_t) - (int)(micro_pause_duration - micro_pause_taking_t);
|
---|
| 431 | [self installTimer: slip < 0.1 ? 1 : slip];
|
---|
| 432 | }
|
---|
[346] | 433 | break;
|
---|
| 434 |
|
---|
| 435 | // taking a work break with window
|
---|
| 436 | case s_taking_work_break:
|
---|
| 437 | // increase work_break_taking_t
|
---|
| 438 | if (idle_time >= 2 || work_break_taking_t < 3) {
|
---|
| 439 | work_break_taking_t += tick_time;
|
---|
| 440 | } else if (idle_time < 1) {
|
---|
[330] | 441 | session_t += tick_time;
|
---|
| 442 | }
|
---|
[346] | 443 |
|
---|
| 444 | // draw window
|
---|
[330] | 445 | [self updateBreakWindowDuration:work_break_duration progress:work_break_taking_t
|
---|
| 446 | nextBreak:work_break_period + work_break_duration - work_break_taking_t];
|
---|
[346] | 447 |
|
---|
| 448 | // and check if we done enough
|
---|
| 449 | if (work_break_taking_t > work_break_duration) {
|
---|
| 450 | micro_pause_t = 0;
|
---|
| 451 | micro_pause_taking_t = micro_pause_duration;
|
---|
| 452 | work_break_t = 0;
|
---|
| 453 | work_break_taking_t = work_break_duration;
|
---|
| 454 | [self endBreak];
|
---|
| 455 | } else {
|
---|
[325] | 456 | double slip = (work_break_duration - work_break_taking_t) - (int)(work_break_duration - work_break_taking_t);
|
---|
| 457 | [self installTimer: slip < 0.1 ? 1 : slip];
|
---|
| 458 | }
|
---|
[346] | 459 | break;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | // draw dock image
|
---|
| 463 | if (draw_dock_image) [self drawDockImage];
|
---|
[325] | 464 | }
|
---|
| 465 |
|
---|
[330] | 466 | // dock image
|
---|
| 467 | - (NSMenu *)applicationDockMenu:(NSApplication *)sender;
|
---|
| 468 | {
|
---|
| 469 | [session_time_item setTitle:[self sessionTimeString]];
|
---|
| 470 | return dock_menu;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[325] | 473 | // draw the dock icon
|
---|
| 474 | - (void)drawDockImage
|
---|
| 475 | {
|
---|
[346] | 476 | [dock_image lockFocus];
|
---|
| 477 |
|
---|
| 478 | // clear all
|
---|
| 479 | [[NSColor clearColor] set];
|
---|
| 480 | NSRectFill(NSMakeRect(0,0,127,127));
|
---|
| 481 |
|
---|
| 482 | NSBezierPath* p;
|
---|
| 483 | float end;
|
---|
| 484 |
|
---|
| 485 | //draw background circle
|
---|
| 486 | [darkbackground set];
|
---|
| 487 | p =[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(6,6,115,115)];
|
---|
| 488 | [p setLineWidth:4];
|
---|
| 489 | [p stroke];
|
---|
| 490 |
|
---|
| 491 | //fill
|
---|
| 492 | [background set];
|
---|
| 493 | [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(8,8,111,111)] fill];
|
---|
| 494 |
|
---|
| 495 | //put dot in middle
|
---|
| 496 | [darkbackground set];
|
---|
| 497 | [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(59,59,9,9)] fill];
|
---|
| 498 |
|
---|
| 499 | // reuse this one
|
---|
| 500 | p = [NSBezierPath bezierPath];
|
---|
| 501 |
|
---|
| 502 | // draw work_break
|
---|
| 503 | [elapsed set];
|
---|
| 504 | end = 360 - (360.0 / work_break_period * work_break_t - 90);
|
---|
| 505 | if (end <= 90) end=90.1;
|
---|
| 506 | [p appendBezierPathWithArcWithCenter:NSMakePoint(63.5, 63.5) radius:40 startAngle:90 endAngle:end clockwise:YES];
|
---|
| 507 | [p setLineWidth:22];
|
---|
| 508 | [p stroke];
|
---|
| 509 |
|
---|
| 510 | // draw work break taking
|
---|
| 511 | [taking set];
|
---|
| 512 | [p removeAllPoints];
|
---|
| 513 | end = 360 - (360.0 / work_break_duration * work_break_taking_t - 90);
|
---|
| 514 | if (end <= 90) end=90.1;
|
---|
| 515 | [p appendBezierPathWithArcWithCenter:NSMakePoint(63.5, 63.5) radius:40 startAngle:90 endAngle:end clockwise:YES];
|
---|
| 516 | [p setLineWidth:18];
|
---|
| 517 | [p stroke];
|
---|
| 518 |
|
---|
| 519 | // draw micro pause
|
---|
| 520 | [elapsed set];
|
---|
| 521 | [p removeAllPoints];
|
---|
| 522 | end = 360 - (360.0 / micro_pause_period * micro_pause_t - 90);
|
---|
| 523 | if (end <= 90) end = 90.1;
|
---|
| 524 | [p appendBezierPathWithArcWithCenter:NSMakePoint(63.5, 63.5) radius:17 startAngle:90 endAngle:end clockwise:YES];
|
---|
| 525 | [p setLineWidth:22];
|
---|
| 526 | [p stroke];
|
---|
| 527 |
|
---|
| 528 | // draw micro pause taking
|
---|
| 529 | [taking set];
|
---|
| 530 | [p removeAllPoints];
|
---|
| 531 | end = 360 - (360.0 / micro_pause_duration * micro_pause_taking_t - 90);
|
---|
| 532 | if (end <= 90) end = 90.1;
|
---|
| 533 | [p appendBezierPathWithArcWithCenter:NSMakePoint(63.5, 63.5) radius:17 startAngle:90 endAngle:end clockwise:YES];
|
---|
| 534 | [p setLineWidth:18];
|
---|
| 535 | [p stroke];
|
---|
| 536 |
|
---|
[332] | 537 | // draw session time
|
---|
| 538 | if (draw_dock_badge) {
|
---|
| 539 | static NSImage *badge = nil;
|
---|
| 540 | int session_minutes = (int)session_t / 60;
|
---|
| 541 | if (badge_session_minutes != session_minutes) {
|
---|
| 542 | if (badge != nil) [badge release];
|
---|
| 543 | badge = [[dock_badge badgeOverlayImageForString: [NSString stringWithFormat:@"%d:%02d", session_minutes / 60, session_minutes % 60] insetX: 3 y: 3] retain];
|
---|
| 544 | badge_session_minutes = session_minutes;
|
---|
| 545 | }
|
---|
| 546 | [badge compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
|
---|
| 547 | }
|
---|
| 548 |
|
---|
[346] | 549 | [dock_image unlockFocus];
|
---|
| 550 |
|
---|
| 551 | // and set it in the dock check draw_dock_image one last time ...
|
---|
| 552 | if (draw_dock_image_q) [NSApp setApplicationIconImage:dock_image];
|
---|
[325] | 553 | }
|
---|
| 554 |
|
---|
[370] | 555 | static ProcessSerialNumber frontmostApp;
|
---|
| 556 |
|
---|
[325] | 557 | // done with micro pause or work break
|
---|
| 558 | - (void)endBreak
|
---|
| 559 | {
|
---|
[346] | 560 | [main_window orderOut:NULL];
|
---|
[370] | 561 | if (lock_focus && (frontmostApp.highLongOfPSN != 0 || frontmostApp.lowLongOfPSN != 0)) {
|
---|
| 562 | SetFrontProcess(&frontmostApp);
|
---|
| 563 | frontmostApp.highLongOfPSN = kNoProcess;
|
---|
| 564 | }
|
---|
[346] | 565 | state = s_normal;
|
---|
| 566 | // reset time interval to user's choice
|
---|
| 567 | [self installTimer:sample_interval];
|
---|
[325] | 568 | }
|
---|
| 569 |
|
---|
[370] | 570 | // bring app to front, saving previous app
|
---|
| 571 | - (void)focus
|
---|
| 572 | {
|
---|
| 573 | NSDictionary *activeProcessInfo = [[NSWorkspace sharedWorkspace] activeApplication];
|
---|
| 574 | if (lock_focus) {
|
---|
| 575 | frontmostApp.highLongOfPSN = [[activeProcessInfo objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
|
---|
| 576 | frontmostApp.lowLongOfPSN = [[activeProcessInfo objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
|
---|
| 577 | }
|
---|
| 578 | [self tick: nil];
|
---|
| 579 | [main_window center];
|
---|
| 580 | [main_window orderFrontRegardless];
|
---|
| 581 | }
|
---|
| 582 |
|
---|
[325] | 583 | // display micro_pause window with appropriate widgets and progress bar
|
---|
| 584 | - (void)doMicroPause
|
---|
| 585 | {
|
---|
[346] | 586 | micro_pause_taking_t = 0;
|
---|
| 587 | [status setStringValue:@"Micro Pause"];
|
---|
| 588 | [progress setMaxValue:micro_pause_duration];
|
---|
| 589 | [progress setDoubleValue:micro_pause_taking_t];
|
---|
[325] | 590 | [progress setWarningValue: 1];
|
---|
| 591 | [progress setCriticalValue: micro_pause_duration];
|
---|
[346] | 592 | [postpone setHidden:YES];
|
---|
| 593 | state = s_taking_micro_pause;
|
---|
[370] | 594 | [self focus];
|
---|
[325] | 595 | }
|
---|
| 596 |
|
---|
| 597 | // display work_break window with appropriate widgets and progress bar
|
---|
| 598 | - (void)doWorkBreak
|
---|
| 599 | {
|
---|
[346] | 600 | work_break_taking_t = 0;
|
---|
| 601 | // incase you were already having an implicit work break and clicked the take work break now button
|
---|
| 602 | // not more then 20 seconds ago we took a natural break longer then 0.2 * normal work break duration
|
---|
| 603 | if (date - work_break_taking_cached_date < 20 && work_break_taking_cached_t > work_break_duration * 0.2) {
|
---|
| 604 | work_break_taking_t = work_break_taking_cached_t;
|
---|
| 605 | }
|
---|
| 606 | [status setStringValue:@"Work Break"];
|
---|
| 607 | [progress setMaxValue:work_break_duration / 60];
|
---|
| 608 | [progress setDoubleValue:work_break_taking_t / 60 - 0.5];
|
---|
[325] | 609 | [progress setWarningValue: 0];
|
---|
| 610 | [progress setCriticalValue: 0.4];
|
---|
[346] | 611 | [postpone setHidden:NO];
|
---|
| 612 | state = s_taking_work_break;
|
---|
[370] | 613 | [self focus];
|
---|
[325] | 614 | }
|
---|
| 615 |
|
---|
[330] | 616 | - (NSString *)sessionTimeString;
|
---|
[325] | 617 | {
|
---|
[348] | 618 | int seconds = lrint(session_t);
|
---|
| 619 | return [NSString stringWithFormat:@"Session: %d:%02d:%02d", seconds / 3600,
|
---|
| 620 | (seconds / 60) % 60, seconds % 60];
|
---|
[325] | 621 | }
|
---|
| 622 |
|
---|
[330] | 623 | - (void)updateBreakWindowDuration:(double)duration progress:(double)progress_t nextBreak:(double)nextBreak;
|
---|
[325] | 624 | {
|
---|
[330] | 625 | // progress
|
---|
| 626 | [progress setDoubleValue:duration >= 60 ? (progress_t / 60 - 0.5) : progress_t];
|
---|
| 627 |
|
---|
| 628 | // time left
|
---|
[348] | 629 | int timeLeft = lrint(duration - progress_t);
|
---|
| 630 | [time setStringValue:[NSString stringWithFormat:@"%d:%02d", timeLeft / 60, timeLeft % 60]];
|
---|
[330] | 631 |
|
---|
| 632 | // cumulative typing time in this session (e.g. today)
|
---|
| 633 | [session_time setStringValue:[self sessionTimeString]];
|
---|
| 634 |
|
---|
| 635 | // next break
|
---|
[346] | 636 | int minutes = round(nextBreak / 60.0);
|
---|
| 637 |
|
---|
| 638 | // nice hours, minutes ...
|
---|
| 639 | if (minutes > 60) {
|
---|
| 640 | [next_break setStringValue:[NSString stringWithFormat:@"next break in %d:%02d hours",
|
---|
| 641 | minutes / 60, minutes % 60]];
|
---|
| 642 | } else {
|
---|
| 643 | [next_break setStringValue:[NSString stringWithFormat:@"next break in %d minutes", minutes]];
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[330] | 646 | // if user likes to be interrupted
|
---|
| 647 | if (lock_focus) {
|
---|
| 648 | [NSApp activateIgnoringOtherApps:YES];
|
---|
| 649 | [main_window makeKeyAndOrderFront:self];
|
---|
| 650 | }
|
---|
[325] | 651 | }
|
---|
| 652 |
|
---|
[329] | 653 | // goto website
|
---|
| 654 | - (IBAction)gotoWebsite:(id)sender
|
---|
| 655 | {
|
---|
[346] | 656 | [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:sURL]];
|
---|
[329] | 657 | }
|
---|
| 658 |
|
---|
| 659 | // check for update
|
---|
| 660 | - (IBAction)checkForUpdate:(id)sender
|
---|
| 661 | {
|
---|
[346] | 662 | NSString *latest_version =
|
---|
| 663 | [NSString stringWithContentsOfURL: [NSURL URLWithString:sLatestVersionURL]];
|
---|
| 664 |
|
---|
| 665 | if (latest_version == Nil) latest_version = @"";
|
---|
| 666 | latest_version = [latest_version stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
---|
| 667 |
|
---|
| 668 | if ([latest_version length] == 0) {
|
---|
| 669 | NSRunInformationalAlertPanel(
|
---|
| 670 | @"Unable to Determine",
|
---|
| 671 | @"Unable to determine the latest AntiRSI version number.",
|
---|
| 672 | @"Ok", nil, nil);
|
---|
| 673 | } else if ([latest_version compare:sVersion] == NSOrderedDescending) {
|
---|
| 674 | int r = NSRunInformationalAlertPanel(
|
---|
| 675 | @"New Version",
|
---|
[675] | 676 | @"A new version (%@) of AntiRSI is available; would you like to go to its Web site now?",
|
---|
| 677 | @"Goto Website", @"Cancel", nil, latest_version);
|
---|
[346] | 678 | if (r == NSOKButton) {
|
---|
| 679 | [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:sURL]];
|
---|
| 680 | }
|
---|
[329] | 681 | } else {
|
---|
| 682 | NSRunInformationalAlertPanel(
|
---|
[346] | 683 | @"No Update Available",
|
---|
| 684 | @"This is the latest version of AntiRSI.",
|
---|
| 685 | @"OK", nil, nil);
|
---|
[329] | 686 | }
|
---|
| 687 | }
|
---|
| 688 |
|
---|
[325] | 689 | // stop work break and postpone by 10 minutes
|
---|
| 690 | - (IBAction)postpone:(id)sender
|
---|
| 691 | {
|
---|
[346] | 692 | if (s_taking_work_break == state) {
|
---|
| 693 | micro_pause_t = 0;
|
---|
| 694 | micro_pause_taking_t = 0;
|
---|
| 695 | work_break_taking_t = 0;
|
---|
| 696 | work_break_taking_cached_t = 0;
|
---|
| 697 | work_break_t -= 10*60; // decrease with 10 minutes
|
---|
| 698 | if (work_break_t < 0) work_break_t = 0;
|
---|
| 699 | [self endBreak];
|
---|
| 700 | }
|
---|
[325] | 701 | }
|
---|
| 702 |
|
---|
| 703 | - (IBAction)breakNow:(id)sender
|
---|
| 704 | {
|
---|
[346] | 705 | [self doWorkBreak];
|
---|
[325] | 706 | }
|
---|
| 707 |
|
---|
[330] | 708 | - (IBAction)resetSession:(id)sender;
|
---|
| 709 | {
|
---|
| 710 | if (s_normal != state) {
|
---|
| 711 | [self endBreak];
|
---|
| 712 | }
|
---|
[331] | 713 | [self resetTimers];
|
---|
[330] | 714 | }
|
---|
| 715 |
|
---|
[325] | 716 | // validate menu items
|
---|
| 717 | - (BOOL)validateMenuItem:(NSMenuItem *)anItem
|
---|
| 718 | {
|
---|
[346] | 719 | if ([anItem action] == @selector(breakNow:) && state == s_normal)
|
---|
| 720 | return YES;
|
---|
| 721 |
|
---|
| 722 | if ([anItem action] == @selector(postpone:) && state == s_taking_work_break)
|
---|
| 723 | return YES;
|
---|
| 724 |
|
---|
[330] | 725 | if ([anItem action] == @selector(resetSession:))
|
---|
[346] | 726 | return YES;
|
---|
| 727 |
|
---|
| 728 | if ([anItem action] == @selector(gotoWebsite:))
|
---|
| 729 | return YES;
|
---|
| 730 |
|
---|
[330] | 731 | if ([anItem action] == @selector(checkForUpdate:))
|
---|
[346] | 732 | return YES;
|
---|
| 733 |
|
---|
| 734 | return NO;
|
---|
[325] | 735 | }
|
---|
| 736 |
|
---|
| 737 | // we are delegate of NSApplication, so we can restore the icon on quit.
|
---|
| 738 | - (void)applicationWillTerminate:(NSNotification *)aNotification
|
---|
| 739 | {
|
---|
[346] | 740 | // make sure timer doesn't tick once more ...
|
---|
| 741 | draw_dock_image_q = NO;
|
---|
| 742 | [mtimer invalidate];
|
---|
| 743 | [mtimer autorelease];
|
---|
| 744 | mtimer = nil;
|
---|
| 745 | [dock_image release];
|
---|
| 746 | // stupid fix for icon beeing restored ... it is not my fault,
|
---|
| 747 | // the dock or NSImage or setApplicationIconImage seem to be caching or taking
|
---|
| 748 | // snapshot or something ... !
|
---|
| 749 | [NSApp setApplicationIconImage:original_dock_image];
|
---|
| 750 | [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
---|
| 751 | [NSApp setApplicationIconImage:original_dock_image];
|
---|
| 752 |
|
---|
[325] | 753 | }
|
---|
| 754 |
|
---|
| 755 | @end
|
---|
| 756 |
|
---|