Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,7 +112,7 @@ jobs:
Set-Location -Path usbmmidd_v2/usbmmidd_v2
./deviceinstaller64 install usbmmidd.inf usbmmidd

# create 2 virtual displays, using 4 can crash the runner
# create 2 virtual displays, using 3+ can crash the runner
# see: https://github.com/LizardByte/libdisplaydevice/pull/36
for ($i = 1; $i -le 2; $i++) {
./deviceinstaller64 enableidd 1
Expand Down
15 changes: 15 additions & 0 deletions src/windows/include/displaydevice/windows/winapiutils.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,6 +61,21 @@ namespace display_device::win_utils {
void
setActive(DISPLAYCONFIG_PATH_INFO &path);

/**
* @brief Check if the display's source mode is primary - if the associated device is a primary display device.
* @param mode Mode to check.
* @returns True if the mode's origin point is at (0, 0) coordinate (primary), false otherwise.
* @note It is possible to have multiple primary source modes at the same time.
*
* EXAMPLES:
* ```cpp
* DISPLAYCONFIG_SOURCE_MODE mode;
* const bool is_primary = isPrimary(mode);
* ```
*/
bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode);

/**
* @brief Get the source mode index from the path.
*
Expand Down
8 changes: 8 additions & 0 deletions src/windows/include/displaydevice/windows/windisplaydevice.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -43,6 +43,14 @@ namespace display_device {
[[nodiscard]] bool
setDisplayModes(const DeviceDisplayModeMap &modes) override;

/** For details @see WinDisplayDeviceInterface::isPrimary */
[[nodiscard]] bool
isPrimary(const std::string &device_id) const override;

/** For details @see WinDisplayDeviceInterface::setAsPrimary */
[[nodiscard]] bool
setAsPrimary(const std::string &device_id) override;

private:
std::shared_ptr<WinApiLayerInterface> m_w_api;
};
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -119,5 +119,36 @@ namespace display_device {
*/
[[nodiscard]] virtual bool
setDisplayModes(const DeviceDisplayModeMap &modes) = 0;

/**
* @brief Check whether the specified device is primary.
* @param device_id A device to perform the check for.
* @returns True if the device is primary, false otherwise.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool is_primary = iface->isPrimary(device_id);
* ```
*/
[[nodiscard]] virtual bool
isPrimary(const std::string &device_id) const = 0;

/**
* @brief Set the device as a primary display.
* @param device_id A device to set as primary.
* @returns True if the device is or was set as primary, false otherwise.
* @note On Windows if the device is duplicated, the other duplicated device(-s) will also become a primary device.
*
* EXAMPLES:
* ```cpp
* const WinDisplayDeviceInterface* iface = getIface(...);
* const std::string device_id { "MY_DEVICE_ID" };
* const bool success = iface->set_as_primary_device(device_id);
* ```
*/
[[nodiscard]] virtual bool
setAsPrimary(const std::string &device_id) = 0;
};
} // namespace display_device
5 changes: 5 additions & 0 deletions src/windows/winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -76,6 +76,11 @@ namespace display_device::win_utils {
path.flags |= DISPLAYCONFIG_PATH_ACTIVE;
}

bool
isPrimary(const DISPLAYCONFIG_SOURCE_MODE &mode) {
return mode.position.x == 0 && mode.position.y == 0;
}

