[client] all: move keybind implementation into app.c/h

This commit is contained in:
Geoffrey McRae 2021-01-26 02:33:28 +11:00
parent 9674421ce4
commit 3d03699cc8
6 changed files with 88 additions and 118 deletions

View File

@ -1,6 +1,6 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2020 Geoffrey McRae <geoff@hostfission.com>
Copyright (C) 2017-2021 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
@ -17,11 +17,25 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _H_LG_APP_
#define _H_LG_APP_
#include <stdbool.h>
#include <linux/input.h>
#include "common/types.h"
#include "interface/displayserver.h"
typedef enum LG_MsgAlert
{
LG_ALERT_INFO ,
LG_ALERT_SUCCESS,
LG_ALERT_WARNING,
LG_ALERT_ERROR
}
LG_MsgAlert;
SDL_Window * app_getWindow(void);
bool app_getProp(LG_DSProperty prop, void * ret);
@ -47,3 +61,37 @@ void app_clipboardRelease(void);
void app_clipboardNotify(const LG_ClipboardData type, size_t size);
void app_clipboardData(const LG_ClipboardData type, uint8_t * data, size_t size);
void app_clipboardRequest(const LG_ClipboardReplyFn replyFn, void * opaque);
typedef struct KeybindHandle * KeybindHandle;
typedef void (*SuperEventFn)(uint32_t sc, void * opaque);
/**
* Show an alert on screen
* @param type The alert type
* param fmt The alert message format
@ param ... formatted message values
*/
void app_alert(LG_MsgAlert type, const char * fmt, ...);
/**
* Register a handler for the <super>+<key> combination
* @param sc The scancode to register
* @param callback The function to be called when the combination is pressed
* @param opaque A pointer to be passed to the callback, may be NULL
* @retval A handle for the binding or NULL on failure.
* The caller is required to release the handle via `app_releaseKeybind` when it is no longer required
*/
KeybindHandle app_registerKeybind(uint32_t sc, SuperEventFn callback, void * opaque);
/**
* Release an existing key binding
* @param handle A pointer to the keybind handle to release, may be NULL
*/
void app_releaseKeybind(KeybindHandle * handle);
/**
* Release all keybindings
*/
void app_releaseAllKeybinds(void);
#endif

View File

@ -1,59 +0,0 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include <SDL2/SDL.h>
#include <linux/input.h>
typedef enum LG_MsgAlert
{
LG_ALERT_INFO ,
LG_ALERT_SUCCESS,
LG_ALERT_WARNING,
LG_ALERT_ERROR
}
LG_MsgAlert;
typedef struct KeybindHandle * KeybindHandle;
typedef void (*SuperEventFn)(uint32_t sc, void * opaque);
/**
* Show an alert on screen
* @param type The alert type
* param fmt The alert message format
@ param ... formatted message values
*/
void app_alert(LG_MsgAlert type, const char * fmt, ...);
/**
* Register a handler for the <super>+<key> combination
* @param sc The scancode to register
* @param callback The function to be called when the combination is pressed
* @param opaque A pointer to be passed to the callback, may be NULL
* @retval A handle for the binding or NULL on failure.
* The caller is required to release the handle via `app_release_keybind` when it is no longer required
*/
KeybindHandle app_register_keybind(uint32_t sc, SuperEventFn callback, void * opaque);
/**
* Release an existing key binding
* @param handle A pointer to the keybind handle to release, may be NULL
*/
void app_release_keybind(KeybindHandle * handle);

View File

@ -22,6 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "common/option.h"
#include "common/locking.h"
#include "app.h"
#include "texture.h"
#include "shader.h"
#include "model.h"
@ -29,8 +30,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdlib.h>
#include <string.h>
#include "interface/app.h"
// these headers are auto generated by cmake
#include "desktop.vert.h"
#include "desktop_rgb.frag.h"
@ -62,12 +61,11 @@ struct EGL_Desktop
struct DesktopShader shader_generic;
// night vision
KeybindHandle kbNV;
int nvMax;
int nvGain;
int nvMax;
int nvGain;
// colorblind mode
int cbMode;
int cbMode;
};
// forwards
@ -136,7 +134,7 @@ bool egl_desktop_init(EGL_Desktop ** desktop, EGLDisplay * display)
egl_model_set_default((*desktop)->model);
egl_model_set_texture((*desktop)->model, (*desktop)->texture);
(*desktop)->kbNV = app_register_keybind(KEY_N, egl_desktop_toggle_nv, *desktop);
app_registerKeybind(KEY_N, egl_desktop_toggle_nv, *desktop);
(*desktop)->nvMax = option_get_int("egl", "nvGainMax");
(*desktop)->nvGain = option_get_int("egl", "nvGain" );
@ -166,8 +164,6 @@ void egl_desktop_free(EGL_Desktop ** desktop)
egl_shader_free (&(*desktop)->shader_generic.shader);
egl_model_free (&(*desktop)->model );
app_release_keybind(&(*desktop)->kbNV);
free(*desktop);
*desktop = NULL;
}

View File

