[cogl/wip/cogl-sharp: 19/42] cogl-sharp: First shot at generating methods, starting with FrameBuffer



commit ba0145a5d1f0a9a77bcb03c4fd67762e2d2dcead
Author: Damien Lespiau <damien lespiau intel com>
Date:   Mon Oct 1 01:32:56 2012 +0100

    cogl-sharp: First shot at generating methods, starting with FrameBuffer
    
    parse-gir.py has gained some extra powers to generate the binding code
    for methods.  FrameBuffer has been selected to be the first class that
    benefit from it.
    
    parse-gir.py will shout as much as possible about things it cannot do
    yet and will skip those unsupported methods.
    
    Generated code is kept in tree so the C# bindings can be compiled
    without gobject-introspection.

 cogl-sharp/BufferBit.cs    |    2 +-
 cogl-sharp/FrameBuffer.cs  |   13 +--
 cogl-sharp/Makefile.am     |    1 +
 cogl-sharp/_FrameBuffer.cs |  306 ++++++++++++++++++++++++++++++++++++++++++++
 cogl-sharp/parse-gir.py    |  212 +++++++++++++++++++++++++++++-
 5 files changed, 515 insertions(+), 19 deletions(-)
---
diff --git a/cogl-sharp/BufferBit.cs b/cogl-sharp/BufferBit.cs
index ad1a4d6..012038b 100644
--- a/cogl-sharp/BufferBit.cs
+++ b/cogl-sharp/BufferBit.cs
@@ -1,4 +1,4 @@
-/* This file is generated by parse-gir.py, do not hand edit */
+/* This file has been generated by parse-gir.py, do not hand edit */
 using System;
 
 namespace Cogl
diff --git a/cogl-sharp/FrameBuffer.cs b/cogl-sharp/FrameBuffer.cs
index a387655..6eace5f 100644
--- a/cogl-sharp/FrameBuffer.cs
+++ b/cogl-sharp/FrameBuffer.cs
@@ -28,20 +28,9 @@ using System.Runtime.InteropServices;
 
 namespace Cogl
 {
-    public class FrameBuffer : Cogl.Object
+    public partial class FrameBuffer : Cogl.Object
     {
         public FrameBuffer(IntPtr h) : base(h) {}
         public FrameBuffer() {}
-
-        [DllImport("cogl2.dll")]
-        private static extern void cogl_framebuffer_clear(IntPtr fb,
-                                                          BufferBit buffers,
-                                                          ref Color color);
-
-        public void Clear(BufferBit buffers, ref Color color)
-        {
-            cogl_framebuffer_clear(handle, buffers, ref color);
-        }
     }
-
 }
diff --git a/cogl-sharp/Makefile.am b/cogl-sharp/Makefile.am
index f9395e5..387b97c 100644
--- a/cogl-sharp/Makefile.am
+++ b/cogl-sharp/Makefile.am
@@ -13,6 +13,7 @@ sources =		\
 	Color.cs	\
 	Context.cs	\
 	FrameBuffer.cs	\
+	_FrameBuffer.cs	\
 	Matrix.cs	\
 	Object.cs	\
 	OnScreen.cs	\
