Skip to content
Snippets Groups Projects
Printer.cpp 9.66 KiB
Newer Older
Philip Müller's avatar
Philip Müller committed
/*
 *  This file is part of the mhwd - Manjaro Hardware Detection project
 *  
dec's avatar
dec committed
 *  mhwd - Manjaro Hardware Detection
 *  Roland Singer <roland@manjaro.org>
 *  Łukasz Matysiak <december0123@gmail.com>
Philip Müller's avatar
Philip Müller committed
 *  Filipe Marques <eagle.software3@gmail.com>
Philip Müller's avatar
Philip Müller committed
 *
dec's avatar
dec committed
 *  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/>.
Philip Müller's avatar
Philip Müller committed
 */

Łukasz Matysiak's avatar
Łukasz Matysiak committed
#include "Printer.hpp"

#include <hd.h>

Philip Müller's avatar
Philip Müller committed
#include <iomanip>
#include <iostream>
#include <memory>
december0123's avatar
december0123 committed
#include <string>
#include <vector>

Philip Müller's avatar
Philip Müller committed
void Printer::printStatus(std::string statusMsg) const
{
    std::cout << CONSOLE_RED_MESSAGE_COLOR << "> "
december0123's avatar
december0123 committed
            << CONSOLE_COLOR_RESET << statusMsg << std::endl;
Philip Müller's avatar
Philip Müller committed
}

void Printer::printError(std::string errorMsg) const
{
    std::cout << CONSOLE_RED_MESSAGE_COLOR << "Error: "
december0123's avatar
december0123 committed
            << CONSOLE_COLOR_RESET << errorMsg << std::endl;
Philip Müller's avatar
Philip Müller committed
}

void Printer::printWarning(std::string warningMsg) const
{
    std::cout << CONSOLE_RED_MESSAGE_COLOR << "Warning: "
december0123's avatar
december0123 committed
            << CONSOLE_COLOR_RESET << warningMsg << std::endl;
Philip Müller's avatar
Philip Müller committed
}

void Printer::printMessage(MHWD::MESSAGETYPE type, std::string msg) const
{
	switch(type)
	{
		case MHWD::MESSAGETYPE::CONSOLE_OUTPUT:
			std::cout << CONSOLE_TEXT_OUTPUT_COLOR << msg << CONSOLE_COLOR_RESET;
			break;
		case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START:
			printStatus("Installing dependency " + msg + "...");
			break;
		case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END:
			printStatus("Successfully installed dependency " + msg);
			break;
		case MHWD::MESSAGETYPE::INSTALL_START:
			printStatus("Installing " + msg + "...");
			break;
		case MHWD::MESSAGETYPE::INSTALL_END:
			printStatus("Successfully installed " + msg);
			break;
		case MHWD::MESSAGETYPE::REMOVE_START:
			printStatus("Removing " + msg + "...");
			break;
		case MHWD::MESSAGETYPE::REMOVE_END:
			printStatus("Successfully removed " + msg);
			break;
		default:
			printError("You shouldn't see this?! Unknown message type!");
			break;
	}
Philip Müller's avatar
Philip Müller committed
}

void Printer::printHelp() const
{
december0123's avatar
december0123 committed
    std::cout << "Usage: mhwd [OPTIONS] <config(s)>\n\n"
            << "  --pci\t\t\t\t\tlist only pci devices and driver configs\n"
            << "  --usb\t\t\t\t\tlist only usb devices and driver configs\n"
            << "  -h/--help\t\t\t\tshow help\n"
Philip Müller's avatar
Philip Müller committed
            << "  -v/--version\t\t\t\tshow version of mhwd\n"
december0123's avatar
december0123 committed
            << "  -f/--force\t\t\t\tforce reinstallation\n"
            << "  -d/--detail\t\t\t\tshow detailed info for -l/-li/-lh\n"
            << "  -l/--list\t\t\t\tlist available configs for devices\n"
            << "  -la/--listall\t\t\t\tlist all driver configs\n"
            << "  -li/--listinstalled\t\t\tlist installed driver configs\n"
            << "  -lh/--listhardware\t\t\tlist hardware information\n"
            << "  -i/--install <usb/pci> <config(s)>\tinstall driver config(s)\n"
            << "  -ic/--installcustom <usb/pci> <path>\tinstall custom config(s)\n"
            << "  -r/--remove <usb/pci> <config(s)>\tremove driver config(s)\n"
            << "  -a/--auto <usb/pci> <free/nonfree> <classid>\tauto install configs for classid\n"
            << "  --pmcachedir <path>\t\t\tset package manager cache path\n"
            << "  --pmconfig <path>\t\t\tset package manager config\n"
            << "  --pmroot <path>\t\t\tset package manager root\n" << std::endl;
Philip Müller's avatar
Philip Müller committed
}

