Commit b2263c4e authored by Roland Singer's avatar Roland Singer

improved libmhwd and created mhwd console application

parent 26907d02
......@@ -4,3 +4,4 @@ project(mhwd)
add_definitions(-Wall)
add_subdirectory(libmhwd)
add_subdirectory(mhwd)
Implement config dependency installing and conflict checking
include_directories(. vita)
set( HEADERS
###
### mhwd library
###
set( LIB_HEADERS
mhwd.h
mhwd_p.h
const.h
vita/string.hpp
)
set( SOURCES
set( LIB_SOURCES
mhwd.cpp
vita/string.cpp
)
set( LIBS hd)
add_library (mhwd SHARED ${SOURCES} ${HEADERS})
target_link_libraries(mhwd ${LIBS})
set( LIB_LIBS hd)
add_subdirectory(test)
add_library (mhwd SHARED ${LIB_SOURCES} ${LIB_HEADERS})
target_link_libraries(mhwd ${LIB_LIBS})
/*
* mhwd - Manjaro Hardware Detection
* Roland Singer <roland@manjaro.org>
*
* Copyright (C) 2007 Free Software Foundation, Inc.
*
......@@ -20,8 +21,8 @@
#define MHWD_CONFIG_NAME "MHWDCONFIG"
#define MHWD_USB_CONFIG_DIR "/var/lib/mhwd/usb"
#define MHWD_PCI_CONFIG_DIR "./db" //"/var/lib/mhwd/pci"
#define MHWD_PCI_CONFIG_DIR "/var/lib/mhwd/pci"
#define MHWD_USB_DATABASE_DIR "/var/lib/mhwd/local/usb"
#define MHWD_PCI_DATABASE_DIR "./local" //"/var/lib/mhwd/local/pci"
#define MHWD_PCI_DATABASE_DIR "/var/lib/mhwd/local/pci"
#define MHWD_CACHE_DIR "/var/cache/pacman/pkg"
#define MHWD_SCRIPT_PATH "/var/lib/mhwd/script/mhwd"
/*
* mhwd - Manjaro Hardware Detection
* Roland Singer <roland@manjaro.org>
*
* Copyright (C) 2007 Free Software Foundation, Inc.
*
......@@ -27,19 +28,21 @@
//########################//
void mhwd::fillData(mhwd::Data *data) {
cleanupData(data);
setInstalledConfigs(data, mhwd::TYPE_PCI);
setInstalledConfigs(data, mhwd::TYPE_USB);
void mhwd::initData(mhwd::Data *data) {
data->PCIDevices.clear();
data->USBDevices.clear();
data->installedPCIConfigs.clear();
data->installedUSBConfigs.clear();
data->invalidConfigs.clear();
data->lastError.clear();
setDevices(data, mhwd::TYPE_PCI);
setDevices(data, mhwd::TYPE_USB);
data->environment.cachePath = MHWD_CACHE_DIR;
data->environment.messageFunc = NULL;
}
void mhwd::cleanupData(mhwd::Data *data) {
void mhwd::fillData(mhwd::Data *data) {
data->PCIDevices.clear();
data->USBDevices.clear();
data->installedPCIConfigs.clear();
......@@ -47,10 +50,12 @@ void mhwd::cleanupData(mhwd::Data *data) {
data->invalidConfigs.clear();
data->lastError.clear();
data->environment.cachePath = MHWD_CACHE_DIR;
data->environment.messageFunc = NULL;
}
setInstalledConfigs(data, mhwd::TYPE_PCI);
setInstalledConfigs(data, mhwd::TYPE_USB);
setDevices(data, mhwd::TYPE_PCI);
setDevices(data, mhwd::TYPE_USB);
}
void mhwd::printDeviceDetails(mhwd::TYPE type, FILE *f) {
......@@ -88,7 +93,7 @@ bool mhwd::installConfig(mhwd::Data *data, mhwd::Config *config) {
// Check if already installed
if (getInstalledConfig(data, config->name, config->type) != NULL) {
data->lastError = "a config is already installed";
data->lastError = "a version of config " + config->name + " is already installed";
return false;
}
......@@ -174,7 +179,6 @@ mhwd::Config* mhwd::getInstalledConfig(mhwd::Data *data, const std::string confi
else
installedConfigs = &data->installedPCIConfigs;
// Check if already installed
for (std::vector<mhwd::Config>::iterator iterator = installedConfigs->begin(); iterator != installedConfigs->end(); iterator++) {
if (configName == (*iterator).name)
return &(*iterator);
......@@ -185,6 +189,31 @@ mhwd::Config* mhwd::getInstalledConfig(mhwd::Data *data, const std::string confi
mhwd::Config* mhwd::getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType) {
std::vector<mhwd::Device> *devices;
// Get the right devices
if (configType == mhwd::TYPE_USB)
devices = &data->USBDevices;
else
devices = &data->PCIDevices;
for (std::vector<mhwd::Device>::iterator dev_iter = devices->begin(); dev_iter != devices->end(); dev_iter++) {
if ((*dev_iter).availableConfigs.empty())
continue;
for (std::vector<mhwd::Config>::iterator iterator = (*dev_iter).availableConfigs.begin(); iterator != (*dev_iter).availableConfigs.end(); iterator++) {
if (configName == (*iterator).name)
return &(*iterator);
}
}
return NULL;
}
//#########################//
......
/*
* mhwd - Manjaro Hardware Detection
* Roland Singer <roland@manjaro.org>
*
* Copyright (C) 2007 Free Software Foundation, Inc.
*
......@@ -71,28 +72,30 @@ namespace mhwd {
//#################//
//### Functions ###//
//#################//
void initData(mhwd::Data *data);
void fillData(mhwd::Data *data);
void cleanupData(mhwd::Data *data);
void printDeviceDetails(mhwd::TYPE type, FILE *f = stdout);
bool installConfig(mhwd::Data *data, mhwd::Config *config);
bool uninstallConfig(mhwd::Data *data, mhwd::Config *config);
mhwd::Config* getInstalledConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
mhwd::Config* getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
}
#else
namespace mhwd {
extern void initData(mhwd::Data *data);
extern void fillData(mhwd::Data *data);
extern void cleanupData(mhwd::Data *data);
extern void printDeviceDetails(mhwd::TYPE type, FILE *f = stdout);
extern bool installConfig(mhwd::Data *data, mhwd::Config *config);
extern bool uninstallConfig(mhwd::Data *data, mhwd::Config *config);
extern mhwd::Config* getInstalledConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
extern mhwd::Config* getAvailableConfig(mhwd::Data *data, const std::string configName, const TYPE configType);
}
......
/*
* mhwd - Manjaro Hardware Detection
* Roland Singer <roland@manjaro.org>
*
* Copyright (C) 2007 Free Software Foundation, Inc.
*
......
add_executable(test test.cpp)
target_link_libraries(test mhwd)
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include "mhwd.h"
using namespace std;
void message(string msg) {
cout << msg;
}
int main (int argc, char *argv[])
{
mhwd::Data data;
mhwd::fillData(&data);
data.environment.messageFunc = &message;
/*for (vector<mhwd::Device>::iterator dev_iter = data.PCIDevices.begin(); dev_iter != data.PCIDevices.end(); dev_iter++) {
for (vector<mhwd::Config>::iterator iterator = (*dev_iter).availableConfigs.begin(); iterator != (*dev_iter).availableConfigs.end(); iterator++) {
if (mhwd::installConfig(&data, &(*iterator)))
std::cout << "installed config!" << std::endl;
else
std::cout << "failed to installed config!\n" << data.lastError << std::endl;
}
}*/
for (vector<mhwd::Config>::iterator iterator = data.installedPCIConfigs.begin(); iterator != data.installedPCIConfigs.end(); iterator++) {
if (mhwd::uninstallConfig(&data, &(*iterator)))
std::cout << "installed config!" << std::endl;
else
std::cout << "failed to installed config!\n" << data.lastError << std::endl;
}
for (vector<mhwd::Device>::iterator dev_iter = data.PCIDevices.begin(); dev_iter != data.PCIDevices.end(); dev_iter++) {
if (!(*dev_iter).availableConfigs.empty())
cout << endl << endl << (*dev_iter).className << " " << (*dev_iter).vendorName << ":" << endl << "Available: ";
for (vector<mhwd::Config>::iterator iterator = (*dev_iter).availableConfigs.begin(); iterator != (*dev_iter).availableConfigs.end(); iterator++) {
cout << (*iterator).name << "-" << (*iterator).version << " ";
}
if (!(*dev_iter).installedConfigs.empty())
cout << endl <<"Installed: ";
for (vector<mhwd::Config>::const_iterator iterator = (*dev_iter).installedConfigs.begin(); iterator != (*dev_iter).installedConfigs.end(); iterator++) {
cout << (*iterator).name << "-" << (*iterator).version << " ";
}
}
/*for(unsigned int i = 0; i < devices.size(); i++) {
cout << setw(30) << devices[i]->getClassName();
cout << setw(10)<< devices[i]->getClassID();
cout << setw(40)<< devices[i]->getVendorName();
cout << setw(10)<< devices[i]->getVendorID();
cout << setw(30)<< devices[i]->getDeviceName();
cout << setw(10)<< devices[i]->getDeviceID();
cout << endl;
}
cout << endl << endl;
mhwd::printDetails(mhwd::HW_USB);*/
cout << endl << endl;
}
include_directories(. ../libmhwd ../libmhwd/vita)
###
### mhwd binary
###
set( HEADERS
)
set( SOURCES
mhwd.cpp
)
set( LIBS mhwd)
add_executable(mhwd-bin ${SOURCES} ${HEADERS})
target_link_libraries(mhwd-bin ${LIBS})
set_target_properties(mhwd-bin PROPERTIES OUTPUT_NAME mhwd)
/*
* 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/>.
*/
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <mhwd.h>
#include "vita/string.hpp"
using namespace std;
enum ARGUMENTS {
ARG_NONE = 0x0000,
ARG_SHOWAVAILABLE = 0x0001,
ARG_SHOWINSTALLED = 0x0002,
ARG_SHOWPCI = 0x0004,
ARG_SHOWUSB = 0x0008,
ARG_INSTALL = 0x0010,
ARG_REMOVE = 0x0020,
ARG_SHOWDETAILED = 0x0040,
ARG_FORCE = 0x0080
};
ARGUMENTS arguments;
mhwd::Data data;
vector<string> configList;
mhwd::TYPE operationType;
void messageFunc(string str);
void printHelp();
void printAvailableConfigs(mhwd::TYPE type);
void printInstalledConfigs(mhwd::TYPE type);
void checkInvalidConfigs();
bool installConfigs();
bool removeConfigs();
int main (int argc, char *argv[])
{
// Set up data object
mhwd::initData(&data);
data.environment.messageFunc = &messageFunc;
if (argc <= 1)
arguments = ARG_SHOWAVAILABLE;
else
arguments = ARG_NONE;
// Get command line arguments
for (int nArg=1; nArg < argc; nArg++) {
if (strcmp(argv[nArg], "-h") == 0 || strcmp(argv[nArg], "--help") == 0) {
printHelp();
return 0;
}
else if (strcmp(argv[nArg], "-f") == 0 || strcmp(argv[nArg], "--force") == 0) {
arguments = (ARGUMENTS)(arguments | ARG_FORCE);
}
else if (strcmp(argv[nArg], "-s") == 0 || strcmp(argv[nArg], "--show") == 0) {
arguments = (ARGUMENTS)(arguments | ARG_SHOWAVAILABLE);
}
else if (strcmp(argv[nArg], "-si") == 0 || strcmp(argv[nArg], "--showinstalled") == 0) {
arguments = (ARGUMENTS)(arguments | ARG_SHOWINSTALLED);
}
else if (strcmp(argv[nArg], "-sh") == 0 || strcmp(argv[nArg], "--showhardware") == 0) {
arguments = (ARGUMENTS)(arguments | ARG_SHOWDETAILED);
}
else if (strcmp(argv[nArg], "--pci") == 0) {
arguments = (ARGUMENTS)(arguments | ARG_SHOWPCI);
}
else if (strcmp(argv[nArg], "--usb") == 0) {
arguments = (ARGUMENTS)(arguments | ARG_SHOWUSB);
}
else if (strcmp(argv[nArg], "-i") == 0 || strcmp(argv[nArg], "--install") == 0) {
++nArg;
if (nArg >= argc || (strcmp(argv[nArg], "pci") != 0 && strcmp(argv[nArg], "usb") != 0)) {
cout << "invalid use of option: -i/--install" << endl << endl;
printHelp();
return 1;
}
if (strcmp(argv[nArg], "usb") == 0)
operationType = mhwd::TYPE_USB;
else
operationType = mhwd::TYPE_PCI;
arguments = (ARGUMENTS)(arguments | ARG_INSTALL);
}
else if (strcmp(argv[nArg], "-r") == 0 || strcmp(argv[nArg], "--remove") == 0) {
++nArg;
if (nArg >= argc || (strcmp(argv[nArg], "pci") != 0 && strcmp(argv[nArg], "usb") != 0)) {
cout << "invalid use of option: -i/--install" << endl << endl;
printHelp();
return 1;
}
if (strcmp(argv[nArg], "usb") == 0)
operationType = mhwd::TYPE_USB;
else
operationType = mhwd::TYPE_PCI;
arguments = (ARGUMENTS)(arguments | ARG_REMOVE);
}
else if (strcmp(argv[nArg], "--cachedir") == 0) {
if (nArg + 1 >= argc) {
cout << "invalid use of option: --cachedir" << endl << endl;
printHelp();
return 1;
}
data.environment.cachePath = Vita::string(argv[++nArg]).trim("\"").trim();
}
else if ((arguments & ARG_INSTALL) || (arguments & ARG_REMOVE)) {
configList.push_back(Vita::string(argv[nArg]).toLower());
}
else {
cout << "invalid option: " << argv[nArg] << endl << endl;
printHelp();
return 1;
}
}
if ((arguments & ARG_INSTALL) && (arguments & ARG_REMOVE)) {
cout << "Error: install and remove option can be used only seperate!" << endl << endl;
printHelp();
return 1;
}
else if ((arguments & ARG_REMOVE || arguments & ARG_INSTALL) && configList.empty()) {
cout << "nothing to do?!" << endl << endl;
printHelp();
return 1;
}
if (!(arguments & ARG_SHOWPCI) && !(arguments & ARG_SHOWUSB))
arguments = (ARGUMENTS)(arguments | ARG_SHOWUSB | ARG_SHOWPCI);
// Fill data with hardware informations
mhwd::fillData(&data);
// Check for invalid configs
checkInvalidConfigs();
// Check set arguments
if (arguments & ARG_SHOWDETAILED && arguments & ARG_SHOWPCI)
mhwd::printDeviceDetails(mhwd::TYPE_PCI);
if (arguments & ARG_SHOWDETAILED && arguments & ARG_SHOWUSB)
mhwd::printDeviceDetails(mhwd::TYPE_USB);
if (arguments & ARG_SHOWAVAILABLE && arguments & ARG_SHOWPCI)
printAvailableConfigs(mhwd::TYPE_PCI);
if (arguments & ARG_SHOWAVAILABLE && arguments & ARG_SHOWUSB)
printAvailableConfigs(mhwd::TYPE_USB);
if (arguments & ARG_SHOWINSTALLED && arguments & ARG_SHOWPCI)
printInstalledConfigs(mhwd::TYPE_PCI);
if (arguments & ARG_SHOWINSTALLED && arguments & ARG_SHOWUSB)
printInstalledConfigs(mhwd::TYPE_USB);
if (arguments & ARG_INSTALL) {
if (!installConfigs())
return 1;
}
else if (arguments & ARG_REMOVE) {
if (!removeConfigs())
return 1;
}
return 0;
}
void messageFunc(string str) {
cout << str;
}
void printHelp() {
cout << "Usage: mhwd [OPTIONS] <config(s)>" << endl << endl;
cout << " --pci\t\t\t\t\tshow only pci devices and driver configs" << endl;
cout << " --usb\t\t\t\t\tshow only usb devices and driver configs" << endl;
cout << " -h/--help\t\t\t\tshow help" << endl;
cout << " -f/--force\t\t\t\tforce reinstallation" << endl;
cout << " -s/--show\t\t\t\tshow devices and driver configs" << endl;
cout << " -si/--showinstalled\t\t\tshow installed driver configs" << endl;
cout << " -sh/--showhardware\t\t\tshow detailed hardware info" << endl;
cout << " -i/--install <usb/pci> <config(s)>\tinstall driver config(s)" << endl;
cout << " -r/--remove <usb/pci> <config(s)>\tremove driver config(s)" << endl;
cout << " --cachedir <path>\t\t\tset package manager cache path" << endl;
cout << endl;
}
void printAvailableConfigs(mhwd::TYPE type) {
string beg;
vector<mhwd::Device> *devices;
if (type == mhwd::TYPE_USB) {
beg = "USB";
devices = &data.USBDevices;
}
else {
beg = "PCI";
devices = &data.PCIDevices;
}
for (vector<mhwd::Device>::iterator dev_iter = devices->begin(); dev_iter != devices->end(); dev_iter++) {
if ((*dev_iter).availableConfigs.empty() && (*dev_iter).installedConfigs.empty())
continue;
cout << endl << setfill('-') << setw(80) << "-" << setfill(' ') << endl;
cout << "> " << beg << " Device: " << (*dev_iter).className << " (" << (*dev_iter).classID << ") ";
cout << (*dev_iter).vendorName << " (" << (*dev_iter).vendorID << ") ";
cout << (*dev_iter).deviceName << " (" << (*dev_iter).deviceID << ") " << endl;
cout << setfill('-') << setw(80) << "-" << setfill(' ') << endl;
if (!(*dev_iter).installedConfigs.empty())
cout << " > INSTALLED:" << endl;
for (vector<mhwd::Config>::iterator iterator = (*dev_iter).installedConfigs.begin(); iterator != (*dev_iter).installedConfigs.end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator).name << endl;
cout << " VERSION:\t" << (*iterator).version << endl;
cout << " INFO:\t" << (*iterator).info << endl;
cout << " PRIORITY:\t" << (*iterator).priority << endl;
if ((*iterator).freedriver)
cout << " FREEDRIVER:\ttrue" << endl;
else
cout << " FREEDRIVER:\tfalse" << endl;
}
if (!(*dev_iter).installedConfigs.empty())
cout << endl << endl;
if (!(*dev_iter).availableConfigs.empty())
cout << " > AVAILABLE:" << endl;
for (vector<mhwd::Config>::iterator iterator = (*dev_iter).availableConfigs.begin(); iterator != (*dev_iter).availableConfigs.end(); iterator++) {
cout << endl << " NAME:\t" << (*iterator).name << endl;
cout << " VERSION:\t" << (*iterator).version << endl;
cout << " INFO:\t" << (*iterator).info << endl;
cout << " PRIORITY:\t" << (*iterator).priority << endl;
if ((*iterator).freedriver)
cout << " FREEDRIVER:\ttrue" << endl;
else
cout << " FREEDRIVER:\tfalse" << endl;
}
cout << endl;
}
}
void printInstalledConfigs(mhwd::TYPE type) {
string beg;
vector<mhwd::Config> *configs;
if (type == mhwd::TYPE_USB) {
beg = "USB";
configs = &data.installedUSBConfigs;
}
else {
beg = "PCI";
configs = &data.installedPCIConfigs;
}
if (configs->empty())