DisplayLink.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Badge.h>
  27. #include <AK/Function.h>
  28. #include <AK/HashMap.h>
  29. #include <LibGUI/DisplayLink.h>
  30. #include <LibGUI/WindowServerConnection.h>
  31. namespace GUI {
  32. class DisplayLinkCallback : public RefCounted<DisplayLinkCallback> {
  33. public:
  34. DisplayLinkCallback(i32 link_id, Function<void(i32)> callback)
  35. : m_link_id(link_id)
  36. , m_callback(move(callback))
  37. {
  38. }
  39. void invoke()
  40. {
  41. m_callback(m_link_id);
  42. }
  43. private:
  44. i32 m_link_id { 0 };
  45. Function<void(i32)> m_callback;
  46. };
  47. static HashMap<i32, RefPtr<DisplayLinkCallback>>& callbacks()
  48. {
  49. static HashMap<i32, RefPtr<DisplayLinkCallback>>* map;
  50. if (!map)
  51. map = new HashMap<i32, RefPtr<DisplayLinkCallback>>;
  52. return *map;
  53. }
  54. static i32 s_next_callback_id = 1;
  55. i32 DisplayLink::register_callback(Function<void(i32)> callback)
  56. {
  57. if (callbacks().is_empty())
  58. WindowServerConnection::the().post_message(Messages::WindowServer::EnableDisplayLink());
  59. i32 callback_id = s_next_callback_id++;
  60. callbacks().set(callback_id, adopt(*new DisplayLinkCallback(callback_id, move(callback))));
  61. return callback_id;
  62. }
  63. bool DisplayLink::unregister_callback(i32 callback_id)
  64. {
  65. ASSERT(callbacks().contains(callback_id));
  66. callbacks().remove(callback_id);
  67. if (callbacks().is_empty())
  68. WindowServerConnection::the().post_message(Messages::WindowServer::DisableDisplayLink());
  69. return true;
  70. }
  71. void DisplayLink::notify(Badge<WindowServerConnection>)
  72. {
  73. auto copy_of_callbacks = callbacks();
  74. for (auto& it : copy_of_callbacks)
  75. it.value->invoke();
  76. }
  77. }