-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAudioSwitch.cpp
More file actions
180 lines (148 loc) · 5 KB
/
AudioSwitch.cpp
File metadata and controls
180 lines (148 loc) · 5 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
/* ***************************************************************************
* Include Files
* **************************************************************************/
#include "AudioSwitch.h"
#include "AudioPlaybackControl.h"
/* ***************************************************************************
* Global Variables
* **************************************************************************/
HHOOK _keyboard_hook = NULL;
HWND _window = NULL;
ATOM _windowClass = NULL;
bool _ctrlPressed = false;
/* ***************************************************************************
* Functions
* **************************************************************************/
#ifdef DEBUG
/* Attaches a console window to a regular Win32 app. */
bool CreateConsole( void )
{
if ( AllocConsole() )
{
int crt = _open_osfhandle( (long)GetStdHandle( STD_OUTPUT_HANDLE ), _O_TEXT );
*stdout = *_fdopen( crt, "w" );
setvbuf( stdout, NULL, _IONBF, NULL );
setlocale( LC_ALL, "Russian" );
return true;
}
return false;
}
#endif
/* Keyboard hook procedure. */
LRESULT CALLBACK KeyboardProc( int code, WPARAM wParam, LPARAM lParam )
{
if ( code >= 0 )
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
bool keyDown = 0 == ( p->flags & LLKHF_UP );
if ( p->vkCode == VK_RCONTROL || p->vkCode == VK_LCONTROL )
{
_ctrlPressed = keyDown;
}
if ( keyDown )
{
if ( _ctrlPressed && ( p->vkCode == VK_F12 ) )
{
NextAudioPlaybackDevice();
}
#ifdef DEBUG
printf(
"vkCode(%d) scanCode(%d) ctrl(%s)\n"
, p->vkCode
, p->scanCode
, _ctrlPressed ? "true" : "false"
);
if ( p->vkCode == VK_ESCAPE )
{
PostMessage( _window, WM_CLOSE, NULL, NULL );
}
#endif
}
}
return CallNextHookEx( _keyboard_hook, code, wParam, lParam );
}
/* Window procedure function that processes messages sent to a window.
* Removes the hook when the window is destroyed. */
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
#ifdef DEBUG
FreeConsole( );
#endif
DestroyWindow( hwnd );
UnhookWindowsHookEx( _keyboard_hook );
break;
case WM_DESTROY:
PostQuitMessage( NULL );
break;
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}
/* The entry point.
* Installs keyboard hook, creates an invisible window and waits for a message
* in a loop until window is closed (WM_CLOSE). */
int WINAPI WinMain (
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd
)
{
#ifdef DEBUG
if ( !CreateConsole() )
{
return EXIT_FAILURE;
}
#endif
_keyboard_hook = SetWindowsHookEx( WH_KEYBOARD_LL, (HOOKPROC)KeyboardProc, GetModuleHandle( NULL ), NULL );
if ( _keyboard_hook )
{
WNDCLASSEX wc;
MSG msg;
HICON icon = LoadIcon( hInstance, "10026" );
wc.cbSize = sizeof( WNDCLASSEX );
wc.lpszClassName = WINDOW_CLASS_NAME;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.style = NULL;
wc.hIconSm = icon;
wc.hIcon = icon;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
_windowClass = RegisterClassEx( &wc );
if ( _windowClass )
{
_window = CreateWindowEx(
NULL, /* dwExStyle */
WINDOW_CLASS_NAME, /* lpClassName */
NULL, /* lpWindowName */
NULL, /* dwStyle */
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
320, /* nWidth */
240, /* nHeight */
NULL, /* hWndParent */
NULL, /* hMenu */
hInstance, /* hInstance */
NULL /* lpParam */
);
if ( _window )
{
while ( GetMessage( &msg, NULL, NULL, NULL ) > 0 )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return EXIT_SUCCESS;
}
}
}
return EXIT_FAILURE;
}