// File: magmastonealex[P]fbkeyboard/src/Display.cpp
// Function: Display::keyAt(int, int)
char Display::keyAt(int x, int y) {
	if(x < entryWidth) {
		return 0;
	}

	for(int xCheck = 1; xCheck < 5; xCheck++) {
		//determine row of touch.

		int lastRowStart = CHAR_Y_START(entryWidth, scale, xCheck - 1, rowWidth);
		int rowStart = CHAR_Y_START(entryWidth, scale, xCheck, rowWidth);

		if(x > lastRowStart && x < rowStart) {
			//std::cout << "row: " << xCheck << std::endl;
			for(int yCheck = 1; yCheck < 12; yCheck++) {
				int lastCharStart = CHAR_X_START(margin, yCheck-1, stepSizePx, scale);
				int thisCharStart = CHAR_X_START(margin, yCheck, stepSizePx, scale);
				if(y > lastCharStart && y < thisCharStart) {
					//std::cout << "col: " << yCheck << std::endl;
					if(xCheck > 3) {
						std::cout << "col: " << yCheck << std::endl;
						switch(yCheck) {
							case 10:
								toggleSym2();
								return 0;
							case 11:
								toggleSym1();
								return 0;
							default:
								toLower();
								return ' ';
						}
					}
					char touched = (*currentKeyboard)[(xCheck-1)][10-(yCheck-1)];

					if(touched == 0x0F) {
						toggleUpper();
						return 0;
					}
					else if(touched == 0x00) {
						toLower();
					}

					return touched;
				}
			}
		}
	}
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movl -0x14(%rbp), %eax
cmpl 0x14(%rcx), %eax
jge 0x2f0b
movb $0x0, -0x1(%rbp)
jmp 0x30da
movl $0x1, -0x1c(%rbp)
cmpl $0x5, -0x1c(%rbp)
jge 0x30d6
movq -0x40(%rbp), %rdx
movl 0x14(%rdx), %eax
movl 0xc(%rdx), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl -0x1c(%rbp), %ecx
movl 0x1c(%rdx), %esi
shll $0x0, %esi
subl %esi, %ecx
addl %ecx, %eax
movl %eax, -0x20(%rbp)
movl 0x14(%rdx), %eax
movl 0xc(%rdx), %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl -0x1c(%rbp), %ecx
imull 0x1c(%rdx), %ecx
addl %ecx, %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x30c6
movl -0x14(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x30c6
movl $0x1, -0x28(%rbp)
cmpl $0xc, -0x28(%rbp)
jge 0x30c4
movq -0x40(%rbp), %rcx
movl 0x8(%rcx), %eax
movl -0x28(%rbp), %edx
subl $0x1, %edx
movl 0x20(%rcx), %esi
movl 0xc(%rcx), %edi
shll $0x3, %edi
addl %edi, %esi
imull %esi, %edx
addl %edx, %eax
subl 0xc(%rcx), %eax
movl %eax, -0x2c(%rbp)
movl 0x8(%rcx), %eax
movl -0x28(%rbp), %edx
movl 0x20(%rcx), %esi
movl 0xc(%rcx), %edi
shll $0x3, %edi
addl %edi, %esi
imull %esi, %edx
addl %edx, %eax
subl 0xc(%rcx), %eax
movl %eax, -0x30(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0x30b4
movl -0x18(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x30b4
cmpl $0x3, -0x1c(%rbp)
jle 0x3052
movq 0x3fe1(%rip), %rdi      # 0x6fc0
leaq 0x20b2(%rip), %rsi      # 0x5098
callq 0x21b0
movq %rax, %rdi
movl -0x28(%rbp), %esi
callq 0x2320
movq %rax, %rdi
movq 0x3fa8(%rip), %rsi      # 0x6fa8
callq 0x21e0
movl -0x28(%rbp), %eax
movl %eax, -0x44(%rbp)
subl $0xa, %eax
je 0x301c
jmp 0x3012
movl -0x44(%rbp), %eax
subl $0xb, %eax
je 0x302e
jmp 0x3040
movq -0x40(%rbp), %rdi
callq 0x3680
movb $0x0, -0x1(%rbp)
jmp 0x30da
movq -0x40(%rbp), %rdi
callq 0x36d0
movb $0x0, -0x1(%rbp)
jmp 0x30da
movq -0x40(%rbp), %rdi
callq 0x3720
movb $0x20, -0x1(%rbp)
jmp 0x30da
movq -0x40(%rbp), %rax
movq 0x60(%rax), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
imulq $0xb, %rcx, %rcx
addq %rcx, %rax
movl -0x28(%rbp), %edx
subl $0x1, %edx
movl $0xa, %ecx
subl %edx, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x31(%rbp)
movsbl -0x31(%rbp), %eax
cmpl $0xf, %eax
jne 0x3098
movq -0x40(%rbp), %rdi
callq 0x3740
movb $0x0, -0x1(%rbp)
jmp 0x30da
movsbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x30aa
movq -0x40(%rbp), %rdi
callq 0x3720
jmp 0x30ac
movb -0x31(%rbp), %al
movb %al, -0x1(%rbp)
jmp 0x30da
jmp 0x30b6
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2f71
jmp 0x30c6
jmp 0x30c8
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x2f12
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)