bool InputManager::processKeyState(LSHandle* handle, LSMessage* msg, void* userData) { // {"get": string} VALIDATE_SCHEMA_AND_RETURN(handle, msg, SCHEMA_1(REQUIRED(get, string))); bool success = false; const char* keyString = NULL; QEvent::Type state = QEvent::None; LSError err; json_object* root = 0; LSErrorInit(&err); // get the text name of the key const char* str = LSMessageGetPayload(msg); if (!str) { g_debug("%s: Unable to get JSON payload from message", __PRETTY_FUNCTION__); return false; } root = json_tokener_parse(str); if (root && !is_error(root)) { // Get the key name from the msg -- the format will be {"get":"NAME"}, // where NAME is something like ringer, slider, etc keyString = json_object_get_string(json_object_object_get(root, "get")); if (keyString) { // lookup the state of the key Qt::Key key = stringToKey(keyString); state = getKeyState(key); success = true; } } json_object* response = 0; if (success) { response = createKeyJson(keyString, state); } else { response = json_object_new_object(); } json_object_object_add(response, "returnValue", json_object_new_boolean(success)); if (!LSMessageReply(handle, msg, json_object_to_json_string(response), &err)) { LSErrorPrint(&err, stderr); LSErrorFree(&err); } if (root && !is_error(root)) json_object_put(root); json_object_put(response); return true; }
static bool cbComPalmImage2Status(LSHandle* lsHandle, LSMessage *message, void *user_data) { LSError lsError; LSErrorInit(&lsError); // {"serviceName": string, "connected": boolean} VALIDATE_SCHEMA_AND_RETURN(lsHandle, message, SCHEMA_2(REQUIRED(serviceName, string), REQUIRED(connected, boolean))); json_object * root = 0; const char* str = LSMessageGetPayload(message); if( !str ) return false; root = json_tokener_parse(str); if ((root == NULL) || (is_error(root))) { root = NULL; return true; } json_object * label = Utils::JsonGetObject(root,"connected"); if (label != NULL) { if (json_object_get_boolean(label) == true) { //image2 is available Settings::settings()->m_image2svcAvailable = true; } else { Settings::settings()->m_image2svcAvailable = false; } } else { Settings::settings()->m_image2svcAvailable = false; } json_object_put(root); return true; }
bool AmbientLightSensor::controlStatus(LSHandle *sh, LSMessage *message, void *ctx) { #if defined (TARGET_DEVICE) LSError lserror; LSErrorInit(&lserror); bool result = true; AmbientLightSensor *als = (AmbientLightSensor*)ctx; // {"subscribe":boolean, "disableALS" : boolean} VALIDATE_SCHEMA_AND_RETURN(sh, message, SCHEMA_2(REQUIRED(subscribe, boolean), REQUIRED(disableALS, boolean))); g_debug ("%s: received '%s;", __PRETTY_FUNCTION__, LSMessageGetPayload(message)); bool subscribed = false; result = LSSubscriptionProcess (sh, message, &subscribed, &lserror); if(!result) { LSErrorFree (&lserror); result = true; subscribed = false; } if (subscribed) { als->m_alsSubscriptions++; als->on (); bool disable = false; const char* str = LSMessageGetPayload(message); if (str) { json_object* root = json_tokener_parse(str); if (root && !is_error(root)) { result = true; disable = json_object_get_boolean(json_object_object_get(root, "disableALS")); json_object_put(root); } } if (disable) als->m_alsDisabled++; } int ptr = (als->m_alsPointer + als->m_alsSamplesNeeded - 1) % als->m_alsSamplesNeeded; gchar *status = g_strdup_printf ("{\"returnValue\":true,\"current\":%i,\"average\":%i,\"disabled\":%s,\"subscribed\":%s}", als->m_alsValue[ptr], als->m_alsSum / als->m_alsSamplesNeeded, als->m_alsDisabled > 0 ? "true" : "false", subscribed ? "true" : "false"); if (NULL != status) result = LSMessageReply(sh, message, status, &lserror); if(!result) { LSErrorPrint (&lserror, stderr); LSErrorFree (&lserror); } g_free(status); #endif return true; }
//static bool ImageServices::lsImageInfo(LSHandle* lsHandle, LSMessage* message,void* user_data) { LSError lserror; LSErrorInit(&lserror); std::string errorText; json_object * root = NULL; const char* str; std::string srcfile; int srcWidth; int srcHeight; int srcBpp; const char* srcType; // {"src": string} VALIDATE_SCHEMA_AND_RETURN(lsHandle, message, SCHEMA_1(REQUIRED(src, string))); ImageServices * pImgSvc = instance(); if (pImgSvc == NULL) { errorText = "Image Service has not started"; goto Done_lsImageInfo; } if (pImgSvc->isValid() == false) { errorText = "Image Service has not started (failed init)"; goto Done_lsImageInfo; } str = LSMessageGetPayload( message ); if (!str) { errorText = "No payload provided"; goto Done_lsImageInfo; } root = json_tokener_parse( str ); if (!root || is_error(root)) { errorText = "Malformed JSON detected in payload"; root = 0; goto Done_lsImageInfo; } if (Utils::extractFromJson(root,"src",srcfile) == false) { errorText = "'src' parameter missing"; goto Done_lsImageInfo; } { QImageReader reader(QString::fromStdString(srcfile)); if(!reader.canRead()) { errorText = reader.errorString().toStdString(); return false; goto Done_lsImageInfo; } srcWidth = reader.size().width(); srcHeight = reader.size().height(); // QImageReader probably won't return all of these, but just to make sure we cover all cases switch(reader.imageFormat()) { case QImage::Format_ARGB32_Premultiplied: case QImage::Format_ARGB32: case QImage::Format_RGB32: srcBpp = 32; break; case QImage::Format_RGB888: case QImage::Format_RGB666: case QImage::Format_ARGB8565_Premultiplied: case QImage::Format_ARGB6666_Premultiplied: case QImage::Format_ARGB8555_Premultiplied: srcBpp = 24; break; case QImage::Format_RGB444: case QImage::Format_ARGB4444_Premultiplied: case QImage::Format_RGB16: case QImage::Format_RGB555: srcBpp = 16; break; case QImage::Format_Indexed8: srcBpp = 8; break; case QImage::Format_Mono: case QImage::Format_MonoLSB: srcBpp = 1; break; default: srcBpp = 0; } srcType = reader.format(); // png/jpg etc } Done_lsImageInfo: if (root) json_object_put(root); json_object * reply = json_object_new_object(); json_object_object_add(reply, "subscribed", json_object_new_boolean(false)); if (errorText.size() > 0) { json_object_object_add(reply, "returnValue", json_object_new_boolean(false)); json_object_object_add(reply, "errorCode", json_object_new_string(errorText.c_str())); } else { json_object_object_add(reply, "returnValue", json_object_new_boolean(true)); json_object_object_add(reply, "width",json_object_new_int(srcWidth)); json_object_object_add(reply, "height",json_object_new_int(srcHeight)); json_object_object_add(reply, "bpp",json_object_new_int(srcBpp)); json_object_object_add(reply, "type", json_object_new_string(srcType)); } if (!LSMessageReply(lsHandle, message, json_object_to_json_string(reply), &lserror)) LSErrorFree (&lserror); json_object_put(reply); return true; }
//static bool ImageServices::lsEzResize(LSHandle* lsHandle, LSMessage* message,void* user_data) { LSError lserror; LSErrorInit(&lserror); std::string errorText; json_object * root = NULL; json_object * label = NULL; const char* str; std::string srcfile; std::string destfile; std::string desttype; uint32_t destSizeW = 0; uint32_t destSizeH = 0; // {"src": string, "dest": string, "destType": string, "destSizeW": integer, "destSizeH": integer} VALIDATE_SCHEMA_AND_RETURN(lsHandle, message, SCHEMA_5(REQUIRED(src, string), REQUIRED(dest, string), REQUIRED(destType, string), REQUIRED(destSizeW, integer), REQUIRED(destSizeH, integer))); ImageServices * pImgSvc = instance(); if (pImgSvc == NULL) { errorText = "Image Service has not started"; goto Done_ezResize; } if (pImgSvc->isValid() == false) { errorText = "Image Service has not started (failed init)"; goto Done_ezResize; } str = LSMessageGetPayload( message ); if (!str) { errorText = "No payload provided"; goto Done_ezResize; } root = json_tokener_parse( str ); if (!root || is_error(root)) { errorText = "Malformed JSON detected in payload"; root = 0; goto Done_ezResize; } if (Utils::extractFromJson(root,"src",srcfile) == false) { errorText = "'src' parameter missing"; goto Done_ezResize; } if (Utils::extractFromJson(root,"dest",destfile) == false) { errorText = "'dest' parameter missing"; goto Done_ezResize; } if (Utils::extractFromJson(root,"destType",desttype) == false) { errorText = "'destType' parameter missing"; goto Done_ezResize; } if ((label = Utils::JsonGetObject(root,"destSizeW")) != NULL) { destSizeW = json_object_get_int(label); } else { errorText = "'destSizeW' missing"; goto Done_ezResize; } if ((label = Utils::JsonGetObject(root,"destSizeH")) != NULL) { destSizeH = json_object_get_int(label); } else { errorText = "'destSizeH' missing"; goto Done_ezResize; } (void)ImageServices::instance()->ezResize(srcfile, destfile, desttype.c_str(), destSizeW, destSizeH, errorText); Done_ezResize: if (root) json_object_put(root); json_object * reply = json_object_new_object(); json_object_object_add(reply, "subscribed", json_object_new_boolean(false)); if (errorText.size() > 0) { json_object_object_add(reply, "returnValue", json_object_new_boolean(false)); json_object_object_add(reply, "errorCode", json_object_new_string(errorText.c_str())); } else { json_object_object_add(reply, "returnValue", json_object_new_boolean(true)); } if (!LSMessageReply(lsHandle, message, json_object_to_json_string(reply), &lserror)) LSErrorFree (&lserror); json_object_put(reply); return true; }
// http://msdn.microsoft.com/en-us/library/ms725481.aspx bool TimeZoneService::cbGetTimeZoneFromEasData(LSHandle* lsHandle, LSMessage *message, void *user_data) { std::string reply; bool ret; LSError lsError; json_object* root = 0; json_object* label = 0; int easBias; EasSystemTime easStandardDate; int easStandardBias; EasSystemTime easDaylightDate; int easDaylightBias; LSErrorInit(&lsError); easBias = 0; easStandardDate.valid = false; easStandardBias = 0; easDaylightDate.valid = false; easDaylightBias = 0; // {"bias": integer, standardDate:{"year": integer, "month": integer, "dayOfWeek": integer, "day": integer, "hour": integer, "minute": integer, "second": integer}, "standardBias": integer, "daylightDate":{"year": integer, "month": integer, "dayOfWeek": integer, "day": integer, "hour": integer, "minute": integer, "second": integer}, "daylightBias": integer} VALIDATE_SCHEMA_AND_RETURN(lsHandle, message, SCHEMA_5(REQUIRED(bias, integer), NAKED_OBJECT_OPTIONAL_7(standardDate, year, integer, month, integer, dayOfWeek, integer, day, integer, hour, integer, minute, integer, second, integer), OPTIONAL(standardBias, integer), NAKED_OBJECT_OPTIONAL_7(daylightDate, year, integer, month, integer, dayOfWeek, integer, day, integer, hour, integer, minute, integer, second, integer),OPTIONAL(daylightBias, integer))); const char* payload = LSMessageGetPayload(message); if (!payload) { reply = "{\"returnValue\": false, " " \"errorText\": \"No payload specifed for message\"}"; goto Done; } root = json_tokener_parse(payload); if (!root || is_error(root)) { reply = "{\"returnValue\": false, " " \"errorText\": \"Cannot parse json payload\"}"; goto Done; } // bias. mandatory (everything else is optional) label = json_object_object_get(root, "bias"); if (!label || !json_object_is_type(label, json_type_int)) { reply = "{\"returnValue\": false, " " \"errorText\": \"bias value missing\"}"; goto Done; } easBias = json_object_get_int(label); // standard date label = json_object_object_get(root, "standardDate"); if (label && json_object_is_type(label, json_type_object)) readEasDate(label, easStandardDate); // standard bias label = json_object_object_get(root, "standardBias"); if (label && json_object_is_type(label, json_type_int)) easStandardBias = json_object_get_int(label); // daylight date label = json_object_object_get(root, "daylightDate"); if (label && json_object_is_type(label, json_type_object)) readEasDate(label, easDaylightDate); // daylight bias label = json_object_object_get(root, "daylightBias"); if (label && json_object_is_type(label, json_type_int)) easDaylightBias = json_object_get_int(label); // Both standard and daylight bias need to specified together, // otherwise both are invalid if (!easDaylightDate.valid) easStandardDate.valid = false; if (!easStandardDate.valid) easDaylightDate.valid = false; { // Get all timezones matching the current offset PrefsHandler* handler = PrefsFactory::instance()->getPrefsHandler("timeZone"); TimePrefsHandler* tzHandler = static_cast<TimePrefsHandler*>(handler); TimeZoneService* tzService = TimeZoneService::instance(); std::list<std::string> timeZones = tzHandler->getTimeZonesForOffset(-easBias); if (timeZones.empty()) { reply = "{\"returnValue\": false, " " \"errorText\": \"Failed to find any timezones with specified bias value\"}"; goto Done; } if (!easStandardDate.valid) { // No additional data available for refinement. Just use the // first timezone entry in the list json_object* obj = json_object_new_object(); json_object_object_add(obj, "returnValue", json_object_new_boolean(true)); json_object_object_add(obj, "timeZone", json_object_new_string(timeZones.begin()->c_str())); reply = json_object_to_json_string(obj); json_object_put(obj); goto Done; } else { time_t utcTime = time(NULL); struct tm* localTime = localtime(&utcTime); int currentYear = localTime->tm_year + 1900; updateEasDateDayOfMonth(easStandardDate, currentYear); updateEasDateDayOfMonth(easDaylightDate, currentYear); for (std::list<std::string>::const_iterator it = timeZones.begin(); it != timeZones.end(); ++it) { TimeZoneEntry tzEntry; tzEntry.tz = (*it); tzEntry.years.push_back(currentYear); TimeZoneResultList tzResultList = tzService->getTimeZoneRuleOne(tzEntry); if (tzResultList.empty()) continue; const TimeZoneResult& tzResult = tzResultList.front(); printf("For timezone: %s\n", tzEntry.tz.c_str()); printf("year: %d, utcOffset: %lld, dstOffset: %lld, dstStart: %lld, dstEnd: %lld\n", tzResult.year, tzResult.utcOffset, tzResult.dstOffset, tzResult.dstStart, tzResult.dstEnd); // Set this timezone as the current timezone, so that we can calculate when the // DST transitions occurs in seconds for the specified eas data setenv("TZ", tzEntry.tz.c_str(), 1); struct tm tzBrokenTime; tzBrokenTime.tm_sec = easStandardDate.second; tzBrokenTime.tm_min = easStandardDate.minute; tzBrokenTime.tm_hour = easStandardDate.hour; tzBrokenTime.tm_mday = easStandardDate.day; tzBrokenTime.tm_mon = easStandardDate.month - 1; tzBrokenTime.tm_year = currentYear - 1900; tzBrokenTime.tm_wday = 0; tzBrokenTime.tm_yday = 0; tzBrokenTime.tm_isdst = 1; time_t easStandardDateSeconds = ::mktime(&tzBrokenTime); tzBrokenTime.tm_sec = easDaylightDate.second; tzBrokenTime.tm_min = easDaylightDate.minute; tzBrokenTime.tm_hour = easDaylightDate.hour; tzBrokenTime.tm_mday = easDaylightDate.day; tzBrokenTime.tm_mon = easDaylightDate.month - 1; tzBrokenTime.tm_year = currentYear - 1900; tzBrokenTime.tm_wday = 0; tzBrokenTime.tm_yday = 0; tzBrokenTime.tm_isdst = 0; time_t easDaylightDateSeconds = ::mktime(&tzBrokenTime); printf("eas dstStart: %ld, dstEnd: %ld\n", easDaylightDateSeconds, easStandardDateSeconds); if (easStandardDateSeconds == tzResult.dstEnd && easDaylightDateSeconds == tzResult.dstStart) { // We have a winner json_object* obj = json_object_new_object(); json_object_object_add(obj, "returnValue", json_object_new_boolean(true)); json_object_object_add(obj, "timeZone", json_object_new_string(tzEntry.tz.c_str())); reply = json_object_to_json_string(obj); json_object_put(obj); goto Done; } } reply = "{\"returnValue\": false, " " \"errorText\": \"Failed to find any timezones with specified parametes\"}"; } } Done: ret = LSMessageReply(lsHandle, message, reply.c_str(), &lsError); if (!ret) LSErrorFree(&lsError); if (root && !is_error(root)) json_object_put(root); return true; }