Ported the user-space programs to use stdin for keyboard access.

The old keyboard API is hereby deprecated and unused.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-01-22 23:46:41 +01:00
parent fa9c7007b4
commit c4264d9c2a
10 changed files with 277 additions and 267 deletions

View File

@ -1,10 +1,7 @@
#include <libmaxsi/platform.h>
#include <libmaxsi/io.h>
#include <libmaxsi/thread.h>
#include <libmaxsi/keyboard.h>
#include <libmaxsi/string.h>
#include <libmaxsi/sortix-vga.h>
#include <libmaxsi/sortix-keyboard.h>
#include <sys/keycodes.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
@ -13,9 +10,6 @@
#include <error.h>
#include <string.h>
using namespace Maxsi;
using namespace Maxsi::Keyboard;
const int width = 80;
const int height = 25;
@ -118,24 +112,28 @@ void Render()
void Update()
{
// Read the keyboard input from the user.
unsigned method = System::Keyboard::POLL;
uint32_t codepoint;
while ( (codepoint = System::Keyboard::ReceiveKeystroke(method) ) != 0 )
unsigned termmode = TERMMODE_KBKEY
| TERMMODE_UNICODE
| TERMMODE_SIGNAL
| TERMMODE_NONBLOCK;
if ( settermmode(0, termmode) ) { error(1, errno, "settermmode"); }
while ( true )
{
bool keyup = codepoint & DEPRESSED;
if ( keyup ) { continue; }
codepoint &= ~DEPRESSED;
if ( codepoint == 'r' || codepoint == 'R' ) { running = !running; }
if ( !running )
uint32_t codepoint;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { break; }
if ( numbytes < 0 ) { break; }
int kbkey = KBKEY_DECODE(codepoint);
if ( kbkey == KBKEY_R ) { running = !running; }
if ( running ) { continue; }
if ( kbkey == KBKEY_C ) { Clear(); }
if ( kbkey == KBKEY_W ) { if ( posy > 1 ) { posy--; } }
if ( kbkey == KBKEY_A ) { if ( posx > 1 ) { posx--; } }
if ( kbkey == KBKEY_S ) { if ( posy < height ) { posy++; } }
if ( kbkey == KBKEY_D ) { if ( posx < width ) { posx++; } }
if ( kbkey == KBKEY_SPACE )
{
if ( codepoint == 'c' || codepoint == 'C' ) { Clear(); }
if ( codepoint == 'w' || codepoint == 'W' ) { if ( posy > 1 ) { posy--; } }
if ( codepoint == 's' || codepoint == 'S' ) { if ( posy < height ) { posy++; } }
if ( codepoint == 'a' || codepoint == 'A' ) { if ( posx > 1 ) { posx--; } }
if ( codepoint == 'd' || codepoint == 'D' ) { if ( posx < width ) { posx++; } }
if ( codepoint == ' ' ) { lastframe[posy * rowstride + posx] = 1 - lastframe[posy * rowstride + posx]; }
lastframe[posy * rowstride + posx] = 1 - lastframe[posy * rowstride + posx];
}
}
@ -161,11 +159,11 @@ int main(int argc, char* argv[])
int sleepms = 50;
for ( int i = 1; i < argc; i++ )
{
if ( String::Compare(argv[i], "--help") == 0 ) { return usage(argc, argv); }
if ( String::Compare(argv[i], "--usage") == 0 ) { return usage(argc, argv); }
if ( String::Compare(argv[i], "--speed") == 0 && 1 < argc-i )
if ( strcmp(argv[i], "--help") == 0 ) { return usage(argc, argv); }
if ( strcmp(argv[i], "--usage") == 0 ) { return usage(argc, argv); }
if ( strcmp(argv[i], "--speed") == 0 && 1 < argc-i )
{
sleepms = String::ToInt(argv[++i]);
sleepms = atoi(argv[++i]);
}
}
@ -175,7 +173,7 @@ int main(int argc, char* argv[])
// Update the game every 50th milisecond.
while ( true )
{
Thread::USleep(sleepms * 1000);
usleep(sleepms * 1000);
Update();
}

View File

@ -1,11 +1,11 @@
#include <libmaxsi/platform.h>
#include <libmaxsi/io.h>
#include <libmaxsi/thread.h>
#include <libmaxsi/keyboard.h>
#include <libmaxsi/string.h>
#include <libmaxsi/sortix-vga.h>
#include <libmaxsi/sortix-keyboard.h>
#include <libmaxsi/sortix-sound.h>
#include <sys/keycodes.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
@ -15,7 +15,6 @@
#include <string.h>
using namespace Maxsi;
using namespace Maxsi::Keyboard;
int Init();
void Reset();
@ -196,18 +195,24 @@ void Update()
void ReadInput()
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint;
while ( (codepoint = System::Keyboard::ReceiveKeystroke(method) ) != 0 )
unsigned termmode = TERMMODE_KBKEY
| TERMMODE_UNICODE
| TERMMODE_SIGNAL
| TERMMODE_NONBLOCK;
if ( settermmode(0, termmode) ) { error(1, errno, "settermmode"); }
while ( true )
{
bool keyup = codepoint & DEPRESSED;
codepoint &= ~DEPRESSED;
if ( codepoint == '\n' && keyup ) { Reset(); }
if ( codepoint == 'w' || codepoint == 'W' ) { p1vup = !keyup; }
if ( codepoint == 's' || codepoint == 'S' ) { p1vdown = !keyup; }
if ( codepoint == UP ) { p2vup = !keyup; }
if ( codepoint == DOWN ) { p2vdown = !keyup; }
uint32_t codepoint;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { return; }
if ( numbytes < 0 ) { return; }
int kbkey = KBKEY_DECODE(codepoint);
int abskbkey = (kbkey < 0) ? -kbkey : kbkey;
if ( kbkey == KBKEY_ENTER ) { Reset(); }
if ( abskbkey == KBKEY_W ) { p1vup = (0 < kbkey); }
if ( abskbkey == KBKEY_S ) { p1vdown = (0 < kbkey); }
if ( abskbkey == KBKEY_UP ) { p2vup = (0 < kbkey); }
if ( abskbkey == KBKEY_DOWN ) { p2vdown = (0 < kbkey); }
}
}

View File

@ -1,9 +1,7 @@
#include <libmaxsi/platform.h>
#include <libmaxsi/io.h>
#include <libmaxsi/thread.h>
#include <libmaxsi/keyboard.h>
#include <libmaxsi/sortix-vga.h>
#include <libmaxsi/sortix-keyboard.h>
#include <sys/keycodes.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
@ -12,9 +10,6 @@
#include <error.h>
#include <string.h>
using namespace Maxsi;
using namespace Maxsi::Keyboard;
const int width = 80;
const int height = 25;
@ -99,21 +94,25 @@ void Update()
int newvely = vely;
// Read the keyboard input from the user.
unsigned method = System::Keyboard::POLL;
uint32_t codepoint;
while ( (codepoint = System::Keyboard::ReceiveKeystroke(method) ) != 0 )
unsigned termmode = TERMMODE_KBKEY
| TERMMODE_UNICODE
| TERMMODE_SIGNAL
| TERMMODE_NONBLOCK;
if ( settermmode(0, termmode) ) { error(1, errno, "settermmode"); }
while ( true )
{
if ( tabhack && codepoint == '\t' ) { tabhacking = true; }
if ( tabhack && codepoint == ('\t' | DEPRESSED ) ) { tabhacking = false; }
bool keyup = codepoint & DEPRESSED;
if ( keyup ) { continue; }
codepoint &= ~DEPRESSED;
if ( codepoint == '\n' ) { Reset(); return; }
if ( codepoint == 'w' || codepoint == 'W' ) { newvelx = 0; newvely = -1; }
if ( codepoint == 'a' || codepoint == 'A' ) { newvelx = -1; newvely = 0; }
if ( codepoint == 's' || codepoint == 'S' ) { newvelx = 0; newvely = 1; }
if ( codepoint == 'd' || codepoint == 'D' ) { newvelx = 1; newvely = 0; }
uint32_t codepoint;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { break; }
if ( numbytes < 0 ) { break; }
int kbkey = KBKEY_DECODE(codepoint);
int abskbkey = (kbkey < 0) ? -kbkey : kbkey;
if ( tabhack && abskbkey == KBKEY_TAB ) { tabhacking = (0 < kbkey); }
if ( kbkey == KBKEY_ENTER ) { Reset(); return; }
if ( kbkey == KBKEY_W ) { newvelx = 0; newvely = -1; }
if ( kbkey == KBKEY_A ) { newvelx = -1; newvely = 0; }
if ( kbkey == KBKEY_S ) { newvelx = 0; newvely = 1; }
if ( kbkey == KBKEY_D ) { newvelx = 1; newvely = 0; }
}
if ( tabhack && tabhacking )
@ -196,7 +195,7 @@ int main(int argc, char* argv[])
// Update the game every once in a while.
while ( true )
{
Thread::USleep(speed * 1000);
usleep(speed * 1000);
Update();
FlushVGA();
}

