summaryrefslogtreecommitdiff
path: root/src/xsync.c
blob: 1074a5c067bd993aeda404d5410c66cbb96ec41a (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
/*      $Id$

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2, or (at your option)
        any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., Inc., 51 Franklin Street, Fifth Floor, Boston,
        MA 02110-1301, USA.


        xfwm4    - (c) 2002-2015 Olivier Fourdan

 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xsync.h"

#ifdef HAVE_XSYNC

/* See http://fishsoup.net/misc/wm-spec-synchronization.html */
#ifndef XSYNC_VALUE_INCREMENT
#define XSYNC_VALUE_INCREMENT 240
#endif

static void
addToXSyncValue (XSyncValue *value, gint i)
{
    XSyncValue add;
    int overflow;

    XSyncIntToValue (&add, i);
    XSyncValueAdd (value, *value, add, &overflow);
}

gboolean
clientCreateXSyncAlarm (Client *c)
{
    ScreenInfo *screen_info;
    DisplayInfo *display_info;
    XSyncAlarmAttributes attrs;

    g_return_val_if_fail (c != NULL, FALSE);
    g_return_val_if_fail (c->xsync_counter != None, FALSE);

    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    screen_info = c->screen_info;
    display_info = screen_info->display_info;

    clientDestroyXSyncAlarm (c);
    if (FLAG_TEST (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER))
    {
        /* Get the counter value from the client, if not, bail out... */
        if (!XSyncQueryCounter(display_info->dpy, c->xsync_counter, &c->xsync_value))
        {
            FLAG_UNSET (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER);
            return FALSE;
        }
    }
    else
    {
        XSyncIntToValue (&c->xsync_value, 0);
        XSyncSetCounter (display_info->dpy, c->xsync_counter, c->xsync_value);
    }

    c->next_xsync_value = c->xsync_value;
    if (!FLAG_TEST (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER) ||
        (XSyncValueLow32(c->next_xsync_value) % 2 == 0))
    {
        addToXSyncValue (&c->next_xsync_value, 1);
    }

    attrs.trigger.counter = c->xsync_counter;
    XSyncIntToValue (&attrs.delta, 1);
    XSyncIntToValue (&attrs.trigger.wait_value, 1);
    attrs.trigger.value_type = XSyncRelative;
    attrs.trigger.test_type = XSyncPositiveComparison;
    attrs.events = TRUE;
    c->xsync_alarm = XSyncCreateAlarm (display_info->dpy,
                                       XSyncCACounter |
                                       XSyncCADelta |
                                       XSyncCAEvents |
                                       XSyncCATestType |
                                       XSyncCAValue |
                                       XSyncCAValueType,
                                       &attrs);
    return (c->xsync_alarm != None);
}

void
clientDestroyXSyncAlarm (Client *c)
{
    ScreenInfo *screen_info;
    DisplayInfo *display_info;

    g_return_if_fail (c != NULL);
    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    clientXSyncClearTimeout (c);
    if (c->xsync_alarm != None)
    {
        screen_info = c->screen_info;
        display_info = screen_info->display_info;

        myDisplayErrorTrapPush (display_info);
        XSyncDestroyAlarm (display_info->dpy, c->xsync_alarm);
        myDisplayErrorTrapPopIgnored (display_info);
        c->xsync_alarm = None;
    }
}

gboolean
clientGetXSyncCounter (Client * c)
{
    ScreenInfo *screen_info;
    DisplayInfo *display_info;
    gulong *data;
    int nitems;

    g_return_val_if_fail (c != NULL, FALSE);
    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    screen_info = c->screen_info;
    display_info = screen_info->display_info;

    data = NULL;
    if (getCardinalList (display_info, c->window, NET_WM_SYNC_REQUEST_COUNTER, &data, &nitems))
    {
        switch (nitems)
        {
            case 0:
                FLAG_UNSET (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER);
                FLAG_UNSET (c->flags, CLIENT_FLAG_XSYNC_ENABLED);
                break;
            case 1:
                c->xsync_counter = (XSyncCounter) data[0];
                FLAG_UNSET (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER);
                FLAG_SET (c->flags, CLIENT_FLAG_XSYNC_ENABLED);
                break;
            default:
                c->xsync_counter = (XSyncCounter) data[1];
                FLAG_SET (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER);
                FLAG_SET (c->flags, CLIENT_FLAG_XSYNC_ENABLED);
                break;
        }
    }

    if (data)
    {
        XFree (data);
    }

    return FLAG_TEST (c->flags, CLIENT_FLAG_XSYNC_ENABLED);
}

void
clientXSyncClearTimeout (Client * c)
{
    g_return_if_fail (c != NULL);

    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    FLAG_UNSET (c->flags, CLIENT_FLAG_XSYNC_WAITING);
    if (c->xsync_timeout_id)
    {
        g_source_remove (c->xsync_timeout_id);
        c->xsync_timeout_id = 0;
    }
}

static gboolean
clientXSyncTimeout (gpointer data)
{
    Client *c;

    TRACE ("entering");

    c = (Client *) data;
    if (c)
    {
        g_warning ("XSync timeout for client \"%s\" (0x%lx)", c->name, c->window);
        clientXSyncClearTimeout (c);

        /* Disable XSync for this client */
        FLAG_UNSET (c->flags, CLIENT_FLAG_XSYNC_ENABLED);
    }
    return FALSE;
}

static void
clientXSyncResetTimeout (Client * c)
{
    g_return_if_fail (c != NULL);

    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    clientXSyncClearTimeout (c);
    c->xsync_timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
                                              CLIENT_XSYNC_TIMEOUT,
                                              clientXSyncTimeout, c, NULL);
}

void
clientXSyncRequest (Client * c)
{
    ScreenInfo *screen_info;
    DisplayInfo *display_info;
    XSyncValue next_value;
    XClientMessageEvent xev;

    g_return_if_fail (c != NULL);
    g_return_if_fail (c->window != None);

    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    if (FLAG_TEST (c->flags, CLIENT_FLAG_XSYNC_WAITING))
    {
        return;
    }

    screen_info = c->screen_info;
    display_info = screen_info->display_info;

    next_value = c->next_xsync_value;
    addToXSyncValue (&next_value, XSYNC_VALUE_INCREMENT);
    c->next_xsync_value = next_value;

    xev.type = ClientMessage;
    xev.window = c->window;
    xev.message_type = display_info->atoms[WM_PROTOCOLS];
    xev.format = 32;
    xev.data.l[0] = (long) display_info->atoms[NET_WM_SYNC_REQUEST];
    xev.data.l[1] = (long) myDisplayGetCurrentTime (display_info);
    xev.data.l[2] = (long) XSyncValueLow32 (next_value);
    xev.data.l[3] = (long) XSyncValueHigh32 (next_value);
    xev.data.l[4] = (long) (FLAG_TEST (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER) ? 1 : 0);
    XSendEvent (display_info->dpy, c->window, FALSE, NoEventMask, (XEvent *) &xev);

    clientXSyncResetTimeout (c);
    FLAG_SET (c->flags, CLIENT_FLAG_XSYNC_WAITING);
}

void
clientXSyncUpdateValue (Client *c, XSyncValue value)
{
    g_return_if_fail (c != NULL);
    TRACE ("client \"%s\" (0x%lx)", c->name, c->window);

    c->xsync_value = value;
    if (FLAG_TEST (c->flags, CLIENT_FLAG_XSYNC_EXT_COUNTER))
    {
        if (XSyncValueLow32(c->xsync_value) % 2 == 0)
        {
            addToXSyncValue (&value, 1);
        }
    }
    c->next_xsync_value = value;
    clientXSyncClearTimeout (c);
}

#endif /* HAVE_XSYNC */