diff options
Diffstat (limited to 'platform/x11')
-rw-r--r-- | platform/x11/SCsub | 1 | ||||
-rw-r--r-- | platform/x11/detect.py | 26 | ||||
-rw-r--r-- | platform/x11/detect_prime.cpp | 1 | ||||
-rw-r--r-- | platform/x11/joypad_linux.cpp | 28 | ||||
-rw-r--r-- | platform/x11/joypad_linux.h | 2 | ||||
-rw-r--r-- | platform/x11/key_mapping_x11.cpp | 134 | ||||
-rw-r--r-- | platform/x11/key_mapping_x11.h | 1 | ||||
-rw-r--r-- | platform/x11/os_x11.cpp | 72 | ||||
-rw-r--r-- | platform/x11/os_x11.h | 7 | ||||
-rw-r--r-- | platform/x11/power_x11.cpp | 577 | ||||
-rw-r--r-- | platform/x11/power_x11.h | 66 |
11 files changed, 197 insertions, 718 deletions
diff --git a/platform/x11/SCsub b/platform/x11/SCsub index 9027c244be..2268e4cc3d 100644 --- a/platform/x11/SCsub +++ b/platform/x11/SCsub @@ -12,7 +12,6 @@ common_x11 = [ "os_x11.cpp", "key_mapping_x11.cpp", "joypad_linux.cpp", - "power_x11.cpp", "detect_prime.cpp" ] diff --git a/platform/x11/detect.py b/platform/x11/detect.py index 9d5affcb3c..9a9ab86068 100644 --- a/platform/x11/detect.py +++ b/platform/x11/detect.py @@ -1,7 +1,6 @@ import os import platform import sys -from methods import get_compiler_version, using_gcc, using_clang def is_active(): @@ -61,6 +60,7 @@ def get_opts(): BoolVariable('use_lld', 'Use the LLD linker', False), BoolVariable('use_thinlto', 'Use ThinLTO', False), BoolVariable('use_static_cpp', 'Link libgcc and libstdc++ statically for better portability', False), + BoolVariable('use_coverage', 'Test Godot coverage', False), BoolVariable('use_ubsan', 'Use LLVM/GCC compiler undefined behavior sanitizer (UBSAN)', False), BoolVariable('use_asan', 'Use LLVM/GCC compiler address sanitizer (ASAN))', False), BoolVariable('use_lsan', 'Use LLVM/GCC compiler leak sanitizer (LSAN))', False), @@ -141,6 +141,10 @@ def configure(env): print("Using LLD with GCC is not supported yet, try compiling with 'use_llvm=yes'.") sys.exit(255) + if env['use_coverage']: + env.Append(CCFLAGS=['-ftest-coverage', '-fprofile-arcs']) + env.Append(LINKFLAGS=['-ftest-coverage', '-fprofile-arcs']) + if env['use_ubsan'] or env['use_asan'] or env['use_lsan'] or env['use_tsan']: env.extra_suffix += "s" @@ -179,18 +183,10 @@ def configure(env): env.Append(CCFLAGS=['-pipe']) env.Append(LINKFLAGS=['-pipe']) - # Check for gcc version >= 6 before adding -no-pie - if using_gcc(env): - version = get_compiler_version(env) - if version != None and version[0] >= '6': - env.Append(CCFLAGS=['-fpie']) - env.Append(LINKFLAGS=['-no-pie']) - # Do the same for clang should be fine with Clang 4 and higher - if using_clang(env): - version = get_compiler_version(env) - if version != None and version[0] >= '4': - env.Append(CCFLAGS=['-fpie']) - env.Append(LINKFLAGS=['-no-pie']) + # -fpie and -no-pie is supported on GCC 6+ and Clang 4+, both below our + # minimal requirements. + env.Append(CCFLAGS=['-fpie']) + env.Append(LINKFLAGS=['-no-pie']) ## Dependencies @@ -229,6 +225,10 @@ def configure(env): sys.exit(255) env.ParseConfig('pkg-config bullet --cflags --libs') + if False: # not env['builtin_assimp']: + # FIXME: Add min version check + env.ParseConfig('pkg-config assimp --cflags --libs') + if not env['builtin_enet']: env.ParseConfig('pkg-config libenet --cflags --libs') diff --git a/platform/x11/detect_prime.cpp b/platform/x11/detect_prime.cpp index 98a51ff27c..a0e5f835c0 100644 --- a/platform/x11/detect_prime.cpp +++ b/platform/x11/detect_prime.cpp @@ -62,6 +62,7 @@ vendor vendormap[] = { { "NVIDIA Corporation", 30 }, { "X.Org", 30 }, { "Intel Open Source Technology Center", 20 }, + { "Intel", 20 }, { "nouveau", 10 }, { "Mesa Project", 0 }, { NULL, 0 } diff --git a/platform/x11/joypad_linux.cpp b/platform/x11/joypad_linux.cpp index a64a25aeee..a9fe7275c2 100644 --- a/platform/x11/joypad_linux.cpp +++ b/platform/x11/joypad_linux.cpp @@ -83,7 +83,6 @@ void JoypadLinux::Joypad::reset() { JoypadLinux::JoypadLinux(InputDefault *in) { exit_udev = false; input = in; - joy_mutex = Mutex::create(); joy_thread = Thread::create(joy_thread_func, this); } @@ -91,7 +90,6 @@ JoypadLinux::~JoypadLinux() { exit_udev = true; Thread::wait_to_finish(joy_thread); memdelete(joy_thread); - memdelete(joy_mutex); close_joypad(); } @@ -137,9 +135,8 @@ void JoypadLinux::enumerate_joypads(udev *p_udev) { String devnode_str = devnode; if (devnode_str.find(ignore_str) == -1) { - joy_mutex->lock(); + MutexLock lock(joy_mutex); open_joypad(devnode); - joy_mutex->unlock(); } } udev_device_unref(dev); @@ -176,7 +173,7 @@ void JoypadLinux::monitor_joypads(udev *p_udev) { if (dev && udev_device_get_devnode(dev) != 0) { - joy_mutex->lock(); + MutexLock lock(joy_mutex); String action = udev_device_get_action(dev); const char *devnode = udev_device_get_devnode(dev); if (devnode) { @@ -192,7 +189,6 @@ void JoypadLinux::monitor_joypads(udev *p_udev) { } udev_device_unref(dev); - joy_mutex->unlock(); } } usleep(50000); @@ -204,15 +200,17 @@ void JoypadLinux::monitor_joypads(udev *p_udev) { void JoypadLinux::monitor_joypads() { while (!exit_udev) { - joy_mutex->lock(); - for (int i = 0; i < 32; i++) { - char fname[64]; - sprintf(fname, "/dev/input/event%d", i); - if (attached_devices.find(fname) == -1) { - open_joypad(fname); + { + MutexLock lock(joy_mutex); + + for (int i = 0; i < 32; i++) { + char fname[64]; + sprintf(fname, "/dev/input/event%d", i); + if (attached_devices.find(fname) == -1) { + open_joypad(fname); + } } } - joy_mutex->unlock(); usleep(1000000); // 1s } } @@ -457,7 +455,7 @@ InputDefault::JoyAxis JoypadLinux::axis_correct(const input_absinfo *p_abs, int void JoypadLinux::process_joypads() { - if (joy_mutex->try_lock() != OK) { + if (joy_mutex.try_lock() != OK) { return; } for (int i = 0; i < JOYPADS_MAX; i++) { @@ -548,6 +546,6 @@ void JoypadLinux::process_joypads() { } } } - joy_mutex->unlock(); + joy_mutex.unlock(); } #endif diff --git a/platform/x11/joypad_linux.h b/platform/x11/joypad_linux.h index e5638899bf..d5719b6dbe 100644 --- a/platform/x11/joypad_linux.h +++ b/platform/x11/joypad_linux.h @@ -72,7 +72,7 @@ private: }; bool exit_udev; - Mutex *joy_mutex; + Mutex joy_mutex; Thread *joy_thread; InputDefault *input; Joypad joypads[JOYPADS_MAX]; diff --git a/platform/x11/key_mapping_x11.cpp b/platform/x11/key_mapping_x11.cpp index 54e1e1d357..78bd2b71a0 100644 --- a/platform/x11/key_mapping_x11.cpp +++ b/platform/x11/key_mapping_x11.cpp @@ -180,6 +180,140 @@ static _XTranslatePair _xkeysym_to_keycode[] = { { 0, 0 } }; +struct _TranslatePair { + + unsigned int keysym; + unsigned int keycode; +}; + +static _TranslatePair _scancode_to_keycode[] = { + + { KEY_ESCAPE, 0x09 }, + { KEY_1, 0x0A }, + { KEY_2, 0x0B }, + { KEY_3, 0x0C }, + { KEY_4, 0x0D }, + { KEY_5, 0x0E }, + { KEY_6, 0x0F }, + { KEY_7, 0x10 }, + { KEY_8, 0x11 }, + { KEY_9, 0x12 }, + { KEY_0, 0x13 }, + { KEY_MINUS, 0x14 }, + { KEY_EQUAL, 0x15 }, + { KEY_BACKSPACE, 0x16 }, + { KEY_TAB, 0x17 }, + { KEY_Q, 0x18 }, + { KEY_W, 0x19 }, + { KEY_E, 0x1A }, + { KEY_R, 0x1B }, + { KEY_T, 0x1C }, + { KEY_Y, 0x1D }, + { KEY_U, 0x1E }, + { KEY_I, 0x1F }, + { KEY_O, 0x20 }, + { KEY_P, 0x21 }, + { KEY_BRACELEFT, 0x22 }, + { KEY_BRACERIGHT, 0x23 }, + { KEY_ENTER, 0x24 }, + { KEY_CONTROL, 0x25 }, + { KEY_A, 0x26 }, + { KEY_S, 0x27 }, + { KEY_D, 0x28 }, + { KEY_F, 0x29 }, + { KEY_G, 0x2A }, + { KEY_H, 0x2B }, + { KEY_J, 0x2C }, + { KEY_K, 0x2D }, + { KEY_L, 0x2E }, + { KEY_SEMICOLON, 0x2F }, + { KEY_APOSTROPHE, 0x30 }, + { KEY_QUOTELEFT, 0x31 }, + { KEY_SHIFT, 0x32 }, + { KEY_BACKSLASH, 0x33 }, + { KEY_Z, 0x34 }, + { KEY_X, 0x35 }, + { KEY_C, 0x36 }, + { KEY_V, 0x37 }, + { KEY_B, 0x38 }, + { KEY_N, 0x39 }, + { KEY_M, 0x3A }, + { KEY_COMMA, 0x3B }, + { KEY_PERIOD, 0x3C }, + { KEY_SLASH, 0x3D }, + { KEY_SHIFT, 0x3E }, + { KEY_KP_MULTIPLY, 0x3F }, + { KEY_ALT, 0x40 }, + { KEY_SPACE, 0x41 }, + { KEY_CAPSLOCK, 0x42 }, + { KEY_F1, 0x43 }, + { KEY_F2, 0x44 }, + { KEY_F3, 0x45 }, + { KEY_F4, 0x46 }, + { KEY_F5, 0x47 }, + { KEY_F6, 0x48 }, + { KEY_F7, 0x49 }, + { KEY_F8, 0x4A }, + { KEY_F9, 0x4B }, + { KEY_F10, 0x4C }, + { KEY_NUMLOCK, 0x4D }, + { KEY_SCROLLLOCK, 0x4E }, + { KEY_KP_7, 0x4F }, + { KEY_KP_8, 0x50 }, + { KEY_KP_9, 0x51 }, + { KEY_KP_SUBTRACT, 0x52 }, + { KEY_KP_4, 0x53 }, + { KEY_KP_5, 0x54 }, + { KEY_KP_6, 0x55 }, + { KEY_KP_ADD, 0x56 }, + { KEY_KP_1, 0x57 }, + { KEY_KP_2, 0x58 }, + { KEY_KP_3, 0x59 }, + { KEY_KP_0, 0x5A }, + { KEY_KP_PERIOD, 0x5B }, + //{ KEY_???, 0x5E }, //NON US BACKSLASH + { KEY_F11, 0x5F }, + { KEY_F12, 0x60 }, + { KEY_KP_ENTER, 0x68 }, + { KEY_CONTROL, 0x69 }, + { KEY_KP_DIVIDE, 0x6A }, + { KEY_PRINT, 0x6B }, + { KEY_ALT, 0x6C }, + { KEY_ENTER, 0x6D }, + { KEY_HOME, 0x6E }, + { KEY_UP, 0x6F }, + { KEY_PAGEUP, 0x70 }, + { KEY_LEFT, 0x71 }, + { KEY_RIGHT, 0x72 }, + { KEY_END, 0x73 }, + { KEY_DOWN, 0x74 }, + { KEY_PAGEDOWN, 0x75 }, + { KEY_INSERT, 0x76 }, + { KEY_DELETE, 0x77 }, + { KEY_VOLUMEMUTE, 0x79 }, + { KEY_VOLUMEDOWN, 0x7A }, + { KEY_VOLUMEUP, 0x7B }, + { KEY_PAUSE, 0x7F }, + { KEY_SUPER_L, 0x85 }, + { KEY_SUPER_R, 0x86 }, + { KEY_MENU, 0x87 }, + { KEY_UNKNOWN, 0 } +}; + +unsigned int KeyMappingX11::get_scancode(unsigned int p_code) { + + unsigned int keycode = KEY_UNKNOWN; + for (int i = 0; _scancode_to_keycode[i].keysym != KEY_UNKNOWN; i++) { + + if (_scancode_to_keycode[i].keycode == p_code) { + keycode = _scancode_to_keycode[i].keysym; + break; + } + } + + return keycode; +} + unsigned int KeyMappingX11::get_keycode(KeySym p_keysym) { // kinda bruteforce.. could optimize. diff --git a/platform/x11/key_mapping_x11.h b/platform/x11/key_mapping_x11.h index e99bf1694b..10db43bcc4 100644 --- a/platform/x11/key_mapping_x11.h +++ b/platform/x11/key_mapping_x11.h @@ -45,6 +45,7 @@ class KeyMappingX11 { public: static unsigned int get_keycode(KeySym p_keysym); + static unsigned int get_scancode(unsigned int p_code); static KeySym get_keysym(unsigned int p_code); static unsigned int get_unicode_from_keysym(KeySym p_keysym); static KeySym get_keysym_from_unicode(unsigned int p_unicode); diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 1fd91ec3c1..ddb5237d1b 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -659,8 +659,6 @@ Error OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_a #endif _ensure_user_data_dir(); - power_manager = memnew(PowerX11); - if (p_desired.layered) { set_window_per_pixel_transparency_enabled(true); } @@ -870,8 +868,6 @@ void OS_X11::finalize() { visual_server->finish(); memdelete(visual_server); - memdelete(power_manager); - #if defined(OPENGL_ENABLED) if (video_driver_index == VIDEO_DRIVER_GLES2) { @@ -1892,6 +1888,8 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) { if (status == XLookupChars) { bool keypress = xkeyevent->type == KeyPress; unsigned int keycode = KeyMappingX11::get_keycode(keysym_keycode); + unsigned int physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode); + if (keycode >= 'a' && keycode <= 'z') keycode -= 'a' - 'A'; @@ -1900,23 +1898,29 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) { for (int i = 0; i < tmp.length(); i++) { Ref<InputEventKey> k; k.instance(); - if (keycode == 0 && tmp[i] == 0) { + if (physical_keycode == 0 && keycode == 0 && tmp[i] == 0) { continue; } + if (keycode == 0) + keycode = physical_keycode; + get_key_modifier_state(xkeyevent->state, k); k->set_unicode(tmp[i]); k->set_pressed(keypress); - k->set_scancode(keycode); + k->set_keycode(keycode); + + k->set_physical_keycode(physical_keycode); k->set_echo(false); - if (k->get_scancode() == KEY_BACKTAB) { + if (k->get_keycode() == KEY_BACKTAB) { //make it consistent across platforms. - k->set_scancode(KEY_TAB); + k->set_keycode(KEY_TAB); + k->set_physical_keycode(KEY_TAB); k->set_shift(true); } @@ -1946,6 +1950,7 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) { // keysym, so it works in all platforms the same. unsigned int keycode = KeyMappingX11::get_keycode(keysym_keycode); + unsigned int physical_keycode = KeyMappingX11::get_scancode(xkeyevent->keycode); /* Phase 3, obtain a unicode character from the keysym */ @@ -1965,9 +1970,12 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) { bool keypress = xkeyevent->type == KeyPress; - if (keycode == 0 && unicode == 0) + if (physical_keycode == 0 && keycode == 0 && unicode == 0) return; + if (keycode == 0) + keycode = physical_keycode; + /* Phase 5, determine modifier mask */ // No problems here, except I had no way to @@ -2004,8 +2012,10 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) { // is correct, but the xorg developers are // not very helpful today. - ::Time tresh = ABSDIFF(peek_event.xkey.time, xkeyevent->time); - if (peek_event.type == KeyPress && tresh < 5) { +#define ABSDIFF(x, y) (((x) < (y)) ? ((y) - (x)) : ((x) - (y))) + ::Time threshold = ABSDIFF(peek_event.xkey.time, xkeyevent->time); +#undef ABSDIFF + if (peek_event.type == KeyPress && threshold < 5) { KeySym rk; XLookupString((XKeyEvent *)&peek_event, str, 256, &rk, NULL); if (rk == keysym_keycode) { @@ -2029,37 +2039,39 @@ void OS_X11::handle_key_event(XKeyEvent *p_event, bool p_echo) { if (keycode >= 'a' && keycode <= 'z') keycode -= 'a' - 'A'; - k->set_scancode(keycode); + k->set_keycode(keycode); + k->set_physical_keycode(physical_keycode); k->set_unicode(unicode); k->set_echo(p_echo); - if (k->get_scancode() == KEY_BACKTAB) { + if (k->get_keycode() == KEY_BACKTAB) { //make it consistent across platforms. - k->set_scancode(KEY_TAB); + k->set_keycode(KEY_TAB); + k->set_physical_keycode(KEY_TAB); k->set_shift(true); } //don't set mod state if modifier keys are released by themselves //else event.is_action() will not work correctly here if (!k->is_pressed()) { - if (k->get_scancode() == KEY_SHIFT) + if (k->get_keycode() == KEY_SHIFT) k->set_shift(false); - else if (k->get_scancode() == KEY_CONTROL) + else if (k->get_keycode() == KEY_CONTROL) k->set_control(false); - else if (k->get_scancode() == KEY_ALT) + else if (k->get_keycode() == KEY_ALT) k->set_alt(false); - else if (k->get_scancode() == KEY_META) + else if (k->get_keycode() == KEY_META) k->set_metakey(false); } - bool last_is_pressed = Input::get_singleton()->is_key_pressed(k->get_scancode()); + bool last_is_pressed = Input::get_singleton()->is_key_pressed(k->get_keycode()); if (k->is_pressed()) { if (last_is_pressed) { k->set_echo(true); } } - //printf("key: %x\n",k->get_scancode()); + //printf("key: %x\n",k->get_keycode()); input->accumulate_input_event(k); } @@ -3080,8 +3092,6 @@ void OS_X11::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c // allocate memory to contain the whole file cursor_image->pixels = (XcursorPixel *)memalloc(size); - image->lock(); - for (XcursorPixel index = 0; index < image_size; index++) { int row_index = floor(index / texture_size.width) + atlas_rect.position.y; int column_index = (index % int(texture_size.width)) + atlas_rect.position.x; @@ -3094,8 +3104,6 @@ void OS_X11::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, c *(cursor_image->pixels + index) = image->get_pixel(column_index, row_index).to_argb32(); } - image->unlock(); - ERR_FAIL_COND(cursor_image->pixels == NULL); // Save it for a further usage @@ -3273,10 +3281,10 @@ void OS_X11::set_icon(const Ref<Image> &p_icon) { pd.write[0] = w; pd.write[1] = h; - PoolVector<uint8_t>::Read r = img->get_data().read(); + const uint8_t *r = img->get_data().ptr(); long *wr = &pd.write[2]; - uint8_t const *pr = r.ptr(); + uint8_t const *pr = r; for (int i = 0; i < w * h; i++) { long v = 0; @@ -3389,18 +3397,6 @@ void OS_X11::set_context(int p_context) { } } -OS::PowerState OS_X11::get_power_state() { - return power_manager->get_power_state(); -} - -int OS_X11::get_power_seconds_left() { - return power_manager->get_power_seconds_left(); -} - -int OS_X11::get_power_percent_left() { - return power_manager->get_power_percent_left(); -} - void OS_X11::disable_crash_handler() { crash_handler.disable(); } diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index fd8a981763..55d24d64a3 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -39,7 +39,6 @@ #include "drivers/unix/os_unix.h" #include "joypad_linux.h" #include "main/input_default.h" -#include "power_x11.h" #include "servers/audio_server.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" @@ -200,8 +199,6 @@ class OS_X11 : public OS_Unix { AudioDriverPulseAudio driver_pulseaudio; #endif - PowerX11 *power_manager; - bool layered_window; CrashHandler crash_handler; @@ -323,10 +320,6 @@ public: virtual void _set_use_vsync(bool p_enable); //virtual bool is_vsync_enabled() const; - virtual OS::PowerState get_power_state(); - virtual int get_power_seconds_left(); - virtual int get_power_percent_left(); - virtual bool _check_internal_feature_support(const String &p_feature); virtual void force_process_input(); diff --git a/platform/x11/power_x11.cpp b/platform/x11/power_x11.cpp deleted file mode 100644 index 5ac5e8e87b..0000000000 --- a/platform/x11/power_x11.cpp +++ /dev/null @@ -1,577 +0,0 @@ -/*************************************************************************/ -/* power_x11.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -/* -Adapted from corresponding SDL 2.0 code. -*/ - -/* - Simple DirectMedia Layer - Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org> - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ - -#include "power_x11.h" - -#include <stdio.h> -#include <unistd.h> - -#include "core/error_macros.h" -#include <dirent.h> -#include <fcntl.h> -#include <sys/stat.h> -#include <sys/types.h> - -// CODE CHUNK IMPORTED FROM SDL 2.0 - -static const char *proc_apm_path = "/proc/apm"; -static const char *proc_acpi_battery_path = "/proc/acpi/battery"; -static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter"; -static const char *sys_class_power_supply_path = "/sys/class/power_supply"; - -FileAccessRef PowerX11::open_power_file(const char *base, const char *node, const char *key) { - String path = String(base) + String("/") + String(node) + String("/") + String(key); - FileAccessRef f = FileAccess::open(path, FileAccess::READ); - return f; -} - -bool PowerX11::read_power_file(const char *base, const char *node, const char *key, char *buf, size_t buflen) { - ssize_t br = 0; - FileAccessRef fd = open_power_file(base, node, key); - if (!fd) { - return false; - } - br = fd->get_buffer(reinterpret_cast<uint8_t *>(buf), buflen - 1); - fd->close(); - if (br < 0) { - return false; - } - buf[br] = '\0'; // null-terminate the string - return true; -} - -bool PowerX11::make_proc_acpi_key_val(char **_ptr, char **_key, char **_val) { - char *ptr = *_ptr; - - while (*ptr == ' ') { - ptr++; /* skip whitespace. */ - } - - if (*ptr == '\0') { - return false; /* EOF. */ - } - - *_key = ptr; - - while ((*ptr != ':') && (*ptr != '\0')) { - ptr++; - } - - if (*ptr == '\0') { - return false; /* (unexpected) EOF. */ - } - - *(ptr++) = '\0'; /* terminate the key. */ - - while (*ptr == ' ') { - ptr++; /* skip whitespace. */ - } - - if (*ptr == '\0') { - return false; /* (unexpected) EOF. */ - } - - *_val = ptr; - - while ((*ptr != '\n') && (*ptr != '\0')) { - ptr++; - } - - if (*ptr != '\0') { - *(ptr++) = '\0'; /* terminate the value. */ - } - - *_ptr = ptr; /* store for next time. */ - return true; -} - -void PowerX11::check_proc_acpi_battery(const char *node, bool *have_battery, bool *charging) { - const char *base = proc_acpi_battery_path; - char info[1024]; - char state[1024]; - char *ptr = NULL; - char *key = NULL; - char *val = NULL; - bool charge = false; - bool choose = false; - int maximum = -1; - int remaining = -1; - int secs = -1; - int pct = -1; - - if (!read_power_file(base, node, "state", state, sizeof(state))) { - return; - } else { - if (!read_power_file(base, node, "info", info, sizeof(info))) - return; - } - - ptr = &state[0]; - while (make_proc_acpi_key_val(&ptr, &key, &val)) { - if (String(key) == "present") { - if (String(val) == "yes") { - *have_battery = true; - } - } else if (String(key) == "charging state") { - /* !!! FIXME: what exactly _does_ charging/discharging mean? */ - if (String(val) == "charging/discharging") { - charge = true; - } else if (String(val) == "charging") { - charge = true; - } - } else if (String(key) == "remaining capacity") { - String sval = val; - const int cvt = sval.to_int(); - remaining = cvt; - } - } - - ptr = &info[0]; - while (make_proc_acpi_key_val(&ptr, &key, &val)) { - if (String(key) == "design capacity") { - String sval = val; - const int cvt = sval.to_int(); - maximum = cvt; - } - } - - if ((maximum >= 0) && (remaining >= 0)) { - pct = (int)((((float)remaining) / ((float)maximum)) * 100.0f); - if (pct < 0) { - pct = 0; - } else if (pct > 100) { - pct = 100; - } - } - - /* !!! FIXME: calculate (secs). */ - - /* - * We pick the battery that claims to have the most minutes left. - * (failing a report of minutes, we'll take the highest percent.) - */ - // -- GODOT start -- - //if ((secs < 0) && (this->nsecs_left < 0)) { - if (this->nsecs_left < 0) { - // -- GODOT end -- - if ((pct < 0) && (this->percent_left < 0)) { - choose = true; /* at least we know there's a battery. */ - } - if (pct > this->percent_left) { - choose = true; - } - } else if (secs > this->nsecs_left) { - choose = true; - } - - if (choose) { - this->nsecs_left = secs; - this->percent_left = pct; - *charging = charge; - } -} - -void PowerX11::check_proc_acpi_ac_adapter(const char *node, bool *have_ac) { - const char *base = proc_acpi_ac_adapter_path; - char state[256]; - char *ptr = NULL; - char *key = NULL; - char *val = NULL; - - if (!read_power_file(base, node, "state", state, sizeof(state))) { - return; - } - - ptr = &state[0]; - while (make_proc_acpi_key_val(&ptr, &key, &val)) { - String skey = key; - if (skey == "state") { - String sval = val; - if (sval == "on-line") { - *have_ac = true; - } - } - } -} - -bool PowerX11::GetPowerInfo_Linux_proc_acpi() { - String node; - DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); - bool have_battery = false; - bool have_ac = false; - bool charging = false; - - this->nsecs_left = -1; - this->percent_left = -1; - this->power_state = OS::POWERSTATE_UNKNOWN; - - dirp->change_dir(proc_acpi_battery_path); - Error err = dirp->list_dir_begin(); - - if (err != OK) { - return false; /* can't use this interface. */ - } else { - node = dirp->get_next(); - while (node != "") { - check_proc_acpi_battery(node.utf8().get_data(), &have_battery, &charging /*, seconds, percent*/); - node = dirp->get_next(); - } - } - dirp->change_dir(proc_acpi_ac_adapter_path); - err = dirp->list_dir_begin(); - if (err != OK) { - return false; /* can't use this interface. */ - } else { - node = dirp->get_next(); - while (node != "") { - check_proc_acpi_ac_adapter(node.utf8().get_data(), &have_ac); - node = dirp->get_next(); - } - } - - if (!have_battery) { - this->power_state = OS::POWERSTATE_NO_BATTERY; - } else if (charging) { - this->power_state = OS::POWERSTATE_CHARGING; - } else if (have_ac) { - this->power_state = OS::POWERSTATE_CHARGED; - } else { - this->power_state = OS::POWERSTATE_ON_BATTERY; - } - - memdelete(dirp); - return true; /* definitive answer. */ -} - -bool PowerX11::next_string(char **_ptr, char **_str) { - char *ptr = *_ptr; - char *str = *_str; - - while (*ptr == ' ') { /* skip any spaces... */ - ptr++; - } - - if (*ptr == '\0') { - return false; - } - - str = ptr; - while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0')) - ptr++; - - if (*ptr != '\0') - *(ptr++) = '\0'; - - *_str = str; - *_ptr = ptr; - return true; -} - -bool PowerX11::int_string(char *str, int *val) { - String sval = str; - *val = sval.to_int(); - return (*str != '\0'); -} - -/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */ -bool PowerX11::GetPowerInfo_Linux_proc_apm() { - bool need_details = false; - int ac_status = 0; - int battery_status = 0; - int battery_flag = 0; - int battery_percent = 0; - int battery_time = 0; - FileAccessRef fd = FileAccess::open(proc_apm_path, FileAccess::READ); - char buf[128]; - char *ptr = &buf[0]; - char *str = NULL; - ssize_t br; - - if (!fd) { - return false; /* can't use this interface. */ - } - - br = fd->get_buffer(reinterpret_cast<uint8_t *>(buf), sizeof(buf) - 1); - fd->close(); - - if (br < 0) { - return false; - } - - buf[br] = '\0'; /* null-terminate the string. */ - if (!next_string(&ptr, &str)) { /* driver version */ - return false; - } - if (!next_string(&ptr, &str)) { /* BIOS version */ - return false; - } - if (!next_string(&ptr, &str)) { /* APM flags */ - return false; - } - - if (!next_string(&ptr, &str)) { /* AC line status */ - return false; - } else if (!int_string(str, &ac_status)) { - return false; - } - - if (!next_string(&ptr, &str)) { /* battery status */ - return false; - } else if (!int_string(str, &battery_status)) { - return false; - } - if (!next_string(&ptr, &str)) { /* battery flag */ - return false; - } else if (!int_string(str, &battery_flag)) { - return false; - } - if (!next_string(&ptr, &str)) { /* remaining battery life percent */ - return false; - } - String sstr = str; - if (sstr[sstr.length() - 1] == '%') { - sstr[sstr.length() - 1] = '\0'; - } - if (!int_string(str, &battery_percent)) { - return false; - } - - if (!next_string(&ptr, &str)) { /* remaining battery life time */ - return false; - } else if (!int_string(str, &battery_time)) { - return false; - } - - if (!next_string(&ptr, &str)) { /* remaining battery life time units */ - return false; - } else if (String(str) == "min") { - battery_time *= 60; - } - - if (battery_flag == 0xFF) { /* unknown state */ - this->power_state = OS::POWERSTATE_UNKNOWN; - } else if (battery_flag & (1 << 7)) { /* no battery */ - this->power_state = OS::POWERSTATE_NO_BATTERY; - } else if (battery_flag & (1 << 3)) { /* charging */ - this->power_state = OS::POWERSTATE_CHARGING; - need_details = true; - } else if (ac_status == 1) { - this->power_state = OS::POWERSTATE_CHARGED; /* on AC, not charging. */ - need_details = true; - } else { - this->power_state = OS::POWERSTATE_ON_BATTERY; - need_details = true; - } - - this->percent_left = -1; - this->nsecs_left = -1; - if (need_details) { - const int pct = battery_percent; - const int secs = battery_time; - - if (pct >= 0) { /* -1 == unknown */ - this->percent_left = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ - } - if (secs >= 0) { /* -1 == unknown */ - this->nsecs_left = secs; - } - } - - return true; -} - -/* !!! FIXME: implement d-bus queries to org.freedesktop.UPower. */ - -bool PowerX11::GetPowerInfo_Linux_sys_class_power_supply(/*PowerState *state, int *seconds, int *percent*/) { - const char *base = sys_class_power_supply_path; - String name; - - DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); - dirp->change_dir(base); - Error err = dirp->list_dir_begin(); - - if (err != OK) { - return false; - } - - this->power_state = OS::POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */ - this->nsecs_left = -1; - this->percent_left = -1; - - name = dirp->get_next(); - - while (name != "") { - bool choose = false; - char str[64]; - OS::PowerState st; - int secs; - int pct; - - if ((name == ".") || (name == "..")) { - name = dirp->get_next(); - continue; //skip these, of course. - } else { - if (!read_power_file(base, name.utf8().get_data(), "type", str, sizeof(str))) { - name = dirp->get_next(); - continue; // Don't know _what_ we're looking at. Give up on it. - } else { - if (String(str) != "Battery\n") { - name = dirp->get_next(); - continue; // we don't care about UPS and such. - } - } - } - - /* some drivers don't offer this, so if it's not explicitly reported assume it's present. */ - if (read_power_file(base, name.utf8().get_data(), "present", str, sizeof(str)) && (String(str) == "0\n")) { - st = OS::POWERSTATE_NO_BATTERY; - } else if (!read_power_file(base, name.utf8().get_data(), "status", str, sizeof(str))) { - st = OS::POWERSTATE_UNKNOWN; /* uh oh */ - } else if (String(str) == "Charging\n") { - st = OS::POWERSTATE_CHARGING; - } else if (String(str) == "Discharging\n") { - st = OS::POWERSTATE_ON_BATTERY; - } else if ((String(str) == "Full\n") || (String(str) == "Not charging\n")) { - st = OS::POWERSTATE_CHARGED; - } else { - st = OS::POWERSTATE_UNKNOWN; /* uh oh */ - } - - if (!read_power_file(base, name.utf8().get_data(), "capacity", str, sizeof(str))) { - pct = -1; - } else { - pct = String(str).to_int(); - pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ - } - - if (!read_power_file(base, name.utf8().get_data(), "time_to_empty_now", str, sizeof(str))) { - secs = -1; - } else { - secs = String(str).to_int(); - secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */ - } - - /* - * We pick the battery that claims to have the most minutes left. - * (failing a report of minutes, we'll take the highest percent.) - */ - if ((secs < 0) && (this->nsecs_left < 0)) { - if ((pct < 0) && (this->percent_left < 0)) { - choose = true; /* at least we know there's a battery. */ - } else if (pct > this->percent_left) { - choose = true; - } - } else if (secs > this->nsecs_left) { - choose = true; - } - - if (choose) { - this->nsecs_left = secs; - this->percent_left = pct; - this->power_state = st; - } - - name = dirp->get_next(); - } - - memdelete(dirp); - return true; /* don't look any further*/ -} - -bool PowerX11::UpdatePowerInfo() { - if (GetPowerInfo_Linux_sys_class_power_supply()) { // try method 1 - return true; - } - if (GetPowerInfo_Linux_proc_acpi()) { // try further - return true; - } - if (GetPowerInfo_Linux_proc_apm()) { // try even further - return true; - } - return false; -} - -PowerX11::PowerX11() : - nsecs_left(-1), - percent_left(-1), - power_state(OS::POWERSTATE_UNKNOWN) { -} - -PowerX11::~PowerX11() { -} - -OS::PowerState PowerX11::get_power_state() { - if (UpdatePowerInfo()) { - return power_state; - } else { - return OS::POWERSTATE_UNKNOWN; - } -} - -int PowerX11::get_power_seconds_left() { - if (UpdatePowerInfo()) { - return nsecs_left; - } else { - return -1; - } -} - -int PowerX11::get_power_percent_left() { - if (UpdatePowerInfo()) { - return percent_left; - } else { - return -1; - } -} diff --git a/platform/x11/power_x11.h b/platform/x11/power_x11.h deleted file mode 100644 index 76f20c68e8..0000000000 --- a/platform/x11/power_x11.h +++ /dev/null @@ -1,66 +0,0 @@ -/*************************************************************************/ -/* power_x11.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#ifndef POWER_X11_H -#define POWER_X11_H - -#include "core/os/dir_access.h" -#include "core/os/file_access.h" -#include "core/os/os.h" - -class PowerX11 { - -private: - int nsecs_left; - int percent_left; - OS::PowerState power_state; - - FileAccessRef open_power_file(const char *base, const char *node, const char *key); - bool read_power_file(const char *base, const char *node, const char *key, char *buf, size_t buflen); - bool make_proc_acpi_key_val(char **_ptr, char **_key, char **_val); - void check_proc_acpi_battery(const char *node, bool *have_battery, bool *charging); - void check_proc_acpi_ac_adapter(const char *node, bool *have_ac); - bool GetPowerInfo_Linux_proc_acpi(); - bool next_string(char **_ptr, char **_str); - bool int_string(char *str, int *val); - bool GetPowerInfo_Linux_proc_apm(); - bool GetPowerInfo_Linux_sys_class_power_supply(); - bool UpdatePowerInfo(); - -public: - PowerX11(); - virtual ~PowerX11(); - - OS::PowerState get_power_state(); - int get_power_seconds_left(); - int get_power_percent_left(); -}; - -#endif // POWER_X11_H |