summaryrefslogtreecommitdiff
path: root/randr/rrlease.c
blob: e25d9ca994404b747c9659c1079e1b2151b966c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright © 2017 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include "randrstr.h"
#include "swaprep.h"
#include <unistd.h>

RESTYPE RRLeaseType;

/*
 * Notify of some lease change
 */
void
RRDeliverLeaseEvent(ClientPtr client, WindowPtr window)
{
    ScreenPtr screen = window->drawable.pScreen;
    rrScrPrivPtr scr_priv = rrGetScrPriv(screen);
    RRLeasePtr lease;

    UpdateCurrentTimeIf();
    xorg_list_for_each_entry(lease, &scr_priv->leases, list) {
        if (lease->id != None && (lease->state == RRLeaseCreating ||
                                  lease->state == RRLeaseTerminating))
        {
            xRRLeaseNotifyEvent le = (xRRLeaseNotifyEvent) {
                .type = RRNotify + RREventBase,
                .subCode = RRNotify_Lease,
                .timestamp = currentTime.milliseconds,
                .window = window->drawable.id,
                .lease = lease->id,
                .created = lease->state == RRLeaseCreating,
            };
            WriteEventsToClient(client, 1, (xEvent *) &le);
        }
    }
}

/*
 * Change the state of a lease and let anyone watching leases know
 */
static void
RRLeaseChangeState(RRLeasePtr lease, RRLeaseState old, RRLeaseState new)
{
    ScreenPtr screen = lease->screen;
    rrScrPrivPtr scr_priv = rrGetScrPriv(screen);

    lease->state = old;
    scr_priv->leasesChanged = TRUE;
    RRSetChanged(lease->screen);
    RRTellChanged(lease->screen);
    scr_priv->leasesChanged = FALSE;
    lease->state = new;
}

/*
 * Allocate and initialize a lease
 */
static RRLeasePtr
RRLeaseAlloc(ScreenPtr screen, RRLease lid, int numCrtcs, int numOutputs)
{
    RRLeasePtr lease;
    lease = calloc(1,
                   sizeof(RRLeaseRec) +
                   numCrtcs * sizeof (RRCrtcPtr) +
                   numOutputs * sizeof(RROutputPtr));
    if (!lease)
        return NULL;
    lease->screen = screen;
    xorg_list_init(&lease->list);
    lease->id = lid;
    lease->state = RRLeaseCreating;
    lease->numCrtcs = numCrtcs;
    lease->numOutputs = numOutputs;
    lease->crtcs = (RRCrtcPtr *) (lease + 1);
    lease->outputs = (RROutputPtr *) (lease->crtcs + numCrtcs);
    return lease;
}

/*
 * Check if a crtc is leased
 */
Bool
RRCrtcIsLeased(RRCrtcPtr crtc)
{
    ScreenPtr screen = crtc->pScreen;
    rrScrPrivPtr scr_priv = rrGetScrPriv(screen);
    RRLeasePtr lease;
    int c;

    xorg_list_for_each_entry(lease, &scr_priv->leases, list) {
        for (c = 0; c < lease->numCrtcs; c++)
            if (lease->crtcs[c] == crtc)
                return TRUE;
    }
    return FALSE;
}

/*
 * Check if an output is leased
 */
Bool
RROutputIsLeased(RROutputPtr output)
{
    ScreenPtr screen = output->pScreen;
    rrScrPrivPtr scr_priv = rrGetScrPriv(screen);
    RRLeasePtr lease;
    int o;

    xorg_list_for_each_entry(lease, &scr_priv->leases, list) {
        for (o = 0; o < lease->numOutputs; o++)
            if (lease->outputs[o] == output)
                return TRUE;
    }
    return FALSE;
}

/*
 * A lease has been terminated.
 * The driver is responsible for noticing and
 * calling this function when that happens
 */

void
RRLeaseTerminated(RRLeasePtr lease)
{
    /* Notify clients with events, but only if this isn't during lease creation */
    if (lease->state == RRLeaseRunning)
        RRLeaseChangeState(lease, RRLeaseTerminating, RRLeaseTerminating);

    if (lease->id != None)
        FreeResource(lease->id, RT_NONE);

    xorg_list_del(&lease->list);
}

/*
 * A lease is completely shut down and is
 * ready to be deallocated
 */

void
RRLeaseFree(RRLeasePtr lease)
{
    free(lease);
}

/*
 * Ask the driver to terminate a lease. The
 * driver will call RRLeaseTerminated when that has
 * finished, which may be some time after this function returns
 * if the driver operation is asynchronous
 */
void
RRTerminateLease(RRLeasePtr lease)
{
    ScreenPtr screen = lease->screen;
    rrScrPrivPtr scr_priv = rrGetScrPriv(screen);

    scr_priv->rrTerminateLease(screen, lease);
}

