~aritra1911/ansi_anim

7bd3d29dd2c21ad08fb05aee982a393873ca2c2b — Aritra Sarkar 2 years ago 1fa5a02
Report screen size
6 files changed, 60 insertions(+), 18 deletions(-)

M Makefile
M README.md
A ansilib/inc/ansi/screen.h
M ansilib/src/ansi/cursor.c
A ansilib/src/ansi/screen.c
M src/main.c
M Makefile => Makefile +9 -7
@@ 3,19 3,21 @@ CC ?= cc
INC = -Iinc -Iansilib/inc
LIB =

CFLAGS = $(INC) -Wall -Wextra -pedantic -std=c99
CFLAGS = $(INC) -Wall -Wextra -pedantic -g -std=c99
CPPFLAGS =
LDFLAGS =

SRCS = ansilib/src/graphics/common.c \
       ansilib/src/graphics/shapes.c \
SRCS = src/main.c \
       ansilib/src/ansi/screen.c \
       ansilib/src/ansi/cursor.c \
       src/main.c
       ansilib/src/graphics/common.c \
       ansilib/src/graphics/shapes.c

OBJS = obj/graphics/common.o \
       obj/graphics/shapes.o \
OBJS = obj/main.o \
       obj/ansi/screen.o \
       obj/ansi/cursor.o \
       obj/main.o
       obj/graphics/common.o \
       obj/graphics/shapes.o

.PHONY: all clean


M README.md => README.md +2 -1
@@ 6,4 6,5 @@ off-site which should be running with FreeRTOS on an STM32 microcontroller which
should be outputting to a serial terminal. Updates coming shortly.

## References
1. https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
1. [Entering Raw Mode](https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html)
2. [ANSI Escape Sequences](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797)

A ansilib/inc/ansi/screen.h => ansilib/inc/ansi/screen.h +8 -0
@@ 0,0 1,8 @@
#ifndef __ANSI_SCREEN_H_
# define __ANSI_SCREEN_H_

# include <graphics/common.h>

point_t get_screen_size(void);

#endif  /* __ANSI_SCREEN_H_ */

M ansilib/src/ansi/cursor.c => ansilib/src/ansi/cursor.c +7 -6
@@ 18,7 18,7 @@ point_t get_cursor_pos(void)
    if ( read(STDIN_FILENO, buf, 16) ) {

        uint32_t line, column;
        sscanf(buf, CSI"%"PRIu32"i;%"PRIu32"iR", &line, &column);
        sscanf(buf, CSI"%"PRIu32";%"PRIu32"R", &line, &column);

        ret.x = column;
        ret.y = line;


@@ 37,12 37,13 @@ void move_cursor(point_t pos)
     * ESC[{line};{column}f
     */

    sprintf(buf, CSI"%i;%iH", /* line */ pos.y, /* column */ pos.x);
    sprintf(buf, CSI"%i;%iH", pos.y, pos.x);
    write(STDOUT_FILENO, (void *) buf, strlen(buf));
}

void nudge_cursor(dir_t direction, uint32_t step)
{
    /* TODO: `direction` is a bitmask */
    char buf[16], end_ch;

    /*


@@ 53,10 54,10 @@ void nudge_cursor(dir_t direction, uint32_t step)
     */

    switch ( direction ) {
    case UP:    end_ch = 'A'; break;
    case DOWN:  end_ch = 'B'; break;
    case RIGHT: end_ch = 'C'; break;
    case LEFT:  end_ch = 'D'; break;
        case UP:    end_ch = 'A'; break;
        case DOWN:  end_ch = 'B'; break;
        case RIGHT: end_ch = 'C'; break;
        case LEFT:  end_ch = 'D';
    }

    sprintf(buf, CSI"%i%c", step, end_ch);

A ansilib/src/ansi/screen.c => ansilib/src/ansi/screen.c +28 -0
@@ 0,0 1,28 @@
#include <unistd.h>

#include <ansi/cursor.h>

point_t get_screen_size(void)
{
    point_t ret, orig_pos = get_cursor_pos();

    /* Quoting from
     * https://viewsourcecode.org/snaptoken/kilo/03.rawInputAndOutput.html#window-size-the-hard-way
     *
     * We use a very large value, 999, which should ensure that the
     * cursor reaches the right and bottom edges of the screen.
     *
     * The C and B commands are specifically documented to stop the
     * cursor from going past the edge of the screen. The reason we
     * don’t use the <esc>[999;999H command is that the documentation
     * doesn’t specify what happens when you try to move the cursor
     * off-screen.
     */
    //nudge_cursor(RIGHT, 999);
    //nudge_cursor(DOWN, 999);
    write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12);
    ret = get_cursor_pos();

    move_cursor(orig_pos);
    return ret;
}

M src/main.c => src/main.c +6 -4
@@ 5,6 5,7 @@
#include <unistd.h>

#include <ansi/cursor.h>
#include <ansi/screen.h>
#include <graphics/common.h>
#include <graphics/shapes.h>



@@ 69,12 70,13 @@ int main(void)
     * }
     */

    /* save current cursor position */
    point_t orig_cursor_pos = get_cursor_pos();
    /* print out screen size */
    point_t screen_size = get_screen_size();
    printf("LINES = %i  COLUMNS = %i\r\n", screen_size.y, screen_size.x);

    /* Draw a rectangle */
    point_t orig_cursor_pos = get_cursor_pos();
    draw_rectangle((point_t) { 20, 40 }, 32, 16);

    /* Return to the original cursor position */
    move_cursor(orig_cursor_pos);

    while ( 1 ) {