diff options
Diffstat (limited to 'src/hardware/dmidecode.c')
-rw-r--r-- | src/hardware/dmidecode.c | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/hardware/dmidecode.c b/src/hardware/dmidecode.c index f29596f..d989a09 100644 --- a/src/hardware/dmidecode.c +++ b/src/hardware/dmidecode.c @@ -1859,3 +1859,145 @@ void dmi_free_system_slots(DmiSystemSlot **slots, unsigned *slots_nb) *slots_nb = 0; *slots = NULL; } + + +/****************************************************************************** + * DmiPointingDevice + */ + +/* + * Initialize DmiPointingDevice attributes. + * @param dev + */ +void init_dmipointingdev_struct(DmiPointingDevice *dev) +{ + dev->type = NULL; + dev->buttons = 0; +} + +/* + * Check attributes of poiting device structure and fill in defaults if needed. + * @param dev + * @return 0 if success, negative value otherwise + */ +short check_dmipointingdev_attributes(DmiPointingDevice *dev) +{ + short ret = -1; + + if (!dev->type) { + if (!(dev->type = strdup("Unknown"))) { + ret = -2; + goto done; + } + } + + ret = 0; + +done: + if (ret != 0) { + warn("Failed to allocate memory."); + } + + return ret; +} + +short dmi_get_pointing_devices(DmiPointingDevice **devices, unsigned *devices_nb) +{ + short ret = -1; + int curr_dev = -1; + unsigned i, buffer_size = 0; + char **buffer = NULL, *buf; + + *devices_nb = 0; + + /* get dmidecode output */ + if (run_command("dmidecode -t 21", &buffer, &buffer_size) != 0) { + ret = -2; + goto done; + } + + /* count pointing devices */ + for (i = 0; i < buffer_size; i++) { + if (strncmp(buffer[i], "Handle 0x", 9) == 0) { + (*devices_nb)++; + } + } + + /* if no slot was found */ + if (*devices_nb < 1) { + warn("Dmidecode didn't recognize any pointing device."); + ret = -3; + goto done; + } + + /* allocate memory for pointing devices */ + *devices = (DmiPointingDevice *)calloc(*devices_nb, sizeof(DmiPointingDevice)); + if (!(*devices)) { + warn("Failed to allocate memory."); + ret = -4; + goto done; + } + + /* parse information about slots */ + for (i = 0; i < buffer_size; i++) { + if (strncmp(buffer[i], "Handle 0x", 9) == 0) { + curr_dev++; + init_dmipointingdev_struct(&(*devices)[curr_dev]); + continue; + } + /* ignore first useless lines */ + if (curr_dev < 0) { + continue; + } + /* Type */ + buf = copy_string_part_after_delim(buffer[i], "Type: "); + if (buf) { + (*devices)[curr_dev].type = buf; + buf = NULL; + continue; + } + /* Buttons */ + buf = copy_string_part_after_delim(buffer[i], "Buttons: "); + if (buf) { + sscanf(buf, "%u", &(*devices)[curr_dev].buttons); + free(buf); + buf = NULL; + continue; + } + } + + /* fill in default attributes if needed */ + for (i = 0; i < *devices_nb; i++) { + if (check_dmipointingdev_attributes(&(*devices)[i]) != 0) { + ret = -8; + goto done; + } + } + + ret = 0; + +done: + free_2d_buffer(&buffer, &buffer_size); + + if (ret != 0) { + dmi_free_pointing_devices(devices, devices_nb); + } + + return ret; +} + +void dmi_free_pointing_devices(DmiPointingDevice **devices, unsigned *devices_nb) +{ + unsigned i; + + if (*devices_nb > 0) { + for (i = 0; i < *devices_nb; i++) { + free((*devices)[i].type); + (*devices)[i].type = NULL; + } + free(*devices); + } + + *devices_nb = 0; + *devices = NULL; +} |