[vlc-commits] [Git][videolan/vlc][master] 3 commits: win32: sensors: use ComPtr smart pointers

Hugo Beauzée-Luyssen (@chouquette) gitlab at videolan.org
Sat Feb 12 13:51:45 UTC 2022



Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
99c3ddaf by Alexandre Janniaux at 2022-02-12T12:23:05+00:00
win32: sensors: use ComPtr smart pointers

They allow easier error handling in the loop, and will provide the
foundation for much more simplification in the end.

- - - - -
374c3d00 by Alexandre Janniaux at 2022-02-12T12:23:05+00:00
win32: sensors: refactor SUCCEEDED into FAILED

Early fail and leave the function or continue the loop when an error
happens. The code will less look like a tornado and will directly
signal what happens in case of error, without breaking the flow of the
current success path.

Indentation is not changed to ease review, and will be changed in
a subsequent commit.

- - - - -
6e9d44b9 by Alexandre Janniaux at 2022-02-12T12:23:05+00:00
win32: reindent after last changes

No functional changes.

- - - - -


1 changed file:

- modules/video_output/win32/sensors.cpp


Changes:

=====================================
modules/video_output/win32/sensors.cpp
=====================================
@@ -29,12 +29,15 @@
 #include "common.h"
 
 #include <initguid.h>
+#include <wrl/client.h>
 #include <propsys.h> /* stupid mingw headers don't include this */
 #include <sensors.h>
 #include <sensorsapi.h>
 
 #include <new>
 