View File

@ -370,6 +370,8 @@ namespace Sortix
{
}
extern DevTerminal* tty;
Device* DevDevFS::Open(const char* path, int flags, mode_t mode)
{
if ( (flags & O_LOWERFLAGS) == O_SEARCH )
@ -380,7 +382,7 @@ namespace Sortix
}
if ( String::Compare(path, "/null") == 0 ) { return new DevNull; }
if ( String::Compare(path, "/tty") == 0 ) { return new DevLogTTY; }
if ( String::Compare(path, "/tty") == 0 ) { tty->Refer(); return tty; }
if ( String::Compare(path, "/vga") == 0 ) { return new DevVGA; }
if ( path[0] == '/' && path[1] == 'a' && path[2] == 't' && path[3] == 'a' && path[4] && !path[5] )
{

View File

@ -28,15 +28,16 @@
#include "keyboard.h"
#include "kb/ps2.h"
#include "kb/layout/us.h"
#include "kbapiadapter.h"
#include "logterminal.h"
namespace Sortix
{
KBAPIAdapter* tty;
DevTerminal* tty;
uint32_t SysReceiveKeystroke()
{
return tty->DequeueKeystroke();
// TODO: Deprecated, please remove this.
return 0;
}
void Keyboard::Init()
@ -47,7 +48,7 @@ namespace Sortix
KeyboardLayout* kblayout = new KBLayoutUS;
if ( !kblayout ) { Panic("Could not allocate keyboard layout driver"); }
tty = new KBAPIAdapter(keyboard, kblayout);
tty = new LogTerminal(keyboard, kblayout);
if ( !tty ) { Panic("Could not allocate a simple terminal"); }
Syscall::Register(SYSCALL_RECEIVE_KEYSTROKE, (void*) SysReceiveKeystroke);

View File

@ -1,10 +1,11 @@
#include <sys/keycodes.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <libmaxsi/sortix-keyboard.h>
int cat(int argc, char* argv[])
{
@ -55,19 +56,27 @@ int main(int argc, char* argv[])
bool lastwasesc = false;
while (true)
// TODO: This is just compatibility with how cat worked in early versions of
// Sortix. Ideally, this should be removed and just cat the raw stdin.
// Read the keyboard input from the user.
unsigned termmode = TERMMODE_KBKEY | TERMMODE_UNICODE | TERMMODE_SIGNAL;
if ( settermmode(0, termmode) ) { error(1, errno, "settermmode"); }
while ( true )
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
if ( codepoint == 0 ) { continue; }
if ( codepoint & Maxsi::Keyboard::DEPRESSED ) { continue; }
if ( codepoint == Maxsi::Keyboard::UP ) { printf("\e[A"); fflush(stdout); continue; }
if ( codepoint == Maxsi::Keyboard::DOWN ) { printf("\e[B"); fflush(stdout); continue; }
if ( codepoint == Maxsi::Keyboard::RIGHT ) { printf("\e[C"); fflush(stdout); continue; }
if ( codepoint == Maxsi::Keyboard::LEFT ) { printf("\e[D"); fflush(stdout); continue; }
if ( codepoint == Maxsi::Keyboard::ESC ) { printf("\e["); fflush(stdout); lastwasesc = true; continue; }
uint32_t codepoint;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { break; }
if ( numbytes < 0 ) { break; }
int kbkey = KBKEY_DECODE(codepoint);
if ( kbkey < 0 ) { continue; }
if ( kbkey == KBKEY_UP ) { printf("\e[A"); fflush(stdout); continue; }
if ( kbkey == KBKEY_DOWN ) { printf("\e[B"); fflush(stdout); continue; }
if ( kbkey == KBKEY_RIGHT ) { printf("\e[C"); fflush(stdout); continue; }
if ( kbkey == KBKEY_LEFT ) { printf("\e[D"); fflush(stdout); continue; }
if ( kbkey == KBKEY_ESC ) { printf("\e["); fflush(stdout); lastwasesc = true; continue; }
if ( kbkey ) { continue; }
if ( lastwasesc && codepoint == '[' ) { continue; }
if ( codepoint >= 0x80 ) { continue; }
char msg[2]; msg[0] = codepoint; msg[1] = '\0';

View File

@ -1,17 +1,18 @@
#include <sys/keycodes.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <error.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/sortix-keyboard.h>
const int MODE_QUIT = 0;
const int MODE_TEXT = 1;
const int MODE_CONFIRM_QUIT = 2;
const int MODE_SAVE = 3;
const int MODE_LOAD = 4;
const int MODE_QUIT = 1;
const int MODE_TEXT = 2;
const int MODE_CONFIRM_QUIT = 3;
const int MODE_SAVE = 4;
const int MODE_LOAD = 5;
const unsigned WIDTH = 80;
const unsigned HEIGHT = 24;
@ -23,7 +24,7 @@ unsigned numlines = 1;
char filename[256];
bool bufferchanged;
bool bufferchanged = false;
void clearbuffers()
{
@ -35,14 +36,56 @@ void clearbuffers()
bufferchanged = true;
}
using namespace Maxsi;
void cursorto(unsigned x, unsigned y)
{
printf("\e[%u;%uH", y+1+1, x+1);
fflush(stdout);
}
char* readline(int fd)
{
unsigned oldtermmode;
if ( gettermmode(fd, &oldtermmode) ) { return NULL; }
unsigned termmode = TERMMODE_UNICODE
| TERMMODE_SIGNAL
| TERMMODE_UTF8
| TERMMODE_LINEBUFFER
| TERMMODE_ECHO;
if ( settermmode(fd, termmode) ) { return NULL; }
size_t lineused = 0;
size_t linelength = 32UL;
char* line = new char[linelength + 1];
line[0] = '\0';
while ( true )
{
char c;
ssize_t numbytes = read(fd, &c, sizeof(c));
if ( numbytes < 0 ) { delete[] line; line = NULL; break; }
if ( !numbytes ) { break; }
if ( c == '\n' ) { break; }
if ( lineused == linelength )
{
size_t newlinelength = 2 * linelength;
char* newline = new char[newlinelength];
if ( !newline ) { delete[] line; line = NULL; break; }
memcpy(newline, line, lineused * sizeof(*line));
delete[] line;
line = newline;
linelength = newlinelength;
}
line[lineused++] = c;
line[lineused] = '\0';
}
if ( settermmode(fd, oldtermmode) ) { delete[] line; line = NULL; }
return line;
}
void drawtextmode()
{
const char* printfilename = ( strlen(filename) > 0 ) ? filename : "New Buffer";
@ -64,6 +107,7 @@ unsigned textmode()
drawtextmode();
bool ctrl = false;
unsigned dectrlmode = 0;
int oldcursorx = -1;
int oldcursory = -1;
@ -76,46 +120,63 @@ unsigned textmode()
oldcursory = cursory;
}
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
unsigned termmode = TERMMODE_KBKEY | TERMMODE_UNICODE | TERMMODE_SIGNAL;
if ( settermmode(0, termmode) ) { error(1, errno, "settermmode"); }
if ( codepoint == 0 ) { continue; }
if ( codepoint & Keyboard::DEPRESSED )
uint32_t codepoint = 0;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { break; }
if ( numbytes < 0 ) { error(1, errno, "read stdin"); }
if ( numbytes < sizeof(codepoint) ) {
printf("unexpectedly got %zi bytes\n", numbytes);
printf("bytes: %x\n", codepoint);
fprintf(stderr, "bad stdin data\n"); exit(1); }
if ( !codepoint ) { continue; }
int kbkey = KBKEY_DECODE(codepoint);
if ( kbkey )
{
if ( codepoint == Keyboard::CTRL | Keyboard::DEPRESSED ) { ctrl = false; }
if ( kbkey == -KBKEY_LCTRL && dectrlmode ) { return dectrlmode; }
int abskbkey = (kbkey < 0) ? -kbkey : kbkey;
if ( abskbkey == KBKEY_LCTRL ) { ctrl = (0 < kbkey); continue; }
switch ( kbkey )
{
case -KBKEY_ESC:
return MODE_CONFIRM_QUIT;
break;
case KBKEY_UP:
if ( cursory ) { cursory--; }
break;
case KBKEY_DOWN:
if ( cursory < numlines-1 ) { cursory++; }
break;
case KBKEY_LEFT:
if ( cursorx ) { cursorx--; }
break;
case KBKEY_RIGHT:
if ( cursorx < WIDTH-1 ) { cursorx++; }
break;
case KBKEY_O:
if ( ctrl ) { dectrlmode = MODE_SAVE; }
break;
case KBKEY_R:
if ( ctrl ) { dectrlmode = MODE_LOAD; }
break;
case KBKEY_X:
if ( ctrl ) { dectrlmode = MODE_CONFIRM_QUIT; }
break;
}
continue;
}
if ( ctrl )
{
if ( codepoint == 'o' || codepoint == 'O' ) { return MODE_SAVE; }
if ( codepoint == 'r' || codepoint == 'R' ) { return MODE_LOAD; }
if ( codepoint == 'x' || codepoint == 'X' ) { return MODE_CONFIRM_QUIT; }
continue;
}
if ( ctrl ) { continue; }
switch ( codepoint )
{
case Keyboard::ESC:
return MODE_CONFIRM_QUIT;
break;
case Keyboard::CTRL:
ctrl = true;
break;
case Keyboard::UP:
if ( cursory ) { cursory--; }
break;
case '\n':
cursorx = 0;
if ( cursory < HEIGHT-1 ) { numlines++; }
case Keyboard::DOWN:
if ( cursory < numlines-1 ) { cursory++; }
break;
case Keyboard::LEFT:
if ( cursorx ) { cursorx--; }
break;
case Keyboard::RIGHT:
if ( cursorx < WIDTH-1 ) { cursorx++; }
if ( cursory < HEIGHT-1 ) { numlines++; cursory++; }
break;
case '\b':
if ( cursorx )
@ -179,24 +240,32 @@ unsigned confirmquit()
printf("\e37m\e40m\e[2J\e[H");
printf("There are unsaved changes: Are you sure you want to quit? (Y/N)\n");
if ( settermmode(0, TERMMODE_KBKEY | TERMMODE_SIGNAL | TERMMODE_ECHO) )
{
error(1, errno, "settermmode");
}
while ( true )
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
uint32_t codepoint;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { exit(0); }
if ( numbytes < 0 ) { error(1, errno, "read stdin"); }
if ( numbytes < sizeof(codepoint) ) { fprintf(stderr, "bad stdin data\n"); exit(1); }
if ( !codepoint ) { continue; }
if ( codepoint == 0 ) { continue; }
if ( codepoint & Keyboard::DEPRESSED ) { continue; }
int kbkey = KBKEY_DECODE(codepoint);
if ( !kbkey ) { continue; }
if ( 0 < kbkey ) { continue; }
switch ( codepoint )
switch ( kbkey )
{
case Keyboard::ESC:
case -KBKEY_ESC:
return MODE_QUIT;
break;
case 'n':
case 'N':
case -KBKEY_N:
return MODE_TEXT;
case 'y':
case 'Y':
case -KBKEY_Y:
return MODE_QUIT;
default:
printf("Would you like to quit? N for No, Y for Yes\n");
@ -219,6 +288,7 @@ bool savetofile(const char* path)
}
if ( close(fd) ) { error(0, errno, "close: %s", path); return false; }
strcpy(filename, path);
return true;
}
@ -226,54 +296,21 @@ int savemode()
{
printf("\e37m\e40m\e[2J\e[H");
printf("Please enter the filename you wish to save the text to and press "
"enter. Type an empty filename or press ESC to abort.\n\n");
"enter. Type an empty filename to abort.\n\n");
char writefilename[256];
strcpy(writefilename, filename);
char* storage = NULL;
retry:
size_t len = strlen(writefilename);
printf("File Name to Write: %s", writefilename);
fflush(stdout);
bool readytosave = false;
while ( !readytosave )
do
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
delete[] storage;
printf("File to Write: ");
fflush(stdout);
storage = readline(0);
if ( !storage ) { error(1, errno, "readline"); }
if ( !storage[0] ) { delete[] storage; return MODE_TEXT; }
} while ( !savetofile(storage) );
delete[] storage;
if ( codepoint == 0 ) { continue; }
if ( codepoint & Keyboard::DEPRESSED ) { continue; }
switch ( codepoint )
{
case Keyboard::ESC:
return MODE_TEXT;
break;
case '\b':
if ( 0 < len ) { printf("\b"); fflush(stdout); writefilename[--len] = 0; }
break;
case '\n':
if ( len == 0 ) { return MODE_TEXT; }
readytosave = true;
printf("\n");
break;
default:
if ( 0x80 <= codepoint ) { continue; }
writefilename[len++] = codepoint;
writefilename[len+1] = 0;
char msg[2];
msg[0] = codepoint;
msg[1] = 0;
printf("%s", msg);
fflush(stdout);
}
}
if ( !savetofile(writefilename) ) { goto retry; }
strcpy(filename, writefilename);
bufferchanged = false;
printf("Succesfully saved\n");
@ -318,6 +355,8 @@ bool loadfromfile(const char* path)
cursorx = 0;
cursory = 0;
strcpy(filename, path);
return true;
}
@ -325,60 +364,21 @@ int loadmode()
{
printf("\e37m\e40m\e[2J\e[H");
printf("Please enter the filename you wish to load text from and press "
"enter. Type an empty filename or press ESC to abort.\n\n");
"enter. Type an empty filename to abort.\n\n");
char loadfilename[256];
loadfilename[0] = 0;
char* storage = NULL;
retry:
size_t len = strlen(loadfilename);
printf("File Name to Load: %s", loadfilename);
fflush(stdout);
bool readytoload = false;
while ( !readytoload )
do
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
delete[] storage;
printf("File to Load: ");
fflush(stdout);
storage = readline(0);
if ( !storage ) { error(1, errno, "readline"); }
if ( !storage[0] ) { delete[] storage; return MODE_TEXT; }
} while ( !loadfromfile(storage) );
delete[] storage;
if ( codepoint == 0 ) { continue; }
if ( codepoint & Keyboard::DEPRESSED ) { continue; }
switch ( codepoint )
{
case Keyboard::ESC:
return MODE_TEXT;
break;
case '\b':
if ( 0 < len ) { printf("\b"); fflush(stdout); loadfilename[--len] = 0; }
break;
case '\n':
if ( len == 0 ) { return MODE_TEXT; }
readytoload = true;
printf("\n");
break;
default:
if ( 0x80 <= codepoint ) { continue; }
loadfilename[len++] = codepoint;
loadfilename[len+1] = 0;
char msg[2];
msg[0] = codepoint;
msg[1] = 0;
printf("%s", msg);
fflush(stdout);
}
}
if ( !loadfromfile(loadfilename) )
{
clearbuffers();
cursorx = 0;
cursory = 0;
goto retry;
}
strcpy(filename, loadfilename);
bufferchanged = false;
return MODE_TEXT;

