1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Apr 2014 17:11:54 -0400
changeset 31 838492d40709
parent 29 769393898e93
child 33 bc1736f8e74d
permissions -rw-r--r--
More Powermate stuff: clean up atexit, autodetect devices.
icculus@28
     1
#include <linux/input.h>
icculus@28
     2
#include <fcntl.h>
icculus@0
     3
#include <stdio.h>
icculus@0
     4
#include <stdlib.h>
icculus@0
     5
#include <string.h>
icculus@0
     6
#include <assert.h>
icculus@28
     7
#include <errno.h>
icculus@28
     8
#include <unistd.h>
icculus@31
     9
#include <dirent.h>
icculus@28
    10
icculus@0
    11
#include "lua.h"
icculus@0
    12
#include "lauxlib.h"
icculus@0
    13
#include "lualib.h"
icculus@0
    14
#include "pkcs5_pbkdf2.h"
icculus@0
    15
#include "aes.h"
icculus@0
    16
#include "base64.h"
icculus@0
    17
#include "md5.h"
icculus@17
    18
#include "keyhook.h"
icculus@11
    19
#include <gtk/gtk.h>
icculus@0
    20
icculus@0
    21
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
icculus@0
    22
icculus@28
    23
// plug in a Griffin Powermate, make sure you have access to it, and run with
icculus@28
    24
//  --powermate=/dev/input/eventX
icculus@28
    25
static int powermate_fd = -1;
icculus@28
    26
static int pumpPowermate(void)
icculus@28
    27
{
icculus@28
    28
    struct input_event buf[32];
icculus@28
    29
    int pressed = 0;
icculus@28
    30
    ssize_t br;
icculus@28
    31
icculus@28
    32
    if (powermate_fd == -1)
icculus@28
    33
        return 0;  // nothing to do.
icculus@28
    34
icculus@28
    35
    while ((br = read(powermate_fd, buf, sizeof (buf))) > 0)
icculus@28
    36
    {
icculus@28
    37
        ssize_t i;
icculus@28
    38
        br /= sizeof (buf[0]);
icculus@28
    39
        for (i = 0; i < br; i++)
icculus@28
    40
        {
icculus@28
    41
            struct input_event *ev = &buf[i];
icculus@28
    42
            if ((ev->type == EV_KEY) && (ev->code == BTN_0) && (ev->value))
icculus@28
    43
                pressed = 1;
icculus@28
    44
        } // for
icculus@28
    45
    } // while
icculus@28
    46
icculus@28
    47
    return pressed;
icculus@28
    48
}
icculus@28
    49
icculus@28
    50
static void setPowermateLED(const int enable)
icculus@28
    51
{
icculus@28
    52
    struct input_event ev;
icculus@28
    53
    const int brightness = enable ? 255 : 0;
icculus@28
    54
    const int pulse_speed = 255;
icculus@28
    55
    const int pulse_table = 0;
icculus@28
    56
    const int pulse_awake = enable ? 1 : 0;
icculus@28
    57
    const int pulse_asleep = 0;
icculus@28
    58
icculus@28
    59
    if (powermate_fd == -1)
icculus@28
    60
        return;
icculus@28
    61
icculus@28
    62
    memset(&ev, '\0', sizeof (ev));
icculus@28
    63
    ev.type = EV_MSC;
icculus@28
    64
    ev.code = MSC_PULSELED;
icculus@28
    65
    ev.value = brightness | (pulse_speed << 8) | (pulse_table << 17) | (pulse_asleep << 19) | (pulse_awake << 20);
icculus@28
    66
icculus@28
    67
    if (write(powermate_fd, &ev, sizeof (ev)) != sizeof (ev))
icculus@28
    68
        fprintf(stderr, "WARNING: tried to set Powermate LED and failed: %s\n", strerror(errno));
icculus@28
    69
} // setPowermateLED
icculus@28
    70
icculus@28
    71
icculus@31
    72
