~ubuntu-branches/ubuntu/saucy/luakit/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/debian-changes-2011.07.22-1/luah.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2011-07-26 14:36:08 UTC
  • Revision ID: james.westby@ubuntu.com-20110726143608-elctsivefctyqsqi
Tags: 2011.07.22-2
Change WebKitGTK+ build dependency to libwebkitgtk-dev.  closes:
#635418.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * luah.c - Lua helper functions
3
 
 *
4
 
 * Copyright © 2010-2011 Mason Larobina <mason.larobina@gmail.com>
5
 
 * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
6
 
 *
7
 
 * This program is free software: you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation, either version 3 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 *
20
 
 */
21
 
 
22
 
#include "luah.h"
23
 
 
24
 
/* include clib headers */
25
 
#include "clib/download.h"
26
 
#include "clib/soup/soup.h"
27
 
#include "clib/sqlite3.h"
28
 
#include "clib/timer.h"
29
 
#include "clib/widget.h"
30
 
#include "clib/luakit.h"
31
 
 
32
 
#include <glib.h>
33
 
#include <gtk/gtk.h>
34
 
 
35
 
void
36
 
luaH_modifier_table_push(lua_State *L, guint state) {
37
 
    gint i = 1;
38
 
    lua_newtable(L);
39
 
    if (state & GDK_MODIFIER_MASK) {
40
 
 
41
 
#define MODKEY(key, name)           \
42
 
    if (state & GDK_##key##_MASK) { \
43
 
        lua_pushstring(L, name);    \
44
 
        lua_rawseti(L, -2, i++);    \
45
 
    }
46
 
 
47
 
        MODKEY(SHIFT, "Shift");
48
 
        MODKEY(LOCK, "Lock");
49
 
        MODKEY(CONTROL, "Control");
50
 
        MODKEY(MOD1, "Mod1");
51
 
        MODKEY(MOD2, "Mod2");
52
 
        MODKEY(MOD3, "Mod3");
53
 
        MODKEY(MOD4, "Mod4");
54
 
        MODKEY(MOD5, "Mod5");
55
 
 
56
 
#undef MODKEY
57
 
 
58
 
    }
59
 
}
60
 
 
61
 
void
62
 
luaH_keystr_push(lua_State *L, guint keyval)
63
 
{
64
 
    gchar ucs[7];
65
 
    guint ulen;
66
 
    guint32 ukval = gdk_keyval_to_unicode(keyval);
67
 
 
68
 
    /* check for printable unicode character */
69
 
    if (g_unichar_isgraph(ukval)) {
70
 
        ulen = g_unichar_to_utf8(ukval, ucs);
71
 
        ucs[ulen] = 0;
72
 
        lua_pushstring(L, ucs);
73
 
    }
74
 
    /* sent keysym for non-printable characters */
75
 
    else
76
 
        lua_pushstring(L, gdk_keyval_name(keyval));
77
 
}
78
 
 
79
 
/* UTF-8 aware string length computing.
80
 
 * Returns the number of elements pushed on the stack. */
81
 
static gint
82
 
luaH_utf8_strlen(lua_State *L)
83
 
{
84
 
    const gchar *cmd  = luaL_checkstring(L, 1);
85
 
    lua_pushnumber(L, (ssize_t) g_utf8_strlen(NONULL(cmd), -1));
86
 
    return 1;
87
 
}
88
 
 
89
 
/* Overload standard Lua next function to use __next key on metatable.
90
 
 * Returns the number of elements pushed on stack. */
91
 
static gint
92
 
luaHe_next(lua_State *L)
93
 
{
94
 
    if(luaL_getmetafield(L, 1, "__next")) {
95
 
        lua_insert(L, 1);
96
 
        lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
97
 
        return lua_gettop(L);
98
 
    }
99
 
    luaL_checktype(L, 1, LUA_TTABLE);
100
 
    lua_settop(L, 2);
101
 
    if(lua_next(L, 1))
102
 
        return 2;
103
 
    lua_pushnil(L);
104
 
    return 1;
105
 
}
106
 
 
107
 
/* Overload lua_next() function by using __next metatable field to get
108
 
 * next elements. `idx` is the index number of elements in stack.
109
 
 * Returns 1 if more elements to come, 0 otherwise. */
