|
| 1 | +#include "DevicesModel.h" |
| 2 | +#include <AK/JsonArray.h> |
| 3 | +#include <AK/JsonObject.h> |
| 4 | +#include <AK/JsonValue.h> |
| 5 | +#include <LibCore/CDirIterator.h> |
| 6 | +#include <LibCore/CFile.h> |
| 7 | +#include <sys/stat.h> |
| 8 | + |
| 9 | +NonnullRefPtr<DevicesModel> DevicesModel::create() |
| 10 | +{ |
| 11 | + return adopt(*new DevicesModel); |
| 12 | +} |
| 13 | + |
| 14 | +DevicesModel::DevicesModel() |
| 15 | +{ |
| 16 | +} |
| 17 | + |
| 18 | +DevicesModel::~DevicesModel() |
| 19 | +{ |
| 20 | +} |
| 21 | + |
| 22 | +int DevicesModel::row_count(const GModelIndex&) const |
| 23 | +{ |
| 24 | + return m_devices.size(); |
| 25 | +} |
| 26 | + |
| 27 | +int DevicesModel::column_count(const GModelIndex&) const |
| 28 | +{ |
| 29 | + return Column::__Count; |
| 30 | +} |
| 31 | + |
| 32 | +String DevicesModel::column_name(int column) const |
| 33 | +{ |
| 34 | + switch (column) { |
| 35 | + case Column::Device: |
| 36 | + return "Device"; |
| 37 | + case Column::Major: |
| 38 | + return "Major"; |
| 39 | + case Column::Minor: |
| 40 | + return "Minor"; |
| 41 | + case Column::ClassName: |
| 42 | + return "Class"; |
| 43 | + case Column::Type: |
| 44 | + return "Type"; |
| 45 | + default: |
| 46 | + ASSERT_NOT_REACHED(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +GModel::ColumnMetadata DevicesModel::column_metadata(int column) const |
| 51 | +{ |
| 52 | + switch (column) { |
| 53 | + case Column::Device: |
| 54 | + return { 70, TextAlignment::CenterLeft }; |
| 55 | + case Column::Major: |
| 56 | + return { 32, TextAlignment::CenterRight }; |
| 57 | + case Column::Minor: |
| 58 | + return { 32, TextAlignment::CenterRight }; |
| 59 | + case Column::ClassName: |
| 60 | + return { 120, TextAlignment::CenterLeft }; |
| 61 | + case Column::Type: |
| 62 | + return { 120, TextAlignment::CenterLeft }; |
| 63 | + default: |
| 64 | + ASSERT_NOT_REACHED(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +GVariant DevicesModel::data(const GModelIndex& index, Role) const |
| 69 | +{ |
| 70 | + ASSERT(is_valid(index)); |
| 71 | + |
| 72 | + const DeviceInfo& device = m_devices[index.row()]; |
| 73 | + switch (index.column()) { |
| 74 | + case Column::Device: |
| 75 | + return device.path; |
| 76 | + case Column::Major: |
| 77 | + return device.major; |
| 78 | + case Column::Minor: |
| 79 | + return device.minor; |
| 80 | + case Column::ClassName: |
| 81 | + return device.class_name; |
| 82 | + case Column::Type: |
| 83 | + switch (device.type) { |
| 84 | + case DeviceInfo::Type::Block: |
| 85 | + return "Block"; |
| 86 | + case DeviceInfo::Type::Character: |
| 87 | + return "Character"; |
| 88 | + default: |
| 89 | + ASSERT_NOT_REACHED(); |
| 90 | + } |
| 91 | + default: |
| 92 | + ASSERT_NOT_REACHED(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void DevicesModel::update() |
| 97 | +{ |
| 98 | + CFile proc_devices { "/proc/devices" }; |
| 99 | + if (!proc_devices.open(CIODevice::OpenMode::ReadOnly)) |
| 100 | + ASSERT_NOT_REACHED(); |
| 101 | + |
| 102 | + auto json = JsonValue::from_string(proc_devices.read_all()).as_array(); |
| 103 | + |
| 104 | + m_devices.clear(); |
| 105 | + json.for_each([this](auto& value) { |
| 106 | + JsonObject device = value.as_object(); |
| 107 | + DeviceInfo device_info; |
| 108 | + |
| 109 | + device_info.major = device.get("major").to_uint(); |
| 110 | + device_info.minor = device.get("minor").to_uint(); |
| 111 | + device_info.class_name = device.get("class_name").to_string(); |
| 112 | + |
| 113 | + String type_str = device.get("type").to_string(); |
| 114 | + if (type_str == "block") |
| 115 | + device_info.type = DeviceInfo::Type::Block; |
| 116 | + else if (type_str == "character") |
| 117 | + device_info.type = DeviceInfo::Type::Character; |
| 118 | + else |
| 119 | + ASSERT_NOT_REACHED(); |
| 120 | + |
| 121 | + m_devices.append(move(device_info)); |
| 122 | + }); |
| 123 | + |
| 124 | + auto fill_in_paths_from_dir = [this](const String& dir) { |
| 125 | + CDirIterator dir_iter { dir, CDirIterator::Flags::SkipDots }; |
| 126 | + while (dir_iter.has_next()) { |
| 127 | + auto name = dir_iter.next_path(); |
| 128 | + auto path = String::format("%s/%s", dir.characters(), name.characters()); |
| 129 | + struct stat statbuf; |
| 130 | + if (lstat(path.characters(), &statbuf) != 0) { |
| 131 | + ASSERT_NOT_REACHED(); |
| 132 | + } |
| 133 | + if (!S_ISBLK(statbuf.st_mode) && !S_ISCHR(statbuf.st_mode)) |
| 134 | + continue; |
| 135 | + unsigned major = ::major(statbuf.st_rdev); |
| 136 | + unsigned minor = ::minor(statbuf.st_rdev); |
| 137 | + |
| 138 | + auto it = m_devices.find([major, minor](auto& device_info) { |
| 139 | + return device_info.major == major && device_info.minor == minor; |
| 140 | + }); |
| 141 | + if (it != m_devices.end()) { |
| 142 | + (*it).path = move(path); |
| 143 | + } |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + fill_in_paths_from_dir("/dev"); |
| 148 | + fill_in_paths_from_dir("/dev/pts"); |
| 149 | + |
| 150 | + did_update(); |
| 151 | +} |
0 commit comments