static int openPowermate(const char *fname)
icculus@31
    73
{
icculus@31
    74
    static const char const *known_names[] = {
icculus@31
    75
        "Griffin PowerMate", "Griffin SoundKnob"
icculus@31
    76
    };
icculus@31
    77
icculus@31
    78
    char buf[255];
icculus@31
    79
    int ok = 0;
icculus@31
    80
    int fd;
icculus@31
    81
    int i;
icculus@31
    82
icculus@31
    83
    if (!fname)
icculus@31
    84
        return -1;
icculus@31
    85
icculus@31
    86
    if ((fd = open(fname, O_RDWR)) == -1)
icculus@31
    87
        fprintf(stderr, "WARNING: couldn't open Powermate at %s: %s\n", fname, strerror(errno));
icculus@31
    88
icculus@31
    89
    if (ioctl(fd, EVIOCGNAME(sizeof (buf)), buf) == -1)
icculus@31
    90
    {
icculus@31
    91
        fprintf(stderr, "EVIOCGNAME failed for %s: %s\n", fname, strerror(errno));
icculus@31
    92
        close(fd);
icculus@31
    93
        return -1;
icculus@31
    94
    } // if
icculus@31
    95
icculus@31
    96
    for (i = 0; !ok && (i < sizeof (known_names) / sizeof (known_names[0])); i++)
icculus@31
    97
    {
icculus@31
    98
        if (strncmp(buf, known_names[i], strlen(known_names[i])) == 0)
icculus@31
    99
            ok = 1;
icculus@31
   100
    } // for
icculus@31
   101
icculus@31
   102
    if (!ok)
icculus@31
   103
    {
icculus@31
   104
        close(fd);
icculus@31
   105
        return -1;
icculus@31
   106
    } // if
icculus@31
   107
icculus@31
   108
    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
icculus@31
   109
    return fd;
icculus@31
   110
} // openPowermate
icculus@31
   111
icculus@31
   112
icculus@31
   113
static void deinitPowermate(void)
icculus@31
   114
{
icculus@31
   115
    if (powermate_fd == -1)
icculus@31
   116
    {
icculus@31
   117
        setPowermateLED(0);
icculus@31
   118
        close(powermate_fd);
icculus@31
   119
        powermate_fd = -1;
icculus@31
   120
    } // if
icculus@31
   121
} // deinitPowermate
icculus@31
   122
icculus@31
   123
icculus@28
   124
static void initPowermate(int *_argc, char **argv)
icculus@28
   125
{
icculus@28
   126
    const char *arg = "--powermate=";
icculus@28
   127
    const size_t arglen = strlen(arg);
icculus@28
   128
    int argc = *_argc;
icculus@28
   129
    int i;
icculus@28
   130
icculus@28
   131
    for (i = 1; i < argc; i++)
icculus@28
   132
    {
icculus@28
   133
        const char *thisarg = argv[i];
icculus@28
   134
        if (strncmp(thisarg, arg, arglen) != 0)
icculus@28
   135
            continue;
icculus@28
   136
icculus@28
   137
        thisarg += arglen;
icculus@31
   138
icculus@31
   139
        if (strcmp(thisarg, "auto") == 0)
icculus@28
   140
        {
icculus@31
   141
            DIR *dirp = opendir("/dev/input");
icculus@31
   142
            if (dirp)
icculus@31
   143
            {
icculus@31
   144
                struct dirent *dent;
icculus@31
   145
                while ((dent = readdir(dirp)) != NULL)
icculus@31
   146
                {
icculus@31
   147
                    const char *name = dent->d_name;
icculus@31
   148
                    char buf[PATH_MAX];
icculus@31
   149
                    if (strncmp(name, "event", 5) != 0)
icculus@31
   150
                        continue;
icculus@31
   151
                    snprintf(buf, sizeof (buf), "/dev/input/%s", name);
icculus@31
   152
                    if (powermate_fd == -1)
icculus@31
   153
                    {
icculus@31
   154
                        powermate_fd = openPowermate(buf);
icculus@31
   155
                        if (powermate_fd != -1)
icculus@31
   156
                        {
icculus@31
   157
                            printf("Found Powermate at %s\n", buf);
icculus@31
   158
                            break;
icculus@31
   159
                        } // if
icculus@31
   160
                    } // if
icculus@31
   161
                } // while
icculus@31
   162
                closedir(dirp);
icculus@31
   163
            } // if
icculus@31
   164
            thisarg = NULL;
icculus@31
   165
icculus@31
   166
        } // if
icculus@28
   167
icculus@28
   168
        // eliminate this command line.
icculus@28
   169
        memmove(&argv[i], &argv[i+1], (argc-i) * sizeof (char *));
icculus@28
   170
        argc--;
icculus@31
   171
icculus@31
   172
        if (powermate_fd == -1)
icculus@31
   173
            powermate_fd = openPowermate(thisarg);
icculus@28
   174
    } // for
icculus@28
   175
icculus@31
   176
    atexit(deinitPowermate);
icculus@31
   177
icculus@28
   178
    *_argc = argc;
icculus@28
   179
} // initPowermate
icculus@28
   180