110
 
gint
111
 
luaH_mtnext(lua_State *L, gint idx)
112
 
{
113
 
    if(luaL_getmetafield(L, idx, "__next")) {
114
 
        /* if idx is relative, reduce it since we got __next */
115
 
        if(idx < 0) idx--;
116
 
        /* copy table and then move key */
117
 
        lua_pushvalue(L, idx);
118
 
        lua_pushvalue(L, -3);
119
 
        lua_remove(L, -4);
120
 
        lua_pcall(L, 2, 2, 0);
121
 
        /* next returned nil, it's the end */
122
 
        if(lua_isnil(L, -1)) {
123
 
            /* remove nil */
124
 
            lua_pop(L, 2);
125
 
            return 0;
126
 
        }
127
 
        return 1;
128
 
    }
129
 
    else if(lua_istable(L, idx))
130
 
        return lua_next(L, idx);
131
 
    /* remove the key */
132
 
    lua_pop(L, 1);
133
 
    return 0;
134
 
}
135
 
 
136
 
/* Generic pairs function.
137
 
 * Returns the number of elements pushed on stack. */
138
 
static gint
139
 
luaH_generic_pairs(lua_State *L)
140
 
{
141
 
    lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */
142
 
    lua_pushvalue(L, 1);  /* state, */
143
 
    lua_pushnil(L);  /* and initial value */
144
 
    return 3;
145
 
}
146
 
 
147
 
/* Overload standard pairs function to use __pairs field of metatables.
148
 
 * Returns the number of elements pushed on stack. */
149
 
static gint
150
 
luaHe_pairs(lua_State *L)
151
 
{
152
 
    if(luaL_getmetafield(L, 1, "__pairs")) {
153
 
        lua_insert(L, 1);
154
 
        lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
155
 
        return lua_gettop(L);
156
 
    }
157
 
    luaL_checktype(L, 1, LUA_TTABLE);
158
 
    return luaH_generic_pairs(L);
159
 
}
160
 
 
161
 
static gint
162
 
luaH_ipairs_aux(lua_State *L)
163
 
{
164
 
    gint i = luaL_checkint(L, 2) + 1;
165
 
    luaL_checktype(L, 1, LUA_TTABLE);
166
 
    lua_pushinteger(L, i);
167
 
    lua_rawgeti(L, 1, i);
168
 
    return (lua_isnil(L, -1)) ? 0 : 2;
169
 
}
170
 
 
171
 
/* Overload standard ipairs function to use __ipairs field of metatables.
172
 
 * Returns the number of elements pushed on stack. */
173
 
static gint
174
 
luaHe_ipairs(lua_State *L)
175
 
{
176
 
    if(luaL_getmetafield(L, 1, "__ipairs")) {
177
 
        lua_insert(L, 1);
178
 
        lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
179
 
        return lua_gettop(L);
180
 
    }
181
 
 
182
 
    luaL_checktype(L, 1, LUA_TTABLE);
183
 
    lua_pushvalue(L, lua_upvalueindex(1));
184
 
    lua_pushvalue(L, 1);
185
 
    lua_pushinteger(L, 0);  /* and initial value */
186
 
    return 3;
187
 
}
188
 
 
189
 
/* Enhanced type() function which recognize luakit objects.
190
 
 * \param L The Lua VM state.
191
 
 * \return The number of arguments pushed on the stack.
192
 
 */
193
 
static gint
194
 
luaHe_type(lua_State *L)
195
 
{
196
 
    luaL_checkany(L, 1);
197
 
    lua_pushstring(L, luaH_typename(L, 1));
198
 
    return 1;
199
 
}
200
 
 
201
 
/* Fix up and add handy standard lib functions */
202
 
static void
203
 
luaH_fixups(lua_State *L)
204
 
