Fix protocol parser and logic. Stuff works now :D
This commit is contained in:
parent
a7ac7da270
commit
a80c3876c7
1 changed files with 47 additions and 16 deletions
|
|
@ -8,12 +8,6 @@
|
||||||
int px_width = 1024;
|
int px_width = 1024;
|
||||||
int px_height = 1024;
|
int px_height = 1024;
|
||||||
|
|
||||||
const char * px_help_text =
|
|
||||||
"\
|
|
||||||
PX x y: Get color at position (x,y)\n\
|
|
||||||
PX x y rrggbb(aa): Draw a pixel (with optional alpha channel)\n\
|
|
||||||
SIZE: Get canvas size";
|
|
||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
|
|
||||||
static int util_str_starts_with(const char* prefix, const char* str) {
|
static int util_str_starts_with(const char* prefix, const char* str) {
|
||||||
|
|
@ -35,39 +29,73 @@ void px_on_read(NetClient *client, char *line) {
|
||||||
const char * ptr = line + 3;
|
const char * ptr = line + 3;
|
||||||
char * endptr = (char*) ptr;
|
char * endptr = (char*) ptr;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
unsigned int x = strtoul(ptr, &endptr, 10);
|
unsigned int x = strtoul(ptr, &endptr, 10);
|
||||||
if (endptr == ptr || errno) {
|
if (endptr == ptr || errno) {
|
||||||
net_err(client,
|
net_err(client,
|
||||||
"First parameter missing or invalid (should be decimal)");
|
"First parameter missing or invalid (should be decimal)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int y = strtoul((ptr = endptr), &endptr, 10);
|
unsigned int y = strtoul((ptr = endptr), &endptr, 10);
|
||||||
if (endptr == ptr || errno) {
|
if (endptr == ptr || errno) {
|
||||||
net_err(client,
|
net_err(client,
|
||||||
"Second parameter missing or invalid (should be decimal)");
|
"Second parameter missing or invalid (should be decimal)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PX <x> <y> -> Get RGB color at position (x,y) or '0x000000' for out-of-range queries
|
||||||
if (*endptr == 0) {
|
if (*endptr == 0) {
|
||||||
|
uint32_t c;
|
||||||
|
canvas_get_px(x, y, &c);
|
||||||
char str[64];
|
char str[64];
|
||||||
sprintf(str, "PX %u %u %x", x, y, 0xABCDEF);
|
sprintf(str, "PX %u %u %06X", x, y, (c >> 8));
|
||||||
net_send(client, str);
|
net_send(client, str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
unsigned int c = strtoul((ptr = endptr), &endptr, 16);
|
|
||||||
|
// PX <x> <y> BB|RRGGBB|RRGGBBAA
|
||||||
|
unsigned long int c = strtoul((ptr = endptr), &endptr, 16);
|
||||||
if (endptr == ptr || errno) {
|
if (endptr == ptr || errno) {
|
||||||
net_err(client,
|
net_err(client,
|
||||||
"Third parameter missing or invalid (should be hex color)");
|
"Third parameter missing or invalid (should be hex color)");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
printf("%d %d %u=%x\n", x, y, c, c);
|
|
||||||
|
if (endptr - 1 - 6 == ptr) {
|
||||||
|
// RGB -> RGBA
|
||||||
|
c = (c << 8) + 0xff;
|
||||||
|
} else if (endptr - 1 - 8 == ptr) {
|
||||||
|
// done
|
||||||
|
} else if (endptr - 1 - 2 == ptr) {
|
||||||
|
// WW -> RGBA
|
||||||
|
c = (c << 24) + (c << 16) + (c << 8) + 0xff;
|
||||||
|
} else {
|
||||||
|
net_err(client,
|
||||||
|
"Color hex code must be 2, 6 or 8 characters long (WW, RGB or RGBA)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas_set_px(x, y, c);
|
||||||
|
|
||||||
} else if (util_str_starts_with("SIZE", line)) {
|
} else if (util_str_starts_with("SIZE", line)) {
|
||||||
unsigned int w, h;
|
|
||||||
canvas_get_size(&w, &h);
|
|
||||||
char str[64];
|
char str[64];
|
||||||
sprintf(str, "SIZE %d %d", w, h);
|
sprintf(str, "SIZE %d %d", px_width, px_height);
|
||||||
net_send(client, str);
|
net_send(client, str);
|
||||||
|
|
||||||
} else if (util_str_starts_with("HELP", line)) {
|
} else if (util_str_starts_with("HELP", line)) {
|
||||||
net_send(client, px_help_text);
|
|
||||||
|
net_send(client,
|
||||||
|
"\
|
||||||
|
PX x y: Get color at position (x,y)\n\
|
||||||
|
PX x y rrggbb(aa): Draw a pixel (with optional alpha channel)\n\
|
||||||
|
SIZE: Get canvas size");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
net_err(client, "Unknown command");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,18 +105,21 @@ void px_on_close(NetClient *client, int error) {
|
||||||
void px_on_key(int key, int scancode, int mods) {
|
void px_on_key(int key, int scancode, int mods) {
|
||||||
if (key == 300) { // F11
|
if (key == 300) { // F11
|
||||||
int display = canvas_get_display();
|
int display = canvas_get_display();
|
||||||
if(display<0)
|
if (display < 0)
|
||||||
canvas_fullscreen(0);
|
canvas_fullscreen(0);
|
||||||
else
|
else
|
||||||
canvas_fullscreen(-1);
|
canvas_fullscreen(-1);
|
||||||
} else if (key == 301) { // F12
|
} else if (key == 301) { // F12
|
||||||
canvas_fullscreen(canvas_get_display()+1);
|
canvas_fullscreen(canvas_get_display() + 1);
|
||||||
} else if (key == 81 || key == 256) { // q or ESC
|
} else if (key == 81 || key == 256) { // q or ESC
|
||||||
canvas_close();
|
canvas_close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void px_on_resize() {}
|
void px_on_resize() {
|
||||||
|
canvas_get_size(&px_width, &px_height);
|
||||||
|
}
|
||||||
|
|
||||||
void px_on_window_close() {
|
void px_on_window_close() {
|
||||||
printf("Window closed\n");
|
printf("Window closed\n");
|
||||||
net_stop();
|
net_stop();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue