Add a few more extra apps from mainline

This commit is contained in:
K. Lange 2018-05-09 19:27:42 +09:00
parent cd70295716
commit 471d56e42d
3 changed files with 237 additions and 0 deletions

75
apps/piano.c Normal file
View File

@ -0,0 +1,75 @@
/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2014-2018 K. Lange
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
int spkr = 0;
struct spkr {
int length;
int frequency;
};
void note(int length, int frequency) {
struct spkr s = {
.length = 2,
.frequency = frequency,
};
write(spkr, &s, sizeof(s));
}
struct termios old;
void set_unbuffered() {
tcgetattr(fileno(stdin), &old);
struct termios new = old;
new.c_lflag &= (~ICANON & ~ECHO);
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
}
int main(int argc, char * argv[]) {
spkr = open("/dev/spkr", O_WRONLY);
if (spkr == -1) {
fprintf(stderr, "%s: could not open speaker\n", argv[0]);
return 1;
}
set_unbuffered();
char c;
while ((c = fgetc(stdin))) {
switch (c) {
case 'q': return 0;
case 'z': return 0;
case 'a': note(10, 1308); break;
case 'w': note(10, 1386); break;
case 's': note(10, 1468); break;
case 'e': note(10, 1556); break;
case 'd': note(10, 1648); break;
case 'f': note(10, 1746); break;
case 't': note(10, 1850); break;
case 'g': note(10, 1960); break;
case 'y': note(10, 2077); break;
case 'h': note(10, 2200); break;
case 'u': note(10, 2331); break;
case 'j': note(10, 2469); break;
case 'k': note(10, 2616); break;
case 'o': note(10, 2772); break;
case 'l': note(10, 2937); break;
case 'p': note(10, 3111); break;
case ';': note(10, 3296); break;
case '\'': note(10, 3492);break;
}
}
return 0;
}

148
apps/serial-console.c Normal file
View File

@ -0,0 +1,148 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2014 K. Lange
*
* serial console
*
* Runs a dumb console on a serial port or something similar.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <syscall.h>
#include <signal.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/wait.h>
int fd = 0;
int keep_echo = 0;
int dos_lines = 0;
int keep_canon = 0;
struct termios old;
void set_unbuffered() {
tcgetattr(fileno(stdin), &old);
struct termios new = old;
if (!keep_canon) {
new.c_lflag &= (~ICANON);
}
if (!keep_echo) {
new.c_lflag &= (~ECHO);
}
tcsetattr(fileno(stdin), TCSAFLUSH, &new);
}
void set_buffered() {
tcsetattr(fileno(stdin), TCSAFLUSH, &old);
}
int show_usage(int argc, char * argv[]) {
printf(
"Serial client.\n"
"\n"
"usage: %s [-e] [-r] [-c] [device path]\n"
"\n"
" -e \033[3mkeep echo enabled\033[0m\n"
" -c \033[3mkeep canon enabled\033[0m\n"
" -r \033[3mtransform line feeds to \\r\\n\033[0m\n"
" -? \033[3mshow this help text\033[0m\n"
"\n", argv[0]);
return 1;
}
int main(int argc, char ** argv) {
int arg = 1;
char * device;
while (arg < argc) {
if (argv[arg][0] != '-') break;
if (!strcmp(argv[arg], "-e")) {
keep_echo = 1;
} else if (!strcmp(argv[arg], "-r")) {
dos_lines = 1;
} else if (!strcmp(argv[arg], "-c")) {
keep_canon = 1;
} else if (!strcmp(argv[arg], "-?")) {
return show_usage(argc, argv);
} else {
fprintf(stderr, "%s: Unrecognized option: %s\n", argv[0], argv[arg]);
}
arg++;
}
if (arg == argc) {
device = "/dev/ttyS0";
} else {
device = argv[arg];
}
set_unbuffered();
fd = open(device, 0, 0);
int fds[2] = {STDIN_FILENO, fd};
while (1) {
int index = syscall_fswait(2, fds);
if (index == -1) {
fprintf(stderr, "serial-console: fswait: erroneous file descriptor\n");
fprintf(stderr, "serial-console: (did you try to open a file that isn't a serial console?\n");
return 1;
}
if (index == 0) {
char c = fgetc(stdin);
if (c == 0x1D) { /* ^] */
while (1) {
printf("serial-console> ");
set_buffered();
fflush(stdout);
char line[1024];
fgets(line, 1024, stdin);
if (feof(stdin)) {
return 0;
}
int i = strlen(line);
line[i-1] = '\0';
if (!strcmp(line, "quit")) {
return 0;
} else if (!strcmp(line, "continue")) {
set_unbuffered();
fflush(stdout);
break;
}
}
} else {
if (dos_lines && c == '\n') {
char buf[1] = {'\r'};
write(fd, buf, 1);
}
char buf[1] = {c};
write(fd, buf, 1);
}
} else {
char buf[1024];
size_t r = read(fd, buf, 1024);
fwrite(buf, 1, r, stdout);
fflush(stdout);
}
}
close(fd);
set_buffered();
return 0;
}

14
apps/term-size.c Normal file
View File

@ -0,0 +1,14 @@
/* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013 Kevin Lange
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <termios.h>
int main(int argc, char * argv[]) {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
printf("Terminal is %dx%d (%d px x %d px)\n", w.ws_col, w.ws_row, w.ws_xpixel, w.ws_ypixel);
return 0;
}