mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	key_state.ch, and command conflicts resolved
This commit is contained in:
		
							parent
							
								
									4db89b5fe4
								
							
						
					
					
						commit
						470b4dfbae
					
				
					 4 changed files with 94 additions and 32 deletions
				
			
		
							
								
								
									
										18
									
								
								include/key_state.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								include/key_state.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,18 @@
 | 
			
		|||
#ifndef  _SWAY_KEY_STATE_H
 | 
			
		||||
#define  _SWAY_KEY_STATE_H
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
typedef uint32_t keycode;
 | 
			
		||||
 | 
			
		||||
// returns true if key has been pressed, otherwise false
 | 
			
		||||
bool check_key(keycode key);
 | 
			
		||||
 | 
			
		||||
// sets a key as pressed
 | 
			
		||||
void press_key(keycode key);
 | 
			
		||||
 | 
			
		||||
// unsets a key as pressed
 | 
			
		||||
void release_key(keycode key);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -76,6 +76,18 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) {
 | 
			
		|||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int bindsym_sort(const void *_lbind, const void *_rbind) {
 | 
			
		||||
	const struct sway_binding *lbind = *(void **)_lbind;
 | 
			
		||||
	const struct sway_binding *rbind = *(void **)_rbind;
 | 
			
		||||
	unsigned int lmod = 0, rmod = 0, i;
 | 
			
		||||
 | 
			
		||||
	//Count how any modifiers are pressed
 | 
			
		||||
	for (i = 0; i < 8 * sizeof(lbind->modifiers); ++i) {
 | 
			
		||||
		lmod += lbind->modifiers & 1 << i;
 | 
			
		||||
		rmod += rbind->modifiers & 1 << i;
 | 
			
		||||
	}
 | 
			
		||||
	return (rbind->keys->length + rmod) - (lbind->keys->length + lmod);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) {
 | 
			
		||||
	if (!checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -118,7 +130,10 @@ static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) {
 | 
			
		|||
	list_free(split);
 | 
			
		||||
 | 
			
		||||
	// TODO: Check if there are other commands with this key binding
 | 
			
		||||
	list_add(config->current_mode->bindings, binding);
 | 
			
		||||
	struct sway_mode *mode = config->current_mode;
 | 
			
		||||
	list_add(mode->bindings, binding);
 | 
			
		||||
	qsort(mode->bindings->items, mode->bindings->length,
 | 
			
		||||
			sizeof(mode->bindings->items[0]), bindsym_sort);
 | 
			
		||||
 | 
			
		||||
	sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
 | 
			
		||||
	return true;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,9 +13,7 @@
 | 
			
		|||
#include "workspace.h"
 | 
			
		||||
#include "container.h"
 | 
			
		||||
#include "focus.h"
 | 
			
		||||
 | 
			
		||||
#define KEY_CACHE_SIZE 32
 | 
			
		||||
uint32_t keys_pressed[KEY_CACHE_SIZE];
 | 
			
		||||
#include "key_state.h"
 | 
			
		||||
 | 
			
		||||
static struct wlc_origin mouse_origin;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -327,7 +325,6 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
 | 
			
		|||
	if (locked_view_focus && state == WLC_KEY_STATE_PRESSED) {
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
	bool cmd_success = false;
 | 
			
		||||
 | 
			
		||||
	// Revert floating container back to original position on keypress
 | 
			
		||||
	if (state == WLC_KEY_STATE_PRESSED && (dragging || resizing)) {
 | 
			
		||||
| 
						 | 
				
			
			@ -356,16 +353,10 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	int total = 0;
 | 
			
		||||
	for (i = 0; i < KEY_CACHE_SIZE && !mod; ++i) {
 | 
			
		||||
		total += keys_pressed[i] != 0;
 | 
			
		||||
		if (state == WLC_KEY_STATE_PRESSED && keys_pressed[i] == 0) {
 | 
			
		||||
			keys_pressed[i] = sym;
 | 
			
		||||
			break;
 | 
			
		||||
		} else if (state == WLC_KEY_STATE_RELEASED && keys_pressed[i] == sym) {
 | 
			
		||||
			keys_pressed[i] = 0;
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	if (state == WLC_KEY_STATE_PRESSED) {
 | 
			
		||||
		press_key(sym);
 | 
			
		||||
	} else { // WLC_KEY_STATE_RELEASED
 | 
			
		||||
		release_key(sym);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO: reminder to check conflicts with mod+q+a versus mod+q
 | 
			
		||||
| 
						 | 
				
			
			@ -376,32 +367,22 @@ static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifier
 | 
			
		|||
			bool match;
 | 
			
		||||
			int j;
 | 
			
		||||
			for (j = 0; j < binding->keys->length; ++j) {
 | 
			
		||||
				match = false;
 | 
			
		||||
				xkb_keysym_t *key = binding->keys->items[j];
 | 
			
		||||
				int k;
 | 
			
		||||
				for (k = 0; k < KEY_CACHE_SIZE; ++k) {
 | 
			
		||||
					if (keys_pressed[k] == *key) {
 | 
			
		||||
						match = true;
 | 
			
		||||
						break;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				if (match == false) {
 | 
			
		||||
				if ((match = check_key(*key)) == false) {
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (match) {
 | 
			
		||||
				// Remove matched keys from keys_pressed
 | 
			
		||||
				if (state == WLC_KEY_STATE_PRESSED) {
 | 
			
		||||
					handle_command(config, binding->command);
 | 
			
		||||
					cmd_success = true;
 | 
			
		||||
					return true;
 | 
			
		||||
				} else if (state == WLC_KEY_STATE_RELEASED) {
 | 
			
		||||
					// TODO: --released
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return cmd_success;
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool handle_pointer_motion(wlc_handle handle, uint32_t time, const struct wlc_origin *origin) {
 | 
			
		||||
| 
						 | 
				
			
			@ -590,10 +571,6 @@ static void handle_wlc_ready(void) {
 | 
			
		|||
	}
 | 
			
		||||
	free_flat_list(config->cmd_queue);
 | 
			
		||||
	config->active = true;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < KEY_CACHE_SIZE; ++i) {
 | 
			
		||||
		keys_pressed[i] = 0;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										52
									
								
								sway/key_state.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								sway/key_state.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,52 @@
 | 
			
		|||
#include <string.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
 | 
			
		||||
#include "key_state.h"
 | 
			
		||||
 | 
			
		||||
enum { KEY_STATE_MAX_LENGTH = 64 };
 | 
			
		||||
 | 
			
		||||
static keycode key_state_array[KEY_STATE_MAX_LENGTH];
 | 
			
		||||
static uint8_t key_state_length = 0;
 | 
			
		||||
 | 
			
		||||
static uint8_t find_key(keycode key)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
	for (i = 0; i < key_state_length; ++i)
 | 
			
		||||
	{
 | 
			
		||||
		if (key_state_array[i] == key)
 | 
			
		||||
		{
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool check_key(keycode key)
 | 
			
		||||
{
 | 
			
		||||
	return find_key(key) < key_state_length;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void press_key(keycode key)
 | 
			
		||||
{
 | 
			
		||||
	// Check if key exists
 | 
			
		||||
	if (!check_key(key))
 | 
			
		||||
	{
 | 
			
		||||
		// Check that we dont exceed buffer length
 | 
			
		||||
		if (key_state_length < KEY_STATE_MAX_LENGTH) {
 | 
			
		||||
			key_state_array[key_state_length++] = key;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void release_key(keycode key)
 | 
			
		||||
{
 | 
			
		||||
	uint8_t index = find_key(key);
 | 
			
		||||
	if (index < key_state_length)
 | 
			
		||||
	{
 | 
			
		||||
		//shift it over and remove key
 | 
			
		||||
		memmove(&key_state_array[index],
 | 
			
		||||
				&key_state_array[index + 1],
 | 
			
		||||
				sizeof(*key_state_array) * (--key_state_length - index));
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue