Serenity Operating System
1/*
2 * Copyright (c) 2018-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
27#include <AK/Badge.h>
28#include <AK/SharedBuffer.h>
29#include <LibCore/EventLoop.h>
30#include <LibGUI/DragOperation.h>
31#include <LibGUI/WindowServerConnection.h>
32#include <LibGfx/Bitmap.h>
33
34namespace GUI {
35
36static DragOperation* s_current_drag_operation;
37
38DragOperation::DragOperation(Core::Object* parent)
39 : Core::Object(parent)
40{
41}
42
43DragOperation::~DragOperation()
44{
45}
46
47DragOperation::Outcome DragOperation::exec()
48{
49 ASSERT(!s_current_drag_operation);
50 ASSERT(!m_event_loop);
51
52 int bitmap_id = -1;
53 Gfx::Size bitmap_size;
54 RefPtr<Gfx::Bitmap> shared_bitmap;
55 if (m_bitmap) {
56 shared_bitmap = m_bitmap->to_shareable_bitmap();
57 shared_bitmap->shared_buffer()->share_with(WindowServerConnection::the().server_pid());
58 bitmap_id = shared_bitmap->shared_buffer_id();
59 bitmap_size = shared_bitmap->size();
60 }
61
62 auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::StartDrag>(m_text, m_data_type, m_data, bitmap_id, bitmap_size);
63 if (!response->started()) {
64 m_outcome = Outcome::Cancelled;
65 return m_outcome;
66 }
67
68 s_current_drag_operation = this;
69 m_event_loop = make<Core::EventLoop>();
70 auto result = m_event_loop->exec();
71 m_event_loop = nullptr;
72 dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
73 remove_from_parent();
74 s_current_drag_operation = nullptr;
75 return m_outcome;
76}
77
78void DragOperation::done(Outcome outcome)
79{
80 ASSERT(m_outcome == Outcome::None);
81 m_outcome = outcome;
82 m_event_loop->quit(0);
83}
84
85void DragOperation::notify_accepted(Badge<WindowServerConnection>)
86{
87 ASSERT(s_current_drag_operation);
88 s_current_drag_operation->done(Outcome::Accepted);
89}
90
91void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
92{
93 ASSERT(s_current_drag_operation);
94 s_current_drag_operation->done(Outcome::Cancelled);
95}
96
97}