Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabiszewski committed Sep 27, 2016
1 parent 8607972 commit 3ea7431
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 88 deletions.
57 changes: 35 additions & 22 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,79 +22,92 @@

#include <gtk/gtk.h>

/** Kterm version */
#ifndef VERSION
# define VERSION "2.0"
#endif
#ifdef KINDLE
// kindle title scheme
/** Kindle title scheme */
# define TITLE "L:A_N:application_ID:net.fabiszewski.kterm_PC:N_O:URL"
# define TITLE_DIALOG "L:D_N:application_ID:net.fabiszewski.kterm_O:URL"
/** Mouse button to open popup menu */
// middle button on kindle
# define BUTTON_MENU 2
// terminfo path
/** Terminfo path */
# define TERMINFO_PATH SYSCONFDIR "/vte/terminfo"
#else
/** Window title */
# define TITLE "kterm " VERSION
/** Mouse button to open popup menu */
# define BUTTON_MENU 3
#endif
// keyboard config path
/** Sysconf path */
#ifndef SYSCONFDIR
# define SYSCONFDIR "/etc/local"
#endif
/** Default keyboard config path */
#define KB_FULL_PATH SYSCONFDIR "/layouts/keyboard.xml"
// keyboard takes at most 1/3 of the screen height
/** Keyboard max factor. Keyboard takes at most 1/3 of the screen height */
#define KB_HEIGHTMAX_FACTOR 3
/** mm to inch conversion multiplier */
#define MM_TO_IN 0.0393701
// preferred key height in inches
/** Preferred key button height in inches */
#define KB_KEYHEIGHT_PREF (double) (8 * MM_TO_IN)
// config file
/** Config file name */
#define CONFIG_FILE "kterm.conf"
/** Default config file path */
#define CONFIG_FULL_PATH SYSCONFDIR "/kterm/" CONFIG_FILE

// resize font
/** Resize font up */
#define FONT_UP 0
/** Resize font down */
#define FONT_DOWN 1
// boxes
#define KB_BOX 0
#define TERM_BOX 1

#ifndef MAX
/** MAX macro */
# define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef PATH_MAX
/** Path max */
# define PATH_MAX 4096
#endif

/** Max count of arguments passed to terminal */
#define TERM_ARGS_MAX 50
#define KB_RELEASE_DELAY_MS 100

/** Delay for key release event */
#define KB_RELEASE_DELAY_MS 100

// terminal scrollback size
/** Terminal scrollback size */
#define VTE_SCROLLBACK_LINES 200
// default font
/** Default terminal font family */
#define VTE_FONT_FAMILY "monospace"
/** Default terminal font size */
#define VTE_FONT_SIZE 8
// default encoding
/** Default terminal encoding */
#define VTE_ENCODING "UTF-8"
// terminal color schemes
/** Terminal color scheme normal */
#define VTE_SCHEME_LIGHT FALSE
/** Terminal color scheme reversed */
#define VTE_SCHEME_DARK TRUE

/** Debug */
extern gboolean debug;
#define D if(debug)

/** Unused variable */
#define UNUSED(x) (void)(x)

// parse_config
/** Kterm config */
typedef struct {
gboolean kb_on;
gboolean color_reversed;
gchar font_family[50];
guint font_size;
gchar kb_conf_path[PATH_MAX];
gboolean kb_on; /** Keyboard visibility */
gboolean color_reversed; /** Color scheme, is reversed */
gchar font_family[50]; /** Terminal font family */
guint font_size; /** Terminal font size */
gchar kb_conf_path[PATH_MAX]; /** Keyboard config path */
} KTconf;

KTconf *parse_config();
KTconf * parse_config(void);

#endif
84 changes: 73 additions & 11 deletions keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include <gtk/gtk.h>
#include <vte/vte.h>
#include <string.h>
#include "keyboard.h"
#include "config.h"