void Printer::printVersion(std::string versionMhwd, std::string yearCopy) const
Philip Müller's avatar
Philip Müller committed
    std::cout << "Manjaro Hardware Detection v"<< versionMhwd <<"\n\n" 
Philip Müller's avatar
Philip Müller committed
            << "Copyright (C) "<< yearCopy <<" Manjaro Linux Developers\n"
Philip Müller's avatar
Philip Müller committed
            << "This is free software licensed under GNU GPL v3.0\n"
Philip Müller's avatar
Philip Müller committed
            << "FITNESS FOR A PARTICULAR PURPOSE.\n" 
            << std::endl;
december0123's avatar
december0123 committed
void Printer::listDevices(const std::vector<std::shared_ptr<Device>>& devices, std::string type) const
Philip Müller's avatar
Philip Müller committed
{
december0123's avatar
december0123 committed
    if (devices.empty())
    {
        printWarning("No " + type + " devices found!");
    }
    else
    {
        printStatus(type + " devices:");
        printLine();
        std::cout << std::setw(30) << "TYPE"
                << std::setw(15) << "BUS"
                << std::setw(8) << "CLASS"
                << std::setw(8) << "VENDOR"
                << std::setw(8) << "DEVICE"
                << std::setw(10) << "CONFIGS" << std::endl;
        printLine();
dec's avatar
dec committed
        for (const auto& device : devices)
december0123's avatar
december0123 committed
        {
december0123's avatar
december0123 committed
            std::cout << std::setw(30) << device->className_
                    << std::setw(15) << device->sysfsBusID_
                    << std::setw(8) << device->classID_
                    << std::setw(8) << device->vendorID_
                    << std::setw(8) << device->deviceID_
                    << std::setw(10) << device->availableConfigs_.size() << std::endl;
december0123's avatar
december0123 committed
        }
        std::cout << std::endl << std::endl;
    }
Philip Müller's avatar
Philip Müller committed
}

void Printer::listConfigs(const std::vector<std::shared_ptr<Config>>& configs, std::string header) const
Philip Müller's avatar
Philip Müller committed
{
    printStatus(header);
    printLine();
    std::cout << std::setw(22) << "NAME"
            << std::setw(22) << "VERSION"
            << std::setw(20) << "FREEDRIVER"
            << std::setw(15) << "TYPE" << std::endl;
    printLine();
dec's avatar
dec committed
    for (const auto& config : configs)
december0123's avatar
december0123 committed
    {
        std::cout << std::setw(22) << config->name_
                << std::setw(22) << config->version_
                << std::setw(20) << std::boolalpha << config->freedriver_
                << std::setw(15) << config->type_ << std::endl;
december0123's avatar
december0123 committed
    }
    std::cout << std::endl << std::endl;
Philip Müller's avatar
Philip Müller committed
}

void Printer::printAvailableConfigsInDetail(const std::string& deviceType,
december0123's avatar
december0123 committed
        const std::vector<std::shared_ptr<Device>>& devices) const
Philip Müller's avatar
Philip Müller committed
{
december0123's avatar
december0123 committed
    bool configFound = false;
Philip Müller's avatar
Philip Müller committed

dec's avatar
dec committed
    for (const auto& device : devices)
december0123's avatar
december0123 committed
    {
december0123's avatar
december0123 committed
        if (device->availableConfigs_.empty() && device->installedConfigs_.empty())
december0123's avatar
december0123 committed
        {
            continue;
        }
        else
        {
            configFound = true;
Philip Müller's avatar
Philip Müller committed

december0123's avatar
december0123 committed
            printLine();
            printStatus(
december0123's avatar
december0123 committed
                    deviceType + " Device: " + device->sysfsID_ + " (" + device->classID_ + ":"
                    + device->vendorID_ + ":" + device->deviceID_ + ")");
            std::cout << "  " << device->className_
                    << " " << device->vendorName_
                    << " " << device->deviceName_ << std::endl;
december0123's avatar
december0123 committed
            printLine();
december0123's avatar
december0123 committed
            if (!device->installedConfigs_.empty())
december0123's avatar
december0123 committed
            {
dec's avatar
dec committed
                std::cout << "  > INSTALLED:\n\n";
                for (auto&& installedConfig : device->installedConfigs_)
december0123's avatar
december0123 committed
                {
                    printConfigDetails(*installedConfig);
                }
dec's avatar
dec committed
                std::cout << "\n\n";
december0123's avatar
december0123 committed
            }
december0123's avatar
december0123 committed
            if (!device->availableConfigs_.empty())
december0123's avatar
december0123 committed
            {
dec's avatar
dec committed
                std::cout << "  > AVAILABLE:\n\n";
                for (auto&& availableConfig : device->availableConfigs_)
december0123's avatar
december0123 committed
                {
                    printConfigDetails(*availableConfig);
                }
                std::cout << std::endl;
            }
        }
    }
Philip Müller's avatar
Philip Müller committed

december0123's avatar
december0123 committed
    if (!configFound)
    {
        printWarning("no configs for " + deviceType + " devices found!");
    }
Philip Müller's avatar
Philip Müller committed
}

