changeset 28:2c8d987416b3

Quasimode now takes a url or file path for the UI as its argument.
author Atul Varma <avarma@mozilla.com>
date Mon, 12 Apr 2010 10:55:46 -0700
parents 5b881055e83d
children 8615b4b2de0c
files Quasimode.m
diffstat 1 files changed, 36 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Quasimode.m	Mon Apr 12 10:16:36 2010 -0700
+++ b/Quasimode.m	Mon Apr 12 10:55:46 2010 -0700
@@ -1,6 +1,7 @@
 #import <Foundation/NSAutoreleasePool.h>
 #import <Foundation/NSURL.h>
 #import <Foundation/NSURLRequest.h>
+#import <Foundation/NSFileManager.h>
 #import <AppKit/NSApplication.h>
 #import <AppKit/NSColor.h>
 #import <WebKit/WebFrame.h>
@@ -89,19 +90,48 @@
 
 int main(int argc, const char *argv[])
 {
+  if (argc < 2) {
+    printf("usage: %s <url-or-path-to-html>\n", argv[0]);
+    return 1;
+  }
+
+  int retval = 0;
+  CGEventFlags quasimodeKey = kCGEventFlagMaskAlternate;
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
   [NSApplication sharedApplication];
 
   QuasimodalEventTap *tap = [[QuasimodalEventTap alloc]
                               initWithName: @"keyboard"
-                              quasimodeKey: kCGEventFlagMaskAlternate];
+                              quasimodeKey: quasimodeKey];
+
+  NSString *arg = [NSString stringWithUTF8String: argv[1]];
+  NSURL *url = [NSURL URLWithString: arg];
+  if (![url scheme]) {
+    arg = [[arg stringByExpandingTildeInPath] stringByStandardizingPath];
+    NSFileManager *fileManager = [NSFileManager defaultManager];
 
-  NSURL *url = [NSURL URLWithString: @"http://127.0.0.1:8000"];
-  Quasimode *app = [[Quasimode alloc] initWithEventSource: @"keyboard"
-                                      url: url];
+    if ([fileManager fileExistsAtPath: arg])
+      url = [NSURL fileURLWithPath: arg];
+    else {
+      printf("File \"%s\" does not exist, nor does it appear "
+             "to be a URL.\n", [arg UTF8String]);
+      retval = 1;
+    }
+  }
 
-  [NSApp run];
+  if (retval == 0) {
+    printf("Creating quasimode with key 0x%x and UI at %s.\n",
+           quasimodeKey,
+           [[url absoluteString] UTF8String]);
+
+    Quasimode *app = [[Quasimode alloc] initWithEventSource: @"keyboard"
+                                        url: url];
 
-  return 0;
+    [NSApp run];
+  }
+
+  [pool release];
+
+  return retval;
 }