+using Microsoft::WRL::ComPtr;
+
 class SensorReceiver : public ISensorEvents
 {
 public:
@@ -151,77 +154,73 @@ private:
 
 void *HookWindowsSensors(vout_display_t *vd, HWND hwnd)
 {
-    ISensor *pSensor = NULL;
-    ISensorManager *pSensorManager;
+    ComPtr<ISensorManager> pSensorManager;
     HRESULT hr = CoCreateInstance( CLSID_SensorManager,
                       NULL, CLSCTX_INPROC_SERVER,
                       IID_ISensorManager, (void**)&pSensorManager );
-    if (SUCCEEDED(hr))
+    if (FAILED(hr))
+        return NULL;
+
+    ComPtr<ISensorCollection> pInclinometers;
+    hr = pSensorManager->GetSensorsByType(SENSOR_TYPE_INCLINOMETER_3D, pInclinometers.GetAddressOf());
+    if (FAILED(hr))
     {
-        ISensorCollection *pInclinometers;
-        hr = pSensorManager->GetSensorsByType(SENSOR_TYPE_INCLINOMETER_3D, &pInclinometers);
-        if (SUCCEEDED(hr))
+        msg_Dbg(vd, "inclinometer not found. (hr=0x%lX)", hr);
+        return NULL;
+    }
+
+    ULONG count;
+    pInclinometers->GetCount(&count);
+    msg_Dbg(vd, "Found %lu inclinometer", count);
+    for (ULONG i=0; i<count; ++i)
+    {
+        ComPtr<ISensor> pSensor;
+        hr = pInclinometers->GetAt(i, pSensor.GetAddressOf());
+        if (FAILED(hr))
+            continue;
+
+        SensorState state = SENSOR_STATE_NOT_AVAILABLE;
+        hr = pSensor->GetState(&state);
+        if (FAILED(hr))
+            continue;
+
+        if (state == SENSOR_STATE_ACCESS_DENIED)
+            hr = pSensorManager->RequestPermissions(hwnd, pInclinometers.Get(), TRUE);
+
+        if (FAILED(hr))
+            continue;
+
+        vlc_viewpoint_t start_viewpoint;
+        vlc_viewpoint_init(&start_viewpoint);
+        PROPVARIANT pvRot;
+        PropVariantInit(&pvRot);
+        hr = pSensor->GetProperty(SENSOR_DATA_TYPE_TILT_X_DEGREES, &pvRot);
+        if (SUCCEEDED(hr) && pvRot.vt == VT_R4)
         {
-            ULONG count;
-            pInclinometers->GetCount(&count);
-            msg_Dbg(vd, "Found %lu inclinometer", count);
-            for (ULONG i=0; i<count; ++i)
-            {
-                hr = pInclinometers->GetAt(i, &pSensor);
-                if (SUCCEEDED(hr))
-                {
-                    SensorState state = SENSOR_STATE_NOT_AVAILABLE;
-                    hr = pSensor->GetState(&state);
-                    if (SUCCEEDED(hr))
-                    {
-                        if (state == SENSOR_STATE_ACCESS_DENIED)
-                            hr = pSensorManager->RequestPermissions(hwnd, pInclinometers, TRUE);
-
-                        if (SUCCEEDED(hr))
-                        {
-                            vlc_viewpoint_t start_viewpoint;
-                            vlc_viewpoint_init(&start_viewpoint);
-                            PROPVARIANT pvRot;
-                            PropVariantInit(&pvRot);
-                            hr = pSensor->GetProperty(SENSOR_DATA_TYPE_TILT_X_DEGREES, &pvRot);
-                            if (SUCCEEDED(hr) && pvRot.vt == VT_R4)
-                            {
-                                start_viewpoint.pitch = pvRot.fltVal;
-                                PropVariantClear(&pvRot);
-                            }
-                            hr = pSensor->GetProperty(SENSOR_DATA_TYPE_TILT_Y_DEGREES, &pvRot);
-                            if (SUCCEEDED(hr) && pvRot.vt == VT_R4)
-                            {
-                                start_viewpoint.roll = pvRot.fltVal;
-                                PropVariantClear(&pvRot);
-                            }
-                            hr = pSensor->GetProperty(SENSOR_DATA_TYPE_TILT_Z_DEGREES, &pvRot);
-                            if (SUCCEEDED(hr) && pvRot.vt == VT_R4)
-                            {
-                                start_viewpoint.yaw = pvRot.fltVal;
-                                PropVariantClear(&pvRot);
-                            }
-
-                            SensorReceiver *received = new(std::nothrow) SensorReceiver(vd, start_viewpoint);
-                            if (received)
-                            {
-                                pSensor->SetEventSink(received);
-                                break;
-                            }
-                        }
-                    }
-
-                    pSensor->Release();
-                    pSensor = NULL;
-                }
-            }
-            pInclinometers->Release();
+            start_viewpoint.pitch = pvRot.fltVal;
+            PropVariantClear(&pvRot);
+        }
+        hr = pSensor->GetProperty(SENSOR_DATA_TYPE_TILT_Y_DEGREES, &pvRot);
+        if (SUCCEEDED(hr) && pvRot.vt == VT_R4)
+        {
+            start_viewpoint.roll = pvRot.fltVal;
+            PropVariantClear(&pvRot);
+        }
+        hr = pSensor->GetProperty(SENSOR_DATA_TYPE_TILT_Z_DEGREES, &pvRot);
+        if (SUCCEEDED(hr) && pvRot.vt == VT_R4)
+        {
+            start_viewpoint.yaw = pvRot.fltVal;
+            PropVariantClear(&pvRot);
+        }
+
+        SensorReceiver *received = new(std::nothrow) SensorReceiver(vd, start_viewpoint);
+        if (received)
+        {
+            pSensor->SetEventSink(received);
+            return pSensor.Detach();
         }
-        else
-            msg_Dbg(vd, "inclinometer not found. (hr=0x%lX)", hr);
-        pSensorManager->Release();
     }
-    return pSensor;
+    return NULL;
 }
 
 void UnhookWindowsSensors(void *vSensor)



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/2d5974555729b0915e9280e822cf66c2dd3304f8...6e9d44b9554f48cdf28abf6a4b9559f302cc8c7e

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/2d5974555729b0915e9280e822cf66c2dd3304f8...6e9d44b9554f48cdf28abf6a4b9559f302cc8c7e
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list