|
| 1 | +/*********************************************************************** |
| 2 | +** |
| 3 | +** REBOL [R3] Language Interpreter and Run-time Environment |
| 4 | +** |
| 5 | +** Copyright 2012 REBOL Technologies |
| 6 | +** Copyright 2012-2019 Rebol Open Source Developers |
| 7 | +** REBOL is a trademark of REBOL Technologies |
| 8 | +** |
| 9 | +** Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +** you may not use this file except in compliance with the License. |
| 11 | +** You may obtain a copy of the License at |
| 12 | +** |
| 13 | +** http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +** |
| 15 | +** Unless required by applicable law or agreed to in writing, software |
| 16 | +** distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +** See the License for the specific language governing permissions and |
| 19 | +** limitations under the License. |
| 20 | +** |
| 21 | +************************************************************************ |
| 22 | +** |
| 23 | +** Module: p-midi.c |
| 24 | +** Summary: MIDI port interface |
| 25 | +** Section: ports |
| 26 | +** Author: Oldes |
| 27 | +** Notes: |
| 28 | +** |
| 29 | +***********************************************************************/ |
| 30 | + |
| 31 | +#include "sys-core.h" |
| 32 | + |
| 33 | +/*********************************************************************** |
| 34 | +** |
| 35 | +*/ static REBOOL Query_MIDI_Field(REBSER *obj, REBCNT field, REBVAL *ret) |
| 36 | +/* |
| 37 | +** Set a value with MIDI related data according specified field |
| 38 | +** The `obj` must be system/standard/midi-info with filled info |
| 39 | +** |
| 40 | +***********************************************************************/ |
| 41 | +{ |
| 42 | + switch (field) { |
| 43 | + case SYM_DEVICES_IN: |
| 44 | + *ret = *Get_Field(obj, STD_MIDI_INFO_DEVICES_IN); |
| 45 | + return TRUE; |
| 46 | + case SYM_DEVICES_OUT: |
| 47 | + *ret = *Get_Field(obj, STD_MIDI_INFO_DEVICES_OUT); |
| 48 | + return TRUE; |
| 49 | + default: |
| 50 | + return FALSE; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/*********************************************************************** |
| 55 | +** |
| 56 | +*/ static int MIDI_Actor(REBVAL *ds, REBSER *port, REBCNT action) |
| 57 | +/* |
| 58 | +***********************************************************************/ |
| 59 | +{ |
| 60 | + REBREQ *req; |
| 61 | + REBINT result; |
| 62 | + REBVAL *arg; |
| 63 | + REBCNT args = 0; |
| 64 | + REBINT len, n; |
| 65 | + REBSER *ser; |
| 66 | + REBVAL *spec; |
| 67 | + REBVAL *val1, *val2; |
| 68 | + |
| 69 | + //printf("MIDI_Actor action: %i\n", action); |
| 70 | + |
| 71 | + Validate_Port(port, action); |
| 72 | + |
| 73 | + req = Use_Port_State(port, RDI_MIDI, sizeof(REBREQ)); |
| 74 | + |
| 75 | + switch (action) { |
| 76 | + |
| 77 | + case A_READ: |
| 78 | + // This device is opened on the READ: |
| 79 | + if (!IS_OPEN(req)) Trap_Port(RE_NOT_OPEN, port, -12); |
| 80 | + |
| 81 | + result = OS_DO_DEVICE(req, RDC_READ); |
| 82 | + if (result < 0) Trap_Port(RE_READ_ERROR, port, req->error); |
| 83 | + |
| 84 | + // Copy and set the string result: |
| 85 | + arg = OFV(port, STD_PORT_DATA); |
| 86 | + //arg = D_ARG(1); |
| 87 | + SET_BINARY(arg, Copy_Bytes(req->data, req->actual)); |
| 88 | + req->data = 0; |
| 89 | + *D_RET = *arg; |
| 90 | + return R_RET; |
| 91 | + |
| 92 | + case A_WRITE: |
| 93 | + if (!IS_OPEN(req)) Trap_Port(RE_NOT_OPEN, port, -12); |
| 94 | + arg = D_ARG(2); |
| 95 | + if(IS_BINARY(arg)) { |
| 96 | + req->data = VAL_BIN(arg); |
| 97 | + req->length = VAL_LEN(arg); |
| 98 | + |
| 99 | + result = OS_DO_DEVICE(req, RDC_WRITE); |
| 100 | + if (result < 0) Trap_Port(RE_WRITE_ERROR, port, req->error); |
| 101 | + } |
| 102 | + break; |
| 103 | + |
| 104 | + case A_OPEN: |
| 105 | + spec = OFV(port, STD_PORT_SPEC); |
| 106 | + args = Find_Refines(ds, AM_OPEN_READ | AM_OPEN_WRITE); |
| 107 | + req->modes = args; |
| 108 | + if (IS_OBJECT(spec)) { |
| 109 | + val1 = Obj_Value(spec, STD_PORT_SPEC_MIDI_DEVICE_IN); |
| 110 | + if (IS_INTEGER(val1)) { |
| 111 | + req->midi.device_in = VAL_INT32(val1); |
| 112 | + //printf(";;;;;;;; requested device-in: %i\n", req->midi.device_in); |
| 113 | + } |
| 114 | + val2 = Obj_Value(spec, STD_PORT_SPEC_MIDI_DEVICE_OUT); |
| 115 | + if (IS_INTEGER(val2)) { |
| 116 | + req->midi.device_out = VAL_INT32(val2); |
| 117 | + //printf(";;;;;;;; requested device-out: %i\n", req->midi.device_out); |
| 118 | + } else if ((args & AM_OPEN_WRITE) && !(args & AM_OPEN_READ)) { |
| 119 | + // it was opened using: open/WRITE midi://device:NUM |
| 120 | + req->midi.device_out = req->midi.device_in; |
| 121 | + req->midi.device_in = 0; |
| 122 | + SET_NONE(val1); |
| 123 | + SET_INTEGER(val2, req->midi.device_out); |
| 124 | + } |
| 125 | + } |
| 126 | + if (OS_DO_DEVICE(req, RDC_OPEN)) Trap_Port(RE_CANNOT_OPEN, port, req->error); |
| 127 | + |
| 128 | + break; |
| 129 | + |
| 130 | + case A_CLOSE: |
| 131 | + if (!IS_OPEN(req)) Trap_Port(RE_NOT_OPEN, port, -12); |
| 132 | + OS_DO_DEVICE(req, RDC_CLOSE); |
| 133 | + break; |
| 134 | + |
| 135 | + case A_QUERY: |
| 136 | + spec = Get_System(SYS_STANDARD, STD_MIDI_INFO); |
| 137 | + if (!IS_OBJECT(spec)) Trap_Arg(spec); |
| 138 | + if (D_REF(2) && IS_NONE(D_ARG(3))) { |
| 139 | + // query/mode midi:// none ;<-- lists possible fields to request |
| 140 | + Set_Block(D_RET, Get_Object_Words(spec)); |
| 141 | + return R_RET; |
| 142 | + } |
| 143 | + |
| 144 | + REBSER *obj = CLONE_OBJECT(VAL_OBJ_FRAME(spec)); |
| 145 | + Set_Block(Get_Field(obj, STD_MIDI_INFO_DEVICES_IN), Make_Block(7)); |
| 146 | + Set_Block(Get_Field(obj, STD_MIDI_INFO_DEVICES_OUT), Make_Block(7)); |
| 147 | + req->data = (REBYTE*)obj; |
| 148 | + OS_DO_DEVICE(req, RDC_QUERY); |
| 149 | + |
| 150 | + if (D_REF(2)) { |
| 151 | + // query/mode used |
| 152 | + REBVAL *field = D_ARG(3); |
| 153 | + if (IS_WORD(field)) { |
| 154 | + if (!Query_MIDI_Field(obj, VAL_WORD_SYM(field), D_RET)) |
| 155 | + Trap_Reflect(VAL_TYPE(D_ARG(1)), field); // better error? |
| 156 | + } |
| 157 | + else if (IS_BLOCK(field)) { |
| 158 | + REBVAL *val; |
| 159 | + REBSER *values = Make_Block(2 * BLK_LEN(VAL_SERIES(field))); |
| 160 | + REBVAL *word = VAL_BLK_DATA(field); |
| 161 | + for (; NOT_END(word); word++) { |
| 162 | + if (ANY_WORD(word)) { |
| 163 | + if (IS_SET_WORD(word)) { |
| 164 | + // keep the set-word in result |
| 165 | + val = Append_Value(values); |
| 166 | + *val = *word; |
| 167 | + VAL_SET_LINE(val); |
| 168 | + } |
| 169 | + val = Append_Value(values); |
| 170 | + if (!Query_MIDI_Field(obj, VAL_WORD_SYM(word), val)) |
| 171 | + Trap1(RE_INVALID_ARG, word); |
| 172 | + } |
| 173 | + else Trap1(RE_INVALID_ARG, word); |
| 174 | + } |
| 175 | + Set_Series(REB_BLOCK, D_RET, values); |
| 176 | + } |
| 177 | + return R_RET; |
| 178 | + } |
| 179 | + Set_Object(D_RET, obj); |
| 180 | + return R_RET; |
| 181 | + |
| 182 | + case A_OPENQ: |
| 183 | + if (IS_OPEN(req)) return R_TRUE; |
| 184 | + return R_FALSE; |
| 185 | + |
| 186 | + case A_UPDATE: |
| 187 | + // Update the port object after a READ or WRITE operation. |
| 188 | + // This is normally called by the WAKE-UP function. |
| 189 | + arg = OFV(port, STD_PORT_DATA); |
| 190 | + //printf("MIDI Update action, req->command = %d\n", req->command); |
| 191 | + if (req->command == RDC_READ) { |
| 192 | + if (ANY_BINSTR(arg)) VAL_TAIL(arg) += req->actual; |
| 193 | + } |
| 194 | + else if (req->command == RDC_WRITE) { |
| 195 | + SET_NONE(arg); // Write is done. |
| 196 | + } |
| 197 | + return R_NONE; |
| 198 | + |
| 199 | + default: |
| 200 | + Trap_Action(REB_PORT, action); |
| 201 | + } |
| 202 | + |
| 203 | + return R_ARG1; // port |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +/*********************************************************************** |
| 208 | +** |
| 209 | +*/ void Init_MIDI_Scheme(void) |
| 210 | +/* |
| 211 | +***********************************************************************/ |
| 212 | +{ |
| 213 | + Register_Scheme(SYM_MIDI, 0, MIDI_Actor); |
| 214 | +} |
0 commit comments