aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 267fe13ff0a010afa0a8ec14415f05ff7333e3c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
#include <poll.h>
#include <unistd.h>

#include "config.h"
#include "array.h"
#include "state.h"
#include "wayland.h"
#include "graphics.h"
#include "input_field.h"
#include "font.h"
#include "array.h"
#include "args.h"

void poll_events(client_state* state);

void read_stdin(client_state* state) {
	ssize_t len = 0;
	size_t a;
	char* line = NULL;
	while((len = getline(&line, &a, stdin)) != -1) {
		if (len) {
			if (line[len - 1] == '\n') {
				line[len - 1] = '\0';
				len -= 1;
			}
		}
		if (len != 0) {
			array_add(state->items, (item_t){0});
			array_last(state->items).text = line;
		} else {
			free(line);
		}
		line = NULL;
	}
	free(line);
}

int main(int argc, char* argv[]) {
	client_state state = {0};
	state.running = true;
	state.items = array_new(item_t, 0);
	state.input_buffer = array_new(char, 0);
	state.filtered_items = array_new(item_display_t, 0);
	state.selected_filtered_item = -1;
	state.page_indices = array_new(size_t, 0);
	state.strstr = strstr;

	init_conf();

	// check locale
	setlocale(LC_ALL, "");
	const char *encoding = nl_langinfo(CODESET);
	if (strcmp(encoding, "UTF-8") != 0) {
		fprintf(stderr, "Cannot continue because encoding (%s), is not UTF-8.", encoding);
		return EXIT_FAILURE;
	}

	// read input
	parse_args(&state, argc, argv);
	read_stdin(&state);

	// find font
	char* font = get_font("Monospace");
	if (!freetype_init(&state, font)) {
		fprintf(stderr, "Failed to Initalize FreeType\n");
	}
	free(font);

	// create font atlas
	atlas_init(&state);
	atlas_calc_item_widths(&state);

	// start wayland
	state.display = wl_display_connect(NULL);
	if (!state.display) {
		fprintf(stderr, "Failed to Connect to wayland display\n");
		return EXIT_FAILURE;
	}

	// bind globals and roundtrip
	setup_registry_and_globals(&state);
	wl_display_roundtrip(state.display);

	// set up other objects and roundtrip
	setup_seat(&state);
	create_surface(&state);
	wl_display_roundtrip(state.display);

	// set up graphics
	if (!init_gl(&state)) {
		fprintf(stderr, "Failed to Initalize EGL/OpenGL\n");
		return EXIT_FAILURE;
	}

	// create font altas textures
	atlas_create_texture(&state);

	// create text buffers
	if (state.prompt && *state.prompt) {
		init_text_buffer(&state, &state.prompt_text_buffer, state.prompt, strlen(state.prompt));
	}
	init_text_buffer(&state, &state.input_buffer_grafix, "", 0);
	array_for_all(item_t, item, state.items) {
		init_text_buffer(&state, &item->text_buffer, item->text, strlen(item->text));
	}
	filter_items(&state);

	// event loop
	while (state.running) {
		poll_events(&state);
		render_frame(&state);
		wl_display_dispatch(state.display);
	}

	// cleanup (of course)
	if (state.items) {
		array_for_all(item_t, item, state.items) {
			free(item->text);
		}
		array_free(state.items);
	}
	array_free(state.input_buffer);
	array_free(state.filtered_items);
	array_free(state.page_indices);
	if (state.clipboard) free(state.clipboard);
	FT_Done_Face(state.ft_face);
	FT_Done_FreeType(state.ft_library);

	xkb_state_unref(state.xkb_state);
	xkb_keymap_unref(state.xkb_keymap);
	xkb_context_unref(state.xkb_context);

	return state.exit_code;
}

void poll_events(client_state* state) {
	enum { DISPLAY_FD, KEYREPEAT_FD };
	struct pollfd fds[] = {
		[DISPLAY_FD] = { wl_display_get_fd(state->display), POLLIN },
		[KEYREPEAT_FD] = { state->key_repeat_timer_fd, POLLIN },
	};

	bool event = false;
	while (!event) {
		while (wl_display_prepare_read(state->display) != 0) {
			if (wl_display_dispatch_pending(state->display) > 0) {
				return;
			}
		}
		if (poll(fds, sizeof(fds) / sizeof(fds[0]), -1) == -1) {
			wl_display_cancel_read(state->display);
			return;
		}
		if (fds[DISPLAY_FD].revents & POLLIN) {
			wl_display_read_events(state->display);
			if (wl_display_dispatch_pending(state->display) > 0)
				event = true;
		}
		else {
			wl_display_cancel_read(state->display);
		}
		if (fds[KEYREPEAT_FD].revents & POLLIN) {
			uint64_t repeats;
			if (read(state->key_repeat_timer_fd, &repeats, sizeof(repeats)) == 8) {
				for (uint64_t i = 0; i < repeats; i++) {
					type_key(state, state->repeat_key);
				}
				event = true;
			}
		}
	}
}