1 /*
2 * Copyright (C) 2011 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 #undef HAVE_MALLOC_H
17 #include <SDL.h>
18 #include <SDL_syswm.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include "libOpenglRender/render_api.h"
22 #include <EventInjector.h>
23
24 static int convert_keysym(int sym); // forward
25
26 #ifdef __linux__
27 #include <X11/Xlib.h>
28 #endif
29 #ifdef _WIN32
30
31 #include <winsock2.h>
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)32 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
33 #else
34 int main(int argc, char *argv[])
35 #endif
36 {
37 int portNum = 22468;
38 int winWidth = 320;
39 int winHeight = 480;
40 int width, height;
41 int mouseDown = 0;
42 const char* env = getenv("ANDROID_WINDOW_SIZE");
43 FBNativeWindowType windowId = NULL;
44 EventInjector* injector;
45 int consolePort = 5554;
46
47 if (env && sscanf(env, "%dx%d", &width, &height) == 2) {
48 winWidth = width;
49 winHeight = height;
50 }
51
52 #ifdef __linux__
53 // some OpenGL implementations may call X functions
54 // it is safer to synchronize all X calls made by all the
55 // rendering threads. (although the calls we do are locked
56 // in the FrameBuffer singleton object).
57 XInitThreads();
58 #endif
59
60 //
61 // Inialize SDL window
62 //
63 if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_VIDEO)) {
64 fprintf(stderr,"SDL init failed: %s\n", SDL_GetError());
65 return -1;
66 }
67
68 SDL_Surface *surface = SDL_SetVideoMode(winWidth, winHeight, 32, SDL_SWSURFACE);
69 if (surface == NULL) {
70 fprintf(stderr,"Failed to set video mode: %s\n", SDL_GetError());
71 return -1;
72 }
73
74 SDL_SysWMinfo wminfo;
75 memset(&wminfo, 0, sizeof(wminfo));
76 SDL_GetWMInfo(&wminfo);
77 #ifdef _WIN32
78 windowId = wminfo.window;
79 WSADATA wsaData;
80 int rc = WSAStartup( MAKEWORD(2,2), &wsaData);
81 if (rc != 0) {
82 printf( "could not initialize Winsock\n" );
83 }
84 #elif __linux__
85 windowId = wminfo.info.x11.window;
86 #elif __APPLE__
87 windowId = wminfo.nsWindowPtr;
88 #endif
89
90 printf("initializing renderer process\n");
91
92 //
93 // initialize OpenGL renderer to render in our window
94 //
95 bool inited = initOpenGLRenderer(winWidth, winHeight, portNum);
96 if (!inited) {
97 return -1;
98 }
99 printf("renderer process started\n");
100
101 float zRot = 0.0f;
102 inited = createOpenGLSubwindow(windowId, 0, 0,
103 winWidth, winHeight, zRot);
104 if (!inited) {
105 printf("failed to create OpenGL subwindow\n");
106 stopOpenGLRenderer();
107 return -1;
108 }
109 int subwinWidth = winWidth;
110 int subwinHeight = winHeight;
111
112 injector = new EventInjector(consolePort);
113
114 // Just wait until the window is closed
115 SDL_Event ev;
116
117 for (;;) {
118 injector->wait(1000/15);
119 injector->poll();
120
121 while (SDL_PollEvent(&ev)) {
122 switch (ev.type) {
123 case SDL_MOUSEBUTTONDOWN:
124 if (!mouseDown) {
125 injector->sendMouseDown(ev.button.x, ev.button.y);
126 mouseDown = 1;
127 }
128 break;
129 case SDL_MOUSEBUTTONUP:
130 if (mouseDown) {
131 injector->sendMouseUp(ev.button.x,ev.button.y);
132 mouseDown = 0;
133 }
134 break;
135 case SDL_MOUSEMOTION:
136 if (mouseDown)
137 injector->sendMouseMotion(ev.button.x,ev.button.y);
138 break;
139
140 case SDL_KEYDOWN:
141 #ifdef __APPLE__
142 /* special code to deal with Command-Q properly */
143 if (ev.key.keysym.sym == SDLK_q &&
144 ev.key.keysym.mod & KMOD_META) {
145 goto EXIT;
146 }
147 #endif
148 injector->sendKeyDown(convert_keysym(ev.key.keysym.sym));
149
150 if (ev.key.keysym.sym == SDLK_KP_MINUS) {
151 subwinWidth /= 2;
152 subwinHeight /= 2;
153
154 bool stat = destroyOpenGLSubwindow();
155 printf("destroy subwin returned %d\n", stat);
156 stat = createOpenGLSubwindow(windowId,
157 (winWidth - subwinWidth) / 2,
158 (winHeight - subwinHeight) / 2,
159 subwinWidth, subwinHeight,
160 zRot);
161 printf("create subwin returned %d\n", stat);
162 }
163 else if (ev.key.keysym.sym == SDLK_KP_PLUS) {
164 subwinWidth *= 2;
165 subwinHeight *= 2;
166
167 bool stat = destroyOpenGLSubwindow();
168 printf("destroy subwin returned %d\n", stat);
169 stat = createOpenGLSubwindow(windowId,
170 (winWidth - subwinWidth) / 2,
171 (winHeight - subwinHeight) / 2,
172 subwinWidth, subwinHeight,
173 zRot);
174 printf("create subwin returned %d\n", stat);
175 }
176 else if (ev.key.keysym.sym == SDLK_KP_MULTIPLY) {
177 zRot += 10.0f;
178 setOpenGLDisplayRotation(zRot);
179 }
180 else if (ev.key.keysym.sym == SDLK_KP_ENTER) {
181 repaintOpenGLDisplay();
182 }
183 break;
184 case SDL_KEYUP:
185 injector->sendKeyUp(convert_keysym(ev.key.keysym.sym));
186 break;
187 case SDL_QUIT:
188 goto EXIT;
189 }
190 }
191 }
192 EXIT:
193 //
194 // stop the renderer
195 //
196 printf("stopping the renderer process\n");
197 stopOpenGLRenderer();
198
199 return 0;
200 }
201
convert_keysym(int sym)202 static int convert_keysym(int sym)
203 {
204 #define EE(x,y) SDLK_##x, EventInjector::KEY_##y,
205 static const int keymap[] = {
206 EE(LEFT,LEFT)
207 EE(RIGHT,RIGHT)
208 EE(DOWN,DOWN)
209 EE(UP,UP)
210 EE(RETURN,ENTER)
211 EE(F1,SOFT1)
212 EE(ESCAPE,BACK)
213 EE(HOME,HOME)
214 -1
215 };
216 int nn;
217 for (nn = 0; keymap[nn] >= 0; nn += 2) {
218 if (keymap[nn] == sym)
219 return keymap[nn+1];
220 }
221 return sym;
222 }
223