-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
IOSKeyboard.mm
100 lines (87 loc) · 2.83 KB
/
IOSKeyboard.mm
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
/*
* Copyright (C) 2012-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "platform/darwin/ios-common/IOSKeyboard.h"
#include "platform/darwin/NSLogDebugHelpers.h"
#include "platform/darwin/ios-common/IOSKeyboardView.h"
#include "platform/darwin/ios/XBMCController.h"
class CIOSKeyboardImpl
{
public:
KeyboardView* g_pIosKeyboard = nil;
};
CIOSKeyboard::CIOSKeyboard()
: CGUIKeyboard()
, m_pCharCallback{nullptr}
, m_bCanceled{false}
, m_impl{new CIOSKeyboardImpl}
{
}
bool CIOSKeyboard::ShowAndGetInput(char_callback_t pCallback, const std::string &initialString, std::string &typedString, const std::string &heading, bool bHiddenInput)
{
// we are in xbmc main thread or python module thread.
@autoreleasepool
{
@synchronized([KeyboardView class])
{
// in case twice open keyboard.
if (m_impl->g_pIosKeyboard)
return false;
// assume we are only drawn on the mainscreen ever!
auto keyboardFrame = [g_xbmcController fullscreenSubviewFrame];
//create the keyboardview
m_impl->g_pIosKeyboard = [[KeyboardView alloc] initWithFrame:keyboardFrame];
if (!m_impl->g_pIosKeyboard)
return false;
// inform the controller that the native keyboard is active
// basically as long as m_impl->g_pIosKeyboard exists...
[g_xbmcController nativeKeyboardActive:true];
}
m_pCharCallback = pCallback;
// init keyboard stuff
SetTextToKeyboard(initialString);
[m_impl->g_pIosKeyboard setHidden:bHiddenInput];
[m_impl->g_pIosKeyboard setHeading:[NSString stringWithUTF8String:heading.c_str()]];
[m_impl->g_pIosKeyboard registerKeyboard:this]; // for calling back
bool confirmed = false;
if (!m_bCanceled)
{
[m_impl->g_pIosKeyboard setCancelFlag:&m_bCanceled];
[m_impl->g_pIosKeyboard activate]; // blocks and shows keyboard
// user is done - get resulted text and confirmation
confirmed = m_impl->g_pIosKeyboard.isConfirmed;
if (confirmed)
typedString = [m_impl->g_pIosKeyboard.text UTF8String];
}
@synchronized([KeyboardView class])
{
m_impl->g_pIosKeyboard = nil;
[g_xbmcController nativeKeyboardActive:false];
}
return confirmed;
}
}
void CIOSKeyboard::Cancel()
{
m_bCanceled = true;
}
bool CIOSKeyboard::SetTextToKeyboard(const std::string &text, bool closeKeyboard /* = false */)
{
if (!m_impl->g_pIosKeyboard)
return false;
[m_impl->g_pIosKeyboard setKeyboardText:[NSString stringWithUTF8String:text.c_str()]
closeKeyboard:closeKeyboard ? YES : NO];
return true;
}
//wrap our callback between objc and c++
void CIOSKeyboard::fireCallback(const std::string &str)
{
if(m_pCharCallback)
{
m_pCharCallback(this, str);
}
}