Home | History | Annotate | Download | only in avdlauncher
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*
     18  * The "AVD Manager" is for Windows only.
     19  * This simple .exe will sit at the root of the Windows SDK
     20  * and currently simply executes tools\android.bat.
     21  *
     22  * TODO: replace by a jar-exe wrapper.
     23  */
     24 
     25 #ifdef _WIN32
     26 
     27 #include <stdio.h>
     28 #include <stdarg.h>
     29 #include <string.h>
     30 #include <windows.h>
     31 
     32 
     33 int _enable_dprintf = 0;
     34 
     35 void dprintf(char *msg, ...) {
     36     va_list ap;
     37     va_start(ap, msg);
     38 
     39     if (_enable_dprintf) {
     40         vfprintf(stderr, msg, ap);
     41     }
     42 
     43     va_end(ap);
     44 }
     45 
     46 void display_error(LPSTR description) {
     47     DWORD err = GetLastError();
     48     LPSTR s, s2;
     49 
     50     fprintf(stderr, "%s, error %ld\n", description, err);
     51 
     52     if (FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | /* dwFlags */
     53                       FORMAT_MESSAGE_FROM_SYSTEM,
     54                       NULL,                             /* lpSource */
     55                       err,                              /* dwMessageId */
     56                       0,                                /* dwLanguageId */
     57                       (LPSTR)&s,                        /* lpBuffer */
     58                       0,                                /* nSize */
     59                       NULL) != 0) {                     /* va_list args */
     60         fprintf(stderr, "%s", s);
     61 
     62         s2 = (LPSTR) malloc(strlen(description) + strlen(s) + 5);
     63         sprintf(s2, "%s\r\n%s", description, s);
     64         MessageBox(NULL, s2, "Android AVD Manager - Error", MB_OK);
     65         free(s2);
     66         LocalFree(s);
     67     }
     68 }
     69 
     70 
     71 int avd_launcher() {
     72     int                   result = 0;
     73     STARTUPINFO           startup;
     74     PROCESS_INFORMATION   pinfo;
     75     CHAR                  program_dir[MAX_PATH];
     76     int                   ret, pos;
     77 
     78     ZeroMemory(&pinfo, sizeof(pinfo));
     79 
     80     ZeroMemory(&startup, sizeof(startup));
     81     startup.cb = sizeof(startup);
     82     startup.dwFlags     = STARTF_USESHOWWINDOW;
     83     startup.wShowWindow = SW_HIDE|SW_MINIMIZE;
     84 
     85     /* get path of current program, to switch dirs here when executing the command. */
     86     ret = GetModuleFileName(NULL, program_dir, sizeof(program_dir));
     87     if (ret == 0) {
     88         display_error("Failed to get program's filename:");
     89         result = 1;
     90     } else {
     91         /* Remove the last segment to keep only the directory. */
     92         pos = ret - 1;
     93         while (pos > 0 && program_dir[pos] != '\\') {
     94             --pos;
     95         }
     96         program_dir[pos] = 0;
     97     }
     98 
     99     if (!result) {
    100         dprintf("Program dir: %s\n", program_dir);
    101 
    102         ret = CreateProcess(
    103                 NULL,                                       /* program path */
    104                 "tools\\android.bat avd",                   /* command-line */
    105                 NULL,                  /* process handle is not inheritable */
    106                 NULL,                   /* thread handle is not inheritable */
    107                 TRUE,                          /* yes, inherit some handles */
    108                 CREATE_NO_WINDOW,                /* we don't want a console */
    109                 NULL,                     /* use parent's environment block */
    110                 program_dir,             /* use parent's starting directory */
    111                 &startup,                 /* startup info, i.e. std handles */
    112                 &pinfo);
    113 
    114         dprintf("CreateProcess returned %d\n", ret);
    115 
    116         if (!ret) {
    117             display_error("Failed to execute tools\\android.bat:");
    118             result = 1;
    119         }
    120     }
    121 
    122     dprintf("Cleanup.\n");
    123 
    124     return result;
    125 }
    126 
    127 int main(int argc, char **argv) {
    128     _enable_dprintf = argc > 1 && strcmp(argv[1], "-v") == 0;
    129     dprintf("Verbose debug mode.\n");
    130 
    131     return avd_launcher();
    132 }
    133 
    134 #endif /* _WIN32 */
    135