std::optional<UINT32>
getSourceIndex(const DISPLAYCONFIG_PATH_INFO &path, const std::vector<DISPLAYCONFIG_MODE_INFO> &modes) {
// The MS docs is not clear when to access the index union struct or not. It appears that union struct is available,
Expand Down
107 changes: 107 additions & 0 deletions src/windows/windisplaydeviceprimary.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
// class header include
#include "displaydevice/windows/windisplaydevice.h"

// local includes
#include "displaydevice/logging.h"
#include "displaydevice/windows/winapiutils.h"

namespace display_device {
bool
WinDisplayDevice::isPrimary(const std::string &device_id) const {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

const auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find active device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

return win_utils::isPrimary(*source_mode);
}

bool
WinDisplayDevice::setAsPrimary(const std::string &device_id) {
if (device_id.empty()) {
DD_LOG(error) << "Device id is empty!";
return false;
}

auto display_data { m_w_api->queryDisplayConfig(QueryType::Active) };
if (!display_data) {
// Error already logged
return false;
}

// Get the current origin point of the device (the one that we want to make primary)
POINTL origin;
{
const auto path { win_utils::getActivePath(*m_w_api, device_id, display_data->m_paths) };
if (!path) {
DD_LOG(error) << "Failed to find device for " << device_id << "!";
return false;
}

const auto source_mode { win_utils::getSourceMode(win_utils::getSourceIndex(*path, display_data->m_modes), display_data->m_modes) };
if (!source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << device_id << "!";
return false;
}

if (win_utils::isPrimary(*source_mode)) {
DD_LOG(debug) << "Device " << device_id << " is already a primary device.";
return true;
}

origin = source_mode->position;
}

// Shift the source mode origin points accordingly, so that the provided
// device moves to (0, 0) position and others to their new positions.
std::set<UINT32> modified_modes;
for (auto &path : display_data->m_paths) {
const auto current_id { m_w_api->getDeviceId(path) };
const auto source_index { win_utils::getSourceIndex(path, display_data->m_modes) };
auto source_mode { win_utils::getSourceMode(source_index, display_data->m_modes) };

if (!source_index || !source_mode) {
DD_LOG(error) << "Active device does not have a source mode: " << current_id << "!";
return false;
}

if (modified_modes.find(*source_index) != std::end(modified_modes)) {
// Happens when VIRTUAL_MODE_AWARE is not specified when querying paths, probably will never happen in our (since it's always set), but just to be safe...
DD_LOG(debug) << "Device " << current_id << " shares the same mode index as a previous device. Device is duplicated. Skipping.";
continue;
}

source_mode->position.x -= origin.x;
source_mode->position.y -= origin.y;

modified_modes.insert(*source_index);
}

const UINT32 flags { SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG | SDC_SAVE_TO_DATABASE | SDC_VIRTUAL_MODE_AWARE };
const LONG result { m_w_api->setDisplayConfig(display_data->m_paths, display_data->m_modes, flags) };
if (result != ERROR_SUCCESS) {
DD_LOG(error) << m_w_api->getErrorString(result) << " failed to set primary mode for " << device_id << "!";
return false;
}

return true;
}
} // namespace display_device
14 changes: 14 additions & 0 deletions tests/unit/windows/test_winapiutils.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -181,6 +181,20 @@ TEST_F_S_MOCKED(IsActiveAndSetActive) {
EXPECT_EQ(display_device::win_utils::isActive(contains_active_path), true);
}

TEST_F_S_MOCKED(IsPrimary) {
DISPLAYCONFIG_SOURCE_MODE primary_mode;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_1;
DISPLAYCONFIG_SOURCE_MODE non_primary_mode_2;

primary_mode.position = { 0, 0 };
non_primary_mode_1.position = { 1, 0 };
non_primary_mode_2.position = { 0, 2 };

EXPECT_TRUE(display_device::win_utils::isPrimary(primary_mode));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_1));
EXPECT_FALSE(display_device::win_utils::isPrimary(non_primary_mode_2));
}

TEST_F_S_MOCKED(GetSourceIndex) {
DISPLAYCONFIG_PATH_INFO path;
std::vector<DISPLAYCONFIG_MODE_INFO> modes;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/windows/test_windisplaydevicemodes.cpp
Original file line numberDiff line numberDiff line change
Expand Up@@ -159,7 +159,7 @@ TEST_F_S(GetCurrentDisplayModes) {
}

TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand All@@ -184,7 +184,7 @@ TEST_F_S(SetCurrentDisplayModes, ExtendedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 2) {
Expand DownExpand Up@@ -214,7 +214,7 @@ TEST_F_S(SetCurrentDisplayModes, DuplicatedTopology) {
}

TEST_F_S(SetCurrentDisplayModes, MixedTopology) {
const auto available_devices { getAvailableDevices(*m_layer, true) };
const auto available_devices { getAvailableDevices(*m_layer) };
ASSERT_TRUE(available_devices);

if (available_devices->size() < 3) {
Expand Down
Loading