feat: implement keyboard and mouse input, pattern placement, and HiDPI scaling
Add configurable key bindings (TOML), modifier-aware dispatch, mouse-driven pattern placement with preview and rotation, cell grid storage/rendering, and fractional/integer HiDPI scaling. Archive the input-and-placement change and sync its specs to main specs. Tests: Hashtable, Config, Grid, Pattern (4/4 pass).
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
|
||||
#define MOD_CTRL (1u << 0)
|
||||
#define MOD_SHIFT (1u << 1)
|
||||
#define MOD_ALT (1u << 2)
|
||||
#define MOD_SUPER (1u << 3)
|
||||
|
||||
typedef enum {
|
||||
ACTION_NONE = 0,
|
||||
ACTION_QUIT,
|
||||
ACTION_CANCEL,
|
||||
ACTION_ROTATE,
|
||||
ACTION_SELECT_GLIDER,
|
||||
ACTION_SELECT_BLINKER,
|
||||
ACTION_SELECT_BLOCK,
|
||||
ACTION_SELECT_TOAD,
|
||||
ACTION_CLEAR,
|
||||
ACTION_PAUSE,
|
||||
ACTION_STEP,
|
||||
ACTION_SPEED_UP,
|
||||
ACTION_SPEED_DOWN,
|
||||
ACTION_TOGGLE_GRID,
|
||||
} action_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t mods;
|
||||
xkb_keysym_t keysym;
|
||||
action_t action;
|
||||
} binding_t;
|
||||
|
||||
typedef struct {
|
||||
binding_t *items;
|
||||
int count;
|
||||
int capacity;
|
||||
} config_t;
|
||||
|
||||
/* Built-in defaults. */
|
||||
config_t config_defaults(void);
|
||||
|
||||
/* Ensure config file exists; create with defaults if missing. Returns 0 on success. */
|
||||
int config_ensure_default_file(const char *path);
|
||||
|
||||
/* Load config from path, merging over defaults. */
|
||||
config_t config_load(const char *path);
|
||||
|
||||
void config_free(config_t *cfg);
|
||||
|
||||
/* Lookup action for a key press. Returns ACTION_NONE if no binding. */
|
||||
action_t config_lookup(const config_t *cfg, uint32_t mods, xkb_keysym_t keysym);
|
||||
|
||||
/* Helpers exposed for tests. */
|
||||
int config_parse_mods(const char *s, uint32_t *out_mods, char **out_key);
|
||||
action_t config_parse_action(const char *s);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef GRID_H
|
||||
#define GRID_H
|
||||
|
||||
typedef struct {
|
||||
int cols;
|
||||
int rows;
|
||||
unsigned char *cells;
|
||||
int show_lines;
|
||||
} grid_t;
|
||||
|
||||
void grid_init(grid_t *g, int cols, int rows);
|
||||
void grid_free(grid_t *g);
|
||||
void grid_resize(grid_t *g, int new_cols, int new_rows);
|
||||
void grid_clear(grid_t *g);
|
||||
int grid_get(const grid_t *g, int c, int r);
|
||||
void grid_set(grid_t *g, int c, int r, int val);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef PATTERN_H
|
||||
#define PATTERN_H
|
||||
|
||||
typedef struct { int dx, dy; } cell_offset_t;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
int count;
|
||||
const cell_offset_t *cells;
|
||||
} pattern_t;
|
||||
|
||||
int pattern_count(void);
|
||||
const pattern_t *pattern_get(int index);
|
||||
|
||||
/* Fill out_dx/out_dy with normalized rotated offsets. Returns cell count. */
|
||||
int pattern_rotated_offsets(int index, int rotation, int *out_dx, int *out_dy);
|
||||
|
||||
/* Bounding box of rotated pattern. */
|
||||
void pattern_bounds(int index, int rotation, int *w, int *h);
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
#include <wayland-client.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
|
||||
typedef void (*keyboard_key_handler_t)(uint32_t key, uint32_t state, xkb_keysym_t sym, void *user_data);
|
||||
typedef void (*keyboard_key_handler_t)(uint32_t key, uint32_t state, xkb_keysym_t sym, uint32_t mods, void *user_data);
|
||||
|
||||
struct keyboard_data {
|
||||
struct wl_keyboard *keyboard;
|
||||
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) CK Tan
|
||||
https://github.com/cktan/tomlc99
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef TOML_H
|
||||
#define TOML_H
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define TOML_EXTERN extern "C"
|
||||
#else
|
||||
#define TOML_EXTERN extern
|
||||
#endif
|
||||
|
||||
typedef struct toml_timestamp_t toml_timestamp_t;
|
||||
typedef struct toml_table_t toml_table_t;
|
||||
typedef struct toml_array_t toml_array_t;
|
||||
typedef struct toml_datum_t toml_datum_t;
|
||||
|
||||
/* Parse a file. Return a table on success, or 0 otherwise.
|
||||
* Caller must toml_free(the-return-value) after use.
|
||||
*/
|
||||
TOML_EXTERN toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz);
|
||||
|
||||
/* Parse a string containing the full config.
|
||||
* Return a table on success, or 0 otherwise.
|
||||
* Caller must toml_free(the-return-value) after use.
|
||||
*/
|
||||
TOML_EXTERN toml_table_t *toml_parse(char *conf, /* NUL terminated, please. */
|
||||
char *errbuf, int errbufsz);
|
||||
|
||||
/* Free the table returned by toml_parse() or toml_parse_file(). Once
|
||||
* this function is called, any handles accessed through this tab
|
||||
* directly or indirectly are no longer valid.
|
||||
*/
|
||||
TOML_EXTERN void toml_free(toml_table_t *tab);
|
||||
|
||||
/* Timestamp types. The year, month, day, hour, minute, second, z
|
||||
* fields may be NULL if they are not relevant. e.g. In a DATE
|
||||
* type, the hour, minute, second and z fields will be NULLs.
|
||||
*/
|
||||
struct toml_timestamp_t {
|
||||
struct { /* internal. do not use. */
|
||||
int year, month, day;
|
||||
int hour, minute, second, millisec;
|
||||
char z[10];
|
||||
} __buffer;
|
||||
int *year, *month, *day;
|
||||
int *hour, *minute, *second, *millisec;
|
||||
char *z;
|
||||
};
|
||||
|
||||
/*-----------------------------------------------------------------
|
||||
* Enhanced access methods
|
||||
*/
|
||||
struct toml_datum_t {
|
||||
int ok;
|
||||
union {
|
||||
toml_timestamp_t *ts; /* ts must be freed after use */
|
||||
char *s; /* string value. s must be freed after use */
|
||||
int b; /* bool value */
|
||||
int64_t i; /* int value */
|
||||
double d; /* double value */
|
||||
} u;
|
||||
};
|
||||
|
||||
/* on arrays: */
|
||||
/* ... retrieve size of array. */
|
||||
TOML_EXTERN int toml_array_nelem(const toml_array_t *arr);
|
||||
/* ... retrieve values using index. */
|
||||
TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t *arr, int idx);
|
||||
TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t *arr, int idx);
|
||||
TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t *arr, int idx);
|
||||
TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t *arr, int idx);
|
||||
TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx);
|
||||
/* ... retrieve array or table using index. */
|
||||
TOML_EXTERN toml_array_t *toml_array_at(const toml_array_t *arr, int idx);
|
||||
TOML_EXTERN toml_table_t *toml_table_at(const toml_array_t *arr, int idx);
|
||||
|
||||
/* on tables: */
|
||||
/* ... retrieve the key in table at keyidx. Return 0 if out of range. */
|
||||
TOML_EXTERN const char *toml_key_in(const toml_table_t *tab, int keyidx);
|
||||
/* ... returns 1 if key exists in tab, 0 otherwise */
|
||||
TOML_EXTERN int toml_key_exists(const toml_table_t *tab, const char *key);
|
||||
/* ... retrieve values using key. */
|
||||
TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr,
|
||||
const char *key);
|
||||
TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key);
|
||||
TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key);
|
||||
TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t *arr,
|
||||
const char *key);
|
||||
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t *arr,
|
||||
const char *key);
|
||||
/* .. retrieve array or table using key. */
|
||||
TOML_EXTERN toml_array_t *toml_array_in(const toml_table_t *tab,
|
||||
const char *key);
|
||||
TOML_EXTERN toml_table_t *toml_table_in(const toml_table_t *tab,
|
||||
const char *key);
|
||||
|
||||
/*-----------------------------------------------------------------
|
||||
* lesser used
|
||||
*/
|
||||
/* Return the array kind: 't'able, 'a'rray, 'v'alue, 'm'ixed */
|
||||
TOML_EXTERN char toml_array_kind(const toml_array_t *arr);
|
||||
|
||||
/* For array kind 'v'alue, return the type of values
|
||||
i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed
|
||||
0 if unknown
|
||||
*/
|
||||
TOML_EXTERN char toml_array_type(const toml_array_t *arr);
|
||||
|
||||
/* Return the key of an array */
|
||||
TOML_EXTERN const char *toml_array_key(const toml_array_t *arr);
|
||||
|
||||
/* Return the number of key-values in a table */
|
||||
TOML_EXTERN int toml_table_nkval(const toml_table_t *tab);
|
||||
|
||||
/* Return the number of arrays in a table */
|
||||
TOML_EXTERN int toml_table_narr(const toml_table_t *tab);
|
||||
|
||||
/* Return the number of sub-tables in a table */
|
||||
TOML_EXTERN int toml_table_ntab(const toml_table_t *tab);
|
||||
|
||||
/* Return the key of a table*/
|
||||
TOML_EXTERN const char *toml_table_key(const toml_table_t *tab);
|
||||
|
||||
/*--------------------------------------------------------------
|
||||
* misc
|
||||
*/
|
||||
TOML_EXTERN int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret);
|
||||
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
|
||||
TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t),
|
||||
void (*xxfree)(void *));
|
||||
|
||||
/*--------------------------------------------------------------
|
||||
* deprecated
|
||||
*/
|
||||
/* A raw value, must be processed by toml_rto* before using. */
|
||||
typedef const char *toml_raw_t;
|
||||
TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key);
|
||||
TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx);
|
||||
TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret);
|
||||
TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret);
|
||||
TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret);
|
||||
TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret);
|
||||
TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double *ret, char *buf, int buflen);
|
||||
TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t *ret);
|
||||
|
||||
#endif /* TOML_H */
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <wayland-cursor.h>
|
||||
|
||||
|
||||
typedef void (*pointer_button_handler_t)(uint32_t button, uint32_t state, double x, double y, void *user_data);
|
||||
|
||||
struct pointer_data {
|
||||
struct wl_cursor_theme *cursor_theme;
|
||||
struct wl_pointer *pointer;
|
||||
@@ -12,10 +14,18 @@ struct pointer_data {
|
||||
struct wl_cursor *cursor;
|
||||
struct wl_cursor_image *cursor_image;
|
||||
struct wl_buffer *cursor_buffer;
|
||||
|
||||
double x;
|
||||
double y;
|
||||
|
||||
pointer_button_handler_t button_handler;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
extern const struct wl_pointer_listener pointer_listener;
|
||||
|
||||
void pointer_init_cursor(struct pointer_data *pointer_data, struct wl_compositor *compositor, struct wl_shm *shm);
|
||||
|
||||
void pointer_set_button_handler(struct pointer_data *pointer_data, pointer_button_handler_t handler, void *user_data);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user