On the forum we to talk about checking the battery level of the laptop under Linux using the Qt library. In fact, Qt does not currently provide a class that would allow you to check the battery charge. But such functionality can be implemented with the help of third-party libraries, for example libacpi.
Let's try to create an application that will take at least some data, and it will look like this:
libacpi
The library itself is written in C and has the following functions and data structures of interest:
- global_t - A global data structure that stores the number of fans, batteries and thermal zones, as well as a structure with a power adapter.
- init_acpi_batt(global_t *globals) - Initializing battery data
- init_acpi_acadapt(global_t *globals) - Initializing adapter data
- init_acpi_fan(global_t *globals) - Initializing fan data
- init_acpi_thermal(global_t *globals) - Initialization of data on thermal zones
- read_acpi_batt(int num) - Battery data reading
- read_acpi_zone(int num, global_t *globals) - Reading of data on thermal zones
- read_acpi_fan(int num) - Read fan data
- battery_t - Battery data structure
- fans_t - Fan data structure
- thermal_t - Structure of data on the thermal zone
- battery_t batteries [MAX_ITEMS] - Array of batteries, it stores the data when called read_acpi_batt
- thermal_t thermals [MAX_ITEMS] - Array of thermal zones, it stores data when called read_acpi_zone
- fan_t fans [MAX_ITEMS] - Array of fans, it stores data when called read_acpi_fan
Using the Library
To use a library in a project on Qt, you must:
- Install the library from the repository:
- sudo apt-get install libacpi-dev
- Connect the library to the project pro file:
- LIBS += -lacpi
- Connect the library header file to the source file where it will receive battery charge data:
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <libacpi.h>
- #ifdef __cplusplus
- }
- #endif
Main application window
The main application window is the Widget class, which is made using a ui file and whose interface is made through a graphical designer. It has ScrollArea, which contains an object for the vertical placement of elements, widgets, which will be created dynamically, depending on what the library could find. And also there is a button for starting the data reading by the library.
widget.h
In the header file, vectors with widgets for batteries, fans and thermal zones are declared. Also, a pointer to the widget with the power adapter is declared. All widgets are custom classes. A little further on is an example of the code of one of the widgets.
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QWidget>
- #include "batterywidget.h"
- #include "adapterwidget.h"
- #include "fanwidget.h"
- #include "thermalwidget.h"
- namespace Ui {
- class Widget;
- }
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
- private slots:
- void on_pushButton_clicked();
- private:
- Ui::Widget *ui;
- QVector<BatteryWidget*> m_butteryWidgetList; // Vector widget with batteries
- QVector<FanWidget*> m_fanWidgetList; // Vector of widgets with fans
- QVector<ThermalWidget*> m_thermalWidgetList; // Vector of widgets with thermal zones
- AdapterWidget* m_adapterWidget; // Widget for power adapter is always one
- };
- #endif // WIDGET_H
widget.cpp
Since the library is written in C, it has a procedural style of code execution. And also there is one little trick. The matter is that in library there are some global variables in which the data are written down. These are arrays with fans, heat zones and batteries.
That is, when you call a function read_acpi_batt(1) , then you write down the data structure battery_t into the cell of the array batteries[1].
Whereas to create a global structure that contains data on the number of all the above listed structures, you need to allocate memory.
- #include "widget.h"
- #include "ui_widget.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <libacpi.h>
- #ifdef __cplusplus
- }
- #endif
- #include <QFileDialog>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget),
- m_adapterWidget(nullptr)
- {
- ui->setupUi(this);
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::on_pushButton_clicked()
- {
- // We allocate memory for the global data structure of the library
- global_t *global = static_cast<global_t*>(malloc(sizeof(global_t)));
- // Initialize the structure for
- init_acpi_batt(global); // Batteries
- init_acpi_acadapt(global); // Power adapter
- init_acpi_fan(global); // Fans
- init_acpi_thermal(global); // Thermal zones
- // Battery
- battery_t *binfo;
- // If there were batteries, then we read the data in the loop and add widgets
- for(int i=0; i < global->batt_count; i++)
- {
- read_acpi_batt(i); // Read the data
- // Which are read into a special array of batteries - it is in the library itself
- binfo = &batteries[i];
- BatteryWidget* butteryWidget;
- // Create a widget
- if (m_butteryWidgetList.count() <= i)
- {
- butteryWidget = new BatteryWidget(this);
- butteryWidget->setTitle(QString(binfo->name));
- m_butteryWidgetList.append(butteryWidget);
- ui->verticalLayout->addWidget(butteryWidget);
- }
- else
- {
- // Or we take it from the list of widgets
- butteryWidget = m_butteryWidgetList.at(i);
- }
- // Set
- butteryWidget->setPercentage(binfo->percentage); // Percent charge
- butteryWidget->setChargeTime(binfo->charge_time); // Time to full charge
- butteryWidget->setRemainingTime(binfo->remaining_time); // Time to full discharge
- // Determine the charge status
- switch (binfo->charge_state)
- {
- case C_CHARGE:
- butteryWidget->setChargeState("Charging");
- break;
- case C_DISCHARGE:
- butteryWidget->setChargeState("Discharging");
- case C_CHARGED:
- butteryWidget->setChargeState("Charged");
- case C_NOINFO:
- butteryWidget->setChargeState("No info");
- default:
- butteryWidget->setChargeState("Error");
- break;
- }
- }
- // Adapter
- // Configuring the Widget for the Power Adapter
- adapter_t *ac = &global->adapt;
- if (!m_adapterWidget)
- {
- m_adapterWidget = new AdapterWidget(this);
- ui->verticalLayout->addWidget(m_adapterWidget);
- }
- // Here we check only whether it is connected or not
- switch (ac->ac_state)
- {
- case P_AC:
- m_adapterWidget->setStatus("Runs on AC");
- break;
- case P_BATT:
- m_adapterWidget->setStatus("Runs on battery");
- default:
- m_adapterWidget->setStatus("Error");
- break;
- }
- // Fan
- fan_t *finfo;
- // If there are fans, then we read the data in the loop and add widgets
- for(int i=0; i < global->fan_count; i++)
- {
- read_acpi_fan(i); // Read the data
- // Which are read into a special array of fans - it is in the library itself
- finfo = &fans[i];
- FanWidget* fanWidget;
- // Create a widget
- if (m_fanWidgetList.count() <= i)
- {
- fanWidget = new FanWidget(this);
- fanWidget->setTitle(QString(finfo->name));
- m_fanWidgetList.append(fanWidget);
- ui->verticalLayout->addWidget(fanWidget);
- }
- else
- {
- // Or we take it from the list of widgets
- fanWidget = m_fanWidgetList.at(i);
- }
- // Set the status, whether it is on or not
- switch (finfo->fan_state)
- {
- case F_ON:
- fanWidget->setStatus("ON");
- break;
- case F_OFF:
- fanWidget->setStatus("OFF");
- default:
- fanWidget->setStatus("Error");
- break;
- }
- }
- // Thermal
- thermal_t *tinfo;
- // If there are thermal zones, then we read the data in the cycle and add widgets
- for(int i=0; i < global->fan_count; i++)
- {
- read_acpi_zone(i, global); // Read the data
- // Which are read into a special array of thermals - it is in the library itself
- tinfo = &thermals[i];
- ThermalWidget* thermalWidget;
- // Create a widget
- if (m_thermalWidgetList.count() <= i)
- {
- thermalWidget = new ThermalWidget(this);
- thermalWidget->setTitle(QString(tinfo->name));
- m_thermalWidgetList.append(thermalWidget);
- ui->verticalLayout->addWidget(thermalWidget);
- }
- else
- {
- // Or we take it from the list of widgets
- thermalWidget = m_thermalWidgetList.at(i);
- }
- thermalWidget->setTemperature(tinfo->temperature); // Set the temperature
- thermalWidget->setFrequency(tinfo->frequency); // And frequency
- }
- free(global);
- }
Widgets
The program contains widgets for all the above structures. They serve only for displaying information and dynamic addition. Here is an example widget that displays information about the battery.
batterywidget.h
- #ifndef BATTERYWIDGET_H
- #define BATTERYWIDGET_H
- #include <QWidget>
- namespace Ui {
- class BatteryWidget;
- }
- class BatteryWidget : public QWidget
- {
- Q_OBJECT
- public:
- explicit BatteryWidget(QWidget *parent = 0);
- ~BatteryWidget();
- void setTitle(QString title); // Battery name (as specified in Linux OS)
- void setPercentage(int percentage); // Percent charge
- void setChargeTime(int chargeTime); // Time to full charge in minutes
- void setRemainingTime(int remainingTime); // Time to full discharge in minutes
- void setChargeState(QString chargeState); // State
- private:
- Ui::BatteryWidget *ui;
- };
- #endif // BATTERYWIDGET_H
batterywidget.cpp
- #include "batterywidget.h"
- #include "ui_batterywidget.h"
- BatteryWidget::BatteryWidget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::BatteryWidget)
- {
- ui->setupUi(this);
- }
- BatteryWidget::~BatteryWidget()
- {
- delete ui;
- }
- void BatteryWidget::setTitle(QString title)
- {
- ui->label_title->setText(title);
- }
- void BatteryWidget::setPercentage(int percentage)
- {
- ui->label_percentage->setText(QString::number(percentage));
- }
- void BatteryWidget::setChargeTime(int chargeTime)
- {
- ui->label_charge_time->setText(QString("%1:%2").arg(chargeTime / 60).arg(chargeTime % 60));
- }
- void BatteryWidget::setRemainingTime(int remainingTime)
- {
- ui->label_remaining_time->setText(QString("%1:%2").arg(remainingTime / 60).arg(remainingTime % 60));
- }
- void BatteryWidget::setChargeState(QString chargeState)
- {
- ui->label_charge_state->setText(chargeState);
- }
The result of the library
The result of the library I would call satisfactory, although it determines the percentage of battery charge, as well as the time for a full charge of the battery. But the time until the discharge, she did not determine. And also did not find any fans and thermal zones. I believe that on my laptop model this data is simply not available. Also, there were occasional drops of the application when trying to access certain parameters of structures.
The screenshot that appears at the beginning of the article displays the data that the library could find in my laptop. There, of course, not all the data are given, if you look at the library header file, you will find that the structures provide a richer selection of parameters. But to demonstrate the launch of the library I consider it sufficient.
Try to run the project at home, and if possible, write, if you have a laptop in this notebook will find thermal zones and fans.