TheRouletteBoi

PS3 On Screen Keyboard

Oct 3rd, 2022
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. #include <mt19937.h>
  2. #define RAND_INIT(s) init_MT(s)
  3. #define RAND() rand_int32_MT()
  4.  
  5.  
  6.  
  7. /* p2p */
  8. #define MAX_CHAT_CHAR (40) /* 1 byte char */
  9. #define MAX_CHAT_MESSAGE (3)
  10. #define APP_VPORT (1)
  11. #define APP_TCPPORT (9332)
  12. #define OSK_LIMIT_LENGTH (MAX_CHAT_CHAR / 2)
  13. #define OSK_MESSAGE (L"chat message")
  14. #define OSK_INIT_TEXT (L"")
  15.  
  16. /* mFlags */
  17. #define OSK_IN_USE (0x00000001)
  18.  
  19. typedef enum osk_callback_type {
  20. OSK_CALLBACK_TYPE_MESSAGE,
  21. OSK_CALLBACK_TYPE_CLOSE,
  22. OSK_CALLBACK_TYPE_MAX
  23. } osk_callback_type_t;
  24.  
  25. typedef struct osk_callback_data {
  26. osk_callback_type_t type;
  27. wchar_t *message;
  28. int len;
  29. } osk_callback_data_t;
  30.  
  31. typedef void(*osk_callback_t)(osk_callback_data_t *data, void *arg);
  32.  
  33. class COsk
  34. {
  35. public:
  36. COsk();
  37. ~COsk();
  38. bool initClass(void);
  39. bool termClass(void);
  40. bool startOsk(osk_callback_t func, void *arg);
  41. bool abortOsk(void);
  42.  
  43. private:
  44. static const int sSlot = 2; /* 0, 1: in use or reserved */
  45. wchar_t mpTextBuffer[OSK_LIMIT_LENGTH + 1];
  46. osk_callback_t mOskCallbackFunc;
  47. void *mOskCallbackArg;
  48. uint32_t mFlags;
  49.  
  50. static void sysutilCb(uint64_t status, uint64_t param, void * userdata);
  51. void sysutilCbInternal(uint64_t status);
  52. void stopOsk(void);
  53. void closeOsk(void);
  54. };
  55.  
  56. COsk::COsk()
  57. {
  58. }
  59.  
  60. COsk::~COsk()
  61. {
  62. }
  63.  
  64. bool COsk::initClass(void)
  65. {
  66. memset(mpTextBuffer, 0, sizeof(*mpTextBuffer));
  67. mFlags = 0;
  68. return (true);
  69. }
  70.  
  71. bool COsk::termClass(void)
  72. {
  73. return (true);
  74. }
  75.  
  76. bool COsk::startOsk(osk_callback_t func, void *arg)
  77. {
  78. CellOskDialogInputFieldInfo info;
  79. CellOskDialogParam param;
  80. CellOskDialogPoint pos;
  81. int ret;
  82.  
  83. if (mFlags & OSK_IN_USE) {
  84. return (true);
  85. }
  86. if (func == NULL) {
  87. _sys_printf("%s, invalid func\n", __FUNCTION__);
  88. goto fail10;
  89. }
  90. ret = cellSysutilRegisterCallback(sSlot, sysutilCb, this);
  91. if (ret != CELL_OK) {
  92. _sys_printf("cellSysutilRegisterCallback() = 0x%x\n", ret);
  93. goto fail20;
  94. }
  95. info.message = (uint16_t *)OSK_MESSAGE;
  96. info.init_text = (uint16_t *)OSK_INIT_TEXT;
  97. info.limit_length = OSK_LIMIT_LENGTH;
  98. pos.x = 0.0f;
  99. pos.y = 0.0f;
  100. param.allowOskPanelFlg =
  101. CELL_OSKDIALOG_PANELMODE_KOREAN |
  102. CELL_OSKDIALOG_PANELMODE_ALPHABET |
  103. CELL_OSKDIALOG_PANELMODE_NUMERAL_FULL_WIDTH |
  104. CELL_OSKDIALOG_PANELMODE_NUMERAL |
  105. CELL_OSKDIALOG_PANELMODE_JAPANESE |
  106. CELL_OSKDIALOG_PANELMODE_JAPANESE_KATAKANA |
  107. CELL_OSKDIALOG_PANELMODE_ENGLISH;
  108. param.firstViewPanel = CELL_OSKDIALOG_PANELMODE_ALPHABET;
  109. param.controlPoint = pos;
  110. param.prohibitFlgs = CELL_OSKDIALOG_NO_RETURN;
  111. ret = cellOskDialogAddSupportLanguage(CELL_OSKDIALOG_PANELMODE_KOREAN);
  112. if (ret < 0) {
  113. _sys_printf("cellOskDialogAddSupportLanguage() failed. ret = 0x%x\n", ret);
  114. goto fail30;
  115. }
  116. ret = cellOskDialogSetKeyLayoutOption(CELL_OSKDIALOG_10KEY_PANEL |
  117. CELL_OSKDIALOG_FULLKEY_PANEL);
  118. if (ret < 0) {
  119. _sys_printf("cellOskDialogSetKeyLayoutOption() failed. ret = 0x%x\n", ret);
  120. goto fail30;
  121. }
  122. ret = cellOskDialogLoadAsync(SYS_MEMORY_CONTAINER_ID_INVALID,
  123. &param, &info);
  124. if (ret < 0) {
  125. _sys_printf("cellOskDialogLoadAsync() failed. ret = 0x%x \n", ret);
  126. goto fail30;
  127. }
  128. mOskCallbackFunc = func;
  129. mOskCallbackArg = arg;
  130. mFlags |= OSK_IN_USE;
  131. return (true);
  132.  
  133. fail30:
  134. cellSysutilUnregisterCallback(sSlot);
  135. fail20:
  136. fail10:
  137. CGame::getCPad()->unlockKey();
  138. return (false);
  139. }
  140.  
  141. bool COsk::abortOsk(void)
  142. {
  143. int ret;
  144.  
  145. if ((mFlags & OSK_IN_USE) == 0) {
  146. return (false);
  147. }
  148. ret = cellOskDialogAbort();
  149. if (ret < 0) {
  150. _sys_printf("cellOskDialogAbort() failed. ret = 0x%x \n", ret);
  151. }
  152. return (true);
  153. }
  154.  
  155. void COsk::stopOsk(void)
  156. {
  157. CellOskDialogCallbackReturnParam param;
  158. osk_callback_data_t data;
  159. int ret;
  160.  
  161. param.result = CELL_OSKDIALOG_INPUT_FIELD_RESULT_OK;
  162. param.numCharsResultString = OSK_LIMIT_LENGTH;
  163. param.pResultString = (uint16_t *)mpTextBuffer;
  164. ret = cellOskDialogUnloadAsync(&param);
  165. if (ret < 0) {
  166. _sys_printf("cellOskDialogUnloadAsync() failed. ret = 0x%x \n", ret);
  167. }
  168. if (param.result == CELL_OSKDIALOG_INPUT_FIELD_RESULT_OK) {
  169. if (wcslen(mpTextBuffer) > 0) {
  170. memset(&data, 0, sizeof(data));
  171. data.type = OSK_CALLBACK_TYPE_MESSAGE;
  172. data.message = mpTextBuffer;
  173. data.len = wcslen(mpTextBuffer) * 2;
  174. (*mOskCallbackFunc)(&data, mOskCallbackArg);
  175. }
  176. }
  177. mFlags &= ~OSK_IN_USE;
  178. }
  179.  
  180. void COsk::closeOsk(void)
  181. {
  182. osk_callback_data_t data;
  183. int ret;
  184.  
  185. ret = cellSysutilUnregisterCallback(sSlot);
  186. if (ret < 0) {
  187. _sys_printf("cellSysutilUnregisterCallback() failed. ret = 0x%x\n", ret);
  188. }
  189. memset(&data, 0, sizeof(data));
  190. data.type = OSK_CALLBACK_TYPE_CLOSE;
  191. (*mOskCallbackFunc)(&data, mOskCallbackArg);
  192. }
  193.  
  194. void COsk::sysutilCbInternal(uint64_t status)
  195. {
  196. switch (status) {
  197. case CELL_SYSUTIL_OSKDIALOG_LOADED:
  198. break;
  199. case CELL_SYSUTIL_OSKDIALOG_FINISHED:
  200. stopOsk();
  201. break;
  202. case CELL_SYSUTIL_OSKDIALOG_UNLOADED:
  203. closeOsk();
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209.  
  210. void COsk::sysutilCb(uint64_t status, uint64_t param, void * userdata)
  211. {
  212. COsk *self = static_cast<COsk *>(userdata);
  213.  
  214. (void)param;
  215. self->sysutilCbInternal(status);
  216. }
  217.  
  218.  
  219. class CGame
  220. {
  221. public:
  222. static COsk *getCOsk(void)
  223. {
  224. return (&spApplication->mOsk);
  225. }
  226. private:
  227. static CGame *spApplication;
  228. COsk mOsk;
  229. };
  230.  
  231. typedef struct game_matching_chat_message {
  232. bool f_use;
  233. p2p_chat_t chat;
  234. wchar_t message[MAX_CHAT_CHAR / 2 + 1];
  235. } game_matching_chat_message_t;
  236.  
  237.  
  238. void setMessage(int *p2pchat, void *data, int size)
  239. {
  240. game_matching_chat_message_t *chat_msg;
  241.  
  242. //messageLock();
  243. chat_msg = &mpChatMessage[mChatIndex];
  244. memcpy(&chat_msg->chat, p2pchat, sizeof(*p2pchat));
  245. memset(chat_msg->message, 0, sizeof(chat_msg->message));
  246. memcpy(chat_msg->message, data, size);
  247. chat_msg->f_use = true;
  248. mChatIndex = (mChatIndex + 1) % MAX_CHAT_MESSAGE;
  249. //messageUnlock();
  250. }
  251. void oskCbMessage(wchar_t *message, int len)
  252. {
  253. p2p_chat_t p2pchat;
  254.  
  255. memcpy(p2pchat.name, CGame::getCNp()->getNpId()->handle.data,
  256. sizeof(p2pchat.name));
  257. setMessage(&p2pchat, (void *)message, len);
  258. CGame::getCNpSignaling()->sendChat(NULL, message, len);
  259. }
  260. void oskCb(osk_callback_data_t *data, void *arg)
  261. {
  262. osk_callback_data_t *ptr = static_cast<osk_callback_data_t *>(data);
  263. //CGameMatching *self = static_cast<CGameMatching *>(arg);
  264.  
  265. switch (ptr->type) {
  266. case OSK_CALLBACK_TYPE_MESSAGE:
  267. oskCbMessage(ptr->message, ptr->len);
  268. break;
  269. case OSK_CALLBACK_TYPE_CLOSE:
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. void doChat(void)
  276. {
  277. CGame::getCOsk()->startOsk(oskCb, this);
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. enum {
  291. MODE_IDLE = 0,
  292. MODE_OPEN,
  293. MODE_RUNNING,
  294. MODE_CLOSE,
  295. MODE_ENTERED,
  296. MODE_CANCELED,
  297. SET_ABORT_TIMER,
  298. CHANGE_PANEL_MODE,
  299. CHANGE_SCALE,
  300. SET_CALLBACK,
  301. MODE_EXIT,
  302. START_DIALOG_TYPE,
  303. START_SEPARATE_TYPE_1,
  304. START_SEPARATE_TYPE_2,
  305. };
  306. static int oskdialog_mode = MODE_IDLE;
  307.  
  308. int getkbLen(char* str)
  309. {
  310. int nullCount = 0;
  311. int i = 0; //num of chars..
  312. for (i = 0; i < 64; i++)
  313. {
  314. if (nullCount == 2) { break; }
  315. if (*(str + i) == 0x00) { nullCount++; }
  316. else { nullCount = 0; }
  317. }
  318. return i;
  319. }
  320. void makekbStr(char* str, char* dest, int len)
  321. {
  322. int nulls = 0;
  323. for (int i = 0; i < len; i++)
  324. {
  325. if (*(str + i) == 0x00) { nulls++; }
  326. else { *(dest + i - nulls) = *(str + i); }
  327. }
  328. *(dest + len + 1 - nulls) = 0x00; //make sure its nulled...
  329. }
  330.  
  331. static void sysutil_callback(uint64_t status, uint64_t param, void *userdata)
  332. {
  333. (void)param;
  334. (void)userdata;
  335. int ret = 0;
  336.  
  337. switch (status) {
  338. case CELL_SYSUTIL_OSKDIALOG_LOADED:
  339. break;
  340. case CELL_SYSUTIL_OSKDIALOG_FINISHED:
  341. oskdialog_mode = MODE_CLOSE;
  342. break;
  343. case CELL_SYSUTIL_OSKDIALOG_UNLOADED:
  344. break;
  345. case CELL_SYSUTIL_REQUEST_EXITGAME:
  346. oskdialog_mode = MODE_EXIT;
  347. break;
  348. case CELL_SYSUTIL_DRAWING_BEGIN:
  349. break;
  350. case CELL_SYSUTIL_DRAWING_END:
  351. break;
  352. case CELL_SYSUTIL_OSKDIALOG_INPUT_ENTERED:
  353. oskdialog_mode = MODE_ENTERED;
  354. break;
  355. case CELL_SYSUTIL_OSKDIALOG_INPUT_CANCELED:
  356. oskdialog_mode = MODE_CANCELED;
  357. break;
  358. case CELL_SYSUTIL_OSKDIALOG_INPUT_DEVICE_CHANGED:
  359. break;
  360. case CELL_SYSUTIL_OSKDIALOG_DISPLAY_CHANGED:
  361. break;
  362. case CELL_SYSUTIL_SYSTEM_MENU_CLOSE:
  363.  
  364. break;
  365. case CELL_SYSUTIL_SYSTEM_MENU_OPEN:
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. int keyboard(char* dest, char* INIT_TEXT, char* MESSAGE)
  372. {
  373. int ret;
  374. CellOskDialogInputFieldInfo inputFieldInfo;
  375. inputFieldInfo.message = (uint16_t*)MESSAGE;
  376. inputFieldInfo.init_text = (uint16_t*)INIT_TEXT;
  377. inputFieldInfo.limit_length = CELL_OSKDIALOG_STRING_SIZE;
  378.  
  379.  
  380. CellOskDialogCallbackReturnParam OutputInfo;
  381. OutputInfo.result = CELL_OSKDIALOG_INPUT_FIELD_RESULT_OK;
  382. OutputInfo.numCharsResultString = 32;
  383.  
  384. uint16_t Result_Text_Buffer[CELL_OSKDIALOG_STRING_SIZE + 1];
  385. OutputInfo.pResultString = Result_Text_Buffer;
  386.  
  387. ret = cellOskDialogSetKeyLayoutOption(CELL_OSKDIALOG_10KEY_PANEL | CELL_OSKDIALOG_FULLKEY_PANEL);
  388.  
  389.  
  390. CellOskDialogPoint pos;
  391. pos.x = 0.0; pos.y = 0.0;
  392. int32_t LayoutMode = CELL_OSKDIALOG_LAYOUTMODE_X_ALIGN_CENTER | CELL_OSKDIALOG_LAYOUTMODE_Y_ALIGN_TOP;
  393. ret = cellOskDialogSetLayoutMode(LayoutMode);
  394.  
  395.  
  396. CellOskDialogParam dialogParam;
  397. dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_KOREAN |
  398. CELL_OSKDIALOG_PANELMODE_ALPHABET |
  399. CELL_OSKDIALOG_PANELMODE_NUMERAL_FULL_WIDTH |
  400. CELL_OSKDIALOG_PANELMODE_NUMERAL |
  401. CELL_OSKDIALOG_PANELMODE_JAPANESE |
  402. CELL_OSKDIALOG_PANELMODE_JAPANESE_KATAKANA |
  403. CELL_OSKDIALOG_PANELMODE_ENGLISH;
  404. /*E Panel to display first */
  405. dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_ALPHABET;
  406. /* E Initial display position of the on-screen keyboard dialog */
  407. dialogParam.controlPoint = pos;
  408. /*E Prohibited operation flag(s) (ex. CELL_OSKDIALOG_NO_SPACE) */
  409. dialogParam.prohibitFlgs = 0;
  410.  
  411. sys_timer_usleep(16 * 1000);
  412. ret = cellSysutilCheckCallback();
  413.  
  414.  
  415. if (oskdialog_mode == MODE_OPEN)
  416. {
  417. if (cellSysutilRegisterCallback(0, sysutil_callback, NULL) != 0) {}
  418. ret = cellOskDialogLoadAsync(SYS_MEMORY_CONTAINER_ID_INVALID, &dialogParam, &inputFieldInfo);
  419. oskdialog_mode = MODE_RUNNING;
  420. }
  421.  
  422. if (oskdialog_mode == MODE_ENTERED)
  423. {
  424. ret = cellOskDialogGetInputText(&OutputInfo);
  425. oskdialog_mode = MODE_RUNNING;
  426. }
  427. if (oskdialog_mode == MODE_CLOSE)
  428. {
  429. ret = cellOskDialogUnloadAsync(&OutputInfo);
  430.  
  431. int strLen = getkbLen((char*)(*(&OutputInfo.pResultString)));
  432. makekbStr((char*)(*(&OutputInfo.pResultString)), dest, strLen);
  433. inputFieldInfo.init_text = (uint16_t*)INIT_TEXT;
  434. if (cellSysutilUnregisterCallback(0) != 0) {}
  435. oskdialog_mode = MODE_EXIT;
  436. }
  437.  
  438. return 0;
  439. }
  440.  
  441. void _useage()
  442. {
  443. char retText[32]; //our buffer
  444. oskdialog_mode = MODE_OPEN;
  445. while (oskdialog_mode != Keyboard::MODE_EXIT)
  446. {
  447. keyboard(retText, "Title", "Preset Text");
  448. }
  449. }
  450.  
Add Comment
Please, Sign In to add comment