aboutsummaryrefslogtreecommitdiff
path: root/menu.h
blob: 4c62d3fae3d0a9bd8a045e93cf922f18822661e2 (plain) (blame)
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
#ifndef WMENU_MENU_H
#define WMENU_MENU_H

#include <xkbcommon/xkbcommon.h>

#include "pool-buffer.h"

// A menu item.
struct item {
	char *text;
	int width;
	struct item *next;       // traverses all items
	struct item *prev_match; // previous matching item
	struct item *next_match; // next matching item
	struct page *page;       // the page holding this item
};

// A page of menu items.
struct page {
	struct item *first; // first item in the page
	struct item *last;  // last item in the page
	struct page *prev;  // previous page
	struct page *next;  // next page
};

// A Wayland output.
struct output {
	struct menu *menu;
	struct wl_output *output;
	const char *name;    // output name
	int32_t scale;       // output scale
	struct output *next; // next output
};

// Keyboard state.
struct keyboard {
	struct menu *menu;
	struct wl_keyboard *keyboard;
	struct xkb_context *context;
	struct xkb_keymap *keymap;
	struct xkb_state *state;

	int repeat_timer;
	int repeat_delay;
	int repeat_period;
	enum wl_keyboard_key_state repeat_key_state;
	xkb_keysym_t repeat_sym;
};

// Menu state.
struct menu {
	// Whether the menu appears at the bottom of the screen
	bool bottom;
	// The function used to match menu items
	int (*strncmp)(const char *, const char *, size_t);
	// The font used to display the menu
	char *font;
	// The number of lines to list items vertically
	int lines;
	// The name of the output to display on
	char *output_name;
	// The prompt displayed to the left of the input field
	char *prompt;
	// Normal colors
	uint32_t normalbg, normalfg;
	// Prompt colors
	uint32_t promptbg, promptfg;
	// Selection colors
	uint32_t selectionbg, selectionfg;

	struct wl_display *display;
	struct wl_registry *registry;
	struct wl_compositor *compositor;
	struct wl_shm *shm;
	struct wl_seat *seat;
	struct wl_data_device_manager *data_device_manager;
	struct zwlr_layer_shell_v1 *layer_shell;
	struct output *output_list;

	struct keyboard *keyboard;
	struct wl_data_device *data_device;
	struct wl_surface *surface;
	struct zwlr_layer_surface_v1 *layer_surface;
	struct wl_data_offer *data_offer;
	struct output *output;

	struct pool_buffer buffers[2];
	struct pool_buffer *current;

	int width;
	int height;
	int line_height;
	int padding;
	int inputw;
	int promptw;
	int left_arrow;
	int right_arrow;

	char input[BUFSIZ];
	size_t cursor;

	struct item *items;       // list of all items
	struct item *matches;     // list of matching items
	struct item *matches_end; // last matching item
	struct item *sel;         // selected item
	struct page *pages;       // list of pages

	bool exit;
	bool failure;
};

struct menu *menu_create();
struct keyboard *keyboard_create(struct menu *menu, struct wl_keyboard *wl_keyboard);
void menu_set_keyboard(struct menu *menu, struct keyboard *keyboard);
struct output *output_create(struct menu *menu, struct wl_output *wl_output);
void menu_add_output(struct menu *menu, struct output *output);
void menu_getopts(struct menu *menu, int argc, char *argv[]);
void read_menu_items(struct menu *menu);
void menu_keypress(struct menu *menu, enum wl_keyboard_key_state key_state,
		xkb_keysym_t sym);
void menu_destroy(struct menu *menu);

#endif