2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991-1997 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/signal.h>
38 #include <sys/consio.h>
42 static void VGLMouseAction(int dummy);
44 #define BORDER 0xff /* default border -- light white in rgb 3:3:2 */
45 #define INTERIOR 0xa0 /* default interior -- red in rgb 3:3:2 */
46 #define LARGE_MOUSE_IMG_XSIZE 19
47 #define LARGE_MOUSE_IMG_YSIZE 32
48 #define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0,
136 0,0,0,0,0,X,X,X,X,0,
137 0,0,0,0,0,0,X,X,0,0,
139 static byte SmallOrMask[] = {
140 B,B,0,0,0,0,0,0,0,0,
141 B,I,B,0,0,0,0,0,0,0,
142 B,I,I,B,0,0,0,0,0,0,
143 B,I,I,I,B,0,0,0,0,0,
144 B,I,I,I,I,B,0,0,0,0,
145 B,I,I,I,I,I,B,0,0,0,
146 B,I,I,I,I,I,I,B,0,0,
147 B,I,I,I,I,I,I,I,B,0,
148 B,I,I,I,I,I,I,I,I,B,
149 B,I,I,I,I,I,B,B,B,B,
150 B,I,I,B,I,I,B,0,0,0,
151 B,I,B,0,B,I,I,B,0,0,
152 B,B,0,0,B,I,I,B,0,0,
153 0,0,0,0,0,B,I,I,B,0,
154 0,0,0,0,0,B,I,I,B,0,
155 0,0,0,0,0,0,B,B,0,0,
160 static VGLBitmap VGLMouseLargeAndMask =
161 VGLBITMAP_INITIALIZER(MEMBUF, LARGE_MOUSE_IMG_XSIZE, LARGE_MOUSE_IMG_YSIZE,
163 static VGLBitmap VGLMouseLargeOrMask =
164 VGLBITMAP_INITIALIZER(MEMBUF, LARGE_MOUSE_IMG_XSIZE, LARGE_MOUSE_IMG_YSIZE,
166 static VGLBitmap VGLMouseSmallAndMask =
167 VGLBITMAP_INITIALIZER(MEMBUF, SMALL_MOUSE_IMG_XSIZE, SMALL_MOUSE_IMG_YSIZE,
169 static VGLBitmap VGLMouseSmallOrMask =
170 VGLBITMAP_INITIALIZER(MEMBUF, SMALL_MOUSE_IMG_XSIZE, SMALL_MOUSE_IMG_YSIZE,
172 static VGLBitmap *VGLMouseAndMask, *VGLMouseOrMask;
173 static int VGLMouseShown = VGL_MOUSEHIDE;
174 static int VGLMouseXpos = 0;
175 static int VGLMouseYpos = 0;
176 static int VGLMouseButtons = 0;
177 static volatile sig_atomic_t VGLMintpending;
178 static volatile sig_atomic_t VGLMsuppressint;
180 #define INTOFF() (VGLMsuppressint++)
181 #define INTON() do { \
182 if (--VGLMsuppressint == 0 && VGLMintpending) \
187 __VGLMouseMode(int mode)
192 oldmode = VGLMouseShown;
193 if (mode == VGL_MOUSESHOW) {
194 if (VGLMouseShown == VGL_MOUSEHIDE) {
195 VGLMouseShown = VGL_MOUSESHOW;
196 __VGLBitmapCopy(&VGLVDisplay, VGLMouseXpos, VGLMouseYpos,
197 VGLDisplay, VGLMouseXpos, VGLMouseYpos,
198 VGLMouseAndMask->VXsize, -VGLMouseAndMask->VYsize);
202 if (VGLMouseShown == VGL_MOUSESHOW) {
203 VGLMouseShown = VGL_MOUSEHIDE;
204 __VGLBitmapCopy(&VGLVDisplay, VGLMouseXpos, VGLMouseYpos,
205 VGLDisplay, VGLMouseXpos, VGLMouseYpos,
206 VGLMouseAndMask->VXsize, VGLMouseAndMask->VYsize);
214 VGLMouseMode(int mode)
216 __VGLMouseMode(mode);
220 VGLMouseAction(int dummy)
222 struct mouse_info mouseinfo;
225 if (VGLMsuppressint) {
232 mouseinfo.operation = MOUSE_GETINFO;
233 ioctl(0, CONS_MOUSECTL, &mouseinfo);
234 if (VGLMouseXpos != mouseinfo.u.data.x ||
235 VGLMouseYpos != mouseinfo.u.data.y) {
236 mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
237 VGLMouseXpos = mouseinfo.u.data.x;
238 VGLMouseYpos = mouseinfo.u.data.y;
239 __VGLMouseMode(mousemode);
241 VGLMouseButtons = mouseinfo.u.data.buttons;
244 * Loop to handle any new (suppressed) signals. This is INTON() without
245 * recursion. !SA_RESTART prevents recursion in signal handling. So the
246 * maximum recursion is 2 levels.
254 VGLMouseSetImage(VGLBitmap *AndMask, VGLBitmap *OrMask)
258 mousemode = __VGLMouseMode(VGL_MOUSEHIDE);
260 VGLMouseAndMask = AndMask;
262 if (VGLMouseOrMask != NULL) {
263 free(VGLMouseOrMask->Bitmap);
264 free(VGLMouseOrMask);
266 VGLMouseOrMask = VGLBitmapCreate(MEMBUF, OrMask->VXsize, OrMask->VYsize, 0);
267 VGLBitmapAllocateBits(VGLMouseOrMask);
268 VGLBitmapCvt(OrMask, VGLMouseOrMask);
270 __VGLMouseMode(mousemode);
274 VGLMouseSetStdImage()
276 if (VGLDisplay->VXsize > 800)
277 VGLMouseSetImage(&VGLMouseLargeAndMask, &VGLMouseLargeOrMask);
279 VGLMouseSetImage(&VGLMouseSmallAndMask, &VGLMouseSmallOrMask);
283 VGLMouseInit(int mode)
285 struct mouse_info mouseinfo;
287 int border, error, i, interior;
289 switch (VGLModeInfo.vi_mem_model) {
290 case V_INFO_MM_PACKED:
291 case V_INFO_MM_PLANAR:
304 if (VGLModeInfo.vi_mode == M_BG640x480)
305 border = 0; /* XXX (palette makes 0x04 look like 0x0f) */
306 if (getenv("VGLMOUSEBORDERCOLOR") != NULL)
307 border = strtoul(getenv("VGLMOUSEBORDERCOLOR"), NULL, 0);
308 if (getenv("VGLMOUSEINTERIORCOLOR") != NULL)
309 interior = strtoul(getenv("VGLMOUSEINTERIORCOLOR"), NULL, 0);
310 ormask = &VGLMouseLargeOrMask;
311 for (i = 0; i < ormask->VXsize * ormask->VYsize; i++)
312 ormask->Bitmap[i] = ormask->Bitmap[i] == BORDER ? border :
313 ormask->Bitmap[i] == INTERIOR ? interior : 0;
314 ormask = &VGLMouseSmallOrMask;
315 for (i = 0; i < ormask->VXsize * ormask->VYsize; i++)
316 ormask->Bitmap[i] = ormask->Bitmap[i] == BORDER ? border :
317 ormask->Bitmap[i] == INTERIOR ? interior : 0;
318 VGLMouseSetStdImage();
319 mouseinfo.operation = MOUSE_MODE;
320 mouseinfo.u.mode.signal = SIGUSR2;
321 if ((error = ioctl(0, CONS_MOUSECTL, &mouseinfo)))
323 signal(SIGUSR2, VGLMouseAction);
324 mouseinfo.operation = MOUSE_GETINFO;
325 ioctl(0, CONS_MOUSECTL, &mouseinfo);
326 VGLMouseXpos = mouseinfo.u.data.x;
327 VGLMouseYpos = mouseinfo.u.data.y;
328 VGLMouseButtons = mouseinfo.u.data.buttons;
334 VGLMouseRestore(void)
336 struct mouse_info mouseinfo;
339 mouseinfo.operation = MOUSE_GETINFO;
340 if (ioctl(0, CONS_MOUSECTL, &mouseinfo) == 0) {
341 mouseinfo.operation = MOUSE_MOVEABS;
342 mouseinfo.u.data.x = VGLMouseXpos;
343 mouseinfo.u.data.y = VGLMouseYpos;
344 ioctl(0, CONS_MOUSECTL, &mouseinfo);
350 VGLMouseStatus(int *x, int *y, char *buttons)
355 *buttons = VGLMouseButtons;
357 return VGLMouseShown;
367 VGLMouseFreezeXY(int x, int y)
370 if (VGLMouseShown != VGL_MOUSESHOW)
372 if (x >= VGLMouseXpos && x < VGLMouseXpos + VGLMouseAndMask->VXsize &&
373 y >= VGLMouseYpos && y < VGLMouseYpos + VGLMouseAndMask->VYsize &&
374 VGLMouseAndMask->Bitmap[(y-VGLMouseYpos)*VGLMouseAndMask->VXsize+
381 VGLMouseOverlap(int x, int y, int width, int hight)
385 if (VGLMouseShown != VGL_MOUSESHOW)
387 if (x > VGLMouseXpos)
388 overlap = (VGLMouseXpos + VGLMouseAndMask->VXsize) - x;
390 overlap = (x + width) - VGLMouseXpos;
393 if (y > VGLMouseYpos)
394 overlap = (VGLMouseYpos + VGLMouseAndMask->VYsize) - y;
396 overlap = (y + hight) - VGLMouseYpos;
401 VGLMouseMerge(int x, int y, int width, byte *line)
403 int pos, x1, xend, xstart;
406 if (xstart < VGLMouseXpos)
407 xstart = VGLMouseXpos;
409 if (xend > VGLMouseXpos + VGLMouseAndMask->VXsize)
410 xend = VGLMouseXpos + VGLMouseAndMask->VXsize;
411 for (x1 = xstart; x1 < xend; x1++) {
412 pos = (y - VGLMouseYpos) * VGLMouseAndMask->VXsize + x1 - VGLMouseXpos;
413 if (VGLMouseAndMask->Bitmap[pos])
414 bcopy(&VGLMouseOrMask->Bitmap[pos * VGLDisplay->PixelBytes],
415 &line[(x1 - x) * VGLDisplay->PixelBytes], VGLDisplay->PixelBytes);