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;
86  std::vector<NVGSDK_ScopePermission> cScopePermissionList;
87  cScopePermissionList.resize(inParams.requiredScopes.size());
88  cOutputParams.scopePermissionTable = &cScopePermissionList[0];
89  cOutputParams.scopePermissionTableSize = cScopePermissionList.size();
90 
91  outParams.returnCode = NVGSDK_Create(&core->m_handle, &cInputParams, &cOutputParams);
92  outParams.versionMajor = cOutputParams.versionMajor;
93  outParams.versionMinor = cOutputParams.versionMinor;
94  outParams.nvidiaGfeVersion = cOutputParams.gfeVersionStr;
95 
96  if (NVGSDK_FAILED(outParams.returnCode))
97  {
98  delete core;
99  return nullptr;
100  }
101 
102  if (cOutputParams.scopePermissionTable)
103  {
104  for (size_t i = 0; i < cOutputParams.scopePermissionTableSize; ++i)
105  {
106  NVGSDK_ScopePermission const& scopePermission = cOutputParams.scopePermissionTable[i];
107  outParams.scopePermissions[scopePermission.scope] = scopePermission.permission;
108  }
109  }
110 
111  return core;
112 }
113 
114 inline CoreImpl::~CoreImpl()
115 {
116  if (!m_handle)
117  {
118  return;
119  }
120 
121  NVGSDK_Release(m_handle);
122 }
123 
124 inline NVGSDK_RetCode CoreImpl::Poll(void)
125 {
126  return NVGSDK_Poll(m_handle);
127 }
128 
129 inline void CoreImpl::RequestPermissionsAsync(RequestPermissionsParams const& params, TCallback callback, void* context)
130 {
132  cParams.scopeTable = const_cast<NVGSDK_Scope*>(&params.scopes[0]);
133  cParams.scopeTableSize = params.scopes.size();
134  return NVGSDK_RequestPermissionsAsync(m_handle, &cParams, callbackWrapper, new CallbackContext<TCallback>({ callback, context }));
135 }
136 
137 inline void CoreImpl::GetUILanguageAsync(TGetUILanguageCallback callback, void* context)
138 {
139  NVGSDK_GetUILanguageAsync(m_handle, [](NVGSDK_RetCode rc, NVGSDK_Language const* data, void* context) {
140  std::unique_ptr<CallbackContext<TGetUILanguageCallback>> callbackContext(reinterpret_cast<CallbackContext<TGetUILanguageCallback>*>(context));
141  if (!callbackContext->callback) return;
142 
143  if (NVGSDK_FAILED(rc))
144  {
145  return callbackContext->callback(rc, nullptr, callbackContext->appContext);
146  }
147 
148  GetUILanguageResponse language;
149  language.cultureCode = data->cultureCode;
150  callbackContext->callback(rc, &language, callbackContext->appContext);
151  }, new CallbackContext<TGetUILanguageCallback>({ callback, context }));
152 }
153 
154 inline NVGSDK_HANDLE* CoreImpl::GetHandle()
155 {
156  return m_handle;
157 }
158 
159 inline void CoreImpl::callbackWrapper(NVGSDK_RetCode rc, void* context)
160 {
161  std::unique_ptr<CallbackContext<TCallback>> callbackContext(reinterpret_cast<CallbackContext<TCallback>*>(context));
162  if (!callbackContext->callback) return;
163 
164  callbackContext->callback(rc, callbackContext->appContext);
165 }
166 
167 inline void CoreImpl::callbackNotificationWrapper(NVGSDK_NotificationType type, NVGSDK_Notification const* data, void* context)
168 {
169  CallbackNotificationContext* callbackContext(reinterpret_cast<CallbackNotificationContext*>(context));
170  if (!callbackContext->callback) return;
171 
172  switch (type)
173  {
175  {
176  PermissionsChangedNotification notification;
177  notification.context = callbackContext->appContext;
178  for (size_t i = 0; i < data->permissionsChanged.scopePermissionTableSize; ++i)
179  {
180  notification.scopePermissions[data->permissionsChanged.scopePermissionTable[i].scope] = data->permissionsChanged.scopePermissionTable[i].permission;
181  }
182  callbackContext->callback(type, notification);
183  break;
184  }
186  {
187  OverlayStateChangedNotification notification;
188  notification.context = callbackContext->appContext;
189  notification.open = data->overlayStateChanged.open;
190  notification.state = data->overlayStateChanged.state;
191  callbackContext->callback(type, notification);
192  break;
193  }
194  default:
195  break;
196  }
197 }
198 
199 inline void CoreImpl::translateLocaleTable(std::map<std::string, std::string> const& localeMap, std::vector<NVGSDK_LocalizedPair>& pairs)
200 {
201  for (auto it = localeMap.begin(); it != localeMap.end(); ++it)
202  {
203  pairs.push_back({
204  it->first.c_str(),
205  it->second.c_str()
206  });
207  }
208 }
209 }
210 
211 #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