Skip to content

Commit aa6a6bc

Browse files
authored
Simba 2.0 fixes/features. (#6)
- Updates for Simba 2.0 EIOS - Updates to String sending for Simba 2.0 - Updates for Simba's Alpha support and Image Resizing
1 parent 60686b4 commit aa6a6bc

8 files changed

+170
-51
lines changed

RemoteInput/EIOS.cxx

+19-18
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,10 @@ EIOS* SimbaPluginTarget_RequestWithDebugImage(const char* initargs, void** image
436436
std::int32_t width = 0;
437437
std::int32_t height = 0;
438438
eios->control_center->get_target_dimensions(&width, &height);
439-
439+
eios->control_center->set_image_format(ImageFormat::BGRA);
440+
eios->control_center->set_debug_graphics(true);
440441
*image = PLUGIN_SIMBA_METHODS.ExternalImage_Create(true);
441-
PLUGIN_SIMBA_METHODS.ExternalImage_SetMemory(*image, EIOS_GetImageBuffer(eios), width, height);
442-
443-
PLUGIN_SIMBA_METHODS.ExternalImage_AddCallbackOnUnlock(*image, []{
444-
445-
});
442+
PLUGIN_SIMBA_METHODS.ExternalImage_SetMemory(*image, eios->control_center->get_debug_image(), width, height);
446443
}
447444

448445
return eios;
@@ -458,28 +455,32 @@ void SimbaPluginTarget_GetDimensions(EIOS* eios, std::int32_t* width, std::int32
458455
EIOS_GetTargetDimensions(eios, width, height);
459456
}
460457

461-
void SimbaPluginTarget_GetImageData(EIOS* eios, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, void** pColorBGRA, std::int32_t* data_width) noexcept
458+
bool SimbaPluginTarget_GetImageData(EIOS* eios, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, void** bgra, std::int32_t* data_width) noexcept
462459
{
463-
std::uint8_t* image_buffer = EIOS_GetImageBuffer(eios);
464-
*pColorBGRA = &image_buffer[y * width + x];
465-
*data_width = width;
460+
if (eios)
461+
{
462+
*data_width = eios->control_center->get_target_width();
463+
*bgra = &eios->control_center->get_image()[(y * (*data_width) + x) * 4];
464+
465+
return true;
466+
} else {
467+
return false;
468+
}
466469
}
467470

468471
bool SimbaPluginTarget_MousePressed(EIOS* eios, int mouse_button) noexcept
469472
{
470473
return EIOS_IsMouseButtonHeld(eios, mouse_button);
471474
}
472475

473-
TPoint SimbaPluginTarget_MousePosition(EIOS* eios) noexcept
476+
void SimbaPluginTarget_MousePosition(EIOS* eios, std::int32_t* x, std::int32_t* y) noexcept
474477
{
475-
std::int32_t x, y;
476-
EIOS_GetMousePosition(eios, &x, &y);
477-
return {x, y};
478+
EIOS_GetMousePosition(eios, x, y);
478479
}
479480

480-
void SimbaPluginTarget_Teleport(EIOS* eios, const TPoint &p) noexcept
481+
void SimbaPluginTarget_MouseTeleport(EIOS* eios, std::int32_t x, std::int32_t y) noexcept
481482
{
482-
EIOS_MoveMouse(eios, p.x, p.y);
483+
EIOS_MoveMouse(eios, x, y);
483484
}
484485

485486
void SimbaPluginTarget_MouseUp(EIOS* eios, int mouse_button) noexcept
@@ -507,11 +508,11 @@ void SimbaPluginTarget_KeyUp(EIOS* eios, std::int32_t key) noexcept
507508
EIOS_ReleaseKey(eios, key);
508509
}
509510

510-
void SimbaPluginTarget_KeySend(EIOS* eios, char key, std::int32_t key_down_time, std::int32_t key_up_time, std::int32_t modifier_down_time, std::int32_t modifier_up_time) noexcept
511+
void SimbaPluginTarget_KeySend(EIOS* eios, char* text, std::int32_t len, std::int32_t* sleeptimes) noexcept
511512
{
512513
if (eios)
513514
{
514-
eios->control_center->send_key(key, key_down_time, key_up_time, modifier_down_time, modifier_up_time);
515+
eios->control_center->key_send(text, len, sleeptimes);
515516
}
516517
}
517518

RemoteInput/EIOS.hxx

