Parse SampleApp command line for a test name. If an argument is passed to
SampleApp on the command line, interpret it as a test name.  If it's a valid
test, open that test at startup.

Review URL:  http://codereview.appspot.com/4661054/



git-svn-id: http://skia.googlecode.com/svn/trunk@1742 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/ios/SkUIView.mm b/gpu/src/ios/SkUIView.mm
index 8cd6c77..58550ae 100644
--- a/gpu/src/ios/SkUIView.mm
+++ b/gpu/src/ios/SkUIView.mm
@@ -52,7 +52,7 @@
 
 static float gScreenScale = 1;
 
-extern SkOSWindow* create_sk_window(void* hwnd);
+extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
 
 #define kREDRAW_UIVIEW_GL "sk_redraw_uiview_gl_iOS"
 
@@ -155,7 +155,8 @@
     fBackend = kGL_Backend;
     fUseWarp = false;
     fRedrawRequestPending = false;
-    fWind = create_sk_window(self);
+    // FIXME:  If iOS has argc & argv, pass them here.
+    fWind = create_sk_window(self, 0, NULL);
     fWind->setConfig(SKWIND_CONFIG);
     fMatrix.reset();
     fLocalMatrix.reset();
diff --git a/include/views/SkApplication.h b/include/views/SkApplication.h
index 4c4a4fb..093f679 100644
--- a/include/views/SkApplication.h
+++ b/include/views/SkApplication.h
@@ -19,7 +19,7 @@
 
 class SkOSWindow;
 
-extern SkOSWindow* create_sk_window(void* hwnd);
+extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);
 extern void application_init();
 extern void application_term();
 
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index d561c85..a5987f2 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -347,7 +347,7 @@
     return gCT[ct];
 }
 
-SampleWindow::SampleWindow(void* hwnd) : INHERITED(hwnd) {
+SampleWindow::SampleWindow(void* hwnd, int argc, char** argv) : INHERITED(hwnd) {
 #ifdef  PIPE_FILE
     //Clear existing file or create file if it doesn't exist
     FILE* f = fopen(FILE_PATH, "wb");
@@ -404,6 +404,19 @@
         }
     }
     fCurrIndex = 0;
+    if (argc > 1) {
+        int i, count = fSamples.count();
+        for (i = 0; i < count; i++) {
+            SkString title = getSampleTitle(i);
+            if (title.equals(argv[1])) {
+                fCurrIndex = i;
+                break;
+            }
+        }
+        if (i == count) {
+            fprintf(stderr, "Unknown sample \"%s\"\n", argv[1]);
+        }
+    }
     this->loadView(fSamples[fCurrIndex]());
 
     // If another constructor set our dimensions, ensure that our
@@ -1673,9 +1686,9 @@
     }
 }
 
