Extracted tunnel, mouse, and keyboard handling from GuacamoleClient
[guacamole-common-js.git] / src / main / resources / mouse.js
1
2 /*
3  *  Guacamole - Clientless Remote Desktop
4  *  Copyright (C) 2010  Michael Jumper
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU Affero General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 function GuacamoleMouse(element) {
22
23         /*****************************************/
24         /*** Mouse Handler                     ***/
25         /*****************************************/
26
27
28     var mouseIndex = 0;
29
30     var mouseLeftButton   = 0;
31     var mouseMiddleButton = 0;
32     var mouseRightButton  = 0;
33
34     var mouseX = 0;
35     var mouseY = 0;
36
37     var absoluteMouseX = 0;
38     var absoluteMouseY = 0;
39
40
41     function getMouseState(up, down) {
42         var mouseState = new MouseEvent(mouseX, mouseY,
43                 mouseLeftButton, mouseMiddleButton, mouseRightButton, up, down);
44
45         return mouseState;
46     }
47
48
49     // Block context menu so right-click gets sent properly
50     element.oncontextmenu = function(e) {return false;};
51
52     element.onmousemove = function(e) {
53
54         e.stopPropagation();
55
56         absoluteMouseX = e.pageX;
57         absoluteMouseY = e.pageY;
58
59         mouseX = absoluteMouseX - element.offsetLeft;
60         mouseY = absoluteMouseY - element.offsetTop;
61
62         // This is all JUST so we can get the mouse position within the element
63         var parent = element.offsetParent;
64         while (parent) {
65             if (parent.offsetLeft && parent.offsetTop) {
66                 mouseX -= parent.offsetLeft;
67                 mouseY -= parent.offsetTop;
68             }
69             parent = parent.offsetParent;
70         }
71
72         movementHandler(getMouseState(0, 0));
73     };
74
75
76     element.onmousedown = function(e) {
77
78         e.stopPropagation();
79
80         switch (e.button) {
81             case 0:
82                 mouseLeftButton = 1;
83                 break;
84             case 1:
85                 mouseMiddleButton = 1;
86                 break;
87             case 2:
88                 mouseRightButton = 1;
89                 break;
90         }
91
92         buttonPressedHandler(getMouseState(0, 0));
93     };
94
95
96     element.onmouseup = function(e) {
97
98         e.stopPropagation();
99
100         switch (e.button) {
101             case 0:
102                 mouseLeftButton = 0;
103                 break;
104             case 1:
105                 mouseMiddleButton = 0;
106                 break;
107             case 2:
108                 mouseRightButton = 0;
109                 break;
110         }
111
112         buttonReleasedHandler(getMouseState(0, 0));
113     };
114
115     // Override selection on mouse event element.
116     element.onselectstart = function() {
117         return false;
118     };
119
120     // Scroll wheel support
121     function handleScroll(e) {
122
123         var delta = 0;
124         if (e.detail)
125             delta = e.detail;
126         else if (e.wheelDelta)
127             delta = -event.wheelDelta;
128
129         // Up
130         if (delta < 0) {
131             buttonPressedHandler(getMouseState(1, 0));
132             buttonReleasedHandler(getMouseState(0, 0));
133         }
134
135         // Down
136         if (delta > 0) {
137             buttonPressedHandler(getMouseState(0, 1));
138             buttonReleasedHandler(getMouseState(0, 0));
139         }
140
141         if (e.preventDefault)
142             e.preventDefault();
143
144         e.returnValue = false;
145     }
146
147     element.addEventListener('DOMMouseScroll', handleScroll, false);
148
149     element.onmousewheel = function(e) {
150         handleScroll(e);
151     }
152
153         var buttonPressedHandler = null;
154         var buttonReleasedHandler = null;
155         var movementHandler = null;
156
157         this.setButtonPressedHandler  = function(mh) {buttonPressedHandler = mh;};
158         this.setButtonReleasedHandler = function(mh) {buttonReleasedHandler = mh;};
159         this.setMovementHandler = function(mh) {movementHandler = mh;};
160
161
162     this.getX = function() {return mouseX;};
163     this.getY = function() {return mouseY;};
164     this.getLeftButton = function() {return mouseLeftButton;};
165     this.getMiddleButton = function() {return mouseMiddleButton;};
166     this.getRightButton = function() {return mouseRightButton;};
167
168 }
169
170 function MouseEvent(x, y, left, middle, right, up, down) {
171
172     this.getX = function() {
173         return x;
174     };
175
176     this.getY = function() {
177         return y;
178     };
179
180     this.getLeft = function() {
181         return left;
182     };
183
184     this.getMiddle = function() {
185         return middle;
186     };
187
188     this.getRight = function() {
189         return right;
190     };
191
192     this.getUp = function() {
193         return up;
194     };
195
196     this.getDown = function() {
197         return down;
198     };
199
200 }