1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 26 Apr 2014 16:09:29 -0400
changeset 34 55a2b89a50cd
parent 33 bc1736f8e74d
child 39 ddb45e88adc9
permissions -rw-r--r--
Whoops, wrong comparison.
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>
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    11
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"
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
    19
#include "keyhook.h"
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    20
#include <gtk/gtk.h>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    24
// 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
    25
//  --powermate=/dev/input/eventX
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    26
static int powermate_fd = -1;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    27
static int pumpPowermate(void)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    28
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    29
    struct input_event buf[32];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    30
    int pressed = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    31
    ssize_t br;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    32
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    33
    if (powermate_fd == -1)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    34
        return 0;  // nothing to do.
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    35
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    36
    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
    37
    {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    38
        ssize_t i;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    39
        br /= sizeof (buf[0]);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    40
        for (i = 0; i < br; i++)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    41
        {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    42
            struct input_event *ev = &buf[i];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    43
            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
    44
                pressed = 1;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    45
        } // for
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    46
    } // while
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    47
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    48
    return pressed;
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
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    51
static void setPowermateLED(const int enable)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    52
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    53
    struct input_event ev;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    54
    const int brightness = enable ? 255 : 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    55
    const int pulse_speed = 255;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    56
    const int pulse_table = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    57
    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
    58
    const int pulse_asleep = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    59
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    60
    if (powermate_fd == -1)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    61
        return;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    62
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    63
    memset(&ev, '\0', sizeof (ev));
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    64
    ev.type = EV_MSC;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    65
    ev.code = MSC_PULSELED;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    66
    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
    67
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    68
    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
    69
        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
    70
} // setPowermateLED
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    71
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    72
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    73
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
    74
{
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    75
    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
    76
        "Griffin PowerMate", "Griffin SoundKnob"
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    77
    };
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    78
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    79
    char buf[255];
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    80
    int ok = 0;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    81
    int fd;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    82
    int i;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    83
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    84
    if (!fname)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    85
        return -1;
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
    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
    88
        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
    89
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    90
    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
    91
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    92
        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
    93
        close(fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    94
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    95
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    96
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    97
    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
    98
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    99
        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
   100
            ok = 1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   101
    } // for
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   102
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   103
    if (!ok)
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
        close(fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   106
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   107
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   108
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   109
    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
   110
    return fd;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   111
} // openPowermate
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
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   114
static void deinitPowermate(void)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   115
{
34
55a2b89a50cd Whoops, wrong comparison.
Ryan C. Gordon <icculus@icculus.org>
parents: 33
diff changeset
   116
    if (powermate_fd != -1)
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   117
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   118
        setPowermateLED(0);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   119
        close(powermate_fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   120
        powermate_fd = -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   121
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   122
} // deinitPowermate
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   123
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   124
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   125
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
   126
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   127
    const char *arg = "--powermate=";
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   128
    const size_t arglen = strlen(arg);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   129
    int argc = *_argc;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   130
    int i;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   131
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   132
    for (i = 1; i < argc; i++)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   133
    {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   134
        const char *thisarg = argv[i];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   135
        if (strncmp(thisarg, arg, arglen) != 0)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   136
            continue;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   137
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   138
        thisarg += arglen;
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   139
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   140
        if (strcmp(thisarg, "auto") == 0)
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   141
        {
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   142
            DIR *dirp = opendir("/dev/input");
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   143
            if (dirp)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   144
            {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   145
                struct dirent *dent;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   146
                while ((dent = readdir(dirp)) != NULL)
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
                    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
   149
                    char buf[PATH_MAX];
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   150
                    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
   151
                        continue;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   152
                    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
   153
                    if (powermate_fd == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   154
                    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   155
                        powermate_fd = openPowermate(buf);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   156
                        if (powermate_fd != -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   157
                        {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   158
                            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
   159
                            break;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   160
                        } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   161
                    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   162
                } // while
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   163
                closedir(dirp);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   164
            } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   165
            thisarg = NULL;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   166
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   167
        } // if
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   168
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   169
        // eliminate this command line.
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   170
        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
   171
        argc--;
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   172
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   173
        if (powermate_fd == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   174
            powermate_fd = openPowermate(thisarg);
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   175
    } // for
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   176
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   177
    atexit(deinitPowermate);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   178
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   179
    *_argc = argc;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   180
} // initPowermate
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   181
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   182
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
static lua_State *luaState = NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
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
   185
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
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
   188
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    if (str != NULL)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
        lua_pushlstring(L, (const char *) str, len);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
} // retvalStringBytes
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   196
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
   197
{
15
e2973f4fb366 Fixed crash.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   198
    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
   199
} // retvalString
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   200
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   201
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
   202
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   203
    if (ptr != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   204
        lua_pushlightuserdata(L, ptr);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   205
    else
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   206
        lua_pushnil(L);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   207
    return 1;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   208
} // retvalPointer
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   209
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    int i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    for (i = 0; i < 16; i++, dst++, src++)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        *dst ^= *src;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
} // xorBlock
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
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
   218
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
    const size_t blocks = *datalen / 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
    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
   221
    const uint8_t *padding = &block[15];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    uint8_t expkey[aesExpandedKeySize];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    size_t i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    if (blocks == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
        return 1;  // nothing to do.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
	aesExpandKey(key, expkey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
    for (i = 0; i < blocks-1; i++)
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
        aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
        xorBlock(block, block-16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
        block -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    xorBlock(block, iv);   // xor against initial vector for final block.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    if (*padding > 16)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        return 0;  // bad data?
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    *datalen -= *padding;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
} // decryptBinaryUsingKeyAndIvec
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
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
static inline int isSalted(const uint8_t *data, const size_t datalen)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    return ( (datalen > sizeof (saltprefix)) &&
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
} // isSalted
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
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
static int decryptUsingPBKDF2(lua_State *L)
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
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
    const char *password = luaL_checkstring(L, 2);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    const int iterations = luaL_checkinteger(L, 3);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    base64_decodestate base64state;
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
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    const uint8_t *salt = zero16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    int saltlen = sizeof (zero16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    if (isSalted(data, datalen))
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
        salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    uint8_t output[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    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
   280
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    const uint8_t *aeskey = &output[0];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    const uint8_t *aesiv = &output[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
} // decryptUsingPBKDF2
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
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
static int decryptBase64UsingKey(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    size_t keylen = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    base64_decodestate base64state;
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
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    uint8_t aeskey[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
    uint8_t aesiv[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    MD5_CTX md5;
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
    if (isSalted(data, datalen))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
        const uint8_t *salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
        const size_t saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
        assert(aesNr == 10);  // AES-256 needs more rounds.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        assert(aesNk == 4);   // hashing size is hardcoded later.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        uint8_t hashing[32];
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
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
        MD5_finish(&md5, hashing);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        MD5_append(&md5, hashing, 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        MD5_finish(&md5, &hashing[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
        memcpy(aeskey, hashing, 4 * aesNk);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
        memcpy(aesiv, &hashing[4 * aesNk], 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        MD5_finish(&md5, aeskey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        memset(aesiv, '\0', sizeof (aesiv));
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    } // else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
} // decryptBase64UsingKey
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
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   353
static int runGuiPasswordPrompt(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   354
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   355
    const char *hintstr = lua_tostring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   356
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   357
                            "Master Password", NULL, GTK_DIALOG_MODAL,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   358
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   359
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   360
                            NULL);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   361
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   362
    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
   363
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   364
    if (hintstr != NULL)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   365
    {
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   366
        GtkWidget *label = gtk_label_new(hintstr);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   367
        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
   368
        gtk_container_add(GTK_CONTAINER(content_area), label);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   369
    } // if
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   370
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   371
    GtkWidget *entry = gtk_entry_new();
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   372
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   373
    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
   374
    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
   375
    gtk_container_add(GTK_CONTAINER(content_area), entry);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   376
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   377
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   378
    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
   379
    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
   380
    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
   381
    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
   382
    gtk_widget_destroy(dialog);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   383
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   384
    return 1;
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   385
} // runGuiPasswordPrompt
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   386
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   387
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   388
static int copyToClipboard(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   389
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   390
    const char *str = luaL_checkstring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   391
    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
   392
    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
   393
} // copyToClipboard
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   394
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   395
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   396
static int makeGuiMenu(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   397
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   398
    return retvalPointer(L, gtk_menu_new());
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   399
} // makeGuiMenu
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   400
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   401
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   402
static void clickedMenuItem(void *arg)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   403
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   404
    // This is the callback from GTK+; now call into our actual Lua callback!
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   405
    const int callback = (int) ((size_t)arg);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   406
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   407
    lua_call(luaState, 0, 0);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   408
} // clickedMenuItem
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   409
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   410
#if 0  // !!! FIXME: figure out how to fire this.
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   411
static void deletedMenuItem(void *arg)
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
    // Clean up the Lua function we referenced in the Registry.
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   414
    const int callback = (int) ((size_t)arg);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   415
printf("unref callback %d\n", callback);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   416
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   417
} // deletedMenuItem
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   418
#endif
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   419
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   420
static int appendGuiMenuItem(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   421
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   422
    const int argc = lua_gettop(L);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   423
    GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   424
    const char *label = luaL_checkstring(L, 2);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   425
    GtkWidget *item = gtk_menu_item_new_with_label(label);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   426
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   427
    if ((argc >= 3) && (!lua_isnil(L, 3)))
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   428
    {
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   429
        assert(lua_isfunction(L, 3));
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   430
        lua_pushvalue(L, 3);  // copy the Lua callback (luaL_ref() pops it).
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   431
        const int callback = luaL_ref(L, LUA_REGISTRYINDEX);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   432
        gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback));
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   433
    } // if
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   434
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   435
    gtk_widget_show(item);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   436
    gtk_menu_append(menu, item);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   437
    return retvalPointer(L, item);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   438
} // appendGuiMenuItem
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   439
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   440
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   441
static int setGuiMenuItemSubmenu(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   442
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   443
    GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   444
    GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   445
    gtk_menu_item_set_submenu(item, submenu);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   446
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   447
} // setGuiMenuItemSubmenu
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   448
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   449
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   450
static int popupGuiMenu(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   451
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   452
    GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   453
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   454
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   455
} // popupGuiMenu
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   456
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   457
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   458
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
   459
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   460
    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
   461
    setPowermateLED(enable);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   462
    return 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   463
} // setPowermateLED_Lua
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   464
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   465
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   466
static void keyhookPressed(void)
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   467
{
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   468
    lua_getglobal(luaState, "keyhookPressed");
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   469
    lua_call(luaState, 0, 0);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   470
} // keyhookPressed
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   471
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   472
29
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   473
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
   474
{
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   475
    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
   476
    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
   477
} // pumpLua
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   478
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   479
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   480
static gboolean keyhookPumper(void *arg)
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   481
{
29
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   482
    pumpLua();
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   483
    if (pumpKeyHook())
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   484
        keyhookPressed();
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   485
    else if (pumpPowermate())
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   486
        keyhookPressed();
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   487
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   488
    return TRUE;  // keep firing timer
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   489
} // keyhookPumper
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   490
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   491
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   492
static int giveControlToGui(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   493
{
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   494
    if (initKeyHook())
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   495
    {
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   496
        atexit(deinitKeyHook);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   497
        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
   498
    } // if
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   499
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   500
    gtk_main();
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   501
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   502
} // giveControlToGui
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   503
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   504
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
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
   506
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    if (nsize == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
        free(ptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
        return NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
    return realloc(ptr, nsize);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
} // luaAlloc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
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
   517
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
    lua_pushcfunction(L, f);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    lua_setglobal(luaState, sym);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
} // luaSetCFunc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   521
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
static int luaFatal(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
    const char *errstr = lua_tostring(L, -1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
    fflush(stderr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
    exit(1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
} // luaFatal
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   532
static void deinitLua(void)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   533
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   534
    if (luaState != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   535
    {
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   536
        lua_close(luaState);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   537
        luaState = NULL;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   538
    } // if
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   539
} // deinitLua
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   540
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   541
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   542
static int initLua(const int argc, char **argv)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
{
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   544
    atexit(deinitLua);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   545
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
    assert(luaState == NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
    luaState = lua_newstate(luaAlloc, NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
    lua_atpanic(luaState, luaFatal);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
    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
   551
    luaL_openlibs(luaState);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
    // 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
   554
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   556
    luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   557
    luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   558
    luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   559
    luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   560
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   561
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   562
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   563
    luaSetCFunc(luaState, setPowermateLED_Lua, "setPowermateLED");
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   565
    // Set up argv table...
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   566
    lua_newtable(luaState);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   567
    int i;
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   568
    int luai = 1;
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   569
    for (i = 0; i < argc; i++)
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   570
    {
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   571
        if (argv[i])
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   572
        {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   573
            lua_pushinteger(luaState, luai);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   574
            lua_pushstring(luaState, argv[i]);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   575
            lua_settable(luaState, -3);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   576
            luai++;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   577
        } // if
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   578
    } // for
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   579
    lua_setglobal(luaState, "argv");
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   580
3
32af809e8c0b Corrected comment. It's basically just moving to Lua as the mainline.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   581
    // Transfer control to Lua...
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
    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
   583
        luaFatal(luaState);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
} // initLua
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   589
static void deinitAll()
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   590
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   591
    deinitPowermate();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   592
    deinitKeyHook();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   593
    deinitLua();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   594
} // deinitAll
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   595
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   596
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   597
static void killerSignalCatcher(int sig)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   598
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   599
    static int been_run = 0;
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   600
    if (been_run)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   601
    {
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   602
        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
   603
        _exit(0);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   604
    } // if
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   605
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   606
    been_run = 1;
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   607
    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
   608
    exit(0);  // trigger atexit handlers.
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   609
} // killerSignalCatcher
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   610
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   611
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   612
static void initSignals(void)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   613
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   614
    signal(SIGINT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   615
    signal(SIGQUIT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   616
    signal(SIGILL, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   617
    signal(SIGFPE, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   618
    signal(SIGTERM, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   619
    signal(SIGPIPE, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   620
    signal(SIGSEGV, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   621
    signal(SIGABRT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   622
    signal(SIGHUP, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   623
} // initSignals
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   624
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   625
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
int main(int argc, char **argv)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
{
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   628
    initSignals();
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   629
    initPowermate(&argc, argv);
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   630
    gtk_init(&argc, &argv);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   632
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
        return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
    return 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
} // main
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
// end of 1pass.c ...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639