View File

@ -4,9 +4,9 @@
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <libmaxsi/thread.h>
using namespace Maxsi;
@ -39,7 +39,7 @@ int main(int argc, char* argv[])
printf("\r\e[m\e[J");
fflush(stdout);
pid_t childpid = Process::Fork();
pid_t childpid = fork();
if ( childpid < 0 ) { perror("fork"); return 2; }
return ( childpid == 0 ) ? child() : parent(childpid);

View File

@ -1,4 +1,5 @@
#include <sys/wait.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -8,8 +9,6 @@
#include <fcntl.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <libmaxsi/sortix-keyboard.h>
#include <libmaxsi/string.h>
using namespace Maxsi;
@ -17,6 +16,13 @@ int status = 0;
void command()
{
unsigned termmode = TERMMODE_UNICODE
| TERMMODE_SIGNAL
| TERMMODE_UTF8
| TERMMODE_LINEBUFFER
| TERMMODE_ECHO;
settermmode(0, termmode);
const size_t CWD_SIZE = 512;
char cwd[CWD_SIZE];
const char* wd = getcwd(cwd, CWD_SIZE);
@ -31,35 +37,23 @@ void command()
while (true)
{
unsigned method = System::Keyboard::POLL;
uint32_t codepoint = System::Keyboard::ReceiveKeystroke(method);
if ( codepoint == 0 ) { continue; }
if ( codepoint & Maxsi::Keyboard::DEPRESSED ) { continue; }
if ( codepoint >= 0x80 ) { continue; }
if ( codepoint == '\b' )
{
if ( 0 < commandused ) { printf("\b"); fflush(stdout); commandused--; }
continue;
}
if ( commandsize <= commandused && codepoint != '\n' ) { continue; }
char msg[2]; msg[0] = codepoint; msg[1] = '\0';
printf("%s", msg);
fflush(stdout);
if ( codepoint == '\n' ) { command[commandused] = '\0'; break; }
command[commandused++] = codepoint;
char c;
ssize_t bytesread = read(1, &c, sizeof(c));
if ( bytesread < 0 ) { error(64, errno, "read stdin"); }
if ( !bytesread ) { break; }
if ( !c ) { continue; }
if ( c == '\n' ) { break; }
if ( commandsize <= commandused ) { continue; }
command[commandused++] = c;
}
command[commandused] = '\0';
if ( command[0] == '\0' ) { return; }
if ( String::Compare(command, "$?") == 0 ) { printf("%u\n", status); status = 0; return; }
if ( String::Compare(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); status = 0; return; }
if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); status = 0; return; }
if ( strcmp(command, "$?") == 0 ) { printf("%u\n", status); status = 0; return; }
if ( strcmp(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); status = 0; return; }
if ( strcmp(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); status = 0; return; }
int argc = 0;
const char* argv[256];

