os_window_linux.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_PLATFORM_LINUX
  7. #include "os_window_linux.h"
  8. #include "assert.h"
  9. #include "string_utils.h"
  10. #include "log.h"
  11. namespace crown
  12. {
  13. Display* m_x11_display = NULL;
  14. Window m_x11_window = None;
  15. void oswindow_set_window(Display* dpy, Window win)
  16. {
  17. m_x11_display = dpy;
  18. m_x11_window = win;
  19. }
  20. OsWindow::OsWindow()
  21. : m_x(0)
  22. , m_y(0)
  23. , m_width(0)
  24. , m_height(0)
  25. , m_resizable(true)
  26. {
  27. }
  28. OsWindow::~OsWindow()
  29. {
  30. }
  31. void OsWindow::show()
  32. {
  33. XMapRaised(m_x11_display, m_x11_window);
  34. }
  35. void OsWindow::hide()
  36. {
  37. XUnmapWindow(m_x11_display, m_x11_window);
  38. }
  39. void OsWindow::get_size(uint32_t& width, uint32_t& height)
  40. {
  41. width = m_width;
  42. height = m_height;
  43. }
  44. void OsWindow::get_position(uint32_t& x, uint32_t& y)
  45. {
  46. x = m_x;
  47. y = m_y;
  48. }
  49. void OsWindow::resize(uint32_t width, uint32_t height)
  50. {
  51. XResizeWindow(m_x11_display, m_x11_window, width, height);
  52. }
  53. void OsWindow::move(uint32_t x, uint32_t y)
  54. {
  55. XMoveWindow(m_x11_display, m_x11_window, x, y);
  56. }
  57. void OsWindow::minimize()
  58. {
  59. XIconifyWindow(m_x11_display, m_x11_window, DefaultScreen(m_x11_display));
  60. }
  61. void OsWindow::restore()
  62. {
  63. XMapRaised(m_x11_display, m_x11_window);
  64. }
  65. bool OsWindow::is_resizable() const
  66. {
  67. return m_resizable;
  68. }
  69. void OsWindow::set_resizable(bool resizable)
  70. {
  71. XSizeHints hints;
  72. hints.flags = PMinSize | PMaxSize;
  73. hints.min_width = resizable ? 1 : m_width;
  74. hints.min_height = resizable ? 1 : m_height;
  75. hints.max_width = resizable ? 65535 : m_width;
  76. hints.max_height = resizable ? 65535 : m_height;
  77. XSetWMNormalHints(m_x11_display, m_x11_window, &hints);
  78. m_resizable = resizable;
  79. }
  80. char* OsWindow::title()
  81. {
  82. static char title[1024];
  83. char* tmp_title;
  84. XFetchName(m_x11_display, m_x11_window, &tmp_title);
  85. strncpy(title, tmp_title, 1024);
  86. XFree(tmp_title);
  87. return title;
  88. }
  89. void OsWindow::set_title(const char* title)
  90. {
  91. XStoreName(m_x11_display, m_x11_window, title);
  92. }
  93. } // namespace crown
  94. #endif // CROWN_PLATFORM_LINUX