Commit 061bbce3 authored by Philip Müller's avatar Philip Müller

Refactor mwhd

- Update .gitignore
- Delete useless files
- Move methods
- Rename variables
- Add const modifier in printer
- Enable C++11
- Use clang as compiler
parent 4ba524aa
*~
build
CMakeLists.txt.user
libmhwd/cmake_install.cmake
libmhwd/libmhwd.so
src/cmake_install.cmake
src/mhwd
cmake_install.cmake
CMakeCache.txt
CMakeFiles
Makefile
*.o
*settings
*project
......@@ -2,9 +2,11 @@ cmake_minimum_required (VERSION 2.6)
project(mhwd)
SET(CMAKE_INSTALL_PREFIX "/usr/")
SET(CMAKE_C_COMPILER clang)
SET(CMAKE_CXX_COMPILER clang++)
add_definitions(-Wall)
add_definitions(-std=c++11 -O0 -g -Wall -Wextra -Wunreachable-code -Werror)
add_subdirectory(libmhwd)
add_subdirectory(mhwd)
add_subdirectory(src)
add_subdirectory(scripts)
......@@ -5,14 +5,11 @@ include_directories(. vita)
###
set( LIB_HEADERS
mhwd.h
mhwd_p.h
const.h
vita/string.hpp
)
set( LIB_SOURCES
mhwd.cpp
vita/string.cpp
)
......
......@@ -29,3 +29,5 @@
#define MHWD_PM_CACHE_DIR "/var/cache/pacman/pkg"
#define MHWD_PM_CONFIG "/etc/pacman.conf"
#define MHWD_PM_ROOT "/"
#define ROOT_UID 0
This diff is collapsed.
/*
* mhwd - Manjaro Hardware Detection
* Roland Singer <roland@manjaro.org>
*
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MHWD_H
#define MHWD_H
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
namespace mhwd {
//#############//
//### Enums ###//
//#############//
enum TYPE { TYPE_PCI, TYPE_USB };
enum STATUS { STATUS_SUCCESS, STATUS_ERROR_CONFLICTS, STATUS_ERROR_REQUIREMENTS, STATUS_ERROR_NOT_INSTALLED, STATUS_ERROR_ALREADY_INSTALLED, STATUS_ERROR_NO_MATCH_LOCAL_CONFIG, STATUS_ERROR_SCRIPT_FAILED, STATUS_ERROR_SET_DATABASE };
enum MESSAGETYPE { MESSAGETYPE_CONSOLE_OUTPUT, MESSAGETYPE_INSTALLDEPENDENCY_START, MESSAGETYPE_INSTALLDEPENDENCY_END, MESSAGETYPE_INSTALL_START, MESSAGETYPE_INSTALL_END, MESSAGETYPE_REMOVE_START, MESSAGETYPE_REMOVE_END };
//####################//
//### Data Structs ###//
//####################//
struct Config {
struct HardwareIDs {
std::vector<std::string> classIDs, vendorIDs, deviceIDs;
std::vector<std::string> blacklistedClassIDs, blacklistedVendorIDs, blacklistedDeviceIDs;
};
TYPE type;
std::string basePath, configPath, name, info, version;
bool freedriver;
int priority;
std::vector<HardwareIDs> hwdIDs;
std::vector<std::string> conflicts, dependencies;
};
struct Device {
TYPE type;
std::string className, deviceName, vendorName, classID, deviceID, vendorID, sysfsBusID, sysfsID;
std::vector<mhwd::Config*> availableConfigs, installedConfigs;
};
struct Data {
struct Environment {
std::string PMCachePath, PMConfigPath, PMRootPath;
bool syncPackageManagerDatabase;
void (*messageFunc)(mhwd::MESSAGETYPE, std::string);
};
std::vector<mhwd::Device*> USBDevices, PCIDevices;
std::vector<mhwd::Config*> installedUSBConfigs, installedPCIConfigs, allUSBConfigs, allPCIConfigs, invalidConfigs;
Environment environment;
};
struct Transaction {
enum TYPE { TYPE_INSTALL, TYPE_REMOVE };
TYPE type;
bool allowReinstallation;
mhwd::Config* config;
std::vector<mhwd::Config*> dependencyConfigs, conflictedConfigs, requiredByConfigs;
};
//#################//
//### Functions ###//
//#################//
void initData(mhwd::Data *data);
void fillData(mhwd::Data *data);
void freeData(mhwd::Data *data);
std::string checkEnvironment();
void updateConfigData(mhwd::Data *data);
void updateInstalledConfigData(mhwd::Data *data);
bool fillConfig(mhwd::Config *config, std::string configPath, mhwd::TYPE type);
void printDeviceDetails(mhwd::TYPE type, FILE *f = stdout);
mhwd::Config* getInstalledConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
mhwd::Config* getDatabaseConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
mhwd::Config* getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
std::vector<mhwd::Config*> getAllDependenciesToInstall(mhwd::Data *data, mhwd::Config *config);
std::vector<mhwd::Config*> getAllLocalConflicts(mhwd::Data *data, mhwd::Config *config);
std::vector<mhwd::Config*> getAllLocalRequirements(mhwd::Data *data, mhwd::Config *config);
mhwd::Transaction createTransaction(mhwd::Data *data, mhwd::Config* config, mhwd::Transaction::TYPE type, bool allowReinstallation = false);
mhwd::STATUS performTransaction(mhwd::Data *data, mhwd::Transaction *transaction);
}
#else
namespace mhwd {
extern void initData(mhwd::Data *data);
extern void fillData(mhwd::Data *data);
extern void freeData(mhwd::Data *data);
extern std::string checkEnvironment();
extern void updateConfigData(mhwd::Data *data);
extern void updateInstalledConfigData(mhwd::Data *data);
extern void printDeviceDetails(mhwd::TYPE type, FILE *f = stdout);
extern mhwd::Config* getInstalledConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
extern mhwd::Config* getDatabaseConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
extern mhwd::Config* getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
extern std::vector<mhwd::Config*> getAllDependenciesToInstall(mhwd::Data *data, mhwd::Config *config);
extern std::vector<mhwd::Config*> getAllLocalConflicts(mhwd::Data *data, mhwd::Config *config);
extern std::vector<mhwd::Config*> getAllLocalRequirements(mhwd::Data *data, mhwd::Config *config);
extern mhwd::Transaction createTransaction(mhwd::Data *data, mhwd::Config* config, mhwd::Transaction::TYPE type, bool allowReinstallation = false);
extern mhwd::STATUS performTransaction(mhwd::Data *data, mhwd::Transaction *transaction);
}
#endif // MHWD_H
......@@ -67,7 +67,7 @@ namespace Vita {
result += replace;
previous = current + search.length();
current = this->find(search, previous);
limit--;
--limit;
}
result += this->substr(previous);
return result;
......
......@@ -11,7 +11,7 @@
#ifndef INC_VITA_STRING_HPP
#define INC_VITA_STRING_HPP
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
......
This diff is collapsed.
......@@ -5,10 +5,23 @@ include_directories(. ../libmhwd ../libmhwd/vita)
###
set( HEADERS
Config.hpp
Data.hpp
Device.hpp
Enums.hpp
Mhwd.hpp
Printer.hpp
Transaction.hpp
)
set( SOURCES
mhwd.cpp
Config.cpp
Data.cpp
Device.cpp
main.cpp
Mhwd.cpp
Printer.cpp
Transaction.cpp
)
set( LIBS mhwd)
......
/*
* Config.cpp
*
* Created on: 28 sie 2014
* Author: dec
*/
#include "Config.hpp"
#include <fstream>
#include <string>
Config::Config(std::string configPath, MHWD::DEVICETYPE type)
: type_(type), basePath_(configPath.substr(0, configPath.find_last_of('/'))),
configPath_(configPath)
{
// Add new HardwareIDs group to vector if vector is empty
if (hwdIDs_.empty())
{
Config::HardwareIDs hwdID;
hwdIDs_.push_back(hwdID);
}
}
bool Config::readConfigFile(std::string configPath)
{
std::ifstream file(configPath.c_str(), std::ios::in);
if (!file.is_open())
{
return false;
}
Vita::string line;
Vita::string key;
Vita::string value;
std::vector<Vita::string> parts;
while (!file.eof())
{
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != std::string::npos)
{
line.erase(pos);
}
if (line.trim().empty())
{
continue;
}
parts = line.explode("=");
key = parts.front().trim().toLower();
value = parts.back().trim("\"").trim();
// Read in extern file
if (value.size() > 1 && value.substr(0, 1) == ">")
{
std::ifstream file(getRightConfigPath(value.substr(1), basePath_).c_str(),
std::ios::in);
if (!file.is_open())
{
return false;
}
Vita::string line;
value.clear();
while (!file.eof())
{
getline(file, line);
size_t pos = line.find_first_of('#');
if (pos != std::string::npos)
{
line.erase(pos);
}
if (line.trim().empty())
{
continue;
}
value += " " + line.trim();
}
value = value.trim();
// remove all multiple spaces
while (value.find(" ") != std::string::npos)
{
value = value.replace(" ", " ");
}
}
if (key == "include")
{
readConfigFile(getRightConfigPath(value, basePath_));
}
else if (key == "name")
{
name_ = value.toLower();
}
else if (key == "version")
{
version_ = value;
}
else if (key == "info")
{
info_ = value;
}
else if (key == "priority")
{
priority_ = value.convert<int>();
}
else if (key == "freedriver")
{
value = value.toLower();
if (value == "false")
{
freedriver_ = false;
}
else if (value == "true")
{
freedriver_ = true;
}
}
else if (key == "classids")
{
// Add new HardwareIDs group to vector if vector is not empty
if (!hwdIDs_.back().classIDs.empty())
{
Config::HardwareIDs hwdID;
hwdIDs_.push_back(hwdID);
}
hwdIDs_.back().classIDs = splitValue(value);
}
else if (key == "vendorids")
{
// Add new HardwareIDs group to vector if vector is not empty
if (!hwdIDs_.back().vendorIDs.empty())
{
Config::HardwareIDs hwdID;
hwdIDs_.push_back(hwdID);
}
hwdIDs_.back().vendorIDs = splitValue(value);
}
else if (key == "deviceids")
{
// Add new HardwareIDs group to vector if vector is not empty
if (!hwdIDs_.back().deviceIDs.empty())
{
Config::HardwareIDs hwdID;
hwdIDs_.push_back(hwdID);
}
hwdIDs_.back().deviceIDs = splitValue(value);
}
else if (key == "blacklistedclassids")
{
hwdIDs_.back().blacklistedClassIDs = splitValue(value);
}
else if (key == "blacklistedvendorids")
{
hwdIDs_.back().blacklistedVendorIDs = splitValue(value);
}
else if (key == "blacklisteddeviceids")
{
hwdIDs_.back().blacklistedDeviceIDs = splitValue(value);
}
else if (key == "mhwddepends")
{
dependencies_ = splitValue(value);
}
else if (key == "mhwdconflicts")
{
conflicts_ = splitValue(value);
}
}
// Append * to all empty vectors
for (std::vector<Config::HardwareIDs>::iterator hwdID = hwdIDs_.begin();
hwdID != hwdIDs_.end(); hwdID++)
{
if ((*hwdID).classIDs.empty())
{
(*hwdID).classIDs.push_back("*");
}
if ((*hwdID).vendorIDs.empty())
{
(*hwdID).vendorIDs.push_back("*");
}
if ((*hwdID).deviceIDs.empty())
{
(*hwdID).deviceIDs.push_back("*");
}
}
if (name_.empty())
{
return false;
}
return true;
}
std::vector<std::string> Config::splitValue(Vita::string str, Vita::string onlyEnding)
{
std::vector<Vita::string> work = str.toLower().explode(" ");
std::vector<std::string> final;
for (std::vector<Vita::string>::const_iterator iterator = work.begin(); iterator != work.end();
iterator++)
{
if (*iterator != "" && onlyEnding.empty())
final.push_back(*iterator);
else if (*iterator != "" && Vita::string(*iterator).explode(".").back() == onlyEnding
&& (*iterator).size() > 5)
final.push_back(Vita::string(*iterator).substr(0, (*iterator).size() - 5));
}
return final;
}
Vita::string Config::getRightConfigPath(Vita::string str, Vita::string baseConfigPath)
{
str = str.trim();
if (str.size() <= 0 || str.substr(0, 1) == "/")
return str;
return baseConfigPath + "/" + str;
}
/*
* Config.hpp
*
* Created on: 28 sie 2014
* Author: dec
*/
#ifndef CONFIG_HPP_
#define CONFIG_HPP_
#include "Enums.hpp"
#include "../libmhwd/vita/string.hpp"
#include <string>
#include <vector>
struct Config
{
Config(std::string configPath, MHWD::DEVICETYPE type);
bool readConfigFile(std::string configPath);
struct HardwareIDs
{
std::vector<std::string> classIDs;
std::vector<std::string> vendorIDs;
std::vector<std::string> deviceIDs;
std::vector<std::string> blacklistedClassIDs;
std::vector<std::string> blacklistedVendorIDs;
std::vector<std::string> blacklistedDeviceIDs;
};
MHWD::DEVICETYPE type_;
std::string basePath_;
std::string configPath_;
std::string name_;
std::string info_;
std::string version_;
bool freedriver_ = true;
int priority_ = 0;
std::vector<HardwareIDs> hwdIDs_;
std::vector<std::string> conflicts_;
std::vector<std::string> dependencies_;
private:
std::vector<std::string> splitValue(Vita::string str, Vita::string onlyEnding = "");
Vita::string getRightConfigPath(Vita::string str, Vita::string baseConfigPath);
};
#endif /* CONFIG_HPP_ */
This diff is collapsed.
/*
* Data.hpp
*
* Created on: 28 sie 2014
* Author: dec
*/
#ifndef DATA_HPP_
#define DATA_HPP_
#include "../libmhwd/const.h"
#include "../libmhwd/vita/string.hpp"
#include "Config.hpp"
#include "Device.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <hd.h>
class Data {
public:
Data();
~Data();
struct Environment {
std::string PMCachePath {MHWD_PM_CACHE_DIR};
std::string PMConfigPath {MHWD_PM_CONFIG};
std::string PMRootPath {MHWD_PM_ROOT};
bool syncPackageManagerDatabase = true;
};
Environment environment;
std::vector<Device*> USBDevices;
std::vector<Device*> PCIDevices;
std::vector<Config*> installedUSBConfigs;
std::vector<Config*> installedPCIConfigs;
std::vector<Config*> allUSBConfigs;
std::vector<Config*> allPCIConfigs;
std::vector<Config*> invalidConfigs;
void updateInstalledConfigData();
void getAllDevicesOfConfig(Config *config, std::vector<Device*>* foundDevices);
bool fillConfig(Config *config, std::string configPath, MHWD::DEVICETYPE type);
std::vector<Config*> getAllDependenciesToInstall(Config *config);
void getAllDependenciesToInstall(Config *config, std::vector<Config*>* installedConfigs,
std::vector<Config*> *depends);
Config* getDatabaseConfig(const std::string configName, const MHWD::DEVICETYPE configType);
std::vector<Config*> getAllLocalConflicts(Config *config);
std::vector<Config*> getAllLocalRequirements(Config *config);
private:
void getAllDevicesOfConfig(std::vector<Device*>* devices, Config *config, std::vector<Device*>* foundDevices);
void fillInstalledConfigs(MHWD::DEVICETYPE type);
void fillDevices(MHWD::DEVICETYPE type);
void fillAllConfigs(MHWD::DEVICETYPE type);
void setMatchingConfigs(std::vector<Device*>* devices, std::vector<Config*>* configs, bool setAsInstalled);
void setMatchingConfig(Config* config, std::vector<Device*>* devices, bool setAsInstalled);
void addConfigSorted(std::vector<Config*>* configs, Config* config);
std::vector<std::string> getRecursiveDirectoryFileList(const std::string directoryPath,
std::string onlyFilename = "");
bool readConfigFile(Config *config, std::string configPath);
Vita::string getRightConfigPath(Vita::string str, Vita::string baseConfigPath);
std::vector<std::string> splitValue(Vita::string str, Vita::string onlyEnding = "");
void updateConfigData();
Vita::string from_Hex(uint16_t hexnum, int fill);
Vita::string from_CharArray(char* c);
};
#endif /* DATA_HPP_ */
/*
* Device.cpp
*
* Created on: 28 sie 2014
* Author: dec
*/
#include "Device.hpp"
/*
* Device.hpp
*