@@ -2363,7 +2363,7 @@ void tablet_lightpen(int tx, int ty, int tmaxx, int tmaxy, int touch, int button
23632363	}
23642364
23652365	if  (touch >= 0 )
2366- 		lightpen_active =  true ;
2366+ 		lightpen_active |=  1 ;
23672367
23682368	if  (touch > 0  && devid >= 0 ) {
23692369		setmousebuttonstate  (devid, 0 , 1 );
@@ -2374,7 +2374,7 @@ void tablet_lightpen(int tx, int ty, int tmaxx, int tmaxy, int touch, int button
23742374
23752375end:
23762376	if  (lightpen_active) {
2377- 		lightpen_active =  false ;
2377+ 		lightpen_active &= ~ 1 ;
23782378		if  (devid >= 0 )
23792379			setmousebuttonstate  (devid, 0 , 0 );
23802380	}
@@ -3020,12 +3020,13 @@ static void charge_cap (int joy, int idx, int charge)
30203020		pot_cap[joy][idx] = 511 ;
30213021}
30223022
3023- static  void  cap_check  ( void )
3023+ static  void  cap_check ( bool  hsync )
30243024{
30253025	int  joy, i;
30263026
30273027	for  (joy = 0 ; joy < 2 ; joy++) {
30283028		for  (i = 0 ; i < 2 ; i++) {
3029+ 			bool  cancharge = true ;
30293030			int  charge = 0 , dong, joypot;
30303031			uae_u16 pdir = 0x0200  << (joy * 4  + i * 2 ); /*  output enable */ 
30313032			uae_u16 pdat = 0x0100  << (joy * 4  + i * 2 ); /*  data */ 
@@ -3060,13 +3061,14 @@ static void cap_check (void)
30603061				charge = 1 ; //  slow charge via pull-up resistor
30613062			// }
30623063			if  (!(potgo_value & pdir)) { //  input?
3063- 				if  (pot_dat_act[joy][i])
3064+ 				if  (pot_dat_act[joy][i] && hsync )
30643065					pot_dat[joy][i]++;
30653066				/*  first 7 or 8 lines after potgo has been started = discharge cap */ 
30663067				if  (pot_dat_act[joy][i] == 1 ) {
30673068					if  (pot_dat[joy][i] < (currprefs.ntscmode  ? POTDAT_DELAY_NTSC : POTDAT_DELAY_PAL)) {
30683069						charge = -2 ; /*  fast discharge delay */ 
3069-           } else  {
3070+ 						cancharge = hsync;
3071+ 					} else  {
30703072						pot_dat_act[joy][i] = 2 ;
30713073						pot_dat[joy][i] = 0 ;
30723074					}
@@ -3075,13 +3077,24 @@ static void cap_check (void)
30753077					if  (pot_dat_act[joy][i] == 2  && pot_cap[joy][i] >= joypot)
30763078						pot_dat_act[joy][i] = 0 ;
30773079				} else  {
3078- 				if  (analog_port[joy][i] && pot_dat_act[joy][i] == 2  && pot_cap[joy][i] >= joypot)
3079- 					pot_dat_act[joy][i] = 0 ;
3080- 				if  ((digital_port[joy][i] || mouse_port[joy]) && pot_dat_act[joy][i] == 2 ) {
3081- 					if  (pot_cap[joy][i] >= 10  && !isbutton)
3080+ 					if  (analog_port[joy][i] && pot_dat_act[joy][i] == 2  && pot_cap[joy][i] >= joypot)
30823081						pot_dat_act[joy][i] = 0 ;
3082+ 					if  ((digital_port[joy][i] || mouse_port[joy]) && pot_dat_act[joy][i] == 2 ) {
3083+ 						if  (pot_cap[joy][i] >= 10  && !isbutton)
3084+ 							pot_dat_act[joy][i] = 0 ;
3085+ 					}
3086+ 				}
3087+ 				//  CD32 pad 3rd button line floating: 2-button mode
3088+ 				if  (cd32_pad_enabled[joy] && i == 0 ) {
3089+ 					if  (charge == 0 )
3090+ 						charge = 2 ;
30833091				}
3092+ 				//  CD32 pad in 2-button mode: blue button is internally pulled up
3093+ 				if  (cd32_pad_enabled[joy] && !cd32padmode (joy) && i == 1 ) {
3094+ 					if  (charge == 0 )
3095+ 						charge = 2 ;
30843096				}
3097+ 
30853098			} else  { //  output?
30863099				charge = (potgo_value & pdat) ? 2  : -2 ; /*  fast (dis)charge if output */ 
30873100				if  (potgo_value & pdat)
@@ -3099,43 +3112,39 @@ static void cap_check (void)
30993112					charge = 2 ;
31003113			}
31013114
3102- 			//  CD32 pad in 2-button mode: blue button is not floating
3103- 			if  (cd32_pad_enabled[joy] && !cd32padmode (joy) && i == 1 ) {
3104- 				if  (charge == 0 )
3105- 				charge = 2 ;
3106- 			}
3107- 		
31083115			if  (dong < 0  && charge == 0 ) {
31093116
31103117				if  (lightpen_port[joy])
31113118					charge = 2 ;
3112- 		 
3113- 			/*  official Commodore mouse has pull-up resistors in button lines
3114- 			* NOTE: 3rd party mice may not have pullups! */  
3119+ 
3120+ 				 /*  official Commodore mouse has pull-up resistors in button lines
3121+ 				  * NOTE: 3rd party mice may not have pullups! */  
31153122				if  (is_mouse_pullup  (joy) && mouse_port[joy] && digital_port[joy][i])
3116- 				charge = 2 ;
3123+ 					 charge = 2 ;
31173124
3118- 			/*  emulate pullup resistor if button mapped because there too many broken
3119- 			* programs that read second button in input-mode (and most 2+ button pads have 
3120- 			* pullups) 
3121- 			*/  
3122- 			   if  (is_joystick_pullup  (joy) && digital_port[joy][i])
3123- 				charge = 2 ;
3125+ 				 /*  emulate pullup resistor if button mapped because there too many broken
3126+ 				  * programs that read second button in input-mode (and most 2+ button pads have 
3127+ 				  * pullups) 
3128+ 				  */  
3129+ 				 if  (is_joystick_pullup  (joy) && digital_port[joy][i])
3130+ 					 charge = 2 ;
31243131			}
31253132
3126- 			charge_cap  (joy, i, charge);
3133+ 			if  (cancharge) {
3134+ 				charge_cap (joy, i, charge);
3135+ 			}
31273136		}
31283137	}
31293138}
31303139
31313140
31323141uae_u8 handle_joystick_buttons  (uae_u8 pra, uae_u8 dra)
31333142{
3134-    uae_u8 but = 0 ;
3143+ 	 uae_u8 but = 0 ;
31353144	int  i;
31363145
31373146	maybe_read_input ();
3138- 	cap_check  ( );
3147+ 	cap_check ( false );
31393148	for  (i = 0 ; i < 2 ; i++) {
31403149		int  mask = 0x40  << i;
31413150		if  (cd32_pad_enabled[i]) {
@@ -3173,7 +3182,7 @@ void handle_cd32_joystick_cia (uae_u8 pra, uae_u8 dra)
31733182	int  i;
31743183
31753184	maybe_read_input ();
3176- 	cap_check  ( );
3185+ 	cap_check ( false );
31773186	for  (i = 0 ; i < 2 ; i++) {
31783187		uae_u8 but = 0x40  << i;
31793188		if  (cd32padmode (i)) {
@@ -3194,7 +3203,7 @@ static uae_u16 handle_joystick_potgor (uae_u16 potgor)
31943203{
31953204	int  i;
31963205
3197- 	cap_check  ( );
3206+ 	cap_check ( false );
31983207	for  (i = 0 ; i < 2 ; i++) {
31993208		uae_u16 p9dir = 0x0800  << (i * 4 ); /*  output enable P9 */ 
32003209		uae_u16 p9dat = 0x0400  << (i * 4 ); /*  data P9 */ 
@@ -3500,7 +3509,7 @@ int handle_custom_event (const TCHAR *custom, int append)
35003509
35013510void  inputdevice_hsync (bool  forceread)
35023511{
3503- 	cap_check  ( );
3512+ 	cap_check ( true );
35043513
35053514#ifdef  CATWEASEL
35063515	catweasel_hsync  ();
@@ -4231,13 +4240,14 @@ static int handle_input_event2(int nr, int state, int max, int flags, int extra)
42314240		{
42324241			int  unit = (ie->data  & 1 ) ? 1  : 0 ;
42334242			int  lpnum = ie->unit  - 5 ;
4234- 			if  (lightpen_active <= 0 ) {
4235- 				lightpen_x[0 ] = vidinfo->outbuffer ->outwidth  / 2 ;
4236- 				lightpen_y[0 ] = vidinfo->outbuffer ->outheight  / 2 ;
4237- 				lightpen_x[1 ] = -1 ;
4238- 				lightpen_y[1 ] = -1 ;
4243+ 			if  (!(lightpen_active & (1  << lpnum))) {
4244+ 				if  (!state) {
4245+ 					break ;
4246+ 				}
4247+ 				lightpen_x[lpnum] = vidinfo->outbuffer ->outwidth  / 2 ;
4248+ 				lightpen_y[lpnum] = vidinfo->outbuffer ->outheight  / 2 ;
4249+ 				lightpen_active |= 1  << lpnum;
42394250			}
4240- 			lightpen_active = true ;
42414251			lightpen_enabled = true ;
42424252			if  (flags & HANDLE_IE_FLAG_ABSOLUTE) {
42434253				lastmxy_abs[lpnum][unit] = extra;
@@ -5400,8 +5410,7 @@ static void scanevents (struct uae_prefs *p)
54005410#ifndef  AMIBERRY
54015411	lightpen_enabled = false ;
54025412	lightpen_enabled2 = false ;
5403- 	if  (lightpen_active > 0 )
5404- 		lightpen_active = -1 ;
5413+ 	lightpen_active = 0 ;
54055414#endif 
54065415	for  (i = 0 ; i < MAX_INPUT_DEVICES; i++) {
54075416		use_joysticks[i] = 0 ;
0 commit comments