-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainFrm.cpp
More file actions
1075 lines (926 loc) · 37.1 KB
/
MainFrm.cpp
File metadata and controls
1075 lines (926 loc) · 37.1 KB
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
/*****************************************************************************/
/* MainFrm.cpp : implementation of the CMainFrame class */
/*****************************************************************************/
/*
Copyright (C) 2019 Hermann Seib
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "wxStd.h"
#if WITH_SPLASH
#include "Splash.h"
#endif
#include "blusb_gui.h"
#include "MainFrm.h"
#ifndef wxHAS_IMAGES_IN_RESOURCES
#include "res/Application.xpm"
#endif
/*===========================================================================*/
/* CMatrixPanel class members */
/*===========================================================================*/
/*****************************************************************************/
/* Layout : performa layout operations */
/*****************************************************************************/
bool CMatrixPanel::Layout()
{
wxSize szNew = pMatrix->GetMinSize();
if (szMatrix != szNew)
{
#if 0
SetMinSize(szNew); // propagate matrix's minimum size
Fit();
// TODO: replace the above with more elaborate moving/resizing once additional
// elements are put on this panel!
wxWindow *pMain = GetApp()->GetMain();
if (pMain && szMatrix.x)
{
wxSize szMain = pMain->GetSize();
if (szMain.x > 0 && szMain.y > 0)
{
szMain.x += (szNew.x - szMatrix.x);
szMain.y += (szNew.y - szMatrix.y);
pMain->SetSize(szMain);
pMain->Fit();
}
}
#endif
szMatrix = szNew;
}
return wxPanel::Layout();
}
/*===========================================================================*/
/* CKbdPanel class members */
/*===========================================================================*/
/*****************************************************************************/
/* CKbdPanel : constructor */
/*****************************************************************************/
CKbdPanel::CKbdPanel
(
wxWindow *parent,
wxWindowID winid,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString& name
)
: wxPanel(parent, winid, pos, size, style, name)
{
pKbd = new CKbdWnd(this, wxID_ANY, wxPoint(0,0),
wxSize(1, 1)); // keyboard sets that up
szKbd = pKbd->GetMinSize();
pKbd->CaptureAllKeys();
// We're not interested in the "released" state at the moment
SetKbdColour(CKbdWnd::ksReleased,
GetKbdColour(CKbdWnd::ksUnpressed));
SetKbdColour(CKbdWnd::ksReleased | CKbdWnd::ksHighlighted,
GetKbdColour(CKbdWnd::ksUnpressed | CKbdWnd::ksHighlighted));
SetSize(szKbd); // propagate keyboard's minimum size ATM
SetMinSize(szKbd); // propagate keyboard's minimum size ATM
}
/*****************************************************************************/
/* Layout : performs layout operations */
/*****************************************************************************/
bool CKbdPanel::Layout()
{
wxSize szKbdNew = pKbd->GetMinSize();
if (szKbd != szKbdNew)
{
SetMinSize(szKbdNew); // propagate keyboard's minimum size
Fit();
// TODO: replace the above with more elaborate moving/resizing once additional
// elements are put on this panel!
wxWindow *pMain = GetApp()->GetMain();
if (pMain)
{
wxSize szMain = pMain->GetSize();
if (szMain.x > 0 && szMain.y > 0)
{
szMain.x += (szKbdNew.x - szKbd.x);
szMain.y += (szKbdNew.y - szKbd.y);
pMain->SetSize(szMain);
pMain->Fit();
}
}
szKbd = szKbdNew;
}
return wxPanel::Layout();
}
// TODO:
// Setting the LEDs has to be done with SendInput() in Windows;
// there seems to be no equivalent in wxWidgets.
// So ... find out substitutions for the other platforms <sigh>
/*===========================================================================*/
/* CMainPanel class members */
/*===========================================================================*/
/*****************************************************************************/
/* CMainPanel : constructor */
/*****************************************************************************/
CMainPanel::CMainPanel(wxWindow *parent)
: wxPanel(parent, wxID_ANY)
{
wxStaticText *pStatic = new wxStaticText(this, wxID_ANY, wxT("Layers: "));
wxPoint pos = pStatic->GetPosition();
pos.x += 3;
pStatic->Move(pos);
pos.x += pStatic->GetSize().GetX();
wxArrayString a_1_maxLayer;
int nLayersMax = (!GetApp()->IsDevOpen() ||
GetApp()->GetFwVersion() >= 0x0105) ?
NUMLAYERS_MAX :
NUMLAYERS_MAX_OLD;
for (int i = 1; i <= nLayersMax; i++)
a_1_maxLayer.Add(wxString::Format(wxT("%d"), i));
wxSize sz(wxDefaultSize);
pLayers = new wxChoice(this, Blusb_LayerCount, pos, sz, a_1_maxLayer);
sz = pLayers->GetSize();
pLayers->Select(0);
pos = pStatic->GetPosition();
pos.y += (pLayers->GetSize().GetY() - pStatic->GetSize().GetY()) / 2;
pStatic->Move(pos);
sz = pLayers->GetSize(); // combobox scaled for 1 digit;
sz.Scale(1.3, 1.0); // scale up to ~3 digits
pKbd = NULL; /* keyboard panel not set up yet */
#ifdef _DEBUG
if (true)
#else
if (GetApp()->IsDevOpen())
#endif
{
pos.x = pLayers->GetPosition().x + pLayers->GetSize().GetWidth() + 20;
pos.y = pStatic->GetPosition().y;
wxStaticText *pStDebounce = new wxStaticText(this, wxID_ANY,
wxT("Debounce time (ms): "),
pos);
pos.x += pStDebounce->GetSize().GetX();
pos.y = pLayers->GetPosition().y;
wxArrayString a_1_20;
for (int i = 1; i <= 20; i++)
a_1_20.Add(wxString::Format(wxT("%d"), i));
pDebounce = new wxChoice(this, Blusb_Debounce, pos, sz, a_1_20);
int nDebounce = GetApp()->ReadDebounce();
if (nDebounce < 1 || nDebounce > 20)
nDebounce = 7;
pDebounce->Select(nDebounce - 1);
wxUint8 pwmUsb, pwmBt;
#ifdef _DEBUG
GetApp()->ReadPWM(pwmUsb, pwmBt);
if (1)
#else
if (GetApp()->ReadPWM(pwmUsb, pwmBt) > BLUSB_SUCCESS)
#endif
{
wxArrayString a_0_255;
for (int i = 0; i <= 255; i++)
a_0_255.Add(wxString::Format(wxT("%d"), i));
pos.x += pDebounce->GetSize().GetX() + 10;
pos.y = pStatic->GetPosition().y;
wxStaticText *pStPwmUsb = new wxStaticText(this, wxID_ANY,
wxT("PWM USB: "),
pos);
pos.x += pStPwmUsb->GetSize().GetX();
pos.y = pLayers->GetPosition().y;
pPwmUsb = new wxChoice(this, Blusb_PwmUsb, pos, sz, a_0_255);
pos.x += pPwmUsb->GetSize().GetX() + 5;
pos.y = pStatic->GetPosition().y;
wxStaticText *pStPwmBt = new wxStaticText(this, wxID_ANY,
wxT("BT: "),
pos);
pos.x += pStPwmBt->GetSize().GetX();
pos.y = pLayers->GetPosition().y;
pPwmBt = new wxChoice(this, Blusb_PwmBt, pos, sz, a_0_255);
pPwmUsb->Select(pwmUsb);
pPwmBt->Select(pwmBt);
}
else
pPwmUsb = pPwmBt = NULL;
}
else
pDebounce = pPwmUsb = pPwmBt = NULL;
wxSizer *sizerV = new wxBoxSizer(wxVERTICAL);
sizerV->AddSpacer(pLayers->GetSize().GetY() + 5);
// needs to be loaded before the matrix window is created
wxString sLayout;
GetApp()->ReadConfig("/Settings/KbdLayout", &sLayout, "ANSI");
KbdGui kbdGui(!sLayout.CmpNoCase("ISO122") ? KbdGui::KbdISO122 :
!sLayout.CmpNoCase("ANSI121") ? KbdGui::KbdANSI121 :
!sLayout.CmpNoCase("ISO") ? KbdGui::KbdISO :
KbdGui::KbdANSI);
bool bLayoutOK = true;
if (sLayout.CmpNoCase("ISO") &&
sLayout.CmpNoCase("ANSI") &&
sLayout.CmpNoCase("ISO122") &&
sLayout.CmpNoCase("ANSI121"))
bLayoutOK = kbdGui.ReadLayoutFile(sLayout);
if (bLayoutOK)
{
GetApp()->SetDefaultLayout(kbdGui);
if (!GetApp()->IsCtlLayoutRead()) /* set layout, unless read from ctl */
GetApp()->SetLayout(GetApp()->GetDefaultLayout());
kbdGuiLayout = kbdGui;
}
pMatrixNotebook = new wxNotebook(this, wxID_ANY);
SetLayers(1, true);
// TODO: add keyboard layout page(s?)
sizerV->Add(pMatrixNotebook, wxSizerFlags(1).Expand());
wxSize szMatrix = matrices[0]->GetSize();
szMatrix.y = 10;
pKbd = new CKbdPanel(this, wxID_ANY, wxDefaultPosition, szMatrix);
if (bLayoutOK)
pKbd->SetKbdLayout(kbdGui);
sizerV->Add(pKbd, wxSizerFlags(0).Expand());
SetSizerAndFit(sizerV);
}
/*****************************************************************************/
/* SetLayers : sets up the number of layers */
/*****************************************************************************/
bool CMainPanel::SetLayers(int nLayers, bool resetExisting)
{
KbdLayout &kbdLayout = GetApp()->GetLayout();
KbdLayout &kbdDefault = GetApp()->GetDefaultLayout();
int i;
for (i = (int)matrices.size(); i > nLayers; i--)
{
pMatrixNotebook->DeletePage(i - 1);
matrices.erase(matrices.begin() + i - 1);
kbdLayout.RemoveLayer(i - 1);
}
if (resetExisting)
{
for (i = 0; i < (int)matrices.size(); i++)
{
kbdLayout[i] = kbdDefault[0];
CMatrixPanel *pPanel = (CMatrixPanel *)pMatrixNotebook->GetPage(i);
pPanel->SetKbdMatrix(kbdLayout[i]);
}
}
CKbdWnd *pKbdWnd = pKbd ? pKbd->GetKbdWnd() : NULL;
for (i = (int)matrices.size(); i < nLayers; i++)
{
CMatrixPanel *pNew = CreateMatrixPage(pMatrixNotebook);
pMatrixNotebook->InsertPage(i, pNew,
wxString::Format(wxT("Matrix Layer %d"), i));
pNew->SetLayer(i);
matrices.push_back(pNew);
if (i >= kbdLayout.GetLayers())
{
kbdLayout.AddLayer();
kbdLayout[i] = kbdDefault[0];
}
pNew->SetKbdMatrix(kbdLayout[i]);
pNew->SetKbdWnd(pKbdWnd);
}
pLayers->SetSelection(nLayers - 1);
return true;
}
/*****************************************************************************/
/* CreateMatrixPage : create a matrix page */
/*****************************************************************************/
CMatrixPanel *CMainPanel::CreateMatrixPage
(
wxWindow *parent,
int numRows,
int numCols
)
{
wxSizerFlags flagsBorder = wxSizerFlags().Border().Centre();
CMatrixPanel *page = new CMatrixPanel(parent, numRows, numCols);
wxSizer *sizerPage = new wxBoxSizer(wxHORIZONTAL);
sizerPage->Add(page->GetMatrix(), wxSizerFlags(1).Expand());
page->SetSizerAndFit(sizerPage);
return page;
}
/*****************************************************************************/
/* SetKbdLayout : loads a keyboard layout into the layers */
/*****************************************************************************/
void CMainPanel::SetKbdLayout(KbdLayout &layout)
{
SetLayers(layout.GetLayers());
CKbdWnd *pKbdWnd = pKbd->GetKbdWnd();
for (int i = 0; i < layout.GetLayers(); i++)
{
CMatrixPanel *pPanel = matrices[i];
pPanel->SetKbdMatrix(layout[i]);
pPanel->SetKbdWnd(pKbdWnd);
}
}
/*****************************************************************************/
/* SelectMatrix : select a matrix position in the current layer */
/*****************************************************************************/
void CMainPanel::SelectMatrix(int row, int col)
{
CMatrixPanel *pPanel = (CMatrixPanel *)pMatrixNotebook->GetCurrentPage();
if (pPanel)
pPanel->SelectMatrix(row, col);
}
/*===========================================================================*/
/* CMainFrame class members */
/*===========================================================================*/
/*****************************************************************************/
/* CMainFrame Event Table */
/*****************************************************************************/
wxBEGIN_EVENT_TABLE(CMainFrame, wxFrame)
EVT_MENU(Blusb_Quit, CMainFrame::OnExit)
EVT_MENU(Blusb_About, CMainFrame::OnAbout)
EVT_CLOSE(CMainFrame::OnClose)
EVT_KEY_DOWN(CMainFrame::OnKeyDown)
EVT_CHAR(CMainFrame::OnChar)
EVT_KEY_UP(CMainFrame::OnKeyUp)
EVT_TIMER(Blusb_Timer1, CMainFrame::OnReadMatrixTimer)
EVT_CHOICE(Blusb_LayerCount, CMainFrame::OnLayerCount)
EVT_CHOICE(Blusb_Debounce, CMainFrame::OnDebounce)
EVT_CHOICE(Blusb_PwmUsb, CMainFrame::OnPwmUsb)
EVT_CHOICE(Blusb_PwmBt, CMainFrame::OnPwmBt)
EVT_MENU(Blusb_ResetLayout, CMainFrame::OnReset)
EVT_MENU(Blusb_ReadLayout, CMainFrame::OnReadLayout)
EVT_UPDATE_UI(Blusb_ReadLayout, CMainFrame::OnUpdateReadLayout)
EVT_MENU(Blusb_WriteLayout, CMainFrame::OnWriteLayout)
EVT_UPDATE_UI(Blusb_WriteLayout, CMainFrame::OnUpdateWriteLayout)
EVT_MENU(Blusb_ServiceMode, CMainFrame::OnServiceMode)
EVT_UPDATE_UI(Blusb_ServiceMode, CMainFrame::OnUpdateServiceMode)
EVT_MENU(Blusb_ReadFile, CMainFrame::OnReadFile)
EVT_MENU(Blusb_WriteFile, CMainFrame::OnWriteFile)
EVT_MENU(Blusb_Kbd_ANSI, CMainFrame::OnKbdANSI)
EVT_UPDATE_UI(Blusb_Kbd_ANSI, CMainFrame::OnUpdateKbdANSI)
EVT_MENU(Blusb_Kbd_ISO, CMainFrame::OnKbdISO)
EVT_UPDATE_UI(Blusb_Kbd_ISO, CMainFrame::OnUpdateKbdISO)
EVT_MENU(Blusb_Kbd_ANSI121, CMainFrame::OnKbdANSI121)
EVT_UPDATE_UI(Blusb_Kbd_ANSI121, CMainFrame::OnUpdateKbdANSI121)
EVT_MENU(Blusb_Kbd_ISO122, CMainFrame::OnKbdISO122)
EVT_UPDATE_UI(Blusb_Kbd_ISO122, CMainFrame::OnUpdateKbdISO122)
EVT_MENU(Blusb_Kbd_Reset, CMainFrame::OnKbdReset)
EVT_MENU(Blusb_Kbd_Load, CMainFrame::OnKbdLoad)
EVT_MENU(Blusb_Kbd_Save, CMainFrame::OnKbdSave)
wxEND_EVENT_TABLE()
/*****************************************************************************/
/* CMainFrame : constructor */
/*****************************************************************************/
CMainFrame::CMainFrame
(
const wxString& title,
const wxPoint& pos,
const wxSize& size
)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
SetIcon(wxICON(Application));
wxMenu *menuFile = new wxMenu;
menuFile->Append(wxID_EXIT);
memset(bufferLast, 0xff, sizeof(bufferLast));
wxMenu *menuLayout = new wxMenu;
menuLayout->Append(Blusb_ResetLayout, wxT("Reset Layout"),
wxT("Reset layout to default values"));
menuLayout->Append(Blusb_ReadFile, wxT("Load from File..."),
wxT("Read layout from file"));
menuLayout->Append(Blusb_WriteFile, wxT("Save to File..."),
wxT("Write layout to file"));
//if (GetApp()->IsDevOpen())
{
menuLayout->AppendSeparator();
menuLayout->Append(Blusb_ReadLayout, wxT("Read Layout"),
wxT("Read layout from attached Model M keyboard"));
menuLayout->Append(Blusb_WriteLayout, wxT("Write Layout"),
wxT("Write layout to attached Model M keyboard"));
// Enable / Disable Service Mode is not available in V1.5 and later
if (GetApp()->GetFwVersion() < 0x0105)
menuLayout->AppendCheckItem(Blusb_ServiceMode, wxT("Service Mode"),
wxT("Put attached Model M keyboard into Service Mode"));
}
menuLayout->AppendSeparator();
menuLayout->Append(Blusb_Kbd_ANSI, wxT("ANSI Keyboard Layout"),
wxT("Use ANSI Keyboard Layout"),
true);
menuLayout->Append(Blusb_Kbd_ISO, wxT("ISO Keyboard Layout"),
wxT("Use ISO Keyboard Layout"),
true);
menuLayout->Append(Blusb_Kbd_ANSI121, wxT("ANSI 121 Keyboard Layout"),
wxT("Use ANSI 121 Keyboard Layout"),
true);
menuLayout->Append(Blusb_Kbd_ISO122, wxT("ISO 122 Keyboard Layout"),
wxT("Use ISO 122 Keyboard Layout"),
true);
menuLayout->Append(Blusb_Kbd_Load, wxT("Load Keyboard Layout..."),
wxT("Load Keyboard Layout from file"));
menuLayout->Append(Blusb_Kbd_Save, wxT("Save Keyboard Layout..."),
wxT("Save Keyboard Layout to file"));
#if 0
// not necessary here, since blusb_gui doesn't discriminate between
// "unpressed" (i.e., never pressed) and "released" keys
menuLayout->Append(Blusb_Kbd_Reset, wxT("Reset Keyboard"),
wxT("Reset all keys on keyboard to unpressed"));
#endif
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, wxT("&File"));
menuBar->Append( menuLayout, wxT("&Layout"));
menuBar->Append( menuHelp, wxT("&Help"));
SetMenuBar( menuBar );
// TODO: create a wxNotebook object to hold 2 child windows:
// one for the matrix (which is a wxGrid object plus a layer selector)
// and one for the keyboard display
// below that, a control area that shows the USB connection
// to the keyboard
m_panel = new CMainPanel(this);
CreateStatusBar();
SelectMatrix(0, 0);
// set up 10ms timer
t.SetOwner(this, Blusb_Timer1);
t.Start(10);
SetStatusText(wxT("Ready"));
}
/*****************************************************************************/
/* OnExit : called when wxID_EXIT comes in */
/*****************************************************************************/
void CMainFrame::OnExit(wxCommandEvent& event)
{
t.Stop();
Close(true);
}
/*****************************************************************************/
/* OnClose called when the window is about to be closed */
/*****************************************************************************/
void CMainFrame::OnClose(wxCloseEvent &event)
{
CNoServiceMode nosm; /* no service mode in here! */
if (event.CanVeto() && GetApp()->IsLayoutModified())
{
if (wxMessageBox("The current layout has not been saved; "
"do you really want to quit?",
"Please confirm",
wxICON_QUESTION | wxYES_NO) != wxYES)
{
event.Veto();
return;
}
}
event.Skip();
}
/*****************************************************************************/
/* OnAbout : called when wxID_ABOUT comes in */
/*****************************************************************************/
void CMainFrame::OnAbout(wxCommandEvent& event)
{
CNoServiceMode nosm; /* no service mode in here! */
#if WITH_SPLASH
CSplashWnd::ShowSplashScreen(this, 0);
#else
wxMessageBox(wxT("Graphical BlUSB Layout Configuration Tool\n\n")
wxT("Copyright (c) H. Seib, 2017-19"),
wxT("About BlUSB_Gui"),
wxOK | wxICON_INFORMATION );
#endif
}
/*****************************************************************************/
/* OnReadMatrixTimer : 10ms timer to read out the keyboard matrix */
/*****************************************************************************/
void CMainFrame::OnReadMatrixTimer(wxTimerEvent& event)
{
wxUint8 buffer[sizeof(bufferLast)];
memset(buffer, 0xff, sizeof(buffer));
if (GetApp()->IsDevOpen() && GetApp()->InServiceMode())
{
int rc = GetApp()->ReadMatrixPos(buffer, sizeof(buffer));
if (rc >= sizeof(bufferLast))
{
bool bChanged = false;
if (buffer[7])
{
for (int i = 0; i < 2; i++)
if (buffer[i] ^ bufferLast[i])
{
bChanged = true;
break;
}
if (bChanged)
{
// this is not exactly true, but we only get the last press,
// so assure that only one key is shown pressed
if (bufferLast[0] != 0xff)
SetKeyState(bufferLast[0], bufferLast[1], CKbdWnd::ksReleased);
memcpy(bufferLast, buffer, sizeof(bufferLast));
SelectMatrix(buffer[0], buffer[1]);
SetKeyState(buffer[0], buffer[1], CKbdWnd::ksPressed);
}
}
else if (buffer[7] != bufferLast[7])
{
SetKeyState(bufferLast[0], bufferLast[1], CKbdWnd::ksReleased);
// memcpy(bufferLast, buffer, sizeof(bufferLast));
memset(bufferLast, 0xff, sizeof(bufferLast));
}
}
}
}
/*****************************************************************************/
/* OnLayerCount : layer count changes */
/*****************************************************************************/
void CMainFrame::OnLayerCount(wxCommandEvent& event)
{
m_panel->SetLayers(m_panel->GetLayerChoice());
}
/*****************************************************************************/
/* OnDebounce : debounce changes */
/*****************************************************************************/
void CMainFrame::OnDebounce(wxCommandEvent& event)
{
if (GetApp()->WriteDebounce(m_panel->GetDebounce()) < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error writing new debounce value to keyboard"),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
}
/*****************************************************************************/
/* OnPwmUsb : USB PWM changes */
/*****************************************************************************/
void CMainFrame::OnPwmUsb(wxCommandEvent& event)
{
wxUint8 pwmUsb, pwmBt;
if (GetApp()->ReadPWM(pwmUsb, pwmBt) < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error reading PWM values from keyboard"),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
pwmUsb = (wxUint8)m_panel->GetPwmUsb();
if (GetApp()->WritePWM(pwmUsb, pwmBt) < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error writing new USB PWM value to keyboard"),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
}
/*****************************************************************************/
/* OnPwmBt : BT PWM changes */
/*****************************************************************************/
void CMainFrame::OnPwmBt(wxCommandEvent& event)
{
wxUint8 pwmUsb, pwmBt;
if (GetApp()->ReadPWM(pwmUsb, pwmBt) < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error reading PWM values from keyboard"),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
pwmBt = (wxUint8)m_panel->GetPwmBt();
if (GetApp()->WritePWM(pwmUsb, pwmBt) < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error writing new BT PWM value to keyboard"),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
}
/*****************************************************************************/
/* OnReadLayout : read layout from attached keyboard */
/*****************************************************************************/
void CMainFrame::OnReadLayout(wxCommandEvent& event)
{
CNoServiceMode nosm; /* no service mode in here! */
if (GetApp()->IsLayoutModified() &&
wxMessageBox("The current layout has not been saved; "
"do you really want to overwrite it?",
"Please confirm",
wxICON_QUESTION | wxYES_NO) != wxYES)
return;
wxBusyCursor wait;
if (GetApp()->ReadLayout() < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error reading layout from keyboard"),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
SetKbdLayout(GetApp()->GetLayout());
}
/*****************************************************************************/
/* OnUpdateReadLayout : update the visual appearance */
/*****************************************************************************/
void CMainFrame::OnUpdateReadLayout(wxUpdateUIEvent& event)
{
event.Enable(GetApp()->IsDevOpen());
}
/*****************************************************************************/
/* OnWriteLayout : write layout to attached keyboard */
/*****************************************************************************/
void CMainFrame::OnWriteLayout(wxCommandEvent& event)
{
wxBusyCursor wait;
if (GetApp()->WriteLayout() < BLUSB_SUCCESS)
{
CNoServiceMode nosm; /* no service mode in here! */
wxMessageBox(wxT("Error writing layout to keyboard"),
wxT("Model M Error"),
wxOK | wxCENTRE);
}
#if 1
// saving to keyboard counts as a switch to "unmodified"
else
GetApp()->SetLayoutModified(false);
#endif
}
/*****************************************************************************/
/* OnUpdateWriteLayout : update the visual appearance */
/*****************************************************************************/
void CMainFrame::OnUpdateWriteLayout(wxUpdateUIEvent& event)
{
event.Enable(GetApp()->IsDevOpen());
}
/*****************************************************************************/
/* OnReset : reset keyboard layout to default */
/*****************************************************************************/
void CMainFrame::OnReset(wxCommandEvent& event)
{
CNoServiceMode nosm; /* no service mode in here! */
if (GetApp()->IsLayoutModified() &&
wxMessageBox("The current layout has not been saved; "
"do you really want to reset it?",
"Please confirm",
wxICON_QUESTION | wxYES_NO) != wxYES)
return;
GetApp()->GetLayout() = GetApp()->GetDefaultLayout();
SetKbdLayout(GetApp()->GetLayout());
}
/*****************************************************************************/
/* OnServiceMode : puts Model M into Service or Normal mode */
/*****************************************************************************/
void CMainFrame::OnServiceMode(wxCommandEvent& event)
{
if (event.IsChecked())
{
// memset(bufferLast, 0, sizeof(bufferLast));
memset(bufferLast, 0xff, sizeof(bufferLast));
GetApp()->EnableServiceMode();
}
else
GetApp()->DisableServiceMode();
}
/*****************************************************************************/
/* OnUpdateServiceMode : update the visual appearance */
/*****************************************************************************/
void CMainFrame::OnUpdateServiceMode(wxUpdateUIEvent& event)
{
event.Enable(GetApp()->IsDevOpen());
}
/*****************************************************************************/
/* OnReadFile : called to read a layout file */
/*****************************************************************************/
void CMainFrame::OnReadFile(wxCommandEvent& event)
{
CNoServiceMode nosm; /* no service mode in here! */
if (GetApp()->IsLayoutModified() &&
wxMessageBox("The current layout has not been saved; "
"do you really want to load another?",
"Please confirm",
wxICON_QUESTION | wxYES_NO) != wxYES)
return;
wxFileDialog of(this, wxT("Open BlUSB File"), wxT("."), wxEmptyString,
wxT("BlUSB Files (*.blu)|*.blu|All Files (*)|*.*"),
wxFD_OPEN | wxFD_FILE_MUST_EXIST);
int rc = of.ShowModal();
if (rc != wxID_OK)
return;
if (GetApp()->ReadLayout(of.GetPath()) < BLUSB_SUCCESS)
{
wxMessageBox(wxT("Error reading layout from ") + of.GetPath(),
wxT("Model M Error"),
wxCANCEL | wxCENTRE);
return;
}
SetKbdLayout(GetApp()->GetLayout());
}
/*****************************************************************************/
/* OnWriteFile : called to write a configuration file */
/*****************************************************************************/
void CMainFrame::OnWriteFile(wxCommandEvent& event)
{
CNoServiceMode nosm; /* no service mode in here! */
wxFileDialog of(this, wxT("Save to BlUSB File"), wxT("."), wxEmptyString,
wxT("BlUSB Files (*.blu)|*.blu|All Files (*)|*.*"),
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
int rc = of.ShowModal();
if (rc != wxID_OK)
return;
if (GetApp()->WriteLayout(of.GetPath(), !of.GetFilterIndex()) < BLUSB_SUCCESS)
wxMessageBox(wxT("Error writing layout to ") + of.GetPath(),
wxT("Model M Error"),
wxOK | wxCENTRE);
}
/*****************************************************************************/
/* SetKbdGuiLayout : setup new Keyboard GUI layout */
/*****************************************************************************/
bool CMainFrame::SetKbdGuiLayout(KbdGui &layout)
{
if (!m_panel)
return false;
int currows, curcols;
m_panel->GetKbdGuiLayout().GetMatrixLayout(currows, curcols);
int kbdrows = NUMROWS, kbdcols = curcols;
GetApp()->ReadMatrixLayout(kbdrows, kbdcols);
int newrows, newcols;
layout.GetMatrixLayout(newrows, newcols);
m_panel->SetKbdGuiLayout(layout);
// TODO:
// We got the following matrix definitions now:
// 1) current matrix on screen
// 2) matrix layout expected by the keyboard
// 3) matrix layout mandated by the GUI layout
// What if these don't match? Hmmm ...
// Ignore the attached keyboard's matrix layout for now.
if (newrows > 1 && newcols > 1 && // <= 1 indicates "no matrix definitions"
(newrows != currows || newcols != curcols))
{
wxString s = wxString::Format(wxT("The new layout redefines the matrix size ")
wxT("from %dx%d to %dx%d!\n")
wxT("Do you want to reset the matrix ")
wxT("definitions to the default for the new ")
wxT("keyboard layout?"),
currows, curcols, newrows, newcols);
if (wxMessageBox(s, wxT("Matrix Redefinition"),
wxICON_QUESTION | wxYES_NO | wxCENTRE) == wxYES)
{
GetApp()->SetDefaultLayout(layout);
m_panel->SetLayers(1, true);
}
}
return true;
}
/*****************************************************************************/
/* OnKeyDown : called when a key is pressed on the window */
/*****************************************************************************/
void CMainFrame::OnKeyDown(wxKeyEvent &ev)
{
#if !defined(__WXMSW__)
wxChar uc = ev.GetKeyCode();
// printf("CMainFrame::OnKeyDown(%d)\n", uc);
#endif
ev.Skip();
}
/*****************************************************************************/
/* OnChar : called for character events */
/*****************************************************************************/
void CMainFrame::OnChar(wxKeyEvent &ev)
{
#if !defined(__WXMSW__)
wxChar uc = ev.GetUnicodeKey();
if (uc == WXK_NONE)
uc = ev.GetKeyCode();
// printf("CMainFrame::OnChar(%d)\n", uc);
#endif
ev.Skip();
}
/*****************************************************************************/
/* OnKeyUp : called when a key is released on the window */
/*****************************************************************************/
void CMainFrame::OnKeyUp(wxKeyEvent &ev)
{
#if !defined(__WXMSW__)
wxChar uc = ev.GetKeyCode();
// printf("CMainFrame::OnKeyUp(%d)\n", uc);
#endif
ev.Skip();
}
/*****************************************************************************/
/* OnKbdAnsi : set keyboard layout to ANSI default */
/*****************************************************************************/
void CMainFrame::OnKbdANSI(wxCommandEvent& event)
{
KbdGui newGui(KbdGui::KbdANSI);
if (SetKbdGuiLayout(newGui))
GetApp()->WriteConfig("/Settings/KbdLayout", "ANSI");
}
/*****************************************************************************/
/* OnUpdateKbdANSI : update the visual appearance */
/*****************************************************************************/
void CMainFrame::OnUpdateKbdANSI(wxUpdateUIEvent& event)
{
wxString s;
// This should be made more performant
GetApp()->ReadConfig("/Settings/KbdLayout", &s, "ANSI");
event.Check(!s.CmpNoCase(wxT("ANSI")));
}
/*****************************************************************************/
/* OnKbdISO : set keyboard layout to ISO default */
/*****************************************************************************/
void CMainFrame::OnKbdISO(wxCommandEvent& event)
{
KbdGui newGui(KbdGui::KbdISO);
if (SetKbdGuiLayout(newGui))
GetApp()->WriteConfig("/Settings/KbdLayout", "ISO");
}
/*****************************************************************************/
/* OnUpdateKbdISO : update the visual appearance */
/*****************************************************************************/
void CMainFrame::OnUpdateKbdISO(wxUpdateUIEvent& event)
{
wxString s;
// This should be made more performant
GetApp()->ReadConfig("/Settings/KbdLayout", &s, "ANSI");
event.Check(!s.CmpNoCase(wxT("ISO")));
}
/*****************************************************************************/
/* OnKbdAnsi121 : set keyboard layout to ANSI 121 default */
/*****************************************************************************/
void CMainFrame::OnKbdANSI121(wxCommandEvent& event)
{
KbdGui newGui(KbdGui::KbdANSI121);
if (SetKbdGuiLayout(newGui))
GetApp()->WriteConfig("/Settings/KbdLayout", "ANSI121");
}
/*****************************************************************************/
/* OnUpdateKbdANSI121 : update the visual appearance */
/*****************************************************************************/
void CMainFrame::OnUpdateKbdANSI121(wxUpdateUIEvent& event)
{
wxString s;
// This should be made more performant
GetApp()->ReadConfig("/Settings/KbdLayout", &s, "ANSI");
event.Check(!s.CmpNoCase(wxT("ANSI121")));
}
/*****************************************************************************/
/* OnKbdISO122 : set keyboard layout to ISO 122 default */
/*****************************************************************************/
void CMainFrame::OnKbdISO122(wxCommandEvent& event)
{
KbdGui newGui(KbdGui::KbdISO122);
if (SetKbdGuiLayout(newGui))