platform-x11.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. /* Here we use xinerama to fetch data about monitor geometry
  16. * Even if there are not multiple monitors, this should still work.
  17. */
  18. #include <obs-config.h>
  19. #include "obs-app.hpp"
  20. #include <xcb/xcb.h>
  21. #include <xcb/xinerama.h>
  22. #include <xcb/randr.h>
  23. #include <unistd.h>
  24. #include <sstream>
  25. #include <locale.h>
  26. #include "platform.hpp"
  27. using namespace std;
  28. static inline bool check_path(const char* data, const char *path,
  29. string &output)
  30. {
  31. ostringstream str;
  32. str << path << data;
  33. output = str.str();
  34. printf("Attempted path: %s\n", output.c_str());
  35. return (access(output.c_str(), R_OK) == 0);
  36. }
  37. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX OBS_DATA_PATH "/obs-studio/"
  38. bool GetDataFilePath(const char *data, string &output)
  39. {
  40. char *data_path = getenv("OBS_DATA_PATH");
  41. if (data_path != NULL) {
  42. if (check_path(data, data_path, output))
  43. return true;
  44. }
  45. if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
  46. return true;
  47. if (check_path(data, INSTALL_DATA_PATH, output))
  48. return true;
  49. return false;
  50. }
  51. void GetMonitors(vector<MonitorInfo> &monitors)
  52. {
  53. xcb_connection_t* xcb_conn;
  54. monitors.clear();
  55. xcb_conn = xcb_connect(NULL, NULL);
  56. bool use_xinerama = false;
  57. if (xcb_get_extension_data(xcb_conn, &xcb_xinerama_id)->present) {
  58. xcb_xinerama_is_active_cookie_t xinerama_cookie;
  59. xcb_xinerama_is_active_reply_t* xinerama_reply = NULL;
  60. xinerama_cookie = xcb_xinerama_is_active(xcb_conn);
  61. xinerama_reply = xcb_xinerama_is_active_reply(xcb_conn,
  62. xinerama_cookie, NULL);
  63. if (xinerama_reply && xinerama_reply->state != 0)
  64. use_xinerama = true;
  65. free(xinerama_reply);
  66. }
  67. if (use_xinerama) {
  68. xcb_xinerama_query_screens_cookie_t screens_cookie;
  69. xcb_xinerama_query_screens_reply_t* screens_reply = NULL;
  70. xcb_xinerama_screen_info_iterator_t iter;
  71. screens_cookie = xcb_xinerama_query_screens(xcb_conn);
  72. screens_reply = xcb_xinerama_query_screens_reply(xcb_conn,
  73. screens_cookie, NULL);
  74. iter = xcb_xinerama_query_screens_screen_info_iterator(
  75. screens_reply);
  76. for(; iter.rem; xcb_xinerama_screen_info_next(&iter)) {
  77. monitors.emplace_back(iter.data->x_org,
  78. iter.data->y_org,
  79. iter.data->width,
  80. iter.data->height);
  81. }
  82. free(screens_reply);
  83. } else {
  84. // no xinerama so fall back to basic x11 calls
  85. xcb_screen_iterator_t iter;
  86. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  87. for(; iter.rem; xcb_screen_next(&iter)) {
  88. monitors.emplace_back(0,0,iter.data->width_in_pixels,
  89. iter.data->height_in_pixels);
  90. }
  91. }
  92. xcb_disconnect(xcb_conn);
  93. }
  94. bool InitApplicationBundle()
  95. {
  96. return true;
  97. }
  98. string GetDefaultVideoSavePath()
  99. {
  100. return string(getenv("HOME"));
  101. }
  102. vector<string> GetPreferredLocales()
  103. {
  104. setlocale(LC_ALL, "");
  105. string messages = setlocale(LC_MESSAGES, NULL);
  106. if (!messages.size() || messages == "C" || messages == "POSIX")
  107. return {};
  108. if (messages.size() > 2)
  109. messages[2] = '-';
  110. for (auto &locale_pair : GetLocaleNames()) {
  111. auto &locale = locale_pair.first;
  112. if (locale == messages.substr(0, locale.size()))
  113. return {locale};
  114. if (locale.substr(0, 2) == messages.substr(0, 2))
  115. return {locale};
  116. }
  117. return {};
  118. }