2020-12-05 13:08:24 +01:00
|
|
|
#include "stdafx.h"
|
2020-02-15 23:36:20 +01:00
|
|
|
#include "Emu/system_config.h"
|
2018-11-21 12:55:12 +01:00
|
|
|
|
2014-07-06 01:30:28 +02:00
|
|
|
#include "sys_tty.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2018-06-29 07:59:56 +02:00
|
|
|
#include <deque>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
2018-08-25 14:39:00 +02:00
|
|
|
LOG_CHANNEL(sys_tty);
|
2014-08-23 16:51:51 +02:00
|
|
|
|
2016-04-27 00:27:24 +02:00
|
|
|
extern fs::file g_tty;
|
2018-03-01 11:48:42 +01:00
|
|
|
extern atomic_t<s64> g_tty_size;
|
2018-06-29 07:59:56 +02:00
|
|
|
extern std::array<std::deque<std::string>, 16> g_tty_input;
|
|
|
|
|
extern std::mutex g_tty_mutex;
|
2016-04-27 00:27:24 +02:00
|
|
|
|
2016-08-16 17:46:24 +02:00
|
|
|
error_code sys_tty_read(s32 ch, vm::ptr<char> buf, u32 len, vm::ptr<u32> preadlen)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2018-06-29 07:59:56 +02:00
|
|
|
sys_tty.trace("sys_tty_read(ch=%d, buf=*0x%x, len=%d, preadlen=*0x%x)", ch, buf, len, preadlen);
|
|
|
|
|
|
2018-11-21 12:55:12 +01:00
|
|
|
if (!g_cfg.core.debug_console_mode)
|
2018-06-29 07:59:56 +02:00
|
|
|
{
|
|
|
|
|
return CELL_EIO;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 14:34:19 +01:00
|
|
|
if (ch > 15 || ch < 0 || !buf)
|
2018-06-29 07:59:56 +02:00
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ch < SYS_TTYP_USER1)
|
|
|
|
|
{
|
|
|
|
|
sys_tty.warning("sys_tty_read called with system channel %d", ch);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
usz chars_to_read = 0; // number of chars that will be read from the input string
|
2018-06-29 07:59:56 +02:00
|
|
|
std::string tty_read; // string for storage of read chars
|
|
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
|
{
|
2018-09-03 21:28:33 +02:00
|
|
|
std::lock_guard lock(g_tty_mutex);
|
2018-06-29 07:59:56 +02:00
|
|
|
|
2019-06-01 15:25:33 +02:00
|
|
|
if (!g_tty_input[ch].empty())
|
2018-06-29 07:59:56 +02:00
|
|
|
{
|
|
|
|
|
// reference to our first queue element
|
|
|
|
|
std::string& input = g_tty_input[ch].front();
|
2014-08-23 16:51:51 +02:00
|
|
|
|
2018-06-29 07:59:56 +02:00
|
|
|
// we have to stop reading at either a new line, the param len, or our input string size
|
2020-12-18 08:39:54 +01:00
|
|
|
usz new_line_pos = input.find_first_of('\n');
|
2018-06-29 07:59:56 +02:00
|
|
|
|
|
|
|
|
if (new_line_pos != input.npos)
|
|
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
chars_to_read = std::min(new_line_pos, static_cast<usz>(len));
|
2018-06-29 07:59:56 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-12-18 08:39:54 +01:00
|
|
|
chars_to_read = std::min(input.size(), static_cast<usz>(len));
|
2018-06-29 07:59:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read the previously calculated number of chars from the beginning of the input string
|
|
|
|
|
tty_read = input.substr(0, chars_to_read);
|
|
|
|
|
|
|
|
|
|
// remove the just read text from the input string
|
|
|
|
|
input = input.substr(chars_to_read, input.size() - 1);
|
|
|
|
|
|
2019-06-01 15:25:33 +02:00
|
|
|
if (input.empty())
|
2018-06-29 07:59:56 +02:00
|
|
|
{
|
|
|
|
|
// pop the first queue element if it was completely consumed
|
|
|
|
|
g_tty_input[ch].pop_front();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!preadlen)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EFAULT;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 23:28:06 +01:00
|
|
|
*preadlen = static_cast<u32>(chars_to_read);
|
2018-06-29 07:59:56 +02:00
|
|
|
|
|
|
|
|
if (chars_to_read > 0)
|
|
|
|
|
{
|
|
|
|
|
std::memcpy(buf.get_ptr(), tty_read.c_str(), chars_to_read);
|
|
|
|
|
sys_tty.success("sys_tty_read(ch=%d, len=%d) read %s with length %d", ch, len, tty_read, *preadlen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CELL_OK;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-16 17:46:24 +02:00
|
|
|
error_code sys_tty_write(s32 ch, vm::cptr<char> buf, u32 len, vm::ptr<u32> pwritelen)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2019-12-20 08:17:22 +01:00
|
|
|
sys_tty.notice("sys_tty_write(ch=%d, buf=*0x%x, len=%d, pwritelen=*0x%x)", ch, buf, len, pwritelen);
|
|
|
|
|
|
2019-12-05 14:34:19 +01:00
|
|
|
std::string msg;
|
2014-08-23 16:51:51 +02:00
|
|
|
|
2020-11-10 18:09:28 +01:00
|
|
|
if (static_cast<s32>(len) > 0 && vm::check_addr(buf.addr(), vm::page_readable, len))
|
2019-12-05 14:34:19 +01:00
|
|
|
{
|
|
|
|
|
msg.resize(len);
|
|
|
|
|
|
|
|
|
|
if (!vm::try_access(buf.addr(), msg.data(), len, false))
|
|
|
|
|
{
|
|
|
|
|
msg.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hack: write to tty even on CEX mode, but disable all error checks
|
2019-12-06 07:34:30 +01:00
|
|
|
if (ch < 0 || ch > 15)
|
2015-07-09 19:19:29 +02:00
|
|
|
{
|
2019-12-06 07:34:30 +01:00
|
|
|
if (g_cfg.core.debug_console_mode)
|
|
|
|
|
{
|
|
|
|
|
return CELL_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
msg.clear();
|
|
|
|
|
}
|
2015-07-09 19:19:29 +02:00
|
|
|
}
|
2018-02-09 15:49:37 +01:00
|
|
|
|
2019-12-05 14:34:19 +01:00
|
|
|
if (g_cfg.core.debug_console_mode)
|
|
|
|
|
{
|
|
|
|
|
// Don't modify it in CEX mode
|
|
|
|
|
len = static_cast<s32>(len) > 0 ? len : 0;
|
|
|
|
|
}
|
2015-07-09 19:19:29 +02:00
|
|
|
|
2019-12-05 14:34:19 +01:00
|
|
|
if (static_cast<s32>(len) > 0)
|
2015-07-09 19:19:29 +02:00
|
|
|
{
|
2019-12-05 14:34:19 +01:00
|
|
|
if (!msg.empty())
|
|
|
|
|
{
|
2020-01-06 21:14:34 +01:00
|
|
|
sys_tty.notice(u8"sys_tty_write(): “%s”", msg);
|
2019-12-20 08:17:22 +01:00
|
|
|
|
2019-12-05 14:34:19 +01:00
|
|
|
if (g_tty)
|
|
|
|
|
{
|
|
|
|
|
// Lock size by making it negative
|
|
|
|
|
g_tty_size -= (1ll << 48);
|
|
|
|
|
g_tty.write(msg);
|
|
|
|
|
g_tty_size += (1ll << 48) + len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (g_cfg.core.debug_console_mode)
|
|
|
|
|
{
|
2019-12-06 07:34:30 +01:00
|
|
|
return {CELL_EFAULT, buf.addr()};
|
2019-12-05 14:34:19 +01:00
|
|
|
}
|
2015-07-09 19:19:29 +02:00
|
|
|
}
|
2018-02-09 15:49:37 +01:00
|
|
|
|
2019-12-05 14:34:19 +01:00
|
|
|
if (!pwritelen.try_write(len))
|
2016-04-27 00:27:24 +02:00
|
|
|
{
|
2019-12-06 07:34:30 +01:00
|
|
|
return {CELL_EFAULT, pwritelen};
|
2016-04-27 00:27:24 +02:00
|
|
|
}
|
2016-01-12 22:57:16 +01:00
|
|
|
|
2012-11-15 00:39:56 +01:00
|
|
|
return CELL_OK;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|