-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
X32GetLib.c
1305 lines (1251 loc) · 40.4 KB
/
X32GetLib.c
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
/*
* X32GetLib.c
*
* Created on: 23 avr. 2015
*
* ©2015-17 - Patrick-Gilles Maillot
*
* X32GetLib - a windows app for saving a Preset Library, or Several Preset files
* from X32 memory to a PC HDD.
*
* ver. 0.20: Introduced the possibility to save ALL libraries, not just type by type
* ver. 0.21: fixed logic issue and timing limit to remove -error- signature
* ver. 0.22: eliminated race condition with sleep 30ms on preset loading in the desk
* ver. 0.23: preventing window resizing & support for 3.08FW
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/time.h>
#include <time.h>
#include <winsock2.h>
#include <windows.h>
#include <Commdlg.h>
#include <WinError.h>
#define BSIZE 512 // Buffer sizes (enough to take into account FX parameters)
#define LINEFEED 10
#define LENPATH 256 // maximum length for directory names
#define LENFILE 64 // maximum length for file names
#define LENFULLPATH LENPATH + LENFILE + 10 //that's 256+64 + a little extra for extension
#define PS_CH 1
#define PS_FX 2
#define PS_RO 4
// Private functions
void LaunchGetLib(); // return status is in PSstatus
void XRcvClean();
void Xlogf(char *header, char *buf, int len);
void Xsave(int Type);
//
// External calls used
extern int Xsprint(char *bd, int index, char format, void *bs);
extern int Xfprint(char *bd, int index, char* text, char format, void *bs);
extern int X32Connect(int Xconnected, char* Xip_str, int btime);
extern int validateIP4Dotted(const char *s);
//
//
char *PComList[] = {"ch/01/config",
"ch/01/delay",
"ch/01/preamp",
"ch/01/gate",
"ch/01/gate/filter",
"ch/01/dyn",
"ch/01/dyn/filter",
"ch/01/eq",
"ch/01/eq/1",
"ch/01/eq/2",
"ch/01/eq/3",
"ch/01/eq/4",
"ch/01/mix",
"ch/01/mix/01",
"ch/01/mix/02",
"ch/01/mix/03",
"ch/01/mix/04",
"ch/01/mix/05",
"ch/01/mix/06",
"ch/01/mix/07",
"ch/01/mix/08",
"ch/01/mix/09",
"ch/01/mix/10",
"ch/01/mix/11",
"ch/01/mix/12",
"ch/01/mix/13",
"ch/01/mix/14",
"ch/01/mix/15",
"ch/01/mix/16"};
//
#define PCom_max (sizeof (PComList) / sizeof (char*))
//
char *PCfxList[] = {"fx/1/type", "fx/1/source", "fx/1/par"};
#define PCfx_max (sizeof (PCfxList) / sizeof (char*))
//
char *PCroList[] = {"config/routing/IN",
"config/routing/AES50A",
"config/routing/AES50B",
"config/routing/CARD",
"config/routing/OUT",
"config/routing/PLAY",
"outputs/main/01",
"outputs/main/01/delay",
"outputs/main/02",
"outputs/main/02/delay",
"outputs/main/03",
"outputs/main/03/delay",
"outputs/main/04",
"outputs/main/04/delay",
"outputs/main/05",
"outputs/main/05/delay",
"outputs/main/06",
"outputs/main/06/delay",
"outputs/main/07",
"outputs/main/07/delay",
"outputs/main/08",
"outputs/main/08/delay",
"outputs/main/09",
"outputs/main/09/delay",
"outputs/main/10",
"outputs/main/10/delay",
"outputs/main/11",
"outputs/main/11/delay",
"outputs/main/12",
"outputs/main/12/delay",
"outputs/main/13",
"outputs/main/13/delay",
"outputs/main/14",
"outputs/main/14/delay",
"outputs/main/15",
"outputs/main/15/delay",
"outputs/main/16",
"outputs/main/16/delay",
"outputs/aux/01",
"outputs/aux/02",
"outputs/aux/03",
"outputs/aux/04",
"outputs/aux/05",
"outputs/aux/06",
"outputs/p16/01",
"outputs/p16/01/iQ",
"outputs/p16/02",
"outputs/p16/02/iQ",
"outputs/p16/03",
"outputs/p16/03/iQ",
"outputs/p16/04",
"outputs/p16/04/iQ",
"outputs/p16/05",
"outputs/p16/05/iQ",
"outputs/p16/06",
"outputs/p16/06/iQ",
"outputs/p16/07",
"outputs/p16/07/iQ",
"outputs/p16/08",
"outputs/p16/08/iQ",
"outputs/p16/09",
"outputs/p16/09/iQ",
"outputs/p16/10",
"outputs/p16/10/iQ",
"outputs/p16/11",
"outputs/p16/11/iQ",
"outputs/p16/12",
"outputs/p16/12/iQ",
"outputs/p16/13",
"outputs/p16/13/iQ",
"outputs/p16/14",
"outputs/p16/14/iQ",
"outputs/p16/15",
"outputs/p16/15/iQ",
"outputs/p16/16",
"outputs/p16/16/iQ",
"outputs/aes/01",
"outputs/aes/02"
};
#define PCro_max (sizeof (PCroList) / sizeof (char*))
//
WINBASEAPI HWND WINAPI GetConsoleWindow(VOID);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInstance = 0;
HWND hwnd, hwndipaddr, hwndconx, hwndGO, hwndprog;
HWND hwndPtyp, hwndverb, hwnddely, hwndsaveto;
RECT Rect;
HFONT hfont, htmp;
HDC hdc, hdcMem;
PAINTSTRUCT ps;
HBITMAP hBmp;
BITMAP bmp;
MSG wMsg;
OPENFILENAME ofn; // common dialog box structure
HANDLE hf; // file handle
FILE *log_file;
FILE *Pfile_pt;
int delta_scene;
char Finipath[1024]; // resolved path to .ini file
char **FinilppPart;
int Finiretval;
int wWidth = 545;
int wHeight = 135;
int zero = 0;
int one = 1;
int sixtythree = 63;
int ninetynine = 99;
int GetLib_request = 0;
//
// Windows UI related storage
char Xip_str[20], Xdelay_str[8];
char Xlogpath[LENPATH], XPresetLibPath[LENPATH], XPresetFile[LENFULLPATH];
char Pfilename[32];
char Xprg_str[32];
char Xcomplete[] = "Complete";
char Xready[] = "Ready";
char Xerror[] = "--Error--";
char XPname[32];
char PSBuffer[BSIZE];
char PFlags[16];
//
// Misc. flags
int Xconnected = 0;
int X32SHOW = 1; // this flags reorients verbose mode to log file rather than stdout
int X32PRESET = 1; // this flags reorients /ch/xx/config to not consider source
int Xdelay = 0; // typically not needed or low value for presets
int Xdebug = 0;
int Xfiles = 0; // Flag to indicate if files are selected or not
int Xverbose = 0; // Verbose
int PSstatus = 0; // Return status (1 = error) after reading preset file
int Xlock = 0;
int keep_running = 1;
int PsetType = 0; // Type of Preset. 0-ALL, 1-Channel, 2-Effects, 3-Routing
int Phasdata = 0;
int PSselidx = 0;
int fx[8]; // saves the FX current type for each of the fx slots
enum PSetType {ALL = 0, CHANNEL, EFFECTS, ROUTING, LAST};
//
// X32 communication buffers
char r_buf[BSIZE], s_buf[BSIZE];
int r_len, s_len;
int p_status;
//
// UDP related data
WSADATA wsa; // Windows Sockets
int Xfd; // X32 socket
struct sockaddr_in Xip; // IP structure
struct sockaddr *Xip_addr = (struct sockaddr*) &Xip;
struct timeval timeout; // timeout for non blocking udp comm
fd_set ufds; // file descriptor
int Xip_len = sizeof(Xip); // length of addresses
//
// resource file data
FILE *res_file;
int r_status;
//
//
//
// type cast union
union littlebig {
char cc[4];
int ii;
float ff;
} endian;
//
//
#define WIN_LOOP \
do { \
while(PeekMessage(&wMsg, NULL, 0, 0, PM_REMOVE)) { \
TranslateMessage(&wMsg); \
DispatchMessage(&wMsg); \
} \
} while (0);
//
// X32 Communication macros
//
#define SEND_TO(b, l) \
do { \
if (Xverbose) Xlogf("->X", b, l); \
if (sendto(Xfd, b, l, 0, Xip_addr, Xip_len) < 0) { \
fprintf (log_file, "Coundn't send data to X32\r\n"); \
exit(EXIT_FAILURE); \
} \
if (Xdelay > 0) MILLISLEEP(Xdelay); \
} while (0);
//
//
#define RECV_FR(b, l) \
do { \
if ((l = recvfrom(Xfd, b, BSIZE, 0, 0, 0)) > 0) { \
if (Xverbose) Xlogf("X->", b, l); \
} \
} while (0);
//
//
//
#define RPOLL \
do { \
FD_ZERO (&ufds); \
FD_SET (Xfd, &ufds); \
p_status = select(Xfd,&ufds,NULL,NULL,&timeout); \
} while (0);
#define MILLISLEEP(t) \
do { \
Sleep(t); \
} while (0);
//
//
//
//
//
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) {
WNDCLASSW wc = { 0 };
wc.lpszClassName = L"X32GetLib";
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClassW(&wc);
CreateWindowW(wc.lpszClassName, L"X32GetLib - Get All Presets from X32 ",
WS_OVERLAPPED | WS_VISIBLE | WS_MINIMIZEBOX | WS_SYSMENU,
100, 220, wWidth, wHeight, 0, 0, hInstance, 0);
while (keep_running) {
if(PeekMessage(&wMsg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&wMsg);
DispatchMessage(&wMsg);
} else {
if (GetLib_request) {
XRcvClean();
LaunchGetLib();
if (PSstatus) {
SetWindowText(hwndprog, (LPSTR)Xerror);
fprintf (log_file, "Done transferXPresetFilePath, with errors or warnings\r\n");
} else {
SetWindowText(hwndprog, (LPSTR)Xcomplete);
fprintf (log_file, "Done transfering presets!\r\n");
}
XRcvClean();
if (log_file) fflush(log_file);
GetLib_request = 0;
} else {
Sleep (100);
}
}
}
return (int) wMsg.wParam;
}
//
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
int i;
char str1[LENFILE];
switch (msg) {
case WM_CREATE:
//
hwndipaddr = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
125, 40, 170, 20, hwnd, (HMENU)0, NULL, NULL);
hwndconx = CreateWindowW(L"button", L"Connect", WS_VISIBLE | WS_CHILD,
305, 40, 75, 20, hwnd, (HMENU)1, NULL, NULL);
hwnddely = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
205, 75, 25, 20, hwnd, (HMENU)0, NULL, NULL);
hwndverb = CreateWindowW(L"button", L"", BS_CHECKBOX | WS_VISIBLE | WS_CHILD,
125, 75, 15, 20, hwnd, (HMENU)2, NULL, NULL);
hwndsaveto = CreateWindowW(L"button", L"Save To", WS_VISIBLE | WS_CHILD,
305, 75, 75, 20, hwnd, (HMENU)8, NULL, NULL);
hwndPtyp = CreateWindowW(L"COMBOBOX", NULL, CBS_DROPDOWN | WS_CHILD | WS_VISIBLE,
440, 7, 92, 90, hwnd, (HMENU)7, NULL, NULL);
// Load dropdown item list
SendMessage(hwndPtyp, CB_ADDSTRING,(WPARAM) 0,(LPARAM)"ALL");
SendMessage(hwndPtyp, CB_ADDSTRING,(WPARAM) 0,(LPARAM)"Channel");
SendMessage(hwndPtyp, CB_ADDSTRING,(WPARAM) 0,(LPARAM)"Effects");
SendMessage(hwndPtyp, CB_ADDSTRING,(WPARAM) 0,(LPARAM)"Routing");
// SEt Default value
SendMessage(hwndPtyp, CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
hwndGO = CreateWindowW(L"button", L"Xfer Presets", WS_VISIBLE | WS_CHILD,
395, 30, 135, 43, hwnd, (HMENU)9, NULL, NULL);
hwndprog = CreateWindowW(L"Edit", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
395, 75, 135, 20, hwnd, (HMENU)0, NULL, NULL);
hBmp = (HBITMAP)LoadImage(NULL,(LPCTSTR)"./.X32GetLib.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_SHARED);
if(hBmp==NULL) {
perror("Pixel bitmap file no found");
}
break;
//
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBmp);
BitBlt(hdc, 5, 2, 115, 100, hdcMem, 0, 0, SRCCOPY);
DeleteDC(hdcMem);
SetBkMode(hdc, TRANSPARENT);
hfont = CreateFont(14, 0, 0, 0, FW_LIGHT, 0, 0, 0,
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY, VARIABLE_PITCH, TEXT("Arial"));
htmp = (HFONT) SelectObject(hdc, hfont);
TextOut(hdc, 150, 25, str1, wsprintf(str1, "Enter X32 IP below:"));
TextOut(hdc, 335, 18, str1, wsprintf(str1, "ver. 0.23"));
DeleteObject(htmp);
DeleteObject(hfont);
//
hfont = CreateFont(16, 0, 0, 0, FW_REGULAR, 0, 0, 0,
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY, VARIABLE_PITCH, TEXT("Arial"));
htmp = (HFONT) SelectObject(hdc, hfont);
TextOut(hdc, 125, 0, str1, wsprintf(str1, "X32GetLib - ©2015 - Patrick-Gilles Maillot"));
DeleteObject(htmp);
DeleteObject(hfont);
hfont = CreateFont(15, 0, 0, 0, FW_MEDIUM, 0, 0, 0,
DEFAULT_CHARSET, OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY, VARIABLE_PITCH, TEXT("Arial"));
htmp = (HFONT) SelectObject(hdc, hfont);
TextOut(hdc, 395, 10, str1, wsprintf(str1, "Library:"));
TextOut(hdc, 140, 77, str1, wsprintf(str1, "Verbose"));
TextOut(hdc, 235, 77, str1, wsprintf(str1, "OSC delay"));
DeleteObject(htmp);
DeleteObject(hfont);
EndPaint(hwnd, &ps);
sprintf(Xdelay_str, "%d", Xdelay);
SetWindowText(hwnddely, (LPSTR)Xdelay_str);
SetWindowText(hwndipaddr, (LPSTR)Xip_str);
if (Xverbose) SendMessage(hwndverb, BM_SETCHECK, BST_CHECKED, 0);
else SendMessage(hwndverb, BM_SETCHECK, BST_UNCHECKED, 0);
break;
//
case WM_COMMAND:
if(HIWORD(wParam) == CBN_DROPDOWN) { // user action = dropdown
SendMessage((HWND)lParam, CB_SHOWDROPDOWN, (WPARAM) 0, (LPARAM) 0);
} else if (HIWORD(wParam) == BN_CLICKED) { // user action
i = LOWORD(wParam);
switch(i) {
case 1:
GetWindowText(hwndipaddr, Xip_str, GetWindowTextLength(hwndipaddr) + 1);
if (validateIP4Dotted(Xip_str)) {
Xconnected = X32Connect(Xconnected, Xip_str, 100000);
if (Xconnected) SetWindowTextW(hwndconx, L"Connected");
else SetWindowTextW(hwndconx, L"Connect");
} else {
fprintf (log_file, "Incorrect IP string form\r\n");
}
break;
case 2:
if (Xverbose)SendMessage(hwndverb, BM_SETCHECK, BST_UNCHECKED, 0);
else SendMessage(hwndverb, BM_SETCHECK, BST_CHECKED, 0);
Xverbose ^= 1;
break;
case 8:
Xfiles = 0;
strcpy(XPresetLibPath, "Filename will be ignored");
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = XPresetLibPath;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(XPresetLibPath);
ofn.lpstrFilter = NULL;
ofn.nFilterIndex = 0;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrTitle = (LPCTSTR)"Save Preset Library to directory selected below\0";
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_EXPLORER | OFN_CREATEPROMPT |
OFN_HIDEREADONLY | OFN_NOVALIDATE |
OFN_PATHMUSTEXIST | OFN_READONLY;
//
// Display the Open dialog box.
if (GetSaveFileName(&ofn)) {
// remove filename from returned path so we can either save to
// existing directory or newly created one
i = strlen(XPresetLibPath);
while (i && (XPresetLibPath[i] != '\\')) --i;
XPresetLibPath[i] = 0;
// printf("Path: %s\n", XPresetLibPath);
// fflush(stdout);
Xfiles = 1;
SetWindowText(hwndprog, (LPSTR)Xready); // display ready status
MessageBox(NULL, XPresetLibPath, "Destination directory: ", MB_OK);
}
break;
case 9:
// GetLib request
GetLib_request = 1;
break;
}
}
break;
//
case WM_DESTROY:
if (Xconnected) close (Xfd);
// read panel data
GetWindowText(hwndipaddr, Xip_str, GetWindowTextLength(hwndipaddr) + 1);
GetWindowText(hwnddely, Xdelay_str, GetWindowTextLength(hwnddely) + 1);
sscanf(Xdelay_str, "%d", &Xdelay);
res_file = fopen(Finipath, "wb");
GetWindowRect(hwnd, &Rect);
fprintf(res_file, "%d %d %d %d\n",(int)(Rect.right - Rect.left), (int)(Rect.bottom - Rect.top), Xverbose, Xdelay);
fprintf(res_file, "%s\n", Xip_str);
fprintf(res_file, "%s\n", Xlogpath);
fclose (res_file);
if (log_file) fclose(log_file);
keep_running = 0;
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
//
//
// Private functions:
//
//
// Empty any pending message from X32 function
//
void XRcvClean() {
//
if (Xverbose) fprintf (log_file, "X32 receive buffer cleanup if needed\r\n");
do {
RPOLL;
if (p_status > 0) {
RECV_FR(r_buf, r_len);
}
} while (p_status > 0);
return;
}
//
//
//
void Xlogf(char *header, char *buf, int len)
{
int i, k, n, j, l, comma = 0, data = 0, dtc = 0;
unsigned char c;
fprintf (log_file, "%s, %4d B: ", header, len);
for (i = 0; i < len; i++) {
c = (unsigned char)buf[i];
if (c < 32 || c == 127 || c == 255 ) c = '~'; // Manage unprintable chars
fprintf (log_file, "%c", c);
if (c == ',') {
comma = i;
dtc = 1;
}
if (dtc && (buf[i] == 0)) {
data = (i + 4) & ~3;
for (dtc = i + 1; dtc < data ; dtc++) {
if (dtc < len) {
fprintf (log_file, "~");
}
}
dtc = 0;
l = data;
while (++comma < l && data < len) {
switch (buf[comma]) {
case 's':
k = (strlen((char*)(buf+data)) + 4) & ~3;
for (j = 0; j < k; j++) {
if (data < len) {
c = (unsigned char)buf[data++];
if (c < 32 || c == 127 || c == 255 ) c = '~'; // Manage unprintable chars
fprintf (log_file, "%c", c);
}
}
break;
case 'i':
for (k = 4; k > 0; endian.cc[--k] = buf[data++]);
fprintf (log_file, "[%6d]", endian.ii);
break;
case 'f':
for (k = 4; k > 0; endian.cc[--k] = buf[data++]);
if (endian.ff < 10.) fprintf (log_file, "[%06.4f]", endian.ff);
else if (endian.ff < 100.) fprintf (log_file, "[%06.3f]", endian.ff);
else if (endian.ff < 1000.) fprintf (log_file, "[%06.2f]", endian.ff);
else if (endian.ff < 10000.) fprintf (log_file, "[%06.1f]", endian.ff);
break;
case 'b':
// Get the number of bytes
for (k = 4; k > 0; endian.cc[--k] = buf[data++]);
n = endian.ii;
// Get the number of data (floats or ints ???) in little-endian format
for (k = 0; k < 4; endian.cc[k++] = buf[data++]);
if (n == endian.ii) {
// Display blob as string
fprintf (log_file, "%d chrs: ", n);
for (j = 0; j < n; j++) fprintf (log_file, "%c ", buf[data++]);
} else {
// Display blob as floats
n = endian.ii;
fprintf (log_file, "%d flts: ", n);
for (j = 0; j < n; j++) {
//floats are little-endian format
for (k = 0; k < 4; endian.cc[k++] = buf[data++]);
fprintf (log_file, "%06.2f ", endian.ff);
}
}
break;
default:
break;
}
}
i = data - 1;
}
}
fprintf (log_file, "\r\n");
}
int
main(int argc, char **argv)
{
HINSTANCE hPrevInstance = 0;
PWSTR pCmdLine = 0;
int nCmdShow = 0;
Xverbose = 0;
strcpy(Xlogpath, ".");
Xip_str[0] = 0;
XPresetLibPath[0] = 0;
// load resource file
if ((res_file = fopen("./.X32GetLib.ini", "r")) != NULL) {
// get and remember real path
if((Finiretval = GetFullPathNameA("./.X32GetLib.ini", 1024, Finipath, FinilppPart)) > 1024){
printf ("Not enough space for file name\n");
}
fscanf(res_file, "%d %d %d %d\n",
&wWidth, &wHeight, &Xverbose, &Xdelay);
fgets(Xip_str, sizeof(Xip_str), res_file);
Xip_str[strlen(Xip_str) - 1] = 0;
fgets(Xlogpath, sizeof(Xlogpath), res_file);
Xlogpath[strlen(Xlogpath) - 1] = 0;
fclose (res_file);
}
if ((p_status = CreateDirectory(Xlogpath, NULL)) == 0) {
if (ERROR_ALREADY_EXISTS != GetLastError()) {
printf ("Cannot create directory\n");
Sleep(10000);
return (0);
}
}
strcpy(r_buf, Xlogpath);
strcat(r_buf, "/.X32GetLib.log");
// create logfile and run program
if ((log_file = fopen(r_buf, "wb")) != NULL) {
fprintf(log_file,"*\r\n*\r\n");
fprintf(log_file,"* X32GetLib Log data - ©2015 - Patrick-Gilles Maillot\r\n");
fprintf(log_file,"*\r\n*\r\n");
if (Finiretval) fprintf(log_file, ".ini file path: %s\r\n", Finipath);
else fprintf(log_file, "couldn't open .X32GetLib.ini file\r\n");
ShowWindow(GetConsoleWindow(), SW_HIDE); // Hide console window
wWinMain(hInstance, hPrevInstance, pCmdLine, nCmdShow);
} else {
printf ("Cannot create log file\n");
Sleep(10000);
}
if (log_file) fclose(log_file);
return 0;
}
//
//
void LaunchGetLib() {
int i;
float stfader, mfader;
int showctrl, prepos_cur;
if (!Xconnected) {
fprintf(log_file, "Error: Not connected to X32\r\n");
PSstatus = 1;
return;
}
if (!Xfiles) {
fprintf(log_file, "Error: No destination directory\r\n");
PSstatus = 1;
return;
}
PSstatus = 0;
// read panel data
GetWindowText(hwnddely, Xdelay_str, GetWindowTextLength(hwnddely) + 1);
sscanf(Xdelay_str, "%d", &Xdelay);
PsetType = SendMessage(hwndPtyp, CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
fprintf (log_file,"Lib Type: %d, Save to: %s\n", PsetType, XPresetLibPath);
fprintf (log_file, "Saving X32 State\r\n");
fflush(log_file);
// Save Master levels (L/R and M/C)
s_len = Xsprint(s_buf, 0, 's', "/main/st/mix/fader");
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /main/st/mix/fader...[int]
if (strncmp(r_buf, "/main/st/mix/fader", 18) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[24 + i];
stfader = endian.ff;
} else {
fprintf (log_file, "Warning: expected /main/st/mix/fader, received %s\r\n", r_buf);
PSstatus = 1;
}
}
s_len = Xsprint(s_buf, 0, 's', "/main/m/mix/fader");
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /main/m/mix/fader...[int]
if (strncmp(r_buf, "/main/m/mix/fader", 17) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[24 + i];
mfader = endian.ff;
} else {
fprintf (log_file, "Warning: expected /main/m/mix/fader, received %s\r\n", r_buf);
PSstatus = 1;
}
}
// set master levels to 0. (-oo)
s_len = Xfprint(s_buf, 0, "/main/st/mix/fader", 'f', &zero);
SEND_TO(s_buf, s_len)
s_len = Xfprint(s_buf, 0, "/main/m/mix/fader", 'f', &zero);
SEND_TO(s_buf, s_len)
// Save selected channel strip select
s_len = Xsprint(s_buf, 0, 's', "/-stat/selidx");
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /-stat/selidx...[int]
if (strncmp(r_buf, "/-stat/selidx", 13) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[20 + i];
PSselidx = endian.ii;
} else {
fprintf (log_file, "Warning: expected -/stat/selidx, received %s\r\n", r_buf);
PSstatus = 1;
}
}
// Select Channel 1 (idx = 0)
s_len = Xfprint(s_buf, 0, "/-stat/selidx", 'i' , &zero);
SEND_TO(s_buf, s_len)
// Save scene pointer: Get Show_control value and set to 1
s_len = Xsprint(s_buf, 0, 's', "/-prefs/show_control");
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /-prefs/show_control...[int]
if (strncmp(r_buf, "/-prefs/show_control", 20) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[28 + i];
showctrl = endian.ii;
} else {
fprintf (log_file, "Warning: expected /-prefs/show_control, received %s\r\n", r_buf);
PSstatus = 1;
}
}
s_len = Xfprint(s_buf, 0, "/-prefs/show_control", 'i', &one);
SEND_TO(s_buf, s_len)
// Save scene pointer: Get scene pointer value
s_len = Xsprint(s_buf, 0, 's', "/-show/prepos/current");
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /-show/prepos/current...[int]
if (strncmp(r_buf, "/-show/prepos/current", 21) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[28 + i];
prepos_cur = endian.ii;
} else {
fprintf (log_file, "Warning: expected /-show/prepos/current, received %s\r\n", r_buf);
PSstatus = 1;
}
}
// Save state
if (1) {
// save state to scene 99
fprintf (log_file, "Notice: Saving state to scene 99 with Name: PG Maillot, Type: X32GetLib\r\n");
// Save Scene at requested location (iD)
s_len = Xsprint(s_buf, 0, 's', "/save");
s_len = Xsprint(s_buf, s_len, 's', ",sissi");
s_len = Xsprint(s_buf, s_len, 's', "scene");
s_len = Xsprint(s_buf, s_len, 'i', &ninetynine);
s_len = Xsprint(s_buf, s_len, 's', "PG Maillot");
s_len = Xsprint(s_buf, s_len, 's', "X32GetLib");
s_len = Xsprint(s_buf, s_len, 'i', &zero);
SEND_TO(s_buf, s_len)
// Wait for save to complete
Xlock = 1;
while (Xlock) {
RPOLL; // read the desk for answer to /-show/showfile/scene/099/name
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /save~~~,si~scene~~~[ 1]
if (strncmp(r_buf, "/save", 5) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[20 + i];
if (endian.ii != 1) {
fprintf (log_file, "Error: Could not save state to scene 99\r\n");
PSstatus = 1;
return;
}
Xlock = 0;
} else {
PSstatus = 1;
fprintf (log_file, "Warning: Expected /save... received: %s\r\n", r_buf);
}
}
}
}
//
// Create directory if needed
if ((p_status = CreateDirectory(XPresetLibPath, NULL)) == 0) {
if (ERROR_ALREADY_EXISTS != GetLastError()) {
fprintf (log_file, "Cannot create directory\r\n");
PSstatus = 1;
return;
}
}
//
// Should we save absolutely ALL?
if (PsetType == ALL) {
for (PsetType = CHANNEL; PsetType < LAST; PsetType++) {
Xsave(PsetType);
}
} else {
// Save the selected Type
Xsave(PsetType);
}
// Restore State
fprintf (log_file, "Restoring X32 State\r\n");
if (1) {
//restore scene 99 / general state
s_len = Xsprint(s_buf, 0, 's', "/load");
s_len = Xsprint(s_buf, s_len, 's', ",si");
s_len = Xsprint(s_buf, s_len, 's', "scene");
s_len = Xsprint(s_buf, s_len, 'i', &ninetynine);
SEND_TO(s_buf, s_len)
Xlock = 1;
while (Xlock) {
RPOLL; // read the desk for answer to /load
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /load~~~,si~scene~~~[ 1]
if (strncmp(r_buf, "/load", 5) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[20 + i];
if (endian.ii != 1) {
fprintf (log_file, "Error: Could not restore state from scene 99\r\n");
PSstatus = 1;
}
Xlock = 0;
} else {
PSstatus = 1;
fprintf (log_file, "Warning: Expected /load... received: %s\r\n", r_buf);
}
}
}
// Delete scene 99
s_len = Xsprint(s_buf, 0, 's', "/delete");
s_len = Xsprint(s_buf, s_len, 's', ",si");
s_len = Xsprint(s_buf, s_len, 's', "scene");
s_len = Xsprint(s_buf, s_len, 'i', &ninetynine);
SEND_TO(s_buf, s_len)
Xlock = 1;
while (Xlock) {
RPOLL; // read the desk for answer to /delete
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /delete~,si~scene~~~[ 1]
if (strncmp(r_buf, "/delete", 7) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[20 + i];
if (endian.ii != 1) {
fprintf (log_file, "Error: Could not delete scene 99\r\n");
PSstatus = 1;
}
Xlock = 0;
} else {
PSstatus = 1;
fprintf (log_file, "Warning: Expected /delete... received: %s\r\n", r_buf);
}
}
}
}
// restore scene pointer
s_len = Xfprint(s_buf, 0, "/-show/prepos/current", 'i', &prepos_cur);
SEND_TO(s_buf, s_len)
// restore show control
s_len = Xfprint(s_buf, 0, "/-prefs/show_control", 'i', &showctrl);
SEND_TO(s_buf, s_len)
// restore selected channel
s_len = Xfprint(s_buf, 0, "/-stat/selidx", 'i' , &PSselidx);
SEND_TO(s_buf, s_len)
// restore master levels
s_len = Xfprint(s_buf, 0, "/main/m/mix/fader", 'f', &mfader);
SEND_TO(s_buf, s_len)
s_len = Xfprint(s_buf, 0, "/main/st/mix/fader", 'f', &stfader);
SEND_TO(s_buf, s_len)
return;
}
void Xsave(int Type) {
int i, j;
int Pind;
char tmpstr[64];
//
tmpstr[0] = 0;
//
// For the directory where to save files
// Loop through Library to save files, one by one
switch (Type) {
case CHANNEL:
for (Pind = 1; Pind < 101; Pind++ ) {
// Does Preset 'Pind' have data?
sprintf(tmpstr, "/-libs/ch/%03d/hasdata", Pind);
s_len = Xsprint(s_buf, 0, 's', tmpstr); // channel preset has data?
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status > 0) { // ignore responses that do not correspond to what is
RECV_FR(r_buf, r_len); // expected: /-libs/ch/xxx/hasdata...[int]
if (strncmp(r_buf, "/-libs/ch", 9) == 0) {
for (i = 0; i < 4; i++) endian.cc[3 - i] = r_buf[28 + i];
Phasdata = endian.ii;
} else {
fprintf (log_file, "Warning: expected /-libs/ch/xxx/hasdata, received %s\r\n", r_buf);
PSstatus = 1;
break;
}
}
if (Phasdata) { // A valid preset, with data
s_len = Xsprint(s_buf, 0, 's', "/node"); // Get header with node command
s_len = Xsprint(s_buf, s_len, 's', ",s");
sprintf(tmpstr, "-libs/ch/%03d", Pind);
s_len = Xsprint(s_buf, s_len, 's', tmpstr); //
// Returned elements: pos, name, type, flags, hasdata
SEND_TO(s_buf, s_len)
// get reply
RPOLL; // read the desk for answer
if (p_status <= 0) { // ignore responses that do not correspond to what is
fprintf (log_file, "Error or Timeout on /-node command for Effect Preset number %d\r\n", Pind);
PSstatus = 1;
break;
} else {
RECV_FR(r_buf, r_len); // expected: node...
if (strncmp(r_buf, "node", 4) != 0) {
fprintf (log_file, "Warning: expected /node..., received %s\r\n", r_buf);
PSstatus = 1;
break;
}
}
// extract name from preset header
i = 0;
while ((r_buf[i] != '"') && (i < 128)) ++i;
++i; j = 0;
while ((r_buf[i] != '"') && (j < 16)) Pfilename[j++] = r_buf[i++];
Pfilename[j] = 0;
fprintf (log_file, "Processing Channel preset: %s\r\n", Pfilename);
// echo name in progress bar
SetWindowText(hwndprog, (LPSTR)Pfilename); // ... and display it
WIN_LOOP
// // extract flags from preset header
// while ((r_buf[i] != '%') && (i < 128)) ++i;
// ++i; j = 0;
// while (j < 16) PFlags[j++] = r_buf[i++];
//
// Create file name to save Preset data to
strcpy (XPresetFile, XPresetLibPath);
strcat (XPresetFile, "\\");
strcat (XPresetFile, Pfilename);
strcat (XPresetFile, ".chn");
if ((Pfile_pt = fopen(XPresetFile, "wb")) == 0) {
fprintf (log_file, "Error: Cannont create Channel Preset File %s\r\n", XPresetFile);
PSstatus = 1;
break;
}
// writing Preset File header
fprintf (Pfile_pt, "#2.1#");
fputs (r_buf + 25, Pfile_pt);
//
// Load the Preset