123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- /*************************************************************************/
- /* os_nacl.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "os_nacl.h"
- #include "drivers/unix/memory_pool_static_malloc.h"
- #include "os/memory_pool_dynamic_static.h"
- #include "main/main.h"
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <time.h>
- #include "io/file_access_memory.h"
- #include "core/io/file_access_pack.h"
- #include "scene/io/scene_loader.h"
- #include "scene/main/scene_main_loop.h"
- #include "servers/visual/visual_server_raster.h"
- #include "drivers/gles2/rasterizer_gles2.h"
- #include "nacl_keycodes.h"
- #include "core/globals.h"
- #include "core/input_map.h"
- #include <ppapi/cpp/point.h>
- #include <ppapi/cpp/var.h>
- #define UNIX_ENABLED
- #include "drivers/unix/thread_posix.h"
- #include "drivers/unix/semaphore_posix.h"
- #include "drivers/unix/mutex_posix.h"
- int OSNacl::get_video_driver_count() const {
- return 1;
- };
- const char * OSNacl::get_video_driver_name(int p_driver) const {
- return "GLES2";
- };
- OS::VideoMode OSNacl::get_default_video_mode() const {
- return OS::VideoMode(800,600,false);
- };
- int OSNacl::get_audio_driver_count() const {
- return 1;
- };
- const char * OSNacl::get_audio_driver_name(int p_driver) const {
- return "nacl_audio";
- };
- static MemoryPoolStaticMalloc *mempool_static=NULL;
- static MemoryPoolDynamicStatic *mempool_dynamic=NULL;
- void OSNacl::initialize_core() {
- ticks_start=0;
- ticks_start=get_ticks_usec();
- };
- void OSNacl::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) {
- rasterizer = memnew( RasterizerGLES2 );
- visual_server = memnew( VisualServerRaster(rasterizer) );
- visual_server->init();
- visual_server->cursor_set_visible(false, 0);
- audio_driver = memnew(AudioDriverNacl);
- audio_driver->set_singleton();
- audio_driver->init();
- sample_manager = memnew( SampleManagerMallocSW );
- audio_server = memnew( AudioServerSW(sample_manager) );
- audio_server->set_mixer_params(AudioMixerSW::INTERPOLATION_LINEAR,false);
- audio_server->init();
- spatial_sound_server = memnew( SpatialSoundServerSW );
- spatial_sound_server->init();
- spatial_sound_2d_server = memnew( SpatialSound2DServerSW );
- spatial_sound_2d_server->init();
- //
- physics_server = memnew( PhysicsServerSW );
- physics_server->init();
- physics_2d_server = memnew( Physics2DServerSW );
- physics_2d_server->init();
- input = memnew(InputDefault);
- };
- void OSNacl::set_main_loop( MainLoop * p_main_loop ) {
- main_loop = p_main_loop;
- input->set_main_loop(p_main_loop);
- main_loop->init();
- };
- void OSNacl::delete_main_loop() {
- if (main_loop)
- memdelete(main_loop);
- };
- void OSNacl::finalize() {
- };
- void OSNacl::finalize_core() {
- if (mempool_dynamic)
- memdelete( mempool_dynamic );
- if (mempool_static)
- delete mempool_static;
- };
- void OSNacl::alert(const String& p_alert,const String& p_title) {
- fprintf(stderr,"ERROR: %s\n",p_alert.utf8().get_data());
- };
- void OSNacl::vprint(const char* p_format, va_list p_list, bool p_strerr) {
- vprintf(p_format,p_list);
- fflush(stdout);
- }
- String OSNacl::get_stdin_string(bool p_block) {
- char buff[1024];
- return fgets(buff,1024,stdin);
- };
- void OSNacl::set_mouse_show(bool p_show) {
- };
- void OSNacl::set_mouse_grab(bool p_grab) {
- };
- bool OSNacl::is_mouse_grab_enabled() const {
- return false;
- };
- int OSNacl::get_mouse_button_state() const {
- return mouse_mask;
- };
- Point2 OSNacl::get_mouse_pos() const {
- return Point2();
- };
- void OSNacl::set_window_title(const String& p_title) {
- };
- void OSNacl::set_video_mode(const VideoMode& p_video_mode, int p_screen) {
- video_mode = p_video_mode;
- };
- OS::VideoMode OSNacl::get_video_mode(int p_screen) const {
- return video_mode;
- };
- void OSNacl::get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen) const {
- };
- Error OSNacl::execute(const String& p_path, const List<String>& p_arguments,bool p_blocking, OS::ProcessID *r_child_id, String* r_pipe, int *r_exitcode) {
- return ERR_UNAVAILABLE;
- };
- Error OSNacl::kill(const ProcessID& p_pid) {
- return ERR_UNAVAILABLE;
- };
- bool OSNacl::has_environment(const String& p_var) const {
- return getenv(p_var.utf8().get_data())!=NULL;
- };
- String OSNacl::get_environment(const String& p_var) const {
- if (getenv(p_var.utf8().get_data()))
- return getenv(p_var.utf8().get_data());
- return "";
- };
- String OSNacl::get_name() {
- return "NaCl";
- };
- MainLoop *OSNacl::get_main_loop() const {
- return main_loop;
- };
- OS::Date OSNacl::get_date() const {
- time_t t=time(NULL);
- struct tm *lt=localtime(&t);
- Date ret;
- ret.year=lt->tm_year;
- ret.month=(Month)lt->tm_mon;
- ret.day=lt->tm_mday;
- ret.weekday=(Weekday)lt->tm_wday;
- ret.dst=lt->tm_isdst;
- return ret;
- };
- OS::Time OSNacl::get_time() const {
- time_t t=time(NULL);
- struct tm *lt=localtime(&t);
- Time ret;
- ret.hour=lt->tm_hour;
- ret.min=lt->tm_min;
- ret.sec=lt->tm_sec;
- return ret;
- };
- void OSNacl::delay_usec(uint32_t p_usec) const {
- //usleep(p_usec);
- };
- uint64_t OSNacl::get_ticks_usec() const {
- struct timeval tv_now;
- gettimeofday(&tv_now,NULL);
- uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec*1000000L;
- longtime-=ticks_start;
- return longtime;
- };
- bool OSNacl::can_draw() const {
- return minimized != true;
- };
- void OSNacl::queue_event(const InputEvent& p_event) {
- ERR_FAIL_INDEX( event_count, MAX_EVENTS );
- event_queue[event_count++] = p_event;
- };
- void OSNacl::add_package(String p_name, Vector<uint8_t> p_data) {
- FileAccessMemory::register_file(p_name, p_data);
- FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES);
- FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA);
- FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM);
- if (!PackedData::get_singleton())
- memnew(PackedData);
- printf("adding package %ls, %x\n", p_name.c_str(), PackedData::get_singleton());
- PackedData::get_singleton()->set_disabled(true);
- PackedData::get_singleton()->add_pack(p_name);
- PackedData::get_singleton()->set_disabled(false);
- printf("added\n");
- };
- void OSNacl::set_cursor_shape(CursorShape p_shape) {
- };
- String OSNacl::get_resource_dir() const {
- return ".";
- };
- static int mouse_button(int p_nacl_but) {
- switch (p_nacl_but) {
- case PP_INPUTEVENT_MOUSEBUTTON_LEFT:
- return BUTTON_LEFT;
- case PP_INPUTEVENT_MOUSEBUTTON_MIDDLE:
- return BUTTON_MIDDLE;
- case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
- return BUTTON_RIGHT;
- };
- return 0;
- };
- static InputModifierState modifier(uint32_t p_mod) {
- InputModifierState mod_mask;
- mod_mask.shift = p_mod & PP_INPUTEVENT_MODIFIER_SHIFTKEY;
- mod_mask.alt = p_mod & PP_INPUTEVENT_MODIFIER_ALTKEY;
- mod_mask.control = p_mod & PP_INPUTEVENT_MODIFIER_CONTROLKEY;
- mod_mask.meta = p_mod & PP_INPUTEVENT_MODIFIER_METAKEY;
- return mod_mask;
- };
- void OSNacl::handle_event(const pp::InputEvent& p_event) {
- int type = p_event.GetType();
- switch (type) {
- case PP_INPUTEVENT_TYPE_MOUSEDOWN:
- case PP_INPUTEVENT_TYPE_MOUSEUP:
- case PP_INPUTEVENT_TYPE_WHEEL: {
- InputEvent event;
- event.ID=++event_id;
- event.type = InputEvent::MOUSE_BUTTON;
- event.device=0;
- pp::MouseInputEvent mevent(p_event);
- if (type == PP_INPUTEVENT_TYPE_WHEEL) {
- pp::WheelInputEvent wevent(p_event);;
- float ticks = wevent.GetTicks().y();
- if (ticks == 0)
- break; // whut?
- event.mouse_button.pressed = true;
- event.mouse_button.button_index = ticks > 0 ? BUTTON_WHEEL_UP : BUTTON_WHEEL_DOWN;
- event.mouse_button.doubleclick = false;
- } else {
- event.mouse_button.pressed = (type == PP_INPUTEVENT_TYPE_MOUSEDOWN);
- event.mouse_button.button_index = mouse_button(mevent.GetButton());
- event.mouse_button.doubleclick = (mevent.GetClickCount() % 2) == 0;
- mouse_mask &= ~(1<< (event.mouse_button.button_index - 1));
- mouse_mask |= (event.mouse_button.pressed << (event.mouse_button.button_index - 1));
- };
- pp::Point pos = mevent.GetPosition();
- event.mouse_button.button_mask = mouse_mask;
- event.mouse_button.global_x = pos.x();
- event.mouse_button.x = pos.x();
- event.mouse_button.global_y = pos.y();
- event.mouse_button.y = pos.y();
- event.mouse_button.pointer_index = 0;
- event.mouse_button.mod = modifier(p_event.GetModifiers());
- queue_event(event);
- } break;
- case PP_INPUTEVENT_TYPE_MOUSEMOVE: {
- pp::MouseInputEvent mevent(p_event);
- pp::Point pos = mevent.GetPosition();
- InputEvent event;
- event.ID=++event_id;
- event.type = InputEvent::MOUSE_MOTION;
- event.mouse_motion.pointer_index = 0;
- event.mouse_motion.global_x = pos.x();
- event.mouse_motion.global_y = pos.y();
- event.mouse_motion.x = pos.x();
- event.mouse_motion.y = pos.y();
- event.mouse_motion.button_mask = mouse_mask;
- event.mouse_motion.mod = modifier(p_event.GetModifiers());
- event.mouse_motion.relative_x = pos.x() - mouse_last_x;
- event.mouse_motion.relative_y = pos.y() - mouse_last_y;
- mouse_last_x = pos.x();
- mouse_last_y = pos.y();
- queue_event(event);
- } break;
- case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
- case PP_INPUTEVENT_TYPE_KEYDOWN:
- case PP_INPUTEVENT_TYPE_KEYUP: {
- pp::KeyboardInputEvent kevent(p_event);
- bool is_char;
- uint32_t key = godot_key(kevent.GetKeyCode(), is_char);
- if (type != PP_INPUTEVENT_TYPE_KEYUP && is_char) {
- last_scancode = key;
- break;
- };
- InputEvent event;
- event.ID=++event_id;
- event.type = InputEvent::KEY;
- event.key.pressed = (type != PP_INPUTEVENT_TYPE_KEYUP);
- event.key.scancode = key;
- event.key.unicode = key;
- event.key.echo = p_event.GetModifiers() & PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT;
- event.key.mod = modifier(p_event.GetModifiers());
- queue_event(event);
- } break;
- case PP_INPUTEVENT_TYPE_CHAR: {
- pp::KeyboardInputEvent kevent(p_event);
- InputEvent event;
- event.ID = ++event_id;
- event.type = InputEvent::KEY;
- event.key.pressed = true;
- event.key.scancode = last_scancode;
- event.key.unicode = kevent.GetCharacterText().AsString().c_str()[0];
- event.key.mod = modifier(p_event.GetModifiers());
- event.key.echo = p_event.GetModifiers() & PP_INPUTEVENT_MODIFIER_ISAUTOREPEAT;
- queue_event(event);
- } break;
- /*
- case NPEventType_Minimize: {
- minimized = p_event->u.minimize.value == 1;
- } break;
- case NPEventType_Focus: {
- if (p_event->u.focus.value == 1) {
- main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_IN);
- } else {
- main_loop->notification(MainLoop::NOTIFICATION_WM_FOCUS_OUT);
- };
- } break;
- */
- default:
- ;
- };
- };
- bool OSNacl::iterate() {
- if (!main_loop) {
- event_count = 0;
- return true;
- };
- for (int i=0; i<event_count; i++) {
- input->parse_input_event(event_queue[i]);
- };
- event_count = 0;
- return Main::iteration();
- };
- OSNacl::OSNacl() {
- main_loop=NULL;
- mempool_dynamic = NULL;
- mempool_static = NULL;
- mouse_last_x = 0;
- mouse_last_y = 0;
- event_count = 0;
- event_id = 0;
- mouse_mask = 0;
- video_mode = get_default_video_mode();
- last_scancode = 0;
- minimized = false;
- ThreadPosix::make_default();
- SemaphorePosix::make_default();
- MutexPosix::make_default();
- mempool_static = new MemoryPoolStaticMalloc;
- mempool_dynamic = memnew( MemoryPoolDynamicStatic );
- };
- OSNacl::~OSNacl() {
- };
|