Skip to content

Commit

Permalink
#63: Rewrite support for browser pointer lock API
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Oct 20, 2019
1 parent 51fa69e commit d90b538
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ set(SOURCES_CXX11
"${CMAKE_CURRENT_LIST_DIR}/js-dos-cpp/js-dos-ci.cpp"
"${CMAKE_CURRENT_LIST_DIR}/js-dos-cpp/js-dos-events.cpp"
"${CMAKE_CURRENT_LIST_DIR}/js-dos-cpp/js-dos-json.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/gui/sdlmain.cpp"
)
set_source_files_properties(${SOURCES_CXX11} PROPERTIES COMPILE_FLAGS "-std=c++11")

Expand Down Expand Up @@ -127,7 +128,6 @@ set(SOURCES
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/shell/shell_cmds.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/shell/shell_batch.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/shell/shell_misc.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/gui/sdlmain.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/gui/curstxt.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/gui/sdl_mapper.cpp"
"${CMAKE_CURRENT_LIST_DIR}/dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/gui/midi.cpp"
Expand Down
10 changes: 5 additions & 5 deletions dist/docs/api/js-dos-ts/js-dos-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ gulpfile.js --> generateBuildInfo
```
export const Build = {
version: "6.22.33 (f4423f98f23ae8f2bd689442f95fc29e)",
jsVersion: "59875851282d50e241d418337cfa413f093cf347",
jsSize: 196977,
wasmVersion: "03ff8f9208bc11b041bebc7cce39e56d",
wasmSize: 1810179,
version: "6.22.33 (008ca316c070f0c1d6f35a178dfda2da)",
jsVersion: "51fa69e2fa69b8f9846f0b10a237037f0751188a",
jsSize: 197995,
wasmVersion: "6d74a1bed329522e5371acaec19b753f",
wasmSize: 1808925,
};
Expand Down
2 changes: 1 addition & 1 deletion dist/docs/api/js-dos-ts/js-dos-conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fulldouble=false
fullresolution=original
windowresolution=original
output=surface
autolock=false
autolock=%autolock%
sensitivity=100
waitonerror=true
priority=higher,normal
Expand Down
19 changes: 18 additions & 1 deletion dist/docs/api/js-dos-ts/js-dos-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class, to configure emulation layer
```
export class DosBoxConfig {
public cycles?: string;
public cycles?: number | string;
```

Expand All @@ -35,6 +35,22 @@ export class DosBoxConfig {



```
public autolock?: boolean;
```







autolock: Mouse will automatically lock, if you click on the screen. (Press CTRL-F10 to unlock)




```
}
Expand Down Expand Up @@ -175,6 +191,7 @@ you can set alternative url for downloading js-dos script, default is 'wdosbox.j
export const DosBoxConfigDefaults: DosBoxConfig = {
cycles: "auto",
autolock: false,
};
Expand Down
60 changes: 36 additions & 24 deletions dreamlayers-em-dosbox-em-dosbox-svn-sdl2/src/gui/sdlmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1215,17 +1215,36 @@ Bitu GFX_SetSize(Bitu width,Bitu height,Bitu flags,double scalex,double scaley,G
}

#ifdef EMSCRIPTEN
static bool use_capture_callback = false;
static bool canUsePointerLock = false;
static void doGFX_CaptureMouse(void);

void GFX_CaptureMouse(void) {
if (use_capture_callback && !sdl.mouse.autoenable) {
if (sdl.mouse.locked) {
if (canUsePointerLock) {
EmscriptenPointerlockChangeEvent lastChangeEvent;
auto haveLockInfo = emscripten_get_pointerlock_status(&lastChangeEvent) == EMSCRIPTEN_RESULT_SUCCESS;
if (!haveLockInfo) {
printf("ERR! Can't get pointer lock info disabling pointer lock\n");
canUsePointerLock = false;
return;
}

auto isLocked = lastChangeEvent.isActive;

if (isLocked != sdl.mouse.locked) {
doGFX_CaptureMouse();
return;
}

if (isLocked) {
emscripten_exit_pointerlock();
} else {
//This only raises a request. A callback will notify when pointer
// lock starts. The user may need to confirm a browser dialog.
emscripten_request_pointerlock(NULL, true);
auto lockRequested = emscripten_request_pointerlock("#canvas", true) == EMSCRIPTEN_RESULT_DEFERRED;
if (!lockRequested) {
printf("ERR! Can't request pointer lock\n");
canUsePointerLock = false;
}
}
} else {
doGFX_CaptureMouse();
Expand Down Expand Up @@ -1283,20 +1302,6 @@ static void CaptureMouse(bool pressed) {
GFX_CaptureMouse();
}

#ifdef EMSCRIPTEN
EM_BOOL em_pointerlock_callback(int eventType,
const EmscriptenPointerlockChangeEvent *keyEvent,
void *userData) {
if (eventType == EMSCRIPTEN_EVENT_POINTERLOCKCHANGE) {
if ((!keyEvent->isActive && sdl.mouse.locked) ||
(keyEvent->isActive && !sdl.mouse.locked)) {
doGFX_CaptureMouse();
}
}
return false;
}
#endif

#if defined (WIN32)
STICKYKEYS stick_keys = {sizeof(STICKYKEYS), 0};
void sticky_keys(bool restore){
Expand Down Expand Up @@ -1895,6 +1900,14 @@ static void GUI_StartUp(Section * sec) {

sdl.mouse.autoenable=section->Get_bool("autolock");
if (!sdl.mouse.autoenable) SDL_ShowCursor(SDL_DISABLE);
#ifdef EMSCRIPTEN
canUsePointerLock = sdl.mouse.autoenable;
if (sdl.mouse.autoenable) {
// because request is always deferred
// need to ask at start
GFX_CaptureMouse();
}
#endif
sdl.mouse.autolock=false;
sdl.mouse.sensitivity=section->Get_int("sensitivity");
std::string output=section->Get_string("output");
Expand Down Expand Up @@ -2905,14 +2918,13 @@ int main(int argc, char* argv[]) {
canvasStyle.imageRendering = "crisp-edges";
canvasStyle.imageRendering = "pixelated";
);
if (emscripten_set_pointerlockchange_callback(NULL, NULL, true,
em_pointerlock_callback)
== EMSCRIPTEN_RESULT_SUCCESS) {
use_capture_callback = true;
}
// register no-op callbacks for defered events
emscripten_set_mousedown_callback("#canvas", NULL, false, [](int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) {
return 0;
});
#endif

/* Display Welcometext in the console */
/* Display Welcome text in the console */
LOG_MSG("DOSBox version %s", getVersion());
LOG_MSG("Copyright 2002-2015 DOSBox Team, published under GNU GPL.");
LOG_MSG("---");
Expand Down
10 changes: 5 additions & 5 deletions js-dos-ts/js-dos-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// gulpfile.js --> generateBuildInfo

export const Build = {
version: "6.22.33 (f4423f98f23ae8f2bd689442f95fc29e)",
jsVersion: "59875851282d50e241d418337cfa413f093cf347",
jsSize: 196977,
wasmVersion: "03ff8f9208bc11b041bebc7cce39e56d",
wasmSize: 1810179,
version: "6.22.33 (008ca316c070f0c1d6f35a178dfda2da)",
jsVersion: "51fa69e2fa69b8f9846f0b10a237037f0751188a",
jsSize: 197995,
wasmVersion: "6d74a1bed329522e5371acaec19b753f",
wasmSize: 1808925,
};
2 changes: 1 addition & 1 deletion js-dos-ts/js-dos-conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fulldouble=false
fullresolution=original
windowresolution=original
output=surface
autolock=false
autolock=%autolock%
sensitivity=100
waitonerror=true
priority=higher,normal
Expand Down
5 changes: 4 additions & 1 deletion js-dos-ts/js-dos-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// class, to configure emulation layer

export class DosBoxConfig {
public cycles?: string;
public cycles?: number | string;
// cycles: Amount of instructions DOSBox tries to emulate each millisecond.
// Setting this value too high results in sound dropouts and lags.
// Cycles can be set in 3 ways:
Expand All @@ -13,6 +13,8 @@ export class DosBoxConfig {
// 'fixed #number' will set a fixed amount of cycles. This is what you usually need if 'auto' fails.
// (Example: fixed 4000).
// 'max' will allocate as much cycles as your computer is able to handle.
public autolock?: boolean;
// autolock: Mouse will automatically lock, if you click on the screen. (Press CTRL-F10 to unlock)
}

// tslint:disable-next-line:max-classes-per-file
Expand Down Expand Up @@ -47,4 +49,5 @@ export class DosOptions extends DosBoxConfig {

export const DosBoxConfigDefaults: DosBoxConfig = {
cycles: "auto",
autolock: false,
};

0 comments on commit d90b538

Please sign in to comment.