evasapp.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 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. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "evasapp.h"
  19. #include <dirent.h>
  20. #include <algorithm>
  21. static void
  22. _on_resize(Ecore_Evas *ee)
  23. {
  24. EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
  25. int w, h;
  26. ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);
  27. app->resize(w, h);
  28. if (app->mResizeCb)
  29. app->mResizeCb(app->mResizeData, nullptr);
  30. }
  31. static void
  32. _on_delete(Ecore_Evas *ee)
  33. {
  34. EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
  35. if (app->mExitCb)
  36. app->mExitCb(app->mExitData, nullptr);
  37. ecore_main_loop_quit();
  38. ecore_evas_free(ee);
  39. }
  40. static Eina_Bool
  41. on_key_down(void *data, int /*type*/, void *event)
  42. {
  43. Ecore_Event_Key *keyData = (Ecore_Event_Key *)event;
  44. EvasApp *app = (EvasApp *) data;
  45. if (app && app->mKeyCb)
  46. app->mKeyCb(app->mKeyData, (void *)keyData->key);
  47. return false;
  48. }
  49. static void
  50. on_pre_render(Ecore_Evas *ee)
  51. {
  52. EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
  53. if (app->mRenderPreCb)
  54. app->mRenderPreCb(app->mRenderPreData, nullptr);
  55. }
  56. static void
  57. on_post_render(Ecore_Evas *ee)
  58. {
  59. EvasApp *app = (EvasApp *)ecore_evas_data_get(ee, "app");
  60. if (app && app->mRenderPostCb)
  61. app->mRenderPostCb(app->mRenderPostData, nullptr);
  62. }
  63. EvasApp::EvasApp(int w, int h)
  64. {
  65. if (!ecore_evas_init())
  66. return;
  67. mw = w;
  68. mh = h;
  69. //setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);
  70. mEcoreEvas = ecore_evas_new(NULL, 0, 0, mw, mh, NULL);
  71. if (!mEcoreEvas) return;
  72. }
  73. void
  74. EvasApp::setup()
  75. {
  76. ecore_evas_data_set(mEcoreEvas, "app", this);
  77. ecore_evas_callback_resize_set(mEcoreEvas, _on_resize);
  78. ecore_evas_callback_delete_request_set(mEcoreEvas, _on_delete);
  79. ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, on_key_down, this);
  80. ecore_evas_callback_pre_render_set(mEcoreEvas, on_pre_render);
  81. ecore_evas_callback_post_render_set(mEcoreEvas, on_post_render);
  82. ecore_evas_show(mEcoreEvas);
  83. mEvas = ecore_evas_get(mEcoreEvas);
  84. mBackground = evas_object_rectangle_add(mEvas);
  85. evas_object_color_set(mBackground, 70, 70, 70, 255);
  86. evas_object_show(mBackground);
  87. }
  88. void
  89. EvasApp::resize(int w, int h)
  90. {
  91. evas_object_resize(mBackground, w, h);
  92. mw = w;
  93. mh = h;
  94. }
  95. void EvasApp::run()
  96. {
  97. resize(mw, mh);
  98. ecore_main_loop_begin();
  99. ecore_evas_shutdown();
  100. }
  101. static bool isJsonFile(const char *filename) {
  102. const char *dot = strrchr(filename, '.');
  103. if(!dot || dot == filename) return false;
  104. return !strcmp(dot + 1, "json");
  105. }
  106. std::vector<std::string>
  107. EvasApp::jsonFiles(const std::string &dirName, bool /*recurse*/)
  108. {
  109. DIR *d;
  110. struct dirent *dir;
  111. std::vector<std::string> result;
  112. d = opendir(dirName.c_str());
  113. if (d) {
  114. while ((dir = readdir(d)) != NULL) {
  115. if (isJsonFile(dir->d_name))
  116. result.push_back(dirName + dir->d_name);
  117. }
  118. closedir(d);
  119. }
  120. std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
  121. return result;
  122. }