From 0a23128c376540cd139dde19a1f09ae40617e670 Mon Sep 17 00:00:00 2001 From: EliteTK Date: Sat, 6 Sep 2014 18:33:33 +0100 Subject: Latest 'stuff' --- dinner.c | 18 +++++ line.c | 7 ++ randtest.c | 35 +++++++++ split32.c | 74 +++++++++++++++++++ strip.c | 9 +++ timer.c | 81 +++++++++++--------- xcb.c | 70 +++++++++++++----- xcb_colormaps.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++ xcb_fixed.c | 211 +++++++++++++++++++++++++++++++++++++++++++++++++++++ xcb_imagereading.c | 86 ++++++++++++++++++++++ 10 files changed, 723 insertions(+), 53 deletions(-) create mode 100644 dinner.c create mode 100644 line.c create mode 100644 randtest.c create mode 100644 split32.c create mode 100644 strip.c create mode 100644 xcb_colormaps.c create mode 100644 xcb_fixed.c create mode 100644 xcb_imagereading.c diff --git a/dinner.c b/dinner.c new file mode 100644 index 0000000..d755ba3 --- /dev/null +++ b/dinner.c @@ -0,0 +1,18 @@ +#include +#include + +int main(int argc, char **argv) { + sleep(1); + printf("Start:\a\n"); + for (int i = 0; i < 45; i += 5) { + sleep(300); + printf("Check water levels.\a\n"); + if (i == 24) { + printf("Add stuff from plate.\n"); + } + } + + printf("All done.\a\n"); + + return 0; +} diff --git a/line.c b/line.c new file mode 100644 index 0000000..06d6ca0 --- /dev/null +++ b/line.c @@ -0,0 +1,7 @@ +#include + +int main(int argc, char **argv) +{ + printf("Line: %d\n", __LINE__); + return 0; +} diff --git a/randtest.c b/randtest.c new file mode 100644 index 0000000..06a71de --- /dev/null +++ b/randtest.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +#define FROM 0 +#define TO 9 + +int8_t randrange(int8_t from, int8_t to) +{ + int base_random = rand(); + if (RAND_MAX == base_random) return randrange(from, to); + int range = to - from, + remainder = RAND_MAX % range, + bucket = RAND_MAX / range; + if (base_random < RAND_MAX - remainder) { + return from + base_random/bucket; + } else { + return randrange(from, to); + } +} + +int main(int argc, char **argv) +{ + srand(time(NULL)); + int r; + uint16_t *amounts = calloc(sizeof(uint16_t), (TO - FROM + 1)); + for (int i = 0; i < 1000; i++) + amounts[randrange(FROM, TO) - FROM]++; + + for (int i = 0; i < TO - FROM + 1; i++) + printf("%d: %d\n", i, amounts[i]); + + return 0; +} diff --git a/split32.c b/split32.c new file mode 100644 index 0000000..78e72c7 --- /dev/null +++ b/split32.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +char *nodup(char *, int *); +char *nnodup(char *, int *); +char *dup(char *, int *); + +int main(int argc, char **argv) +{ + int i; + if(argc!=2) + exit(1); + + char *string = *(argv+1); + int dict[26]; + memset(dict, 0, 26*sizeof(int)); + + for(i = 0; i < strlen(string); i++) + string[i] = toupper(string[i]); + + printf("nodup: %s\n", nodup(string, dict)); + printf("nnodup: %s\n", nnodup(string, dict)); + printf("dup: %s\n", dup(string, dict)); + + int ii; + + for(i=0; i<26; i++){ + putchar('A'+i); + for(ii=0; ii1) + output[outpt++]=input[i]; + output[outpt] = '\0'; + int ndict[26]; + memset(ndict, 0, 26*sizeof(int)); + return nodup(output, ndict); +} diff --git a/strip.c b/strip.c new file mode 100644 index 0000000..32b0685 --- /dev/null +++ b/strip.c @@ -0,0 +1,9 @@ +#include + +int main(int argc, char **argv) +{ + int c; + while((c = getchar()) != EOF) + if(isdigit(c) || c == '\n') + putchar(c); +} diff --git a/timer.c b/timer.c index 5ab51c6..153bcb8 100644 --- a/timer.c +++ b/timer.c @@ -7,52 +7,61 @@ unsigned long long int get_seconds(char *); void usage(char *cmd) { - printf("Usage:\n" - " %s {s,m,h,D,M,Y} ...\n", cmd); + printf("Usage:\n" + " %s {s,m,h,D,M,Y} ...\n", cmd); } int main(int argc, char **argv) { - if (argc < 2) { - printf("Not enough arguments.\n"); - usage(argv[0]); - exit(1); - } + if (argc < 2) { + printf("Not enough arguments.\n"); + usage(argv[0]); + exit(1); + } - unsigned long long int total_seconds = 0; + unsigned long long int total_seconds = 0; - int i; - for (i = 1; i < argc; i++) - total_seconds += get_seconds(argv[i]); + int i; + for (i = 1; i < argc; i++) + total_seconds += get_seconds(argv[i]); - printf("Total time: %llu second(s).\n", total_seconds); + printf("Total time: %llu second(s).\nStarted at: %d\n", total_seconds, time(NULL)); - return 0; + sleep(total_seconds); + + printf("Ended at: %d\n", time(NULL)); + + while (1) { + printf("Ring!\a\n"); + sleep(1); + } + + return 0; } unsigned long long int get_seconds(char *code) { - int length = strlen(code); - if (length < 2) { - return 0; - } - - int multiplier = 0; - char suffix = code[length - 1]; - switch (suffix) { - case 's': multiplier = 1; break; // 1 second - case 'm': multiplier = 60; break; // 1 minute - case 'h': multiplier = 3600; break; // 1 hour - case 'D': multiplier = 86400; break; // 1 day - case 'M': multiplier = 2419200; break; // 28 days - case 'Y': multiplier = 31536000; break; // 365 days - default : return 0; - } - - char value[length + 1]; - strcpy(value, code); - - value[length - 1] = '\0'; - - return strtoull(value, NULL, 10) * multiplier; + int length = strlen(code); + if (length < 2) { + return 0; + } + + int multiplier = 0; + char suffix = code[length - 1]; + switch (suffix) { + case 's': multiplier = 1; break; // 1 second + case 'm': multiplier = 60; break; // 1 minute + case 'h': multiplier = 3600; break; // 1 hour + case 'D': multiplier = 86400; break; // 1 day + case 'M': multiplier = 2629746; break; // 30.4368 days + case 'Y': multiplier = 31556940; break; // 365.242 days + default : return 0; + } + + char value[length + 1]; + strcpy(value, code); + + value[length - 1] = '\0'; + + return strtoull(value, NULL, 10) * multiplier; } diff --git a/xcb.c b/xcb.c index 82f3a83..db8d7ac 100644 --- a/xcb.c +++ b/xcb.c @@ -1,4 +1,5 @@ -#include +#include +#include #include int main(int argc, char **argv) @@ -17,7 +18,7 @@ int main(int argc, char **argv) {10, 10} }; - xcb_segment_t segments[] { + xcb_segment_t segments[] = { {100, 10, 140, 30}, {110, 25, 130, 60} }; @@ -34,27 +35,23 @@ int main(int argc, char **argv) xcb_connection_t *connection = xcb_connect(NULL, NULL); - const xcb_setup_t *setup = xcb_get_setup(connection); - xcb_screen_t *screen = xcb_setup_roots_iterator(setup).data; + xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; xcb_drawable_t window = screen->root; xcb_gcontext_t foreground = xcb_generate_id(connection); - uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; - uint32_t values[2] = { - screen->black_pixel, - 0 - }; + uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; + uint32_t values[2] = { screen->black_pixel, 0}; xcb_create_gc(connection, foreground, window, mask, values); - mask = XCB_CW_BLACK_PIXEL | XCB_CW_EVENT_MASK; - values = { - screen->white_pixel, + window = xcb_generate_id(connection); + mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + values[0] = screen->white_pixel; + values[1] = XCB_EVENT_MASK_EXPOSURE; - xcb_window_t window = xcb_generate_id(connection); xcb_create_window(connection, - XCB_COPY_FROM_PARENT, + screen->root_depth, window, screen->root, 0, 0, @@ -62,13 +59,52 @@ int main(int argc, char **argv) 10, XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, - 0, NULL); + mask, values); xcb_map_window(connection, window); - xcb_flush(connection); - pause(); + printf("Window mapped.\n"); + + xcb_generic_event_t *event; + while ((event = xcb_wait_for_event(connection))) { + printf("We're waiting.\n"); + switch (event->response_type & ~0x80) { + case XCB_EXPOSE: + printf("Expose event.\n"); + xcb_poly_point(connection, + XCB_COORD_MODE_ORIGIN, + window, + foreground, + 4, points); + xcb_poly_line(connection, + XCB_COORD_MODE_PREVIOUS, + window, + foreground, + 4, polyline); + xcb_poly_segment(connection, + window, + foreground, + 2, segments); + xcb_poly_rectangle(connection, + window, + foreground, + 2, rectangles); + xcb_poly_arc(connection, + window, + foreground, + 2, arcs); + xcb_flush(connection); + + break; + default: + break; + } + + free(event); + } + + printf("We did derp.\n"); xcb_disconnect(connection); diff --git a/xcb_colormaps.c b/xcb_colormaps.c new file mode 100644 index 0000000..4a8d560 --- /dev/null +++ b/xcb_colormaps.c @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include +#include + +#define WIDTH 640 +#define HEIGHT 480 + +void perlin(uint8_t * const, uint16_t, uint16_t); + +int main(int arg, char **argv) +{ + srand(time(NULL)); + xcb_connection_t *connection = xcb_connect(NULL, NULL); + + xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; + + xcb_colormap_t colormap = screen->default_colormap; + + xcb_drawable_t window = xcb_generate_id(connection); + uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + uint32_t values[] = {screen->black_pixel, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS}; + xcb_create_window(connection, + /*screen->root_depth,*/ + 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); + + uint8_t *limg = img; + /*for (int y = 0; y < HEIGHT; y++)*/ + /*for (int x = 0; x < WIDTH; x++) {*/ + /**(limg++) = 128;*/ + /**(limg++) = 128;*/ + /**(limg++) = 128;*/ + /*limg++;*/ + /*}*/ + perlin(img, WIDTH, HEIGHT); + + xcb_image_t *image = xcb_image_create(WIDTH, HEIGHT, + XCB_IMAGE_FORMAT_Z_PIXMAP, + 8, 24, 32, + 0, + /*xcb_get_setup(connection)->image_byte_order,*/ + XCB_IMAGE_ORDER_MSB_FIRST, + XCB_IMAGE_ORDER_LSB_FIRST, + img, + WIDTH * HEIGHT * 4, + img); + + xcb_gcontext_t gc = xcb_generate_id(connection); + xcb_create_gc(connection, + gc, + pixmap, + 0, NULL); + + xcb_image_put(connection, pixmap, gc, image, 0, 0, 0); + + xif_write(image, "test.xif"); + + xcb_map_window(connection, window); + xcb_flush(connection); + + xcb_generic_event_t *event; + while ((event = xcb_wait_for_event(connection))) { + switch (event->response_type & ~0x80) { + case XCB_EXPOSE: ; + xcb_expose_event_t *expose = (xcb_expose_event_t *)event; + xcb_copy_area(connection, + pixmap, + window, + gc, + expose->x, expose->y, + expose->x, expose->y, + expose->width, expose->height); + xcb_flush(connection); + break; + case XCB_BUTTON_PRESS: + goto end; + break; + default: + break; + } + free(event); + } +end: + + xcb_free_pixmap(connection, pixmap); + xcb_disconnect(connection); + + xcb_image_destroy(image); + + return 0; +} + +int8_t randrange(int8_t from, int8_t to) +{ + int base_random = rand(); + if (RAND_MAX == base_random) return randrange(from, to); + int range = to - from, + remainder = RAND_MAX % range, + bucket = RAND_MAX / range; + if (base_random < RAND_MAX - remainder) { + return from + base_random/bucket; + } else { + return randrange(from, to); + } +} + +#define lerp(t, a, b) (a + t * (b - a)) + +void perlin(uint8_t * const img, uint16_t width, uint16_t height) +{ + uint8_t *limg = img; + for (int y = 0; y < HEIGHT; y++) + for (int x = 0; x < WIDTH; x++) { + *(limg++) = 128; + *(limg++) = 128; + *(limg++) = 128; + limg++; + } + + int8_t *mipmap = malloc(sizeof(int8_t) * (2 * 2 + 4 * 4 + 8 * 8 + 16 * 16) * 4); + + int8_t *lmipmap = mipmap; + for (int s = 2; s <= 16; s*=2) + for (int x = 0; x < s * s; x++) { + *(lmipmap++) = randrange(-128 / s, 128 / s); + *(lmipmap++) = randrange(-128 / s, 128 / s); + *(lmipmap++) = randrange(-128 / s, 128 / s); + lmipmap++; + printf("S: %d X: %d Y: %d R: %d G: %d B: %d\n", s, x / s, x, *(lmipmap-4), *(lmipmap-3), *(lmipmap-2)); + }; + + limg = img; + lmipmap = mipmap; + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + for (int bit = 0; bit < 2; bit ++) { + for (int s = 2; s <= 16; s*=2) { + size_t offset = 0; + for (int i = s / 2; i >= 2; i/=2) + offset += i * i; + lmipmap = mipmap + offset; + uint16_t xblock = width / (s - 1); + uint16_t yblock = height / (s - 1); + + uint8_t xbpos = (float)x / (float)xblock; + uint8_t ybpos = (float)y / (float)yblock; + + float xmul = (float)x / (float)width - (float)xbpos; + float ymul = (float)y / (float)height - (float)ybpos; + + int8_t valtl, valtr, valbl, valbr; + valtl = lmipmap[bit + 4 * (ybpos * s + xbpos)]; + valtr = lmipmap[bit + 4 * (ybpos * s + xbpos + 1)]; + valbl = lmipmap[bit + 4 * ((ybpos + 1) * s + xbpos)]; + valbr = lmipmap[bit + 4 * ((ybpos + 1) * s + xbpos + 1)]; + + int8_t wlerpu = lerp(xmul, valtl, valtr); + int8_t wlerpl = lerp(xmul, valbl, valbr); + + int8_t endval = lerp(ymul, wlerpl, wlerpu); + + limg[bit + 4 * (y * width + x)] = limg[bit + 4 * (y * width + x)] + endval; + } + } + free(mipmap); +} diff --git a/xcb_fixed.c b/xcb_fixed.c new file mode 100644 index 0000000..b230b78 --- /dev/null +++ b/xcb_fixed.c @@ -0,0 +1,211 @@ +/* XCB application drawing an updating bitmap in a window +* +* Inspired by the xcb black rectangle in a window example +* +* Copyright 2010 V. R. Sanders, released under the MIT licence +*/ + +/* compile with: +* gcc -Wall -lxcb-icccm -lxcb -lxcb-image -o disp disp.c +*/ + +#include + +#include +#include +#include +#include +#include +#include +#include + +static xcb_format_t * +find_format (xcb_connection_t * c, uint8_t depth, uint8_t bpp) +{ + const xcb_setup_t *setup = xcb_get_setup(c); + xcb_format_t *fmt = xcb_setup_pixmap_formats(setup); + xcb_format_t *fmtend = fmt + xcb_setup_pixmap_formats_length(setup); + for(; fmt != fmtend; ++fmt) + if((fmt->depth == depth) && (fmt->bits_per_pixel == bpp)) { + /* printf("fmt %p has pad %d depth %d, bpp %d\n", + fmt,fmt->scanline_pad, depth,bpp); */ + return fmt; + } + return 0; +} + +void +fillimage(unsigned char *p, int width, int height) +{ + int i, j; + for(j=0; j < height; j++) + { + for(i=0; i < width; i++) + { + if((i < 256)&&(j < 256)) + { + *p++=rand()%256; // blue + *p++=rand()%256; // green + *p++=rand()%256; // red + } else { + *p++=i%256; // blue + *p++=j%256; // green + if(i < 256) + *p++=i%256; // red + else if(j < 256) + *p++=j%256; // red + else + *p++=(256-j)%256; // red + } + p++; /* unused byte */ + } + } +} + +xcb_image_t * +CreateTrueColorImage(xcb_connection_t *c, + int width, + int height) +{ + const xcb_setup_t *setup = xcb_get_setup(c); + unsigned char *image32=(unsigned char *)malloc(width*height*4); + xcb_format_t *fmt = find_format(c, 24, 32); + if (fmt == NULL) + return NULL; + + fillimage(image32, width, height); + + return xcb_image_create(width, + height, + XCB_IMAGE_FORMAT_Z_PIXMAP, + fmt->scanline_pad, + fmt->depth, + fmt->bits_per_pixel, + 0, + setup->image_byte_order, + XCB_IMAGE_ORDER_LSB_FIRST, + image32, + width*height*4, + image32); +} + +int +main (int argc, char **argv) +{ + xcb_connection_t *c; + xcb_screen_t *s; + xcb_window_t w; + xcb_pixmap_t pmap; + xcb_gcontext_t gc; + xcb_generic_event_t *e; + uint32_t mask; + uint32_t values[2]; + int done=0; + xcb_image_t *image; + uint8_t *image32; + xcb_expose_event_t *ee; + char *title="Hello World!"; + xcb_size_hints_t *hints; + + /* open connection with the server */ + c = xcb_connect (NULL, NULL); + + if (!c) { + printf ("Cannot open display\n"); + exit (1); + } + + s = xcb_setup_roots_iterator (xcb_get_setup (c)).data; + + /* printf("root depth %d\n",s->root_depth); */ + + /* create image */ + image = CreateTrueColorImage(c, 640, 480); + if (image == NULL) { + printf ("Cannot create iamge\n"); + xcb_disconnect(c); + return 1; + } + image32 = image->data; + + /* create window */ + mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + values[0] = s->white_pixel; + values[1] = XCB_EVENT_MASK_EXPOSURE | + XCB_EVENT_MASK_KEY_PRESS | + XCB_EVENT_MASK_BUTTON_PRESS; + + w = xcb_generate_id (c); + xcb_create_window (c, XCB_COPY_FROM_PARENT, w, s->root, + 10, 10, image->width, image->height, 1, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + s->root_visual, + mask, values); + + /*[> set size hits on window <]*/ + /*hints = xcb_alloc_size_hints();*/ + /*xcb_size_hints_set_max_size(hints, image->width,image->height);*/ + /*xcb_size_hints_set_min_size(hints, image->width,image->height);*/ + /*xcb_set_wm_size_hints(c, w, WM_NORMAL_HINTS, hints);*/ + + /* create backing pixmap */ + pmap = xcb_generate_id(c); + xcb_create_pixmap(c, 24, pmap, w, image->width, image->height); + + /* create pixmap plot gc */ + mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND; + values[0] = s->black_pixel; + values[1] = 0xffffff; + + gc = xcb_generate_id (c); + xcb_create_gc (c, gc, pmap, mask, values); + + /* put the image into the pixmap */ + xcb_image_put(c, pmap, gc, image, 0, 0, 0); + + /* show the window */ + xcb_map_window (c, w); + xcb_flush (c); + + /* event loop */ + while (!done && (e = xcb_wait_for_event (c))) { + switch (e->response_type) { + case XCB_EXPOSE: + ee=(xcb_expose_event_t *)e; + /* printf ("expose %d,%d - %d,%d\n", + ee->x,ee->y,ee->width,ee->height); */ + xcb_copy_area(c, pmap, w, gc, + ee->x, + ee->y, + ee->x, + ee->y, + ee->width, + ee->height); + xcb_flush (c); + image32+=16; + break; + + /*case XCB_KEY_PRESS:*/ + /*[> exit on keypress <]*/ + /*done = 1;*/ + /*break;*/ + + case XCB_BUTTON_PRESS: + fillimage(image->data, image->width, image->height); + memset(image->data, 0, image32 - image->data); + xcb_image_put(c, pmap, gc, image, 0, 0, 0); + xcb_copy_area(c, pmap, w, gc, 0,0,0,0,image->width,image->height); + xcb_flush (c); + break; + } + free (e); + } + + /* free pixmap */ + xcb_free_pixmap(c, pmap); + + /* close connection to server */ + xcb_disconnect (c); + + return 0; +} diff --git a/xcb_imagereading.c b/xcb_imagereading.c new file mode 100644 index 0000000..ca06054 --- /dev/null +++ b/xcb_imagereading.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include + +#define WIDTH 640 +#define HEIGHT 480 + +int main(int arg, char **argv) +{ + xcb_connection_t *connection = xcb_connect(NULL, NULL); + + xcb_screen_t *screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; + + xcb_colormap_t colormap = screen->default_colormap; + + xcb_drawable_t window = xcb_generate_id(connection); + uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + uint32_t values[] = {screen->black_pixel, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS}; + xcb_create_window(connection, + /*screen->root_depth,*/ + 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); + xcb_image_t *image; + if (!(image = xif_read("test.xif"))) { + fputs("Error: xif_read returned null.\n", stderr); + exit(1); + } + + xcb_gcontext_t gc = xcb_generate_id(connection); + xcb_create_gc(connection, + gc, + pixmap, + 0, NULL); + + xcb_image_put(connection, pixmap, gc, image, 0, 0, 0); + + xcb_map_window(connection, window); + xcb_flush(connection); + + xcb_generic_event_t *event; + while ((event = xcb_wait_for_event(connection))) { + switch (event->response_type & ~0x80) { + case XCB_EXPOSE: ; + xcb_expose_event_t *expose = (xcb_expose_event_t *)event; + xcb_copy_area(connection, + pixmap, + window, + gc, + expose->x, expose->y, + expose->x, expose->y, + expose->width, expose->height); + xcb_flush(connection); + break; + case XCB_BUTTON_PRESS: + goto end; + break; + default: + break; + } + free(event); + } +end: + + xcb_free_pixmap(connection, pixmap); + xcb_disconnect(connection); + + xcb_image_destroy(image); + + return 0; +} -- cgit v1.2.3-54-g00ecf