-SkOSWindow* create_sk_window(void* hwnd) {
+SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
 //    test();
-    return new SampleWindow(hwnd);
+    return new SampleWindow(hwnd, argc, argv);
 }
 
 void get_preferred_size(int* x, int* y, int* width, int* height) {
diff --git a/samplecode/SampleApp.h b/samplecode/SampleApp.h
index 9c39ca4..515a27d 100644
--- a/samplecode/SampleApp.h
+++ b/samplecode/SampleApp.h
@@ -44,7 +44,7 @@
 class SampleWindow : public SkOSWindow {
     SkTDArray<SkViewFactory> fSamples;
 public:
-    SampleWindow(void* hwnd);
+    SampleWindow(void* hwnd, int argc, char** argv);
     virtual ~SampleWindow();
 
     virtual void draw(SkCanvas* canvas);
diff --git a/src/utils/mac/skia_mac.cpp b/src/utils/mac/skia_mac.cpp
index a1345cf..653e663 100644
--- a/src/utils/mac/skia_mac.cpp
+++ b/src/utils/mac/skia_mac.cpp
@@ -1,43 +1,43 @@
-#include <Carbon/Carbon.h>

-#include "SkApplication.h"

-#include "SkWindow.h"

-

-int main(int argc, char* argv[])

-{

-    WindowRef			window;

-    OSStatus			err = noErr;

-

-    Rect bounds = {100, 100, 500, 500};

-    WindowAttributes attrs = kWindowStandardHandlerAttribute | 

-                             kWindowLiveResizeAttribute |

-                             kWindowInWindowMenuAttribute | 

-                             kWindowCompositingAttribute |

-                             kWindowAsyncDragAttribute | 

-                             kWindowFullZoomAttribute | 

-                             kWindowFrameworkScaledAttribute;

-                             //kWindowDoesNotCycleAttribute;

-    CreateNewWindow(kDocumentWindowClass, attrs, &bounds, &window);

-

-    MenuRef menu;

-    CreateNewMenu(0, 0, &menu);

-

-    // if we get here, we can start our normal Skia sequence

-    {

-        application_init();

-        (void)create_sk_window(window);

-        SizeWindow(window, 640, 480, false);

-    }

-    

-    // The window was created hidden so show it.

-    ShowWindow( window );

-    

-    // Call the event loop

-    RunApplicationEventLoop();

-

-	application_term();

-

-CantCreateWindow:

-CantGetNibRef:

-	return err;

-}

-

+#include <Carbon/Carbon.h>
+#include "SkApplication.h"
+#include "SkWindow.h"
+
+int main(int argc, char* argv[])
+{
+    WindowRef			window;
+    OSStatus			err = noErr;
+
+    Rect bounds = {100, 100, 500, 500};
+    WindowAttributes attrs = kWindowStandardHandlerAttribute | 
+                             kWindowLiveResizeAttribute |
+                             kWindowInWindowMenuAttribute | 
+                             kWindowCompositingAttribute |
+                             kWindowAsyncDragAttribute | 
+                             kWindowFullZoomAttribute | 
+                             kWindowFrameworkScaledAttribute;
+                             //kWindowDoesNotCycleAttribute;
+    CreateNewWindow(kDocumentWindowClass, attrs, &bounds, &window);
+
+    MenuRef menu;
+    CreateNewMenu(0, 0, &menu);
+
+    // if we get here, we can start our normal Skia sequence
+    {
+        application_init();
+        (void)create_sk_window(window, argc, argv);
+        SizeWindow(window, 640, 480, false);
+    }
+    
+    // The window was created hidden so show it.
+    ShowWindow( window );
+    
+    // Call the event loop
+    RunApplicationEventLoop();
+
+	application_term();
+
+CantCreateWindow:
+CantGetNibRef:
+	return err;
+}
+
diff --git a/src/utils/win/skia_win.cpp b/src/utils/win/skia_win.cpp
index 5ef1f7a..c7aa9fd 100644
--- a/src/utils/win/skia_win.cpp
+++ b/src/utils/win/skia_win.cpp
@@ -9,7 +9,7 @@
 

 // Forward declarations of functions included in this code module:

 ATOM                MyRegisterClass(HINSTANCE hInstance);

-BOOL                InitInstance(HINSTANCE, int);

+BOOL                InitInstance(HINSTANCE, int, LPTSTR);

 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);

 INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

 

@@ -19,7 +19,6 @@
                      int       nCmdShow)

 {

     UNREFERENCED_PARAMETER(hPrevInstance);

-    UNREFERENCED_PARAMETER(lpCmdLine);

 

     MSG msg;

 

@@ -27,7 +26,7 @@
     MyRegisterClass(hInstance);

 

     // Perform application initialization:

-    if (!InitInstance (hInstance, nCmdShow))

+    if (!InitInstance (hInstance, nCmdShow, lpCmdLine))

     {

         return FALSE;

     }

@@ -82,12 +81,24 @@
 }

 

 #include "SkOSWindow_Win.h"

-extern SkOSWindow* create_sk_window(void* hwnd);

+extern SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv);

 

 static SkOSWindow* gSkWind;

 

+char* tchar_to_utf8(const TCHAR* str) {

+#ifdef _UNICODE

+    int size = WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), NULL, 0, NULL, NULL);

+    char* str8 = (char*) malloc(size+1);

+    WideCharToMultiByte(CP_UTF8, 0, str, wcslen(str), str8, size, NULL, NULL);

+    str8[size] = '\0';

+    return str8;

+#else

+    return strdup(str);

+#endif

+}

+

 //

-//   FUNCTION: InitInstance(HINSTANCE, int)

+//   FUNCTION: InitInstance(HINSTANCE, int, LPTSTR)

 //

 //   PURPOSE: Saves instance handle and creates main window

 //

@@ -96,7 +107,9 @@
 //        In this function, we save the instance handle in a global variable and

 //        create and display the main program window.

 //

-BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

+

+

+BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPTSTR lpCmdLine)

 {

    HWND hWnd;

 

@@ -110,7 +123,21 @@
       return FALSE;

    }

 

-   gSkWind = create_sk_window(hWnd);

+   char* argv[4096];

+   int argc = 0;

+   TCHAR exename[1024], *next;

+   int exenameLen = GetModuleFileName(NULL, exename, 1024);

+   argv[argc++] = tchar_to_utf8(exename);

+   TCHAR* arg = _tcstok_s(lpCmdLine, _T(" "), &next);

+   while (arg != NULL) {

+      argv[argc++] = tchar_to_utf8(arg);

+      arg = _tcstok_s(NULL, _T(" "), &next);

+   }

+

+   gSkWind = create_sk_window(hWnd, argc, argv);

+   for (int i = 0; i < argc; ++i) {

+      free(argv[i]);

+   }

 

    ShowWindow(hWnd, nCmdShow);

    UpdateWindow(hWnd);

diff --git a/unix_test_app/main.cpp b/unix_test_app/main.cpp
index 9dafb3c..21de4d2 100644
--- a/unix_test_app/main.cpp
+++ b/unix_test_app/main.cpp
@@ -16,10 +16,10 @@
     SkEvent::ServiceQueueTimer();
 }
 
-int main(){
+int main(int argc, char** argv){
     signal(SIGALRM, catch_alarm);
 
-    gWindow = create_sk_window(NULL);
+    gWindow = create_sk_window(NULL, argc, argv);
     // Start normal Skia sequence
     application_init();