icculus@28
   181
icculus@0
   182
static lua_State *luaState = NULL;
icculus@0
   183
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
icculus@0
   184
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
icculus@0
   185
icculus@0
   186
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
icculus@0
   187
{
icculus@0
   188
    if (str != NULL)
icculus@0
   189
        lua_pushlstring(L, (const char *) str, len);
icculus@0
   190
    else
icculus@0
   191
        lua_pushnil(L);
icculus@0
   192
    return 1;
icculus@0
   193
} // retvalStringBytes
icculus@0
   194
icculus@12
   195
static inline int retvalString(lua_State *L, const char *str)
icculus@12
   196
{
icculus@15
   197
    return retvalStringBytes(L, (const uint8_t *) str, str ? strlen(str) : 0);
icculus@12
   198
} // retvalString
icculus@12
   199
icculus@11
   200
static inline int retvalPointer(lua_State *L, void *ptr)
icculus@11
   201
{
icculus@11
   202
    if (ptr != NULL)
icculus@11
   203
        lua_pushlightuserdata(L, ptr);
icculus@11
   204
    else
icculus@11
   205
        lua_pushnil(L);
icculus@11
   206
    return 1;
icculus@11
   207
} // retvalPointer
icculus@11
   208
icculus@0
   209
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
icculus@0
   210
{
icculus@0
   211
    int i;
icculus@0
   212
    for (i = 0; i < 16; i++, dst++, src++)
icculus@0
   213
        *dst ^= *src;
icculus@0
   214
} // xorBlock
icculus@0
   215
icculus@0
   216
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv)
icculus@0
   217
{
icculus@0
   218
    const size_t blocks = *datalen / 16;
icculus@0
   219
    uint8_t *block = data + ((blocks-1) * 16);   // start at final block, work backwards
icculus@0
   220
    const uint8_t *padding = &block[15];
icculus@0
   221
    uint8_t expkey[aesExpandedKeySize];
icculus@0
   222
    size_t i;
icculus@0
   223
icculus@0
   224
    if (blocks == 0)
icculus@0
   225
        return 1;  // nothing to do.
icculus@0
   226
icculus@0
   227
	aesExpandKey(key, expkey);
icculus@0
   228
icculus@0
   229
    for (i = 0; i < blocks-1; i++)
icculus@0
   230
    {
icculus@0
   231
        aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
   232
        xorBlock(block, block-16);
icculus@0
   233
        block -= 16;
icculus@0
   234
    }
icculus@0
   235
    aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
   236
    xorBlock(block, iv);   // xor against initial vector for final block.
icculus@0
   237
icculus@0
   238
    if (*padding > 16)
icculus@0
   239
        return 0;  // bad data?
icculus@0
   240
icculus@0
   241
    *datalen -= *padding;
icculus@0
   242
icculus@0
   243
    return 1;
icculus@0
   244
} // decryptBinaryUsingKeyAndIvec
icculus@0
   245
icculus@0
   246
icculus@0
   247
