diff nodejs/mbfly_njs.cc @ 1067:7b4e80ab671a openvg

merge from default branch
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 01 Dec 2010 12:25:56 +0800
parents 88bd0eee2b00
children 3eaf708b52c3
line wrap: on
line diff
--- a/nodejs/mbfly_njs.cc	Mon Jul 19 15:44:49 2010 +0800
+++ b/nodejs/mbfly_njs.cc	Wed Dec 01 12:25:56 2010 +0800
@@ -1,95 +1,136 @@
+// -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
+// vim: sw=4:ts=8:sts=4
 #include <stdio.h>
+#include <string.h>
 #include <v8.h>
 
 extern "C" {
-#include "X_supp_njs.h"
+#include "njs_mb_supp.h"
 }
 
 #include "mbfly_njs.h"
 
 using namespace v8;
 
+/*! \defgroup xnjsmb_mb_rt JS binding for MB runtime.
+ * \ingroup xnjsmb
+ *
+ * @{
+ */
+static coord_t *
+xnjsmb_coord_new(njs_runtime_t *rt, coord_t *parent, const char **err) {
+    coord_t *coord;
+    redraw_man_t *rdman;
+
+    rdman = njs_mb_rdman(rt);
+    coord = rdman_coord_new(rdman, parent);
+    if(coord == NULL) {
+        *err = "Can not allocate a redraw_man_t";
+	return NULL;
+    }
+    rdman_coord_changed(rdman, coord);
+
+    return coord;
+}
+
+static void
+xnjsmb_mb_rt_objs_mod(Handle<Object> mbrt, Handle<Value> ret) {
+    Handle<Object> ret_obj = ret->ToObject();
+
+    SET(ret_obj, "mbrt", mbrt);
+}
+
+#define xnjsmb_auto_coord_new export_xnjsmb_auto_coord_new
+
+static void
+xnjsmb_redraw_changed(njs_runtime_t *rt) {
+    redraw_man_t *rdman;
+
+    rdman = njs_mb_rdman(rt);
+    rdman_redraw_changed(rdman);
+}
+
+static void
+xnjsmb_redraw_all(njs_runtime_t *rt) {
+    redraw_man_t *rdman;
+
+    rdman = njs_mb_rdman(rt);
+    rdman_redraw_all(rdman);
+}
+
+static void
+xnjsmb_handle_single_event(njs_runtime_t *rt, void *evt) {
+    njs_mb_handle_single_event(rt, evt);
+}
+
+static void
+xnjsmb_no_more_event(njs_runtime_t *rt) {
+    njs_mb_no_more_event(rt);
+}
+
+static njs_runtime_t *
+_njs_mb_new(Handle<Object> self, char *display_name,
+	      int width, int height) {
+    njs_runtime_t *obj;
+    subject_t *subject;
+    Handle<Value> subject_o;
+
+    obj = njs_mb_new(display_name, width, height);
+    WRAP(self, obj);		/* mkroot need a wrapped object, but
+				 * it is wrapped after returning of
+				 * this function.  So, we wrap it
+				 * here. */
+    xnjsmb_coord_mkroot(self);
+
+    subject = njs_mb_kbevents(obj);
+    subject_o = export_xnjsmb_auto_subject_new(subject);
+    SET(self, "kbevents", subject_o);
+
+    return obj;
+}
+
+static njs_runtime_t *
+_njs_mb_new_with_win(Handle<Object> self, void *display,
+		       long win) {
+    njs_runtime_t *obj;
+    subject_t *subject;
+    Handle<Value> subject_o;
+
+    obj = njs_mb_new_with_win(display, win);
+    WRAP(self, obj);		/* mkroot need a wrapped object, but
+				 * it is wrapped after returning of
+				 * this function.  So, we wrap it
+				 * here. */
+    xnjsmb_coord_mkroot(self);
+
+    subject = njs_mb_kbevents(obj);
+    subject_o = export_xnjsmb_auto_subject_new(subject);
+    SET(self, "kbevents", subject_o);
+
+    return obj;
+}
+
 /*! \defgroup njs_template_cb Callback functions for v8 engine and nodejs.
  *
  * @{
  */
 
-/*! \brief to Create a njs runtime object for MadButterfly.
- *
- * Three arguments are requried.  They are
- *   - display name,
- *   - width, and
- *   - height.
+/*
+ * Redirect following function to respective exported version from
+ * other modules.  Since gen_v8_binding.m4 make all functions static,
+ * we need a exported version to call them indrectly from other
+ * modules.
  */
