NVIDIAGeForceExperienceSDK
isdk_cpp_impl.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
2 *
3 * NVIDIA CORPORATION and its licensors retain all intellectual property
4 * and proprietary rights in and to this software, related documentation
5 * and any modifications thereto. Any use, reproduction, disclosure or
6 * distribution of this software and related documentation without an express
7 * license agreement from NVIDIA CORPORATION is strictly prohibited.
8 */
9 
16 #ifndef GFESDK_ISDK_CPP_IMPL_H
17 #define GFESDK_ISDK_CPP_IMPL_H
18 
19 #include "isdk_cpp.h"
20 
21 #include <memory>
22 
23 namespace GfeSDK
24 {
25 class CoreImpl : public Core
26 {
27 public:
28  ~CoreImpl();
29 
30  virtual NVGSDK_RetCode Poll(void);
31  virtual void RequestPermissionsAsync(RequestPermissionsParams const&, TCallback cb = nullptr, void* cbContext = nullptr);
32  virtual void GetUILanguageAsync(TGetUILanguageCallback cb = nullptr, void* cbContext = nullptr);
33 
34  NVGSDK_HANDLE* GetHandle();
35 
36  friend class Core;
37  friend class HighlightsImpl;
38 private:
39  // Remove default construct, copy and assignment operators
40  CoreImpl() {}
41  CoreImpl(const CoreImpl& other) {}
42  CoreImpl& operator=(const CoreImpl& other) {}
43 
44 private:
45  template <typename T>
46  struct CallbackContext
47  {
48  T callback;
49  void* appContext;
50  };
51  struct CallbackNotificationContext
52  {
53  TNotificationCallback callback;
54  void* appContext;
55  };
56  static void __stdcall callbackWrapper(NVGSDK_RetCode rc, void* appContext);
57  static void __stdcall callbackNotificationWrapper(NVGSDK_NotificationType type, NVGSDK_Notification const* data, void* context);
58  static inline void translateLocaleTable(std::map<std::string, std::string> const& localeMap, std::vector<NVGSDK_LocalizedPair>& pairs);
59 
60  NVGSDK_HANDLE* m_handle;
61  std::unique_ptr<CallbackNotificationContext> m_notificationCallbackContext;
62 };
63 
64 inline Core* Core::Create(CreateInputParams const& inParams, CreateResponse& outParams)
65 {
66  CoreImpl* core = new CoreImpl();
67 
68  NVGSDK_CreateInputParams cInputParams;
69  memset(&cInputParams, 0, sizeof(cInputParams));
70  cInputParams.appName = inParams.appName.c_str();
71  cInputParams.pollForCallbacks = inParams.pollForCallbacks;
72  if (!inParams.requiredScopes.empty())
73  {
74  cInputParams.scopeTable = const_cast<NVGSDK_Scope*>(&inParams.requiredScopes[0]);
75  cInputParams.scopeTableSize = inParams.requiredScopes.size();
76  }
77  if (inParams.notificationCallback)
78  {
79  cInputParams.notificationCallback = &CoreImpl::callbackNotificationWrapper;
80  core->m_notificationCallbackContext.reset(new CoreImpl::CallbackNotificationContext({ inParams.notificationCallback, inParams.notificationCallbackContext }));
81  cInputParams.notificationCallbackContext = core->m_notificationCallbackContext.get();
82 
83  }
84 
85  NVGSDK_CreateResponse cOutputParams = { 0 };
86  std::vector<NVGSDK_ScopePermission> cScopePermissionList;
87  cScopePermissionList.resize(inParams.requiredScopes.size());
88 
89  if (!inParams.requiredScopes.empty())
90  {
91  cOutputParams.scopePermissionTable = &cScopePermissionList[0];
92  cOutputParams.scopePermissionTableSize = cScopePermissionList.size();
93  }
94 
95  outParams.returnCode = NVGSDK_Create(&core->m_handle, &cInputParams, &cOutputParams);
96  outParams.versionMajor = cOutputParams.versionMajor;
97  outParams.versionMinor = cOutputParams.versionMinor;
98  outParams.nvidiaGfeVersion = cOutputParams.gfeVersionStr;
99 
100  if (NVGSDK_FAILED(outParams.returnCode))
101  {
102  delete core;
103  return nullptr;
104  }
105 
106  if (cOutputParams.scopePermissionTable)
107  {
108  for (size_t i = 0; i < cOutputParams.scopePermissionTableSize; ++i)
109  {
110  NVGSDK_ScopePermission const& scopePermission = cOutputParams.scopePermissionTable[i];
111  outParams.scopePermissions[scopePermission.scope] = scopePermission.permission;
112  }
113  }
114 
115  return core;
116 }
117 
118 inline CoreImpl::~CoreImpl()
119 {
120  if (!m_handle)
121  {
122  return;
123  }
124 
125  NVGSDK_Release(m_handle);
126 }
127 
128 inline NVGSDK_RetCode CoreImpl::Poll(void)
129 {
130  return NVGSDK_Poll(m_handle);
131 }
132 
133 inline void CoreImpl::RequestPermissionsAsync(RequestPermissionsParams const& params, TCallback callback, void* context)
134 {
136  cParams.scopeTable = const_cast<NVGSDK_Scope*>(&params.scopes[0]);
137  cParams.scopeTableSize = params.scopes.size();
138  return NVGSDK_RequestPermissionsAsync(m_handle, &cParams, callbackWrapper, new CallbackContext<TCallback>({ callback, context }));
139 }
140 
141 inline void CoreImpl::GetUILanguageAsync(TGetUILanguageCallback callback, void* context)
142 {
143  NVGSDK_GetUILanguageAsync(m_handle, [](NVGSDK_RetCode rc, NVGSDK_Language const* data, void* context) {
144  std::unique_ptr<CallbackContext<TGetUILanguageCallback>> callbackContext(reinterpret_cast<CallbackContext<TGetUILanguageCallback>*>(context));
145  if (!callbackContext->callback) return;
146 
147  if (NVGSDK_FAILED(rc))
148  {
149  return callbackContext->callback(rc, nullptr, callbackContext->appContext);
150  }
151 
152  GetUILanguageResponse language;
153  language.cultureCode = data->cultureCode;
154  callbackContext->callback(rc, &language, callbackContext->appContext);
155  }, new CallbackContext<TGetUILanguageCallback>({ callback, context }));
156 }
157 
158 inline NVGSDK_HANDLE* CoreImpl::GetHandle()
159 {
160  return m_handle;
161 }
162 
163 inline void CoreImpl::callbackWrapper(NVGSDK_RetCode rc, void* context)
164 {
165  std::unique_ptr<CallbackContext<TCallback>> callbackContext(reinterpret_cast<CallbackContext<TCallback>*>(context));
166  if (!callbackContext->callback) return;
167 
168  callbackContext->callback(rc, callbackContext->appContext);
169 }
170 
171 inline void CoreImpl::callbackNotificationWrapper(NVGSDK_NotificationType type, NVGSDK_Notification const* data, void* context)
172 {
173  CallbackNotificationContext* callbackContext(reinterpret_cast<CallbackNotificationContext*>(context));
174  if (!callbackContext->callback) return;
175 
176  switch (type)
177  {
179  {
180  PermissionsChangedNotification notification;
181  notification.context = callbackContext->appContext;
182  for (size_t i = 0; i < data->permissionsChanged.scopePermissionTableSize; ++i)
183  {
184  notification.scopePermissions[data->permissionsChanged.scopePermissionTable[i].scope] = data->permissionsChanged.scopePermissionTable[i].permission;
185  }
186  callbackContext->callback(type, notification);
187  break;
188  }
190  {
191  OverlayStateChangedNotification notification;
192  notification.context = callbackContext->appContext;
193  notification.open = data->overlayStateChanged.open;
194  notification.state = data->overlayStateChanged.state;
195  callbackContext->callback(type, notification);
196  break;
197  }
198  default:
199  break;
200  }
201 }
202 
203 inline void CoreImpl::translateLocaleTable(std::map<std::string, std::string> const& localeMap, std::vector<NVGSDK_LocalizedPair>& pairs)
204 {
205  for (auto it = localeMap.begin(); it != localeMap.end(); ++it)
206  {
207  pairs.push_back({
208  it->first.c_str(),
209  it->second.c_str()
210  });
211  }
212 }
213 }
214 
215 #endif //GFESDK_ISDK_CPP_H
NVGSDK_ScopePermission * scopePermissionTable
Must be provided to call. Will be filled with scope/permission pairs.
Definition: sdk_types.h:128
NVGSDK_EXPORT NVGSDK_RetCode NVGSDKApi NVGSDK_Poll(NVGSDK_HANDLE *handle)
C++ binding for NVGSDK_GetUILanguageResponse.
Definition: sdk_types_cpp.h:67
char gfeVersionStr[NVGSDK_MAX_LENGTH]
Must be provided to call. Will be populated by GFE version string.
Definition: sdk_types.h:127
NVGSDK_OverlayState state
State the overlay is closing from or opening to.
Definition: sdk_types.h:86
C++ binding for NVGSDK_CreateResponse.
Definition: sdk_types_cpp.h:57
Sent when the user accepts/denies the permissions dialog, or toggles in GFE3.
Definition: sdk_types.h:51
char const * appName
Provide the name of the application. Will be used in cases when NVIDIA cannot detect game...
Definition: sdk_types.h:104
NVGSDK_Scope * scopeTable
List of scopes used by the application.
Definition: sdk_types.h:105
NVGSDK_NotificationType
Definition: sdk_types.h:49
NVGSDK_EXPORT void NVGSDKApi NVGSDK_RequestPermissionsAsync(NVGSDK_HANDLE *handle, NVGSDK_RequestPermissionsParams const *params, NVGSDK_EmptyCallback callback, void *context)
size_t scopeTableSize
Number of entries in the scope table.
Definition: sdk_types.h:106
void * notificationCallbackContext
Passed along unchanged with each notification.
Definition: sdk_types.h:110
NVGSDK_EXPORT NVGSDK_RetCode NVGSDKApi NVGSDK_Release(NVGSDK_HANDLE *handle)
bool pollForCallbacks
Set to true to poll for asynchronous callbacks on an app thread. If false, callbacks will occur on a ...
Definition: sdk_types.h:107
char const * cultureCode
Will be populated by the user&#39;s GFE language selection.
Definition: sdk_types.h:137
uint16_t versionMinor
Will be populated with SDK minor version.
Definition: sdk_types.h:126
virtual void RequestPermissionsAsync(RequestPermissionsParams const &, TCallback cb=nullptr, void *cbContext=nullptr)
virtual void GetUILanguageAsync(TGetUILanguageCallback cb=nullptr, void *cbContext=nullptr)
NVGSDK_EXPORT void NVGSDKApi NVGSDK_GetUILanguageAsync(NVGSDK_HANDLE *handle, NVGSDK_GetUILanguageCallback callback, void *context)
virtual NVGSDK_RetCode Poll(void)
static Core * Create(CreateInputParams const &, CreateResponse &)
Definition: isdk_cpp_impl.h:64
bool open
true if the overlay is opening, false if it is closing
Definition: sdk_types.h:85
uint16_t versionMajor
Will be populated with SDK major version.
Definition: sdk_types.h:125
Sent when the in-game overlay is shown or removed.
Definition: sdk_types.h:52
NVGSDK_EXPORT NVGSDK_RetCode NVGSDKApi NVGSDK_Create(NVGSDK_HANDLE **handle, NVGSDK_CreateInputParams const *inParams, NVGSDK_CreateResponse *outParams)
NVGSDK_NotificationCallback notificationCallback
Called on unsolicited notifications.
Definition: sdk_types.h:109
size_t scopePermissionTableSize
Must be provided to call.
Definition: sdk_types.h:129