static inline int isSalted(const uint8_t *data, const size_t datalen)
icculus@0
   248
{
icculus@0
   249
    return ( (datalen > sizeof (saltprefix)) &&
icculus@0
   250
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
icculus@0
   251
} // isSalted
icculus@0
   252
icculus@0
   253
icculus@0
   254
static int decryptUsingPBKDF2(lua_State *L)
icculus@0
   255
{
icculus@0
   256
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
   257
    const char *password = luaL_checkstring(L, 2);
icculus@0
   258
    const int iterations = luaL_checkinteger(L, 3);
icculus@0
   259
    size_t datalen = strlen(base64);
icculus@0
   260
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
   261
    uint8_t *data = dataptr;
icculus@0
   262
    base64_decodestate base64state;
icculus@0
   263
icculus@0
   264
    base64_init_decodestate(&base64state);
icculus@0
   265
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   266
icculus@0
   267
    const uint8_t *salt = zero16;
icculus@0
   268
    int saltlen = sizeof (zero16);
icculus@0
   269
    if (isSalted(data, datalen))
icculus@0
   270
    {
icculus@0
   271
        salt = data + 8;
icculus@0
   272
        saltlen = 8;
icculus@0
   273
        data += 16;
icculus@0
   274
        datalen -= 16;
icculus@0
   275
    } // if
icculus@0
   276
icculus@0
   277
    uint8_t output[32];
icculus@0
   278
    pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations);
icculus@0
   279
icculus@0
   280
    const uint8_t *aeskey = &output[0];
icculus@0
   281
    const uint8_t *aesiv = &output[16];
icculus@0
   282
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   283
        retvalStringBytes(L, data, datalen);
icculus@0
   284
    else
icculus@0
   285
        lua_pushnil(L);
icculus@0
   286
icculus@0
   287
    free(dataptr);
icculus@0
   288
    return 1;
icculus@0
   289
} // decryptUsingPBKDF2
icculus@0
   290
icculus@0
   291
icculus@0
   292
static int decryptBase64UsingKey(lua_State *L)
icculus@0
   293
{
icculus@0
   294
    size_t keylen = 0;
icculus@0
   295
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
   296
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
icculus@0
   297
    size_t datalen = strlen(base64);
icculus@0
   298
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
   299
    uint8_t *data = dataptr;
icculus@0
   300
    base64_decodestate base64state;
icculus@0
   301
icculus@0
   302
    base64_init_decodestate(&base64state);
icculus@0
   303
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   304
icculus@0
   305
    uint8_t aeskey[16];
icculus@0
   306
    uint8_t aesiv[16];
icculus@0
   307
    MD5_CTX md5;
icculus@0
   308
icculus@0
   309
    if (isSalted(data, datalen))
icculus@0
   310
    {
icculus@0
   311
        const uint8_t *salt = data + 8;
icculus@0
   312
        const size_t saltlen = 8;
icculus@0
   313
        data += 16;
icculus@0
   314
        datalen -= 16;
icculus@0
   315
icculus@0
   316
        assert(aesNr == 10);  // AES-256 needs more rounds.
icculus@0
   317
        assert(aesNk == 4);   // hashing size is hardcoded later.
icculus@0
   318
        uint8_t hashing[32];
icculus@0
   319
icculus@0
   320
        MD5_init(&md5);
icculus@0
   321
        MD5_append(&md5, key, keylen);
icculus@0
   322
        MD5_append(&md5, salt, saltlen);
icculus@0
   323
        MD5_finish(&md5, hashing);
icculus@0
   324
icculus@0
   325
        MD5_init(&md5);
icculus@0
   326
        MD5_append(&md5, hashing, 16);
icculus@0
   327
        MD5_append(&md5, key, keylen);
icculus@0
   328
        MD5_append(&md5, salt, saltlen);
icculus@0
   329
        MD5_finish(&md5, &hashing[16]);
icculus@0
   330
icculus@0
   331
        memcpy(aeskey, hashing, 4 * aesNk);
icculus@0
   332
        memcpy(aesiv, &hashing[4 * aesNk], 16);
icculus@0
   333
    } // if
icculus@0
   334
    else
icculus@0
   335
    {
icculus@0
   336
        MD5_init(&md5);
icculus@0
   337
        MD5_append(&md5, key, keylen);
icculus@0
   338
        MD5_finish(&md5, aeskey);
icculus@0
   339
        memset(aesiv, '\0', sizeof (aesiv));
icculus@0
   340
    } // else
icculus@0
   341
icculus@0
   342
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   343
        retvalStringBytes(L, data, datalen);
icculus@0
   344
    else
icculus@0
   345
        lua_pushnil(L);
icculus@0
   346
icculus@0
   347
    free(dataptr);
icculus@0
   348
    return 1;
icculus@0
   349
} // decryptBase64UsingKey
icculus@0
   350