-static Handle<Value>
-xnjsmb_new(const Arguments &args) {
-    HandleScope scope;
-    int argc;
-    Handle<Value> exc;
-    njs_runtime_t *rt;
-    char *display_name;
-    int width, height;
-    Handle<Object> self;
-
-    argc = args.Length();
-    if(argc != 3) {
-	exc = Exception::Error(String::New("Need 3 arguments."));
-	return ThrowException(exc);
-    }
-
-    if(!args[0]->IsString() || !args[1]->IsInt32() || !args[2]->IsInt32()) {
-	exc = Exception::Error(String::New("Invalid argument type."));
-	return ThrowException(exc);
-    }
-    
-    String::Utf8Value disp_utf8(args[0]->ToString());
-    display_name = *disp_utf8;
-    width = args[1]->Int32Value();
-    height = args[2]->Int32Value();
-    rt = X_njs_MB_new(display_name, width, height);
+#define xnjsmb_auto_path_new export_xnjsmb_auto_path_new
+#define xnjsmb_auto_stext_new export_xnjsmb_auto_stext_new
+#define xnjsmb_auto_image_new export_xnjsmb_auto_image_new
+#define xnjsmb_auto_rect_new export_xnjsmb_auto_rect_new
+#define xnjsmb_auto_paint_color_new export_xnjsmb_auto_paint_color_new
+#define xnjsmb_auto_paint_image_new export_xnjsmb_auto_paint_image_new
+#define xnjsmb_auto_paint_linear_new export_xnjsmb_auto_paint_linear_new
+#define xnjsmb_auto_paint_radial_new export_xnjsmb_auto_paint_radial_new
 
-    self = args.This();
-    WRAP(self, rt);
-    xnjsmb_coord_mkroot(self);
-    
-    X_njs_MB_init_handle_connection(rt);
-
-    return Null();
-}
-
-static Handle<Value>
-xnjsmb_handle_connection(const Arguments &args) {
-}
-
-static Handle<Value>
-xnjsmb_rt_redraw_changed(const Arguments &args) {
-    Handle<Object> self = args.This();
-    njs_runtime_t *rt;
-    redraw_man_t *rdman;
-    
-    rdman = xnjsmb_rt_rdman(self);
-    rdman_redraw_changed(rdman);
-    
-    rt = (njs_runtime_t *)UNWRAP(self);
-    X_njs_MB_flush(rt);
-    
-    return Null();
-}
-
-static Handle<Value>
-xnjsmb_rt_redraw_all(const Arguments &args) {
-    Handle<Object> self = args.This();
-    njs_runtime_t *rt;
-    redraw_man_t *rdman;
-    
-    rdman = xnjsmb_rt_rdman(self);
-    rdman_redraw_all(rdman);
-    
-    rt = (njs_runtime_t *)UNWRAP(self);
-    X_njs_MB_flush(rt);
-    
-    return Null();
-}
+#include "mbfly_njs-inc.h"
 
 /* @} */
 
@@ -102,8 +143,8 @@
     redraw_man_t *rdman;
 
     rt = (njs_runtime_t *)UNWRAP(mbrt);
-    rdman = X_njs_MB_rdman(rt);
-    
+    rdman = njs_mb_rdman(rt);
+
     return rdman;
 }
 
@@ -126,28 +167,25 @@
     /*
      * Initialize template for MadButterfly runtime objects.
      */
-    mb_rt_func = FunctionTemplate::New(xnjsmb_new);
-    mb_rt_func->SetClassName(String::New("mb_rt"));
-    
-    rt_instance_temp = mb_rt_func->InstanceTemplate();
-    rt_instance_temp->SetInternalFieldCount(1);
-    
-    rt_proto_temp = mb_rt_func->PrototypeTemplate();
-    func = FunctionTemplate::New(xnjsmb_coord_new);
-    SET(rt_proto_temp, "coord_new", func);
-
-    func = FunctionTemplate::New(xnjsmb_rt_redraw_changed);
-    SET(rt_proto_temp, "redraw_changed", func);
-
-    func = FunctionTemplate::New(xnjsmb_rt_redraw_all);
-    SET(rt_proto_temp, "redraw_all", func);
+    xnjsmb_auto_mb_rt_init();
+    xnjsmb_auto_mb_rt_display_init();
+    xnjsmb_auto_mb_rt_with_win_init();
+    njs_mb_reg_timer_man();
+    njs_mb_reg_IO_man();
 
     /*
      * Add properties to mb_rt templates for other modules.
      */
-    xnjsmb_shapes_init_mb_rt_temp(mb_rt_func);
-    xnjsmb_paints_init_mb_rt_temp(mb_rt_func);
-    xnjsmb_font_init_mb_rt_temp(mb_rt_func);
-    
-    target->Set(String::New("mb_rt"), mb_rt_func->GetFunction());    
+    xnjsmb_shapes_init_mb_rt_temp(xnjsmb_auto_mb_rt_temp);
+    xnjsmb_paints_init_mb_rt_temp(xnjsmb_auto_mb_rt_temp);
+    xnjsmb_font_init_mb_rt_temp(xnjsmb_auto_mb_rt_temp);
+    xnjsmb_img_ldr_init_mb_rt_temp(target);
+    xnjsmb_observer_init();
+
+    target->Set(String::New("mb_rt"),
+		xnjsmb_auto_mb_rt_temp->GetFunction());
+    target->Set(String::New("mb_rt_with_win"),
+		xnjsmb_auto_mb_rt_with_win_temp->GetFunction());
 }
+
+/* @} */