summaryrefslogtreecommitdiffstats
path: root/src/web.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/web.c')
-rw-r--r--src/web.c74
1 files changed, 38 insertions, 36 deletions
diff --git a/src/web.c b/src/web.c
index f3aa5f7..afd8c57 100644
--- a/src/web.c
+++ b/src/web.c
@@ -58,6 +58,38 @@ char *read_file_to_string(const char *filename) {
return buffer;
}
+char *get_from_template() {
+ int size = 1024^3 * 1000;
+ char * ret = (char *)malloc(sizeof(char) * size);
+
+ int pipefd[2];
+ if (pipe(pipefd) == -1) {
+ perror("pipe");
+ return NULL;
+ }
+
+ synth_t * synth = synthx;
+ #define OUT pipefd[1]
+ #define INT(x) dprintf(OUT, "%d", x);
+ #define FLOAT(x) dprintf(OUT, "%f", x);
+ #define STR(x) dprintf(OUT, "%s", x);
+ #define PERCENT dprintf(OUT, "%s", "%");
+ #include "index.html.h"
+ close(pipefd[1]);
+
+ // Read from the pipe into a buffer
+ ssize_t bytes_read = read(pipefd[0], ret, size - 1);
+ if (bytes_read == -1) {
+ perror("read");
+ return NULL;
+ }
+
+ // Null-terminate the ret
+ ret[bytes_read] = '\0';
+ close(pipefd[0]);
+ return ret;
+}
+
int
key_to_number(char key) {
switch (key) {
@@ -164,7 +196,7 @@ static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
break;
}
case LWS_CALLBACK_SERVER_WRITEABLE: {
- printf("\nLWS_CALLBACK_SERVER_WRITEABLE\n\n");
+ printf("LWS_CALLBACK_SERVER_WRITEABLE\n");
/* size_t msg_len = strlen(message_buffer); */
/* unsigned char buffer[LWS_PRE + BUFFER_SIZE]; */
/* memcpy(&buffer[LWS_PRE], message_buffer, msg_len); */
@@ -172,10 +204,12 @@ static int callback_ws(struct lws *wsi, enum lws_callback_reasons reason,
break;
}
case LWS_CALLBACK_HTTP: {
+ html_content = get_from_template();
snprintf(tmp, sizeof(tmp), html_header, strlen(html_content));
strcpy(buf, tmp);
strcat(buf, html_content);
lws_write(wsi, (unsigned char *)buf, strlen(buf), LWS_WRITE_HTTP);
+ free(html_content);
break;
}
case LWS_CALLBACK_CLOSED: {
@@ -221,40 +255,10 @@ void *websocket_server_thread(void *arg) {
return NULL;
}
-char *get_from_template() {
- int size = 1024^3 * 1000;
- char * ret = (char *)malloc(sizeof(char) * size);
-
- int pipefd[2];
- if (pipe(pipefd) == -1) {
- perror("pipe");
- return NULL;
- }
-
- #define OUT pipefd[1]
- #define INT(x) dprintf(OUT, "%d", x);
- #define PERCENT dprintf(OUT, "%s", "%");
- #include "index.html.h"
- close(pipefd[1]);
-
- // Read from the pipe into a buffer
- ssize_t bytes_read = read(pipefd[0], ret, size - 1);
- if (bytes_read == -1) {
- perror("read");
- return NULL;
- }
-
- // Null-terminate the ret
- ret[bytes_read] = '\0';
- close(pipefd[0]);
- return ret;
-}
-
void
init_web(synth_t * synth)
{
//html_content = read_file_to_string("src/index.html");
- html_content = get_from_template();
synthx = synth;
lws_set_log_level(LLL_WARN, NULL);
@@ -274,14 +278,12 @@ free_web()
void ws_send_message(const char *message) {
if (client_wsi != NULL) {
- /* strcpy(message_buffer, message); */
- /* lws_callback_on_writable(client_wsi); */
size_t msg_len = strlen(message);
unsigned char buffer[LWS_PRE + BUFFER_SIZE];
memcpy(&buffer[LWS_PRE], message, msg_len);
lws_write(client_wsi, &buffer[LWS_PRE], msg_len, LWS_WRITE_TEXT);
- printf("[WS]: Sent <<%s>>\n", message);
-
- //lws_write(client_wsi, (unsigned char *)message, strlen(message), LWS_WRITE_TEXT);
+ // printf("[WS]: Sent <<%s>>\n", message);
+ // do I need this?
+ //lws_callback_on_writable(client_wsi); ???
}
}