diff --git a/src/device/device.c b/src/device/device.c index 4b73647..afae99d 100644 --- a/src/device/device.c +++ b/src/device/device.c @@ -1,10 +1,25 @@ #include "device.h" +#include "utils/array.h" +#include "usb/usb.h" struct DEVICE { }; +array_t* device_array; + +void device_init() +{ + device_array = array_alloc(); + usb_init(); +} + +array_t* device_get_array() +{ + return device_array; +} + char* device_get_image(device_t* device) { return "/home/valentin/Documents/prjs/ginput/drivers/g-wolves/htx/htx_0.png"; diff --git a/src/device/device.h b/src/device/device.h index d1ab28c..a147f76 100644 --- a/src/device/device.h +++ b/src/device/device.h @@ -1,8 +1,16 @@ #ifndef DEVICE_H #define DEVICE_H +#include +#include "utils/array.h" + typedef struct DEVICE device_t; +// Device layer +void device_init(); +array_t* device_get_array(); + +// Handling a single device char* device_get_image(device_t* device); char* device_get_name(device_t* device); diff --git a/src/device/usb/usb.c b/src/device/usb/usb.c index c62cf25..fc98aef 100644 --- a/src/device/usb/usb.c +++ b/src/device/usb/usb.c @@ -2,6 +2,8 @@ #include #include +#include "device/device.h" + void usb_init() { // List all usb devices obtaining 'vendor:product' @@ -24,5 +26,7 @@ void usb_init() libusb_get_device_descriptor(current, &desc); printf("Found usb device %04x:%04x\n", desc.idVendor, desc.idProduct); + array_t* array = device_get_array(); + array_add(array, 0); } } diff --git a/src/main.c b/src/main.c index 112dafd..469ced2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,9 @@ #include "ui/ginput-application.h" -#include "device/usb/usb.h" +#include "device/device.h" int main(gint argc, gchar** argv) { - usb_init(); + device_init(); AdwApplication* application = ginput_application_new(); return g_application_run(G_APPLICATION(application), argc, argv); diff --git a/src/ui/main-window.c b/src/ui/main-window.c index 8acb341..549bfa1 100644 --- a/src/ui/main-window.c +++ b/src/ui/main-window.c @@ -2,6 +2,7 @@ #include "main-window.h" #include "panels/empty-panel.h" +#include "device/device.h" #include @@ -43,6 +44,7 @@ enum static gboolean activate_panel(MainWindow* self, Panel* panel, const gchar* id) { self->current_panel = GTK_WIDGET(panel); + self->active_panel = panel; gtk_stack_add_named(self->stack, self->current_panel, id); gtk_stack_set_visible_child_name (self->stack, id); } @@ -88,9 +90,9 @@ main_window_get_property(GObject* object, guint property_id, GValue* value, GPar switch(property_id) { - // case PROP_ACTIVE_PANEL: - // g_value_set_object (value, self->active_panel); - // break; + case PROP_ACTIVE_PANEL: + g_value_set_object (value, self->active_panel); + break; // case PROP_MODEL: // g_value_set_object (value, self->store); @@ -115,9 +117,9 @@ main_window_set_property(GObject* object, switch(property_id) { - // case PROP_ACTIVE_PANEL: - // set_active_panel(self, g_value_get_object(value)); - // break; + // case PROP_ACTIVE_PANEL: + // set_active_panel(self, g_value_get_object(value)); + // break; // case PROP_MODEL: // g_assert(self->store == NULL); @@ -153,6 +155,14 @@ load_window_state(MainWindow* self) gtk_window_maximize(GTK_WINDOW(self)); } +void main_window_add_all_devices(MainWindow* self) +{ + // Obtain devices, and iterate over the array to add them + array_t* devices = device_get_array(); + for(size_t i = 0; i < array_count(devices); i++) + main_window_add_device_to_list(self, array_get(devices, i)); +} + static void main_window_constructed(GObject* object) { @@ -160,6 +170,8 @@ main_window_constructed(GObject* object) load_window_state(self); + main_window_add_all_devices(self); + /* Add the panels */ // setup_model (self); @@ -198,12 +210,6 @@ main_window_finalize(GObject* object) { MainWindow* self = GINPUT_MAIN_WINDOW(object); - // if (self->previous_panels) - // { - // g_queue_free_full (self->previous_panels, g_free); - // self->previous_panels = NULL; - // } - g_clear_object(&self->settings); G_OBJECT_CLASS(main_window_parent_class)->finalize(object); @@ -243,8 +249,6 @@ main_window_init(MainWindow* self) gtk_widget_init_template(GTK_WIDGET(self)); self->settings = g_settings_new("v.ginput"); - // self->previous_panels = g_queue_new (); - // self->previous_list_view = cc_panel_list_get_view (self->panel_list); g_object_bind_property(self->main_leaflet, "folded", @@ -271,3 +275,17 @@ main_window_new(AdwApplication* application) "show-menubar", FALSE, NULL); } + +void main_window_add_device_to_list(MainWindow* self, device_t* device) +{ + // Setup row + GtkListBoxRow* row = (GtkListBoxRow*) gtk_list_box_row_new(); + GtkLabel* label = (GtkLabel*) gtk_label_new(device_get_name(device)); + gtk_widget_add_css_class(GTK_WIDGET(label), "body"); + gtk_widget_set_margin_top(GTK_WIDGET(label), 7); + gtk_widget_set_margin_bottom(GTK_WIDGET(label), 7); + gtk_list_box_row_set_child(row, GTK_WIDGET(label)); + + // Add row to listbox + gtk_list_box_append(GTK_LIST_BOX(self->device_list), GTK_WIDGET(row)); +} diff --git a/src/ui/main-window.h b/src/ui/main-window.h index 7792408..669b72c 100644 --- a/src/ui/main-window.h +++ b/src/ui/main-window.h @@ -1,6 +1,7 @@ #pragma once #include +#include "device/device.h" G_BEGIN_DECLS @@ -8,4 +9,6 @@ G_DECLARE_FINAL_TYPE(MainWindow, main_window, GINPUT, MAIN_WINDOW, AdwApplicatio MainWindow* main_window_new(AdwApplication* application); +void main_window_add_device_to_list(MainWindow* self, device_t* device); + G_END_DECLS diff --git a/src/utils/array.c b/src/utils/array.c new file mode 100644 index 0000000..5af709d --- /dev/null +++ b/src/utils/array.c @@ -0,0 +1,74 @@ +#include "array.h" +#include + +#define DEFAULT_ARRAY_SIZE 16 + +struct ARRAY +{ + void** data; + size_t count; + size_t size; +}; + +array_t* array_alloc() +{ + array_t* tr = malloc(sizeof(array_t)); + tr->data = malloc(sizeof(void*) * DEFAULT_ARRAY_SIZE); + tr->size = DEFAULT_ARRAY_SIZE; + tr->count = 0; + + return tr; +} + +void array_free(array_t* array) +{ + free(array->data); + free(array); +} + +void array_put(array_t* array, size_t index, void* elem) +{ + // Make sure allocated space is big enough + if(index >= array->size) + { + array->data = realloc(array->data, (index + 1) * 2); + if(!array->data) + { + perror("Catastrophic memory allocation failure:"); + exit(EXIT_FAILURE); + } + array->size = (index + 1) * 2; + } + + // Put element in array + array->data[index] = elem; + + // Update array count (max element position) if necessary + if(array->count <= index) array->count = index + 1; +} + +void array_add(array_t* array, void* elem) +{ + array_put(array, array->count, elem); +} + +void* array_get(array_t* array, size_t index) +{ + if(index >= array->size) + { + fprintf(stderr, "Catastrophic memory access failure: Trying to access index %lu of %lu-sized array\n", index, array->size); + exit(EXIT_FAILURE); + } + + if(index >= array->count) + { + fprintf(stderr, "Warning: Trying to access index %lu of array with %lu element, uninitialized access\n", index, array->count); + } + + return array->data[index]; +} + +size_t array_count(array_t* array) +{ + return array->count; +} diff --git a/src/utils/array.h b/src/utils/array.h new file mode 100644 index 0000000..20546e0 --- /dev/null +++ b/src/utils/array.h @@ -0,0 +1,17 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#include + +typedef struct ARRAY array_t; + +array_t* array_alloc(); +void array_free(array_t* array); + +void array_add(array_t* array, void* elem); +void array_put(array_t* array, size_t index, void* elem); + +void* array_get(array_t* array, size_t index); +size_t array_count(array_t* array); + +#endif \ No newline at end of file