Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit 30fccb9

Browse files
committed
initial update for windows
1 parent db96884 commit 30fccb9

File tree

6 files changed

+267
-15
lines changed

6 files changed

+267
-15
lines changed

latency-benchmark.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'src/server.c',
1111
'src/oculus.cpp',
1212
'src/oculus.h',
13+
'src/getopt.c',
1314
'src/clioptions.c',
1415
'src/clioptions.h',
1516
'<(INTERMEDIATE_DIR)/packaged-html-files.c',

src/clioptions.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#include <stdlib.h>
2-
#include <unistd.h>
32
#include "clioptions.h"
43

4+
extern char *optarg;
5+
extern int optind;
6+
extern char optopt;
7+
int getopt(int, char **, char *);
8+
59
clioptions* parse_commandline(int argc, const char **argv) {
610
clioptions *options = (clioptions *) malloc(sizeof(clioptions));
711

@@ -14,7 +18,7 @@ clioptions* parse_commandline(int argc, const char **argv) {
1418
int c;
1519

1620
//TODO: use getopt_long for better looking cli args
17-
while ((c = getopt(argc, argv, "ab:d:r:e:")) != -1) {
21+
while ((c = getopt(argc, (char **)argv, "ab:d:r:e:")) != -1) {
1822
switch(c) {
1923
case 'a':
2024
options->automated = true;
@@ -28,11 +32,12 @@ clioptions* parse_commandline(int argc, const char **argv) {
2832
case 'e':
2933
options->browser_args = optarg;
3034
break;
31-
case ':':
35+
case ':':
3236
fprintf(stderr,
3337
"Option -%c requires an operand\n", optopt);
3438
break;
3539
case '?':
40+
printf("JMAHER: %c", optopt);
3641
fprintf(stderr,
3742
"Unrecognized option: '-%c'\n", optopt);
3843
}

src/clioptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <stdio.h>
22
#include <string.h>
3-
#include <stdbool.h>
3+
4+
#define false 0
5+
#define true 1
6+
#define bool int
47

58
typedef struct {
69
bool automated; // is this an automated run, shutdown the browser when done

src/getopt.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// http://docs.freeswitch.org/getopt_8c-source.html
2+
/*****************************************************************************
3+
*
4+
* MODULE NAME : GETOPT.C
5+
*
6+
* COPYRIGHTS:
7+
* This module contains code made available by IBM
8+
* Corporation on an AS IS basis. Any one receiving the
9+
* module is considered to be licensed under IBM copyrights
10+
* to use the IBM-provided source code in any way he or she
11+
* deems fit, including copying it, compiling it, modifying
12+
* it, and redistributing it, with or without
13+
* modifications. No license under any IBM patents or
14+
* patent applications is to be implied from this copyright
15+
* license.
16+
*
17+
* A user of the module should understand that IBM cannot
18+
* provide technical support for the module and will not be
19+
* responsible for any consequences of use of the program.
20+
*
21+
* Any notices, including this one, are not to be removed
22+
* from the module without the prior written consent of
23+
* IBM.
24+
*
25+
* AUTHOR: Original author:
26+
* G. R. Blair (BOBBLAIR at AUSVM1)
27+
* Internet: bobblair@bobblair.austin.ibm.com
28+
*
29+
* Extensively revised by:
30+
* John Q. Walker II, Ph.D. (JOHHQ at RALVM6)
31+
* Internet: johnq@ralvm6.vnet.ibm.com
32+
*
33+
*****************************************************************************/
34+
35+
/******************************************************************************
36+
* getopt()
37+
*
38+
* The getopt() function is a command line parser. It returns the next
39+
* option character in argv that matches an option character in opstring.
40+
*
41+
* The argv argument points to an array of argc+1 elements containing argc
42+
* pointers to character strings followed by a null pointer.
43+
*
44+
* The opstring argument points to a string of option characters; if an
45+
* option character is followed by a colon, the option is expected to have
46+
* an argument that may or may not be separated from it by white space.
47+
* The external variable optarg is set to point to the start of the option
48+
* argument on return from getopt().
49+
*
50+
* The getopt() function places in optind the argv index of the next argument
51+
* to be processed. The system initializes the external variable optind to
52+
* 1 before the first call to getopt().
53+
*
54+
* When all options have been processed (that is, up to the first nonoption
55+
* argument), getopt() returns EOF. The special option "--" may be used to
56+
* delimit the end of the options; EOF will be returned, and "--" will be
57+
* skipped.
58+
*
59+
* The getopt() function returns a question mark (?) when it encounters an
60+
* option character not included in opstring. This error message can be
61+
* disabled by setting opterr to zero. Otherwise, it returns the option
62+
* character that was detected.
63+
*
64+
* If the special option "--" is detected, or all options have been
65+
* processed, EOF is returned.
66+
*
67+
* Options are marked by either a minus sign (-) or a slash (/).
68+
*
69+
* No errors are defined.
70+
*****************************************************************************/
71+
72+
#include <stdio.h> /* for EOF */
73+
#include <string.h> /* for strchr() */
74+
75+
/* static (global) variables that are specified as exported by getopt() */
76+
char *optarg = NULL; /* pointer to the start of the option argument */
77+
int optind = 1; /* number of the next argv[] to be evaluated */
78+
int opterr = 1; /* non-zero if a question mark should be returned
79+
when a non-valid option character is detected */
80+
char optopt = NULL; /* value of the last character we matched */
81+
82+
/* handle possible future character set concerns by putting this in a macro */
83+
#define _next_char(string) (char)(*(string+1))
84+
85+
int getopt(int argc, char **argv, char *opstring)
86+
{
87+
static char *pIndexPosition = NULL; /* place inside current argv string */
88+
char *pArgString = NULL; /* where to start from next */
89+
char *pOptString; /* the string in our program */
90+
91+
92+
if (pIndexPosition != NULL) {
93+
/* we last left off inside an argv string */
94+
if (*(++pIndexPosition)) {
95+
/* there is more to come in the most recent argv */
96+
pArgString = pIndexPosition;
97+
}
98+
}
99+
100+
if (pArgString == NULL) {
101+
/* we didn't leave off in the middle of an argv string */
102+
if (optind >= argc) {
103+
/* more command-line arguments than the argument count */
104+
pIndexPosition = NULL; /* not in the middle of anything */
105+
return EOF; /* used up all command-line arguments */
106+
}
107+
108+
/*---------------------------------------------------------------------
109+
* If the next argv[] is not an option, there can be no more options.
110+
*-------------------------------------------------------------------*/
111+
pArgString = argv[optind++]; /* set this to the next argument ptr */
112+
113+
if (('/' != *pArgString) && /* doesn't start with a slash or a dash? */
114+
('-' != *pArgString)) {
115+
--optind; /* point to current arg once we're done */
116+
optarg = NULL; /* no argument follows the option */
117+
pIndexPosition = NULL; /* not in the middle of anything */
118+
return EOF; /* used up all the command-line flags */
119+
}
120+
121+
/* check for special end-of-flags markers */
122+
if ((strcmp(pArgString, "-") == 0) ||
123+
(strcmp(pArgString, "--") == 0)) {
124+
optarg = NULL; /* no argument follows the option */
125+
pIndexPosition = NULL; /* not in the middle of anything */
126+
return EOF; /* encountered the special flag */
127+
}
128+
129+
pArgString++; /* look past the / or - */
130+
}
131+
132+
if (':' == *pArgString) { /* is it a colon? */
133+
/*---------------------------------------------------------------------
134+
* Rare case: if opterr is non-zero, return a question mark;
135+
* otherwise, just return the colon we're on.
136+
*-------------------------------------------------------------------*/
137+
optopt = *pArgString;
138+
return (opterr ? (int)'?' : (int)':');
139+
}
140+
else if ((pOptString = strchr(opstring, *pArgString)) == 0) {
141+
/*---------------------------------------------------------------------
142+
* The letter on the command-line wasn't any good.
143+
*-------------------------------------------------------------------*/
144+
optarg = NULL; /* no argument follows the option */
145+
pIndexPosition = NULL; /* not in the middle of anything */
146+
optopt = *pArgString;
147+
return (opterr ? (int)'?' : (int)*pArgString);
148+
}
149+
else {
150+
/*---------------------------------------------------------------------
151+
* The letter on the command-line matches one we expect to see
152+
*-------------------------------------------------------------------*/
153+
if (':' == _next_char(pOptString)) { /* is the next letter a colon? */
154+
/* It is a colon. Look for an argument string. */
155+
if ('\0' != _next_char(pArgString)) { /* argument in this argv? */
156+
optarg = &pArgString[1]; /* Yes, it is */
157+
}
158+
else {
159+
/*-------------------------------------------------------------
160+
* The argument string must be in the next argv.
161+
* But, what if there is none (bad input from the user)?
162+
* In that case, return the letter, and optarg as NULL.
163+
*-----------------------------------------------------------*/
164+
if (optind < argc)
165+
optarg = argv[optind++];
166+
else {
167+
optarg = NULL;
168+
optopt = *pArgString;
169+
return (opterr ? (int)'?' : (int)*pArgString);
170+
}
171+
}
172+
pIndexPosition = NULL; /* not in the middle of anything */
173+
}
174+
else {
175+
/* it's not a colon, so just return the letter */
176+
optarg = NULL; /* no argument follows the option */
177+
pIndexPosition = pArgString; /* point to the letter we're on */
178+
}
179+
return (int)*pArgString; /* return the letter that matched */
180+
}
181+
}

src/win/main.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#include "stdafx.h"
1818
#include "../latency-benchmark.h"
1919
#include "../screenscraper.h"
20+
#include "../clioptions.h"
2021

21-
void run_server(void);
22+
void run_server(clioptions*);
2223

2324
static BOOL (APIENTRY *wglSwapIntervalEXT)(int) = 0;
2425
static HGLRC context = NULL;
@@ -177,7 +178,34 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
177178
// Prevent automatic DPI scaling.
178179
SetProcessDPIAware();
179180

180-
if (__argc == 1) {
181+
clioptions *opts = parse_commandline(__argc, (const char **)__argv);
182+
if (opts == NULL) {
183+
return 1;
184+
}
185+
186+
int cliopts = 1;
187+
if (opts->automated)
188+
cliopts++;
189+
190+
if (opts->browser == NULL) {
191+
opts->browser = "";
192+
} else {
193+
cliopts += 2;
194+
}
195+
196+
if (opts->results == NULL) {
197+
opts->results = "";
198+
} else {
199+
cliopts += 2;
200+
}
201+
202+
if (opts->browser_args == NULL) {
203+
opts->browser_args = "";
204+
} else {
205+
cliopts += 2;
206+
}
207+
208+
if (__argc == cliopts) {
181209
debug_log("running server");
182210
HDC desktop = GetDC(NULL);
183211
assert(desktop);
@@ -188,21 +216,23 @@ int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
188216
return 1;
189217
}
190218
ReleaseDC(NULL, desktop);
191-
run_server();
219+
run_server(opts);
192220
}
221+
free(opts);
222+
193223
debug_log("opening native reference window");
194-
if (__argc != 3) {
195-
debug_log("Unrecognized number of arguments");
224+
if (__argc != (cliopts+2)) {
225+
debug_log("Unrecognized number of arguments: %d", __argc);
196226
return 1;
197227
}
198228
// The first argument is the magic pattern to draw on the window, encoded as hex.
199229
memset(pattern, 0, sizeof(pattern));
200-
if (!parse_hex_magic_pattern(__argv[1], pattern)) {
230+
if (!parse_hex_magic_pattern(__argv[cliopts], pattern)) {
201231
debug_log("Failed to parse pattern");
202232
return 1;
203233
}
204234
// The second argument is the handle of the parent process.
205-
HANDLE parent_process = (HANDLE)_strtoui64(__argv[2], NULL, 16);
235+
HANDLE parent_process = (HANDLE)_strtoui64(__argv[cliopts+1], NULL, 16);
206236
message_loop(parent_process);
207237
return 0;
208238
}

src/win/screenscraper.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,46 @@ int usleep(unsigned int microseconds) {
368368
return 0;
369369
}
370370

371+
HANDLE browser_process_handle = NULL;
371372

372-
bool open_browser(const char *url) {
373+
bool open_browser(const char *program, const char *args, const char *url) {
373374
// Recommended by:
374375
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
375-
HRESULT hr =
376+
377+
if (program == NULL || strcmp(program, "") == 0) {
378+
HRESULT hr =
376379
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
377-
assert(hr == S_OK);
378-
return 32 < (int)ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
380+
assert(hr == S_OK);
381+
return 32 < (int)ShellExecuteA(NULL, "open", url, args, NULL, SW_SHOWNORMAL);
382+
}
383+
384+
PROCESS_INFORMATION process_info;
385+
STARTUPINFO startup_info;
386+
memset(&startup_info, 0, sizeof(startup_info));
387+
startup_info.cb = sizeof(startup_info);
388+
char command_line[4096];
389+
sprintf_s(command_line, sizeof(command_line), "%s %s %s", program, args, url);
390+
if (!CreateProcess(NULL, command_line, NULL, NULL, TRUE, 0, NULL, NULL,
391+
&startup_info, &process_info)) {
392+
debug_log("Failed to start process");
393+
return false;
394+
}
395+
browser_process_handle = process_info.hProcess;
396+
return true;
397+
}
398+
399+
bool close_browser() {
400+
if (browser_process_handle == NULL) {
401+
debug_log("browser not open");
402+
return false;
403+
}
404+
if (!TerminateProcess(browser_process_handle, 0)) {
405+
debug_log("Failed to terminate process");
406+
browser_process_handle = NULL;
407+
return false;
408+
}
409+
browser_process_handle = NULL;
410+
return true;
379411
}
380412

381413
HANDLE window_process_handle = NULL;

0 commit comments

Comments
 (0)