#if GTK_CHECK_VERSION(3,0,0)
static GdkDevice *getkbdevice(void) {
/**
* Get gdk keyboard device
* @return GdkDevice device. This object must not be freed.
*/
static GdkDevice * getkbdevice(void) {
GdkDevice *device = NULL;
#if GTK_CHECK_VERSION(3,20,0)
GdkDisplay *display = gdk_display_get_default();
Expand All @@ -47,7 +52,12 @@ static GdkDevice *getkbdevice(void) {
}
#endif

static KBtype kbstate_to_kbtype(guint32 modifier_mask) {
/**
* Translate modifier mask to layout variant
* @param modifier_mask Modifier mask
* @return KBtype layout variant
*/
static KBtype kbstate_to_kbtype(const guint32 modifier_mask) {
KBtype type = KBT_DEFAULT;
if (modifier_mask & (GDK_LOCK_MASK | GDK_SHIFT_MASK)) {
type = KBT_SHIFT;
Expand All @@ -61,11 +71,20 @@ static KBtype kbstate_to_kbtype(guint32 modifier_mask) {
return type;
}

static gboolean modifier_only_caps(Keyboard *keyboard) {
/**
* Is caps lock selected and caps shift deslected
* @param keyboard Keyboard structure
* @return True if only caps lock selected
*/
static gboolean modifier_only_caps(const Keyboard *keyboard) {
return (keyboard->modifier_mask & GDK_LOCK_MASK) && !(keyboard->modifier_mask & GDK_SHIFT_MASK);
}

static void keyboard_set_layout(Keyboard *keyboard) {
/**
* Set layout variant based on modifiers set
* @param keyboard Keyboard structure
*/
static void keyboard_set_layout(const Keyboard *keyboard) {
KBtype kb_type = kbstate_to_kbtype(keyboard->modifier_mask);
gboolean only_caps = modifier_only_caps(keyboard);
D printf("setting layout %d, caps: %i\n", kb_type, only_caps);
Expand Down Expand Up @@ -95,6 +114,10 @@ static void keyboard_set_layout(Keyboard *keyboard) {
}
}

/**
* Deactivate modifier buttons based on modifiers set
* @param keyboard Keyboard structure
*/
static void keyboard_reset_modifiers(Keyboard *keyboard) {
D printf("resetting modifier_mask\n");
for (guint i = 0; i < keyboard->key_count; i++) {
Expand All @@ -106,6 +129,11 @@ static void keyboard_reset_modifiers(Keyboard *keyboard) {
keyboard->modifier_mask &= GDK_LOCK_MASK;
}

/**
* Calculate and set key button sizes
* @param keyboard_box Keyboard widget
* @param keyboard Keyboard structure
*/
void keyboard_set_size(GtkWidget *keyboard_box, Keyboard *keyboard) {
if G_UNLIKELY(keyboard == NULL) {
return;
Expand All @@ -114,18 +142,18 @@ void keyboard_set_size(GtkWidget *keyboard_box, Keyboard *keyboard) {
GdkScreen *screen = gdk_screen_get_default();
gint screen_height = gdk_screen_get_height(screen);
gint screen_width = gdk_screen_get_width(screen);
gboolean is_portrait = FALSE;
if (screen_width < screen_height) {
keyboard->is_portrait = TRUE;
is_portrait = TRUE;
}
keyboard->row_width = (guint) screen_width;
// count units per row
guint units_row_max = 0;
Key **p = keyboard->keys;
for (guint i = 0; i < keyboard->row_count; i++) {
guint units = 0;
for (guint j = 0; j < keyboard->key_per_row[i]; j++) {
Key *key = *p++;
if (key->extended && keyboard->is_portrait) {
if (key->extended && is_portrait) {
continue;
}
if (key->width) {
Expand All @@ -139,7 +167,7 @@ void keyboard_set_size(GtkWidget *keyboard_box, Keyboard *keyboard) {
units_row_max = MAX(units_row_max, units);
}
guint unit_hmin = keyboard->unit_height;
guint unit_wmax = (units_row_max) ? (keyboard->row_width / units_row_max) : 0;
guint unit_wmax = (units_row_max) ? ((guint) screen_width / units_row_max) : 0;
guint unit_wmin = keyboard->unit_width;
// add padding and border
GtkWidget *first = keyboard->keys[0]->button;
Expand Down Expand Up @@ -172,7 +200,7 @@ void keyboard_set_size(GtkWidget *keyboard_box, Keyboard *keyboard) {
width *= key->width;
width /= KEY_UNIT;
}
if (key->extended && key->keyboard->is_portrait) {
if (key->extended && is_portrait) {
if (gtk_widget_get_visible(key->button)) {
gtk_widget_hide(key->button);
}
Expand All @@ -188,7 +216,7 @@ void keyboard_set_size(GtkWidget *keyboard_box, Keyboard *keyboard) {
gtk_widget_set_size_request(key->button, (gint) width, -1);
}
}
gint kb_width = (gint) keyboard->row_width;
gint kb_width = screen_width;

// calculate keyboard widget height
gdouble dpi = gdk_screen_get_resolution(screen);
Expand All @@ -209,6 +237,11 @@ void keyboard_set_size(GtkWidget *keyboard_box, Keyboard *keyboard) {
gtk_widget_set_size_request(keyboard_box, -1, kb_height);
}

/**
* Key press event callback
* @param key Event key structure
* @return True on success, false otherwise
*/
static gboolean keyboard_event_press(Key *key) {
Keyboard *keyboard = key->keyboard;
GtkWidget *button = key->button;
Expand Down Expand Up @@ -263,6 +296,11 @@ static gboolean keyboard_event_press(Key *key) {
return TRUE;
}

/**
* Key release event callback
* @param data Event key structure
* @return Always false to cancel timout, as called by g_timeout_add()
*/
static gboolean keyboard_event_release(gpointer data) {
Key *key = data;
Keyboard *keyboard = key->keyboard;
Expand Down Expand Up @@ -306,7 +344,12 @@ static gboolean keyboard_event_release(gpointer data) {
return FALSE;
}

static void keyboard_terminal_feed(GtkWidget *button, Key *key) {
/**
* Key release event callback
* @param button Key button widget
* @param key Key structure
*/
static void keyboard_terminal_feed(GtkWidget *button, const Key *key) {
// send characters directly via vte_terminal_feed_child()
GtkWidget *toplevel = gtk_widget_get_toplevel(button);
GList *box_list = gtk_container_get_children(GTK_CONTAINER(toplevel));
Expand Down Expand Up @@ -335,6 +378,13 @@ static void keyboard_terminal_feed(GtkWidget *button, Key *key) {
vte_terminal_feed_child(VTE_TERMINAL(terminal), utf, utf_len);
}

/**
* Key event callback
* @param button Key button widget
* @param ev Gdk event
* @param key Key structure
* @return True to stop processing event, false otherwise
*/
gboolean keyboard_event(GtkWidget *button, GdkEvent *ev, Key *key) {
if (ev->type == GDK_BUTTON_PRESS) {
if (!keyboard_event_press(key)) {
Expand All @@ -349,6 +399,10 @@ gboolean keyboard_event(GtkWidget *button, GdkEvent *ev, Key *key) {
return FALSE;
}

/**
* Free key structure
* @param key Key structure
*/
void keyboard_key_free(Key *key) {
if (key) {
if (key->button && GTK_IS_WIDGET(key->button)) { gtk_widget_destroy(key->button); }
Expand All @@ -361,6 +415,10 @@ void keyboard_key_free(Key *key) {
}
}

/**
* Recursively free array of key structures
* @param keys Array of key structures
*/
static void keyboard_keys_free(Key **keys) {
if (keys == NULL) {
return;
Expand All @@ -373,6 +431,10 @@ static void keyboard_keys_free(Key **keys) {
g_free(keys);
}

/**
* Recursively free keyboard structure
* @param keyboard structure
*/
void keyboard_free(Keyboard **keyboard) {
if (keyboard && *keyboard) {
keyboard_keys_free((*keyboard)->keys);
Expand Down
Loading

0 comments on commit 3ea7431

Please sign in to comment.