-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
159 lines (122 loc) · 4.79 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdio.h>
#include "include/SDL2/SDL.h"
#include "include/SDL2/SDL_image.h"
#include "include/imgui/imgui.h"
#include "include/imgui/backends/imgui_impl_sdl.h"
#include "include/imgui/backends/imgui_impl_sdlrenderer.h"
#include "imgui_colors.h"
#include "usbdevice.h"
#if !SDL_VERSION_ATLEAST(2,0,17)
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
#endif
#define test 2
// Main code
int main(int argc, char** argv)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("Error: %s\n", SDL_GetError());
return -1;
}
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("USB Device Viewer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 435, 195, window_flags);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
SDL_Log("Error creating SDL_Renderer!");
return false;
}
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer_Init(renderer);
libusb_context* context = NULL;
libusb_device** list = NULL;
int rc = 0;
rc = libusb_init(&context);
struct Device* devices = GetDevices(rc, list);
int count = libusb_get_device_list(context, &list);
char* c_devices[count];
for (int i = 0; i < count; ++i)
c_devices[i] = (char*)malloc(sizeof(char) * 9);
int current_device = 0;
for (int i = 0; i < count; ++i) {
sprintf(c_devices[i], "[%d] %04X:%04X", devices[i].queue, devices[i].vendor_id, devices[i].device_id);
}
//0x2f3238
ImVec4 clear_color = ImVec4(0.21f, 0.22f, 0.23f, 1.00f);
bool im_mainWindow = true;
imgui_set_color();
SDL_Event event;
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
quit = true;
}
ImGui_ImplSDLRenderer_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// Main Window
{
ImGui::Begin("USB Device Viewer", &im_mainWindow, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
/* i dont like this
if (!ImGui::Combo("Device List", ¤t_device, c_devices, IM_ARRAYSIZE(c_devices))) {
ImGui::Text("Speed: %s", devices[current_device].speed);
}
*/
ImGui::Combo("Device List", ¤t_device, c_devices, IM_ARRAYSIZE(c_devices));
for (int i = 0; i < count; ++i) {
if (current_device == i) {
ImGui::TextColored(ImVec4(0, 255, 0, 255), "[Bus: %d | Device: %d]", devices[i].bus_number, devices[i].address);
ImGui::SameLine();
if (devices[i].port_number != '\0') {
ImGui::TextColored(ImVec4(255, 255, 0, 255), "COM Port: %d", devices[i].port_number);
ImGui::NewLine();
}
else {
ImGui::TextColored(ImVec4(255, 255, 0, 255), "COM Port: %c", devices[i].port_number);
ImGui::NewLine();
}
ImGui::Text("Vendor ID: %04X", devices[i].vendor_id);
ImGui::Text("Device ID: %04X", devices[i].device_id);
ImGui::Text("Device Speed: %s", devices[i].speed);
ImGui::NewLine();
ImGui::Text("Product: %s", devices[i].product);
ImGui::Text("Serial: %s", devices[i].serial);
ImGui::Text("Manufacturer: %s", devices[i].manufacturer);
}
}
ImGui::End();
}
// Rendering
ImGui::Render();
SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
SDL_RenderClear(renderer);
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
}
// Cleanup
libusb_free_device_list(list, 1);
for (int i = 0; i < count; ++i)
free(c_devices[i]);
list = NULL;
context = NULL;
libusb_exit(NULL);
ImGui_ImplSDLRenderer_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}