Commit aa2479a2 authored by Milan Santosi's avatar Milan Santosi Committed by Christopher Spinrath

Added Corny's str_concat branch of showkeys to tools

Also, use this for a larger term font:
urxvt -fn xft:terminus:pixelsize=30 -fade 0&
Signed-off-by: 's avatarMilan Santosi <milan.santosi@gmail.com>
parent 5b024cb1
This diff is collapsed.
showkeys: showkeys.c showkeys.h keystack.o
gcc -g -Wall showkeys.c keystack.o -o showkeys -lX11 -lxosd -lpthread -lXext -lX11 -lXinerama -lXtst
keystack.o: keystack.c keystack.h
gcc -c -g keystack.c
clean:
rm showkeys keystack.o record-attempt record-example
check-syntax:
gcc -Wall -o nul -S ${CHK_SOURCES}
record-attempt: record-attempt.c
gcc -g -Wall record-attempt.c -L/usr/lib -lXtst -lxosd -lpthread -lXext -lX11 -lXinerama -o record-attempt
record-example: record-example.c
gcc -g -Wall record-example.c -L/usr/lib -lXtst -lxosd -lpthread -lXext -lX11 -lXinerama -o record-example
Showkeys
--------
Showkeys is a simple program to display keys being pressed on the screen.
It is useful while making presentations and screencasts. The audience
will be able to see the keys being pressed.
It is similar to [key-mon](http://code.google.com/p/key-mon/). Key-mon
has more features that showkeys. The advantages of showkeys are
1. Doesn't use gtk/gnome.
2. Uses [libxosd](http://sourceforge.net/projects/libxosd/) to display
keys directly onto the screen.
3. No floating windows to that always need to be on top. This is very
useful if you're using a tiling WM like Xmonad.
4. Keystroke history. key-mon has this but doesn't show
modifiers. showkeys does using and Emacs style key notation.
The list of things which key-mon does better than showkeys is far
longer but I wanted something that fixed these specific issues.
Requirements
------------
You will need
1. The Xlib development libraries. (available as `libx11-dev` on Debian).
2. The libxosd development libraries (available as `libxosd-dev` on Debian).
3. Your X server should have the [record](http://www.x.org/releases/X11R7.6/doc/libXtst/recordlib.html) extension enabled.
Compiling
---------
1. Unzip the tarball
2. Run `make showkeys`. It should create the executable.
This program uses some [Gnu extensions to libc](http://www.crasseux.com/books/ctutorial/asprintf.html) so might not work with a pure POSIX libc.
Bugs
----
The project is on Github. Please report bugs using the issue tracker.
Showkeys
--------
Showkeys is a simple program to display keys being pressed on the screen.
It is useful while making presentations and screencasts. The audience
will be able to see the keys being pressed.
It is similar to [key-mon](http://code.google.com/p/key-mon/). Key-mon
has more features that showkeys. The advantages of showkeys are
1. Doesn't use gtk/gnome.
2. Uses [libxosd](http://sourceforge.net/projects/libxosd/) to display
keys directly onto the screen.
3. No floating windows that always need to be on top. This is very
useful if you're using a tiling WM like Xmonad.
4. Keystroke history. key-mon has this but doesn't show
modifiers. showkeys does using and Emacs style key notation.
The list of things which key-mon does better than showkeys is far
longer but I wanted something that fixed these specific issues.
Requirements
------------
You will need
1. The Xlib development libraries. (available as `libx11-dev` on Debian).
2. The libxosd development libraries (available as `libxosd-dev` on Debian).
3. Your X server should have the [record](http://www.x.org/releases/X11R7.6/doc/libXtst/recordlib.html) extension enabled.
Compiling
---------
1. Unzip the tarball
2. Run `make showkeys`. It should create the executable.
This program uses some [Gnu extensions to libc](http://www.crasseux.com/books/ctutorial/asprintf.html) so might not work with a pure POSIX libc.
Bugs
----
The project is on Github. Please report bugs using the issue tracker.
/* Showkeys
Copyright Noufal Ibrahim <noufal@nibrahim.net.in> 2011
Licensed under the GPLv3 : http://www.gnu.org/licenses/gpl.txt
Please see LICENSE file for complete license.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keystack.h"
static void
push_back(KeyStack *stack, int index)
{
int i;
free(stack->keystrokes[0].keyname);
for (i=0; i<index; i++)
stack->keystrokes[i] = stack->keystrokes[i+1];
}
KeyStack *
create_keystack(int size)
{
int i;
KeyStack *retval = NULL;
KeyStroke *stack = NULL;
stack = (KeyStroke *)malloc(sizeof(KeyStroke) * size);
for (i=0; i<size; i++) {
stack[i].keyname = NULL;
stack[i].times = 0;
}
retval = (KeyStack *)malloc(sizeof(KeyStack));
retval->size = size;
retval->pos = -1;
retval->keystrokes = stack;
return retval;
}
void
push(KeyStack *stack, char *keyname)
{
int index;
KeyStroke *last;
char *last_key;
last = &stack->keystrokes[stack->pos];
last_key = last->keyname;
index = stack->pos + 1;
if (index && strlen (keyname) < 3 && last->times != 0) {
/* If the same as the top of the stack, increment count */
int newlen = strlen(keyname) + strlen(last_key) + 1;
last->keyname = malloc(newlen);
snprintf(last->keyname, newlen, "%s%s", last_key, keyname);
free(last_key);
} else {
/* Add a new entry */
if (index == stack->size) {
push_back(stack, stack->pos);
index = stack->size-1;
}
stack->keystrokes[index].keyname = keyname;
int concat = 0;
if(strlen(keyname) < 3){concat = 1;}else {concat = 0;}
stack->keystrokes[index].times = concat;
stack->pos = index;
}
}
void
display_keystack(KeyStack *stack)
{
int i;
printf("---- Keystack ----\n");
for (i = 0; i <= stack->pos; i++) {
printf("%s %d times\n", stack->keystrokes[i].keyname, stack->keystrokes[i].times);
}
printf("---- -------- ----\n\n");
}
/* Showkeys
Copyright Noufal Ibrahim <noufal@nibrahim.net.in> 2011
Licensed under the GPLv3 : http://www.gnu.org/licenses/gpl.txt
Please see LICENSE file for complete license.
*/
#define NKEYS 10
typedef struct {
char *keyname;
int times;
} KeyStroke;
typedef struct {
int size;
int pos;
KeyStroke *keystrokes;
} KeyStack;
KeyStack *create_keystack();
void push(KeyStack *, char *);
void display_keystack(KeyStack *);
/* Showkeys
Copyright Noufal Ibrahim <noufal@nibrahim.net.in> 2011
Licensed under the GPLv3 : http://www.gnu.org/licenses/gpl.txt
Please see LICENSE file for complete license.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/extensions/record.h>
#include <xosd.h>
#include "showkeys.h"
#include "keystack.h"
Display *d0, *d1;
KeyStack *keystack;
xosd *osd;
int
process_modifiers(KeySym ks, int * meta, int *ctrl, int *shift, int val)
{
int modifier_pressed = 0;
switch(ks) {
case XK_Shift_L:
case XK_Shift_R:
*shift = val;
modifier_pressed = 1;
break;
case XK_Control_L:
case XK_Control_R:
*ctrl = val;
modifier_pressed = 1;
break;
case XK_Alt_L:
case XK_Alt_R:
*meta = val; /* This is not accurate but it's correct for my keyboard mappings */
modifier_pressed = 1;
break;
}
return modifier_pressed;
}
char *
create_emacs_keyname(char *keyname, int meta, int ctrl, int shift)
{
char *retval;
/* TBD: Handle <. > and others like that wehere XLookupString gives the right values */
printf("%d %d %d ", meta, ctrl, shift);
asprintf(&retval, "%s%s%s%s", ctrl?"C-":"", meta?"M-":"", shift?"S-":"", keyname);
printf(" %s\n",retval);
return retval;
}
xosd *
configure_osd(int lines)
{
xosd *osd;
osd = xosd_create (NKEYS);
//xosd_set_font(osd, "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1");
xosd_set_font(osd, "-misc-fixed-bold-r-normal-*-18-*-*-*-*-90-iso8859-*");
xosd_set_pos(osd, XOSD_top);
xosd_set_align(osd, XOSD_right);
xosd_set_colour(osd, "green");
xosd_set_outline_colour(osd, "black");
xosd_set_outline_offset(osd, 7);
xosd_set_shadow_colour(osd, "grey");
xosd_set_shadow_offset(osd, 0);
xosd_set_timeout(osd, -1);
return osd;
}
void
display_keystrokes(xosd *osd, KeyStack *stack)
{
int i;
for(i = 0; i < NKEYS; i++) {
if (stack->keystrokes[i].keyname) {
xosd_display(osd, i, XOSD_printf, "%s", stack->keystrokes[i].keyname);
}
}
}
void
update_key_ring (XPointer priv, XRecordInterceptData *data)
{
static int meta = 0;
static int ctrl = 0;
static int shift = 0;
xEvent *event;
KeySym ks;
char *display_string;
char *ksname;
if (data->category==XRecordFromServer) {
event=(xEvent *)data->data;
display_keystack(keystack);
switch (event->u.u.type) {
case KeyPress:
ks = XKeycodeToKeysym(d0, event->u.u.detail, 0);
ksname = XKeysymToString (ks); /* TBD: Might have to handle no symbol keys */
if (! process_modifiers(ks, &meta, &ctrl, &shift, 1)) {
display_string = create_emacs_keyname(ksname, meta, ctrl, shift);
push(keystack, display_string);
display_keystrokes(osd, keystack);
}
break;
case KeyRelease:
ks = XKeycodeToKeysym(d0, event->u.u.detail, 0);
process_modifiers(ks, &meta, &ctrl, &shift, 0);
break;
}
}
}
int
main()
{
XRecordContext xrd;
XRecordRange *range;
XRecordClientSpec client;
osd = configure_osd(NKEYS);
keystack = create_keystack(NKEYS);
d0 = XOpenDisplay(NULL);
d1 = XOpenDisplay(NULL);
XSynchronize(d0, True);
if (d0 == NULL || d1 == NULL) {
fprintf(stderr, "Cannot connect to X server");
exit (-1);
}
client=XRecordAllClients;
range=XRecordAllocRange();
memset(range, 0, sizeof(XRecordRange));
range->device_events.first=KeyPress;
range->device_events.last=KeyRelease;
xrd = XRecordCreateContext(d0, 0, &client, 1, &range, 1);
if (! xrd) {
fprintf(stderr, "Error in creating context");
exit (-1);
}
XRecordEnableContext(d1, xrd, update_key_ring, (XPointer)osd);
XRecordProcessReplies (d1);
XRecordDisableContext (d0, xrd);
XRecordFreeContext (d0, xrd);
XCloseDisplay(d0);
XCloseDisplay(d1);
exit(0);
}
/* Showkeys
Copyright Noufal Ibrahim <noufal@nibrahim.net.in> 2011
Licensed under the GPLv3 : http://www.gnu.org/licenses/gpl.txt
Please see LICENSE file for complete license.
*/
void update_key_ring (XPointer priv, XRecordInterceptData *data);
char *create_emacs_keyname(char *, int, int, int);
int process_modifiers(KeySym , int * , int *, int *, int);
xosd *configure_osd(int);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment