Monday, October 12, 2009

Mac Keycodes and keyboards

So for assignment one in OOP344 the optional task is to get it working on the Mac's OS X.
Seemed easy enough. Switches ncurses with curses and I'd be good to go right?
Of course not. I started with the Linux keys because they would probably be the closest, and for the most part they were. For the most part.

Turns out mac's key binding are... odd. The first problem was backspace returning an unknown key (127) so I made backspace 127.

FIXED!

Next problem: the fact that I had no "end" "home" or "insert" keys on my keyboard. I got around this in the least elegant way possible. I jammed random key combinations until I got one that returned an unknown value and assigned that the key.
This gave me "Insert" sitting on Ctrl 'w'(integer: 23 - Ctrl 'i' and 'o' both being taken) "End" sitting on Ctrl 'e' (integer: 5) and "Home" sitting on Ctrl 'h'(integer: 263).

So that took care of that. The next problem was the function keys. They all had pre-existent hardware command. A short trip to google told me how to disable them and I now have f5-f12 working properly.
Only problem is that f1-f4 don't return f1-f4. They return 'P' 'Q' 'R' and 'S' respectively... except for when they crash my program for some reason. There doesn't seem to be much reason for this as it happens only occasionally and with no real pattern but yeah, whole thing, just crashes.
I could assign the functions to ctrl 'p' 'l' 'k' and 'u' since they have unused codes but that seems sloppy since I HAVE the keys I just can't get them to distinguish between themselves and capital P through S and "die you miserable program".

Other than that though I have gotten my code to pass every test on the mac.

Thursday, October 1, 2009

OOP344 Challenge 3

For our third challenge, we had to reduce our display program even moreso. Down to one line as it were. This seemed pretty much impossible until I remembered 2 things. First that since "row" and "col" are passed by value, once we've positioned the cursor they aren't needed for anything and can be used as counters and second that we have conditional operators.

Don't let the wrap around and ridiculous size fool you. That's one, long, ridiculous For Loop.

void io_display(const char *str, int row, int col, int len){

for(io_move(row, col), (len <= 0) ? (col=len,(io_putstr(str))) : (col=0); col < len ;(str[col] ? io_putch(str[col]) : io_putch(' ')), col++);
}