Olav SørensenPushed v1.62 code4188e803 years agoHistory
3584 lines · 75.3 KB · C
RawDownload
// for finding memory leaks in debug mode with Visual Studio #if defined _DEBUG && defined _MSC_VER#include <crtdbg.h>#endif #include <stdio.h>#include <stdint.h>#include <stdbool.h>#include <ctype.h> // tolower()#ifdef _WIN32#include <windows.h>#else#include <unistd.h>#endif#include "pt2_textout.h"#include "pt2_helpers.h"#include "pt2_visuals.h"#include "pt2_diskop.h"#include "pt2_edit.h"#include "pt2_sampler.h"#include "pt2_audio.h"#include "pt2_tables.h"#include "pt2_module_saver.h"#include "pt2_sample_saver.h"#include "pt2_config.h"#include "pt2_sampling.h"#include "pt2_chordmaker.h"#include "pt2_askbox.h"#include "pt2_replayer.h"#include "pt2_posed.h"#include "pt2_textedit.h" #if defined _WIN32 && !defined _DEBUGextern bool windowsKeyIsDown;extern HHOOK g_hKeyboardHook;#endif static bool handleGeneralModes(SDL_Scancode scancode);static void movePatCurPrevCh(void);static void movePatCurNextCh(void);static void movePatCurRight(void);static void movePatCurLeft(void); bool handleTextEditMode(SDL_Scancode scancode); void readKeyModifiers(void){	const SDL_Keymod modState = SDL_GetModState(); 	keyb.leftCtrlPressed = (modState & KMOD_LCTRL)  ? true : false;	keyb.leftAltPressed = (modState & KMOD_LALT) ? true : false;	keyb.shiftPressed = (modState & (KMOD_LSHIFT + KMOD_RSHIFT)) ? true : false; #ifdef __APPLE__	keyb.leftCommandPressed = (modState & KMOD_LGUI) ? true : false;#endif #if defined _WIN32 && !defined _DEBUG	keyb.leftAmigaPressed = windowsKeyIsDown; // Windows: handled in lowLevelKeyboardProc#else	keyb.leftAmigaPressed = (modState & KMOD_LGUI) ? true : false;#endif} #if defined _WIN32 && !defined _DEBUG/* For taking control over the windows key if the program has focus.** Warning: Don't do this in debug mode, it will completely ruin the keyboard input** latency (in the OS in general) when the debugger is breaking.*/LRESULT CALLBACK lowLevelKeyboardProc(int32_t nCode, WPARAM wParam, LPARAM lParam){	SDL_Window *window = video.window; 	if (nCode == HC_ACTION && window != NULL)	{		switch (wParam)		{			case WM_KEYUP:			case WM_KEYDOWN:			case WM_SYSKEYUP: // needed to prevent stuck Windows key if used with ALT			{				const bool windowHasFocus = SDL_GetWindowFlags(window) & SDL_WINDOW_INPUT_FOCUS;				if (!windowHasFocus)				{					windowsKeyIsDown = false;					break;				} 				if (((KBDLLHOOKSTRUCT *)lParam)->vkCode != VK_LWIN)					break; 				windowsKeyIsDown = (wParam == WM_KEYDOWN);				return 1; // eat keystroke			}			break; 			default: break;		}	} 	return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam);}#endif void keyUpHandler(SDL_Scancode scancode){	if (scancode == SDL_SCANCODE_KP_PLUS)		keyb.keypadEnterPressed = false; 	if (scancode == keyb.lastRepKey)		keyb.lastRepKey = SDL_SCANCODE_UNKNOWN; 	switch (scancode)	{		// modifiers shouldn't reset keyb repeat/delay flags & counters		case SDL_SCANCODE_LCTRL:		case SDL_SCANCODE_RCTRL:		case SDL_SCANCODE_LSHIFT:		case SDL_SCANCODE_RSHIFT:		case SDL_SCANCODE_LALT:		case SDL_SCANCODE_RALT:		case SDL_SCANCODE_LGUI:		case SDL_SCANCODE_RGUI:		case SDL_SCANCODE_MENU:		case SDL_SCANCODE_MODE:		case SDL_SCANCODE_CAPSLOCK:		break; 		default:		{			keyb.repeatKey = false;			keyb.delayKey = false;			keyb.repeatFrac = 0;			keyb.delayCounter = 0;		}		break;	}} static void incMulti(uint8_t slot){	char str[32]; 	assert(slot < 4);	if (editor.multiModeNext[slot] == 4)		editor.multiModeNext[slot] = 1;	else		editor.multiModeNext[slot]++; 	sprintf(str, "MULTI=%d-%d-%d-%d", editor.multiModeNext[0], editor.multiModeNext[1],		editor.multiModeNext[2], editor.multiModeNext[3]);	displayMsg(str);} void keyDownHandler(SDL_Scancode scancode, SDL_Keycode keycode){	if (scancode == SDL_SCANCODE_CAPSLOCK)	{		editor.repeatKeyFlag ^= 1;		return;	} 	// kludge: allow certain specific key combos to be repeated with the ctrl key	const bool nonRepeatAltKeys = keyb.leftAltPressed && scancode != SDL_SCANCODE_DELETE    && scancode != SDL_SCANCODE_RETURN	                                                  && scancode != SDL_SCANCODE_BACKSPACE && scancode != SDL_SCANCODE_BACKSLASH	                                                  && scancode != SDL_SCANCODE_EQUALS    && scancode != SDL_SCANCODE_MINUS	                                                  && scancode <  SDL_SCANCODE_1         && scancode >  SDL_SCANCODE_0; 	// kludge: allow certain specific key combos to be repeated with the alt key	const bool nonRepeatCtrlKeys = keyb.leftCtrlPressed && scancode != SDL_SCANCODE_DELETE && scancode != SDL_SCANCODE_RETURN	                                                    && scancode != SDL_SCANCODE_BACKSPACE; 	// these keys should not allow to be repeated in keyrepeat mode (caps lock)	const bool nonRepeatKeys = keyb.leftAmigaPressed || nonRepeatAltKeys || nonRepeatCtrlKeys		|| scancode == SDL_SCANCODE_LEFT || scancode == SDL_SCANCODE_RIGHT		|| scancode == SDL_SCANCODE_UP   || scancode == SDL_SCANCODE_DOWN;	if (editor.repeatKeyFlag && keyb.repeatKey && scancode == keyb.lastRepKey && nonRepeatKeys)		return; 	if (scancode == SDL_SCANCODE_KP_PLUS)		keyb.keypadEnterPressed = true; 	// TOGGLE FULLSCREEN (should always react)	if (scancode == SDL_SCANCODE_F11 && !keyb.leftAltPressed)	{		toggleFullscreen(); 		// prevent fullscreen toggle from firing twice on certain SDL2 Linux ports#ifdef __unix__		SDL_Delay(100);#endif		return;	} 	// don't handle input if an error message wait is active or if an unknown key is passed	if ((editor.errorMsgActive && editor.errorMsgBlock) || scancode == SDL_SCANCODE_UNKNOWN)		return; 	// if no ALT/SHIFT/CTRL/AMIGA, update last key for repeat routine	if (scancode != SDL_SCANCODE_LALT     && scancode != SDL_SCANCODE_RALT   &&		scancode != SDL_SCANCODE_LCTRL    && scancode != SDL_SCANCODE_RCTRL  &&		scancode != SDL_SCANCODE_LSHIFT   && scancode != SDL_SCANCODE_RSHIFT &&		scancode != SDL_SCANCODE_LGUI     && scancode != SDL_SCANCODE_RGUI   &&		scancode != SDL_SCANCODE_MENU     && scancode != SDL_SCANCODE_MODE   &&		scancode != SDL_SCANCODE_CAPSLOCK && scancode != SDL_SCANCODE_ESCAPE)	{		if (editor.repeatKeyFlag)		{			// if Repeat Flag, repeat all keys			if (!keyb.repeatKey)			{				keyb.repeatCounter = 0;				keyb.repeatFrac = 0;			} 			keyb.repeatKey = true;			keyb.delayKey = true;		} 		keyb.repeatCounter = 0;		keyb.repeatFrac = 0;		keyb.lastRepKey = scancode;	} 	// ENTRY JUMPING IN DISK OP. FILELIST	if (ui.diskOpScreenShown && keyb.shiftPressed && !ui.editTextFlag)	{		if (keycode >= 32 && keycode <= 126)		{			handleEntryJumping(keycode);			return;		}	} 	// XXX: This really needs some refactoring, it's messy and not logical 	if (!handleGeneralModes(scancode)) return;	if (!handleTextEditMode(scancode)) return;	if (ui.samplerVolBoxShown || ui.samplingBoxShown) return; 	if (ui.samplerFiltersBoxShown)	{		handleEditKeys(scancode, EDIT_NORMAL);		return;	} 	// GENERAL KEYS	switch (scancode)	{		case SDL_SCANCODE_NONUSBACKSLASH: turnOffVoices(); break; // magic "kill all voices" button 		case SDL_SCANCODE_APOSTROPHE:		{			if (editor.autoInsFlag)			{				if (keyb.shiftPressed)					editor.autoInsSlot -= 4;				else					editor.autoInsSlot--; 				if (editor.autoInsSlot < 0)					editor.autoInsSlot = 0; 				ui.updateTrackerFlags = true;			}		}		break; 		case SDL_SCANCODE_BACKSLASH:		{			if (keyb.leftAltPressed)			{				if (handleSpecialKeys(scancode) && editor.currMode != MODE_RECORD)					modSetPos(DONT_SET_ORDER, (song->currRow + editor.editMoveAdd) & 63);			}			else			{				if (editor.autoInsFlag)				{					if (keyb.shiftPressed)						editor.autoInsSlot += 4;					else						editor.autoInsSlot++; 					if (editor.autoInsSlot > 9)						editor.autoInsSlot = 9;				}				else				{					editor.pNoteFlag = (editor.pNoteFlag + 1) % 3;				} 				ui.updateTrackerFlags = true;			}		}		break; #ifdef __APPLE__		case SDL_SCANCODE_RGUI:#else		case SDL_SCANCODE_RALT:#endif		{			// right Amiga key on Amiga keyb			if (!ui.askBoxShown)			{				editor.playMode = PLAY_MODE_NORMAL;				modPlay(DONT_SET_PATTERN, song->currPos, DONT_SET_ROW);				editor.currMode = MODE_PLAY;				pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);				statusAllRight();			}		}		break; #ifdef __APPLE__		case SDL_SCANCODE_RALT:#else		case SDL_SCANCODE_RCTRL:#endif		{			// right alt on Amiga keyb			if (!ui.askBoxShown)			{				editor.playMode = PLAY_MODE_PATTERN;				modPlay(song->currPattern, DONT_SET_ORDER, DONT_SET_ROW);				editor.currMode = MODE_PLAY;				pointerSetMode(POINTER_MODE_PLAY, DO_CARRY);				statusAllRight();			}		}		break; 		case SDL_SCANCODE_RSHIFT:		{			// right shift on Amiga keyb			if (!ui.samplerScreenShown && !ui.askBoxShown)			{				editor.playMode = PLAY_MODE_PATTERN;				modPlay(song->currPattern, DONT_SET_ORDER, DONT_SET_ROW);				editor.currMode = MODE_RECORD;				pointerSetMode(POINTER_MODE_EDIT, DO_CARRY);				statusAllRight();			}		}		break; 		case SDL_SCANCODE_ESCAPE:		{			if (ui.posEdScreenShown)			{				ui.posEdScreenShown = false;				displayMainScreen();			}			else if (ui.diskOpScreenShown)			{				ui.diskOpScreenShown = false;				displayMainScreen();			}			else if (ui.samplerScreenShown)			{				exitFromSam();			}			else if (ui.editOpScreenShown)			{				ui.editOpScreenShown = false;				displayMainScreen();			}			else			{				if (askBox(ASKBOX_YES_NO, "REALLY QUIT ?"))				{					ui.throwExit = true;					return;				}			} 			pointerSetPreviousMode();			setPrevStatusMessage();		}		break; 		case SDL_SCANCODE_INSERT:		{			if (ui.samplerScreenShown)			{				samplerSamPaste();				return;			}		}		break; 		case SDL_SCANCODE_PAGEUP:		{			if (ui.posEdScreenShown)			{				if (song->currPos > 0)				{					if (song->currPos-(POSED_LIST_SIZE-1) > 0)						modSetPos(song->currPos-(POSED_LIST_SIZE-1), DONT_SET_ROW);					else						modSetPos(0, DONT_SET_ROW);				}			}			else if (ui.diskOpScreenShown)			{				diskop.scrollOffset -= DISKOP_LINES - 1;				if (diskop.scrollOffset < 0)					diskop.scrollOffset = 0; 				ui.updateDiskOpFileList = true;			}			else			{				if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)				{					if (song->currRow == 63)						modSetPos(DONT_SET_ORDER, song->currRow - 15);					else if (song->currRow == 15)						modSetPos(DONT_SET_ORDER, 0); // 15-16 would turn into -1, which is "DON'T SET ROW" flag					else						modSetPos(DONT_SET_ORDER, song->currRow - 16);				}			} 			if (!keyb.repeatKey)				keyb.delayCounter = 0; 			keyb.repeatKey = true;			keyb.delayKey = true;		}		break; 		case SDL_SCANCODE_PAGEDOWN:		{			if (ui.posEdScreenShown)			{				if (song->currPos != song->header.songLength-1)				{					if (song->currPos+(POSED_LIST_SIZE-1) <= song->header.songLength-1)						modSetPos(song->currPos+(POSED_LIST_SIZE-1), DONT_SET_ROW);					else						modSetPos(song->header.songLength - 1, DONT_SET_ROW);				}			}			else if (ui.diskOpScreenShown)			{				if (diskop.numEntries > DISKOP_LINES)				{					diskop.scrollOffset += DISKOP_LINES-1;					if (diskop.scrollOffset > diskop.numEntries-DISKOP_LINES)						diskop.scrollOffset = diskop.numEntries-DISKOP_LINES; 					ui.updateDiskOpFileList = true;				}			}			else			{				if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)					modSetPos(DONT_SET_ORDER, song->currRow + 16);			} 			if (!keyb.repeatKey)				keyb.delayCounter = 0; 			keyb.repeatKey = true;			keyb.delayKey = true;		}		break; 		case SDL_SCANCODE_HOME:		{			if (ui.posEdScreenShown)			{				if (song->currPos > 0)					modSetPos(0, DONT_SET_ROW);			}			else if (ui.diskOpScreenShown)			{				if (diskop.scrollOffset != 0)				{					diskop.scrollOffset = 0;					ui.updateDiskOpFileList = true;				}			}			else			{				if (editor.currMode == MODE_IDLE || editor.currMode == MODE_EDIT)					modSetPos(DONT_SET_ORDER, 0);			}		}		break; 		case SDL_SCANCODE_END:		{			if (ui.posEdScreenShown)			{				modSetPos(song->header.songLength - 1, DONT_SET_ROW);			}			else if (ui.diskOpScreenShown)

Lines 1–500 of 3584.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t