{
205
 
    /* export string.wlen */
206
 
    lua_getglobal(L, "string");
207
 
    lua_pushcfunction(L, &luaH_utf8_strlen);
208
 
    lua_setfield(L, -2, "wlen");
209
 
    lua_pop(L, 1);
210
 
    /* replace next */
211
 
    lua_pushliteral(L, "next");
212
 
    lua_pushcfunction(L, luaHe_next);
213
 
    lua_settable(L, LUA_GLOBALSINDEX);
214
 
    /* replace pairs */
215
 
    lua_pushliteral(L, "pairs");
216
 
    lua_pushcfunction(L, luaHe_next);
217
 
    lua_pushcclosure(L, luaHe_pairs, 1); /* pairs get next as upvalue */
218
 
    lua_settable(L, LUA_GLOBALSINDEX);
219
 
    /* replace ipairs */
220
 
    lua_pushliteral(L, "ipairs");
221
 
    lua_pushcfunction(L, luaH_ipairs_aux);
222
 
    lua_pushcclosure(L, luaHe_ipairs, 1);
223
 
    lua_settable(L, LUA_GLOBALSINDEX);
224
 
    /* replace type */
225
 
    lua_pushliteral(L, "type");
226
 
    lua_pushcfunction(L, luaHe_type);
227
 
    lua_settable(L, LUA_GLOBALSINDEX);
228
 
}
229
 
 
230
 
/* Look for an item: table, function, etc.
231
 
 * \param L The Lua VM state.
232
 
 * \param item The pointer item.
233
 
 */
234
 
gboolean
235
 
luaH_hasitem(lua_State *L, gconstpointer item)
236
 
{
237
 
    lua_pushnil(L);
238
 
    while(luaH_mtnext(L, -2)) {
239
 
        if(lua_topointer(L, -1) == item) {
240
 
            /* remove value and key */
241
 
            lua_pop(L, 2);
242
 
            return TRUE;
243
 
        }
244
 
        if(lua_istable(L, -1))
245
 
            if(luaH_hasitem(L, item)) {
246
 
                /* remove key and value */
247
 
                lua_pop(L, 2);
248
 
                return TRUE;
249
 
            }
250
 
        /* remove value */
251
 
        lua_pop(L, 1);
252
 
    }
253
 
    return FALSE;
254
 
}
255
 
 
256
 
/* Browse a table pushed on top of the index, and put all its table and
257
 
 * sub-table ginto an array.
258
 
 * \param L The Lua VM state.
259
 
 * \param elems The elements array.
260
 
 * \return False if we encounter an elements already in list.
261
 
 */
262
 
static gboolean
263
 
luaH_isloop_check(lua_State *L, GPtrArray *elems)
264
 
{
265
 
    if(lua_istable(L, -1)) {
266
 
        gconstpointer object = lua_topointer(L, -1);
267
 
 
268
 
        /* Check that the object table is not already in the list */
269
 
        for(guint i = 0; i < elems->len; i++)
270
 
            if(elems->pdata[i] == object)
271
 
                return FALSE;
272
 
 
273
 
        /* push the table in the elements list */
274
 
        g_ptr_array_add(elems, (gpointer) object);
275
 
 
276
 
        /* look every object in the "table" */
277
 
        lua_pushnil(L);
278
 
        while(luaH_mtnext(L, -2)) {
279
 
            if(!luaH_isloop_check(L, elems)) {
280
 
                /* remove key and value */
281
 
                lua_pop(L, 2);
282
 
                return FALSE;
283
 
            }
284
 
            /* remove value, keep key for next iteration */
285
 
            lua_pop(L, 1);
286
 
        }
287
 
    }
288
 
    return TRUE;
289
 
}
290
 
 
291
 
/* Check if a table is a loop. When using tables as direct acyclic digram,
292
 
 * this is useful.
293
 
 * \param L The Lua VM state.
294
 
 * \param idx The index of the table in the stack
295
 
 * \return True if the table loops.
296
 
 */
297
 
gboolean
298
 
luaH_isloop(lua_State *L, gint idx)
299
 
{
300
 
    /* elems is an elements array that we will fill with all array we
301
 
     * encounter while browsing the tables */
302
 
    GPtrArray *elems = g_ptr_array_new();
303
 
 
304
 
    /* push table on top */
305
 
    lua_pushvalue(L, idx);
306
 
 
307
 
    gboolean ret = luaH_isloop_check(L, elems);
308
 
 
309
 
    /* remove pushed table */
310
 
    lua_pop(L, 1);
311
 
 
312
 
    g_ptr_array_free(elems, TRUE);
313
 
 
314
 
    return !ret;
315
 
}
316
 
 
317
 
