-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathIMEView.cpp
138 lines (117 loc) · 4.34 KB
/
IMEView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <utility>
#include <psp2/ime_dialog.h>
#include "IMEView.h"
IMEView::IMEView() {
log_printf(DBG_DEBUG, "IMEView::IMEView()");
commonDialogSetConfig();
// FIXME HACK: when IMEView is passed to Activity::AddView() it's destroyed once the activity is closed
// Keeping an internal shared_ptr of itself makes sure that it's never destroyed
me_ptr = std::shared_ptr<IMEView>(this);
}
void IMEView::openIMEView(std::shared_ptr<IMEViewResult> result, std::string title, SceUInt32 maxInputLength) {
openIMEView(std::move(result), std::move(title), "", maxInputLength);
}
void IMEView::openIMEView(std::shared_ptr<IMEViewResult> result, std::string title, std::string initialText,
SceUInt32 maxInputLength) {
IMEView *imeView = IMEView::create_instance();
imeView->prepare(std::move(result), std::move(title), std::move(initialText), maxInputLength);
Activity::get_instance()->AddView(imeView->me_ptr);
}
void IMEView::closeIMEView() {
sceImeDialogTerm();
}
void IMEView::prepare(std::shared_ptr<IMEViewResult> result, std::string title, std::string initialText,
SceUInt32 maxInputLength) {
log_printf(DBG_DEBUG, "Created IMEView \"%s\"", title.c_str());
if (_status == COMMON_DIALOG_STATUS_RUNNING) {
log_printf(DBG_WARNING, "Canceling current IMEView");
sceImeDialogTerm();
if (_result)
_result->status = COMMON_DIALOG_STATUS_CANCELED;
}
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
_title = converter.from_bytes(title);
_result = result;
if (!initialText.empty())
log_printf(DBG_DEBUG, "initialText set \"%s\"", initialText.c_str());
_initialText = converter.from_bytes(initialText);
_maxTextLength = maxInputLength;
shown_dialog = false;
request_destroy = false;
}
IMEView::~IMEView() {
log_printf(DBG_WARNING, "IMEView destructor called");
sceImeDialogTerm();
delete _input_text_buffer_utf16;
}
int IMEView::Display() {
if (request_destroy) { // done here!
sceImeDialogTerm();
return 0;
}
if (!shown_dialog) {
log_printf(DBG_DEBUG, "IMEView: Initializing dialog");
delete _input_text_buffer_utf16;
_input_text_buffer_utf16 = new SceWChar16[_maxTextLength + 1];
SceImeDialogParam param;
sceImeDialogParamInit(¶m);
param.supportedLanguages = 0x0001FFFF;
param.languagesForced = SCE_TRUE;
// param.type = SCE_IME_TYPE_BASIC_LATIN;
param.title = (const SceWChar16 *)_title.c_str();
param.enterLabel = SCE_IME_ENTER_LABEL_SEARCH;
// param.dialogMode = SCE_IME_DIALOG_DIALOG_MODE_WITH_CANCEL;
param.maxTextLength = _maxTextLength;
param.inputMethod = 0;
if (!_initialText.empty())
param.initialText = (SceWChar16*)_initialText.c_str();
param.inputTextBuffer = _input_text_buffer_utf16;
SceInt32 res = sceImeDialogInit(¶m);
shown_dialog = res == 0;
if (!shown_dialog) {
log_printf(DBG_ERROR,
"Couldn't open IMEView dialog: 0x%lx\nFind the status codes at "
"https://github.com/vitasdk/vita-headers/blob/master/include/psp2/common_dialog.h",
res);
if (_result)
_result->status = COMMON_DIALOG_STATUS_CANCELED;
request_destroy = true;
sceImeDialogTerm();
}
return 0;
}
auto new_status = (CommonDialogStatus)sceImeDialogGetStatus();
if (_status != new_status)
switch (new_status) {
case COMMON_DIALOG_STATUS_NONE:
log_printf(DBG_DEBUG, "IMEView status \"COMMON_DIALOG_STATUS_NONE\"");
break;
case COMMON_DIALOG_STATUS_RUNNING:
log_printf(DBG_DEBUG, "IMEView status \"COMMON_DIALOG_STATUS_RUNNING\"");
break;
case COMMON_DIALOG_STATUS_FINISHED:
log_printf(DBG_DEBUG, "IMEView status \"COMMON_DIALOG_STATUS_FINISHED\"");
break;
case COMMON_DIALOG_STATUS_CANCELED:
log_printf(DBG_DEBUG, "IMEView status \"COMMON_DIALOG_STATUS_CANCELED\"");
break;
}
_status = new_status;
if (_status == COMMON_DIALOG_STATUS_FINISHED) {
SceImeDialogResult result={};
sceImeDialogGetResult(&result);
if (result.button == SCE_IME_DIALOG_BUTTON_CLOSE)
_status = COMMON_DIALOG_STATUS_CANCELED;
else {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> converter;
_input_text_buffer_utf8 = converter.to_bytes((char16_t*)_input_text_buffer_utf16);
if (_result)
_result->userText = std::string(_input_text_buffer_utf8);
}
request_destroy = true;
sceImeDialogTerm();
}
if (_result)
_result->status = _status;
return 0;
}