1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*---------------------------------------------------------*\
| ProfileManager.cpp |
| |
| OpenRGB profile manager |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-or-later |
\*---------------------------------------------------------*/
#include <fstream>
#include <iostream>
#include <cstring>
#include "ProfileManager.h"
#include "ResourceManager.h"
#include "RGBController_Dummy.h"
#include "LogManager.h"
#include "NetworkProtocol.h"
#include "filesystem.h"
#include "StringUtils.h"
#define OPENRGB_PROFILE_HEADER "OPENRGB_PROFILE"
#define OPENRGB_PROFILE_VERSION OPENRGB_SDK_PROTOCOL_VERSION
ProfileManager::ProfileManager(const filesystem::path& config_dir)
{
configuration_directory = config_dir;
UpdateProfileList();
}
ProfileManager::~ProfileManager()
{
}
bool ProfileManager::SaveProfile(std::string profile_name, bool sizes)
{
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
/*---------------------------------------------------------*\
| Get the list of controllers from the resource manager |
\*---------------------------------------------------------*/
std::vector<RGBController *> controllers = ResourceManager::get()->GetRGBControllers();
/*---------------------------------------------------------*\
| If a name was entered, save the profile file |
\*---------------------------------------------------------*/
if(profile_name != "")
{
/*---------------------------------------------------------*\
| Extension .orp - OpenRgb Profile |
\*---------------------------------------------------------*/
std::string filename = profile_name;
/*---------------------------------------------------------*\
| Determine file extension |
\*---------------------------------------------------------*/
if(sizes)
{
filename += ".ors";
}
else
{
filename += ".orp";
}
/*---------------------------------------------------------*\
| Open an output file in binary mode |
\*---------------------------------------------------------*/
filesystem::path profile_path = configuration_directory / filesystem::u8path(filename);
std::ofstream controller_file(profile_path, std::ios::out | std::ios::binary | std::ios::trunc);
/*---------------------------------------------------------*\
| Write header |
| 16 bytes - "OPENRGB_PROFILE" |
| 4 bytes - Version, unsigned int |
\*---------------------------------------------------------*/
unsigned int profile_version = OPENRGB_PROFILE_VERSION;
controller_file.write(OPENRGB_PROFILE_HEADER, 16);
controller_file.write((char *)&profile_version, sizeof(unsigned int));
/*---------------------------------------------------------*\
| Write controller data for each controller |
\*---------------------------------------------------------*/
for(std::size_t controller_index = 0; controller_index < controllers.size(); controller_index++)
{
/*-----------------------------------------------------*\
| Ignore remote and virtual controllers when saving |
| sizes |
\*-----------------------------------------------------*/
if(sizes && (controllers[controller_index]->flags & CONTROLLER_FLAG_REMOTE
|| controllers[controller_index]->flags & CONTROLLER_FLAG_VIRTUAL))
{
break;
}
unsigned char *controller_data = controllers[controller_index]->GetDeviceDescription(profile_version);
unsigned int controller_size;
memcpy(&controller_size, controller_data, sizeof(controller_size));
controller_file.write((const char *)controller_data, controller_size);
delete[] controller_data;
}
/*---------------------------------------------------------*\
| Close the file when done |
\*---------------------------------------------------------*/
controller_file.close();
/*---------------------------------------------------------*\
| Update the profile list |
\*---------------------------------------------------------*/
UpdateProfileList();
return(true);
}
else
{
return(false);
}
}
void ProfileManager::SetConfigurationDirectory(const filesystem::path& directory)
{
configuration_directory = directory;
UpdateProfileList();
}
bool ProfileManager::LoadProfile(std::string profile_name)
{
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
return(LoadProfileWithOptions(profile_name, false, true));
}
bool ProfileManager::LoadSizeFromProfile(std::string profile_name)
{
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
return(LoadProfileWithOptions(profile_name, true, false));
}
std::vector<RGBController*> ProfileManager::LoadProfileToList
(
std::string profile_name,
bool sizes
)
{
std::vector<RGBController*> temp_controllers;
unsigned int controller_size;
unsigned int controller_offset = 0;
filesystem::path filename = configuration_directory / filesystem::u8path(profile_name);
/*---------------------------------------------------------*\
| Determine file extension |
\*---------------------------------------------------------*/
if(sizes)
{
filename.concat(".ors");
}
else
{
if(filename.extension() != ".orp")
{
filename.concat(".orp");
}
}
/*---------------------------------------------------------*\
| Open input file in binary mode |
\*---------------------------------------------------------*/
std::ifstream controller_file(filename, std::ios::in | std::ios::binary);
/*---------------------------------------------------------*\
| Read and verify file header |
\*---------------------------------------------------------*/
char profile_string[16] = "";
unsigned int profile_version = 0;
controller_file.read(profile_string, 16);
controller_file.read((char *)&profile_version, sizeof(unsigned int));
/*---------------------------------------------------------*\
| Profile version started at 1 and protocol version started |
| at 0. Version 1 profiles should use protocol 0, but 2 or |
| greater should be synchronized |
\*---------------------------------------------------------*/
if(profile_version == 1)
{
profile_version = 0;
}
controller_offset += 16 + sizeof(unsigned int);
controller_file.seekg(controller_offset);
if(strcmp(profile_string, OPENRGB_PROFILE_HEADER) == 0)
{
if(profile_version <= OPENRGB_PROFILE_VERSION)
{
/*---------------------------------------------------------*\
| Read controller data from file until EOF |
\*---------------------------------------------------------*/
while(!(controller_file.peek() == EOF))
{
controller_file.read((char *)&controller_size, sizeof(controller_size));
unsigned char *controller_data = new unsigned char[controller_size];
controller_file.seekg(controller_offset);
controller_file.read((char *)controller_data, controller_size);
RGBController_Dummy *temp_controller = new RGBController_Dummy();
temp_controller->ReadDeviceDescription(controller_data, profile_version);
temp_controllers.push_back(temp_controller);
delete[] controller_data;
controller_offset += controller_size;
controller_file.seekg(controller_offset);
}
}
}
return(temp_controllers);
}
bool ProfileManager::LoadDeviceFromListWithOptions
(
std::vector<RGBController*>& temp_controllers,
std::vector<bool>& temp_controller_used,
RGBController* load_controller,
bool load_size,
bool load_settings
)
{
for(std::size_t temp_index = 0; temp_index < temp_controllers.size(); temp_index++)
{
RGBController *temp_controller = temp_controllers[temp_index];
/*---------------------------------------------------------*\
| Do not compare location string for HID devices, as the |
| location string may change between runs as devices are |
| connected and disconnected. Also do not compare the I2C |
| bus number, since it is not persistent across reboots |
| on Linux - strip the I2C number and compare only address. |
\*---------------------------------------------------------*/
bool location_check;
if(load_controller->GetLocation().find("HID: ") == 0)
{
location_check = true;
}
else if(load_controller->GetLocation().find("I2C: ") == 0)
{
std::size_t loc = load_controller->GetLocation().rfind(", ");
if(loc == std::string::npos)
{
location_check = false;
}
else
{
std::string i2c_address = load_controller->GetLocation().substr(loc + 2);
location_check = temp_controller->GetLocation().find(i2c_address) != std::string::npos;
}
}
else
{
location_check = temp_controller->GetLocation() == load_controller->GetLocation();
}
/*---------------------------------------------------------*\
| Test if saved controller data matches this controller |
\*---------------------------------------------------------*/
if((temp_controller_used[temp_index] == false )
&&(temp_controller->type == load_controller->type )
&&(temp_controller->GetName() == load_controller->GetName() )
&&(temp_controller->GetDescription() == load_controller->GetDescription())
&&(temp_controller->GetVersion() == load_controller->GetVersion() )
&&(temp_controller->GetSerial() == load_controller->GetSerial() )
&&(location_check == true ))
{
/*---------------------------------------------------------*\
| Set used flag for this temp device |
\*---------------------------------------------------------*/
temp_controller_used[temp_index] = true;
/*---------------------------------------------------------*\
| Update zone sizes if requested |
\*---------------------------------------------------------*/
if(load_size)
{
if(temp_controller->zones.size() == load_controller->zones.size())
{
for(std::size_t zone_idx = 0; zone_idx < temp_controller->zones.size(); zone_idx++)
{
if((temp_controller->zones[zone_idx].name == load_controller->zones[zone_idx].name )
&&(temp_controller->zones[zone_idx].type == load_controller->zones[zone_idx].type )
&&(temp_controller->zones[zone_idx].leds_min == load_controller->zones[zone_idx].leds_min )
&&(temp_controller->zones[zone_idx].leds_max == load_controller->zones[zone_idx].leds_max ))
{
if(temp_controller->zones[zone_idx].leds_count != load_controller->zones[zone_idx].leds_count)
{
load_controller->ResizeZone((int)zone_idx, temp_controller->zones[zone_idx].leds_count);
}
if(temp_controller->zones[zone_idx].segments.size() != load_controller->zones[zone_idx].segments.size())
{
load_controller->zones[zone_idx].segments.clear();
for(std::size_t segment_idx = 0; segment_idx < temp_controller->zones[zone_idx].segments.size(); segment_idx++)
{
load_controller->zones[zone_idx].segments.push_back(temp_controller->zones[zone_idx].segments[segment_idx]);
}
}
}
}
}
}
/*---------------------------------------------------------*\
| Update settings if requested |
\*---------------------------------------------------------*/
if(load_settings)
{
/*---------------------------------------------------------*\
| Update all modes |
\*---------------------------------------------------------*/
if(temp_controller->modes.size() == load_controller->modes.size())
{
for(std::size_t mode_index = 0; mode_index < temp_controller->modes.size(); mode_index++)
{
if((temp_controller->modes[mode_index].name == load_controller->modes[mode_index].name )
&&(temp_controller->modes[mode_index].value == load_controller->modes[mode_index].value )
&&(temp_controller->modes[mode_index].flags == load_controller->modes[mode_index].flags )
&&(temp_controller->modes[mode_index].speed_min == load_controller->modes[mode_index].speed_min )
&&(temp_controller->modes[mode_index].speed_max == load_controller->modes[mode_index].speed_max )
//&&(temp_controller->modes[mode_index].brightness_min == load_controller->modes[mode_index].brightness_min)
//&&(temp_controller->modes[mode_index].brightness_max == load_controller->modes[mode_index].brightness_max)
&&(temp_controller->modes[mode_index].colors_min == load_controller->modes[mode_index].colors_min )
&&(temp_controller->modes[mode_index].colors_max == load_controller->modes[mode_index].colors_max ))
{
load_controller->modes[mode_index].speed = temp_controller->modes[mode_index].speed;
load_controller->modes[mode_index].brightness = temp_controller->modes[mode_index].brightness;
load_controller->modes[mode_index].direction = temp_controller->modes[mode_index].direction;
load_controller->modes[mode_index].color_mode = temp_controller->modes[mode_index].color_mode;
load_controller->modes[mode_index].colors.resize(temp_controller->modes[mode_index].colors.size());
for(std::size_t mode_color_index = 0; mode_color_index < temp_controller->modes[mode_index].colors.size(); mode_color_index++)
{
load_controller->modes[mode_index].colors[mode_color_index] = temp_controller->modes[mode_index].colors[mode_color_index];
}
}
}
load_controller->active_mode = temp_controller->active_mode;
}
/*---------------------------------------------------------*\
| Update all colors |
\*---------------------------------------------------------*/
if(temp_controller->colors.size() == load_controller->colors.size())
{
for(std::size_t color_index = 0; color_index < temp_controller->colors.size(); color_index++)
{
load_controller->colors[color_index] = temp_controller->colors[color_index];
}
}
}
return(true);
}
}
return(false);
}
bool ProfileManager::LoadProfileWithOptions
(
std::string profile_name,
bool load_size,
bool load_settings
)
{
std::vector<RGBController*> temp_controllers;
std::vector<bool> temp_controller_used;
bool ret_val = false;
/*---------------------------------------------------------*\
| Get the list of controllers from the resource manager |
\*---------------------------------------------------------*/
std::vector<RGBController *> controllers = ResourceManager::get()->GetRGBControllers();
/*---------------------------------------------------------*\
| Open input file in binary mode |
\*---------------------------------------------------------*/
temp_controllers = LoadProfileToList(profile_name);
/*---------------------------------------------------------*\
| Set up used flag vector |
\*---------------------------------------------------------*/
temp_controller_used.resize(temp_controllers.size());
for(unsigned int controller_idx = 0; controller_idx < temp_controller_used.size(); controller_idx++)
{
temp_controller_used[controller_idx] = false;
}
/*---------------------------------------------------------*\
| Loop through all controllers. For each controller, search|
| all saved controllers until a match is found |
\*---------------------------------------------------------*/
for(std::size_t controller_index = 0; controller_index < controllers.size(); controller_index++)
{
bool temp_ret_val = LoadDeviceFromListWithOptions(temp_controllers, temp_controller_used, controllers[controller_index], load_size, load_settings);
std::string current_name = controllers[controller_index]->GetName() + " @ " + controllers[controller_index]->GetLocation();
LOG_INFO("[ProfileManager] Profile loading: %s for %s", ( temp_ret_val ? "Succeeded" : "FAILED!" ), current_name.c_str());
ret_val |= temp_ret_val;
}
/*---------------------------------------------------------*\
| Delete all temporary controllers |
\*---------------------------------------------------------*/
for(unsigned int controller_idx = 0; controller_idx < temp_controllers.size(); controller_idx++)
{
delete temp_controllers[controller_idx];
}
return(ret_val);
}
void ProfileManager::DeleteProfile(std::string profile_name)
{
profile_name = StringUtils::remove_null_terminating_chars(profile_name);
filesystem::path filename = configuration_directory / profile_name;
filename.concat(".orp");
filesystem::remove(filename);
UpdateProfileList();
}
void ProfileManager::UpdateProfileList()
{
profile_list.clear();
/*---------------------------------------------------------*\
| Load profiles by looking for .orp files in current dir |
\*---------------------------------------------------------*/
for(const auto & entry : filesystem::directory_iterator(configuration_directory))
{
std::string filename = entry.path().filename().string();
if(filename.find(".orp") != std::string::npos)
{
LOG_INFO("[ProfileManager] Found file: %s attempting to validate header", filename.c_str());
/*---------------------------------------------------------*\
| Open input file in binary mode |
\*---------------------------------------------------------*/
filesystem::path file_path = configuration_directory;
file_path.append(filename);
std::ifstream profile_file(file_path, std::ios::in | std::ios::binary);
/*---------------------------------------------------------*\
| Read and verify file header |
\*---------------------------------------------------------*/
char profile_string[16];
unsigned int profile_version;
profile_file.read(profile_string, 16);
profile_file.read((char *)&profile_version, sizeof(unsigned int));
if(strcmp(profile_string, OPENRGB_PROFILE_HEADER) == 0)
{
if(profile_version <= OPENRGB_PROFILE_VERSION)
{
/*---------------------------------------------------------*\
| Add this profile to the list |
\*---------------------------------------------------------*/
filename.erase(filename.length() - 4);
profile_list.push_back(filename);
LOG_INFO("[ProfileManager] Valid v%i profile found for %s", profile_version, filename.c_str());
}
else
{
LOG_WARNING("[ProfileManager] Profile %s isn't valid for current version (v%i, expected v%i at most)", filename.c_str(), profile_version, OPENRGB_PROFILE_VERSION);
}
}
else
{
LOG_WARNING("[ProfileManager] Profile %s isn't valid: header is missing", filename.c_str());
}
profile_file.close();
}
}
}
unsigned char * ProfileManager::GetProfileListDescription()
{
unsigned int data_ptr = 0;
unsigned int data_size = 0;
/*---------------------------------------------------------*\
| Calculate data size |
\*---------------------------------------------------------*/
unsigned short num_profiles = (unsigned short)profile_list.size();
data_size += sizeof(data_size);
data_size += sizeof(num_profiles);
for(unsigned int i = 0; i < num_profiles; i++)
{
data_size += sizeof (unsigned short);
data_size += (unsigned int)strlen(profile_list[i].c_str()) + 1;
}
/*---------------------------------------------------------*\
| Create data buffer |
\*---------------------------------------------------------*/
unsigned char *data_buf = new unsigned char[data_size];
/*---------------------------------------------------------*\
| Copy in data size |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &data_size, sizeof(data_size));
data_ptr += sizeof(data_size);
/*---------------------------------------------------------*\
| Copy in num_profiles |
\*---------------------------------------------------------*/
memcpy(&data_buf[data_ptr], &num_profiles, sizeof(num_profiles));
data_ptr += sizeof(num_profiles);
/*---------------------------------------------------------*\
| Copy in profile names (size+data) |
\*---------------------------------------------------------*/
for(unsigned int i = 0; i < num_profiles; i++)
{
unsigned short name_len = (unsigned short)strlen(profile_list[i].c_str()) + 1;
memcpy(&data_buf[data_ptr], &name_len, sizeof(name_len));
data_ptr += sizeof(name_len);
strcpy((char *)&data_buf[data_ptr], profile_list[i].c_str());
data_ptr += name_len;
}
return(data_buf);
}
|