december0123's avatar
december0123 committed
void Printer::printInstalledConfigs(const std::string& deviceType,
december0123's avatar
december0123 committed
        const std::vector<std::shared_ptr<Config>>& installedConfigs) const
Philip Müller's avatar
Philip Müller committed
{
december0123's avatar
december0123 committed
    if (installedConfigs.empty())
    {
        printWarning("no installed configs for " + deviceType + " devices found!");
    }
    else
    {
dec's avatar
dec committed
        for (const auto& config : installedConfigs)
december0123's avatar
december0123 committed
        {
            printConfigDetails(*config);
        }
        std::cout << std::endl;
    }
Philip Müller's avatar
Philip Müller committed
}

void Printer::printConfigDetails(const Config& config) const
{
december0123's avatar
december0123 committed
    std::string classids;
    std::string vendorids;
dec's avatar
dec committed
    for (const auto& hwd : config.hwdIDs_)
december0123's avatar
december0123 committed
    {
dec's avatar
dec committed
        for (const auto& vendorID : hwd.vendorIDs)
december0123's avatar
december0123 committed
        {
            vendorids += vendorID + " ";
        }
Philip Müller's avatar
Philip Müller committed

dec's avatar
dec committed
        for (const auto& classID : hwd.classIDs)
december0123's avatar
december0123 committed
        {
            classids += classID + " ";
        }
    }
december0123's avatar
december0123 committed
    std::string dependencies;
dec's avatar
dec committed
    for (const auto& dependency : config.dependencies_)
december0123's avatar
december0123 committed
    {
        dependencies += dependency + " ";
    }
december0123's avatar
december0123 committed
    std::string conflicts;
dec's avatar
dec committed
    for (const auto& conflict : config.conflicts_)
december0123's avatar
december0123 committed
    {
        conflicts += conflict + " ";
    }
Philip Müller's avatar
Philip Müller committed

december0123's avatar
december0123 committed
    std::cout << "   NAME:\t" << config.name_
            << "\n   ATTACHED:\t" << config.type_
            << "\n   VERSION:\t" << config.version_
december0123's avatar
december0123 committed
            << "\n   INFO:\t" << (config.info_.empty() ? "-" : config.info_)
december0123's avatar
december0123 committed
            << "\n   PRIORITY:\t" << config.priority_
            << "\n   FREEDRIVER:\t" << std::boolalpha << config.freedriver_
december0123's avatar
december0123 committed
            << "\n   DEPENDS:\t" << (dependencies.empty() ? "-" : dependencies)
            << "\n   CONFLICTS:\t" << (conflicts.empty() ? "-" : conflicts)
december0123's avatar
december0123 committed
            << "\n   CLASSIDS:\t" << classids
dec's avatar
dec committed
            << "\n   VENDORIDS:\t" << vendorids << "\n" << std::endl;
december0123's avatar
december0123 committed
}

void Printer::printLine() const
{
december0123's avatar
december0123 committed
    std::cout << std::setfill('-') << std::setw(80) << "-" << std::setfill(' ') << std::endl;
Philip Müller's avatar
Philip Müller committed
}
void Printer::printDeviceDetails(hw_item hw, FILE *f) const
{
    std::unique_ptr<hd_data_t> hd_data{new hd_data_t()};
    hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr);

    for (hd_t* hdIter = hd; hdIter; hdIter = hdIter->next)
    {
        hd_dump_entry(hd_data.get(), hdIter, f);
    }

    hd_free_hd_list(hd);
    hd_free_hd_data(hd_data.get());
}