Redo to minimize code duplication.

This commit is contained in:
Lasse Luttermann 2024-05-16 12:37:53 +02:00
parent 28ef5b7db2
commit d2a719d67c
1 changed files with 16 additions and 24 deletions

View File

@ -364,7 +364,7 @@ void UPower::setDisplayDevice() {
upDevice_.upDevice = up_client_get_display_device(upClient_); upDevice_.upDevice = up_client_get_display_device(upClient_);
getUpDeviceInfo(upDevice_); getUpDeviceInfo(upDevice_);
} else if (!nativePath_.empty()) { } else {
g_ptr_array_foreach( g_ptr_array_foreach(
up_client_get_devices2(upClient_), up_client_get_devices2(upClient_),
[](gpointer data, gpointer user_data) { [](gpointer data, gpointer user_data) {
@ -372,30 +372,22 @@ void UPower::setDisplayDevice() {
auto thisPtr{static_cast<UPower *>(user_data)}; auto thisPtr{static_cast<UPower *>(user_data)};
upDevice.upDevice = static_cast<UpDevice *>(data); upDevice.upDevice = static_cast<UpDevice *>(data);
thisPtr->getUpDeviceInfo(upDevice); thisPtr->getUpDeviceInfo(upDevice);
if (upDevice.nativePath == nullptr) return; upDevice_output displayDevice{NULL};
if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) { if (!thisPtr->nativePath_.empty()) {
// Unref current upDevice if (upDevice.nativePath == nullptr) return;
if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice); if (0 == std::strcmp(upDevice.nativePath, thisPtr->nativePath_.c_str())) {
// Reassign new upDevice displayDevice = upDevice;
thisPtr->upDevice_ = upDevice; }
} } else {
}, if (upDevice.model == nullptr) return;
this); if (0 == std::strcmp(upDevice.model, thisPtr->model_.c_str())) {
} else { // if `nativePath_` is empty, but `model_` is not. displayDevice = upDevice;
g_ptr_array_foreach( }
up_client_get_devices2(upClient_),
[](gpointer data, gpointer user_data) {
upDevice_output upDevice;
auto thisPtr{static_cast<UPower *>(user_data)};
upDevice.upDevice = static_cast<UpDevice *>(data);
thisPtr->getUpDeviceInfo(upDevice);
if (upDevice.model == nullptr) return;
if (0 == std::strcmp(upDevice.model, thisPtr->model_.c_str())) {
// Unref current upDevice
if (thisPtr->upDevice_.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice);
// Reassign new upDevice
thisPtr->upDevice_ = upDevice;
} }
// Unref current upDevice
if (displayDevice.upDevice != NULL) g_object_unref(thisPtr->upDevice_.upDevice);
// Reassign new upDevice
thisPtr->upDevice_ = displayDevice;
}, },
this); this);
} }