legacy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. int machine_video_set_pixel(uint32_t x, uint32_t y, COLOR_TYPE color) {
  2. //sdl_setpixel(x,y,color);
  3. return 1;
  4. }
  5. int machine_video_rect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, COLOR_TYPE color) {
  6. uint32_t y1=y;
  7. uint32_t y2=y+h;
  8. uint32_t x2=x+w;
  9. for (; y1<y2; y1++) {
  10. uint32_t x1=x;
  11. for (; x1<x2; x1++) {
  12. //sdl_setpixel(x1,y1,color);
  13. }
  14. }
  15. }
  16. inline int machine_video_flip() {
  17. //sdl_mainloop();
  18. machine_video_rect(0,0,1920,1080,0xff);
  19. return 1;
  20. }
  21. int machine_get_key(int modifiers) {
  22. //if (modifiers) return sdl_get_modifiers();
  23. //int k = sdl_get_key();
  24. int k = 0;
  25. //if (k) printf("k: %d\n",k);
  26. if (k==43) k=134;
  27. if (k==80) k=130;
  28. if (k==79) k=131;
  29. if (k==82) k=132;
  30. if (k==81) k=133;
  31. return k;
  32. }
  33. Cell* machine_save_file(Cell* cell, char* path) {
  34. printf("about to save: %s\n",path);
  35. if (cell->tag == TAG_STR || cell->tag == TAG_BYTES) {
  36. FILE* f = fopen(path,"wb");
  37. if (!f) return alloc_error(ERR_FORBIDDEN);
  38. fwrite(cell->addr, 1, cell->size, f);
  39. fclose(f);
  40. return alloc_int(1);
  41. } else {
  42. printf("error: cannot save tag %d\n",cell->tag);
  43. }
  44. return alloc_int(0);
  45. }
  46. static char sysfs_tmp[1024];
  47. Cell* machine_load_file(char* path) {
  48. // sysfs
  49. if (!strcmp(path,"/sys/mem")) {
  50. MemStats* mst = alloc_stats();
  51. sprintf(sysfs_tmp, "(%d %d %d %d)", mst->byte_heap_used, mst->byte_heap_max, mst->cells_used, mst->cells_max);
  52. return read_string(sysfs_tmp);
  53. }
  54. char buf[512];
  55. sprintf(buf,"../rootfs/%s",path);
  56. path = buf;
  57. printf("about to load: %s\n",path);
  58. struct stat st;
  59. if (stat(path, &st)) return alloc_error(ERR_NOT_FOUND);
  60. if (st.st_size < 1) return alloc_bytes(); // zero-byte file
  61. FILE* f = fopen(path,"rb");
  62. if (!f) return alloc_error(ERR_FORBIDDEN);
  63. Cell* result_cell = alloc_num_bytes(st.st_size);
  64. fread(result_cell->addr, 1, st.st_size, f);
  65. fclose(f);
  66. return result_cell;
  67. }
  68. #include <sys/socket.h>
  69. #include <netinet/in.h>
  70. #include <unistd.h>
  71. // TODO: create socket handle and return it
  72. static int tcp_outbound_sockfd = 0;
  73. Cell* my_tcp_connected_callback;
  74. Cell* my_tcp_data_callback;
  75. Cell* network_cell;
  76. Cell* machine_poll_udp() {
  77. if (my_tcp_data_callback) {
  78. int len = read(tcp_outbound_sockfd,network_cell->addr,1024*64);
  79. if (len>0) {
  80. printf("-- received tcp packet of len: %d\n",len);
  81. ((uint8_t*)network_cell->addr)[len] = 0;
  82. network_cell->size = len;
  83. funcptr fn = (funcptr)my_tcp_data_callback->next;
  84. fn();
  85. return NULL;
  86. }
  87. }
  88. return NULL;
  89. }
  90. Cell* machine_send_udp(Cell* data_cell) {
  91. return data_cell;
  92. }
  93. Cell* machine_connect_tcp(Cell* host_cell, Cell* port_cell, Cell* connected_fn_cell, Cell* data_fn_cell) {
  94. my_tcp_connected_callback = connected_fn_cell;
  95. my_tcp_data_callback = data_fn_cell;
  96. struct sockaddr_in serv_addr;
  97. bzero((char *) &serv_addr, sizeof(serv_addr));
  98. serv_addr.sin_family = AF_INET;
  99. bcopy((char *)host_cell->addr,
  100. (char *)&serv_addr.sin_addr.s_addr,
  101. 4); // ipv4 only
  102. tcp_outbound_sockfd = socket(AF_INET, SOCK_STREAM, 0);
  103. struct timeval tv;
  104. tv.tv_sec = 1; /* 1 Sec Timeout */
  105. tv.tv_usec = 0; // Not init'ing this can cause strange errors
  106. setsockopt(tcp_outbound_sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
  107. serv_addr.sin_port = htons(port_cell->value);
  108. printf("[machine_connect_tcp] trying to connect to %x:%d\r\n",serv_addr.sin_addr.s_addr,port_cell->value);
  109. if (connect(tcp_outbound_sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
  110. printf("[machine_connect_tcp] couldn't connect.\r\n");
  111. } else {
  112. // call connected callback
  113. printf("[machine_connect_tcp] connected. calling %p…\r\n",my_tcp_connected_callback->next);
  114. funcptr fn = (funcptr)my_tcp_connected_callback->next;
  115. fn();
  116. }
  117. return port_cell;
  118. }
  119. Cell* machine_send_tcp(Cell* data_cell) {
  120. write(tcp_outbound_sockfd,data_cell->addr,data_cell->size);
  121. return data_cell;
  122. }
  123. Cell* machine_bind_tcp(Cell* port_cell, Cell* fn_cell) {
  124. return fn_cell;
  125. }