Skip to content

Commit

Permalink
programs: x
Browse files Browse the repository at this point in the history
  • Loading branch information
rouseabout committed Feb 2, 2025
1 parent 1bd6f75 commit 33d9127
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ INITRDSIZE=640
#NASMFORMAT=elf64
#INITRDSIZE=1280

PROGRAMS="alloc asm box chat crash draw flash forkbomb getty hello hello++ hexdump hostname init ld sh testcases tolower truncate unixping unixserver"
PROGRAMS="alloc asm box chat crash draw flash forkbomb getty hello hello++ hexdump hostname init ld sh testcases tolower truncate unixping unixserver x"
BOXPROGRAMS="cal cat chmod cksum clear cmp date dd df echo env expr false find grep head kill ln logger ls mkdir mktemp more mv pwd reset rm rmdir sleep sort strings tail tee touch tr true uname vi wc xargs"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tolower
truncate
unixping
unixserver
x
dumpelf
dumpext2
dumpgpt
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ To use the text mode console, it is neccessary to first set the `MULTIBOOT_VIDEO
|unixserver |unix sockets demonstration server |
|vi |vi |
|wc |word count |
|x |mouse and framebuffer demonstration |
|xargs |run program with arguments from standard input |

There is no `cp` program; use `cat source > dest` instead.
Expand Down
103 changes: 103 additions & 0 deletions programs/x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <linux/fb.h>

static uint8_t * fb_addr;
static int fb_stride, fb_width, fb_height, fb_bpp;

static void xor_box(int xx, int yy, int width, int height)
{
if (fb_bpp == 1) {
for (int j = yy; j < yy + height && j < fb_height; j++)
for (int i = xx; i < xx + width && i < fb_width; i++)
fb_addr[j * fb_stride + i/8] ^= ~(1 << (7 - (i & 7)));
} else {
for (int j = yy; j < yy + height && j < fb_height; j++)
for (int i = xx; i < xx + width && i < fb_width; i++)
for (unsigned int k = 0; k < fb_bpp/8; k++)
fb_addr[j * fb_stride + i * fb_bpp/8 + k] ^= 0xFF;
}
}

int main(int argc, char ** argv)
{
int fd = open("/dev/fb0", O_RDWR);
if (fd == -1) {
perror("/dev/fb0");
return EXIT_FAILURE;
}

struct fb_fix_screeninfo fixinfo;
if (ioctl(fd, FBIOGET_FSCREENINFO, &fixinfo) < 0)
return EXIT_FAILURE;

struct fb_var_screeninfo varinfo;
if (ioctl(fd, FBIOGET_VSCREENINFO, &varinfo) < 0)
return EXIT_FAILURE;

fb_stride = fixinfo.line_length;
fb_width = varinfo.xres;
fb_height = varinfo.yres;
fb_bpp = varinfo.bits_per_pixel;

fb_addr = mmap(NULL, fixinfo.smem_len, PROT_WRITE, MAP_SHARED, fd, 0);
if (fb_addr == MAP_FAILED) {
perror("mmap");
return EXIT_FAILURE;
}

int mouse_fd = open("/dev/mouse0", O_RDONLY);
if (mouse_fd == -1) {
perror("/dev/mouse0");
return EXIT_FAILURE;
}

int x = fb_width / 2;
int y = fb_height / 2;
int buttons = 0;
xor_box(x, y, 8, 8);

while (1) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(mouse_fd, &rfds);

#define MIN(a, b) ((a)<(b)?(a):(b))
#define MAX(a, b) ((a)>(b)?(a):(b))
int n = select(mouse_fd + 1, &rfds, NULL, NULL, NULL);
if (n < 0) {
perror("select");
return EXIT_FAILURE;
}

if (FD_ISSET(mouse_fd, &rfds)) {
char mouse[3];
if (read(mouse_fd, mouse, 3) == 3) {
xor_box(x, y, 8, 8);
int b = mouse[0] & 7;
int dx = mouse[1];
int dy = mouse[2];
x = MIN(MAX(x + dx, 0), fb_width - 1);
y = MIN(MAX(y - dy, 0), fb_height - 1);
if (buttons ^ b) {
fprintf(stdout, "[%d%d%d]", !!(b & 1), !!(b & 2), !!(b & 4));
fflush(stdout);
}
buttons = b & 7;
xor_box(x, y, 8, 8);
}
}
}

munmap(fb_addr, fixinfo.smem_len);
close(fd);
close(mouse_fd);

return EXIT_SUCCESS;
}

0 comments on commit 33d9127

Please sign in to comment.