Exemple #1
0
    File.prototype.readLine = function(max)
    {
        var str;
        var len;
        var limit = (max > -1);

        if (!limit)
            max = this.buf_size;
        else if (max > this.buf_size)
            this.resizeBuf(max);

        var line = c.fgets(this.buffer, max, this.ptr);

        // Handle cases where line fits within buf_size/max or could not be read
        if (ffi.isNullPtr(line))
            return '';

        len = c.strlen(line);

        if ($ir_load_u8(line, len - 1) === 10 || limit)
            return ffi.string(line, len);

        // Handle cases where line does not fit within buf_size
        str = ffi.string(line, len);

        do {
            line = c.fgets(this.buffer, max, this.ptr);
            if (ffi.isNullPtr(line))
                return str;
            len = c.strlen(line);
            str += ffi.string(line, len);
        } while ($ir_load_u8(line, len - 1) !== 10);

        return str;
    };
Exemple #2
0
 /**
 Get an environmental variable
 */
 function getenv(name)
 {
     var c_name = ffi.cstr(name);
     var result = c.getenv(c_name);
     c.free(c_name);
     return ffi.string(result);
 }
Exemple #3
0
 io.tmpname = function()
 {
     var buf = c.malloc(128);
     var name = c.tmpnam(buf);
     if(ffi.isNullPtr(name))
         throw 'Unable to get temp name.';
     name = ffi.string(buf);
     c.free(buf);
     return name;
 };
Exemple #4
0
    File.prototype.read = function(max)
    {
        var str;
        var len;
        var limit = (max > -1);

        // Handle read(size)
        if (limit)
        {
            if (max > this.buf_size)
                this.resizeBuf(max);
            len = c.fread(this.buffer, 1, max, this.ptr);
            return ffi.string(this.buffer, len);
        }

        // Handle read() entire file
        max = this.buf_size;
        str = '';
        do {
            len = c.fread(this.buffer, 1, max, this.ptr);
            str += ffi.string(this.buffer, len);
        } while (c.feof(this.ptr) === 0);
        return str;
    };
Exemple #5
0
    WindowProto.show = function()
    {
        // canvas/drawing
        var draw = true;
        var display = this.display;
        var canvas = this.canvas;
        var window = this.id;
        var frame_rate = this.frame_rate;
        // TODO: may need to check these each loop in case of resize
        var width = this.width;
        var height = this.height;
        // timing
        var work_time;
        var timeout;
        // events
        var event = Xlib.XEvent();
        var event_type;
        var key_sym;
        var key_name_c;
        var key_name;
        var e = event.ptr;
        // handlers
        var key_funs = this.key_funs;
        var render_funs = this.render_funs;
        var key_funs_i = 0;
        var render_funs_i = 0;

        // event loop
        while (draw)
        {
            // work time includes rendering and time handling events
            work_time = $ir_get_time_ms();

            key_funs_i = key_funs.length;
            render_funs_i = render_funs.length;

            // render
            while (render_funs_i > 0)
            {
                render_funs[--render_funs_i](canvas);
            }

            // Copy from Canvas buffer
            Xlib.XCopyArea(display, canvas.id,
                           window, canvas.gc,
                           0, 0, width, height, 0, 0
                          );

            // handle events
            while (Xlib.XPending(display) > 0)
            {
                // get event
                Xlib.XNextEvent(display, e);

                // dispatch on event type
                event_type = event.get_type();

                if (event_type === XEvents.Expose)
                {
                    // Copy from Canvas buffer
                    Xlib.XCopyArea(display, canvas.id,
                                   window, canvas.gc,
                                   0, 0, width, height, 0, 0
                                  );
                }
                else if (event_type === XEvents.KeyPress)
                {
                    // TODO: index? change to number?
                    key_sym = Xlib.XLookupKeysym(e, 0);
                    key_name_c = Xlib.XKeysymToString(key_sym);
                    key_name = ffi.string(key_name_c);

                    while (key_funs_i > 0)
                    {
                        key_funs[--key_funs_i](canvas, key_name);
                    }
                }
                else if (event_type === XEvents.ClientMessage)
                {
                    // TODO: Should check here for other client message types -
                    // for now we just care about the window closing
                    draw = false;
                }
            }

            // Calculate how long to wait
            work_time = $ir_get_time_ms() - work_time;
            timeout = 1000 / frame_rate - work_time;
            if (timeout < 0)
                timeout = 0;
            else if ($ir_is_float64(timeout))
                timeout = $ir_f64_to_i32(timeout);

            // Just sleep for a bit to not grind the CPU
            // TODO: eventually this should be more clever;
            // polling on the X fd or hooking into some higgs
            // event system so that all the libs like this can
            // be used together
            poll(CNULL, 0, timeout);
        }

        this.close();
    };
Exemple #6
0
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
*  NOT LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
*  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/

var test = require('lib/test');
var console = require('lib/console');
var ffi = require('lib/ffi');
var c = ffi.c;

// JS <=> C string conversion
assert(ffi.string(ffi.cstr('foo')) == 'foo');
assert(ffi.string(ffi.cstr('f' + 'oo')) == 'foo');

// Test array wrappers
c.cdef("\
       int TestIntArray[3];\
");

assertEq(c.TestIntArray.toString(), "[ 1, 2, 3 ]");
assertEqArray(c.TestIntArray.toJS(), [1,2,3]);

// Test struct wrappers
c.cdef("\
       struct CustomerStruct { int num; double balance; char name[10]; };\
       typedef struct CustomerStruct Customer;\
       Customer TestCustomer;\
Exemple #7
0
// Test enum wrappers

c.cdef("\
       enum Charms { HEARTS, STARS, HORSESHOES };\
");

assertEq(c.Charms.HEARTS, 0);
assertEq(c.Charms.STARS, 1);
assertEq(c.Charms.HORSESHOES, 2);

// Test string wrapping
c.cdef("\
       char *getTestString();\
");

assertEq(ffi.string(c.getTestString()), "Hello World!");

// Test os name
var os = ffi.os;
assertTrue(os === "LINUX" || os === "BSD" || os === "OSX");

// issue #102 regression
c.cdef(`
       typedef unsigned long long  __uint64_t;
       typedef unsigned int        __uint32_t;
       typedef unsigned short      __uint16_t;
       typedef unsigned char       __uint8_t;
       struct direntBSD {
           __uint64_t d_fileno;
           __uint16_t d_seekoff;
           __uint16_t d_reclen;