diff options
author | David Walter Seikel | 2014-01-28 01:18:01 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-01-28 01:18:01 +1000 |
commit | 71ee93c0334984f257a3748b032237aaaedec04b (patch) | |
tree | a760e4a599387313ad720c0fb4e396579065a26a | |
parent | Fix some typos. (diff) | |
download | boxes-71ee93c0334984f257a3748b032237aaaedec04b.zip boxes-71ee93c0334984f257a3748b032237aaaedec04b.tar.gz boxes-71ee93c0334984f257a3748b032237aaaedec04b.tar.bz2 boxes-71ee93c0334984f257a3748b032237aaaedec04b.tar.xz |
Some Rob style simplifications.
-rw-r--r-- | boxes.c | 67 |
1 files changed, 32 insertions, 35 deletions
@@ -49,7 +49,6 @@ GLOBALS( | |||
49 | #define FLAG_h 8 | 49 | #define FLAG_h 8 |
50 | #define FLAG_w 16 | 50 | #define FLAG_w 16 |
51 | 51 | ||
52 | #define MEM_SIZE 128 | ||
53 | 52 | ||
54 | /* This is trying to be a generic text editing, text viewing, and terminal | 53 | /* This is trying to be a generic text editing, text viewing, and terminal |
55 | * handling system. The current code is a work in progress, and the design | 54 | * handling system. The current code is a work in progress, and the design |
@@ -423,6 +422,15 @@ struct key keys[] = | |||
423 | {"\x1B\x39", "F9"}, | 422 | {"\x1B\x39", "F9"}, |
424 | {"\x1B\x30", "F10"}, | 423 | {"\x1B\x30", "F10"}, |
425 | 424 | ||
425 | /* TODO - Rob says - | ||
426 | ...you don't need a NULL terminator for | ||
427 | an array, you can do sizeof(table)/sizeof(*table). Divide the size of | ||
428 | the table (in bytes) by the size of a member of the table (in bytes) to | ||
429 | get the number of entries. | ||
430 | |||
431 | I should try that trick. | ||
432 | */ | ||
433 | |||
426 | {NULL, NULL} | 434 | {NULL, NULL} |
427 | }; | 435 | }; |
428 | 436 | ||
@@ -464,8 +472,7 @@ struct function | |||
464 | 472 | ||
465 | struct keyCommand | 473 | struct keyCommand |
466 | { | 474 | { |
467 | char *key; // Key name. | 475 | char *key, *command; |
468 | char *command; | ||
469 | }; | 476 | }; |
470 | 477 | ||
471 | struct item | 478 | struct item |
@@ -483,8 +490,7 @@ struct item | |||
483 | 490 | ||
484 | struct borderWidget | 491 | struct borderWidget |
485 | { | 492 | { |
486 | char *text; | 493 | char *text, *command; |
487 | char *command; | ||
488 | }; | 494 | }; |
489 | 495 | ||
490 | // TODO - No idea if we will actually need this. | 496 | // TODO - No idea if we will actually need this. |
@@ -549,14 +555,9 @@ struct context // Defines a context for content. Text viewer, editor, file br | |||
549 | // Status lines can have them to. | 555 | // Status lines can have them to. |
550 | struct border | 556 | struct border |
551 | { | 557 | { |
552 | struct borderWidget *topLeft; | 558 | struct borderWidget *topLeft, *topMiddle, *topRight; |
553 | struct borderWidget *topMiddle; | 559 | struct borderWidget *bottomLeft, *bottomMiddle, *bottomRight; |
554 | struct borderWidget *topRight; | 560 | struct borderWidget *left, *right; |
555 | struct borderWidget *bottomLeft; | ||
556 | struct borderWidget *bottomMiddle; | ||
557 | struct borderWidget *bottomRight; | ||
558 | struct borderWidget *left; | ||
559 | struct borderWidget *right; | ||
560 | }; | 561 | }; |
561 | 562 | ||
562 | struct line | 563 | struct line |
@@ -658,6 +659,8 @@ void doCommand(struct function *functions, char *command, view *view, event *eve | |||
658 | } | 659 | } |
659 | } | 660 | } |
660 | 661 | ||
662 | #define MEM_SIZE 128 // Chunk size for line memory allocation. | ||
663 | |||
661 | // Inserts the line after the given line, or at the end of content if no line. | 664 | // Inserts the line after the given line, or at the end of content if no line. |
662 | struct line *addLine(struct content *content, struct line *line, char *text, uint32_t length) | 665 | struct line *addLine(struct content *content, struct line *line, char *text, uint32_t length) |
663 | { | 666 | { |
@@ -1738,9 +1741,6 @@ void nop(box *box, event *event) | |||
1738 | } | 1741 | } |
1739 | 1742 | ||
1740 | 1743 | ||
1741 | |||
1742 | #define BUFFER_LEN 16 | ||
1743 | |||
1744 | // Basically this is the main loop. | 1744 | // Basically this is the main loop. |
1745 | 1745 | ||
1746 | // X and Y are screen coords. | 1746 | // X and Y are screen coords. |
@@ -1751,10 +1751,10 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1751 | { | 1751 | { |
1752 | struct termios termio, oldtermio; | 1752 | struct termios termio, oldtermio; |
1753 | struct pollfd pollfds[1]; | 1753 | struct pollfd pollfds[1]; |
1754 | char buffer[BUFFER_LEN + 1]; | 1754 | char buffer[20]; |
1755 | char command[BUFFER_LEN + 1]; | 1755 | char command[20]; |
1756 | int pollcount = 1; | 1756 | int pollcount = 1; |
1757 | int i = 0; | 1757 | int index = 0; |
1758 | // TODO - multiline editLine is an advanced feature. Editing boxes just moves the editLine up and down. | 1758 | // TODO - multiline editLine is an advanced feature. Editing boxes just moves the editLine up and down. |
1759 | // uint16_t h = 1; | 1759 | // uint16_t h = 1; |
1760 | // TODO - should check if it's at the top of the box, then grow it down instead of up if so. | 1760 | // TODO - should check if it's at the top of the box, then grow it down instead of up if so. |
@@ -1800,10 +1800,8 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1800 | 1800 | ||
1801 | // Coz things might change out from under us, find the current view. | 1801 | // Coz things might change out from under us, find the current view. |
1802 | // TODO - see if I can get this lot out of here. | 1802 | // TODO - see if I can get this lot out of here. |
1803 | if (commandMode) | 1803 | if (commandMode) view = commandLine; |
1804 | view = commandLine; | 1804 | else view = currentBox->view; |
1805 | else | ||
1806 | view = currentBox->view; | ||
1807 | y = view->Y + (view->cY - view->offsetY); | 1805 | y = view->Y + (view->cY - view->offsetY); |
1808 | len = strlen(view->prompt); | 1806 | len = strlen(view->prompt); |
1809 | drawLine(y, view->X, view->X + view->W, "\0", " ", view->prompt, '\0', 0); | 1807 | drawLine(y, view->X, view->X + view->W, "\0", " ", view->prompt, '\0', 0); |
@@ -1821,11 +1819,11 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1821 | if (0 > p) perror_exit("poll"); | 1819 | if (0 > p) perror_exit("poll"); |
1822 | if (0 == p) // A timeout, trigger a time event. | 1820 | if (0 == p) // A timeout, trigger a time event. |
1823 | { | 1821 | { |
1824 | if ((1 == i) && ('\x1B' == buffer[0])) | 1822 | if ((1 == index) && ('\x1B' == buffer[0])) |
1825 | { | 1823 | { |
1826 | // After a short delay to check, this is a real Escape key, not part of an escape sequence, so deal with it. | 1824 | // After a short delay to check, this is a real Escape key, not part of an escape sequence, so deal with it. |
1827 | strcpy(command, "^["); | 1825 | strcpy(command, "^["); |
1828 | i = 0; | 1826 | index = 0; |
1829 | buffer[0] = 0; | 1827 | buffer[0] = 0; |
1830 | } | 1828 | } |
1831 | // TODO - Send a timer event somewhere. This wont be a precise timed event, but don't think we need one. | 1829 | // TODO - Send a timer event somewhere. This wont be a precise timed event, but don't think we need one. |
@@ -1838,7 +1836,7 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1838 | { | 1836 | { |
1839 | // I am assuming that we get the input atomically, each multibyte key fits neatly into one read. | 1837 | // I am assuming that we get the input atomically, each multibyte key fits neatly into one read. |
1840 | // If that's not true (which is entirely likely), then we have to get complicated with circular buffers and stuff, or just one byte at a time. | 1838 | // If that's not true (which is entirely likely), then we have to get complicated with circular buffers and stuff, or just one byte at a time. |
1841 | ret = read(pollfds[p].fd, &buffer[i], BUFFER_LEN - i); | 1839 | ret = read(pollfds[p].fd, &buffer[index], sizeof(buffer) - (index + 1)); |
1842 | if (ret < 0) // An error happened. | 1840 | if (ret < 0) // An error happened. |
1843 | { | 1841 | { |
1844 | // For now, just ignore errors. | 1842 | // For now, just ignore errors. |
@@ -1852,18 +1850,17 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1852 | } | 1850 | } |
1853 | else | 1851 | else |
1854 | { | 1852 | { |
1855 | i += ret; | 1853 | index += ret; |
1856 | if (BUFFER_LEN <= i) // Ran out of buffer. | 1854 | if (sizeof(buffer) < (index + 1)) // Ran out of buffer. |
1857 | { | 1855 | { |
1858 | fprintf(stderr, "Full buffer - %s -> %s\n", buffer, command); | 1856 | fprintf(stderr, "Full buffer - %s -> %s\n", buffer, command); |
1859 | for (j = 0; buffer[j + 1]; j++) | 1857 | for (j = 0; buffer[j + 1]; j++) |
1860 | fprintf(stderr, "(%x) %c, ", (int) buffer[j], buffer[j]); | 1858 | fprintf(stderr, "(%x) %c, ", (int) buffer[j], buffer[j]); |
1861 | fflush(stderr); | 1859 | fflush(stderr); |
1862 | i = 0; | 1860 | index = 0; |
1863 | buffer[0] = 0; | 1861 | buffer[0] = 0; |
1864 | } | 1862 | } |
1865 | else | 1863 | else buffer[index] = 0; |
1866 | buffer[i] = 0; | ||
1867 | } | 1864 | } |
1868 | } | 1865 | } |
1869 | } | 1866 | } |
@@ -1877,14 +1874,14 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1877 | if (strcmp(keys[j].code, buffer) == 0) | 1874 | if (strcmp(keys[j].code, buffer) == 0) |
1878 | { | 1875 | { |
1879 | strcat(command, keys[j].name); | 1876 | strcat(command, keys[j].name); |
1880 | i = 0; | 1877 | index = 0; |
1881 | buffer[0] = 0; | 1878 | buffer[0] = 0; |
1882 | break; | 1879 | break; |
1883 | } | 1880 | } |
1884 | } | 1881 | } |
1885 | 1882 | ||
1886 | // See if it's an ordinary key, | 1883 | // See if it's an ordinary key, |
1887 | if ((1 == i) && isprint(buffer[0])) | 1884 | if ((1 == index) && isprint(buffer[0])) |
1888 | { | 1885 | { |
1889 | // If there's an outstanding command, add this to the end of it. | 1886 | // If there's an outstanding command, add this to the end of it. |
1890 | if (command[0]) | 1887 | if (command[0]) |
@@ -1898,14 +1895,14 @@ void editLine(view *view, int16_t X, int16_t Y, int16_t W, int16_t H) | |||
1898 | view->oW = formatLine(view, view->line->line, &(view->output)); | 1895 | view->oW = formatLine(view, view->line->line, &(view->output)); |
1899 | moveCursorRelative(view, strlen(buffer), 0, 0, 0); | 1896 | moveCursorRelative(view, strlen(buffer), 0, 0, 0); |
1900 | } | 1897 | } |
1901 | i = 0; | 1898 | index = 0; |
1902 | buffer[0] = 0; | 1899 | buffer[0] = 0; |
1903 | } | 1900 | } |
1904 | 1901 | ||
1905 | // TODO - If the view->context has an event handler, use it, otherwise look up the specific event handler in the context modes ourselves. | 1902 | // TODO - If the view->context has an event handler, use it, otherwise look up the specific event handler in the context modes ourselves. |
1906 | if (command[0]) // Search for a bound key. | 1903 | if (command[0]) // Search for a bound key. |
1907 | { | 1904 | { |
1908 | if (BUFFER_LEN <= strlen(command)) | 1905 | if (sizeof(command) < (strlen(command) + 1)) |
1909 | { | 1906 | { |
1910 | fprintf(stderr, "Full command buffer - %s \n", command); | 1907 | fprintf(stderr, "Full command buffer - %s \n", command); |
1911 | fflush(stderr); | 1908 | fflush(stderr); |