summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/bindings.c2
-rw-r--r--src/main/cmds.c52
-rw-r--r--src/main/main.c46
3 files changed, 50 insertions, 50 deletions
diff --git a/src/main/bindings.c b/src/main/bindings.c
index 9038f37..b49a45e 100644
--- a/src/main/bindings.c
+++ b/src/main/bindings.c
@@ -49,7 +49,7 @@ void set_default_buffer_bindings(struct keymap *keymap) {
BINDING(ENTER, "newline"),
BINDING(TAB, "indent"),
- BINDING(Spec, 'Z', "insert-tab"),
+ BINDING(Spec, 'Z', "indent-alt"),
BINDING(Ctrl, 'K', "kill-line"),
BINDING(DELETE, "delete-char"),
diff --git a/src/main/cmds.c b/src/main/cmds.c
index b454889..2853352 100644
--- a/src/main/cmds.c
+++ b/src/main/cmds.c
@@ -13,6 +13,7 @@
#include "dged/minibuffer.h"
#include "dged/path.h"
#include "dged/settings.h"
+#include "dged/timers.h"
#include "dged/utf8.h"
#include "bindings.h"
@@ -123,17 +124,38 @@ int32_t switch_buffer(struct command_ctx ctx, int argc, const char *argv[]) {
ctx.active_window, ctx.buffers, argc, argv);
}
+void timer_to_list_line(const struct timer *timer, void *userdata) {
+ struct buffer *target = (struct buffer *)userdata;
+
+ static char buf[128];
+ const char *name = timer_name(timer);
+ size_t namelen = strlen(name);
+ size_t len =
+ snprintf(buf, 128, "%s - %.2f ms", name, (timer_average(timer) / 1e6));
+ buffer_add(target, buffer_end(target), (uint8_t *)buf, len);
+}
+
+void timers_refresh(struct buffer *buffer, void *userdata) {
+ buffer_set_readonly(buffer, false);
+ buffer_clear(buffer);
+ timers_for_each(timer_to_list_line, buffer);
+ uint32_t nlines = buffer_num_lines(buffer);
+ if (nlines > 0) {
+ buffer_sort_lines(buffer, 0, nlines);
+ }
+ buffer_set_readonly(buffer, true);
+}
+
int32_t timers(struct command_ctx ctx, int argc, const char *argv[]) {
- struct buffer *b = buffers_add(ctx.buffers, buffer_create("timers"));
- buffer_set_readonly(b, true);
- struct window *new_window_a, *new_window_b;
- window_split(ctx.active_window, &new_window_a, &new_window_b);
+ struct buffer *b = buffers_find(ctx.buffers, "*timers*");
+ if (b == NULL) {
+ b = buffers_add(ctx.buffers, buffer_create("*timers*"));
+ buffer_add_update_hook(b, timers_refresh, NULL);
+ }
- const char *txt =
- "TODO: this is not real values!\ntimer 1: 1ms\ntimer 2: 2ms\n";
- buffer_set_text(b, (uint8_t *)txt, strlen(txt));
+ window_set_buffer(ctx.active_window, b);
+ timers_refresh(b, NULL);
- window_set_buffer(new_window_b, b);
return 0;
}
@@ -223,12 +245,12 @@ int32_t buffer_list(struct command_ctx ctx, int argc, const char *argv[]) {
struct buffer *b = buffers_find(ctx.buffers, "*buffers*");
if (b == NULL) {
b = buffers_add(ctx.buffers, buffer_create("*buffers*"));
+ buffer_add_update_hook(b, buflist_refresh, ctx.buffers);
}
struct window *w = ctx.active_window;
window_set_buffer(ctx.active_window, b);
- buffer_add_update_hook(b, buflist_refresh, ctx.buffers);
buflist_refresh(b, ctx.buffers);
static struct command buflist_visit = {
@@ -415,6 +437,7 @@ BUFFER_VIEW_WRAPCMD(goto_end_of_line);
BUFFER_VIEW_WRAPCMD(goto_beginning_of_line);
BUFFER_VIEW_WRAPCMD(newline);
BUFFER_VIEW_WRAPCMD(indent);
+BUFFER_VIEW_WRAPCMD(indent_alt);
BUFFER_VIEW_WRAPCMD(set_mark);
BUFFER_VIEW_WRAPCMD(clear_mark);
BUFFER_VIEW_WRAPCMD(copy);
@@ -424,6 +447,7 @@ BUFFER_VIEW_WRAPCMD(paste_older);
BUFFER_VIEW_WRAPCMD(goto_beginning);
BUFFER_VIEW_WRAPCMD(goto_end);
BUFFER_VIEW_WRAPCMD(undo);
+BUFFER_VIEW_WRAPCMD(sort_lines);
static int32_t scroll_up_cmd(struct command_ctx ctx, int argc,
const char *argv[]) {
@@ -464,13 +488,6 @@ static int32_t goto_line(struct command_ctx ctx, int argc, const char *argv[]) {
return 0;
}
-static int32_t insert_tab(struct command_ctx ctx, int argc,
- const char *argv[]) {
- struct buffer_view *v = window_buffer_view(ctx.active_window);
- buffer_view_add(v, (uint8_t *)"\t", 1);
- return 0;
-}
-
void register_buffer_commands(struct commands *commands) {
static struct command buffer_commands[] = {
{.name = "kill-line", .fn = kill_line_cmd},
@@ -487,7 +504,7 @@ void register_buffer_commands(struct commands *commands) {
{.name = "beginning-of-line", .fn = goto_beginning_of_line_cmd},
{.name = "newline", .fn = newline_cmd},
{.name = "indent", .fn = indent_cmd},
- {.name = "insert-tab", .fn = insert_tab},
+ {.name = "indent-alt", .fn = indent_alt_cmd},
{.name = "buffer-write-to-file", .fn = to_file_cmd},
{.name = "set-mark", .fn = set_mark_cmd},
{.name = "clear-mark", .fn = clear_mark_cmd},
@@ -502,6 +519,7 @@ void register_buffer_commands(struct commands *commands) {
{.name = "scroll-up", .fn = scroll_up_cmd},
{.name = "reload", .fn = reload_cmd},
{.name = "goto-line", .fn = goto_line},
+ {.name = "sort-lines", .fn = sort_lines_cmd},
};
register_commands(commands, buffer_commands,
diff --git a/src/main/main.c b/src/main/main.c
index e722ed2..d0d118b 100644
--- a/src/main/main.c
+++ b/src/main/main.c
@@ -19,6 +19,7 @@
#include "dged/path.h"
#include "dged/reactor.h"
#include "dged/settings.h"
+#include "dged/timers.h"
#ifdef SYNTAX_ENABLE
#include "dged/syntax.h"
@@ -52,24 +53,6 @@ void resized() {
signal(SIGWINCH, resized);
}
-uint64_t calc_frame_time_ns(struct timespec *timers, uint32_t num_timer_pairs) {
- uint64_t total = 0;
- for (uint32_t ti = 0; ti < num_timer_pairs * 2; ti += 2) {
- struct timespec *start_timer = &timers[ti];
- struct timespec *end_timer = &timers[ti + 1];
-
- total +=
- ((uint64_t)end_timer->tv_sec * 1e9 + (uint64_t)end_timer->tv_nsec) -
- ((uint64_t)start_timer->tv_sec * 1e9 + (uint64_t)start_timer->tv_nsec);
- }
-
- return total;
-}
-
-#define DECLARE_TIMER(timer) struct timespec timer##_begin, timer##_end
-#define TIMED_SCOPE_BEGIN(timer) clock_gettime(CLOCK_MONOTONIC, &timer##_begin)
-#define TIMED_SCOPE_END(timer) clock_gettime(CLOCK_MONOTONIC, &timer##_end)
-
#define INVALID_WATCH -1
struct watched_file {
uint32_t watch_id;
@@ -297,32 +280,30 @@ int main(int argc, char *argv[]) {
init_bindings();
init_completion(&buflist);
+ timers_init();
- DECLARE_TIMER(buffer);
- DECLARE_TIMER(display);
- DECLARE_TIMER(keyboard);
-
- uint64_t frame_time = 0;
+ float frame_time = 0.f;
static char keyname[64] = {0};
static uint32_t nkeychars = 0;
while (running) {
+ timers_start_frame();
if (display_resized) {
windows_resize(display_height(display), display_width(display));
display_resized = false;
}
/* Update all windows together with the buffers in them. */
- TIMED_SCOPE_BEGIN(buffer);
+ struct timer *update_windows = timer_start("update-windows");
windows_update(frame_alloc, frame_time);
- TIMED_SCOPE_END(buffer);
+ timer_stop(update_windows);
struct window *active_window = windows_get_active();
/* Update the screen by flushing command lists collected
* from updating the buffers.
*/
- TIMED_SCOPE_BEGIN(display);
+ struct timer *update_display = timer_start("display");
display_begin_render(display);
windows_render(display);
struct buffer_view *view = window_buffer_view(active_window);
@@ -330,7 +311,7 @@ int main(int argc, char *argv[]) {
struct window_position winpos = window_position(active_window);
display_move_cursor(display, winpos.y + cursor.line, winpos.x + cursor.col);
display_end_render(display);
- TIMED_SCOPE_END(display);
+ timer_stop(update_display);
/* This blocks for events, so if nothing has happened we block here and let
* the CPU do something more useful than updating this editor for no reason.
@@ -339,7 +320,7 @@ int main(int argc, char *argv[]) {
*/
reactor_update(reactor);
- TIMED_SCOPE_BEGIN(keyboard);
+ struct timer *update_keyboard = timer_start("update-keyboard");
struct keyboard_update kbd_upd =
keyboard_update(&kbd, reactor, frame_alloc);
@@ -413,17 +394,18 @@ int main(int argc, char *argv[]) {
keyname[0] = '\0';
}
}
- TIMED_SCOPE_END(keyboard);
+ timer_stop(update_keyboard);
update_file_watches(reactor);
// calculate frame time
- struct timespec timers[] = {buffer_begin, buffer_end, display_begin,
- display_end, keyboard_begin, keyboard_end};
- frame_time = calc_frame_time_ns(timers, 3);
+ frame_time = timer_average(update_windows) +
+ timer_average(update_keyboard) + timer_average(update_display);
+
frame_allocator_clear(&frame_allocator);
}
+ timers_destroy();
destroy_completion();
windows_destroy();
minibuffer_destroy();