aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends/winmidi.c
diff options
context:
space:
mode:
Diffstat (limited to 'backends/winmidi.c')
-rw-r--r--backends/winmidi.c80
1 files changed, 42 insertions, 38 deletions
diff --git a/backends/winmidi.c b/backends/winmidi.c
index 0722ca2..d9b3047 100644
--- a/backends/winmidi.c
+++ b/backends/winmidi.c
@@ -95,19 +95,14 @@ static int winmidi_configure_instance(instance* inst, char* option, char* value)
return 1;
}
-static instance* winmidi_instance(){
- instance* i = mm_instance();
- if(!i){
- return NULL;
- }
-
- i->impl = calloc(1, sizeof(winmidi_instance_data));
- if(!i->impl){
+static int winmidi_instance(instance* inst){
+ inst->impl = calloc(1, sizeof(winmidi_instance_data));
+ if(!inst->impl){
LOG("Failed to allocate memory");
- return NULL;
+ return 1;
}
- return i;
+ return 0;
}
static channel* winmidi_channel(instance* inst, char* spec, uint8_t flags){
@@ -122,7 +117,7 @@ static channel* winmidi_channel(instance* inst, char* spec, uint8_t flags){
next_token = spec + 7;
}
}
-
+
if(!next_token){
LOGPF("Invalid channel specification %s", spec);
return NULL;
@@ -218,7 +213,7 @@ static int winmidi_set(instance* inst, size_t num, channel** c, channel_value* v
midiOutShortMsg(data->device_out, output.dword);
}
-
+
return 0;
}
@@ -263,7 +258,7 @@ static int winmidi_handle(size_t num, managed_fd* fds){
backend_config.event[u].inst->name,
backend_config.event[u].channel.fields.channel,
winmidi_type_name(backend_config.event[u].channel.fields.type),
- backend_config.event[u].value);
+ backend_config.event[u].value.normalised);
}
else{
LOGPF("Incoming data on channel %s.ch%d.%s%d, value %f",
@@ -271,7 +266,7 @@ static int winmidi_handle(size_t num, managed_fd* fds){
backend_config.event[u].channel.fields.channel,
winmidi_type_name(backend_config.event[u].channel.fields.type),
backend_config.event[u].channel.fields.control,
- backend_config.event[u].value);
+ backend_config.event[u].value.normalised);
}
}
chan = mm_channel(backend_config.event[u].inst, backend_config.event[u].channel.label, 0);
@@ -315,7 +310,7 @@ static void CALLBACK winmidi_input_callback(HMIDIIN device, unsigned message, DW
ident.fields.type = input.components.status & 0xF0;
ident.fields.control = input.components.data1;
val.normalised = (double) input.components.data2 / 127.0;
-
+
if(ident.fields.type == 0x80){
ident.fields.type = note;
val.normalised = 0;
@@ -340,7 +335,6 @@ static void CALLBACK winmidi_input_callback(HMIDIIN device, unsigned message, DW
case MIM_CLOSE:
//device opened/closed
return;
-
}
DBGPF("Incoming message type %d channel %d control %d value %f",
@@ -396,7 +390,7 @@ static int winmidi_match_input(char* prefix){
for(n = 0; n < inputs; n++){
midiInGetDevCaps(n, &input_caps, sizeof(MIDIINCAPS));
if(!prefix){
- printf("\tID %d: %s", n, input_caps.szPname);
+ LOGPF("\tID %d: %s", n, input_caps.szPname);
}
else if(!strncmp(input_caps.szPname, prefix, strlen(prefix))){
LOGPF("Selected input device %s (ID %" PRIsize_t ") for name %s", input_caps.szPname, n, prefix);
@@ -429,7 +423,7 @@ static int winmidi_match_output(char* prefix){
for(n = 0; n < outputs; n++){
midiOutGetDevCaps(n, &output_caps, sizeof(MIDIOUTCAPS));
if(!prefix){
- printf("\tID %d: %s", n, output_caps.szPname);
+ LOGPF("\tID %d: %s", n, output_caps.szPname);
}
else if(!strncmp(output_caps.szPname, prefix, strlen(prefix))){
LOGPF("Selected output device %s (ID %" PRIsize_t " for name %s", output_caps.szPname, n, prefix);
@@ -440,32 +434,22 @@ static int winmidi_match_output(char* prefix){
return -1;
}
-static int winmidi_start(size_t n, instance** inst){
- size_t p;
- int device, rv = -1;
- winmidi_instance_data* data = NULL;
+static int winmidi_socket_pair(int* fds){
+ //this really should be a size_t but getsockname specifies int* for some reason
+ int sockadd_len = sizeof(struct sockaddr_storage);
+ char* error = NULL;
struct sockaddr_storage sockadd = {
0
};
- //this really should be a size_t but getsockname specifies int* for some reason
- int sockadd_len = sizeof(sockadd);
- char* error = NULL;
- DBGPF("Main thread ID is %ld", GetCurrentThreadId());
- //output device list if requested
- if(backend_config.list_devices){
- winmidi_match_input(NULL);
- winmidi_match_output(NULL);
- }
-
- //open the feedback sockets
//for some reason the feedback connection fails to work on 'real' windows with ipv6
- backend_config.socket_pair[0] = mmbackend_socket("127.0.0.1", "0", SOCK_DGRAM, 1, 0);
- if(backend_config.socket_pair[0] < 0){
+ fds[0] = mmbackend_socket("127.0.0.1", "0", SOCK_DGRAM, 1, 0);
+ if(fds[0] < 0){
LOG("Failed to open feedback socket");
return 1;
}
- if(getsockname(backend_config.socket_pair[0], (struct sockaddr*) &sockadd, &sockadd_len)){
+
+ if(getsockname(fds[0], (struct sockaddr*) &sockadd, &sockadd_len)){
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &error, 0, NULL);
LOGPF("Failed to query feedback socket information: %s", error);
@@ -488,8 +472,8 @@ static int winmidi_start(size_t n, instance** inst){
return 1;
}
DBGPF("Feedback socket family %d port %d", sockadd.ss_family, be16toh(((struct sockaddr_in*)&sockadd)->sin_port));
- backend_config.socket_pair[1] = socket(sockadd.ss_family, SOCK_DGRAM, IPPROTO_UDP);
- if(backend_config.socket_pair[1] < 0 || connect(backend_config.socket_pair[1], (struct sockaddr*) &sockadd, sockadd_len)){
+ fds[1] = socket(sockadd.ss_family, SOCK_DGRAM, IPPROTO_UDP);
+ if(fds[1] < 0 || connect(backend_config.socket_pair[1], (struct sockaddr*) &sockadd, sockadd_len)){
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &error, 0, NULL);
LOGPF("Failed to connect to feedback socket: %s", error);
@@ -497,6 +481,26 @@ static int winmidi_start(size_t n, instance** inst){
return 1;
}
+ return 0;
+}
+
+static int winmidi_start(size_t n, instance** inst){
+ size_t p;
+ int device, rv = -1;
+ winmidi_instance_data* data = NULL;
+ DBGPF("Main thread ID is %ld", GetCurrentThreadId());
+
+ //output device list if requested
+ if(backend_config.list_devices){
+ winmidi_match_input(NULL);
+ winmidi_match_output(NULL);
+ }
+
+ //open the feedback sockets
+ if(winmidi_socket_pair(backend_config.socket_pair)){
+ return 1;
+ }
+
//set up instances and start input
for(p = 0; p < n; p++){
data = (winmidi_instance_data*) inst[p]->impl;