Skip to content

Commit

Permalink
ipts: Move functions that check for report functions to protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
StollD committed Feb 8, 2024
1 parent c904905 commit c7a923e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 104 deletions.
1 change: 0 additions & 1 deletion src/core/linux/hidraw-device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <hid/parser.hpp>
#include <hid/report.hpp>
#include <ipts/parser.hpp>
#include <ipts/protocol.hpp>

#include <gsl/gsl>

Expand Down
85 changes: 13 additions & 72 deletions src/ipts/descriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef IPTSD_IPTS_DESCRIPTOR_HPP
#define IPTSD_IPTS_DESCRIPTOR_HPP

#include "protocol.hpp"
#include "protocol/descriptor.hpp"

#include <common/types.hpp>
#include <hid/report.hpp>
Expand All @@ -22,7 +22,7 @@ class Descriptor {
Descriptor(const std::vector<hid::Report> &reports) : m_reports {reports} {};

/*!
* Tries to find all reports that contain touch data in the HID descriptor.
* Searches for all reports that contain touch data in the HID descriptor.
*
* @return A list of reports containing IPTS touch data.
*/
Expand All @@ -31,107 +31,48 @@ class Descriptor {
std::vector<hid::Report> out {};

for (const hid::Report &report : m_reports) {
if (is_touch_data_report(report))
if (protocol::descriptor::is_touch_data(report))
out.push_back(report);
}

return out;
}

/*!
* Tries to find the report for modesetting in the HID descriptor.
* Searches for the modesetting report in the HID descriptor.
*
* The modesetting report is a feature report for changing the mode of the device from
* singletouch to multitouch and vice versa.
*
* @return The HID report for modesetting if it exists, null otherwise.
*/
[[nodiscard]] std::optional<hid::Report> find_modesetting_report() const
{
for (const hid::Report &report : m_reports) {
if (is_modesetting_report(report))
if (protocol::descriptor::is_set_mode(report))
return report;
}

return std::nullopt;
}

/*!
* Tries to find the metadata report in the HID descriptor.
* Searches for the metadata report in the HID descriptor.
*
* The metadata report is a feature report that returns data about the device, such as
* the screen size or the orientation of the touch data.
*
* @return The HID report for fetching metadata if it exists, null otherwise.
*/
[[nodiscard]] std::optional<hid::Report> find_metadata_report() const
{
for (const hid::Report &report : m_reports) {
if (is_metadata_report(report))
if (protocol::descriptor::is_metadata(report))
return report;
}

return std::nullopt;
}

/*!
* Checks whether a HID report matches the properties for an IPTS touch data report.
*
* @param[in] report The HID report to check.
* @return Whether the given report contains touch data.
*/
static bool is_touch_data_report(const hid::Report &report)
{
const std::unordered_set<hid::Usage> &usages = report.usages();

if (report.type() != hid::ReportType::Input)
return false;

if (usages.size() != 2)
return false;

return report.find_usage(IPTS_HID_REPORT_USAGE_PAGE_DIGITIZER,
IPTS_HID_REPORT_USAGE_SCAN_TIME) &&
report.find_usage(IPTS_HID_REPORT_USAGE_PAGE_DIGITIZER,
IPTS_HID_REPORT_USAGE_GESTURE_DATA);
}

/*!
* Checks whether a HID report matches the properties for the modesetting report.
*
* @param[in] report The HID report to check.
* @return Whether the given report is a modesetting report.
*/
static bool is_modesetting_report(const hid::Report &report)
{
const std::unordered_set<hid::Usage> &usages = report.usages();

if (report.type() != hid::ReportType::Feature)
return false;

if (usages.size() != 1)
return false;

if (report.size() != 8)
return false;

return report.find_usage(IPTS_HID_REPORT_USAGE_PAGE_VENDOR,
IPTS_HID_REPORT_USAGE_SET_MODE);
}

/*!
* Checks whether a HID report matches the properties of the metadata report.
*
* @param[in] report The HID report to check.
* @return Whether the given report is a metadata report.
*/
static bool is_metadata_report(const hid::Report &report)
{
const std::unordered_set<hid::Usage> &usages = report.usages();

if (report.type() != hid::ReportType::Feature)
return false;

if (usages.size() != 1)
return false;

return report.find_usage(IPTS_HID_REPORT_USAGE_PAGE_DIGITIZER,
IPTS_HID_REPORT_USAGE_METADATA);
}
};

} // namespace iptsd::ipts
Expand Down
31 changes: 0 additions & 31 deletions src/ipts/protocol.hpp

This file was deleted.

60 changes: 60 additions & 0 deletions src/ipts/protocol/descriptor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: GPL-2.0-or-later

#ifndef IPTSD_IPTS_PROTOCOL_DESCRIPTOR_HPP
#define IPTSD_IPTS_PROTOCOL_DESCRIPTOR_HPP

#include <common/types.hpp>
#include <hid/report.hpp>

namespace iptsd::ipts::protocol::descriptor {

namespace hid = iptsd::hid;

constexpr u16 USAGE_PAGE_DIGITIZER = 0x000D;
constexpr u16 USAGE_PAGE_VENDOR = 0xFF00;

constexpr u8 USAGE_SCAN_TIME = 0x56;
constexpr u8 USAGE_GESTURE_DATA = 0x61;
constexpr u8 USAGE_SET_MODE = 0xC8;
constexpr u8 USAGE_METADATA = 0x63;

/*!
* Checks if a given report contains touch data.
*
* @param[in] report The report to check.
* @return Whether the report matches the properties for a touch data report.
*/
inline bool is_touch_data(const hid::Report &report)
{
return report.type() == hid::ReportType::Input &&
report.find_usage(USAGE_PAGE_DIGITIZER, USAGE_SCAN_TIME) &&
report.find_usage(USAGE_PAGE_DIGITIZER, USAGE_GESTURE_DATA);
}

/*!
* Checks if a given report sets the mode of the device.
*
* @param[in] report The report to check.
* @return Whether the report matches the properties for a modesetting report.
*/
inline bool is_set_mode(const hid::Report &report)
{
return report.type() == hid::ReportType::Feature && report.size() == 8 &&
report.find_usage(USAGE_PAGE_VENDOR, USAGE_SET_MODE);
}

/*!
* Checks if a given report returns metadata for the device.
*
* @param[in] report The report to check.
* @return Whether the report matches the properties for a metadata report.
*/
inline bool is_metadata(const hid::Report &report)
{
return report.type() == hid::ReportType::Feature &&
report.find_usage(USAGE_PAGE_DIGITIZER, USAGE_METADATA);
}

} // namespace iptsd::ipts::protocol::descriptor

#endif // IPTSD_IPTS_PROTOCOL_DESCRIPTOR_HPP

0 comments on commit c7a923e

Please sign in to comment.