From ecdc45374aecdbcdb5c1d374742d9c528fb8298e Mon Sep 17 00:00:00 2001 From: EliteTK Date: Mon, 29 Dec 2014 01:30:17 +0000 Subject: More code. --- ant.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ blocks.c | 5 ++ cap.c | 11 ++-- csgocolors.c | 8 +++ fibbonacci.c | 13 ++++ simple_x.c | 73 +++++++++++++++++++++ test.c | 0 timer.c | 10 +-- xcb_gol.c | 4 ++ xcb_gol_macro.c | 4 ++ xosd_test.c | 22 +++++++ 11 files changed, 337 insertions(+), 11 deletions(-) create mode 100644 ant.c create mode 100644 blocks.c create mode 100644 csgocolors.c create mode 100644 fibbonacci.c create mode 100644 simple_x.c create mode 100644 test.c create mode 100644 xosd_test.c diff --git a/ant.c b/ant.c new file mode 100644 index 0000000..a6b2fa8 --- /dev/null +++ b/ant.c @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2014 Tomasz Kramkowski + * + * This program is free software. It is licensed under version 3 of the + * GNU General Public License. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see [http://www.gnu.org/licenses/]. + */ +#include +#include +#include +#include +#include +#include +#include + +#define WIDTH 500 +#define HEIGHT 500 + +#define ANT_BPOS(ant) ((ant)->buffer[(ant)->posy * (ant)->width + (ant)->posx]) + +enum ant_direction { + NORTH, + EAST, + SOUTH, + WEST +}; + +typedef struct ant { + uint8_t *buffer; + char *actions; + uint16_t width, height; + uint16_t posx, posy; + enum ant_direction direction; +} Ant; + +Ant *ant_new(uint16_t width, uint16_t height, char *actions) +{ + Ant *ant = malloc(sizeof(Ant)); + + ant->buffer = calloc(sizeof(uint8_t), width * height); + + ant->actions = strdup(actions); + + ant->width = width; + ant->height = height; + + ant->posx = width / 2; + ant->posy = width / 2; + + ant->direction = NORTH; + + return ant; +} + +void ant_del(Ant *ant) +{ + free(ant->buffer); + free(ant->actions); + free(ant); +} + +void inline ant_left(Ant *ant) +{ + ant->direction = ant->direction == 0 ? 3 : ant->direction - 1; +} + +void inline ant_right(Ant *ant) +{ + ant->direction = ant->direction == 3 ? 0 : ant->direction + 1; +} + +void inline ant_forward(Ant *ant) +{ + if (ant->direction & 1) { /* EAST or WEST */ + ant->posx = ant->direction == EAST ? ant->posx + 1 : ant->posx - 1; + } else { + ant->posy = ant->direction == SOUTH ? ant->posy + 1 : ant->posy - 1; + } +} + +void ant_simulate(Ant *ant) +{ + uint8_t act_current = ANT_BPOS(ant); + uint8_t act_next = ant->actions[act_current + 1] != '\0' ? act_current + 1 : 0; + + if (ant->actions[act_current] == 'L') { + ant_left(ant); + ANT_BPOS(ant) = act_next; + ant_forward(ant); + } else { + ant_right(ant); + ANT_BPOS(ant) = act_next; + ant_forward(ant); + } + +} + +int main(int argc, char **argv) +{ + if (argc != 2) { + printf("Usage: ant \n"); + exit(1); + } + + xcb_connection_t *connection = xcb_connect(NULL, NULL); + + xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; + + uint32_t mask = XCB_CW_BACK_PIXEL; + uint32_t values[] = {screen->black_pixel}; + + xcb_drawable_t window = xcb_generate_id(connection); + xcb_create_window(connection, + 24, + window, + screen->root, + 0, 0, + WIDTH, HEIGHT, + 1, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + screen->root_visual, + mask, values); + + xcb_pixmap_t pixmap = xcb_generate_id(connection); + xcb_create_pixmap(connection, + 24, + pixmap, + window, + WIDTH, HEIGHT); + + uint8_t *img = malloc(WIDTH * HEIGHT * 4); + xcb_image_t *image = xcb_image_create(WIDTH, HEIGHT, + XCB_IMAGE_FORMAT_Z_PIXMAP, + 8, 24, 32, + 0, + XCB_IMAGE_ORDER_MSB_FIRST, + XCB_IMAGE_ORDER_LSB_FIRST, + img, + WIDTH * HEIGHT * 4, + img); + + mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND; + values[0] = screen->black_pixel; + values[1] = 0xFFFFFF; + + xcb_gcontext_t gc = xcb_generate_id(connection); + xcb_create_gc(connection, + gc, + pixmap, + mask, values); + + xcb_image_put(connection, pixmap, gc, image, 0, 0, 0); + + xcb_map_window(connection, window); + xcb_flush(connection); + + uint8_t value = 0; + + uint32_t *limg; + + uint32_t step = 0; + + Ant *ant = ant_new(WIDTH, HEIGHT, argv[1]); + putchar('\n'); + + while (1) { + printf("\rStep: %d", step++); + fflush(stdout); + limg = (uint32_t *)image->data; + + for (int i = 0; i < WIDTH * HEIGHT; i++) { + uint32_t colour = 0; + + colour |= (ant->buffer[i] & 1) * 0xFF << 0; + colour |= (ant->buffer[i] >> 1) * 0xFF << 8; + colour |= (ant->buffer[i] >> 2) * 0xFF << 16; + + *(limg++) = colour; + } + + xcb_image_put(connection, pixmap, gc, image, 0, 0, 0); + + xcb_copy_area(connection, + pixmap, + window, + gc, + 0, 0, + 0, 0, + WIDTH, HEIGHT); + xcb_flush(connection); + value++; + + ant_simulate(ant); + } + return 0; +} diff --git a/blocks.c b/blocks.c new file mode 100644 index 0000000..baf31f6 --- /dev/null +++ b/blocks.c @@ -0,0 +1,5 @@ +int main(void) +{ + int i = ({ static int i = 3; i + 5; }); + return i; +} diff --git a/cap.c b/cap.c index c36732d..d87339f 100644 --- a/cap.c +++ b/cap.c @@ -12,11 +12,10 @@ #include #include -char *capitalise(char *string) +char *capitalise(char *string, size_t size) { - size_t length = strlen(string); bool last_sep = true; /* Nothing is a separator too. */ - for (unsigned i = 0; i < length; i++) { + for (size_t i = 0; i < size; i++) { if (isalpha(string[i])) { string[i] = last_sep ? toupper(string[i]) : tolower(string[i]); last_sep = false; @@ -28,7 +27,7 @@ char *capitalise(char *string) int main(void) { - char string[] = "thIs iS MEANT to Be some Kind OF tEsT strinG?\n"; - printf(capitalise(string)); - return true; + char string[] = "thIs iS MEANT to Be some Kind OF tEsT strinG?!.-+=#'; +-'#~@\n"; + printf("%s", capitalise(string, strlen(string))); + return 0; } diff --git a/csgocolors.c b/csgocolors.c new file mode 100644 index 0000000..e221741 --- /dev/null +++ b/csgocolors.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + for (unsigned char i = 0; i < 128; i++) + printf("%c%04x ", i, i); + return 0; +} diff --git a/fibbonacci.c b/fibbonacci.c new file mode 100644 index 0000000..4ebad5c --- /dev/null +++ b/fibbonacci.c @@ -0,0 +1,13 @@ +#include + +int main(/*int argc, char **argv*/) { + int a = 0, b = 1, c = 0; + + do { + printf("%d, ", a); + a = b + c; + c = b; + b = a; + } while (a < 100); + return 0; +} diff --git a/simple_x.c b/simple_x.c new file mode 100644 index 0000000..5857130 --- /dev/null +++ b/simple_x.c @@ -0,0 +1,73 @@ +/* SimplX - Proposed name if not taken already. */ +#include +#include +#include + +/* + * TODO: Figure out the alignment of these things fix the arrangement. + * Find a good name for this. + */ +typedef struct sx_gc { + /*struct {*/ + xcb_connection_t *connection; + xcb_screen_t *screen; + xcb_drawable_t window; + xcb_pixmap_t pixmap; + xcb_image_t *image; + xcb_gcontext_t gc; + /*} xcb;*/ +} SX; + +SX *sx_new_gc(uint16_t width, uint16_t height) +{ + SX *sx = malloc(sizeof(SX)); + + sx->connection = xcb_connect(NULL, NULL); + + sx->screen = xcb_setup_roots_iterator(xcb_get_setup(sx->connection)).data; + + uint32_t mask = XCB_CW_BACK_PIXEL; /* Needs events */ + uint32_t values[] = {sx->screen->black_pixel}; + + sx->window = xcb_generate_id(sx->connection); + xcb_create_window(sx->connection, + 24, + sx->window, + sx->screen->root, + 0, 0, + width, height, + 1, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + sx->screen->root_visual, + mask, values); + + sx->pixmap = xcb_generate_id(sx->connection); + xcb_create_pixmap(sx->connection, + 24, + sx->pixmap, + sx->window, + width, height); + + uint8_t *img = malloc(width * height * 4); + xcb_image_t *image = xcb_image_create(width, height, + XCB_IMAGE_FORMAT_Z_PIXMAP, + 8, 24, 32, + 0, + XCB_IMAGE_ORDER_MSB_FIRST, + XCB_IMAGE_ORDER_LSB_FIRST, + img, + width * height * 4, + img); + + mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND; + values[0] = sx->screen->black_pixel; + values[1] = 0xFFFFFF; + + sx->gc = xcb_generate_id(sx->connection); + xcb_create_gc(sx->connection, + sx->gc, + sx->pixmap, + mask, values); + + return sx; +} diff --git a/test.c b/test.c new file mode 100644 index 0000000..e69de29 diff --git a/timer.c b/timer.c index b3ca4f9..506af50 100644 --- a/timer.c +++ b/timer.c @@ -16,7 +16,7 @@ #include #include -unsigned long long int get_seconds(char *); +unsigned long int get_seconds(char *); void usage(char *cmd) { @@ -32,12 +32,12 @@ int main(int argc, char **argv) exit(1); } - unsigned long long int total_seconds = 0; + unsigned long int total_seconds = 0; for (int i = 1; i < argc; i++) total_seconds += get_seconds(argv[i]); - printf("Total time: %llu second(s).\nStarted at: %d\n", total_seconds, time(NULL)); + printf("Total time: %lu second(s).\nStarted at: %d\n", total_seconds, time(NULL)); sleep(total_seconds); @@ -51,7 +51,7 @@ int main(int argc, char **argv) return 0; } -unsigned long long int get_seconds(char *code) +unsigned long int get_seconds(char *code) { int length = strlen(code); if (length < 2) { @@ -75,5 +75,5 @@ unsigned long long int get_seconds(char *code) value[length - 1] = '\0'; - return strtoull(value, NULL, 10) * multiplier; + return strtoul(value, NULL, 10) * multiplier; } diff --git a/xcb_gol.c b/xcb_gol.c index 71beeb0..d07c2d6 100644 --- a/xcb_gol.c +++ b/xcb_gol.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -126,6 +127,9 @@ int main(int argc, char **argv) XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, mask, values); + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window, + XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, + strlen(argv[0]), argv[0]); xcb_pixmap_t pixmap = xcb_generate_id(connection); xcb_create_pixmap(connection, diff --git a/xcb_gol_macro.c b/xcb_gol_macro.c index 6e04b77..9c35855 100644 --- a/xcb_gol_macro.c +++ b/xcb_gol_macro.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -114,6 +115,9 @@ int main(int argc, char **argv) XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, mask, values); + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, window, + XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, + strlen(argv[0]), argv[0]); xcb_pixmap_t pixmap = xcb_generate_id(connection); xcb_create_pixmap(connection, diff --git a/xosd_test.c b/xosd_test.c new file mode 100644 index 0000000..4ed5c1f --- /dev/null +++ b/xosd_test.c @@ -0,0 +1,22 @@ +#include +#include +#include + +int main(void) +{ + xosd *osd = xosd_create(1); + + xosd_set_font(osd, "fixed"); + xosd_set_colour(osd, "LawnGreen"); + xosd_set_timeout(osd, 3); + xosd_set_shadow_offset(osd, 1); + + xosd_display(osd, 0, XOSD_string, "Example XOSD output"); + + xosd_wait_until_no_display(osd); + + xosd_destroy(osd); + + return EXIT_SUCCESS; +} + -- cgit v1.2.3-54-g00ecf