diff --git a/scripts/javascript/examples/mouse.js b/scripts/javascript/examples/mouse.js index 83a5183a..51f42400 100644 --- a/scripts/javascript/examples/mouse.js +++ b/scripts/javascript/examples/mouse.js @@ -1,13 +1,23 @@ -m = new MouseController(); -register_controller(m); +geo = new GeometryLayer(); +geo.activate(true); +geo.color(255,255,255,255); +add_layer(geo); + + +m = new MouseController(); +m.activate(true); // MouseController.button(button, state, x, y) m.button = function(b, s, x, y) { - echo("b"+b+" s"+s+" x"+x+" y"+y); + // echo("b"+b+" s"+s+" x"+x+" y"+y); this.grab(s); } // MouseController.motion(buttonmask, x, y, xrel, yrel) m.motion = function(b, x, y, dx, dy) { - echo("b"+b+" x"+x+" y"+y+" dx"+dx+" dy"+dy); + geo.pixel(x,y); + // geo.ellipse_fill(x,y,dx,dy); + // echo("b"+b+" x"+x+" y"+y+" dx"+dx+" dy"+dy); } + +register_controller(m); diff --git a/src/console_widgets_ctrl.cpp b/src/console_widgets_ctrl.cpp index 61948ff4..2690e045 100644 --- a/src/console_widgets_ctrl.cpp +++ b/src/console_widgets_ctrl.cpp @@ -228,7 +228,7 @@ bool SlwSelector::refresh() { /////////////// // layer print color = LAYERS_COLOR; - putnch(tmp, 1, 1, 0); + putnch(tmp, 1, 0, 0); if(env->layers.len()) { diff --git a/src/controller.cpp b/src/controller.cpp index 0bc12bca..fa4ccd8b 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -184,35 +184,24 @@ int Controller::JSCall(const char *funcname, int argc, const char *format, ...) /* less bloat but this only works with 4 byte argv values */ -int Controller::JSCall(const char *funcname, int argc, jsval *argv, JSBool *res) { +int Controller::JSCall(const char *funcname, int argc, jsval *argv) { + JSObject *objp = NULL; jsval fval = JSVAL_VOID; jsval ret = JSVAL_VOID; - - func("%s calling method %s.%s()", __func__, name, funcname); - JS_GetProperty(jsenv, jsobj, funcname, &fval); - if(!JSVAL_IS_VOID(fval)) { - - *res = JS_CallFunctionValue(jsenv, jsobj, fval, argc, argv, &ret); - // if (*res) { - // if(!JSVAL_IS_VOID(ret)) { - JSBool ok; - JS_ValueToBoolean(jsenv, ret, &ok); - - if (ok) { // JSfunc returned 'true', so event is done - func("callback function executed, returned true"); - return 1; - } else { - func("callback function executed, returned false"); - return 0; - } - - // } - - // } - // warning("JS_CallFunctionValue returned NULL in %s",__FUNCTION__); - // return 0; // requeue event for next controller + JSBool res; + + func("calling js %s.%s()", name, funcname); + res = JS_GetMethod(jsenv, jsobj, funcname, &objp, &fval); + if(!res || JSVAL_IS_VOID(fval)) { + error("method %s not found in MouseController", funcname); + return(0); + } + + res = JS_CallFunctionValue(jsenv, jsobj, fval, argc, argv, &ret); + if(res == JS_FALSE) { + error("%s : failed call", __PRETTY_FUNCTION__); + return(0); } - warning("no callback found, function name unresolved by JS_GetProperty"); - return 0; // no callback, redo on next controller + return(1); } diff --git a/src/freej.cpp b/src/freej.cpp index 2c45eb5b..1834954d 100644 --- a/src/freej.cpp +++ b/src/freej.cpp @@ -267,7 +267,7 @@ int scripts(char *path) { #ifndef HAVE_DARWIN int main (int argc, char **argv) { Layer *lay = NULL; - Console *con; + Console *con = NULL; freej = new Context(); @@ -389,7 +389,7 @@ int main (int argc, char **argv) { /* quit */ - delete con; + if(con) delete con; delete freej; exit(1); diff --git a/src/include/controller.h b/src/include/controller.h index c58eefb1..6b4402a9 100644 --- a/src/include/controller.h +++ b/src/include/controller.h @@ -66,7 +66,7 @@ class Controller: public Entry { jsval lol; // TODO: eliminate runtime resolution -> C++ overhead alert! - int JSCall(const char *funcname, int argc, jsval *argv, JSBool *res); + int JSCall(const char *funcname, int argc, jsval *argv); int JSCall(const char *funcname, int argc, const char *format, ...); }; diff --git a/src/include/mouse_ctrl.h b/src/include/mouse_ctrl.h index f539482d..e7375410 100644 --- a/src/include/mouse_ctrl.h +++ b/src/include/mouse_ctrl.h @@ -38,6 +38,13 @@ class MouseController : public Controller { void grab(bool state); + private: + + JSObject *objp; + JSBool res; + + jsval motion_f; + jsval button_f; }; #endif diff --git a/src/include/trigger_ctrl.h b/src/include/trigger_ctrl.h index ce17cbc0..b867c8e9 100644 --- a/src/include/trigger_ctrl.h +++ b/src/include/trigger_ctrl.h @@ -32,6 +32,9 @@ class TriggerController : public Controller { int poll(); virtual int dispatch(); + + private: + jsval frame_func; }; #endif diff --git a/src/midi_ctrl.cpp b/src/midi_ctrl.cpp index c868684d..7e9a795e 100644 --- a/src/midi_ctrl.cpp +++ b/src/midi_ctrl.cpp @@ -156,62 +156,59 @@ int MidiController::dispatch() { int MidiController::event_ctrl(int channel, int param, int value) { func("midi Control event on Channel\t%2d: %5d %5d (param/value)", channel, param, value); - JSBool ret = JS_FALSE; if (jsenv == NULL) { error("Midi handle action: jsobj is null"); - return ret; + return(0); } jsval js_data[] = { channel, param, value }; - JSCall("event_ctrl", 3, js_data, &ret); - return ret; + JSCall("event_ctrl", 3, js_data); + return(1); } int MidiController::event_pitch(int channel, int param, int value) { func("midi Pitchbender event on Channel\t%2d: %5d %5d ", channel, param, value); - JSBool ret = JS_FALSE; if (jsenv == NULL) { error("Midi handle action: jsobj is null"); - return ret; + return(0); } jsval js_data[] = { channel, param, value }; - JSCall("event_pitch", 3, js_data, &ret); - return ret; + JSCall("event_pitch", 3, js_data); + return(1); } int MidiController::event_noteon(int channel, int note, int velocity) { func("midi Note On event on Channel\t%2d: %5d %5d ", channel, note, velocity); - JSBool ret = JS_FALSE; if (jsenv == NULL) { error("Midi handle action: jsobj is null"); - return ret; + return(0); } jsval js_data[] = { channel, note, velocity }; - JSCall("event_noteon", 3, js_data, &ret); - return ret; + JSCall("event_noteon", 3, js_data); + return(1); } int MidiController::event_noteoff(int channel, int note, int velocity) { func("midi Note Off event on Channel\t%2d: %5d ", channel, note); - JSBool ret = JS_FALSE; + if (jsenv == NULL) { error("Midi handle action: jsobj is null"); - return ret; + return(0); } + jsval js_data[] = { channel, note, velocity }; - JSCall("event_noteoff", 3, js_data, &ret); - return ret; + JSCall("event_noteoff", 3, js_data); + return(1); } int MidiController::event_pgmchange(int channel, int param, int value) { func("midi PGM change event on Channel\t%2d: %5d %5d ", channel, param, value); - JSBool ret = JS_FALSE; if (jsenv == NULL) { error("Midi handle action: jsobj is null"); - return ret; + return(0); } jsval js_data[] = { channel, param, value }; - JSCall("event_pgmchange", 3, js_data, &ret); - return ret; + JSCall("event_pgmchange", 3, js_data); + return(1); } /* diff --git a/src/mouse_ctrl.cpp b/src/mouse_ctrl.cpp index e87b91ef..cd1a09c0 100644 --- a/src/mouse_ctrl.cpp +++ b/src/mouse_ctrl.cpp @@ -1,5 +1,6 @@ -/* FreeJ - * (c) Copyright 2001-2007 Denis Roio aka jaromil +/* FreeJ - Mouse controller + * + * (c) Copyright 2008 Christoph Rudorff * * This source code is free software; you can redistribute it and/or * modify it under the terms of the GNU Public License as published @@ -77,6 +78,10 @@ JS(js_mouse_grab) { MouseController::MouseController() :Controller() { set_name("Mouse"); + + motion_f = NULL; + button_f = NULL; + objp = NULL; } MouseController::~MouseController() { @@ -100,8 +105,25 @@ MouseController::~MouseController() { // } int MouseController::poll() { - poll_sdlevents(SDL_MOUSEEVENTMASK); // calls dispatch() foreach SDL_Event - return 0; + + if(!motion_f) { // find javascript function pointer + res = JS_GetMethod(jsenv, jsobj, "motion", &objp, &motion_f); + if(!res || JSVAL_IS_VOID(motion_f)) { + error("motion method not found in MouseController"); + return(-1); + } + } + + if(!button_f) { // find javascript function pointer + res = JS_GetMethod(jsenv, jsobj, "button", &objp, &button_f); + if(!res || JSVAL_IS_VOID(button_f)) { + error("button method not found in MouseController"); + return(-1); + } + } + + poll_sdlevents(SDL_MOUSEEVENTMASK); // calls dispatch() foreach SDL_Event + return 0; } /* @@ -121,15 +143,40 @@ typedef struct{ */ int MouseController::motion(int state, int x, int y, int xrel, int yrel) { - JSBool ret= JS_TRUE; - jsval js_data[] = {state, x, y, xrel, yrel}; - return JSCall("motion", 5, js_data, &ret); + jsval ret = JSVAL_VOID; + JSBool res; + + jsval js_data[] = { + INT_TO_JSVAL(state), + INT_TO_JSVAL(x), INT_TO_JSVAL(y), + INT_TO_JSVAL(xrel), INT_TO_JSVAL(yrel) + }; + res = JS_CallFunctionValue(jsenv, jsobj, motion_f, 5, js_data, &ret); + if (res == JS_FALSE) { + error("mouse motion() function undefined"); + motion_f = NULL; + return(0); + } + return(1); } int MouseController::button(int button, int state, int x, int y) { - JSBool ret= JS_TRUE; - jsval js_data[] = {button, state, x, y}; - return JSCall("button", 4, js_data, &ret); + jsval ret = JSVAL_VOID; + JSBool res; + + jsval js_data[] = { + INT_TO_JSVAL(button), + INT_TO_JSVAL(state), + INT_TO_JSVAL(x), + INT_TO_JSVAL(y) + }; + res = JS_CallFunctionValue(jsenv, jsobj, button_f, 4, js_data, &ret); + if (res == JS_FALSE) { + error("mouse button() function undefined"); + button_f = NULL; + return(0); + } + return(1); } int MouseController::dispatch() { diff --git a/src/osc_ctrl.cpp b/src/osc_ctrl.cpp index 6dfad3fe..3feecb25 100644 --- a/src/osc_ctrl.cpp +++ b/src/osc_ctrl.cpp @@ -143,7 +143,6 @@ int osc_command_handler(const char *path, const char *types, int OscController::dispatch() { jsval ret = JSVAL_VOID; int res; - JSBool ok; int c = 0; // execute pending comamnds (javascript calls) JsCommand *jscmd = (JsCommand*) commands_pending.begin(); @@ -153,8 +152,9 @@ int OscController::dispatch() { // (jsenv, jsobj, jscmd->function, jscmd->argc, jscmd->argv, &ret); func("OSC controller dispatching %s(%s)", jscmd->name, jscmd->format); - res = Controller::JSCall(jscmd->name, jscmd->argc, jscmd->argv, &ok); - if (ok) func("OSC dispatched call to %s", jscmd->name); + res = Controller::JSCall(jscmd->name, jscmd->argc, jscmd->argv); + if (res) func("OSC dispatched call to %s", jscmd->name); + else error("OSC failed JSCall to %s", jscmd->name); free(jscmd->argv); // must free previous callod on argv diff --git a/src/trigger_ctrl.cpp b/src/trigger_ctrl.cpp index 4c376603..35be110e 100644 --- a/src/trigger_ctrl.cpp +++ b/src/trigger_ctrl.cpp @@ -1,5 +1,6 @@ -/* FreeJ - * (c) Copyright 2001-2007 Denis Roio aka jaromil +/* FreeJ - Trigger controller + * + * (c) Copyright 2007 Christoph Rudorff * * This source code is free software; you can redistribute it and/or * modify it under the terms of the GNU Public License as published @@ -20,8 +21,6 @@ */ -// I used a Logitech WingMan here. It got 2 joys and 7 Buttons - #include #include @@ -49,37 +48,40 @@ JSFunctionSpec js_trigger_ctrl_methods[] = { TriggerController::TriggerController() :Controller() { set_name("Trigger"); + frame_func = NULL; } TriggerController::~TriggerController() { } int TriggerController::poll() { + + + if(!frame_func) { + JSObject *objp = NULL; + JSBool res; + res = JS_GetMethod(jsenv, jsobj, "frame", &objp, &frame_func); + if(!res || JSVAL_IS_VOID(frame_func)) { + error("method frame not found in TriggerController"); + return(-1); + } + } + return dispatch(); } int TriggerController::dispatch() { - jsval fval = JSVAL_VOID; + jsval ret = JSVAL_VOID; - JSObject *objp = NULL; JSBool res; - res = JS_GetMethod(env->js->global_context, jsobj, "frame", &objp, &fval); - - if(!res || JSVAL_IS_VOID(fval)) { - error("method frame not found in TriggerController"); - // return false; + res = JS_CallFunctionValue(jsenv, jsobj, frame_func, 0, NULL, &ret); + if (res == JS_FALSE) { + error("trigger call frame() failed, deactivate ctrl"); + frame_func = NULL; + active = false; } - - if(!JSVAL_IS_VOID(fval)) { - res = JS_CallFunctionValue(jsenv, jsobj, fval, 0, NULL, &ret); - if (res == JS_FALSE) { - error("trigger call frame() failed, deactivate ctrl"); - active = false; - } else { - //func("trigger frame() called"); - } - } + return(1); } diff --git a/src/vimo_ctrl.cpp b/src/vimo_ctrl.cpp index 90fbf3d0..9597b0de 100644 --- a/src/vimo_ctrl.cpp +++ b/src/vimo_ctrl.cpp @@ -1,5 +1,6 @@ -/* FreeJ - * (c) Copyright 2006 Denis Roio aka jaromil +/* FreeJ - serial Video Mouse jogger controller + * + * (c) Copyright 2008 Christoph Rudorff * * This source code is free software; you can redistribute it and/or * modify it under the terms of the GNU Public License as published @@ -15,7 +16,6 @@ * this source code; if not, write to: * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * - * serial VideoMouse Controller by MrGoil (c) 2008 * */