CCDevice-tizen.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "platform/CCPlatformConfig.h"
  21. #if CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN
  22. #include "platform/CCDevice.h"
  23. #include <app.h>
  24. #include <Evas.h>
  25. #include <Elementary.h>
  26. #include <Evas_Engine_Buffer.h>
  27. #include <sensor.h>
  28. #include <system_info.h>
  29. #include "base/CCEventDispatcher.h"
  30. #include "base/CCEventAcceleration.h"
  31. #include "base/CCDirector.h"
  32. #include "platform/CCFileUtils.h"
  33. #include "platform/tizen/CCApplication-tizen.h"
  34. using namespace std;
  35. NS_CC_BEGIN
  36. static sensor_h s_sensorHandle = nullptr;
  37. static sensor_listener_h s_sensorListener = nullptr;
  38. static float s_sensorInterval = 30.0f;
  39. #define GRAVITY_EARTH 9.80665f
  40. static bool checkAccelerometerValues(sensor_event_s *sensor_data)
  41. {
  42. for(int i = 0; i < sensor_data->value_count; ++i)
  43. {
  44. float value = sensor_data->values[i];
  45. if(isnan(value))//or other limitations
  46. {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. static void accelerometer_sensor_cb(sensor_h _sensor, sensor_event_s *sensor_data, void *user_data)
  53. {
  54. if(!checkAccelerometerValues(sensor_data))
  55. {
  56. CCLOG("sensor value is invalid!!");
  57. return;
  58. }
  59. if(3 != sensor_data->value_count)
  60. {
  61. return;
  62. }
  63. Acceleration *_acceleration = new Acceleration();
  64. _acceleration->x = sensor_data->values[0] / GRAVITY_EARTH;
  65. _acceleration->y = sensor_data->values[1] / GRAVITY_EARTH;
  66. _acceleration->z = sensor_data->values[2] / GRAVITY_EARTH;
  67. _acceleration->timestamp = sensor_data->timestamp;
  68. double tmp = _acceleration->x;
  69. Application *app = Application::getInstance();
  70. int orientation = elm_win_rotation_get(app->_win);
  71. switch (orientation)
  72. {
  73. case 0:
  74. _acceleration->x = _acceleration->y;
  75. _acceleration->y = tmp;
  76. break;
  77. case 90:
  78. _acceleration->x = -_acceleration->y;
  79. _acceleration->y = tmp;
  80. break;
  81. case 180:
  82. _acceleration->x = -_acceleration->y;
  83. _acceleration->y = -tmp;
  84. break;
  85. case 270:
  86. _acceleration->x = _acceleration->y;
  87. _acceleration->y = -tmp;
  88. break;
  89. default:
  90. CCLOG("unknown orientation");
  91. }
  92. cocos2d::EventAcceleration event(*_acceleration);
  93. auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher();
  94. dispatcher->dispatchEvent(&event);
  95. }
  96. void stopAccelerometerSensor()
  97. {
  98. if (s_sensorListener)
  99. {
  100. sensor_listener_stop(s_sensorListener);
  101. sensor_destroy_listener(s_sensorListener);
  102. s_sensorListener = nullptr;
  103. s_sensorHandle = nullptr;
  104. }
  105. }
  106. void startAccelerometerSensor()
  107. {
  108. if (!s_sensorListener)
  109. {
  110. sensor_get_default_sensor(SENSOR_ACCELEROMETER, &s_sensorHandle);
  111. auto err = sensor_create_listener(s_sensorHandle, &s_sensorListener);
  112. if(SENSOR_ERROR_NONE != err)
  113. {
  114. CCLOG("create sensor failed!");
  115. }
  116. sensor_listener_set_event_cb(s_sensorListener, s_sensorInterval, accelerometer_sensor_cb, NULL);
  117. sensor_listener_start(s_sensorListener);
  118. }
  119. }
  120. static bool s_resumeAccelerometerSensor = false;
  121. void pauseAccelerometerSensor()
  122. {
  123. if (s_sensorListener)
  124. {
  125. s_resumeAccelerometerSensor = true;
  126. stopAccelerometerSensor();
  127. }
  128. else
  129. {
  130. s_resumeAccelerometerSensor = false;
  131. }
  132. }
  133. void resumeAccelerometerSensor()
  134. {
  135. if (s_resumeAccelerometerSensor)
  136. {
  137. startAccelerometerSensor();
  138. }
  139. }
  140. void Device::setAccelerometerEnabled(bool isEnabled)
  141. {
  142. if(isEnabled)
  143. {
  144. startAccelerometerSensor();
  145. }
  146. else
  147. {
  148. stopAccelerometerSensor();
  149. }
  150. }
  151. void Device::setAccelerometerInterval(float interval)
  152. {
  153. s_sensorInterval = interval;
  154. if(s_sensorListener)
  155. {
  156. sensor_listener_set_interval(s_sensorListener, s_sensorInterval);
  157. }
  158. }
  159. int Device::getDPI()
  160. {
  161. static int dpi = -1;
  162. if (dpi == -1)
  163. {
  164. auto ret = system_info_get_platform_int("tizen.org/feature/screen.dpi",&dpi);
  165. if(ret != SYSTEM_INFO_ERROR_NONE)
  166. {
  167. //error handing
  168. dpi = -1;
  169. }
  170. }
  171. return dpi;
  172. }
  173. static void getTextBitmapData(const char * text, const FontDefinition& textDefinition, Data& bitmapData, int &bitmapWidth, int &bitmapHeight)
  174. {
  175. //create canvas object
  176. auto method = evas_render_method_lookup("buffer");
  177. if (method <= 0)
  178. {
  179. log("ERROR: evas was not compiled with 'buffer' engine!");
  180. return ;
  181. }
  182. //evas_init();//not necessary, removed when use Tizen 2.4.
  183. auto canvas = evas_new();
  184. if(!canvas){
  185. log("ERROR: could not instantiate new evas canvas.");
  186. return;
  187. }
  188. evas_output_method_set(canvas, method);
  189. std::string fontFullPath;
  190. if(textDefinition._fontName.empty())
  191. {
  192. fontFullPath = "Sans";
  193. }
  194. else
  195. {
  196. fontFullPath= FileUtils::getInstance()->fullPathForFilename(textDefinition._fontName);
  197. }
  198. const char* fontName = fontFullPath.c_str();
  199. const char* textAlign = "left";
  200. const char* textValign = "top";
  201. char richTextStyle[50];
  202. richTextStyle[0] = 0;
  203. if (textDefinition._stroke._strokeEnabled)
  204. {
  205. sprintf(richTextStyle,"style=outline outline_color=#%02X%02X%02X",
  206. textDefinition._stroke._strokeColor.r, textDefinition._stroke._strokeColor.g, textDefinition._stroke._strokeColor.b);
  207. }
  208. switch (textDefinition._alignment)
  209. {
  210. case TextHAlignment::CENTER:
  211. textAlign = "center";
  212. break;
  213. case TextHAlignment::RIGHT:
  214. textAlign = "right";
  215. break;
  216. default:
  217. break;
  218. }
  219. double valignment = 0.0;
  220. switch(textDefinition._vertAlignment)
  221. {
  222. case TextVAlignment::CENTER:
  223. textValign = "center";
  224. valignment = 0.5;
  225. break;
  226. case TextVAlignment::BOTTOM:
  227. textValign = "bottom";
  228. valignment = 1.0;
  229. break;
  230. default:
  231. break;
  232. }
  233. auto richText = (char*)malloc(strlen(text) + 150);
  234. sprintf(richText,"DEFAULT='font=%s font_size=%d align=%s valign=%s %s color=#%02X%02X%02X wrap=word'",
  235. fontName, textDefinition._fontSize, textAlign, textValign, richTextStyle,
  236. textDefinition._fontFillColor.b, textDefinition._fontFillColor.g, textDefinition._fontFillColor.r);
  237. auto entry = evas_object_textblock_add(canvas);
  238. auto st = evas_textblock_style_new();
  239. evas_textblock_style_set(st, richText);
  240. evas_object_textblock_style_set(entry, st);
  241. evas_textblock_style_free(st);
  242. evas_object_textblock_valign_set(entry, valignment);
  243. evas_object_textblock_text_markup_set(entry, evas_textblock_text_utf8_to_markup(entry, text));
  244. free(richText);
  245. evas_object_resize(entry, 2048, 0);//allocate one default size first.
  246. Evas_Coord width = textDefinition._dimensions.width;
  247. Evas_Coord height = textDefinition._dimensions.height;
  248. if(0==width*height)
  249. {
  250. evas_object_textblock_size_formatted_get(entry, &width, &height);
  251. }
  252. evas_object_resize(entry, width, height);
  253. //init canvas
  254. evas_output_size_set(canvas, width, height);
  255. evas_output_viewport_set(canvas, 0, 0, width, height);
  256. auto einfo = (Evas_Engine_Info_Buffer *)evas_engine_info_get(canvas);
  257. if (!einfo){
  258. log("ERROR: could not get evas engine info!");
  259. evas_free(canvas);
  260. return ;
  261. }
  262. // ARGB32 is sizeof(int), that is 4 bytes, per pixel
  263. bitmapWidth = width;
  264. bitmapHeight = height;
  265. void* pixels = malloc(bitmapWidth * bitmapHeight * 4);
  266. if (!pixels){
  267. log("ERROR: could not allocate canvas pixels!");
  268. evas_free(canvas);
  269. return;
  270. }
  271. einfo->info.depth_type = EVAS_ENGINE_BUFFER_DEPTH_ARGB32;
  272. einfo->info.dest_buffer = pixels;
  273. einfo->info.dest_buffer_row_bytes = bitmapWidth * sizeof(int);
  274. einfo->info.use_color_key = 0;
  275. einfo->info.alpha_threshold = 0;
  276. einfo->info.func.new_update_region = NULL;
  277. einfo->info.func.free_update_region = NULL;
  278. evas_engine_info_set(canvas, (Evas_Engine_Info *)einfo);
  279. //render
  280. evas_object_show(entry);
  281. auto updates = evas_render_updates(canvas);
  282. evas_render_updates_free(updates);
  283. bitmapData.fastSet((unsigned char*)pixels,bitmapWidth * bitmapHeight * 4);
  284. evas_free(canvas);
  285. //evas_shutdown();//not necessary, removed when use Tizen 2.4.
  286. }
  287. Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
  288. {
  289. Data ret;
  290. do
  291. {
  292. getTextBitmapData(text, textDefinition, ret, width, height);
  293. hasPremultipliedAlpha = true;
  294. } while (0);
  295. return ret;
  296. }
  297. void Device::setKeepScreenOn(bool value)
  298. {
  299. }
  300. void Device::vibrate(float /*duration*/)
  301. {
  302. }
  303. NS_CC_END
  304. #endif // CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN