Implement selection offers from compositor to clients.
[profile/ivi/qtwayland.git] / src / compositor / wayland_wrapper / wldatadevice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Compositor.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "wldatadevice.h"
42
43 #include "wldatasource.h"
44 #include "wldataoffer.h"
45 #include "wldatadevicemanager.h"
46
47 #include <stdlib.h>
48
49 #include <QDebug>
50
51 namespace Wayland {
52
53 void DataDevice::start_drag(struct wl_client *client,
54                    struct wl_resource *resource,
55                    struct wl_resource *source,
56                    struct wl_resource *surface,
57                    uint32_t time)
58 {
59     DataDevice *data_device = static_cast<DataDevice *>(resource->data);
60     DataSource *data_source = static_cast<DataSource *>(source->data);
61
62
63 }
64
65 void DataDevice::attach(struct wl_client *client,
66                struct wl_resource *resource,
67                uint32_t time,
68                struct wl_resource *buffer,
69                int32_t x,
70                int32_t y)
71 {
72
73 }
74
75 void DataDevice::set_selection(struct wl_client *client,
76                       struct wl_resource *data_device_resource,
77                       struct wl_resource *source,
78                       uint32_t time)
79 {
80     DataDevice *data_device = static_cast<DataDevice *>(data_device_resource->data);
81     DataSource *data_source = static_cast<DataSource *>(source->data);
82
83     data_device->m_data_device_manager->setCurrentSelectionSource(data_source);
84
85 }
86
87 const struct wl_data_device_interface DataDevice::data_device_interface = {
88     DataDevice::start_drag,
89     DataDevice::attach,
90     DataDevice::set_selection
91 };
92
93 DataDevice::DataDevice(DataDeviceManager *data_device_manager, struct wl_client *client, uint32_t id)
94     : m_data_device_manager(data_device_manager)
95     , m_sent_selection_time(0)
96 {
97
98     static int i = 0;
99     qDebug() << "data device" << ++i;
100     m_data_device_resource =
101             wl_client_add_object(client,&wl_data_device_interface,&data_device_interface,id, this);
102 }
103
104 void DataDevice::sendSelectionFocus()
105 {
106     if (m_data_device_manager->offerFromCompositorToClient(m_data_device_resource))
107         return;
108
109     DataSource *source = m_data_device_manager->currentSelectionSource();
110     if (!source) {
111         return;
112     }
113     if (source->time() > m_sent_selection_time) { //this makes sure we don't resend
114         if (source->client() != m_data_device_resource->client) { //don't send selection to the client that owns the selection
115             DataOffer *data_offer = m_data_device_manager->currentSelectionSource()->dataOffer();
116             wl_resource *client_resource =
117                     data_offer->addDataDeviceResource(m_data_device_resource);
118             qDebug() << "sending data_offer for source" << source;
119             wl_resource_post_event(m_data_device_resource,WL_DATA_DEVICE_SELECTION,client_resource);
120             m_sent_selection_time = source->time();
121         }
122     }
123 }
124
125 struct wl_resource *DataDevice::dataDeviceResource() const
126 {
127     return m_data_device_resource;
128 }
129
130 }