Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose OS.read_string_from_stdin() to the scripting API (3.x) #70378

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ Error _OS::shell_open(String p_uri) {
return OS::get_singleton()->shell_open(p_uri);
};

String _OS::read_string_from_stdin() {
return OS::get_singleton()->get_stdin_string();
}

int _OS::execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output, bool p_read_stderr, bool p_open_console) {
OS::ProcessID pid = -2;
int exitcode = 0;
Expand Down Expand Up @@ -1415,6 +1419,7 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_processor_name"), &_OS::get_processor_name);

ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
ClassDB::bind_method(D_METHOD("read_string_from_stdin"), &_OS::read_string_from_stdin);
ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "blocking", "output", "read_stderr", "open_console"), &_OS::execute, DEFVAL(true), DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
ClassDB::bind_method(D_METHOD("kill", "pid"), &_OS::kill);
ClassDB::bind_method(D_METHOD("shell_open", "uri"), &_OS::shell_open);
Expand Down
1 change: 1 addition & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class _OS : public Object {
int get_low_processor_usage_mode_sleep_usec() const;

String get_executable_path() const;
String read_string_from_stdin();
int execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking = true, Array p_output = Array(), bool p_read_stderr = false, bool p_open_console = false);

Error kill(int p_pid);
Expand Down
2 changes: 1 addition & 1 deletion core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class OS {
void printerr(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3;

virtual void alert(const String &p_alert, const String &p_title = "ALERT!") = 0;
virtual String get_stdin_string(bool p_block = true) = 0;
virtual String get_stdin_string() = 0;

enum MouseMode {
MOUSE_MODE_VISIBLE,
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,13 @@
Shows all resources currently used by the game.
</description>
</method>
<method name="read_string_from_stdin">
<return type="String" />
<description>
Reads a user input string from the standard input (usually the terminal). This operation is [i]blocking[/i], which causes the window to freeze if [method read_string_from_stdin] is called on the main thread. The thread calling [method read_string_from_stdin] will block until the program receives a line break in standard input (usually by the user pressing [kbd]Enter[/kbd]).
[b]Note:[/b] This method is implemented on Linux, macOS and Windows.
</description>
</method>
<method name="request_attention">
<return type="void" />
<description>
Expand Down
14 changes: 5 additions & 9 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,11 @@ void OS_Unix::alert(const String &p_alert, const String &p_title) {
fprintf(stderr, "ALERT: %s: %s\n", p_title.utf8().get_data(), p_alert.utf8().get_data());
}

String OS_Unix::get_stdin_string(bool p_block) {
if (p_block) {
char buff[1024];
String ret = stdin_buf + fgets(buff, 1024, stdin);
stdin_buf = "";
return ret;
}

return "";
String OS_Unix::get_stdin_string() {
char buff[1024];
String ret = stdin_buf + fgets(buff, 1024, stdin);
stdin_buf = "";
return ret;
}

String OS_Unix::get_name() const {
Expand Down
2 changes: 1 addition & 1 deletion drivers/unix/os_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class OS_Unix : public OS {
OS_Unix();

virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
virtual String get_stdin_string(bool p_block);
virtual String get_stdin_string();

//virtual void set_mouse_show(bool p_show);
//virtual void set_mouse_grab(bool p_grab);
Expand Down
2 changes: 1 addition & 1 deletion platform/uwp/os_uwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ bool OS_UWP::set_environment(const String &p_var, const String &p_value) const {
return false;
}

String OS_UWP::get_stdin_string(bool p_block) {
String OS_UWP::get_stdin_string() {
return String();
}

Expand Down
2 changes: 1 addition & 1 deletion platform/uwp/os_uwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class OS_UWP : public OS {
HANDLE mouse_mode_changed;

virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
String get_stdin_string(bool p_block);
String get_stdin_string();

void set_mouse_mode(MouseMode p_mode);
MouseMode get_mouse_mode() const;
Expand Down
10 changes: 3 additions & 7 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3254,13 +3254,9 @@ bool OS_Windows::set_environment(const String &p_var, const String &p_value) con
return (bool)SetEnvironmentVariableW(p_var.c_str(), p_value.c_str());
}

String OS_Windows::get_stdin_string(bool p_block) {
if (p_block) {
char buff[1024];
return fgets(buff, 1024, stdin);
};

return String();
String OS_Windows::get_stdin_string() {
char buff[1024];
return fgets(buff, 1024, stdin);
}

void OS_Windows::enable_for_stealing_focus(ProcessID pid) {
Expand Down
2 changes: 1 addition & 1 deletion platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class OS_Windows : public OS {
LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
String get_stdin_string(bool p_block);
String get_stdin_string();

void set_mouse_mode(MouseMode p_mode);
MouseMode get_mouse_mode() const;
Expand Down