1 /* kempmouse.c: Kempston mouse emulation
2    Copyright (c) 2004-2008 Darren Salt, Fredrick Meunier
3 
4    $Id: kempmouse.c 4926 2013-05-05 07:58:18Z sbaldovi $
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 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 General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20    Author contact information:
21 
22    E-mail: linux@youmustbejoking.demon.co.uk
23 
24 */
25 
26 #include <config.h>
27 
28 #include <libspectrum.h>
29 
30 #include "kempmouse.h"
31 #include "module.h"
32 #include "periph.h"
33 #include "settings.h"
34 #include "ui/ui.h"
35 
36 static void kempmouse_from_snapshot( libspectrum_snap *snap );
37 static void kempmouse_to_snapshot( libspectrum_snap *snap );
38 
39 static module_info_t kempmouse_module_info = {
40 
41   NULL,
42   NULL,
43   NULL,
44   kempmouse_from_snapshot,
45   kempmouse_to_snapshot,
46 
47 };
48 
49 static struct {
50   struct { libspectrum_byte x, y; } pos;
51   libspectrum_byte buttons;
52 } kempmouse = { {0, 0}, 255 };
53 
54 #define READ(name,item) \
55   static libspectrum_byte \
56   read_##name( libspectrum_word port GCC_UNUSED, int *attached ) \
57   { \
58     *attached = 1; \
59     return kempmouse.item; \
60   }
61 
62 READ( buttons, buttons );
63 READ( x_pos, pos.x );
64 READ( y_pos, pos.y );
65 
66 static const periph_port_t kempmouse_ports[] = {
67   /* _we_ require b0 set */
68   { 0x0121, 0x0001, read_buttons, NULL },
69   { 0x0521, 0x0101, read_x_pos, NULL },
70   { 0x0521, 0x0501, read_y_pos, NULL },
71 };
72 
73 static const periph_t kempmouse_periph = {
74   &settings_current.kempston_mouse,
75   kempmouse_ports,
76   1,
77   NULL
78 };
79 
80 void
kempmouse_init(void)81 kempmouse_init( void )
82 {
83   module_register( &kempmouse_module_info );
84   periph_register( PERIPH_TYPE_KEMPSTON_MOUSE, &kempmouse_periph );
85 }
86 
87 void
kempmouse_update(int dx,int dy,int btn,int down)88 kempmouse_update( int dx, int dy, int btn, int down )
89 {
90   kempmouse.pos.x += dx;
91   kempmouse.pos.y -= dy;
92   if( btn != -1 ) {
93     if( down )
94       kempmouse.buttons &= ~(1 << btn);
95     else
96       kempmouse.buttons |= 1 << btn;
97   }
98 }
99 
100 static void
kempmouse_from_snapshot(libspectrum_snap * snap)101 kempmouse_from_snapshot( libspectrum_snap *snap )
102 {
103   settings_current.kempston_mouse =
104     libspectrum_snap_kempston_mouse_active( snap );
105 }
106 
107 static void
kempmouse_to_snapshot(libspectrum_snap * snap)108 kempmouse_to_snapshot( libspectrum_snap *snap )
109 {
110   libspectrum_snap_set_kempston_mouse_active( snap,
111                                               settings_current.kempston_mouse );
112 }
113