-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ellen.cpp
2727 lines (2391 loc) · 75.5 KB
/
Ellen.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Ellen.cpp
//Implements definitions of Ellen functionality
//"We wanted to end the cycle of war between man and machine," - Ellen Tigh
//includes
//custom
#include "Ellen.h"
//namespace
using namespace std;
//Dyanamic Library Stuff
//Constant for how many versions in the future we look forward for checking lib #'s
dynLib allLibs[libCount];
//keep track of if libraries have been initialized for function calls
bool sdlInitialized = false;
bool usbInitialized = false;
libFunc libsdlFunctions[libsdlCount] =
{
{
SDL_NUMJOYSTICKS,
NULL
},
{
SDL_ISGAMECONTROLLER,
NULL
},
{
SDL_JOYSTICKOPEN,
NULL
},
{
SDL_GAMECONTROLLEROPEN,
NULL
},
{
SDL_GAMECONTROLLERNAME,
NULL
},
{
SDL_JOYSTICKINSTANCEID,
NULL
},
{
SDL_GAMECONTROLLERGETAXIS,
NULL
},
{
SDL_POLLEVENT,
NULL
},
{
SDL_GAMECONTROLLERNAMEFORINDEX,
NULL
},
{
SDL_JOYSTICKNAMEFORINDEX,
NULL
},
{
SDL_JOYSTICKGETAXIS,
NULL
},
{
SDL_SETHINT,
NULL
},
{
SDL_CREATEWINDOW,
NULL
},
{
SDL_INIT,
NULL
},
{
SDL_JOYSTICKNAME,
NULL
},
{
SDL_GL_SETATTRIBUTE,
NULL
},
{
SDL_GETNUMVIDEODISPLAYS,
NULL
},
{
SDL_GETDISPLAYNAME,
NULL
},
{
SDL_GETDISPLAYBOUNDS,
NULL
},
{
SDL_GETCURRENTDISPLAYMODE,
NULL
},
{
SDL_QUIT_METHOD,
NULL
}
//END ENTRIES
}; //END ARRAY
libFunc libusbFunctions[libusbCount] =
{
{
LIBUSB_INIT,
NULL
},
{
LIBUSB_GET_DEVICE_LIST,
NULL
},
{
LIBUSB_GET_DEVICE_DESCRIPTOR,
NULL
},
{
LIBUSB_GET_ACTIVE_CONFIG_DESCRIPTOR,
NULL
},
{
LIBUSB_FREE_DEVICE_LIST,
NULL
},
{
LIBUSB_EXIT,
NULL
},
{
LIBUSB_GET_BUS_NUMBER,
NULL
}
};
void fillTable()
{
//Fill allLibs with info
//libusb
allLibs[libusb].libName = LIBUSB_LIB_NAME;
allLibs[libusb].libAddr = NULL;
allLibs[libusb].versionNumber = LIBUSB_LATEST_VERSION;
allLibs[libusb].funcCount = libusbCount;
allLibs[libusb].functions = libusbFunctions;
allLibs[libusb].opened = false;
//libsdl
allLibs[libsdl].libName = LIBSDL_LIB_NAME;
allLibs[libsdl].libAddr = NULL;
allLibs[libsdl].versionNumber = LIBSDL_LATEST_VERSION;
allLibs[libsdl].funcCount = libsdlCount;
allLibs[libsdl].functions = libsdlFunctions;
allLibs[libsdl].opened = false;
}//END method
void openLibs()
{
for (int i = 0; i < libCount; i++)
{
//Iterate over versions until we find one that successfully dlopens
for (int j = (allLibs[i].versionNumber + NUMBER_OF_VERSIONS_TO_LOOK_FORWARD); j >= 0; j--)
{
//attempt dlopen
if(j == 0)
{
//just use file name for if no numbered version works
allLibs[i].libAddr = dlopen(allLibs[i].libName, RTLD_LAZY);
}
else
{
//build file name to check in dlopen()
stringstream ss;
ss << allLibs[i].libName << "." << j;
string strName = ss.str();
const char* libraryName = strName.c_str();
//attempt to open
allLibs[i].libAddr = dlopen(libraryName, RTLD_LAZY);
}//END elsif J == 0
//if lib loaded successfully
if(allLibs[i].libAddr != NULL)
{
cout<<"Found lib "<<allLibs[i].libName<<endl;
//set opened to true
allLibs[i].opened = true;
//Grab the functions
for (int k = 0; k < allLibs[i].funcCount; k++ )
{
/*char* errstr;
errstr = dlerror();
printf("This happened: (%s)\n", errstr);
*/
//Grab function address
allLibs[i].functions[k].funcAddr = dlsym(allLibs[i].libAddr, allLibs[i].functions[k].funcName);
if(allLibs[i].functions[k].funcAddr != NULL)
{
cout<<"Found method "<<allLibs[i].functions[k].funcName<<endl;
}
else
{
cout<<"Didn't find method "<<allLibs[i].functions[k].funcName<<endl;
}
}//END function for loop
}//end if lib loaded successfully
}//END inner for
}//END outer for
}//END func
void closeLibs()
{
//iterate through allLibs
for (int i = 0; i < libCount; i++)
{
//Variable Declaration
int close;
//if the library was marked as opened
if(allLibs[i].opened)
{
//Close it
close = dlclose(allLibs[i].libAddr);
allLibs[i].opened = false;
}
else
{
close = 0;
}//end IF opened
if (close != 0)
{
//Could possibly handle errors somehow here. Have yet to reach this point when dlclose fails
}//END if error
}//END for
}//END method
void sdlExit()
{
SDL_Quit_t _SDL_Quit = (SDL_Quit_t)allLibs[libsdl].functions[SDL_Quit_e].funcAddr;
_SDL_Quit();
sdlInitialized = false;
}
//END DYLIB stuff
//Producers
void produceUserProfile(struct cylonStruct& et)
{
//set default
et.pictureLocation = ERROR_INT;
struct passwd* pw;
uid_t uid;
uid = geteuid();
pw = getpwuid(uid);
if (pw)
{
const char* charUname = pw->pw_name;
string strUname(charUname);
et.username = strUname;
//Build file name
string avatarName = AVATAR_PATH + et.username;
//Check if file exists
ifstream f(avatarName.c_str());
if (f.good())
{
//set picture Path and Type
et.picturePath = avatarName;
et.pictureType = AVATAR_TYPE;
}
else
{
//set defaults
et.picturePath = ERROR_STRING;
et.pictureType = ERROR_STRING;
}
//close file
f.close();
}//END if pw
else
{
//set defaults
et.username = ERROR_STRING;
et.picturePath = ERROR_STRING;
et.pictureType = ERROR_STRING;
}
}//END produceUsername
void produceDeviceName(struct cylonStruct& et)
{
//Method Credit to Saurabh Gupta @ ccplusplus.com
//Variable Declaration
int result;
char buf[32];
//grab host name
result = gethostname(buf, sizeof buf);
//if error, set to default value
if (result != 0)
{
et.deviceName = "0";
return;
}
//otherwise, set deviceName field
string strDeviceName(buf);
et.deviceName = strDeviceName;
}//END produceDeviceName
void produceDateTimeZone(struct cylonStruct& et)
{
//Credit to cppreference.com for partial method implementation
//Variable Declaration
int yearZero = 1900;
int minutesInAnHour = 60;
//Grab time
time_t timeLocal = std::time(NULL);
//Convert to local time
tm* localTime = std::localtime(&timeLocal);
//Set components of date/time
et.milliseconds = 0;
et.seconds = localTime->tm_sec;
et.minutes = localTime->tm_min;
et.hours = localTime->tm_hour;
et.day = localTime->tm_wday;
et.date = localTime->tm_mday;
et.month = localTime->tm_mon + 1;
et.year = localTime->tm_year + yearZero;
et.dst = localTime->tm_isdst;
et.timeZoneName = localTime->tm_zone;
et.timeZone = localTime->tm_gmtoff/minutesInAnHour;
}//END produceTimeZone
void produceProcessorInfo(struct cylonStruct& et)
{
//Grab processor count
et.processorCount = sysconf(_SC_NPROCESSORS_ONLN);
//Credit to MakeLinux.net for partial method code
//Variable Declaration
FILE* fp;
char buffer[1024];
size_t bytes_read;
char* match;
float clock_speed;
float mhzToHz = 1000000;
int processorLevel;
//Read contents of /proc/cpuinfo into the buffer
fp = fopen("/proc/cpuinfo", "r");
bytes_read = fread (buffer, 1, sizeof(buffer), fp);
fclose(fp);
//if read failed or exceeds buffer, get out
if (bytes_read == 0 || bytes_read > sizeof(buffer))
{
//Set defaults
et.hertz = ERROR_INT;
et.processorLevel = ERROR_INT;
et.architecture = ERROR_STRING;
}
else
{
//Add null termination to buffer
buffer[bytes_read] = '\0';
//Grab CPU speed
match = strstr(buffer, "cpu MHz");
if (match == NULL)
{
//Set Default
et.hertz = ERROR_INT;
}
else
{
sscanf(match, "cpu MHz : %f", &clock_speed);
et.hertz = clock_speed * mhzToHz;
}//END if match is null
//Grab architecture
match = strstr(buffer, "model");
if (match == NULL)
{
//Set default
et.processorLevel = ERROR_INT;
}
else
{
sscanf(match, "model : %d", &processorLevel);
et.processorLevel = (uint16_t)processorLevel;
}//END if match is null
}//END if
//Credit to Amber @ stack overflow for partial method code
//Grab utsname struct that contains architecture info
struct utsname unameData;
int result = uname(&unameData);
//Check for errors
if (result != 0)
{
//set default
et.architecture = ERROR_STRING;
} //end if result != 0
else
{
et.architecture = unameData.machine;
}
} //END produceProcessorInfo
void produceMemoryInfo(struct cylonStruct& et)
{
//Credit to David R. Nadeau @ nadeausoftware.com for partial method code
//Variable Declaration
struct sysinfo info;
float lowMemory = 0.99;
//Grab info
sysinfo(&info);
//Set cylonStruct memory fields
et.memoryBytes = info.totalram;
et.bytesAvails = info.freeram;
et.threshold = info.totalram * lowMemory;
et.pageSize = sysconf (_SC_PAGESIZE);
et.allocationGranularity = et.pageSize;
et.minAppAddress = ERROR_INT;
et.maxAppAddress = ERROR_INT;
//Calculate low memory
if(info.freeram/info.totalram >= lowMemory)
{
et.lowMemory = 1;
}
else
{
et.lowMemory = 0;
}
//set osArch
if (__WORDSIZE % 2)
{
//set default
et.osArchitecture = ERROR_INT;
}
else
{
et.osArchitecture = __WORDSIZE;
}
} //END produceMemoryInfo
void produceDeviceInfo(struct cylonStruct& et)
{
//Grab Device Types
produceUsbDeviceInfo(et);
produceControllerInfo(et);
produceDisplayInfo(et);
produceStorageInfo(et);
//Grab total count
et.detectedDeviceCount = et.detectedDevices.size();
} //END produceDeviceInfo
void produceControllerInfo(struct cylonStruct& et)
{
//don't call this if SDL didn't open
if(!allLibs[libsdl].opened || !sdlInitialized)
{
return;
}
//add mappings
int mappingsResult = SDL_GameControllerAddMappingsFromFile("./src/Ellen/SDL_GameControllerDB/gamecontrollerdb.txt");
if(mappingsResult == -1)
{
printf("Warning: device button mappings failed to load from file.");
}
//Cast functions
SDL_NumJoysticks_t _SDL_NumJoysticks = (SDL_NumJoysticks_t) allLibs[libsdl].functions[SDL_NumJoysticks_e].funcAddr;
SDL_IsGameController_t _SDL_IsGameController = (SDL_IsGameController_t) allLibs[libsdl].functions[SDL_IsGameController_e].funcAddr;
SDL_JoystickOpen_t _SDL_JoystickOpen = (SDL_JoystickOpen_t) allLibs[libsdl].functions[SDL_JoystickOpen_e].funcAddr;
SDL_GameControllerOpen_t _SDL_GameControllerOpen = (SDL_GameControllerOpen_t) allLibs[libsdl].functions[SDL_GameControllerOpen_e].funcAddr;
SDL_GameControllerName_t _SDL_GameControllerName = (SDL_GameControllerName_t) allLibs[libsdl].functions[SDL_GameControllerName_e].funcAddr;
SDL_JoystickInstanceID_t _SDL_JoystickInstanceID = (SDL_JoystickInstanceID_t) allLibs[libsdl].functions[SDL_JoystickInstanceID_e].funcAddr;
SDL_GameControllerGetAxis_t _SDL_GameControllerGetAxis = (SDL_GameControllerGetAxis_t) allLibs[libsdl].functions[SDL_GameControllerGetAxis_e].funcAddr;
SDL_JoystickName_t _SDL_JoystickName = (SDL_JoystickName_t) allLibs[libsdl].functions[SDL_JoystickName_e].funcAddr;
//grab gamepad count
int gamepadCount = _SDL_NumJoysticks();
if(gamepadCount <= 0)
{
//error or no controllers
return;
}
//Credit to SDL Wiki for partial method code
for (int i = 0; i < gamepadCount; i++)
{
SDL_Joystick* joy;
joy = _SDL_JoystickOpen(i);
if(!joy)
{
//Retrieval failed, go on to next device
continue;
}
//Use SDL_GameController Class
if(_SDL_IsGameController(i))
{
//Open controller via index
SDL_GameController* sdlPad = _SDL_GameControllerOpen(i);
//Make sure controller open worked
if(!sdlPad)
{
//Retrieval failed, go on to next device
continue;
}
else
{
//build structs
deviceStruct device = buildControllerDevice(i, _SDL_GameControllerName(sdlPad), _SDL_JoystickInstanceID(joy));
controllerStruct controller = buildController(device, i, _SDL_JoystickInstanceID(joy));
//handle errors
if(device.id_int < 0 || controller.id < 0)
{
et.error |= INVALID_CONTROLLER_ID;
}
//credit to davidgow.net for partial input code
controller.thumbLeftX = normalizeAxis(_SDL_GameControllerGetAxis(sdlPad, SDL_CONTROLLER_AXIS_LEFTX), false);
controller.thumbLeftY = normalizeAxis(_SDL_GameControllerGetAxis(sdlPad, SDL_CONTROLLER_AXIS_LEFTY), false);
controller.leftTrigger = normalizeAxis(_SDL_GameControllerGetAxis(sdlPad, SDL_CONTROLLER_AXIS_TRIGGERLEFT), true);
controller.thumbRightX = normalizeAxis(_SDL_GameControllerGetAxis(sdlPad, SDL_CONTROLLER_AXIS_RIGHTX), false);
controller.thumbRightY = normalizeAxis(_SDL_GameControllerGetAxis(sdlPad, SDL_CONTROLLER_AXIS_RIGHTY), false);
controller.rightTrigger = normalizeAxis(_SDL_GameControllerGetAxis(sdlPad, SDL_CONTROLLER_AXIS_TRIGGERRIGHT), true);
//add to lists for ellen
et.controllers.push_back(controller);
device.controllerIndex = et.controllers.size() - 1;
et.detectedDevices.push_back(device);
et.controllers.back().superDevice = et.detectedDevices.back();
}//END if sdlController successfully opened
}//END if sdl is controller
//use SDL_Joystick class
else
{
//Use Joystick class to build device and controller structs
deviceStruct device = buildControllerDevice(i, _SDL_JoystickName(joy), _SDL_JoystickInstanceID(joy));
controllerStruct controller = buildController(device, i, _SDL_JoystickInstanceID(joy));
//handle errors
if(device.id_int < 0 || controller.id < 0)
{
et.error |= INVALID_CONTROLLER_ID;
}
//add to lists for ellen
et.controllers.push_back(controller);
device.controllerIndex = et.controllers.size() - 1;
et.detectedDevices.push_back(device);
et.controllers.back().superDevice = et.detectedDevices.back();
}
}//END for
}//END Method
void produceUsbDeviceInfo(cylonStruct& et)
{
if (!allLibs[libusb].opened)
{
//Get out, this method won't work without libusb loaded!
return;
}
//Credit to LibUSB examples for partial method code
//Variable Declaration:
//device array
libusb_device** devices;
libusb_context* context;
int result;
std::list<struct deviceStruct> detectedDevices;
//Convert method to type
libusb_init_t _libusb_init = (libusb_init_t) allLibs[libusb].functions[libusb_init_e].funcAddr;
//init lib & session
result = _libusb_init(&context);
//check for errors
if(result < 0)
{
usbInitialized = false;
return;
}
else
{
usbInitialized = true;
}
//Convert method to type
libusb_get_device_list_t _libusb_get_device_list = (libusb_get_device_list_t) allLibs[libusb].functions[libusb_get_device_list_e].funcAddr;
//grab devices
_libusb_get_device_list(context, &devices);
//check for errors
if(result < 0)
{
return;
}
//Variable Declaration
libusb_device *device;
int i = 0;
int interfaceClass;
//iterate over the devices retrieved
while ( (device = devices[i++]) != NULL )
{
//Variable Declaration
struct libusb_device_descriptor descriptor;
struct libusb_config_descriptor* config;
const struct libusb_interface* interface;
const struct libusb_interface_descriptor* interfaceDescriptor;
struct deviceStruct devStrc;
int result;
//Convert method to type
libusb_get_device_descriptor_t _libusb_get_device_descriptor = (libusb_get_device_descriptor_t) allLibs[libusb].functions[libusb_get_device_descriptor_e].funcAddr;
//Grab description for device
result = _libusb_get_device_descriptor(device, &descriptor);
//Check for errors
if (result < 0)
{
//build device
devStrc = buildBlankDevice();
//place deviceStruct in the back of the devices list
detectedDevices.insert(detectedDevices.end(), devStrc);
//Do nothing else for this device
continue;
}//END if error
//convert method to type
libusb_get_active_config_descriptor_t _libusb_get_active_config_descriptor = (libusb_get_active_config_descriptor_t) allLibs[libusb].functions[libusb_get_active_config_descriptor_e].funcAddr;
//get config descriptor
result = _libusb_get_active_config_descriptor(device, &config);
//check for errors
if (result < 0)
{
//build device
devStrc = buildUsbDevice(device, descriptor);
//place deviceStruct in the back of the devices list
detectedDevices.insert(detectedDevices.end(), devStrc);
//do nothing else for this device
continue;
}
//get interface class
interface = config->interface;
interfaceDescriptor = interface->altsetting;
interfaceClass = interfaceDescriptor->bInterfaceClass;
//set fields of deviceStruct via descriptor
devStrc = buildUsbDevice(device, descriptor, interfaceClass);
//place deviceStruct in the back of the devices list
detectedDevices.insert(detectedDevices.end(), devStrc);
}//END while
//convert method to type
libusb_free_device_list_t _libusb_free_device_list = (libusb_free_device_list_t) allLibs[libusb].functions[libusb_free_device_list_e].funcAddr;
//free the list
_libusb_free_device_list(devices, 1);
//grab the lsusb output (if available)
//Variable Declaration
char buffer[8192];
const char* consoleCommand = "lsusb";
//open the pipe
FILE* pipe = popen(consoleCommand, "r");
//Check for errors
if(!pipe)
{
//close pipe
pclose(pipe);
//pipe command failed, we're done here
return;
}
//read into the buffer
size_t bytes_read = fread (buffer, 1, sizeof(buffer), pipe);
//close pipe
pclose(pipe);
if(bytes_read <= 0)
{
//No data, bail
return;
}
//Convert buffer into strings
//Credit to Component 10, Jamie Bullock & billz @ stackoverflow for partial method code
std::string line;
std::vector <std::string> lines;
std::stringstream ss(buffer);
if(buffer != NULL)
{
//grab each line
while ( std::getline(ss,line,'\n') )
{
lines.push_back(line);
}//END inner while
}//END if buffer exists
//parse result for every device we found so far
while(!detectedDevices.empty())
{
//Build string and convert it for strstr checking
//Credit to Benoit @ stackoverflow for partial method code
ostringstream oss;
oss << hex<< setfill('0')<<setw(4)<<detectedDevices.front().vendorID;
std::string spotStr = oss.str();
oss << dec;
spotStr = spotStr + ":" + detectedDevices.front().id_string;
const char* spot = spotStr.c_str();
char* match = strstr(buffer, spot);
//Check if match exists
if (match != NULL)
{
//For storing the name ripped out of the buffer
char englishName[bytes_read];
int dontcare;
int dontcare2;
//Search buffer and rip name
sscanf(match, "%x:%x %[^\t\n]", &dontcare, &dontcare2, englishName);
detectedDevices.front().name = englishName;
}//END if match is null
//set udev number for devices
//grab the first line
int deviceNumber;
int busNumber;
int vendorID;
int deviceID;
char deviceName[detectedDevices.front().name.size()];
sscanf(lines.begin()->c_str(), "Bus %d Device %d: ID %x:%x %[^\t\n]", &busNumber, &deviceNumber, &vendorID, &deviceID, deviceName);
lines.erase(lines.begin());
detectedDevices.front().udev_deviceNumber = deviceNumber;
//empty list and move contents to cylon's list
et.detectedDevices.push_back(detectedDevices.front());
detectedDevices.pop_front();
}//END WHILE
//convert method to type
libusb_exit_t _libusb_exit = (libusb_exit_t) allLibs[libusb].functions[libusb_exit_e].funcAddr;
//exit from lib
_libusb_exit(context);
usbInitialized = false;
}//END produce USB device info
void produceDisplayInfo(struct cylonStruct& et)
{
if (!allLibs[libsdl].opened || !sdlInitialized)
{
//Get out, this method won't work without libsdl loaded!
return;
}
//Cast functions
SDL_GetNumVideoDisplays_t _SDL_GetNumVideoDisplays = (SDL_GetNumVideoDisplays_t) allLibs[libsdl].functions[SDL_GetNumVideoDisplays_e].funcAddr;
SDL_GetDisplayName_t _SDL_GetDisplayName = (SDL_GetDisplayName_t) allLibs[libsdl].functions[SDL_GetDisplayName_e].funcAddr;
//Get the number of display devices attached
int displayCount = _SDL_GetNumVideoDisplays();
if(displayCount <= 0)
{
//no display devices detected
return;
}
for(int i = 0; i < displayCount; i++)
{
//Build structs
const char* displayName = _SDL_GetDisplayName(i);
struct deviceStruct device = buildDisplayDevice(displayName, i);
struct displayStruct display = buildDisplay(device, i);
//Add to lists
et.displayDevices.push_back(display);
device.displayIndex = et.displayDevices.size() - 1;
et.detectedDevices.push_back(device);
et.displayDevices.back().superDevice = et.detectedDevices.back();
}//END for all displays
}//END producer
void produceStorageInfo(struct cylonStruct& et)
{
//MEDIA/ DEVICES:
if(et.username.empty())
{
//no username set
return;
}
//credit to jtshaw @ linuxquestions.org for partial method code
//grab storage names
std::string directory_string = "/media/" + et.username;
vector<string> files = vector<string>();
DIR* dp;
struct dirent* dirp;
const char* currentDir = ".";
const char* prevDir = "..";
//attempt to open directory
if( (dp = opendir(directory_string.c_str())) == NULL )
{
//do nothing
}
else
{
//go through all the files in the directory
while( (dirp = readdir(dp)) != NULL)
{
//inspect name
bool isStorageDrive = true;
if ( (strcmp(dirp->d_name, prevDir) == 0) || (strcmp(dirp->d_name, currentDir) == 0) )
{
isStorageDrive = false;
}//END if storageDrive
//make sure the files we read are directories and not regular files/the current directory/the parent directory
if((dirp->d_type == DT_DIR) && isStorageDrive)
{
//grab the folder info
std::string dirName_string = directory_string + "/" + dirp->d_name;
const char* dirName_char = dirName_string.c_str();
struct statvfs buf;
int result = statvfs(dirName_char, &buf);
//if successfully opened
if(result == 0)
{
//grab storage specs
uint64_t freeSpace = (uint64_t)(buf.f_bsize * buf.f_bfree);
uint64_t totalSpace = (uint64_t)(buf.f_bsize * buf.f_blocks);
//build structs and store them in lists
struct deviceStruct device = buildStorageDevice(dirp->d_name);
struct storageStruct storage = buildStorage(device, dirName_string, freeSpace, totalSpace);
//Add to lists
et.storages.push_back(storage);
device.storageIndex = et.storages.size() - 1;
et.detectedDevices.push_back(device);
et.storages.back().superDevice = et.detectedDevices.back();
}//END if successfully opened
}//END if is folder and storage drive
}//END while
//close the directory
closedir(dp);
}//END if directory open attempt successful
//END MEDIA/ DEVICES
//Try to open main user directory (the likely place you'll be reading/writing from on a per-user basis)
directory_string = "/home/" + et.username;
//attempt to open directory
if( (dp = opendir(directory_string.c_str())) == NULL )
{
//do nothing
}
else
{
while( (dirp = readdir(dp)) != NULL)
{
//inspect name for "."
if ( (strcmp(dirp->d_name, currentDir) == 0) )
{
const char* dirName_char = directory_string.c_str();
struct statvfs buf;
int result = statvfs(dirName_char, &buf);
//if successfully opened
if(result == 0)
{
//grab storage specs
uint64_t freeSpace = (uint64_t)(buf.f_bsize * buf.f_bfree);
uint64_t totalSpace = (uint64_t)(buf.f_bsize * buf.f_blocks);
//build structs and store them in lists
struct deviceStruct device = buildStorageDevice(et.username);
struct storageStruct storage = buildStorage(device, directory_string, freeSpace, totalSpace);
//Add to lists
et.storages.push_back(storage);
device.storageIndex = et.storages.size() - 1;
et.detectedDevices.push_back(device);
et.storages.back().superDevice = et.detectedDevices.back();
}//END if successfully opened
}//END if user directory
}//END while
//close the directory
closedir(dp);
}//END if user dir opened
//END user directory
//Add gfvs MTP devices e.g. mtp://[usb:002,013]/
//Grab effective user ID
//NOTE: should this be Real user ID? change to geteuid
uid_t euid = getuid();
//build gvfs directory string
ostringstream oss;
oss<<"/run/user/"<<euid<<"/gvfs";
directory_string = oss.str();
//attempt to open directory
if( (dp = opendir(directory_string.c_str())) == NULL )
{
//do nothing
}