Commit 8fc8e3504c09e0d37a4972f7da2974c5f8e3b5c1

Authored by henyxia
1 parent a761c408

USB Initialization done

Showing 1 changed file with 81 additions and 202 deletions   Show diff stats
1   -#include <X11/Xlib.h>
2 1 #include <stdio.h>
3   -#include <stdlib.h> /* getenv(), etc. */
4   -#include <unistd.h> /* sleep(), etc. */
  2 +#include <stdbool.h>
  3 +#include <libusb-1.0/libusb.h>
5 4  
6   -Window create_simple_window(Display* display, int width, int height, int x, int y)
7   -{
8   - int screen_num = DefaultScreen(display);
9   - int win_border_width = 2;
10   - Window win;
11   -
12   - win = XCreateSimpleWindow(display, RootWindow(display, screen_num), x, y, width, height, win_border_width, BlackPixel(display, screen_num), WhitePixel(display, screen_num));
13   -
14   - XMapWindow(display, win);
15   - XFlush(display);
16   -
17   - return win;
18   -}
  5 +#define VENDOR_ID 0xfccf
  6 +#define PRODUCT_ID 0xa001
19 7  
20   -GC create_gc(Display* display, Window win, int reverse_video)
  8 +bool initUI(void)
21 9 {
22   - GC gc;
23   - unsigned long valuemask = 0; /* which values in 'values' to */
24   - /* check when creating the GC. */
25   - XGCValues values; /* initial values for the GC. */
26   - unsigned int line_width = 2; /* line width for the GC. */
27   - int line_style = LineSolid; /* style for lines drawing and */
28   - int cap_style = CapButt; /* style of the line's edje and */
29   - int join_style = JoinBevel; /* joined lines. */
30   - int screen_num = DefaultScreen(display);
31   -
32   - gc = XCreateGC(display, win, valuemask, &values);
33   - if (gc < 0) {
34   - fprintf(stderr, "XCreateGC: \n");
35   - }
36   -
37   - if (reverse_video)
38   - {
39   - XSetForeground(display, gc, WhitePixel(display, screen_num));
40   - XSetBackground(display, gc, BlackPixel(display, screen_num));
41   - }
42   - else
43   - {
44   - XSetForeground(display, gc, BlackPixel(display, screen_num));
45   - XSetBackground(display, gc, WhitePixel(display, screen_num));
46   - }
47   -
48   - XSetLineAttributes(display, gc, line_width, line_style, cap_style, join_style);
49   - XSetFillStyle(display, gc, FillSolid);
50   -
51   - return gc;
52   -}
  10 + int ret;
