summaryrefslogtreecommitdiff
path: root/riscos/mouseactions.c
blob: aa068d00df9c6db435e20f542e15e47b563b1b87 (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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
 */

#include <math.h>
#include <stdlib.h>
#include "oslib/os.h"
#include "netsurf/utils/config.h"
#include "netsurf/desktop/browser.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/menus.h"
#include "netsurf/riscos/options.h"
#include "netsurf/utils/log.h"

typedef enum {
	mouseaction_NONE,
	mouseaction_BACK, mouseaction_FORWARD,
	mouseaction_RELOAD, mouseaction_PARENT,
	mouseaction_NEWWINDOW_OR_LINKFG, mouseaction_DUPLICATE_OR_LINKBG,
	mouseaction_TOGGLESIZE, mouseaction_ICONISE, mouseaction_CLOSE
} mouseaction;


static double calculate_angle(double x, double y);
static int anglesDifferent(double a, double b);
static mouseaction ro_gui_try_mouse_action(void);


void ro_gui_mouse_action(struct gui_window *g) {

  int x, y;
  mouseaction ma = mouseaction_NONE;

  if (option_use_mouse_gestures)
    ma = ro_gui_try_mouse_action();

  if (ma == mouseaction_NONE) {

    os_mouse(&x, &y, NULL, NULL);
    ro_gui_menu_create(browser_menu, x - 64, y, g->window);
  }
  else {

    LOG(("MOUSE GESTURE %d", ma));
    switch (ma) {

      case mouseaction_BACK:
/*            browser_window_back(g->data.browser.bw); */
           break;

      case mouseaction_FORWARD:
/*            browser_window_forward(g->data.browser.bw); */
           break;

      case mouseaction_RELOAD:
/*           browser_window_open_location_historical(g->data.browser.bw,
           		g->data.browser.bw->url
#ifdef WITH_POST
           		, 0, 0
#endif
           		);*/
           break;

      default: break;
    }
  }
}

double calculate_angle(double x, double y) {

  double a;

  if (x == 0.0) {

    if (y < 0.0)
      a = 0.0;
    else
      a = M_PI;
  }
  else {

    a = atan(y / x);

    if (x > 0.0)
      a += M_PI_2;
    else
      a -= M_PI_2;
  }

  return a;
}

int anglesDifferent(double a, double b) {

  double c;

  if (a < 0.0)
    a += M_2_PI;

  if (b < 0.0)
    b += M_2_PI;

  if (a > M_2_PI)
    a -= M_2_PI;

  if (b > M_2_PI)
    b -= M_2_PI;

  c = a - b;

  if (c < 0.0)
    c += M_2_PI;

  if (c > M_2_PI)
    c -= M_2_PI;

  return (c > M_PI / 6.0);
}

#define STOPPED 2
#define THRESHOLD 16
#define DAMPING 1

/* TODO - something in the following function causes a segfault when
 *        mouse actions are turned on. Obviously this needs fixing.
 */

mouseaction ro_gui_try_mouse_action(void) {

  os_coord start, current, last, offset, moved;
  double offsetDistance, movedDistance;
  double angle, oldAngle=0.0;
  bits z;
  os_t now;
  int status;
  int m;
  enum {move_NONE, move_LEFT, move_RIGHT, move_UP, move_DOWN} moves[5];

  moves[0] = move_NONE;
  m = 1;

  os_mouse(&start.x, &start.y, &z, &now);
  status = 0;

  do {

    os_mouse(&current.x, &current.y, &z, &now);
    offset.x = current.x - start.x;
    offset.y = current.y - start.y;
    moved.x = current.x - last.x;
    moved.y = current.y - last.y;
    offsetDistance = sqrt((float)(offset.x * offset.x + offset.y * offset.y));
    if (moved.x > 0 || moved.y > 0)
      movedDistance = sqrt((float)(moved.x * moved.x + moved.y * moved.y));
    else
      movedDistance = 0.0;

    angle = calculate_angle((float)offset.x, (float)offset.y);

    switch (status) {

      case 1:
        if (movedDistance < STOPPED ||
         (movedDistance > STOPPED*2.0 && anglesDifferent(angle, oldAngle))) {

          start.x = current.x;
          start.y = current.y;
          status = 0;
        }
        break;

      case 0:
        if (offsetDistance > THRESHOLD) {

          if (fabs((float)offset.x) > fabs((float)offset.y)) {

            if (fabs((float)offset.y) < fabs((float)offset.x) * DAMPING &&
                fabs((float)offset.x) > THRESHOLD*0.75) {

              if (offset.x < 0)
                moves[m] = move_LEFT;
              else
                moves[m] = move_RIGHT;

              if (moves[m] != moves[m-1])
                m++;

              start.x = current.x;
              start.y = current.y;
              oldAngle = angle;
              status = 1;
            }
          }
          else if (fabs((float)offset.y) > fabs((float)offset.x)) {

            if (fabs((float)offset.x) < fabs((float)offset.y) * DAMPING &&
                fabs((float)offset.y) > THRESHOLD*0.75) {

              if (offset.y < 0)
                moves[m] = move_DOWN;
              else
                moves[m] = move_UP;

              if (moves[m] != moves[m-1])
                m++;

              start.x = current.x;
              start.y = current.y;
              oldAngle = angle;
              status = 1;
            }
          }
        }
        break;
    }

    last.x = current.x;
    last.y = current.y;

  } while ((z & 2) != 0 && m < 4);

  LOG(("MOUSEACTIONS: %d %d %d %d\n", moves[0], moves[1],
                                      moves[2], moves[3]));
  if (m == 2) {

    switch (moves[1]) {

      case move_LEFT:
        LOG(("mouse action: go back"));
        return mouseaction_BACK;

      case move_RIGHT:
        LOG(("MOUSE ACTION: GO FORWARD"));
        return mouseaction_FORWARD;

      case move_DOWN:
        LOG(("mouse action: create new window // open link in new window, foreground"));
        return mouseaction_NEWWINDOW_OR_LINKFG;

      default: break;
    }
  }

  if (m == 3) {

    switch (moves[1]) {

      case move_UP:
        switch (moves[2]) {

          case move_DOWN:
            LOG(("mouse action: reload"));
            return mouseaction_RELOAD;

          case move_RIGHT:
            LOG(("mouse action: toggle size"));
            return mouseaction_TOGGLESIZE;

          case move_LEFT:
            LOG(("mouse action: parent directroy"));
            return mouseaction_PARENT;

          default: break;
        }
        break;

      case move_DOWN:
        switch (moves[2]) {

          case move_LEFT:
            LOG(("mouse action: iconise"));
            return mouseaction_ICONISE;

          case move_UP:
            LOG(("mouse action: duplicate // open link in new window, background"));
            return mouseaction_DUPLICATE_OR_LINKBG;

          case move_RIGHT:
            LOG(("mouse action: close"));
            return mouseaction_CLOSE;

          default: break;
        }
        break;

      default: break;
    }
  }

  if (m == 4) {

    if (moves[1] == move_RIGHT && moves[2] == move_LEFT &&
        moves[3] == move_RIGHT) {

      LOG(("mouse action: close window"));
      return mouseaction_CLOSE;
    }
  }

  return mouseaction_NONE;
}