Browse Source

[Automated] Change style to right-pointers

master
cancel 5 years ago
parent
commit
beeb85720e
  1. 1
      .clang-format
  2. 12
      bank.c
  3. 12
      bank.h
  4. 6
      cli_main.c
  5. 36
      field.c
  6. 28
      field.h
  7. 12
      gbuffer.c
  8. 22
      gbuffer.h
  9. 56
      osc_out.c
  10. 34
      osc_out.h
  11. 42
      sim.c
  12. 4
      sim.h
  13. 46
      sysmisc.c
  14. 20
      sysmisc.h
  15. 225
      term_util.c
  16. 68
      term_util.h
  17. 354
      tui_main.c

1
.clang-format

@ -1,5 +1,4 @@
BasedOnStyle: LLVM
PointerAlignment: Left
ReflowComments: false
MacroBlockBegin: "^BEGIN_OPERATOR"

12
bank.c

@ -1,13 +1,13 @@
#include "bank.h"
void oevent_list_init(Oevent_list* olist) {
void oevent_list_init(Oevent_list *olist) {
olist->buffer = NULL;
olist->count = 0;
olist->capacity = 0;
}
void oevent_list_deinit(Oevent_list* olist) { free(olist->buffer); }
void oevent_list_clear(Oevent_list* olist) { olist->count = 0; }
void oevent_list_copy(Oevent_list const* src, Oevent_list* dest) {
void oevent_list_deinit(Oevent_list *olist) { free(olist->buffer); }
void oevent_list_clear(Oevent_list *olist) { olist->count = 0; }
void oevent_list_copy(Oevent_list const *src, Oevent_list *dest) {
Usz src_count = src->count;
if (dest->capacity < src_count) {
Usz new_cap = orca_round_up_power2(src_count);
@ -17,7 +17,7 @@ void oevent_list_copy(Oevent_list const* src, Oevent_list* dest) {
memcpy(dest->buffer, src->buffer, src_count * sizeof(Oevent));
dest->count = src_count;
}
Oevent* oevent_list_alloc_item(Oevent_list* olist) {
Oevent *oevent_list_alloc_item(Oevent_list *olist) {
Usz count = olist->count;
if (olist->capacity == count) {
// Note: no overflow check, but you're probably out of memory if this
@ -27,7 +27,7 @@ Oevent* oevent_list_alloc_item(Oevent_list* olist) {
olist->buffer = realloc(olist->buffer, capacity * sizeof(Oevent));
olist->capacity = capacity;
}
Oevent* result = olist->buffer + count;
Oevent *result = olist->buffer + count;
olist->count = count + 1;
return result;
}

12
bank.h

@ -45,15 +45,15 @@ typedef union {
} Oevent;
typedef struct {
Oevent* buffer;
Oevent *buffer;
Usz count;
Usz capacity;
} Oevent_list;
void oevent_list_init(Oevent_list* olist);
void oevent_list_deinit(Oevent_list* olist);
void oevent_list_clear(Oevent_list* olist);
void oevent_list_init(Oevent_list *olist);
void oevent_list_deinit(Oevent_list *olist);
void oevent_list_clear(Oevent_list *olist);
ORCA_FORCE_NO_INLINE
void oevent_list_copy(Oevent_list const* src, Oevent_list* dest);
void oevent_list_copy(Oevent_list const *src, Oevent_list *dest);
ORCA_FORCE_NO_INLINE
Oevent* oevent_list_alloc_item(Oevent_list* olist);
Oevent *oevent_list_alloc_item(Oevent_list *olist);

6
cli_main.c

@ -18,11 +18,11 @@ static void usage(void) {
// clang-format on
}
int main(int argc, char** argv) {
int main(int argc, char **argv) {
static struct option cli_options[] = {{"help", no_argument, 0, 'h'},
{NULL, 0, NULL, 0}};
char* input_file = NULL;
char *input_file = NULL;
int ticks = 1;
for (;;) {
@ -73,7 +73,7 @@ int main(int argc, char** argv) {
Field_load_error fle = field_load_file(input_file, &field);
if (fle != Field_load_error_ok) {
field_deinit(&field);
char const* errstr = "Unknown";
char const *errstr = "Unknown";
switch (fle) {
case Field_load_error_ok:
break;

36
field.c

@ -2,13 +2,13 @@
#include "gbuffer.h"
#include <ctype.h>
void field_init(Field* f) {
void field_init(Field *f) {
f->buffer = NULL;
f->height = 0;
f->width = 0;
}
void field_init_fill(Field* f, Usz height, Usz width, Glyph fill_char) {
void field_init_fill(Field *f, Usz height, Usz width, Glyph fill_char) {
assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
Usz num_cells = height * width;
f->buffer = malloc(num_cells * sizeof(Glyph));
@ -17,9 +17,9 @@ void field_init_fill(Field* f, Usz height, Usz width, Glyph fill_char) {
f->width = (U16)width;
}
void field_deinit(Field* f) { free(f->buffer); }
void field_deinit(Field *f) { free(f->buffer); }
void field_resize_raw(Field* f, Usz height, Usz width) {
void field_resize_raw(Field *f, Usz height, Usz width) {
assert(height <= ORCA_Y_MAX && width <= ORCA_X_MAX);
Usz cells = height * width;
f->buffer = realloc(f->buffer, cells * sizeof(Glyph));
@ -27,13 +27,13 @@ void field_resize_raw(Field* f, Usz height, Usz width) {
f->width = (U16)width;
}
void field_resize_raw_if_necessary(Field* field, Usz height, Usz width) {
void field_resize_raw_if_necessary(Field *field, Usz height, Usz width) {
if (field->height != height || field->width != width) {
field_resize_raw(field, height, width);
}
}
void field_copy(Field* src, Field* dest) {
void field_copy(Field *src, Field *dest) {
field_resize_raw_if_necessary(dest, src->height, src->width);
gbuffer_copy_subrect(src->buffer, dest->buffer, src->height, src->width,
dest->height, dest->width, 0, 0, 0, 0, src->height,
@ -42,16 +42,16 @@ void field_copy(Field* src, Field* dest) {
static inline bool glyph_char_is_valid(char c) { return c >= '!' && c <= '~'; }
void field_fput(Field* f, FILE* stream) {
void field_fput(Field *f, FILE *stream) {
enum { Column_buffer_count = 4096 };
char out_buffer[Column_buffer_count];
Usz f_height = f->height;
Usz f_width = f->width;
Glyph* f_buffer = f->buffer;
Glyph *f_buffer = f->buffer;
if (f_width > Column_buffer_count - 2)
return;
for (Usz iy = 0; iy < f_height; ++iy) {
Glyph* row_p = f_buffer + f_width * iy;
Glyph *row_p = f_buffer + f_width * iy;
for (Usz ix = 0; ix < f_width; ++ix) {
char c = row_p[ix];
out_buffer[ix] = glyph_char_is_valid(c) ? c : '?';
@ -62,8 +62,8 @@ void field_fput(Field* f, FILE* stream) {
}
}
Field_load_error field_load_file(char const* filepath, Field* field) {
FILE* file = fopen(filepath, "r");
Field_load_error field_load_file(char const *filepath, Field *field) {
FILE *file = fopen(filepath, "r");
if (file == NULL) {
return Field_load_error_cant_open_file;
}
@ -72,7 +72,7 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
Usz first_row_columns = 0;
Usz rows = 0;
for (;;) {
char* s = fgets(buf, Bufsize, file);
char *s = fgets(buf, Bufsize, file);
if (s == NULL)
break;
if (rows == ORCA_Y_MAX) {
@ -105,7 +105,7 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
return Field_load_error_not_a_rectangle;
}
field_resize_raw(field, rows + 1, first_row_columns);
Glyph* rowbuff = field->buffer + first_row_columns * rows;
Glyph *rowbuff = field->buffer + first_row_columns * rows;
for (Usz i = 0; i < len; ++i) {
char c = buf[i];
rowbuff[i] = glyph_char_is_valid(c) ? c : '.';
@ -116,8 +116,8 @@ Field_load_error field_load_file(char const* filepath, Field* field) {
return Field_load_error_ok;
}
char const* field_load_error_string(Field_load_error fle) {
char const* errstr = "Unknown";
char const *field_load_error_string(Field_load_error fle) {
char const *errstr = "Unknown";
switch (fle) {
case Field_load_error_ok:
errstr = "OK";
@ -141,12 +141,12 @@ char const* field_load_error_string(Field_load_error fle) {
return errstr;
}
void mbuf_reusable_init(Mbuf_reusable* mbr) {
void mbuf_reusable_init(Mbuf_reusable *mbr) {
mbr->buffer = NULL;
mbr->capacity = 0;
}
void mbuf_reusable_ensure_size(Mbuf_reusable* mbr, Usz height, Usz width) {
void mbuf_reusable_ensure_size(Mbuf_reusable *mbr, Usz height, Usz width) {
Usz capacity = height * width;
if (mbr->capacity < capacity) {
mbr->buffer = realloc(mbr->buffer, capacity);
@ -154,4 +154,4 @@ void mbuf_reusable_ensure_size(Mbuf_reusable* mbr, Usz height, Usz width) {
}
}
void mbuf_reusable_deinit(Mbuf_reusable* mbr) { free(mbr->buffer); }
void mbuf_reusable_deinit(Mbuf_reusable *mbr) { free(mbr->buffer); }

28
field.h

@ -7,18 +7,18 @@
// might want to do. Not used by the VM.
typedef struct {
Glyph* buffer;
Glyph *buffer;
U16 height;
U16 width;
} Field;
void field_init(Field* field);
void field_init_fill(Field* field, Usz height, Usz width, Glyph fill_char);
void field_deinit(Field* field);
void field_resize_raw(Field* field, Usz height, Usz width);
void field_resize_raw_if_necessary(Field* field, Usz height, Usz width);
void field_copy(Field* src, Field* dest);
void field_fput(Field* field, FILE* stream);
void field_init(Field *field);
void field_init_fill(Field *field, Usz height, Usz width, Glyph fill_char);
void field_deinit(Field *field);
void field_resize_raw(Field *field, Usz height, Usz width);
void field_resize_raw_if_necessary(Field *field, Usz height, Usz width);
void field_copy(Field *src, Field *dest);
void field_fput(Field *field, FILE *stream);
typedef enum {
Field_load_error_ok = 0,
@ -29,9 +29,9 @@ typedef enum {
Field_load_error_not_a_rectangle = 5,
} Field_load_error;
Field_load_error field_load_file(char const* filepath, Field* field);
Field_load_error field_load_file(char const *filepath, Field *field);
char const* field_load_error_string(Field_load_error fle);
char const *field_load_error_string(Field_load_error fle);
// A reusable buffer for the per-grid-cell flags. Similar to how Field is a
// reusable buffer for Glyph, Mbuf_reusable is for Mark. The naming isn't so
@ -42,10 +42,10 @@ char const* field_load_error_string(Field_load_error fle);
// that functionality.
typedef struct Mbuf_reusable {
Mark* buffer;
Mark *buffer;
Usz capacity;
} Mbuf_reusable;
void mbuf_reusable_init(Mbuf_reusable* mbr);
void mbuf_reusable_ensure_size(Mbuf_reusable* mbr, Usz height, Usz width);
void mbuf_reusable_deinit(Mbuf_reusable* mbr);
void mbuf_reusable_init(Mbuf_reusable *mbr);
void mbuf_reusable_ensure_size(Mbuf_reusable *mbr, Usz height, Usz width);
void mbuf_reusable_deinit(Mbuf_reusable *mbr);

12
gbuffer.c

@ -1,6 +1,6 @@
#include "gbuffer.h"
void gbuffer_copy_subrect(Glyph* src, Glyph* dest, Usz src_height,
void gbuffer_copy_subrect(Glyph *src, Glyph *dest, Usz src_height,
Usz src_width, Usz dest_height, Usz dest_width,
Usz src_y, Usz src_x, Usz dest_y, Usz dest_x,
Usz height, Usz width) {
@ -24,8 +24,8 @@ void gbuffer_copy_subrect(Glyph* src, Glyph* dest, Usz src_height,
if (row_copy_1 < row_copy)
row_copy = row_copy_1;
Usz copy_bytes = row_copy * sizeof(Glyph);
Glyph* src_p = src + src_y * src_width + src_x;
Glyph* dest_p = dest + dest_y * dest_width + dest_x;
Glyph *src_p = src + src_y * src_width + src_x;
Glyph *dest_p = dest + dest_y * dest_width + dest_x;
Isz src_stride;
Isz dest_stride;
if (src_y >= dest_y) {
@ -48,7 +48,7 @@ void gbuffer_copy_subrect(Glyph* src, Glyph* dest, Usz src_height,
}
}
void gbuffer_fill_subrect(Glyph* gbuffer, Usz f_height, Usz f_width, Usz y,
void gbuffer_fill_subrect(Glyph *gbuffer, Usz f_height, Usz f_width, Usz y,
Usz x, Usz height, Usz width, Glyph fill_char) {
if (y >= f_height || x >= f_width)
return;
@ -63,7 +63,7 @@ void gbuffer_fill_subrect(Glyph* gbuffer, Usz f_height, Usz f_width, Usz y,
if (columns_0 < columns)
columns = columns_0;
Usz fill_bytes = columns * sizeof(Glyph);
Glyph* p = gbuffer + y * f_width + x;
Glyph *p = gbuffer + y * f_width + x;
Usz iy = 0;
for (;;) {
memset(p, fill_char, fill_bytes);
@ -74,7 +74,7 @@ void gbuffer_fill_subrect(Glyph* gbuffer, Usz f_height, Usz f_width, Usz y,
}
}
void mbuffer_clear(Mark* mbuf, Usz height, Usz width) {
void mbuffer_clear(Mark *mbuf, Usz height, Usz width) {
Usz cleared_size = height * width;
memset(mbuf, 0, cleared_size);
}

22
gbuffer.h

@ -1,14 +1,14 @@
#pragma once
#include "base.h"
ORCA_PURE static inline Glyph gbuffer_peek(Glyph* gbuf, Usz height, Usz width,
ORCA_PURE static inline Glyph gbuffer_peek(Glyph *gbuf, Usz height, Usz width,
Usz y, Usz x) {
assert(y < height && x < width);
(void)height;
return gbuf[y + width + x];
}
ORCA_PURE static inline Glyph gbuffer_peek_relative(Glyph* gbuf, Usz height,
ORCA_PURE static inline Glyph gbuffer_peek_relative(Glyph *gbuf, Usz height,
Usz width, Usz y, Usz x,
Isz delta_y, Isz delta_x) {
Isz y0 = (Isz)y + delta_y;
@ -18,14 +18,14 @@ ORCA_PURE static inline Glyph gbuffer_peek_relative(Glyph* gbuf, Usz height,
return gbuf[(Usz)y0 * width + (Usz)x0];
}
static inline void gbuffer_poke(Glyph* gbuf, Usz height, Usz width, Usz y,
static inline void gbuffer_poke(Glyph *gbuf, Usz height, Usz width, Usz y,
Usz x, Glyph g) {
assert(y < height && x < width);
(void)height;
gbuf[y * width + x] = g;
}
static inline void gbuffer_poke_relative(Glyph* gbuf, Usz height, Usz width,
static inline void gbuffer_poke_relative(Glyph *gbuf, Usz height, Usz width,
Usz y, Usz x, Isz delta_y, Isz delta_x,
Glyph g) {
Isz y0 = (Isz)y + delta_y;
@ -36,13 +36,13 @@ static inline void gbuffer_poke_relative(Glyph* gbuf, Usz height, Usz width,
}
ORCA_FORCE_NO_INLINE
void gbuffer_copy_subrect(Glyph* src, Glyph* dest, Usz src_grid_h,
void gbuffer_copy_subrect(Glyph *src, Glyph *dest, Usz src_grid_h,
Usz src_grid_w, Usz dest_grid_h, Usz dest_grid_w,
Usz src_y, Usz src_x, Usz dest_y, Usz dest_x,
Usz height, Usz width);
ORCA_FORCE_NO_INLINE
void gbuffer_fill_subrect(Glyph* gbuf, Usz grid_h, Usz grid_w, Usz y, Usz x,
void gbuffer_fill_subrect(Glyph *gbuf, Usz grid_h, Usz grid_w, Usz y, Usz x,
Usz height, Usz width, Glyph fill_char);
typedef enum {
@ -55,14 +55,14 @@ typedef enum {
} Mark_flags;
ORCA_OK_IF_UNUSED
static Mark_flags mbuffer_peek(Mark* mbuf, Usz height, Usz width, Usz y,
static Mark_flags mbuffer_peek(Mark *mbuf, Usz height, Usz width, Usz y,
Usz x) {
(void)height;
return mbuf[y * width + x];
}
ORCA_OK_IF_UNUSED
static Mark_flags mbuffer_peek_relative(Mark* mbuf, Usz height, Usz width,
static Mark_flags mbuffer_peek_relative(Mark *mbuf, Usz height, Usz width,
Usz y, Usz x, Isz offs_y, Isz offs_x) {
Isz y0 = (Isz)y + offs_y;
Isz x0 = (Isz)x + offs_x;
@ -72,14 +72,14 @@ static Mark_flags mbuffer_peek_relative(Mark* mbuf, Usz height, Usz width,
}
ORCA_OK_IF_UNUSED
static void mbuffer_poke_flags_or(Mark* mbuf, Usz height, Usz width, Usz y,
static void mbuffer_poke_flags_or(Mark *mbuf, Usz height, Usz width, Usz y,
Usz x, Mark_flags flags) {
(void)height;
mbuf[y * width + x] |= (Mark)flags;
}
ORCA_OK_IF_UNUSED
static void mbuffer_poke_relative_flags_or(Mark* mbuf, Usz height, Usz width,
static void mbuffer_poke_relative_flags_or(Mark *mbuf, Usz height, Usz width,
Usz y, Usz x, Isz offs_y, Isz offs_x,
Mark_flags flags) {
Isz y0 = (Isz)y + offs_y;
@ -89,4 +89,4 @@ static void mbuffer_poke_relative_flags_or(Mark* mbuf, Usz height, Usz width,
mbuf[(Usz)y0 * width + (Usz)x0] |= (Mark)flags;
}
void mbuffer_clear(Mark* mbuf, Usz height, Usz width);
void mbuffer_clear(Mark *mbuf, Usz height, Usz width);

56
osc_out.c

@ -11,21 +11,21 @@ struct Oosc_dev {
int fd;
// Just keep the whole list around, since juggling the strict-aliasing
// problems with sockaddr_storage is not worth it.
struct addrinfo* chosen;
struct addrinfo* head;
struct addrinfo *chosen;
struct addrinfo *head;
};
Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev** out_ptr,
char const* dest_addr,
char const* dest_port) {
Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev **out_ptr,
char const *dest_addr,
char const *dest_port) {
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_ADDRCONFIG;
struct addrinfo* chosen = NULL;
struct addrinfo* head = NULL;
struct addrinfo *chosen = NULL;
struct addrinfo *head = NULL;
int err = getaddrinfo(dest_addr, dest_port, &hints, &head);
if (err != 0) {
fprintf(stderr, "Failed to get address info, error: %d\n", errno);
@ -43,7 +43,7 @@ Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev** out_ptr,
if (!dest_addr) {
#endif
{
for (struct addrinfo* a = head; a; a = a->ai_next) {
for (struct addrinfo *a = head; a; a = a->ai_next) {
if (a->ai_family != AF_INET)
continue;
chosen = a;
@ -77,7 +77,7 @@ Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev** out_ptr,
freeaddrinfo(head);
return Oosc_udp_create_error_couldnt_open_socket;
}
Oosc_dev* dev = malloc(sizeof(Oosc_dev));
Oosc_dev *dev = malloc(sizeof(Oosc_dev));
dev->fd = udpfd;
dev->chosen = chosen;
dev->head = head;
@ -85,13 +85,13 @@ Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev** out_ptr,
return Oosc_udp_create_error_ok;
}
void oosc_dev_destroy(Oosc_dev* dev) {
void oosc_dev_destroy(Oosc_dev *dev) {
close(dev->fd);
freeaddrinfo(dev->head);
free(dev);
}
void oosc_send_datagram(Oosc_dev* dev, char const* data, Usz size) {
void oosc_send_datagram(Oosc_dev *dev, char const *data, Usz size) {
ssize_t res = sendto(dev->fd, data, size, 0, dev->chosen->ai_addr,
dev->chosen->ai_addrlen);
if (res < 0) {
@ -100,8 +100,8 @@ void oosc_send_datagram(Oosc_dev* dev, char const* data, Usz size) {
}
}
static bool oosc_write_strn(char* restrict buffer, Usz buffer_size,
Usz* buffer_pos, char const* restrict in_str,
static bool oosc_write_strn(char *restrict buffer, Usz buffer_size,
Usz *buffer_pos, char const *restrict in_str,
Usz in_str_len) {
// no overflow check, should be fine
Usz in_plus_null = in_str_len + 1;
@ -122,7 +122,7 @@ static bool oosc_write_strn(char* restrict buffer, Usz buffer_size,
return true;
}
void oosc_send_int32s(Oosc_dev* dev, char const* osc_address, I32 const* vals,
void oosc_send_int32s(Oosc_dev *dev, char const *osc_address, I32 const *vals,
Usz count) {
char buffer[2048];
Usz buf_pos = 0;
@ -160,20 +160,20 @@ void oosc_send_int32s(Oosc_dev* dev, char const* osc_address, I32 const* vals,
oosc_send_datagram(dev, buffer, buf_pos);
}
void susnote_list_init(Susnote_list* sl) {
void susnote_list_init(Susnote_list *sl) {
sl->buffer = NULL;
sl->count = 0;
sl->capacity = 0;
}
void susnote_list_deinit(Susnote_list* sl) { free(sl->buffer); }
void susnote_list_deinit(Susnote_list *sl) { free(sl->buffer); }
void susnote_list_clear(Susnote_list* sl) { sl->count = 0; }
void susnote_list_clear(Susnote_list *sl) { sl->count = 0; }
void susnote_list_add_notes(Susnote_list* sl, Susnote const* restrict notes,
Usz added_count, Usz* restrict start_removed,
Usz* restrict end_removed) {
Susnote* buffer = sl->buffer;
void susnote_list_add_notes(Susnote_list *sl, Susnote const *restrict notes,
Usz added_count, Usz *restrict start_removed,
Usz *restrict end_removed) {
Susnote *buffer = sl->buffer;
Usz count = sl->count;
Usz cap = sl->capacity;
Usz rem = count + added_count;
@ -205,11 +205,11 @@ void susnote_list_add_notes(Susnote_list* sl, Susnote const* restrict notes,
*end_removed = rem;
}
void susnote_list_advance_time(Susnote_list* sl, double delta_time,
Usz* restrict start_removed,
Usz* restrict end_removed,
double* soonest_deadline) {
Susnote* restrict buffer = sl->buffer;
void susnote_list_advance_time(Susnote_list *sl, double delta_time,
Usz *restrict start_removed,
Usz *restrict end_removed,
double *soonest_deadline) {
Susnote *restrict buffer = sl->buffer;
Usz count = sl->count;
*end_removed = count;
float delta_float = (float)delta_time;
@ -233,9 +233,9 @@ void susnote_list_advance_time(Susnote_list* sl, double delta_time,
sl->count = count;
}
double susnote_list_soonest_deadline(Susnote_list const* sl) {
double susnote_list_soonest_deadline(Susnote_list const *sl) {
float soonest = 1.0f;
Susnote const* buffer = sl->buffer;
Susnote const *buffer = sl->buffer;
for (Usz i = 0, n = sl->count; i < n; ++i) {
float rem = buffer[i].remaining;
if (rem < soonest)

34
osc_out.h

@ -9,17 +9,17 @@ typedef enum {
Oosc_udp_create_error_couldnt_open_socket = 2,
} Oosc_udp_create_error;
Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev** out_ptr,
char const* dest_addr,
char const* dest_port);
void oosc_dev_destroy(Oosc_dev* dev);
Oosc_udp_create_error oosc_dev_create_udp(Oosc_dev **out_ptr,
char const *dest_addr,
char const *dest_port);
void oosc_dev_destroy(Oosc_dev *dev);
// Send a raw UDP datagram.
void oosc_send_datagram(Oosc_dev* dev, char const* data, Usz size);
void oosc_send_datagram(Oosc_dev *dev, char const *data, Usz size);
// Send a list/array of 32-bit integers in OSC format to the specified "osc
// address" (a path like /foo) as a UDP datagram.
void oosc_send_int32s(Oosc_dev* dev, char const* osc_address, I32 const* vals,
void oosc_send_int32s(Oosc_dev *dev, char const *osc_address, I32 const *vals,
Usz count);
// Susnote is for handling MIDI note sustains -- each MIDI on event should be
@ -33,22 +33,22 @@ typedef struct {
} Susnote;
typedef struct {
Susnote* buffer;
Susnote *buffer;
Usz count;
Usz capacity;
} Susnote_list;
void susnote_list_init(Susnote_list* sl);
void susnote_list_deinit(Susnote_list* sl);
void susnote_list_clear(Susnote_list* sl);
void susnote_list_add_notes(Susnote_list* sl, Susnote const* restrict notes,
Usz count, Usz* restrict start_removed,
Usz* restrict end_removed);
void susnote_list_init(Susnote_list *sl);
void susnote_list_deinit(Susnote_list *sl);
void susnote_list_clear(Susnote_list *sl);
void susnote_list_add_notes(Susnote_list *sl, Susnote const *restrict notes,
Usz count, Usz *restrict start_removed,
Usz *restrict end_removed);
void susnote_list_advance_time(
Susnote_list* sl, double delta_time, Usz* restrict start_removed,
Usz* restrict end_removed,
Susnote_list *sl, double delta_time, Usz *restrict start_removed,
Usz *restrict end_removed,
// 1.0 if no notes remain or none are shorter than 1.0
double* soonest_deadline);
double *soonest_deadline);
// Returns 1.0 if no notes remain or none are shorter than 1.0
double susnote_list_soonest_deadline(Susnote_list const* sl);
double susnote_list_soonest_deadline(Susnote_list const *sl);

42
sim.c

@ -81,9 +81,9 @@ static inline Glyph glyph_with_case(Glyph g, Glyph caser) {
(caser & Case_bit));
}
ORCA_PURE static bool oper_has_neighboring_bang(Glyph const* gbuf, Usz h, Usz w,
ORCA_PURE static bool oper_has_neighboring_bang(Glyph const *gbuf, Usz h, Usz w,
Usz y, Usz x) {
Glyph const* gp = gbuf + w * y + x;
Glyph const *gp = gbuf + w * y + x;
if (x < w - 1 && gp[1] == '*')
return true;
if (x > 0 && *(gp - 1) == '*')
@ -146,12 +146,12 @@ static ORCA_FORCE_NO_INLINE U8 midi_velocity_of(Glyph g) {
}
typedef struct {
Glyph* vars_slots;
Oevent_list* oevent_list;
Glyph *vars_slots;
Oevent_list *oevent_list;
Usz random_seed;
} Oper_extra_params;
static void oper_poke_and_stun(Glyph* restrict gbuffer, Mark* restrict mbuffer,
static void oper_poke_and_stun(Glyph *restrict gbuffer, Mark *restrict mbuffer,
Usz height, Usz width, Usz y, Usz x, Isz delta_y,
Isz delta_x, Glyph g) {
Isz y0 = (Isz)y + delta_y;
@ -167,9 +167,9 @@ static void oper_poke_and_stun(Glyph* restrict gbuffer, Mark* restrict mbuffer,
#define BEGIN_OPERATOR(_oper_name) \
OPER_FUNCTION_ATTRIBS oper_behavior_##_oper_name( \
Glyph* const restrict gbuffer, Mark* const restrict mbuffer, \
Glyph *const restrict gbuffer, Mark *const restrict mbuffer, \
Usz const height, Usz const width, Usz const y, Usz const x, \
Usz Tick_number, Oper_extra_params* const extra_params, \
Usz Tick_number, Oper_extra_params *const extra_params, \
Mark const cell_flags, Glyph const This_oper_char) { \
(void)gbuffer; \
(void)mbuffer; \
@ -288,7 +288,7 @@ BEGIN_OPERATOR(movement)
gbuffer[y * width + x] = '*';
return;
}
Glyph* restrict g_at_dest = gbuffer + (Usz)y0 * width + (Usz)x0;
Glyph *restrict g_at_dest = gbuffer + (Usz)y0 * width + (Usz)x0;
if (*g_at_dest == '.') {
*g_at_dest = This_oper_char;
gbuffer[y * width + x] = '.';
@ -304,8 +304,8 @@ END_OPERATOR
BEGIN_OPERATOR(comment)
// restrict probably ok here...
Glyph const* restrict gline = gbuffer + y * width;
Mark* restrict mline = mbuffer + y * width;
Glyph const *restrict gline = gbuffer + y * width;
Mark *restrict mline = mbuffer + y * width;
Usz max_x = x + 255;
if (width < max_x)
max_x = width;
@ -342,8 +342,8 @@ BEGIN_OPERATOR(midi)
Usz channel_num = index_of(channel_g);
if (channel_num > 15)
channel_num = 15;
Oevent_midi* oe =
(Oevent_midi*)oevent_list_alloc_item(extra_params->oevent_list);
Oevent_midi *oe =
(Oevent_midi *)oevent_list_alloc_item(extra_params->oevent_list);
oe->oevent_type = (U8)Oevent_type_midi;
oe->channel = (U8)channel_num;
oe->octave = octave_num;
@ -356,8 +356,8 @@ BEGIN_OPERATOR(udp)
Usz n = width - x - 1;
if (n > 16)
n = 16;
Glyph const* restrict gline = gbuffer + y * width + x + 1;
Mark* restrict mline = mbuffer + y * width + x + 1;
Glyph const *restrict gline = gbuffer + y * width + x + 1;
Mark *restrict mline = mbuffer + y * width + x + 1;
Glyph cpy[Oevent_udp_string_count];
Usz i;
for (i = 0; i < n; ++i) {
@ -369,8 +369,8 @@ BEGIN_OPERATOR(udp)
}
n = i;
STOP_IF_NOT_BANGED;
Oevent_udp_string* oe =
(Oevent_udp_string*)oevent_list_alloc_item(extra_params->oevent_list);
Oevent_udp_string *oe =
(Oevent_udp_string *)oevent_list_alloc_item(extra_params->oevent_list);
oe->oevent_type = (U8)Oevent_type_udp_string;
oe->count = (U8)n;
for (i = 0; i < n; ++i) {
@ -394,7 +394,7 @@ BEGIN_OPERATOR(osc)
for (Usz i = 0; i < len; ++i) {
buff[i] = (U8)index_of(PEEK(0, (Isz)i + 3));
}
Oevent_osc_ints* oe =
Oevent_osc_ints *oe =
&oevent_list_alloc_item(extra_params->oevent_list)->osc_ints;
oe->oevent_type = (U8)Oevent_type_osc_ints;
oe->glyph = g;
@ -737,8 +737,8 @@ END_OPERATOR
//////// Run simulation
void orca_run(Glyph* restrict gbuf, Mark* restrict mbuf, Usz height, Usz width,
Usz tick_number, Oevent_list* oevent_list, Usz random_seed) {
void orca_run(Glyph *restrict gbuf, Mark *restrict mbuf, Usz height, Usz width,
Usz tick_number, Oevent_list *oevent_list, Usz random_seed) {
Glyph vars_slots[Glyphs_index_count];
memset(vars_slots, '.', sizeof(vars_slots));
Oper_extra_params extras;
@ -747,8 +747,8 @@ void orca_run(Glyph* restrict gbuf, Mark* restrict mbuf, Usz height, Usz width,
extras.random_seed = random_seed;
for (Usz iy = 0; iy < height; ++iy) {
Glyph const* glyph_row = gbuf + iy * width;
Mark const* mark_row = mbuf + iy * width;
Glyph const *glyph_row = gbuf + iy * width;
Mark const *mark_row = mbuf + iy * width;
for (Usz ix = 0; ix < width; ++ix) {
Glyph glyph_char = glyph_row[ix];
if (ORCA_LIKELY(glyph_char == '.'))

4
sim.h

@ -2,6 +2,6 @@
#include "bank.h"
#include "base.h"
void orca_run(Glyph* restrict gbuffer, Mark* restrict mbuffer, Usz height,
Usz width, Usz tick_number, Oevent_list* oevent_list,
void orca_run(Glyph *restrict gbuffer, Mark *restrict mbuffer, Usz height,
Usz width, Usz tick_number, Oevent_list *oevent_list,
Usz random_seed);

46
sysmisc.c

@ -6,15 +6,15 @@
#include <sys/stat.h>
ORCA_FORCE_NO_INLINE
Cboard_error cboard_copy(Glyph const* gbuffer, Usz field_height,
Cboard_error cboard_copy(Glyph const *gbuffer, Usz field_height,
Usz field_width, Usz rect_y, Usz rect_x, Usz rect_h,
Usz rect_w) {
(void)field_height;
FILE* fp = popen("xclip -i -selection clipboard 2>/dev/null", "w");
FILE *fp = popen("xclip -i -selection clipboard 2>/dev/null", "w");
if (!fp)
return Cboard_error_popen_failed;
for (Usz iy = 0; iy < rect_h; iy++) {
Glyph const* row = gbuffer + (rect_y + iy) * field_width + rect_x;
Glyph const *row = gbuffer + (rect_y + iy) * field_width + rect_x;
fwrite(row, sizeof(Glyph), rect_w, fp);
if (iy + 1 < rect_h)
fputc('\n', fp);
@ -24,9 +24,9 @@ Cboard_error cboard_copy(Glyph const* gbuffer, Usz field_height,
}
ORCA_FORCE_NO_INLINE
Cboard_error cboard_paste(Glyph* gbuffer, Usz height, Usz width, Usz y, Usz x,
Usz* out_h, Usz* out_w) {
FILE* fp = popen("xclip -o -selection clipboard 2>/dev/null", "r");
Cboard_error cboard_paste(Glyph *gbuffer, Usz height, Usz width, Usz y, Usz x,
Usz *out_h, Usz *out_w) {
FILE *fp = popen("xclip -o -selection clipboard 2>/dev/null", "r");
Usz start_y = y, start_x = x, max_y = y, max_x = x;
if (!fp)
return Cboard_error_popen_failed;
@ -60,14 +60,14 @@ Cboard_error cboard_paste(Glyph* gbuffer, Usz height, Usz width, Usz y, Usz x,
}
ORCA_FORCE_NO_INLINE
Conf_read_result conf_read_line(FILE* file, char* buf, Usz bufsize,
char** out_left, Usz* out_leftsize,
char** out_right, Usz* out_rightsize) {
Conf_read_result conf_read_line(FILE *file, char *buf, Usz bufsize,
char **out_left, Usz *out_leftsize,
char **out_right, Usz *out_rightsize) {
// a0 and a1 are the start and end positions of the left side of an "foo=bar"
// pair. b0 and b1 are the positions right side. Leading and trailing spaces
// will be removed.
Usz len, a0, a1, b0, b1;
char* s;
char *s;
if (bufsize < 2)
goto insufficient_buffer;
#if SIZE_MAX > INT_MAX
@ -177,12 +177,12 @@ typedef enum {
Conf_dir_no_home,
} Conf_dir_error;
static char const* const xdg_config_home_env = "XDG_CONFIG_HOME";
static char const* const home_env = "HOME";
static char const* const conf_file_name = "/orca.conf";
static char const *const xdg_config_home_env = "XDG_CONFIG_HOME";
static char const *const home_env = "HOME";
static char const *const conf_file_name = "/orca.conf";
static Conf_dir_error try_get_conf_dir(oso** out) {
char const* xdgcfgdir = getenv(xdg_config_home_env);
static Conf_dir_error try_get_conf_dir(oso **out) {
char const *xdgcfgdir = getenv(xdg_config_home_env);
if (xdgcfgdir) {
Usz xdgcfgdirlen = strlen(xdgcfgdir);
if (xdgcfgdirlen > 0) {
@ -190,7 +190,7 @@ static Conf_dir_error try_get_conf_dir(oso** out) {
return Conf_dir_ok;
}
}
char const* homedir = getenv(home_env);
char const *homedir = getenv(home_env);
if (homedir) {
Usz homedirlen = strlen(homedir);
if (homedirlen > 0) {
@ -201,21 +201,21 @@ static Conf_dir_error try_get_conf_dir(oso** out) {
return Conf_dir_no_home;
}
FILE* conf_file_open_for_reading(void) {
oso* path = NULL;
FILE *conf_file_open_for_reading(void) {
oso *path = NULL;
if (try_get_conf_dir(&path))
return NULL;
osocat(&path, conf_file_name);
if (!path)
return NULL;
FILE* file = fopen(osoc(path), "r");
FILE *file = fopen(osoc(path), "r");
osofree(path);
return file;
}
Conf_save_start_error conf_save_start(Conf_save* p) {
Conf_save_start_error conf_save_start(Conf_save *p) {
memset(p, 0, sizeof(Conf_save));
oso* dir = NULL;
oso *dir = NULL;
Conf_save_start_error err;
if (try_get_conf_dir(&dir)) {
err = Conf_save_start_no_home;
@ -276,7 +276,7 @@ cleanup:
return err;
}
void conf_save_cancel(Conf_save* p) {
void conf_save_cancel(Conf_save *p) {
osofree(p->canonpath);
osofree(p->temppath);
if (p->origfile)
@ -286,7 +286,7 @@ void conf_save_cancel(Conf_save* p) {
memset(p, 0, sizeof(Conf_save));
}
Conf_save_commit_error conf_save_commit(Conf_save* p) {
Conf_save_commit_error conf_save_commit(Conf_save *p) {
Conf_save_commit_error err;
fclose(p->tempfile);
p->tempfile = NULL;

20
sysmisc.h

@ -9,12 +9,12 @@ typedef enum {
Cboard_error_process_exit_error,
} Cboard_error;
Cboard_error cboard_copy(Glyph const* gbuffer, Usz field_height,
Cboard_error cboard_copy(Glyph const *gbuffer, Usz field_height,
Usz field_width, Usz rect_y, Usz rect_x, Usz rect_h,
Usz rect_w);
Cboard_error cboard_paste(Glyph* gbuffer, Usz height, Usz width, Usz y, Usz x,
Usz* out_h, Usz* out_w);
Cboard_error cboard_paste(Glyph *gbuffer, Usz height, Usz width, Usz y, Usz x,
Usz *out_h, Usz *out_w);
typedef enum {
Conf_read_left_and_right = 0, // left and right will be set
@ -24,11 +24,11 @@ typedef enum {
Conf_read_io_error, // "
} Conf_read_result;
Conf_read_result conf_read_line(FILE* file, char* buf, Usz bufsize,
char** out_left, Usz* out_leftlen,
char** out_right, Usz* out_rightlen);
Conf_read_result conf_read_line(FILE *file, char *buf, Usz bufsize,
char **out_left, Usz *out_leftlen,
char **out_right, Usz *out_rightlen);
FILE* conf_file_open_for_reading(void);
FILE *conf_file_open_for_reading(void);
typedef struct {
FILE *origfile, *tempfile;
@ -53,7 +53,7 @@ typedef enum {
Conf_save_commit_rename_failed,
} Conf_save_commit_error;
Conf_save_start_error conf_save_start(Conf_save* p);
Conf_save_start_error conf_save_start(Conf_save *p);
// `*p` may be passed in uninitialized or zeroed -- either is fine. If the
// return value is `Conf_save_start_ok`, then you must call either
// `conf_save_cancel()` or `conf_save_commit()`, otherwise file handles and
@ -65,11 +65,11 @@ Conf_save_start_error conf_save_start(Conf_save* p);
// there to be no existing config file. It might be the first time a config
// file is being written.
void conf_save_cancel(Conf_save* p);
void conf_save_cancel(Conf_save *p);
// Cancels a config save. Closes any file handles and frees any necessary
// strings. Calling with a zeroed `*p` is fine, but don't call it with
// uninitialized data. Afterwards, `*p` will be zeroed.
Conf_save_commit_error conf_save_commit(Conf_save* p);
Conf_save_commit_error conf_save_commit(Conf_save *p);
// Finishes. Do not call this with a zeroed `*p`. Afterwards, `*p` will be
// zeroed.

225
term_util.c

@ -30,7 +30,8 @@ void term_util_init_colors() {
}
#define ORCA_CONTAINER_OF(ptr, type, member) \
((type*)((char*)(1 ? (ptr) : &((type*)0)->member) - offsetof(type, member)))
((type *)((char *)(1 ? (ptr) : &((type *)0)->member) - \
offsetof(type, member)))
struct Qmsg {
Qblock qblock;
@ -44,11 +45,11 @@ struct Qmenu_item_extra {
struct Qmenu {
Qblock qblock;
MENU* ncurses_menu;
ITEM** ncurses_items;
MENU *ncurses_menu;
ITEM **ncurses_items;
Usz items_count;
Usz items_cap;
ITEM* initial_item;
ITEM *initial_item;
int id;
// Flag for right-padding hack. Temp until we do our own menus
U8 has_submenu_item : 1;
@ -56,8 +57,8 @@ struct Qmenu {
struct Qform {
Qblock qblock;
FORM* ncurses_form;
FIELD* ncurses_fields[32];
FORM *ncurses_form;
FIELD *ncurses_fields[32];
Usz fields_count;
int id;
};
@ -73,7 +74,7 @@ void qnav_deinit() {
while (qnav_stack.count != 0)
qnav_stack_pop();
}
static ORCA_FORCE_NO_INLINE void qnav_stack_push(Qblock* qb, int height,
static ORCA_FORCE_NO_INLINE void qnav_stack_push(Qblock *qb, int height,
int width) {
#ifndef NDEBUG
for (Usz i = 0; i < qnav_stack.count; ++i) {
@ -83,7 +84,7 @@ static ORCA_FORCE_NO_INLINE void qnav_stack_push(Qblock* qb, int height,
int top = 0, left = 0;
int total_h = height + 2, total_w = width + 2;
if (qnav_stack.count > 0) {
WINDOW* w = qnav_stack.blocks[qnav_stack.count - 1]->outer_window;
WINDOW *w = qnav_stack.blocks[qnav_stack.count - 1]->outer_window;
int prev_y, prev_x, prev_h, prev_w;
getbegyx(w, prev_y, prev_x);
getmaxyx(w, prev_h, prev_w);
@ -123,26 +124,26 @@ static ORCA_FORCE_NO_INLINE void qnav_stack_push(Qblock* qb, int height,
qnav_stack.stack_changed = true;
}
Qblock* qnav_top_block() {
Qblock *qnav_top_block() {
if (qnav_stack.count == 0)
return NULL;
return qnav_stack.blocks[qnav_stack.count - 1];
}
void qblock_init(Qblock* qb, Qblock_type_tag tag) {
void qblock_init(Qblock *qb, Qblock_type_tag tag) {
qb->tag = tag;
qb->outer_window = NULL;
qb->content_window = NULL;
qb->title = NULL;
}
void qmenu_free(Qmenu* qm);
void qform_free(Qform* qf);
void qmenu_free(Qmenu *qm);
void qform_free(Qform *qf);
void qnav_free_block(Qblock* qb) {
void qnav_free_block(Qblock *qb) {
switch (qb->tag) {
case Qblock_type_qmsg: {
Qmsg* qm = qmsg_of(qb);
Qmsg *qm = qmsg_of(qb);
free(qm);
} break;
case Qblock_type_qmenu: {
@ -158,9 +159,9 @@ void qnav_stack_pop() {
assert(qnav_stack.count > 0);
if (qnav_stack.count == 0)
return;
Qblock* qb = qnav_stack.blocks[qnav_stack.count - 1];
WINDOW* content_window = qb->content_window;
WINDOW* outer_window = qb->outer_window;
Qblock *qb = qnav_stack.blocks[qnav_stack.count - 1];
WINDOW *content_window = qb->content_window;
WINDOW *outer_window = qb->outer_window;
// erase any stuff underneath where this window is, in case it's outside of
// the grid in an area that isn't actively redraw
werase(outer_window);
@ -173,13 +174,13 @@ void qnav_stack_pop() {
qnav_stack.stack_changed = true;
}
void qblock_print_border(Qblock* qb, unsigned int attr) {
void qblock_print_border(Qblock *qb, unsigned int attr) {
wborder(qb->outer_window, ACS_VLINE | attr, ACS_VLINE | attr,
ACS_HLINE | attr, ACS_HLINE | attr, ACS_ULCORNER | attr,
ACS_URCORNER | attr, ACS_LLCORNER | attr, ACS_LRCORNER | attr);
}
void qblock_print_title(Qblock* qb, char const* title, int attr) {
void qblock_print_title(Qblock *qb, char const *title, int attr) {
wmove(qb->outer_window, 0, 1);
attr_t attrs = A_NORMAL;
short pair = 0;
@ -191,41 +192,41 @@ void qblock_print_title(Qblock* qb, char const* title, int attr) {
wattr_set(qb->outer_window, attrs, pair, NULL);
}
void qblock_set_title(Qblock* qb, char const* title) { qb->title = title; }
void qblock_set_title(Qblock *qb, char const *title) { qb->title = title; }
void qblock_print_frame(Qblock* qb, bool active) {
void qblock_print_frame(Qblock *qb, bool active) {
qblock_print_border(qb, active ? A_NORMAL : A_DIM);
if (qb->title) {
qblock_print_title(qb, qb->title, active ? A_NORMAL : A_DIM);
}
if (qb->tag == Qblock_type_qform) {
Qform* qf = qform_of(qb);
Qform *qf = qform_of(qb);
if (qf->ncurses_form) {
pos_form_cursor(qf->ncurses_form);
}
}
}
WINDOW* qmsg_window(Qmsg* qm) { return qm->qblock.content_window; }
WINDOW *qmsg_window(Qmsg *qm) { return qm->qblock.content_window; }
void qmsg_set_title(Qmsg* qm, char const* title) {
void qmsg_set_title(Qmsg *qm, char const *title) {
qblock_set_title(&qm->qblock, title);
}
Qmsg* qmsg_push(int height, int width) {
Qmsg* qm = malloc(sizeof(Qmsg));
Qmsg *qmsg_push(int height, int width) {
Qmsg *qm = malloc(sizeof(Qmsg));
qblock_init(&qm->qblock, Qblock_type_qmsg);
qnav_stack_push(&qm->qblock, height, width);
return qm;
}
void qmsg_printf_push(char const* title, char const* fmt, ...) {
void qmsg_printf_push(char const *title, char const *fmt, ...) {
int titlewidth = title ? (int)strlen(title) : 0;
va_list ap;
va_start(ap, fmt);
int msgbytes = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
char* buffer = malloc((Usz)msgbytes + 1);
char *buffer = malloc((Usz)msgbytes + 1);
if (!buffer)
exit(1);
va_start(ap, fmt);
@ -251,8 +252,8 @@ void qmsg_printf_push(char const* title, char const* fmt, ...) {
maxlinewidth = curlinewidth;
int width = titlewidth > maxlinewidth ? titlewidth : maxlinewidth;
width += 2; // 1 padding on left and right each
Qmsg* msg = qmsg_push(lines, width); // no wrapping yet, no real wcwidth, etc
WINDOW* msgw = qmsg_window(msg);
Qmsg *msg = qmsg_push(lines, width); // no wrapping yet, no real wcwidth, etc
WINDOW *msgw = qmsg_window(msg);
int i = 0;
int offset = 0;
for (;;) {
@ -269,7 +270,7 @@ void qmsg_printf_push(char const* title, char const* fmt, ...) {
qmsg_set_title(msg, title);
}
bool qmsg_drive(Qmsg* qm, int key) {
bool qmsg_drive(Qmsg *qm, int key) {
(void)qm;
switch (key) {
case ' ':
@ -281,10 +282,10 @@ bool qmsg_drive(Qmsg* qm, int key) {
return false;
}
Qmsg* qmsg_of(Qblock* qb) { return ORCA_CONTAINER_OF(qb, Qmsg, qblock); }
Qmsg *qmsg_of(Qblock *qb) { return ORCA_CONTAINER_OF(qb, Qmsg, qblock); }
Qmenu* qmenu_create(int id) {
Qmenu* qm = (Qmenu*)malloc(sizeof(Qmenu));
Qmenu *qmenu_create(int id) {
Qmenu *qm = (Qmenu *)malloc(sizeof(Qmenu));
qblock_init(&qm->qblock, Qblock_type_qmenu);
qm->ncurses_menu = NULL;
qm->ncurses_items = NULL;
@ -295,90 +296,90 @@ Qmenu* qmenu_create(int id) {
qm->has_submenu_item = 0;
return qm;
}
void qmenu_destroy(Qmenu* qm) { qmenu_free(qm); }
int qmenu_id(Qmenu const* qm) { return qm->id; }
void qmenu_destroy(Qmenu *qm) { qmenu_free(qm); }
int qmenu_id(Qmenu const *qm) { return qm->id; }
static ORCA_FORCE_NO_INLINE void
qmenu_allocitems(Qmenu* qm, Usz count, Usz* out_idx, ITEM*** out_items,
struct Qmenu_item_extra** out_extras) {
qmenu_allocitems(Qmenu *qm, Usz count, Usz *out_idx, ITEM ***out_items,
struct Qmenu_item_extra **out_extras) {
Usz old_count = qm->items_count;
// Add 1 for the extra null terminator guy
Usz new_count = old_count + count + 1;
Usz items_cap = qm->items_cap;
ITEM** items = qm->ncurses_items;
ITEM **items = qm->ncurses_items;
if (new_count > items_cap) {
// todo overflow check, realloc fail check
Usz old_cap = items_cap;
Usz new_cap = new_count < 32 ? 32 : orca_round_up_power2(new_count);
Usz new_size = new_cap * (sizeof(ITEM*) + sizeof(struct Qmenu_item_extra));
ITEM** new_items = (ITEM**)realloc(items, new_size);
Usz new_size = new_cap * (sizeof(ITEM *) + sizeof(struct Qmenu_item_extra));
ITEM **new_items = (ITEM **)realloc(items, new_size);
if (!new_items)
exit(1);
items = new_items;
items_cap = new_cap;
// Move old extras data to new position
Usz old_extras_offset = sizeof(ITEM*) * old_cap;
Usz new_extras_offset = sizeof(ITEM*) * new_cap;
Usz old_extras_offset = sizeof(ITEM *) * old_cap;
Usz new_extras_offset = sizeof(ITEM *) * new_cap;
Usz old_extras_size = sizeof(struct Qmenu_item_extra) * old_count;
memmove((char*)items + new_extras_offset, (char*)items + old_extras_offset,
old_extras_size);
memmove((char *)items + new_extras_offset,
(char *)items + old_extras_offset, old_extras_size);
qm->ncurses_items = new_items;
qm->items_cap = new_cap;
}
// Not using new_count here in order to leave an extra 1 for the null
// terminator as required by ncurses.
qm->items_count = old_count + count;
Usz extras_offset = sizeof(ITEM*) * items_cap;
Usz extras_offset = sizeof(ITEM *) * items_cap;
*out_idx = old_count;
*out_items = items + old_count;
*out_extras =
(struct Qmenu_item_extra*)((char*)items + extras_offset) + old_count;
(struct Qmenu_item_extra *)((char *)items + extras_offset) + old_count;
}
ORCA_FORCE_STATIC_INLINE struct Qmenu_item_extra*
qmenu_item_extras_ptr(Qmenu* qm) {
Usz offset = sizeof(ITEM*) * qm->items_cap;
return (struct Qmenu_item_extra*)((char*)qm->ncurses_items + offset);
ORCA_FORCE_STATIC_INLINE struct Qmenu_item_extra *
qmenu_item_extras_ptr(Qmenu *qm) {
Usz offset = sizeof(ITEM *) * qm->items_cap;
return (struct Qmenu_item_extra *)((char *)qm->ncurses_items + offset);
}
// Get the curses menu item user pointer out, turn it to an int, and use it as
// an index into the 'extras' arrays.
ORCA_FORCE_STATIC_INLINE
struct Qmenu_item_extra* qmenu_itemextra(struct Qmenu_item_extra* extras,
ITEM* item) {
struct Qmenu_item_extra *qmenu_itemextra(struct Qmenu_item_extra *extras,
ITEM *item) {
return extras + (int)(intptr_t)(item_userptr(item));
}
void qmenu_set_title(Qmenu* qm, char const* title) {
void qmenu_set_title(Qmenu *qm, char const *title) {
qblock_set_title(&qm->qblock, title);
}
void qmenu_add_choice(Qmenu* qm, int id, char const* text) {
void qmenu_add_choice(Qmenu *qm, int id, char const *text) {
assert(id != 0);
Usz idx;
ITEM** items;
struct Qmenu_item_extra* extras;
ITEM **items;
struct Qmenu_item_extra *extras;
qmenu_allocitems(qm, 1, &idx, &items, &extras);
items[0] = new_item(text, NULL);
set_item_userptr(items[0], (void*)(uintptr_t)idx);
set_item_userptr(items[0], (void *)(uintptr_t)idx);
extras[0].user_id = id;
extras[0].owns_string = false;
extras[0].is_spacer = false;
}
void qmenu_add_submenu(Qmenu* qm, int id, char const* text) {
void qmenu_add_submenu(Qmenu *qm, int id, char const *text) {
assert(id != 0);
qm->has_submenu_item = true; // don't add +1 right padding to subwindow
Usz idx;
ITEM** items;
struct Qmenu_item_extra* extras;
ITEM **items;
struct Qmenu_item_extra *extras;
qmenu_allocitems(qm, 1, &idx, &items, &extras);
items[0] = new_item(text, ">");
set_item_userptr(items[0], (void*)(uintptr_t)idx);
set_item_userptr(items[0], (void *)(uintptr_t)idx);
extras[0].user_id = id;
extras[0].owns_string = false;
extras[0].is_spacer = false;
}
void qmenu_add_printf(Qmenu* qm, int id, char const* fmt, ...) {
void qmenu_add_printf(Qmenu *qm, int id, char const *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int textsize = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
char* buffer = malloc((Usz)textsize + 1);
char *buffer = malloc((Usz)textsize + 1);
if (!buffer)
exit(1);
va_start(ap, fmt);
@ -387,33 +388,33 @@ void qmenu_add_printf(Qmenu* qm, int id, char const* fmt, ...) {
if (printedsize != textsize)
exit(1); // todo better handling?
Usz idx;
ITEM** items;
struct Qmenu_item_extra* extras;
ITEM **items;
struct Qmenu_item_extra *extras;
qmenu_allocitems(qm, 1, &idx, &items, &extras);
items[0] = new_item(buffer, NULL);
set_item_userptr(items[0], (void*)(uintptr_t)idx);
set_item_userptr(items[0], (void *)(uintptr_t)idx);
extras[0].user_id = id;
extras[0].owns_string = true;
extras[0].is_spacer = false;
}
void qmenu_add_spacer(Qmenu* qm) {
void qmenu_add_spacer(Qmenu *qm) {
Usz idx;
ITEM** items;
struct Qmenu_item_extra* extras;
ITEM **items;
struct Qmenu_item_extra *extras;
qmenu_allocitems(qm, 1, &idx, &items, &extras);
items[0] = new_item(" ", NULL);
item_opts_off(items[0], O_SELECTABLE);
set_item_userptr(items[0], (void*)(uintptr_t)idx);
set_item_userptr(items[0], (void *)(uintptr_t)idx);
extras[0].user_id = 0;
extras[0].owns_string = false;
extras[0].is_spacer = true;
}
void qmenu_set_current_item(Qmenu* qm, int id) {
ITEM** items = qm->ncurses_items;
struct Qmenu_item_extra* extras = qmenu_item_extras_ptr(qm);
ITEM* found = NULL;
void qmenu_set_current_item(Qmenu *qm, int id) {
ITEM **items = qm->ncurses_items;
struct Qmenu_item_extra *extras = qmenu_item_extras_ptr(qm);
ITEM *found = NULL;
for (Usz i = 0, n = qm->items_count; i < n; i++) {
ITEM* item = items[i];
ITEM *item = items[i];
if (qmenu_itemextra(extras, item)->user_id == id) {
found = item;
break;
@ -427,13 +428,13 @@ void qmenu_set_current_item(Qmenu* qm, int id) {
qm->initial_item = found;
}
}
void qmenu_set_displayed_active(Qmenu* qm, bool active) {
void qmenu_set_displayed_active(Qmenu *qm, bool active) {
// Could add a flag in the Qmenu to avoid redundantly changing this stuff.
set_menu_fore(qm->ncurses_menu, active ? A_BOLD : A_DIM);
set_menu_back(qm->ncurses_menu, active ? A_NORMAL : A_DIM);
set_menu_grey(qm->ncurses_menu, active ? A_DIM : A_DIM);
}
void qmenu_push_to_nav(Qmenu* qm) {
void qmenu_push_to_nav(Qmenu *qm) {
// new_menu() will get angry if there are no items in the menu. We'll get a
// null pointer back, and our code will get angry. Instead, just add an empty
// spacer item. This will probably only ever occur as a programming error,
@ -474,36 +475,36 @@ void qmenu_push_to_nav(Qmenu* qm) {
post_menu(qm->ncurses_menu);
}
void qmenu_free(Qmenu* qm) {
void qmenu_free(Qmenu *qm) {
unpost_menu(qm->ncurses_menu);
free_menu(qm->ncurses_menu);
struct Qmenu_item_extra* extras = qmenu_item_extras_ptr(qm);
struct Qmenu_item_extra *extras = qmenu_item_extras_ptr(qm);
for (Usz i = 0; i < qm->items_count; ++i) {
ITEM* item = qm->ncurses_items[i];
struct Qmenu_item_extra* extra = qmenu_itemextra(extras, item);
char const* freed_str = NULL;
ITEM *item = qm->ncurses_items[i];
struct Qmenu_item_extra *extra = qmenu_itemextra(extras, item);
char const *freed_str = NULL;
if (extra->owns_string)
freed_str = item_name(item);
free_item(qm->ncurses_items[i]);
if (freed_str)
free((void*)freed_str);
free((void *)freed_str);
}
free(qm->ncurses_items);
free(qm);
}
ORCA_FORCE_NO_INLINE
static void qmenu_drive_upordown(Qmenu* qm, int req_up_or_down) {
struct Qmenu_item_extra* extras = qmenu_item_extras_ptr(qm);
ITEM* starting = current_item(qm->ncurses_menu);
static void qmenu_drive_upordown(Qmenu *qm, int req_up_or_down) {
struct Qmenu_item_extra *extras = qmenu_item_extras_ptr(qm);
ITEM *starting = current_item(qm->ncurses_menu);
menu_driver(qm->ncurses_menu, req_up_or_down);
ITEM* cur = current_item(qm->ncurses_menu);
ITEM *cur = current_item(qm->ncurses_menu);
for (;;) {
if (!cur || cur == starting)
break;
if (!qmenu_itemextra(extras, cur)->is_spacer)
break;
ITEM* prev = cur;
ITEM *prev = cur;
menu_driver(qm->ncurses_menu, req_up_or_down);
cur = current_item(qm->ncurses_menu);
if (cur == prev)
@ -511,8 +512,8 @@ static void qmenu_drive_upordown(Qmenu* qm, int req_up_or_down) {
}
}
bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action) {
struct Qmenu_item_extra* extras = qmenu_item_extras_ptr(qm);
bool qmenu_drive(Qmenu *qm, int key, Qmenu_action *out_action) {
struct Qmenu_item_extra *extras = qmenu_item_extras_ptr(qm);
switch (key) {
case 27: {
out_action->any.type = Qmenu_action_type_canceled;
@ -521,7 +522,7 @@ bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action) {
case ' ':
case '\r':
case KEY_ENTER: {
ITEM* cur = current_item(qm->ncurses_menu);
ITEM *cur = current_item(qm->ncurses_menu);
out_action->picked.type = Qmenu_action_type_picked;
out_action->picked.id = cur ? qmenu_itemextra(extras, cur)->user_id : 0;
return true;
@ -536,20 +537,20 @@ bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action) {
return false;
}
Qmenu* qmenu_of(Qblock* qb) { return ORCA_CONTAINER_OF(qb, Qmenu, qblock); }
Qmenu *qmenu_of(Qblock *qb) { return ORCA_CONTAINER_OF(qb, Qmenu, qblock); }
bool qmenu_top_is_menu(int id) {
Qblock* qb = qnav_top_block();
Qblock *qb = qnav_top_block();
if (!qb)
return false;
if (qb->tag != Qblock_type_qmenu)
return false;
Qmenu* qm = qmenu_of(qb);
Qmenu *qm = qmenu_of(qb);
return qm->id == id;
}
Qform* qform_create(int id) {
Qform* qf = (Qform*)malloc(sizeof(Qform));
Qform *qform_create(int id) {
Qform *qf = (Qform *)malloc(sizeof(Qform));
qblock_init(&qf->qblock, Qblock_type_qform);
qf->ncurses_form = NULL;
qf->ncurses_fields[0] = NULL;
@ -558,22 +559,22 @@ Qform* qform_create(int id) {
return qf;
}
Qform* qform_of(Qblock* qb) { return ORCA_CONTAINER_OF(qb, Qform, qblock); }
Qform *qform_of(Qblock *qb) { return ORCA_CONTAINER_OF(qb, Qform, qblock); }
int qform_id(Qform const* qf) { return qf->id; }
int qform_id(Qform const *qf) { return qf->id; }
void qform_add_text_line(Qform* qf, int id, char const* initial) {
FIELD* f = new_field(1, 30, 0, 0, 0, 0);
void qform_add_text_line(Qform *qf, int id, char const *initial) {
FIELD *f = new_field(1, 30, 0, 0, 0, 0);
if (initial)
set_field_buffer(f, 0, initial);
set_field_userptr(f, (void*)(intptr_t)(id));
set_field_userptr(f, (void *)(intptr_t)(id));
field_opts_off(f, O_WRAP | O_BLANK | O_STATIC);
qf->ncurses_fields[qf->fields_count] = f;
++qf->fields_count;
qf->ncurses_fields[qf->fields_count] = NULL;
}
void qform_push_to_nav(Qform* qf) {
void qform_push_to_nav(Qform *qf) {
qf->ncurses_form = new_form(qf->ncurses_fields);
int form_min_h, form_min_w;
scale_form(qf->ncurses_form, &form_min_h, &form_min_w);
@ -586,11 +587,11 @@ void qform_push_to_nav(Qform* qf) {
form_driver(qf->ncurses_form, REQ_END_LINE);
}
void qform_set_title(Qform* qf, char const* title) {
void qform_set_title(Qform *qf, char const *title) {
qblock_set_title(&qf->qblock, title);
}
void qform_free(Qform* qf) {
void qform_free(Qform *qf) {
curs_set(0);
unpost_form(qf->ncurses_form);
free_form(qf->ncurses_form);
@ -600,7 +601,7 @@ void qform_free(Qform* qf) {
free(qf);
}
bool qform_drive(Qform* qf, int key, Qform_action* out_action) {
bool qform_drive(Qform *qf, int key, Qform_action *out_action) {
switch (key) {
case 27: {
out_action->any.type = Qform_action_type_canceled;
@ -644,7 +645,7 @@ bool qform_drive(Qform* qf, int key, Qform_action* out_action) {
return false;
}
static Usz size_without_trailing_spaces(char const* str) {
static Usz size_without_trailing_spaces(char const *str) {
Usz size = strlen(str);
for (;;) {
if (size == 0)
@ -656,22 +657,22 @@ static Usz size_without_trailing_spaces(char const* str) {
return size;
}
FIELD* qform_find_field(Qform const* qf, int id) {
FIELD *qform_find_field(Qform const *qf, int id) {
Usz count = qf->fields_count;
for (Usz i = 0; i < count; ++i) {
FIELD* f = qf->ncurses_fields[i];
FIELD *f = qf->ncurses_fields[i];
if ((int)(intptr_t)field_userptr(f) == id)
return f;
}
return NULL;
}
bool qform_get_text_line(Qform const* qf, int id, oso** out) {
FIELD* f = qform_find_field(qf, id);
bool qform_get_text_line(Qform const *qf, int id, oso **out) {
FIELD *f = qform_find_field(qf, id);
if (!f)
return false;
form_driver(qf->ncurses_form, REQ_VALIDATION);
char* buf = field_buffer(f, 0);
char *buf = field_buffer(f, 0);
if (!buf)
return false;
Usz trimmed = size_without_trailing_spaces(buf);

68
term_util.h

@ -34,8 +34,8 @@ typedef enum {
A_reverse = A_REVERSE,
} Term_attr;
static ORCA_FORCE_INLINE ORCA_OK_IF_UNUSED
attr_t fg_bg(Color_name fg, Color_name bg) {
static ORCA_FORCE_INLINE ORCA_OK_IF_UNUSED attr_t fg_bg(Color_name fg,
Color_name bg) {
return COLOR_PAIR(1 + fg * Colors_count + bg);
}
@ -49,14 +49,14 @@ typedef enum {
typedef struct {
Qblock_type_tag tag;
WINDOW* outer_window;
WINDOW* content_window;
char const* title;
WINDOW *outer_window;
WINDOW *content_window;
char const *title;
int y, x;
} Qblock;
typedef struct {
Qblock* blocks[16];
Qblock *blocks[16];
Usz count;
bool stack_changed;
} Qnav_stack;
@ -99,50 +99,50 @@ typedef union {
void qnav_init(void);
void qnav_deinit(void);
Qblock* qnav_top_block(void);
Qblock *qnav_top_block(void);
void qnav_stack_pop(void);
void qblock_print_frame(Qblock* qb, bool active);
void qblock_set_title(Qblock* qb, char const* title);
void qblock_print_frame(Qblock *qb, bool active);
void qblock_set_title(Qblock *qb, char const *title);
Qmsg* qmsg_push(int height, int width);
WINDOW* qmsg_window(Qmsg* qm);
void qmsg_set_title(Qmsg* qm, char const* title);
Qmsg *qmsg_push(int height, int width);
WINDOW *qmsg_window(Qmsg *qm);
void qmsg_set_title(Qmsg *qm, char const *title);
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
void qmsg_printf_push(char const* title, char const* fmt, ...);
bool qmsg_drive(Qmsg* qm, int key);
Qmsg* qmsg_of(Qblock* qb);
bool qmsg_drive(Qmsg *qm, int key);
Qmsg *qmsg_of(Qblock *qb);
Qmenu* qmenu_create(int id);
Qmenu *qmenu_create(int id);
// Useful if menu creation needs to be aborted part-way. Otherwise, no need to
// call -- pushing the qmenu to the qnav stack transfers ownership. (Still
// working on this design, not sure yet.)
void qmenu_destroy(Qmenu* qm);
int qmenu_id(Qmenu const* qm);
void qmenu_set_title(Qmenu* qm, char const* title);
void qmenu_add_choice(Qmenu* qm, int id, char const* text);
void qmenu_add_submenu(Qmenu* qm, int id, char const* text);
void qmenu_destroy(Qmenu *qm);
int qmenu_id(Qmenu const *qm);
void qmenu_set_title(Qmenu *qm, char const *title);
void qmenu_add_choice(Qmenu *qm, int id, char const *text);
void qmenu_add_submenu(Qmenu *qm, int id, char const *text);
#ifdef __GNUC__
__attribute__((format(printf, 3, 4)))
#endif
void qmenu_add_printf(Qmenu* qm, int id, char const* fmt, ...);
void qmenu_add_spacer(Qmenu* qm);
void qmenu_set_current_item(Qmenu* qm, int id);
void qmenu_set_displayed_active(Qmenu* qm, bool active);
void qmenu_push_to_nav(Qmenu* qm);
bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action);
Qmenu* qmenu_of(Qblock* qb);
void qmenu_add_spacer(Qmenu *qm);
void qmenu_set_current_item(Qmenu *qm, int id);
void qmenu_set_displayed_active(Qmenu *qm, bool active);
void qmenu_push_to_nav(Qmenu *qm);
bool qmenu_drive(Qmenu *qm, int key, Qmenu_action *out_action);
Qmenu *qmenu_of(Qblock *qb);
bool qmenu_top_is_menu(int id);
Qform* qform_create(int id);
int qform_id(Qform const* qf);
Qform* qform_of(Qblock* qb);
void qform_add_text_line(Qform* qf, int id, char const* initial);
void qform_push_to_nav(Qform* qf);
void qform_set_title(Qform* qf, char const* title);
bool qform_drive(Qform* qf, int key, Qform_action* out_action);
bool qform_get_text_line(Qform const* qf, int id, struct oso** out);
Qform *qform_create(int id);
int qform_id(Qform const *qf);
Qform *qform_of(Qblock *qb);
void qform_add_text_line(Qform *qf, int id, char const *initial);
void qform_push_to_nav(Qform *qf);
void qform_set_title(Qform *qf, char const *title);
bool qform_drive(Qform *qf, int key, Qform_action *out_action);
bool qform_get_text_line(Qform const *qf, int id, struct oso **out);
extern Qnav_stack qnav_stack;

354
tui_main.c

File diff suppressed because it is too large
Loading…
Cancel
Save