Skip to content

Commit

Permalink
Version 1.1.7
Browse files Browse the repository at this point in the history
 * Implement bookmarks, see README.md for details
  • Loading branch information
gahr committed Apr 3, 2014
1 parent e626e82 commit 0091b19
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)

SET (TL_PROJECT_NAME thingylaunch)
SET (TL_PROJECT_VERS 1.1.6)
SET (TL_PROJECT_VERS 1.1.7)

PROJECT (${TL_PROJECT_NAME})

Expand All @@ -14,6 +14,7 @@ INCLUDE_DIRECTORIES (${X11_X11_INCLUDE_PATH})

ADD_EXECUTABLE (
${TL_PROJECT_NAME}
bookmark.c
completion.c
history.c
thingylaunch.c
Expand Down
3 changes: 3 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2014-04-03 - 1.1.7
* Implement bookmarks, see README.md for details

2014-04-03 - 1.1.6
* Fix a bug where the command line wasn't always '\0'-terminated
* Fix style here and there
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
thingylaunch
============

Simple X11 application launcher with tab-completion
Simple X11 application launcher.

The project is a fork of the original thinglaunch by Matt Johnston, available at http://unix.freecode.com/projects/thinglaunch, enhanced with tab-completion and navigation keys support.
The project is a fork of the original thinglaunch by Matt Johnston, available at http://unix.freecode.com/projects/thinglaunch.

Thingylaunch has been enhanced with tab-completion, history navigation, and
bookmarks support.

The history is browsed using the UpArrow and DownArrow keys. Auto-completion
is triggered by the Tab key. Bookmarks are loaded from the ~/.thingylaunch.bookmarks
file, which consists of lines structured as follows:

<char> <command>

Example:

f firefox
x xterm
c gcalctool

Bookmarks are activated by Alt+char, e.g., Alt+f for firefox.

See also http://gahr.ch/thingylaunch/ .
130 changes: 130 additions & 0 deletions bookmark.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*-
Copyright (C) 2014 Pietro Cerutti <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bookmark.h"
#include "completion.h"

typedef struct book_elem_ * book_elem_t;

struct book_elem_ {
book_elem_t next;
char * command;
char letter;
};

/* head of the linked list */
struct book_ {
book_elem_t head;
};

book_t
book_init(void)
{
book_t book;
book_elem_t curr;
char book_file[MAX_CMD_LEN];
FILE * fd = NULL;
int rc, nof_chars, c;
char letter;
char command[MAX_CMD_LEN+1];

/*
* initialize structure
*/
if (!(book = malloc(sizeof *book)))
return NULL;

book->head = NULL;

snprintf(book_file, MAX_CMD_LEN, "%s/%s", getenv("HOME"), "/.thingylaunch.bookmarks");

if (!(fd = fopen(book_file, "r"))) {
return (book);
}

while ((rc = fscanf(fd, "%c %" XSTRING(MAX_CMD_LEN) "[^\n]%n", &letter, command, &nof_chars)) != EOF) {
if ((c = fgetc(fd)) != '\n') {
goto error;
}
if (rc == 0) {
continue;
}
command[nof_chars] = '\0';
/* allocate new element */
if (!(curr = malloc(sizeof *curr))) {
goto error;
}
if (!(curr->command = strdup(command))) {
free(curr);
goto error;
}
curr->letter = letter;
curr->next = book->head;
book->head = curr;
}
fclose(fd);
fd = NULL;

return (book);

error:
if (book)
book_cleanup(book);
if (fd)
fclose(fd);
return (NULL);
}

void
book_cleanup(book_t book)
{
book_elem_t curr = book->head;
book_elem_t next;

while (curr != NULL) {
next = curr->next;
free(curr->command);
free(curr);
curr = next;
}
free(book);
}

char *
book_lookup(book_t book, char letter)
{
book_elem_t curr = book->head;

while (curr != NULL) {
if (curr->letter == letter)
return (curr->command);
curr = curr->next;
}
return (NULL);
}
35 changes: 35 additions & 0 deletions bookmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*-
Copyright (C) 2014 Pietro Cerutti <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

#ifndef BOOKMARK_H
#define BOOKMARK_H

typedef struct book_ * book_t;

book_t book_init(void);
char * book_lookup(book_t, char);
void book_cleanup(book_t);

#endif /* !BOOKMARK_H*/
2 changes: 2 additions & 0 deletions completion.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <limits.h>

#define MAX_CMD_LEN 256
#define XSTRING(x) STRING(x)
#define STRING(x) #x

typedef struct comp_ * comp_t;

Expand Down
3 changes: 0 additions & 3 deletions history.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
#include "history.h"
#include "completion.h"

#define XSTRING(x) STRING(x)
#define STRING(x) #x

struct hist_ {
char ** hist_elem;
size_t curr_elem;
Expand Down
51 changes: 40 additions & 11 deletions thingylaunch.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "completion.h"
#include "history.h"
#include "bookmark.h"

static void createWindow();
static void eventLoop();
Expand All @@ -31,6 +32,7 @@ static void redraw();
static void keypress(XKeyEvent * keyevent);
static void execcmd();
static void die(const char * message);
static void cleanup();

Display * display;
GC gc;
Expand All @@ -41,6 +43,7 @@ int screen_num;

comp_t comp;
hist_t hist;
book_t book;

#define WINWIDTH 640
#define WINHEIGHT 25
Expand All @@ -55,11 +58,15 @@ main(void)
cursor_pos = 0;

if (!(comp = comp_init())) {
exit(1);
die("Could not initialize completion");
}

if (!(hist = hist_init())) {
exit (1);
die("Could not initialize history");
}

if (!(book = book_init())) {
die("Could not initialize bookmarks");
}

createWindow();
Expand Down Expand Up @@ -234,11 +241,20 @@ keypress(XKeyEvent * keyevent)
buffer[len] = '\0';
len = strlen(command);

/* check for an Alt-key meaning bookmark lookup */
if (keyevent->state & Mod1Mask) {
cmd = book_lookup(book, buffer[0]);
if (cmd != NULL) {
snprintf(command, MAX_CMD_LEN, "%s", cmd);
cursor_pos = strlen(command);
hist_save(hist, command);
execcmd();
}
}

switch(key_symbol) {
case XK_Escape:
comp_cleanup(comp);
hist_cleanup(hist);
exit(0);
cleanup(0);
break;

case XK_BackSpace:
Expand Down Expand Up @@ -352,14 +368,15 @@ keypress(XKeyEvent * keyevent)
static void
execcmd()
{
command[cursor_pos] = '\0';
command[cursor_pos] = '\0';
char * shell;
char * argv[4];

XUngrabKeyboard(display, CurrentTime);
XDestroyWindow(display, win);

if (fork()) {
exit(0);
cleanup(0);
}

shell = getenv("SHELL");
Expand All @@ -374,14 +391,26 @@ execcmd()

execv(shell, argv);
die("aiee, after exec");

}

static void
die(const char * msg) {

die(const char * msg)
{
fprintf(stderr, "Error: %s\n", msg);
exit(1);
cleanup(1);
}

static void
cleanup(int rc)
{
if (comp)
comp_cleanup(comp);
if (hist)
hist_cleanup(hist);
if (book)
book_cleanup(book);

exit(rc);
}


Expand Down

0 comments on commit 0091b19

Please sign in to comment.