static gint
318
 
luaH_panic(lua_State *L)
319
 
{
320
 
    warn("unprotected error in call to Lua API (%s)", lua_tostring(L, -1));
321
 
    return 0;
322
 
}
323
 
 
324
 
static gint
325
 
luaH_dofunction_on_error(lua_State *L)
326
 
{
327
 
    /* duplicate string error */
328
 
    lua_pushvalue(L, -1);
329
 
    /* emit error signal */
330
 
    signal_object_emit(L, luakit_class.signals, "debug::error", 1, 0);
331
 
 
332
 
    if(!luaL_dostring(L, "return debug.traceback(\"error while running function\", 3)"))
333
 
    {
334
 
        /* Move traceback before error */
335
 
        lua_insert(L, -2);
336
 
        /* Insert sentence */
337
 
        lua_pushliteral(L, "\nerror: ");
338
 
        /* Move it before error */
339
 
        lua_insert(L, -2);
340
 
        lua_concat(L, 3);
341
 
    }
342
 
    return 1;
343
 
}
344
 
 
345
 
void
346
 
luaH_init(void)
347
 
{
348
 
    lua_State *L;
349
 
 
350
 
    /* Lua VM init */
351
 
    L = globalconf.L = luaL_newstate();
352
 
 
353
 
    /* Set panic fuction */
354
 
    lua_atpanic(L, luaH_panic);
355
 
 
356
 
    /* Set error handling function */
357
 
    lualib_dofunction_on_error = luaH_dofunction_on_error;
358
 
 
359
 
    luaL_openlibs(L);
360
 
 
361
 
    luaH_fixups(L);
362
 
 
363
 
    luaH_object_setup(L);
364
 
 
365
 
    /* Export luakit lib */
366
 
    luakit_lib_setup(L);
367
 
 
368
 
    /* Export soup lib */
369
 
    soup_lib_setup(L);
370
 
 
371
 
    /* Export widget */
372
 
    widget_class_setup(L);
373
 
 
374
 
    /* Export download */
375
 
    download_class_setup(L);
376
 
 
377
 
    /* Export sqlite3 */
378
 
    sqlite3_class_setup(L);
379
 
 
380
 
    /* Export timer */
381
 
    timer_class_setup(L);
382
 
 
383
 
    /* add Lua search paths */
384
 
    lua_getglobal(L, "package");
385
 
    if(LUA_TTABLE != lua_type(L, 1)) {
386
 
        warn("package is not a table");
387
 
        return;
388
 
    }
389
 
    lua_getfield(L, 1, "path");
390
 
    if(LUA_TSTRING != lua_type(L, 2)) {
391
 
        warn("package.path is not a string");
392
 
        lua_pop(L, 1);
393
 
        return;
394
 
    }
395
 
 
396
 
    /* compile list of package search paths */
397
 
    GPtrArray *paths = g_ptr_array_new_with_free_func(g_free);
398
 
 
399
 
#if DEVELOPMENT_PATHS
400
 
    /* allows for testing luakit in the project directory */
401
 
    g_ptr_array_add(paths, g_strdup("./lib"));
402
 
    g_ptr_array_add(paths, g_strdup("./config"));
403
 
#endif
404
 
 
405
 
    /* add users config dir (see: XDG_CONFIG_DIR) */
406
 
    g_ptr_array_add(paths, g_strdup(globalconf.config_dir));
407
 
 
408
 
    /* add system config dirs (see: XDG_CONFIG_DIRS) */
409
 
    const gchar* const *config_dirs = g_get_system_config_dirs();
410
 
    for (; *config_dirs; config_dirs++)
411
 
        g_ptr_array_add(paths, g_build_filename(*config_dirs, "luakit", NULL));
412
 
 
413
 
    /* add luakit install path */
414
 
    g_ptr_array_add(paths, g_build_filename(LUAKIT_INSTALL_PATH, "lib", NULL));
415
 
 
416
 
    const gchar *path;
417
 
    for (guint i = 0; i < paths->len; i++) {
418
 
        path = paths->pdata[i];
419
 
        /* Search for file */
420
 
        lua_pushliteral(L, ";");
421
 
        lua_pushstring(L, path);
422
 
        lua_pushliteral(L, "/?.lua");
423
 
        lua_concat(L, 3);
424
 
        /* Search for lib */
425
 
        lua_pushliteral(L, ";");
426
 
        lua_pushstring(L, path);
427
 
        lua_pushliteral(L, "/?/init.lua");
428
 
        lua_concat(L, 3);
429
 
        /* concat with package.path */
430
 
        lua_concat(L, 3);
431
 
    }
432
 
 
433
 
    g_ptr_array_free(paths, TRUE);
434
 
 
435
 
    /* package.path = "concatenated string" */
436
 
    lua_setfield(L, 1, "path");
437
 
 
438
 
    /* remove package module from stack */
439
 
    lua_pop(L, 1);
440
 
}
441
 
 
442
 
