OS additions and fixes for WebAssembly/asm.js

- Implement alert, shell_open, set_window_title
 - Add locale lookup, fixes #2477
 - Print without color control sequences
 - Move get_executable_path implementation to OS_JavaScript
This commit is contained in:
eska 2016-11-18 18:52:44 +01:00
parent e167c664c8
commit a6ae3204fb
4 changed files with 49 additions and 57 deletions

View File

@ -522,9 +522,6 @@ String OS_Unix::get_executable_path() const {
delete[] resolved_path; delete[] resolved_path;
return path; return path;
#elif defined(EMSCRIPTEN)
// We return nothing
return String();
#else #else
ERR_PRINT("Warning, don't know how to obtain executable path on this OS! Please override this function properly."); ERR_PRINT("Warning, don't know how to obtain executable path on this OS! Please override this function properly.");
return OS::get_executable_path(); return OS::get_executable_path();

View File

@ -148,7 +148,7 @@ int main(int argc, char *argv[]) {
/* Initialize the window */ /* Initialize the window */
printf("let it go!\n"); printf("let it go!\n");
glutInit(&argc, argv); glutInit(&argc, argv);
os = new OS_JavaScript(_gfx_init,NULL,NULL,NULL,NULL); os = new OS_JavaScript(_gfx_init,NULL,NULL);
#if 0 #if 0
char *args[]={"-test","gui","-v",NULL}; char *args[]={"-test","gui","-v",NULL};
Error err = Main::setup("apk",3,args); Error err = Main::setup("apk",3,args);

View File

@ -37,6 +37,7 @@
#include "main/main.h" #include "main/main.h"
#include "core/globals.h" #include "core/globals.h"
#include "stdlib.h"
#include "emscripten.h" #include "emscripten.h"
#include "dom_keys.h" #include "dom_keys.h"
@ -89,7 +90,9 @@ static InputEvent _setup_key_event(const EmscriptenKeyboardEvent *emscripten_eve
ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode); ev.key.scancode = dom2godot_scancode(emscripten_event->keyCode);
String unicode = String::utf8(emscripten_event->key); String unicode = String::utf8(emscripten_event->key);
// check if empty or multi-character (e.g. `CapsLock`)
if (unicode.length()!=1) { if (unicode.length()!=1) {
// might be empty as well, but better than nonsense
unicode = String::utf8(emscripten_event->charValue); unicode = String::utf8(emscripten_event->charValue);
} }
if (unicode.length()==1) { if (unicode.length()==1) {
@ -153,6 +156,25 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int
default_videomode=p_desired; default_videomode=p_desired;
// find locale, emscripten only sets "C"
char locale_ptr[16];
EM_ASM_({
var locale = "";
if (Module.locale) {
// best case: server-side script reads Accept-Language early and
// defines the locale to be read here
locale = Module.locale;
} else {
// no luck, use what the JS engine can tell us
// if this turns out not compatible enough, add tests for
// browserLanguage, systemLanguage and userLanguage
locale = navigator.languages ? navigator.languages[0] : navigator.language;
}
locale = locale.split('.')[0];
stringToUTF8(locale, $0, 16);
}, locale_ptr);
setenv("LANG", locale_ptr, true);
print_line("Init Audio"); print_line("Init Audio");
AudioDriverManagerSW::add_driver(&audio_driver_javascript); AudioDriverManagerSW::add_driver(&audio_driver_javascript);
@ -254,32 +276,11 @@ void OS_JavaScript::finalize() {
memdelete(input); memdelete(input);
} }
void OS_JavaScript::alert(const String& p_alert,const String& p_title) {
void OS_JavaScript::vprint(const char* p_format, va_list p_list, bool p_stderr) { EM_ASM_({
window.alert(UTF8ToString($0));
if (p_stderr) { }, p_alert.utf8().get_data());
vfprintf(stderr,p_format,p_list);
fflush(stderr);
} else {
vprintf(p_format,p_list);
fflush(stdout);
}
}
void OS_JavaScript::print(const char *p_format, ... ) {
va_list argp;
va_start(argp, p_format);
vprintf(p_format, argp );
va_end(argp);
}
void OS_JavaScript::alert(const String& p_alert) {
print("ALERT: %s\n",p_alert.utf8().get_data());
} }
@ -306,9 +307,12 @@ int OS_JavaScript::get_mouse_button_state() const {
return 0; return 0;
} }
void OS_JavaScript::set_window_title(const String& p_title) { void OS_JavaScript::set_window_title(const String& p_title) {
EM_ASM_({
document.title = UTF8ToString($0);
}, p_title.utf8().get_data());
} }
//interesting byt not yet //interesting byt not yet
@ -664,25 +668,17 @@ void OS_JavaScript::reload_gfx() {
} }
Error OS_JavaScript::shell_open(String p_uri) { Error OS_JavaScript::shell_open(String p_uri) {
EM_ASM_({
if (open_uri_func) window.open(UTF8ToString($0), '_blank');
return open_uri_func(p_uri)?ERR_CANT_OPEN:OK; }, p_uri.utf8().get_data());
return ERR_UNAVAILABLE; return OK;
}; }
String OS_JavaScript::get_resource_dir() const { String OS_JavaScript::get_resource_dir() const {
return "/"; //javascript has it's own filesystem for resources inside the APK return "/"; //javascript has it's own filesystem for resources inside the APK
} }
String OS_JavaScript::get_locale() const {
if (get_locale_func)
return get_locale_func();
return OS_Unix::get_locale();
}
String OS_JavaScript::get_data_dir() const { String OS_JavaScript::get_data_dir() const {
//if (get_data_dir_func) //if (get_data_dir_func)
@ -691,6 +687,10 @@ String OS_JavaScript::get_data_dir() const {
//return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir"); //return Globals::get_singleton()->get_singleton_object("GodotOS")->call("get_data_dir");
}; };
String OS_JavaScript::get_executable_path() const {
return String();
}
void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) { void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) {
@ -757,9 +757,7 @@ String OS_JavaScript::get_joy_guid(int p_device) const {
return input->get_joy_guid_remapped(p_device); return input->get_joy_guid_remapped(p_device);
} }
OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func) { OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) {
default_videomode.width=800; default_videomode.width=800;
default_videomode.height=600; default_videomode.height=600;
default_videomode.fullscreen=true; default_videomode.fullscreen=true;
@ -772,9 +770,7 @@ OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, Ope
gl_extensions=NULL; gl_extensions=NULL;
rasterizer=NULL; rasterizer=NULL;
open_uri_func=p_open_uri_func;
get_data_dir_func=p_get_data_dir_func; get_data_dir_func=p_get_data_dir_func;
get_locale_func=p_get_locale_func;
FileAccessUnix::close_notification_func=_close_notification_funcs; FileAccessUnix::close_notification_func=_close_notification_funcs;
time_to_save_sync=-1; time_to_save_sync=-1;

View File

@ -45,9 +45,7 @@
#include "javascript_eval.h" #include "javascript_eval.h"
typedef void (*GFXInitFunc)(void *ud,bool gl2,int w, int h, bool fs); typedef void (*GFXInitFunc)(void *ud,bool gl2,int w, int h, bool fs);
typedef int (*OpenURIFunc)(const String&);
typedef String (*GetDataDirFunc)(); typedef String (*GetDataDirFunc)();
typedef String (*GetLocaleFunc)();
class OS_JavaScript : public OS_Unix { class OS_JavaScript : public OS_Unix {
public: public:
@ -85,9 +83,7 @@ private:
VideoMode default_videomode; VideoMode default_videomode;
MainLoop * main_loop; MainLoop * main_loop;
OpenURIFunc open_uri_func;
GetDataDirFunc get_data_dir_func; GetDataDirFunc get_data_dir_func;
GetLocaleFunc get_locale_func;
#ifdef JAVASCRIPT_EVAL_ENABLED #ifdef JAVASCRIPT_EVAL_ENABLED
JavaScript* javascript_eval; JavaScript* javascript_eval;
@ -121,9 +117,12 @@ public:
//static OS* get_singleton(); //static OS* get_singleton();
virtual void vprint(const char* p_format, va_list p_list, bool p_stderr=false); virtual void print_error(const char* p_function, const char* p_file, int p_line, const char *p_code, const char* p_rationale, ErrorType p_type) {
virtual void print(const char *p_format, ... );
virtual void alert(const String& p_alert); OS::print_error(p_function, p_file, p_line, p_code, p_rationale, p_type);
}
virtual void alert(const String& p_alert,const String& p_title="ALERT!");
virtual void set_mouse_show(bool p_show); virtual void set_mouse_show(bool p_show);
@ -164,8 +163,8 @@ public:
virtual Error shell_open(String p_uri); virtual Error shell_open(String p_uri);
virtual String get_data_dir() const; virtual String get_data_dir() const;
String get_executable_path() const;
virtual String get_resource_dir() const; virtual String get_resource_dir() const;
virtual String get_locale() const;
void process_accelerometer(const Vector3& p_accelerometer); void process_accelerometer(const Vector3& p_accelerometer);
void process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points); void process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points);
@ -175,7 +174,7 @@ public:
virtual String get_joy_guid(int p_device) const; virtual String get_joy_guid(int p_device) const;
bool joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event); bool joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event);
OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func); OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func);
~OS_JavaScript(); ~OS_JavaScript();
}; };