platform-x11.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <X11/Xlib.h>
  21. #include <X11/extensions/Xinerama.h>
  22. #include <unistd.h>
  23. #include <sstream>
  24. #include <locale.h>
  25. #include "platform.hpp"
  26. using namespace std;
  27. static inline bool check_path(const char* data, const char *path,
  28. string &output)
  29. {
  30. ostringstream str;
  31. str << path << data;
  32. output = str.str();
  33. printf("Attempted path: %s\n", output.c_str());
  34. return (access(output.c_str(), R_OK) == 0);
  35. }
  36. #define INSTALL_DATA_PATH OBS_INSTALL_PREFIX OBS_DATA_PATH "/obs-studio/"
  37. bool GetDataFilePath(const char *data, string &output)
  38. {
  39. char *data_path = getenv("OBS_DATA_PATH");
  40. if (data_path != NULL) {
  41. if (check_path(data, data_path, output))
  42. return true;
  43. }
  44. if (check_path(data, OBS_DATA_PATH "/obs-studio/", output))
  45. return true;
  46. if (check_path(data, INSTALL_DATA_PATH, output))
  47. return true;
  48. return false;
  49. }
  50. void GetMonitors(vector<MonitorInfo> &monitors)
  51. {
  52. int num_screens;
  53. XineramaScreenInfo *screens;
  54. int event_code = 0, error_code = 0;
  55. Display* display = XOpenDisplay(NULL);
  56. if (!XineramaQueryExtension(display, &event_code, &error_code)) {
  57. printf("Xinerama extension unavailable. We don't handle this "
  58. "yet.\n");
  59. return;
  60. }
  61. /* Do I need to make a call to XineramaQueryVersion...? */
  62. screens = XineramaQueryScreens(display, &num_screens);
  63. if (num_screens == 0 || !screens) {
  64. printf("Xinerama isn't active on this screen.\n");
  65. return;
  66. }
  67. monitors.clear();
  68. do {
  69. --num_screens;
  70. monitors.emplace_back(
  71. screens[num_screens].x_org,
  72. screens[num_screens].y_org,
  73. screens[num_screens].width,
  74. screens[num_screens].height
  75. );
  76. } while (num_screens > 0);
  77. XFree(screens);
  78. XCloseDisplay(display);
  79. }
  80. bool InitApplicationBundle()
  81. {
  82. return true;
  83. }
  84. string GetDefaultVideoSavePath()
  85. {
  86. return string(getenv("HOME"));
  87. }
  88. vector<string> GetPreferredLocales()
  89. {
  90. setlocale(LC_ALL, "");
  91. string messages = setlocale(LC_MESSAGES, NULL);
  92. if (!messages.size() || messages == "C" || messages == "POSIX")
  93. return {};
  94. if (messages.size() > 2)
  95. messages[2] = '-';
  96. for (auto &locale_pair : GetLocaleNames()) {
  97. auto &locale = locale_pair.first;
  98. if (locale == messages.substr(0, locale.size()))
  99. return {locale};
  100. if (locale.substr(0, 2) == messages.substr(0, 2))
  101. return {locale};
  102. }
  103. return {};
  104. }