| 1 |
use std::{mem, slice}; |
| 2 |
use std::io::{Write, stderr}; |
| 3 |
|
| 4 |
use super::winapi::shared::basetsd::DWORD_PTR; |
| 5 |
use super::winapi::shared::minwindef::{DWORD, UINT}; |
| 6 |
use super::winapi::um::mmeapi::midiInAddBuffer; |
| 7 |
use super::winapi::um::mmsystem::{HMIDIIN, MIDIHDR, MMSYSERR_NOERROR, MM_MIM_DATA, |
| 8 |
MM_MIM_LONGDATA, MM_MIM_LONGERROR}; |
| 9 |
use super::HandlerData; |
| 10 |
use Ignore; |
| 11 |
|
| 12 |
pub extern "system" fn handle_input<T>(_: HMIDIIN, |
| 13 |
input_status: UINT, |
| 14 |
instance_ptr: DWORD_PTR, |
| 15 |
midi_message: DWORD_PTR, |
| 16 |
timestamp: DWORD) { |
| 17 |
if input_status != MM_MIM_DATA && input_status != MM_MIM_LONGDATA && input_status != MM_MIM_LONGERROR { return; } |
| 18 |
|
| 19 |
let data: &mut HandlerData<T> = unsafe { &mut *(instance_ptr as *mut HandlerData<T>) }; |
| 20 |
|
| 21 |
// Calculate time stamp. |
| 22 |
data.message.timestamp = timestamp as u64 * 1000; // milliseconds -> microseconds |
| 23 |
|
| 24 |
if input_status == MM_MIM_DATA { // Channel or system message |
| 25 |
// Make sure the first byte is a status byte. |
| 26 |
let status: u8 = (midi_message & 0x000000FF) as u8; |
| 27 |
if !(status & 0x80 != 0) { return; } |
| 28 |
|
| 29 |
// Determine the number of bytes in the MIDI message. |
| 30 |
let nbytes: u16 = if status < 0xC0 { 3 } |
| 31 |
else if status < 0xE0 { 2 } |
| 32 |
else if status < 0xF0 { 3 } |
| 33 |
else if status == 0xF1 { |
| 34 |
if data.ignore_flags.contains(Ignore::Time) { return; } |
| 35 |
else { 2 } |
| 36 |
} else if status == 0xF2 { 3 } |
| 37 |
else if status == 0xF3 { 2 } |
| 38 |
else if status == 0xF8 && (data.ignore_flags.contains(Ignore::Time)) { |
| 39 |
// A MIDI timing tick message and we're ignoring it. |
| 40 |
return; |
| 41 |
} else if status == 0xFE && (data.ignore_flags.contains(Ignore::ActiveSense)) { |
| 42 |
// A MIDI active sensing message and we're ignoring it. |
| 43 |
return; |
| 44 |
} else { 1 }; |
| 45 |
|
| 46 |
// Copy bytes to our MIDI message. |
| 47 |
let ptr = (&midi_message) as *const DWORD_PTR as *const u8; |
| 48 |
let bytes: &[u8] = unsafe { slice::from_raw_parts(ptr, nbytes as usize) }; |
| 49 |
data.message.bytes.extend_from_slice(bytes); |
| 50 |
} else { // Sysex message (MIM_LONGDATA or MIM_LONGERROR) |
| 51 |
let sysex = unsafe { &*(midi_message as *const MIDIHDR) }; |
| 52 |
if !data.ignore_flags.contains(Ignore::Sysex) && input_status != MM_MIM_LONGERROR { |
| 53 |
// Sysex message and we're not ignoring it |
| 54 |
let bytes: &[u8] = unsafe { slice::from_raw_parts(sysex.lpData as *const u8, sysex.dwBytesRecorded as usize) }; |
| 55 |
data.message.bytes.extend_from_slice(bytes); |
| 56 |
// TODO: If sysex messages are longer than RT_SYSEX_BUFFER_SIZE, they |
| 57 |
// are split in chunks. We could reassemble a single message. |
| 58 |
} |
| 59 |
|
| 60 |
// The WinMM API requires that the sysex buffer be requeued after |
| 61 |
// input of each sysex message. Even if we are ignoring sysex |
| 62 |
// messages, we still need to requeue the buffer in case the user |
| 63 |
// decides to not ignore sysex messages in the future. However, |
| 64 |
// it seems that WinMM calls this function with an empty sysex |
| 65 |
// buffer when an application closes and in this case, we should |
| 66 |
// avoid requeueing it, else the computer suddenly reboots after |
| 67 |
// one or two minutes. |
| 68 |
if (unsafe {*data.sysex_buffer.0[sysex.dwUser as usize]}).dwBytesRecorded > 0 { |
| 69 |
//if ( sysex->dwBytesRecorded > 0 ) { |
| 70 |
let in_handle = data.in_handle.as_ref().unwrap().0.lock().unwrap(); |
| 71 |
let result = unsafe { midiInAddBuffer(*in_handle, data.sysex_buffer.0[sysex.dwUser as usize], mem::size_of::<MIDIHDR>() as u32) }; |
| 72 |
drop(in_handle); |
| 73 |
if result != MMSYSERR_NOERROR { |
| 74 |
let _ = writeln!(stderr(), "\nError in handle_input: Requeuing WinMM input sysex buffer failed.\n"); |
| 75 |
} |
| 76 |
|
| 77 |
if data.ignore_flags.contains(Ignore::Sysex) { return; } |
| 78 |
} else { return; } |
| 79 |
} |
| 80 |
|
| 81 |
(data.callback)(data.message.timestamp, &data.message.bytes, data.user_data.as_mut().unwrap()); |
| 82 |
|
| 83 |
// Clear the vector for the next input message. |
| 84 |
data.message.bytes.clear(); |
| 85 |
} |