1pass.c
changeset 12 6a2d5b34d5ca
parent 11 b52e0f1798b8
child 15 e2973f4fb366
equal deleted inserted replaced
11:b52e0f1798b8 12:6a2d5b34d5ca
    24     else
    24     else
    25         lua_pushnil(L);
    25         lua_pushnil(L);
    26     return 1;
    26     return 1;
    27 } // retvalStringBytes
    27 } // retvalStringBytes
    28 
    28 
       
    29 static inline int retvalString(lua_State *L, const char *str)
       
    30 {
       
    31     return retvalStringBytes(L, (const uint8_t *) str, strlen(str));
       
    32 } // retvalString
       
    33 
    29 static inline int retvalPointer(lua_State *L, void *ptr)
    34 static inline int retvalPointer(lua_State *L, void *ptr)
    30 {
    35 {
    31     if (ptr != NULL)
    36     if (ptr != NULL)
    32         lua_pushlightuserdata(L, ptr);
    37         lua_pushlightuserdata(L, ptr);
    33     else
    38     else
   176     free(dataptr);
   181     free(dataptr);
   177     return 1;
   182     return 1;
   178 } // decryptBase64UsingKey
   183 } // decryptBase64UsingKey
   179 
   184 
   180 
   185 
       
   186 static int runGuiPasswordPrompt(lua_State *L)
       
   187 {
       
   188     const char *hintstr = lua_tostring(L, 1);
       
   189     GtkWidget *dialog = gtk_dialog_new_with_buttons(
       
   190                             "Master Password", NULL, GTK_DIALOG_MODAL,
       
   191                             GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
       
   192                             GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
       
   193                             NULL);
       
   194 
       
   195     GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
       
   196 
       
   197     if (hintstr != NULL)
       
   198     {
       
   199         GtkWidget *label = gtk_label_new(hintstr);
       
   200         gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
       
   201         gtk_container_add(GTK_CONTAINER(content_area), label);
       
   202     } // if
       
   203 
       
   204     GtkWidget *entry = gtk_entry_new();
       
   205     gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
       
   206     gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
       
   207     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
       
   208     gtk_container_add(GTK_CONTAINER(content_area), entry);
       
   209 
       
   210     gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
       
   211     gtk_widget_show_all(dialog);
       
   212     const int ok = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT);
       
   213     retvalString(L, ok ? (const char *) gtk_entry_get_text(GTK_ENTRY(entry)) : NULL);
       
   214     gtk_widget_destroy(dialog);
       
   215 
       
   216     return 1;
       
   217 } // runGuiPasswordPrompt
       
   218 
       
   219 
       
   220 static int copyToClipboard(lua_State *L)
       
   221 {
       
   222     const char *str = luaL_checkstring(L, 1);
       
   223     gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), str, -1);
       
   224     gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, -1);
       
   225 } // copyToClipboard
       
   226 
       
   227 
   181 static int makeGuiMenu(lua_State *L)
   228 static int makeGuiMenu(lua_State *L)
   182 {
   229 {
   183     return retvalPointer(L, gtk_menu_new());
   230     return retvalPointer(L, gtk_menu_new());
   184 } // makeGuiMenu
   231 } // makeGuiMenu
   185 
   232 
   190     const int callback = (int) ((size_t)arg);
   237     const int callback = (int) ((size_t)arg);
   191     lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
   238     lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
   192     lua_call(luaState, 0, 0);
   239     lua_call(luaState, 0, 0);
   193 } // clickedMenuItem
   240 } // clickedMenuItem
   194 
   241 
   195 
   242 #if 0  // !!! FIXME: figure out how to fire this.
   196 // !!! FIXME: on destruction of a menu item, we need to luaL_unref(L, LUA_REGISTRYINDEX, callback)...
   243 static void deletedMenuItem(void *arg)
       
   244 {
       
   245     // Clean up the Lua function we referenced in the Registry.
       
   246     const int callback = (int) ((size_t)arg);
       
   247 printf("unref callback %d\n", callback);
       
   248     luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
       
   249 } // deletedMenuItem
       
   250 #endif
       
   251 
   197 static int appendGuiMenuItem(lua_State *L)
   252 static int appendGuiMenuItem(lua_State *L)
   198 {
   253 {
   199     const int argc = lua_gettop(L);
   254     const int argc = lua_gettop(L);
   200     GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
   255     GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
   201     const char *label = luaL_checkstring(L, 2);
   256     const char *label = luaL_checkstring(L, 2);
   293     luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
   348     luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
   294     luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
   349     luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
   295     luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
   350     luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
   296     luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
   351     luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
   297     luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
   352     luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
       
   353     luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
       
   354     luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
   298 
   355 
   299     // Set up argv table...
   356     // Set up argv table...
   300     lua_newtable(luaState);
   357     lua_newtable(luaState);
   301     int i;
   358     int i;
   302     for (i = 0; i < argc; i++)
   359     for (i = 0; i < argc; i++)