1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 17 Oct 2015 20:24:39 -0400
branchgtkui
changeset 45 cf6a06f368e6
parent 39 ddb45e88adc9
child 46 fe4f59680246
permissions -rw-r--r--
Moved to a more robust GTK UI. This removes the popup windows, in favor of something searchable, etc, but it's not without its problems, too, so it's sitting in branch until I decide I really like it or I should replace it with SDL or something. :) (If someone can figure out how to get the window to steal focus when popped up, that would fix the most egregious issue, but there are other small details that need fixing, too.)
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"
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    20
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
    21
#include <gtk/gtk.h>
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    22
#include <gdk/gdk.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    23
#include <gdk/gdkx.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    24
#include <gdk/gdkkeysyms.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    25
#include <X11/Xlib.h>
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
    26
#include <X11/Xlibint.h>
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    30
// 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
    31
//  --powermate=/dev/input/eventX
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    32
static int powermate_fd = -1;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    33
static int pumpPowermate(void)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    34
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    35
    struct input_event buf[32];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    36
    int pressed = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    37
    ssize_t br;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    38
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    39
    if (powermate_fd == -1)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    40
        return 0;  // nothing to do.
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
    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
    43
    {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    44
        ssize_t i;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    45
        br /= sizeof (buf[0]);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    46
        for (i = 0; i < br; i++)
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
            struct input_event *ev = &buf[i];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    49
            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
    50
                pressed = 1;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    51
        } // for
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    52
    } // while
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    53
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    54
    return pressed;
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
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    57
static void setPowermateLED(const int enable)
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
    struct input_event ev;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    60
    const int brightness = enable ? 255 : 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    61
    const int pulse_speed = 255;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    62
    const int pulse_table = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    63
    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
    64
    const int pulse_asleep = 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    65
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    66
    if (powermate_fd == -1)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    67
        return;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    68
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    69
    memset(&ev, '\0', sizeof (ev));
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    70
    ev.type = EV_MSC;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    71
    ev.code = MSC_PULSELED;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    72
    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
    73
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    74
    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
    75
        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
    76
} // setPowermateLED
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    77
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
    78
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    79
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
    80
{
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    81
    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
    82
        "Griffin PowerMate", "Griffin SoundKnob"
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
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    85
    char buf[255];
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    86
    int ok = 0;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    87
    int fd;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    88
    int i;
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 (!fname)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    91
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    92
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    93
    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
    94
        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
    95
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    96
    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
    97
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
    98
        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
    99
        close(fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   100
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   101
    } // if
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
    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
   104
    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   105
        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
   106
            ok = 1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   107
    } // for
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
    if (!ok)
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
        close(fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   112
        return -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   113
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   114
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   115
    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
   116
    return fd;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   117
} // openPowermate
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   118
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   119
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   120
static void deinitPowermate(void)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   121
{
34
55a2b89a50cd Whoops, wrong comparison.
Ryan C. Gordon <icculus@icculus.org>
parents: 33
diff changeset
   122
    if (powermate_fd != -1)
31
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
        setPowermateLED(0);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   125
        close(powermate_fd);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   126
        powermate_fd = -1;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   127
    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   128
} // deinitPowermate
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   129
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   130
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   131
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
   132
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   133
    const char *arg = "--powermate=";
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   134
    const size_t arglen = strlen(arg);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   135
    int argc = *_argc;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   136
    int i;
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
    for (i = 1; i < argc; 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
        const char *thisarg = argv[i];
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   141
        if (strncmp(thisarg, arg, arglen) != 0)
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   142
            continue;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   143
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   144
        thisarg += arglen;
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   145
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   146
        if (strcmp(thisarg, "auto") == 0)
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   147
        {
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   148
            DIR *dirp = opendir("/dev/input");
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   149
            if (dirp)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   150
            {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   151
                struct dirent *dent;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   152
                while ((dent = readdir(dirp)) != NULL)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   153
                {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   154
                    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
   155
                    char buf[PATH_MAX];
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   156
                    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
   157
                        continue;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   158
                    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
   159
                    if (powermate_fd == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   160
                    {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   161
                        powermate_fd = openPowermate(buf);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   162
                        if (powermate_fd != -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   163
                        {
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   164
                            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
   165
                            break;
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   166
                        } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   167
                    } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   168
                } // while
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   169
                closedir(dirp);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   170
            } // if
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   171
            thisarg = NULL;
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
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   174
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   175
        // eliminate this command line.
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   176
        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
   177
        argc--;
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   178
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   179
        if (powermate_fd == -1)
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   180
            powermate_fd = openPowermate(thisarg);
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   181
    } // for
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   182
31
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   183
    atexit(deinitPowermate);
838492d40709 More Powermate stuff: clean up atexit, autodetect devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 29
diff changeset
   184
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   185
    *_argc = argc;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   186
} // initPowermate
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   187
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   188
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
static lua_State *luaState = NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
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
   191
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   193
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
   194
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   195
    assert(lua_isfunction(L, idx));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   196
    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
   197
    return luaL_ref(L, LUA_REGISTRYINDEX);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   198
} // makeLuaCallback
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   199
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
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
   201
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    if (str != NULL)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        lua_pushlstring(L, (const char *) str, len);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
} // retvalStringBytes
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   209
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
   210
{
15
e2973f4fb366 Fixed crash.
Ryan C. Gordon <icculus@icculus.org>
parents: 12
diff changeset
   211
    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
   212
} // retvalString
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   213
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   214
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
   215
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   216
    if (ptr != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   217
        lua_pushlightuserdata(L, ptr);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   218
    else
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   219
        lua_pushnil(L);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   220
    return 1;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   221
} // retvalPointer
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   222
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
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
    int i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    for (i = 0; i < 16; i++, dst++, src++)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
        *dst ^= *src;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
} // xorBlock
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
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
   231
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    const size_t blocks = *datalen / 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
    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
   234
    const uint8_t *padding = &block[15];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
    uint8_t expkey[aesExpandedKeySize];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    size_t i;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
    if (blocks == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
        return 1;  // nothing to do.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
	aesExpandKey(key, expkey);
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
    for (i = 0; i < blocks-1; i++)
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
        aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        xorBlock(block, block-16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
        block -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    }
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    aesDecrypt(block, expkey, block);   // decrypt in place.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    xorBlock(block, iv);   // xor against initial vector for final block.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    if (*padding > 16)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
        return 0;  // bad data?
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
    *datalen -= *padding;
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
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
} // decryptBinaryUsingKeyAndIvec
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
static inline int isSalted(const uint8_t *data, const size_t datalen)
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
    return ( (datalen > sizeof (saltprefix)) &&
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
} // isSalted
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
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
static int decryptUsingPBKDF2(lua_State *L)
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
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
    const char *password = luaL_checkstring(L, 2);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
    const int iterations = luaL_checkinteger(L, 3);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    base64_decodestate base64state;
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
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
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 *salt = zero16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    int saltlen = sizeof (zero16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
    if (isSalted(data, datalen))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
        salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
        saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    uint8_t output[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    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
   293
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    const uint8_t *aeskey = &output[0];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    const uint8_t *aesiv = &output[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
} // decryptUsingPBKDF2
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
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
static int decryptBase64UsingKey(lua_State *L)
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
    size_t keylen = 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    const char *base64 = luaL_checkstring(L, 1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    size_t datalen = strlen(base64);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    uint8_t *data = dataptr;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    base64_decodestate base64state;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    base64_init_decodestate(&base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
    uint8_t aeskey[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    uint8_t aesiv[16];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    MD5_CTX md5;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    if (isSalted(data, datalen))
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
        const uint8_t *salt = data + 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
        const size_t saltlen = 8;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        data += 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        datalen -= 16;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
        assert(aesNr == 10);  // AES-256 needs more rounds.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
        assert(aesNk == 4);   // hashing size is hardcoded later.
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
        uint8_t hashing[32];
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
        MD5_finish(&md5, hashing);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        MD5_append(&md5, hashing, 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
        MD5_append(&md5, salt, saltlen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
        MD5_finish(&md5, &hashing[16]);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
        memcpy(aeskey, hashing, 4 * aesNk);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
        memcpy(aesiv, &hashing[4 * aesNk], 16);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
        MD5_init(&md5);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
        MD5_append(&md5, key, keylen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
        MD5_finish(&md5, aeskey);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        memset(aesiv, '\0', sizeof (aesiv));
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    } // else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
        retvalStringBytes(L, data, datalen);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    else
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
        lua_pushnil(L);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
    free(dataptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
} // decryptBase64UsingKey
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   366
static int runGuiPasswordPrompt(lua_State *L)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   367
{
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   368
    const char *hintstr = lua_tostring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   369
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   370
                            "Master Password", NULL, GTK_DIALOG_MODAL,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   371
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   372
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   373
                            NULL);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   374
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   375
    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
   376
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   377
    if (hintstr != NULL)
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   378
    {
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   379
        GtkWidget *label = gtk_label_new(hintstr);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   380
        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
   381
        gtk_container_add(GTK_CONTAINER(content_area), label);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   382
    } // if
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
    GtkWidget *entry = gtk_entry_new();
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   385
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   386
    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
   387
    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
   388
    gtk_container_add(GTK_CONTAINER(content_area), entry);
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
    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
   391
    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
   392
    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
   393
    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
   394
    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
   395
    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
   396
    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
   397
    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
   398
    gtk_widget_destroy(dialog);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   399
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   400
    return 1;
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   401
} // runGuiPasswordPrompt
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   402
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
static int copyToClipboard(lua_State *L)
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
    const char *str = luaL_checkstring(L, 1);
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   407
    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
   408
    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
   409
} // copyToClipboard
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   410
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   411
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
   412
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   413
    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
   414
    {
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   415
        // !!! FIXME: this is a little hacky
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   416
        lua_getglobal(luaState, "escapePressed");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   417
        lua_call(luaState, 0, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   418
        return TRUE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   419
    } // if
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   420
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   421
    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
   422
} // checkForEscapeKey
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   423
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   424
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
   425
static void searchChanged(GtkEditable *editable, gpointer arg)
11
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
    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
   428
    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
   429
    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
   430
    lua_pushlightuserdata(luaState, vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   431
    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
   432
    lua_call(luaState, 2, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   433
    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
   434
    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
   435
    {
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   436
        gtk_editable_set_position(editable, -1);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   437
        wasSearchDeleteText = FALSE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   438
    } // if
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   439
} // searchChanged
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   440
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   441
// HACK to workaround gtk+ nonsense.
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   442
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
   443
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   444
    wasSearchDeleteText = TRUE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   445
} // searchDeleteText
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   446
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   447
static void destroyLuaCallback(const int callback)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   448
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   449
    //printf("unref callback %d\n", callback);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   450
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   451
} // destroyLuaCallback
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   452
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   453
static void destroyCallback(GtkWidget *widget, gpointer arg)
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   454
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   455
    destroyLuaCallback((int) ((size_t)arg));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   456
} // destroyCallback
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   457
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   458
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
   459
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   460
    // !!! FIXME: hack
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   461
    int *cbs = (int *) arg;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   462
    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
   463
    lua_call(luaState, 0, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   464
    destroyLuaCallback(cbs[0]);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   465
    destroyLuaCallback(cbs[1]);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   466
    free(cbs);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   467
} // destroyTopLevelMenu
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   468
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   469
#if 0
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   470
static gboolean
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   471
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
   472
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   473
    GdkEventClient e;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   474
    memset(&e, '\0', sizeof (e));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   475
    e.type = GDK_CLIENT_EVENT;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   476
    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
   477
    e.send_event = 1;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   478
    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
   479
    e.data_format = 32;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   480
    e.data.l[0] = 1;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   481
    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
   482
    e.data.l[2] = 0;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   483
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   484
    gdk_window_raise (e.window);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   485
    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
   486
    return TRUE;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   487
}
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   488
#endif
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   489
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   490
static int guiCreateTopLevelMenu(lua_State *L)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   491
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   492
    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
   493
    const int changedCallback = makeLuaCallback(L, 2);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   494
    const int destroyedCallback = makeLuaCallback(L, 3);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   495
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   496
    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
   497
    cbs[0] = changedCallback;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   498
    cbs[1] = destroyedCallback;
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   499
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   500
    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
   501
    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
   502
    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
   503
    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
   504
    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
   505
    gtk_window_set_title(window, title);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   506
    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
   507
    gtk_window_set_decorated(window, FALSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   508
    gtk_window_set_resizable(window, FALSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   509
    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
   510
    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
   511
    gtk_entry_set_text(search, "Search...");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   512
    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
   513
    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
   514
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   515
//    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
   516
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   517
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   518
    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
   519
    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
   520
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   521
    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
   522
    gtk_widget_show(GTK_WIDGET(search));
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   523
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   524
    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
   525
    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
   526
    gtk_widget_show(vsep);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   527
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   528
    return retvalPointer(L, vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   529
} // guiCreateTopLevelMenu
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   530
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   531
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
   532
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   533
    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
   534
    lua_pushlightuserdata(luaState, button);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   535
    lua_call(luaState, 1, 0);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   536
} // clickedMenuItem
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   537
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   538
static int guiAddMenuItem(lua_State *L)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   539
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   540
    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
   541
    const char *label = luaL_checkstring(L, 2);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   542
    const int callback = makeLuaCallback(L, 3);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   543
    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
   544
    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
   545
    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
   546
    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
   547
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   548
    //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
   549
    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
   550
    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
   551
    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
   552
    gtk_widget_show(item);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   553
    return retvalPointer(L, item);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   554
} // guiAddMenuItem
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   555
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   556
static int guiRemoveAllMenuItems(lua_State *L)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   557
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   558
    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
   559
    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
   560
    GList *iter;
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   561
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   562
    gtk_widget_hide(vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   563
    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
   564
    {
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   565
        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
   566
             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
   567
    } // for
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   568
    g_list_free(children);
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   569
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   570
    return 0;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   571
} // guiRemoveAllMenuItems
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   572
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   573
static int guiDestroyMenu(lua_State *L)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   574
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   575
    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
   576
    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
   577
    return 0;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   578
} // guiDestroyMenu
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   579
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   580
static int guiShowWindow(lua_State *L)
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   581
{
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   582
    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
   583
    //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
   584
    gtk_widget_show(widget);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   585
    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
   586
    gtk_window_present(GTK_WINDOW(toplevel));
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   587
    return 0;
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   588
} // guiShowWindow
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   589
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   590
static int guiCreateSubMenu(lua_State *L)
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   591
{
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   592
    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
   593
    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
   594
    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
   595
    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
   596
    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
   597
    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
   598
    //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
   599
    gtk_window_set_decorated(window, FALSE);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   600
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   601
    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
   602
    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
   603
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   604
    // line the new submenu up...
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   605
    //  !!! 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
   606
    gint basex, basey, x, y;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   607
    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
   608
    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
   609
    x += basex;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   610
    y += basey;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   611
    x += widget->allocation.width;
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   612
    gtk_window_move(window, x, y);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   613
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   614
    return retvalPointer(L, vbox);
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   615
} // guiCreateSubMenu
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   616
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   617
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   618
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
   619
{
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   620
    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
   621
    setPowermateLED(enable);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   622
    return 0;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   623
} // setPowermateLED_Lua
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   624
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   625
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   626
static void keyhookPressed(void)
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   627
{
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   628
    lua_getglobal(luaState, "keyhookPressed");
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   629
    lua_call(luaState, 0, 0);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   630
} // keyhookPressed
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   631
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   632
29
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   633
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
   634
{
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   635
    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
   636
    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
   637
} // pumpLua
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   638
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   639
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   640
static gboolean keyhookPumper(void *arg)
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   641
{
29
769393898e93 Forcibly lock keychain on timeout instead of checking timeout on next access.
Ryan C. Gordon <icculus@icculus.org>
parents: 28
diff changeset
   642
    pumpLua();
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   643
    if (pumpKeyHook())
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   644
        keyhookPressed();
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   645
    else if (pumpPowermate())
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   646
        keyhookPressed();
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   647
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   648
    return TRUE;  // keep firing timer
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   649
} // keyhookPumper
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   650
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   651
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   652
static int giveControlToGui(lua_State *L)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   653
{
17
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   654
    if (initKeyHook())
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   655
    {
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   656
        atexit(deinitKeyHook);
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   657
        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
   658
    } // if
e884dbb403cc Added keyhook code, reworked things to use it.
Ryan C. Gordon <icculus@icculus.org>
parents: 16
diff changeset
   659
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   660
    gtk_main();
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   661
    return 0;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   662
} // giveControlToGui
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   663
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   664
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
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
   666
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
    if (nsize == 0)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
    {
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
        free(ptr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
        return NULL;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    } // if
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
    return realloc(ptr, nsize);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
} // luaAlloc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
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
   677
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
    lua_pushcfunction(L, f);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
    lua_setglobal(luaState, sym);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
} // luaSetCFunc
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
static int luaFatal(lua_State *L)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
{
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
    const char *errstr = lua_tostring(L, -1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
    fflush(stderr);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
    exit(1);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
} // luaFatal
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   692
static void deinitLua(void)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   693
{
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   694
    if (luaState != NULL)
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   695
    {
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   696
        lua_close(luaState);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   697
        luaState = NULL;
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   698
    } // if
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   699
} // deinitLua
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   700
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   701
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   702
static int initLua(const int argc, char **argv)
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
{
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   704
    atexit(deinitLua);
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   705
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    assert(luaState == NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
    luaState = lua_newstate(luaAlloc, NULL);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
    lua_atpanic(luaState, luaFatal);
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
    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
   711
    luaL_openlibs(luaState);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
    // 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
   714
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   716
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
12
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   717
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
6a2d5b34d5ca Whole bunch of GUI work.
Ryan C. Gordon <icculus@icculus.org>
parents: 11
diff changeset
   718
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   719
    luaSetCFunc(luaState, setPowermateLED_Lua, "setPowermateLED");
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
45
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   721
    luaSetCFunc(luaState, guiCreateTopLevelMenu, "guiCreateTopLevelMenu");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   722
    luaSetCFunc(luaState, guiCreateSubMenu, "guiCreateSubMenu");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   723
    luaSetCFunc(luaState, guiAddMenuItem, "guiAddMenuItem");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   724
    luaSetCFunc(luaState, guiRemoveAllMenuItems, "guiRemoveAllMenuItems");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   725
    luaSetCFunc(luaState, guiDestroyMenu, "guiDestroyMenu");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   726
    luaSetCFunc(luaState, guiShowWindow, "guiShowWindow");
cf6a06f368e6 Moved to a more robust GTK UI.
Ryan C. Gordon <icculus@icculus.org>
parents: 39
diff changeset
   727
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   728
    // Set up argv table...
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   729
    lua_newtable(luaState);
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   730
    int i;
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   731
    int luai = 1;
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   732
    for (i = 0; i < argc; i++)
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   733
    {
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   734
        if (argv[i])
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   735
        {
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   736
            lua_pushinteger(luaState, luai);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   737
            lua_pushstring(luaState, argv[i]);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   738
            lua_settable(luaState, -3);
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   739
            luai++;
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   740
        } // if
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   741
    } // for
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   742
    lua_setglobal(luaState, "argv");
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   743
3
32af809e8c0b Corrected comment. It's basically just moving to Lua as the mainline.
Ryan C. Gordon <icculus@icculus.org>
parents: 2
diff changeset
   744
    // Transfer control to Lua...
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
    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
   746
        luaFatal(luaState);
0
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
    return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
} // initLua
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   752
static void deinitAll()
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   753
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   754
    deinitPowermate();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   755
    deinitKeyHook();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   756
    deinitLua();
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   757
} // deinitAll
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   758
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   759
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   760
static void killerSignalCatcher(int sig)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   761
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   762
    static int been_run = 0;
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   763
    if (been_run)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   764
    {
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   765
        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
   766
        _exit(0);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   767
    } // if
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   768
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   769
    been_run = 1;
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   770
    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
   771
    exit(0);  // trigger atexit handlers.
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   772
} // killerSignalCatcher
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   773
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   774
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   775
static void initSignals(void)
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   776
{
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   777
    signal(SIGINT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   778
    signal(SIGQUIT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   779
    signal(SIGILL, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   780
    signal(SIGFPE, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   781
    signal(SIGTERM, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   782
    signal(SIGPIPE, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   783
    signal(SIGSEGV, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   784
    signal(SIGABRT, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   785
    signal(SIGHUP, killerSignalCatcher);
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   786
} // initSignals
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   787
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   788
39
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   789
// 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
   790
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
   791
{
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   792
    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
   793
    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
   794
    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
   795
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   796
    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
   797
    {
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   798
         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
   799
         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
   800
             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
   801
         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
   802
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   803
         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
   804
         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
   805
             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
   806
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   807
         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
   808
         {
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   809
             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
   810
             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
   811
         } // 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
   812
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   813
         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
   814
    } // while
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   815
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   816
    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
   817
        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
   818
    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
   819
} // readSymLink
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   820
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   821
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   822
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
   823
{
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   824
    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
   825
    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
   826
    
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   827
    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
   828
        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
   829
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   830
    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
   831
    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
   832
    {
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   833
        *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
   834
        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
   835
            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
   836
    } // if
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   837
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   838
    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
   839
} // chdirToApp
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   840
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   841
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   842
int main(int argc, char **argv)
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   843
{
39
ddb45e88adc9 chdir() to where the binary is running from before doing anything else.
Ryan C. Gordon <icculus@icculus.org>
parents: 34
diff changeset
   844
    chdirToApp();
33
bc1736f8e74d Signal catching to force atexit handling.
Ryan C. Gordon <icculus@icculus.org>
parents: 31
diff changeset
   845
    initSignals();
28
60136c984e3b Hooked up support for a Griffin Powermate.
Ryan C. Gordon <icculus@icculus.org>
parents: 26
diff changeset
   846
    initPowermate(&argc, argv);
11
b52e0f1798b8 Start building in GUI stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 7
diff changeset
   847
    gtk_init(&argc, &argv);
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   848
7
682d7ea1e7f3 Moved command lines into Lua.
Ryan C. Gordon <icculus@icculus.org>
parents: 4
diff changeset
   849
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
0
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   850
        return 1;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   851
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   852
    return 0;
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   853
} // main
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   854
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   855
// end of 1pass.c ...
d7ee4e2ed49d Initial work.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   856