View File

@ -1,3 +1,5 @@
#include <sys/keycodes.h>
#include <sys/termmode.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -5,10 +7,6 @@
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/sortix-keyboard.h>
using namespace Maxsi;
int main(int argc, char* argv[])
{
@ -45,17 +43,21 @@ int main(int argc, char* argv[])
{
printf("\n--pager--");
fflush(stdout);
uint32_t codepoint;
settermmode(0, TERMMODE_KBKEY | TERMMODE_SIGNAL);
bool doexit = false;
int kbkey;
do
{
unsigned method = System::Keyboard::POLL;
codepoint = System::Keyboard::ReceiveKeystroke(method);
if ( codepoint == Keyboard::DOWN ) { break; }
if ( codepoint == Keyboard::PGDOWN ) { linesleft = HEIGHT-1; break; }
if ( codepoint == 'q' ) { doexit = true; break; }
}
while ( codepoint != '\n' );
uint32_t codepoint;
ssize_t numbytes = read(0, &codepoint, sizeof(codepoint));
if ( !numbytes ) { exit(0); }
if ( numbytes < 0 ) { error(1, errno, "read(stdin)"); }
if ( numbytes < sizeof(codepoint) ) { error(1, errno, "bad stdin"); }
if ( !(kbkey = KBKEY_DECODE(codepoint)) ) { continue; }
if ( kbkey == KBKEY_DOWN ) { break; }
if ( kbkey == KBKEY_PGDOWN ) { linesleft = HEIGHT-1; break; }
if ( kbkey == -KBKEY_Q ) { doexit = true; break; }
} while ( kbkey != KBKEY_ENTER );
printf("\r\e[J");
if ( doexit ) { exit(result); }
continue;