From 65036b0e441efa04b07a4d82852c4f05ee98af23 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Fri, 9 Apr 2010 13:15:55 +0200 Subject: Reorder and clean up daemon.c --- daemon.c | 439 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 224 insertions(+), 215 deletions(-) diff --git a/daemon.c b/daemon.c index b137cbb..9751fd3 100644 --- a/daemon.c +++ b/daemon.c @@ -13,210 +13,9 @@ static GOptionEntry cmd_entries[] = { {NULL} }; -static const char base64_alphabet[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -static void -base64_uint8 (guint8 v, char *c) -{ - c[0] = base64_alphabet[(v >> 0) & 0x3f]; - c[1] = base64_alphabet[(v >> 6) & 0x3]; -} - -static void -base64_uint16 (guint32 v, char *c) -{ - c[0] = base64_alphabet[(v >> 0) & 0x3f]; - c[1] = base64_alphabet[(v >> 6) & 0x3f]; - c[2] = base64_alphabet[(v >> 12) & 0xf]; -} - -static void -base64_uint24 (guint32 v, char *c) -{ - c[0] = base64_alphabet[(v >> 0) & 0x3f]; - c[1] = base64_alphabet[(v >> 6) & 0x3f]; - c[2] = base64_alphabet[(v >> 12) & 0x3f]; - c[3] = base64_alphabet[(v >> 18) & 0x3f]; -} - -static void -base64_uint32 (guint32 v, char *c) -{ - c[0] = base64_alphabet[(v >> 0) & 0x3f]; - c[1] = base64_alphabet[(v >> 6) & 0x3f]; - c[2] = base64_alphabet[(v >> 12) & 0x3f]; - c[3] = base64_alphabet[(v >> 18) & 0x3f]; - c[4] = base64_alphabet[(v >> 24) & 0x3f]; - c[5] = base64_alphabet[(v >> 30) & 0x2]; -} - -static void -send_rgb (GOutputStream *out, guint32 rgb) -{ - char buf[5]; - - buf[0] = 'c'; - base64_uint24(rgb, &buf[1]); - - g_output_stream_write_all (out, buf, 5, - NULL, NULL, NULL); -} - -static void -send_rgba (GOutputStream *out, guint32 argb) -{ - char buf[7]; - - buf[0] = 'C'; - base64_uint32(argb, &buf[1]); - - g_output_stream_write_all (out, buf, 7, - NULL, NULL, NULL); -} - -static void -send_rect (GOutputStream *out, int x, int y, int w, int h) -{ - char buf[13]; - - buf[0] = 'r'; - base64_uint16(x, &buf[1]); - base64_uint16(y, &buf[4]); - base64_uint16(w, &buf[7]); - base64_uint16(h, &buf[10]); - - g_output_stream_write_all (out, buf, 13, - NULL, NULL, NULL); -} - -static void -send_copyrect (GOutputStream *out, int sx, int sy, int w, int h, int dx, int dy) -{ - char buf[19]; - - buf[0] = 'b'; - base64_uint16(sx, &buf[1]); - base64_uint16(sy, &buf[4]); - base64_uint16(w, &buf[7]); - base64_uint16(h, &buf[10]); - base64_uint16(dx, &buf[13]); - base64_uint16(dy, &buf[16]); - - g_output_stream_write_all (out, buf, 19, - NULL, NULL, NULL); -} - -static void -send_blend (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy) -{ - char buf[22]; - - buf[0] = 'e'; - base64_uint16(source_surface, &buf[1]); - base64_uint16(sx, &buf[4]); - base64_uint16(sy, &buf[7]); - base64_uint16(w, &buf[10]); - base64_uint16(h, &buf[13]); - base64_uint16(dx, &buf[16]); - base64_uint16(dy, &buf[19]); - - g_output_stream_write_all (out, buf, 22, - NULL, NULL, NULL); -} - -static void -send_blend_alpha (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy, guint8 overall_alpha) -{ - char buf[24]; - - buf[0] = 'E'; - base64_uint16(source_surface, &buf[1]); - base64_uint16(sx, &buf[4]); - base64_uint16(sy, &buf[7]); - base64_uint16(w, &buf[10]); - base64_uint16(h, &buf[13]); - base64_uint16(dx, &buf[16]); - base64_uint16(dy, &buf[19]); - base64_uint8(dy, &buf[22]); - - g_output_stream_write_all (out, buf, 24, - NULL, NULL, NULL); -} - -static void -send_mask (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy) -{ - char buf[22]; - - buf[0] = 'm'; - base64_uint16(source_surface, &buf[1]); - base64_uint16(sx, &buf[4]); - base64_uint16(sy, &buf[7]); - base64_uint16(w, &buf[10]); - base64_uint16(h, &buf[13]); - base64_uint16(dx, &buf[16]); - base64_uint16(dy, &buf[19]); - - g_output_stream_write_all (out, buf, 22, - NULL, NULL, NULL); -} - -static void -send_blit (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy) -{ - char buf[22]; - - buf[0] = 'B'; - base64_uint16(source_surface, &buf[1]); - base64_uint16(sx, &buf[4]); - base64_uint16(sy, &buf[7]); - base64_uint16(w, &buf[10]); - base64_uint16(h, &buf[13]); - base64_uint16(dx, &buf[16]); - base64_uint16(dy, &buf[19]); - - g_output_stream_write_all (out, buf, 22, - NULL, NULL, NULL); -} - -static void -send_new_surface(GOutputStream *out, int id, int w, int h) -{ - char buf[10]; - - buf[0] = 's'; - base64_uint16(id, &buf[1]); - base64_uint16(w, &buf[4]); - base64_uint16(h, &buf[7]); - - g_output_stream_write_all (out, buf, 10, - NULL, NULL, NULL); -} - -static void -send_select_surface(GOutputStream *out, int id) -{ - char buf[4]; - - buf[0] = 'a'; - base64_uint16(id, &buf[1]); - - g_output_stream_write_all (out, buf, 4, - NULL, NULL, NULL); -} - -static void -send_destroy_surface(GOutputStream *out, int id) -{ - char buf[4]; - - buf[0] = 'd'; - base64_uint16(id, &buf[1]); - - g_output_stream_write_all (out, buf, 4, - NULL, NULL, NULL); -} +/************************************************************************ + * conversion of raw image data to uncompressed png data: uris * + ************************************************************************/ /* Table of CRCs of all 8-bit messages. */ static unsigned long crc_table[256]; @@ -569,11 +368,7 @@ to_png_a (int w, int h, int stride, guint8 *data) data += stride; for (x = 0; x < w; x++) { pixel = *row++; - *p++ = 0xff; /* white */ -#if 0 - *p++ = 0xff; /* white */ - *p++ = 0xff; /* white */ -#endif + *p++ = 0x00; /* gray */ *p++ = pixel; /* alpha */ } adler = update_adler32(adler, p_row, p - p_row); @@ -604,6 +399,215 @@ to_png_a (int w, int h, int stride, guint8 *data) return url; } +/************************************************************************ + * Core rendering operations * + ************************************************************************/ + +static const char base64_alphabet[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +static void +base64_uint8 (guint8 v, char *c) +{ + c[0] = base64_alphabet[(v >> 0) & 0x3f]; + c[1] = base64_alphabet[(v >> 6) & 0x3]; +} + +static void +base64_uint16 (guint32 v, char *c) +{ + c[0] = base64_alphabet[(v >> 0) & 0x3f]; + c[1] = base64_alphabet[(v >> 6) & 0x3f]; + c[2] = base64_alphabet[(v >> 12) & 0xf]; +} + +static void +base64_uint24 (guint32 v, char *c) +{ + c[0] = base64_alphabet[(v >> 0) & 0x3f]; + c[1] = base64_alphabet[(v >> 6) & 0x3f]; + c[2] = base64_alphabet[(v >> 12) & 0x3f]; + c[3] = base64_alphabet[(v >> 18) & 0x3f]; +} + +static void +base64_uint32 (guint32 v, char *c) +{ + c[0] = base64_alphabet[(v >> 0) & 0x3f]; + c[1] = base64_alphabet[(v >> 6) & 0x3f]; + c[2] = base64_alphabet[(v >> 12) & 0x3f]; + c[3] = base64_alphabet[(v >> 18) & 0x3f]; + c[4] = base64_alphabet[(v >> 24) & 0x3f]; + c[5] = base64_alphabet[(v >> 30) & 0x2]; +} + +static void +send_rgb (GOutputStream *out, guint32 rgb) +{ + char buf[5]; + + buf[0] = 'c'; + base64_uint24(rgb, &buf[1]); + + g_output_stream_write_all (out, buf, 5, + NULL, NULL, NULL); +} + +static void +send_rgba (GOutputStream *out, guint32 argb) +{ + char buf[7]; + + buf[0] = 'C'; + base64_uint32(argb, &buf[1]); + + g_output_stream_write_all (out, buf, 7, + NULL, NULL, NULL); +} + +static void +send_rect (GOutputStream *out, int x, int y, int w, int h) +{ + char buf[13]; + + buf[0] = 'r'; + base64_uint16(x, &buf[1]); + base64_uint16(y, &buf[4]); + base64_uint16(w, &buf[7]); + base64_uint16(h, &buf[10]); + + g_output_stream_write_all (out, buf, 13, + NULL, NULL, NULL); +} + +static void +send_copyrect (GOutputStream *out, int sx, int sy, int w, int h, int dx, int dy) +{ + char buf[19]; + + buf[0] = 'b'; + base64_uint16(sx, &buf[1]); + base64_uint16(sy, &buf[4]); + base64_uint16(w, &buf[7]); + base64_uint16(h, &buf[10]); + base64_uint16(dx, &buf[13]); + base64_uint16(dy, &buf[16]); + + g_output_stream_write_all (out, buf, 19, + NULL, NULL, NULL); +} + +static void +send_blend (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy) +{ + char buf[22]; + + buf[0] = 'e'; + base64_uint16(source_surface, &buf[1]); + base64_uint16(sx, &buf[4]); + base64_uint16(sy, &buf[7]); + base64_uint16(w, &buf[10]); + base64_uint16(h, &buf[13]); + base64_uint16(dx, &buf[16]); + base64_uint16(dy, &buf[19]); + + g_output_stream_write_all (out, buf, 22, + NULL, NULL, NULL); +} + +static void +send_blend_alpha (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy, guint8 overall_alpha) +{ + char buf[24]; + + buf[0] = 'E'; + base64_uint16(source_surface, &buf[1]); + base64_uint16(sx, &buf[4]); + base64_uint16(sy, &buf[7]); + base64_uint16(w, &buf[10]); + base64_uint16(h, &buf[13]); + base64_uint16(dx, &buf[16]); + base64_uint16(dy, &buf[19]); + base64_uint8(dy, &buf[22]); + + g_output_stream_write_all (out, buf, 24, + NULL, NULL, NULL); +} + +static void +send_mask (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy) +{ + char buf[22]; + + buf[0] = 'm'; + base64_uint16(source_surface, &buf[1]); + base64_uint16(sx, &buf[4]); + base64_uint16(sy, &buf[7]); + base64_uint16(w, &buf[10]); + base64_uint16(h, &buf[13]); + base64_uint16(dx, &buf[16]); + base64_uint16(dy, &buf[19]); + + g_output_stream_write_all (out, buf, 22, + NULL, NULL, NULL); +} + +static void +send_blit (GOutputStream *out, int source_surface, int sx, int sy, int w, int h, int dx, int dy) +{ + char buf[22]; + + buf[0] = 'B'; + base64_uint16(source_surface, &buf[1]); + base64_uint16(sx, &buf[4]); + base64_uint16(sy, &buf[7]); + base64_uint16(w, &buf[10]); + base64_uint16(h, &buf[13]); + base64_uint16(dx, &buf[16]); + base64_uint16(dy, &buf[19]); + + g_output_stream_write_all (out, buf, 22, + NULL, NULL, NULL); +} + +static void +send_new_surface(GOutputStream *out, int id, int w, int h) +{ + char buf[10]; + + buf[0] = 's'; + base64_uint16(id, &buf[1]); + base64_uint16(w, &buf[4]); + base64_uint16(h, &buf[7]); + + g_output_stream_write_all (out, buf, 10, + NULL, NULL, NULL); +} + +static void +send_select_surface(GOutputStream *out, int id) +{ + char buf[4]; + + buf[0] = 'a'; + base64_uint16(id, &buf[1]); + + g_output_stream_write_all (out, buf, 4, + NULL, NULL, NULL); +} + +static void +send_destroy_surface(GOutputStream *out, int id) +{ + char buf[4]; + + buf[0] = 'd'; + base64_uint16(id, &buf[1]); + + g_output_stream_write_all (out, buf, 4, + NULL, NULL, NULL); +} + static void send_image_rgb (GOutputStream *out, int x, int y, int w, int h, int stride, guint32 *data) @@ -700,6 +704,11 @@ flush (GOutputStream *out) g_output_stream_flush (out, NULL, NULL); } +/************************************************************************ + * Demo * + ************************************************************************/ + + static int font_w = 286; static int font_cell_w = 11; static int font_h = 18; @@ -775,6 +784,10 @@ send_draw_ops (GOutputStream *out) return TRUE; } +/************************************************************************ + * lame http server * + ************************************************************************/ + static void send_error (GOutputStream *out, int error_code, @@ -888,13 +901,9 @@ handler (GThreadedSocketService *service, if (strcmp(unescaped, "/client.html") == 0) res = handle_read_file("client.html", out); else if (strcmp(unescaped, "/ops") == 0) - { - res = send_draw_ops(out); - } + res = send_draw_ops(out); else - { - send_error (out, 404, "No such file"); - } + send_error (out, 404, "No such file"); g_free (unescaped); g_free (line); -- cgit v1.2.3