Skip to content

Commit 21f1033

Browse files
committed
Added 0.98 to main.cpp
Main improvement is performance and automatic initialization once opened.
1 parent fb5e015 commit 21f1033

1 file changed

Lines changed: 99 additions & 34 deletions

File tree

Windows/Gopher/Gopher/main.cpp

Lines changed: 99 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
---------------------------------------------------------------------------------*/
1515

1616
//changes 0.96 -> 0.97: speed variable is global, detects bumpers, all timed (no enter), lbumper speed toggler
17-
//changes 0.97 -> 0.98: ...
17+
//changes 0.97 -> 0.98: performance improvements, operational volume function, shorter beeps, no XY text
18+
//changes 0.98 -> 0.99: unknown
19+
//1.0 requirements: bumpers+dpadup = bring back. bumpers+dpaddown = minimize to tray
1820

1921
#include <Windows.h> //for Beep()
2022
#include <Xinput.h> //controller
2123
#include <stdio.h> //for printf
2224
#include <cmath> //for abs()
25+
#include <mmdeviceapi.h> //vol
26+
#include <endpointvolume.h> //vol
27+
2328
#pragma comment(lib, "XInput.lib")
29+
#pragma comment(lib, "winmm") //for volume
2430

2531
void gopherLoop();
32+
bool ChangeVolume(double nVolume,bool bScalar);
2633
BOOL IsElevated();
2734

