os_window_linux.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "config.h"
  23. #if CROWN_PLATFORM_LINUX
  24. #include "os_window_linux.h"
  25. #include "assert.h"
  26. #include "string_utils.h"
  27. #include "log.h"
  28. namespace crown
  29. {
  30. Display* m_x11_display = NULL;
  31. Window m_x11_window = None;
  32. void oswindow_set_window(Display* dpy, Window win)
  33. {
  34. m_x11_display = dpy;
  35. m_x11_window = win;
  36. }
  37. OsWindow::OsWindow()
  38. : m_x(0)
  39. , m_y(0)
  40. , m_width(0)
  41. , m_height(0)
  42. , m_resizable(true)
  43. {
  44. }
  45. OsWindow::~OsWindow()
  46. {
  47. }
  48. void OsWindow::show()
  49. {
  50. XMapRaised(m_x11_display, m_x11_window);
  51. }
  52. void OsWindow::hide()
  53. {
  54. XUnmapWindow(m_x11_display, m_x11_window);
  55. }
  56. void OsWindow::get_size(uint32_t& width, uint32_t& height)
  57. {
  58. width = m_width;
  59. height = m_height;
  60. }
  61. void OsWindow::get_position(uint32_t& x, uint32_t& y)
  62. {
  63. x = m_x;
  64. y = m_y;
  65. }
  66. void OsWindow::resize(uint32_t width, uint32_t height)
  67. {
  68. XResizeWindow(m_x11_display, m_x11_window, width, height);
  69. }
  70. void OsWindow::move(uint32_t x, uint32_t y)
  71. {
  72. XMoveWindow(m_x11_display, m_x11_window, x, y);
  73. }
  74. void OsWindow::minimize()
  75. {
  76. XIconifyWindow(m_x11_display, m_x11_window, DefaultScreen(m_x11_display));
  77. }
  78. void OsWindow::restore()
  79. {
  80. XMapRaised(m_x11_display, m_x11_window);
  81. }
  82. bool OsWindow::is_resizable() const
  83. {
  84. return m_resizable;
  85. }
  86. void OsWindow::set_resizable(bool resizable)
  87. {
  88. XSizeHints hints;
  89. hints.flags = PMinSize | PMaxSize;
  90. hints.min_width = resizable ? 1 : m_width;
  91. hints.min_height = resizable ? 1 : m_height;
  92. hints.max_width = resizable ? 65535 : m_width;
  93. hints.max_height = resizable ? 65535 : m_height;
  94. XSetWMNormalHints(m_x11_display, m_x11_window, &hints);
  95. m_resizable = resizable;
  96. }
  97. char* OsWindow::title()
  98. {
  99. static char title[1024];
  100. char* tmp_title;
  101. XFetchName(m_x11_display, m_x11_window, &tmp_title);
  102. string::strncpy(title, tmp_title, 1024);
  103. XFree(tmp_title);
  104. return title;
  105. }
  106. void OsWindow::set_title(const char* title)
  107. {
  108. XStoreName(m_x11_display, m_x11_window, title);
  109. }
  110. } // namespace crown
  111. #endif // CROWN_PLATFORM_LINUX