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  friend class OpsImpl;
39 private:
40  // Remove default construct, copy and assignment operators
41  CoreImpl() {}
42  CoreImpl(const CoreImpl& other) {}
43  CoreImpl& operator=(const CoreImpl& other) {}
44 
45 private:
46  template <typename T>
47  struct CallbackContext
48  {
49  T callback;
50  void* appContext;
51  };
52  struct CallbackNotificationContext
53  {
54  TNotificationCallback callback;
55  void* appContext;
56  };
57  static void __stdcall callbackWrapper(NVGSDK_RetCode rc, void* appContext);
58  static void __stdcall callbackNotificationWrapper(NVGSDK_NotificationType type, NVGSDK_Notification const* data, void* context);
59  static inline void translateLocaleTable(std::map<std::string, std::string> const& localeMap, std::vector<NVGSDK_LocalizedPair>& pairs);
60 
61  NVGSDK_HANDLE* m_handle;
62  std::unique_ptr<CallbackNotificationContext> m_notificationCallbackContext;
63 };
64 
65 inline Core* Core::Create(CreateInputParams const& inParams, CreateResponse& outParams)
66 {
67  CoreImpl* core = new CoreImpl();
68 
69  NVGSDK_CreateInputParams cInputParams;
70  memset(&cInputParams, 0, sizeof(cInputParams));
71  cInputParams.appName = inParams.appName.c_str();
72  cInputParams.pollForCallbacks = inParams.pollForCallbacks;
73  if (!inParams.requiredScopes.empty())
74  {
75  cInputParams.scopeTable = const_cast<NVGSDK_Scope*>(&inParams.requiredScopes[0]);
76  cInputParams.scopeTableSize = inParams.requiredScopes.size();
77  }
78  if (inParams.notificationCallback)
79  {
80  cInputParams.notificationCallback = &CoreImpl::callbackNotificationWrapper;
81  core->m_notificationCallbackContext.reset(new CoreImpl::CallbackNotificationContext({ inParams.notificationCallback, inParams.notificationCallbackContext }));
82  cInputParams.notificationCallbackContext = core->m_notificationCallbackContext.get();
83  }
84  cInputParams.targetPid = inParams.targetPid;
85  if (!inParams.targetPath.empty())
86  {
87  cInputParams.targetPath = inParams.targetPath.c_str();
88  }
89 
90  NVGSDK_CreateResponse cOutputParams = { 0 };
91  std::vector<NVGSDK_ScopePermission> cScopePermissionList;
92  cScopePermissionList.resize(inParams.requiredScopes.size());
93 
94  if (!inParams.requiredScopes.empty())
95  {
96  cOutputParams.scopePermissionTable = &cScopePermissionList[0];
97  cOutputParams.scopePermissionTableSize = cScopePermissionList.size();
98  }
99 
100  outParams.returnCode = NVGSDK_Create(&core->m_handle, &cInputParams, &cOutputParams);
101  outParams.versionMajor = cOutputParams.versionMajor;
102  outParams.versionMinor = cOutputParams.versionMinor;
103  outParams.nvidiaGfeVersion = cOutputParams.gfeVersionStr;
104 
105  if (NVGSDK_FAILED(outParams.returnCode))
106  {
107  delete core;
108  return nullptr;
109  }
110 
111  if (cOutputParams.scopePermissionTable)
112  {
113  for (size_t i = 0; i < cOutputParams.scopePermissionTableSize; ++i)
114  {
115  NVGSDK_ScopePermission const& scopePermission = cOutputParams.scopePermissionTable[i];
116  outParams.scopePermissions[scopePermission.scope] = scopePermission.permission;
117  }
118  }
119 
120  return core;
121 }
122 
123 inline CoreImpl::~CoreImpl()
124 {
125  if (!m_handle)
126  {
127  return;
128  }
129 
130  NVGSDK_Release(m_handle);
131 }
132 
133 inline NVGSDK_RetCode CoreImpl::Poll(void)
134 {
135  return NVGSDK_Poll(m_handle);
136 }
137 
138 inline void CoreImpl::RequestPermissionsAsync(RequestPermissionsParams const& params, TCallback callback, void* context)
139 {
141  cParams.scopeTable = const_cast<NVGSDK_Scope*>(&params.scopes[0]);
142  cParams.scopeTableSize = params.scopes.size();
143  return NVGSDK_RequestPermissionsAsync(m_handle, &cParams, callbackWrapper, new CallbackContext<TCallback>({ callback, context }));
144 }
145 
146 inline void CoreImpl::GetUILanguageAsync(TGetUILanguageCallback callback, void* context)
147 {
148  NVGSDK_GetUILanguageAsync(m_handle, [](NVGSDK_RetCode rc, NVGSDK_Language const* data, void* context) {
149  std::unique_ptr<CallbackContext<TGetUILanguageCallback>> callbackContext(reinterpret_cast<CallbackContext<TGetUILanguageCallback>*>(context));
150  if (!callbackContext->callback) return;
151 
152  if (NVGSDK_FAILED(rc))
153  {
154  return callbackContext->callback(rc, nullptr, callbackContext->appContext);
155  }
156 
157  GetUILanguageResponse language;
158  language.cultureCode = data->cultureCode;
159  callbackContext->callback(rc, &language, callbackContext->appContext);
160  }, new CallbackContext<TGetUILanguageCallback>({ callback, context }));
161 }
162 
163 inline NVGSDK_HANDLE* CoreImpl::GetHandle()
164 {
165  return m_handle;
166 }
167 
168 inline void CoreImpl::callbackWrapper(NVGSDK_RetCode rc, void* context)
169 {
170  std::unique_ptr<CallbackContext<TCallback>> callbackContext(reinterpret_cast<CallbackContext<TCallback>*>(context));
171  if (!callbackContext->callback) return;
172 
173  callbackContext->callback(rc, callbackContext->appContext);
174 }
175 
176 inline void CoreImpl::callbackNotificationWrapper(NVGSDK_NotificationType type, NVGSDK_Notification const* data, void* context)
177 {
178  CallbackNotificationContext* callbackContext(reinterpret_cast<CallbackNotificationContext*>(context));
179  if (!callbackContext->callback) return;
180 
181  switch (type)
182  {
184  {
185  PermissionsChangedNotification notification;
186  notification.context = callbackContext->appContext;
187  for (size_t i = 0; i < data->permissionsChanged.scopePermissionTableSize; ++i)
188  {
189  notification.scopePermissions[data->permissionsChanged.scopePermissionTable[i].scope] = data->permissionsChanged.scopePermissionTable[i].permission;
190  }
191  callbackContext->callback(type, notification);
192  break;
193  }
195  {
196  OverlayStateChangedNotification notification;
197  notification.context = callbackContext->appContext;
198  notification.open = data->overlayStateChanged.open;
199  notification.state = data->overlayStateChanged.state;
200  callbackContext->callback(type, notification);
201  break;
202  }
203  default:
204  break;
205  }
206 }
207 
208 inline void CoreImpl::translateLocaleTable(std::map<std::string, std::string> const& localeMap, std::vector<NVGSDK_LocalizedPair>& pairs)
209 {
210  for (auto it = localeMap.begin(); it != localeMap.end(); ++it)
211  {
212  pairs.push_back({
213  it->first.c_str(),
214  it->second.c_str()
215  });
216  }
217 }
218 }
219 
220 #endif //GFESDK_ISDK_CPP_H
NVGSDK_ScopePermission * scopePermissionTable
Must be provided to call. Will be filled with scope/permission pairs.
Definition: sdk_types.h:134
NVGSDK_EXPORT NVGSDK_RetCode NVGSDKApi NVGSDK_Poll(NVGSDK_HANDLE *handle)
C++ binding for NVGSDK_GetUILanguageResponse.
Definition: sdk_types_cpp.h:71
char gfeVersionStr[NVGSDK_MAX_LENGTH]
Must be provided to call. Will be populated by GFE version string.
Definition: sdk_types.h:133
NVGSDK_OverlayState state
State the overlay is closing from or opening to.
Definition: sdk_types.h:87
C++ binding for NVGSDK_CreateResponse.
Definition: sdk_types_cpp.h:61
Sent when the user accepts/denies the permissions dialog, or toggles in GFE3.
Definition: sdk_types.h:52
char const * appName
Provide the name of the application. Will be used in cases when NVIDIA cannot detect game...
Definition: sdk_types.h:105
NVGSDK_Scope * scopeTable
List of scopes used by the application.
Definition: sdk_types.h:106
NVGSDK_NotificationType
Definition: sdk_types.h:50
uint32_t targetPid
Advanced usage.
Definition: sdk_types_cpp.h:46
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:107
std::string targetPath
Advanced usage.
Definition: sdk_types_cpp.h:47
void * notificationCallbackContext
Passed along unchanged with each notification.
Definition: sdk_types.h:111
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:108
char const * cultureCode
Will be populated by the user&#39;s GFE language selection.
Definition: sdk_types.h:143
uint16_t versionMinor
Will be populated with SDK minor version.
Definition: sdk_types.h:132
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)
uint32_t targetPid
Used for linking to game-related process&#39;s PID that also supports NVIDIA Highlights. Leave empty when not used.
Definition: sdk_types.h:113
char const * targetPath
Used for linking to game-related process&#39;s path that also supports NVIDIA Highlights. Must be defined if targetPid is defined. Leave empty when not used.
Definition: sdk_types.h:115
static Core * Create(CreateInputParams const &, CreateResponse &)
Definition: isdk_cpp_impl.h:65
bool open
true if the overlay is opening, false if it is closing
Definition: sdk_types.h:86
uint16_t versionMajor
Will be populated with SDK major version.
Definition: sdk_types.h:131
Sent when the in-game overlay is shown or removed.
Definition: sdk_types.h:53
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:110
size_t scopePermissionTableSize
Must be provided to call.
Definition: sdk_types.h:135