vtgl

terminal emulator implemented in OpenGL
git clone anongit@rnpnr.xyz:vtgl.git
Log | Files | Refs | Feed | LICENSE

test-common.c (3137B)


      1 /* See LICENSE for copyright details */
      2 #include "vtgl.h"
      3 #include "config.h"
      4 
      5 /* NOTE: stubs for stuff we aren't testing */
      6 function void get_gpu_glyph_index(Arena, void *, void *, u32, u32, u32, CachedGlyph **);
      7 
      8 KEYBIND_FN(copy)   { return 0; }
      9 KEYBIND_FN(paste)  { return 0; }
     10 KEYBIND_FN(scroll) { return 0; }
     11 KEYBIND_FN(zoom)   { return 0; }
     12 
     13 #include "font.c"
     14 #include "terminal.c"
     15 
     16 function OS_WRITE_FN(test_write)
     17 {
     18 	/* NOTE(rnp): for testing the caller will provide a stream via the platform
     19 	 * child handle. Then this function writes into it and the caller can compare
     20 	 * with the expected results */
     21 	Stream *s = (Stream *)file;
     22 	stream_push_s8(s, raw);
     23 	return !s->errors;
     24 }
     25 
     26 function OS_GET_WINDOW_TITLE_FN(test_get_window_title)
     27 {
     28 	return (u8 *)"test_title";
     29 }
     30 
     31 function OS_SET_WINDOW_TITLE_FN(test_set_window_title)
     32 {
     33 	ASSERT(title.len);
     34 }
     35 
     36 function iz
     37 copy_into_ringbuf(OSRingBuffer *rb, s8 raw)
     38 {
     39 	ASSERT(raw.len < rb->capacity);
     40 	mem_copy(rb->data + rb->write_index, raw.data, raw.len);
     41 
     42 	rb->write_index += raw.len;
     43 	rb->filled      += raw.len;
     44 
     45 	CLAMP(rb->filled, 0, rb->capacity);
     46 	if (rb->write_index >= rb->capacity)
     47 		rb->write_index -= rb->capacity;
     48 
     49 	ASSERT(rb->filled >= 0);
     50 	ASSERT(rb->write_index >= 0 && rb->write_index < rb->capacity);
     51 	return raw.len;
     52 }
     53 
     54 function s8
     55 launder_static_string(Term *term, s8 static_str)
     56 {
     57 	OSRingBuffer *rb = &term->views[term->view_idx].log;
     58 	term->unprocessed_bytes += copy_into_ringbuf(rb, static_str);
     59 	s8 raw = {
     60 		.len  = term->unprocessed_bytes,
     61 		.data = rb->data + (rb->write_index - term->unprocessed_bytes)
     62 	};
     63 	return raw;
     64 }
     65 
     66 function Term *
     67 place_term_into_memory(OSMemoryBlock memory, i32 rows, i32 columns)
     68 {
     69 	Arena tmp = arena_from_memory_block(memory);
     70 	Term *t   = push_struct(&tmp, Term);
     71 	t->size   = (iv2){.w = 80, .h = 24};
     72 
     73 	t->os = push_struct(&tmp, typeof(*t->os));
     74 	t->os->set_window_title = test_set_window_title;
     75 	t->os->get_window_title = test_get_window_title;
     76 	t->os->write            = test_write;
     77 
     78 	t->arena_for_frame = tmp;
     79 
     80 	t->views[0].log   = os_allocate_ring_buffer(MB(2));
     81 	t->views[0].lines = line_buffer_alloc(&t->arena_for_frame, t->views[0].log.data,
     82 	                                      t->cursor.style, BACKLOG_LINES);
     83 
     84 	t->views[1].log   = os_allocate_ring_buffer(MB(2));
     85 	t->views[1].lines = line_buffer_alloc(&t->arena_for_frame, t->views[1].log.data,
     86 	                                      t->cursor.style, ALT_BACKLOG_LINES);
     87 
     88 	t->views[0].fb.backing_store = memory_block_from_arena(&t->arena_for_frame, MB(1));
     89 	t->views[1].fb.backing_store = memory_block_from_arena(&t->arena_for_frame, MB(1));
     90 	initialize_framebuffer(&t->views[0].fb, t->size);
     91 	initialize_framebuffer(&t->views[1].fb, t->size);
     92 
     93 	term_reset(t);
     94 
     95 	return t;
     96 }
     97 
     98 /* NOTE(rnp): these work better with ASAN for testing */
     99 /* TODO(rnp): add appropriate poison to normal os_block_allocator when compiling with ASAN */
    100 void *malloc(size_t);
    101 void  free(void *);
    102 
    103 function void
    104 release_term_memory(OSMemoryBlock backing)
    105 {
    106 	Term *t = backing.memory;
    107 	os_release_ring_buffer(&t->views[0].log);
    108 	os_release_ring_buffer(&t->views[1].log);
    109 	free(backing.memory);
    110 }