static JSBool SMJS_FUNCTION(upnp_service_set_action_listener) { PLT_ActionDesc *action; PLT_ArgumentDesc *desc; GPAC_ActionArgListener *argl = NULL; char *name; Bool script_callback = GF_FALSE; u32 i; SMJS_OBJ SMJS_ARGS GPAC_ServiceItem *service = (GPAC_ServiceItem *)SMJS_GET_PRIVATE(c, obj); if (!service || (argc<2) || !JSVAL_IS_STRING(argv[0]) || !JSVAL_IS_OBJECT(argv[1])) return JS_FALSE; name = SMJS_CHARS(c, argv[0]); if (!name) return JS_FALSE; action = service->m_service->FindActionDesc(name); SMJS_FREE(c, name); if (!action) return JS_FALSE; desc = NULL; if (argc==3) { if (JSVAL_IS_BOOLEAN(argv[2])) { script_callback = GF_TRUE; } else { if (!JSVAL_IS_STRING(argv[2]) ) return JS_FALSE; name = SMJS_CHARS(c, argv[2]); if (!name) return JS_FALSE; desc = action->GetArgumentDesc(name); SMJS_FREE(c, name); if (!desc) return JS_FALSE; } } /*action listener*/ i=0; while ((argl = (GPAC_ActionArgListener *)gf_list_enum(service->m_ArgListeners, &i))) { if (argl->arg == desc) break; argl = NULL; } if (!argl) { argl = new GPAC_ActionArgListener(); argl->arg = desc; gf_list_add(service->m_ArgListeners, argl); } argl->action = action; if (!JSVAL_IS_NULL(argl->on_event)) gf_js_remove_root(c, &argl->on_event, GF_JSGC_VAL); if (JSVAL_IS_NULL(argv[1])) { gf_list_del_item(service->m_ArgListeners, argl); delete argl; } argl->on_event = argv[1]; argl->is_script = script_callback; gf_js_add_root(c, &argl->on_event, GF_JSGC_VAL); return JS_TRUE; }
static JSBool SMJS_FUNCTION_EXT(widget_activate_component, Bool is_deactivate) { u32 i, count; char *comp_id; SMJS_OBJ SMJS_ARGS GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid) return JS_FALSE; if (!argc || !JSVAL_IS_STRING(argv[0])) return JS_TRUE; comp_id = SMJS_CHARS(c, argv[0]); count = gf_list_count(wid->widget->main->components); for (i=0; i<count; i++) { GF_WidgetComponent *comp = gf_list_get(wid->widget->main->components, i); if (!comp->id || strcmp(comp->id, comp_id)) continue; if (is_deactivate) { wm_deactivate_component(c, wid, comp, NULL); } else { wm_activate_component(c, wid, comp, 0); } break; } SMJS_FREE(c, comp_id); return JS_TRUE; }
static JSBool SMJS_FUNCTION(widget_get_interfaces) { u32 i, count; char *ifce_name; JSObject *list; jsuint idx; jsval v; SMJS_OBJ SMJS_ARGS GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid) return JS_FALSE; if (!argc || !JSVAL_IS_STRING(argv[0])) return JS_TRUE; ifce_name = SMJS_CHARS(c, argv[0]); list = JS_NewArrayObject(c, 0, 0); count = gf_list_count(wid->bound_ifces); for (i=0; i<count; i++) { GF_WidgetInterfaceInstance *ifce = gf_list_get(wid->bound_ifces, i); if (strcmp(ifce->ifce->type, ifce_name)) continue; widget_interface_js_bind(c, ifce); JS_GetArrayLength(c, list, &idx); v = OBJECT_TO_JSVAL(ifce->obj); JS_SetElement(c, list, idx, &v); } SMJS_SET_RVAL( OBJECT_TO_JSVAL(list) ); SMJS_FREE(c, ifce_name); return JS_TRUE; }
static JSBool SMJS_FUNCTION(widget_message_handler_factory) { char *msg_name; u32 i, count; SMJS_OBJ SMJS_ARGS GF_WidgetInterfaceInstance *bifce = (GF_WidgetInterfaceInstance *)SMJS_GET_PRIVATE(c, obj); if (!bifce) return JS_FALSE; if (!argc) return JS_FALSE; if (!JSVAL_IS_STRING(argv[0])) return JS_FALSE; msg_name = SMJS_CHARS(c, argv[0]); if (!msg_name ) return JS_FALSE; SMJS_SET_RVAL( JSVAL_NULL ); count = gf_list_count(bifce->ifce->messages); for (i=0; i<count; i++) { GF_WidgetMessage *msg = gf_list_get(bifce->ifce->messages, i); if (!strcmp(msg->name, msg_name)) { JSObject *an_obj = JS_NewObject(c, &bifce->wid->widget->wm->widgetAnyClass._class, 0, 0); SMJS_SET_PRIVATE(c, an_obj, msg); JS_DefineProperty(c, an_obj, "msgName", STRING_TO_JSVAL( JS_NewStringCopyZ(c, msg->name) ), 0, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineProperty(c, an_obj, "interfaceHandler", OBJECT_TO_JSVAL( obj ), 0, 0, JSPROP_READONLY | JSPROP_PERMANENT); JS_DefineFunction(c, an_obj, "onInvokeReply", widget_call_message_reply_callback, 1, 0); if ((argc==2) && JSVAL_IS_OBJECT(argv[1]) && !JSVAL_IS_NULL(argv[1])) JS_DefineProperty(c, an_obj, "replyCallback", argv[1], 0, 0, JSPROP_READONLY | JSPROP_PERMANENT); SMJS_SET_RVAL( OBJECT_TO_JSVAL(an_obj) ); } } SMJS_FREE(c, msg_name); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_service_set_listener) { GPAC_StateVariableListener *svl = NULL; char *name=NULL; u32 i; SMJS_OBJ SMJS_ARGS GPAC_ServiceItem *service = (GPAC_ServiceItem *)SMJS_GET_PRIVATE(c, obj); if (!service || !argc || !JSVAL_IS_OBJECT(argv[0])) return JS_FALSE; if (argc<1) { if (!JSVAL_IS_NULL(service->on_event)) gf_js_remove_root(c, &service->on_event, GF_JSGC_VAL); service->on_event = JSVAL_NULL; if (!JSVAL_IS_NULL(argv[0])) { service->on_event = argv[0]; gf_js_add_root(c, &service->on_event, GF_JSGC_VAL); if (!service->subscribed) { service->m_device->m_pUPnP->m_pGenericController->m_CtrlPoint->Subscribe(service->m_service); service->subscribed = GF_TRUE; } } return JS_TRUE; } if (!JSVAL_IS_STRING(argv[1])) return JS_FALSE; name = SMJS_CHARS(c, argv[1]); if (!name) return JS_FALSE; /*variable state listener*/ i=0; while ((svl = (GPAC_StateVariableListener *)gf_list_enum(service->m_StateListeners, &i))) { if (svl->name == (const char *) name) break; } if (!svl) { svl = new GPAC_StateVariableListener(); svl->name = name; svl->var = service->m_service->FindStateVariable(name); gf_list_add(service->m_StateListeners, svl); } if (!JSVAL_IS_NULL(svl->on_event)) gf_js_remove_root(c, &svl->on_event, GF_JSGC_VAL); if (JSVAL_IS_NULL(argv[0])) { gf_list_del_item(service->m_StateListeners, svl); delete svl; } svl->on_event = argv[0]; gf_js_add_root(c, &svl->on_event, GF_JSGC_VAL); if (!service->subscribed) { service->m_device->m_pUPnP->m_pGenericController->m_CtrlPoint->Subscribe(service->m_service); service->subscribed = GF_TRUE; } SMJS_FREE(c, name); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_service_set_state_variable) { char *name, *val; SMJS_OBJ SMJS_ARGS GPAC_Service* service = (GPAC_Service*) SMJS_GET_PRIVATE(c, obj); if (!service) return JS_FALSE; name = SMJS_CHARS(c, argv[0]); if (!name) return JS_FALSE; val = SMJS_CHARS(c, argv[1]); if (!val) { SMJS_FREE(c, name); return JS_FALSE; } service->SetStateVariable(name, val); SMJS_FREE(c, name); SMJS_FREE(c, val); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_service_has_var) { char *name = NULL; SMJS_OBJ SMJS_ARGS GPAC_ServiceItem *service = (GPAC_ServiceItem *)SMJS_GET_PRIVATE(c, obj); if (!service || !argc || !JSVAL_IS_STRING(argv[0]) ) return JS_FALSE; name = SMJS_CHARS(c, argv[0]); SMJS_SET_RVAL( BOOLEAN_TO_JSVAL( (service->m_service->FindStateVariable(name)==NULL) ? JS_FALSE : JS_TRUE ) ); SMJS_FREE(c, name); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_service_has_action) { PLT_ActionDesc *action; char *name = NULL; SMJS_OBJ SMJS_ARGS GPAC_ServiceItem *service = (GPAC_ServiceItem *)SMJS_GET_PRIVATE(c, obj); if (!service || !argc || !JSVAL_IS_STRING(argv[0]) ) return JS_FALSE; name = SMJS_CHARS(c, argv[0]); action = service->m_service->FindActionDesc(name); SMJS_FREE(c, name); SMJS_SET_RVAL( BOOLEAN_TO_JSVAL(action ? JS_TRUE : JS_FALSE) ); if (!action) return JS_TRUE; if ((argc==2) && JSVAL_IS_STRING(argv[1])) { name = SMJS_CHARS(c, argv[1]); if (action->GetArgumentDesc(name) == NULL) SMJS_SET_RVAL( BOOLEAN_TO_JSVAL(JS_FALSE) ); SMJS_FREE(c, name); } return JS_TRUE; }
JSBool SMJS_FUNCTION(widget_open_url) { GF_Event evt; SMJS_OBJ SMJS_ARGS GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid || !argc || !JSVAL_IS_STRING(argv[0])) return JS_TRUE; memset(&evt, 0, sizeof(GF_Event)); evt.type = GF_EVENT_NAVIGATE; evt.navigate.to_url = SMJS_CHARS(c, argv[0]); gf_term_send_event(wid->widget->wm->term, &evt); SMJS_FREE(c, (char *)evt.navigate.to_url); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_action_get_argument) { char *act_name; SMJS_OBJ SMJS_ARGS GPAC_GenericDevice *device = (GPAC_GenericDevice *)SMJS_GET_PRIVATE(c, obj); if (!device || !argc || !JSVAL_IS_STRING(argv[0]) ) return JS_FALSE; act_name = SMJS_CHARS(c, argv[0]); NPT_String value; if (device->act_ref->GetArgumentValue(act_name, value) != NPT_SUCCESS) return JS_FALSE; SMJS_SET_RVAL( STRING_TO_JSVAL( JS_NewStringCopyZ(c, value) )); SMJS_FREE(c, act_name); return JS_TRUE; }
JSBool SMJS_FUNCTION(widget_has_feature) { char *feat_name; SMJS_OBJ SMJS_ARGS GF_WidgetInstance *wid = (GF_WidgetInstance *)SMJS_GET_PRIVATE(c, obj); if (!wid) return JS_FALSE; if (!argc || !JSVAL_IS_STRING(argv[0])) return JS_TRUE; feat_name = SMJS_CHARS(c, argv[0]); SMJS_SET_RVAL( BOOLEAN_TO_JSVAL( JS_FALSE ) ); if (!strcmp(feat_name, "urn:mpeg:systems:mpeg-u:2009")) SMJS_SET_RVAL( BOOLEAN_TO_JSVAL( JS_TRUE )); SMJS_FREE(c, feat_name); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_action_get_argument_value) { NPT_String res; char *arg_name = NULL; SMJS_OBJ SMJS_ARGS PLT_Action *action = (PLT_Action *) SMJS_GET_PRIVATE(c, obj); if (!action || !argc || !JSVAL_IS_STRING(argv[0])) return JS_FALSE; arg_name = SMJS_CHARS(c, argv[0]); if (!arg_name) return JS_FALSE; action->GetArgumentValue(arg_name, res); SMJS_SET_RVAL( STRING_TO_JSVAL( JS_NewStringCopyZ(c, res) )); SMJS_FREE(c, arg_name); return JS_TRUE; }
JSBool upnpservice_getProperty(JSContext *c, JSObject *obj, SMJS_PROP_GETTER, jsval *vp) { char *prop_name; GPAC_ServiceItem *service = (GPAC_ServiceItem *)JS_GetPrivate(c, obj); if (!service) return JS_FALSE; if (!SMJS_ID_IS_STRING(id)) return JS_TRUE; prop_name = SMJS_CHARS_FROM_STRING(c, SMJS_ID_TO_STRING(id)); if (!prop_name) return JS_FALSE; if (!strcmp(prop_name, "Device")) { *vp = OBJECT_TO_JSVAL(service->m_device->obj); } else if (!strcmp(prop_name, "ModifiedStateVariablesCount")) { *vp = INT_TO_JSVAL(service->vars ? service->vars->GetItemCount() : 0); } SMJS_FREE(c, prop_name); return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_action_send_reply) { SMJS_OBJ SMJS_ARGS GPAC_GenericDevice *device = (GPAC_GenericDevice *)SMJS_GET_PRIVATE(c, obj); if (!device) return JS_FALSE; if (argc && JSVAL_IS_OBJECT(argv[0]) ) { JSObject *list = JSVAL_TO_OBJECT(argv[0]); u32 i, count; JS_GetArrayLength(c, list, (jsuint*) &count); GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, ("[UPnP] Calling response %s(", (char *) device->act_ref->GetActionDesc().GetName())); i=0; while (i+2<=count) { jsval an_arg; NPT_Result res; char *param_val, *_param_val = NULL; char szParamVal[1024]; JS_GetElement(c, list, (jsint) i, &an_arg); char *param_name = SMJS_CHARS(c, an_arg); JS_GetElement(c, list, (jsint) i+1, &an_arg); param_val = (char*)""; if (JSVAL_IS_STRING(an_arg)) { param_val = _param_val = SMJS_CHARS(c, an_arg); } else if (JSVAL_IS_BOOLEAN(an_arg)) { param_val = (char *) ((JSVAL_TO_BOOLEAN(an_arg) == JS_TRUE) ? "true" : "false"); } else if (JSVAL_IS_INT(argv[1])) { sprintf(szParamVal, "%d", JSVAL_TO_INT(an_arg)); param_val = szParamVal; } else if (JSVAL_IS_NUMBER(an_arg)) { jsdouble v; JS_ValueToNumber(c, an_arg, &v); sprintf(szParamVal, "%g", v); param_val = szParamVal; } if (!param_name || !param_val) res = NPT_FAILURE; else { GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, (" %s(%s)", param_name, param_val)); res = device->act_ref->SetArgumentValue(param_name, param_val); } SMJS_FREE(c, param_name); SMJS_FREE(c, _param_val); if (res != NPT_SUCCESS) return JS_FALSE; i+=2; } GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, (" )\n")); } //notify we are ready if (device->m_pSema) { gf_sema_notify(device->m_pSema, 1); } return JS_TRUE; }
static JSBool SMJS_FUNCTION(upnp_service_call_action) { GPAC_ActionUDTA *act_udta = NULL; char *action_name = NULL; SMJS_OBJ SMJS_ARGS GPAC_ServiceItem *service = (GPAC_ServiceItem *)SMJS_GET_PRIVATE(c, obj); if (!service || !argc || !JSVAL_IS_STRING(argv[0]) ) return JS_FALSE; action_name = SMJS_CHARS(c, argv[0]); PLT_ActionDesc* action_desc = service->m_service->FindActionDesc(action_name); SMJS_FREE(c, action_name); if (action_desc == NULL) return JS_FALSE; PLT_ActionReference action; NPT_CHECK_SEVERE( service->m_device->m_pUPnP->m_pGenericController->m_CtrlPoint->CreateAction( service->m_device->m_device, service->m_service->GetServiceType(), action_name, action) ); if ((argc>=2) && JSVAL_IS_OBJECT(argv[1])) { JSObject *list = JSVAL_TO_OBJECT(argv[1]); u32 i, count; JS_GetArrayLength(c, list, (jsuint*) &count); GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, ("[UPnP] Calling %s(", action_name)); i=0; while (i+2<=count) { NPT_Result res; jsval an_arg; char *param_val, *_param_val = NULL; char szParamVal[1024]; JS_GetElement(c, list, (jsint) i, &an_arg); char *param_name = SMJS_CHARS(c, an_arg); JS_GetElement(c, list, (jsint) i+1, &an_arg); param_val = (char*)""; if (JSVAL_IS_STRING(an_arg)) { param_val = _param_val = SMJS_CHARS(c, an_arg); } else if (JSVAL_IS_BOOLEAN(an_arg)) { param_val = (char *) ((JSVAL_TO_BOOLEAN(an_arg) == JS_TRUE) ? "true" : "false"); } else if (JSVAL_IS_INT(argv[1])) { sprintf(szParamVal, "%d", JSVAL_TO_INT(an_arg)); param_val = szParamVal; } else if (JSVAL_IS_NUMBER(an_arg)) { jsdouble v; JS_ValueToNumber(c, an_arg, &v); sprintf(szParamVal, "%g", v); param_val = szParamVal; } if (!param_name || !param_val) res = NPT_FAILURE; else { GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, (" %s(%s)", param_name, param_val)); res = action->SetArgumentValue(param_name, param_val); } SMJS_FREE(c, param_name); SMJS_FREE(c, _param_val); if (res != NPT_SUCCESS) return JS_FALSE; i+=2; } GF_LOG(GF_LOG_INFO, GF_LOG_NETWORK, (" )\n")); } if ((argc==3) && JSVAL_IS_OBJECT(argv[2])) { act_udta = new GPAC_ActionUDTA(); act_udta->udta = argv[2]; gf_js_add_root(c, &act_udta->udta, GF_JSGC_VAL); } service->m_device->m_pUPnP->m_pGenericController->m_CtrlPoint->InvokeAction(action, act_udta); return JS_TRUE; }