gboolean
443
 
luaH_loadrc(const gchar *confpath, gboolean run)
444
 
{
445
 
    debug("Loading rc: %s", confpath);
446
 
    lua_State *L = globalconf.L;
447
 
    if(!luaL_loadfile(L, confpath)) {
448
 
        if(run) {
449
 
            if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
450
 
                g_fprintf(stderr, "%s\n", lua_tostring(L, -1));
451
 
            } else
452
 
                return TRUE;
453
 
        } else
454
 
            lua_pop(L, 1);
455
 
        return TRUE;
456
 
    } else
457
 
        g_fprintf(stderr, "%s\n", lua_tostring(L, -1));
458
 
    return FALSE;
459
 
}
460
 
 
461
 
/* Load a configuration file. */
462
 
gboolean
463
 
luaH_parserc(const gchar *confpath, gboolean run)
464
 
{
465
 
    const gchar* const *config_dirs = NULL;
466
 
    gboolean ret = FALSE;
467
 
    GPtrArray *paths = NULL;
468
 
 
469
 
    /* try to load, return if it's ok */
470
 
    if(confpath) {
471
 
        if(luaH_loadrc(confpath, run))
472
 
            ret = TRUE;
473
 
        goto bailout;
474
 
    }
475
 
 
476
 
    /* compile list of config search paths */
477
 
    paths = g_ptr_array_new_with_free_func(g_free);
478
 
 
479
 
#if DEVELOPMENT_PATHS
480
 
    /* allows for testing luakit in the project directory */
481
 
    g_ptr_array_add(paths, g_strdup("./config/rc.lua"));
482
 
#endif
483
 
 
484
 
    /* search users config dir (see: XDG_CONFIG_HOME) */
485
 
    g_ptr_array_add(paths, g_build_filename(globalconf.config_dir, "rc.lua", NULL));
486
 
 
487
 
    /* search system config dirs (see: XDG_CONFIG_DIRS) */
488
 
    config_dirs = g_get_system_config_dirs();
489
 
    for(; *config_dirs; config_dirs++)
490
 
        g_ptr_array_add(paths, g_build_filename(*config_dirs, "luakit", "rc.lua", NULL));
491
 
 
492
 
    const gchar *path;
493
 
    for (guint i = 0; i < paths->len; i++) {
494
 
        path = paths->pdata[i];
495
 
        if (file_exists(path)) {
496
 
            if(luaH_loadrc(path, run)) {
497
 
                globalconf.confpath = g_strdup(path);
498
 
                ret = TRUE;
499
 
                goto bailout;
500
 
            } else if(!run)
501
 
                goto bailout;
502
 
        }
503
 
    }
504
 
 
505
 
bailout:
506
 
 
507
 
    if (paths) g_ptr_array_free(paths, TRUE);
508
 
    return ret;
509
 
}
510
 
 
511
 
gint
512
 
luaH_class_index_miss_property(lua_State *L, lua_object_t *obj)
513
 
{
514
 
    (void) obj;
515
 
    signal_object_emit(L, luakit_class.signals, "debug::index::miss", 2, 0);
516
 
    return 0;
517
 
}
518
 
 
519
 
gint
520
 
luaH_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
521
 
{
522
 
    (void) obj;
523
 
    signal_object_emit(L, luakit_class.signals, "debug::newindex::miss", 3, 0);
524
 
    return 0;
525
 
}
526
 
 
527
 
// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80