-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathmain.c
264 lines (212 loc) · 7.21 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "parsec-dso.h"
#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"
#define MSG_CLIPBOARD 7
#define WINDOW_W 1280
#define WINDOW_H 720
#if defined(_WIN32)
#define SDK_PATH "../../sdk/windows/parsec.dll"
#elif defined(__APPLE__)
#define SDK_PATH "../../sdk/macos/libparsec.dylib"
#else
#define SDK_PATH "../../sdk/linux/libparsec.so"
#endif
struct context {
bool done;
ParsecDSO *parsec;
SDL_Window *window;
SDL_Surface *surface;
SDL_Cursor *cursor;
SDL_AudioDeviceID audio;
};
static void logCallback(enum ParsecLogLevel level, char *msg, void *opaque)
{
opaque;
printf("[%s] %s\n", level == LOG_DEBUG ? "D" : "I", msg);
}
static void audio(int16_t *pcm, uint32_t frames, void *opaque)
{
struct context *context = (struct context *) opaque;
if (SDL_GetQueuedAudioSize(context->audio) < 20000)
SDL_QueueAudio(context->audio, pcm, frames * 2 * sizeof(int16_t));
}
static void userData(struct context *context, uint32_t id, uint32_t bufferKey)
{
char *msg = ParsecGetBuffer(context->parsec, bufferKey);
if (msg && id == 7) // Parsec application user defined clipboard msg id
SDL_SetClipboardText(msg);
ParsecFree(context->parsec, msg);
}
static void cursor(struct context *context, ParsecCursor *cursor, uint32_t bufferKey)
{
if (cursor->imageUpdate) {
uint8_t *image = ParsecGetBuffer(context->parsec, bufferKey);
if (image) {
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(image, cursor->width, cursor->height,
32, cursor->width * 4, 0xff, 0xff00, 0xff0000, 0xff000000);
SDL_Cursor *sdlCursor = SDL_CreateColorCursor(surface, cursor->hotX, cursor->hotY);
SDL_SetCursor(sdlCursor);
SDL_FreeCursor(context->cursor);
context->cursor = sdlCursor;
SDL_FreeSurface(context->surface);
context->surface = surface;
ParsecFree(context->parsec, image);
}
}
if (cursor->modeUpdate) {
if (SDL_GetRelativeMouseMode() && !cursor->relative) {
SDL_SetRelativeMouseMode(SDL_DISABLE);
SDL_WarpMouseInWindow(context->window, cursor->positionX, cursor->positionY);
} else if (!SDL_GetRelativeMouseMode() && cursor->relative) {
SDL_SetRelativeMouseMode(SDL_ENABLE);
}
}
}
static int32_t audioThread(void *opaque)
{
struct context *context = (struct context *) opaque;
while (!context->done)
ParsecClientPollAudio(context->parsec, audio, 100, context);
return 0;
}
static int32_t renderThread(void *opaque)
{
struct context *context = (struct context *) opaque;
SDL_GLContext *gl = SDL_GL_CreateContext(context->window);
SDL_GL_SetSwapInterval(1);
void (*glFinish)(void) = (void (*)(void)) SDL_GL_GetProcAddress("glFinish");
while (!context->done) {
ParsecClientGLRenderFrame(context->parsec, 100);
SDL_GL_SwapWindow(context->window);
glFinish();
}
ParsecClientGLDestroy(context->parsec);
SDL_GL_DeleteContext(gl);
return 0;
}
int32_t main(int32_t argc, char **argv)
{
if (argc < 3) {
printf("Usage: demo sessionID peerID\n");
return 1;
}
struct context context = {0};
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER);
SDL_AudioSpec want = {0}, have;
want.freq = 48000;
want.format = AUDIO_S16;
want.channels = 2;
want.samples = 2048;
context.audio = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
SDL_PauseAudioDevice(context.audio, 0);
context.window = SDL_CreateWindow("Parsec Client Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_W, WINDOW_H, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
int32_t glW = 0;
SDL_GL_GetDrawableSize(context.window, &glW, NULL);
float scale = (float) glW / (float) WINDOW_W;
ParsecStatus e = ParsecInit(NULL, NULL, SDK_PATH, &context.parsec);
if (e != PARSEC_OK) goto except;
ParsecSetLogCallback(context.parsec, logCallback, NULL);
e = ParsecClientConnect(context.parsec, NULL, argv[1], argv[2]);
if (e != PARSEC_OK) goto except;
ParsecClientSetDimensions(context.parsec, WINDOW_W, WINDOW_H, scale);
SDL_Thread *render_thread = SDL_CreateThread(renderThread, "renderThread", &context);
SDL_Thread *audio_thread = SDL_CreateThread(audioThread, "audioThread", &context);
while (!context.done) {
for (SDL_Event msg; SDL_PollEvent(&msg);) {
ParsecMessage pmsg = {0};
switch (msg.type) {
case SDL_QUIT:
context.done = true;
break;
case SDL_WINDOWEVENT:
if (msg.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
ParsecClientSetDimensions(context.parsec, msg.window.data1, msg.window.data2, scale);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
pmsg.type = MESSAGE_KEYBOARD;
pmsg.keyboard.code = (ParsecKeycode) msg.key.keysym.scancode;
pmsg.keyboard.mod = msg.key.keysym.mod;
pmsg.keyboard.pressed = msg.key.type == SDL_KEYDOWN;
break;
case SDL_MOUSEMOTION:
pmsg.type = MESSAGE_MOUSE_MOTION;
pmsg.mouseMotion.relative = SDL_GetRelativeMouseMode();
pmsg.mouseMotion.x = pmsg.mouseMotion.relative ? msg.motion.xrel : msg.motion.x;
pmsg.mouseMotion.y = pmsg.mouseMotion.relative ? msg.motion.yrel : msg.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
pmsg.type = MESSAGE_MOUSE_BUTTON;
pmsg.mouseButton.button = msg.button.button;
pmsg.mouseButton.pressed = msg.button.type == SDL_MOUSEBUTTONDOWN;
break;
case SDL_MOUSEWHEEL:
pmsg.type = MESSAGE_MOUSE_WHEEL;
pmsg.mouseWheel.x = msg.wheel.x;
pmsg.mouseWheel.y = msg.wheel.y;
break;
case SDL_CLIPBOARDUPDATE:
ParsecClientSendUserData(context.parsec, MSG_CLIPBOARD, SDL_GetClipboardText());
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
pmsg.type = MESSAGE_GAMEPAD_BUTTON;
pmsg.gamepadButton.id = msg.cbutton.which;
pmsg.gamepadButton.button = msg.cbutton.button;
pmsg.gamepadButton.pressed = msg.cbutton.type == SDL_CONTROLLERBUTTONDOWN;
break;
case SDL_CONTROLLERAXISMOTION:
pmsg.type = MESSAGE_GAMEPAD_AXIS;
pmsg.gamepadAxis.id = msg.caxis.which;
pmsg.gamepadAxis.axis = msg.caxis.axis;
pmsg.gamepadAxis.value = msg.caxis.value;
break;
case SDL_CONTROLLERDEVICEADDED:
SDL_GameControllerOpen(msg.cdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
pmsg.type = MESSAGE_GAMEPAD_UNPLUG;
pmsg.gamepadUnplug.id = msg.cdevice.which;
SDL_GameControllerClose(SDL_GameControllerFromInstanceID(msg.cdevice.which));
break;
}
if (pmsg.type != 0) {
if (ParsecClientSendMessage(context.parsec, &pmsg) != PARSEC_OK)
context.done = true;
}
}
for (ParsecClientEvent event; ParsecClientPollEvents(context.parsec, 0, &event);) {
switch (event.type) {
case CLIENT_EVENT_CURSOR:
cursor(&context, &event.cursor.cursor, event.cursor.key);
break;
case CLIENT_EVENT_USER_DATA:
userData(&context, event.userData.id, event.userData.key);
break;
case CLIENT_EVENT_RUMBLE:
break;
default:
break;
}
}
SDL_Delay(1);
}
SDL_WaitThread(audio_thread, NULL);
SDL_WaitThread(render_thread, NULL);
except:
if (e != PARSEC_OK) {
char error[32];
snprintf(error, 32, "Parsec error: %d\n", e);
SDL_ShowSimpleMessageBox(0, "Parsec Error", error, NULL);
}
ParsecDestroy(context.parsec);
SDL_DestroyWindow(context.window);
SDL_CloseAudioDevice(context.audio);
SDL_Quit();
return 0;
}