2835
/*To do:
@@ -97,7 +104,7 @@ CXBOXController* Controller;
97104

98105
int main()
99106
{
100-
SetConsoleTitle( TEXT( "Gopher v0.97" ) );
107+
SetConsoleTitle( TEXT( "Gopher v0.98" ) );
101108
Controller = new CXBOXController(1);
102109

103110
system("Color 1D");
@@ -109,9 +116,9 @@ int main()
109116
printf("Welcome to Gopher/Gopher360 - a lightweight controller->keyboard & mouse tool.\nSee the GitHub repository at bit.ly/1syAhMT for more info. Copyleft 2014.\n\n-------------------------\n\n");
110117
printf("Gopher is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n");
111118
printf("\nYou should have received a copy of the GNU General Public License\nalong with this program. If not, see http://www.gnu.org/licenses/.\n\n-------------------------\n\n");
112-
printf("Verify controller and wait 8 seconds to begin...\n\n\n");
119+
printf("Plug in controller and THEN wait 5 seconds to begin...\n\n\n");
113120

114-
Sleep(8000);
121+
Sleep(5000);
115122

116123
//getchar(); //press enter
117124

@@ -124,11 +131,11 @@ int main()
124131

125132

126133
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nBeginning read sequence at ~62.5 loops per second. Prepare for main loop!\n");
127-
Sleep(1000);
134+
Sleep(800);
128135

129-
Beep(1400,100);
130-
Beep(1400,100);
131-
Beep(1400,100);
136+
Beep(1400,80);
137+
Beep(1400,80);
138+
Beep(1400,80);
132139

133140
while(true){
134141
gopherLoop();
@@ -140,6 +147,7 @@ int main()
140147
void gopherLoop(){
141148

142149
//initialize variables ------------------------------------------------------------------------------------------------------------------
150+
XINPUT_STATE currentState = Controller->GetState(); //added in 0.98
143151

144152
int leftX;
145153
int leftY;
@@ -164,11 +172,11 @@ void gopherLoop(){
164172

165173

166174
//XINPUT_GAMEPAD_BACK
167-
if(Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_BACK)
175+
if(currentState.Gamepad.wButtons == XINPUT_GAMEPAD_BACK)
168176
{
169177
holdBack = true;
170178
}
171-
else if (Controller->GetState().Gamepad.wButtons != XINPUT_GAMEPAD_BACK)
179+
else if (currentState.Gamepad.wButtons != XINPUT_GAMEPAD_BACK)
172180
{
173181
holdBack = false;
174182
}
@@ -205,14 +213,14 @@ void gopherLoop(){
205213
}
206214

207215

208-
if(disabled == false){
216+
if(disabled == false){ //only listen to these if Gopher is enabled, otherwise only listens for the button that enables it, up ^
209217

210218

211219

212220
//get LX info
213-
if(abs(Controller->GetState().Gamepad.sThumbLX) > deadZone)
221+
if(abs(currentState.Gamepad.sThumbLX) > deadZone)
214222
{
215-
addXLeft = (speed * (Controller->GetState().Gamepad.sThumbLX*range));
223+
addXLeft = (speed * (currentState.Gamepad.sThumbLX*range));
216224
}
217225

218226
//zero check
@@ -224,9 +232,9 @@ void gopherLoop(){
224232

225233

226234
//get LY info
227-
if(abs(Controller->GetState().Gamepad.sThumbLY) > deadZone)
235+
if(abs(currentState.Gamepad.sThumbLY) > deadZone)
228236
{
229-
addYLeft = -(speed * (Controller->GetState().Gamepad.sThumbLY*range));
237+
addYLeft = -(speed * (currentState.Gamepad.sThumbLY*range));
230238
}
231239

232240
//zero check
@@ -236,13 +244,13 @@ void gopherLoop(){
236244
}
237245

238246
//Get RY info
239-
holdScrollUp = (Controller->GetState().Gamepad.sThumbRY > scrollDeadZone);
240-
holdScrollDown = (Controller->GetState().Gamepad.sThumbRY < -scrollDeadZone);
247+
holdScrollUp = (currentState.Gamepad.sThumbRY > scrollDeadZone);
248+
holdScrollDown = (currentState.Gamepad.sThumbRY < -scrollDeadZone);
241249

242250

243251

244252
//XINPUT_GAMEPAD_A
245-
holdLeft = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_A);
253+
holdLeft = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_A);
246254

247255
if (holdLeft)
248256
{
@@ -256,7 +264,7 @@ void gopherLoop(){
256264

257265

258266
//XINPUT_GAMEPAD_X
259-
holdRight = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_X);
267+
holdRight = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_X);
260268

261269
if (holdRight)
262270
{
@@ -269,7 +277,7 @@ void gopherLoop(){
269277

270278

271279
//XINPUT_GAMEPAD_B
272-
holdEnter = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_B);
280+
holdEnter = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_B);
273281

274282
if (holdEnter)
275283
{
@@ -281,28 +289,28 @@ void gopherLoop(){
281289
}
282290

283291
//bumpers/shoulders
284-
holdBLeft = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_LEFT_SHOULDER);
285-
holdBRight = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_RIGHT_SHOULDER);
292+
holdBLeft = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_LEFT_SHOULDER);
293+
holdBRight = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_RIGHT_SHOULDER);
286294

287295

288296

289297
//XINPUT_GAMEPAD_START
290-
holdStart = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_START);
298+
holdStart = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_START);
291299

292300
//XINPUT_GAMEPAD_LEFT_THUMB
293-
holdLThumb = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_LEFT_THUMB);
301+
holdLThumb = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_LEFT_THUMB);
294302

295303
//XINPUT_GAMEPAD_DPAD_UP
296-
holdDUp = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_UP);
304+
holdDUp = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_UP);
297305

298306
//XINPUT_GAMEPAD_DPAD_DOWN
299-
holdDDown = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_DOWN);
307+
holdDDown = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_DOWN);
300308

301309
//XINPUT_GAMEPAD_DPAD_LEFT
302-
holdDLeft = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_LEFT);
310+
holdDLeft = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_LEFT);
303311

304312
//XINPUT_GAMEPAD_DPAD_RIGHT
305-
holdDRight = (Controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_RIGHT);
313+
holdDRight = (currentState.Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_RIGHT);
306314

307315

308316
//process input ---------------------------------------------------------------------------------------------------------------------------------
@@ -333,7 +341,7 @@ void gopherLoop(){
333341
}
334342

335343

336-
//filter non-32768 and 32767, wireless ones can glitch sometimes and send it to the edge of the screen
344+
//filter non-32768 and 32767, wireless ones can glitch sometimes and send it to the edge of the screen, it'll toss out some HUGE integer even when it's centered
337345
if(addYLeft > 32767) addYLeft = 0;
338346
if(addYLeft < -32768) addYLeft = 0;
339347
if(addXLeft > 32767) addXLeft = 0;
@@ -612,11 +620,13 @@ void gopherLoop(){
612620
else if(!holdBLeft && holdingBLeft){
613621

614622
holdingBLeft = false;
615-
printf("---------------BLEFT-UP\n");
616-
if(speed == speed_low) {Beep(240,250); speed = speed_med;}
617-
else if(speed == speed_med) {Beep(260,250); speed = speed_high;}
618-
else if(speed == speed_high){Beep(200,250); speed = speed_low;}
623+
printf("---------------BLEFT-UP - CURSOR SPEED CHANGED\n");
624+
if(speed == speed_low) {Beep(240,210); speed = speed_med;}
625+
else if(speed == speed_med) {Beep(260,210); speed = speed_high;}
626+
else if(speed == speed_high){Beep(200,210); speed = speed_low;}
627+
619628

629+
ChangeVolume(0.5,true); //works
620630
}
621631

622632

@@ -638,7 +648,7 @@ void gopherLoop(){
638648
SetCursorPos(leftX,leftY); //after all click input processing
639649

640650

641-
printf("Move X:%d, Y:%d\n", (int)addXLeft, -(int)addYLeft);
651+
//printf("Move X:%d, Y:%d\n", (int)addXLeft, -(int)addYLeft); //disabled for being annoying
642652

643653
}
644654
Sleep(sleepAmount);
@@ -671,6 +681,61 @@ BOOL IsElevated()
671681
}
672682

673683

684+
//this works, but it's not enabled in the software since the best button for it is still undecided
685+
bool ChangeVolume(double nVolume,bool bScalar) //o b
686+
{
687+
688+
HRESULT hr=NULL;
689+
bool decibels = false;
690+
bool scalar = false;
691+
double newVolume=nVolume;
692+
693+
CoInitialize(NULL);
694+
IMMDeviceEnumerator *deviceEnumerator = NULL;
695+
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
696+
__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
697+
IMMDevice *defaultDevice = NULL;
698+
699+
hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
700+
deviceEnumerator->Release();
701+
deviceEnumerator = NULL;
702+
703+
IAudioEndpointVolume *endpointVolume = NULL;
704+
hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
705+
CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
706+
defaultDevice->Release();
707+
defaultDevice = NULL;
708+
709+
// -------------------------
710+
float currentVolume = 0;
711+
endpointVolume->GetMasterVolumeLevel(&currentVolume);
712+
//printf("Current volume in dB is: %f\n", currentVolume);
713+
714+
hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
715+
//CString strCur=L"";
716+
//strCur.Format(L"%f",currentVolume);
717+
//AfxMessageBox(strCur);
718+
719+
// printf("Current volume as a scalar is: %f\n", currentVolume);
720+
if (bScalar==false)
721+
{
722+
hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
723+
}
724+
else if (bScalar==true)
725+
{
726+
hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
727+
}
728+
endpointVolume->Release();
729+
730+
CoUninitialize();
731+
732+
return FALSE;
733+
}
734+
735+
736+
737+
738+
674739
CXBOXController::CXBOXController(int playerNumber)
675740
{
676741
_controllerNum = playerNumber - 1; //set number

0 commit comments

Comments
 (0)