#include #include #include #include #include #include #include #include #include #include "ss-gui/fltk.h" #define r_list_sql \ " select r.name, r.desc, r.text " \ " from recipes as r " \ " " \ " except " \ " " \ " select r.name, r.desc, r.text " \ " from recipes as r, ingredients as n, inventory as i " \ " where n.type_id not in (select type_id from inventory) " \ " and r.name = n.recipe " \ " " \ "order by r.name; " static char* line_buf; static int line_pos, line_len, line_cap; sqlite* db; int osum; SmartShelfUI* main_window = new SmartShelfUI(); void handle_input(int); void display_inventory(void); void display_recipes(void); static int rlist_cb(void* extra, int argc, char** argv, char** columns); inline void cb_display_i(Fl_Button*, void*); void cb_display(Fl_Button*, void*); void init_sigio(void) { /* fcntl(0, F_SETOWN, getpid()); fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_ASYNC | O_NONBLOCK); signal(SIGIO, handle_input); */ fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); } void cleanup(void) { sqlite_close(db); } void handle_packet(char* line_buf, int len) { char* err; char* delim = strchr(line_buf, ' '); assert(delim != NULL); *delim = '\0'; char* args = delim + 1; if (strcmp(line_buf, "TAG-ADD") == 0) { unsigned long long uid; unsigned int type; sscanf(args, "%llx%x", &uid, &type); /* add item to the database */ char buf[256]; snprintf(buf, 256, "insert into inventory " "values(\"%016llx\", \"%08x\");", uid, type); if (sqlite_exec(db, buf, NULL, NULL, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_exec(): %s\n", err); free(err); } display_inventory(); } else if (strcmp(line_buf, "TAG-DEL") == 0) { unsigned long long uid; sscanf(args, "%llx", &uid); char buf[256]; snprintf(buf, 256, "delete from inventory " "where obj_id = \"%016llx\";", uid); if (sqlite_exec(db, buf, NULL, NULL, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_exec(): %s\n", err); free(err); } display_inventory(); } else if (strcmp(line_buf, "PRESSURE") == 0) { int p1, p2, p3, p4, sum; int op1, op2, op3, op4, res; //old values sscanf(args, "%d%d%d%d", &p1, &p2, &p3, &p4); main_window->S1->value(p1); main_window->S2->value(p2); main_window->S3->value(p3); main_window->S4->value(p4); sum = p1 + p2 + p3 + p4; res = sum - osum; main_window->sum->value(0); main_window->sum->value(res); osum = p1 + p2 + p3 + p4; } else { printf("<%s>\n", line_buf); assert(!"Invalid command"); } } void handle_input(int sig) { int ret, i; int k = 0; char* err; if (line_cap - line_len < 32) { /* Grow line_buf. */ line_cap += 32; line_buf = (char*)realloc(line_buf, line_cap); } ret = read(0, line_buf + line_len, line_cap - line_len); if (ret < 0) { if (errno != EAGAIN) { perror("read"); exit(EXIT_FAILURE); } } else if (ret == 0) { exit(EXIT_SUCCESS); } else { line_len += ret; for (i = line_pos; i < line_len; ++i) { if (line_buf[i] == '\n') { handle_packet(line_buf, i); memmove(line_buf, line_buf + i + 1, line_len - i - 1); line_len -= i + 1; ret = line_pos = i = 0; } } line_pos += ret; } } void open_database(void) { /* database filename*/ char* db_name = "smartshelf.db"; char* err; /* Opens database. */ db = sqlite_open(db_name, 0, &err); if (db == NULL) { fprintf(stderr, "sqlite_open(): %s\n", err); free(err); exit(1); } atexit(cleanup); } inline void cb_display_i(Fl_Button* display, void*) { // display->label("Good job"); // main_window->inventory->insert("OK"); display_recipes(); } void cb_display(Fl_Button* display, void* v) { cb_display_i(display,v); } static int dict_cb(void* extra, int argc, char** argv, char** columns) { printf(" %0.8X : %s\n", atoi(argv[0]), argv[1]); return 0; } static int rlist_cb(void* extra, int argc, char** argv, char** columns) { printf("OK"); printf(" %-25s : %s\n", argv[0], argv[1] == NULL ? "" : argv[1]); return 0; } void display_inventory(void) { const char* tail; char* err; sqlite_vm* vm; int num_cols; const char** col_data; const char** col_names; Fl_Text_Buffer* tbuf = main_window->inventory->buffer(); char str[256]; if (sqlite_compile(db, /* " select i.obj_id, d.name, count(*) " " from dictionary as d, inventory as i" " where d.type_id = i.type_id " " group by d.type_id " " order by d.name; ", */ " select d.name, count(*) " " from dictionary as d, inventory as i" " where d.type_id = i.type_id " " group by d.type_id " " order by d.name; ", &tail, &vm, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_compile(): %s\n", err); free(err); return; } /* Clear display. */ tbuf->remove(0, tbuf->length()); while (sqlite_step(vm, &num_cols, &col_data, &col_names) != SQLITE_DONE) { /* snprintf(str, 256, "%s : %s : %s", col_data[0], col_data[1], col_data[2]); */ snprintf(str, 256, "%s : %s\n", col_data[0], col_data[1]); tbuf->append(str); } if (sqlite_finalize(vm, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_compile(): %s\n", err); free(err); } } void display_recipes(void) { const char* tail; char* err; sqlite_vm* vm; int num_cols; const char** col_data; const char** col_names; Fl_Text_Buffer* tbuf = main_window->inventory->buffer(); char str[3000]; char buf[3000]; char ing[3000]; if (sqlite_compile(db, r_list_sql, &tail, &vm, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_compile(): %s\n", err); free(err); return; } tbuf->remove(0, tbuf->length()); while (sqlite_step(vm, &num_cols, &col_data, &col_names) != SQLITE_DONE) { snprintf(str, 3000, "%s : %s\n %s\n\n", col_data[0], col_data[1], col_data[2]); tbuf->append(str); } if (sqlite_finalize(vm, &err) != SQLITE_OK) { fprintf(stderr, "sqlite_compile(): %s\n", err); free(err); } } /* vim: set ts=4 sts=4 sw=4 et: */