TheRouletteBoi

OnScreenKeyboard & Controller

Jul 6th, 2018 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.00 KB | None | 0 0
  1. #define DEFAULT_SYSUTIL_CALLBACK_SLOT   0
  2.  
  3. struct _Callback
  4. {
  5.     int id;
  6.     CellSysutilCallback cb;
  7.     void* userdata;
  8. };
  9.  
  10. std::list<_Callback> SysCallback::m_sys_callbacks;
  11. static std::list<_Callback>::iterator _cb_it;
  12.  
  13. class SysCallback
  14. {
  15. public:
  16.     static int reg_cb(const CellSysutilCallback& cb, void* userdata)
  17.     {
  18.         static int _id = 1;
  19.  
  20.         int ret;
  21.         if (m_sys_callbacks.empty())
  22.         {
  23.             ret = cellSysutilRegisterCallback(DEFAULT_SYSUTIL_CALLBACK_SLOT, SysCallback::sysutil_callback, NULL);
  24.             if (ret < 0)
  25.                 return ret;
  26.         }
  27.  
  28.         _Callback _cb;
  29.         _cb.id = _id;
  30.         _cb.cb = cb;
  31.         _cb.userdata = userdata;
  32.         m_sys_callbacks.push_back(_cb);
  33.  
  34.         ret = _id;
  35.         ++_id;
  36.  
  37.         return ret;
  38.     }
  39.     static int SysCallback::unreg_cb(int id)
  40.     {
  41.         for (_cb_it = m_sys_callbacks.begin(); _cb_it != m_sys_callbacks.end(); ++_cb_it)
  42.         {
  43.             if (_cb_it->id == id)
  44.             {
  45.                 m_sys_callbacks.erase(_cb_it);
  46.                 if (m_sys_callbacks.empty())
  47.                 {
  48.                     cellSysutilUnregisterCallback(DEFAULT_SYSUTIL_CALLBACK_SLOT);
  49.                 }
  50.                 return 0;
  51.             }
  52.         }
  53.  
  54.         return -1;
  55.     }
  56. private:
  57.     static void sysutil_callback(uint64_t status, uint64_t param, void* userdata)
  58.     {
  59.         (void)userdata;
  60.  
  61.         for (_cb_it = m_sys_callbacks.begin(); _cb_it != m_sys_callbacks.end(); ++_cb_it)
  62.         {
  63.             _cb_it->cb(status, param, _cb_it->userdata);
  64.         }
  65.     }
  66.  
  67.     static std::list<_Callback> m_sys_callbacks;
  68. };
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. #include <cell/pad.h>
  86.  
  87.  
  88. #define GAMEPAD_ENTER_BUTTON_ASSIGN_CIRCLE  0
  89. #define GAMEPAD_ENTER_BUTTON_ASSIGN_CROSS   1
  90.  
  91. #define GAMEPAD_CONTROLLER_1        0
  92. #define GAMEPAD_CONTROLLER_2        1
  93.  
  94. #define GAMEPAD_CELL_MAX_CONNECTION     7   // Recommended by libpad reference
  95. #define GAMEPAD_CELL_STATUS_CONNECTED       1
  96. #define GAMEPAD_CELL_STATUS_DISCONNECTED    0
  97. #define GAMEPAD_UNINITIALIZED           0xFFFF
  98.  
  99.  
  100. class Gamepad
  101. {
  102. public:
  103.     Gamepad() :
  104.         m_main_pad_port(GAMEPAD_UNINITIALIZED),
  105.         m_enter_button_assign(0),
  106.         CELL_CIRCLE_BUTTON_MASK(0),
  107.         CELL_CROSS_BUTTON_MASK(0),
  108.         CELL_SQUARE_BUTTON_MASK(0),
  109.         CELL_TRIANGLE_BUTTON_MASK(0),
  110.         CELL_UP_BUTTON_MASK(0),
  111.         CELL_DOWN_BUTTON_MASK(0),
  112.         CELL_LEFT_BUTTON_MASK(0),
  113.         CELL_RIGHT_BUTTON_MASK(0),
  114.         CELL_R1_BUTTON_MASK(0),
  115.         CELL_L1_BUTTON_MASK(0),
  116.         CELL_R2_BUTTON_MASK(0),
  117.         CELL_L2_BUTTON_MASK(0),
  118.         CELL_SELECT_BUTTON_MASK(0)
  119.     {
  120.     }
  121.  
  122.     ~Gamepad()
  123.     {
  124.     }
  125.  
  126.     bool init()
  127.     {
  128.         int ret;
  129.         ret = cellPadInit(GAMEPAD_CELL_MAX_CONNECTION);
  130.         if (ret != CELL_PAD_OK)
  131.         {
  132.             printf("cellPadInit() failed. error: 0x%x\n", ret);
  133.             return false;
  134.         }
  135.  
  136.         //-------------------------------------------------------------------------------
  137.         //Get Enter Button settings (CROSS or CIRCLE)
  138.         //-------------------------------------------------------------------------------
  139.         if (cellSysutilGetSystemParamInt(CELL_SYSUTIL_SYSTEMPARAM_ID_ENTER_BUTTON_ASSIGN,
  140.             &m_enter_button_assign) != CELL_OK)
  141.         {
  142.             printf("cellSysutilGetSystemParamInt() CELL_SYSUTIL_SYSTEMPARAM_ID_ENTER_BUTTON_ASSIGN error\n");
  143.             return false;
  144.         }
  145.  
  146.         //If Enter button is assigned to circle
  147.         if (m_enter_button_assign == GAMEPAD_ENTER_BUTTON_ASSIGN_CIRCLE)
  148.         {
  149.             m_enter_button_symbol = "O";
  150.             m_cancel_button_symbol = "X";
  151.         }
  152.         else    //If Enter button is assigned to cross
  153.         {
  154.             m_enter_button_symbol = "X";
  155.             m_cancel_button_symbol = "O";
  156.         }
  157.  
  158.         //--------------------------------------------------------------
  159.         //Bind gamepad button bit masks
  160.         //--------------------------------------------------------------
  161.         if (m_enter_button_assign == GAMEPAD_ENTER_BUTTON_ASSIGN_CIRCLE)
  162.         {
  163.             CELL_CIRCLE_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_CIRCLE;
  164.             CELL_CROSS_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_CROSS;
  165.         }
  166.         else
  167.         {
  168.             CELL_CIRCLE_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_CROSS;
  169.             CELL_CROSS_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_CIRCLE;
  170.         }
  171.         CELL_SQUARE_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_SQUARE;
  172.         CELL_TRIANGLE_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_TRIANGLE;
  173.         CELL_UP_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_UP;
  174.         CELL_DOWN_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_DOWN;
  175.         CELL_LEFT_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_LEFT;
  176.         CELL_RIGHT_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_RIGHT;
  177.         CELL_R1_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_R1;
  178.         CELL_L1_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_L1;
  179.         CELL_R2_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_R2;
  180.         CELL_L2_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_L2;
  181.         CELL_SELECT_BUTTON_MASK = 0X00FF & CELL_PAD_CTRL_SELECT;
  182.  
  183.         GAMEPAD_CIRCLE_BUTTON = CELL_CIRCLE_BUTTON_MASK;
  184.         GAMEPAD_CIRCLE_BUTTON = GAMEPAD_CIRCLE_BUTTON << 16;
  185.         GAMEPAD_CROSS_BUTTON = CELL_CROSS_BUTTON_MASK;
  186.         GAMEPAD_CROSS_BUTTON = GAMEPAD_CROSS_BUTTON << 16;
  187.         GAMEPAD_SQUARE_BUTTON = CELL_SQUARE_BUTTON_MASK;
  188.         GAMEPAD_SQUARE_BUTTON = GAMEPAD_SQUARE_BUTTON << 16;
  189.         GAMEPAD_TRIANGLE_BUTTON = CELL_TRIANGLE_BUTTON_MASK;
  190.         GAMEPAD_TRIANGLE_BUTTON = GAMEPAD_TRIANGLE_BUTTON << 16;
  191.         GAMEPAD_UP_BUTTON = CELL_UP_BUTTON_MASK;
  192.         GAMEPAD_DOWN_BUTTON = CELL_DOWN_BUTTON_MASK;
  193.         GAMEPAD_LEFT_BUTTON = CELL_LEFT_BUTTON_MASK;
  194.         GAMEPAD_RIGHT_BUTTON = CELL_RIGHT_BUTTON_MASK;
  195.         GAMEPAD_R1_BUTTON = CELL_R1_BUTTON_MASK;
  196.         GAMEPAD_R1_BUTTON = GAMEPAD_R1_BUTTON << 16;
  197.         GAMEPAD_L1_BUTTON = CELL_L1_BUTTON_MASK;
  198.         GAMEPAD_L1_BUTTON = GAMEPAD_L1_BUTTON << 16;
  199.         GAMEPAD_R2_BUTTON = CELL_R2_BUTTON_MASK;
  200.         GAMEPAD_R2_BUTTON = GAMEPAD_R2_BUTTON << 16;
  201.         GAMEPAD_L2_BUTTON = CELL_L2_BUTTON_MASK;
  202.         GAMEPAD_L2_BUTTON = GAMEPAD_L2_BUTTON << 16;
  203.         GAMEPAD_SELECT_BUTTON = CELL_SELECT_BUTTON_MASK;
  204.         //------------------------------------------------------------
  205.         //End button binding
  206.         //------------------------------------------------------------
  207.  
  208.         return true;
  209.     }
  210.     //*********************************************************
  211.     // Class: Gamepad
  212.     // Member function: update
  213.     // Description: This function is used for reading and
  214.     //              processing the gamepad input.
  215.     //*********************************************************
  216.     void update()
  217.     {
  218.         m_monitor_gamepad();
  219.  
  220.         if (m_main_pad_port == GAMEPAD_UNINITIALIZED)
  221.             return;
  222.  
  223.         int ret = cellPadGetData(m_main_pad_port, &m_pad_data);
  224.         if (ret != CELL_PAD_OK)
  225.             printf("Error cellPadGetData() failed. error: 0x%x\n", ret);
  226.     }
  227.     //*********************************************************
  228.     // Class: Gamepad
  229.     // Member function: shutdown
  230.     // Description: Call this to clear up input devices,
  231.     //              normally called at program shutdown.
  232.     //*********************************************************
  233.     bool shutdown()
  234.     {
  235.         int ret;
  236.  
  237.         ret = cellPadEnd();
  238.         if (ret != CELL_PAD_OK)
  239.         {
  240.             printf("cellPadEnd() failed. error: 0x%x\n", ret);
  241.             return false;
  242.         }
  243.  
  244.         return true;
  245.     }
  246.  
  247.     bool circle_pressed() const
  248.     {
  249.         if (m_pad_data.len == 0)
  250.             return false;
  251.  
  252.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_CIRCLE_BUTTON_MASK) == CELL_CIRCLE_BUTTON_MASK)
  253.             return true;
  254.  
  255.         return false;
  256.     }
  257.     bool cross_pressed() const
  258.     {
  259.         if (m_pad_data.len == 0)
  260.             return false;
  261.  
  262.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_CROSS_BUTTON_MASK) == CELL_CROSS_BUTTON_MASK)
  263.             return true;
  264.  
  265.         return false;
  266.     }
  267.     bool square_pressed() const
  268.     {
  269.         if (m_pad_data.len == 0)
  270.             return false;
  271.  
  272.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_SQUARE_BUTTON_MASK) == CELL_SQUARE_BUTTON_MASK)
  273.             return true;
  274.  
  275.         return false;
  276.     }
  277.     bool triangle_pressed() const
  278.     {
  279.         if (m_pad_data.len == 0)
  280.             return false;
  281.  
  282.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_TRIANGLE_BUTTON_MASK) == CELL_TRIANGLE_BUTTON_MASK)
  283.             return true;
  284.  
  285.         return false;
  286.     }
  287.     bool up_pressed() const
  288.     {
  289.         if (m_pad_data.len == 0)
  290.             return false;
  291.  
  292.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL1] & CELL_UP_BUTTON_MASK) == CELL_UP_BUTTON_MASK)
  293.             return true;
  294.  
  295.         return false;
  296.     }
  297.     bool down_pressed() const
  298.     {
  299.         if (m_pad_data.len == 0)
  300.             return false;
  301.  
  302.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL1] & CELL_DOWN_BUTTON_MASK) == CELL_DOWN_BUTTON_MASK)
  303.             return true;
  304.  
  305.         return false;
  306.     }
  307.     bool left_pressed() const
  308.     {
  309.         if (m_pad_data.len == 0)
  310.             return false;
  311.  
  312.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL1] & CELL_LEFT_BUTTON_MASK) == CELL_LEFT_BUTTON_MASK)
  313.             return true;
  314.  
  315.         return false;
  316.     }
  317.     bool right_pressed() const
  318.     {
  319.         if (m_pad_data.len == 0)
  320.             return false;
  321.  
  322.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL1] & CELL_RIGHT_BUTTON_MASK) == CELL_RIGHT_BUTTON_MASK)
  323.             return true;
  324.  
  325.         return false;
  326.     }
  327.     bool r1_pressed() const
  328.     {
  329.         if (m_pad_data.len == 0)
  330.             return false;
  331.  
  332.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_R1_BUTTON_MASK) == CELL_R1_BUTTON_MASK)
  333.             return true;
  334.  
  335.         return false;
  336.     }
  337.     bool l1_pressed() const
  338.     {
  339.         if (m_pad_data.len == 0)
  340.             return false;
  341.  
  342.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_L1_BUTTON_MASK) == CELL_L1_BUTTON_MASK)
  343.             return true;
  344.  
  345.         return false;
  346.     }
  347.     bool r2_pressed() const
  348.     {
  349.         if (m_pad_data.len == 0)
  350.             return false;
  351.  
  352.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_R2_BUTTON_MASK) == CELL_R2_BUTTON_MASK)
  353.             return true;
  354.  
  355.         return false;
  356.     }
  357.     bool l2_pressed() const
  358.     {
  359.         if (m_pad_data.len == 0)
  360.             return false;
  361.  
  362.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & CELL_L2_BUTTON_MASK) == CELL_L2_BUTTON_MASK)
  363.             return true;
  364.  
  365.         return false;
  366.     }
  367.     bool select_pressed() const
  368.     {
  369.         if (m_pad_data.len == 0)
  370.             return false;
  371.  
  372.         if ((m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL1] & CELL_SELECT_BUTTON_MASK) == CELL_SELECT_BUTTON_MASK)
  373.             return true;
  374.  
  375.         return false;
  376.     }
  377.  
  378.  
  379.     static uint32_t GAMEPAD_CIRCLE_BUTTON;
  380.     static uint32_t GAMEPAD_CROSS_BUTTON;
  381.     static uint32_t GAMEPAD_SQUARE_BUTTON;
  382.     static uint32_t GAMEPAD_TRIANGLE_BUTTON;
  383.     static uint32_t GAMEPAD_UP_BUTTON;
  384.     static uint32_t GAMEPAD_DOWN_BUTTON;
  385.     static uint32_t GAMEPAD_LEFT_BUTTON;
  386.     static uint32_t GAMEPAD_RIGHT_BUTTON;
  387.     static uint32_t GAMEPAD_R1_BUTTON;
  388.     static uint32_t GAMEPAD_L1_BUTTON;
  389.     static uint32_t GAMEPAD_R2_BUTTON;
  390.     static uint32_t GAMEPAD_L2_BUTTON;
  391.     static uint32_t GAMEPAD_SELECT_BUTTON;
  392.  
  393.     uint32_t button_pressed() const
  394.     {
  395.         if (m_pad_data.len == 0)
  396.             return 0;
  397.  
  398.         uint32_t buttons_bit_field;
  399.         buttons_bit_field = 0;
  400.         buttons_bit_field = m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL2] & 0X00FF;
  401.         buttons_bit_field = buttons_bit_field << 16;
  402.         buttons_bit_field = buttons_bit_field | (m_pad_data.button[CELL_PAD_BTN_OFFSET_DIGITAL1] & 0X00FF);
  403.  
  404.         return buttons_bit_field;
  405.     }
  406.  
  407.     int enter_button_assign() const { return m_enter_button_assign; }
  408.     std::string enter_button_symbol() const { return m_enter_button_symbol; }
  409.     std::string cancel_button_symbol() const { return m_cancel_button_symbol; }
  410.  
  411. private:
  412.     void m_monitor_gamepad()
  413.     {
  414.         int ret = cellPadGetInfo2(&m_pad_info);
  415.         if (ret != CELL_PAD_OK)
  416.         {
  417.             printf("cellPadGetInfo2() failed. error: 0x%x\n", ret);
  418.             return;
  419.         }
  420.  
  421.         static bool print_no_pad_error = true;
  422.         if (m_pad_info.now_connect == 0)
  423.         {
  424.             if (print_no_pad_error)
  425.             {
  426.                 printf("No Pad detected\n");
  427.                 cellMsgDialogOpen2(CELL_MSGDIALOG_TYPE_SE_TYPE_ERROR,
  428.                     "Error:\nNo Gamepad detected\nPlease press the PS button if the gamepad is connected",
  429.                     NULL, NULL, NULL);
  430.                 print_no_pad_error = false;
  431.             }
  432.             return;
  433.         }
  434.         //Reaching this point means at least one pad is connected
  435.         if (print_no_pad_error == false)    //Do this only if the error dialog was opened
  436.         {
  437.             cellMsgDialogClose(0);  //Close message dialog
  438.             print_no_pad_error = true;  //Reset print error flag
  439.         }
  440.  
  441.         if (m_main_pad_port != GAMEPAD_UNINITIALIZED)
  442.         {
  443.             if ((m_pad_info.port_status[m_main_pad_port] & GAMEPAD_CELL_STATUS_CONNECTED) == 0)
  444.             {
  445.                 printf("Gamepad got disconnected\n");
  446.                 m_main_pad_port = GAMEPAD_UNINITIALIZED;
  447.             }
  448.         }
  449.         else
  450.         {
  451.             for (uint32_t i = 0; i<m_pad_info.max_connect; ++i)
  452.             {
  453.                 if ((m_pad_info.port_status[i] & GAMEPAD_CELL_STATUS_CONNECTED) != 0)
  454.                 {
  455.                     m_main_pad_port = i;
  456.                     cellPadClearBuf(i);
  457.                     break;
  458.                 }
  459.             }
  460.         }
  461.     }
  462.  
  463.     CellPadInfo2 m_pad_info;
  464.     CellPadData m_pad_data;
  465.     uint32_t m_main_pad_port;
  466.  
  467.     int m_enter_button_assign;
  468.     std::string m_enter_button_symbol;
  469.     std::string m_cancel_button_symbol;
  470.  
  471.     uint16_t CELL_CIRCLE_BUTTON_MASK;
  472.     uint16_t CELL_CROSS_BUTTON_MASK;
  473.     uint16_t CELL_SQUARE_BUTTON_MASK;
  474.     uint16_t CELL_TRIANGLE_BUTTON_MASK;
  475.     uint16_t CELL_UP_BUTTON_MASK;
  476.     uint16_t CELL_DOWN_BUTTON_MASK;
  477.     uint16_t CELL_LEFT_BUTTON_MASK;
  478.     uint16_t CELL_RIGHT_BUTTON_MASK;
  479.     uint16_t CELL_R1_BUTTON_MASK;
  480.     uint16_t CELL_L1_BUTTON_MASK;
  481.     uint16_t CELL_R2_BUTTON_MASK;
  482.     uint16_t CELL_L2_BUTTON_MASK;
  483.     uint16_t CELL_SELECT_BUTTON_MASK;
  484. };
  485.  
  486. uint32_t Gamepad::GAMEPAD_CIRCLE_BUTTON = 0;
  487. uint32_t Gamepad::GAMEPAD_CROSS_BUTTON = 0;
  488. uint32_t Gamepad::GAMEPAD_SQUARE_BUTTON = 0;
  489. uint32_t Gamepad::GAMEPAD_TRIANGLE_BUTTON = 0;
  490. uint32_t Gamepad::GAMEPAD_UP_BUTTON = 0;
  491. uint32_t Gamepad::GAMEPAD_DOWN_BUTTON = 0;
  492. uint32_t Gamepad::GAMEPAD_LEFT_BUTTON = 0;
  493. uint32_t Gamepad::GAMEPAD_RIGHT_BUTTON = 0;
  494. uint32_t Gamepad::GAMEPAD_R1_BUTTON = 0;
  495. uint32_t Gamepad::GAMEPAD_L1_BUTTON = 0;
  496. uint32_t Gamepad::GAMEPAD_R2_BUTTON = 0;
  497. uint32_t Gamepad::GAMEPAD_L2_BUTTON = 0;
  498. uint32_t Gamepad::GAMEPAD_SELECT_BUTTON = 0;
  499.  
  500.  
  501.  
  502.  
  503.  
  504. enum InputStatus
  505. {
  506.     STATUS_INPUT_FAILED,
  507.     STATUS_INPUT_OK,
  508. };
  509.  
  510. // Memory container size for the OskDialogInput panels
  511. #define OSK_DIALOG_MEMORY_CONTAINER_SIZE_7  ( 7 * 1024 * 1024 )
  512. #define OSK_DIALOG_MEMORY_CONTAINER_SIZE_5  ( 5 * 1024 * 1024 )
  513. #define OSK_DIALOG_MEMORY_CONTAINER_SIZE_3  ( 3 * 1024 * 1024 )
  514. #define OSK_DIALOG_MEMORY_CONTAINER_SIZE_2  ( 2 * 1024 * 1024 )
  515.  
  516. #define OSK_DIALOG_DEFAULT_X        0.1
  517. #define OSK_DIALOG_DEFAULT_Y        0.1
  518.  
  519. enum TextInputStatus
  520. {
  521.     TEXT_INPUT_STATUS_OSK_UNLOADED,
  522.     TEXT_INPUT_STATUS_WAITING,
  523.     TEXT_INPUT_STATUS_CANCELED,
  524.     TEXT_INPUT_STATUS_OK,
  525.     TEXT_INPUT_STATUS_ABORTED,
  526.     TEXT_INPUT_STATUS_NO_INPUT
  527. };
  528.  
  529. struct Text_Input_Info
  530. {
  531.     CellOskDialogCallbackReturnParam OutputInfo;
  532.     uint16_t Result_Text_Buffer[CELL_OSKDIALOG_STRING_SIZE + 1];
  533.     sys_memory_container_t container_id;
  534.     CellOskDialogInputFieldInfo inputFieldInfo;
  535.     CellOskDialogParam dialogParam;
  536.     TextInputStatus status;
  537. };
  538.  
  539. class Input
  540. {
  541. public:
  542.     static bool init()
  543.     {
  544.         int ret;
  545.         ret = 0; // cellSysmoduleLoadModule(CELL_SYSMODULE_IO); we dont need this since the game already loads it
  546.         if (ret != CELL_OK)
  547.         {
  548.             printf("cellSysmoduleLoadModule( CELL_SYSMODULE_IO ) failed: 0x%x\n", ret);
  549.             m_status = STATUS_INPUT_FAILED;
  550.             return false;
  551.         }
  552.  
  553.         if (m_gamepad.init() == false)
  554.         {
  555.             m_status = STATUS_INPUT_FAILED;
  556.             return false;
  557.         }
  558.  
  559.         if (m_init_osk() == false)
  560.         {
  561.             m_status = STATUS_INPUT_FAILED;
  562.             return false;
  563.         }
  564.  
  565.         m_status = STATUS_INPUT_OK;
  566.         return true;
  567.     }
  568.     static bool shutdown()
  569.     {
  570.         m_gamepad.shutdown();
  571.  
  572.         m_shutdown_osk();
  573.  
  574.         int ret;
  575.         ret = 0; // cellSysmoduleUnloadModule(CELL_SYSMODULE_IO); we dont need this since the game already loads it
  576.         if (ret != CELL_OK)
  577.         {
  578.             //printf("cellSysmoduleLoadModule( CELL_SYSMODULE_IO ) failed: 0x%x\n", ret);
  579.             return false;
  580.         }
  581.  
  582.         return true;
  583.     }
  584.     static void update()
  585.     {
  586.         m_gamepad.update();
  587.     }
  588.  
  589.  
  590.     static InputStatus status() { return m_status; }
  591.     static const Gamepad* gamepad() { return &m_gamepad; }
  592.     static bool open_text_input(size_t length, const wchar_t* dialog_message, wchar_t* init_text = NULL)
  593.     {
  594.         if (m_osk_inited == false)
  595.             return false;
  596.  
  597.         if (length > CELL_OSKDIALOG_STRING_SIZE)
  598.             length = CELL_OSKDIALOG_STRING_SIZE;
  599.  
  600.         m_text_input_info.OutputInfo.numCharsResultString = length;
  601.         memset(m_text_input_info.Result_Text_Buffer, 0,
  602.             sizeof(m_text_input_info.Result_Text_Buffer));
  603.  
  604.         if (init_text == NULL)
  605.             m_text_input_info.inputFieldInfo.init_text = (uint16_t*)L"";
  606.         else
  607.             m_text_input_info.inputFieldInfo.init_text = (uint16_t*)init_text;
  608.  
  609.         m_text_input_info.inputFieldInfo.limit_length = length;
  610.  
  611.         m_text_input_info.inputFieldInfo.message = (uint16_t*)dialog_message;
  612.  
  613.         int ret = cellOskDialogLoadAsync(m_text_input_info.container_id,
  614.             &m_text_input_info.dialogParam,
  615.             &m_text_input_info.inputFieldInfo);
  616.         if (ret < 0)
  617.         {
  618.             printf("cellOskDialogLoadAsync() = 0x%x \n", ret);
  619.             return false;
  620.         }
  621.  
  622.         m_text_input_info.status = TEXT_INPUT_STATUS_WAITING;
  623.  
  624.         return true;
  625.     }
  626.     static void abort_text_input()
  627.     {
  628.         if (m_osk_inited == false)
  629.             return;
  630.  
  631.         int ret = cellOskDialogAbort();
  632.         if (ret < 0)
  633.             printf("cellOskDialog failed. ret = %#x\n", ret);
  634.     }
  635.     static void abort_text_input_wait()
  636.     {
  637.         if (m_osk_inited == false)
  638.             return;
  639.  
  640.         int ret = cellOskDialogAbort();
  641.         if (ret < 0)
  642.         {
  643.             printf("cellOskDialogAbort failed. ret = %#x\n", ret);
  644.             return;
  645.         }
  646.  
  647.         while (m_text_input_info.status != TEXT_INPUT_STATUS_ABORTED)
  648.             cellSysutilCheckCallback();
  649.     }
  650.     static bool set_osk_pos(float x, float y)
  651.     {
  652.         m_text_input_info.dialogParam.controlPoint.x = x;
  653.         m_text_input_info.dialogParam.controlPoint.y = y;
  654.  
  655.         return true;
  656.     }
  657.     static bool text_input_set_flags_default()
  658.     {
  659.         memset(&m_text_input_info.dialogParam, 0, sizeof(CellOskDialogParam));
  660.  
  661.         return true;
  662.     }
  663.     static bool text_input_set_flags_alpha_only()
  664.     {
  665.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_ALPHABET;
  666.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_ALPHABET;
  667.         m_text_input_info.dialogParam.prohibitFlgs = 0;
  668.  
  669.         return true;
  670.     }
  671.     static bool text_input_set_flags_alpha_nospace()
  672.     {
  673.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_ALPHABET;
  674.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_ALPHABET;
  675.         m_text_input_info.dialogParam.prohibitFlgs = CELL_OSKDIALOG_NO_SPACE;
  676.  
  677.         return true;
  678.     }
  679.     static bool text_input_set_flags_num_only()
  680.     {
  681.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_NUMERAL;
  682.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_NUMERAL;
  683.         m_text_input_info.dialogParam.prohibitFlgs = 0;
  684.  
  685.         return true;
  686.     }
  687.     static bool text_input_set_flags_num_nospace()
  688.     {
  689.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_NUMERAL;
  690.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_NUMERAL;
  691.         m_text_input_info.dialogParam.prohibitFlgs = CELL_OSKDIALOG_NO_SPACE;
  692.  
  693.         return true;
  694.     }
  695.     static bool text_input_set_flags_alphanum()
  696.     {
  697.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_ENGLISH;
  698.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_ENGLISH;
  699.         m_text_input_info.dialogParam.prohibitFlgs = 0;
  700.  
  701.         return true;
  702.     }
  703.     static bool text_input_set_flags_alphanum_nospace()
  704.     {
  705.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_ENGLISH;
  706.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_ENGLISH;
  707.         m_text_input_info.dialogParam.prohibitFlgs = CELL_OSKDIALOG_NO_SPACE;
  708.  
  709.         return true;
  710.     }
  711.     static bool text_input_set_flags_password()
  712.     {
  713.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_PASSWORD;
  714.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_PASSWORD;
  715.         m_text_input_info.dialogParam.prohibitFlgs = 0;
  716.  
  717.         return true;
  718.     }
  719.     static bool text_input_set_flags_url()
  720.     {
  721.         m_text_input_info.dialogParam.allowOskPanelFlg = CELL_OSKDIALOG_PANELMODE_URL;
  722.         m_text_input_info.dialogParam.firstViewPanel = CELL_OSKDIALOG_PANELMODE_URL;
  723.         m_text_input_info.dialogParam.prohibitFlgs = 0;
  724.  
  725.         return true;
  726.     }
  727.     static bool text_input_set_flags(unsigned int allowOskPanelFlg,
  728.         unsigned int firstViewPanel,
  729.         int prohibitFlgs)
  730.     {
  731.         m_text_input_info.dialogParam.allowOskPanelFlg = allowOskPanelFlg;
  732.         m_text_input_info.dialogParam.firstViewPanel = firstViewPanel;
  733.         m_text_input_info.dialogParam.prohibitFlgs = prohibitFlgs;
  734.  
  735.         return true;
  736.     }
  737.  
  738.     static TextInputStatus text_input_status() { return m_text_input_info.status; }
  739.     static const uint16_t* text_input_result_str() { return m_text_input_info.Result_Text_Buffer; }
  740.  
  741. private:
  742.     static Gamepad m_gamepad;
  743.     static InputStatus m_status;
  744.     Input() {}
  745.  
  746.     static bool m_osk_inited;
  747.     static bool m_init_osk()
  748.     {
  749.         //--------------------------------------------------------------------------------
  750.         // Initialize on-screen keyboard resources
  751.         //--------------------------------------------------------------------------------
  752.         int ret = sys_memory_container_create(&m_text_input_info.container_id, OSK_DIALOG_MEMORY_CONTAINER_SIZE_7);
  753.         if (ret != CELL_OK)
  754.         {
  755.             switch (ret)
  756.             {
  757.             case ENOMEM:
  758.                 printf("Cannot allocate memory of the specified size from the default memory container\n");
  759.                 break;
  760.             case EAGAIN:
  761.                 printf("Kernel memory shortage\n");
  762.                 break;
  763.             case EFAULT:
  764.                 printf("An invalid address is specified\n");
  765.                 break;
  766.             default:
  767.                 break;
  768.             }
  769.             cellMsgDialogOpenErrorCode(ret, NULL, NULL, NULL);
  770.  
  771.             return false;
  772.         }
  773.  
  774.         CellOskDialogPoint pos;
  775.         pos.x = OSK_DIALOG_DEFAULT_X;
  776.         pos.y = OSK_DIALOG_DEFAULT_Y;
  777.         m_text_input_info.dialogParam.controlPoint = pos;
  778.  
  779.         m_text_input_info.OutputInfo.pResultString = m_text_input_info.Result_Text_Buffer;
  780.  
  781.         m_text_input_info.status = TEXT_INPUT_STATUS_OSK_UNLOADED;
  782.  
  783.         m_sys_cb_id = SysCallback::reg_cb(osk_sysutil_callback, NULL);
  784.         if (m_sys_cb_id < 0)
  785.             return false;
  786.         //--------------------------------------------------------------------------------
  787.         // End on-screen keyboard resources initialization
  788.         //--------------------------------------------------------------------------------
  789.  
  790.         m_osk_inited = true;
  791.         return true;
  792.     }
  793.     static void m_shutdown_osk()
  794.     {
  795.         if (!m_osk_inited)
  796.             return;
  797.  
  798.         sys_memory_container_destroy(m_text_input_info.container_id);
  799.  
  800.         SysCallback::unreg_cb(m_sys_cb_id);
  801.  
  802.         m_osk_inited = false;
  803.     }
  804.     static int m_sys_cb_id;
  805.     static void osk_sysutil_callback(uint64_t status, uint64_t param, void * userdata)
  806.     {
  807.         (void)param;
  808.         (void)userdata;
  809.  
  810.         switch (status)
  811.         {
  812.         case CELL_SYSUTIL_OSKDIALOG_LOADED:
  813.             break;
  814.         case CELL_SYSUTIL_OSKDIALOG_FINISHED:
  815.             if (cellOskDialogUnloadAsync(&m_text_input_info.OutputInfo) != 0)
  816.                 printf("cellOskDialogUnloadAsync() failed\n");
  817.  
  818.             m_text_input_info.status = TEXT_INPUT_STATUS_OK;
  819.  
  820.             if (m_text_input_info.OutputInfo.result != CELL_OSKDIALOG_INPUT_FIELD_RESULT_OK)
  821.             {
  822.                 if (m_text_input_info.OutputInfo.result == CELL_OSKDIALOG_INPUT_FIELD_RESULT_CANCELED)
  823.                 {
  824.                     m_text_input_info.status = TEXT_INPUT_STATUS_CANCELED;
  825.                     printf("User canceled input\n");
  826.                 }
  827.                 else if (m_text_input_info.OutputInfo.result == CELL_OSKDIALOG_INPUT_FIELD_RESULT_NO_INPUT_TEXT)
  828.                 {
  829.                     m_text_input_info.status = TEXT_INPUT_STATUS_NO_INPUT;
  830.                     printf("No text inputed\n");
  831.                 }
  832.                 else if (m_text_input_info.OutputInfo.result == CELL_OSKDIALOG_INPUT_FIELD_RESULT_ABORT)
  833.                 {
  834.                     m_text_input_info.status = TEXT_INPUT_STATUS_ABORTED;
  835.                     printf("User aborted input\n");
  836.                 }
  837.                 else
  838.                 {
  839.                     m_text_input_info.status = TEXT_INPUT_STATUS_ABORTED;
  840.                     printf("OutputInfo.result not OK. Uknown result\n");
  841.                 }
  842.                 break;
  843.             }
  844.             printf("%ls\n", (wchar_t*)m_text_input_info.OutputInfo.pResultString);
  845.             break;
  846.         case CELL_SYSUTIL_OSKDIALOG_UNLOADED:
  847.             m_text_input_info.status = TEXT_INPUT_STATUS_OSK_UNLOADED;
  848.             break;
  849.         case CELL_SYSUTIL_OSKDIALOG_INPUT_ENTERED:
  850.             if (cellOskDialogGetInputText(&m_text_input_info.OutputInfo) != 0)
  851.                 printf("cellOskDialogGetInputText() failed\n");;
  852.             printf("%ls\n", (wchar_t*)m_text_input_info.OutputInfo.pResultString);
  853.             break;
  854.         case CELL_SYSUTIL_OSKDIALOG_INPUT_CANCELED:
  855.             cellOskDialogAbort();
  856.             break;
  857.         default: break;
  858.         }
  859.     }
  860.     static Text_Input_Info m_text_input_info;
  861. };
  862. Gamepad Input::m_gamepad;
  863. InputStatus Input::m_status;
  864. Text_Input_Info Input::m_text_input_info;
  865. int Input::m_sys_cb_id;
  866. bool Input::m_osk_inited = false;
  867.  
  868.  
  869.  
  870.  
  871. static void RetrieveInputLoop(
  872.     char *str_buf,
  873.     size_t size,
  874.     bool *canceled = NULL)
  875. {
  876.     while (1) {
  877.         switch (Input::text_input_status())
  878.         {
  879.             //E When the On Screen Keyboard is being used the Input status should be Waiting
  880.         case TEXT_INPUT_STATUS_WAITING:
  881.             break;
  882.  
  883.             //E When the status is no longer Waiting
  884.             //E Get input if the status is OK
  885.         case TEXT_INPUT_STATUS_OK:
  886.             snprintf(str_buf, size, "%ls", (wchar_t*)Input::text_input_result_str());
  887.             break;
  888.  
  889.         case TEXT_INPUT_STATUS_CANCELED:
  890.             if (canceled != NULL) {
  891.                 *canceled = true;
  892.             }
  893.             break;
  894.         case TEXT_INPUT_STATUS_ABORTED:
  895.         case TEXT_INPUT_STATUS_NO_INPUT:
  896.             break;
  897.  
  898.             //E Go back to normal state for any case when we are no longer waiting for the OSK to finish
  899.         default:
  900.             printf("state = %d\n", Input::text_input_status());
  901.             break;
  902.         case TEXT_INPUT_STATUS_OSK_UNLOADED:
  903.             /* OSK has been safely unloaded */
  904.             return;
  905.         }
  906.         sys_timer_usleep(100);
  907.     }
  908.  
  909.     //return; /* end of function */
  910. }
  911.  
  912. void RetrieveInputForText(
  913.     char *str_buf,
  914.     int char_len,
  915.     size_t buf_byte_size,
  916.     const char *initialText,
  917.     bool *canceled)
  918. {
  919.     if (char_len > CELL_OSKDIALOG_STRING_SIZE) char_len = CELL_OSKDIALOG_STRING_SIZE;
  920.  
  921.     memset(str_buf, 0x00, buf_byte_size);
  922.  
  923.     size_t length = strlen(initialText);
  924.     wchar_t *wc = (wchar_t*)malloc(sizeof(wchar_t) * (length + 1));
  925.     memset(wc, 0x00, sizeof(wchar_t) * (length + 1));
  926.     mbstowcs(wc, initialText, length + 1);
  927.     Input::text_input_set_flags(CELL_OSKDIALOG_PANELMODE_ALPHABET,
  928.         CELL_OSKDIALOG_PANELMODE_ALPHABET,
  929.         CELL_OSKDIALOG_NO_RETURN);
  930.     Input::open_text_input(char_len, L"Input text", wc);
  931.  
  932.     RetrieveInputLoop(str_buf, buf_byte_size, canceled);
  933.  
  934.     free(wc);
  935. }
  936.  
  937. void RetrieveInputForUrl(
  938.     char *str_buf,
  939.     int char_len,
  940.     size_t buf_byte_size,
  941.     const char *initialText,
  942.     bool *canceled)
  943. {
  944.     if (char_len > CELL_OSKDIALOG_STRING_SIZE) char_len = CELL_OSKDIALOG_STRING_SIZE;
  945.  
  946.     memset(str_buf, 0x00, buf_byte_size);
  947.  
  948.     size_t length = strlen(initialText);
  949.     wchar_t *wc = (wchar_t*)malloc(sizeof(wchar_t) * (length + 1));
  950.     memset(wc, 0x00, sizeof(wchar_t) * (length + 1));
  951.     mbstowcs(wc, initialText, length + 1);
  952.     Input::text_input_set_flags(CELL_OSKDIALOG_PANELMODE_URL,
  953.         CELL_OSKDIALOG_PANELMODE_URL,
  954.         0);
  955.     Input::open_text_input(char_len, L"Input text", wc);
  956.  
  957.     RetrieveInputLoop(str_buf, buf_byte_size, canceled);
  958.  
  959.     free(wc);
  960. }
  961.  
  962.  
  963.  
  964.  
  965. // useage
  966. // Input::gamepad()->l2_pressed()
Add Comment
Please, Sign In to add comment