icculus@0
   351
icculus@12
   352
static int runGuiPasswordPrompt(lua_State *L)
icculus@12
   353
{
icculus@12
   354
    const char *hintstr = lua_tostring(L, 1);
icculus@12
   355
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
icculus@12
   356
                            "Master Password", NULL, GTK_DIALOG_MODAL,
icculus@12
   357
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
icculus@12
   358
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
icculus@12
   359
                            NULL);
icculus@12
   360
icculus@12
   361
    GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
icculus@12
   362
icculus@12
   363
    if (hintstr != NULL)
icculus@12
   364
    {
icculus@12
   365
        GtkWidget *label = gtk_label_new(hintstr);
icculus@12
   366
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
icculus@12
   367
        gtk_container_add(GTK_CONTAINER(content_area), label);
icculus@12
   368
    } // if
icculus@12
   369
icculus@12
   370
    GtkWidget *entry = gtk_entry_new();
icculus@12
   371
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
icculus@12
   372
    gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
icculus@12
   373
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
icculus@12
   374
    gtk_container_add(GTK_CONTAINER(content_area), entry);
icculus@12
   375
icculus@12
   376
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
icculus@12
   377
    gtk_widget_show_all(dialog);
icculus@26
   378
    gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
icculus@12
   379
    const int ok = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT);
icculus@12
   380
    retvalString(L, ok ? (const char *) gtk_entry_get_text(GTK_ENTRY(entry)) : NULL);
icculus@12
   381
    gtk_widget_destroy(dialog);
icculus@12
   382
icculus@12
   383
    return 1;
icculus@12
   384
} // runGuiPasswordPrompt
icculus@12
   385
icculus@12
   386
icculus@12
   387
static int copyToClipboard(lua_State *L)
icculus@12
   388
{
icculus@12
   389
    const char *str = luaL_checkstring(L, 1);
icculus@12
   390
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), str, -1);
icculus@12
   391
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, -1);
icculus@12
   392
} // copyToClipboard
icculus@12
   393
icculus@12
   394
icculus@11
   395
static int makeGuiMenu(lua_State *L)
icculus@11
   396
{
icculus@11
   397
    return retvalPointer(L, gtk_menu_new());
icculus@11
   398
} // makeGuiMenu
icculus@11
   399
icculus@11
   400
icculus@11
   401
static void clickedMenuItem(void *arg)
icculus@11
   402
{
icculus@11
   403
    // This is the callback from GTK+; now call into our actual Lua callback!
icculus@11
   404
    const int callback = (int) ((size_t)arg);
icculus@11
   405
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
icculus@11
   406
    lua_call(luaState, 0, 0);
icculus@11
   407
} // clickedMenuItem
icculus@11
   408
icculus@12
   409
#if 0  // !!! FIXME: figure out how to fire this.
icculus@12
   410
static void deletedMenuItem(void *arg)
icculus@12
   411
{
icculus@12
   412
    // Clean up the Lua function we referenced in the Registry.
icculus@12
   413
    const int callback = (int) ((size_t)arg);
icculus@12
   414
printf("unref callback %d\n", callback);
icculus@12
   415
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
icculus@12
   416
} // deletedMenuItem
icculus@12
   417
#endif
icculus@11
   418
icculus@11
   419