+5-25
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,21 @@ EXPORT void STD_CALL EIOS_KillZombieClients() noexcept;
7878
EXPORT std::size_t STD_CALL EIOS_GetClients(bool unpaired_only) noexcept;
7979
EXPORT std::int32_t STD_CALL EIOS_GetClientPID(std::size_t index) noexcept;
8080

81-
#ifdef __cplusplus
82-
}
83-
#endif
84-
85-
typedef struct
86-
{
87-
std::int32_t x;
88-
std::int32_t y;
89-
} __attribute__((__packed__)) TPoint;
90-
91-
class TSimbaExternalImage
92-
{
93-
public:
94-
TSimbaExternalImage() {}
95-
~TSimbaExternalImage() {}
96-
};
97-
98-
#ifdef __cplusplus
99-
extern "C" {
100-
#endif
101-
81+
// New in Simba 2.0 https://villavu.github.io/Simba/tutorials/plugins/plugin-target.html
10282
EXPORT EIOS* SimbaPluginTarget_Request(const char* initargs) noexcept;
10383
EXPORT EIOS* SimbaPluginTarget_RequestWithDebugImage(const char* initargs, void** image) noexcept;
10484
EXPORT void SimbaPluginTarget_Release(EIOS* eios) noexcept;
10585
EXPORT void SimbaPluginTarget_GetDimensions(EIOS* eios, std::int32_t* width, std::int32_t* height) noexcept;
106-
EXPORT void SimbaPluginTarget_GetImageData(EIOS* eios, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, void** pColorBGRA, std::int32_t* data_width) noexcept;
86+
EXPORT bool SimbaPluginTarget_GetImageData(EIOS* eios, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height, void** bgra, std::int32_t* data_width) noexcept;
10787
EXPORT bool SimbaPluginTarget_MousePressed(EIOS* eios, int mouse_button) noexcept;
108-
EXPORT TPoint SimbaPluginTarget_MousePosition(EIOS* eios) noexcept;
109-
EXPORT void SimbaPluginTarget_Teleport(EIOS* eios, const TPoint &p) noexcept;
88+
EXPORT void SimbaPluginTarget_MousePosition(EIOS* eios, std::int32_t* x, std::int32_t* y) noexcept;
89+
EXPORT void SimbaPluginTarget_MouseTeleport(EIOS* eios, std::int32_t x, std::int32_t y) noexcept;
11090
EXPORT void SimbaPluginTarget_MouseUp(EIOS* eios, int mouse_button) noexcept;
11191
EXPORT void SimbaPluginTarget_MouseDown(EIOS* eios, int mouse_button) noexcept;
11292
EXPORT void SimbaPluginTarget_MouseScroll(EIOS* eios, int scrolls) noexcept;
11393
EXPORT void SimbaPluginTarget_KeyDown(EIOS* eios, std::int32_t key) noexcept;
11494
EXPORT void SimbaPluginTarget_KeyUp(EIOS* eios, std::int32_t key) noexcept;
115-
EXPORT void SimbaPluginTarget_KeySend(EIOS* eios, char key, std::int32_t key_down_time, std::int32_t key_up_time, std::int32_t modifier_down_time, std::int32_t modifier_up_time) noexcept;
95+
EXPORT void SimbaPluginTarget_KeySend(EIOS* eios, char* text, std::int32_t len, std::int32_t* sleeptimes) noexcept;
11696
EXPORT bool SimbaPluginTarget_KeyPressed(EIOS* eios, std::int32_t key) noexcept;
11797

11898
#ifdef __cplusplus

RemoteInput/EIOSTypes.hxx

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ enum class EIOSCommand: std::uint32_t
3939
IS_MOUSE_HELD,
4040
SEND_STRING,
4141
SEND_KEY,
42+
KEY_SEND,
4243
HOLD_KEY,
4344
RELEASE_KEY,
4445
IS_KEY_HELD,

RemoteInput/Plugin/ControlCenter.cxx

+22
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,19 @@ void ControlCenter::process_command() noexcept
333333
}
334334
break;
335335

336+
case EIOSCommand::KEY_SEND:
337+
{
338+
std::string string = stream.read<std::string>();
339+
std::vector<std::int32_t> sleeps;
340+
341+
// Simba sends string.length*4 of sleep times to control the speed of typing
342+
sleeps.resize(string.length() * 4);
343+
stream.read(sleeps.data(), (string.length() * 4) * sizeof(int32_t));
344+
345+
io_controller->key_send(string, sleeps);
346+
}
347+
break;
348+
336349
case EIOSCommand::HOLD_KEY:
337350
{
338351
std::int32_t keycode = stream.read<std::int32_t>();
@@ -1474,6 +1487,15 @@ void ControlCenter::send_key(char key, std::int32_t key_down_time, std::int32_t
14741487
});
14751488
}
14761489

