├── Shake to Undo ├── en.lproj │ ├── InfoPlist.strings │ └── Credits.rtf ├── Icon.icns ├── Shake to Undo-Prefix.pch ├── main.m ├── SUOverlayFrame.h ├── SUOverlayWindow.h ├── SUOverlayView.h ├── SUOverlayButton.h ├── Shake to Undo-Info.plist ├── SUAppDelegate.h ├── SUOverlayView.m ├── SUOverlayWindow.m ├── SUOverlayFrame.m ├── SUOverlayButton.m ├── SUAppDelegate.m ├── smslib.h ├── Menu.xib └── smslib.m ├── .gitignore └── Shake to Undo.xcodeproj └── project.pbxproj /Shake to Undo/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /Shake to Undo/Icon.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/natestedman/shake-to-undo/HEAD/Shake to Undo/Icon.icns -------------------------------------------------------------------------------- /Shake to Undo/Shake to Undo-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'Shake to Undo' target in the 'Shake to Undo' project 3 | // 4 | 5 | #ifdef __OBJC__ 6 | #import 7 | #endif 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # lol os x 2 | .DS_Store 3 | 4 | # vim 5 | *.swp 6 | 7 | # xcode 8 | build/* 9 | *.pbxuser 10 | !default.pbxuser 11 | *.mode1v3 12 | !default.mode1v3 13 | *.mode2v3 14 | !default.mode2v3 15 | *.perspectivev3 16 | !default.perspectivev3 17 | *.xcworkspace 18 | !default.xcworkspace 19 | xcuserdata 20 | profile 21 | *.moved-aside 22 | 23 | -------------------------------------------------------------------------------- /Shake to Undo/main.m: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | int main(int argc, char *argv[]) 19 | { 20 | return NSApplicationMain(argc, (const char **)argv); 21 | } 22 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayFrame.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | #define OVERLAY_BUTTON_PADDING 12.0 19 | 20 | @interface SUOverlayFrame : NSView 21 | { 22 | } 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayWindow.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | @interface SUOverlayWindow : NSWindow 19 | { 20 | } 21 | 22 | -(id)initForScreen:(NSScreen*)screen; 23 | 24 | @end 25 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayView.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | #define OVERLAY_FRAME_WIDTH 325.0 19 | #define OVERLAY_FRAME_HEIGHT 180.0 20 | 21 | @interface SUOverlayView : NSView 22 | { 23 | } 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayButton.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | #define OVERLAY_BUTTON_RADIUS 5.0 19 | #define OVERLAY_FONT_SIZE 30.0 20 | #define OVERLAY_FONT_NAME @"Helvetica Neue Bold" 21 | #define OVERLAY_INSET_OFFSET 2.0 22 | 23 | @interface SUOverlayButton : NSButton 24 | { 25 | } 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /Shake to Undo/Shake to Undo-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | Icon 11 | CFBundleIdentifier 12 | com.natestedman.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSMinimumSystemVersion 26 | ${MACOSX_DEPLOYMENT_TARGET} 27 | NSPrincipalClass 28 | NSApplication 29 | NSMainNibFile 30 | Menu 31 | LSUIElement 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Shake to Undo/SUAppDelegate.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | #import "smslib.h" 19 | 20 | #define ACCEL_THRESHOLD 0.2 21 | #define COUNT_THRESHOLD 40 22 | #define COUNT_PER_SHAKE 13 23 | 24 | @interface SUAppDelegate : NSObject 25 | { 26 | IBOutlet NSMenu* statusMenu; 27 | NSStatusItem* statusItem; 28 | NSTimer* timer; 29 | 30 | sms_acceleration previous; 31 | int shakeCount; 32 | 33 | NSMutableArray* overlayWindows; 34 | NSRunningApplication* mostRecentApp; 35 | } 36 | 37 | -(void)cancelOverlay:(id)sender; 38 | -(void)performUndo:(id)sender; 39 | -(void)timerCallback:(id)sender; 40 | 41 | -(void)appChanged:(NSNotification*)notification; 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayView.m: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import 17 | 18 | #import "SUOverlayView.h" 19 | 20 | #import "SUOverlayFrame.h" 21 | 22 | @implementation SUOverlayView 23 | 24 | -(id)initWithFrame:(NSRect)frameRect 25 | { 26 | self = [super initWithFrame:frameRect]; 27 | 28 | if (self) 29 | { 30 | frameRect.origin.x = frameRect.size.width / 2.0 - OVERLAY_FRAME_WIDTH / 2.0; 31 | frameRect.origin.y = frameRect.size.height / 2.0 - OVERLAY_FRAME_HEIGHT / 2.0; 32 | frameRect.size.width = OVERLAY_FRAME_WIDTH; 33 | frameRect.size.height = OVERLAY_FRAME_HEIGHT; 34 | 35 | NSView* overlayFrame = [[SUOverlayFrame alloc] initWithFrame:frameRect]; 36 | [self addSubview:overlayFrame]; 37 | } 38 | 39 | return self; 40 | } 41 | 42 | -(void)drawRect:(NSRect)dirtyRect 43 | { 44 | [[NSColor colorWithCalibratedWhite:0.0 alpha:0.8] set]; 45 | NSRectFill([self bounds]); 46 | } 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /Shake to Undo/en.lproj/Credits.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360 2 | {\fonttbl\f0\fswiss\fcharset0 Helvetica;} 3 | {\colortbl;\red255\green255\blue255;} 4 | \vieww9600\viewh8400\viewkind0 5 | \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural 6 | 7 | \f0\b\fs24 \cf0 By Nate Stedman\ 8 | \ 9 | SMSLib Sudden Motion Sensor Access Library\ 10 | \pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural 11 | 12 | \b0 \cf0 Copyright (c) 2010 Suitable Systems\ 13 | All rights reserved.\ 14 | \ 15 | Developed by: Daniel Griscom\ 16 | Suitable Systems\ 17 | http://www.suitable.com\ 18 | \ 19 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal with the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\ 20 | \ 21 | - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.\ 22 | \ 23 | - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimers in the documentation and/or other materials provided with the distribution.\ 24 | \ 25 | - Neither the names of Suitable Systems nor the names of its contributors may be used to endorse or promote products derived from this Software without specific prior written permission.\ 26 | \ 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.\ 28 | \ 29 | For more information about SMSLib, see \ 30 | \ 31 | or contact\ 32 | Daniel Griscom\ 33 | Suitable Systems\ 34 | 1 Centre Street, Suite 204\ 35 | Wakefield, MA 01880\ 36 | (781) 665-0053} -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayWindow.m: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import "SUOverlayWindow.h" 17 | 18 | #import "SUOverlayView.h" 19 | 20 | extern void CGSNewConnection(void*, void*); 21 | extern void CGSNewCIFilterByName(void*, CFStringRef, uint32_t*); 22 | extern void CGSSetCIFilterValuesFromDictionary(void*, uint32_t, CFDictionaryRef); 23 | extern void CGSAddWindowFilter(void*, NSInteger, uint32_t, int); 24 | 25 | @implementation SUOverlayWindow 26 | 27 | -(id)initForScreen:(NSScreen*)screen 28 | { 29 | self = [super initWithContentRect:[screen frame] 30 | styleMask:NSBorderlessWindowMask 31 | backing:NSBackingStoreBuffered 32 | defer:NO]; 33 | 34 | if (self) 35 | { 36 | NSView* overlay = [[SUOverlayView alloc] initWithFrame:[[self contentView] bounds]]; 37 | [[self contentView] addSubview:overlay]; 38 | [self setLevel:NSScreenSaverWindowLevel]; 39 | [self setOpaque:NO]; 40 | 41 | // hack in some blur with private API nonsense 42 | void* connection; 43 | uint32_t filter; 44 | 45 | CGSNewConnection(NULL , &connection); 46 | 47 | CGSNewCIFilterByName(connection, 48 | (CFStringRef)@"CIGaussianBlur", 49 | &filter); 50 | NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:2.0] forKey:@"inputRadius"]; 51 | 52 | CGSSetCIFilterValuesFromDictionary(connection, filter, (CFDictionaryRef)dict); 53 | 54 | CGSAddWindowFilter(connection, [self windowNumber], filter, 1); 55 | } 56 | 57 | return self; 58 | } 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayFrame.m: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import "SUOverlayFrame.h" 17 | 18 | #import "SUAppDelegate.h" 19 | #import "SUOverlayButton.h" 20 | 21 | @implementation SUOverlayFrame 22 | 23 | -(id)initWithFrame:(NSRect)frameRect 24 | { 25 | self = [super initWithFrame:frameRect]; 26 | 27 | if (self) 28 | { 29 | NSRect buttonRect = NSInsetRect([self bounds], OVERLAY_BUTTON_PADDING, OVERLAY_BUTTON_PADDING); 30 | buttonRect.size.height = buttonRect.size.height / 2.0 - OVERLAY_BUTTON_PADDING / 2.0; 31 | 32 | NSButton* cancel = [[SUOverlayButton alloc] initWithFrame:buttonRect]; 33 | [cancel setTitle:@"Cancel"]; 34 | [cancel setTarget:[NSApp delegate]]; 35 | [cancel setAction:@selector(cancelOverlay:)]; 36 | 37 | buttonRect.origin.y += buttonRect.size.height + OVERLAY_BUTTON_PADDING; 38 | NSButton* undo = [[SUOverlayButton alloc] initWithFrame:buttonRect]; 39 | [undo setTitle:@"Undo"]; 40 | [undo setTarget:[NSApp delegate]]; 41 | [undo setAction:@selector(performUndo:)]; 42 | 43 | [self addSubview:cancel]; 44 | [self addSubview:undo]; 45 | } 46 | 47 | return self; 48 | } 49 | 50 | -(void)drawRect:(NSRect)dirtyRect 51 | { 52 | NSRect rect = NSInsetRect([self bounds], 1.0, 1.0); 53 | NSBezierPath* bezier = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:10 yRadius:10]; 54 | [bezier setLineWidth:2.0]; 55 | 56 | NSGradient* gradient = [[NSGradient alloc] 57 | initWithStartingColor:[NSColor colorWithCalibratedRed:0.12 green:0.18 blue:0.34 alpha:0.5] 58 | endingColor:[NSColor colorWithCalibratedRed:0.06 green:0.12 blue:0.29 alpha:0.5]]; 59 | [gradient drawInBezierPath:bezier angle:270.0]; 60 | 61 | [[NSColor colorWithCalibratedRed:0.72 green:0.75 blue:0.80 alpha:0.6] set]; 62 | [bezier stroke]; 63 | } 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /Shake to Undo/SUOverlayButton.m: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import "SUOverlayButton.h" 17 | 18 | @implementation SUOverlayButton 19 | 20 | -(void)drawRect:(NSRect)dirtyRect 21 | { 22 | // draw the dark outline 23 | NSRect rect = NSInsetRect([self bounds], 1.0, 1.0); 24 | NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:rect 25 | xRadius:OVERLAY_BUTTON_RADIUS 26 | yRadius:OVERLAY_BUTTON_RADIUS]; 27 | 28 | [[NSColor colorWithCalibratedWhite:0.0 alpha:0.3] set]; 29 | [path stroke]; 30 | 31 | rect.size.height -= 2; 32 | rect.origin.y += 1; 33 | rect.size.width -= 2; 34 | rect.origin.x += 1; 35 | 36 | [[NSColor colorWithCalibratedWhite:1.0 alpha:0.3] set]; 37 | 38 | // draw the light outline 39 | path = [NSBezierPath bezierPathWithRoundedRect:rect 40 | xRadius:OVERLAY_BUTTON_RADIUS - 1.0 41 | yRadius:OVERLAY_BUTTON_RADIUS - 1.0]; 42 | [path stroke]; 43 | 44 | // draw the glossy gradient 45 | NSGradient* gradient = [[NSGradient alloc] 46 | initWithColorsAndLocations: 47 | [NSColor colorWithCalibratedRed:0.47 green:0.51 blue:0.61 alpha:0.3], 0.0, 48 | [NSColor colorWithCalibratedRed:0.21 green:0.27 blue:0.40 alpha:0.3], 0.5, 49 | [NSColor colorWithCalibratedRed:0.06 green:0.12 blue:0.29 alpha:0.3], 0.5, 50 | nil]; 51 | 52 | [gradient drawInBezierPath:path angle:90.0]; 53 | 54 | // prepare for text drawing 55 | NSFont* font = [NSFont fontWithName:OVERLAY_FONT_NAME size:OVERLAY_FONT_SIZE]; 56 | NSColor* color = [NSColor colorWithCalibratedWhite:0.0 alpha:0.7]; 57 | NSDictionary* inset = [NSDictionary dictionaryWithObjectsAndKeys: 58 | font, NSFontAttributeName, color, NSForegroundColorAttributeName, nil]; 59 | color = [NSColor colorWithCalibratedWhite:1.0 alpha:1.0]; 60 | NSDictionary* white = [NSDictionary dictionaryWithObjectsAndKeys: 61 | font, NSFontAttributeName, color, NSForegroundColorAttributeName, nil]; 62 | 63 | NSString* label = [self title]; 64 | NSSize size = [label sizeWithAttributes:white]; 65 | 66 | // draw the inset text 67 | NSPoint position = NSMakePoint(rect.origin.x + rect.size.width / 2.0 - size.width / 2.0, 68 | rect.origin.y + rect.size.height / 2.0 - size.height / 2.0); 69 | position.y -= OVERLAY_INSET_OFFSET; 70 | [label drawAtPoint:position withAttributes:inset]; 71 | 72 | // draw the white text 73 | position.y += OVERLAY_INSET_OFFSET; 74 | [label drawAtPoint:position withAttributes:white]; 75 | } 76 | 77 | @end 78 | -------------------------------------------------------------------------------- /Shake to Undo/SUAppDelegate.m: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011, Nate Stedman 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #import "SUAppDelegate.h" 17 | 18 | #import "SUOverlayWindow.h" 19 | 20 | @implementation SUAppDelegate 21 | 22 | -(void)applicationDidFinishLaunching:(NSNotification*)aNotification 23 | { 24 | [[NSUserDefaults standardUserDefaults] registerDefaults: 25 | [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"showOverlay"]]; 26 | 27 | shakeCount = 0; 28 | overlayWindows = nil; 29 | 30 | // format status item 31 | statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:30.0]; 32 | [statusItem setTitle:@"⌘Z"]; 33 | [statusItem setMenu:statusMenu]; 34 | [statusItem setHighlightMode:YES]; 35 | 36 | // start motion sensor 37 | int code = smsStartup(NULL, NULL); 38 | if (code != SMS_SUCCESS) 39 | { 40 | NSLog(@"SMS error code %d", code); 41 | } 42 | 43 | smsGetData(&previous); 44 | 45 | // repeatedly get sensor data 46 | timer = [NSTimer scheduledTimerWithTimeInterval:0.05 47 | target:self 48 | selector:@selector(timerCallback:) 49 | userInfo:nil 50 | repeats:YES]; 51 | 52 | // get app change notifications to switch back to previous app with overlay 53 | [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self 54 | selector:@selector(appChanged:) 55 | name:NSWorkspaceDidDeactivateApplicationNotification 56 | object:nil]; 57 | } 58 | 59 | -(void)appChanged:(NSNotification*)notification 60 | { 61 | mostRecentApp = [[notification userInfo] valueForKey:NSWorkspaceApplicationKey]; 62 | } 63 | 64 | -(void)cancelOverlay:(id)sender 65 | { 66 | if (overlayWindows) 67 | { 68 | for (NSWindow* window in overlayWindows) 69 | { 70 | [window orderOut:self]; 71 | } 72 | overlayWindows = nil; 73 | } 74 | 75 | [mostRecentApp activateWithOptions:0]; 76 | } 77 | 78 | -(void)performUndo:(id)sender 79 | { 80 | [self cancelOverlay:self]; 81 | 82 | CGEventRef down = CGEventCreateKeyboardEvent(NULL, 6, true); 83 | CGEventRef up = CGEventCreateKeyboardEvent(NULL, 6, false); 84 | 85 | CGEventSetFlags(down, kCGEventFlagMaskCommand); 86 | CGEventSetFlags(up, kCGEventFlagMaskCommand); 87 | 88 | CGEventPost(kCGAnnotatedSessionEventTap, down); 89 | CGEventPost(kCGAnnotatedSessionEventTap, up); 90 | } 91 | 92 | -(void)timerCallback:(id)sender 93 | { 94 | sms_acceleration accel; 95 | smsGetData(&accel); 96 | 97 | float difference = previous.x - accel.x + previous.y - accel.y + previous.z - accel.z; 98 | 99 | if (difference > ACCEL_THRESHOLD) 100 | { 101 | shakeCount += COUNT_PER_SHAKE; 102 | } 103 | 104 | if (shakeCount > COUNT_THRESHOLD) 105 | { 106 | if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"showOverlay"] boolValue]) 107 | { 108 | if (overlayWindows == nil) 109 | { 110 | NSArray* screens = [NSScreen screens]; 111 | overlayWindows = [[NSMutableArray alloc] initWithCapacity:[screens count]]; 112 | 113 | for (NSScreen* screen in screens) 114 | { 115 | NSWindow* window = [[SUOverlayWindow alloc] initForScreen:screen]; 116 | [window makeKeyAndOrderFront:self]; 117 | [overlayWindows addObject:window]; 118 | } 119 | } 120 | } 121 | else 122 | { 123 | [self performUndo:self]; 124 | } 125 | 126 | shakeCount = 0; 127 | } 128 | 129 | if (shakeCount > 0) 130 | { 131 | shakeCount--; 132 | } 133 | 134 | previous = accel; 135 | } 136 | 137 | @end 138 | -------------------------------------------------------------------------------- /Shake to Undo/smslib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * smslib.h 3 | * 4 | * SMSLib Sudden Motion Sensor Access Library 5 | * Copyright (c) 2010 Suitable Systems 6 | * All rights reserved. 7 | * 8 | * Developed by: Daniel Griscom 9 | * Suitable Systems 10 | * http://www.suitable.com 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a 13 | * copy of this software and associated documentation files (the 14 | * "Software"), to deal with the Software without restriction, including 15 | * without limitation the rights to use, copy, modify, merge, publish, 16 | * distribute, sublicense, and/or sell copies of the Software, and to 17 | * permit persons to whom the Software is furnished to do so, subject to 18 | * the following conditions: 19 | * 20 | * - Redistributions of source code must retain the above copyright notice, 21 | * this list of conditions and the following disclaimers. 22 | * 23 | * - Redistributions in binary form must reproduce the above copyright 24 | * notice, this list of conditions and the following disclaimers in the 25 | * documentation and/or other materials provided with the distribution. 26 | * 27 | * - Neither the names of Suitable Systems nor the names of its 28 | * contributors may be used to endorse or promote products derived from 29 | * this Software without specific prior written permission. 30 | * 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 32 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 34 | * IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR 35 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 36 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 37 | * SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. 38 | * 39 | * For more information about SMSLib, see 40 | * 41 | * or contact 42 | * Daniel Griscom 43 | * Suitable Systems 44 | * 1 Centre Street, Suite 204 45 | * Wakefield, MA 01880 46 | * (781) 665-0053 47 | * 48 | */ 49 | 50 | #import 51 | 52 | #define SMSLIB_VERSION "1.8" 53 | 54 | #pragma mark Structure definitions 55 | 56 | // Structure for specifying a 3-axis acceleration. 0.0 means "zero gravities", 57 | // 1.0 means "one gravity". 58 | typedef struct sms_acceleration { 59 | float x; // Right-left acceleration (positive is rightwards) 60 | float y; // Front-rear acceleration (positive is rearwards) 61 | float z; // Up-down acceleration (positive is upwards) 62 | } sms_acceleration; 63 | 64 | // Structure for specifying a calibration. 65 | typedef struct sms_calibration { 66 | float zeros[3]; // Zero points for three axes (X, Y, Z) 67 | float onegs[3]; // One gravity values for three axes 68 | } sms_calibration; 69 | 70 | #pragma mark Return value definitions 71 | 72 | // These are the return values for accelStartup(), giving the 73 | // various stages where the most successful attempt at accessing 74 | // the accelerometer failed. The higher the value, the further along the 75 | // software progressed before failing. The options are: 76 | // - Didn't match model name 77 | #define SMS_FAIL_MODEL (-7) 78 | // - Failure getting dictionary matching desired services 79 | #define SMS_FAIL_DICTIONARY (-6) 80 | // - Failure getting list of services 81 | #define SMS_FAIL_LIST_SERVICES (-5) 82 | // - Failure if list of services is empty. The process generally fails 83 | // here if run on a machine without a Sudden Motion Sensor. 84 | #define SMS_FAIL_NO_SERVICES (-4) 85 | // - Failure if error opening device. 86 | #define SMS_FAIL_OPENING (-3) 87 | // - Failure if opened, but didn't get a connection 88 | #define SMS_FAIL_CONNECTION (-2) 89 | // - Failure if couldn't access connction using given function and size. This 90 | // is where the process would probably fail with a change in Apple's API. 91 | // Driver problems often also cause failures here. 92 | #define SMS_FAIL_ACCESS (-1) 93 | // - Success! 94 | #define SMS_SUCCESS (0) 95 | 96 | #pragma mark Function declarations 97 | 98 | // This starts up the accelerometer code, trying each possible sensor 99 | // specification. Note that for logging purposes it 100 | // takes an object and a selector; the object's selector is then invoked 101 | // with a single NSString as argument giving progress messages. Example 102 | // logging method: 103 | // - (void)logMessage: (NSString *)theString 104 | // which would be used in accelStartup's invocation thusly: 105 | // result = accelStartup(self, @selector(logMessage:)); 106 | // If the object is nil, then no logging is done. Sets calibation from built-in 107 | // value table. Returns ACCEL_SUCCESS for success, and other (negative) 108 | // values for various failures (returns value indicating result of 109 | // most successful trial). 110 | int smsStartup(id logObject, SEL logSelector); 111 | 112 | // This starts up the library in debug mode, ignoring the actual hardware. 113 | // Returned data is in the form of 1Hz sine waves, with the X, Y and Z 114 | // axes 120 degrees out of phase; "calibrated" data has range +/- (1.0/5); 115 | // "uncalibrated" data has range +/- (256/5). X and Y axes centered on 0.0, 116 | // Z axes centered on 1 (calibrated) or 256 (uncalibrated). 117 | // Don't use smsGetBufferLength or smsGetBufferData. Always returns SMS_SUCCESS. 118 | int smsDebugStartup(id logObject, SEL logSelector); 119 | 120 | // Returns the current calibration values. 121 | void smsGetCalibration(sms_calibration *calibrationRecord); 122 | 123 | // Sets the calibration, but does NOT store it as a preference. If the argument 124 | // is nil then the current calibration is set from the built-in value table. 125 | void smsSetCalibration(sms_calibration *calibrationRecord); 126 | 127 | // Stores the current calibration values as a stored preference. 128 | void smsStoreCalibration(void); 129 | 130 | // Loads the stored preference values into the current calibration. 131 | // Returns YES if successful. 132 | BOOL smsLoadCalibration(void); 133 | 134 | // Deletes any stored calibration, and then takes the current calibration values 135 | // from the built-in value table. 136 | void smsDeleteCalibration(void); 137 | 138 | // Fills in the accel record with calibrated acceleration data. Takes 139 | // 1-2ms to return a value. Returns 0 if success, error number if failure. 140 | int smsGetData(sms_acceleration *accel); 141 | 142 | // Fills in the accel record with uncalibrated acceleration data. 143 | // Returns 0 if success, error number if failure. 144 | int smsGetUncalibratedData(sms_acceleration *accel); 145 | 146 | // Returns the length of a raw block of data for the current type of sensor. 147 | int smsGetBufferLength(void); 148 | 149 | // Takes a pointer to accelGetRawLength() bytes; sets those bytes 150 | // to return value from sensor. Make darn sure the buffer length is right! 151 | void smsGetBufferData(char *buffer); 152 | 153 | // This returns an NSString describing the current calibration in 154 | // human-readable form. Also include a description of the machine. 155 | NSString *smsGetCalibrationDescription(void); 156 | 157 | // Shuts down the accelerometer. 158 | void smsShutdown(void); 159 | 160 | -------------------------------------------------------------------------------- /Shake to Undo/Menu.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1060 5 | 10K540 6 | 1306 7 | 1038.36 8 | 461.00 9 | 10 | com.apple.InterfaceBuilder.CocoaPlugin 11 | 1306 12 | 13 | 14 | YES 15 | NSUserDefaultsController 16 | NSMenu 17 | NSMenuItem 18 | NSCustomObject 19 | 20 | 21 | YES 22 | com.apple.InterfaceBuilder.CocoaPlugin 23 | 24 | 25 | YES 26 | 27 | YES 28 | 29 | 30 | 31 | 32 | YES 33 | 34 | NSObject 35 | 36 | 37 | FirstResponder 38 | 39 | 40 | NSApplication 41 | 42 | 43 | SUAppDelegate 44 | 45 | 46 | 47 | 48 | YES 49 | 50 | 51 | Confirmation Overlay 52 | 53 | 2147483647 54 | 55 | NSImage 56 | NSMenuCheckmark 57 | 58 | 59 | NSImage 60 | NSMenuMixedState 61 | 62 | 63 | 64 | 65 | YES 66 | YES 67 | 68 | 69 | 2147483647 70 | 71 | 72 | 73 | 74 | 75 | About Shake to Undo 76 | 77 | 2147483647 78 | 79 | 80 | 81 | 82 | 83 | YES 84 | YES 85 | 86 | 87 | 2147483647 88 | 89 | 90 | 91 | 92 | 93 | Quit 94 | 95 | 2147483647 96 | 97 | 98 | 99 | 100 | 101 | 102 | YES 103 | 104 | 105 | 106 | 107 | YES 108 | 109 | 110 | delegate 111 | 112 | 113 | 114 | 2 115 | 116 | 117 | 118 | terminate: 119 | 120 | 121 | 122 | 9 123 | 124 | 125 | 126 | value: values.showOverlay 127 | 128 | 129 | 130 | 131 | 132 | value: values.showOverlay 133 | value 134 | values.showOverlay 135 | 2 136 | 137 | 138 | 17 139 | 140 | 141 | 142 | statusMenu 143 | 144 | 145 | 146 | 18 147 | 148 | 149 | 150 | orderFrontStandardAboutPanel: 151 | 152 | 153 | 154 | 24 155 | 156 | 157 | 158 | 159 | YES 160 | 161 | 0 162 | 163 | 164 | 165 | 166 | 167 | -2 168 | 169 | 170 | File's Owner 171 | 172 | 173 | -1 174 | 175 | 176 | First Responder 177 | 178 | 179 | -3 180 | 181 | 182 | Application 183 | 184 | 185 | 1 186 | 187 | 188 | App Delegate 189 | 190 | 191 | 3 192 | 193 | 194 | YES 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 4 205 | 206 | 207 | 208 | 209 | 7 210 | 211 | 212 | 213 | 214 | 8 215 | 216 | 217 | 218 | 219 | 10 220 | 221 | 222 | 223 | 224 | 21 225 | 226 | 227 | 228 | 229 | 22 230 | 231 | 232 | 233 | 234 | 235 | 236 | YES 237 | 238 | YES 239 | -1.IBPluginDependency 240 | -2.IBPluginDependency 241 | -3.IBPluginDependency 242 | 1.IBPluginDependency 243 | 21.IBPluginDependency 244 | 22.IBPluginDependency 245 | 3.IBPluginDependency 246 | 4.IBPluginDependency 247 | 7.IBPluginDependency 248 | 8.IBPluginDependency 249 | 250 | 251 | YES 252 | com.apple.InterfaceBuilder.CocoaPlugin 253 | com.apple.InterfaceBuilder.CocoaPlugin 254 | com.apple.InterfaceBuilder.CocoaPlugin 255 | com.apple.InterfaceBuilder.CocoaPlugin 256 | com.apple.InterfaceBuilder.CocoaPlugin 257 | com.apple.InterfaceBuilder.CocoaPlugin 258 | com.apple.InterfaceBuilder.CocoaPlugin 259 | com.apple.InterfaceBuilder.CocoaPlugin 260 | com.apple.InterfaceBuilder.CocoaPlugin 261 | com.apple.InterfaceBuilder.CocoaPlugin 262 | 263 | 264 | 265 | YES 266 | 267 | 268 | 269 | 270 | 271 | YES 272 | 273 | 274 | 275 | 276 | 24 277 | 278 | 279 | 280 | YES 281 | 282 | SUAppDelegate 283 | NSObject 284 | 285 | statusMenu 286 | NSMenu 287 | 288 | 289 | statusMenu 290 | 291 | statusMenu 292 | NSMenu 293 | 294 | 295 | 296 | IBProjectSource 297 | ./Classes/SUAppDelegate.h 298 | 299 | 300 | 301 | 302 | 0 303 | IBCocoaFramework 304 | 305 | com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3 306 | 307 | 308 | YES 309 | 3 310 | 311 | YES 312 | 313 | YES 314 | NSMenuCheckmark 315 | NSMenuMixedState 316 | 317 | 318 | YES 319 | {9, 8} 320 | {7, 2} 321 | 322 | 323 | 324 | 325 | -------------------------------------------------------------------------------- /Shake to Undo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 43302C9213E761950038C188 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43302C9113E761950038C188 /* Cocoa.framework */; }; 11 | 43302C9C13E761950038C188 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 43302C9A13E761950038C188 /* InfoPlist.strings */; }; 12 | 43302C9F13E761950038C188 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302C9E13E761950038C188 /* main.m */; }; 13 | 43302CA213E761950038C188 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 43302CA013E761950038C188 /* Credits.rtf */; }; 14 | 43302CA513E761950038C188 /* SUAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302CA413E761950038C188 /* SUAppDelegate.m */; }; 15 | 43302CB013E7645A0038C188 /* smslib.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302CAF13E7645A0038C188 /* smslib.m */; }; 16 | 43302CB213E76B590038C188 /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43302CB113E76B590038C188 /* IOKit.framework */; }; 17 | 43302CB413E76BFB0038C188 /* Menu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 43302CB313E76BFB0038C188 /* Menu.xib */; }; 18 | 43302CB813E7729C0038C188 /* SUOverlayWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302CB713E7729C0038C188 /* SUOverlayWindow.m */; }; 19 | 43302CBB13E7735D0038C188 /* SUOverlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302CBA13E7735D0038C188 /* SUOverlayView.m */; }; 20 | 43302CBD13E778970038C188 /* Quartz.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 43302CBC13E778970038C188 /* Quartz.framework */; }; 21 | 43302CC013E77A350038C188 /* SUOverlayFrame.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302CBF13E77A340038C188 /* SUOverlayFrame.m */; }; 22 | 43302CC313E77E260038C188 /* SUOverlayButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 43302CC213E77E260038C188 /* SUOverlayButton.m */; }; 23 | 43302CC513E78A070038C188 /* Icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 43302CC413E78A070038C188 /* Icon.icns */; }; 24 | /* End PBXBuildFile section */ 25 | 26 | /* Begin PBXFileReference section */ 27 | 43302C8D13E761950038C188 /* Shake to Undo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Shake to Undo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 28 | 43302C9113E761950038C188 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 29 | 43302C9413E761950038C188 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; 30 | 43302C9513E761950038C188 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; 31 | 43302C9613E761950038C188 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 32 | 43302C9913E761950038C188 /* Shake to Undo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Shake to Undo-Info.plist"; sourceTree = ""; }; 33 | 43302C9B13E761950038C188 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 34 | 43302C9D13E761950038C188 /* Shake to Undo-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Shake to Undo-Prefix.pch"; sourceTree = ""; }; 35 | 43302C9E13E761950038C188 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 36 | 43302CA113E761950038C188 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = ""; }; 37 | 43302CA313E761950038C188 /* SUAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SUAppDelegate.h; sourceTree = ""; }; 38 | 43302CA413E761950038C188 /* SUAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SUAppDelegate.m; sourceTree = ""; }; 39 | 43302CAE13E7645A0038C188 /* smslib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = smslib.h; sourceTree = ""; }; 40 | 43302CAF13E7645A0038C188 /* smslib.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = smslib.m; sourceTree = ""; }; 41 | 43302CB113E76B590038C188 /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = System/Library/Frameworks/IOKit.framework; sourceTree = SDKROOT; }; 42 | 43302CB313E76BFB0038C188 /* Menu.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = Menu.xib; sourceTree = ""; }; 43 | 43302CB613E7729C0038C188 /* SUOverlayWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUOverlayWindow.h; sourceTree = ""; }; 44 | 43302CB713E7729C0038C188 /* SUOverlayWindow.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUOverlayWindow.m; sourceTree = ""; }; 45 | 43302CB913E7735D0038C188 /* SUOverlayView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUOverlayView.h; sourceTree = ""; }; 46 | 43302CBA13E7735D0038C188 /* SUOverlayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUOverlayView.m; sourceTree = ""; }; 47 | 43302CBC13E778970038C188 /* Quartz.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quartz.framework; path = System/Library/Frameworks/Quartz.framework; sourceTree = SDKROOT; }; 48 | 43302CBE13E77A340038C188 /* SUOverlayFrame.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUOverlayFrame.h; sourceTree = ""; }; 49 | 43302CBF13E77A340038C188 /* SUOverlayFrame.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUOverlayFrame.m; sourceTree = ""; }; 50 | 43302CC113E77E260038C188 /* SUOverlayButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUOverlayButton.h; sourceTree = ""; }; 51 | 43302CC213E77E260038C188 /* SUOverlayButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUOverlayButton.m; sourceTree = ""; }; 52 | 43302CC413E78A070038C188 /* Icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Icon.icns; sourceTree = ""; }; 53 | /* End PBXFileReference section */ 54 | 55 | /* Begin PBXFrameworksBuildPhase section */ 56 | 43302C8A13E761950038C188 /* Frameworks */ = { 57 | isa = PBXFrameworksBuildPhase; 58 | buildActionMask = 2147483647; 59 | files = ( 60 | 43302CBD13E778970038C188 /* Quartz.framework in Frameworks */, 61 | 43302CB213E76B590038C188 /* IOKit.framework in Frameworks */, 62 | 43302C9213E761950038C188 /* Cocoa.framework in Frameworks */, 63 | ); 64 | runOnlyForDeploymentPostprocessing = 0; 65 | }; 66 | /* End PBXFrameworksBuildPhase section */ 67 | 68 | /* Begin PBXGroup section */ 69 | 43302C8213E761950038C188 = { 70 | isa = PBXGroup; 71 | children = ( 72 | 43302C9713E761950038C188 /* Shake to Undo */, 73 | 43302C9013E761950038C188 /* Frameworks */, 74 | 43302C8E13E761950038C188 /* Products */, 75 | ); 76 | sourceTree = ""; 77 | }; 78 | 43302C8E13E761950038C188 /* Products */ = { 79 | isa = PBXGroup; 80 | children = ( 81 | 43302C8D13E761950038C188 /* Shake to Undo.app */, 82 | ); 83 | name = Products; 84 | sourceTree = ""; 85 | }; 86 | 43302C9013E761950038C188 /* Frameworks */ = { 87 | isa = PBXGroup; 88 | children = ( 89 | 43302C9113E761950038C188 /* Cocoa.framework */, 90 | 43302CB113E76B590038C188 /* IOKit.framework */, 91 | 43302CBC13E778970038C188 /* Quartz.framework */, 92 | 43302C9313E761950038C188 /* Other Frameworks */, 93 | ); 94 | name = Frameworks; 95 | sourceTree = ""; 96 | }; 97 | 43302C9313E761950038C188 /* Other Frameworks */ = { 98 | isa = PBXGroup; 99 | children = ( 100 | 43302C9413E761950038C188 /* AppKit.framework */, 101 | 43302C9513E761950038C188 /* CoreData.framework */, 102 | 43302C9613E761950038C188 /* Foundation.framework */, 103 | ); 104 | name = "Other Frameworks"; 105 | sourceTree = ""; 106 | }; 107 | 43302C9713E761950038C188 /* Shake to Undo */ = { 108 | isa = PBXGroup; 109 | children = ( 110 | 43302CB513E7728B0038C188 /* Overlay */, 111 | 43302CA313E761950038C188 /* SUAppDelegate.h */, 112 | 43302CA413E761950038C188 /* SUAppDelegate.m */, 113 | 43302CAE13E7645A0038C188 /* smslib.h */, 114 | 43302CAF13E7645A0038C188 /* smslib.m */, 115 | 43302C9813E761950038C188 /* Supporting Files */, 116 | ); 117 | path = "Shake to Undo"; 118 | sourceTree = ""; 119 | }; 120 | 43302C9813E761950038C188 /* Supporting Files */ = { 121 | isa = PBXGroup; 122 | children = ( 123 | 43302CC413E78A070038C188 /* Icon.icns */, 124 | 43302C9913E761950038C188 /* Shake to Undo-Info.plist */, 125 | 43302C9A13E761950038C188 /* InfoPlist.strings */, 126 | 43302C9D13E761950038C188 /* Shake to Undo-Prefix.pch */, 127 | 43302C9E13E761950038C188 /* main.m */, 128 | 43302CA013E761950038C188 /* Credits.rtf */, 129 | 43302CB313E76BFB0038C188 /* Menu.xib */, 130 | ); 131 | name = "Supporting Files"; 132 | sourceTree = ""; 133 | }; 134 | 43302CB513E7728B0038C188 /* Overlay */ = { 135 | isa = PBXGroup; 136 | children = ( 137 | 43302CB613E7729C0038C188 /* SUOverlayWindow.h */, 138 | 43302CB713E7729C0038C188 /* SUOverlayWindow.m */, 139 | 43302CB913E7735D0038C188 /* SUOverlayView.h */, 140 | 43302CBA13E7735D0038C188 /* SUOverlayView.m */, 141 | 43302CBE13E77A340038C188 /* SUOverlayFrame.h */, 142 | 43302CBF13E77A340038C188 /* SUOverlayFrame.m */, 143 | 43302CC113E77E260038C188 /* SUOverlayButton.h */, 144 | 43302CC213E77E260038C188 /* SUOverlayButton.m */, 145 | ); 146 | name = Overlay; 147 | sourceTree = ""; 148 | }; 149 | /* End PBXGroup section */ 150 | 151 | /* Begin PBXNativeTarget section */ 152 | 43302C8C13E761950038C188 /* Shake to Undo */ = { 153 | isa = PBXNativeTarget; 154 | buildConfigurationList = 43302CAB13E761950038C188 /* Build configuration list for PBXNativeTarget "Shake to Undo" */; 155 | buildPhases = ( 156 | 43302C8913E761950038C188 /* Sources */, 157 | 43302C8A13E761950038C188 /* Frameworks */, 158 | 43302C8B13E761950038C188 /* Resources */, 159 | ); 160 | buildRules = ( 161 | ); 162 | dependencies = ( 163 | ); 164 | name = "Shake to Undo"; 165 | productName = "Shake to Undo"; 166 | productReference = 43302C8D13E761950038C188 /* Shake to Undo.app */; 167 | productType = "com.apple.product-type.application"; 168 | }; 169 | /* End PBXNativeTarget section */ 170 | 171 | /* Begin PBXProject section */ 172 | 43302C8413E761950038C188 /* Project object */ = { 173 | isa = PBXProject; 174 | attributes = { 175 | ORGANIZATIONNAME = "Nate Stedman"; 176 | }; 177 | buildConfigurationList = 43302C8713E761950038C188 /* Build configuration list for PBXProject "Shake to Undo" */; 178 | compatibilityVersion = "Xcode 3.2"; 179 | developmentRegion = English; 180 | hasScannedForEncodings = 0; 181 | knownRegions = ( 182 | en, 183 | ); 184 | mainGroup = 43302C8213E761950038C188; 185 | productRefGroup = 43302C8E13E761950038C188 /* Products */; 186 | projectDirPath = ""; 187 | projectRoot = ""; 188 | targets = ( 189 | 43302C8C13E761950038C188 /* Shake to Undo */, 190 | ); 191 | }; 192 | /* End PBXProject section */ 193 | 194 | /* Begin PBXResourcesBuildPhase section */ 195 | 43302C8B13E761950038C188 /* Resources */ = { 196 | isa = PBXResourcesBuildPhase; 197 | buildActionMask = 2147483647; 198 | files = ( 199 | 43302C9C13E761950038C188 /* InfoPlist.strings in Resources */, 200 | 43302CA213E761950038C188 /* Credits.rtf in Resources */, 201 | 43302CB413E76BFB0038C188 /* Menu.xib in Resources */, 202 | 43302CC513E78A070038C188 /* Icon.icns in Resources */, 203 | ); 204 | runOnlyForDeploymentPostprocessing = 0; 205 | }; 206 | /* End PBXResourcesBuildPhase section */ 207 | 208 | /* Begin PBXSourcesBuildPhase section */ 209 | 43302C8913E761950038C188 /* Sources */ = { 210 | isa = PBXSourcesBuildPhase; 211 | buildActionMask = 2147483647; 212 | files = ( 213 | 43302C9F13E761950038C188 /* main.m in Sources */, 214 | 43302CA513E761950038C188 /* SUAppDelegate.m in Sources */, 215 | 43302CB013E7645A0038C188 /* smslib.m in Sources */, 216 | 43302CB813E7729C0038C188 /* SUOverlayWindow.m in Sources */, 217 | 43302CBB13E7735D0038C188 /* SUOverlayView.m in Sources */, 218 | 43302CC013E77A350038C188 /* SUOverlayFrame.m in Sources */, 219 | 43302CC313E77E260038C188 /* SUOverlayButton.m in Sources */, 220 | ); 221 | runOnlyForDeploymentPostprocessing = 0; 222 | }; 223 | /* End PBXSourcesBuildPhase section */ 224 | 225 | /* Begin PBXVariantGroup section */ 226 | 43302C9A13E761950038C188 /* InfoPlist.strings */ = { 227 | isa = PBXVariantGroup; 228 | children = ( 229 | 43302C9B13E761950038C188 /* en */, 230 | ); 231 | name = InfoPlist.strings; 232 | sourceTree = ""; 233 | }; 234 | 43302CA013E761950038C188 /* Credits.rtf */ = { 235 | isa = PBXVariantGroup; 236 | children = ( 237 | 43302CA113E761950038C188 /* en */, 238 | ); 239 | name = Credits.rtf; 240 | sourceTree = ""; 241 | }; 242 | /* End PBXVariantGroup section */ 243 | 244 | /* Begin XCBuildConfiguration section */ 245 | 43302CA913E761950038C188 /* Debug */ = { 246 | isa = XCBuildConfiguration; 247 | buildSettings = { 248 | ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; 249 | GCC_C_LANGUAGE_STANDARD = gnu99; 250 | GCC_OPTIMIZATION_LEVEL = 0; 251 | GCC_PREPROCESSOR_DEFINITIONS = DEBUG; 252 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 253 | GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 254 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 255 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 256 | GCC_WARN_UNUSED_VARIABLE = YES; 257 | MACOSX_DEPLOYMENT_TARGET = 10.6; 258 | ONLY_ACTIVE_ARCH = YES; 259 | SDKROOT = macosx; 260 | }; 261 | name = Debug; 262 | }; 263 | 43302CAA13E761950038C188 /* Release */ = { 264 | isa = XCBuildConfiguration; 265 | buildSettings = { 266 | ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; 267 | GCC_C_LANGUAGE_STANDARD = gnu99; 268 | GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 269 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 270 | GCC_WARN_ABOUT_RETURN_TYPE = YES; 271 | GCC_WARN_UNUSED_VARIABLE = YES; 272 | MACOSX_DEPLOYMENT_TARGET = 10.6; 273 | SDKROOT = macosx; 274 | }; 275 | name = Release; 276 | }; 277 | 43302CAC13E761950038C188 /* Debug */ = { 278 | isa = XCBuildConfiguration; 279 | buildSettings = { 280 | ALWAYS_SEARCH_USER_PATHS = NO; 281 | COPY_PHASE_STRIP = NO; 282 | GCC_DYNAMIC_NO_PIC = NO; 283 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 284 | GCC_ENABLE_OBJC_GC = required; 285 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 286 | GCC_PREFIX_HEADER = "Shake to Undo/Shake to Undo-Prefix.pch"; 287 | INFOPLIST_FILE = "Shake to Undo/Shake to Undo-Info.plist"; 288 | PRODUCT_NAME = "$(TARGET_NAME)"; 289 | WRAPPER_EXTENSION = app; 290 | }; 291 | name = Debug; 292 | }; 293 | 43302CAD13E761950038C188 /* Release */ = { 294 | isa = XCBuildConfiguration; 295 | buildSettings = { 296 | ALWAYS_SEARCH_USER_PATHS = NO; 297 | COPY_PHASE_STRIP = YES; 298 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 299 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 300 | GCC_ENABLE_OBJC_GC = required; 301 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 302 | GCC_PREFIX_HEADER = "Shake to Undo/Shake to Undo-Prefix.pch"; 303 | INFOPLIST_FILE = "Shake to Undo/Shake to Undo-Info.plist"; 304 | PRODUCT_NAME = "$(TARGET_NAME)"; 305 | WRAPPER_EXTENSION = app; 306 | }; 307 | name = Release; 308 | }; 309 | /* End XCBuildConfiguration section */ 310 | 311 | /* Begin XCConfigurationList section */ 312 | 43302C8713E761950038C188 /* Build configuration list for PBXProject "Shake to Undo" */ = { 313 | isa = XCConfigurationList; 314 | buildConfigurations = ( 315 | 43302CA913E761950038C188 /* Debug */, 316 | 43302CAA13E761950038C188 /* Release */, 317 | ); 318 | defaultConfigurationIsVisible = 0; 319 | defaultConfigurationName = Release; 320 | }; 321 | 43302CAB13E761950038C188 /* Build configuration list for PBXNativeTarget "Shake to Undo" */ = { 322 | isa = XCConfigurationList; 323 | buildConfigurations = ( 324 | 43302CAC13E761950038C188 /* Debug */, 325 | 43302CAD13E761950038C188 /* Release */, 326 | ); 327 | defaultConfigurationIsVisible = 0; 328 | }; 329 | /* End XCConfigurationList section */ 330 | }; 331 | rootObject = 43302C8413E761950038C188 /* Project object */; 332 | } 333 | -------------------------------------------------------------------------------- /Shake to Undo/smslib.m: -------------------------------------------------------------------------------- 1 | /* 2 | * smslib.m 3 | * 4 | * SMSLib Sudden Motion Sensor Access Library 5 | * Copyright (c) 2010 Suitable Systems 6 | * All rights reserved. 7 | * 8 | * Developed by: Daniel Griscom 9 | * Suitable Systems 10 | * http://www.suitable.com 11 | * 12 | * Permission is hereby granted, free of charge, to any person obtaining a 13 | * copy of this software and associated documentation files (the 14 | * "Software"), to deal with the Software without restriction, including 15 | * without limitation the rights to use, copy, modify, merge, publish, 16 | * distribute, sublicense, and/or sell copies of the Software, and to 17 | * permit persons to whom the Software is furnished to do so, subject to 18 | * the following conditions: 19 | * 20 | * - Redistributions of source code must retain the above copyright notice, 21 | * this list of conditions and the following disclaimers. 22 | * 23 | * - Redistributions in binary form must reproduce the above copyright 24 | * notice, this list of conditions and the following disclaimers in the 25 | * documentation and/or other materials provided with the distribution. 26 | * 27 | * - Neither the names of Suitable Systems nor the names of its 28 | * contributors may be used to endorse or promote products derived from 29 | * this Software without specific prior written permission. 30 | * 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 32 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 34 | * IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR 35 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 36 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 37 | * SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. 38 | * 39 | * For more information about SMSLib, see 40 | * 41 | * or contact 42 | * Daniel Griscom 43 | * Suitable Systems 44 | * 1 Centre Street, Suite 204 45 | * Wakefield, MA 01880 46 | * (781) 665-0053 47 | * 48 | */ 49 | 50 | #import 51 | #import 52 | #import 53 | #import "smslib.h" 54 | 55 | #pragma mark Internal structures 56 | 57 | // Represents a single axis of a type of sensor. 58 | typedef struct axisStruct { 59 | int enabled; // Non-zero if axis is valid in this sensor 60 | int index; // Location in struct of first byte 61 | int size; // Number of bytes 62 | float zerog; // Value meaning "zero g" 63 | float oneg; // Change in value meaning "increase of one g" 64 | // (can be negative if axis sensor reversed) 65 | } axisStruct; 66 | 67 | // Represents the configuration of a type of sensor. 68 | typedef struct sensorSpec { 69 | char *model; // Prefix of model to be tested 70 | char *name; // Name of device to be read 71 | unsigned int function; // Kernel function index 72 | int recordSize; // Size of record to be sent/received 73 | axisStruct axes[3]; // Description of three axes (X, Y, Z) 74 | } sensorSpec; 75 | 76 | // Configuration of all known types of sensors. The configurations are 77 | // tried in order until one succeeds in returning data. 78 | // All default values are set here, but each axis' zerog and oneg values 79 | // may be changed to saved (calibrated) values. 80 | // 81 | // These values came from SeisMaCalibrate calibration reports. In general I've 82 | // found the following: 83 | // - All Intel-based SMSs have 250 counts per g, centered on 0, but the signs 84 | // are different (and in one case two axes are swapped) 85 | // - PowerBooks and iBooks all have sensors centered on 0, and reading 86 | // 50-53 steps per gravity (but with differing polarities!) 87 | // - PowerBooks and iBooks of the same model all have the same axis polarities 88 | // - PowerBook and iBook access methods are model- and OS version-specific 89 | // 90 | // So, the sequence of tests is: 91 | // - Try model-specific access methods. Note that the test is for a match to the 92 | // beginning of the model name, e.g. the record with model name "MacBook" 93 | // matches computer models "MacBookPro1,2" and "MacBook1,1" (and "" 94 | // matches any model). 95 | // - If no model-specific record's access fails, then try each model-independent 96 | // access method in order, stopping when one works. 97 | static const sensorSpec sensors[] = { 98 | // ****** Model-dependent methods ****** 99 | // The PowerBook5,6 is one of the G4 models that seems to lose 100 | // SMS access until the next reboot. 101 | {"PowerBook5,6", "IOI2CMotionSensor", 21, 60, { 102 | {1, 0, 1, 0, 51.5}, 103 | {1, 1, 1, 0, -51.5}, 104 | {1, 2, 1, 0, -51.5} 105 | } 106 | }, 107 | // The PowerBook5,7 is one of the G4 models that seems to lose 108 | // SMS access until the next reboot. 109 | {"PowerBook5,7", "IOI2CMotionSensor", 21, 60, { 110 | {1, 0, 1, 0, 51.5}, 111 | {1, 1, 1, 0, 51.5}, 112 | {1, 2, 1, 0, 51.5} 113 | } 114 | }, 115 | // Access seems to be reliable on the PowerBook5,8 116 | {"PowerBook5,8", "PMUMotionSensor", 21, 60, { 117 | {1, 0, 1, 0, -51.5}, 118 | {1, 1, 1, 0, 51.5}, 119 | {1, 2, 1, 0, -51.5} 120 | } 121 | }, 122 | // Access seems to be reliable on the PowerBook5,9 123 | {"PowerBook5,9", "PMUMotionSensor", 21, 60, { 124 | {1, 0, 1, 0, 51.5}, 125 | {1, 1, 1, 0, -51.5}, 126 | {1, 2, 1, 0, -51.5} 127 | } 128 | }, 129 | // The PowerBook6,7 is one of the G4 models that seems to lose 130 | // SMS access until the next reboot. 131 | {"PowerBook6,7", "IOI2CMotionSensor", 21, 60, { 132 | {1, 0, 1, 0, 51.5}, 133 | {1, 1, 1, 0, 51.5}, 134 | {1, 2, 1, 0, 51.5} 135 | } 136 | }, 137 | // The PowerBook6,8 is one of the G4 models that seems to lose 138 | // SMS access until the next reboot. 139 | {"PowerBook6,8", "IOI2CMotionSensor", 21, 60, { 140 | {1, 0, 1, 0, 51.5}, 141 | {1, 1, 1, 0, 51.5}, 142 | {1, 2, 1, 0, 51.5} 143 | } 144 | }, 145 | // MacBook Pro Core 2 Duo 17". Note the reversed Y and Z axes. 146 | {"MacBookPro2,1", "SMCMotionSensor", 5, 40, { 147 | {1, 0, 2, 0, 251}, 148 | {1, 2, 2, 0, -251}, 149 | {1, 4, 2, 0, -251} 150 | } 151 | }, 152 | // MacBook Pro Core 2 Duo 15" AND 17" with LED backlight, introduced June '07. 153 | // NOTE! The 17" machines have the signs of their X and Y axes reversed 154 | // from this calibration, but there's no clear way to discriminate between 155 | // the two machines. 156 | {"MacBookPro3,1", "SMCMotionSensor", 5, 40, { 157 | {1, 0, 2, 0, -251}, 158 | {1, 2, 2, 0, 251}, 159 | {1, 4, 2, 0, -251} 160 | } 161 | }, 162 | // ... specs? 163 | {"MacBook5,2", "SMCMotionSensor", 5, 40, { 164 | {1, 0, 2, 0, -251}, 165 | {1, 2, 2, 0, 251}, 166 | {1, 4, 2, 0, -251} 167 | } 168 | }, 169 | // ... specs? 170 | {"MacBookPro5,1", "SMCMotionSensor", 5, 40, { 171 | {1, 0, 2, 0, -251}, 172 | {1, 2, 2, 0, -251}, 173 | {1, 4, 2, 0, 251} 174 | } 175 | }, 176 | // ... specs? 177 | {"MacBookPro5,2", "SMCMotionSensor", 5, 40, { 178 | {1, 0, 2, 0, -251}, 179 | {1, 2, 2, 0, -251}, 180 | {1, 4, 2, 0, 251} 181 | } 182 | }, 183 | // This is speculative, based on a single user's report. Looks like the X and Y axes 184 | // are swapped. This is true for no other known Appple laptop. 185 | {"MacBookPro5,3", "SMCMotionSensor", 5, 40, { 186 | {1, 2, 2, 0, -251}, 187 | {1, 0, 2, 0, -251}, 188 | {1, 4, 2, 0, -251} 189 | } 190 | }, 191 | // ... specs? 192 | {"MacBookPro5,4", "SMCMotionSensor", 5, 40, { 193 | {1, 0, 2, 0, -251}, 194 | {1, 2, 2, 0, -251}, 195 | {1, 4, 2, 0, 251} 196 | } 197 | }, 198 | // ****** Model-independent methods ****** 199 | // Seen once with PowerBook6,8 under system 10.3.9; I suspect 200 | // other G4-based 10.3.* systems might use this 201 | {"", "IOI2CMotionSensor", 24, 60, { 202 | {1, 0, 1, 0, 51.5}, 203 | {1, 1, 1, 0, 51.5}, 204 | {1, 2, 1, 0, 51.5} 205 | } 206 | }, 207 | // PowerBook5,6 , PowerBook5,7 , PowerBook6,7 , PowerBook6,8 208 | // under OS X 10.4.* 209 | {"", "IOI2CMotionSensor", 21, 60, { 210 | {1, 0, 1, 0, 51.5}, 211 | {1, 1, 1, 0, 51.5}, 212 | {1, 2, 1, 0, 51.5} 213 | } 214 | }, 215 | // PowerBook5,8 , PowerBook5,9 under OS X 10.4.* 216 | {"", "PMUMotionSensor", 21, 60, { 217 | // Each has two out of three gains negative, but it's different 218 | // for the different models. So, this will be right in two out 219 | // of three axis for either model. 220 | {1, 0, 1, 0, -51.5}, 221 | {1, 1, 1, -6, -51.5}, 222 | {1, 2, 1, 0, -51.5} 223 | } 224 | }, 225 | // All MacBook, MacBookPro models. Hardware (at least on early MacBookPro 15") 226 | // is Kionix KXM52-1050 three-axis accelerometer chip. Data is at 227 | // http://kionix.com/Product-Index/product-index.htm. Specific MB and MBP models 228 | // that use this are: 229 | // MacBook1,1 230 | // MacBook2,1 231 | // MacBook3,1 232 | // MacBook4,1 233 | // MacBook5,1 234 | // MacBook6,1 235 | // MacBookAir1,1 236 | // MacBookPro1,1 237 | // MacBookPro1,2 238 | // MacBookPro4,1 239 | // MacBookPro5,5 240 | {"", "SMCMotionSensor", 5, 40, { 241 | {1, 0, 2, 0, 251}, 242 | {1, 2, 2, 0, 251}, 243 | {1, 4, 2, 0, 251} 244 | } 245 | } 246 | }; 247 | 248 | #define SENSOR_COUNT (sizeof(sensors)/sizeof(sensorSpec)) 249 | 250 | #pragma mark Internal prototypes 251 | 252 | static int getData(sms_acceleration *accel, int calibrated, id logObject, SEL logSelector); 253 | static float getAxis(int which, int calibrated); 254 | static int signExtend(int value, int size); 255 | static NSString *getModelName(void); 256 | static NSString *getOSVersion(void); 257 | static BOOL loadCalibration(void); 258 | static void storeCalibration(void); 259 | static void defaultCalibration(void); 260 | static void deleteCalibration(void); 261 | static int prefIntRead(NSString *prefName, BOOL *success); 262 | static void prefIntWrite(NSString *prefName, int prefValue); 263 | static float prefFloatRead(NSString *prefName, BOOL *success); 264 | static void prefFloatWrite(NSString *prefName, float prefValue); 265 | static void prefDelete(NSString *prefName); 266 | static void prefSynchronize(void); 267 | static long getMicroseconds(void); 268 | float fakeData(NSTimeInterval time); 269 | 270 | #pragma mark Static variables 271 | 272 | static int debugging = NO; // True if debugging (synthetic data) 273 | static io_connect_t connection; // Connection for reading accel values 274 | static int running = NO; // True if we successfully started 275 | static int sensorNum = 0; // The current index into sensors[] 276 | static char *serviceName; // The name of the current service 277 | static char *iRecord, *oRecord; // Pointers to read/write records for sensor 278 | static int recordSize; // Size of read/write records 279 | static unsigned int function; // Which kernel function should be used 280 | static float zeros[3]; // X, Y and Z zero calibration values 281 | static float onegs[3]; // X, Y and Z one-g calibration values 282 | 283 | #pragma mark Defines 284 | 285 | // Pattern for building axis letter from axis number 286 | #define INT_TO_AXIS(a) (a == 0 ? @"X" : a == 1 ? @"Y" : @"Z") 287 | // Name of configuration for given axis' zero (axis specified by integer) 288 | #define ZERO_NAME(a) [NSString stringWithFormat:@"%@-Axis-Zero", INT_TO_AXIS(a)] 289 | // Name of configuration for given axis' oneg (axis specified by integer) 290 | #define ONEG_NAME(a) [NSString stringWithFormat:@"%@-Axis-One-g", INT_TO_AXIS(a)] 291 | // Name of "Is calibrated" preference 292 | #define CALIBRATED_NAME (@"Calibrated") 293 | // Application domain for SeisMac library 294 | #define APP_ID ((CFStringRef)@"com.suitable.SeisMacLib") 295 | 296 | // These #defines make the accelStartup code a LOT easier to read. 297 | #define LOG(message) \ 298 | if (logObject) { \ 299 | [logObject performSelector:logSelector withObject:message]; \ 300 | } 301 | #define LOG_ARG(format, var1) \ 302 | if (logObject) { \ 303 | [logObject performSelector:logSelector \ 304 | withObject:[NSString stringWithFormat:format, var1]]; \ 305 | } 306 | #define LOG_2ARG(format, var1, var2) \ 307 | if (logObject) { \ 308 | [logObject performSelector:logSelector \ 309 | withObject:[NSString stringWithFormat:format, var1, var2]]; \ 310 | } 311 | #define LOG_3ARG(format, var1, var2, var3) \ 312 | if (logObject) { \ 313 | [logObject performSelector:logSelector \ 314 | withObject:[NSString stringWithFormat:format, var1, var2, var3]]; \ 315 | } 316 | 317 | #pragma mark Function definitions 318 | 319 | // This starts up the accelerometer code, trying each possible sensor 320 | // specification. Note that for logging purposes it 321 | // takes an object and a selector; the object's selector is then invoked 322 | // with a single NSString as argument giving progress messages. Example 323 | // logging method: 324 | // - (void)logMessage: (NSString *)theString 325 | // which would be used in accelStartup's invocation thusly: 326 | // result = accelStartup(self, @selector(logMessage:)); 327 | // If the object is nil, then no logging is done. Sets calibation from built-in 328 | // value table. Returns ACCEL_SUCCESS for success, and other (negative) 329 | // values for various failures (returns value indicating result of 330 | // most successful trial). 331 | int smsStartup(id logObject, SEL logSelector) { 332 | io_iterator_t iterator; 333 | io_object_t device; 334 | kern_return_t result; 335 | sms_acceleration accel; 336 | int failure_result = SMS_FAIL_MODEL; 337 | 338 | running = NO; 339 | debugging = NO; 340 | 341 | NSString *modelName = getModelName(); 342 | 343 | LOG_ARG(@"Machine model: %@\n", modelName); 344 | LOG_ARG(@"OS X version: %@\n", getOSVersion()); 345 | LOG_ARG(@"Accelerometer library version: %s\n", SMSLIB_VERSION); 346 | 347 | for (sensorNum = 0; sensorNum < SENSOR_COUNT; sensorNum++) { 348 | 349 | // Set up all specs for this type of sensor 350 | serviceName = sensors[sensorNum].name; 351 | recordSize = sensors[sensorNum].recordSize; 352 | function = sensors[sensorNum].function; 353 | 354 | LOG_3ARG(@"Trying service \"%s\" with selector %d and %d byte record:\n", 355 | serviceName, function, recordSize); 356 | 357 | NSString *targetName = [NSString stringWithCString:sensors[sensorNum].model 358 | encoding:NSMacOSRomanStringEncoding]; 359 | LOG_ARG(@" Comparing model name to target \"%@\": ", targetName); 360 | if ([targetName length] == 0 || [modelName hasPrefix:targetName]) { 361 | LOG(@"success.\n"); 362 | } else { 363 | LOG(@"failure.\n"); 364 | // Don't need to increment failure_result. 365 | continue; 366 | } 367 | 368 | LOG(@" Fetching dictionary for service: "); 369 | CFMutableDictionaryRef dict = IOServiceMatching(serviceName); 370 | 371 | if (dict) { 372 | LOG(@"success.\n"); 373 | } else { 374 | LOG(@"failure.\n"); 375 | if (failure_result < SMS_FAIL_DICTIONARY) { 376 | failure_result = SMS_FAIL_DICTIONARY; 377 | } 378 | continue; 379 | } 380 | 381 | LOG(@" Getting list of matching services: "); 382 | result = IOServiceGetMatchingServices(kIOMasterPortDefault, 383 | dict, 384 | &iterator); 385 | 386 | if (result == KERN_SUCCESS) { 387 | LOG(@"success.\n"); 388 | } else { 389 | LOG_ARG(@"failure, with return value 0x%x.\n", result); 390 | if (failure_result < SMS_FAIL_LIST_SERVICES) { 391 | failure_result = SMS_FAIL_LIST_SERVICES; 392 | } 393 | continue; 394 | } 395 | 396 | LOG(@" Getting first device in list: "); 397 | device = IOIteratorNext(iterator); 398 | 399 | if (device == 0) { 400 | LOG(@"failure.\n"); 401 | if (failure_result < SMS_FAIL_NO_SERVICES) { 402 | failure_result = SMS_FAIL_NO_SERVICES; 403 | } 404 | continue; 405 | } else { 406 | LOG(@"success.\n"); 407 | LOG(@" Opening device: "); 408 | } 409 | 410 | result = IOServiceOpen(device, mach_task_self(), 0, &connection); 411 | 412 | if (result != KERN_SUCCESS) { 413 | LOG_ARG(@"failure, with return value 0x%x.\n", result); 414 | IOObjectRelease(device); 415 | if (failure_result < SMS_FAIL_OPENING) { 416 | failure_result = SMS_FAIL_OPENING; 417 | } 418 | continue; 419 | } else if (connection == 0) { 420 | LOG_ARG(@"'success', but didn't get a connection.\n", result); 421 | IOObjectRelease(device); 422 | if (failure_result < SMS_FAIL_CONNECTION) { 423 | failure_result = SMS_FAIL_CONNECTION; 424 | } 425 | continue; 426 | } else { 427 | IOObjectRelease(device); 428 | LOG(@"success.\n"); 429 | } 430 | LOG(@" Testing device.\n"); 431 | 432 | defaultCalibration(); 433 | 434 | iRecord = malloc(recordSize); 435 | oRecord = malloc(recordSize); 436 | 437 | running = YES; 438 | result = getData(&accel, true, logObject, logSelector); 439 | running = NO; 440 | 441 | if (result) { 442 | LOG_ARG(@" Failure testing device, with result 0x%x.\n", result); 443 | free(iRecord); 444 | iRecord = 0; 445 | free(oRecord); 446 | oRecord = 0; 447 | if (failure_result < SMS_FAIL_ACCESS) { 448 | failure_result = SMS_FAIL_ACCESS; 449 | } 450 | continue; 451 | } else { 452 | LOG(@" Success testing device!\n"); 453 | running = YES; 454 | return SMS_SUCCESS; 455 | } 456 | } 457 | return failure_result; 458 | } 459 | 460 | // This starts up the library in debug mode, ignoring the actual hardware. 461 | // Returned data is in the form of 1Hz sine waves, with the X, Y and Z 462 | // axes 120 degrees out of phase; "calibrated" data has range +/- (1.0/5); 463 | // "uncalibrated" data has range +/- (256/5). X and Y axes centered on 0.0, 464 | // Z axes centered on 1 (calibrated) or 256 (uncalibrated). 465 | // Don't use smsGetBufferLength or smsGetBufferData. Always returns SMS_SUCCESS. 466 | int smsDebugStartup(id logObject, SEL logSelector) { 467 | LOG(@"Starting up in debug mode\n"); 468 | debugging = YES; 469 | return SMS_SUCCESS; 470 | } 471 | 472 | // Returns the current calibration values. 473 | void smsGetCalibration(sms_calibration *calibrationRecord) { 474 | int x; 475 | 476 | for (x = 0; x < 3; x++) { 477 | calibrationRecord->zeros[x] = (debugging ? 0 : zeros[x]); 478 | calibrationRecord->onegs[x] = (debugging ? 256 : onegs[x]); 479 | } 480 | } 481 | 482 | // Sets the calibration, but does NOT store it as a preference. If the argument 483 | // is nil then the current calibration is set from the built-in value table. 484 | void smsSetCalibration(sms_calibration *calibrationRecord) { 485 | int x; 486 | 487 | if (!debugging) { 488 | if (calibrationRecord) { 489 | for (x = 0; x < 3; x++) { 490 | zeros[x] = calibrationRecord->zeros[x]; 491 | onegs[x] = calibrationRecord->onegs[x]; 492 | } 493 | } else { 494 | defaultCalibration(); 495 | } 496 | } 497 | } 498 | 499 | // Stores the current calibration values as a stored preference. 500 | void smsStoreCalibration(void) { 501 | if (!debugging) 502 | storeCalibration(); 503 | } 504 | 505 | // Loads the stored preference values into the current calibration. 506 | // Returns YES if successful. 507 | BOOL smsLoadCalibration(void) { 508 | if (debugging) { 509 | return YES; 510 | } else if (loadCalibration()) { 511 | return YES; 512 | } else { 513 | defaultCalibration(); 514 | return NO; 515 | } 516 | } 517 | 518 | // Deletes any stored calibration, and then takes the current calibration values 519 | // from the built-in value table. 520 | void smsDeleteCalibration(void) { 521 | if (!debugging) { 522 | deleteCalibration(); 523 | defaultCalibration(); 524 | } 525 | } 526 | 527 | // Fills in the accel record with calibrated acceleration data. Takes 528 | // 1-2ms to return a value. Returns 0 if success, error number if failure. 529 | int smsGetData(sms_acceleration *accel) { 530 | NSTimeInterval time; 531 | if (debugging) { 532 | usleep(1500); // Usually takes 1-2 milliseconds 533 | time = [NSDate timeIntervalSinceReferenceDate]; 534 | accel->x = fakeData(time)/5; 535 | accel->y = fakeData(time - 1)/5; 536 | accel->z = fakeData(time - 2)/5 + 1.0; 537 | return true; 538 | } else { 539 | return getData(accel, true, nil, nil); 540 | } 541 | } 542 | 543 | // Fills in the accel record with uncalibrated acceleration data. 544 | // Returns 0 if success, error number if failure. 545 | int smsGetUncalibratedData(sms_acceleration *accel) { 546 | NSTimeInterval time; 547 | if (debugging) { 548 | usleep(1500); // Usually takes 1-2 milliseconds 549 | time = [NSDate timeIntervalSinceReferenceDate]; 550 | accel->x = fakeData(time) * 256 / 5; 551 | accel->y = fakeData(time - 1) * 256 / 5; 552 | accel->z = fakeData(time - 2) * 256 / 5 + 256; 553 | return true; 554 | } else { 555 | return getData(accel, false, nil, nil); 556 | } 557 | } 558 | 559 | // Returns the length of a raw block of data for the current type of sensor. 560 | int smsGetBufferLength(void) { 561 | if (debugging) { 562 | return 0; 563 | } else if (running) { 564 | return sensors[sensorNum].recordSize; 565 | } else { 566 | return 0; 567 | } 568 | } 569 | 570 | // Takes a pointer to accelGetRawLength() bytes; sets those bytes 571 | // to return value from sensor. Make darn sure the buffer length is right! 572 | void smsGetBufferData(char *buffer) { 573 | IOItemCount iSize = recordSize; 574 | IOByteCount oSize = recordSize; 575 | kern_return_t result; 576 | 577 | if (debugging || running == NO) { 578 | return; 579 | } 580 | 581 | memset(iRecord, 1, iSize); 582 | memset(buffer, 0, oSize); 583 | #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 584 | const size_t InStructSize = recordSize; 585 | size_t OutStructSize = recordSize; 586 | result = IOConnectCallStructMethod(connection, 587 | function, // magic kernel function number 588 | (const void *)iRecord, 589 | InStructSize, 590 | (void *)buffer, 591 | &OutStructSize 592 | ); 593 | #else // __MAC_OS_X_VERSION_MIN_REQUIRED 1050 594 | result = IOConnectMethodStructureIStructureO(connection, 595 | function, // magic kernel function number 596 | iSize, 597 | &oSize, 598 | iRecord, 599 | buffer 600 | ); 601 | #endif // __MAC_OS_X_VERSION_MIN_REQUIRED 1050 602 | 603 | if (result != KERN_SUCCESS) { 604 | running = NO; 605 | } 606 | } 607 | 608 | // This returns an NSString describing the current calibration in 609 | // human-readable form. Also include a description of the machine. 610 | NSString *smsGetCalibrationDescription(void) { 611 | BOOL success; 612 | NSMutableString *s = [[NSMutableString alloc] init]; 613 | 614 | if (debugging) { 615 | [s release]; 616 | return @"Debugging!"; 617 | } 618 | 619 | [s appendString:@"---- SeisMac Calibration Record ----\n \n"]; 620 | [s appendFormat:@"Machine model: %@\n", 621 | getModelName()]; 622 | [s appendFormat:@"OS X build: %@\n", 623 | getOSVersion()]; 624 | [s appendFormat:@"SeisMacLib version %s, record %d\n \n", 625 | SMSLIB_VERSION, sensorNum]; 626 | [s appendFormat:@"Using service \"%s\", function index %d, size %d\n \n", 627 | serviceName, function, recordSize]; 628 | if (prefIntRead(CALIBRATED_NAME, &success) && success) { 629 | [s appendString:@"Calibration values (from calibration):\n"]; 630 | } else { 631 | [s appendString:@"Calibration values (from defaults):\n"]; 632 | } 633 | [s appendFormat:@" X-Axis-Zero = %.2f\n", zeros[0]]; 634 | [s appendFormat:@" X-Axis-One-g = %.2f\n", onegs[0]]; 635 | [s appendFormat:@" Y-Axis-Zero = %.2f\n", zeros[1]]; 636 | [s appendFormat:@" Y-Axis-One-g = %.2f\n", onegs[1]]; 637 | [s appendFormat:@" Z-Axis-Zero = %.2f\n", zeros[2]]; 638 | [s appendFormat:@" Z-Axis-One-g = %.2f\n \n", onegs[2]]; 639 | [s appendString:@"---- End Record ----\n"]; 640 | return s; 641 | } 642 | 643 | // Shuts down the accelerometer. 644 | void smsShutdown(void) { 645 | if (!debugging) { 646 | running = NO; 647 | if (iRecord) free(iRecord); 648 | if (oRecord) free(oRecord); 649 | IOServiceClose(connection); 650 | } 651 | } 652 | 653 | #pragma mark Internal functions 654 | 655 | // Loads the current calibration from the stored preferences. 656 | // Returns true iff successful. 657 | BOOL loadCalibration(void) { 658 | BOOL thisSuccess, allSuccess; 659 | int x; 660 | 661 | prefSynchronize(); 662 | 663 | if (prefIntRead(CALIBRATED_NAME, &thisSuccess) && thisSuccess) { 664 | // Calibrated. Set all values from saved values. 665 | allSuccess = YES; 666 | for (x = 0; x < 3; x++) { 667 | zeros[x] = prefFloatRead(ZERO_NAME(x), &thisSuccess); 668 | allSuccess &= thisSuccess; 669 | onegs[x] = prefFloatRead(ONEG_NAME(x), &thisSuccess); 670 | allSuccess &= thisSuccess; 671 | } 672 | return allSuccess; 673 | } 674 | 675 | return NO; 676 | } 677 | 678 | // Stores the current calibration into the stored preferences. 679 | static void storeCalibration(void) { 680 | int x; 681 | prefIntWrite(CALIBRATED_NAME, 1); 682 | for (x = 0; x < 3; x++) { 683 | prefFloatWrite(ZERO_NAME(x), zeros[x]); 684 | prefFloatWrite(ONEG_NAME(x), onegs[x]); 685 | } 686 | prefSynchronize(); 687 | } 688 | 689 | 690 | // Sets the calibration to its default values. 691 | void defaultCalibration(void) { 692 | int x; 693 | for (x = 0; x < 3; x++) { 694 | zeros[x] = sensors[sensorNum].axes[x].zerog; 695 | onegs[x] = sensors[sensorNum].axes[x].oneg; 696 | } 697 | } 698 | 699 | // Deletes the stored preferences. 700 | static void deleteCalibration(void) { 701 | int x; 702 | 703 | prefDelete(CALIBRATED_NAME); 704 | for (x = 0; x < 3; x++) { 705 | prefDelete(ZERO_NAME(x)); 706 | prefDelete(ONEG_NAME(x)); 707 | } 708 | prefSynchronize(); 709 | } 710 | 711 | // Read a named floating point value from the stored preferences. Sets 712 | // the success boolean based on, you guessed it, whether it succeeds. 713 | static float prefFloatRead(NSString *prefName, BOOL *success) { 714 | float result = 0.0f; 715 | 716 | CFPropertyListRef ref = CFPreferencesCopyAppValue((CFStringRef)prefName, 717 | APP_ID); 718 | // If there isn't such a preference, fail 719 | if (ref == NULL) { 720 | *success = NO; 721 | return result; 722 | } 723 | CFTypeID typeID = CFGetTypeID(ref); 724 | // Is it a number? 725 | if (typeID == CFNumberGetTypeID()) { 726 | // Is it a floating point number? 727 | if (CFNumberIsFloatType((CFNumberRef)ref)) { 728 | // Yup: grab it. 729 | *success = CFNumberGetValue(ref, kCFNumberFloat32Type, &result); 730 | } else { 731 | // Nope: grab as an integer, and convert to a float. 732 | long num; 733 | if (CFNumberGetValue((CFNumberRef)ref, kCFNumberLongType, &num)) { 734 | result = num; 735 | *success = YES; 736 | } else { 737 | *success = NO; 738 | } 739 | } 740 | // Or is it a string (e.g. set by the command line "defaults" command)? 741 | } else if (typeID == CFStringGetTypeID()) { 742 | result = (float)CFStringGetDoubleValue((CFStringRef)ref); 743 | *success = YES; 744 | } else { 745 | // Can't convert to a number: fail. 746 | *success = NO; 747 | } 748 | CFRelease(ref); 749 | return result; 750 | } 751 | 752 | // Writes a named floating point value to the stored preferences. 753 | static void prefFloatWrite(NSString *prefName, float prefValue) { 754 | CFNumberRef cfFloat = CFNumberCreate(kCFAllocatorDefault, 755 | kCFNumberFloatType, 756 | &prefValue); 757 | CFPreferencesSetAppValue((CFStringRef)prefName, 758 | cfFloat, 759 | APP_ID); 760 | CFRelease(cfFloat); 761 | } 762 | 763 | // Reads a named integer value from the stored preferences. 764 | static int prefIntRead(NSString *prefName, BOOL *success) { 765 | Boolean internalSuccess; 766 | CFIndex result = CFPreferencesGetAppIntegerValue((CFStringRef)prefName, 767 | APP_ID, 768 | &internalSuccess); 769 | *success = internalSuccess; 770 | 771 | return (int)result; 772 | } 773 | 774 | // Writes a named integer value to the stored preferences. 775 | static void prefIntWrite(NSString *prefName, int prefValue) { 776 | CFPreferencesSetAppValue((CFStringRef)prefName, 777 | (CFNumberRef)[NSNumber numberWithInt:prefValue], 778 | APP_ID); 779 | } 780 | 781 | // Deletes the named preference values. 782 | static void prefDelete(NSString *prefName) { 783 | CFPreferencesSetAppValue((CFStringRef)prefName, 784 | NULL, 785 | APP_ID); 786 | } 787 | 788 | // Synchronizes the local preferences with the stored preferences. 789 | static void prefSynchronize(void) { 790 | CFPreferencesAppSynchronize(APP_ID); 791 | } 792 | 793 | // Internal version of accelGetData, with logging 794 | int getData(sms_acceleration *accel, int calibrated, id logObject, SEL logSelector) { 795 | IOItemCount iSize = recordSize; 796 | IOByteCount oSize = recordSize; 797 | kern_return_t result; 798 | 799 | if (running == NO) { 800 | return -1; 801 | } 802 | 803 | memset(iRecord, 1, iSize); 804 | memset(oRecord, 0, oSize); 805 | 806 | LOG_2ARG(@" Querying device: ", 807 | sensors[sensorNum].function, sensors[sensorNum].recordSize); 808 | 809 | #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 810 | const size_t InStructSize = recordSize; 811 | size_t OutStructSize = recordSize; 812 | result = IOConnectCallStructMethod(connection, 813 | function, // magic kernel function number 814 | (const void *)iRecord, 815 | InStructSize, 816 | (void *)oRecord, 817 | &OutStructSize 818 | ); 819 | #else // __MAC_OS_X_VERSION_MIN_REQUIRED 1050 820 | result = IOConnectMethodStructureIStructureO(connection, 821 | function, // magic kernel function number 822 | iSize, 823 | &oSize, 824 | iRecord, 825 | oRecord 826 | ); 827 | #endif // __MAC_OS_X_VERSION_MIN_REQUIRED 1050 828 | 829 | if (result != KERN_SUCCESS) { 830 | LOG(@"failed.\n"); 831 | running = NO; 832 | return result; 833 | } else { 834 | LOG(@"succeeded.\n"); 835 | 836 | accel->x = getAxis(0, calibrated); 837 | accel->y = getAxis(1, calibrated); 838 | accel->z = getAxis(2, calibrated); 839 | return 0; 840 | } 841 | } 842 | 843 | // Given the returned record, extracts the value of the given axis. If 844 | // calibrated, then zero G is 0.0, and one G is 1.0. 845 | float getAxis(int which, int calibrated) { 846 | // Get various values (to make code cleaner) 847 | int indx = sensors[sensorNum].axes[which].index; 848 | int size = sensors[sensorNum].axes[which].size; 849 | float zerog = zeros[which]; 850 | float oneg = onegs[which]; 851 | // Storage for value to be returned 852 | int value = 0; 853 | 854 | // Although the values in the returned record should have the proper 855 | // endianness, we still have to get it into the proper end of value. 856 | #if (BYTE_ORDER == BIG_ENDIAN) 857 | // On PowerPC processors 858 | memcpy(((char *)&value) + (sizeof(int) - size), &oRecord[indx], size); 859 | #endif 860 | #if (BYTE_ORDER == LITTLE_ENDIAN) 861 | // On Intel processors 862 | memcpy(&value, &oRecord[indx], size); 863 | #endif 864 | 865 | value = signExtend(value, size); 866 | 867 | if (calibrated) { 868 | // Scale and shift for zero. 869 | return ((float)(value - zerog)) / oneg; 870 | } else { 871 | return value; 872 | } 873 | } 874 | 875 | // Extends the sign, given the length of the value. 876 | int signExtend(int value, int size) { 877 | // Extend sign 878 | switch (size) { 879 | case 1: 880 | if (value & 0x00000080) 881 | value |= 0xffffff00; 882 | break; 883 | case 2: 884 | if (value & 0x00008000) 885 | value |= 0xffff0000; 886 | break; 887 | case 3: 888 | if (value & 0x00800000) 889 | value |= 0xff000000; 890 | break; 891 | } 892 | return value; 893 | } 894 | 895 | // Returns the model name of the computer (e.g. "MacBookPro1,1") 896 | NSString *getModelName(void) { 897 | char model[32]; 898 | size_t len = sizeof(model); 899 | int name[2] = {CTL_HW, HW_MODEL}; 900 | NSString *result; 901 | 902 | if (sysctl(name, 2, &model, &len, NULL, 0) == 0) { 903 | result = [NSString stringWithFormat:@"%s", model]; 904 | } else { 905 | result = @""; 906 | } 907 | 908 | return result; 909 | } 910 | 911 | // Returns the current OS X version and build (e.g. "10.4.7 (build 8J2135a)") 912 | NSString *getOSVersion(void) { 913 | NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: 914 | @"/System/Library/CoreServices/SystemVersion.plist"]; 915 | NSString *versionString = [dict objectForKey:@"ProductVersion"]; 916 | NSString *buildString = [dict objectForKey:@"ProductBuildVersion"]; 917 | NSString *wholeString = [NSString stringWithFormat:@"%@ (build %@)", 918 | versionString, buildString]; 919 | return wholeString; 920 | } 921 | 922 | // Returns time within the current second in microseconds. 923 | long getMicroseconds() { 924 | struct timeval t; 925 | gettimeofday(&t, 0); 926 | return t.tv_usec; 927 | } 928 | 929 | // Returns fake data given the time. Range is +/-1. 930 | float fakeData(NSTimeInterval time) { 931 | long secs = lround(floor(time)); 932 | long secsMod3 = secs % 3l; 933 | double angle = time * 10 * M_PI * 2; 934 | double mag = exp(-(time - (secs - secsMod3)) * 2); 935 | return sin(angle) * mag; 936 | } 937 | 938 | --------------------------------------------------------------------------------