1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Jun 2017 19:50:43 -0400
changeset 57 4974e5368a29
parent 56 a573346e6f7b
permissions -rw-r--r--
Minor type cleanup ("unsigned char" -> "uint8_t").
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
     1
#include <linux/input.h>
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
     2
#include <fcntl.h>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <stdio.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <stdlib.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include <string.h>
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include <assert.h>
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
     7
#include <errno.h>
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
     8
#include <unistd.h>
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
     9
#include <dirent.h>
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
    10
#include <signal.h>
46
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
    11
#include <mntent.h>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include "lua.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include "lauxlib.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include "lualib.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include "pkcs5_pbkdf2.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include "aes.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include "base64.h"
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include "md5.h"
46
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
    19
#include "sha256.h"
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
    20
#include "keyhook.h"
56
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
    21
#include "otp.h"
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    22
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    23
#include <gtk/gtk.h>
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    24
#include <gdk/gdk.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    25
#include <gdk/gdkx.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    26
#include <gdk/gdkkeysyms.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    27
#include <X11/Xlib.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    28
#include <X11/Xlibint.h>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    32
// plug in a Griffin Powermate, make sure you have access to it, and run with
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    33
//  --powermate=/dev/input/eventX
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    34
static int powermate_fd = -1;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    35
static int pumpPowermate(void)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    36
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    37
    struct input_event buf[32];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    38
    int pressed = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    39
    ssize_t br;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    40
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    41
    if (powermate_fd == -1)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    42
        return 0;  // nothing to do.
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    43
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    44
    while ((br = read(powermate_fd, buf, sizeof (buf))) > 0)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    45
    {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    46
        ssize_t i;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    47
        br /= sizeof (buf[0]);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    48
        for (i = 0; i < br; i++)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    49
        {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    50
            struct input_event *ev = &buf[i];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    51
            if ((ev->type == EV_KEY) && (ev->code == BTN_0) && (ev->value))
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    52
                pressed = 1;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    53
        } // for
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    54
    } // while
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    55
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    56
    return pressed;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    57
}
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    58
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    59
static void setPowermateLED(const int enable)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    60
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    61
    struct input_event ev;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    62
    const int brightness = enable ? 255 : 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    63
    const int pulse_speed = 255;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    64
    const int pulse_table = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    65
    const int pulse_awake = enable ? 1 : 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    66
    const int pulse_asleep = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    67
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    68
    if (powermate_fd == -1)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    69
        return;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    70
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    71
    memset(&ev, '\0', sizeof (ev));
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    72
    ev.type = EV_MSC;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    73
    ev.code = MSC_PULSELED;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    74
    ev.value = brightness | (pulse_speed << 8) | (pulse_table << 17) | (pulse_asleep << 19) | (pulse_awake << 20);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    75
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    76
    if (write(powermate_fd, &ev, sizeof (ev)) != sizeof (ev))
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    77
        fprintf(stderr, "WARNING: tried to set Powermate LED and failed: %s\n", strerror(errno));
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    78
} // setPowermateLED
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    79
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    80
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    81
static int openPowermate(const char *fname)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    82
{
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    83
    static const char const *known_names[] = {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    84
        "Griffin PowerMate", "Griffin SoundKnob"
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    85
    };
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    86
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    87
    char buf[255];
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    88
    int ok = 0;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    89
    int fd;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    90
    int i;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    91
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    92
    if (!fname)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    93
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    94
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    95
    if ((fd = open(fname, O_RDWR)) == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    96
        fprintf(stderr, "WARNING: couldn't open Powermate at %s: %s\n", fname, strerror(errno));
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    97
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    98
    if (ioctl(fd, EVIOCGNAME(sizeof (buf)), buf) == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    99
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   100
        fprintf(stderr, "EVIOCGNAME failed for %s: %s\n", fname, strerror(errno));
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   101
        close(fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   102
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   103
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   104
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   105
    for (i = 0; !ok && (i < sizeof (known_names) / sizeof (known_names[0])); i++)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   106
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   107
        if (strncmp(buf, known_names[i], strlen(known_names[i])) == 0)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   108
            ok = 1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   109
    } // for
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   110
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   111
    if (!ok)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   112
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   113
        close(fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   114
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   115
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   116
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   117
    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   118
    return fd;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   119
} // openPowermate
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   120
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   121
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   122
static void deinitPowermate(void)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   123
{
34
55a2b89a50cd Whoops, wrong comparison.
Ryan C. Gordon <icculus@icculus.org>
parents: 33
diff changeset
   124
    if (powermate_fd != -1)
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   125
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   126
        setPowermateLED(0);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   127
        close(powermate_fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   128
        powermate_fd = -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   129
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   130
} // deinitPowermate
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   131
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   132
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   133
static void initPowermate(int *_argc, char **argv)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   134
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   135
    const char *arg = "--powermate=";
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   136
    const size_t arglen = strlen(arg);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   137
    int argc = *_argc;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   138
    int i;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   139
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   140
    for (i = 1; i < argc; i++)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   141
    {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   142
        const char *thisarg = argv[i];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   143
        if (strncmp(thisarg, arg, arglen) != 0)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   144
            continue;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   145
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   146
        thisarg += arglen;
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   147
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   148
        if (strcmp(thisarg, "auto") == 0)
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   149
        {
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   150
            DIR *dirp = opendir("/dev/input");
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   151
            if (dirp)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   152
            {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   153
                struct dirent *dent;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   154
                while ((dent = readdir(dirp)) != NULL)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   155
                {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   156
                    const char *name = dent->d_name;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   157
                    char buf[PATH_MAX];
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   158
                    if (strncmp(name, "event", 5) != 0)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   159
                        continue;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   160
                    snprintf(buf, sizeof (buf), "/dev/input/%s", name);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   161
                    if (powermate_fd == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   162
                    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   163
                        powermate_fd = openPowermate(buf);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   164
                        if (powermate_fd != -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   165
                        {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   166
                            printf("Found Powermate at %s\n", buf);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   167
                            break;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   168
                        } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   169
                    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   170
                } // while
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   171
                closedir(dirp);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   172
            } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   173
            thisarg = NULL;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   174
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   175
        } // if
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   176
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   177
        // eliminate this command line.
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   178
        memmove(&argv[i], &argv[i+1], (argc-i) * sizeof (char *));
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   179
        argc--;
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   180
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   181
        if (powermate_fd == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   182
            powermate_fd = openPowermate(thisarg);
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   183
    } // for
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   184
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   185
    atexit(deinitPowermate);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   186
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   187
    *_argc = argc;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   188
} // initPowermate
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   189
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   190
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
static lua_State *luaState = NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   195
static int makeLuaCallback(lua_State *L, const int idx)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   196
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   197
    assert(lua_isfunction(L, idx));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   198
    lua_pushvalue(L, idx);  // copy the Lua callback (luaL_ref() pops it).
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   199
    return luaL_ref(L, LUA_REGISTRYINDEX);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   200
} // makeLuaCallback
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   201
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    if (str != NULL)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
        lua_pushlstring(L, (const char *) str, len);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
} // retvalStringBytes
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   211
static inline int retvalString(lua_State *L, const char *str)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   212
{
15
e2973f4fb366 Fixed crash.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   213
    return retvalStringBytes(L, (const uint8_t *) str, str ? strlen(str) : 0);
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   214
} // retvalString
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   215
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   216
static inline int retvalPointer(lua_State *L, void *ptr)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   217
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   218
    if (ptr != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   219
        lua_pushlightuserdata(L, ptr);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   220
    else
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   221
        lua_pushnil(L);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   222
    return 1;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   223
} // retvalPointer
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   224
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
    int i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    for (i = 0; i < 16; i++, dst++, src++)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
        *dst ^= *src;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
} // xorBlock
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    const size_t blocks = *datalen / 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    uint8_t *block = data + ((blocks-1) * 16);   // start at final block, work backwards
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    const uint8_t *padding = &block[15];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    uint8_t expkey[aesExpandedKeySize];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    size_t i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    if (blocks == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
        return 1;  // nothing to do.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
	aesExpandKey(key, expkey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    for (i = 0; i < blocks-1; i++)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        xorBlock(block, block-16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        block -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    xorBlock(block, iv);   // xor against initial vector for final block.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    if (*padding > 16)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
        return 0;  // bad data?
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    *datalen -= *padding;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
} // decryptBinaryUsingKeyAndIvec
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
static inline int isSalted(const uint8_t *data, const size_t datalen)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
    return ( (datalen > sizeof (saltprefix)) &&
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
} // isSalted
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
static int decryptUsingPBKDF2(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    const char *password = luaL_checkstring(L, 2);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    const int iterations = luaL_checkinteger(L, 3);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    base64_decodestate base64state;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    const uint8_t *salt = zero16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    int saltlen = sizeof (zero16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    if (isSalted(data, datalen))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
        salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
    uint8_t output[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    const uint8_t *aeskey = &output[0];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    const uint8_t *aesiv = &output[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
} // decryptUsingPBKDF2
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
static int decryptBase64UsingKey(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    size_t keylen = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    base64_decodestate base64state;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    uint8_t aeskey[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    uint8_t aesiv[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    MD5_CTX md5;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    if (isSalted(data, datalen))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        const uint8_t *salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        const size_t saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        assert(aesNr == 10);  // AES-256 needs more rounds.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        assert(aesNk == 4);   // hashing size is hardcoded later.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        uint8_t hashing[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        MD5_finish(&md5, hashing);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
        MD5_append(&md5, hashing, 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
        MD5_finish(&md5, &hashing[16]);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        memcpy(aeskey, hashing, 4 * aesNk);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
        memcpy(aesiv, &hashing[4 * aesNk], 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
        MD5_finish(&md5, aeskey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
        memset(aesiv, '\0', sizeof (aesiv));
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    } // else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
} // decryptBase64UsingKey
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
56
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   368
static int decryptTopt(lua_State *L)
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   369
{
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   370
    const char *base32_secret = luaL_checkstring(L, 1);
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   371
    char otpstr[16];
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   372
    const int rc = totp(base32_secret, otpstr, sizeof (otpstr));
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   373
    return retvalString(L, (rc == -1) ? NULL : otpstr);
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   374
} // decryptTopt
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   375
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   376
46
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   377
static void calcSha256(const BYTE *buf, const size_t len, BYTE *hash)
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   378
{
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   379
    SHA256_CTX sha256;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   380
    sha256_init(&sha256);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   381
    sha256_update(&sha256, buf, len);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   382
    sha256_final(&sha256, hash);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   383
} // calcSha256
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   384
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   385
static int calcSha256_Lua(lua_State *L)
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   386
{
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   387
    size_t len = 0;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   388
    const char *str = luaL_checklstring(L, 1, &len);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   389
    BYTE hash[32];
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   390
    calcSha256(str, len, hash);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   391
    return retvalStringBytes(L, hash, sizeof (hash));
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   392
} // calcSha256_Lua
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   393
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   394
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   395
static int runGuiPasswordPrompt(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   396
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   397
    const char *hintstr = lua_tostring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   398
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   399
                            "Master Password", NULL, GTK_DIALOG_MODAL,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   400
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   401
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   402
                            NULL);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   403
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   404
    GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   405
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   406
    if (hintstr != NULL)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   407
    {
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   408
        GtkWidget *label = gtk_label_new(hintstr);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   409
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   410
        gtk_container_add(GTK_CONTAINER(content_area), label);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   411
    } // if
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   412
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   413
    GtkWidget *entry = gtk_entry_new();
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   414
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   415
    gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   416
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   417
    gtk_container_add(GTK_CONTAINER(content_area), entry);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   418
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   419
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   420
    gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   421
    gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   422
    gtk_window_set_skip_pager_hint(GTK_WINDOW(dialog), TRUE);
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   423
    gtk_widget_show_all(dialog);
26
52d346bbaa51 Try to get the password window to the top of the z-order.
Ryan C. Gordon <icculus@icculus.org>
parents: 17
diff changeset
   424
    gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   425
    const int ok = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   426
    retvalString(L, ok ? (const char *) gtk_entry_get_text(GTK_ENTRY(entry)) : NULL);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   427
    gtk_widget_destroy(dialog);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   428
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   429
    return 1;
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   430
} // runGuiPasswordPrompt
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   431
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   432
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   433
static int copyToClipboard(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   434
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   435
    const char *str = luaL_checkstring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   436
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), str, -1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   437
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, -1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   438
} // copyToClipboard
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   439
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   440
static gboolean checkForEscapeKey(GtkWidget *widget, GdkEvent *event, gpointer arg)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   441
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   442
    if ((event->type == GDK_KEY_PRESS) && (event->key.keyval == GDK_KEY_Escape))
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   443
    {
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   444
        // !!! FIXME: this is a little hacky
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   445
        lua_getglobal(luaState, "escapePressed");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   446
        lua_call(luaState, 0, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   447
        return TRUE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   448
    } // if
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   449
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   450
    return FALSE;  // pass this to other event handlers.
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   451
} // checkForEscapeKey
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   452
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   453
static gboolean wasSearchDeleteText = FALSE;  // HACK to workaround gtk+ nonsense.
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   454
static void searchChanged(GtkEditable *editable, gpointer arg)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   455
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   456
    const int callback = (int) ((size_t)arg);
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   457
    GtkWidget *vbox = gtk_widget_get_parent(GTK_WIDGET(editable));
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   458
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   459
    lua_pushlightuserdata(luaState, vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   460
    lua_pushstring(luaState, gtk_entry_get_text(GTK_ENTRY(editable)));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   461
    lua_call(luaState, 2, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   462
    gtk_widget_grab_focus(GTK_WIDGET(editable));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   463
    if (wasSearchDeleteText)  // HACK to workaround gtk+ nonsense.
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   464
    {
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   465
        gtk_editable_set_position(editable, -1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   466
        wasSearchDeleteText = FALSE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   467
    } // if
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   468
} // searchChanged
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   469
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   470
// HACK to workaround gtk+ nonsense.
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   471
static void searchDeleteText(GtkEditable *editable, gint start_pos, gint end_pos, gpointer user_data)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   472
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   473
    wasSearchDeleteText = TRUE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   474
} // searchDeleteText
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   475
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   476
static void destroyLuaCallback(const int callback)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   477
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   478
    //printf("unref callback %d\n", callback);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   479
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   480
} // destroyLuaCallback
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   481
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   482
static void destroyCallback(GtkWidget *widget, gpointer arg)
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   483
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   484
    destroyLuaCallback((int) ((size_t)arg));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   485
} // destroyCallback
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   486
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   487
static void destroyTopLevelMenu(GtkWidget *widget, gpointer arg)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   488
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   489
    // !!! FIXME: hack
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   490
    int *cbs = (int *) arg;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   491
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, cbs[1]);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   492
    lua_call(luaState, 0, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   493
    destroyLuaCallback(cbs[0]);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   494
    destroyLuaCallback(cbs[1]);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   495
    free(cbs);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   496
} // destroyTopLevelMenu
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   497
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   498
#if 0
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   499
static gboolean
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   500
mappedWindow(GtkWidget *widget, GdkEvent *event, gpointer user_data)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   501
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   502
    GdkEventClient e;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   503
    memset(&e, '\0', sizeof (e));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   504
    e.type = GDK_CLIENT_EVENT;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   505
    e.window = gtk_widget_get_window(widget);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   506
    e.send_event = 1;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   507
    e.message_type = gdk_atom_intern_static_string("_NET_ACTIVE_WINDOW");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   508
    e.data_format = 32;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   509
    e.data.l[0] = 1;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   510
    e.data.l[1] = (long) gdk_x11_get_server_time(e.window);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   511
    e.data.l[2] = 0;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   512
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   513
    gdk_window_raise (e.window);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   514
    gdk_event_send_client_message((GdkEvent *) &e, gdk_x11_drawable_get_xid(gtk_widget_get_root_window(widget)));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   515
    return TRUE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   516
}
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   517
#endif
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   518
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   519
static int guiCreateTopLevelMenu(lua_State *L)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   520
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   521
    const char *title = luaL_checkstring(L, 1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   522
    const int changedCallback = makeLuaCallback(L, 2);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   523
    const int destroyedCallback = makeLuaCallback(L, 3);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   524
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   525
    int *cbs = (int *) malloc(sizeof (int) * 2);  // !!! FIXME: hack
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   526
    cbs[0] = changedCallback;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   527
    cbs[1] = destroyedCallback;
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   528
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   529
    GtkWindow *window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   530
    gtk_window_set_keep_above(window, TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   531
    gtk_window_set_skip_taskbar_hint(window, TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   532
    gtk_window_set_skip_pager_hint(window, TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   533
    g_signal_connect(window, "destroy", G_CALLBACK(destroyTopLevelMenu), cbs);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   534
    gtk_window_set_title(window, title);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   535
    gtk_window_set_position(window, GTK_WIN_POS_MOUSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   536
    gtk_window_set_decorated(window, FALSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   537
    gtk_window_set_resizable(window, FALSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   538
    GtkEntry *search = GTK_ENTRY(gtk_entry_new());
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   539
    g_signal_connect(search, "key-press-event", G_CALLBACK(checkForEscapeKey), window);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   540
    gtk_entry_set_text(search, "Search...");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   541
    g_signal_connect(search, "changed", G_CALLBACK(searchChanged), (gpointer) ((size_t)changedCallback));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   542
    g_signal_connect(search, "delete-text", G_CALLBACK(searchDeleteText), 0); // HACK to workaround gtk+ nonsense.
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   543
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   544
//    g_signal_connect(window, "map-event", G_CALLBACK(mappedWindow), NULL);
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   545
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   546
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   547
    GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(FALSE, 0));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   548
    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vbox));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   549
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   550
    gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(search), FALSE, FALSE, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   551
    gtk_widget_show(GTK_WIDGET(search));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   552
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   553
    GtkWidget *vsep = GTK_WIDGET(gtk_vseparator_new());
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   554
    gtk_box_pack_start(GTK_BOX(vbox), vsep, FALSE, FALSE, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   555
    gtk_widget_show(vsep);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   556
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   557
    return retvalPointer(L, vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   558
} // guiCreateTopLevelMenu
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   559
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   560
static void clickedMenuItem(GtkButton *button, gpointer arg)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   561
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   562
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, (int) ((size_t)arg));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   563
    lua_pushlightuserdata(luaState, button);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   564
    lua_call(luaState, 1, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   565
} // clickedMenuItem
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   566
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   567
static int guiAddMenuItem(lua_State *L)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   568
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   569
    GtkWidget *vbox = (GtkWidget *) lua_touserdata(L, 1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   570
    const char *label = luaL_checkstring(L, 2);
46
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   571
    const int checked = lua_toboolean(L, 3);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   572
    const int callback = makeLuaCallback(L, 4);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   573
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   574
    if (checked)
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   575
    {
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   576
        // !!! FIXME: this is pretty lousy.
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   577
        const size_t len = strlen(label) + 5;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   578
        char *buf = (char *) alloca(len);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   579
        snprintf(buf, len, "[X] %s", label);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   580
        label = buf;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   581
    } // if
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   582
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   583
    GtkWidget *item = GTK_WIDGET(gtk_button_new_with_label(label));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   584
    g_signal_connect(item, "key-press-event", G_CALLBACK(checkForEscapeKey), NULL);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   585
    g_signal_connect(item, "clicked", G_CALLBACK(clickedMenuItem), (gpointer) ((size_t)callback));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   586
    g_signal_connect(item, "destroy", G_CALLBACK(destroyCallback), (gpointer) ((size_t)callback));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   587
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   588
    //gtk_button_set_image(button, gtk_image_new_from_stock(GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   589
    gtk_button_set_alignment (GTK_BUTTON(item), 0.0f, 0.5f);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   590
    gtk_button_set_relief(GTK_BUTTON(item), GTK_RELIEF_NONE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   591
    gtk_box_pack_start(GTK_BOX(vbox), item, FALSE, FALSE, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   592
    gtk_widget_show(item);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   593
    return retvalPointer(L, item);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   594
} // guiAddMenuItem
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   595
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   596
static int guiRemoveAllMenuItems(lua_State *L)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   597
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   598
    GtkWidget *vbox = (GtkWidget *) lua_touserdata(L, 1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   599
    GList *children = gtk_container_get_children(GTK_CONTAINER(vbox));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   600
    GList *iter;
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   601
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   602
    gtk_widget_hide(vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   603
    for (iter = children; iter != NULL; iter = g_list_next(iter))
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   604
    {
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   605
        if (G_OBJECT_TYPE(iter->data) == GTK_TYPE_BUTTON)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   606
             gtk_widget_destroy(GTK_WIDGET(iter->data));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   607
    } // for
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   608
    g_list_free(children);
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   609
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   610
    return 0;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   611
} // guiRemoveAllMenuItems
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   612
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   613
static int guiDestroyMenu(lua_State *L)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   614
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   615
    GtkWidget *widget = (GtkWidget *) lua_touserdata(L, 1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   616
    gtk_widget_destroy(gtk_widget_get_toplevel(widget));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   617
    return 0;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   618
} // guiDestroyMenu
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   619
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   620
static int guiShowWindow(lua_State *L)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   621
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   622
    GtkWidget *widget = (GtkWidget *) lua_touserdata(L, 1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   623
    //gtk_container_resize_children(GTK_CONTAINER(vbox));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   624
    gtk_widget_show(widget);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   625
    GtkWidget *toplevel = gtk_widget_get_toplevel(widget);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   626
    gtk_window_present(GTK_WINDOW(toplevel));
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   627
    return 0;
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   628
} // guiShowWindow
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   629
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   630
static int guiCreateSubMenu(lua_State *L)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   631
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   632
    GtkWidget *widget = (GtkWidget *) lua_touserdata(L, 1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   633
    GtkWidget *topwindow = gtk_widget_get_toplevel(widget);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   634
    GtkWindow *window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   635
    gtk_window_set_keep_above(window, TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   636
    gtk_window_set_skip_taskbar_hint(window, TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   637
    gtk_window_set_skip_pager_hint(window, TRUE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   638
    //g_signal_connect(window, "destroy", G_CALLBACK(destroySubMenu), topwindow);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   639
    gtk_window_set_decorated(window, FALSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   640
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   641
    GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(FALSE, 0));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   642
    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(vbox));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   643
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   644
    // line the new submenu up...
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   645
    //  !!! FIXME: if overflow off right end of screen, go to the left of (widget) instead.
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   646
    gint basex, basey, x, y;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   647
    gtk_window_get_position(GTK_WINDOW(topwindow), &basex, &basey);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   648
    gtk_widget_translate_coordinates(widget, topwindow, 0, 0, &x, &y);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   649
    x += basex;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   650
    y += basey;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   651
    x += widget->allocation.width;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   652
    gtk_window_move(window, x, y);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   653
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   654
    return retvalPointer(L, vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   655
} // guiCreateSubMenu
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   656
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   657
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   658
static int setPowermateLED_Lua(lua_State *L)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   659
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   660
    const int enable = lua_toboolean(L, 1);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   661
    setPowermateLED(enable);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   662
    return 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   663
} // setPowermateLED_Lua
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   664
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   665
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   666
static void keyhookPressed(void)
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   667
{
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   668
    lua_getglobal(luaState, "keyhookPressed");
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   669
    lua_call(luaState, 0, 0);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   670
} // keyhookPressed
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   671
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   672
29
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   673
static int pumpLua(void)
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   674
{
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   675
    lua_getglobal(luaState, "pumpLua");
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   676
    lua_call(luaState, 0, 0);
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   677
} // pumpLua
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   678
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   679
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   680
static gboolean keyhookPumper(void *arg)
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   681
{
29
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   682
    pumpLua();
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   683
    if (pumpKeyHook())
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   684
        keyhookPressed();
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   685
    else if (pumpPowermate())
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   686
        keyhookPressed();
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   687
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   688
    return TRUE;  // keep firing timer
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   689
} // keyhookPumper
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   690
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   691
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   692
static int giveControlToGui(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   693
{
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   694
    if (initKeyHook())
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   695
    {
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   696
        atexit(deinitKeyHook);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   697
        g_timeout_add(200, (GSourceFunc) keyhookPumper, (gpointer) NULL);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   698
    } // if
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   699
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   700
    gtk_main();
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   701
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   702
} // giveControlToGui
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   703
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   704
46
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   705
static int getMountedDisks(lua_State *L)
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   706
{
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   707
    lua_newtable(L);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   708
    int luai = 1;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   709
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   710
    FILE *mounts = setmntent("/etc/mtab", "r");
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   711
    if (mounts != NULL)
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   712
    {
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   713
        struct mntent *ent = NULL;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   714
        while ((ent = getmntent(mounts)) != NULL)
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   715
        {
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   716
            lua_pushinteger(luaState, luai);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   717
            lua_pushstring(luaState, ent->mnt_dir);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   718
            lua_settable(luaState, -3);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   719
            luai++;
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   720
        } // while
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   721
        endmntent(mounts);
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   722
    } // if
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   723
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   724
    return 1;  // return the table.
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   725
} // getMountedDisks
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   726
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   727
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
    if (nsize == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
        free(ptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
        return NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
    return realloc(ptr, nsize);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
} // luaAlloc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
    lua_pushcfunction(L, f);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
    lua_setglobal(luaState, sym);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
} // luaSetCFunc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
static int luaFatal(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
    const char *errstr = lua_tostring(L, -1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
    fflush(stderr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
    exit(1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
} // luaFatal
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   755
static void deinitLua(void)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   756
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   757
    if (luaState != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   758
    {
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   759
        lua_close(luaState);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   760
        luaState = NULL;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   761
    } // if
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   762
} // deinitLua
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   763
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   764
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   765
static int initLua(const int argc, char **argv)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
{
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   767
    atexit(deinitLua);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   768
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
    assert(luaState == NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
    luaState = lua_newstate(luaAlloc, NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
    lua_atpanic(luaState, luaFatal);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
    assert(lua_checkstack(luaState, 20));  // Just in case.
2
16a2d269fd41 Just use the built-in luaL_openlibs()
Ryan C. Gordon <icculus@icculus.org>
parents: 1
diff changeset
   774
    luaL_openlibs(luaState);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
    // Set up initial C functions, etc we want to expose to Lua code...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
56
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 46
diff changeset
   779
    luaSetCFunc(luaState, decryptTopt, "decryptTopt");
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   780
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   781
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   782
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   783
    luaSetCFunc(luaState, setPowermateLED_Lua, "setPowermateLED");
46
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   784
    luaSetCFunc(luaState, calcSha256_Lua, "calcSha256");
fe4f59680246 Added experimental support for using a "trusted device."
Ryan C. Gordon <icculus@icculus.org>
parents: 45
diff changeset
   785
    luaSetCFunc(luaState, getMountedDisks, "getMountedDisks");
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   787
    luaSetCFunc(luaState, guiCreateTopLevelMenu, "guiCreateTopLevelMenu");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   788
    luaSetCFunc(luaState, guiCreateSubMenu, "guiCreateSubMenu");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   789
    luaSetCFunc(luaState, guiAddMenuItem, "guiAddMenuItem");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   790
    luaSetCFunc(luaState, guiRemoveAllMenuItems, "guiRemoveAllMenuItems");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   791
    luaSetCFunc(luaState, guiDestroyMenu, "guiDestroyMenu");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   792
    luaSetCFunc(luaState, guiShowWindow, "guiShowWindow");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   793
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   794
    // Set up argv table...
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   795
    lua_newtable(luaState);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   796
    int i;
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   797
    int luai = 1;
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   798
    for (i = 0; i < argc; i++)
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   799
    {
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   800
        if (argv[i])
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   801
        {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   802
            lua_pushinteger(luaState, luai);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   803
            lua_pushstring(luaState, argv[i]);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   804
            lua_settable(luaState, -3);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   805
            luai++;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   806
        } // if
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   807
    } // for
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   808
    lua_setglobal(luaState, "argv");
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   809
3
32af809e8c0b Corrected comment. It's basically just moving to Lua as the mainline.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   810
    // Transfer control to Lua...
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   811
    if (luaL_dofile(luaState, "1pass.lua") != 0)
16
f5a7ede6c1f3 Use luaFatal() if luaL_dofile() explodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 15
diff changeset
   812
        luaFatal(luaState);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   814
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
} // initLua
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   816
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   817
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   818
static void deinitAll()
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   819
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   820
    deinitPowermate();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   821
    deinitKeyHook();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   822
    deinitLua();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   823
} // deinitAll
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   824
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   825
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   826
static void killerSignalCatcher(int sig)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   827
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   828
    static int been_run = 0;
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   829
    if (been_run)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   830
    {
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   831
        fprintf(stderr, "Caught signal %d, terminating HARD.\n", sig);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   832
        _exit(0);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   833
    } // if
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   834
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   835
    been_run = 1;
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   836
    fprintf(stderr, "Caught signal %d, terminating.\n", sig);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   837
    exit(0);  // trigger atexit handlers.
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   838
} // killerSignalCatcher
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   839
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   840
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   841
static void initSignals(void)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   842
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   843
    signal(SIGINT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   844
    signal(SIGQUIT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   845
    signal(SIGILL, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   846
    signal(SIGFPE, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   847
    signal(SIGTERM, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   848
    signal(SIGPIPE, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   849
    signal(SIGSEGV, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   850
    signal(SIGABRT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   851
    signal(SIGHUP, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   852
} // initSignals
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   853
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   854
39
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   855
// this was from PhysicsFS ( https://icculus.org/physfs/ ), zlib license.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   856
static char *readSymLink(const char *path)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   857
{
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   858
    ssize_t len = 64;
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   859
    ssize_t rc = -1;
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   860
    char *retval = NULL;
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   861
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   862
    while (1)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   863
    {
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   864
         char *ptr = (char *) realloc(retval, (size_t) len);
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   865
         if (ptr == NULL)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   866
             break;   // out of memory.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   867
         retval = ptr;
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   868
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   869
         rc = readlink(path, retval, len);
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   870
         if (rc == -1)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   871
             break;  // not a symlink, i/o error, etc.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   872
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   873
         else if (rc < len)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   874
         {
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   875
             retval[rc] = '\0';  // readlink doesn't null-terminate.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   876
             return retval;  // we're good to go.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   877
         } // else if
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   878
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   879
         len *= 2;  // grow buffer, try again.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   880
    } // while
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   881
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   882
    if (retval != NULL)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   883
        free(retval);
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   884
    return NULL;
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   885
} // readSymLink
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   886
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   887
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   888
static void chdirToApp(void)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   889
{
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   890
    char *ptr;
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   891
    char *path = readSymLink("/proc/self/exe");
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   892
    
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   893
    if (path == NULL)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   894
        return;  // maybe we're already there, fail later if not.
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   895
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   896
    ptr = strrchr(path, '/');
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   897
    if (ptr != NULL)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   898
    {
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   899
        *ptr = '\0';
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   900
        if (chdir(path) == -1)
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   901
            fprintf(stderr, "Couldn't chdir() to app directory?! %s\n", strerror(errno));
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   902
    } // if
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   903
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   904
    free(path);
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   905
} // chdirToApp
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   906
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   907
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   908
int main(int argc, char **argv)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   909
{
39
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   910
    chdirToApp();
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   911
    initSignals();
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   912
    initPowermate(&argc, argv);
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   913
    gtk_init(&argc, &argv);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   914
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   915
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
        return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   918
    return 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
} // main
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   920
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   921
// end of 1pass.c ...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   922