File: pointer.c

package info (click to toggle)
notion 3%2B2012042300-1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 4,724 kB
  • sloc: ansic: 45,614; makefile: 544; sh: 409; perl: 113
file content (393 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download | duplicates (2)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * ion/ioncore/pointer.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2009. 
 *
 * See the included file LICENSE for details.
 */

#include "common.h"
#include "pointer.h"
#include "cursor.h"
#include "event.h"
#include "global.h"
#include "focus.h"
#include "regbind.h"
#include "grab.h"
#include "xwindow.h"


/*{{{ Variables */


static uint p_button=0, p_state=0;
static int p_x=-1, p_y=-1;
static int p_orig_x=-1, p_orig_y=-1;
static bool p_motion=FALSE;
static int p_clickcnt=0;
static Time p_time=0;
static int p_area=0;
static enum{ST_NO, ST_INIT, ST_HELD} p_grabstate=ST_NO;

static WButtonHandler *p_motion_end_handler=NULL;
static WMotionHandler *p_motion_handler=NULL;
static WMotionHandler *p_motion_begin_handler=NULL;
static GrabHandler *p_key_handler=NULL;
static GrabKilledHandler *p_killed_handler=NULL;

static Watch p_regwatch=WATCH_INIT, p_subregwatch=WATCH_INIT;

#define p_reg ((WRegion*)p_regwatch.obj)
#define p_subreg ((WRegion*)p_subregwatch.obj)


/*}}}*/


/*{{{ Handler setup */


bool ioncore_set_drag_handlers(WRegion *reg,
                         WMotionHandler *begin,
                         WMotionHandler *motion,
                         WButtonHandler *end,
                         GrabHandler *keypress,
                         GrabKilledHandler *killed)
{
    if(ioncore_pointer_grab_region()==NULL || p_motion)
        return FALSE;
    
    /* A motion handler set at this point may not set a begin handler */
    if(p_grabstate!=ST_HELD && begin!=NULL)
        return FALSE;
    
    if(p_reg!=reg){
        watch_setup(&p_regwatch, (Obj*)reg, NULL);
        watch_reset(&p_subregwatch);
    }
    
    p_motion_begin_handler=begin;
    p_motion_handler=motion;
    p_motion_end_handler=end;
    p_key_handler=keypress;
    p_killed_handler=killed;
    p_motion=TRUE;
    
    return TRUE;
}


/*}}}*/


/*{{{ Misc. */


static bool time_in_threshold(Time time)
{
    Time t;
    
    if(time<p_time)
        t=p_time-time;
    else
        t=time-p_time;
    
    return t<ioncore_g.dblclick_delay;
}


static bool motion_in_threshold(int x, int y)
{
    return (x>p_x-CF_DRAG_TRESHOLD && x<p_x+CF_DRAG_TRESHOLD &&
            y>p_y-CF_DRAG_TRESHOLD && y<p_y+CF_DRAG_TRESHOLD);
}


WRegion *ioncore_pointer_grab_region()
{
    if(p_grabstate==ST_NO)
        return NULL;
    return p_reg;
}


/*}}}*/


/*{{{ Call handlers */


static XEvent *p_curr_event=NULL;


XEvent *ioncore_current_pointer_event()
{
    return p_curr_event;
}


static void call_button(WBinding *binding, XButtonEvent *ev)
{
    WButtonHandler *fn;

    if(binding==NULL)
        return;

    p_curr_event=(XEvent*)ev;
    extl_call(binding->func, "ooo", NULL, p_reg, p_subreg, 
              (p_reg!=NULL ? p_reg->active_sub : NULL));
    p_curr_event=NULL;
}


static void call_motion(XMotionEvent *ev, int dx, int dy)
{
    if(p_motion_handler!=NULL && p_reg!=NULL){
        p_curr_event=(XEvent*)ev;
        p_motion_handler(p_reg, ev, dx, dy);
        p_curr_event=NULL;
    }
}


static void call_motion_end(XButtonEvent *ev)
{
    if(p_motion_end_handler!=NULL && p_reg!=NULL){
        p_curr_event=(XEvent*)ev;
        p_motion_end_handler(p_reg, ev);
        p_curr_event=NULL;
    }
}


static void call_motion_begin(WBinding *binding, XMotionEvent *ev,
                              int dx, int dy)
{
    WMotionHandler *fn;
    
    if(binding==NULL)
        return;

    p_curr_event=(XEvent*)ev;
    
    extl_call(binding->func, "oo", NULL, p_reg, p_subreg);
    
    if(p_motion_begin_handler!=NULL && p_reg!=NULL)
        p_motion_begin_handler(p_reg, ev, dx, dy);
    
    p_motion_begin_handler=NULL;
    
    p_curr_event=NULL;
}


/*}}}*/


/*{{{ ioncore_handle_button_press/release/motion */


static void finish_pointer()
{
    if(p_reg!=NULL)
        window_release((WWindow*)p_reg);
    p_grabstate=ST_NO;
    watch_reset(&p_subregwatch);
}


static bool handle_key(WRegion *reg, XEvent *ev)
{
    if(p_key_handler!=NULL){
        if(p_key_handler(reg, ev)){
            finish_pointer();
            return TRUE;
        }
    }
    return FALSE;
}


static void pointer_grab_killed(WRegion *unused)
{
    if(p_reg!=NULL && p_killed_handler!=NULL)
        p_killed_handler(p_reg);
    watch_reset(&p_regwatch);
    finish_pointer();
}


static bool listens_to(WRegion *reg, uint state, uint button, int area)
{
    static const int acts[]={BINDING_BUTTONMOTION, BINDING_BUTTONCLICK, 
                             BINDING_BUTTONDBLCLICK};
    static const int n_acts=3;
    int i;
    
    for(i=0; i<n_acts; i++){
        if(region_lookup_binding(reg, acts[i], state, button, area))
            return TRUE;
    }
    
    return FALSE;
}


static bool ioncore_dodo_handle_buttonpress(XButtonEvent *ev, bool sub)
{
    WBinding *pressbind=NULL;
    WRegion *reg=NULL;
    WRegion *subreg=NULL;
    uint button, state;
    bool dblclick;
    int area;
    
    state=ev->state;
    button=ev->button;
    
    reg=(WRegion*)XWINDOW_REGION_OF_T(ev->window, WWindow);
    
    if(reg==NULL)
        return FALSE;
    
    dblclick=(p_clickcnt==1 && time_in_threshold(ev->time) && 
              p_button==button && p_state==state);
    
    if(dblclick && p_reg!=reg){
        if(sub)
            return FALSE;
        dblclick=FALSE;
    }
    
    subreg=region_current(reg);
    area=window_press((WWindow*)reg, ev, &subreg);
    
    if(dblclick){
        pressbind=region_lookup_binding(reg, BINDING_BUTTONDBLCLICK, state,
                                             button, area);
    }
    
    if(pressbind==NULL){
        pressbind=region_lookup_binding(reg, BINDING_BUTTONPRESS, state,
                                             button, area);
    }
    
    if(pressbind==NULL && sub){
        /* If subwindow doesn't listen to state/button(/area) at all, return and
         * let the parent that has the event grabbed, handle it. Otherwise we 
         * fully block the parent.
         */
        if(!dblclick && !listens_to(reg, state, button, area))
            return FALSE;
    }
    
    p_motion=FALSE;
    p_motion_begin_handler=NULL;
    p_motion_handler=NULL;
    p_motion_end_handler=NULL;
    p_key_handler=NULL;
    p_killed_handler=NULL;
    p_grabstate=ST_INIT;
    p_button=button;
    p_state=state;
    p_orig_x=p_x=ev->x_root;
    p_orig_y=p_y=ev->y_root;
    p_time=ev->time;
    p_clickcnt=0;
    p_area=area;
    
    watch_setup(&p_regwatch, (Obj*)reg, NULL);
    if(subreg!=NULL)
        watch_setup(&p_subregwatch, (Obj*)subreg, NULL);

    ioncore_grab_establish(reg, handle_key, pointer_grab_killed, 0);
    p_grabstate=ST_HELD;
    
    if(pressbind!=NULL)
        call_button(pressbind, ev);
    
    return TRUE;
}


bool ioncore_do_handle_buttonpress(XButtonEvent *ev)
{
    /* Only one level of subwindows is supported... more would require
     * searching through the trees thanks to grabbed events being reported
     * relative to the outermost grabbing window.
     */
    if(ev->subwindow!=None && ev->state!=0){
        XButtonEvent ev2=*ev;
        ev2.window=ev->subwindow;
        ev2.subwindow=None;
        if(XTranslateCoordinates(ioncore_g.dpy, ev->window, ev2.window,
                                 ev->x, ev->y, &(ev2.x), &(ev2.y),
                                 &(ev2.subwindow))){
            if(ioncore_dodo_handle_buttonpress(&ev2, TRUE))
                return TRUE;
        }
    }
    
    return ioncore_dodo_handle_buttonpress(ev, FALSE);
}


bool ioncore_do_handle_buttonrelease(XButtonEvent *ev)
{
    WBinding *binding=NULL;
    
    if(p_button!=ev->button)
           return FALSE;

    if(p_reg!=NULL){
        if(p_motion==FALSE){
            p_clickcnt=1;
            binding=region_lookup_binding(p_reg, BINDING_BUTTONCLICK,
                                               p_state, p_button, p_area);
            if(binding!=NULL)
                call_button(binding, ev);
        }else{
            call_motion_end(ev);
        }
        
    }
    
    ioncore_grab_remove(handle_key);
    finish_pointer();
    
    return TRUE;
}


void ioncore_do_handle_motionnotify(XMotionEvent *ev)
{
    int dx, dy;
    WBinding *binding=NULL;
    
    if(p_reg==NULL)
        return;
    
    if(!p_motion){
        if(motion_in_threshold(ev->x_root, ev->y_root))
            return;
        binding=region_lookup_binding(p_reg, BINDING_BUTTONMOTION,
                                           p_state, p_button, p_area);
    }
    
    p_time=ev->time;
    dx=ev->x_root-p_x;
    dy=ev->y_root-p_y;
    p_x=ev->x_root;
    p_y=ev->y_root;    
    
    if(!p_motion){
        call_motion_begin(binding, ev, dx, dy);
        p_motion=TRUE;
    }else{
        call_motion(ev, dx, dy);
    }
}


/*}}}*/