53 11  
54   -int main(int argc, char* argv[])
55   -{
56   - Display* display; /* pointer to X Display structure. */
57   - int screen_num; /* number of screen to place the window on. */
58   - Window win; /* pointer to the newly created window. */
59   - unsigned int display_width,
60   - display_height; /* height and width of the X display. */
61   - unsigned int width, height; /* height and width for the new window. */
62   - char *display_name = getenv("DISPLAY"); /* address of the X display. */
63   - GC gc; /* GC (graphics context) used for drawing */
64   - /* in our window. */
65   - Colormap screen_colormap; /* color map to use for allocating colors. */
66   - XColor red, brown, blue, yellow, green;
67   - /* used for allocation of the given color */
68   - /* map entries. */
69   - Status rc; /* return status of various Xlib functions. */
70   - int initOffsetBMP;
71   - XColor col;
72   -
73   - display = XOpenDisplay(display_name);
74   - if (display == NULL) {
75   - fprintf(stderr, "%s: cannot connect to X server '%s'\n",
76   - argv[0], display_name);
77   - exit(1);
78   - }
79   -
80   - screen_num = DefaultScreen(display);
81   - display_width = DisplayWidth(display, screen_num);
82   - display_height = DisplayHeight(display, screen_num);
83   -
84   - width = display_width;
85   - height = display_height;
86   - printf("window width - '%d'; height - '%d'\n", width, height);
87   -
88   - win = create_simple_window(display, width, height, 0, 0);
89   -
90   - gc = create_gc(display, win, 0);
91   - XSync(display, False);
92   - screen_colormap = DefaultColormap(display, DefaultScreen(display));
93   -
94   - rc = XAllocNamedColor(display, screen_colormap, "red", &red, &red);
95   - if (rc == 0) {
96   - fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n");
97   - exit(1);
98   - }
99   - rc = XAllocNamedColor(display, screen_colormap, "brown", &brown, &brown);
100   - if (rc == 0) {
101   - fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n");
102   - exit(1);
103   - }
104   - rc = XAllocNamedColor(display, screen_colormap, "blue", &blue, &blue);
105   - if (rc == 0) {
106   - fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n");
107   - exit(1);
108   - }
109   - rc = XAllocNamedColor(display, screen_colormap, "yellow", &yellow, &yellow);
110   - if (rc == 0) {
111   - fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n");
112   - exit(1);
113   - }
114   - rc = XAllocNamedColor(display, screen_colormap, "green", &green, &green);
115   - if (rc == 0) {
116   - fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n");
117   - exit(1);
118   - }
119   -
120   - /* draw one pixel near each corner of the window */
121   - /* draw the pixels in a red color. */
122   - XSetForeground(display, gc, red.pixel);
123   - XDrawPoint(display, win, gc, 5, 5);
124   - XDrawPoint(display, win, gc, 5, height-5);
125   - XDrawPoint(display, win, gc, width-5, 5);
126   - XDrawPoint(display, win, gc, width-5, height-5);
127   -
128   - FILE* home = NULL;
129   - char buffer[4];
130   - home = fopen("home.bmp", "r");
131   - if(home == NULL)
132   - return 0;
133   -
134   - fseek(home, 0x0A, SEEK_SET);
135   - buffer[0] = getc(home);
136   - buffer[1] = getc(home);
137   - buffer[2] = getc(home);
138   - buffer[3] = getc(home);
139   - printf("Pixel array offset %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
140   - initOffsetBMP = buffer[0] + (buffer[1] >> 8) + (buffer[2] >> 16) + (buffer[3] >> 24);
141   -
142   - fseek(home, 0x12, SEEK_SET);
143   - buffer[0] = getc(home);
144   - buffer[1] = getc(home);
145   - buffer[2] = getc(home);
146   - buffer[3] = getc(home);
147   - printf("Width %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
148   -
149   - //fseek(home, 0x12, SEEK_SET);
150   - buffer[0] = getc(home);
151   - buffer[1] = getc(home);
152   - buffer[2] = getc(home);
153   - buffer[3] = getc(home);
154   - printf("Height %02x %02x %02x %02x\n", buffer[0], buffer[1], buffer[2], buffer[3]);
155   -
156   - fseek(home, 0x1C, SEEK_SET);
157   - buffer[0] = getc(home);
158   - buffer[1] = getc(home);
159   - printf("Bits Per Pixel %02x %02x\n", buffer[0], buffer[1]);
160   -
161   - if(buffer[1] != 0 || buffer[0] != 0x18)
  12 + libusb_device_handle* screenHandle;
  13 +
  14 + ret = libusb_init(NULL);
  15 + if(ret != 0)
162 16 {
163   - printf("Could not read BMP\n");
164   - return 1;
  17 + printf("Error while initializing libusb, return : %d\n", ret);
  18 + return false;
165 19 }
166 20  
  21 + struct libusb_config_descriptor* dConfig = NULL;
  22 + libusb_device** list = NULL;
  23 + ssize_t cnt = libusb_get_device_list(NULL, &list);
167 24  
168   - fseek(home, initOffsetBMP, SEEK_SET);
  25 + struct libusb_device_descriptor dDevice;
169 26  
170   - col.flags= DoRed | DoGreen | DoBlue;
171   -
172   - int i;
173   - for(i=0; i<76800; i++)
  27 +
  28 + printf("Starting lsusb things\n");
  29 +
  30 + if(cnt < 0)
174 31 {
175   - col.red = buffer[2] * 257;
176   - col.green = buffer[1] * 257;
177   - col.blue = buffer[0] * 257;
178   -
179   - if(XAllocColor(display, screen_colormap, &col) == 0)
180   - printf("Failed to allocate color\n");
181   - buffer[0] = getc(home);
182   - buffer[1] = getc(home);
183   - buffer[2] = getc(home);
184   - printf("Alloc X %03d Y %03d R %02x G %02x B %02x\n", i%width, i/width, buffer[0], buffer[1], buffer[2]);
  32 + printf("Unable to get USB device list\n");
  33 + return false;
185 34 }
186 35  
  36 + printf("%d devices detected\n", cnt);
  37 + printf("List of compatible devices detected\n");
187 38  
188   - fseek(home, initOffsetBMP, SEEK_SET);
189   -
190   - for(i=0; i<76800; i++)
  39 + for (int i = 0; i < cnt; i++)
191 40 {
192   - col.red = buffer[2] * 257;
193   - col.green = buffer[1] * 257;
194   - col.blue = buffer[0] * 257;
195   -
196   - if(XSetForeground(display, gc, col.pixel) == 0)
197   - printf("Failed to set foreground\n");
198   - if(XDrawPoint(display,win,gc,i%width,height-1-(i/width)) == 0)
199   - printf("Failed to draw point\n");
200   - buffer[0] = getc(home);
201   - buffer[1] = getc(home);
202   - buffer[2] = getc(home);
203   - printf("Disp X %03d Y %03d R %02x G %02x B %02x\n", i%width, i/width, buffer[0], buffer[1], buffer[2]);
  41 + libusb_device *device = list[i];
  42 + ret = libusb_get_device_descriptor(device, &dDevice);
  43 + if(VENDOR_ID == dDevice.idVendor && PRODUCT_ID == dDevice.idProduct)
  44 + {
  45 + printf("Bus %03d Device %03d: ID %04x:%04x\n",
  46 + libusb_get_bus_number(device),
  47 + libusb_get_device_address(device), dDevice.idVendor,
  48 + dDevice.idProduct);
  49 + ret = libusb_open(device, &screenHandle);
  50 + if(ret != 0)
  51 + {
  52 + printf("Unable to open this device, error %d\n", ret);
  53 + return false;
  54 + }
  55 + }
204 56 }
205 57  
  58 + libusb_free_device_list(list, 1);
206 59  
  60 + ret = libusb_get_config_descriptor(libusb_get_device(screenHandle), 0, &dConfig);
  61 + if(ret!=0)
  62 + {
  63 + printf("Descriptor for this device unavailable\n");
  64 + return false;
  65 + }
  66 + else
  67 + {
  68 + for(int j=0; j<dConfig->bNumInterfaces; j++)
  69 + {
  70 + if(libusb_kernel_driver_active(screenHandle, j) && (libusb_detach_kernel_driver(screenHandle, j) != 0))
  71 + {
  72 + printf("Unable to detach this device\n");
  73 + return false;
  74 + }
  75 + }
  76 + ret = libusb_set_configuration(screenHandle, dConfig->bConfigurationValue);
  77 + if(ret != 0)
  78 + {
  79 + printf("Configuration unavailable, error %d\n", ret);
  80 + return false;
  81 + }
  82 + for(int j=0; j<dConfig->bNumInterfaces; j++)
  83 + if(libusb_claim_interface(screenHandle, j) != 0)
  84 + {
  85 + printf("Device not claimed\n");
  86 + return false;
  87 + }
  88 + else
  89 + printf("Interface %d ready\n", j);
  90 + }
  91 +
  92 + libusb_free_config_descriptor(dConfig);
207 93  
208   - /* draw two intersecting lines, one horizontal and one vertical, */
209   - /* which intersect at point "50,100". */
210   - /* draw the line in a brown color. */
211   - //XSetForeground(display, gc, brown.pixel);
212   - //XDrawLine(display, win, gc, 50, 0, 50, 200);
213   - //XDrawLine(display, win, gc, 0, 100, 200, 100);
214   -
215   - /* flush all pending requests to the X server. */
216   - XFlush(display);
  94 + return true;
  95 +}
217 96  
218   - /* make a delay for a short period. */
219   - sleep(4);
  97 +int main(void)
  98 +{
  99 + if(!initUI())
  100 + printf("Unable to initialize the UI\n");
220 101  
221   - /* close the connection to the X server. */
222   - XCloseDisplay(display);
223   - return(0);
  102 + return 0;
224 103 }
... ...