static int appendGuiMenuItem(lua_State *L)
icculus@11
   420
{
icculus@11
   421
    const int argc = lua_gettop(L);
icculus@11
   422
    GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
icculus@11
   423
    const char *label = luaL_checkstring(L, 2);
icculus@11
   424
    GtkWidget *item = gtk_menu_item_new_with_label(label);
icculus@11
   425
icculus@11
   426
    if ((argc >= 3) && (!lua_isnil(L, 3)))
icculus@11
   427
    {
icculus@11
   428
        assert(lua_isfunction(L, 3));
icculus@11
   429
        lua_pushvalue(L, 3);  // copy the Lua callback (luaL_ref() pops it).
icculus@11
   430
        const int callback = luaL_ref(L, LUA_REGISTRYINDEX);
icculus@11
   431
        gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback));
icculus@11
   432
    } // if
icculus@11
   433
icculus@11
   434
    gtk_widget_show(item);
icculus@11
   435
    gtk_menu_append(menu, item);
icculus@11
   436
    return retvalPointer(L, item);
icculus@11
   437
} // appendGuiMenuItem
icculus@11
   438
icculus@11
   439
icculus@11
   440
static int setGuiMenuItemSubmenu(lua_State *L)
icculus@11
   441
{
icculus@11
   442
    GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1);
icculus@11
   443
    GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2);
icculus@11
   444
    gtk_menu_item_set_submenu(item, submenu);
icculus@11
   445
    return 0;
icculus@11
   446
} // setGuiMenuItemSubmenu
icculus@11
   447
icculus@11
   448
icculus@11
   449
static int popupGuiMenu(lua_State *L)
icculus@11
   450
{
icculus@11
   451
    GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1);
icculus@11
   452
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
icculus@11
   453
    return 0;
icculus@11
   454
} // popupGuiMenu
icculus@11
   455
icculus@11
   456
icculus@28
   457
static int setPowermateLED_Lua(lua_State *L)
icculus@28
   458
{
icculus@28
   459
    const int enable = lua_toboolean(L, 1);
icculus@28
   460
    setPowermateLED(enable);
icculus@28
   461
    return 0;
icculus@28
   462
} // setPowermateLED_Lua
icculus@28
   463
icculus@28
   464
icculus@17
   465
static void keyhookPressed(void)
icculus@17
   466
{
icculus@17
   467
    lua_getglobal(luaState, "keyhookPressed");
icculus@17
   468
    lua_call(luaState, 0, 0);
icculus@17
   469
} // keyhookPressed
icculus@17
   470
icculus@17
   471
icculus@29
   472
static int pumpLua(void)
icculus@29
   473
{
icculus@29
   474
    lua_getglobal(luaState, "pumpLua");
icculus@29
   475
    lua_call(luaState, 0, 0);
icculus@29
   476
} // pumpLua
icculus@29
   477
icculus@29
   478
icculus@17
   479
static gboolean keyhookPumper(void *arg)
icculus@17
   480
{
icculus@29
   481
    pumpLua();
icculus@17
   482
    if (pumpKeyHook())
icculus@17
   483
        keyhookPressed();
icculus@28
   484
    else if (pumpPowermate())
icculus@28
   485
        keyhookPressed();
icculus@28
   486
icculus@17
   487
    return TRUE;  // keep firing timer
icculus@17
   488
} // keyhookPumper
icculus@17
   489
icculus@17
   490
icculus@11
   491
static int giveControlToGui(lua_State *L)
icculus@11
   492
{
icculus@17
   493
    if (initKeyHook())
icculus@17
   494
    {
icculus@17
   495
        atexit(deinitKeyHook);
icculus@17
   496
        g_timeout_add(200, (GSourceFunc) keyhookPumper, (gpointer) NULL);
icculus@17
   497
    } // if
icculus@17
   498
icculus@11
   499
    gtk_main();
icculus@11
   500
    return 0;
icculus@11
   501
} // giveControlToGui
icculus@11
   502
icculus@7
   503