1490+
void ControlCenter::key_send(const char* string, std::int32_t len, std::int32_t* sleeptimes) const noexcept
1491+
{
1492+
send_command([string, len, sleeptimes](Stream &stream, ImageData* image_data) {
1493+
image_data->set_command(EIOSCommand::KEY_SEND);
1494+
image_data->data_stream() << std::string(string, len);
1495+
stream.write(sleeptimes, (len * 4) * sizeof(int32_t));
1496+
});
1497+
}
1498+
14771499
void ControlCenter::hold_key(std::int32_t key) const noexcept
14781500
{
14791501
send_command([key](Stream &stream, ImageData* image_data) {

RemoteInput/Plugin/ControlCenter.hxx

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public:
106106
bool is_mouse_held(std::int32_t button) const noexcept;
107107
void send_string(const char* string, std::int32_t keywait, std::int32_t keymodwait) const noexcept;
108108
void send_key(char key, std::int32_t key_down_time, std::int32_t key_up_time, std::int32_t modifier_down_time, std::int32_t modifier_up_time) const noexcept;
109+
void key_send(const char* string, std::int32_t len, std::int32_t* sleeptimes) const noexcept;
109110
void hold_key(std::int32_t key) const noexcept;
110111
void release_key(std::int32_t key) const noexcept;
111112
bool is_key_held(std::int32_t key) const noexcept;

RemoteInput/Plugin/InputOutput.cxx

+114
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,120 @@ void InputOutput::send_key(char key, std::int32_t key_down_time, std::int32_t ke
679679
}
680680
}
681681

682+
void InputOutput::key_send(std::string string, std::vector<std::int32_t> sleeptimes) const noexcept
683+
{
684+
extern std::unique_ptr<ControlCenter> control_center;
685+
if (!control_center)
686+
{
687+
return;
688+
}
689+
690+
java::Component receiver = control_center->reflect_canvas();
691+
JNIEnv* env = receiver.getEnv();
692+
693+
if (!this->has_focus(&receiver))
694+
{
695+
this->gain_focus(&receiver);
696+
}
697+
698+
bool isShiftDown = false;
699+
std::int32_t sleepindex = 0;
700+
701+
for (std::size_t i = 0; i < string.length(); ++i)
702+
{
703+
char c = string[i];
704+
char n = i == string.length() - 1 ? '\0' : string[i + 1];
705+
std::int32_t modifiers = this->ModifiersForChar(c);
706+
707+
//Modifier Key
708+
if (modifiers & java::InputEvent::InputEventMasks::SHIFT_DOWN_MASK)
709+
{
710+
if (!isShiftDown)
711+
{
712+
isShiftDown = true;
713+
714+
std::int32_t code = VK_LSHIFT;
715+
std::int64_t when = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
716+
std::int32_t modifiers = java::InputEvent::InputEventMasks::SHIFT_DOWN_MASK;
717+
std::int32_t keycode = GetJavaKeyCode(code);
718+
std::int32_t location = GetKeyLocation(code);
719+
720+
java::KeyEvent::Post(env,
721+
&receiver,
722+
java::KeyEvent::KeyCodes::KEY_PRESSED,
723+
when,
724+
modifiers,
725+
keycode,
726+
static_cast<jchar>(java::KeyEvent::KeyCodes::CHAR_UNDEFINED),
727+
location);
728+
729+
yield_thread(std::chrono::milliseconds(sleeptimes[sleepindex++]));
730+
}
731+
}
732+
733+
//Character Key
734+
std::int32_t code = static_cast<std::int32_t>(c);
735+
std::int64_t when = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
736+
std::int32_t keycode = CharToJavaKeyCode(code);
737+
std::int32_t location = GetKeyLocation(code);
738+
739+
java::KeyEvent::Post(env,
740+
&receiver,
741+
java::KeyEvent::KeyCodes::KEY_PRESSED,
742+
when,
743+
modifiers,
744+
keycode,
745+
static_cast<jchar>(c),
746+
location);
747+
748+
java::KeyEvent::Post(env,
749+
&receiver,
750+
java::KeyEvent::KeyCodes::KEY_TYPED,
751+
when,
752+
modifiers,
753+
0,
754+
static_cast<jchar>(c),
755+
java::KeyEvent::KeyCodes::KEY_LOCATION_UNKNOWN);
756+
757+
yield_thread(std::chrono::milliseconds(sleeptimes[sleepindex++]));
758+
when = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
759+
760+
java::KeyEvent::Post(env,
761+
&receiver,
762+
java::KeyEvent::KeyCodes::KEY_RELEASED,
763+
when,
764+
modifiers,
765+
keycode,
766+
static_cast<jchar>(c),
767+
location);
768+
769+
yield_thread(std::chrono::milliseconds(sleeptimes[sleepindex++]));
770+
771+
//Modifier Key
772+
if ((isShiftDown && i == string.length() - 1) || (n != '\0' && !(this->ModifiersForChar(n) & java::InputEvent::InputEventMasks::SHIFT_DOWN_MASK)))
773+
{
774+
isShiftDown = false;
775+
776+
std::int32_t code = VK_LSHIFT;
777+
std::int64_t when = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
778+
std::int32_t modifiers = java::InputEvent::InputEventMasks::SHIFT_DOWN_MASK;
779+
std::int32_t keycode = GetJavaKeyCode(code);
780+
std::int32_t location = GetKeyLocation(code);
781+
782+
java::KeyEvent::Post(env,
783+
&receiver,
784+
java::KeyEvent::KeyCodes::KEY_RELEASED,
785+
when,
786+
modifiers,
787+
keycode,
788+
static_cast<jchar>(java::KeyEvent::KeyCodes::CHAR_UNDEFINED),
789+
location);
790+
791+
yield_thread(std::chrono::milliseconds(sleeptimes[sleepindex++]));
792+
}
793+
}
794+
}
795+
682796
bool InputOutput::has_focus() const noexcept
683797
{
684798
extern std::unique_ptr<ControlCenter> control_center;

RemoteInput/Plugin/InputOutput.hxx

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public:
7979
bool is_key_held(std::int32_t code) const noexcept;
8080
void send_string(std::string string, std::int32_t keywait, std::int32_t keymodwait) const noexcept;
8181
void send_key(char key, std::int32_t key_down_time, std::int32_t key_up_time, std::int32_t modifier_down_time, std::int32_t modifier_up_time) const noexcept;
82-
82+
void key_send(std::string string, std::vector<std::int32_t> sleeptimes) const noexcept;
83+
8384
void get_mouse_position(std::int32_t* x, std::int32_t* y) noexcept;
8485
void get_real_mouse_position(std::int32_t* x, std::int32_t* y) const noexcept;
8586
void move_mouse(std::int32_t x, std::int32_t y) noexcept;

RemoteInput/Plugin/TMemoryManager.hxx

+6-7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ STRUCT_PACK(typedef struct
103103
void(*RaiseException)(const char* message);
104104

105105
void* (*GetTypeInfo)(void* Compiler, const char* Type);
106+
void* (*GetTypeInfoSize)(void* TypeInfo);
106107
std::size_t (*GetTypeInfoFieldOffset)(void* TypeInfo, const char* FieldName);
107108

108109
void* (*AllocateRawArray)(std::size_t element_size, std::size_t length);
@@ -111,17 +112,15 @@ STRUCT_PACK(typedef struct
111112
void* (*AllocateArray)(void* TypeInfo, std::size_t length);
112113
void* (*AllocateString)(const char* data);
113114
void* (*AllocateUnicodeString)(const wchar_t* data);
115+
114116
void (*SetArrayLength)(void* TypeInfo, void** var, std::size_t new_len);
115117
std::size_t (*GetArrayLength)(void* array);
116118

117-
void* (*ExternalImage_Create)(bool FreeOnTerminate);
119+
void* (*ExternalImage_Create)(bool AutoResize);
118120
void (*ExternalImage_SetMemory)(void* img, void* bgra_data, std::int32_t width, std::int32_t height);
119-
bool (*ExternalImage_TryLock)(void* image);
120-
void (*ExternalImage_Lock)(void* image);
121-
void (*ExternalImage_UnLock)(void* image);
122-
123-
void (*ExternalImage_AddCallbackOnUnlock)(void* img, void (*callback)());
124-
void (*ExternalImage_RemoveCallbackOnUnlock)(void* img, void (*callback)());
121+
void (*ExternalImage_Resize)(void* img, std::int32_t new_width, std::int32_t new_height);
122+
void (*ExternalImage_SetUserData)(void* img, void* userdata);
123+
void* (*ExternalImage_GetUserData)(void* img);
125124
}) TSimbaMethodsExtended;
126125

127126
#endif // TMEMORYMANAGER_HXX_INCLUDED

0 commit comments

Comments
 (0)