diff --git a/cogl-sharp/_FrameBuffer.cs b/cogl-sharp/_FrameBuffer.cs
new file mode 100644
index 0000000..bdd7907
--- /dev/null
+++ b/cogl-sharp/_FrameBuffer.cs
@@ -0,0 +1,306 @@
+/* This file has been generated by parse-gir.py, do not hand edit */
+using System;
+using System.Runtime.InteropServices;
+
+namespace Cogl
+{
+    public partial class FrameBuffer
+    {
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_clear(IntPtr o, BufferBit buffers, ref Color color);
+
+        public void Clear(BufferBit buffers, ref Color color)
+        {
+            cogl_framebuffer_clear(handle, buffers, ref color);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_clear4f(IntPtr o, BufferBit buffers, float red, float green, float blue, float alpha);
+
+        public void Clear4f(BufferBit buffers, float red, float green, float blue, float alpha)
+        {
+            cogl_framebuffer_clear4f(handle, buffers, red, green, blue, alpha);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_discard_buffers(IntPtr o, BufferBit buffers);
+
+        public void DiscardBuffers(BufferBit buffers)
+        {
+            cogl_framebuffer_discard_buffers(handle, buffers);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_finish(IntPtr o);
+
+        public void Finish()
+        {
+            cogl_framebuffer_finish(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_frustum(IntPtr o, float left, float right, float bottom, float top, float z_near, float z_far);
+
+        public void Frustum(float left, float right, float bottom, float top, float z_near, float z_far)
+        {
+            cogl_framebuffer_frustum(handle, left, right, bottom, top, z_near, z_far);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_alpha_bits(IntPtr o);
+
+        public int GetAlphaBits()
+        {
+            return cogl_framebuffer_get_alpha_bits(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_blue_bits(IntPtr o);
+
+        public int GetBlueBits()
+        {
+            return cogl_framebuffer_get_blue_bits(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_green_bits(IntPtr o);
+
+        public int GetGreenBits()
+        {
+            return cogl_framebuffer_get_green_bits(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_height(IntPtr o);
+
+        public int GetHeight()
+        {
+            return cogl_framebuffer_get_height(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_get_modelview_matrix(IntPtr o);
+
+        public void GetModelviewMatrix()
+        {
+            cogl_framebuffer_get_modelview_matrix(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_get_projection_matrix(IntPtr o);
+
+        public void GetProjectionMatrix()
+        {
+            cogl_framebuffer_get_projection_matrix(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_red_bits(IntPtr o);
+
+        public int GetRedBits()
+        {
+            return cogl_framebuffer_get_red_bits(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_samples_per_pixel(IntPtr o);
+
+        public int GetSamplesPerPixel()
+        {
+            return cogl_framebuffer_get_samples_per_pixel(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_get_viewport4fv(IntPtr o);
+
+        public void GetViewport4fv()
+        {
+            cogl_framebuffer_get_viewport4fv(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern float cogl_framebuffer_get_viewport_height(IntPtr o);
+
+        public float GetViewportHeight()
+        {
+            return cogl_framebuffer_get_viewport_height(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern float cogl_framebuffer_get_viewport_width(IntPtr o);
+
+        public float GetViewportWidth()
+        {
+            return cogl_framebuffer_get_viewport_width(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern float cogl_framebuffer_get_viewport_x(IntPtr o);
+
+        public float GetViewportX()
+        {
+            return cogl_framebuffer_get_viewport_x(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern float cogl_framebuffer_get_viewport_y(IntPtr o);
+
+        public float GetViewportY()
+        {
+            return cogl_framebuffer_get_viewport_y(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern int cogl_framebuffer_get_width(IntPtr o);
+
+        public int GetWidth()
+        {
+            return cogl_framebuffer_get_width(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_identity_matrix(IntPtr o);
+
+        public void IdentityMatrix()
+        {
+            cogl_framebuffer_identity_matrix(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_orthographic(IntPtr o, float x_1, float y_1, float x_2, float y_2, float near, float far);
+
+        public void Orthographic(float x_1, float y_1, float x_2, float y_2, float near, float far)
+        {
+            cogl_framebuffer_orthographic(handle, x_1, y_1, x_2, y_2, near, far);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_perspective(IntPtr o, float fov_y, float aspect, float z_near, float z_far);
+
+        public void Perspective(float fov_y, float aspect, float z_near, float z_far)
+        {
+            cogl_framebuffer_perspective(handle, fov_y, aspect, z_near, z_far);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_pop_clip(IntPtr o);
+
+        public void PopClip()
+        {
+            cogl_framebuffer_pop_clip(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_pop_matrix(IntPtr o);
+
+        public void PopMatrix()
+        {
+            cogl_framebuffer_pop_matrix(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_push_matrix(IntPtr o);
+
+        public void PushMatrix()
+        {
+            cogl_framebuffer_push_matrix(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_push_rectangle_clip(IntPtr o, float x_1, float y_1, float x_2, float y_2);
+
+        public void PushRectangleClip(float x_1, float y_1, float x_2, float y_2)
+        {
+            cogl_framebuffer_push_rectangle_clip(handle, x_1, y_1, x_2, y_2);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_push_scissor_clip(IntPtr o, int x, int y, int width, int height);
+
+        public void PushScissorClip(int x, int y, int width, int height)
+        {
+            cogl_framebuffer_push_scissor_clip(handle, x, y, width, height);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_resolve_samples(IntPtr o);
+
+        public void ResolveSamples()
+        {
+            cogl_framebuffer_resolve_samples(handle);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_resolve_samples_region(IntPtr o, int x, int y, int width, int height);
+
+        public void ResolveSamplesRegion(int x, int y, int width, int height)
+        {
+            cogl_framebuffer_resolve_samples_region(handle, x, y, width, height);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_rotate(IntPtr o, float angle, float x, float y, float z);
+
+        public void Rotate(float angle, float x, float y, float z)
+        {
+            cogl_framebuffer_rotate(handle, angle, x, y, z);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_scale(IntPtr o, float x, float y, float z);
+
+        public void Scale(float x, float y, float z)
+        {
+            cogl_framebuffer_scale(handle, x, y, z);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_set_modelview_matrix(IntPtr o, ref Matrix matrix);
+
+        public void SetModelviewMatrix(ref Matrix matrix)
+        {
+            cogl_framebuffer_set_modelview_matrix(handle, ref matrix);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_set_projection_matrix(IntPtr o, ref Matrix matrix);
+
+        public void SetProjectionMatrix(ref Matrix matrix)
+        {
+            cogl_framebuffer_set_projection_matrix(handle, ref matrix);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_set_samples_per_pixel(IntPtr o, int samples_per_pixel);
+
+        public void SetSamplesPerPixel(int samples_per_pixel)
+        {
+            cogl_framebuffer_set_samples_per_pixel(handle, samples_per_pixel);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_set_viewport(IntPtr o, float x, float y, float width, float height);
+
+        public void SetViewport(float x, float y, float width, float height)
+        {
+            cogl_framebuffer_set_viewport(handle, x, y, width, height);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_transform(IntPtr o, ref Matrix matrix);
+
+        public void Transform(ref Matrix matrix)
+        {
+            cogl_framebuffer_transform(handle, ref matrix);
+        }
+
+        [DllImport("cogl2.dll")]
+        public static extern void cogl_framebuffer_translate(IntPtr o, float x, float y, float z);
+
+        public void Translate(float x, float y, float z)
+        {
+            cogl_framebuffer_translate(handle, x, y, z);
+        }
+
+    }
+}
diff --git a/cogl-sharp/parse-gir.py b/cogl-sharp/parse-gir.py
index 4e45d6e..1c05f4d 100755
--- a/cogl-sharp/parse-gir.py
+++ b/cogl-sharp/parse-gir.py
@@ -3,17 +3,52 @@ import sys
 import xml.dom.minidom
 from xml.dom.minidom import Node
 
-types = (
+C_NS = "http://www.gtk.org/introspection/c/1.0";
+
+# enum types to dump (.gir names)
+enum_types = (
     "BufferBit"
 )
 
-header="""/* This file is generated by parse-gir.py, do not hand edit */
+# object types to dump (.gir names)
+object_types = (
+    "Framebuffer"
+)
+
+# The struct types (value types) are written by hand
+struct_types = (
+    'Color',
+    'Matrix'
+)
+
+# maps from .gir names to cogl-sharp types/methods
+name_overrides = {
+    'Framebuffer': {
+        'class': 'FrameBuffer',
+        'methods': {
+            'clear4f', 'clear'
+        }
+    },
+    'Onscreen': {
+        'class': 'OnScreen'
+    },
+    'Offscreen': {
+        'class': 'OffScreen'
+    }
+}
+
+glib_types_map = {
+    'gfloat': 'float',
+    'gint': 'int'
+}
+
+header_enum="""/* This file has been generated by parse-gir.py, do not hand edit */
 using System;
 
 namespace Cogl
 {
 """
-footer="""}
+footer_enum="""}
 """
 
 def make_enum_name(gir_name):
@@ -22,13 +57,13 @@ def make_enum_name(gir_name):
 def generate_bitfields(doc):
     for node in doc.getElementsByTagName("bitfield"):
         type_name = node.getAttribute("name")
-        if type_name not in types:
+        if type_name not in enum_types:
             continue
 
         print("Generate %s" % type_name + ".cs")
 
         fo = open(type_name + ".cs", "w+")
-        fo.write(header)
+        fo.write(header_enum)
         fo.write("    [Flags]\n")
         fo.write("    public enum %s\n" % type_name)
         fo.write("    {\n")
@@ -44,8 +79,173 @@ def generate_bitfields(doc):
                 fo.write("        %s = %s\n" % (enum_name, value))
             n += 1
         fo.write("    }\n")
-        fo.write(footer)
+        fo.write(footer_enum)
+        fo.close()
+
+header_class="""/* This file has been generated by parse-gir.py, do not hand edit */
+using System;
+using System.Runtime.InteropServices;
+
+namespace Cogl
+{
+"""
+footer_class="""}
+"""
+
+def make_method_name(gir_name):
+    words = gir_name.split('_')
+    return "".join(map(lambda x: x.capitalize(), words))
+
+def known_type(gir_name):
+    if not gir_name:
+        return False
+
+    if gir_name == "none":
+        return True;
+
+    return (gir_name in enum_types or
+            gir_name in object_types or
+            gir_name in glib_types_map or
+            gir_name in struct_types)
+
+def is_pointer_type(c_type):
+    return c_type.endswith("*")
+
+def derive_native_type(gir_type, c_type):
+    if gir_type == 'none':
+        return 'void'
+
+    if gir_type in struct_types:
+        return gir_type
+
+    if is_pointer_type(c_type):
+        return 'IntPtr'
+
+    if gir_type in enum_types:
+        return gir_type
+
+    if gir_type in glib_types_map:
+        return glib_types_map[gir_type]
+
+    print("Error: trying to derivate (%s,%s)" % (gir_type, c_type))
+    assert False
+
+def derive_cs_type(gir_type, c_type):
+    if gir_type == 'none':
+        return 'void'
+
+    if gir_type in glib_types_map:
+        return glib_types_map[gir_type]
+
+    return gir_type
+
+def generate_method(node, overrides, fo):
+
+    native_method_name = node.getAttributeNS(C_NS, "identifier")
+    native_return_value = "void"
+    native_params = ['IntPtr o']
+    cs_method_name = make_method_name(node.getAttribute("name"))
+    cs_return_value = "void"
+    cs_params = []
+    call_params = ['handle']
+
+    # Let's figure out if we can generate that method (ie if we know how to
+    # handle the types of the return value and of the parameters).
+    # At the same time, we compute the nececessary return value and parameter
+    # strings for the generation code below.
+
+    # Fist let's start with the return value, ...
+    return_value = node.getElementsByTagName("return-value")
+    assert len(return_value) == 1
+    return_value = return_value.item(0)
+    return_type = return_value.getElementsByTagName("type").item(0)
+    return_type_name = return_type.getAttribute("name")
+    return_c_type = return_type.getAttributeNS(C_NS, "type")
+
+    if not known_type(return_type_name):
+        print("  Skipping %s, unknown return type '%s' (%s)" %
+              (cs_method_name, return_type_name, return_c_type))
+        return
+
+    native_return_value = derive_native_type(return_type_name, return_c_type)
+    cs_return_value = derive_cs_type(return_type_name, return_c_type)
+
+    # ... then the parameters
+    params_list = node.getElementsByTagName("parameters")
+    assert len(params_list) <= 1
+    params_list = params_list.item(0)
+
+    generatable = True
+    if params_list:
+        params = params_list.getElementsByTagName("parameter")
+        for param in params:
+            direction = param.getAttribute("direction")
+            if direction == 'out':
+                print("  Skipping %s, out parameters not supported yet" %
+                      (cs_method_name))
+                break
+
+            param_type = param.getElementsByTagName("type")
+            assert len(param_type) == 1
+            c_type = param_type.item(0).getAttributeNS(C_NS, "type")
+            gir_type = param_type.item(0).getAttribute("name")
+            if not known_type(gir_type):
+                print("  Skipping %s, unknown parameter type '%s' (%s)" %
+                      (cs_method_name, gir_type, c_type))
+                generatable = False
+                break
+
+            # time to update {native,cs}_params and call_params
+            param_name = param.getAttribute("name")
+            ref = 'ref ' if gir_type in struct_types else ''
+            t = derive_native_type(gir_type, c_type)
+            native_params.append(ref + t + ' ' + param_name)
+
+            t = derive_cs_type(gir_type, c_type)
+            cs_params.append(ref + t + ' ' + param_name)
+
+            call_params.append(ref + param_name)
+
+    if not generatable:
+        return
+
+    return_str = 'return ' if (cs_return_value != 'void') else ''
+
+    fo.write("        [DllImport(\"cogl2.dll\")]\n")
+    fo.write("        public static extern %s %s(%s);\n\n" %
+            (native_return_value, native_method_name, ", ".join(native_params)))
+
+    fo.write("        public %s %s(%s)\n" %
+             (cs_return_value, cs_method_name, ", ".join(cs_params)))
+    fo.write("        {\n")
+    fo.write("            %s%s(%s);\n" %
+             (return_str, native_method_name, ", ".join(call_params)))
+    fo.write("        }\n\n")
+
+def generate_classes(doc):
+    for node in doc.getElementsByTagName("record"):
+        type_name = node.getAttribute("name")
+        if type_name not in object_types:
+            continue
+
+        overrides = None
+        if type_name in name_overrides:
+            overrides = name_overrides[type_name]
+        if 'class' in overrides:
+            type_name = overrides['class']
+
+        print("Generate _%s" % type_name + ".cs")
+
+        fo = open("_" + type_name + ".cs", "w+")
+        fo.write(header_class)
+        fo.write("    public partial class %s\n" % type_name)
+        fo.write("    {\n")
+        for method in node.getElementsByTagName("method"):
+            generate_method(method, overrides, fo)
+        fo.write("    }\n")
+        fo.write(footer_class)
         fo.close()
 
 doc = xml.dom.minidom.parse(sys.argv[1])
 generate_bitfields(doc)
+generate_classes(doc)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]