icculus@0
   504
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
icculus@0
   505
{
icculus@0
   506
    if (nsize == 0)
icculus@0
   507
    {
icculus@0
   508
        free(ptr);
icculus@0
   509
        return NULL;
icculus@0
   510
    } // if
icculus@0
   511
    return realloc(ptr, nsize);
icculus@0
   512
} // luaAlloc
icculus@0
   513
icculus@0
   514
icculus@0
   515
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym)
icculus@0
   516
{
icculus@0
   517
    lua_pushcfunction(L, f);
icculus@0
   518
    lua_setglobal(luaState, sym);
icculus@0
   519
} // luaSetCFunc
icculus@0
   520
icculus@0
   521
icculus@0
   522
static int luaFatal(lua_State *L)
icculus@0
   523
{
icculus@0
   524
    const char *errstr = lua_tostring(L, -1);
icculus@0
   525
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
icculus@0
   526
    fflush(stderr);
icculus@0
   527
    exit(1);
icculus@0
   528
} // luaFatal
icculus@0
   529
icculus@0
   530
icculus@11
   531
static void deinitLua(void)
icculus@11
   532
{
icculus@11
   533
    if (luaState != NULL)
icculus@11
   534
    {
icculus@11
   535
        lua_close(luaState);
icculus@11
   536
        luaState = NULL;
icculus@11
   537
    } // if
icculus@11
   538
} // deinitLua
icculus@11
   539
icculus@11
   540
icculus@7
   541
static int initLua(const int argc, char **argv)
icculus@0
   542
{
icculus@11
   543
    atexit(deinitLua);
icculus@11
   544
icculus@0
   545
    assert(luaState == NULL);
icculus@0
   546
    luaState = lua_newstate(luaAlloc, NULL);
icculus@0
   547
icculus@0
   548
    lua_atpanic(luaState, luaFatal);
icculus@0
   549
    assert(lua_checkstack(luaState, 20));  // Just in case.
icculus@2
   550
    luaL_openlibs(luaState);
icculus@0
   551
icculus@0
   552
    // Set up initial C functions, etc we want to expose to Lua code...
icculus@0
   553
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
icculus@0
   554
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
icculus@11
   555
    luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
icculus@11
   556
    luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
icculus@11
   557
    luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
icculus@11
   558
    luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
icculus@11
   559
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
icculus@12
   560
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
icculus@12
   561
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
icculus@28
   562
    luaSetCFunc(luaState, setPowermateLED_Lua, "setPowermateLED");
icculus@0
   563
icculus@7
   564
    // Set up argv table...
icculus@7
   565
    lua_newtable(luaState);
icculus@7
   566
    int i;
icculus@28
   567
    int luai = 1;
icculus@7
   568
    for (i = 0; i < argc; i++)
icculus@7
   569
    {
icculus@28
   570
        if (argv[i])
icculus@28
   571
        {
icculus@28
   572
            lua_pushinteger(luaState, luai);
icculus@28
   573
            lua_pushstring(luaState, argv[i]);
icculus@28
   574
            lua_settable(luaState, -3);
icculus@28
   575
            luai++;
icculus@28
   576
        } // if
icculus@7
   577
    } // for
icculus@7
   578
    lua_setglobal(luaState, "argv");
icculus@7
   579
icculus@3
   580
    // Transfer control to Lua...
icculus@0
   581
    if (luaL_dofile(luaState, "1pass.lua") != 0)
icculus@16
   582
        luaFatal(luaState);
icculus@0
   583
icculus@0
   584
    return 1;
icculus@0
   585
} // initLua
icculus@0
   586
icculus@0
   587
icculus@0
   588
int main(int argc, char **argv)
icculus@0
   589
{
icculus@28
   590
    initPowermate(&argc, argv);
icculus@11
   591
    gtk_init(&argc, &argv);
icculus@0
   592
icculus@7
   593
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
icculus@0
   594
        return 1;
icculus@0
   595
icculus@0
   596
    return 0;
icculus@0
   597
} // main
icculus@0
   598
icculus@0
   599
// end of 1pass.c ...
icculus@0
   600