diff options
Diffstat (limited to 'core/os')
-rw-r--r-- | core/os/keyboard.cpp | 713 | ||||
-rw-r--r-- | core/os/keyboard.h | 543 | ||||
-rw-r--r-- | core/os/main_loop.cpp | 4 | ||||
-rw-r--r-- | core/os/main_loop.h | 5 | ||||
-rw-r--r-- | core/os/memory.cpp | 4 | ||||
-rw-r--r-- | core/os/memory.h | 10 | ||||
-rw-r--r-- | core/os/midi_driver.cpp | 23 | ||||
-rw-r--r-- | core/os/midi_driver.h | 4 | ||||
-rw-r--r-- | core/os/mutex.cpp | 4 | ||||
-rw-r--r-- | core/os/mutex.h | 4 | ||||
-rw-r--r-- | core/os/os.cpp | 62 | ||||
-rw-r--r-- | core/os/os.h | 28 | ||||
-rw-r--r-- | core/os/pool_allocator.cpp | 8 | ||||
-rw-r--r-- | core/os/pool_allocator.h | 33 | ||||
-rw-r--r-- | core/os/rw_lock.h | 4 | ||||
-rw-r--r-- | core/os/semaphore.h | 4 | ||||
-rw-r--r-- | core/os/spin_lock.h | 6 | ||||
-rw-r--r-- | core/os/thread.cpp | 5 | ||||
-rw-r--r-- | core/os/thread.h | 5 | ||||
-rw-r--r-- | core/os/thread_safe.h | 4 | ||||
-rw-r--r-- | core/os/threaded_array_processor.h | 5 | ||||
-rw-r--r-- | core/os/time.cpp | 166 | ||||
-rw-r--r-- | core/os/time.h | 13 |
23 files changed, 855 insertions, 802 deletions
diff --git a/core/os/keyboard.cpp b/core/os/keyboard.cpp index 4c5f0b5220..24907d34c8 100644 --- a/core/os/keyboard.cpp +++ b/core/os/keyboard.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -33,400 +33,399 @@ #include "core/os/os.h" struct _KeyCodeText { - int code; + Key code; const char *text; }; static const _KeyCodeText _keycodes[] = { /* clang-format off */ - {KEY_ESCAPE ,"Escape"}, - {KEY_TAB ,"Tab"}, - {KEY_BACKTAB ,"BackTab"}, - {KEY_BACKSPACE ,"BackSpace"}, - {KEY_ENTER ,"Enter"}, - {KEY_KP_ENTER ,"Kp Enter"}, - {KEY_INSERT ,"Insert"}, - {KEY_DELETE ,"Delete"}, - {KEY_PAUSE ,"Pause"}, - {KEY_PRINT ,"Print"}, - {KEY_SYSREQ ,"SysReq"}, - {KEY_CLEAR ,"Clear"}, - {KEY_HOME ,"Home"}, - {KEY_END ,"End"}, - {KEY_LEFT ,"Left"}, - {KEY_UP ,"Up"}, - {KEY_RIGHT ,"Right"}, - {KEY_DOWN ,"Down"}, - {KEY_PAGEUP ,"PageUp"}, - {KEY_PAGEDOWN ,"PageDown"}, - {KEY_SHIFT ,"Shift"}, - {KEY_CTRL ,"Ctrl"}, + {Key::ESCAPE ,"Escape"}, + {Key::TAB ,"Tab"}, + {Key::BACKTAB ,"Backtab"}, + {Key::BACKSPACE ,"Backspace"}, + {Key::ENTER ,"Enter"}, + {Key::KP_ENTER ,"Kp Enter"}, + {Key::INSERT ,"Insert"}, + {Key::KEY_DELETE ,"Delete"}, + {Key::PAUSE ,"Pause"}, + {Key::PRINT ,"Print"}, + {Key::SYSREQ ,"SysReq"}, + {Key::CLEAR ,"Clear"}, + {Key::HOME ,"Home"}, + {Key::END ,"End"}, + {Key::LEFT ,"Left"}, + {Key::UP ,"Up"}, + {Key::RIGHT ,"Right"}, + {Key::DOWN ,"Down"}, + {Key::PAGEUP ,"PageUp"}, + {Key::PAGEDOWN ,"PageDown"}, + {Key::SHIFT ,"Shift"}, + {Key::CTRL ,"Ctrl"}, #ifdef OSX_ENABLED - {KEY_META ,"Command"}, + {Key::META ,"Command"}, #else - {KEY_META ,"Meta"}, + {Key::META ,"Meta"}, #endif - {KEY_ALT ,"Alt"}, - {KEY_CAPSLOCK ,"CapsLock"}, - {KEY_NUMLOCK ,"NumLock"}, - {KEY_SCROLLLOCK ,"ScrollLock"}, - {KEY_F1 ,"F1"}, - {KEY_F2 ,"F2"}, - {KEY_F3 ,"F3"}, - {KEY_F4 ,"F4"}, - {KEY_F5 ,"F5"}, - {KEY_F6 ,"F6"}, - {KEY_F7 ,"F7"}, - {KEY_F8 ,"F8"}, - {KEY_F9 ,"F9"}, - {KEY_F10 ,"F10"}, - {KEY_F11 ,"F11"}, - {KEY_F12 ,"F12"}, - {KEY_F13 ,"F13"}, - {KEY_F14 ,"F14"}, - {KEY_F15 ,"F15"}, - {KEY_F16 ,"F16"}, - {KEY_KP_MULTIPLY ,"Kp Multiply"}, - {KEY_KP_DIVIDE ,"Kp Divide"}, - {KEY_KP_SUBTRACT ,"Kp Subtract"}, - {KEY_KP_PERIOD ,"Kp Period"}, - {KEY_KP_ADD ,"Kp Add"}, - {KEY_KP_0 ,"Kp 0"}, - {KEY_KP_1 ,"Kp 1"}, - {KEY_KP_2 ,"Kp 2"}, - {KEY_KP_3 ,"Kp 3"}, - {KEY_KP_4 ,"Kp 4"}, - {KEY_KP_5 ,"Kp 5"}, - {KEY_KP_6 ,"Kp 6"}, - {KEY_KP_7 ,"Kp 7"}, - {KEY_KP_8 ,"Kp 8"}, - {KEY_KP_9 ,"Kp 9"}, - {KEY_SUPER_L ,"Super L"}, - {KEY_SUPER_R ,"Super R"}, - {KEY_MENU ,"Menu"}, - {KEY_HYPER_L ,"Hyper L"}, - {KEY_HYPER_R ,"Hyper R"}, - {KEY_HELP ,"Help"}, - {KEY_DIRECTION_L ,"Direction L"}, - {KEY_DIRECTION_R ,"Direction R"}, - {KEY_BACK ,"Back"}, - {KEY_FORWARD ,"Forward"}, - {KEY_STOP ,"Stop"}, - {KEY_REFRESH ,"Refresh"}, - {KEY_VOLUMEDOWN ,"VolumeDown"}, - {KEY_VOLUMEMUTE ,"VolumeMute"}, - {KEY_VOLUMEUP ,"VolumeUp"}, - {KEY_BASSBOOST ,"BassBoost"}, - {KEY_BASSUP ,"BassUp"}, - {KEY_BASSDOWN ,"BassDown"}, - {KEY_TREBLEUP ,"TrebleUp"}, - {KEY_TREBLEDOWN ,"TrebleDown"}, - {KEY_MEDIAPLAY ,"MediaPlay"}, - {KEY_MEDIASTOP ,"MediaStop"}, - {KEY_MEDIAPREVIOUS ,"MediaPrevious"}, - {KEY_MEDIANEXT ,"MediaNext"}, - {KEY_MEDIARECORD ,"MediaRecord"}, - {KEY_HOMEPAGE ,"HomePage"}, - {KEY_FAVORITES ,"Favorites"}, - {KEY_SEARCH ,"Search"}, - {KEY_STANDBY ,"StandBy"}, - {KEY_LAUNCHMAIL ,"LaunchMail"}, - {KEY_LAUNCHMEDIA ,"LaunchMedia"}, - {KEY_LAUNCH0 ,"Launch0"}, - {KEY_LAUNCH1 ,"Launch1"}, - {KEY_LAUNCH2 ,"Launch2"}, - {KEY_LAUNCH3 ,"Launch3"}, - {KEY_LAUNCH4 ,"Launch4"}, - {KEY_LAUNCH5 ,"Launch5"}, - {KEY_LAUNCH6 ,"Launch6"}, - {KEY_LAUNCH7 ,"Launch7"}, - {KEY_LAUNCH8 ,"Launch8"}, - {KEY_LAUNCH9 ,"Launch9"}, - {KEY_LAUNCHA ,"LaunchA"}, - {KEY_LAUNCHB ,"LaunchB"}, - {KEY_LAUNCHC ,"LaunchC"}, - {KEY_LAUNCHD ,"LaunchD"}, - {KEY_LAUNCHE ,"LaunchE"}, - {KEY_LAUNCHF ,"LaunchF"}, - - {KEY_UNKNOWN ,"Unknown"}, - - {KEY_SPACE ,"Space"}, - {KEY_EXCLAM ,"Exclam"}, - {KEY_QUOTEDBL ,"QuoteDbl"}, - {KEY_NUMBERSIGN ,"NumberSign"}, - {KEY_DOLLAR ,"Dollar"}, - {KEY_PERCENT ,"Percent"}, - {KEY_AMPERSAND ,"Ampersand"}, - {KEY_APOSTROPHE ,"Apostrophe"}, - {KEY_PARENLEFT ,"ParenLeft"}, - {KEY_PARENRIGHT ,"ParenRight"}, - {KEY_ASTERISK ,"Asterisk"}, - {KEY_PLUS ,"Plus"}, - {KEY_COMMA ,"Comma"}, - {KEY_MINUS ,"Minus"}, - {KEY_PERIOD ,"Period"}, - {KEY_SLASH ,"Slash"}, - {KEY_0 ,"0"}, - {KEY_1 ,"1"}, - {KEY_2 ,"2"}, - {KEY_3 ,"3"}, - {KEY_4 ,"4"}, - {KEY_5 ,"5"}, - {KEY_6 ,"6"}, - {KEY_7 ,"7"}, - {KEY_8 ,"8"}, - {KEY_9 ,"9"}, - {KEY_COLON ,"Colon"}, - {KEY_SEMICOLON ,"Semicolon"}, - {KEY_LESS ,"Less"}, - {KEY_EQUAL ,"Equal"}, - {KEY_GREATER ,"Greater"}, - {KEY_QUESTION ,"Question"}, - {KEY_AT ,"At"}, - {KEY_A ,"A"}, - {KEY_B ,"B"}, - {KEY_C ,"C"}, - {KEY_D ,"D"}, - {KEY_E ,"E"}, - {KEY_F ,"F"}, - {KEY_G ,"G"}, - {KEY_H ,"H"}, - {KEY_I ,"I"}, - {KEY_J ,"J"}, - {KEY_K ,"K"}, - {KEY_L ,"L"}, - {KEY_M ,"M"}, - {KEY_N ,"N"}, - {KEY_O ,"O"}, - {KEY_P ,"P"}, - {KEY_Q ,"Q"}, - {KEY_R ,"R"}, - {KEY_S ,"S"}, - {KEY_T ,"T"}, - {KEY_U ,"U"}, - {KEY_V ,"V"}, - {KEY_W ,"W"}, - {KEY_X ,"X"}, - {KEY_Y ,"Y"}, - {KEY_Z ,"Z"}, - {KEY_BRACKETLEFT ,"BracketLeft"}, - {KEY_BACKSLASH ,"BackSlash"}, - {KEY_BRACKETRIGHT ,"BracketRight"}, - {KEY_ASCIICIRCUM ,"AsciiCircum"}, - {KEY_UNDERSCORE ,"UnderScore"}, - {KEY_QUOTELEFT ,"QuoteLeft"}, - {KEY_BRACELEFT ,"BraceLeft"}, - {KEY_BAR ,"Bar"}, - {KEY_BRACERIGHT ,"BraceRight"}, - {KEY_ASCIITILDE ,"AsciiTilde"}, - {KEY_NOBREAKSPACE ,"NoBreakSpace"}, - {KEY_EXCLAMDOWN ,"ExclamDown"}, - {KEY_CENT ,"Cent"}, - {KEY_STERLING ,"Sterling"}, - {KEY_CURRENCY ,"Currency"}, - {KEY_YEN ,"Yen"}, - {KEY_BROKENBAR ,"BrokenBar"}, - {KEY_SECTION ,"Section"}, - {KEY_DIAERESIS ,"Diaeresis"}, - {KEY_COPYRIGHT ,"Copyright"}, - {KEY_ORDFEMININE ,"Ordfeminine"}, - {KEY_GUILLEMOTLEFT ,"GuillemotLeft"}, - {KEY_NOTSIGN ,"NotSign"}, - {KEY_HYPHEN ,"Hyphen"}, - {KEY_REGISTERED ,"Registered"}, - {KEY_MACRON ,"Macron"}, - {KEY_DEGREE ,"Degree"}, - {KEY_PLUSMINUS ,"PlusMinus"}, - {KEY_TWOSUPERIOR ,"TwoSuperior"}, - {KEY_THREESUPERIOR ,"ThreeSuperior"}, - {KEY_ACUTE ,"Acute"}, - {KEY_MU ,"Mu"}, - {KEY_PARAGRAPH ,"Paragraph"}, - {KEY_PERIODCENTERED ,"PeriodCentered"}, - {KEY_CEDILLA ,"Cedilla"}, - {KEY_ONESUPERIOR ,"OneSuperior"}, - {KEY_MASCULINE ,"Masculine"}, - {KEY_GUILLEMOTRIGHT ,"GuillemotRight"}, - {KEY_ONEQUARTER ,"OneQuarter"}, - {KEY_ONEHALF ,"OneHalf"}, - {KEY_THREEQUARTERS ,"ThreeQuarters"}, - {KEY_QUESTIONDOWN ,"QuestionDown"}, - {KEY_AGRAVE ,"Agrave"}, - {KEY_AACUTE ,"Aacute"}, - {KEY_ACIRCUMFLEX ,"AcircumFlex"}, - {KEY_ATILDE ,"Atilde"}, - {KEY_ADIAERESIS ,"Adiaeresis"}, - {KEY_ARING ,"Aring"}, - {KEY_AE ,"Ae"}, - {KEY_CCEDILLA ,"Ccedilla"}, - {KEY_EGRAVE ,"Egrave"}, - {KEY_EACUTE ,"Eacute"}, - {KEY_ECIRCUMFLEX ,"Ecircumflex"}, - {KEY_EDIAERESIS ,"Ediaeresis"}, - {KEY_IGRAVE ,"Igrave"}, - {KEY_IACUTE ,"Iacute"}, - {KEY_ICIRCUMFLEX ,"Icircumflex"}, - {KEY_IDIAERESIS ,"Idiaeresis"}, - {KEY_ETH ,"Eth"}, - {KEY_NTILDE ,"Ntilde"}, - {KEY_OGRAVE ,"Ograve"}, - {KEY_OACUTE ,"Oacute"}, - {KEY_OCIRCUMFLEX ,"Ocircumflex"}, - {KEY_OTILDE ,"Otilde"}, - {KEY_ODIAERESIS ,"Odiaeresis"}, - {KEY_MULTIPLY ,"Multiply"}, - {KEY_OOBLIQUE ,"Ooblique"}, - {KEY_UGRAVE ,"Ugrave"}, - {KEY_UACUTE ,"Uacute"}, - {KEY_UCIRCUMFLEX ,"Ucircumflex"}, - {KEY_UDIAERESIS ,"Udiaeresis"}, - {KEY_YACUTE ,"Yacute"}, - {KEY_THORN ,"Thorn"}, - {KEY_SSHARP ,"Ssharp"}, - - {KEY_DIVISION ,"Division"}, - {KEY_YDIAERESIS ,"Ydiaeresis"}, - {0 ,nullptr} + {Key::ALT ,"Alt"}, + {Key::CAPSLOCK ,"CapsLock"}, + {Key::NUMLOCK ,"NumLock"}, + {Key::SCROLLLOCK ,"ScrollLock"}, + {Key::F1 ,"F1"}, + {Key::F2 ,"F2"}, + {Key::F3 ,"F3"}, + {Key::F4 ,"F4"}, + {Key::F5 ,"F5"}, + {Key::F6 ,"F6"}, + {Key::F7 ,"F7"}, + {Key::F8 ,"F8"}, + {Key::F9 ,"F9"}, + {Key::F10 ,"F10"}, + {Key::F11 ,"F11"}, + {Key::F12 ,"F12"}, + {Key::F13 ,"F13"}, + {Key::F14 ,"F14"}, + {Key::F15 ,"F15"}, + {Key::F16 ,"F16"}, + {Key::KP_MULTIPLY ,"Kp Multiply"}, + {Key::KP_DIVIDE ,"Kp Divide"}, + {Key::KP_SUBTRACT ,"Kp Subtract"}, + {Key::KP_PERIOD ,"Kp Period"}, + {Key::KP_ADD ,"Kp Add"}, + {Key::KP_0 ,"Kp 0"}, + {Key::KP_1 ,"Kp 1"}, + {Key::KP_2 ,"Kp 2"}, + {Key::KP_3 ,"Kp 3"}, + {Key::KP_4 ,"Kp 4"}, + {Key::KP_5 ,"Kp 5"}, + {Key::KP_6 ,"Kp 6"}, + {Key::KP_7 ,"Kp 7"}, + {Key::KP_8 ,"Kp 8"}, + {Key::KP_9 ,"Kp 9"}, + {Key::SUPER_L ,"Super L"}, + {Key::SUPER_R ,"Super R"}, + {Key::MENU ,"Menu"}, + {Key::HYPER_L ,"Hyper L"}, + {Key::HYPER_R ,"Hyper R"}, + {Key::HELP ,"Help"}, + {Key::DIRECTION_L ,"Direction L"}, + {Key::DIRECTION_R ,"Direction R"}, + {Key::BACK ,"Back"}, + {Key::FORWARD ,"Forward"}, + {Key::STOP ,"Stop"}, + {Key::REFRESH ,"Refresh"}, + {Key::VOLUMEDOWN ,"VolumeDown"}, + {Key::VOLUMEMUTE ,"VolumeMute"}, + {Key::VOLUMEUP ,"VolumeUp"}, + {Key::BASSBOOST ,"BassBoost"}, + {Key::BASSUP ,"BassUp"}, + {Key::BASSDOWN ,"BassDown"}, + {Key::TREBLEUP ,"TrebleUp"}, + {Key::TREBLEDOWN ,"TrebleDown"}, + {Key::MEDIAPLAY ,"MediaPlay"}, + {Key::MEDIASTOP ,"MediaStop"}, + {Key::MEDIAPREVIOUS ,"MediaPrevious"}, + {Key::MEDIANEXT ,"MediaNext"}, + {Key::MEDIARECORD ,"MediaRecord"}, + {Key::HOMEPAGE ,"HomePage"}, + {Key::FAVORITES ,"Favorites"}, + {Key::SEARCH ,"Search"}, + {Key::STANDBY ,"StandBy"}, + {Key::LAUNCHMAIL ,"LaunchMail"}, + {Key::LAUNCHMEDIA ,"LaunchMedia"}, + {Key::LAUNCH0 ,"Launch0"}, + {Key::LAUNCH1 ,"Launch1"}, + {Key::LAUNCH2 ,"Launch2"}, + {Key::LAUNCH3 ,"Launch3"}, + {Key::LAUNCH4 ,"Launch4"}, + {Key::LAUNCH5 ,"Launch5"}, + {Key::LAUNCH6 ,"Launch6"}, + {Key::LAUNCH7 ,"Launch7"}, + {Key::LAUNCH8 ,"Launch8"}, + {Key::LAUNCH9 ,"Launch9"}, + {Key::LAUNCHA ,"LaunchA"}, + {Key::LAUNCHB ,"LaunchB"}, + {Key::LAUNCHC ,"LaunchC"}, + {Key::LAUNCHD ,"LaunchD"}, + {Key::LAUNCHE ,"LaunchE"}, + {Key::LAUNCHF ,"LaunchF"}, + {Key::UNKNOWN ,"Unknown"}, + {Key::SPACE ,"Space"}, + {Key::EXCLAM ,"Exclam"}, + {Key::QUOTEDBL ,"QuoteDbl"}, + {Key::NUMBERSIGN ,"NumberSign"}, + {Key::DOLLAR ,"Dollar"}, + {Key::PERCENT ,"Percent"}, + {Key::AMPERSAND ,"Ampersand"}, + {Key::APOSTROPHE ,"Apostrophe"}, + {Key::PARENLEFT ,"ParenLeft"}, + {Key::PARENRIGHT ,"ParenRight"}, + {Key::ASTERISK ,"Asterisk"}, + {Key::PLUS ,"Plus"}, + {Key::COMMA ,"Comma"}, + {Key::MINUS ,"Minus"}, + {Key::PERIOD ,"Period"}, + {Key::SLASH ,"Slash"}, + {Key::KEY_0 ,"0"}, + {Key::KEY_1 ,"1"}, + {Key::KEY_2 ,"2"}, + {Key::KEY_3 ,"3"}, + {Key::KEY_4 ,"4"}, + {Key::KEY_5 ,"5"}, + {Key::KEY_6 ,"6"}, + {Key::KEY_7 ,"7"}, + {Key::KEY_8 ,"8"}, + {Key::KEY_9 ,"9"}, + {Key::COLON ,"Colon"}, + {Key::SEMICOLON ,"Semicolon"}, + {Key::LESS ,"Less"}, + {Key::EQUAL ,"Equal"}, + {Key::GREATER ,"Greater"}, + {Key::QUESTION ,"Question"}, + {Key::AT ,"At"}, + {Key::A ,"A"}, + {Key::B ,"B"}, + {Key::C ,"C"}, + {Key::D ,"D"}, + {Key::E ,"E"}, + {Key::F ,"F"}, + {Key::G ,"G"}, + {Key::H ,"H"}, + {Key::I ,"I"}, + {Key::J ,"J"}, + {Key::K ,"K"}, + {Key::L ,"L"}, + {Key::M ,"M"}, + {Key::N ,"N"}, + {Key::O ,"O"}, + {Key::P ,"P"}, + {Key::Q ,"Q"}, + {Key::R ,"R"}, + {Key::S ,"S"}, + {Key::T ,"T"}, + {Key::U ,"U"}, + {Key::V ,"V"}, + {Key::W ,"W"}, + {Key::X ,"X"}, + {Key::Y ,"Y"}, + {Key::Z ,"Z"}, + {Key::BRACKETLEFT ,"BracketLeft"}, + {Key::BACKSLASH ,"BackSlash"}, + {Key::BRACKETRIGHT ,"BracketRight"}, + {Key::ASCIICIRCUM ,"AsciiCircum"}, + {Key::UNDERSCORE ,"UnderScore"}, + {Key::QUOTELEFT ,"QuoteLeft"}, + {Key::BRACELEFT ,"BraceLeft"}, + {Key::BAR ,"Bar"}, + {Key::BRACERIGHT ,"BraceRight"}, + {Key::ASCIITILDE ,"AsciiTilde"}, + {Key::NOBREAKSPACE ,"NoBreakSpace"}, + {Key::EXCLAMDOWN ,"ExclamDown"}, + {Key::CENT ,"Cent"}, + {Key::STERLING ,"Sterling"}, + {Key::CURRENCY ,"Currency"}, + {Key::YEN ,"Yen"}, + {Key::BROKENBAR ,"BrokenBar"}, + {Key::SECTION ,"Section"}, + {Key::DIAERESIS ,"Diaeresis"}, + {Key::COPYRIGHT ,"Copyright"}, + {Key::ORDFEMININE ,"Ordfeminine"}, + {Key::GUILLEMOTLEFT ,"GuillemotLeft"}, + {Key::NOTSIGN ,"NotSign"}, + {Key::HYPHEN ,"Hyphen"}, + {Key::KEY_REGISTERED ,"Registered"}, + {Key::MACRON ,"Macron"}, + {Key::DEGREE ,"Degree"}, + {Key::PLUSMINUS ,"PlusMinus"}, + {Key::TWOSUPERIOR ,"TwoSuperior"}, + {Key::THREESUPERIOR ,"ThreeSuperior"}, + {Key::ACUTE ,"Acute"}, + {Key::MU ,"Mu"}, + {Key::PARAGRAPH ,"Paragraph"}, + {Key::PERIODCENTERED ,"PeriodCentered"}, + {Key::CEDILLA ,"Cedilla"}, + {Key::ONESUPERIOR ,"OneSuperior"}, + {Key::MASCULINE ,"Masculine"}, + {Key::GUILLEMOTRIGHT ,"GuillemotRight"}, + {Key::ONEQUARTER ,"OneQuarter"}, + {Key::ONEHALF ,"OneHalf"}, + {Key::THREEQUARTERS ,"ThreeQuarters"}, + {Key::QUESTIONDOWN ,"QuestionDown"}, + {Key::AGRAVE ,"Agrave"}, + {Key::AACUTE ,"Aacute"}, + {Key::ACIRCUMFLEX ,"AcircumFlex"}, + {Key::ATILDE ,"Atilde"}, + {Key::ADIAERESIS ,"Adiaeresis"}, + {Key::ARING ,"Aring"}, + {Key::AE ,"Ae"}, + {Key::CCEDILLA ,"Ccedilla"}, + {Key::EGRAVE ,"Egrave"}, + {Key::EACUTE ,"Eacute"}, + {Key::ECIRCUMFLEX ,"Ecircumflex"}, + {Key::EDIAERESIS ,"Ediaeresis"}, + {Key::IGRAVE ,"Igrave"}, + {Key::IACUTE ,"Iacute"}, + {Key::ICIRCUMFLEX ,"Icircumflex"}, + {Key::IDIAERESIS ,"Idiaeresis"}, + {Key::ETH ,"Eth"}, + {Key::NTILDE ,"Ntilde"}, + {Key::OGRAVE ,"Ograve"}, + {Key::OACUTE ,"Oacute"}, + {Key::OCIRCUMFLEX ,"Ocircumflex"}, + {Key::OTILDE ,"Otilde"}, + {Key::ODIAERESIS ,"Odiaeresis"}, + {Key::MULTIPLY ,"Multiply"}, + {Key::OOBLIQUE ,"Ooblique"}, + {Key::UGRAVE ,"Ugrave"}, + {Key::UACUTE ,"Uacute"}, + {Key::UCIRCUMFLEX ,"Ucircumflex"}, + {Key::UDIAERESIS ,"Udiaeresis"}, + {Key::YACUTE ,"Yacute"}, + {Key::THORN ,"Thorn"}, + {Key::SSHARP ,"Ssharp"}, + {Key::DIVISION ,"Division"}, + {Key::YDIAERESIS ,"Ydiaeresis"}, + {Key::NONE ,nullptr} /* clang-format on */ }; -bool keycode_has_unicode(uint32_t p_keycode) { +bool keycode_has_unicode(Key p_keycode) { switch (p_keycode) { - case KEY_ESCAPE: - case KEY_TAB: - case KEY_BACKTAB: - case KEY_BACKSPACE: - case KEY_ENTER: - case KEY_KP_ENTER: - case KEY_INSERT: - case KEY_DELETE: - case KEY_PAUSE: - case KEY_PRINT: - case KEY_SYSREQ: - case KEY_CLEAR: - case KEY_HOME: - case KEY_END: - case KEY_LEFT: - case KEY_UP: - case KEY_RIGHT: - case KEY_DOWN: - case KEY_PAGEUP: - case KEY_PAGEDOWN: - case KEY_SHIFT: - case KEY_CTRL: - case KEY_META: - case KEY_ALT: - case KEY_CAPSLOCK: - case KEY_NUMLOCK: - case KEY_SCROLLLOCK: - case KEY_F1: - case KEY_F2: - case KEY_F3: - case KEY_F4: - case KEY_F5: - case KEY_F6: - case KEY_F7: - case KEY_F8: - case KEY_F9: - case KEY_F10: - case KEY_F11: - case KEY_F12: - case KEY_F13: - case KEY_F14: - case KEY_F15: - case KEY_F16: - case KEY_SUPER_L: - case KEY_SUPER_R: - case KEY_MENU: - case KEY_HYPER_L: - case KEY_HYPER_R: - case KEY_HELP: - case KEY_DIRECTION_L: - case KEY_DIRECTION_R: - case KEY_BACK: - case KEY_FORWARD: - case KEY_STOP: - case KEY_REFRESH: - case KEY_VOLUMEDOWN: - case KEY_VOLUMEMUTE: - case KEY_VOLUMEUP: - case KEY_BASSBOOST: - case KEY_BASSUP: - case KEY_BASSDOWN: - case KEY_TREBLEUP: - case KEY_TREBLEDOWN: - case KEY_MEDIAPLAY: - case KEY_MEDIASTOP: - case KEY_MEDIAPREVIOUS: - case KEY_MEDIANEXT: - case KEY_MEDIARECORD: - case KEY_HOMEPAGE: - case KEY_FAVORITES: - case KEY_SEARCH: - case KEY_STANDBY: - case KEY_OPENURL: - case KEY_LAUNCHMAIL: - case KEY_LAUNCHMEDIA: - case KEY_LAUNCH0: - case KEY_LAUNCH1: - case KEY_LAUNCH2: - case KEY_LAUNCH3: - case KEY_LAUNCH4: - case KEY_LAUNCH5: - case KEY_LAUNCH6: - case KEY_LAUNCH7: - case KEY_LAUNCH8: - case KEY_LAUNCH9: - case KEY_LAUNCHA: - case KEY_LAUNCHB: - case KEY_LAUNCHC: - case KEY_LAUNCHD: - case KEY_LAUNCHE: - case KEY_LAUNCHF: + case Key::ESCAPE: + case Key::TAB: + case Key::BACKTAB: + case Key::BACKSPACE: + case Key::ENTER: + case Key::KP_ENTER: + case Key::INSERT: + case Key::KEY_DELETE: + case Key::PAUSE: + case Key::PRINT: + case Key::SYSREQ: + case Key::CLEAR: + case Key::HOME: + case Key::END: + case Key::LEFT: + case Key::UP: + case Key::RIGHT: + case Key::DOWN: + case Key::PAGEUP: + case Key::PAGEDOWN: + case Key::SHIFT: + case Key::CTRL: + case Key::META: + case Key::ALT: + case Key::CAPSLOCK: + case Key::NUMLOCK: + case Key::SCROLLLOCK: + case Key::F1: + case Key::F2: + case Key::F3: + case Key::F4: + case Key::F5: + case Key::F6: + case Key::F7: + case Key::F8: + case Key::F9: + case Key::F10: + case Key::F11: + case Key::F12: + case Key::F13: + case Key::F14: + case Key::F15: + case Key::F16: + case Key::SUPER_L: + case Key::SUPER_R: + case Key::MENU: + case Key::HYPER_L: + case Key::HYPER_R: + case Key::HELP: + case Key::DIRECTION_L: + case Key::DIRECTION_R: + case Key::BACK: + case Key::FORWARD: + case Key::STOP: + case Key::REFRESH: + case Key::VOLUMEDOWN: + case Key::VOLUMEMUTE: + case Key::VOLUMEUP: + case Key::BASSBOOST: + case Key::BASSUP: + case Key::BASSDOWN: + case Key::TREBLEUP: + case Key::TREBLEDOWN: + case Key::MEDIAPLAY: + case Key::MEDIASTOP: + case Key::MEDIAPREVIOUS: + case Key::MEDIANEXT: + case Key::MEDIARECORD: + case Key::HOMEPAGE: + case Key::FAVORITES: + case Key::SEARCH: + case Key::STANDBY: + case Key::OPENURL: + case Key::LAUNCHMAIL: + case Key::LAUNCHMEDIA: + case Key::LAUNCH0: + case Key::LAUNCH1: + case Key::LAUNCH2: + case Key::LAUNCH3: + case Key::LAUNCH4: + case Key::LAUNCH5: + case Key::LAUNCH6: + case Key::LAUNCH7: + case Key::LAUNCH8: + case Key::LAUNCH9: + case Key::LAUNCHA: + case Key::LAUNCHB: + case Key::LAUNCHC: + case Key::LAUNCHD: + case Key::LAUNCHE: + case Key::LAUNCHF: return false; + default: { + } } return true; } -String keycode_get_string(uint32_t p_code) { +String keycode_get_string(Key p_code) { String codestr; - if (p_code & KEY_MASK_SHIFT) { - codestr += find_keycode_name(KEY_SHIFT); + if ((p_code & KeyModifierMask::SHIFT) != Key::NONE) { + codestr += find_keycode_name(Key::SHIFT); codestr += "+"; } - if (p_code & KEY_MASK_ALT) { - codestr += find_keycode_name(KEY_ALT); + if ((p_code & KeyModifierMask::ALT) != Key::NONE) { + codestr += find_keycode_name(Key::ALT); codestr += "+"; } - if (p_code & KEY_MASK_CTRL) { - codestr += find_keycode_name(KEY_CTRL); + if ((p_code & KeyModifierMask::CTRL) != Key::NONE) { + codestr += find_keycode_name(Key::CTRL); codestr += "+"; } - if (p_code & KEY_MASK_META) { - codestr += find_keycode_name(KEY_META); + if ((p_code & KeyModifierMask::META) != Key::NONE) { + codestr += find_keycode_name(Key::META); codestr += "+"; } - p_code &= KEY_CODE_MASK; + p_code &= KeyModifierMask::CODE_MASK; const _KeyCodeText *kct = &_keycodes[0]; while (kct->text) { - if (kct->code == (int)p_code) { + if (kct->code == p_code) { codestr += kct->text; return codestr; } kct++; } - codestr += String::chr(p_code); + codestr += String::chr((char32_t)p_code); return codestr; } -int find_keycode(const String &p_code) { +Key find_keycode(const String &p_code) { const _KeyCodeText *kct = &_keycodes[0]; while (kct->text) { @@ -436,10 +435,10 @@ int find_keycode(const String &p_code) { kct++; } - return 0; + return Key::NONE; } -const char *find_keycode_name(int p_keycode) { +const char *find_keycode_name(Key p_keycode) { const _KeyCodeText *kct = &_keycodes[0]; while (kct->text) { @@ -464,7 +463,7 @@ int keycode_get_count() { } int keycode_get_value_by_index(int p_index) { - return _keycodes[p_index].code; + return (int)_keycodes[p_index].code; } const char *keycode_get_name_by_index(int p_index) { diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 52174432d9..3176a8a210 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -33,148 +33,142 @@ #include "core/string/ustring.h" -/* - Special Key: - - The strategy here is similar to the one used by toolkits, - which consists in leaving the 24 bits unicode range for printable - characters, and use the upper 8 bits for special keys and - modifiers. This way everything (char/keycode) can fit nicely in one 32 bits unsigned integer. -*/ -enum { - SPKEY = (1 << 24) -}; - -enum Key { - KEY_NONE = 0, +enum class Key { + NONE = 0, + // Special key: The strategy here is similar to the one used by toolkits, + // which consists in leaving the 24 bits unicode range for printable + // characters, and use the upper 8 bits for special keys and modifiers. + // This way everything (char/keycode) can fit nicely in one 32-bit + // integer (the enum's underlying type is `int` by default). + SPECIAL = (1 << 24), /* CURSOR/FUNCTION/BROWSER/MULTIMEDIA/MISC KEYS */ - KEY_ESCAPE = SPKEY | 0x01, - KEY_TAB = SPKEY | 0x02, - KEY_BACKTAB = SPKEY | 0x03, - KEY_BACKSPACE = SPKEY | 0x04, - KEY_ENTER = SPKEY | 0x05, - KEY_KP_ENTER = SPKEY | 0x06, - KEY_INSERT = SPKEY | 0x07, - KEY_DELETE = SPKEY | 0x08, - KEY_PAUSE = SPKEY | 0x09, - KEY_PRINT = SPKEY | 0x0A, - KEY_SYSREQ = SPKEY | 0x0B, - KEY_CLEAR = SPKEY | 0x0C, - KEY_HOME = SPKEY | 0x0D, - KEY_END = SPKEY | 0x0E, - KEY_LEFT = SPKEY | 0x0F, - KEY_UP = SPKEY | 0x10, - KEY_RIGHT = SPKEY | 0x11, - KEY_DOWN = SPKEY | 0x12, - KEY_PAGEUP = SPKEY | 0x13, - KEY_PAGEDOWN = SPKEY | 0x14, - KEY_SHIFT = SPKEY | 0x15, - KEY_CTRL = SPKEY | 0x16, - KEY_META = SPKEY | 0x17, - KEY_ALT = SPKEY | 0x18, - KEY_CAPSLOCK = SPKEY | 0x19, - KEY_NUMLOCK = SPKEY | 0x1A, - KEY_SCROLLLOCK = SPKEY | 0x1B, - KEY_F1 = SPKEY | 0x1C, - KEY_F2 = SPKEY | 0x1D, - KEY_F3 = SPKEY | 0x1E, - KEY_F4 = SPKEY | 0x1F, - KEY_F5 = SPKEY | 0x20, - KEY_F6 = SPKEY | 0x21, - KEY_F7 = SPKEY | 0x22, - KEY_F8 = SPKEY | 0x23, - KEY_F9 = SPKEY | 0x24, - KEY_F10 = SPKEY | 0x25, - KEY_F11 = SPKEY | 0x26, - KEY_F12 = SPKEY | 0x27, - KEY_F13 = SPKEY | 0x28, - KEY_F14 = SPKEY | 0x29, - KEY_F15 = SPKEY | 0x2A, - KEY_F16 = SPKEY | 0x2B, - KEY_KP_MULTIPLY = SPKEY | 0x81, - KEY_KP_DIVIDE = SPKEY | 0x82, - KEY_KP_SUBTRACT = SPKEY | 0x83, - KEY_KP_PERIOD = SPKEY | 0x84, - KEY_KP_ADD = SPKEY | 0x85, - KEY_KP_0 = SPKEY | 0x86, - KEY_KP_1 = SPKEY | 0x87, - KEY_KP_2 = SPKEY | 0x88, - KEY_KP_3 = SPKEY | 0x89, - KEY_KP_4 = SPKEY | 0x8A, - KEY_KP_5 = SPKEY | 0x8B, - KEY_KP_6 = SPKEY | 0x8C, - KEY_KP_7 = SPKEY | 0x8D, - KEY_KP_8 = SPKEY | 0x8E, - KEY_KP_9 = SPKEY | 0x8F, - KEY_SUPER_L = SPKEY | 0x2C, - KEY_SUPER_R = SPKEY | 0x2D, - KEY_MENU = SPKEY | 0x2E, - KEY_HYPER_L = SPKEY | 0x2F, - KEY_HYPER_R = SPKEY | 0x30, - KEY_HELP = SPKEY | 0x31, - KEY_DIRECTION_L = SPKEY | 0x32, - KEY_DIRECTION_R = SPKEY | 0x33, - KEY_BACK = SPKEY | 0x40, - KEY_FORWARD = SPKEY | 0x41, - KEY_STOP = SPKEY | 0x42, - KEY_REFRESH = SPKEY | 0x43, - KEY_VOLUMEDOWN = SPKEY | 0x44, - KEY_VOLUMEMUTE = SPKEY | 0x45, - KEY_VOLUMEUP = SPKEY | 0x46, - KEY_BASSBOOST = SPKEY | 0x47, - KEY_BASSUP = SPKEY | 0x48, - KEY_BASSDOWN = SPKEY | 0x49, - KEY_TREBLEUP = SPKEY | 0x4A, - KEY_TREBLEDOWN = SPKEY | 0x4B, - KEY_MEDIAPLAY = SPKEY | 0x4C, - KEY_MEDIASTOP = SPKEY | 0x4D, - KEY_MEDIAPREVIOUS = SPKEY | 0x4E, - KEY_MEDIANEXT = SPKEY | 0x4F, - KEY_MEDIARECORD = SPKEY | 0x50, - KEY_HOMEPAGE = SPKEY | 0x51, - KEY_FAVORITES = SPKEY | 0x52, - KEY_SEARCH = SPKEY | 0x53, - KEY_STANDBY = SPKEY | 0x54, - KEY_OPENURL = SPKEY | 0x55, - KEY_LAUNCHMAIL = SPKEY | 0x56, - KEY_LAUNCHMEDIA = SPKEY | 0x57, - KEY_LAUNCH0 = SPKEY | 0x58, - KEY_LAUNCH1 = SPKEY | 0x59, - KEY_LAUNCH2 = SPKEY | 0x5A, - KEY_LAUNCH3 = SPKEY | 0x5B, - KEY_LAUNCH4 = SPKEY | 0x5C, - KEY_LAUNCH5 = SPKEY | 0x5D, - KEY_LAUNCH6 = SPKEY | 0x5E, - KEY_LAUNCH7 = SPKEY | 0x5F, - KEY_LAUNCH8 = SPKEY | 0x60, - KEY_LAUNCH9 = SPKEY | 0x61, - KEY_LAUNCHA = SPKEY | 0x62, - KEY_LAUNCHB = SPKEY | 0x63, - KEY_LAUNCHC = SPKEY | 0x64, - KEY_LAUNCHD = SPKEY | 0x65, - KEY_LAUNCHE = SPKEY | 0x66, - KEY_LAUNCHF = SPKEY | 0x67, + ESCAPE = SPECIAL | 0x01, + TAB = SPECIAL | 0x02, + BACKTAB = SPECIAL | 0x03, + BACKSPACE = SPECIAL | 0x04, + ENTER = SPECIAL | 0x05, + KP_ENTER = SPECIAL | 0x06, + INSERT = SPECIAL | 0x07, + KEY_DELETE = SPECIAL | 0x08, // "DELETE" is a reserved word on Windows. + PAUSE = SPECIAL | 0x09, + PRINT = SPECIAL | 0x0A, + SYSREQ = SPECIAL | 0x0B, + CLEAR = SPECIAL | 0x0C, + HOME = SPECIAL | 0x0D, + END = SPECIAL | 0x0E, + LEFT = SPECIAL | 0x0F, + UP = SPECIAL | 0x10, + RIGHT = SPECIAL | 0x11, + DOWN = SPECIAL | 0x12, + PAGEUP = SPECIAL | 0x13, + PAGEDOWN = SPECIAL | 0x14, + SHIFT = SPECIAL | 0x15, + CTRL = SPECIAL | 0x16, + META = SPECIAL | 0x17, + ALT = SPECIAL | 0x18, + CAPSLOCK = SPECIAL | 0x19, + NUMLOCK = SPECIAL | 0x1A, + SCROLLLOCK = SPECIAL | 0x1B, + F1 = SPECIAL | 0x1C, + F2 = SPECIAL | 0x1D, + F3 = SPECIAL | 0x1E, + F4 = SPECIAL | 0x1F, + F5 = SPECIAL | 0x20, + F6 = SPECIAL | 0x21, + F7 = SPECIAL | 0x22, + F8 = SPECIAL | 0x23, + F9 = SPECIAL | 0x24, + F10 = SPECIAL | 0x25, + F11 = SPECIAL | 0x26, + F12 = SPECIAL | 0x27, + F13 = SPECIAL | 0x28, + F14 = SPECIAL | 0x29, + F15 = SPECIAL | 0x2A, + F16 = SPECIAL | 0x2B, + KP_MULTIPLY = SPECIAL | 0x81, + KP_DIVIDE = SPECIAL | 0x82, + KP_SUBTRACT = SPECIAL | 0x83, + KP_PERIOD = SPECIAL | 0x84, + KP_ADD = SPECIAL | 0x85, + KP_0 = SPECIAL | 0x86, + KP_1 = SPECIAL | 0x87, + KP_2 = SPECIAL | 0x88, + KP_3 = SPECIAL | 0x89, + KP_4 = SPECIAL | 0x8A, + KP_5 = SPECIAL | 0x8B, + KP_6 = SPECIAL | 0x8C, + KP_7 = SPECIAL | 0x8D, + KP_8 = SPECIAL | 0x8E, + KP_9 = SPECIAL | 0x8F, + SUPER_L = SPECIAL | 0x2C, + SUPER_R = SPECIAL | 0x2D, + MENU = SPECIAL | 0x2E, + HYPER_L = SPECIAL | 0x2F, + HYPER_R = SPECIAL | 0x30, + HELP = SPECIAL | 0x31, + DIRECTION_L = SPECIAL | 0x32, + DIRECTION_R = SPECIAL | 0x33, + BACK = SPECIAL | 0x40, + FORWARD = SPECIAL | 0x41, + STOP = SPECIAL | 0x42, + REFRESH = SPECIAL | 0x43, + VOLUMEDOWN = SPECIAL | 0x44, + VOLUMEMUTE = SPECIAL | 0x45, + VOLUMEUP = SPECIAL | 0x46, + BASSBOOST = SPECIAL | 0x47, + BASSUP = SPECIAL | 0x48, + BASSDOWN = SPECIAL | 0x49, + TREBLEUP = SPECIAL | 0x4A, + TREBLEDOWN = SPECIAL | 0x4B, + MEDIAPLAY = SPECIAL | 0x4C, + MEDIASTOP = SPECIAL | 0x4D, + MEDIAPREVIOUS = SPECIAL | 0x4E, + MEDIANEXT = SPECIAL | 0x4F, + MEDIARECORD = SPECIAL | 0x50, + HOMEPAGE = SPECIAL | 0x51, + FAVORITES = SPECIAL | 0x52, + SEARCH = SPECIAL | 0x53, + STANDBY = SPECIAL | 0x54, + OPENURL = SPECIAL | 0x55, + LAUNCHMAIL = SPECIAL | 0x56, + LAUNCHMEDIA = SPECIAL | 0x57, + LAUNCH0 = SPECIAL | 0x58, + LAUNCH1 = SPECIAL | 0x59, + LAUNCH2 = SPECIAL | 0x5A, + LAUNCH3 = SPECIAL | 0x5B, + LAUNCH4 = SPECIAL | 0x5C, + LAUNCH5 = SPECIAL | 0x5D, + LAUNCH6 = SPECIAL | 0x5E, + LAUNCH7 = SPECIAL | 0x5F, + LAUNCH8 = SPECIAL | 0x60, + LAUNCH9 = SPECIAL | 0x61, + LAUNCHA = SPECIAL | 0x62, + LAUNCHB = SPECIAL | 0x63, + LAUNCHC = SPECIAL | 0x64, + LAUNCHD = SPECIAL | 0x65, + LAUNCHE = SPECIAL | 0x66, + LAUNCHF = SPECIAL | 0x67, - KEY_UNKNOWN = SPKEY | 0xFFFFFF, + UNKNOWN = SPECIAL | 0xFFFFFF, /* PRINTABLE LATIN 1 CODES */ - KEY_SPACE = 0x0020, - KEY_EXCLAM = 0x0021, - KEY_QUOTEDBL = 0x0022, - KEY_NUMBERSIGN = 0x0023, - KEY_DOLLAR = 0x0024, - KEY_PERCENT = 0x0025, - KEY_AMPERSAND = 0x0026, - KEY_APOSTROPHE = 0x0027, - KEY_PARENLEFT = 0x0028, - KEY_PARENRIGHT = 0x0029, - KEY_ASTERISK = 0x002A, - KEY_PLUS = 0x002B, - KEY_COMMA = 0x002C, - KEY_MINUS = 0x002D, - KEY_PERIOD = 0x002E, - KEY_SLASH = 0x002F, + SPACE = 0x0020, + EXCLAM = 0x0021, + QUOTEDBL = 0x0022, + NUMBERSIGN = 0x0023, + DOLLAR = 0x0024, + PERCENT = 0x0025, + AMPERSAND = 0x0026, + APOSTROPHE = 0x0027, + PARENLEFT = 0x0028, + PARENRIGHT = 0x0029, + ASTERISK = 0x002A, + PLUS = 0x002B, + COMMA = 0x002C, + MINUS = 0x002D, + PERIOD = 0x002E, + SLASH = 0x002F, KEY_0 = 0x0030, KEY_1 = 0x0031, KEY_2 = 0x0032, @@ -185,134 +179,133 @@ enum Key { KEY_7 = 0x0037, KEY_8 = 0x0038, KEY_9 = 0x0039, - KEY_COLON = 0x003A, - KEY_SEMICOLON = 0x003B, - KEY_LESS = 0x003C, - KEY_EQUAL = 0x003D, - KEY_GREATER = 0x003E, - KEY_QUESTION = 0x003F, - KEY_AT = 0x0040, - KEY_A = 0x0041, - KEY_B = 0x0042, - KEY_C = 0x0043, - KEY_D = 0x0044, - KEY_E = 0x0045, - KEY_F = 0x0046, - KEY_G = 0x0047, - KEY_H = 0x0048, - KEY_I = 0x0049, - KEY_J = 0x004A, - KEY_K = 0x004B, - KEY_L = 0x004C, - KEY_M = 0x004D, - KEY_N = 0x004E, - KEY_O = 0x004F, - KEY_P = 0x0050, - KEY_Q = 0x0051, - KEY_R = 0x0052, - KEY_S = 0x0053, - KEY_T = 0x0054, - KEY_U = 0x0055, - KEY_V = 0x0056, - KEY_W = 0x0057, - KEY_X = 0x0058, - KEY_Y = 0x0059, - KEY_Z = 0x005A, - KEY_BRACKETLEFT = 0x005B, - KEY_BACKSLASH = 0x005C, - KEY_BRACKETRIGHT = 0x005D, - KEY_ASCIICIRCUM = 0x005E, - KEY_UNDERSCORE = 0x005F, - KEY_QUOTELEFT = 0x0060, - KEY_BRACELEFT = 0x007B, - KEY_BAR = 0x007C, - KEY_BRACERIGHT = 0x007D, - KEY_ASCIITILDE = 0x007E, - KEY_NOBREAKSPACE = 0x00A0, - KEY_EXCLAMDOWN = 0x00A1, - KEY_CENT = 0x00A2, - KEY_STERLING = 0x00A3, - KEY_CURRENCY = 0x00A4, - KEY_YEN = 0x00A5, - KEY_BROKENBAR = 0x00A6, - KEY_SECTION = 0x00A7, - KEY_DIAERESIS = 0x00A8, - KEY_COPYRIGHT = 0x00A9, - KEY_ORDFEMININE = 0x00AA, - KEY_GUILLEMOTLEFT = 0x00AB, - KEY_NOTSIGN = 0x00AC, - KEY_HYPHEN = 0x00AD, - KEY_REGISTERED = 0x00AE, - KEY_MACRON = 0x00AF, - KEY_DEGREE = 0x00B0, - KEY_PLUSMINUS = 0x00B1, - KEY_TWOSUPERIOR = 0x00B2, - KEY_THREESUPERIOR = 0x00B3, - KEY_ACUTE = 0x00B4, - KEY_MU = 0x00B5, - KEY_PARAGRAPH = 0x00B6, - KEY_PERIODCENTERED = 0x00B7, - KEY_CEDILLA = 0x00B8, - KEY_ONESUPERIOR = 0x00B9, - KEY_MASCULINE = 0x00BA, - KEY_GUILLEMOTRIGHT = 0x00BB, - KEY_ONEQUARTER = 0x00BC, - KEY_ONEHALF = 0x00BD, - KEY_THREEQUARTERS = 0x00BE, - KEY_QUESTIONDOWN = 0x00BF, - KEY_AGRAVE = 0x00C0, - KEY_AACUTE = 0x00C1, - KEY_ACIRCUMFLEX = 0x00C2, - KEY_ATILDE = 0x00C3, - KEY_ADIAERESIS = 0x00C4, - KEY_ARING = 0x00C5, - KEY_AE = 0x00C6, - KEY_CCEDILLA = 0x00C7, - KEY_EGRAVE = 0x00C8, - KEY_EACUTE = 0x00C9, - KEY_ECIRCUMFLEX = 0x00CA, - KEY_EDIAERESIS = 0x00CB, - KEY_IGRAVE = 0x00CC, - KEY_IACUTE = 0x00CD, - KEY_ICIRCUMFLEX = 0x00CE, - KEY_IDIAERESIS = 0x00CF, - KEY_ETH = 0x00D0, - KEY_NTILDE = 0x00D1, - KEY_OGRAVE = 0x00D2, - KEY_OACUTE = 0x00D3, - KEY_OCIRCUMFLEX = 0x00D4, - KEY_OTILDE = 0x00D5, - KEY_ODIAERESIS = 0x00D6, - KEY_MULTIPLY = 0x00D7, - KEY_OOBLIQUE = 0x00D8, - KEY_UGRAVE = 0x00D9, - KEY_UACUTE = 0x00DA, - KEY_UCIRCUMFLEX = 0x00DB, - KEY_UDIAERESIS = 0x00DC, - KEY_YACUTE = 0x00DD, - KEY_THORN = 0x00DE, - KEY_SSHARP = 0x00DF, + COLON = 0x003A, + SEMICOLON = 0x003B, + LESS = 0x003C, + EQUAL = 0x003D, + GREATER = 0x003E, + QUESTION = 0x003F, + AT = 0x0040, + A = 0x0041, + B = 0x0042, + C = 0x0043, + D = 0x0044, + E = 0x0045, + F = 0x0046, + G = 0x0047, + H = 0x0048, + I = 0x0049, + J = 0x004A, + K = 0x004B, + L = 0x004C, + M = 0x004D, + N = 0x004E, + O = 0x004F, + P = 0x0050, + Q = 0x0051, + R = 0x0052, + S = 0x0053, + T = 0x0054, + U = 0x0055, + V = 0x0056, + W = 0x0057, + X = 0x0058, + Y = 0x0059, + Z = 0x005A, + BRACKETLEFT = 0x005B, + BACKSLASH = 0x005C, + BRACKETRIGHT = 0x005D, + ASCIICIRCUM = 0x005E, + UNDERSCORE = 0x005F, + QUOTELEFT = 0x0060, + BRACELEFT = 0x007B, + BAR = 0x007C, + BRACERIGHT = 0x007D, + ASCIITILDE = 0x007E, + NOBREAKSPACE = 0x00A0, + EXCLAMDOWN = 0x00A1, + CENT = 0x00A2, + STERLING = 0x00A3, + CURRENCY = 0x00A4, + YEN = 0x00A5, + BROKENBAR = 0x00A6, + SECTION = 0x00A7, + DIAERESIS = 0x00A8, + COPYRIGHT = 0x00A9, + ORDFEMININE = 0x00AA, + GUILLEMOTLEFT = 0x00AB, + NOTSIGN = 0x00AC, + HYPHEN = 0x00AD, + KEY_REGISTERED = 0x00AE, // "REGISTERED" is a reserved word on Windows. + MACRON = 0x00AF, + DEGREE = 0x00B0, + PLUSMINUS = 0x00B1, + TWOSUPERIOR = 0x00B2, + THREESUPERIOR = 0x00B3, + ACUTE = 0x00B4, + MU = 0x00B5, + PARAGRAPH = 0x00B6, + PERIODCENTERED = 0x00B7, + CEDILLA = 0x00B8, + ONESUPERIOR = 0x00B9, + MASCULINE = 0x00BA, + GUILLEMOTRIGHT = 0x00BB, + ONEQUARTER = 0x00BC, + ONEHALF = 0x00BD, + THREEQUARTERS = 0x00BE, + QUESTIONDOWN = 0x00BF, + AGRAVE = 0x00C0, + AACUTE = 0x00C1, + ACIRCUMFLEX = 0x00C2, + ATILDE = 0x00C3, + ADIAERESIS = 0x00C4, + ARING = 0x00C5, + AE = 0x00C6, + CCEDILLA = 0x00C7, + EGRAVE = 0x00C8, + EACUTE = 0x00C9, + ECIRCUMFLEX = 0x00CA, + EDIAERESIS = 0x00CB, + IGRAVE = 0x00CC, + IACUTE = 0x00CD, + ICIRCUMFLEX = 0x00CE, + IDIAERESIS = 0x00CF, + ETH = 0x00D0, + NTILDE = 0x00D1, + OGRAVE = 0x00D2, + OACUTE = 0x00D3, + OCIRCUMFLEX = 0x00D4, + OTILDE = 0x00D5, + ODIAERESIS = 0x00D6, + MULTIPLY = 0x00D7, + OOBLIQUE = 0x00D8, + UGRAVE = 0x00D9, + UACUTE = 0x00DA, + UCIRCUMFLEX = 0x00DB, + UDIAERESIS = 0x00DC, + YACUTE = 0x00DD, + THORN = 0x00DE, + SSHARP = 0x00DF, - KEY_DIVISION = 0x00F7, - KEY_YDIAERESIS = 0x00FF, + DIVISION = 0x00F7, + YDIAERESIS = 0x00FF, + END_LATIN1 = 0x0100, }; -enum KeyModifierMask { - KEY_CODE_MASK = ((1 << 25) - 1), ///< Apply this mask to any keycode to remove modifiers. - KEY_MODIFIER_MASK = (0xFF << 24), ///< Apply this mask to isolate modifiers. - KEY_MASK_SHIFT = (1 << 25), - KEY_MASK_ALT = (1 << 26), - KEY_MASK_META = (1 << 27), - KEY_MASK_CTRL = (1 << 28), +enum class KeyModifierMask { + CODE_MASK = ((1 << 25) - 1), ///< Apply this mask to any keycode to remove modifiers. + MODIFIER_MASK = (0x7F << 24), ///< Apply this mask to isolate modifiers. + SHIFT = (1 << 25), + ALT = (1 << 26), + META = (1 << 27), + CTRL = (1 << 28), #ifdef APPLE_STYLE_KEYS - KEY_MASK_CMD = KEY_MASK_META, + CMD = META, #else - KEY_MASK_CMD = KEY_MASK_CTRL, + CMD = CTRL, #endif - - KEY_MASK_KPAD = (1 << 29), - KEY_MASK_GROUP_SWITCH = (1 << 30) - // bit 31 can't be used because variant uses regular 32 bits int as datatype + KPAD = (1 << 29), + GROUP_SWITCH = (1 << 30) }; // To avoid having unnecessary operators, only define the ones that are needed. @@ -325,10 +318,26 @@ inline Key &operator-=(Key &a, int b) { return (Key &)((int &)a -= b); } +inline Key operator+(Key a, int b) { + return (Key)((int)a + (int)b); +} + inline Key operator+(Key a, Key b) { + return (Key)((int)a + (int)b); +} + +inline Key operator-(Key a, Key b) { return (Key)((int)a - (int)b); } +inline Key operator&(Key a, Key b) { + return (Key)((int)a & (int)b); +} + +inline Key operator|(Key a, Key b) { + return (Key)((int)a | (int)b); +} + inline Key &operator|=(Key &a, Key b) { return (Key &)((int &)a |= (int)b); } @@ -337,6 +346,10 @@ inline Key &operator|=(Key &a, KeyModifierMask b) { return (Key &)((int &)a |= (int)b); } +inline Key &operator&=(Key &a, KeyModifierMask b) { + return (Key &)((int &)a &= (int)b); +} + inline Key operator|(Key a, KeyModifierMask b) { return (Key)((int)a | (int)b); } @@ -361,10 +374,10 @@ inline KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) { return (KeyModifierMask)((int)a | (int)b); } -String keycode_get_string(uint32_t p_code); -bool keycode_has_unicode(uint32_t p_keycode); -int find_keycode(const String &p_code); -const char *find_keycode_name(int p_keycode); +String keycode_get_string(Key p_code); +bool keycode_has_unicode(Key p_keycode); +Key find_keycode(const String &p_code); +const char *find_keycode_name(Key p_keycode); int keycode_get_count(); int keycode_get_value_by_index(int p_index); const char *keycode_get_name_by_index(int p_index); diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index 0ba69a8d47..a96e1989f9 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/main_loop.h b/core/os/main_loop.h index 4da01d767e..2bb1ea2ef4 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -38,7 +38,6 @@ class MainLoop : public Object { GDCLASS(MainLoop, Object); - OBJ_CATEGORY("Main Loop"); Ref<Script> initialize_script; diff --git a/core/os/memory.cpp b/core/os/memory.cpp index a756c1d5dd..6ff0f9ae00 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/memory.h b/core/os/memory.h index ac56a12330..baa96ef3e9 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -186,9 +186,9 @@ void memdelete_arr(T *p_class) { struct _GlobalNil { int color = 1; - _GlobalNil *right; - _GlobalNil *left; - _GlobalNil *parent; + _GlobalNil *right = nullptr; + _GlobalNil *left = nullptr; + _GlobalNil *parent = nullptr; _GlobalNil(); }; diff --git a/core/os/midi_driver.cpp b/core/os/midi_driver.cpp index ee87346dfc..410b62068a 100644 --- a/core/os/midi_driver.cpp +++ b/core/os/midi_driver.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -31,7 +31,6 @@ #include "midi_driver.h" #include "core/input/input.h" -#include "core/os/os.h" uint8_t MIDIDriver::last_received_message = 0x00; MIDIDriver *MIDIDriver::singleton = nullptr; @@ -68,46 +67,46 @@ void MIDIDriver::receive_input_packet(uint64_t timestamp, uint8_t *data, uint32_ } switch (event->get_message()) { - case MIDI_MESSAGE_AFTERTOUCH: + case MIDIMessage::AFTERTOUCH: if (length >= 2 + param_position) { event->set_pitch(data[param_position]); event->set_pressure(data[param_position + 1]); } break; - case MIDI_MESSAGE_CONTROL_CHANGE: + case MIDIMessage::CONTROL_CHANGE: if (length >= 2 + param_position) { event->set_controller_number(data[param_position]); event->set_controller_value(data[param_position + 1]); } break; - case MIDI_MESSAGE_NOTE_ON: - case MIDI_MESSAGE_NOTE_OFF: + case MIDIMessage::NOTE_ON: + case MIDIMessage::NOTE_OFF: if (length >= 2 + param_position) { event->set_pitch(data[param_position]); event->set_velocity(data[param_position + 1]); - if (event->get_message() == MIDI_MESSAGE_NOTE_ON && event->get_velocity() == 0) { + if (event->get_message() == MIDIMessage::NOTE_ON && event->get_velocity() == 0) { // https://www.midi.org/forum/228-writing-midi-software-send-note-off,-or-zero-velocity-note-on - event->set_message(MIDI_MESSAGE_NOTE_OFF); + event->set_message(MIDIMessage::NOTE_OFF); } } break; - case MIDI_MESSAGE_PITCH_BEND: + case MIDIMessage::PITCH_BEND: if (length >= 2 + param_position) { event->set_pitch((data[param_position + 1] << 7) | data[param_position]); } break; - case MIDI_MESSAGE_PROGRAM_CHANGE: + case MIDIMessage::PROGRAM_CHANGE: if (length >= 1 + param_position) { event->set_instrument(data[param_position]); } break; - case MIDI_MESSAGE_CHANNEL_PRESSURE: + case MIDIMessage::CHANNEL_PRESSURE: if (length >= 1 + param_position) { event->set_pressure(data[param_position]); } diff --git a/core/os/midi_driver.h b/core/os/midi_driver.h index ccf624e07e..c23614af00 100644 --- a/core/os/midi_driver.h +++ b/core/os/midi_driver.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/mutex.cpp b/core/os/mutex.cpp index b7d7752d35..1d4400bfc1 100644 --- a/core/os/mutex.cpp +++ b/core/os/mutex.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/mutex.h b/core/os/mutex.h index d77ec362a1..a51248807b 100644 --- a/core/os/mutex.h +++ b/core/os/mutex.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/os.cpp b/core/os/os.cpp index 03e251880f..4f7095b0fc 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -80,7 +80,9 @@ void OS::print_error(const char *p_function, const char *p_file, int p_line, con return; } - _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type); + if (_logger) { + _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type); + } } void OS::print(const char *p_format, ...) { @@ -91,7 +93,9 @@ void OS::print(const char *p_format, ...) { va_list argp; va_start(argp, p_format); - _logger->logv(p_format, argp, false); + if (_logger) { + _logger->logv(p_format, argp, false); + } va_end(argp); } @@ -104,7 +108,9 @@ void OS::printerr(const char *p_format, ...) { va_list argp; va_start(argp, p_format); - _logger->logv(p_format, argp, true); + if (_logger) { + _logger->logv(p_format, argp, true); + } va_end(argp); } @@ -173,7 +179,7 @@ void OS::dump_memory_to_file(const char *p_file) { //Memory::dump_static_mem_to_file(p_file); } -static FileAccess *_OSPRF = nullptr; +static Ref<FileAccess> _OSPRF; static void _OS_printres(Object *p_obj) { Resource *res = Object::cast_to<Resource>(p_obj); @@ -182,7 +188,7 @@ static void _OS_printres(Object *p_obj) { } String str = vformat("%s - %s - %s", res->to_string(), res->get_name(), res->get_path()); - if (_OSPRF) { + if (_OSPRF.is_valid()) { _OSPRF->store_line(str); } else { print_line(str); @@ -190,24 +196,19 @@ static void _OS_printres(Object *p_obj) { } void OS::print_all_resources(String p_to_file) { - ERR_FAIL_COND(p_to_file != "" && _OSPRF); - if (p_to_file != "") { + ERR_FAIL_COND(!p_to_file.is_empty() && _OSPRF.is_valid()); + if (!p_to_file.is_empty()) { Error err; _OSPRF = FileAccess::open(p_to_file, FileAccess::WRITE, &err); if (err != OK) { - _OSPRF = nullptr; + _OSPRF.unref(); ERR_FAIL_MSG("Can't print all resources to file: " + String(p_to_file) + "."); } } ObjectDB::debug_objects(_OS_printres); - if (p_to_file != "") { - if (_OSPRF) { - memdelete(_OSPRF); - } - _OSPRF = nullptr; - } + _OSPRF.unref(); } void OS::print_resources_in_use(bool p_short) { @@ -236,6 +237,11 @@ String OS::get_locale_language() const { return get_locale().left(3).replace("_", ""); } +// Embedded PCK offset. +uint64_t OS::get_embedded_pck_offset() const { + return 0; +} + // Helper function to ensure that a dir name/path will be valid on the OS String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const { Vector<String> invalid_chars = String(": * ? \" < > |").split(" "); @@ -328,17 +334,13 @@ void OS::yield() { void OS::ensure_user_data_dir() { String dd = get_user_data_dir(); - DirAccess *da = DirAccess::open(dd); - if (da) { - memdelete(da); + if (DirAccess::exists(dd)) { return; } - da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); Error err = da->make_dir_recursive(dd); ERR_FAIL_COND_MSG(err != OK, "Error attempting to create data dir: " + dd + "."); - - memdelete(da); } String OS::get_model_name() const { @@ -346,7 +348,7 @@ String OS::get_model_name() const { } void OS::set_cmdline(const char *p_execpath, const List<String> &p_args) { - _execpath = p_execpath; + _execpath = String::utf8(p_execpath); _cmdline = p_args; } @@ -358,6 +360,10 @@ int OS::get_processor_count() const { return 1; } +String OS::get_processor_name() const { + return ""; +} + bool OS::can_use_threads() const { #ifdef NO_THREADS return false; @@ -387,16 +393,18 @@ bool OS::has_feature(const String &p_feature) { return true; } #else - if (p_feature == "release") + if (p_feature == "release") { return true; + } #endif #ifdef TOOLS_ENABLED if (p_feature == "editor") { return true; } #else - if (p_feature == "standalone") + if (p_feature == "standalone") { return true; + } #endif if (sizeof(void *) == 8 && p_feature == "64") { @@ -548,6 +556,8 @@ OS::OS() { } OS::~OS() { - memdelete(_logger); + if (_logger) { + memdelete(_logger); + } singleton = nullptr; } diff --git a/core/os/os.h b/core/os/os.h index 52bf731501..5eac77d634 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -34,7 +34,6 @@ #include "core/config/engine.h" #include "core/io/image.h" #include "core/io/logger.h" -#include "core/os/main_loop.h" #include "core/string/ustring.h" #include "core/templates/list.h" #include "core/templates/vector.h" @@ -55,14 +54,11 @@ class OS { bool _single_window = false; String _local_clipboard; int _exit_code = EXIT_FAILURE; // unexpected exit is marked as failure - int _orientation; bool _allow_hidpi = false; bool _allow_layered = false; bool _stdout_enabled = true; bool _stderr_enabled = true; - char *last_error; - CompositeLogger *_logger = nullptr; bool restart_on_exit = false; @@ -71,7 +67,7 @@ class OS { // for the user interface we keep a record of the current display driver // so we can retrieve the rendering drivers available int _display_driver_id = -1; - String _current_rendering_driver_name = ""; + String _current_rendering_driver_name; protected: void _set_logger(CompositeLogger *p_logger); @@ -133,13 +129,15 @@ public: virtual String get_stdin_string(bool p_block = true) = 0; + virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) = 0; // Should return cryptographically-safe random bytes. + virtual PackedStringArray get_connected_midi_inputs(); virtual void open_midi_inputs(); virtual void close_midi_inputs(); virtual void alert(const String &p_alert, const String &p_title = "ALERT!"); - virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false) { return ERR_UNAVAILABLE; } + virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false, String *r_resolved_path = nullptr) { return ERR_UNAVAILABLE; } virtual Error close_dynamic_library(void *p_library_handle) { return ERR_UNAVAILABLE; } virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false) { return ERR_UNAVAILABLE; } @@ -149,11 +147,12 @@ public: virtual int get_low_processor_usage_mode_sleep_usec() const; virtual String get_executable_path() const; - virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr) = 0; - virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr) = 0; + virtual Error execute(const String &p_path, const List<String> &p_arguments, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false) = 0; + virtual Error create_process(const String &p_path, const List<String> &p_arguments, ProcessID *r_child_id = nullptr, bool p_open_console = false) = 0; virtual Error create_instance(const List<String> &p_arguments, ProcessID *r_child_id = nullptr) { return create_process(get_executable_path(), p_arguments, r_child_id); }; virtual Error kill(const ProcessID &p_pid) = 0; virtual int get_process_id() const; + virtual bool is_process_running(const ProcessID &p_pid) const = 0; virtual void vibrate_handheld(int p_duration_ms = 500); virtual Error shell_open(String p_uri); @@ -243,7 +242,7 @@ public: void set_stdout_enabled(bool p_enabled); void set_stderr_enabled(bool p_enabled); - bool is_single_window() const; + virtual bool is_single_window() const; virtual void disable_crash_handler() {} virtual bool is_disable_crash_handler() const { return false; } @@ -265,6 +264,8 @@ public: virtual String get_locale() const; String get_locale_language() const; + virtual uint64_t get_embedded_pck_offset() const; + String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const; virtual String get_godot_dir_name() const; @@ -295,9 +296,14 @@ public: virtual void debug_break(); virtual int get_exit_code() const; + // `set_exit_code` should only be used from `SceneTree` (or from a similar + // level, e.g. from the `Main::start` if leaving without creating a `SceneTree`). + // For other components, `SceneTree.quit()` should be used instead. virtual void set_exit_code(int p_code); virtual int get_processor_count() const; + virtual String get_processor_name() const; + virtual int get_default_thread_pool_size() const { return get_processor_count(); } virtual String get_unique_id() const; diff --git a/core/os/pool_allocator.cpp b/core/os/pool_allocator.cpp index 74e9c24e04..f622e2c7c5 100644 --- a/core/os/pool_allocator.cpp +++ b/core/os/pool_allocator.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -149,7 +149,7 @@ void PoolAllocator::compact_up(int p_from) { } } -bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos, Entry *p_entry) { +bool PoolAllocator::find_entry_index(EntryIndicesPos *p_map_pos, const Entry *p_entry) { EntryArrayPos entry_pos = entry_max; for (int i = 0; i < entry_count; i++) { @@ -424,7 +424,7 @@ bool PoolAllocator::is_locked(ID p_mem) const { } mt_lock(); - const Entry *e = ((PoolAllocator *)(this))->get_entry(p_mem); + const Entry *e = const_cast<PoolAllocator *>(this)->get_entry(p_mem); if (!e) { mt_unlock(); ERR_PRINT("!e"); diff --git a/core/os/pool_allocator.h b/core/os/pool_allocator.h index 49f433ba97..a7a8523aa4 100644 --- a/core/os/pool_allocator.h +++ b/core/os/pool_allocator.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -34,13 +34,12 @@ #include "core/typedefs.h" /** - @author Juan Linietsky <reduzio@gmail.com> * Generic Pool Allocator. * This is a generic memory pool allocator, with locking, compacting and alignment. (@TODO alignment) * It used as a standard way to manage allocation in a specific region of memory, such as texture memory, * audio sample memory, or just any kind of memory overall. * (@TODO) abstraction should be greater, because in many platforms, you need to manage a nonreachable memory. -*/ + */ enum { POOL_ALLOCATOR_INVALID_ID = -1 ///< default invalid value. use INVALID_ID( id ) to test @@ -76,22 +75,22 @@ private: typedef int EntryArrayPos; typedef int EntryIndicesPos; - Entry *entry_array; - int *entry_indices; - int entry_max; - int entry_count; + Entry *entry_array = nullptr; + int *entry_indices = nullptr; + int entry_max = 0; + int entry_count = 0; - uint8_t *pool; - void *mem_ptr; - int pool_size; + uint8_t *pool = nullptr; + void *mem_ptr = nullptr; + int pool_size = 0; - int free_mem; - int free_mem_peak; + int free_mem = 0; + int free_mem_peak = 0; - unsigned int check_count; - int align; + unsigned int check_count = 0; + int align = 1; - bool needs_locking; + bool needs_locking = false; inline int entry_end(const Entry &p_entry) const { return p_entry.pos + aligned(p_entry.len); @@ -109,7 +108,7 @@ private: void compact_up(int p_from = 0); bool get_free_entry(EntryArrayPos *p_pos); bool find_hole(EntryArrayPos *p_pos, int p_for_size); - bool find_entry_index(EntryIndicesPos *p_map_pos, Entry *p_entry); + bool find_entry_index(EntryIndicesPos *p_map_pos, const Entry *p_entry); Entry *get_entry(ID p_mem); const Entry *get_entry(ID p_mem) const; diff --git a/core/os/rw_lock.h b/core/os/rw_lock.h index 560ec57a5f..a046f474ea 100644 --- a/core/os/rw_lock.h +++ b/core/os/rw_lock.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/semaphore.h b/core/os/semaphore.h index 01ae7a3c65..72df52dd34 100644 --- a/core/os/semaphore.h +++ b/core/os/semaphore.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/spin_lock.h b/core/os/spin_lock.h index 929e8b9a58..6e8d7f46d5 100644 --- a/core/os/spin_lock.h +++ b/core/os/spin_lock.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -41,7 +41,7 @@ class SpinLock { public: _ALWAYS_INLINE_ void lock() { while (locked.test_and_set(std::memory_order_acquire)) { - ; + // Continue. } } _ALWAYS_INLINE_ void unlock() { diff --git a/core/os/thread.cpp b/core/os/thread.cpp index 27aefc98de..c8072b7280 100644 --- a/core/os/thread.cpp +++ b/core/os/thread.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#include "platform_config.h" #ifndef PLATFORM_THREAD_OVERRIDE // See details in thread.h #include "thread.h" diff --git a/core/os/thread.h b/core/os/thread.h index 59cb58ac57..3382dd81f9 100644 --- a/core/os/thread.h +++ b/core/os/thread.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -28,6 +28,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#include "platform_config.h" // Define PLATFORM_THREAD_OVERRIDE in your platform's `platform_config.h` // to use a custom Thread implementation defined in `platform/[your_platform]/platform_thread.h` // Overriding the platform implementation is required in some proprietary platforms diff --git a/core/os/thread_safe.h b/core/os/thread_safe.h index 81de079bf3..fa443048e4 100644 --- a/core/os/thread_safe.h +++ b/core/os/thread_safe.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/os/threaded_array_processor.h b/core/os/threaded_array_processor.h index fec6473589..935fc7a360 100644 --- a/core/os/threaded_array_processor.h +++ b/core/os/threaded_array_processor.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -31,7 +31,6 @@ #ifndef THREADED_ARRAY_PROCESSOR_H #define THREADED_ARRAY_PROCESSOR_H -#include "core/os/mutex.h" #include "core/os/os.h" #include "core/os/thread.h" #include "core/os/thread_safe.h" diff --git a/core/os/time.cpp b/core/os/time.cpp index a185029969..a30e2a906b 100644 --- a/core/os/time.cpp +++ b/core/os/time.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -95,16 +95,21 @@ VARIANT_ENUM_CAST(Time::Weekday); day = day_number_copy + 1; \ } -#define VALIDATE_YMDHMS \ - ERR_FAIL_COND_V_MSG(month == 0, 0, "Invalid month value of: " + itos(month) + ", months are 1-indexed and cannot be 0. See the Time.Month enum for valid values."); \ - ERR_FAIL_COND_V_MSG(month > 12, 0, "Invalid month value of: " + itos(month) + ". See the Time.Month enum for valid values."); \ - ERR_FAIL_COND_V_MSG(hour > 23, 0, "Invalid hour value of: " + itos(hour) + "."); \ - ERR_FAIL_COND_V_MSG(minute > 59, 0, "Invalid minute value of: " + itos(minute) + "."); \ - ERR_FAIL_COND_V_MSG(second > 59, 0, "Invalid second value of: " + itos(second) + " (leap seconds are not supported)."); \ - /* Do this check after month is tested as valid. */ \ - ERR_FAIL_COND_V_MSG(day == 0, 0, "Invalid day value of: " + itos(month) + ", days are 1-indexed and cannot be 0."); \ - uint8_t days_in_this_month = MONTH_DAYS_TABLE[IS_LEAP_YEAR(year)][month - 1]; \ - ERR_FAIL_COND_V_MSG(day > days_in_this_month, 0, "Invalid day value of: " + itos(day) + " which is larger than the maximum for this month, " + itos(days_in_this_month) + "."); +#define VALIDATE_YMDHMS(ret) \ + ERR_FAIL_COND_V_MSG(month == 0, ret, "Invalid month value of: " + itos(month) + ", months are 1-indexed and cannot be 0. See the Time.Month enum for valid values."); \ + ERR_FAIL_COND_V_MSG(month < 0, ret, "Invalid month value of: " + itos(month) + "."); \ + ERR_FAIL_COND_V_MSG(month > 12, ret, "Invalid month value of: " + itos(month) + ". See the Time.Month enum for valid values."); \ + ERR_FAIL_COND_V_MSG(hour > 23, ret, "Invalid hour value of: " + itos(hour) + "."); \ + ERR_FAIL_COND_V_MSG(hour < 0, ret, "Invalid hour value of: " + itos(hour) + "."); \ + ERR_FAIL_COND_V_MSG(minute > 59, ret, "Invalid minute value of: " + itos(minute) + "."); \ + ERR_FAIL_COND_V_MSG(minute < 0, ret, "Invalid minute value of: " + itos(minute) + "."); \ + ERR_FAIL_COND_V_MSG(second > 59, ret, "Invalid second value of: " + itos(second) + " (leap seconds are not supported)."); \ + ERR_FAIL_COND_V_MSG(second < 0, ret, "Invalid second value of: " + itos(second) + "."); \ + ERR_FAIL_COND_V_MSG(day == 0, ret, "Invalid day value of: " + itos(day) + ", days are 1-indexed and cannot be 0."); \ + ERR_FAIL_COND_V_MSG(day < 0, ret, "Invalid day value of: " + itos(day) + "."); \ + /* Do this check after month is tested as valid. */ \ + uint8_t days_in_this_month = MONTH_DAYS_TABLE[IS_LEAP_YEAR(year)][month - 1]; \ + ERR_FAIL_COND_V_MSG(day > days_in_this_month, ret, "Invalid day value of: " + itos(day) + " which is larger than the maximum for this month, " + itos(days_in_this_month) + "."); #define YMD_TO_DAY_NUMBER \ /* The day number since Unix epoch (0-index). Days before 1970 are negative. */ \ @@ -124,61 +129,65 @@ VARIANT_ENUM_CAST(Time::Weekday); } \ } -#define PARSE_ISO8601_STRING \ - int64_t year = UNIX_EPOCH_YEAR_AD; \ - Month month = MONTH_JANUARY; \ - uint8_t day = 1; \ - uint8_t hour = 0; \ - uint8_t minute = 0; \ - uint8_t second = 0; \ - { \ - bool has_date = false, has_time = false; \ - String date, time; \ - if (p_datetime.find_char('T') > 0) { \ - has_date = has_time = true; \ - PackedStringArray array = p_datetime.split("T"); \ - date = array[0]; \ - time = array[1]; \ - } else if (p_datetime.find_char(' ') > 0) { \ - has_date = has_time = true; \ - PackedStringArray array = p_datetime.split(" "); \ - date = array[0]; \ - time = array[1]; \ - } else if (p_datetime.find_char('-', 1) > 0) { \ - has_date = true; \ - date = p_datetime; \ - } else if (p_datetime.find_char(':') > 0) { \ - has_time = true; \ - time = p_datetime; \ - } \ - /* Set the variables from the contents of the string. */ \ - if (has_date) { \ - PackedInt32Array array = date.split_ints("-", false); \ - year = array[0]; \ - month = (Month)array[1]; \ - day = array[2]; \ - /* Handle negative years. */ \ - if (p_datetime.find_char('-') == 0) { \ - year *= -1; \ - } \ - } \ - if (has_time) { \ - PackedInt32Array array = time.split_ints(":", false); \ - hour = array[0]; \ - minute = array[1]; \ - second = array[2]; \ - } \ +#define PARSE_ISO8601_STRING(ret) \ + int64_t year = UNIX_EPOCH_YEAR_AD; \ + Month month = MONTH_JANUARY; \ + int day = 1; \ + int hour = 0; \ + int minute = 0; \ + int second = 0; \ + { \ + bool has_date = false, has_time = false; \ + String date, time; \ + if (p_datetime.find_char('T') > 0) { \ + has_date = has_time = true; \ + PackedStringArray array = p_datetime.split("T"); \ + ERR_FAIL_COND_V_MSG(array.size() < 2, ret, "Invalid ISO 8601 date/time string."); \ + date = array[0]; \ + time = array[1]; \ + } else if (p_datetime.find_char(' ') > 0) { \ + has_date = has_time = true; \ + PackedStringArray array = p_datetime.split(" "); \ + ERR_FAIL_COND_V_MSG(array.size() < 2, ret, "Invalid ISO 8601 date/time string."); \ + date = array[0]; \ + time = array[1]; \ + } else if (p_datetime.find_char('-', 1) > 0) { \ + has_date = true; \ + date = p_datetime; \ + } else if (p_datetime.find_char(':') > 0) { \ + has_time = true; \ + time = p_datetime; \ + } \ + /* Set the variables from the contents of the string. */ \ + if (has_date) { \ + PackedInt32Array array = date.split_ints("-", false); \ + ERR_FAIL_COND_V_MSG(array.size() < 3, ret, "Invalid ISO 8601 date string."); \ + year = array[0]; \ + month = (Month)array[1]; \ + day = array[2]; \ + /* Handle negative years. */ \ + if (p_datetime.find_char('-') == 0) { \ + year *= -1; \ + } \ + } \ + if (has_time) { \ + PackedInt32Array array = time.split_ints(":", false); \ + ERR_FAIL_COND_V_MSG(array.size() < 3, ret, "Invalid ISO 8601 time string."); \ + hour = array[0]; \ + minute = array[1]; \ + second = array[2]; \ + } \ } #define EXTRACT_FROM_DICTIONARY \ /* Get all time values from the dictionary. If it doesn't exist, set the */ \ /* values to the default values for Unix epoch (1970-01-01 00:00:00). */ \ int64_t year = p_datetime.has(YEAR_KEY) ? int64_t(p_datetime[YEAR_KEY]) : UNIX_EPOCH_YEAR_AD; \ - Month month = Month((p_datetime.has(MONTH_KEY)) ? uint8_t(p_datetime[MONTH_KEY]) : 1); \ - uint8_t day = p_datetime.has(DAY_KEY) ? uint8_t(p_datetime[DAY_KEY]) : 1; \ - uint8_t hour = p_datetime.has(HOUR_KEY) ? uint8_t(p_datetime[HOUR_KEY]) : 0; \ - uint8_t minute = p_datetime.has(MINUTE_KEY) ? uint8_t(p_datetime[MINUTE_KEY]) : 0; \ - uint8_t second = p_datetime.has(SECOND_KEY) ? uint8_t(p_datetime[SECOND_KEY]) : 0; + Month month = Month((p_datetime.has(MONTH_KEY)) ? int(p_datetime[MONTH_KEY]) : 1); \ + int day = p_datetime.has(DAY_KEY) ? int(p_datetime[DAY_KEY]) : 1; \ + int hour = p_datetime.has(HOUR_KEY) ? int(p_datetime[HOUR_KEY]) : 0; \ + int minute = p_datetime.has(MINUTE_KEY) ? int(p_datetime[MINUTE_KEY]) : 0; \ + int second = p_datetime.has(SECOND_KEY) ? int(p_datetime[SECOND_KEY]) : 0; Time *Time::singleton = nullptr; @@ -252,8 +261,8 @@ String Time::get_time_string_from_unix_time(int64_t p_unix_time_val) const { return vformat("%02d:%02d:%02d", hour, minute, second); } -Dictionary Time::get_datetime_dict_from_string(String p_datetime, bool p_weekday) const { - PARSE_ISO8601_STRING +Dictionary Time::get_datetime_dict_from_datetime_string(String p_datetime, bool p_weekday) const { + PARSE_ISO8601_STRING(Dictionary()) Dictionary dict; dict[YEAR_KEY] = year; dict[MONTH_KEY] = (uint8_t)month; @@ -270,9 +279,10 @@ Dictionary Time::get_datetime_dict_from_string(String p_datetime, bool p_weekday return dict; } -String Time::get_datetime_string_from_dict(Dictionary p_datetime, bool p_use_space) const { +String Time::get_datetime_string_from_datetime_dict(const Dictionary p_datetime, bool p_use_space) const { ERR_FAIL_COND_V_MSG(p_datetime.is_empty(), "", "Invalid datetime Dictionary: Dictionary is empty."); EXTRACT_FROM_DICTIONARY + VALIDATE_YMDHMS("") // vformat only supports up to 6 arguments, so we need to split this up into 2 parts. String timestamp = vformat("%04d-%02d-%02d", year, (uint8_t)month, day); if (p_use_space) { @@ -283,21 +293,36 @@ String Time::get_datetime_string_from_dict(Dictionary p_datetime, bool p_use_spa return timestamp; } -int64_t Time::get_unix_time_from_datetime_dict(Dictionary p_datetime) const { +int64_t Time::get_unix_time_from_datetime_dict(const Dictionary p_datetime) const { ERR_FAIL_COND_V_MSG(p_datetime.is_empty(), 0, "Invalid datetime Dictionary: Dictionary is empty"); EXTRACT_FROM_DICTIONARY - VALIDATE_YMDHMS + VALIDATE_YMDHMS(0) YMD_TO_DAY_NUMBER return day_number * SECONDS_PER_DAY + hour * 3600 + minute * 60 + second; } int64_t Time::get_unix_time_from_datetime_string(String p_datetime) const { - PARSE_ISO8601_STRING - VALIDATE_YMDHMS + PARSE_ISO8601_STRING(-1) + VALIDATE_YMDHMS(0) YMD_TO_DAY_NUMBER return day_number * SECONDS_PER_DAY + hour * 3600 + minute * 60 + second; } +String Time::get_offset_string_from_offset_minutes(int64_t p_offset_minutes) const { + String sign; + if (p_offset_minutes < 0) { + sign = "-"; + p_offset_minutes = -p_offset_minutes; + } else { + sign = "+"; + } + // These two lines can be optimized to one instruction on x86 and others. + // Note that % is acceptable here only because we ensure it's positive. + int64_t offset_hours = p_offset_minutes / 60; + int64_t offset_minutes = p_offset_minutes % 60; + return vformat("%s%02d:%02d", sign, offset_hours, offset_minutes); +} + Dictionary Time::get_datetime_dict_from_system(bool p_utc) const { OS::Date date = OS::get_singleton()->get_date(p_utc); OS::Time time = OS::get_singleton()->get_time(p_utc); @@ -385,10 +410,11 @@ void Time::_bind_methods() { ClassDB::bind_method(D_METHOD("get_datetime_string_from_unix_time", "unix_time_val", "use_space"), &Time::get_datetime_string_from_unix_time, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_date_string_from_unix_time", "unix_time_val"), &Time::get_date_string_from_unix_time); ClassDB::bind_method(D_METHOD("get_time_string_from_unix_time", "unix_time_val"), &Time::get_time_string_from_unix_time); - ClassDB::bind_method(D_METHOD("get_datetime_dict_from_string", "datetime", "weekday"), &Time::get_datetime_dict_from_string); - ClassDB::bind_method(D_METHOD("get_datetime_string_from_dict", "datetime", "use_space"), &Time::get_datetime_string_from_dict); + ClassDB::bind_method(D_METHOD("get_datetime_dict_from_datetime_string", "datetime", "weekday"), &Time::get_datetime_dict_from_datetime_string); + ClassDB::bind_method(D_METHOD("get_datetime_string_from_datetime_dict", "datetime", "use_space"), &Time::get_datetime_string_from_datetime_dict); ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime_dict", "datetime"), &Time::get_unix_time_from_datetime_dict); ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime_string", "datetime"), &Time::get_unix_time_from_datetime_string); + ClassDB::bind_method(D_METHOD("get_offset_string_from_offset_minutes", "offset_minutes"), &Time::get_offset_string_from_offset_minutes); ClassDB::bind_method(D_METHOD("get_datetime_dict_from_system", "utc"), &Time::get_datetime_dict_from_system, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_date_dict_from_system", "utc"), &Time::get_date_dict_from_system, DEFVAL(false)); diff --git a/core/os/time.h b/core/os/time.h index 4325f93d56..4b4ce3526a 100644 --- a/core/os/time.h +++ b/core/os/time.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -51,7 +51,7 @@ class Time : public Object { public: static Time *get_singleton(); - enum Month : uint8_t { + enum Month { /// Start at 1 to follow Windows SYSTEMTIME structure /// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx MONTH_JANUARY = 1, @@ -85,10 +85,11 @@ public: String get_datetime_string_from_unix_time(int64_t p_unix_time_val, bool p_use_space = false) const; String get_date_string_from_unix_time(int64_t p_unix_time_val) const; String get_time_string_from_unix_time(int64_t p_unix_time_val) const; - Dictionary get_datetime_dict_from_string(String p_datetime, bool p_weekday = true) const; - String get_datetime_string_from_dict(Dictionary p_datetime, bool p_use_space = false) const; - int64_t get_unix_time_from_datetime_dict(Dictionary p_datetime) const; + Dictionary get_datetime_dict_from_datetime_string(String p_datetime, bool p_weekday = true) const; + String get_datetime_string_from_datetime_dict(const Dictionary p_datetime, bool p_use_space = false) const; + int64_t get_unix_time_from_datetime_dict(const Dictionary p_datetime) const; int64_t get_unix_time_from_datetime_string(String p_datetime) const; + String get_offset_string_from_offset_minutes(int64_t p_offset_minutes) const; // Methods that get information from OS. Dictionary get_datetime_dict_from_system(bool p_utc = false) const; |