@ -55,7 +55,7 @@ void app_alert(LG_MsgAlert type, const char * fmt, ...)
free(buffer);
}
KeybindHandle app_register_keybind(SDL_Scancode key, SuperEventFn callback, void * opaque)
KeybindHandle app_registerKeybind(SDL_Scancode key, SuperEventFn callback, void * opaque)
{
// don't allow duplicate binds
if (g_state.bindings[key])
@ -73,7 +73,7 @@ KeybindHandle app_register_keybind(SDL_Scancode key, SuperEventFn callback, void
return handle;
}
void app_release_keybind(KeybindHandle * handle)
void app_releaseKeybind(KeybindHandle * handle)
{
if (!*handle)
return;
@ -83,6 +83,16 @@ void app_release_keybind(KeybindHandle * handle)
*handle = NULL;
}
void app_releaseAllKeybinds(void)
{
for(int i = 0; i < KEY_MAX; ++i)
if (g_state.bindings[i])
{
free(g_state.bindings[i]);
g_state.bindings[i] = NULL;
}
}
bool app_getProp(LG_DSProperty prop, void * ret)
{
return g_state.ds->getProp(prop, ret);

View File

@ -745,49 +745,35 @@ static void key_passthrough(uint32_t sc, void * opaque)
static void register_key_binds(void)
{
g_state.kbFS = app_register_keybind(KEY_F , toggle_fullscreen, NULL);
g_state.kbVideo = app_register_keybind(KEY_V , toggle_video , NULL);
g_state.kbRotate = app_register_keybind(KEY_R , toggle_rotate , NULL);
g_state.kbQuit = app_register_keybind(KEY_Q , quit , NULL);
app_registerKeybind(KEY_F, toggle_fullscreen, NULL);
app_registerKeybind(KEY_V, toggle_video , NULL);
app_registerKeybind(KEY_R, toggle_rotate , NULL);
app_registerKeybind(KEY_Q, quit , NULL);
if (g_params.useSpiceInput)
{
g_state.kbInput = app_register_keybind(KEY_I , toggle_input , NULL);
g_state.kbMouseSensInc = app_register_keybind(KEY_INSERT, mouse_sens_inc , NULL);
g_state.kbMouseSensDec = app_register_keybind(KEY_DELETE, mouse_sens_dec , NULL);
app_registerKeybind(KEY_I , toggle_input , NULL);
app_registerKeybind(KEY_INSERT, mouse_sens_inc , NULL);
app_registerKeybind(KEY_DELETE, mouse_sens_dec , NULL);
g_state.kbCtrlAltFn[0 ] = app_register_keybind(KEY_F1 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[1 ] = app_register_keybind(KEY_F2 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[2 ] = app_register_keybind(KEY_F3 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[3 ] = app_register_keybind(KEY_F4 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[4 ] = app_register_keybind(KEY_F5 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[5 ] = app_register_keybind(KEY_F6 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[6 ] = app_register_keybind(KEY_F7 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[7 ] = app_register_keybind(KEY_F8 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[8 ] = app_register_keybind(KEY_F9 , ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[9 ] = app_register_keybind(KEY_F10, ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[10] = app_register_keybind(KEY_F11, ctrl_alt_fn, NULL);
g_state.kbCtrlAltFn[11] = app_register_keybind(KEY_F12, ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F1 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F2 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F3 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F4 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F5 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F6 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F7 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F8 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F9 , ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F10, ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F11, ctrl_alt_fn, NULL);
app_registerKeybind(KEY_F12, ctrl_alt_fn, NULL);
g_state.kbPass[0] = app_register_keybind(KEY_LEFTMETA , key_passthrough, NULL);
g_state.kbPass[1] = app_register_keybind(KEY_RIGHTMETA, key_passthrough, NULL);
app_registerKeybind(KEY_LEFTMETA , key_passthrough, NULL);
app_registerKeybind(KEY_RIGHTMETA, key_passthrough, NULL);
}
}
static void release_key_binds(void)
{
app_release_keybind(&g_state.kbFS );
app_release_keybind(&g_state.kbVideo);
app_release_keybind(&g_state.kbInput);
app_release_keybind(&g_state.kbQuit );
app_release_keybind(&g_state.kbMouseSensInc);
app_release_keybind(&g_state.kbMouseSensDec);
for(int i = 0; i < 12; ++i)
app_release_keybind(&g_state.kbCtrlAltFn[i]);
for(int i = 0; i < 2; ++i)
app_release_keybind(&g_state.kbPass[i]);
}
static void initSDLCursor(void)
{
const uint8_t data[4] = {0xf, 0x9, 0x9, 0xf};
@ -1245,7 +1231,7 @@ static void lg_shutdown(void)
ivshmemClose(&g_state.shm);
release_key_binds();
app_releaseAllKeybinds();
SDL_Quit();
}

View File

@ -22,7 +22,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <SDL2/SDL.h>
#include <linux/input.h>
#include "interface/app.h"
#include "dynamic/displayservers.h"
#include "dynamic/renderers.h"
@ -92,16 +91,6 @@ struct AppState
uint64_t resizeTimeout;
bool resizeDone;
KeybindHandle kbFS;
KeybindHandle kbVideo;
KeybindHandle kbRotate;
KeybindHandle kbInput;
KeybindHandle kbQuit;
KeybindHandle kbMouseSensInc;
KeybindHandle kbMouseSensDec;
KeybindHandle kbCtrlAltFn[12];
KeybindHandle kbPass[2];
};
struct AppParams