1/* 2 Copyright (C) 2011 ProFUSION embedded systems 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Library General Public 6 License as published by the Free Software Foundation; either 7 version 2 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Library General Public License for more details. 13 14 You should have received a copy of the GNU Library General Public License 15 along with this library; see the file COPYING.LIB. If not, write to 16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 Boston, MA 02110-1301, USA. 18*/ 19 20/** 21 * @file ewk_js.h 22 * @brief Allows to export objects to JavaScript API. 23 */ 24 25#ifndef ewk_js_h 26#define ewk_js_h 27 28#include <Eina.h> 29#include <Evas.h> 30 31#ifdef __cplusplus 32extern "C" { 33#endif 34 35#define EWK_JS_CLASS_META_VERSION 0 36 37typedef struct _Ewk_JS_Object Ewk_JS_Object; 38typedef struct _Ewk_JS_Class_Meta Ewk_JS_Class_Meta; 39typedef struct _Ewk_JS_Variant Ewk_JS_Variant; 40typedef struct _Ewk_JS_Method Ewk_JS_Method; 41typedef struct _Ewk_JS_Property Ewk_JS_Property; 42typedef struct _Ewk_JS_Default Ewk_JS_Default; 43typedef Eina_Bool (*Ewk_JS_Set_Cb)(Ewk_JS_Object *obj, const char *name, const Ewk_JS_Variant *value); 44typedef Eina_Bool (*Ewk_JS_Get_Cb)(Ewk_JS_Object *obj, const char *name, Ewk_JS_Variant *value); 45typedef Eina_Bool (*Ewk_JS_Del_Cb)(Ewk_JS_Object *obj, const char *name); 46typedef Ewk_JS_Variant* (*Ewk_JS_Invoke_Cb)(Ewk_JS_Object *obj, Ewk_JS_Variant *args, int argCount); 47 48typedef enum { 49 EWK_JS_VARIANT_VOID, 50 EWK_JS_VARIANT_NULL, 51 EWK_JS_VARIANT_BOOL, 52 EWK_JS_VARIANT_INT32, 53 EWK_JS_VARIANT_DOUBLE, 54 EWK_JS_VARIANT_STRING, 55 EWK_JS_VARIANT_OBJECT 56} Ewk_JS_Variant_Type; 57 58typedef enum { 59 EWK_JS_OBJECT_OBJECT, 60 EWK_JS_OBJECT_ARRAY, 61 EWK_JS_OBJECT_FUNCTION, 62 EWK_JS_OBJECT_INVALID 63} Ewk_JS_Object_Type; 64 65struct _Ewk_JS_Variant { 66 Ewk_JS_Variant_Type type; 67 union { 68 Eina_Bool b; 69 int i; 70 double d; 71 const char *s; 72 Ewk_JS_Object *o; 73 } value; 74}; 75 76struct _Ewk_JS_Method { 77 const char *name; 78 Ewk_JS_Invoke_Cb invoke; 79}; 80 81struct _Ewk_JS_Property { 82 const char *name; 83 Ewk_JS_Set_Cb set; 84 Ewk_JS_Get_Cb get; 85 Ewk_JS_Del_Cb del; 86 Ewk_JS_Variant value; 87}; 88 89struct _Ewk_JS_Default { 90 Ewk_JS_Set_Cb set; 91 Ewk_JS_Get_Cb get; 92 Ewk_JS_Del_Cb del; 93}; 94 95struct _Ewk_JS_Class_Meta { 96 unsigned int version; // define 97 const Ewk_JS_Method *methods; // null terminated array 98 const Ewk_JS_Property *properties; // null terminated array 99 Ewk_JS_Default default_prop; 100}; 101 102/** 103 * Gets Eina_Hash with object's properties. 104 * 105 * @param obj Object whose properties are wanted. 106 * 107 * @return object's properties. 108 */ 109EAPI Eina_Hash *ewk_js_object_properties_get(const Ewk_JS_Object *obj); 110 111/** 112 * Gets name of the object. 113 * 114 * @param obj Object whose name is wanted. 115 * 116 * @return name of object. 117 */ 118 119EAPI const char *ewk_js_object_name_get(const Ewk_JS_Object *obj); 120 121/** 122 * Returns the view associated with an Ewk_JS_Object. 123 * 124 * The returned view is the one passed to ewk_view_js_object_add. Right now, 125 * the object is always added to the view's main frame. 126 * 127 * @param obj The object to be queried. 128 * 129 * @return The view whose main frame the object has been inserted into, or 130 * @c NULL if the object has not been added to a view yet. 131 * 132 * @sa ewk_view_js_object_add, ewk_view_frame_main_get 133 */ 134EAPI Evas_Object *ewk_js_object_view_get(const Ewk_JS_Object *obj); 135 136/** 137 * Release resources allocated by @a var. 138 * 139 * @param var @a Ewk_JS_Variant to be release 140 */ 141EAPI void ewk_js_variant_free(Ewk_JS_Variant *var); 142 143/** 144 * Release resources allocated by @a var. 145 * 146 * @param var @a Ewk_JS_Variant to be release 147 * @param count @a size of array 148 */ 149EAPI void ewk_js_variant_array_free(Ewk_JS_Variant *var, int count); 150 151/** 152 * Create a Ewk_JS_Object to be used in @a ewk_view_js_object_add. The Meta class's 153 * methods and properties are not modified but references to it are kept as long 154 * as the object created from it lives. All properties created here 155 * will be added to the object hash of properties. Properties using default_prop's 156 * get/set/del methods should also be added to the objects hash(see: 157 * @a ewk_js_object_properties_get). Methods must free the arguments they receive(see: 158 * @a ewk_js_variang_array_free). 159 * 160 * 161 * @param cls @a Ewk_JS_Class that describes the object to be created. 162 * 163 * @return The Ewk_JS_Object created. 164 */ 165EAPI Ewk_JS_Object *ewk_js_object_new(const Ewk_JS_Class_Meta *meta_cls); 166 167/** 168 * Release resources allocated by @a obj. 169 * 170 * @param obj @a Ewk_JS_Object to be released. 171 */ 172EAPI void ewk_js_object_free(Ewk_JS_Object *obj); 173 174/** 175 * Calls the function this object represents. 176 * 177 * @param obj Object that represents function. 178 * @param args Arguments to be passed to function. 179 * @param arg_count Number of arguments. 180 * @param result Return value of the invoked function. 181 * 182 * @return @c EINA_TRUE if function was executed, @c EINA_FALSE if function was not executed. 183 */ 184EAPI Eina_Bool ewk_js_object_invoke(Ewk_JS_Object *obj, Ewk_JS_Variant *args, int arg_count, Ewk_JS_Variant *result); 185 186/** 187 * Returns the type this object represents. 188 * 189 * @param obj Object 190 * 191 * @return @c EWK_JS_OBJECT if it is an object, @c EWK_JS_ARRAY if it is an array and 192 * @c EWK_JS_FUNCTION if it is a function. 193 */ 194EAPI Ewk_JS_Object_Type ewk_js_object_type_get(Ewk_JS_Object *obj); 195 196/** 197 * Sets the type this object represents. 198 * 199 * @param obj Object 200 * @param type Type 201 * 202 */ 203EAPI void ewk_js_object_type_set(Ewk_JS_Object *obj, Ewk_JS_Object_Type type); 204 205#ifdef __cplusplus 206} 207#endif 208 209#endif // ewk_js_h 210