/*
 * Destroy a lease resource ID. All this
 * does is note that the lease no longer has an ID, and
 * so doesn't appear over the protocol anymore.
 */
static int
RRLeaseDestroyResource(void *value, XID pid)
{
    RRLeasePtr lease = value;

    lease->id = None;
    return 1;
}

/*
 * Create the lease resource type during server initialization
 */
Bool
RRLeaseInit(void)
{
    RRLeaseType = CreateNewResourceType(RRLeaseDestroyResource, "LEASE");
    if (!RRLeaseType)
        return FALSE;
    return TRUE;
}

int
ProcRRCreateLease(ClientPtr client)
{
    REQUEST(xRRCreateLeaseReq);
    xRRCreateLeaseReply rep;
    WindowPtr window;
    ScreenPtr screen;
    rrScrPrivPtr scr_priv;
    RRLeasePtr lease;
    RRCrtc *crtcIds;
    RROutput *outputIds;
    int fd;
    int rc;
    unsigned long len;
    int c, o;

    REQUEST_AT_LEAST_SIZE(xRRCreateLeaseReq);

    LEGAL_NEW_RESOURCE(stuff->lid, client);

    rc = dixLookupWindow(&window, stuff->window, client, DixGetAttrAccess);
    if (rc != Success)
        return rc;

    len = client->req_len - bytes_to_int32(sizeof(xRRCreateLeaseReq));

    if (len != stuff->nCrtcs + stuff->nOutputs)
        return BadLength;

    screen = window->drawable.pScreen;
    scr_priv = rrGetScrPriv(screen);

    if (!scr_priv)
        return BadMatch;

    if (!scr_priv->rrCreateLease)
        return BadMatch;

    /* Allocate a structure to hold all of the lease information */

    lease = RRLeaseAlloc(screen, stuff->lid, stuff->nCrtcs, stuff->nOutputs);
    if (!lease)
        return BadAlloc;

    /* Look up all of the crtcs */
    crtcIds = (RRCrtc *) (stuff + 1);
    for (c = 0; c < stuff->nCrtcs; c++) {
        RRCrtcPtr crtc;

	rc = dixLookupResourceByType((void **)&crtc, crtcIds[c],
                                     RRCrtcType, client, DixSetAttrAccess);

        if (rc != Success) {
            client->errorValue = crtcIds[c];
            goto bail_lease;
        }

        if (RRCrtcIsLeased(crtc)) {
            client->errorValue = crtcIds[c];
            rc = BadAccess;
            goto bail_lease;
        }

        lease->crtcs[c] = crtc;
    }

    /* Look up all of the outputs */
    outputIds = (RROutput *) (crtcIds + stuff->nCrtcs);
    for (o = 0; o < stuff->nOutputs; o++) {
        RROutputPtr output;

	rc = dixLookupResourceByType((void **)&output, outputIds[o],
                                     RROutputType, client, DixSetAttrAccess);
        if (rc != Success) {
            client->errorValue = outputIds[o];
            goto bail_lease;
        }

        if (RROutputIsLeased(output)) {
            client->errorValue = outputIds[o];
            rc = BadAccess;
            goto bail_lease;
        }

        lease->outputs[o] = output;
    }

    rc = scr_priv->rrCreateLease(screen, lease, &fd);
    if (rc != Success)
        goto bail_lease;

    xorg_list_add(&scr_priv->leases, &lease->list);

    if (!AddResource(stuff->lid, RRLeaseType, lease)) {
        close(fd);
        return BadAlloc;
    }

    if (WriteFdToClient(client, fd, TRUE) < 0) {
        RRTerminateLease(lease);
        close(fd);
        return BadAlloc;
    }

    RRLeaseChangeState(lease, RRLeaseCreating, RRLeaseRunning);

    rep = (xRRCreateLeaseReply) {
        .type = X_Reply,
        .nfd = 1,
        .sequenceNumber = client->sequence,
        .length = 0,
    };

    if (client->swapped) {
        swaps(&rep.sequenceNumber);
        swapl(&rep.length);
    }

    WriteToClient(client, sizeof (rep), &rep);

    return Success;

bail_lease:
    free(lease);
    return rc;
}

int
ProcRRFreeLease(ClientPtr client)
{
    REQUEST(xRRFreeLeaseReq);
    RRLeasePtr lease;

    REQUEST_SIZE_MATCH(xRRFreeLeaseReq);

    VERIFY_RR_LEASE(stuff->lid, lease, DixDestroyAccess);

    if (stuff->terminate)
        RRTerminateLease(lease);
    else
        /* Get rid of the resource database entry */
        FreeResource(stuff->lid, RT_NONE);

    return Success;
}