Debian Codegraph @

src/core/chuck_io.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // name: chuck_io.cpp
28 // desc: chuck i/o
29 //
30 // authors: Spencer Salazar ([email protected])
31 // date: Summer 2012
32 //-----------------------------------------------------------------------------
33 #include "chuck_io.h"
34 #include "chuck_errmsg.h"
35 #include "chuck_instr.h"
36 #include "chuck_type.h"
37 #include "chuck_vm.h"
38 #include "util_platforms.h"
39 #include "util_string.h"
40
41 #ifndef __DISABLE_HID__
42 #include "hidio_sdl.h"
43 #endif
44
45 #ifndef __DISABLE_MIDI__
46 #include "midiio_rtmidi.h"
47 #endif
48
49 #ifndef __DISABLE_SERIAL__
50 #include "util_serial.h"
51 #endif
52
53 #ifndef __PLATFORM_WINDOWS__
54 #include <sys/select.h>
55 #include <poll.h>
56 #include <termios.h>
57 #include <unistd.h>
58 #else
59 #include <io.h>
60 #include "dirent_win32.h"
61 #endif // __PLATFORM_WINDOWS__
62
63 #include <fcntl.h>
64 #include <math.h>
65 #include <stdint.h> // 1.5.0.5
66
67 #include <iomanip>
68 #include <iostream>
69 using namespace std;
70
71
72 #ifdef __PLATFORM_WINDOWS__
73 typedef BYTE uint8_t;
74 typedef WORD uint16_t;
75 // ge: this needed in earlier/some versions of windows
76 // 1.5.0.7 (ge) make "__WINDOWS_MODERN__" default
77 #ifdef __WINDOWS_OLDSCHOOL__ // was: #ifndef __WINDOWS_MODERN__
78 typedef __int32 int32_t; // 1.4.1.0 (ge) added
79 typedef DWORD uint32_t;
80 #endif
81 #endif
82
83 // for legacy use of disable fileio | 1.5.1.5
84 #ifdef __DISABLE_FILEIO__
85 // auto define new, more granular macro
86 #ifndef __DISABLE_ASYNCH_IO__
87 #define __DISABLE_ASYNCH_IO__
88 #endif
89 #endif
90
91
92 // global
93 static Chuck_String * g_newline = new Chuck_String();
94
95
96
97
98 //-----------------------------------------------------------------------------
99 // name: init_class_io()
100 // desc: ...
101 //-----------------------------------------------------------------------------
102 t_CKBOOL init_class_io( Chuck_Env * env, Chuck_Type * type )
103 {
104 // init as base class
105 Chuck_DL_Func * func = NULL;
106 std::string doc = "Base class for other IO classes such as FileIO, StdOut and StdErr. Besides IO.newline(), it’s unlikely you need to use this class directly.";
107
108 // log
109 EM_log( CK_LOG_HERALD, "class 'IO'" );
110
111 // init as base class
112 // TODO: ctor/dtor?
113 // TODO: replace dummy with pure function
114 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, doc.c_str() ) )
115 return FALSE;
116
117 // add good()
118 func = make_new_mfun( "int", "good", io_dummy_int );
119 func->doc = "Returns whether IO is ready for reading.";
120 if( !type_engine_import_mfun( env, func ) ) goto error;
121
122 // add close()
123 func = make_new_mfun( "void", "close", io_dummy_void );
124 func->doc = "Close the currently open IO.";
125 if( !type_engine_import_mfun( env, func ) ) goto error;
126
127 // add flush()
128 func = make_new_mfun( "void", "flush", io_dummy_void );
129 func->doc = "Write any buffered output.";
130 if( !type_engine_import_mfun( env, func ) ) goto error;
131
132 // add mode(int)
133 func = make_new_mfun( "int", "mode", io_dummy_int );
134 func->add_arg( "int", "flag" );
135 func->doc = "Set the current IO mode; either IO.MODE_ASYNC or IO.MODE_SYNC.";
136 if( !type_engine_import_mfun( env, func ) ) goto error;
137
138 // add mode()
139 func = make_new_mfun( "int", "mode", io_dummy_int );
140 func->doc = "Get the current IO mode; either IO.MODE_ASYNC or IO.MODE_SYNC.";
141 if( !type_engine_import_mfun( env, func ) ) goto error;
142
143 // TODO: add this later?
144 // add read()
145 // func = make_new_mfun( "string", "read", io_dummy_string );
146 // func->add_arg( "int", "length" );
147 // if( !type_engine_import_mfun( env, func ) ) goto error;
148
149 // add readLine()
150 func = make_new_mfun( "string", "readLine", io_dummy_string );
151 func->doc = "Read until an end-of-line character.";
152 if( !type_engine_import_mfun( env, func ) ) goto error;
153
154 // add readInt()
155 func = make_new_mfun( "int", "readInt", io_dummy_int );
156 func->add_arg( "int", "flags" );
157 func->doc = "Read and return the next integer; binary mode: 'flags' denotes the bit-size of int (IO.INT8, IO.INT16, or IO.INT32).";
158 if( !type_engine_import_mfun( env, func ) ) goto error;
159
160 // add readFloat()
161 func = make_new_mfun( "float", "readFloat", io_dummy_float );
162 func->add_arg( "int", "flags" );
163 func->doc = "Read and return the next floating point value; binary mode: 'flags' denotes the size of float to read (IO.FLOAT32 or IO.FLOAT64).";
164 if( !type_engine_import_mfun( env, func ) ) goto error;
165
166 // add eof()
167 func = make_new_mfun( "int", "eof", io_dummy_int );
168 func->doc = "Return whether end-of-file has been reached; the opposite of .more().";
169 if( !type_engine_import_mfun( env, func ) ) goto error;
170
171 // add more()
172 func = make_new_mfun( "int", "more", io_dummy_int );
173 func->doc = "Return whether there is more to read; the opposite of .eof().";
174 if( !type_engine_import_mfun( env, func ) ) goto error;
175
176 // add write(string)
177 func = make_new_mfun( "void", "write", io_dummy_void );
178 func->add_arg( "string", "val" );
179 func->doc = "Write string 'val'.";
180 if( !type_engine_import_mfun( env, func ) ) goto error;
181
182 // add write(int)
183 func = make_new_mfun( "void", "write", io_dummy_void );
184 func->add_arg( "int", "val" );
185 func->doc = "Write integer 'val'.";
186 if( !type_engine_import_mfun( env, func ) ) goto error;
187
188 // add write(float)
189 func = make_new_mfun( "void", "write", io_dummy_void );
190 func->add_arg( "float", "val" );
191 func->doc = "Write floating point number 'val'.";
192 if( !type_engine_import_mfun( env, func ) ) goto error;
193
194 // add newline
195 func = make_new_sfun( "string", "newline", io_newline );
196 func->doc = "newline character; same as IO.nl().";
197 if( !type_engine_import_sfun( env, func ) ) goto error;
198 // add nl
199 func = make_new_sfun( "string", "nl", io_newline );
200 func->doc = "newline character; same as IO.newline().";
201 if( !type_engine_import_sfun( env, func ) ) goto error;
202 // hmm
203 // func = make_new_sfun( "string", "newlineEx2VistaHWNDVisualFoxProA", io_newline );
204 // func->doc = "_";
205 // if( !type_engine_import_sfun( env, func ) ) goto error;
206 // new line string
207 initialize_object( g_newline, env->ckt_string, NULL, NULL );
208 // add reference, since we are keeping a reference of it | 1.5.1.7
209 CK_SAFE_ADD_REF( g_newline );
210 // also lock it | 1.5.1.7
211 g_newline->lock();
212 // set as newline
213 g_newline->set( "\n" );
214
215 // add TYPE_ASCII
216 if( !type_engine_import_svar( env, "int", "ASCII",
217 TRUE, (t_CKUINT)&Chuck_IO::TYPE_ASCII, "Flag denoting ASCII IO mode." ) ) goto error;
218 // add TYPE_BINARY
219 if( !type_engine_import_svar( env, "int", "BINARY",
220 TRUE, (t_CKUINT)&Chuck_IO::TYPE_BINARY, "Flag denoting binary IO mode." ) ) goto error;
221 // add INT8
222 if( !type_engine_import_svar( env, "int", "INT8",
223 TRUE, (t_CKUINT)&Chuck_IO::INT8, "Flag denoting 8-bit integer type." ) ) goto error;
224 // add INT16
225 if( !type_engine_import_svar( env, "int", "INT16",
226 TRUE, (t_CKUINT)&Chuck_IO::INT16, "Flag denoting 16-bit integer type." ) ) goto error;
227 // add INT24 | 1.5.0.1 (ge) added
228 if( !type_engine_import_svar( env, "int", "INT24",
229 TRUE, (t_CKUINT)&Chuck_IO::INT24, "Flag denoting 24-bit integer type." ) ) goto error;
230 // add INT32
231 if( !type_engine_import_svar( env, "int", "INT32",
232 TRUE, (t_CKUINT)&Chuck_IO::INT32, "Flag denoting 32-bit integer type." ) ) goto error;
233 // add INT64 | 1.5.0.1 (ge) added
234 if( !type_engine_import_svar( env, "int", "INT64",
235 TRUE, (t_CKUINT)&Chuck_IO::INT64, "Flag denoting 64-bit integer type." ) ) goto error;
236 // add SINT8 | 1.5.0.1 (ge) added
237 if( !type_engine_import_svar( env, "int", "SINT8",
238 TRUE, (t_CKUINT)&Chuck_IO::SINT8, "Flag denoting 8-bit signed integer type." ) ) goto error;
239 // add SINT16 | 1.5.0.1 (ge) added
240 if( !type_engine_import_svar( env, "int", "SINT16",
241 TRUE, (t_CKUINT)&Chuck_IO::SINT16, "Flag denoting 16-bit signed integer type." ) ) goto error;
242 // add SINT24 | 1.5.0.1 (ge) added
243 if( !type_engine_import_svar( env, "int", "SINT24",
244 TRUE, (t_CKUINT)&Chuck_IO::SINT24, "Flag denoting 24-bit signed integer type." ) ) goto error;
245 // add SINT32 | 1.5.0.1 (ge) added
246 if( !type_engine_import_svar( env, "int", "SINT32",
247 TRUE, (t_CKUINT)&Chuck_IO::SINT32, "Flag denoting 32-bit signed integer type." ) ) goto error;
248 // add SINT64 | 1.5.0.1 (ge) added
249 if( !type_engine_import_svar( env, "int", "SINT64",
250 TRUE, (t_CKUINT)&Chuck_IO::SINT64, "Flag denoting 64-bit signed integer type." ) ) goto error;
251 // add UINT8 | 1.5.0.1 (ge) added
252 if( !type_engine_import_svar( env, "int", "UINT8",
253 TRUE, (t_CKUINT)&Chuck_IO::UINT8, "Flag denoting 8-bit unsigned integer type." ) ) goto error;
254 // add UINT16 | 1.5.0.1 (ge) added
255 if( !type_engine_import_svar( env, "int", "UINT16",
256 TRUE, (t_CKUINT)&Chuck_IO::UINT16, "Flag denoting 16-bit unsigned integer type." ) ) goto error;
257 // add UINT24 | 1.5.0.1 (ge) added
258 if( !type_engine_import_svar( env, "int", "UINT24",
259 TRUE, (t_CKUINT)&Chuck_IO::UINT24, "Flag denoting 24-bit unsigned integer type." ) ) goto error;
260 // add UINT32 | 1.5.0.1 (ge) added
261 if( !type_engine_import_svar( env, "int", "UINT32",
262 TRUE, (t_CKUINT)&Chuck_IO::UINT32, "Flag denoting 32-bit unsigned integer type." ) ) goto error;
263 // add UINT64 | 1.5.0.1 (ge) added
264 if( !type_engine_import_svar( env, "int", "UINT64",
265 TRUE, (t_CKUINT)&Chuck_IO::UINT64, "Flag denoting 64-bit unsigned integer type." ) ) goto error;
266 // add FLOAT32 | 1.5.0.1 (ge) added
267 if( !type_engine_import_svar( env, "int", "FLOAT32",
268 TRUE, (t_CKUINT)&Chuck_IO::FLOAT32, "Flag denoting 32-bit floating point type." ) ) goto error;
269 // add FLOAT64 | 1.5.0.1 (ge) added
270 if( !type_engine_import_svar( env, "int", "FLOAT64",
271 TRUE, (t_CKUINT)&Chuck_IO::FLOAT64, "Flag denoting 64-bit floating point type." ) ) goto error;
272 // add MODE_SYNC
273 if( !type_engine_import_svar( env, "int", "MODE_SYNC",
274 TRUE, (t_CKUINT)&Chuck_IO::MODE_SYNC, "Flag denoting synchronous IO." ) ) goto error;
275 #ifndef __DISABLE_ASYNCH_IO__
276 // add MODE_ASYNC
277 if( !type_engine_import_svar( env, "int", "MODE_ASYNC",
278 TRUE, (t_CKUINT)&Chuck_IO::MODE_ASYNC, "Flag denoting asychronous IO." ) ) goto error;
279 #endif
280 // add FLAG_READONLY
281 if( !type_engine_import_svar( env, "int", "READ",
282 TRUE, (t_CKUINT)&Chuck_IO::FLAG_READONLY, "Flag denoting read mode." ) ) goto error;
283 // add FLAG_WRITEONLY
284 if( !type_engine_import_svar( env, "int", "WRITE",
285 TRUE, (t_CKUINT)&Chuck_IO::FLAG_WRITEONLY, "Flag denoting write mode." ) ) goto error;
286 // add FLAG_APPEND
287 if( !type_engine_import_svar( env, "int", "APPEND",
288 TRUE, (t_CKUINT)&Chuck_IO::FLAG_APPEND, "Flag denoting append mode." ) ) goto error;
289 // add FLAG_READ_WRITE
290 if( !type_engine_import_svar( env, "int", "READ_WRITE",
291 TRUE, (t_CKUINT)&Chuck_IO::FLAG_READ_WRITE, "Flag denoting read/write mode." ) ) goto error;
292
293 // end the class import
294 type_engine_import_class_end( env );
295
296 return TRUE;
297
298 error:
299
300 // end the class import
301 type_engine_import_class_end( env );
302
303 return FALSE;
304 }
305
306
307
308
309 //-----------------------------------------------------------------------------
310 // name: init_class_fileio()
311 // desc: ...
312 //-----------------------------------------------------------------------------
313 t_CKBOOL init_class_fileio( Chuck_Env * env, Chuck_Type * type )
314 {
315 // init as base class
316 Chuck_DL_Func * func = NULL;
317
318 // log
319 EM_log( CK_LOG_HERALD, "class 'FileIO'" );
320
321 // init as base class
322 // TODO: ctor/dtor?
323 // TODO: replace dummy with pure function
324 if( !type_engine_import_class_begin( env, type, env->global(), fileio_ctor, fileio_dtor,
325 "File input and output utilities for reading, writing, seeking, etc. See examples for usage." ) )
326 return FALSE;
327
328 // add open(string)
329 func = make_new_mfun( "int", "open", fileio_open );
330 func->add_arg( "string", "path" );
331 func->doc = "Open a file by name (and by default in ASCII mode).";
332 if( !type_engine_import_mfun( env, func ) ) goto error;
333
334 // add open(string, flags)
335 func = make_new_mfun( "int", "open", fileio_openflags );
336 func->add_arg( "string", "path" );
337 func->add_arg( "int", "flags" );
338 func->doc = "Open a file by name with flags (bitwise combinations of IO.READ, IO.WRITE, IO.READ_WRITE, IO_APPEND, IO.ASCII, IO.BINARY).";
339 if( !type_engine_import_mfun( env, func ) ) goto error;
340
341 // add good()
342 func = make_new_mfun( "int", "good", fileio_good );
343 func->doc = "Returns whether the file is ready for reading.";
344 if( !type_engine_import_mfun( env, func ) ) goto error;
345
346 // add close()
347 func = make_new_mfun( "void", "close", fileio_close );
348 func->doc = "Close (and flush) the currently open file.";
349 if( !type_engine_import_mfun( env, func ) ) goto error;
350
351 // add flush()
352 func = make_new_mfun( "void", "flush", fileio_flush );
353 func->doc = "Write any buffered output to file.";
354 if( !type_engine_import_mfun( env, func ) ) goto error;
355
356 // add mode()
357 func = make_new_mfun( "int", "mode", fileio_getmode );
358 func->doc = "Get file IO mode (IO.MODE_SYNC or IO.MODE_ASYNC).";
359 if( !type_engine_import_mfun( env, func ) ) goto error;
360
361 // add mode(int)
362 func = make_new_mfun( "int", "mode", fileio_setmode );
363 func->add_arg( "int", "flag" );
364 func->doc = "Set file IO mode (IO.MODE_SYNC or IO.MODE_ASYNC).";
365 if( !type_engine_import_mfun( env, func ) ) goto error;
366
367 // add size()
368 func = make_new_mfun( "int", "size", fileio_size );
369 func->doc = "Return the size of the file in bytes, or -1 if no file is opened or if a directory is opened.";
370 if( !type_engine_import_mfun( env, func ) ) goto error;
371
372 // add seek(int)
373 func = make_new_mfun( "void", "seek", fileio_seek );
374 func->add_arg( "int", "pos" );
375 func->doc = "Seek to a specified byte offset in file.";
376 if( !type_engine_import_mfun( env, func ) ) goto error;
377
378 // add tell()
379 func = make_new_mfun( "int", "tell", fileio_tell );
380 func->doc = "Return the byte read offset of the file, or -1 if no file is opened.";
381 if( !type_engine_import_mfun( env, func ) ) goto error;
382
383 // add isDir()
384 func = make_new_mfun( "int", "isDir", fileio_isdir );
385 func->doc = "Return if the open file is a directory.";
386 if( !type_engine_import_mfun( env, func ) ) goto error;
387
388 // add dirList()
389 func = make_new_mfun( "string[]", "dirList", fileio_dirlist );
390 func->doc = "Get an array of file names in an open directory.";
391 if( !type_engine_import_mfun( env, func ) ) goto error;
392
393 // add read()
394 // func = make_new_mfun( "string", "read", fileio_read );
395 // func->add_arg( "int", "length" );
396 // if( !type_engine_import_mfun( env, func ) ) goto error;
397
398 // add readLine()
399 func = make_new_mfun( "string", "readLine", fileio_readline );
400 func->doc = "Read and return the next line from file.";
401 if( !type_engine_import_mfun( env, func ) ) goto error;
402
403 // add readInt()
404 // func = make_new_mfun( "int", "readInt", fileio_readint );
405 // if( !type_engine_import_mfun( env, func ) ) goto error;
406
407 // add readInt(int)
408 func = make_new_mfun( "int", "readInt", fileio_readintflags );
409 func->add_arg( "int", "flags" );
410 func->doc = "Read and return an integer; binary mode: 'flags' specifies int size to read (IO.INT8, IO.INT16, IO.INT32 default to unsigned values; for signed integers use IO.SINT8, IO.SINT16, IO.SINT32).";
411 if( !type_engine_import_mfun( env, func ) ) goto error;
412
413 // add readFloat()
414 func = make_new_mfun( "float", "readFloat", fileio_readfloat );
415 func->doc = "Read and return the next floating point value.";
416 if( !type_engine_import_mfun( env, func ) ) goto error;
417
418 // add readFloat()
419 func = make_new_mfun( "float", "readFloat", fileio_readfloatflags );
420 func->add_arg( "int", "flags" );
421 func->doc = "Read and return the next floating point value; if binary mode: 'flags' denotes the size of float to read (IO.FLOAT32 or IO.FLOAT64).";
422 if( !type_engine_import_mfun( env, func ) ) goto error;
423
424 // add eof()
425 func = make_new_mfun( "int", "eof", fileio_eof );
426 func->doc = "Return whether end-of-file has been reached; the opposite of .more().";
427 if( !type_engine_import_mfun( env, func ) ) goto error;
428
429 // add more()
430 func = make_new_mfun( "int", "more", fileio_more );
431 func->doc = "Return whether there is more to read; the opposite of .eof().";
432 if( !type_engine_import_mfun( env, func ) ) goto error;
433
434 // add write(string)
435 func = make_new_mfun( "void", "write", fileio_writestring );
436 func->add_arg( "string", "val" );
437 func->doc = "Write a string to file.";
438 if( !type_engine_import_mfun( env, func ) ) goto error;
439
440 // add write(int)
441 func = make_new_mfun( "void", "write", fileio_writeint );
442 func->add_arg( "int", "val" );
443 func->doc = "Write an integer to file.";
444 if( !type_engine_import_mfun( env, func ) ) goto error;
445
446 // add write(int,flags)
447 func = make_new_mfun( "void", "write", fileio_writeintflags );
448 func->add_arg( "int", "val" );
449 func->add_arg( "int", "flags" );
450 func->doc = "Write integer value to file; binary mode: int size specified by 'flags' (IO.INT8, IO.INT16, IO.INT32).";
451 if( !type_engine_import_mfun( env, func ) ) goto error;
452
453 // add write(float)
454 func = make_new_mfun( "void", "write", fileio_writefloat );
455 func->add_arg( "float", "val" );
456 func->doc = "Write floating point value to file.";
457 if( !type_engine_import_mfun( env, func ) ) goto error;
458
459 // add write(float)
460 func = make_new_mfun( "void", "write", fileio_writefloatflags );
461 func->add_arg( "float", "val" );
462 func->add_arg( "int", "flags" );
463 func->doc = "Write floating point value to file; binary mode: flags indicate float size (IO.FLOAT32 or IO.FLOAT64).";
464 if( !type_engine_import_mfun( env, func ) ) goto error;
465
466 // add autoPrefixAndExt() | 1.5.1.5 (ge)
467 func = make_new_mfun( "void", "autoPrefixExtension", file_ctrl_autoPrefixAndExtension ); //! set auto prefix and extension string
468 func->add_arg( "string", "prefix" );
469 func->add_arg( "string", "extension" );
470 func->doc = "set auto prefix and extension for \"special:auto\" filename generation (applicable to file writing only).";
471 if( !type_engine_import_mfun( env, func ) ) goto error;
472
473 // add autoPrefix()
474 func = make_new_mfun( "string", "autoPrefix", file_cget_autoPrefix ); //! get auto prefix string
475 func->doc = "get auto prefix for \"special:auto\" filename generation (applicable to file writing only).";
476 if( !type_engine_import_mfun( env, func ) ) goto error;
477
478 // add autoExtension()
479 func = make_new_mfun( "string", "autoExtension", file_cget_autoExtension ); //! get auto extension string
480 func->doc = "get auto extension for \"special:auto\" filename generation (applicable to file writing only).";
481 if( !type_engine_import_mfun( env, func ) ) goto error;
482
483 // add filename()
484 func = make_new_mfun( "string", "filename", file_cget_filename ); //! get auto extension string
485 func->doc = "get current filename.";
486 if( !type_engine_import_mfun( env, func ) ) goto error;
487
488 // add expandPath | 1.5.1.3
489 func = make_new_sfun( "string", "expandPath", fileio_expandpath_impl );
490 func->add_arg( "string", "path" );
491 func->doc = "expand platform-specific filepath to an absolute path, which is returned. On macOS and Linux expandPath() will attempt to resolve `~` or `~[username]`; on Windows expandPath() will attempt to resolve %USERNAME%. (Known issue: (macOS) expandPath currently introduced an audio click; it recommended to call expandPath() at the beginning; e.g., expanding path ahead of time could avoid a click instead of calling Machine.add() on a filepath with `~`.)";
492 if( !type_engine_import_sfun( env, func ) ) goto error;
493
494 // add examples
495 if( !type_engine_import_add_ex( env, "io/chout.ck" ) ) goto error;
496 if( !type_engine_import_add_ex( env, "io/read-float.ck" ) ) goto error;
497 if( !type_engine_import_add_ex( env, "io/read-int.ck" ) ) goto error;
498 if( !type_engine_import_add_ex( env, "io/read-line.ck" ) ) goto error;
499 if( !type_engine_import_add_ex( env, "io/read-str.ck" ) ) goto error;
500 if( !type_engine_import_add_ex( env, "io/read-tokens.ck" ) ) goto error;
501 if( !type_engine_import_add_ex( env, "io/seek.ck" ) ) goto error;
502 if( !type_engine_import_add_ex( env, "io/write.ck" ) ) goto error;
503 if( !type_engine_import_add_ex( env, "io/write2.ck" ) ) goto error;
504 if( !type_engine_import_add_ex( env, "io/write-auto.ck" ) ) goto error;
505 if( !type_engine_import_add_ex( env, "io/read-byte.ck" ) ) goto error;
506 if( !type_engine_import_add_ex( env, "io/write-byte.ck" ) ) goto error;
507
508 // end the class import
509 type_engine_import_class_end( env );
510
511 return TRUE;
512
513 error:
514
515 // end the class import
516 type_engine_import_class_end( env );
517
518 return FALSE;
519 }
520
521
522
523
524 //-----------------------------------------------------------------------------
525 // name: init_class_chout()
526 // desc: added 1.3.0.0 as full class
527 //-----------------------------------------------------------------------------
528 t_CKBOOL init_class_chout( Chuck_Env * env, Chuck_Type * type )
529 {
530 // init as base class
531 Chuck_DL_Func * func = NULL;
532
533 // log
534 EM_log( CK_LOG_HERALD, "class 'chout'" );
535
536 // TODO: ctor/dtor?
537 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, "Do not instantiate this class directly! Use 'chout' instead." ) )
538 return FALSE;
539
540 // add good()
541 func = make_new_mfun( "int", "good", chout_good );
542 if( !type_engine_import_mfun( env, func ) ) goto error;
543
544 // add close()
545 func = make_new_mfun( "void", "close", chout_close );
546 if( !type_engine_import_mfun( env, func ) ) goto error;
547
548 // add flush()
549 func = make_new_mfun( "void", "flush", chout_flush );
550 if( !type_engine_import_mfun( env, func ) ) goto error;
551
552 // add mode()
553 func = make_new_mfun( "int", "mode", chout_getmode );
554 if( !type_engine_import_mfun( env, func ) ) goto error;
555
556 // add mode(int)
557 func = make_new_mfun( "int", "mode", chout_setmode );
558 func->add_arg( "int", "flag" );
559 if( !type_engine_import_mfun( env, func ) ) goto error;
560
561 // add readLine()
562 func = make_new_mfun( "string", "readLine", chout_readline );
563 if( !type_engine_import_mfun( env, func ) ) goto error;
564
565 // add readInt(int)
566 func = make_new_mfun( "int", "readInt", chout_readintflags );
567 func->add_arg( "int", "flags" );
568 if( !type_engine_import_mfun( env, func ) ) goto error;
569
570 // add readFloat()
571 // func = make_new_mfun( "float", "readFloat", chout_readfloat );
572 // func->add_arg( "int", "flags" );
573 // if( !type_engine_import_mfun( env, func ) ) goto error;
574
575 // add eof()
576 func = make_new_mfun( "int", "eof", chout_eof );
577 if( !type_engine_import_mfun( env, func ) ) goto error;
578
579 // add more()
580 func = make_new_mfun( "int", "more", chout_more );
581 if( !type_engine_import_mfun( env, func ) ) goto error;
582
583 // add write(string)
584 func = make_new_mfun( "void", "write", chout_writestring );
585 func->add_arg( "string", "val" );
586 if( !type_engine_import_mfun( env, func ) ) goto error;
587
588 // add write(int)
589 func = make_new_mfun( "void", "write", chout_writeint );
590 func->add_arg( "int", "val" );
591 if( !type_engine_import_mfun( env, func ) ) goto error;
592
593 // add write(float)
594 func = make_new_mfun( "void", "write", chout_writefloat );
595 func->add_arg( "float", "val" );
596 if( !type_engine_import_mfun( env, func ) ) goto error;
597
598 // end the class import
599 type_engine_import_class_end( env );
600
601 return TRUE;
602
603 error:
604
605 // end the class import
606 type_engine_import_class_end( env );
607
608 return FALSE;
609 }
610
611
612
613
614 //-----------------------------------------------------------------------------
615 // name: init_class_cherr()
616 // desc: added 1.3.0.0 -- as full class
617 //-----------------------------------------------------------------------------
618 t_CKBOOL init_class_cherr( Chuck_Env * env, Chuck_Type * type )
619 {
620 // init as base class
621 Chuck_DL_Func * func = NULL;
622
623 // log
624 EM_log( CK_LOG_HERALD, "class 'cherr'" );
625
626 // TODO: ctor/dtor?
627 if( !type_engine_import_class_begin( env, type, env->global(), NULL, NULL, "Do not instantiate this class directly! Use 'cherr' instead." ) )
628 return FALSE;
629
630 // add good()
631 func = make_new_mfun( "int", "good", cherr_good );
632 if( !type_engine_import_mfun( env, func ) ) goto error;
633
634 // add close()
635 func = make_new_mfun( "void", "close", cherr_close );
636 if( !type_engine_import_mfun( env, func ) ) goto error;
637
638 // add flush()
639 func = make_new_mfun( "void", "flush", cherr_flush );
640 if( !type_engine_import_mfun( env, func ) ) goto error;
641
642 // add mode()
643 func = make_new_mfun( "int", "mode", cherr_getmode );
644 if( !type_engine_import_mfun( env, func ) ) goto error;
645
646 // add mode(int)
647 func = make_new_mfun( "int", "mode", cherr_setmode );
648 func->add_arg( "int", "flag" );
649 if( !type_engine_import_mfun( env, func ) ) goto error;
650
651 // add readLine()
652 func = make_new_mfun( "string", "readLine", cherr_readline );
653 if( !type_engine_import_mfun( env, func ) ) goto error;
654
655 // add readInt(int)
656 // func = make_new_mfun( "int", "readInt", cherr_readintflags );
657 // func->add_arg( "int", "flags" );
658 // if( !type_engine_import_mfun( env, func ) ) goto error;
659
660 // add readFloat()
661 // func = make_new_mfun( "float", "readFloat", cherr_readfloat );
662 // func->add_arg( "int", "flags" );
663 // if( !type_engine_import_mfun( env, func ) ) goto error;
664
665 // add eof()
666 func = make_new_mfun( "int", "eof", cherr_eof );
667 if( !type_engine_import_mfun( env, func ) ) goto error;
668
669 // add more()
670 func = make_new_mfun( "int", "more", cherr_more );
671 if( !type_engine_import_mfun( env, func ) ) goto error;
672
673 // add write(string)
674 func = make_new_mfun( "void", "write", cherr_writestring );
675 func->add_arg( "string", "val" );
676 if( !type_engine_import_mfun( env, func ) ) goto error;
677
678 // add write(int)
679 func = make_new_mfun( "void", "write", cherr_writeint );
680 func->add_arg( "int", "val" );
681 if( !type_engine_import_mfun( env, func ) ) goto error;
682
683 // add write(float)
684 func = make_new_mfun( "void", "write", cherr_writefloat );
685 func->add_arg( "float", "val" );
686 if( !type_engine_import_mfun( env, func ) ) goto error;
687
688 // end the class import
689 type_engine_import_class_end( env );
690
691 return TRUE;
692
693 error:
694
695 // end the class import
696 type_engine_import_class_end( env );
697
698 return FALSE;
699 }
700
701
702
703
704
705
706 // static
707 static t_CKUINT MidiIn_offset_data = 0;
708 t_CKUINT MidiMsg_offset_data1 = 0;
709 t_CKUINT MidiMsg_offset_data2 = 0;
710 t_CKUINT MidiMsg_offset_data3 = 0;
711 t_CKUINT MidiMsg_offset_when = 0;
712 static t_CKUINT MidiOut_offset_data = 0;
713 //-----------------------------------------------------------------------------
714 // name: init_class_Midi()
715 // desc: ...
716 //-----------------------------------------------------------------------------
717 t_CKBOOL init_class_Midi( Chuck_Env * env )
718 {
719 Chuck_DL_Func * func = NULL;
720 std::string doc;
721
722 // doc string
723 doc = "Creates a message for sending and receiving Midi information.";
724
725 // init base class
726 // TODO: ctor/dtor?
727 if( !type_engine_import_class_begin( env, "MidiMsg", "Object",
728 env->global(), NULL, NULL, doc.c_str() ) )
729 return FALSE;
730
731 // add member variable
732 doc = "First byte of a Midi message, usually a status byte or command byte.";
733 MidiMsg_offset_data1 = type_engine_import_mvar( env, "int", "data1", FALSE, doc.c_str() );
734 if( MidiMsg_offset_data1 == CK_INVALID_OFFSET ) goto error;
735
736 // add member variable
737 doc = "Second byte of a Midi message, usually a note value.";
738 MidiMsg_offset_data2 = type_engine_import_mvar( env, "int", "data2", FALSE, doc.c_str() );
739 if( MidiMsg_offset_data2 == CK_INVALID_OFFSET ) goto error;
740
741 // add member variable
742 doc = "Third byte of a Midi message, usually a velocity value.";
743 MidiMsg_offset_data3 = type_engine_import_mvar( env, "int", "data3", FALSE, doc.c_str() );
744 if( MidiMsg_offset_data3 == CK_INVALID_OFFSET ) goto error;
745
746 // add member variable
747 doc = "Duration since the last MidiMsg (only valid for MidiFileIn).";
748 // Time when the MidiMsg occurred, relative to the start of the file (only valid for MidiFileIn).";
749 MidiMsg_offset_when = type_engine_import_mvar( env, "dur", "when", FALSE, doc.c_str() );
750 if( MidiMsg_offset_when == CK_INVALID_OFFSET ) goto error;
751
752 // add examples
753 if( !type_engine_import_add_ex( env, "midi/gomidi.ck" ) ) goto error;
754 if( !type_engine_import_add_ex( env, "midi/gomidi2.ck" ) ) goto error;
755 if( !type_engine_import_add_ex( env, "midi/polyfony.ck" ) ) goto error;
756 if( !type_engine_import_add_ex( env, "midi/polyfony2.ck" ) ) goto error;
757
758 // end the class import
759 type_engine_import_class_end( env );
760
761 #ifndef __DISABLE_MIDI__
762 // init base class
763
764 // doc string
765 doc = "Class that can be ChucKed to now as an event. When receiving a message, an event is signaled and Midi information can be read.";
766
767 if( !type_engine_import_class_begin( env, "MidiIn", "Event",
768 env->global(), MidiIn_ctor, MidiIn_dtor, doc.c_str() ) )
769 return FALSE;
770
771 // add open()
772 func = make_new_mfun( "int", "open", MidiIn_open );
773 func->add_arg( "int", "port" );
774 func->doc = "Open Midi device using a port number.";
775 if( !type_engine_import_mfun( env, func ) ) goto error;
776
777 // add open() (added 1.3.0.0)
778 func = make_new_mfun( "int", "open", MidiIn_open_named );
779 func->add_arg( "string", "name" );
780 func->doc = "Open Midi device using the device's name.";
781 if( !type_engine_import_mfun( env, func ) ) goto error;
782
783 // add good()
784 func = make_new_mfun( "int", "good", MidiIn_good );
785 func->doc = "Return true (1) if a device has been opened for this instance and there was no error connecting to it. Return false (0) if a device has not been opened or there was an error opening a device.";
786 if( !type_engine_import_mfun( env, func ) ) goto error;
787
788 // add num()
789 func = make_new_mfun( "int", "num", MidiIn_num );
790 func->doc = "Return the device number of the device (i.e. the number passed to MidiIn/MidiOut.open).";
791 if( !type_engine_import_mfun( env, func ) ) goto error;
792
793 // add name()
794 func = make_new_mfun( "string", "name", MidiIn_name );
795 func->doc = "Return the Midi device's name as string.";
796 if( !type_engine_import_mfun( env, func ) ) goto error;
797
798 // add printerr()
799 func = make_new_mfun( "void", "printerr", MidiIn_printerr );
800 func->add_arg( "int", "print_or_not" );
801 func->doc = "Set error printing (1 for on, 0 for off). On by default.";
802 if( !type_engine_import_mfun( env, func ) ) goto error;
803
804 // add recv()
805 func = make_new_mfun( "int", "recv", MidiIn_recv );
806 func->add_arg( "MidiMsg", "msg" );
807 func->doc = "Return into the MidiMsg argument the next message in the queue from the device. Return 0 if the queue is empty or 1 if a message was in the queue and returned in the argument.";
808 if( !type_engine_import_mfun( env, func ) ) goto error;
809
810 // add can_wait()
811 func = make_new_mfun( "int", "can_wait", MidiIn_can_wait );
812 func->doc = "(internal) used by virtual machine for synthronization.";
813 // func->doc = "Return 1 if the device has no more messages in its queue from the device, 0 if it has more messages.";
814 if( !type_engine_import_mfun( env, func ) ) goto error;
815
816 // add member variable
817 MidiIn_offset_data = type_engine_import_mvar( env, "int", "@MidiIn_data", FALSE );
818 if( MidiIn_offset_data == CK_INVALID_OFFSET ) goto error;
819
820 // add examples
821 if( !type_engine_import_add_ex( env, "midi/gomidi.ck" ) ) goto error;
822 if( !type_engine_import_add_ex( env, "midi/gomidi2.ck" ) ) goto error;
823 if( !type_engine_import_add_ex( env, "midi/polyfony.ck" ) ) goto error;
824 if( !type_engine_import_add_ex( env, "midi/polyfony2.ck" ) ) goto error;
825
826 // end the class import
827 type_engine_import_class_end( env );
828
829 // doc string
830 doc = "Class for sending out MIDI messages. Note that channel numbers are 0-based.";
831 // init base class
832 if( !type_engine_import_class_begin( env, "MidiOut", "Object",
833 env->global(), MidiOut_ctor, MidiOut_dtor, doc.c_str() ) )
834 return FALSE;
835
836 // add open()
837 func = make_new_mfun( "int", "open", MidiOut_open );
838 func->add_arg( "int", "port" );
839 func->doc = "Open Midi device using a port number.";
840 if( !type_engine_import_mfun( env, func ) ) goto error;
841
842 // add open() (added 1.3.0.0)
843 func = make_new_mfun( "int", "open", MidiOut_open_named );
844 func->add_arg( "string", "name" );
845 func->doc = "Open Midi device using the device's name.";
846 if( !type_engine_import_mfun( env, func ) ) goto error;
847
848 // add good()
849 func = make_new_mfun( "int", "good", MidiOut_good );
850 func->doc = "Return true (1) if a device has been opened for this instance and there was no error connecting to it. Return false (0) if a device has not been opened or there was an error opening a device.";
851 if( !type_engine_import_mfun( env, func ) ) goto error;
852
853 // add num()
854 func = make_new_mfun( "int", "num", MidiOut_num );
855 func->doc = "Return the device number of the device (i.e. the number passed to MidiIn/MidiOut.open).";
856 if( !type_engine_import_mfun( env, func ) ) goto error;
857
858 // add name()
859 func = make_new_mfun( "string", "name", MidiOut_name );
860 func->doc = "Return the Midi device's name as string.";
861 if( !type_engine_import_mfun( env, func ) ) goto error;
862
863 // add printerr()
864 func = make_new_mfun( "void", "printerr", MidiOut_printerr );
865 func->add_arg( "int", "print_or_not" );
866 func->doc = "Set error printing (1 for on, 0 for off). On by default.";
867 if( !type_engine_import_mfun( env, func ) ) goto error;
868
869 // add send() | 1.5.2.5 (ge) added
870 func = make_new_mfun( "int", "send", MidiOut_send );
871 func->add_arg( "int", "status" );
872 func->add_arg( "int", "data1" );
873 func->add_arg( "int", "data2" );
874 func->doc = "Send out a MIDI message consisting of one status byte and two data bytes.";
875 if( !type_engine_import_mfun( env, func ) ) goto error;
876
877 // add send()
878 func = make_new_mfun( "int", "send", MidiOut_send_msg );
879 func->add_arg( "MidiMsg", "msg" );
880 func->doc = "Send out a MIDI message using a MidiMsg.";
881 if( !type_engine_import_mfun( env, func ) ) goto error;
882
883 // add noteOn() | 1.5.2.5 (cviejo) added
884 func = make_new_mfun( "int", "noteOn", MidiOut_noteOn );
885 func->add_arg( "int", "channel" );
886 func->add_arg( "int", "note" );
887 func->add_arg( "int", "velocity" );
888 func->doc = "Send out a noteOn message.";
889 if( !type_engine_import_mfun( env, func ) ) goto error;
890
891 // add noteOff() | 1.5.2.5 (cviejo) added
892 func = make_new_mfun( "int", "noteOff", MidiOut_noteOff );
893 func->add_arg( "int", "channel" );
894 func->add_arg( "int", "note" );
895 func->add_arg( "int", "velocity" );
896 func->doc = "Send out a noteOff message.";
897 if( !type_engine_import_mfun( env, func ) ) goto error;
898
899 // add controlChange() | 1.5.2.5 (cviejo) added
900 func = make_new_mfun( "int", "controlChange", MidiOut_controlChange );
901 func->add_arg( "int", "channel" );
902 func->add_arg( "int", "controller" );
903 func->add_arg( "int", "value" );
904 func->doc = "Send out a controlChange message.";
905 if( !type_engine_import_mfun( env, func ) ) goto error;
906
907 // add programChange() | 1.5.2.5 (cviejo) added
908 func = make_new_mfun( "int", "programChange", MidiOut_programChange );
909 func->add_arg( "int", "channel" );
910 func->add_arg( "int", "program" );
911 func->doc = "Send out a programChange message.";
912 if( !type_engine_import_mfun( env, func ) ) goto error;
913
914 // add pitchBend() | 1.5.2.5 (cviejo) added
915 func = make_new_mfun( "int", "pitchBend", MidiOut_pitchBend );
916 func->add_arg( "int", "channel" );
917 func->add_arg( "int", "value" );
918 func->doc = "Send out a pitchBend message.";
919 if( !type_engine_import_mfun( env, func ) ) goto error;
920
921 // add pitchBend() - fine resolution | 1.5.2.5 (cviejo) added
922 func = make_new_mfun( "int", "pitchBend", MidiOut_pitchBend_fine );
923 func->add_arg( "int", "channel" );
924 func->add_arg( "int", "lsb" );
925 func->add_arg( "int", "msb" );
926 func->doc = "Send out a pitchBend message with fine and coarse values.";
927 if( !type_engine_import_mfun( env, func ) ) goto error;
928
929 // add polyPressure() | 1.5.2.5 (cviejo) added
930 func = make_new_mfun( "int", "polyPressure", MidiOut_polyPressure );
931 func->add_arg( "int", "channel" );
932 func->add_arg( "int", "note" );
933 func->add_arg( "int", "pressure" );
934 func->doc = "Send out a polyPressure message.";
935 if( !type_engine_import_mfun( env, func ) ) goto error;
936
937 // add channelPressure() | 1.5.2.5 (cviejo) added
938 func = make_new_mfun( "int", "channelPressure", MidiOut_channelPressure );
939 func->add_arg( "int", "channel" );
940 func->add_arg( "int", "pressure" );
941 func->doc = "Send out a channelPressure message.";
942 if( !type_engine_import_mfun( env, func ) ) goto error;
943
944 // add examples
945 if( !type_engine_import_add_ex( env, "midi/midiout.ck" ) ) goto error;
946 if( !type_engine_import_add_ex( env, "midi/midiout2.ck" ) ) goto error;
947
948 // add member variable
949 MidiOut_offset_data = type_engine_import_mvar( env, "int", "@MidiOut_data", FALSE );
950 if( MidiOut_offset_data == CK_INVALID_OFFSET ) goto error;
951
952 // end the class import
953 type_engine_import_class_end( env );
954
955 #endif // __DISABLE_MIDI__
956
957 return TRUE;
958
959 error:
960
961 // end the class import
962 type_engine_import_class_end( env );
963
964 return FALSE;
965 }
966
967
968
969
970 #ifndef __DISABLE_HID__
971 // static
972 static t_CKUINT HidIn_offset_data = 0;
973
974 static t_CKUINT HidMsg_offset_device_type = 0;
975 static t_CKUINT HidMsg_offset_device_num = 0;
976 static t_CKUINT HidMsg_offset_type = 0;
977 static t_CKUINT HidMsg_offset_which = 0;
978 static t_CKUINT HidMsg_offset_idata = 0;
979 static t_CKUINT HidMsg_offset_fdata = 0;
980 static t_CKUINT HidMsg_offset_when = 0;
981 static t_CKUINT HidMsg_offset_deltax = 0;
982 static t_CKUINT HidMsg_offset_deltay = 0;
983 static t_CKUINT HidMsg_offset_axis_position = 0; // deprecated
984 static t_CKUINT HidMsg_offset_axis_position2 = 0;
985 static t_CKUINT HidMsg_offset_scaled_axis_position = 0; // deprecated
986 static t_CKUINT HidMsg_offset_hat_position = 0;
987 static t_CKUINT HidMsg_offset_cursorx = 0;
988 static t_CKUINT HidMsg_offset_cursory = 0;
989 static t_CKUINT HidMsg_offset_scaledcursorx = 0;
990 static t_CKUINT HidMsg_offset_scaledcursory = 0;
991 static t_CKUINT HidMsg_offset_x = 0;
992 static t_CKUINT HidMsg_offset_y = 0;
993 static t_CKUINT HidMsg_offset_z = 0;
994 static t_CKUINT HidMsg_offset_touchx = 0; // added 1.3.0.0
995 static t_CKUINT HidMsg_offset_touchy = 0; // added 1.3.0.0
996 static t_CKUINT HidMsg_offset_touchsize = 0; // added 1.3.0.0
997 static t_CKUINT HidMsg_offset_ascii = 0;
998 static t_CKUINT HidMsg_offset_key = 0;
999
1000 static t_CKUINT HidOut_offset_data = 0;
1001
1002 //-----------------------------------------------------------------------------
1003 // name: init_class_HID()
1004 // desc: ...
1005 //-----------------------------------------------------------------------------
1006 t_CKBOOL init_class_HID( Chuck_Env * env )
1007 {
1008 Chuck_DL_Func * func = NULL;
1009 std::string doc;
1010
1011 // doc string
1012 doc = "Helper class for receiving HID information; e.g., which key was pressed, joystick position, etc.";
1013
1014 // init base class
1015 if( !type_engine_import_class_begin( env, "HidMsg", "Object",
1016 env->global(), NULL, NULL, doc.c_str() ) )
1017 return FALSE;
1018
1019 // add member variable
1020 doc = "Device type that produced the message.";
1021 HidMsg_offset_device_type = type_engine_import_mvar( env, "int", "deviceType", FALSE, doc.c_str() );
1022 if( HidMsg_offset_device_type == CK_INVALID_OFFSET ) goto error;
1023
1024 // add member variable
1025 doc = "Device number that produced the message.";
1026 HidMsg_offset_device_num = type_engine_import_mvar( env, "int", "deviceNum", FALSE, doc.c_str() );
1027 if( HidMsg_offset_device_num == CK_INVALID_OFFSET ) goto error;
1028
1029 // add member variable
1030 doc = "A number representing the message type.";
1031 HidMsg_offset_type = type_engine_import_mvar( env, "int", "type", FALSE, doc.c_str() );
1032 if( HidMsg_offset_type == CK_INVALID_OFFSET ) goto error;
1033
1034 // add member variable
1035 doc = "Code (platform dependent) associated with a keyboard key.";
1036 HidMsg_offset_which = type_engine_import_mvar( env, "int", "which", FALSE, doc.c_str() );
1037 if( HidMsg_offset_which == CK_INVALID_OFFSET ) goto error;
1038
1039 // add member variable
1040 doc = "Integer data generated from the Hid device.";
1041 HidMsg_offset_idata = type_engine_import_mvar( env, "int", "idata", FALSE, doc.c_str() );
1042 if( HidMsg_offset_idata == CK_INVALID_OFFSET ) goto error;
1043
1044 // add member variable
1045 doc = "Float data generate from the Hid device.";
1046 HidMsg_offset_fdata = type_engine_import_mvar( env, "float", "fdata", FALSE, doc.c_str() );
1047 if( HidMsg_offset_fdata == CK_INVALID_OFFSET ) goto error;
1048
1049 // add member variable
1050 doc = "Time when the HidMsg occurred, relative to the start of the file.";
1051 HidMsg_offset_when = type_engine_import_mvar( env, "time", "when", FALSE, doc.c_str() );
1052 if( HidMsg_offset_when == CK_INVALID_OFFSET ) goto error;
1053
1054 // add member variable
1055 doc = "Change in X-axis of pointing device.";
1056 HidMsg_offset_deltax = type_engine_import_mvar( env, "int", "deltaX", FALSE, doc.c_str() );
1057 if( HidMsg_offset_deltax == CK_INVALID_OFFSET ) goto error;
1058
1059 // add member variable
1060 doc = "Change in Y-axis of pointing device.";
1061 HidMsg_offset_deltay = type_engine_import_mvar( env, "int", "deltaY", FALSE, doc.c_str() );
1062 if( HidMsg_offset_deltay == CK_INVALID_OFFSET ) goto error;
1063
1064 // add member variable
1065 doc = "Position of X-axis of pointing device.";
1066 HidMsg_offset_cursorx = type_engine_import_mvar( env, "int", "cursorX", FALSE, doc.c_str() );
1067 if( HidMsg_offset_cursorx == CK_INVALID_OFFSET ) goto error;
1068
1069 // add member variable
1070 doc = "Position of Y-axis of pointing device.";
1071 HidMsg_offset_cursory = type_engine_import_mvar( env, "int", "cursorY", FALSE, doc.c_str() );
1072 if( HidMsg_offset_cursory == CK_INVALID_OFFSET ) goto error;
1073
1074 // add member variable
1075 doc = "Position of X-axis of pointing device, scaled between 0.0 and 1.0.";
1076 HidMsg_offset_scaledcursorx = type_engine_import_mvar( env, "float", "scaledCursorX", FALSE, doc.c_str() );
1077 if( HidMsg_offset_scaledcursorx == CK_INVALID_OFFSET ) goto error;
1078
1079 // add member variable
1080 doc = "Position of Y-axis of pointing device, scaled between 0.0 and 1.0.";
1081 HidMsg_offset_scaledcursory = type_engine_import_mvar( env, "float", "scaledCursorY", FALSE, doc.c_str() );
1082 if( HidMsg_offset_scaledcursory == CK_INVALID_OFFSET ) goto error;
1083
1084 // add member variable
1085 doc = "Change in X-axis of pointing device (same as deltaX).";
1086 HidMsg_offset_x = type_engine_import_mvar( env, "int", "x", FALSE, doc.c_str() );
1087 if( HidMsg_offset_x == CK_INVALID_OFFSET ) goto error;
1088
1089 // add member variable
1090 doc = "Change in Y-axis of pointing device (same as deltaY).";
1091 HidMsg_offset_y = type_engine_import_mvar( env, "int", "y", FALSE, doc.c_str() );
1092 if( HidMsg_offset_y == CK_INVALID_OFFSET ) goto error;
1093
1094 // add member variable
1095 doc = "Change in Z-axis of pointing device.";
1096 HidMsg_offset_z = type_engine_import_mvar( env, "int", "z", FALSE, doc.c_str() );
1097 if( HidMsg_offset_z == CK_INVALID_OFFSET ) goto error;
1098
1099 // add member variable (added 1.3.0.0)
1100 doc = "Position of X-axis of pointing device, scaled between 0.0 and 1.0.";
1101 HidMsg_offset_touchx = type_engine_import_mvar( env, "float", "touchX", FALSE, doc.c_str() );
1102 if( HidMsg_offset_touchx == CK_INVALID_OFFSET ) goto error;
1103
1104 // add member variable (added 1.3.0.0)
1105 doc = "Position of X-axis of pointing device, scaled between 0.0 and 1.0.";
1106 HidMsg_offset_touchy = type_engine_import_mvar( env, "float", "touchY", FALSE, doc.c_str() );
1107 if( HidMsg_offset_touchy == CK_INVALID_OFFSET ) goto error;
1108
1109 // add member variable (added 1.3.0.0)
1110 doc = "Multi-touch size.";
1111 HidMsg_offset_touchsize = type_engine_import_mvar( env, "float", "touchSize", FALSE, doc.c_str() );
1112 if( HidMsg_offset_touchsize == CK_INVALID_OFFSET ) goto error;
1113
1114 // add member variable
1115 doc = "Joystick axis position (int).";
1116 HidMsg_offset_axis_position = type_engine_import_mvar( env, "int", "axis_position", FALSE, doc.c_str() );
1117 if( HidMsg_offset_axis_position == CK_INVALID_OFFSET ) goto error;
1118
1119 // add member variable
1120 doc = "Joystick axis position (float).";
1121 HidMsg_offset_axis_position2 = type_engine_import_mvar( env, "float", "axisPosition", FALSE, doc.c_str() );
1122 if( HidMsg_offset_axis_position2 == CK_INVALID_OFFSET ) goto error;
1123
1124 // add member variable
1125 doc = "Joystick hat position.";
1126 HidMsg_offset_hat_position = type_engine_import_mvar( env, "int", "hatPosition", FALSE, doc.c_str() );
1127 if( HidMsg_offset_hat_position == CK_INVALID_OFFSET ) goto error;
1128
1129 // add member variable
1130 doc = "ASCII value associated with a keyboard key.";
1131 HidMsg_offset_ascii = type_engine_import_mvar( env, "int", "ascii", FALSE, doc.c_str() );
1132 if( HidMsg_offset_ascii == CK_INVALID_OFFSET ) goto error;
1133
1134 // add member variable
1135 doc = "Code (USB) for a keyboard key.";
1136 HidMsg_offset_key = type_engine_import_mvar( env, "int", "key", FALSE, doc.c_str() );
1137 if( HidMsg_offset_key == CK_INVALID_OFFSET ) goto error;
1138
1139 // add member variable
1140 doc = "Position of the primary pointing device, scaled between 0.0 and 1.0.";
1141 HidMsg_offset_scaled_axis_position = type_engine_import_mvar( env, "float", "scaled_axis_position", FALSE, doc.c_str() );
1142 if( HidMsg_offset_scaled_axis_position == CK_INVALID_OFFSET ) goto error;
1143
1144 // add is_axis_motion()
1145 func = make_new_mfun( "int", "is_axis_motion", HidMsg_is_axis_motion ); // deprecated
1146 func->doc = "(Deprecated; use .isAxisMotion() instead).";
1147 if( !type_engine_import_mfun( env, func ) ) goto error;
1148
1149 // add isAxisMotion()
1150 func = make_new_mfun( "int", "isAxisMotion", HidMsg_is_axis_motion );
1151 func->doc = "Return whether this message is an axis-motion event.";
1152 if( !type_engine_import_mfun( env, func ) ) goto error;
1153
1154 // add is_button_down()
1155 func = make_new_mfun( "int", "is_button_down", HidMsg_is_button_down ); // deprecated
1156 func->doc = "(Deprecated; use .isButtonDown() instead).";
1157 if( !type_engine_import_mfun( env, func ) ) goto error;
1158
1159 // add isButtonDown()
1160 func = make_new_mfun( "int", "isButtonDown", HidMsg_is_button_down );
1161 func->doc = "Return whether this message is a button-down event.";
1162 if( !type_engine_import_mfun( env, func ) ) goto error;
1163
1164 // add is_button_up()
1165 func = make_new_mfun( "int", "is_button_up", HidMsg_is_button_up ); // deprecated
1166 func->doc = "(Deprecated; use .isButtonUp() instead).";
1167 if( !type_engine_import_mfun( env, func ) ) goto error;
1168
1169 // add isButtonUp()
1170 func = make_new_mfun( "int", "isButtonUp", HidMsg_is_button_up );
1171 func->doc = "Return whether this message is a button-up event.";
1172 if( !type_engine_import_mfun( env, func ) ) goto error;
1173
1174 // add is_mouse_motion()
1175 func = make_new_mfun( "int", "is_mouse_motion", HidMsg_is_mouse_motion ); // deprecated
1176 func->doc = "(Deprecated; use .isMouseMotion() instead).";
1177 if( !type_engine_import_mfun( env, func ) ) goto error;
1178
1179 // add isMouseMotion()
1180 func = make_new_mfun( "int", "isMouseMotion", HidMsg_is_mouse_motion );
1181 func->doc = "Return whether this message is a mouse-motion event.";
1182 if( !type_engine_import_mfun( env, func ) ) goto error;
1183
1184 // add is_hat_motion()
1185 func = make_new_mfun( "int", "is_hat_motion", HidMsg_is_hat_motion ); // deprecated
1186 func->doc = "(Deprecated; use .isHatMotion() instead).";
1187 if( !type_engine_import_mfun( env, func ) ) goto error;
1188
1189 // add isHatMotion()
1190 func = make_new_mfun( "int", "isHatMotion", HidMsg_is_hat_motion );
1191 func->doc = "Return whether this message is a hat-motion event.";
1192 if( !type_engine_import_mfun( env, func ) ) goto error;
1193
1194 // add isWheelMotion()
1195 func = make_new_mfun( "int", "isWheelMotion", HidMsg_is_wheel_motion );
1196 func->doc = "Return whether this message is a wheel-motion event.";
1197 if( !type_engine_import_mfun( env, func ) ) goto error;
1198
1199 // add examples
1200 if( !type_engine_import_add_ex( env, "hid/kb.ck" ) ) goto error;
1201 if( !type_engine_import_add_ex( env, "hid/keyboard-organ.ck" ) ) goto error;
1202 if( !type_engine_import_add_ex( env, "hid/mouse.ck" ) ) goto error;
1203 if( !type_engine_import_add_ex( env, "hid/mouse-abs.ck" ) ) goto error;
1204 if( !type_engine_import_add_ex( env, "hid/mouse-fm.ck" ) ) goto error;
1205 if( !type_engine_import_add_ex( env, "hid/joy.ck" ) ) goto error;
1206 if( !type_engine_import_add_ex( env, "hid/joy-fm.ck" ) ) goto error;
1207 if( !type_engine_import_add_ex( env, "hid/joy-noise.ck" ) ) goto error;
1208 if( !type_engine_import_add_ex( env, "hid/joy-shake.ck" ) ) goto error;
1209
1210 // end the class import
1211 type_engine_import_class_end( env );
1212
1213
1214 // register deprecate
1215 type_engine_register_deprecate( env, "HidIn", "Hid" );
1216
1217 // init base class Hid (copy of HidIn + constants)
1218 if( !type_engine_import_class_begin( env, "Hid", "Event",
1219 env->global(), HidIn_ctor, HidIn_dtor, "Class for interacting with human-interface devices (HIDs) such as keyboards, mice, gamepads, joysticks, etc." ) )
1220 return FALSE;
1221
1222 // add examples
1223 if( !type_engine_import_add_ex( env, "hid/kb.ck" ) ) goto error;
1224 if( !type_engine_import_add_ex( env, "hid/keyboard-organ.ck" ) ) goto error;
1225 if( !type_engine_import_add_ex( env, "hid/mouse.ck" ) ) goto error;
1226 if( !type_engine_import_add_ex( env, "hid/mouse-abs.ck" ) ) goto error;
1227 if( !type_engine_import_add_ex( env, "hid/mouse-fm.ck" ) ) goto error;
1228 if( !type_engine_import_add_ex( env, "hid/joy.ck" ) ) goto error;
1229 if( !type_engine_import_add_ex( env, "hid/joy-fm.ck" ) ) goto error;
1230 if( !type_engine_import_add_ex( env, "hid/joy-noise.ck" ) ) goto error;
1231 if( !type_engine_import_add_ex( env, "hid/joy-shake.ck" ) ) goto error;
1232
1233 // add open()
1234 func = make_new_mfun( "int", "open", HidIn_open );
1235 func->add_arg( "int", "type" );
1236 func->add_arg( "int", "num" );
1237 func->doc = "Open a HID device by device number ('num') and type ('type'). See static member variables for possible types.";
1238 if( !type_engine_import_mfun( env, func ) ) goto error;
1239
1240 // add open()
1241 func = make_new_mfun( "int", "open", HidIn_open_named );
1242 func->add_arg( "string", "name" );
1243 func->doc = "Open a HID device by name.";
1244 if( !type_engine_import_mfun( env, func ) ) goto error;
1245
1246 // add openJoystick()
1247 func = make_new_mfun( "int", "openJoystick", HidIn_open_joystick );
1248 func->add_arg( "int", "num" );
1249 func->doc = "Open a joystick/gamepad by device number.";
1250 if( !type_engine_import_mfun( env, func ) ) goto error;
1251
1252 // add openJoystick()
1253 func = make_new_mfun( "int", "openJoystick", HidIn_open_joystick_2 );
1254 func->add_arg( "int", "num" );
1255 func->add_arg( "int", "suppressErrMsg" );
1256 func->doc = "Open a joystick/gamepad by device number, with option (true/false) to suppress error messages.";
1257 if( !type_engine_import_mfun( env, func ) ) goto error;
1258
1259 // add openMouse()
1260 func = make_new_mfun( "int", "openMouse", HidIn_open_mouse );
1261 func->add_arg( "int", "num" );
1262 func->doc = "Open a mouse/trackpad by device number.";
1263 if( !type_engine_import_mfun( env, func ) ) goto error;
1264
1265 // add openMouse()
1266 func = make_new_mfun( "int", "openMouse", HidIn_open_mouse_2 );
1267 func->add_arg( "int", "num" );
1268 func->add_arg( "int", "suppressErrMsg" );
1269 func->doc = "Open a mouse/trackpad by device number, with option (true/false) to suppress error messages.";
1270 if( !type_engine_import_mfun( env, func ) ) goto error;
1271
1272 // add openKeyboard()
1273 func = make_new_mfun( "int", "openKeyboard", HidIn_open_keyboard );
1274 func->add_arg( "int", "num" );
1275 func->doc = "Open a keyboard by device number.";
1276 if( !type_engine_import_mfun( env, func ) ) goto error;
1277
1278 // add openKeyboard() | 1.5.1.5 (ge & andrew)
1279 func = make_new_mfun( "int", "openKeyboard", HidIn_open_keyboard_2 );
1280 func->add_arg( "int", "num" );
1281 func->add_arg( "int", "suppressErrMsg" );
1282 func->doc = "Open a keyboard by device number, with option (true/false) to suppress error messages.";
1283 if( !type_engine_import_mfun( env, func ) ) goto error;
1284
1285 // add openTiltSensor()
1286 func = make_new_mfun( "int", "openTiltSensor", HidIn_open_tiltsensor );
1287 func->doc = "Open a tilt-sensor by device number.";
1288 if( !type_engine_import_mfun( env, func ) ) goto error;
1289
1290 // add good()
1291 func = make_new_mfun( "int", "good", HidIn_good );
1292 func->doc = "Get whether a device has been successfully opened on this HID instance.";
1293 if( !type_engine_import_mfun( env, func ) ) goto error;
1294
1295 // add num()
1296 func = make_new_mfun( "int", "num", HidIn_num );
1297 func->doc = "Get the number of the currently open device; returns -1 if no device is open on this HID instance.";
1298 if( !type_engine_import_mfun( env, func ) ) goto error;
1299
1300 // add name()
1301 func = make_new_mfun( "string", "name", HidIn_name );
1302 func->doc = "Get the name of the currently open device; return empty string (\"\") if no device is open on this HID instance.";
1303 if( !type_engine_import_mfun( env, func ) ) goto error;
1304
1305 // add printerr()
1306 func = make_new_mfun( "void", "printerr", HidIn_printerr );
1307 func->add_arg( "int", "toPrintOrNot" );
1308 func->doc = "Set whether to print errors (default is YES).";
1309 if( !type_engine_import_mfun( env, func ) ) goto error;
1310
1311 // add recv()
1312 func = make_new_mfun( "int", "recv", HidIn_recv );
1313 func->add_arg( "HidMsg", "msg" );
1314 func->doc = "Receive the next available HidMsg.";
1315 if( !type_engine_import_mfun( env, func ) ) goto error;
1316
1317 // add read()
1318 func = make_new_mfun( "int", "read", HidIn_read );
1319 func->add_arg( "int", "type" );
1320 func->add_arg( "int", "which" );
1321 func->add_arg( "HidMsg", "msg" );
1322 func->doc = "Read the next HidMsg from device of type 'type' with device id 'which'.";
1323 if( !type_engine_import_mfun( env, func ) ) goto error;
1324
1325 // add send()
1326 func = make_new_mfun( "int", "send", HidIn_send );
1327 func->add_arg( "HidMsg", "msg" );
1328 func->doc = "Send a HidMsg to device; return whether the operation was successful.";
1329 if( !type_engine_import_mfun( env, func ) ) goto error;
1330
1331 // add can_wait()
1332 func = make_new_mfun( "int", "can_wait", HidIn_can_wait );
1333 func->doc = "(internal) used by virtual machine for synthronization.";
1334 if( !type_engine_import_mfun( env, func ) ) goto error;
1335
1336 // add readTiltSensor()
1337 func = make_new_sfun( "int[]", "readTiltSensor", HidIn_read_tilt_sensor );
1338 func->doc = "Read tilt-sensor and return as an int array.";
1339 if( !type_engine_import_sfun( env, func ) ) goto error;
1340
1341 // add globalTiltPollRate()
1342 func = make_new_sfun( "dur", "globalTiltPollRate", HidIn_ctrl_tiltPollRate );
1343 func->add_arg( "dur", "d" );
1344 func->doc = "Set tilt-sensor poll rate.";
1345 if( !type_engine_import_sfun( env, func ) ) goto error;
1346
1347 // add globalTiltPollRate()
1348 func = make_new_sfun( "dur", "globalTiltPollRate", HidIn_cget_tiltPollRate );
1349 func->doc = "Get tilt-sensor poll rate.";
1350 if( !type_engine_import_sfun( env, func ) ) goto error;
1351
1352 // add startCursorTrack()
1353 func = make_new_sfun( "int", "startCursorTrack", HidIn_start_cursor_track );
1354 func->doc = "Start cursor tracking; return whether the request was successful.";
1355 if( !type_engine_import_sfun( env, func ) ) goto error;
1356
1357 // add stopCursorTrack()
1358 func = make_new_sfun( "int", "stopCursorTrack", HidIn_stop_cursor_track );
1359 func->doc = "Stop cursor tracking; return whether the request was successful.";
1360 if( !type_engine_import_sfun( env, func ) ) goto error;
1361
1362 // add member variable
1363 HidIn_offset_data = type_engine_import_mvar( env, "int", "@Hid_data", FALSE );
1364 if( HidIn_offset_data == CK_INVALID_OFFSET ) goto error;
1365
1366 // add static member variable joystick
1367 if( type_engine_import_svar( env, "int", "JOYSTICK", TRUE,
1368 ( t_CKUINT ) &CK_HID_DEV_JOYSTICK ) == FALSE )
1369 goto error;
1370
1371 // add static member variable keyboard
1372 if( type_engine_import_svar( env, "int", "KEYBOARD", TRUE,
1373 ( t_CKUINT ) &CK_HID_DEV_KEYBOARD ) == FALSE )
1374 goto error;
1375
1376 // add static member variable mouse
1377 if( type_engine_import_svar( env, "int", "MOUSE", TRUE,
1378 ( t_CKUINT ) &CK_HID_DEV_MOUSE ) == FALSE )
1379 goto error;
1380
1381 // add static member variable wii_remote
1382 if( type_engine_import_svar( env, "int", "WII_REMOTE", TRUE,
1383 ( t_CKUINT ) &CK_HID_DEV_WIIREMOTE ) == FALSE )
1384 goto error;
1385
1386 // add static member variable wii_remote
1387 if( type_engine_import_svar( env, "int", "TILT_SENSOR", TRUE,
1388 ( t_CKUINT ) &CK_HID_DEV_TILTSENSOR ) == FALSE )
1389 goto error;
1390
1391 // add static member variable tablet
1392 if( type_engine_import_svar( env, "int", "TABLET", TRUE,
1393 ( t_CKUINT ) &CK_HID_DEV_TABLET ) == FALSE )
1394 goto error;
1395
1396 // add static member variable axisMotion
1397 if( type_engine_import_svar( env, "int", "AXIS_MOTION", TRUE,
1398 ( t_CKUINT ) &CK_HID_JOYSTICK_AXIS ) == FALSE )
1399 goto error;
1400
1401 // add static member variable buttonDown
1402 if( type_engine_import_svar( env, "int", "BUTTON_DOWN", TRUE,
1403 ( t_CKUINT ) &CK_HID_BUTTON_DOWN ) == FALSE )
1404 goto error;
1405
1406 // add static member variable buttonUp
1407 if( type_engine_import_svar( env, "int", "BUTTON_UP", TRUE,
1408 ( t_CKUINT ) &CK_HID_BUTTON_UP ) == FALSE )
1409 goto error;
1410
1411 // add static member variable joystickHat
1412 if( type_engine_import_svar( env, "int", "JOYSTICK_HAT", TRUE,
1413 ( t_CKUINT ) &CK_HID_JOYSTICK_HAT ) == FALSE )
1414 goto error;
1415
1416 // add static member variable JOYSTICK_BALL
1417 if( type_engine_import_svar( env, "int", "JOYSTICK_BALL", TRUE,
1418 ( t_CKUINT ) &CK_HID_JOYSTICK_BALL ) == FALSE )
1419 goto error;
1420
1421 // add static member variable mouseMotion
1422 if( type_engine_import_svar( env, "int", "MOUSE_MOTION", TRUE,
1423 ( t_CKUINT ) &CK_HID_MOUSE_MOTION ) == FALSE )
1424 goto error;
1425
1426 // add static member variable mouseWheel
1427 if( type_engine_import_svar( env, "int", "MOUSE_WHEEL", TRUE,
1428 ( t_CKUINT ) &CK_HID_MOUSE_WHEEL ) == FALSE )
1429 goto error;
1430
1431 // add static member variable DEVICE_CONNECTED
1432 if( type_engine_import_svar( env, "int", "DEVICE_CONNECTED", TRUE,
1433 ( t_CKUINT ) &CK_HID_DEVICE_CONNECTED ) == FALSE )
1434 goto error;
1435
1436 // add static member variable DEVICE_DISCONNECTED
1437 if( type_engine_import_svar( env, "int", "DEVICE_DISCONNECTED", TRUE,
1438 ( t_CKUINT ) &CK_HID_DEVICE_DISCONNECTED ) == FALSE )
1439 goto error;
1440
1441 // add static member variable ACCELEROMETER
1442 if( type_engine_import_svar( env, "int", "ACCELEROMETER", TRUE,
1443 ( t_CKUINT ) &CK_HID_ACCELEROMETER ) == FALSE )
1444 goto error;
1445
1446 // add static member variable LED
1447 if( type_engine_import_svar( env, "int", "LED", TRUE,
1448 ( t_CKUINT ) &CK_HID_LED ) == FALSE )
1449 goto error;
1450
1451 // add static member variable LED
1452 if( type_engine_import_svar( env, "int", "FORCE_FEEDBACK", TRUE,
1453 ( t_CKUINT ) &CK_HID_FORCE_FEEDBACK ) == FALSE )
1454 goto error;
1455
1456 // end the class import
1457 type_engine_import_class_end( env );
1458
1459 /*
1460 // init base class
1461 if( !type_engine_import_class_begin( env, "HidOut", "Object",
1462 env->global(), HidOut_ctor ) )
1463 return FALSE;
1464
1465 // add open()
1466 func = make_new_mfun( "int", "open", HidOut_open );
1467 func->add_arg( "int", "port" );
1468 if( !type_engine_import_mfun( env, func ) ) goto error;
1469
1470 // add good()
1471 func = make_new_mfun( "int", "good", HidOut_good );
1472 if( !type_engine_import_mfun( env, func ) ) goto error;
1473
1474 // add num()
1475 func = make_new_mfun( "int", "num", HidOut_num );
1476 if( !type_engine_import_mfun( env, func ) ) goto error;
1477
1478 // add name()
1479 func = make_new_mfun( "string", "name", HidOut_name );
1480 if( !type_engine_import_mfun( env, func ) ) goto error;
1481
1482 // add printerr()
1483 func = make_new_mfun( "void", "printerr", HidOut_printerr );
1484 func->add_arg( "int", "print_or_not" );
1485 if( !type_engine_import_mfun( env, func ) ) goto error;
1486
1487 // add send()
1488 func = make_new_mfun( "int", "send", HidOut_send );
1489 func->add_arg( "HidMsg", "msg" );
1490 if( !type_engine_import_mfun( env, func ) ) goto error;
1491
1492 // add member variable
1493 HidOut_offset_data = type_engine_import_mvar( env, "int", "@HidOut_data", FALSE );
1494 if( HidOut_offset_data == CK_INVALID_OFFSET ) goto error;
1495
1496 // end the class import
1497 type_engine_import_class_end( env );
1498 */
1499
1500 // RELEASE NOTES for when we are ready
1501 // (added) new `MouseCursor` class for quickly accessing mouse cursor:
1502 // // absolute screen coordinate of cursor
1503 // vec2 MouseCursor.xy();
1504 // // scaled coordinates in range [0,1]
1505 // vec2 MouseCursor.scaled();
1506 // (see API reference for more information)
1507 // https://chuck.stanford.edu/doc/reference/io.html#MouseCursor
1508 // (added) new `Mousor` class; `Mousor` is functionally equivalent to `MouseCursor`,
1509 // purely made for those prefer slightly less typing and slightly more chaos!
1510 // (added) examples/hid/mouse-cursor.ck
1511
1512 // init MouseCursor class | 1.5.4.2 (ge & spencer) added
1513 if( !type_engine_import_class_begin( env, "MouseCursor", "Object",
1514 env->global(), MouseCursor_ctor, MouseCursor_dtor,
1515 "Mouse cursor position tracking. (For full access to mouse input, see the Hid class.)" ) )
1516 return FALSE;
1517
1518 // add examples
1519 if( !type_engine_import_add_ex( env, "hid/mouse-cursor.ck" ) ) goto error;
1520
1521 // add pos() | 1.5.4.2 (ge & spencer ) added
1522 func = make_new_sfun( "vec2", "scaled", MouseCursor_scaled );
1523 func->doc = "get the current X and Y normalized positions of the mouse cursor relative to its containing monitor; yields X and Y values in the range [0.0,1.0]";
1524 if( !type_engine_import_sfun( env, func ) ) goto error;
1525
1526 // add abs() | 1.5.4.2 (ge & spencer ) added
1527 func = make_new_sfun( "vec2", "abs", MouseCursor_abs );
1528 func->doc = "get the current X and Y absolute coordinates of the mouse cursor; dependent on screen resolution; could yield positive and negative values in a multi-monitor setup; same as .xy()";
1529 if( !type_engine_import_sfun( env, func ) ) goto error;
1530
1531 // add xy() | 1.5.4.2 (ge & spencer ) added
1532 func = make_new_sfun( "vec2", "xy", MouseCursor_abs );
1533 func->doc = "get the current X and Y absolute coordinates of the mouse cursor; dependent on screen resolution; could yield positive and negative values in a multi-monitor setup; same as .abs()";
1534 if( !type_engine_import_sfun( env, func ) ) goto error;
1535
1536 // add x() | 1.5.4.2 (ge & spencer ) added
1537 func = make_new_sfun( "float", "x", MouseCursor_abs_x );
1538 func->doc = "get the current X absolute coordinate of the mouse cursor; dependent on screen resolution; could yield positive and negative values in a multi-monitor setup";
1539 if( !type_engine_import_sfun( env, func ) ) goto error;
1540
1541 // add y() | 1.5.4.2 (ge & spencer ) added
1542 func = make_new_sfun( "float", "y", MouseCursor_abs_y );
1543 func->doc = "get the current Y absolute coordinate of the mouse cursor; dependent on screen resolution; could yield positive and negative values in a multi-monitor setup";
1544 if( !type_engine_import_sfun( env, func ) ) goto error;
1545
1546 // end the class import
1547 type_engine_import_class_end( env );
1548
1549 // init Mousor class | 1.5.4.2 (chuck team) added as a shorter and more fun/chaotic alias for MouseCursor
1550 if( !type_engine_import_class_begin( env, "Mousor", "MouseCursor",
1551 env->global(), Mousor_ctor, Mousor_dtor,
1552 "Same as MouseCursor; for those who prefer less typing and more chaos. (For full access to mouse input, see the Hid class.)" ) )
1553 return FALSE;
1554
1555 // end the class import
1556 type_engine_import_class_end( env );
1557
1558 return TRUE;
1559
1560 error:
1561
1562 // end the class import
1563 type_engine_import_class_end( env );
1564
1565 return FALSE;
1566 }
1567 #endif // __DISABLE_HID__
1568
1569
1570
1571
1572
1573 #ifndef __DISABLE_MIDI__
1574 // static
1575 static t_CKUINT MidiRW_offset_data = 0;
1576 static t_CKUINT MidiMsgOut_offset_data = 0;
1577 static t_CKUINT MidiMsgIn_offset_data = 0;
1578 //-----------------------------------------------------------------------------
1579 // name: init_class_MidiRW()
1580 // desc: ...
1581 //-----------------------------------------------------------------------------
1582 t_CKBOOL init_class_MidiRW( Chuck_Env * env )
1583 {
1584 Chuck_DL_Func * func = NULL;
1585
1586 // init base class
1587 if( !type_engine_import_class_begin( env, "MidiRW", "Object",
1588 env->global(), MidiRW_ctor, MidiRW_dtor ) )
1589 return FALSE;
1590
1591 // add open()
1592 func = make_new_mfun( "int", "open", MidiRW_open );
1593 func->add_arg( "string", "filename" );
1594 if( !type_engine_import_mfun( env, func ) ) goto error;
1595
1596 // add close()
1597 func = make_new_mfun( "int", "close", MidiRW_close );
1598 if( !type_engine_import_mfun( env, func ) ) goto error;
1599
1600 // add write()
1601 func = make_new_mfun( "int", "write", MidiRW_write );
1602 func->add_arg( "MidiMsg", "msg" );
1603 func->add_arg( "time", "t" );
1604 if( !type_engine_import_mfun( env, func ) ) goto error;
1605
1606 // add read()
1607 func = make_new_mfun( "int", "read", MidiRW_read );
1608 func->add_arg( "MidiMsg", "msg" );
1609 //func->add_arg( "time", "t" );
1610 if( !type_engine_import_mfun( env, func ) ) goto error;
1611
1612 // add member variable
1613 MidiRW_offset_data = type_engine_import_mvar( env, "int", "@MidiRW_data", FALSE );
1614 if( MidiRW_offset_data == CK_INVALID_OFFSET ) goto error;
1615
1616 // end the class import
1617 type_engine_import_class_end( env );
1618
1619 // init base class
1620 if( !type_engine_import_class_begin( env, "MidiMsgOut", "Object",
1621 env->global(), MidiMsgOut_ctor, MidiMsgOut_dtor ) )
1622 return FALSE;
1623
1624 // add open()
1625 func = make_new_mfun( "int", "open", MidiMsgOut_open );
1626 func->add_arg( "string", "filename" );
1627 if( !type_engine_import_mfun( env, func ) ) goto error;
1628
1629 // add close()
1630 func = make_new_mfun( "int", "close", MidiMsgOut_close );
1631 if( !type_engine_import_mfun( env, func ) ) goto error;
1632
1633 // add write()
1634 func = make_new_mfun( "int", "write", MidiMsgOut_write );
1635 func->add_arg( "MidiMsg", "msg" );
1636 func->add_arg( "time", "t" );
1637 if( !type_engine_import_mfun( env, func ) ) goto error;
1638
1639 // add member variable
1640 MidiMsgOut_offset_data = type_engine_import_mvar( env, "int", "@MidiMsgOut_data", FALSE );
1641 if( MidiMsgOut_offset_data == CK_INVALID_OFFSET ) goto error;
1642
1643 // end the class import
1644 type_engine_import_class_end( env );
1645
1646 // init base class
1647 if( !type_engine_import_class_begin( env, "MidiMsgIn", "Object",
1648 env->global(), MidiMsgIn_ctor, MidiMsgIn_dtor ) )
1649 return FALSE;
1650
1651 // add open()
1652 func = make_new_mfun( "int", "open", MidiMsgIn_open );
1653 func->add_arg( "string", "filename" );
1654 if( !type_engine_import_mfun( env, func ) ) goto error;
1655
1656 // add close()
1657 func = make_new_mfun( "int", "close", MidiMsgIn_close );
1658 if( !type_engine_import_mfun( env, func ) ) goto error;
1659
1660 // add read()
1661 func = make_new_mfun( "int", "read", MidiMsgIn_read );
1662 func->add_arg( "MidiMsg", "msg" );
1663 //func->add_arg( "time", "t" );
1664 if( !type_engine_import_mfun( env, func ) ) goto error;
1665
1666 // add member variable
1667 MidiMsgIn_offset_data = type_engine_import_mvar( env, "int", "@MidiMsgIn_data", FALSE );
1668 if( MidiMsgIn_offset_data == CK_INVALID_OFFSET ) goto error;
1669
1670 // end the class import
1671 type_engine_import_class_end( env );
1672
1673 // initialize
1674 // HidInManager::init();
1675
1676 return TRUE;
1677
1678 error:
1679
1680 // end the class import
1681 type_engine_import_class_end( env );
1682
1683 return FALSE;
1684 }
1685 #endif // __DISABLE_MIDI__
1686
1687
1688
1689
1690
1691
1692 //-----------------------------------------------------------------------------
1693 // IO API
1694 //-----------------------------------------------------------------------------
1695 CK_DLL_MFUN( io_dummy_int )
1696 {
1697 // EM_error3( "(IO): internal error! inside an abstract function! help!" );
1698 // EM_error3( " note: please hunt down someone (e.g., Ge) to fix this..." );
1699 // assert( FALSE );
1700 RETURN->v_int = 0;
1701 }
1702
1703 CK_DLL_MFUN( io_dummy_float )
1704 {
1705 // return 0.0
1706 RETURN->v_float = 0;
1707 }
1708
1709 CK_DLL_MFUN( io_dummy_string )
1710 {
1711 // return NULL
1712 RETURN->v_string = NULL;
1713 }
1714
1715 CK_DLL_MFUN( io_dummy_void )
1716 {
1717 // do nothing
1718 }
1719
1720 CK_DLL_SFUN( io_newline )
1721 {
1722 RETURN->v_string = g_newline;
1723 }
1724
1725
1726 //-----------------------------------------------------------------------------
1727 // FileIO API
1728 //-----------------------------------------------------------------------------
1729 CK_DLL_CTOR( fileio_ctor )
1730 { }
1731
1732 CK_DLL_DTOR( fileio_dtor )
1733 { }
1734
1735 CK_DLL_MFUN( fileio_open )
1736 {
1737 std::string filename = GET_NEXT_STRING(ARGS)->str();
1738 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1739 t_CKINT default_flags =
1740 Chuck_IO::FLAG_READ_WRITE | Chuck_IO::TYPE_ASCII;
1741
1742 RETURN->v_int = f->open(filename, default_flags);
1743 }
1744
1745 CK_DLL_MFUN( fileio_openflags )
1746 {
1747 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1748 std::string filename = GET_NEXT_STRING(ARGS)->str();
1749 t_CKINT flags = GET_NEXT_INT(ARGS);
1750
1751 RETURN->v_int = f->open(filename, flags);
1752 }
1753
1754 CK_DLL_MFUN( fileio_close )
1755 {
1756 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1757 f->close();
1758 }
1759
1760 CK_DLL_MFUN( fileio_good )
1761 {
1762 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1763 RETURN->v_int = f->good();
1764 }
1765
1766 CK_DLL_MFUN( fileio_flush )
1767 {
1768 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1769 f->flush();
1770 }
1771
1772 CK_DLL_MFUN( fileio_getmode )
1773 {
1774 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1775 RETURN->v_int = f->mode();
1776 }
1777
1778 CK_DLL_MFUN( fileio_setmode )
1779 {
1780 t_CKINT flag = GET_NEXT_INT(ARGS);
1781
1782 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1783 f->mode( flag );
1784 RETURN->v_int = 0;
1785 }
1786
1787 CK_DLL_MFUN( fileio_size )
1788 {
1789 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1790 RETURN->v_int = f->size();
1791 }
1792
1793 CK_DLL_MFUN( fileio_seek )
1794 {
1795 t_CKINT pos = GET_NEXT_INT(ARGS);
1796
1797 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1798 f->seek(pos);
1799 }
1800
1801 CK_DLL_MFUN( fileio_tell )
1802 {
1803 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1804 RETURN->v_int = f->tell();
1805 }
1806
1807 CK_DLL_MFUN( fileio_isdir )
1808 {
1809 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1810 RETURN->v_int = f->isDir();
1811 }
1812
1813 CK_DLL_MFUN( fileio_dirlist )
1814 {
1815 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1816 Chuck_ArrayInt * a = f->dirList();
1817 RETURN->v_object = a;
1818 }
1819
1820 CK_DLL_MFUN( file_ctrl_autoPrefixAndExtension )
1821 {
1822 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1823 Chuck_String * prefix = GET_NEXT_STRING(ARGS);
1824 Chuck_String * extension = GET_NEXT_STRING(ARGS);
1825
1826 f->m_autoPrefix = prefix ? prefix->str() : "";
1827 f->m_autoExtension = extension ? extension->str() : "";
1828 }
1829
1830 CK_DLL_MFUN( file_cget_autoPrefix )
1831 {
1832 // get this
1833 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1834 // create string, no ref count here, as we are not holding on to it
1835 RETURN->v_object = ck_create_string( VM, f->m_autoPrefix.c_str(), FALSE );
1836 }
1837
1838 CK_DLL_MFUN( file_cget_autoExtension )
1839 {
1840 // get this
1841 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1842 // create string, no ref count here, as we are not holding on to it
1843 RETURN->v_object = ck_create_string( VM, f->m_autoExtension.c_str(), FALSE );
1844 }
1845
1846 CK_DLL_MFUN( file_cget_filename )
1847 {
1848 // get this
1849 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1850 // create string, no ref count here, as we are not holding on to it
1851 RETURN->v_object = ck_create_string( VM, f->filename().c_str(), FALSE );
1852 }
1853
1854 // expandPath | 1.5.1.3
1855 CK_DLL_SFUN( fileio_expandpath_impl )
1856 {
1857 Chuck_String * ckPath = GET_CK_STRING(ARGS);
1858 // get arg
1859 string path = ckPath != NULL ? ckPath->str() : "";
1860 // expand; don't ensure path
1861 string expanded = expand_filepath( path, FALSE );
1862 // instantiate and initalize object
1863 Chuck_String * string = (Chuck_String *)instantiate_and_initialize_object( VM->env()->ckt_string, VM );
1864 // set the value
1865 string->set( expanded );
1866 // return
1867 RETURN->v_string = string;
1868 }
1869
1870 /*
1871 CK_DLL_MFUN( fileio_read )
1872 {
1873 t_CKINT len = GET_NEXT_INT(ARGS);
1874 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1875
1876 Chuck_String * s = f->read( len );
1877 RETURN->v_object = s;
1878 }
1879 */
1880
1881 CK_DLL_MFUN( fileio_readline )
1882 {
1883 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1884 Chuck_String * ret = f->readLine();
1885 RETURN->v_object = ret;
1886 }
1887
1888 CK_DLL_MFUN( fileio_readint )
1889 {
1890 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1891 t_CKINT defaultflags = Chuck_IO::INT32;
1892
1893 /* (ATODO: doesn't look like asynchronous reading will work)
1894 if (f->mode() == Chuck_IO::MODE_ASYNC)
1895 {
1896 // set up arguments
1897 Chuck_IO::async_args *args = new Chuck_IO::async_args;
1898 args->RETURN = (void *)RETURN;
1899 args->fileio_obj = f;
1900 args->intArg = defaultflags;
1901 // set shred to wait for I/O completion
1902 f->m_asyncEvent->wait( SHRED, SHRED->vm_ref );
1903 // start thread
1904 bool ret = f->m_thread->start( f->readInt_thread, (void *)args );
1905 if (!ret)
1906 {
1907 cerr << "m_thread->start failed; recreating m_thread" << endl;
1908 delete f->m_thread;
1909 f->m_thread = new XThread;
1910 ret = f->m_thread->start( f->readInt_thread, (void *)args );
1911 if (!ret) {
1912 EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" );
1913 }
1914 }
1915 } else {*/
1916
1917 t_CKINT ret = f->readInt( defaultflags );
1918 RETURN->v_int = ret;
1919
1920 //}
1921 // ATODO: Debug
1922 //sleep(1);
1923 //cerr << "fileio_readint exiting" << endl;
1924 }
1925
1926 CK_DLL_MFUN( fileio_readintflags )
1927 {
1928 t_CKINT flags = GET_NEXT_INT(ARGS);
1929 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1930 RETURN->v_int = f->readInt( flags );
1931 }
1932
1933 CK_DLL_MFUN( fileio_readint8 )
1934 {
1935 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1936 RETURN->v_int = f->readInt( Chuck_IO::INT8 );
1937 }
1938
1939 CK_DLL_MFUN( fileio_readint16 )
1940 {
1941 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1942 RETURN->v_int = f->readInt( Chuck_IO::INT16 );
1943 }
1944
1945 CK_DLL_MFUN( fileio_readint32 )
1946 {
1947 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1948 RETURN->v_int = f->readInt( Chuck_IO::INT32 );
1949 }
1950
1951 CK_DLL_MFUN( fileio_readfloat )
1952 {
1953 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1954 RETURN->v_float = f->readFloat();
1955 }
1956
1957 CK_DLL_MFUN( fileio_readfloatflags )
1958 {
1959 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1960 t_CKINT flags = GET_NEXT_INT(ARGS);
1961 RETURN->v_float = f->readFloat( flags );
1962 }
1963
1964 CK_DLL_MFUN( fileio_eof )
1965 {
1966 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1967 t_CKBOOL ret = f->eof();
1968 RETURN->v_int = ret;
1969 }
1970
1971 CK_DLL_MFUN( fileio_more )
1972 {
1973 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1974 t_CKBOOL ret = !f->eof();
1975 RETURN->v_int = ret;
1976 }
1977
1978 CK_DLL_MFUN( fileio_writestring )
1979 {
1980 std::string val = GET_NEXT_STRING(ARGS)->str();
1981 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
1982
1983 #ifndef __DISABLE_ASYNCH_IO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK)
1984 if (f->mode() == Chuck_IO::MODE_ASYNC)
1985 {
1986 // set up arguments
1987 Chuck_IO::async_args *args = new Chuck_IO::async_args;
1988 args->RETURN = (void *)RETURN;
1989 args->fileio_obj = f;
1990 args->stringArg = std::string(val);
1991 // set shred to wait for I/O completion
1992 assert( SHRED != NULL );
1993 f->m_asyncEvent->wait( SHRED, SHRED->vm_ref );
1994 // start thread
1995 bool ret = f->m_thread->start( f->writeStr_thread, (void *)args );
1996 if (!ret) {
1997 // for some reason, the XThread object needs to be
1998 // deleted and reconstructed every time after call #375
1999 delete f->m_thread;
2000 f->m_thread = new XThread;
2001 ret = f->m_thread->start( f->writeStr_thread, (void *)args );
2002 if (!ret) {
2003 EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" );
2004 }
2005 }
2006 } else {
2007 f->write(val);
2008 }
2009 #else
2010 f->write(val);
2011 #endif // __DISABLE_ASYNCH_IO__
2012 }
2013
2014 CK_DLL_MFUN( fileio_writeint )
2015 {
2016 t_CKINT val = GET_NEXT_INT(ARGS);
2017 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
2018
2019 #ifndef __DISABLE_ASYNCH_IO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK)
2020 if (f->mode() == Chuck_IO::MODE_ASYNC)
2021 {
2022 // set up arguments
2023 Chuck_IO::async_args *args = new Chuck_IO::async_args;
2024 args->RETURN = (void *)RETURN;
2025 args->fileio_obj = f;
2026 args->intArg = val;
2027 // set shred to wait for I/O completion
2028 assert( SHRED != NULL );
2029 f->m_asyncEvent->wait( SHRED, SHRED->vm_ref );
2030 // start thread
2031 bool ret = f->m_thread->start( f->writeInt_thread, (void *)args );
2032 if (!ret) {
2033 // for some reason, the XThread object needs to be
2034 // deleted and reconstructed every time after call #375
2035 delete f->m_thread;
2036 f->m_thread = new XThread;
2037 ret = f->m_thread->start( f->writeInt_thread, (void *)args );
2038 if (!ret) {
2039 EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" );
2040 }
2041 }
2042 } else {
2043 f->write(val);
2044 }
2045 #else
2046 f->write(val);
2047 #endif // __DISABLE_ASYNCH_IO__
2048 }
2049
2050 CK_DLL_MFUN( fileio_writeintflags )
2051 {
2052 t_CKINT val = GET_NEXT_INT(ARGS);
2053 t_CKINT flags = GET_NEXT_INT(ARGS);
2054 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
2055
2056 #ifndef __DISABLE_ASYNCH_IO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK)
2057 if (f->mode() == Chuck_IO::MODE_ASYNC)
2058 {
2059 // TODO: pass flags in args
2060 // set up arguments
2061 Chuck_IO::async_args *args = new Chuck_IO::async_args;
2062 args->RETURN = (void *)RETURN;
2063 args->fileio_obj = f;
2064 args->intArg = val;
2065 // set shred to wait for I/O completion
2066 assert( SHRED != NULL );
2067 f->m_asyncEvent->wait( SHRED, SHRED->vm_ref );
2068 // start thread
2069 bool ret = f->m_thread->start( f->writeInt_thread, (void *)args );
2070 if (!ret) {
2071 // for some reason, the XThread object needs to be
2072 // deleted and reconstructed every time after call #375
2073 delete f->m_thread;
2074 f->m_thread = new XThread;
2075 ret = f->m_thread->start( f->writeInt_thread, (void *)args );
2076 if (!ret) {
2077 EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" );
2078 }
2079 }
2080 } else {
2081 f->write(val, flags);
2082 }
2083 #else
2084 f->write(val, flags);
2085 #endif // __DISABLE_ASYNCH_IO__
2086
2087 }
2088
2089 CK_DLL_MFUN( fileio_writefloat )
2090 {
2091 t_CKFLOAT val = GET_NEXT_FLOAT(ARGS);
2092 t_CKINT flagsDefault = Chuck_IO::FLOAT32;
2093 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
2094
2095 #ifndef __DISABLE_ASYNCH_IO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK)
2096 if (f->mode() == Chuck_IO::MODE_ASYNC)
2097 {
2098 // set up arguments
2099 Chuck_IO::async_args *args = new Chuck_IO::async_args;
2100 args->RETURN = (void *)RETURN;
2101 args->fileio_obj = f;
2102 args->floatArg = val;
2103 args->intArg = flagsDefault; // 1.5.0.1 (ge) add default
2104 // set shred to wait for I/O completion
2105 assert( SHRED != NULL );
2106 f->m_asyncEvent->wait( SHRED, SHRED->vm_ref );
2107 // start thread
2108 bool ret = f->m_thread->start( f->writeFloat_thread, (void *)args );
2109 if (!ret) {
2110 // for some reason, the XThread object needs to be
2111 // deleted and reconstructed every time after call #375
2112 delete f->m_thread;
2113 f->m_thread = new XThread;
2114 ret = f->m_thread->start( f->writeFloat_thread, (void *)args );
2115 if (!ret) {
2116 EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" );
2117 }
2118 }
2119 } else {
2120 f->write( val, flagsDefault );
2121 }
2122 #else
2123 f->write( val, flagsDefault );
2124 #endif //__DISABLE_ASYNCH_IO__
2125 }
2126
2127 CK_DLL_MFUN( fileio_writefloatflags )
2128 {
2129 t_CKFLOAT val = GET_NEXT_FLOAT(ARGS);
2130 t_CKINT flags = GET_NEXT_INT(ARGS);
2131 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
2132
2133 #ifndef __DISABLE_ASYNCH_IO__ // 1.5.0.0 (ge) | made more granular (e.g., for WebChucK)
2134 if (f->mode() == Chuck_IO::MODE_ASYNC)
2135 {
2136 // set up arguments
2137 Chuck_IO::async_args *args = new Chuck_IO::async_args;
2138 args->RETURN = (void *)RETURN;
2139 args->fileio_obj = f;
2140 args->floatArg = val;
2141 args->intArg = flags; // 1.5.0.1 (ge) added
2142 // set shred to wait for I/O completion
2143 assert( SHRED != NULL );
2144 f->m_asyncEvent->wait( SHRED, SHRED->vm_ref );
2145 // start thread
2146 bool ret = f->m_thread->start( f->writeFloat_thread, (void *)args );
2147 if (!ret) {
2148 // for some reason, the XThread object needs to be
2149 // deleted and reconstructed every time after call #375
2150 delete f->m_thread;
2151 f->m_thread = new XThread;
2152 ret = f->m_thread->start( f->writeFloat_thread, (void *)args );
2153 if (!ret) {
2154 EM_error3( "(FileIO): failed to start thread for asynchronous mode I/O" );
2155 }
2156 }
2157 } else {
2158 f->write( val,flags );
2159 }
2160 #else
2161 f->write( val,flags );
2162 #endif //__DISABLE_ASYNCH_IO__
2163 }
2164
2165
2166
2167
2168 //-----------------------------------------------------------------------------
2169 // Chout API
2170 //-----------------------------------------------------------------------------
2171 CK_DLL_MFUN( chout_close )
2172 {
2173 // problem
2174 // CK_FPRINTF_STDERR( "[chuck]: cannot close 'chout'...\n" );
2175 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2176 c->close();
2177 }
2178
2179 CK_DLL_MFUN( chout_good )
2180 {
2181 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2182 RETURN->v_int = c->good();
2183 }
2184
2185 CK_DLL_MFUN( chout_flush )
2186 {
2187 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2188 c->flush();
2189 }
2190
2191 CK_DLL_MFUN( chout_getmode )
2192 {
2193 // problem
2194 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2195 RETURN->v_int = c->mode();
2196 }
2197
2198 CK_DLL_MFUN( chout_setmode )
2199 {
2200 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2201 c->mode( GET_NEXT_INT(ARGS) );
2202 RETURN->v_int = c->mode();
2203 }
2204
2205 /*
2206 CK_DLL_MFUN( chout_read )
2207 {
2208 t_CKINT len = GET_NEXT_INT(ARGS);
2209 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
2210
2211 Chuck_String * s = f->read( len );
2212 RETURN->v_object = s;
2213 }
2214 */
2215
2216 CK_DLL_MFUN( chout_readline )
2217 {
2218 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2219 Chuck_String * ret = c->readLine();
2220 RETURN->v_object = ret;
2221 }
2222
2223 CK_DLL_MFUN( chout_readint )
2224 {
2225 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2226 RETURN->v_int = c->readInt( Chuck_IO::INT32 );
2227 }
2228
2229 CK_DLL_MFUN( chout_readintflags )
2230 {
2231 t_CKINT flags = GET_NEXT_INT(ARGS);
2232
2233 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2234 t_CKINT ret = c->readInt( flags );
2235
2236 RETURN->v_int = ret;
2237 }
2238
2239 CK_DLL_MFUN( chout_readfloat )
2240 {
2241 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2242 t_CKFLOAT ret = c->readFloat();
2243 RETURN->v_float = ret;
2244 }
2245
2246 CK_DLL_MFUN( chout_eof )
2247 {
2248 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2249 t_CKBOOL ret = c->eof();
2250 RETURN->v_int = ret;
2251 }
2252
2253 CK_DLL_MFUN( chout_more )
2254 {
2255 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2256 t_CKBOOL ret = !c->eof();
2257 RETURN->v_int = ret;
2258 }
2259
2260 CK_DLL_MFUN( chout_writestring )
2261 {
2262 std::string val = GET_NEXT_STRING(ARGS)->str();
2263
2264 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2265 c->write( val );
2266
2267 // 1.5.0.0 | (ge) added a single newline -> trigger chout flush
2268 if( val == "\n" ) c->flush();
2269 }
2270
2271 CK_DLL_MFUN( chout_writeint )
2272 {
2273 t_CKINT val = GET_NEXT_INT(ARGS);
2274
2275 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2276 c->write(val);
2277 }
2278
2279 CK_DLL_MFUN( chout_writefloat )
2280 {
2281 t_CKFLOAT val = GET_NEXT_FLOAT(ARGS);
2282
2283 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2284 c->write(val);
2285 }
2286
2287
2288
2289
2290 //-----------------------------------------------------------------------------
2291 // Cherr API
2292 //-----------------------------------------------------------------------------
2293 CK_DLL_MFUN( cherr_close )
2294 {
2295 // problem
2296 // CK_FPRINTF_STDERR( "[chuck]: cannot close 'cherr'...\n" );
2297 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2298 c->close();
2299 }
2300
2301 CK_DLL_MFUN( cherr_good )
2302 {
2303 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2304 RETURN->v_int = c->good();
2305 }
2306
2307 CK_DLL_MFUN( cherr_flush )
2308 {
2309 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2310 c->flush();
2311 }
2312
2313 CK_DLL_MFUN( cherr_getmode )
2314 {
2315 // problem
2316 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2317 RETURN->v_int = c->mode();
2318 }
2319
2320 CK_DLL_MFUN( cherr_setmode )
2321 {
2322 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2323 c->mode( GET_NEXT_INT(ARGS) );
2324 RETURN->v_int = c->mode();
2325 }
2326
2327 /*
2328 CK_DLL_MFUN( cherr_read )
2329 {
2330 t_CKINT len = GET_NEXT_INT(ARGS);
2331 Chuck_IO_File * f = (Chuck_IO_File *)SELF;
2332
2333 Chuck_String * s = f->read( len );
2334 RETURN->v_object = s;
2335 }
2336 */
2337
2338 CK_DLL_MFUN( cherr_readline )
2339 {
2340 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2341 Chuck_String * ret = c->readLine();
2342 RETURN->v_object = ret;
2343 }
2344
2345 CK_DLL_MFUN( cherr_readint )
2346 {
2347 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2348 RETURN->v_int = c->readInt( Chuck_IO::INT32 );
2349 }
2350
2351 CK_DLL_MFUN( cherr_readintflags )
2352 {
2353 t_CKINT flags = GET_NEXT_INT(ARGS);
2354
2355 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2356 t_CKINT ret = c->readInt( flags );
2357
2358 RETURN->v_int = ret;
2359 }
2360
2361 CK_DLL_MFUN( cherr_readfloat )
2362 {
2363 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2364 t_CKFLOAT ret = c->readFloat();
2365 RETURN->v_float = ret;
2366 }
2367
2368 CK_DLL_MFUN( cherr_eof )
2369 {
2370 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2371 t_CKBOOL ret = c->eof();
2372 RETURN->v_int = ret;
2373 }
2374
2375 CK_DLL_MFUN( cherr_more )
2376 {
2377 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2378 t_CKBOOL ret = !c->eof();
2379 RETURN->v_int = ret;
2380 }
2381
2382 CK_DLL_MFUN( cherr_writestring )
2383 {
2384 std::string val = GET_NEXT_STRING(ARGS)->str();
2385
2386 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2387 c->write( val );
2388 }
2389
2390 CK_DLL_MFUN( cherr_writeint )
2391 {
2392 t_CKINT val = GET_NEXT_INT(ARGS);
2393
2394 Chuck_IO_Chout * c = SHRED->vm_ref->chout();
2395 c->write(val);
2396 }
2397
2398 CK_DLL_MFUN( cherr_writefloat )
2399 {
2400 t_CKFLOAT val = GET_NEXT_FLOAT(ARGS);
2401
2402 Chuck_IO_Cherr * c = SHRED->vm_ref->cherr();
2403 c->write(val);
2404 }
2405
2406
2407
2408
2409 #ifndef __DISABLE_MIDI__
2410
2411 //-----------------------------------------------------------------------------
2412 // MidiIn API
2413 //-----------------------------------------------------------------------------
2414 CK_DLL_CTOR( MidiIn_ctor )
2415 {
2416 MidiIn * min = new MidiIn;
2417 min->SELF = SELF;
2418 OBJ_MEMBER_INT(SELF, MidiIn_offset_data) = (t_CKINT)min;
2419 }
2420
2421 CK_DLL_DTOR( MidiIn_dtor )
2422 {
2423 delete (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2424 OBJ_MEMBER_INT(SELF, MidiIn_offset_data) = 0;
2425 }
2426
2427 CK_DLL_MFUN( MidiIn_open )
2428 {
2429 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2430 t_CKINT port = GET_CK_INT(ARGS);
2431 RETURN->v_int = min->open( SHRED->vm_ref, port );
2432 }
2433
2434 CK_DLL_MFUN( MidiIn_open_named ) // added 1.3.0.0
2435 {
2436 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2437 Chuck_String * name = GET_CK_STRING(ARGS);
2438 RETURN->v_int = min->open( SHRED->vm_ref, name->str() );
2439 }
2440
2441 CK_DLL_MFUN( MidiIn_good )
2442 {
2443 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2444 RETURN->v_int = (t_CKINT)min->good();
2445 }
2446
2447 CK_DLL_MFUN( MidiIn_num )
2448 {
2449 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2450 RETURN->v_int = min->num();
2451 }
2452
2453 CK_DLL_MFUN( MidiIn_printerr )
2454 {
2455 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2456 t_CKINT print_or_not = GET_CK_INT(ARGS);
2457 min->set_suppress( !print_or_not );
2458 }
2459
2460 CK_DLL_MFUN( MidiIn_name )
2461 {
2462 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2463 // TODO: memory leak, please fix, Thanks.
2464 Chuck_String * a = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2465 // only if valid
2466 if( min->good() )
2467 a->set( min->min->getPortName( min->num() ) );
2468 RETURN->v_string = a;
2469 }
2470
2471 CK_DLL_MFUN( MidiIn_recv )
2472 {
2473 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2474 Chuck_Object * fake_msg = GET_CK_OBJECT(ARGS);
2475 MidiMsg the_msg;
2476 RETURN->v_int = min->recv( &the_msg );
2477 if( RETURN->v_int )
2478 {
2479 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data1) = the_msg.data[0];
2480 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data2) = the_msg.data[1];
2481 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data3) = the_msg.data[2];
2482 }
2483 }
2484
2485
2486 CK_DLL_MFUN( MidiIn_can_wait )
2487 {
2488 MidiIn * min = (MidiIn *)OBJ_MEMBER_INT(SELF, MidiIn_offset_data);
2489 RETURN->v_int = min->empty();
2490 }
2491
2492
2493 //-----------------------------------------------------------------------------
2494 // MidiOut API
2495 //-----------------------------------------------------------------------------
2496 CK_DLL_CTOR( MidiOut_ctor )
2497 {
2498 OBJ_MEMBER_INT(SELF, MidiOut_offset_data) = (t_CKUINT)new MidiOut;
2499 }
2500
2501 CK_DLL_DTOR( MidiOut_dtor )
2502 {
2503 delete (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2504 OBJ_MEMBER_INT(SELF, MidiOut_offset_data) = 0;
2505 }
2506
2507 CK_DLL_MFUN( MidiOut_open )
2508 {
2509 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2510 t_CKINT port = GET_CK_INT(ARGS);
2511 RETURN->v_int = mout->open( port );
2512 }
2513
2514 CK_DLL_MFUN( MidiOut_open_named ) // added 1.3.0.0
2515 {
2516 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2517 Chuck_String * name = GET_CK_STRING(ARGS);
2518 RETURN->v_int = mout->open( name->str() );
2519 }
2520
2521 CK_DLL_MFUN( MidiOut_good )
2522 {
2523 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2524 RETURN->v_int = (t_CKINT)mout->good();
2525 }
2526
2527 CK_DLL_MFUN( MidiOut_num )
2528 {
2529 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2530 RETURN->v_int = mout->num();
2531 }
2532
2533 CK_DLL_MFUN( MidiOut_name )
2534 {
2535 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2536 // TODO: memory leak, please fix, Thanks.
2537 Chuck_String * a = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2538 // only if valid
2539 if( mout->good() )
2540 a->set( mout->mout->getPortName( mout->num() ) );
2541 RETURN->v_string = a;
2542 }
2543
2544 CK_DLL_MFUN( MidiOut_printerr )
2545 {
2546 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2547 t_CKINT print_or_not = GET_CK_INT(ARGS);
2548 mout->set_suppress( !print_or_not );
2549 }
2550
2551 CK_DLL_MFUN( MidiOut_send ) // 1.5.2.5 | (ge) added
2552 {
2553 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2554 t_CKBYTE status = (t_CKBYTE)GET_NEXT_INT(ARGS);
2555 t_CKBYTE data1 = (t_CKBYTE)GET_NEXT_INT(ARGS);
2556 t_CKBYTE data2 = (t_CKBYTE)GET_NEXT_INT(ARGS);
2557 RETURN->v_int = mout->send( status, data1, data2 );
2558 }
2559
2560 CK_DLL_MFUN( MidiOut_send_msg )
2561 {
2562 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2563 Chuck_Object * fake_msg = GET_CK_OBJECT(ARGS);
2564 MidiMsg the_msg;
2565 the_msg.data[0] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data1);
2566 the_msg.data[1] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data2);
2567 the_msg.data[2] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data3);
2568 RETURN->v_int = mout->send( &the_msg );
2569 }
2570
2571 CK_DLL_MFUN( MidiOut_noteOn ) // 1.5.2.5 (cviejo) added
2572 {
2573 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2574 t_CKINT channel = GET_NEXT_INT(ARGS);
2575 t_CKINT note = GET_NEXT_INT(ARGS);
2576 t_CKINT velocity = GET_NEXT_INT(ARGS);
2577 RETURN->v_int = mout->noteon( channel, note, velocity );
2578 }
2579
2580 CK_DLL_MFUN( MidiOut_noteOff )
2581 {
2582 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2583 t_CKINT channel = GET_NEXT_INT(ARGS);
2584 t_CKINT note = GET_NEXT_INT(ARGS);
2585 t_CKINT velocity = GET_NEXT_INT(ARGS);
2586 RETURN->v_int = mout->noteoff( channel, note, velocity );
2587 }
2588
2589 CK_DLL_MFUN( MidiOut_controlChange )
2590 {
2591 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2592 t_CKINT channel = GET_NEXT_INT(ARGS);
2593 t_CKINT control = GET_NEXT_INT(ARGS);
2594 t_CKINT value = GET_NEXT_INT(ARGS);
2595 RETURN->v_int = mout->ctrlchange( channel, control, value );
2596 }
2597
2598 CK_DLL_MFUN( MidiOut_programChange )
2599 {
2600 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2601 t_CKINT channel = GET_NEXT_INT(ARGS);
2602 t_CKINT program = GET_NEXT_INT(ARGS);
2603 RETURN->v_int = mout->progchange( channel, program );
2604 }
2605
2606 CK_DLL_MFUN( MidiOut_pitchBend )
2607 {
2608 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2609 t_CKINT channel = GET_NEXT_INT(ARGS);
2610 t_CKINT value = GET_NEXT_INT(ARGS);
2611 RETURN->v_int = mout->pitchbend( channel, value );
2612 }
2613
2614 CK_DLL_MFUN( MidiOut_pitchBend_fine )
2615 {
2616 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2617 t_CKINT channel = GET_NEXT_INT(ARGS);
2618 t_CKINT lsb = GET_NEXT_INT(ARGS);
2619 t_CKINT msb = GET_NEXT_INT(ARGS);
2620 RETURN->v_int = mout->pitchbendFine( channel, lsb, msb );
2621 }
2622
2623 CK_DLL_MFUN( MidiOut_polyPressure )
2624 {
2625 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2626 t_CKINT channel = GET_NEXT_INT(ARGS);
2627 t_CKINT note = GET_NEXT_INT(ARGS);
2628 t_CKINT pressure = GET_NEXT_INT(ARGS);
2629 RETURN->v_int = mout->polypress( channel, note, pressure );
2630 }
2631
2632 CK_DLL_MFUN( MidiOut_channelPressure )
2633 {
2634 MidiOut * mout = (MidiOut *)OBJ_MEMBER_INT(SELF, MidiOut_offset_data);
2635 t_CKINT channel = GET_NEXT_INT(ARGS);
2636 t_CKINT pressure = GET_NEXT_INT(ARGS);
2637 RETURN->v_int = mout->chanpress( channel, pressure );
2638 }
2639
2640 #endif // __DISABLE_MIDI__
2641
2642
2643 #ifndef __DISABLE_HID__
2644 //-----------------------------------------------------------------------------
2645 // HidMsg API
2646 //-----------------------------------------------------------------------------
2647 CK_DLL_MFUN( HidMsg_is_axis_motion )
2648 {
2649 RETURN->v_int = ( ( t_CKINT ) OBJ_MEMBER_INT( SELF, HidMsg_offset_type ) ==
2650 CK_HID_JOYSTICK_AXIS ? 1 : 0 );
2651 }
2652
2653 CK_DLL_MFUN( HidMsg_is_button_down )
2654 {
2655 RETURN->v_int = ( ( t_CKINT ) OBJ_MEMBER_INT( SELF, HidMsg_offset_type ) ==
2656 CK_HID_BUTTON_DOWN ? 1 : 0 );
2657 }
2658
2659 CK_DLL_MFUN( HidMsg_is_button_up )
2660 {
2661 RETURN->v_int = ( ( t_CKINT ) OBJ_MEMBER_INT( SELF, HidMsg_offset_type ) ==
2662 CK_HID_BUTTON_UP ? 1 : 0 );
2663 }
2664
2665 CK_DLL_MFUN( HidMsg_is_mouse_motion )
2666 {
2667 RETURN->v_int = ( ( t_CKINT ) OBJ_MEMBER_INT( SELF, HidMsg_offset_type ) ==
2668 CK_HID_MOUSE_MOTION ? 1 : 0 );
2669 }
2670
2671 CK_DLL_MFUN( HidMsg_is_hat_motion )
2672 {
2673 RETURN->v_int = ( ( t_CKINT ) OBJ_MEMBER_INT( SELF, HidMsg_offset_type ) ==
2674 CK_HID_JOYSTICK_HAT ? 1 : 0 );
2675 }
2676
2677 CK_DLL_MFUN( HidMsg_is_wheel_motion )
2678 {
2679 RETURN->v_int = ( ( t_CKINT ) OBJ_MEMBER_INT( SELF, HidMsg_offset_type ) ==
2680 CK_HID_MOUSE_WHEEL ? 1 : 0 );
2681 }
2682
2683 //-----------------------------------------------------------------------------
2684 // HidIn API
2685 //-----------------------------------------------------------------------------
2686 CK_DLL_CTOR( HidIn_ctor )
2687 {
2688 HidIn * min = new HidIn;
2689 min->SELF = SELF;
2690 OBJ_MEMBER_INT(SELF, HidIn_offset_data) = (t_CKINT)min;
2691 }
2692
2693 CK_DLL_DTOR( HidIn_dtor )
2694 {
2695 delete (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2696 OBJ_MEMBER_INT(SELF, HidIn_offset_data) = 0;
2697 }
2698
2699 CK_DLL_MFUN( HidIn_open )
2700 {
2701 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2702 t_CKINT type = GET_NEXT_INT(ARGS);
2703 t_CKINT num = GET_NEXT_INT(ARGS);
2704 // CK_FPRINTF_STDERR( "HidIn_open %li %li\n", type, num );
2705 RETURN->v_int = min->open( SHRED->vm_ref, type, num );
2706 }
2707
2708 CK_DLL_MFUN( HidIn_open_named )
2709 {
2710 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2711 Chuck_String * name = GET_NEXT_STRING(ARGS);
2712 std::string s = name->str();
2713 // set CK_HID_DEV_COUNT as a special flag
2714 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_COUNT, s );
2715 }
2716
2717 CK_DLL_MFUN( HidIn_open_joystick )
2718 {
2719 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2720 t_CKINT num = GET_NEXT_INT(ARGS);
2721 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_JOYSTICK, num );
2722 }
2723
2724 CK_DLL_MFUN( HidIn_open_joystick_2 )
2725 {
2726 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2727 t_CKINT num = GET_NEXT_INT(ARGS);
2728 t_CKINT suppressErrMsg = GET_NEXT_INT(ARGS);
2729 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_JOYSTICK, num, suppressErrMsg );
2730 }
2731
2732 CK_DLL_MFUN( HidIn_open_mouse )
2733 {
2734 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2735 t_CKINT num = GET_NEXT_INT(ARGS);
2736 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_MOUSE, num );
2737 }
2738
2739 CK_DLL_MFUN( HidIn_open_mouse_2 )
2740 {
2741 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2742 t_CKINT num = GET_NEXT_INT(ARGS);
2743 t_CKINT suppressErrMsg = GET_NEXT_INT(ARGS);
2744 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_MOUSE, num, suppressErrMsg );
2745 }
2746
2747 CK_DLL_MFUN( HidIn_open_keyboard )
2748 {
2749 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2750 t_CKINT num = GET_NEXT_INT(ARGS);
2751 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_KEYBOARD, num );
2752 }
2753
2754 CK_DLL_MFUN( HidIn_open_keyboard_2 )
2755 {
2756 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2757 t_CKINT num = GET_NEXT_INT(ARGS);
2758 t_CKINT suppressErrMsg = GET_NEXT_INT(ARGS);
2759 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_KEYBOARD, num, suppressErrMsg );
2760 }
2761
2762 CK_DLL_MFUN( HidIn_open_tiltsensor )
2763 {
2764 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2765 RETURN->v_int = min->open( SHRED->vm_ref, CK_HID_DEV_TILTSENSOR, 0 );
2766 }
2767
2768 CK_DLL_MFUN( HidIn_good )
2769 {
2770 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2771 RETURN->v_int = (t_CKINT)min->good();
2772 }
2773
2774 CK_DLL_MFUN( HidIn_num )
2775 {
2776 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2777 RETURN->v_int = min->num();
2778 }
2779
2780 CK_DLL_MFUN( HidIn_printerr )
2781 {
2782 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2783 t_CKINT print_or_not = GET_CK_INT(ARGS);
2784 min->set_suppress( !print_or_not );
2785 }
2786
2787 CK_DLL_MFUN( HidIn_name )
2788 {
2789 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2790 // TODO: memory leak, please fix, Thanks.
2791 // Chuck_String * a = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2792 // only if valid
2793 // if( min->good() )
2794 // a->str = min->phin->getPortName( min->num() );
2795 // TODO: is null problem?
2796 RETURN->v_string = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
2797 RETURN->v_string->set( min->name() );
2798 }
2799
2800 CK_DLL_MFUN( HidIn_recv )
2801 {
2802 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2803 Chuck_Object * fake_msg = GET_CK_OBJECT(ARGS);
2804 HidMsg the_msg;
2805 RETURN->v_int = min->recv( &the_msg );
2806 if( RETURN->v_int )
2807 {
2808 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_device_type) = the_msg.device_type;
2809 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_device_num) = the_msg.device_num;
2810 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_type) = the_msg.type;
2811 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_which) = the_msg.eid;
2812 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_idata) = the_msg.idata[0];
2813 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_fdata) = the_msg.fdata[0];
2814
2815 // mouse motion specific member variables
2816 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_deltax) = the_msg.idata[0];
2817 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_deltay) = the_msg.idata[1];
2818 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_cursorx) = the_msg.idata[2];
2819 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_cursory) = the_msg.idata[3];
2820 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_scaledcursorx) = the_msg.fdata[0];
2821 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_scaledcursory) = the_msg.fdata[1];
2822
2823 // axis motion specific member variables
2824 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_axis_position) = the_msg.idata[0];
2825 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_scaled_axis_position) = the_msg.fdata[0];
2826 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_axis_position2) = the_msg.fdata[0];
2827
2828 // hat motion specific variables
2829 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_hat_position) = the_msg.idata[0];
2830
2831 // keyboard specific variables
2832 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_key) = the_msg.idata[1];
2833 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_ascii) = the_msg.idata[2];
2834
2835 // accelerometer (tilt sensor, wii remote) specific members
2836 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_x) = the_msg.idata[0];
2837 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_y) = the_msg.idata[1];
2838 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_z) = the_msg.idata[2];
2839
2840 // multitouch stuff - added 1.3.0.0
2841 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_touchx) = the_msg.fdata[0];
2842 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_touchy) = the_msg.fdata[1];
2843 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_touchsize) = the_msg.fdata[2];
2844 }
2845 }
2846
2847 CK_DLL_MFUN( HidIn_read )
2848 {
2849 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2850 t_CKINT type = GET_NEXT_INT(ARGS);
2851 t_CKINT num = GET_NEXT_INT(ARGS);
2852 Chuck_Object * fake_msg = GET_NEXT_OBJECT(ARGS);
2853
2854 HidMsg the_msg;
2855
2856 RETURN->v_int = min->read( type, num, &the_msg );
2857
2858 if( RETURN->v_int )
2859 {
2860 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_device_type) = the_msg.device_type;
2861 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_device_num) = the_msg.device_num;
2862 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_type) = the_msg.type;
2863 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_which) = the_msg.eid;
2864
2865 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_idata) = the_msg.idata[0];
2866 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_fdata) = the_msg.fdata[0];
2867
2868 // mouse motion specific member members
2869 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_deltax) = the_msg.idata[0];
2870 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_deltay) = the_msg.idata[1];
2871 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_cursorx) = the_msg.idata[2];
2872 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_cursory) = the_msg.idata[3];
2873 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_scaledcursorx) = the_msg.fdata[0];
2874 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_scaledcursory) = the_msg.fdata[1];
2875
2876 // joystick axis specific member members
2877 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_axis_position) = the_msg.idata[0];
2878 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_scaled_axis_position) = the_msg.fdata[0];
2879 OBJ_MEMBER_FLOAT(fake_msg, HidMsg_offset_axis_position2) = the_msg.fdata[0];
2880
2881 // joystick hat specific member members
2882 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_hat_position) = the_msg.idata[0];
2883
2884 // keyboard specific members
2885 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_key) = the_msg.idata[1];
2886 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_ascii) = the_msg.idata[2];
2887
2888 // accelerometer (tilt sensor, wii remote) specific members
2889 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_x) = the_msg.idata[0];
2890 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_y) = the_msg.idata[1];
2891 OBJ_MEMBER_INT(fake_msg, HidMsg_offset_z) = the_msg.idata[2];
2892 }
2893 }
2894
2895 CK_DLL_MFUN( HidIn_send )
2896 {
2897 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2898 Chuck_Object * fake_msg = GET_NEXT_OBJECT(ARGS);
2899
2900 HidMsg the_msg;
2901 the_msg.device_type = OBJ_MEMBER_INT( fake_msg, HidMsg_offset_device_type );
2902 the_msg.device_num = OBJ_MEMBER_INT( fake_msg, HidMsg_offset_device_num );
2903 the_msg.type = OBJ_MEMBER_INT( fake_msg, HidMsg_offset_type );
2904 the_msg.eid = OBJ_MEMBER_INT( fake_msg, HidMsg_offset_which );
2905 the_msg.idata[0] = OBJ_MEMBER_INT( fake_msg, HidMsg_offset_idata );
2906
2907 RETURN->v_int = min->send( &the_msg );
2908 }
2909
2910 CK_DLL_MFUN( HidIn_can_wait )
2911 {
2912 HidIn * min = (HidIn *)OBJ_MEMBER_INT(SELF, HidIn_offset_data);
2913 RETURN->v_int = min->empty();
2914 }
2915
2916 CK_DLL_SFUN( HidIn_read_tilt_sensor )
2917 {
2918 static HidIn * hi;
2919 static t_CKBOOL hi_good = TRUE;
2920
2921 Chuck_ArrayInt * array = new Chuck_ArrayInt( FALSE, 3 );
2922 initialize_object( array, VM->env()->ckt_array, SHRED, VM ); // 1.5.0.0 (ge) added
2923 array->set( 0, 0 );
2924 array->set( 1, 0 );
2925 array->set( 2, 0 );
2926
2927 // TODO: reference count?
2928 array->add_ref();
2929 RETURN->v_object = array;
2930
2931 if( hi_good == FALSE )
2932 return;
2933
2934 if( !hi )
2935 {
2936 hi = new HidIn;
2937 if( !hi->open( SHRED->vm_ref, CK_HID_DEV_TILTSENSOR, 0 ) )
2938 {
2939 hi_good = FALSE;
2940 return;
2941 }
2942 }
2943
2944 HidMsg msg;
2945
2946 if( !hi->read( CK_HID_ACCELEROMETER, 0, &msg ) )
2947 {
2948 return;
2949 }
2950
2951 array->set( 0, msg.idata[0] );
2952 array->set( 1, msg.idata[1] );
2953 array->set( 2, msg.idata[2] );
2954 }
2955
2956 CK_DLL_SFUN( HidIn_ctrl_tiltPollRate )
2957 {
2958 // get srate
2959 if( !SHRED || !SHRED->vm_ref )
2960 {
2961 // problem
2962 CK_FPRINTF_STDERR( "[chuck](via HID): can't set tiltPollRate on NULL shred/VM...\n" );
2963 RETURN->v_dur = 0;
2964 return;
2965 }
2966 t_CKFLOAT srate = SHRED->vm_ref->srate();
2967
2968 // get arg
2969 t_CKDUR v = GET_NEXT_DUR( ARGS );
2970
2971 // get in microseconds
2972 t_CKINT usec = (t_CKINT)( v / srate * 1000000 );
2973
2974 // make sure it's nonnegative
2975 if( usec < 0 ) usec = 0;
2976
2977 // go
2978 RETURN->v_dur = TiltSensor_setPollRate( usec ) * srate / 1000000;
2979 }
2980
2981 CK_DLL_SFUN( HidIn_cget_tiltPollRate )
2982 {
2983 // get srate
2984 if( !SHRED || !SHRED->vm_ref )
2985 {
2986 // problem
2987 CK_FPRINTF_STDERR( "[chuck](via HID): can't get tiltPollRate on NULL shred/VM...\n" );
2988 RETURN->v_dur = 0;
2989 return;
2990 }
2991 t_CKFLOAT srate = SHRED->vm_ref->srate();
2992
2993 RETURN->v_dur = TiltSensor_getPollRate() * srate / 1000000;
2994 }
2995
2996 CK_DLL_SFUN( HidIn_start_cursor_track )
2997 {
2998 RETURN->v_int = !Mouse_start_cursor_track();
2999 }
3000
3001 CK_DLL_SFUN( HidIn_stop_cursor_track )
3002 {
3003 RETURN->v_int = !Mouse_stop_cursor_track();
3004 }
3005
3006 //-----------------------------------------------------------------------------
3007 // HidOut API
3008 //-----------------------------------------------------------------------------
3009 CK_DLL_CTOR( HidOut_ctor )
3010 {
3011 OBJ_MEMBER_INT(SELF, HidOut_offset_data) = (t_CKUINT)new HidOut;
3012 }
3013
3014 CK_DLL_DTOR( HidOut_dtor )
3015 {
3016 delete (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3017 OBJ_MEMBER_INT(SELF, HidOut_offset_data) = 0;
3018 }
3019
3020 CK_DLL_MFUN( HidOut_open )
3021 {
3022 HidOut * mout = (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3023 t_CKINT num = GET_CK_INT(ARGS);
3024 RETURN->v_int = mout->open( num );
3025 }
3026
3027 CK_DLL_MFUN( HidOut_good )
3028 {
3029 HidOut * mout = (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3030 RETURN->v_int = (t_CKINT)mout->good();
3031 }
3032
3033 CK_DLL_MFUN( HidOut_num )
3034 {
3035 HidOut * mout = (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3036 RETURN->v_int = mout->num();
3037 }
3038
3039 CK_DLL_MFUN( HidOut_name )
3040 {
3041 // HidOut * mout = (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3042 // TODO: memory leak, please fix, Thanks.
3043 Chuck_String * a = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
3044 // only if valid
3045 // if( mout->good() )
3046 // a->str = mout->mout->getPortName( mout->num() );
3047 RETURN->v_string = a;
3048 }
3049
3050 CK_DLL_MFUN( HidOut_printerr )
3051 {
3052 HidOut * mout = (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3053 t_CKINT print_or_not = GET_CK_INT(ARGS);
3054 mout->set_suppress( !print_or_not );
3055 }
3056
3057 CK_DLL_MFUN( HidOut_send )
3058 {
3059 HidOut * mout = (HidOut *)OBJ_MEMBER_INT(SELF, HidOut_offset_data);
3060 // Chuck_Object * fake_msg = GET_CK_OBJECT(ARGS);
3061 HidMsg the_msg;
3062 // the_msg.data[0] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, HidMsg_offset_data1);
3063 // the_msg.data[1] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, HidMsg_offset_data2);
3064 // the_msg.data[2] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, HidMsg_offset_data3);
3065 RETURN->v_int = mout->send( &the_msg );
3066 }
3067
3068 // Mouse constructor
3069 CK_DLL_CTOR( MouseCursor_ctor ) { }
3070 // Mouse desctructor
3071 CK_DLL_DTOR( MouseCursor_dtor ) { }
3072
3073 // get normalized mouse XY position, range [0,1]
3074 CK_DLL_SFUN( MouseCursor_scaled )
3075 {
3076 RETURN->v_vec2 = ck_get_mouse_xy_normalize();
3077 }
3078
3079 CK_DLL_SFUN( MouseCursor_abs )
3080 {
3081 RETURN->v_vec2 = ck_get_mouse_xy_absolute();
3082 }
3083
3084 CK_DLL_SFUN( MouseCursor_abs_x )
3085 {
3086 RETURN->v_float = ck_get_mouse_xy_absolute().x;
3087 }
3088
3089 CK_DLL_SFUN( MouseCursor_abs_y )
3090 {
3091 RETURN->v_float = ck_get_mouse_xy_absolute().y;
3092 }
3093
3094 // Mouse constructor
3095 CK_DLL_CTOR( Mousor_ctor ) { }
3096 // Mouse desctructor
3097 CK_DLL_DTOR( Mousor_dtor ) { }
3098
3099 #endif // __DISABLE_HID__
3100
3101
3102 #ifndef __DISABLE_MIDI__
3103 //-----------------------------------------------------------------------------
3104 // MidiRW API
3105 //-----------------------------------------------------------------------------
3106 CK_DLL_CTOR( MidiRW_ctor )
3107 {
3108 #ifndef __DISABLE_MIDI__
3109 OBJ_MEMBER_INT(SELF, MidiRW_offset_data) = (t_CKUINT)new MidiRW;
3110 #endif // __DISABLE_MIDI__
3111 }
3112
3113 CK_DLL_DTOR( MidiRW_dtor )
3114 {
3115 #ifndef __DISABLE_MIDI__
3116 delete (MidiRW *)OBJ_MEMBER_INT(SELF, MidiRW_offset_data);
3117 OBJ_MEMBER_INT(SELF, MidiRW_offset_data) = 0;
3118 #endif // __DISABLE_MIDI__
3119 }
3120
3121 CK_DLL_MFUN( MidiRW_open )
3122 {
3123 #ifndef __DISABLE_MIDI__
3124 MidiRW * mrw = (MidiRW *)OBJ_MEMBER_INT(SELF, MidiRW_offset_data);
3125 const char * filename = GET_CK_STRING(ARGS)->str().c_str();
3126 RETURN->v_int = mrw->open( filename );
3127 #endif // __DISABLE_MIDI__
3128 }
3129
3130 CK_DLL_MFUN( MidiRW_close )
3131 {
3132 #ifndef __DISABLE_MIDI__
3133 MidiRW * mrw = (MidiRW *)OBJ_MEMBER_INT(SELF, MidiRW_offset_data);
3134 RETURN->v_int = mrw->close();
3135 #endif // __DISABLE_MIDI__
3136 }
3137
3138 CK_DLL_MFUN( MidiRW_read )
3139 {
3140 #ifndef __DISABLE_MIDI__
3141 MidiRW * mrw = (MidiRW *)OBJ_MEMBER_INT(SELF, MidiRW_offset_data);
3142 Chuck_Object * fake_msg = GET_NEXT_OBJECT(ARGS);
3143 MidiMsg the_msg;
3144 t_CKTIME time = 0.0;
3145 RETURN->v_int = mrw->read( &the_msg, &time );
3146 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data1) = the_msg.data[0];
3147 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data2) = the_msg.data[1];
3148 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data3) = the_msg.data[2];
3149 OBJ_MEMBER_TIME(fake_msg, MidiMsg_offset_when) = time;
3150 #endif // __DISABLE_MIDI__
3151 }
3152
3153 CK_DLL_MFUN( MidiRW_write )
3154 {
3155 #ifndef __DISABLE_MIDI__
3156 MidiRW * mrw = (MidiRW *)OBJ_MEMBER_INT(SELF, MidiRW_offset_data);
3157 Chuck_Object * fake_msg = GET_NEXT_OBJECT(ARGS);
3158 t_CKTIME time = GET_NEXT_TIME(ARGS);
3159 MidiMsg the_msg;
3160 the_msg.data[0] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data1);
3161 the_msg.data[1] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data2);
3162 the_msg.data[2] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data3);
3163 RETURN->v_int = mrw->write( &the_msg, &time );
3164 #endif // __DISABLE_MIDI__
3165 }
3166
3167
3168 //-----------------------------------------------------------------------------
3169 // MidiMsgOut API
3170 //-----------------------------------------------------------------------------
3171 CK_DLL_CTOR( MidiMsgOut_ctor )
3172 {
3173 #ifndef __DISABLE_MIDI__
3174 OBJ_MEMBER_INT(SELF, MidiMsgOut_offset_data) = (t_CKUINT)new MidiMsgOut;
3175 #endif // __DISABLE_MIDI__
3176 }
3177
3178 CK_DLL_DTOR( MidiMsgOut_dtor )
3179 {
3180 #ifndef __DISABLE_MIDI__
3181 delete (MidiMsgOut *)OBJ_MEMBER_INT(SELF, MidiMsgOut_offset_data);
3182 OBJ_MEMBER_INT(SELF, MidiMsgOut_offset_data) = 0;
3183 #endif // __DISABLE_MIDI__
3184 }
3185
3186 CK_DLL_MFUN( MidiMsgOut_open )
3187 {
3188 #ifndef __DISABLE_MIDI__
3189 MidiMsgOut * mrw = (MidiMsgOut *)OBJ_MEMBER_INT(SELF, MidiMsgOut_offset_data);
3190 const char * filename = GET_CK_STRING(ARGS)->str().c_str();
3191 RETURN->v_int = mrw->open( filename );
3192 #endif // __DISABLE_MIDI__
3193 }
3194
3195 CK_DLL_MFUN( MidiMsgOut_close )
3196 {
3197 #ifndef __DISABLE_MIDI__
3198 MidiMsgOut * mrw = (MidiMsgOut *)OBJ_MEMBER_INT(SELF, MidiMsgOut_offset_data);
3199 RETURN->v_int = mrw->close();
3200 #endif // __DISABLE_MIDI__
3201 }
3202
3203 CK_DLL_MFUN( MidiMsgOut_write )
3204 {
3205 #ifndef __DISABLE_MIDI__
3206 MidiMsgOut * mrw = (MidiMsgOut *)OBJ_MEMBER_INT(SELF, MidiMsgOut_offset_data);
3207 Chuck_Object * fake_msg = GET_NEXT_OBJECT(ARGS);
3208 t_CKTIME time = GET_NEXT_TIME(ARGS);
3209 MidiMsg the_msg;
3210 the_msg.data[0] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data1);
3211 the_msg.data[1] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data2);
3212 the_msg.data[2] = (t_CKBYTE)OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data3);
3213 RETURN->v_int = mrw->write( &the_msg, &time );
3214 #endif // __DISABLE_MIDI__
3215 }
3216
3217
3218 //-----------------------------------------------------------------------------
3219 // MidiMsgIn API
3220 //-----------------------------------------------------------------------------
3221 CK_DLL_CTOR( MidiMsgIn_ctor )
3222 {
3223 #ifndef __DISABLE_MIDI__
3224 OBJ_MEMBER_INT(SELF, MidiMsgIn_offset_data) = (t_CKUINT)new MidiMsgIn;
3225 #endif // __DISABLE_MIDI__
3226 }
3227
3228 CK_DLL_DTOR( MidiMsgIn_dtor )
3229 {
3230 #ifndef __DISABLE_MIDI__
3231 delete (MidiMsgIn *)OBJ_MEMBER_INT(SELF, MidiMsgIn_offset_data);
3232 OBJ_MEMBER_INT(SELF, MidiMsgIn_offset_data) = 0;
3233 #endif // __DISABLE_MIDI__
3234 }
3235
3236 CK_DLL_MFUN( MidiMsgIn_open )
3237 {
3238 #ifndef __DISABLE_MIDI__
3239 MidiMsgIn * mrw = (MidiMsgIn *)OBJ_MEMBER_INT(SELF, MidiMsgIn_offset_data);
3240 const char * filename = GET_CK_STRING(ARGS)->str().c_str();
3241 RETURN->v_int = mrw->open( filename );
3242 #endif // __DISABLE_MIDI__
3243 }
3244
3245 CK_DLL_MFUN( MidiMsgIn_close )
3246 {
3247 #ifndef __DISABLE_MIDI__
3248 MidiMsgIn * mrw = (MidiMsgIn *)OBJ_MEMBER_INT(SELF, MidiMsgIn_offset_data);
3249 RETURN->v_int = mrw->close();
3250 #endif // __DISABLE_MIDI__
3251 }
3252
3253 CK_DLL_MFUN( MidiMsgIn_read )
3254 {
3255 #ifndef __DISABLE_MIDI__
3256 MidiMsgIn * mrw = (MidiMsgIn *)OBJ_MEMBER_INT(SELF, MidiMsgIn_offset_data);
3257 Chuck_Object * fake_msg = GET_NEXT_OBJECT(ARGS);
3258 MidiMsg the_msg;
3259 t_CKTIME time = 0.0;
3260 RETURN->v_int = mrw->read( &the_msg, &time );
3261 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data1) = the_msg.data[0];
3262 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data2) = the_msg.data[1];
3263 OBJ_MEMBER_INT(fake_msg, MidiMsg_offset_data3) = the_msg.data[2];
3264 OBJ_MEMBER_TIME(fake_msg, MidiMsg_offset_when) = time;
3265 #endif // __DISABLE_MIDI__
3266 }
3267 #endif // __DISABLE_MIDI__
3268
3269
3270
3271
3272 // constants
3273 const t_CKINT Chuck_IO::TYPE_ASCII = 0x1;
3274 const t_CKINT Chuck_IO::TYPE_BINARY = 0x2;
3275 const t_CKINT Chuck_IO::FLOAT32 = 0x10;
3276 const t_CKINT Chuck_IO::FLOAT64 = 0x20;
3277 const t_CKINT Chuck_IO::INT8 = 0x100;
3278 const t_CKINT Chuck_IO::INT16 = 0x200;
3279 const t_CKINT Chuck_IO::INT24 = 0x400;
3280 const t_CKINT Chuck_IO::INT32 = 0x800;
3281 const t_CKINT Chuck_IO::INT64 = 0x1000;
3282 const t_CKINT Chuck_IO::SINT8 = 0x2000;
3283 const t_CKINT Chuck_IO::SINT16 = 0x4000;
3284 const t_CKINT Chuck_IO::SINT24 = 0x8000;
3285 const t_CKINT Chuck_IO::SINT32 = 0x10000;
3286 const t_CKINT Chuck_IO::SINT64 = 0x20000;
3287 const t_CKINT Chuck_IO::UINT8 = 0x40000;
3288 const t_CKINT Chuck_IO::UINT16 = 0x80000;
3289 const t_CKINT Chuck_IO::UINT24 = 0x100000;
3290 const t_CKINT Chuck_IO::UINT32 = 0x200000;
3291 const t_CKINT Chuck_IO::UINT64 = 0x400000;
3292 const t_CKINT Chuck_IO::FLAG_READONLY = 0x100;
3293 const t_CKINT Chuck_IO::FLAG_WRITEONLY = 0x200;
3294 const t_CKINT Chuck_IO::FLAG_READ_WRITE = 0x400;
3295 const t_CKINT Chuck_IO::FLAG_APPEND = 0x800;
3296
3297 #ifndef __DISABLE_THREADS__
3298 const t_CKINT Chuck_IO::MODE_SYNC = 0;
3299 const t_CKINT Chuck_IO::MODE_ASYNC = 1;
3300 #else
3301 const t_CKINT Chuck_IO::MODE_SYNC = 1;
3302 const t_CKINT Chuck_IO::MODE_ASYNC = 0;
3303 #endif
3304
3305
3306
3307
3308 //-----------------------------------------------------------------------------
3309 // name: Chuck_IO Constructor
3310 // desc: Empty because you cannot construct a Chuck_IO object
3311 //-----------------------------------------------------------------------------
3312 Chuck_IO::Chuck_IO() : m_asyncEvent( NULL )
3313 {
3314 #ifndef __DISABLE_THREADS__
3315 m_thread = NULL;
3316 #endif
3317 }
3318
3319
3320
3321
3322 //-----------------------------------------------------------------------------
3323 // name: Chuck_IO Destructor
3324 // desc: Empty becuase you cannot destruct a Chuck_IO object
3325 //-----------------------------------------------------------------------------
3326 Chuck_IO::~Chuck_IO()
3327 { }
3328
3329
3330
3331
3332 //-----------------------------------------------------------------------------
3333 // name: Chuck_IO_File()
3334 // desc: constructor
3335 //-----------------------------------------------------------------------------
3336 Chuck_IO_File::Chuck_IO_File( Chuck_VM * vm )
3337 {
3338 m_vmRef = vm;
3339 // zero things out
3340 m_flags = 0;
3341 m_iomode = MODE_SYNC;
3342 m_path = "";
3343 m_dir = NULL;
3344 m_dir_start = 0;
3345 m_asyncEvent = new Chuck_Event;
3346 initialize_object( m_asyncEvent, vm->env()->ckt_event, NULL, vm );
3347 #ifndef __DISABLE_THREADS__
3348 m_thread = new XThread;
3349 #endif
3350
3351 // initialize prefix and extensions for auto | 1.5.1.5
3352 m_autoPrefix = "chuck-file";
3353 m_autoExtension = "txt";
3354 }
3355
3356
3357
3358
3359 //-----------------------------------------------------------------------------
3360 // name: ~Chuck_IO_File()
3361 // desc: destructor
3362 //-----------------------------------------------------------------------------
3363 Chuck_IO_File::~Chuck_IO_File()
3364 {
3365 // clean up
3366 this->close();
3367 delete m_asyncEvent;
3368 #ifndef __DISABLE_THREADS__
3369 delete m_thread;
3370 #endif
3371 }
3372
3373
3374
3375
3376 //-----------------------------------------------------------------------------
3377 // name: open
3378 // desc: open file from disk
3379 //-----------------------------------------------------------------------------
3380 t_CKBOOL Chuck_IO_File::open( const string & path, t_CKINT flags )
3381 {
3382 // the filename
3383 string theFilename = path;
3384
3385 // log
3386 EM_log( CK_LOG_INFO, "FileIO: opening file from disk..." );
3387 EM_log( CK_LOG_INFO, "FileIO: path: %s", theFilename.c_str() );
3388 EM_pushlog();
3389
3390 // if no flag specified, make it READ by default
3391 if( !(flags & FLAG_READ_WRITE) &&
3392 !(flags & FLAG_READONLY) &&
3393 !(flags & FLAG_WRITEONLY) &&
3394 !(flags & FLAG_APPEND) )
3395 {
3396 flags |= FLAG_READONLY;
3397 }
3398
3399 // if both read and write, enable read and write
3400 if( flags & FLAG_READONLY && flags & FLAG_WRITEONLY )
3401 {
3402 flags ^= FLAG_READONLY;
3403 flags ^= FLAG_WRITEONLY;
3404 flags |= FLAG_READ_WRITE;
3405 }
3406
3407 // set open flags
3408 ios_base::openmode theMode;
3409
3410 // check flags for errors
3411 if( (flags & TYPE_ASCII) &&
3412 (flags & TYPE_BINARY) )
3413 {
3414 EM_error3( "[chuck](via FileIO): cannot open file in both ASCII and binary mode" );
3415 goto error;
3416 }
3417
3418 if( (flags & FLAG_READ_WRITE) &&
3419 (flags & FLAG_READONLY) )
3420 {
3421 EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and READ" );
3422 goto error;
3423 }
3424
3425 if( (flags & FLAG_READ_WRITE) &&
3426 (flags & FLAG_WRITEONLY) )
3427 {
3428 EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and WRITE" );
3429 goto error;
3430 }
3431
3432 if( (flags & FLAG_READ_WRITE) &&
3433 (flags & FLAG_APPEND) )
3434 {
3435 EM_error3( "[chuck](via FileIO): conflicting flags: READ_WRITE and APPEND" );
3436 goto error;
3437 }
3438
3439 if( (flags & FLAG_WRITEONLY) &&
3440 (flags & FLAG_READONLY) )
3441 {
3442 EM_error3( "[chuck](via FileIO): conflicting flags: WRITE and READ" );
3443 goto error;
3444 }
3445
3446 if( (flags & FLAG_APPEND) &&
3447 (flags & FLAG_READONLY) )
3448 {
3449 EM_error3( "[chuck](via FileIO): conflicting flags: APPEND and FLAG_READ" );
3450 goto error;
3451 }
3452
3453 if( flags & FLAG_READ_WRITE )
3454 theMode = ios_base::in | ios_base::out;
3455 else if( flags & FLAG_READONLY )
3456 theMode = ios_base::in;
3457 else if( flags & FLAG_APPEND )
3458 theMode = ios_base::out | ios_base::app;
3459 else if( flags & FLAG_WRITEONLY )
3460 theMode = ios_base::out | ios_base::trunc;
3461
3462 if( flags & TYPE_BINARY )
3463 theMode |= ios_base::binary;
3464
3465 // close first
3466 if( m_io.is_open() )
3467 this->close();
3468
3469 // special
3470 if( strstr( theFilename.c_str(), "special:auto" ) )
3471 {
3472 // check output
3473 if( theMode & ios_base::out )
3474 {
3475 // generate auto name
3476 theFilename = autoFilename( this->m_autoPrefix, this->m_autoExtension );
3477 }
3478 else
3479 {
3480 EM_error3( "[chuck](via FileIO): \"special:auto\" can only be used for output" );
3481 goto error;
3482 }
3483 }
3484
3485 // try to open as a dir first (fixed 1.3.0.0 removed warning)
3486 m_dir = opendir( theFilename.c_str() );
3487 if( m_dir )
3488 {
3489 EM_poplog();
3490 return TRUE;
3491 }
3492
3493 // not a dir, create file if it does not exist unless flag is
3494 // readonly
3495 if( !(flags & FLAG_READONLY) )
3496 {
3497 m_io.open( theFilename.c_str(), ios_base::in );
3498 if( m_io.fail() )
3499 {
3500 m_io.clear();
3501 m_io.open( theFilename.c_str(), ios_base::out | ios_base::trunc );
3502 m_io.close();
3503 }
3504 else
3505 m_io.close();
3506 }
3507
3508 //open file
3509 m_io.open( theFilename.c_str(), theMode );
3510
3511 // seek to beginning if necessary
3512 if( flags & FLAG_READ_WRITE )
3513 {
3514 m_io.seekp( 0 );
3515 m_io.seekg( 0 );
3516 }
3517
3518 /* ATODO: Ge's code
3519 // windows sucks for being creative in the wrong places
3520 #ifdef __PLATFORM_WINDOWS__
3521 // if( flags ^ Chuck_IO::TRUNCATE && flags | Chuck_IO::READ ) nMode |= ios::nocreate;
3522 m_io.open( theFilename.c_str(), nMode );
3523 #else
3524 m_io.open( theFilename.c_str(), (_Ios_Openmode)nMode );
3525 #endif
3526 */
3527
3528 // check for error
3529 if( !(m_io.is_open()) )
3530 {
3531 // EM_error3( "[chuck](via FileIO): cannot open file: '%s'", theFilename.c_str() );
3532 goto error;
3533 }
3534
3535 // set path
3536 m_path = theFilename;
3537 // set flags
3538 m_flags = flags;
3539 if( !(flags & TYPE_BINARY) )
3540 m_flags |= Chuck_IO_File::TYPE_ASCII; // ASCII is default
3541 // set mode
3542 m_iomode = MODE_SYNC;
3543
3544 // pop
3545 EM_poplog();
3546
3547 return TRUE;
3548
3549 error:
3550
3551 // pop
3552 EM_poplog();
3553
3554 // reset
3555 m_path = "";
3556 m_flags = 0;
3557 m_iomode = MODE_SYNC;
3558 m_io.clear();
3559 m_io.close();
3560
3561 return FALSE;
3562 }
3563
3564
3565
3566
3567 //-----------------------------------------------------------------------------
3568 // name: close
3569 // desc: close file
3570 //-----------------------------------------------------------------------------
3571 void Chuck_IO_File::close()
3572 {
3573 // log
3574 EM_log( CK_LOG_INFO, "FileIO: closing file '%s'...", m_path.c_str() );
3575 // close it
3576 m_io.close();
3577 m_flags = 0;
3578 m_path = "";
3579 m_iomode = Chuck_IO::MODE_SYNC;
3580 if( m_dir ) {
3581 closedir( m_dir );
3582 m_dir = NULL;
3583 m_dir_start = 0;
3584 }
3585 }
3586
3587
3588
3589
3590 //-----------------------------------------------------------------------------
3591 // name: good()
3592 // desc: ...
3593 //-----------------------------------------------------------------------------
3594 t_CKBOOL Chuck_IO_File::good()
3595 {
3596 return m_dir || m_io.is_open();
3597 // return (!m_dir) && (!m_io.is_open());
3598 }
3599
3600
3601
3602
3603 //-----------------------------------------------------------------------------
3604 // name: flush()
3605 // desc: ...
3606 //-----------------------------------------------------------------------------
3607 void Chuck_IO_File::flush()
3608 {
3609 // sanity
3610 if( m_dir )
3611 {
3612 EM_error3( "[chuck](via FileIO): cannot flush on directory" );
3613 return;
3614 }
3615 m_io.flush();
3616 }
3617
3618
3619
3620
3621 //-----------------------------------------------------------------------------
3622 // name: mode()
3623 // desc: ...
3624 //-----------------------------------------------------------------------------
3625 t_CKINT Chuck_IO_File::mode()
3626 {
3627 // sanity
3628 if( m_dir )
3629 {
3630 EM_error3( "[chuck](via FileIO): cannot get mode on directory" );
3631 return -1;
3632 }
3633 return m_iomode;
3634 }
3635
3636
3637
3638
3639 //-----------------------------------------------------------------------------
3640 // name: mode( t_CKINT flag )
3641 // desc: ...
3642 //-----------------------------------------------------------------------------
3643 void Chuck_IO_File::mode( t_CKINT flag )
3644 {
3645 // sanity
3646 if( m_dir )
3647 {
3648 EM_error3( "[chuck](via FileIO): cannot set mode on directory" );
3649 return;
3650 }
3651 if( (flag != Chuck_IO::MODE_ASYNC) && (flag != Chuck_IO::MODE_SYNC) )
3652 {
3653 EM_error3( "[chuck](via FileIO): invalid mode flag" );
3654 return;
3655 }
3656
3657 m_iomode = flag;
3658 }
3659
3660
3661
3662
3663 //-----------------------------------------------------------------------------
3664 // name: size()
3665 // desc: Returns the size of the file in bytes, or -1 if no file is opened or
3666 // if a directory is opened.
3667 //-----------------------------------------------------------------------------
3668 t_CKINT Chuck_IO_File::size()
3669 {
3670 if( !(m_io.is_open()) ) return -1;
3671 if( m_dir )
3672 {
3673 EM_error3( "[chuck](via FileIO): cannot get size on a directory" );
3674 return -1;
3675 }
3676
3677 // no easy way to find file size in C++
3678 // have to seek to end, report position
3679 FILE * stream = fopen( m_path.c_str(), "r" );
3680 fseek( stream, 0L, SEEK_END );
3681 long endPos = ftell( stream );
3682 fclose( stream );
3683 return endPos;
3684 }
3685
3686
3687
3688
3689 //-----------------------------------------------------------------------------
3690 // name: seek( t_CKINT pos )
3691 // desc: Seeks to the specified byte offset in the file.
3692 //-----------------------------------------------------------------------------
3693 void Chuck_IO_File::seek( t_CKINT pos )
3694 {
3695 if( !(m_io.is_open()) )
3696 {
3697 EM_error3( "[chuck](via FileIO): cannot seek: no file is open" );
3698 return;
3699 }
3700 if( m_dir )
3701 {
3702 EM_error3( "[chuck](via FileIO): cannot seek on a directory" );
3703 return;
3704 }
3705 // 1.5.0.0 (ge) | added since seekg fails if EOF already reached
3706 m_io.clear();
3707 m_io.seekg( pos );
3708 m_io.seekp( pos );
3709 }
3710
3711
3712
3713
3714 //-----------------------------------------------------------------------------
3715 // name: tell()
3716 // desc: Returns the byte offset into the file, or -1 if no file is opened.
3717 //-----------------------------------------------------------------------------
3718 t_CKINT Chuck_IO_File::tell()
3719 {
3720 if( !(m_io.is_open()) )
3721 return -1;
3722 if( m_dir )
3723 {
3724 EM_error3( "[chuck](via FileIO): cannot tell on directory" );
3725 return -1;
3726 }
3727
3728 return m_io.tellg();
3729 }
3730
3731
3732
3733
3734 //-----------------------------------------------------------------------------
3735 // name: isDir()
3736 // desc: ...
3737 //-----------------------------------------------------------------------------
3738 t_CKINT Chuck_IO_File::isDir()
3739 {
3740 return m_dir != NULL;
3741 }
3742
3743
3744
3745
3746 //-----------------------------------------------------------------------------
3747 // name: dirList()
3748 // desc: ...
3749 //-----------------------------------------------------------------------------
3750 Chuck_ArrayInt * Chuck_IO_File::dirList()
3751 {
3752 // sanity
3753 if( !m_dir )
3754 {
3755 EM_error3( "[chuck](via FileIO): cannot get list: no directory open" );
3756 Chuck_ArrayInt * ret = new Chuck_ArrayInt( TRUE, 0 );
3757 initialize_object( ret, m_vmRef->env()->ckt_array, NULL, m_vmRef );
3758 return ret;
3759 }
3760
3761 // fill vector with entry names
3762 rewinddir( m_dir );
3763 std::vector<Chuck_String *> entrylist;
3764
3765 // 1.5.0.0 (ge) | #chunreal UE-forced refactor
3766 struct dirent * ent = readdir( m_dir );
3767 // was: while( (ent = readdir( m_dir ) ) <-- in ge's opinion this is cleaner splitting readir() into two places
3768 while( ent != NULL ) // fixed 1.3.0.0: removed warning
3769 {
3770 // pass NULL as shred ref
3771 Chuck_String * s = (Chuck_String *)instantiate_and_initialize_object( m_vmRef->env()->ckt_string, NULL, m_vmRef );
3772 s->set( std::string( ent->d_name ) );
3773 if( s->str() != ".." && s->str() != "." )
3774 {
3775 // don't include .. and . in the list
3776 entrylist.push_back( s );
3777 }
3778 // #chunreal refactor
3779 ent = readdir( m_dir );
3780 }
3781
3782 // make array
3783 Chuck_ArrayInt * array = new Chuck_ArrayInt( true, entrylist.size() );
3784 initialize_object( array, m_vmRef->env()->ckt_array, NULL, m_vmRef );
3785 for( int i = 0; i < entrylist.size(); i++ )
3786 array->set( i, (t_CKUINT)entrylist[i] );
3787 return array;
3788 }
3789
3790
3791
3792
3793 //-----------------------------------------------------------------------------
3794 // name: read( t_CKINT length )
3795 // desc: ...
3796 //-----------------------------------------------------------------------------
3797 /*Chuck_String * Chuck_IO_File::read( t_CKINT length )
3798 {
3799 // sanity
3800 if (!(m_io.is_open())) {
3801 EM_error3( "[chuck](via FileIO): cannot read: no file open" );
3802 return new Chuck_String( "" );
3803 }
3804
3805 if (m_io.fail()) {
3806 EM_error3( "[chuck](via FileIO): cannot read: I/O stream failed" );
3807 return new Chuck_String( "" );
3808 }
3809
3810 if ( m_dir )
3811 {
3812 EM_error3( "[chuck](via FileIO): cannot read on a directory" );
3813 return new Chuck_String( "" );
3814 }
3815
3816 char buf[length+1];
3817 m_io.read( buf, length );
3818 buf[m_io.gcount()] = '\0';
3819 string s( buf );
3820 return new Chuck_String( s );
3821 }*/
3822
3823
3824
3825
3826 //-----------------------------------------------------------------------------
3827 // name: readLine()
3828 // desc: read line
3829 //-----------------------------------------------------------------------------
3830 Chuck_String * Chuck_IO_File::readLine()
3831 {
3832 // sanity
3833 if( !(m_io.is_open()) ) {
3834 EM_error3( "[chuck](via FileIO): cannot readLine: no file open" );
3835 return new Chuck_String( "" );
3836 }
3837
3838 if( m_io.fail() ) {
3839 EM_error3( "[chuck](via FileIO): cannot readLine: I/O stream failed" );
3840 return new Chuck_String( "" );
3841 }
3842
3843 if( m_dir )
3844 {
3845 EM_error3( "[chuck](via FileIO): cannot readLine on directory" );
3846 return new Chuck_String( "" );
3847 }
3848
3849 string s;
3850 getline( m_io, s );
3851
3852 // chuck str
3853 Chuck_String * str = new Chuck_String( s );
3854 // initialize | 1.5.0.0 (ge) | added initialize_object
3855 initialize_object( str, m_vmRef->env()->ckt_string, NULL, m_vmRef );
3856
3857 // note this chuck string still needs to be initialized
3858 return str;
3859 }
3860
3861
3862
3863
3864 //-----------------------------------------------------------------------------
3865 // name: readInt( t_CKINT flags )
3866 // desc: ...
3867 //-----------------------------------------------------------------------------
3868 t_CKINT Chuck_IO_File::readInt( t_CKINT flags )
3869 {
3870 // sanity
3871 if( !(m_io.is_open()) ) {
3872 EM_error3( "[chuck](via FileIO): cannot readInt: no file open" );
3873 return 0;
3874 }
3875
3876 if( m_io.eof() ) {
3877 EM_error3( "[chuck](via FileIO): cannot readInt: EOF reached" );
3878 return 0;
3879 }
3880
3881 if( m_dir )
3882 {
3883 EM_error3( "[chuck](via FileIO): cannot read on directory" );
3884 return 0;
3885 }
3886
3887 if( m_io.fail() ) {
3888 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3889 return 0;
3890 }
3891
3892 // check mode ASCII vs. binary
3893 if( m_flags & TYPE_ASCII )
3894 {
3895 // ASCII
3896 t_CKINT val = 0;
3897 m_io >> val;
3898 // if (m_io.fail())
3899 // EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3900 return val;
3901 }
3902 else if( m_flags & TYPE_BINARY )
3903 {
3904 // binary
3905 if( flags & Chuck_IO::INT8 || flags & Chuck_IO::UINT8 ) // 1.5.0.1 (ge) added UINT
3906 {
3907 // unsigned 8-bit
3908 uint8_t i; // 1.5.0.1 (ge) changed to int8_t from unsigned char
3909 m_io.read( (char *)&i, 1 );
3910 if( m_io.gcount() != 1 )
3911 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3912 else if( m_io.fail() )
3913 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3914 return (t_CKINT)i;
3915 }
3916 else if( flags & Chuck_IO::INT16 || flags & Chuck_IO::UINT16 ) // 1.5.0.1 (ge) added UINT
3917 {
3918 // unsigned 16-bit
3919 uint16_t i; // 1.5.0.1 (ge) changed to uint16_t from t_CKINT
3920 m_io.read( (char *)&i, 2 );
3921 if( m_io.gcount() != 2 )
3922 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3923 else if( m_io.fail() )
3924 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3925 return (t_CKINT)i;
3926 }
3927 else if( flags & Chuck_IO::INT32 || flags & Chuck_IO::UINT32 ) // 1.5.0.1 (ge) added UINT
3928 {
3929 // unsigned 32-bit
3930 uint32_t i; // 1.5.0.1 (ge) changed to uint32_t from t_CKINT
3931 m_io.read( (char *)&i, 4 );
3932 if( m_io.gcount() != 4 )
3933 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3934 else if( m_io.fail() )
3935 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3936 return (t_CKINT)i;
3937 }
3938 else if( flags & Chuck_IO::INT64 || flags & Chuck_IO::UINT64 ) // 1.5.0.1 (ge) added 64-bit
3939 {
3940 // unsigned 64-bit
3941 uint64_t i; // 1.5.0.1 (ge) added
3942 m_io.read( (char *)&i, 8 );
3943 if( m_io.gcount() != 8 )
3944 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3945 else if( m_io.fail() )
3946 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3947 return (t_CKINT)i; // TODO: handle signed vs unsigned
3948 }
3949 else if( flags & Chuck_IO::SINT8 ) // 1.5.0.1 (ge) added SINT
3950 {
3951 // signed 8-bit
3952 int8_t i;
3953 m_io.read( (char *)&i, 1 );
3954 if( m_io.gcount() != 1 )
3955 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3956 else if( m_io.fail() )
3957 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3958 return (t_CKINT)i;
3959 }
3960 else if( flags & Chuck_IO::SINT16 ) // 1.5.0.1 (ge) added SINT
3961 {
3962 // signed 16-bit
3963 int16_t i;
3964 m_io.read( (char *)&i, 2 );
3965 if( m_io.gcount() != 2 )
3966 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3967 else if( m_io.fail() )
3968 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3969 return (t_CKINT)i;
3970 }
3971 else if( flags & Chuck_IO::SINT32 ) // 1.5.0.1 (ge) added SINT
3972 {
3973 // signed 32-bit
3974 int32_t i;
3975 m_io.read( (char *)&i, 4 );
3976 if( m_io.gcount() != 4 )
3977 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3978 else if( m_io.fail() )
3979 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3980 return (t_CKINT)i;
3981 }
3982 else if( flags & Chuck_IO::SINT64 ) // 1.5.0.1 (ge) added SINT
3983 {
3984 // signed 64-bit
3985 int64_t i;
3986 m_io.read( (char *)&i, 4 );
3987 if( m_io.gcount() != 8 )
3988 EM_error3( "[chuck](via FileIO): cannot readInt: not enough bytes left" );
3989 else if( m_io.fail() )
3990 EM_error3( "[chuck](via FileIO): cannot readInt: I/O stream failed" );
3991 return (t_CKINT)i;
3992 }
3993 else
3994 {
3995 EM_error3( "[chuck](via FileIO): readInt error: invalid/unsupported int size flag" );
3996 return 0;
3997 }
3998 }
3999 else
4000 {
4001 EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" );
4002 return 0;
4003 }
4004 }
4005
4006
4007
4008
4009 //-----------------------------------------------------------------------------
4010 // name: readFloat()
4011 // desc: read next as floating point value; could be ASCII or BINARY
4012 //-----------------------------------------------------------------------------
4013 t_CKFLOAT Chuck_IO_File::readFloat()
4014 {
4015 return this->readFloat( Chuck_IO::FLOAT32 );
4016 }
4017
4018
4019
4020
4021 //-----------------------------------------------------------------------------
4022 // name: readFloat()
4023 // desc: read next as floating point value; could be ASCII or BINARY
4024 //-----------------------------------------------------------------------------
4025 t_CKFLOAT Chuck_IO_File::readFloat( t_CKINT flags )
4026 {
4027 // sanity
4028 if( !(m_io.is_open()) ) {
4029 EM_error3( "[chuck](via FileIO): cannot readFloat: no file open" );
4030 return 0;
4031 }
4032 if( m_io.eof() ) {
4033 EM_error3( "[chuck](via FileIO): cannot readFloat: EOF reached" );
4034 return 0;
4035 }
4036 if( m_io.fail() ) {
4037 EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" );
4038 return 0;
4039 }
4040 if( m_dir ) {
4041 EM_error3( "[chuck](via FileIO): cannot read a directory" );
4042 return 0;
4043 }
4044
4045 // check mode
4046 if( m_flags & TYPE_ASCII )
4047 {
4048 // ASCII
4049 t_CKFLOAT val = 0;
4050 m_io >> val;
4051 // if (m_io.fail())
4052 // EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" );
4053 return val;
4054
4055 }
4056 else if( m_flags & TYPE_BINARY )
4057 {
4058 // 1.5.0.1
4059 if( flags & Chuck_IO::FLOAT32 )
4060 {
4061 float i;
4062 m_io.read( (char *)&i, sizeof( float ) );
4063 if( m_io.gcount() != sizeof( float ) )
4064 EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" );
4065 else if( m_io.fail() )
4066 EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" );
4067 return (t_CKFLOAT)i;
4068 }
4069 else if( flags & Chuck_IO::FLOAT64 )
4070 {
4071 double i;
4072 m_io.read( (char *)&i, sizeof( double ) );
4073 if( m_io.gcount() != sizeof( double ) )
4074 EM_error3( "[chuck](via FileIO): cannot readFloat: not enough bytes left" );
4075 else if( m_io.fail() )
4076 EM_error3( "[chuck](via FileIO): cannot readFloat: I/O stream failed" );
4077 return (t_CKFLOAT)i;
4078 }
4079 else
4080 {
4081 EM_error3( "[chuck](via FileIO): readFloat error: invalid/unsupported datatype size flag" );
4082 return 0;
4083 }
4084 }
4085 else
4086 {
4087 EM_error3( "[chuck](via FileIO): readFloat error: invalid ASCII/binary flag" );
4088 return 0;
4089 }
4090 }
4091
4092
4093
4094
4095 //-----------------------------------------------------------------------------
4096 // name: readString()
4097 // desc: ...
4098 //-----------------------------------------------------------------------------
4099 t_CKBOOL Chuck_IO_File::readString( std::string & str )
4100 {
4101 // set
4102 str = "";
4103
4104 // sanity
4105 if( !(m_io.is_open()) ) {
4106 EM_error3( "[chuck](via FileIO): cannot readString: no file open" );
4107 return FALSE;
4108 }
4109
4110 if( m_io.eof() ) {
4111 EM_error3( "[chuck](via FileIO): cannot readString: EOF reached" );
4112 return FALSE;
4113 }
4114
4115 if( m_dir )
4116 {
4117 EM_error3( "[chuck](via FileIO): cannot read on directory" );
4118 return FALSE;
4119 }
4120
4121 if( m_io.fail() ) {
4122 EM_error3( "[chuck](via FileIO): cannot readString: I/O stream failed" );
4123 return FALSE;
4124 }
4125
4126 if( m_flags & TYPE_ASCII ) {
4127 // ASCII
4128 m_io >> str;
4129 return TRUE;
4130 }
4131 else if( m_flags & TYPE_BINARY ) {
4132 EM_error3( "[chuck](via FileIO): readString not supported for binary mode" );
4133 return FALSE;
4134 }
4135 else {
4136 EM_error3( "[chuck](via FileIO): readInt error: invalid ASCII/binary flag" );
4137 return FALSE;
4138 }
4139 }
4140
4141
4142
4143
4144 //-----------------------------------------------------------------------------
4145 /* (ATODO: doesn't look like asynchronous reads will work)
4146 //-----------------------------------------------------------------------------
4147 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::read_thread ) (void * data)
4148 {
4149 // (ATODO: test this)
4150 cerr << "In thread" << endl;
4151 async_args * args = (async_args *)data;
4152 Chuck_String * ret = (Chuck_String *)(args->RETURN);
4153 ret = args->fileio_obj->read( args->intArg );
4154 cerr << "Called FileIO.read(int)" << endl;
4155 args->fileio_obj->m_asyncEvent->broadcast(); // wake up
4156 cerr << "Broadcasted on event" << endl;
4157 cerr << "Sleeping" << endl;
4158 sleep( 5 );
4159 cerr << "Woke up" << endl;
4160 delete args;
4161 cerr << "Deleted args" << endl;
4162
4163 return (THREAD_RETURN)0;
4164 }
4165
4166 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::readLine_thread ) (void * data)
4167 {
4168 // not yet implemented
4169 return NULL;
4170 }
4171
4172 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::readInt_thread ) (void * data)
4173 {
4174 // (ATODO: test this)
4175 async_args * args = (async_args *)data;
4176 Chuck_DL_Return * ret = (Chuck_DL_Return *)(args->RETURN);
4177 ret->v_int = args->fileio_obj->readInt( args->intArg );
4178 cerr << "Called readInt, set ret->v_int to " << ret->v_int << endl;
4179 args->fileio_obj->m_asyncEvent->broadcast(); // wake up
4180 cerr << "Called broadcast" << endl;
4181 delete args;
4182
4183 return (THREAD_RETURN)0;
4184 }
4185
4186 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::readFloat_thread ) (void * data)
4187 {
4188 // not yet implemented
4189 return NULL;
4190 }
4191 */
4192 //-----------------------------------------------------------------------------
4193
4194
4195
4196
4197 //-----------------------------------------------------------------------------
4198 // name: eof()
4199 // desc: end of file?
4200 //-----------------------------------------------------------------------------
4201 t_CKBOOL Chuck_IO_File::eof()
4202 {
4203 if( !m_io.is_open() )
4204 {
4205 // EM_error3( "[chuck](via FileIO): cannot check eof: no file open" );
4206 return TRUE;
4207 }
4208 if( m_dir )
4209 {
4210 EM_error3( "[chuck](via FileIO): cannot check eof on directory" );
4211 return TRUE;
4212 }
4213
4214 // return EOF conditions (1.5.0.0: peek() was added since eof() is set
4215 // ONLY AFTER an effort is made to read and no more data is left)
4216 // https://stackoverflow.com/questions/4533063/how-does-ifstreams-eof-work
4217 return m_io.eof() || m_io.fail() || m_io.peek() == EOF;
4218 }
4219
4220
4221
4222
4223 //-----------------------------------------------------------------------------
4224 // name: write( const std::string & val )
4225 // desc: ...
4226 //-----------------------------------------------------------------------------
4227 void Chuck_IO_File::write( const std::string & val )
4228 {
4229 // sanity
4230 if( !(m_io.is_open()) ) {
4231 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4232 return;
4233 }
4234
4235 if( m_io.fail() ) {
4236 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4237 return;
4238 }
4239
4240 if( m_dir )
4241 {
4242 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4243 return;
4244 }
4245
4246 m_io.write( val.c_str(), val.size() );
4247
4248 if( m_io.fail() ) { // check both before and after write if stream is ok
4249 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4250 }
4251 }
4252
4253
4254
4255
4256 //-----------------------------------------------------------------------------
4257 // name: write( t_CKINT val )
4258 // desc: write an integer value to file
4259 //-----------------------------------------------------------------------------
4260 void Chuck_IO_File::write( t_CKINT val )
4261 {
4262 // 0 flags means write (if binary will write in native int size)
4263 this->write( val, 0 );
4264
4265 // // sanity
4266 // if( !(m_io.is_open()) ) {
4267 // EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4268 // return;
4269 // }
4270 //
4271 // if( m_io.fail() ) {
4272 // EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4273 // return;
4274 // }
4275 //
4276 // if( m_dir )
4277 // {
4278 // EM_error3( "[chuck](via FileIO): cannot write on directory" );
4279 // return;
4280 // }
4281 //
4282 // if( m_flags & TYPE_ASCII ) {
4283 // m_io << val;
4284 // }
4285 // else if( m_flags & TYPE_BINARY ) {
4286 // m_io.write( (char *)&val, sizeof( t_CKINT ) );
4287 // }
4288 // else {
4289 // EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4290 // }
4291 //
4292 // if( m_io.fail() ) { // check both before and after write if stream is ok
4293 // EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4294 // }
4295 }
4296
4297
4298
4299
4300 //-----------------------------------------------------------------------------
4301 // name: write( t_CKINT val )
4302 // desc: ...
4303 //-----------------------------------------------------------------------------
4304 void Chuck_IO_File::write( t_CKINT val, t_CKINT flags )
4305 {
4306 // sanity
4307 if( !(m_io.is_open()) ) {
4308 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4309 return;
4310 }
4311
4312 if( m_io.fail() ) {
4313 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4314 return;
4315 }
4316
4317 if( m_dir )
4318 {
4319 EM_error3( "[chuck](via FileIO): cannot write on directory" );
4320 return;
4321 }
4322
4323 // check file mode
4324 if( m_flags & TYPE_ASCII ) // ASCII mode
4325 {
4326 // insert into file stream
4327 m_io << val;
4328 }
4329 else if( m_flags & TYPE_BINARY ) // BINARY mode
4330 {
4331 // check datatype size flags
4332 // 1.5.0.1 (ge) modified to new signed/unsigned handling
4333 if( flags & Chuck_IO::INT8 || flags & Chuck_IO::UINT8 )
4334 {
4335 // unsigned 8-bit
4336 uint8_t v = val;
4337 m_io.write( (char *)&v, 1 );
4338 }
4339 else if( flags & Chuck_IO::INT16 || flags & Chuck_IO::UINT16 )
4340 {
4341 // unsigned 16-bit
4342 uint16_t v = val;
4343 m_io.write( (char *)&v, 2 );
4344 }
4345 else if( flags & Chuck_IO::INT32 || flags & Chuck_IO::UINT32 )
4346 {
4347 // unsigned 32-bit
4348 uint32_t v = (uint32_t)val;
4349 m_io.write( (char *)&v, 4 );
4350 }
4351 else if( flags & Chuck_IO::INT64 || flags & Chuck_IO::UINT64 )
4352 {
4353 // unsigned 64-bit
4354 uint64_t v = val;
4355 m_io.write( (char *)&v, 8 );
4356 }
4357 else if( flags & Chuck_IO::SINT8 )
4358 {
4359 // signed 8-bit
4360 int8_t v = val;
4361 m_io.write( (char *)&v, 1 );
4362 }
4363 else if( flags & Chuck_IO::SINT16 )
4364 {
4365 // signed 16-bit
4366 int16_t v = val;
4367 m_io.write( (char *)&v, 2 );
4368 }
4369 else if( flags & Chuck_IO::SINT32 )
4370 {
4371 // signed 32-bit
4372 int32_t v = (uint32_t)val;
4373 m_io.write( (char *)&v, 4 );
4374 }
4375 else if( flags & Chuck_IO::SINT64 )
4376 {
4377 // signed 32-bit
4378 int64_t v = val;
4379 m_io.write( (char *)&v, 8 );
4380 }
4381 else
4382 {
4383 // write to native int size | 1.5.1.0
4384 m_io.write( (char *)&val, sizeof(t_CKINT) );
4385 }
4386 }
4387 else {
4388 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4389 }
4390
4391 if( m_io.fail() ) { // check both before and after write if stream is ok
4392 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4393 }
4394 }
4395
4396
4397
4398
4399 //-----------------------------------------------------------------------------
4400 // name: write( t_CKFLOAT val )
4401 // desc: write a floating point value
4402 //-----------------------------------------------------------------------------
4403 void Chuck_IO_File::write( t_CKFLOAT val )
4404 {
4405 this->write( val, Chuck_IO::FLOAT32 );
4406 }
4407
4408
4409
4410
4411 //-----------------------------------------------------------------------------
4412 // name: write( t_CKFLOAT val, t_CKINT flags )
4413 // desc: write a floating point value; binary mode will heed flags for size
4414 //-----------------------------------------------------------------------------
4415 void Chuck_IO_File::write( t_CKFLOAT val, t_CKINT flags )
4416 {
4417 // sanity
4418 if( !(m_io.is_open()) ) {
4419 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4420 return;
4421 }
4422 if( m_io.fail() ) {
4423 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4424 return;
4425 }
4426 if( m_dir ) {
4427 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4428 return;
4429 }
4430
4431 // check ASCII or BINARY
4432 if( m_flags & TYPE_ASCII )
4433 {
4434 // insert into stream
4435 m_io << val;
4436 }
4437 else if( m_flags & TYPE_BINARY )
4438 {
4439 // 1.5.0.1 (ge) add distinction between different float sizes
4440 if( flags & Chuck_IO::FLOAT32 )
4441 {
4442 // 32-bit
4443 float v = (float)val;
4444 m_io.write( (char *)&v, 4 );
4445 }
4446 else if( flags & Chuck_IO::FLOAT64 )
4447 {
4448 // 64-bit
4449 double v = (double)val;
4450 m_io.write( (char *)&v, 8 );
4451 }
4452 else
4453 {
4454 EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" );
4455 }
4456 }
4457 else
4458 {
4459 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4460 }
4461
4462 if( m_io.fail() ) { // check both before and after write if stream is ok
4463 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4464 }
4465 }
4466
4467 void Chuck_IO_File::write( const t_CKCOMPLEX & val )
4468 {
4469 this->write( val, Chuck_IO::FLOAT32 );
4470 }
4471
4472 void Chuck_IO_File::write( const t_CKCOMPLEX & val, t_CKINT flags )
4473 {
4474 // sanity
4475 if( !(m_io.is_open()) ) {
4476 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4477 return;
4478 }
4479 if( m_io.fail() ) {
4480 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4481 return;
4482 }
4483 if( m_dir ) {
4484 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4485 return;
4486 }
4487
4488 // check ASCII or BINARY
4489 if( m_flags & TYPE_ASCII )
4490 {
4491 // insert into stream
4492 m_io << "#(" << val.re << "," << val.im << ")";
4493 }
4494 else if( m_flags & TYPE_BINARY )
4495 {
4496 // 1.5.0.1 (ge) add distinction between different float sizes
4497 if( flags & Chuck_IO::FLOAT32 )
4498 {
4499 // 32-bit
4500 float v = (float)val.re; m_io.write( (char *)&v, 4 );
4501 v = (float)val.im; m_io.write( (char *)&v, 4 );
4502 }
4503 else if( flags & Chuck_IO::FLOAT64 )
4504 {
4505 // 64-bit
4506 double v = (double)val.re; m_io.write( (char *)&v, 8 );
4507 v = (double)val.im; m_io.write( (char *)&v, 8 );
4508 }
4509 else
4510 {
4511 EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" );
4512 }
4513 }
4514 else
4515 {
4516 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4517 }
4518
4519 if( m_io.fail() ) { // check both before and after write if stream is ok
4520 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4521 }
4522 }
4523
4524 void Chuck_IO_File::write( const t_CKPOLAR & val )
4525 {
4526 this->write( val, Chuck_IO::FLOAT32 );
4527 }
4528
4529 void Chuck_IO_File::write( const t_CKPOLAR & val, t_CKINT flags )
4530 {
4531 // sanity
4532 if( !(m_io.is_open()) ) {
4533 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4534 return;
4535 }
4536 if( m_io.fail() ) {
4537 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4538 return;
4539 }
4540 if( m_dir ) {
4541 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4542 return;
4543 }
4544
4545 // check ASCII or BINARY
4546 if( m_flags & TYPE_ASCII )
4547 {
4548 // insert into stream
4549 m_io << "%(" << val.modulus << "," << val.phase << ")";
4550 }
4551 else if( m_flags & TYPE_BINARY )
4552 {
4553 // 1.5.0.1 (ge) add distinction between different float sizes
4554 if( flags & Chuck_IO::FLOAT32 )
4555 {
4556 // 32-bit
4557 float v = (float)val.modulus; m_io.write( (char *)&v, 4 );
4558 v = (float)val.phase; m_io.write( (char *)&v, 4 );
4559 }
4560 else if( flags & Chuck_IO::FLOAT64 )
4561 {
4562 // 64-bit
4563 double v = (double)val.modulus; m_io.write( (char *)&v, 8 );
4564 v = (double)val.phase; m_io.write( (char *)&v, 8 );
4565 }
4566 else
4567 {
4568 EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" );
4569 }
4570 }
4571 else
4572 {
4573 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4574 }
4575
4576 if( m_io.fail() ) { // check both before and after write if stream is ok
4577 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4578 }
4579 }
4580
4581 void Chuck_IO_File::write( const t_CKVEC2 & val )
4582 {
4583 this->write( val, Chuck_IO::FLOAT32 );
4584 }
4585
4586 void Chuck_IO_File::write( const t_CKVEC2 & val, t_CKINT flags )
4587 {
4588 // sanity
4589 if( !(m_io.is_open()) ) {
4590 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4591 return;
4592 }
4593 if( m_io.fail() ) {
4594 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4595 return;
4596 }
4597 if( m_dir ) {
4598 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4599 return;
4600 }
4601
4602 // check ASCII or BINARY
4603 if( m_flags & TYPE_ASCII )
4604 {
4605 // insert into stream
4606 m_io << "@(" << val.x << "," << val.y << ")";
4607 }
4608 else if( m_flags & TYPE_BINARY )
4609 {
4610 // 1.5.0.1 (ge) add distinction between different float sizes
4611 if( flags & Chuck_IO::FLOAT32 )
4612 {
4613 // 32-bit
4614 float v = (float)val.x; m_io.write( (char *)&v, 4 );
4615 v = (float)val.y; m_io.write( (char *)&v, 4 );
4616 }
4617 else if( flags & Chuck_IO::FLOAT64 )
4618 {
4619 // 64-bit
4620 double v = (double)val.x; m_io.write( (char *)&v, 8 );
4621 v = (double)val.y; m_io.write( (char *)&v, 8 );
4622 }
4623 else
4624 {
4625 EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" );
4626 }
4627 }
4628 else
4629 {
4630 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4631 }
4632
4633 if( m_io.fail() ) { // check both before and after write if stream is ok
4634 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4635 }
4636 }
4637
4638 void Chuck_IO_File::write( const t_CKVEC3 & val )
4639 {
4640 this->write( val, Chuck_IO::FLOAT32 );
4641 }
4642
4643 void Chuck_IO_File::write( const t_CKVEC3 & val, t_CKINT flags )
4644 {
4645 // sanity
4646 if( !(m_io.is_open()) ) {
4647 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4648 return;
4649 }
4650 if( m_io.fail() ) {
4651 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4652 return;
4653 }
4654 if( m_dir ) {
4655 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4656 return;
4657 }
4658
4659 // check ASCII or BINARY
4660 if( m_flags & TYPE_ASCII )
4661 {
4662 // insert into stream
4663 m_io << "@(" << val.x << "," << val.y << "," << val.z << ")";
4664 }
4665 else if( m_flags & TYPE_BINARY )
4666 {
4667 // 1.5.0.1 (ge) add distinction between different float sizes
4668 if( flags & Chuck_IO::FLOAT32 )
4669 {
4670 // 32-bit
4671 float v = (float)val.x; m_io.write( (char *)&v, 4 );
4672 v = (float)val.y; m_io.write( (char *)&v, 4 );
4673 v = (float)val.z; m_io.write( (char *)&v, 4 );
4674 }
4675 else if( flags & Chuck_IO::FLOAT64 )
4676 {
4677 // 64-bit
4678 double v = (double)val.x; m_io.write( (char *)&v, 8 );
4679 v = (double)val.y; m_io.write( (char *)&v, 8 );
4680 v = (double)val.z; m_io.write( (char *)&v, 8 );
4681 }
4682 else
4683 {
4684 EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" );
4685 }
4686 }
4687 else
4688 {
4689 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4690 }
4691
4692 if( m_io.fail() ) { // check both before and after write if stream is ok
4693 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4694 }
4695 }
4696
4697 void Chuck_IO_File::write( const t_CKVEC4 & val )
4698 {
4699 this->write( val, Chuck_IO::FLOAT32 );
4700 }
4701
4702 void Chuck_IO_File::write( const t_CKVEC4 & val, t_CKINT flags )
4703 {
4704 // sanity
4705 if( !(m_io.is_open()) ) {
4706 EM_error3( "[chuck](via FileIO): cannot write: no file open" );
4707 return;
4708 }
4709 if( m_io.fail() ) {
4710 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4711 return;
4712 }
4713 if( m_dir ) {
4714 EM_error3( "[chuck](via FileIO): cannot write to a directory" );
4715 return;
4716 }
4717
4718 // check ASCII or BINARY
4719 if( m_flags & TYPE_ASCII )
4720 {
4721 // insert into stream
4722 m_io << "@(" << val.x << "," << val.y << "," << val.z << "," << val.w << ")";
4723 }
4724 else if( m_flags & TYPE_BINARY )
4725 {
4726 // 1.5.0.1 (ge) add distinction between different float sizes
4727 if( flags & Chuck_IO::FLOAT32 )
4728 {
4729 // 32-bit
4730 float v = (float)val.x; m_io.write( (char *)&v, 4 );
4731 v = (float)val.y; m_io.write( (char *)&v, 4 );
4732 v = (float)val.z; m_io.write( (char *)&v, 4 );
4733 v = (float)val.w; m_io.write( (char *)&v, 4 );
4734 }
4735 else if( flags & Chuck_IO::FLOAT64 )
4736 {
4737 // 64-bit
4738 double v = (double)val.x; m_io.write( (char *)&v, 8 );
4739 v = (double)val.y; m_io.write( (char *)&v, 8 );
4740 v = (double)val.z; m_io.write( (char *)&v, 8 );
4741 v = (double)val.w; m_io.write( (char *)&v, 8 );
4742 }
4743 else
4744 {
4745 EM_error3( "[chuck](via FileIO): writeFloat error: invalid/unsupport datatype size flag" );
4746 }
4747 }
4748 else
4749 {
4750 EM_error3( "[chuck](via FileIO): write error: invalid ASCII/binary flag" );
4751 }
4752
4753 if( m_io.fail() ) { // check both before and after write if stream is ok
4754 EM_error3( "[chuck](via FileIO): cannot write: I/O stream failed" );
4755 }
4756 }
4757
4758
4759
4760
4761 #ifndef __DISABLE_THREADS__
4762 // static helper functions for writing asynchronously
4763 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::writeStr_thread ) (void * data)
4764 {
4765 async_args * args = (async_args *)data;
4766 args->fileio_obj->write( args->stringArg );
4767 Chuck_Event * e = args->fileio_obj->m_asyncEvent;
4768 delete args;
4769 e->broadcast_local(); // wake up
4770 e->broadcast_global();
4771
4772 return (THREAD_RETURN)0;
4773 }
4774
4775 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::writeInt_thread ) (void * data)
4776 {
4777 async_args * args = (async_args *)data;
4778 args->fileio_obj->write( args->intArg );
4779 args->fileio_obj->m_asyncEvent->broadcast_local(); // wake up
4780 args->fileio_obj->m_asyncEvent->broadcast_global();
4781 delete args;
4782
4783 return (THREAD_RETURN)0;
4784 }
4785
4786 THREAD_RETURN( THREAD_TYPE Chuck_IO_File::writeFloat_thread ) (void * data)
4787 {
4788 async_args * args = (async_args *)data;
4789 args->fileio_obj->write( args->floatArg, args->intArg );
4790 args->fileio_obj->m_asyncEvent->broadcast_local(); // wake up
4791 args->fileio_obj->m_asyncEvent->broadcast_global();
4792 delete args;
4793
4794 return (THREAD_RETURN)0;
4795 }
4796 #endif // __DISABLE_THREADS__
4797
4798
4799
4800
4801 //-----------------------------------------------------------------------------
4802 // name: Chuck_IO_Chout()
4803 // desc: constructor
4804 //-----------------------------------------------------------------------------
4805 Chuck_IO_Chout::Chuck_IO_Chout( Chuck_Carrier * carrier )
4806 {
4807 // store
4808 carrier->chout = this;
4809 // add ref
4810 this->add_ref();
4811 // zero out
4812 m_callback = NULL;
4813 // initialize (added 1.3.0.0)
4814 initialize_object( this, carrier->env->ckt_chout, NULL, carrier->vm );
4815 // lock so can't be deleted conventionally
4816 this->lock();
4817 }
4818
4819 Chuck_IO_Chout::~Chuck_IO_Chout()
4820 {
4821 m_callback = NULL;
4822 }
4823
4824 void Chuck_IO_Chout::set_output_callback( void (*fp)(const char *) )
4825 {
4826 m_callback = fp;
4827 }
4828
4829 t_CKBOOL Chuck_IO_Chout::good()
4830 {
4831 return m_callback != NULL || cout.good();
4832 }
4833
4834 void Chuck_IO_Chout::close()
4835 { /* uh can't do it */
4836 }
4837
4838 void Chuck_IO_Chout::flush()
4839 {
4840 if( m_callback )
4841 {
4842 // send to callback
4843 m_callback( m_buffer.str().c_str() );
4844 }
4845 else
4846 {
4847 // send to cout
4848 g_ck_stdoutstream << m_buffer.str();
4849 // flush
4850 g_ck_stdoutstream.flush();
4851 }
4852 // clear buffer
4853 m_buffer.str( std::string() );
4854 }
4855
4856 t_CKINT Chuck_IO_Chout::mode()
4857 {
4858 return 0;
4859 }
4860
4861 void Chuck_IO_Chout::mode( t_CKINT flag )
4862 { }
4863
4864 Chuck_String * Chuck_IO_Chout::readLine()
4865 {
4866 return NULL;
4867 }
4868
4869 t_CKINT Chuck_IO_Chout::readInt( t_CKINT flags )
4870 {
4871 return 0;
4872 }
4873
4874 t_CKFLOAT Chuck_IO_Chout::readFloat()
4875 {
4876 return 0;
4877 }
4878
4879 t_CKFLOAT Chuck_IO_Chout::readFloat( t_CKINT flags )
4880 {
4881 return 0;
4882 }
4883
4884 t_CKBOOL Chuck_IO_Chout::readString( std::string & str )
4885 {
4886 str = "";
4887 return FALSE;
4888 }
4889
4890 t_CKBOOL Chuck_IO_Chout::eof()
4891 {
4892 return TRUE;
4893 }
4894
4895 void Chuck_IO_Chout::write( const std::string & val )
4896 // added 1.3.0.0: the flush
4897 {
4898 // insert into stream
4899 m_buffer << val;
4900 // if there is an endline anywhere | 1.5.1.1
4901 if( val.find("\n") != string::npos ) flush();
4902 // previous: only if val == endline
4903 // if( val == "\n" ) flush();
4904 }
4905
4906 void Chuck_IO_Chout::write( t_CKINT val )
4907 {
4908 m_buffer << val;
4909 }
4910
4911 void Chuck_IO_Chout::write( t_CKINT val, t_CKINT flags )
4912 {
4913 // ignore flag for chout
4914 m_buffer << val;
4915 }
4916
4917 void Chuck_IO_Chout::write( t_CKFLOAT val )
4918 {
4919 m_buffer << val;
4920 }
4921
4922 void Chuck_IO_Chout::write( t_CKFLOAT val, t_CKINT flags )
4923 {
4924 // ignore flags for chout
4925 m_buffer << val;
4926 }
4927
4928 void Chuck_IO_Chout::write( const t_CKCOMPLEX & val )
4929 {
4930 // print complex value
4931 m_buffer << "#(" << val.re << "," << val.im << ")";
4932 }
4933
4934 void Chuck_IO_Chout::write( const t_CKCOMPLEX & val, t_CKINT flags )
4935 {
4936 // ignore flags for chout
4937 this->write( val );
4938 }
4939
4940 void Chuck_IO_Chout::write( const t_CKPOLAR & val )
4941 {
4942 // print polar value
4943 m_buffer << "%(" << val.modulus << "," << val.phase << ")";
4944 }
4945
4946 void Chuck_IO_Chout::write( const t_CKPOLAR & val, t_CKINT flags )
4947 {
4948 // ignore flags for chout
4949 this->write( val );
4950 }
4951
4952 void Chuck_IO_Chout::write( const t_CKVEC2 & v )
4953 {
4954 // print vec2 value
4955 m_buffer << "@(" << v.x << "," << v.y << ")";
4956 }
4957
4958 void Chuck_IO_Chout::write( const t_CKVEC2 & v, t_CKINT flags )
4959 {
4960 // ignore flags for chout
4961 this->write( v );
4962 }
4963
4964 void Chuck_IO_Chout::write( const t_CKVEC3 & v )
4965 {
4966 // print vec3 value
4967 m_buffer << "@(" << v.x << "," << v.y << "," << v.z << ")";
4968 }
4969
4970 void Chuck_IO_Chout::write( const t_CKVEC3 & v, t_CKINT flags )
4971 {
4972 // ignore flags for chout
4973 this->write( v );
4974 }
4975
4976 void Chuck_IO_Chout::write( const t_CKVEC4 & v )
4977 {
4978 // print vec4 value
4979 m_buffer << "@(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")";
4980 }
4981
4982 void Chuck_IO_Chout::write( const t_CKVEC4 & v, t_CKINT flags )
4983 {
4984 // ignore flags for chout
4985 this->write( v );
4986 }
4987
4988
4989
4990
4991 //-----------------------------------------------------------------------------
4992 // name: Chuck_IO_Cherr()
4993 // desc: constructor
4994 //-----------------------------------------------------------------------------
4995 Chuck_IO_Cherr::Chuck_IO_Cherr( Chuck_Carrier * carrier )
4996 {
4997 // store
4998 carrier->cherr = this;
4999 // add ref
5000 this->add_ref();
5001 // zero out
5002 m_callback = NULL;
5003 // initialize (added 1.3.0.0)
5004 initialize_object( this, carrier->env->ckt_cherr, NULL, carrier->vm );
5005 // lock so can't be deleted conventionally
5006 this->lock();
5007 }
5008
5009 Chuck_IO_Cherr::~Chuck_IO_Cherr()
5010 {
5011 m_callback = NULL;
5012 }
5013
5014 void Chuck_IO_Cherr::set_output_callback( void (*fp)(const char *) )
5015 {
5016 m_callback = fp;
5017 }
5018
5019 t_CKBOOL Chuck_IO_Cherr::good()
5020 {
5021 return m_callback != NULL || cerr.good();
5022 }
5023
5024 void Chuck_IO_Cherr::close()
5025 { /* uh can't do it */
5026 }
5027
5028 void Chuck_IO_Cherr::flush()
5029 {
5030 if( m_callback )
5031 {
5032 // send to callback
5033 m_callback( m_buffer.str().c_str() );
5034 }
5035 else
5036 {
5037 // send to cerr | this should auto-flush
5038 g_ck_stderrstream << m_buffer.str();
5039 }
5040
5041 // clear buffer
5042 m_buffer.str( std::string() );
5043 }
5044
5045 t_CKINT Chuck_IO_Cherr::mode()
5046 {
5047 return 0;
5048 }
5049
5050 void Chuck_IO_Cherr::mode( t_CKINT flag )
5051 { }
5052
5053 Chuck_String * Chuck_IO_Cherr::readLine()
5054 {
5055 return NULL;
5056 }
5057
5058 t_CKINT Chuck_IO_Cherr::readInt( t_CKINT flags )
5059 {
5060 return 0;
5061 }
5062
5063 t_CKFLOAT Chuck_IO_Cherr::readFloat()
5064 {
5065 return 0;
5066 }
5067
5068 t_CKFLOAT Chuck_IO_Cherr::readFloat( t_CKINT flags )
5069 {
5070 return 0;
5071 }
5072
5073 t_CKBOOL Chuck_IO_Cherr::readString( std::string & str )
5074 {
5075 str = "";
5076 return FALSE;
5077 }
5078
5079 t_CKBOOL Chuck_IO_Cherr::eof()
5080 {
5081 return TRUE;
5082 }
5083
5084 void Chuck_IO_Cherr::write( const std::string & val )
5085 {
5086 // insert into stream
5087 m_buffer << val;
5088 // always flush for cerr | 1.5.0.0 (ge) added
5089 flush();
5090 // if( val.find("\n") != string::npos ) flush();
5091 }
5092
5093 void Chuck_IO_Cherr::write( t_CKINT val )
5094 {
5095 m_buffer << val;
5096 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5097 }
5098
5099 void Chuck_IO_Cherr::write( t_CKINT val, t_CKINT flags )
5100 {
5101 m_buffer << val;
5102 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5103 }
5104
5105 void Chuck_IO_Cherr::write( t_CKFLOAT val )
5106 {
5107 m_buffer << val;
5108 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5109 }
5110
5111 void Chuck_IO_Cherr::write( t_CKFLOAT val, t_CKINT flags )
5112 {
5113 // ignore flags for cherr
5114 m_buffer << val;
5115 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5116 }
5117
5118 void Chuck_IO_Cherr::write( const t_CKCOMPLEX & val )
5119 {
5120 // print complex value
5121 m_buffer << "#(" << val.re << "," << val.im << ")";
5122 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5123 }
5124
5125 void Chuck_IO_Cherr::write( const t_CKCOMPLEX & val, t_CKINT flags )
5126 {
5127 // ignore flags for cherr
5128 this->write( val );
5129 }
5130
5131 void Chuck_IO_Cherr::write( const t_CKPOLAR & val )
5132 {
5133 // print polar value
5134 m_buffer << "%(" << val.modulus << "," << val.phase << ")";
5135 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5136 }
5137
5138 void Chuck_IO_Cherr::write( const t_CKPOLAR & val, t_CKINT flags )
5139 {
5140 // ignore flags for cherr
5141 this->write( val );
5142 }
5143
5144 void Chuck_IO_Cherr::write( const t_CKVEC2 & v )
5145 {
5146 // print vec2 value
5147 m_buffer << "@(" << v.x << "," << v.y << ")";
5148 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5149 }
5150
5151 void Chuck_IO_Cherr::write( const t_CKVEC2 & v, t_CKINT flags )
5152 {
5153 // ignore flags for cherr
5154 this->write( v );
5155 }
5156
5157 void Chuck_IO_Cherr::write( const t_CKVEC3 & v )
5158 {
5159 // print vec3 value
5160 m_buffer << "@(" << v.x << "," << v.y << "," << v.z << ")";
5161 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5162 }
5163
5164 void Chuck_IO_Cherr::write( const t_CKVEC3 & v, t_CKINT flags )
5165 {
5166 // ignore flags for cherr
5167 this->write( v );
5168 }
5169
5170 void Chuck_IO_Cherr::write( const t_CKVEC4 & v )
5171 {
5172 // print vec4 value
5173 m_buffer << "@(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")";
5174 flush(); // always flush for cerr | 1.5.0.0 (ge) added
5175 }
5176
5177 void Chuck_IO_Cherr::write( const t_CKVEC4 & v, t_CKINT flags )
5178 {
5179 // ignore flags for cherr
5180 this->write( v );
5181 }
5182
5183
5184
5185
5186 #ifndef __DISABLE_SERIAL__
5187 // available baud rates
5188 const t_CKUINT Chuck_IO_Serial::CK_BAUD_2400 = 2400;
5189 const t_CKUINT Chuck_IO_Serial::CK_BAUD_4800 = 4800;
5190 const t_CKUINT Chuck_IO_Serial::CK_BAUD_9600 = 9600;
5191 const t_CKUINT Chuck_IO_Serial::CK_BAUD_19200 = 19200;
5192 const t_CKUINT Chuck_IO_Serial::CK_BAUD_38400 = 38400;
5193 const t_CKUINT Chuck_IO_Serial::CK_BAUD_7200 = 7200;
5194 const t_CKUINT Chuck_IO_Serial::CK_BAUD_14400 = 14400;
5195 const t_CKUINT Chuck_IO_Serial::CK_BAUD_28800 = 28800;
5196 const t_CKUINT Chuck_IO_Serial::CK_BAUD_57600 = 57600;
5197 const t_CKUINT Chuck_IO_Serial::CK_BAUD_76800 = 76800;
5198 const t_CKUINT Chuck_IO_Serial::CK_BAUD_115200 = 115200;
5199 const t_CKUINT Chuck_IO_Serial::CK_BAUD_230400 = 230400;
5200
5201 const t_CKUINT Chuck_IO_Serial::TYPE_NONE = 0;
5202 const t_CKUINT Chuck_IO_Serial::TYPE_BYTE = 1;
5203 const t_CKUINT Chuck_IO_Serial::TYPE_WORD = 2;
5204 const t_CKUINT Chuck_IO_Serial::TYPE_INT = 3;
5205 const t_CKUINT Chuck_IO_Serial::TYPE_FLOAT = 4;
5206 const t_CKUINT Chuck_IO_Serial::TYPE_STRING = 5;
5207 const t_CKUINT Chuck_IO_Serial::TYPE_LINE = 6;
5208 const t_CKUINT Chuck_IO_Serial::TYPE_WRITE = 100;
5209
5210 std::list<Chuck_IO_Serial *> Chuck_IO_Serial::s_serials;
5211
5212 // static const t_CKUINT TYPE_NONE = 0;
5213 // static const t_CKUINT TYPE_BYTE = 1;
5214 // static const t_CKUINT TYPE_WORD = 2;
5215 // static const t_CKUINT TYPE_INT = 3;
5216 // static const t_CKUINT TYPE_FLOAT = 4;
5217 // static const t_CKUINT TYPE_STRING = 5;
5218 // static const t_CKUINT TYPE_LINE = 6;
5219 // static const t_CKUINT TYPE_WRITE = 100;
5220
5221 const t_CKUINT CHUCK_IO_DEFAULT_BUFSIZE = 1024;
5222 const char * CHUCK_IO_SCANF_STRING = "%1024s";
5223
5224 CK_DLL_MFUN(serialio_canWait);
5225
5226 void * Chuck_IO_Serial::shell_read_cb( void *_this )
5227 {
5228 Chuck_IO_Serial *cereal = (Chuck_IO_Serial *) _this;
5229
5230 cereal->read_cb();
5231
5232 return NULL;
5233 }
5234
5235 void * Chuck_IO_Serial::shell_write_cb( void *_this )
5236 {
5237 Chuck_IO_Serial *cereal = (Chuck_IO_Serial *) _this;
5238
5239 cereal->write_cb();
5240
5241 return NULL;
5242 }
5243
5244 void Chuck_IO_Serial::shutdown()
5245 {
5246 EM_log(CK_LOG_INFO, "shutting down serial devices");
5247
5248 for(std::list<Chuck_IO_Serial *>::iterator i = s_serials.begin(); i != s_serials.end(); i++)
5249 {
5250 (*i)->close();
5251 }
5252 }
5253
5254
5255 Chuck_IO_Serial::Chuck_IO_Serial( Chuck_VM * vm ) :
5256 m_asyncRequests(CircularBuffer<Request>(32)),
5257 m_asyncResponses(CircularBuffer<Request>(32)),
5258 m_asyncWriteRequests(CircularBuffer<Request>(32)),
5259 m_writeBuffer(1024)
5260 {
5261 m_fd = -1;
5262 m_cfd = NULL;
5263
5264 m_io_buf_max = CHUCK_IO_DEFAULT_BUFSIZE;
5265 m_io_buf = new unsigned char[m_io_buf_max];
5266 m_io_buf_pos = m_io_buf_available = 0;
5267
5268 m_tmp_buf_max = CHUCK_IO_DEFAULT_BUFSIZE;
5269 m_tmp_buf = new unsigned char[m_tmp_buf_max];
5270
5271 m_read_thread = NULL;
5272 m_event_buffer = NULL;
5273
5274 m_write_thread = NULL;
5275 m_do_write_thread = TRUE;
5276
5277 m_do_exit = FALSE;
5278
5279 s_serials.push_back(this);
5280
5281 m_vmRef = vm;
5282 }
5283
5284 Chuck_IO_Serial::~Chuck_IO_Serial()
5285 {
5286 m_do_read_thread = FALSE;
5287 m_do_write_thread = FALSE;
5288 CK_SAFE_DELETE(m_read_thread);
5289 if( m_event_buffer )
5290 m_vmRef->destroy_event_buffer( m_event_buffer );
5291
5292 close();
5293
5294 CK_SAFE_DELETE(m_io_buf);
5295 m_io_buf_max = 0;
5296 m_io_buf_pos = m_io_buf_available = 0;
5297
5298 CK_SAFE_DELETE(m_tmp_buf);
5299 m_tmp_buf_max = 0;
5300
5301 s_serials.remove(this);
5302 }
5303
5304 t_CKBOOL Chuck_IO_Serial::ready()
5305 {
5306 #ifndef __PLATFORM_WINDOWS__
5307 struct pollfd pollfds;
5308 pollfds.fd = m_fd;
5309 pollfds.events = POLLIN;
5310 pollfds.revents = 0;
5311
5312 int result = poll(&pollfds, 1, 0);
5313
5314 if( result > 0 && (pollfds.revents & POLLIN) ) return TRUE;
5315 else return FALSE; // TODO: error handling
5316 #else
5317 return FALSE;
5318 #endif
5319 }
5320
5321 t_CKBOOL Chuck_IO_Serial::open( const t_CKUINT i, t_CKINT flags, t_CKUINT baud )
5322 {
5323 vector<string> ports = SerialIOManager::availableSerialDevices();
5324
5325 if( i < ports.size() )
5326 {
5327 const string &path = ports[i];
5328
5329 open(path, flags, baud);
5330 }
5331 else
5332 {
5333 EM_log(CK_LOG_WARNING, "(Serial.open): error: invalid port number");
5334 return FALSE;
5335 }
5336
5337 return TRUE;
5338 }
5339
5340
5341 t_CKBOOL Chuck_IO_Serial::open( const std::string & path, t_CKINT flags, t_CKUINT baud )
5342 {
5343 if( flags & Chuck_IO::TYPE_BINARY )
5344 {
5345 m_flags = Chuck_IO::TYPE_BINARY;
5346 }
5347 else if( flags & Chuck_IO::TYPE_ASCII )
5348 {
5349 m_flags = Chuck_IO::TYPE_ASCII;
5350 }
5351 else
5352 {
5353 EM_log(CK_LOG_WARNING, "(Serial.open): warning: invalid binary/ASCII flag requested, defaulting to ASCII");
5354 m_flags = Chuck_IO::TYPE_ASCII;
5355 }
5356
5357 int fd = ::open( path.c_str(), O_RDWR );
5358 if( fd < 0 )
5359 {
5360 EM_log(CK_LOG_WARNING, "(Serial.open): error: unable to open file at '%s'", path.c_str());
5361 return FALSE;
5362 }
5363
5364 m_fd = fd;
5365 m_path = path;
5366
5367 // set default baud rate
5368 setBaudRate(baud);
5369
5370 m_cfd = fdopen(fd, "a+");
5371 m_iomode = MODE_ASYNC;
5372 #ifndef __PLATFORM_WINDOWS__
5373 fcntl(m_fd, F_SETFL, O_NONBLOCK);
5374 #endif
5375 m_eof = FALSE;
5376
5377 return TRUE;
5378 }
5379
5380
5381 t_CKBOOL Chuck_IO_Serial::good()
5382 {
5383 return m_fd >= 0 && m_cfd != NULL;
5384 }
5385
5386 void Chuck_IO_Serial::close()
5387 {
5388 if(m_iomode == MODE_SYNC)
5389 {
5390 close_int();
5391 }
5392 else
5393 {
5394 m_do_exit = TRUE;
5395 #ifdef __PLATFORM_WINDOWS__
5396 m_read_thread->wait(-1, TRUE);
5397 close_int();
5398 #else
5399 m_read_thread->wait(-1, FALSE);
5400 #endif
5401 }
5402 }
5403
5404 void Chuck_IO_Serial::close_int()
5405 {
5406 EM_log(CK_LOG_INFO, "(Serial.close): closing serial device '%s'", m_path.c_str());
5407
5408 if(good())
5409 {
5410 if(m_cfd)
5411 fclose(m_cfd);
5412 else
5413 ::close(m_fd);
5414 m_fd = -1;
5415 m_cfd = NULL;
5416 }
5417 }
5418
5419 void Chuck_IO_Serial::flush()
5420 {
5421 if(good())
5422 {
5423 if(m_iomode == MODE_SYNC)
5424 fflush(m_cfd);
5425 else
5426 ; // TODO
5427 }
5428 }
5429
5430 t_CKBOOL Chuck_IO_Serial::can_wait()
5431 {
5432 return m_asyncResponses.numElements() == 0;
5433 }
5434
5435 t_CKINT Chuck_IO_Serial::mode()
5436 {
5437 return m_iomode;
5438 }
5439
5440 void Chuck_IO_Serial::mode( t_CKINT flag )
5441 {
5442 if( flag == MODE_SYNC )
5443 m_iomode = MODE_SYNC;
5444 else if( flag == MODE_ASYNC )
5445 m_iomode = MODE_ASYNC;
5446 }
5447
5448 // reading
5449 t_CKINT Chuck_IO_Serial::readInt( t_CKINT flags )
5450 {
5451 if( !good() )
5452 {
5453 EM_log(CK_LOG_WARNING, "(Serial.readInt): warning: file not open");
5454 return 0;
5455 }
5456
5457 if( m_iomode == MODE_ASYNC )
5458 {
5459 EM_log(CK_LOG_WARNING, "(Serial.readInt): warning: calling synchronous read function on asynchronous file");
5460 return 0;
5461 }
5462
5463 long i = 0; // 1.4.1.1 (ge) changed from t_CKUINT; to clear warning for fscanf using %li
5464
5465 if( m_flags & Chuck_IO::TYPE_BINARY )
5466 {
5467 if( flags & Chuck_IO::INT8 || flags & Chuck_IO::UINT8 ) // 1.5.0.1 (ge) added UINT
5468 {
5469 uint8_t byte = 0;
5470 if(!fread(&byte, 1, 1, m_cfd))
5471 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5472
5473 i = byte;
5474 }
5475 else if( flags & Chuck_IO::INT16 || flags & Chuck_IO::UINT16 ) // 1.5.0.1 (ge) added UINT
5476 {
5477 uint16_t word = 0;
5478 if(!fread(&word, 2, 1, m_cfd))
5479 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5480
5481 i = word;
5482 }
5483 else if( flags & Chuck_IO::INT32 || flags & Chuck_IO::UINT32 ) // 1.5.0.1 (ge) added UINT
5484 {
5485 uint32_t dword = 0;
5486 if(!fread(&dword, 4, 1, m_cfd))
5487 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5488
5489 i = dword;
5490 }
5491 else if( flags & Chuck_IO::INT64 || flags & Chuck_IO::UINT64 ) // 1.5.0.1 (ge) added UINT and 64-bit
5492 {
5493 uint64_t val = 0;
5494 if(!fread(&val, 8, 1, m_cfd))
5495 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5496
5497 i = val;
5498 }
5499 else if( flags & Chuck_IO::SINT8 ) // 1.5.0.1 (ge) added SINT
5500 {
5501 int8_t byte = 0;
5502 if(!fread(&byte, 1, 1, m_cfd))
5503 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5504
5505 i = byte;
5506 }
5507 else if( flags & Chuck_IO::SINT16 ) // 1.5.0.1 (ge) added SINT
5508 {
5509 int16_t word = 0;
5510 if(!fread(&word, 2, 1, m_cfd))
5511 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5512
5513 i = word;
5514 }
5515 else if( flags & Chuck_IO::SINT32 ) // 1.5.0.1 (ge) added SINT
5516 {
5517 int32_t dword = 0;
5518 if(!fread(&dword, 4, 1, m_cfd))
5519 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5520
5521 i = dword;
5522 }
5523 else if( flags & Chuck_IO::SINT64 ) // 1.5.0.1 (ge) added SINT and 64-bit
5524 {
5525 int64_t val = 0;
5526 if(!fread(&val, 8, 1, m_cfd))
5527 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5528
5529 i = val;
5530 }
5531 }
5532 else
5533 {
5534 if(!fscanf(m_cfd, "%li", &i))
5535 EM_log(CK_LOG_WARNING, "(Serial.readInt): error: read failed");
5536 }
5537
5538 return i;
5539 }
5540
5541 t_CKFLOAT Chuck_IO_Serial::readFloat( )
5542 {
5543 // 1.5.0.1 (ge) redirect
5544 return readFloat( Chuck_IO::FLOAT32 );
5545 }
5546
5547 t_CKFLOAT Chuck_IO_Serial::readFloat( t_CKINT flags )
5548 {
5549 if( !good() )
5550 {
5551 EM_log(CK_LOG_WARNING, "(Serial.readFloat): warning: file not open");
5552 return 0;
5553 }
5554
5555 if( m_iomode == MODE_ASYNC )
5556 {
5557 EM_log(CK_LOG_WARNING, "(Serial.readFloat): warning: calling synchronous read function on asynchronous file");
5558 return 0;
5559 }
5560
5561 t_CKFLOAT f = 0;
5562
5563 if( m_flags & Chuck_IO::TYPE_BINARY )
5564 {
5565 // check size
5566 if( flags & Chuck_IO::FLOAT32 )
5567 {
5568 float v = 0.0f;
5569 if(!fread(&v, 4, 1, m_cfd))
5570 EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed");
5571 f = v;
5572 }
5573 else if( flags & Chuck_IO::FLOAT64 )
5574 {
5575 double v = 0.0;
5576 if( !fread(&v, 8, 1, m_cfd) )
5577 EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed");
5578 f = v;
5579 }
5580 else
5581 {
5582 // error message
5583 EM_log(CK_LOG_WARNING, "(Serial.readFloat): invalid or unsupported size flag");
5584 }
5585 }
5586 else
5587 {
5588 if(!fscanf(m_cfd, "%lf", &f))
5589 EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed");
5590 }
5591
5592 return f;
5593 }
5594
5595 t_CKBOOL Chuck_IO_Serial::readString( std::string & str )
5596 {
5597 if( !good() )
5598 {
5599 EM_log(CK_LOG_WARNING, "(Serial.readString): warning: file not open");
5600 return FALSE;
5601 }
5602
5603 if( m_iomode == MODE_ASYNC )
5604 {
5605 EM_log(CK_LOG_WARNING, "(Serial.readString): warning: calling synchronous read function on asynchronous file");
5606 return FALSE;
5607 }
5608
5609 if( m_flags & Chuck_IO::TYPE_BINARY )
5610 {
5611 EM_log(CK_LOG_WARNING, "(Serial.readString): warning: cannot read string from binary file");
5612 return FALSE;
5613 }
5614
5615 if(!fscanf(m_cfd, CHUCK_IO_SCANF_STRING, &m_tmp_buf))
5616 {
5617 EM_log(CK_LOG_WARNING, "(Serial.readFloat): error: read failed");
5618 return FALSE;
5619 }
5620
5621 str = (char *) m_tmp_buf;
5622
5623 return TRUE;
5624 }
5625
5626
5627 Chuck_String * Chuck_IO_Serial::readLine()
5628 {
5629 if( !good() )
5630 {
5631 EM_log(CK_LOG_WARNING, "(Serial.readLine): warning: file not open");
5632 return NULL;
5633 }
5634
5635 if( m_iomode == MODE_ASYNC )
5636 {
5637 EM_log(CK_LOG_WARNING, "(Serial.readLine): warning: calling synchronous read function on asynchronous file");
5638 return NULL;
5639 }
5640
5641 if( m_flags & Chuck_IO::TYPE_BINARY )
5642 {
5643 EM_log(CK_LOG_WARNING, "(Serial.readLine): warning: cannot read line from binary file");
5644 return NULL;
5645 }
5646
5647 if(!fgets((char *)m_tmp_buf, (int)m_tmp_buf_max, m_cfd))
5648 {
5649 EM_log(CK_LOG_WARNING, "(Serial.readLine): error: from fgets");
5650 return NULL;
5651 }
5652
5653 Chuck_String * str = new Chuck_String;
5654 initialize_object(str, m_vmRef->env()->ckt_string, NULL, m_vmRef);
5655
5656 str->set( string((char *)m_tmp_buf) );
5657
5658 return str;
5659 }
5660
5661
5662 t_CKBOOL Chuck_IO_Serial::eof()
5663 {
5664 if( !good() )
5665 {
5666 EM_log(CK_LOG_WARNING, "(Serial.eof): warning: file not open");
5667 return FALSE;
5668 }
5669
5670 if( MODE_SYNC )
5671 return (m_eof = feof( m_cfd ));
5672 else
5673 return m_eof;
5674 }
5675
5676
5677 // writing
5678 void Chuck_IO_Serial::write( const std::string & val )
5679 {
5680 if( !good() )
5681 {
5682 EM_log(CK_LOG_WARNING, "(Serial.write): warning: file not open");
5683 return;
5684 }
5685
5686 if( m_iomode == MODE_ASYNC )
5687 {
5688 start_read_thread();
5689
5690 t_CKUINT len = val.length();
5691 for(t_CKUINT i = 0; i < len; i++)
5692 // TODO: efficiency
5693 m_writeBuffer.put(val[i]);
5694
5695 Request r;
5696 r.m_type = TYPE_WRITE;
5697 r.m_val = 0;
5698 r.m_num = 0;
5699 r.m_status = Request::RQ_STATUS_PENDING;
5700
5701 m_asyncWriteRequests.put(r);
5702 }
5703 else if( m_iomode == MODE_SYNC )
5704 {
5705 fprintf( m_cfd, "%s", val.c_str() );
5706 }
5707 }
5708
5709
5710 void Chuck_IO_Serial::write( t_CKINT val )
5711 {
5712 write( val, Chuck_IO::INT32 );
5713 }
5714
5715
5716 void Chuck_IO_Serial::write( t_CKINT val, t_CKINT flags )
5717 {
5718 if( !good() )
5719 {
5720 EM_log(CK_LOG_WARNING, "(Serial.write): warning: file not open");
5721 return;
5722 }
5723
5724 // check flags
5725 if( (flags & Chuck_IO::INT8 ) || (flags & Chuck_IO::INT16) ||
5726 (flags & Chuck_IO::INT32) || (flags & Chuck_IO::INT64) )
5727 {
5728 EM_log( CK_LOG_WARNING, "(Serial.write): warning: invalid int size flag, ignoring write request" );
5729 return;
5730 }
5731
5732 if( m_iomode == MODE_ASYNC )
5733 {
5734 start_read_thread();
5735
5736 if( m_flags & Chuck_IO::TYPE_ASCII )
5737 {
5738 // TODO: don't use m_tmp_buf (thread safety?)
5739 #ifdef __PLATFORM_WINDOWS__
5740 _snprintf((char *)m_tmp_buf, m_tmp_buf_max, "%li", (long)val);
5741 m_tmp_buf[m_tmp_buf_max - 1] = '\0'; // force NULL terminator -- see http://www.di-mgt.com.au/cprog.html#snprintf
5742 #else
5743 snprintf((char *)m_tmp_buf, m_tmp_buf_max, "%li", val);
5744 #endif
5745 t_CKUINT len = strlen((char *)m_tmp_buf);
5746 for(t_CKUINT i = 0; i < len; i++)
5747 // TODO: efficiency
5748 m_writeBuffer.put(m_tmp_buf[i]);
5749
5750 Request r;
5751 r.m_type = TYPE_WRITE;
5752 r.m_val = 0;
5753 r.m_num = 0;
5754 r.m_status = Request::RQ_STATUS_PENDING;
5755
5756 m_asyncWriteRequests.put(r);
5757 }
5758 else
5759 {
5760 // 1.5.0.1 (ge) check and handle individually
5761 uint8_t v8 = (uint8_t)val;
5762 uint16_t v16 = (uint16_t)val;
5763 uint32_t v32 = (uint32_t)val;
5764 uint64_t v64 = (uint64_t)val;
5765 char * buf = NULL;
5766 t_CKINT size = 0;
5767
5768 if( flags & Chuck_IO::INT8 ) { buf = (char *)&v8; size = 1; }
5769 else if( flags & Chuck_IO::INT16 ) { buf = (char *)&v16; size = 2; }
5770 else if( flags & Chuck_IO::INT32 ) { buf = (char *)&v32; size = 4; }
5771 else if( flags & Chuck_IO::INT64 ) { buf = (char *)&v64; size = 8; }
5772 // other size values should be caught above
5773
5774 // put into write buffer
5775 for(int i = 0; i < size; i++)
5776 m_writeBuffer.put( buf[i] );
5777
5778 Request r;
5779 r.m_type = TYPE_WRITE;
5780 r.m_val = 0;
5781 r.m_num = 0;
5782 r.m_status = Request::RQ_STATUS_PENDING;
5783
5784 m_asyncWriteRequests.put(r);
5785 }
5786 }
5787 else if( m_iomode == MODE_SYNC )
5788 {
5789 if( m_flags & Chuck_IO::TYPE_ASCII )
5790 {
5791 fprintf( m_cfd, "%li", (long)val );
5792 }
5793 else
5794 {
5795 // 1.5.0.1 (ge) check and handle individually
5796 if( flags & Chuck_IO::INT8 )
5797 {
5798 uint8_t v = (uint8_t)val;
5799 fwrite( (char*)&v, 1, 1, m_cfd );
5800 }
5801 else if( flags & Chuck_IO::INT16 )
5802 {
5803 uint16_t v = (uint16_t)val;
5804 fwrite( (char*)&v, 1, 2, m_cfd );
5805 }
5806 else if( flags & Chuck_IO::INT32 )
5807 {
5808 uint32_t v = (uint32_t)val;
5809 fwrite( (char*)&v, 1, 4, m_cfd );
5810 }
5811 else if( flags & Chuck_IO::INT64 )
5812 {
5813 uint64_t v = (uint64_t)val;
5814 fwrite( (char*)&v, 1, 8, m_cfd );
5815 }
5816 // other size values should be caught above
5817 }
5818 }
5819 }
5820
5821 void Chuck_IO_Serial::write( t_CKFLOAT val )
5822 {
5823 this->write( val, Chuck_IO::FLOAT32 );
5824 }
5825
5826 void Chuck_IO_Serial::write( t_CKFLOAT val, t_CKINT flags )
5827 {
5828 if( !good() )
5829 {
5830 EM_log(CK_LOG_WARNING, "(Serial.write): warning: file not open");
5831 return;
5832 }
5833
5834 if( m_iomode == MODE_ASYNC )
5835 {
5836 start_read_thread();
5837
5838 if( m_flags & Chuck_IO::TYPE_ASCII )
5839 {
5840 #ifdef __PLATFORM_WINDOWS__
5841 _snprintf((char *)m_tmp_buf, m_tmp_buf_max, "%f", val);
5842 m_tmp_buf[m_tmp_buf_max - 1] = '\0'; // force NULL terminator -- see http://www.di-mgt.com.au/cprog.html#snprintf
5843 #else
5844 snprintf((char *)m_tmp_buf, m_tmp_buf_max, "%f", val);
5845 #endif
5846
5847 t_CKUINT len = strlen((char *)m_tmp_buf);
5848 for(t_CKUINT i = 0; i < len; i++)
5849 // TODO: efficiency
5850 m_writeBuffer.put(m_tmp_buf[i]);
5851
5852 Request r;
5853 r.m_type = TYPE_WRITE;
5854 r.m_val = 0;
5855 r.m_num = 0;
5856 r.m_status = Request::RQ_STATUS_PENDING;
5857
5858 m_asyncWriteRequests.put(r);
5859 }
5860 else
5861 {
5862 // 1.5.0.1 (ge) check datatype size flag
5863 if( flags & Chuck_IO::FLOAT32 )
5864 {
5865 float v = (float)val;
5866 char * buf = (char *)&v;
5867 for(int i = 0; i < sizeof(v); i++)
5868 m_writeBuffer.put(buf[i]);
5869 }
5870 else if( flags & Chuck_IO::FLOAT64 )
5871 {
5872 double v = (double)val;
5873 char * buf = (char *)&v;
5874 for(int i = 0; i < sizeof(v); i++)
5875 m_writeBuffer.put(buf[i]);
5876 }
5877 else
5878 {
5879 // error message
5880 EM_log(CK_LOG_WARNING, "(Serial.write): invalid or unsupported size flag");
5881 }
5882
5883 Request r;
5884 r.m_type = TYPE_WRITE;
5885 r.m_val = 0;
5886 r.m_num = 0;
5887 r.m_status = Request::RQ_STATUS_PENDING;
5888
5889 m_asyncWriteRequests.put(r);
5890 }
5891 }
5892 else if( m_iomode == MODE_SYNC )
5893 {
5894 if( m_flags & Chuck_IO::TYPE_ASCII )
5895 {
5896 fprintf( m_cfd, "%f", val );
5897 }
5898 else // BINARY
5899 {
5900 // 1.5.0.1 (ge) check datatype size flag
5901 if( flags & Chuck_IO::FLOAT32 )
5902 {
5903 float v = (float)val;
5904 char * buf = (char *)&v;
5905 for(int i = 0; i < sizeof(v); i++)
5906 m_writeBuffer.put(buf[i]);
5907 }
5908 else if( flags & Chuck_IO::FLOAT64 )
5909 {
5910 double v = (double)val;
5911 char * buf = (char *)&v;
5912 for(int i = 0; i < sizeof(v); i++)
5913 m_writeBuffer.put(buf[i]);
5914 }
5915 else
5916 {
5917 // error message
5918 EM_log(CK_LOG_WARNING, "(Serial.write): invalid or unsupported size flag");
5919 }
5920 }
5921 }
5922 }
5923
5924 void Chuck_IO_Serial::write( const t_CKCOMPLEX & val )
5925 {
5926 this->write( val, Chuck_IO::FLOAT32 );
5927 }
5928
5929 void Chuck_IO_Serial::write( const t_CKCOMPLEX & val, t_CKINT flags )
5930 {
5931 // NOTE this will NOT output chuck-format complex #(re,im) in ASCII mode
5932 this->write( val.re, flags );
5933 this->write( val.im, flags );
5934 }
5935
5936 void Chuck_IO_Serial::write( const t_CKPOLAR & val )
5937 {
5938 this->write( val, Chuck_IO::FLOAT32 );
5939 }
5940
5941 void Chuck_IO_Serial::write( const t_CKPOLAR & val, t_CKINT flags )
5942 {
5943 // NOTE this will NOT output chuck-format polar %(modulus,phase) in ASCII mode
5944 this->write( val.modulus, flags );
5945 this->write( val.phase, flags );
5946 }
5947
5948 void Chuck_IO_Serial::write( const t_CKVEC2 & val )
5949 {
5950 this->write( val, Chuck_IO::FLOAT32 );
5951 }
5952
5953 void Chuck_IO_Serial::write( const t_CKVEC2 & val, t_CKINT flags )
5954 {
5955 // NOTE this will NOT output chuck-format vec2 @(x,y) in ASCII mode
5956 this->write( val.x, flags );
5957 this->write( val.y, flags );
5958 }
5959
5960 void Chuck_IO_Serial::write( const t_CKVEC3 & val )
5961 {
5962 this->write( val, Chuck_IO::FLOAT32 );
5963 }
5964
5965 void Chuck_IO_Serial::write( const t_CKVEC3 & val, t_CKINT flags )
5966 {
5967 // NOTE this will NOT output chuck-format vec3 @(x,y,z) in ASCII mode
5968 this->write( val.x, flags );
5969 this->write( val.y, flags );
5970 this->write( val.z, flags );
5971 }
5972
5973 void Chuck_IO_Serial::write( const t_CKVEC4 & val )
5974 {
5975 this->write( val, Chuck_IO::FLOAT32 );
5976 }
5977
5978 void Chuck_IO_Serial::write( const t_CKVEC4 & val, t_CKINT flags )
5979 {
5980 // NOTE this will NOT output chuck-format vec4 @(x,y,z,w) in ASCII mode
5981 this->write( val.x, flags );
5982 this->write( val.y, flags );
5983 this->write( val.z, flags );
5984 this->write( val.w, flags );
5985 }
5986
5987 void Chuck_IO_Serial::writeBytes( Chuck_ArrayInt * arr )
5988 {
5989 if( !good() )
5990 {
5991 EM_log(CK_LOG_WARNING, "(Serial.writeBytes): warning: file not open");
5992 return;
5993 }
5994
5995 if( m_iomode == MODE_ASYNC )
5996 {
5997 start_read_thread();
5998
5999 t_CKINT len = arr->size();
6000 for(int i = 0; i < len; i++)
6001 {
6002 // TODO: efficiency
6003 t_CKUINT byte;
6004 arr->get(i, &byte);
6005 m_writeBuffer.put((char) byte);
6006 }
6007
6008 Request r;
6009 r.m_type = TYPE_WRITE;
6010 r.m_val = 0;
6011 r.m_num = 0;
6012 r.m_status = Request::RQ_STATUS_PENDING;
6013
6014 m_asyncWriteRequests.put(r);
6015 }
6016 else if( m_iomode == MODE_SYNC )
6017 {
6018 t_CKINT len = arr->size();
6019 for(int i = 0; i < len; i++)
6020 {
6021 // TODO: efficiency
6022 t_CKUINT byte;
6023 arr->get(i, &byte);
6024 fwrite(&byte, 1, 1, m_cfd);
6025 }
6026 }
6027 }
6028
6029 void Chuck_IO_Serial::start_read_thread()
6030 {
6031 if(m_read_thread == NULL)
6032 {
6033 m_read_thread = new XThread();
6034 #ifdef __PLATFORM_WINDOWS__
6035 m_read_thread->start((unsigned int (__stdcall *)(void*))shell_read_cb, this);
6036 #else
6037 m_read_thread->start(shell_read_cb, this);
6038 #endif
6039 assert(m_event_buffer == NULL);
6040 m_event_buffer = m_vmRef->create_event_buffer();
6041 }
6042 }
6043
6044
6045 t_CKBOOL Chuck_IO_Serial::readAsync( t_CKUINT type, t_CKUINT num )
6046 {
6047 if( !good() )
6048 {
6049 EM_log(CK_LOG_WARNING, "(Serial.readAsync): warning: file not open");
6050 return FALSE;
6051 }
6052
6053 start_read_thread();
6054
6055 Request read;
6056 read.m_type = type;
6057 read.m_num = num;
6058 read.m_val = 0;
6059 read.m_status = Request::RQ_STATUS_PENDING;
6060
6061 if(!m_asyncRequests.atMaximum() )
6062 {
6063 m_asyncRequests.put(read);
6064 }
6065 else
6066 {
6067 EM_log(CK_LOG_HERALD, "(SerialIO.readAsync): warning: request buffer overflow, dropping read");
6068 return FALSE;
6069 }
6070
6071 return TRUE;
6072 }
6073
6074 Chuck_String * Chuck_IO_Serial::getLine()
6075 {
6076 if( !good() )
6077 {
6078 EM_log(CK_LOG_WARNING, "(Serial.getLine): warning: file not open");
6079 return NULL;
6080 }
6081
6082 Request r;
6083
6084 Chuck_String * str = NULL;
6085
6086 if(m_asyncResponses.peek(r, 1) &&
6087 r.m_type == TYPE_LINE && r.m_status == Request::RQ_STATUS_SUCCESS)
6088 {
6089 str = (Chuck_String *) r.m_val;
6090 m_asyncResponses.get(r);
6091 }
6092
6093 return str;
6094 }
6095
6096
6097 t_CKINT Chuck_IO_Serial::getByte()
6098 {
6099 if( !good() )
6100 {
6101 EM_log(CK_LOG_WARNING, "(Serial.getByte): warning: file not open");
6102 return 0;
6103 }
6104
6105 Request r;
6106
6107 t_CKINT i = 0;
6108
6109 if(m_asyncResponses.peek(r, 1) &&
6110 r.m_type == TYPE_BYTE && r.m_num == 1 &&
6111 r.m_status == Request::RQ_STATUS_SUCCESS)
6112 {
6113 i = (t_CKINT) r.m_val;
6114 m_asyncResponses.get(r);
6115 }
6116
6117 return i;
6118 }
6119
6120 Chuck_Array * Chuck_IO_Serial::getBytes()
6121 {
6122 if( !good() )
6123 {
6124 EM_log(CK_LOG_WARNING, "(Serial.getBytes): warning: file not open");
6125 return NULL;
6126 }
6127
6128 Request r;
6129
6130 Chuck_Array * arr = NULL;
6131
6132 if(m_asyncResponses.peek(r, 1) &&
6133 r.m_type == TYPE_BYTE && r.m_num > 1 &&
6134 r.m_status == Request::RQ_STATUS_SUCCESS)
6135 {
6136 arr = (Chuck_Array *) r.m_val;
6137 initialize_object(arr, m_vmRef->env()->ckt_array, NULL, m_vmRef);
6138 m_asyncResponses.get(r);
6139 }
6140
6141 return arr;
6142 }
6143
6144 Chuck_Array * Chuck_IO_Serial::getInts()
6145 {
6146 if( !good() )
6147 {
6148 EM_log(CK_LOG_WARNING, "(Serial.getInts): warning: file not open");
6149 return NULL;
6150 }
6151
6152 Request r;
6153
6154 Chuck_Array * arr = NULL;
6155
6156 if(m_asyncResponses.peek(r, 1) &&
6157 r.m_type == TYPE_INT && r.m_status == Request::RQ_STATUS_SUCCESS)
6158 {
6159 arr = (Chuck_Array *) r.m_val;
6160 initialize_object(arr, m_vmRef->env()->ckt_array, NULL, m_vmRef);
6161 m_asyncResponses.get(r);
6162 }
6163
6164 return arr;
6165 }
6166
6167 Chuck_Array * Chuck_IO_Serial::getFloats()
6168 {
6169 if( !good() )
6170 {
6171 EM_log(CK_LOG_WARNING, "(Serial.getFloats): warning: file not open");
6172 return NULL;
6173 }
6174
6175 Request r;
6176
6177 Chuck_Array * arr = NULL;
6178
6179 if(m_asyncResponses.peek(r, 1) &&
6180 r.m_type == TYPE_FLOAT && r.m_status == Request::RQ_STATUS_SUCCESS)
6181 {
6182 arr = (Chuck_Array *) r.m_val;
6183 initialize_object(arr, m_vmRef->env()->ckt_array, NULL, m_vmRef);
6184 m_asyncResponses.get(r);
6185 }
6186
6187 return arr;
6188 }
6189
6190 Chuck_String * Chuck_IO_Serial::getString()
6191 {
6192 if( !good() )
6193 {
6194 EM_log(CK_LOG_WARNING, "(Serial.getString): warning: file not open");
6195 return NULL;
6196 }
6197
6198 Request r;
6199
6200 Chuck_String * str = NULL;
6201
6202 if(m_asyncResponses.peek(r, 1) &&
6203 r.m_type == TYPE_STRING && r.m_status == Request::RQ_STATUS_SUCCESS)
6204 {
6205 str = (Chuck_String *) r.m_val;
6206 m_asyncResponses.get(r);
6207 }
6208
6209 return str;
6210 }
6211
6212
6213 t_CKBOOL Chuck_IO_Serial::get_buffer(t_CKINT timeout_ms)
6214 {
6215 #ifndef __PLATFORM_WINDOWS__
6216 fd_set fds;
6217 FD_ZERO(&fds);
6218 FD_SET(m_fd, &fds);
6219
6220 timeval tv;
6221 tv.tv_sec = timeout_ms/1000;
6222 tv.tv_usec = 1000 * (timeout_ms%1000);
6223
6224 t_CKINT result = select(m_fd+1, &fds, NULL, NULL, &tv);
6225
6226 if(result > 0 && FD_ISSET(m_fd, &fds))
6227 {
6228 result = ::read(m_fd, m_io_buf, m_io_buf_max);
6229 if(result > 0)
6230 {
6231 m_io_buf_available = result;
6232 m_io_buf_pos = 0;
6233
6234 EM_log(CK_LOG_FINE, "(SerialIO::get_buffer): read() returned %i bytes", result);
6235
6236 return TRUE;
6237 }
6238 else
6239 {
6240 EM_log(CK_LOG_HERALD, "(SerialIO::get_buffer): read() returned 0 bytes");
6241 }
6242 }
6243 else
6244 {
6245 EM_log(CK_LOG_FINE, "(SerialIO::get_buffer): select() timeout");
6246 }
6247
6248 return FALSE;
6249
6250 #else
6251
6252 int result = ::read(m_fd, m_io_buf, m_io_buf_max);
6253 if(result > 0)
6254 {
6255 m_io_buf_available = result;
6256 m_io_buf_pos = 0;
6257
6258 return TRUE;
6259 }
6260
6261 Sleep(timeout_ms);
6262
6263 return FALSE;
6264
6265 #endif
6266 }
6267
6268 // peek next byte
6269 t_CKINT Chuck_IO_Serial::peek_buffer()
6270 {
6271 // CK_FPRINTF_STDERR( "Chuck_IO_Serial::peek_buffer %i/%i\n", m_io_buf_pos, m_io_buf_available);
6272
6273 if(m_io_buf_pos >= m_io_buf_available)
6274 {
6275 // refresh data
6276 while(!m_do_exit && !m_eof && !get_buffer(5))
6277 ;
6278
6279 if(m_do_exit || m_eof)
6280 return -1;
6281 }
6282
6283 return m_io_buf[m_io_buf_pos];
6284 }
6285
6286 // consume next byte
6287 // return -1 on error/exit condition
6288 t_CKINT Chuck_IO_Serial::pull_buffer()
6289 {
6290 // CK_FPRINTF_STDERR( "Chuck_IO_Serial::pull_buffer %i/%i\n", m_io_buf_pos, m_io_buf_available);
6291
6292 if(m_io_buf_pos >= m_io_buf_available)
6293 {
6294 // refresh data
6295 while(!m_do_exit && !m_eof && !get_buffer(5))
6296 ;
6297
6298 if(m_do_exit || m_eof)
6299 return -1;
6300 }
6301
6302 return m_io_buf[m_io_buf_pos++];
6303 }
6304
6305
6306 t_CKINT Chuck_IO_Serial::buffer_bytes_to_tmp(t_CKINT num_bytes)
6307 {
6308 t_CKUINT len = 0;
6309 while(len < num_bytes && !m_do_exit)
6310 {
6311 if(peek_buffer() == -1)
6312 break;
6313
6314 m_tmp_buf[len++] = pull_buffer();
6315 }
6316
6317 return len;
6318 }
6319
6320
6321 t_CKBOOL Chuck_IO_Serial::handle_line(Chuck_IO_Serial::Request &r)
6322 {
6323 t_CKUINT len = 0;
6324 Chuck_String * str = NULL;
6325
6326 while(!m_do_exit)
6327 {
6328 if(peek_buffer() == -1)
6329 break;
6330
6331 if(peek_buffer() == '\r')
6332 {
6333 // consume newline character
6334 pull_buffer();
6335 if(peek_buffer() == '\n') pull_buffer(); // handle \r\n
6336 break;
6337 }
6338
6339 if(peek_buffer() == '\n')
6340 {
6341 // consume newline character
6342 pull_buffer();
6343 if(peek_buffer() == '\r') pull_buffer(); // handle \n\r (unlikely)
6344 break;
6345 }
6346
6347 m_tmp_buf[len++] = pull_buffer();
6348
6349 if(len >= m_tmp_buf_max)
6350 {
6351 EM_log(CK_LOG_WARNING, "(Serial.handle_line): warning: line exceeds buffer length, truncating to %i characters", m_tmp_buf_max);
6352 break;
6353 }
6354 }
6355
6356 if(m_do_exit)
6357 goto error;
6358 // TODO: eof
6359
6360 str = new Chuck_String;
6361 str->set( std::string((char *)m_tmp_buf, len) );
6362
6363 r.m_val = (t_CKUINT) str;
6364 r.m_status = Chuck_IO_Serial::Request::RQ_STATUS_SUCCESS;
6365
6366 return TRUE;
6367
6368 error:
6369 r.m_val = 0;
6370 r.m_status = Chuck_IO_Serial::Request::RQ_STATUS_FAILURE;
6371
6372 #ifdef __PLATFORM_WINDOWS__
6373 HANDLE hFile = (HANDLE) _get_osfhandle(m_fd);
6374 DWORD error;
6375 ClearCommError(hFile, &error, NULL);
6376
6377 PurgeComm(hFile, PURGE_RXCLEAR);
6378 #endif
6379
6380 return TRUE;
6381 }
6382
6383 t_CKBOOL Chuck_IO_Serial::handle_string(Chuck_IO_Serial::Request & r)
6384 {
6385 return FALSE;
6386 }
6387
6388 t_CKBOOL Chuck_IO_Serial::handle_float_ascii(Chuck_IO_Serial::Request & r)
6389 {
6390 t_CKFLOAT val = 0;
6391 int numRead = 0;
6392 Chuck_ArrayFloat * array = new Chuck_ArrayFloat(0);
6393
6394 for(int i = 0; i < r.m_num && !m_do_exit; i++)
6395 {
6396 t_CKUINT len = 0;
6397
6398 while(!m_do_exit)
6399 {
6400 if(peek_buffer() == -1)
6401 break;
6402
6403 // TODO: '\r'?
6404 int c = (int)peek_buffer();
6405
6406 if(isdigit(c) || c=='.' || (len==0 && (c=='-' || c=='+')))
6407 {
6408 m_tmp_buf[len++] = pull_buffer();
6409 }
6410 else if(len > 0)
6411 {
6412 m_tmp_buf[len++] = '\0';
6413 val = strtod((char *)m_tmp_buf, NULL);
6414
6415 numRead++;
6416 array->push_back(val);
6417
6418 break;
6419 }
6420 }
6421 }
6422
6423 if(m_do_exit || m_eof)
6424 goto error;
6425
6426 r.m_num = numRead;
6427 r.m_val = (t_CKUINT) array;
6428 r.m_status = Request::RQ_STATUS_SUCCESS;
6429
6430 return TRUE;
6431
6432 error:
6433 CK_SAFE_DELETE(array);
6434 r.m_val = 0;
6435 r.m_status = Chuck_IO_Serial::Request::RQ_STATUS_FAILURE;
6436
6437 return TRUE;
6438 }
6439
6440 t_CKBOOL Chuck_IO_Serial::handle_int_ascii(Chuck_IO_Serial::Request & r)
6441 {
6442 t_CKINT val = 0;
6443 int numRead = 0;
6444 Chuck_ArrayInt * array = new Chuck_ArrayInt(FALSE, 0);
6445 initialize_object( array, m_vmRef->env()->ckt_array, NULL, m_vmRef ); // 1.5.0.0 (ge) added
6446 for(int i = 0; i < r.m_num; i++)
6447 {
6448 t_CKUINT len = 0;
6449
6450 while(!m_do_exit)
6451 {
6452 if(peek_buffer() == -1)
6453 break;
6454
6455 int c = (int)pull_buffer();
6456
6457 if(isdigit(c) || (len==0 && (c=='-' || c=='+')))
6458 {
6459 m_tmp_buf[len++] = c;
6460 }
6461 else if(len > 0)
6462 {
6463 m_tmp_buf[len++] = '\0';
6464 val = strtol((char *)m_tmp_buf, NULL, 10);
6465
6466 numRead++;
6467 array->push_back(val);
6468
6469 break;
6470 }
6471 }
6472 }
6473
6474 if(m_do_exit || m_eof)
6475 goto error;
6476
6477 r.m_num = numRead;
6478 r.m_val = (t_CKUINT) array;
6479 r.m_status = Request::RQ_STATUS_SUCCESS;
6480
6481 return TRUE;
6482
6483 error:
6484 CK_SAFE_DELETE(array);
6485 r.m_val = 0;
6486 r.m_status = Chuck_IO_Serial::Request::RQ_STATUS_FAILURE;
6487
6488 return TRUE;
6489 }
6490
6491 t_CKBOOL Chuck_IO_Serial::handle_byte(Chuck_IO_Serial::Request & r)
6492 {
6493 // binary
6494 int size = 1;
6495 t_CKUINT num = r.m_num;
6496 t_CKUINT val = 0;
6497
6498 if(size*num > m_tmp_buf_max)
6499 {
6500 int new_num = (int) floorf(((float)m_tmp_buf_max)/size);
6501 EM_log(CK_LOG_WARNING, "SerialIO.read_cb: error: request size %i too large (%i bytes), truncating to %i",
6502 num, size*num, new_num);
6503 num = new_num;
6504 }
6505
6506 t_CKINT len = buffer_bytes_to_tmp(num);
6507
6508 if(m_do_exit || m_eof)
6509 goto error;
6510
6511 r.m_num = len;
6512
6513 if(r.m_num == 1)
6514 val = m_tmp_buf[0];
6515 else
6516 {
6517 Chuck_ArrayInt * array = new Chuck_ArrayInt(FALSE, r.m_num);
6518 initialize_object( array, m_vmRef->env()->ckt_array, NULL, m_vmRef ); // 1.5.0.0 (ge) added
6519 for(int i = 0; i < r.m_num; i++)
6520 {
6521 array->set(i, m_tmp_buf[i]);
6522 }
6523
6524 val = (t_CKUINT) array;
6525 }
6526
6527 r.m_val = val;
6528 r.m_status = Request::RQ_STATUS_SUCCESS;
6529
6530 return TRUE;
6531
6532 error:
6533 r.m_val = 0;
6534 r.m_status = Chuck_IO_Serial::Request::RQ_STATUS_FAILURE;
6535
6536 return TRUE;
6537 }
6538
6539 t_CKBOOL Chuck_IO_Serial::handle_float_binary(Chuck_IO_Serial::Request & r)
6540 {
6541 // binary
6542 int size = 4; // SPENCERTODO: should these be based on arch (64 bit)?
6543 assert(sizeof(t_CKSINGLE) == 4);
6544
6545 t_CKUINT num = r.m_num;
6546
6547 if(size*num > m_tmp_buf_max)
6548 {
6549 int new_num = (int) floorf(((float)m_tmp_buf_max)/size);
6550 EM_log(CK_LOG_WARNING, "(SerialIO.read_cb): error: request size %i too large (%i bytes), truncating to %i",
6551 num, size*num, new_num);
6552 num = new_num;
6553 }
6554
6555 r.m_num = buffer_bytes_to_tmp(size*num)/size;
6556
6557 t_CKUINT val = 0;
6558 t_CKSINGLE * m_floats = (t_CKSINGLE *) m_tmp_buf;
6559
6560 Chuck_ArrayFloat * array = new Chuck_ArrayFloat(r.m_num);
6561 for(int i = 0; i < r.m_num; i++)
6562 {
6563 array->set(i, m_floats[i]);
6564 }
6565
6566 val = (t_CKUINT) array;
6567
6568 r.m_val = val;
6569 r.m_status = Request::RQ_STATUS_SUCCESS;
6570
6571 return TRUE;
6572 }
6573
6574 t_CKBOOL Chuck_IO_Serial::handle_int_binary(Chuck_IO_Serial::Request & r)
6575 {
6576 // binary
6577 int size = 4; // SPENCERTODO: should these be based on arch (64 bit)?
6578
6579 t_CKUINT num = r.m_num;
6580
6581 if(size*num > m_tmp_buf_max)
6582 {
6583 int new_num = (int) floorf(((float)m_tmp_buf_max)/size);
6584 EM_log(CK_LOG_WARNING, "SerialIO.read_cb: error: request size %i too large (%i bytes), truncating to %i",
6585 num, size*num, new_num);
6586 num = new_num;
6587 }
6588
6589 r.m_num = buffer_bytes_to_tmp(size*num)/size;
6590
6591 t_CKUINT val = 0;
6592 uint32_t * m_ints = (uint32_t *) m_tmp_buf;
6593
6594 Chuck_ArrayInt * array = new Chuck_ArrayInt(FALSE, r.m_num);
6595 initialize_object( array, m_vmRef->env()->ckt_array, NULL, m_vmRef ); // 1.5.0.0 (ge) added
6596 for(int i = 0; i < r.m_num; i++)
6597 {
6598 array->set(i, m_ints[i]);
6599 }
6600
6601 val = (t_CKUINT) array;
6602
6603 r.m_val = val;
6604 r.m_status = Request::RQ_STATUS_SUCCESS;
6605
6606 return TRUE;
6607 }
6608
6609
6610 void Chuck_IO_Serial::read_cb()
6611 {
6612 m_do_read_thread = TRUE;
6613
6614 m_write_thread = new XThread;
6615 #ifdef __PLATFORM_WINDOWS__
6616 m_read_thread->start((unsigned int (__stdcall *)(void*))shell_write_cb, this);
6617 #else
6618 m_write_thread->start(shell_write_cb, this);
6619 #endif
6620
6621 while(m_do_read_thread && !m_do_exit)
6622 {
6623 Request r;
6624 int num_responses = 0;
6625
6626 while(m_asyncRequests.get(r) && m_do_read_thread && !m_do_exit)
6627 {
6628 if( m_asyncResponses.atMaximum() )
6629 {
6630 EM_log(CK_LOG_HERALD, "SerialIO.read_cb: error: response buffer overflow, dropping read");
6631 continue;
6632 }
6633
6634 if(m_flags & Chuck_IO::TYPE_ASCII)
6635 {
6636 switch(r.m_type)
6637 {
6638 case TYPE_LINE:
6639 handle_line(r);
6640 break;
6641
6642 case TYPE_STRING:
6643 handle_string(r);
6644 break;
6645
6646 case TYPE_INT:
6647 handle_int_ascii(r);
6648 break;
6649
6650 case TYPE_FLOAT:
6651 handle_float_ascii(r);
6652 break;
6653
6654 default:
6655 // this shouldnt happen
6656 r.m_type = TYPE_NONE;
6657 r.m_num = 0;
6658 r.m_status = Request::RQ_STATUS_INVALID;
6659 r.m_val = 0;
6660 EM_log(CK_LOG_WARNING, "SerialIO.read_cb: error: invalid request");
6661 }
6662 }
6663 else
6664 {
6665 // binary
6666 switch(r.m_type)
6667 {
6668 case TYPE_BYTE:
6669 handle_byte(r);
6670 break;
6671 case TYPE_INT:
6672 handle_int_binary(r);
6673 break;
6674 case TYPE_FLOAT:
6675 handle_float_binary(r);
6676 break;
6677
6678 default:
6679 // this shouldnt happen
6680 r.m_type = TYPE_NONE;
6681 r.m_num = 0;
6682 r.m_status = Request::RQ_STATUS_INVALID;
6683 r.m_val = 0;
6684 EM_log(CK_LOG_WARNING, "SerialIO.read_cb: error: invalid request");
6685 }
6686 }
6687
6688 m_asyncResponses.put(r);
6689 num_responses++;
6690 }
6691
6692 if(m_asyncResponses.numElements() > 0)
6693 queue_broadcast(m_event_buffer);
6694
6695 ck_usleep(100);
6696 }
6697
6698 m_write_thread->wait(-1);
6699
6700 close_int();
6701 }
6702
6703 void Chuck_IO_Serial::write_cb()
6704 {
6705 m_do_write_thread = TRUE;
6706
6707 char *tmp_writethread_buf = new char[CHUCK_IO_DEFAULT_BUFSIZE];
6708 t_CKINT tmp_writethread_buf_max = CHUCK_IO_DEFAULT_BUFSIZE;
6709
6710 while(m_do_write_thread && !m_do_exit)
6711 {
6712 Request r;
6713
6714 while(m_asyncWriteRequests.get(r) && m_do_write_thread && !m_do_exit)
6715 {
6716 if(r.m_type == TYPE_WRITE)
6717 {
6718 int numBytes = 0;
6719 char c;
6720 while(m_writeBuffer.get(c))
6721 {
6722 tmp_writethread_buf[numBytes] = c;
6723 numBytes++;
6724 }
6725
6726 assert(numBytes < tmp_writethread_buf_max);
6727
6728 if(numBytes)
6729 {
6730 fwrite(tmp_writethread_buf, 1, numBytes, m_cfd);
6731 fflush(m_cfd);
6732 }
6733 }
6734 }
6735
6736 // todo: replace with semaphore?
6737 ck_usleep(100);
6738 }
6739
6740 delete[] tmp_writethread_buf;
6741 tmp_writethread_buf = NULL;
6742 }
6743
6744
6745 t_CKBOOL Chuck_IO_Serial::setBaudRate( t_CKUINT rate )
6746 {
6747 #ifndef __PLATFORM_WINDOWS__
6748 // set serial IO baud rate
6749 struct termios tios;
6750 tcgetattr(m_fd, &tios);
6751 cfsetispeed(&tios, rate);
6752 cfsetospeed(&tios, rate);
6753 tcsetattr(m_fd, TCSAFLUSH, &tios);
6754
6755 return TRUE;
6756 #else
6757 DCB dcb;
6758 COMMTIMEOUTS timeouts;
6759 HANDLE hFile = (HANDLE) _get_osfhandle(m_fd);
6760
6761 bool was_error = false;
6762 DWORD error_flag = 0;
6763
6764 if(!hFile)
6765 {
6766 goto error;
6767 }
6768
6769 if(!GetCommTimeouts(hFile, &timeouts))
6770 {
6771 goto error;
6772 }
6773
6774 timeouts.ReadIntervalTimeout = 10;
6775 timeouts.WriteTotalTimeoutConstant = 5000;
6776
6777 if(!SetCommTimeouts(hFile, &timeouts))
6778 {
6779 goto error;
6780 }
6781
6782 if(!GetCommState(hFile, &dcb))
6783 {
6784 goto error;
6785 }
6786
6787 // set baud rate
6788 dcb.BaudRate = rate;
6789
6790 // set important stuff
6791 dcb.ByteSize = 8;
6792 dcb.StopBits = ONESTOPBIT;
6793
6794 if(!SetCommState(hFile, &dcb))
6795 {
6796 //goto error;
6797 was_error = true;
6798 ClearCommError(hFile, &error_flag, NULL);
6799 }
6800
6801 // set everything else
6802 if(!GetCommState(hFile, &dcb))
6803 {
6804 goto error;
6805 }
6806
6807 dcb.fBinary = TRUE;
6808 dcb.fParity = FALSE;
6809 dcb.fOutxCtsFlow = FALSE;
6810 dcb.fOutxDsrFlow = FALSE;
6811 dcb.fDtrControl = DTR_CONTROL_ENABLE;
6812 dcb.fDsrSensitivity = FALSE;
6813 dcb.fTXContinueOnXoff = TRUE;
6814 dcb.fOutX = FALSE;
6815 dcb.fInX = FALSE;
6816 dcb.fErrorChar = FALSE;
6817 dcb.fNull = FALSE;
6818 dcb.fRtsControl = RTS_CONTROL_ENABLE;
6819 dcb.fAbortOnError = TRUE;
6820 dcb.Parity = NOPARITY;
6821
6822 if(!SetCommState(hFile, &dcb))
6823 {
6824 //goto error;
6825 was_error = true;
6826 ClearCommError(hFile, &error_flag, NULL);
6827 }
6828
6829 if(!was_error)
6830 return TRUE;
6831
6832 error:
6833
6834 DWORD error = GetLastError();
6835
6836 return FALSE;
6837
6838 #endif
6839 }
6840
6841
6842 t_CKUINT Chuck_IO_Serial::getBaudRate()
6843 {
6844 #ifndef __PLATFORM_WINDOWS__
6845 // set serial IO baud rate
6846 struct termios tios;
6847 tcgetattr(m_fd, &tios);
6848 speed_t rate = cfgetispeed(&tios);
6849 return rate;
6850 #else
6851
6852 DCB dcb;
6853 HANDLE hFile = (HANDLE) _get_osfhandle(m_fd);
6854
6855 if(!hFile)
6856 {
6857 goto error;
6858 }
6859
6860 if(!GetCommState(hFile, &dcb))
6861 {
6862 goto error;
6863 }
6864
6865 return dcb.BaudRate;
6866
6867 error:
6868 return 0;
6869
6870 #endif
6871 }
6872
6873
6874 //-----------------------------------------------------------------------------
6875 // serialio API
6876 //-----------------------------------------------------------------------------
6877 CK_DLL_SFUN( serialio_list );
6878 CK_DLL_ALLOC( serialio_alloc );
6879 CK_DLL_CTOR( serialio_ctor );
6880 CK_DLL_DTOR( serialio_dtor );
6881 CK_DLL_MFUN( serialio_open );
6882 CK_DLL_MFUN( serialio_close );
6883 CK_DLL_MFUN( serialio_ready );
6884 CK_DLL_MFUN( serialio_readLine );
6885 CK_DLL_MFUN( serialio_onLine );
6886 CK_DLL_MFUN( serialio_onByte );
6887 CK_DLL_MFUN( serialio_onBytes );
6888 CK_DLL_MFUN( serialio_onInts );
6889 CK_DLL_MFUN( serialio_onFloats );
6890 CK_DLL_MFUN( serialio_getLine );
6891 CK_DLL_MFUN( serialio_getByte );
6892 CK_DLL_MFUN( serialio_getBytes );
6893 CK_DLL_MFUN( serialio_getInts );
6894 CK_DLL_MFUN( serialio_setBaudRate );
6895 CK_DLL_MFUN( serialio_getBaudRate );
6896 CK_DLL_MFUN( serialio_writeByte );
6897 CK_DLL_MFUN( serialio_writeBytes );
6898
6899 CK_DLL_MFUN( serialio_flush )
6900 {
6901 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
6902
6903 cereal->flush();
6904 }
6905
6906
6907 //-----------------------------------------------------------------------------
6908 // name: init_class_serialio()
6909 // desc: ...
6910 //-----------------------------------------------------------------------------
6911 t_CKBOOL init_class_serialio( Chuck_Env * env )
6912 {
6913 // init as base class
6914 Chuck_DL_Func * func = NULL;
6915
6916 std::string doc = "serial input/output. popularly used to communicate with systems like Arduino.";
6917
6918 Chuck_Type * type = type_engine_import_class_begin( env, "SerialIO", "IO",
6919 env->global(), serialio_ctor, serialio_dtor, doc.c_str() );
6920 // TODO: ctor/dtor?
6921 if( !type )
6922 return FALSE;
6923
6924 // HACK; SPENCERTODO: better way to set this
6925 type->allocator = serialio_alloc;
6926
6927 // add list()
6928 func = make_new_sfun( "string[]", "list", serialio_list );
6929 func->doc = "get list of available serial devices.";
6930 if( !type_engine_import_sfun( env, func ) ) goto error;
6931
6932 func = make_new_mfun("int", "open", serialio_open);
6933 func->add_arg("int", "i");
6934 func->add_arg("int", "baud");
6935 func->add_arg("int", "mode");
6936 func->doc = "open serial device i with specified baud rate and mode (binary or ASCII).";
6937 if( !type_engine_import_mfun( env, func ) ) goto error;
6938
6939 func = make_new_mfun("void", "close", serialio_close);
6940 if( !type_engine_import_mfun( env, func ) ) goto error;
6941
6942 // add ready (data available)
6943 func = make_new_mfun("int", "dataAvailable", serialio_ready);
6944 if( !type_engine_import_mfun( env, func ) ) goto error;
6945
6946 // add readLine
6947 func = make_new_mfun("string", "readLine", serialio_readLine);
6948 if( !type_engine_import_mfun( env, func ) ) goto error;
6949
6950 // add onLine
6951 func = make_new_mfun("SerialIO", "onLine", serialio_onLine);
6952 func->doc = "wait for one line (ASCII mode only).";
6953 if( !type_engine_import_mfun( env, func ) ) goto error;
6954
6955 // add onByte
6956 func = make_new_mfun("SerialIO", "onByte", serialio_onByte);
6957 func->doc = "wait for one byte (binary mode only).";
6958 if( !type_engine_import_mfun( env, func ) ) goto error;
6959
6960 // add onBytes
6961 func = make_new_mfun("SerialIO", "onBytes", serialio_onBytes);
6962 func->add_arg("int", "num");
6963 func->doc = "wait for requested number of bytes (binary mode only).";
6964 if( !type_engine_import_mfun( env, func ) ) goto error;
6965
6966 // add onInts
6967 func = make_new_mfun("SerialIO", "onInts", serialio_onInts);
6968 func->add_arg("int", "num");
6969 func->doc = "wait for requested number of ints (ASCII or binary mode).";
6970 if( !type_engine_import_mfun( env, func ) ) goto error;
6971
6972 // add onInts
6973 func = make_new_mfun("SerialIO", "onFloats", serialio_onFloats);
6974 func->add_arg("int", "num");
6975 func->doc = "wait for requested number of floats (ASCII or binary mode).";
6976 if( !type_engine_import_mfun( env, func ) ) goto error;
6977
6978 // add getLine
6979 func = make_new_mfun("string", "getLine", serialio_getLine);
6980 func->doc = "get next requested line.";
6981 if( !type_engine_import_mfun( env, func ) ) goto error;
6982
6983 // add getByte
6984 func = make_new_mfun("int", "getByte", serialio_getByte);
6985 func->doc = "get next requested byte.";
6986 if( !type_engine_import_mfun( env, func ) ) goto error;
6987
6988 // add getBytes
6989 func = make_new_mfun("int[]", "getBytes", serialio_getBytes);
6990 func->doc = "get next requested number of bytes.";
6991 if( !type_engine_import_mfun( env, func ) ) goto error;
6992
6993 // add getInts
6994 func = make_new_mfun("int[]", "getInts", serialio_getInts);
6995 func->doc = "get next requested number of integers.";
6996 if( !type_engine_import_mfun( env, func ) ) goto error;
6997
6998 // add writeByte
6999 func = make_new_mfun("void", "writeByte", serialio_writeByte);
7000 func->add_arg("int", "b");
7001 func->doc = "write a single byte.";
7002 if( !type_engine_import_mfun( env, func ) ) goto error;
7003
7004 // add writeBytes
7005 func = make_new_mfun("void", "writeBytes", serialio_writeBytes);
7006 func->add_arg("int[]", "b");
7007 func->doc = "write array of bytes.";
7008 if( !type_engine_import_mfun( env, func ) ) goto error;
7009
7010 // add setBaudRate
7011 func = make_new_mfun("int", "baudRate", serialio_setBaudRate);
7012 func->add_arg("int", "r");
7013 func->doc = "set baud rate.";
7014 if( !type_engine_import_mfun( env, func ) ) goto error;
7015
7016 // add getBaudRate
7017 func = make_new_mfun("int", "baudRate", serialio_getBaudRate);
7018 func->doc = "get current baud rate.";
7019 if( !type_engine_import_mfun( env, func ) ) goto error;
7020
7021 // add getBaudRate
7022 func = make_new_mfun("void", "flush", serialio_flush);
7023 func->doc = "flush the IO buffer.";
7024 if( !type_engine_import_mfun( env, func ) ) goto error;
7025
7026 // add can_wait
7027 // func = make_new_mfun("int", "can_wait", serialio_canWait);
7028 // func->doc = "(internal) used by virtual machine for synthronization.";
7029 // if( !type_engine_import_mfun( env, func ) ) goto error;
7030
7031 // add file modes (moved into IO | 1.5.0.0 (ge))
7032 // type_engine_import_svar(env, "int", "BINARY", TRUE, (t_CKUINT) &Chuck_IO_File::TYPE_BINARY, "Binary mode");
7033 // type_engine_import_svar(env, "int", "ASCII", TRUE, (t_CKUINT) &Chuck_IO_File::TYPE_ASCII, "ASCII mode");
7034
7035 // add baud rate constants
7036 type_engine_import_svar(env, "int", "B2400", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_2400, "2400 baud");
7037 type_engine_import_svar(env, "int", "B4800", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_4800, "4800 baud");
7038 type_engine_import_svar(env, "int", "B9600", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_9600, "9600 baud");
7039 type_engine_import_svar(env, "int", "B19200", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_19200, "19200 baud");
7040 type_engine_import_svar(env, "int", "B38400", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_38400, "38400 baud");
7041 type_engine_import_svar(env, "int", "B7200", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_7200, "7200 baud");
7042 type_engine_import_svar(env, "int", "B14400", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_14400, "14400 baud");
7043 type_engine_import_svar(env, "int", "B28800", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_28800, "28800 baud");
7044 type_engine_import_svar(env, "int", "B57600", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_57600, "57600 baud");
7045 type_engine_import_svar(env, "int", "B76800", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_76800, "76800 baud");
7046 type_engine_import_svar(env, "int", "B115200", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_115200, "115200 baud");
7047 type_engine_import_svar(env, "int", "B230400", TRUE, (t_CKUINT) &Chuck_IO_Serial::CK_BAUD_230400, "230400 baud");
7048
7049 // add examples
7050 if( !type_engine_import_add_ex( env, "serial/byte.ck" ) ) goto error;
7051 if( !type_engine_import_add_ex( env, "serial/bytes.ck" ) ) goto error;
7052 if( !type_engine_import_add_ex( env, "serial/ints-bin.ck" ) ) goto error;
7053 if( !type_engine_import_add_ex( env, "serial/ints-ascii.ck" ) ) goto error;
7054 if( !type_engine_import_add_ex( env, "serial/lines.ck" ) ) goto error;
7055 if( !type_engine_import_add_ex( env, "serial/list.ck" ) ) goto error;
7056 if( !type_engine_import_add_ex( env, "serial/write-bytes.ck" ) ) goto error;
7057 if( !type_engine_import_add_ex( env, "serial/write.ck" ) ) goto error;
7058
7059 // end the class import
7060 type_engine_import_class_end( env );
7061
7062 return TRUE;
7063
7064 error:
7065
7066 // end the class import
7067 type_engine_import_class_end( env );
7068
7069 return FALSE;
7070 }
7071
7072
7073
7074 CK_DLL_SFUN( serialio_list )
7075 {
7076 vector<string> devices = SerialIOManager::availableSerialDevices();
7077
7078 // ISSUE: 64-bit
7079 Chuck_ArrayInt * array = new Chuck_ArrayInt(TRUE, 0);
7080 initialize_object( array, SHRED->vm_ref->env()->ckt_array, SHRED, VM );
7081
7082 for(vector<string>::iterator i = devices.begin(); i != devices.end(); i++)
7083 {
7084 Chuck_String * name = new Chuck_String(*i);
7085 initialize_object(name, SHRED->vm_ref->env()->ckt_string, SHRED, VM );
7086 array->push_back((t_CKUINT) name);
7087 }
7088
7089 RETURN->v_object = array;
7090 }
7091
7092
7093 CK_DLL_ALLOC( serialio_alloc )
7094 {
7095 return new Chuck_IO_Serial( SHRED->vm_ref );
7096 }
7097
7098 CK_DLL_CTOR( serialio_ctor )
7099 {
7100 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7101 SHRED->add_serialio(cereal);
7102 }
7103
7104 CK_DLL_DTOR( serialio_dtor )
7105 {
7106 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7107 if(cereal)
7108 {
7109 SHRED->remove_serialio(cereal);
7110 cereal->close();
7111 }
7112 }
7113
7114 CK_DLL_MFUN( serialio_open )
7115 {
7116 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7117 t_CKINT num = GET_NEXT_INT(ARGS);
7118 t_CKINT baud = GET_NEXT_INT(ARGS);
7119 t_CKINT mode = GET_NEXT_INT(ARGS);
7120
7121 RETURN->v_int = cereal->open(num, mode, baud);
7122 }
7123
7124 CK_DLL_MFUN( serialio_close )
7125 {
7126 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7127
7128 cereal->close();
7129 }
7130
7131 CK_DLL_MFUN( serialio_ready )
7132 {
7133 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7134
7135 RETURN->v_int = cereal->ready();
7136 }
7137
7138 CK_DLL_MFUN( serialio_readLine )
7139 {
7140 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7141
7142 RETURN->v_string = cereal->readLine();
7143 }
7144
7145
7146 CK_DLL_MFUN( serialio_onLine )
7147 {
7148 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7149
7150 cereal->readAsync(Chuck_IO_Serial::TYPE_LINE, 1);
7151
7152 RETURN->v_object = cereal;
7153 }
7154
7155
7156 CK_DLL_MFUN( serialio_onByte )
7157 {
7158 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7159
7160 cereal->readAsync(Chuck_IO_Serial::TYPE_BYTE, 1);
7161
7162 RETURN->v_object = cereal;
7163 }
7164
7165
7166 CK_DLL_MFUN( serialio_onBytes )
7167 {
7168 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7169
7170 t_CKINT num = GET_NEXT_INT(ARGS);
7171
7172 cereal->readAsync(Chuck_IO_Serial::TYPE_BYTE, num);
7173
7174 RETURN->v_object = cereal;
7175 }
7176
7177
7178 CK_DLL_MFUN( serialio_onInts )
7179 {
7180 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7181
7182 t_CKINT num = GET_NEXT_INT(ARGS);
7183
7184 cereal->readAsync(Chuck_IO_Serial::TYPE_INT, num);
7185
7186 RETURN->v_object = cereal;
7187 }
7188
7189
7190 CK_DLL_MFUN( serialio_onFloats )
7191 {
7192 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7193
7194 t_CKINT num = GET_NEXT_INT(ARGS);
7195
7196 cereal->readAsync(Chuck_IO_Serial::TYPE_FLOAT, num);
7197
7198 RETURN->v_object = cereal;
7199 }
7200
7201
7202 CK_DLL_MFUN( serialio_getLine )
7203 {
7204 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7205 RETURN->v_string = cereal->getLine();
7206 }
7207
7208
7209 CK_DLL_MFUN( serialio_getByte )
7210 {
7211 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7212 RETURN->v_int = cereal->getByte();
7213 }
7214
7215
7216 CK_DLL_MFUN( serialio_getBytes )
7217 {
7218 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7219 RETURN->v_object = cereal->getBytes();
7220 }
7221
7222
7223 CK_DLL_MFUN( serialio_getInts )
7224 {
7225 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7226 RETURN->v_object = cereal->getInts();
7227 }
7228
7229
7230 CK_DLL_MFUN( serialio_setBaudRate )
7231 {
7232 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7233 cereal->setBaudRate(GET_NEXT_INT(ARGS));
7234 RETURN->v_int = cereal->getBaudRate();
7235 }
7236
7237
7238 CK_DLL_MFUN( serialio_getBaudRate )
7239 {
7240 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7241 RETURN->v_int = cereal->getBaudRate();
7242 }
7243
7244
7245 CK_DLL_MFUN( serialio_writeByte )
7246 {
7247 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7248 t_CKINT byte = GET_NEXT_INT(ARGS);
7249 cereal->write(byte, 1);
7250 }
7251
7252
7253 CK_DLL_MFUN( serialio_writeBytes )
7254 {
7255 Chuck_IO_Serial * cereal = (Chuck_IO_Serial *) SELF;
7256 Chuck_ArrayInt * arr = (Chuck_ArrayInt *) GET_NEXT_OBJECT(ARGS);
7257 cereal->writeBytes(arr);
7258 }
7259
7260 CK_DLL_MFUN(serialio_canWait)
7261 {
7262 Chuck_IO_Serial *cereal = (Chuck_IO_Serial *) SELF;
7263 RETURN->v_int = cereal->can_wait();
7264 }
7265
7266 #endif // __DISABLE_SERIAL__