diff --git a/awesome.c b/awesome.c index 3931558b9..45c501e74 100644 --- a/awesome.c +++ b/awesome.c @@ -70,15 +70,16 @@ static float main_loop_iteration_limit = 0.1; void awesome_atexit(bool restart) { - lua_pushboolean(globalconf.L, restart); - signal_object_emit(globalconf.L, &global_signals, "exit", 1); + lua_State *L = globalconf_get_lua_State(); + lua_pushboolean(L, restart); + signal_object_emit(L, &global_signals, "exit", 1); a_dbus_cleanup(); systray_cleanup(); /* Close Lua */ - lua_close(globalconf.L); + lua_close(L); /* X11 is a great protocol. There is a save-set so that reparenting WMs * don't kill clients when they shut down. However, when a focused windows diff --git a/dbus.c b/dbus.c index 7ded3c680..79d183726 100644 --- a/dbus.c +++ b/dbus.c @@ -61,11 +61,12 @@ a_dbus_cleanup_bus(DBusConnection *dbus_connection, GSource **source) } /** Iterate through the D-Bus messages counting each or traverse each sub message. + * \param L The Lua VM state. * \param iter The D-Bus message iterator pointer * \return The number of arguments in the iterator */ static int -a_dbus_message_iter(DBusMessageIter *iter) +a_dbus_message_iter(lua_State *L, DBusMessageIter *iter) { int nargs = 0; @@ -74,7 +75,7 @@ a_dbus_message_iter(DBusMessageIter *iter) switch(dbus_message_iter_get_arg_type(iter)) { default: - lua_pushnil(globalconf.L); + lua_pushnil(L); nargs++; break; case DBUS_TYPE_INVALID: @@ -83,7 +84,7 @@ a_dbus_message_iter(DBusMessageIter *iter) { DBusMessageIter subiter; dbus_message_iter_recurse(iter, &subiter); - a_dbus_message_iter(&subiter); + a_dbus_message_iter(L, &subiter); } nargs++; break; @@ -94,7 +95,7 @@ a_dbus_message_iter(DBusMessageIter *iter) /* initialize a sub iterator */ dbus_message_iter_recurse(iter, &subiter); /* create a new table to store the dict */ - a_dbus_message_iter(&subiter); + a_dbus_message_iter(L, &subiter); } nargs++; break; @@ -104,15 +105,15 @@ a_dbus_message_iter(DBusMessageIter *iter) /* initialize a sub iterator */ dbus_message_iter_recurse(iter, &subiter); - int n = a_dbus_message_iter(&subiter); + int n = a_dbus_message_iter(L, &subiter); /* create a new table to store all the value */ - lua_createtable(globalconf.L, n, 0); + lua_createtable(L, n, 0); /* move the table before array elements */ - lua_insert(globalconf.L, - n - 1); + lua_insert(L, - n - 1); for(int i = n; i > 0; i--) - lua_rawseti(globalconf.L, - i - 1, i); + lua_rawseti(L, - i - 1, i); } nargs++; break; @@ -133,11 +134,11 @@ a_dbus_message_iter(DBusMessageIter *iter) { \ const type *data; \ dbus_message_iter_get_fixed_array(&sub, &data, &datalen); \ - lua_createtable(globalconf.L, datalen, 0); \ + lua_createtable(L, datalen, 0); \ for(int i = 0; i < datalen; i++) \ { \ - lua_pushnumber(globalconf.L, data[i]); \ - lua_rawseti(globalconf.L, -2, i + 1); \ + lua_pushnumber(L, data[i]); \ + lua_rawseti(L, -2, i + 1); \ } \ } \ break; @@ -153,18 +154,18 @@ a_dbus_message_iter(DBusMessageIter *iter) { const char *c; dbus_message_iter_get_fixed_array(&sub, &c, &datalen); - lua_pushlstring(globalconf.L, c, datalen); + lua_pushlstring(L, c, datalen); } break; case DBUS_TYPE_BOOLEAN: { const dbus_bool_t *b; dbus_message_iter_get_fixed_array(&sub, &b, &datalen); - lua_createtable(globalconf.L, datalen, 0); + lua_createtable(L, datalen, 0); for(int i = 0; i < datalen; i++) { - lua_pushboolean(globalconf.L, b[i]); - lua_rawseti(globalconf.L, -2, i + 1); + lua_pushboolean(L, b[i]); + lua_rawseti(L, -2, i + 1); } } break; @@ -178,15 +179,15 @@ a_dbus_message_iter(DBusMessageIter *iter) /* get the keys and the values * n is the number of entry in dict */ - int n = a_dbus_message_iter(&subiter); + int n = a_dbus_message_iter(L, &subiter); /* create a new table to store all the value */ - lua_createtable(globalconf.L, n, 0); + lua_createtable(L, n, 0); /* move the table before array elements */ - lua_insert(globalconf.L, - (n * 2) - 1); + lua_insert(L, - (n * 2) - 1); for(int i = 0; i < n; i ++) - lua_rawset(globalconf.L, - (n * 2) - 1 + i * 2); + lua_rawset(L, - (n * 2) - 1 + i * 2); } else { @@ -195,15 +196,15 @@ a_dbus_message_iter(DBusMessageIter *iter) dbus_message_iter_recurse(iter, &subiter); /* now iterate over every element of the array */ - int n = a_dbus_message_iter(&subiter); + int n = a_dbus_message_iter(L, &subiter); /* create a new table to store all the value */ - lua_createtable(globalconf.L, n, 0); + lua_createtable(L, n, 0); /* move the table before array elements */ - lua_insert(globalconf.L, - n - 1); + lua_insert(L, - n - 1); for(int i = n; i > 0; i--) - lua_rawseti(globalconf.L, - i - 1, i); + lua_rawseti(L, - i - 1, i); } } nargs++; @@ -212,7 +213,7 @@ a_dbus_message_iter(DBusMessageIter *iter) { dbus_bool_t b; dbus_message_iter_get_basic(iter, &b); - lua_pushboolean(globalconf.L, b); + lua_pushboolean(L, b); } nargs++; break; @@ -220,7 +221,7 @@ a_dbus_message_iter(DBusMessageIter *iter) { char c; dbus_message_iter_get_basic(iter, &c); - lua_pushlstring(globalconf.L, &c, 1); + lua_pushlstring(L, &c, 1); } nargs++; break; @@ -229,7 +230,7 @@ a_dbus_message_iter(DBusMessageIter *iter) { \ type ui; \ dbus_message_iter_get_basic(iter, &ui); \ - lua_pushnumber(globalconf.L, ui); \ + lua_pushnumber(L, ui); \ } \ nargs++; \ break; @@ -244,7 +245,7 @@ a_dbus_message_iter(DBusMessageIter *iter) { char *s; dbus_message_iter_get_basic(iter, &s); - lua_pushstring(globalconf.L, s); + lua_pushstring(L, s); } nargs++; break; @@ -345,54 +346,55 @@ static void a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg) { const char *interface = dbus_message_get_interface(msg); - int old_top = lua_gettop(globalconf.L); + lua_State *L = globalconf_get_lua_State(); + int old_top = lua_gettop(L); - lua_createtable(globalconf.L, 0, 5); + lua_createtable(L, 0, 5); switch(dbus_message_get_type(msg)) { case DBUS_MESSAGE_TYPE_SIGNAL: - lua_pushliteral(globalconf.L, "signal"); + lua_pushliteral(L, "signal"); break; case DBUS_MESSAGE_TYPE_METHOD_CALL: - lua_pushliteral(globalconf.L, "method_call"); + lua_pushliteral(L, "method_call"); break; case DBUS_MESSAGE_TYPE_METHOD_RETURN: - lua_pushliteral(globalconf.L, "method_return"); + lua_pushliteral(L, "method_return"); break; case DBUS_MESSAGE_TYPE_ERROR: - lua_pushliteral(globalconf.L, "error"); + lua_pushliteral(L, "error"); break; default: - lua_pushliteral(globalconf.L, "unknown"); + lua_pushliteral(L, "unknown"); break; } - lua_setfield(globalconf.L, -2, "type"); + lua_setfield(L, -2, "type"); - lua_pushstring(globalconf.L, interface); - lua_setfield(globalconf.L, -2, "interface"); + lua_pushstring(L, interface); + lua_setfield(L, -2, "interface"); const char *s = dbus_message_get_path(msg); - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "path"); + lua_pushstring(L, s); + lua_setfield(L, -2, "path"); s = dbus_message_get_member(msg); - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "member"); + lua_pushstring(L, s); + lua_setfield(L, -2, "member"); if(dbus_connection == dbus_connection_system) - lua_pushliteral(globalconf.L, "system"); + lua_pushliteral(L, "system"); else - lua_pushliteral(globalconf.L, "session"); - lua_setfield(globalconf.L, -2, "bus"); + lua_pushliteral(L, "session"); + lua_setfield(L, -2, "bus"); /* + 1 for the table above */ DBusMessageIter iter; int nargs = 1; if(dbus_message_iter_init(msg, &iter)) - nargs += a_dbus_message_iter(&iter); + nargs += a_dbus_message_iter(L, &iter); if(dbus_message_get_no_reply(msg)) { @@ -400,7 +402,7 @@ a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg) a_strhash((const unsigned char *) NONULL(interface))); /* emit signals */ if(sigfound) - signal_object_emit(globalconf.L, &dbus_signals, NONULL(interface), nargs); + signal_object_emit(L, &dbus_signals, NONULL(interface), nargs); } else { @@ -411,12 +413,12 @@ a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg) /* there can be only ONE handler to send reply */ void *func = (void *) sig->sigfuncs.tab[0]; - int n = lua_gettop(globalconf.L) - nargs; + int n = lua_gettop(L) - nargs; - luaA_object_push(globalconf.L, (void *) func); - luaA_dofunction(globalconf.L, nargs, LUA_MULTRET); + luaA_object_push(L, (void *) func); + luaA_dofunction(L, nargs, LUA_MULTRET); - n -= lua_gettop(globalconf.L); + n -= lua_gettop(L); DBusMessage *reply = dbus_message_new_method_return(msg); @@ -424,26 +426,26 @@ a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg) if(n % 2 != 0) { - luaA_warn(globalconf.L, + luaA_warn(L, "your D-Bus signal handling method returned wrong number of arguments"); /* Restore stack */ - lua_settop(globalconf.L, old_top); + lua_settop(L, old_top); return; } /* i is negative */ for(int i = n; i < 0; i += 2) { - if(!a_dbus_convert_value(globalconf.L, i, &iter)) + if(!a_dbus_convert_value(L, i, &iter)) { - luaA_warn(globalconf.L, "your D-Bus signal handling method returned bad data"); + luaA_warn(L, "your D-Bus signal handling method returned bad data"); /* Restore stack */ - lua_settop(globalconf.L, old_top); + lua_settop(L, old_top); return; } - lua_remove(globalconf.L, i); - lua_remove(globalconf.L, i + 1); + lua_remove(L, i); + lua_remove(L, i + 1); } dbus_connection_send(dbus_connection, reply, NULL); @@ -451,7 +453,7 @@ a_dbus_process_request(DBusConnection *dbus_connection, DBusMessage *msg) } } /* Restore stack */ - lua_settop(globalconf.L, old_top); + lua_settop(L, old_top); } /** Attempt to process all the requests in the D-Bus connection. diff --git a/event.c b/event.c index 57a7fa1f3..c0a8bfae0 100644 --- a/event.c +++ b/event.c @@ -47,19 +47,20 @@ static void \ event_##xcbtype##_callback(xcb_##xcbtype##_press_event_t *ev, \ arraytype *arr, \ + lua_State *L, \ int oud, \ int nargs, \ void *data) \ { \ - int abs_oud = oud < 0 ? ((lua_gettop(globalconf.L) + 1) + oud) : oud; \ + int abs_oud = oud < 0 ? ((lua_gettop(L) + 1) + oud) : oud; \ int item_matching = 0; \ foreach(item, *arr) \ if(match(ev, *item, data)) \ { \ if(oud) \ - luaA_object_push_item(globalconf.L, abs_oud, *item); \ + luaA_object_push_item(L, abs_oud, *item); \ else \ - luaA_object_push(globalconf.L, *item); \ + luaA_object_push(L, *item); \ item_matching++; \ } \ for(; item_matching > 0; item_matching--) \ @@ -68,18 +69,18 @@ { \ case xcbeventprefix##_PRESS: \ for(int i = 0; i < nargs; i++) \ - lua_pushvalue(globalconf.L, - nargs - item_matching); \ - luaA_object_emit_signal(globalconf.L, - nargs - 1, "press", nargs); \ + lua_pushvalue(L, - nargs - item_matching); \ + luaA_object_emit_signal(L, - nargs - 1, "press", nargs); \ break; \ case xcbeventprefix##_RELEASE: \ for(int i = 0; i < nargs; i++) \ - lua_pushvalue(globalconf.L, - nargs - item_matching); \ - luaA_object_emit_signal(globalconf.L, - nargs - 1, "release", nargs); \ + lua_pushvalue(L, - nargs - item_matching); \ + luaA_object_emit_signal(L, - nargs - 1, "release", nargs); \ break; \ } \ - lua_pop(globalconf.L, 1); \ + lua_pop(L, 1); \ } \ - lua_pop(globalconf.L, nargs); \ + lua_pop(L, nargs); \ } static bool @@ -113,16 +114,17 @@ event_handle_mousegrabber(int x, int y, uint16_t mask) { if(globalconf.mousegrabber != LUA_REFNIL) { - lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.mousegrabber); - mousegrabber_handleevent(globalconf.L, x, y, mask); - if(lua_pcall(globalconf.L, 1, 1, 0)) + lua_State *L = globalconf_get_lua_State(); + lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.mousegrabber); + mousegrabber_handleevent(L, x, y, mask); + if(lua_pcall(L, 1, 1, 0)) { - warn("error running function: %s", lua_tostring(globalconf.L, -1)); - luaA_mousegrabber_stop(globalconf.L); + warn("error running function: %s", lua_tostring(L, -1)); + luaA_mousegrabber_stop(L); } - else if(!lua_isboolean(globalconf.L, -1) || !lua_toboolean(globalconf.L, -1)) - luaA_mousegrabber_stop(globalconf.L); - lua_pop(globalconf.L, 1); /* pop returned value */ + else if(!lua_isboolean(L, -1) || !lua_toboolean(L, -1)) + luaA_mousegrabber_stop(L); + lua_pop(L, 1); /* pop returned value */ return true; } return false; @@ -130,10 +132,11 @@ event_handle_mousegrabber(int x, int y, uint16_t mask) /** Emit a button signal. * The top of the lua stack has to be the object on which to emit the event. + * \param L The Lua VM state. * \param ev The event to handle. */ static void -event_emit_button(xcb_button_press_event_t *ev) +event_emit_button(lua_State *L, xcb_button_press_event_t *ev) { const char *name; switch(XCB_EVENT_RESPONSE_TYPE(ev)) @@ -149,12 +152,12 @@ event_emit_button(xcb_button_press_event_t *ev) } /* Push the event's info */ - lua_pushnumber(globalconf.L, ev->event_x); - lua_pushnumber(globalconf.L, ev->event_y); - lua_pushnumber(globalconf.L, ev->detail); - luaA_pushmodifiers(globalconf.L, ev->state); + lua_pushnumber(L, ev->event_x); + lua_pushnumber(L, ev->event_y); + lua_pushnumber(L, ev->detail); + luaA_pushmodifiers(L, ev->state); /* And emit the signal */ - luaA_object_emit_signal(globalconf.L, -5, name, 4); + luaA_object_emit_signal(L, -5, name, 4); } /** The button press event handler. @@ -163,6 +166,7 @@ event_emit_button(xcb_button_press_event_t *ev) static void event_handle_button(xcb_button_press_event_t *ev) { + lua_State *L = globalconf_get_lua_State(); client_t *c; drawin_t *drawin; @@ -189,13 +193,13 @@ event_handle_button(xcb_button_press_event_t *ev) } /* Push the drawable */ - luaA_object_push(globalconf.L, drawin); - luaA_object_push_item(globalconf.L, -1, drawin->drawable); + luaA_object_push(L, drawin); + luaA_object_push_item(L, -1, drawin->drawable); /* and handle the button raw button event */ - event_emit_button(ev); - lua_pop(globalconf.L, 1); + event_emit_button(L, ev); + lua_pop(L, 1); /* check if any button object matches */ - event_button_callback(ev, &drawin->buttons, -1, 1, NULL); + event_button_callback(ev, &drawin->buttons, L, -1, 1, NULL); /* Either we are receiving this due to ButtonPress/Release on the root * window or because we grabbed the button on the window. In the later * case we have to call AllowEvents. @@ -209,9 +213,9 @@ event_handle_button(xcb_button_press_event_t *ev) } else if((c = client_getbyframewin(ev->event))) { - luaA_object_push(globalconf.L, c); + luaA_object_push(L, c); /* And handle the button raw button event */ - event_emit_button(ev); + event_emit_button(L, ev); /* then check if a titlebar was "hit" */ int x = ev->event_x, y = ev->event_y; drawable_t *d = client_get_drawable_offset(c, &x, &y); @@ -221,12 +225,12 @@ event_handle_button(xcb_button_press_event_t *ev) xcb_button_press_event_t event = *ev; event.event_x = x; event.event_y = y; - luaA_object_push_item(globalconf.L, -1, d); - event_emit_button(&event); - lua_pop(globalconf.L, 1); + luaA_object_push_item(L, -1, d); + event_emit_button(L, &event); + lua_pop(L, 1); } /* then check if any button objects match */ - event_button_callback(ev, &c->buttons, -1, 1, NULL); + event_button_callback(ev, &c->buttons, L, -1, 1, NULL); xcb_allow_events(globalconf.connection, XCB_ALLOW_REPLAY_POINTER, XCB_CURRENT_TIME); @@ -234,7 +238,7 @@ event_handle_button(xcb_button_press_event_t *ev) else if(ev->child == XCB_NONE) if(globalconf.screen->root == ev->event) { - event_button_callback(ev, &globalconf.buttons, 0, 0, NULL); + event_button_callback(ev, &globalconf.buttons, L, 0, 0, NULL); return; } } @@ -330,13 +334,15 @@ event_handle_configurerequest(xcb_configure_request_event_t *ev) } if(ev->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) { + lua_State *L = globalconf_get_lua_State(); + diff_border = ev->border_width - c->border_width; diff_h += diff_border; diff_w += diff_border; - luaA_object_push(globalconf.L, c); - window_set_border_width(globalconf.L, -1, ev->border_width); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + window_set_border_width(L, -1, ev->border_width); + lua_pop(L, 1); } /* If the client resizes without moving itself, apply window gravity */ @@ -398,6 +404,7 @@ event_handle_destroynotify(xcb_destroy_notify_event_t *ev) static void event_handle_motionnotify(xcb_motion_notify_event_t *ev) { + lua_State *L = globalconf_get_lua_State(); drawin_t *w; client_t *c; @@ -408,33 +415,33 @@ event_handle_motionnotify(xcb_motion_notify_event_t *ev) if((c = client_getbyframewin(ev->event))) { - luaA_object_push(globalconf.L, c); - lua_pushnumber(globalconf.L, ev->event_x); - lua_pushnumber(globalconf.L, ev->event_y); - luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2); + luaA_object_push(L, c); + lua_pushnumber(L, ev->event_x); + lua_pushnumber(L, ev->event_y); + luaA_object_emit_signal(L, -3, "mouse::move", 2); /* now check if a titlebar was "hit" */ int x = ev->event_x, y = ev->event_y; drawable_t *d = client_get_drawable_offset(c, &x, &y); if (d) { - luaA_object_push_item(globalconf.L, -1, d); - lua_pushnumber(globalconf.L, x); - lua_pushnumber(globalconf.L, y); - luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2); - lua_pop(globalconf.L, 1); + luaA_object_push_item(L, -1, d); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + luaA_object_emit_signal(L, -3, "mouse::move", 2); + lua_pop(L, 1); } - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } if((w = drawin_getbywin(ev->event))) { - luaA_object_push(globalconf.L, w); - luaA_object_push_item(globalconf.L, -1, w->drawable); - lua_pushnumber(globalconf.L, ev->event_x); - lua_pushnumber(globalconf.L, ev->event_y); - luaA_object_emit_signal(globalconf.L, -3, "mouse::move", 2); - lua_pop(globalconf.L, 2); + luaA_object_push(L, w); + luaA_object_push_item(L, -1, w->drawable); + lua_pushnumber(L, ev->event_x); + lua_pushnumber(L, ev->event_y); + luaA_object_emit_signal(L, -3, "mouse::move", 2); + lua_pop(L, 2); } } @@ -444,6 +451,7 @@ event_handle_motionnotify(xcb_motion_notify_event_t *ev) static void event_handle_leavenotify(xcb_leave_notify_event_t *ev) { + lua_State *L = globalconf_get_lua_State(); drawin_t *drawin; client_t *c; @@ -454,24 +462,24 @@ event_handle_leavenotify(xcb_leave_notify_event_t *ev) if((c = client_getbyframewin(ev->event))) { - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "mouse::leave", 0); drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y); if (d) { - luaA_object_push_item(globalconf.L, -1, d); - luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0); - lua_pop(globalconf.L, 1); + luaA_object_push_item(L, -1, d); + luaA_object_emit_signal(L, -1, "mouse::leave", 0); + lua_pop(L, 1); } - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } if((drawin = drawin_getbywin(ev->event))) { - luaA_object_push(globalconf.L, drawin); - luaA_object_push_item(globalconf.L, -1, drawin->drawable); - luaA_object_emit_signal(globalconf.L, -1, "mouse::leave", 0); - lua_pop(globalconf.L, 2); + luaA_object_push(L, drawin); + luaA_object_push_item(L, -1, drawin->drawable); + luaA_object_emit_signal(L, -1, "mouse::leave", 0); + lua_pop(L, 2); } } @@ -481,6 +489,7 @@ event_handle_leavenotify(xcb_leave_notify_event_t *ev) static void event_handle_enternotify(xcb_enter_notify_event_t *ev) { + lua_State *L = globalconf_get_lua_State(); client_t *c; drawin_t *drawin; @@ -491,24 +500,24 @@ event_handle_enternotify(xcb_enter_notify_event_t *ev) if((drawin = drawin_getbywin(ev->event))) { - luaA_object_push(globalconf.L, drawin); - luaA_object_push_item(globalconf.L, -1, drawin->drawable); - luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0); - lua_pop(globalconf.L, 2); + luaA_object_push(L, drawin); + luaA_object_push_item(L, -1, drawin->drawable); + luaA_object_emit_signal(L, -1, "mouse::enter", 0); + lua_pop(L, 2); } if((c = client_getbyframewin(ev->event))) { - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "mouse::enter", 0); drawable_t *d = client_get_drawable(c, ev->event_x, ev->event_y); if (d) { - luaA_object_push_item(globalconf.L, -1, d); - luaA_object_emit_signal(globalconf.L, -1, "mouse::enter", 0); - lua_pop(globalconf.L, 1); + luaA_object_push_item(L, -1, d); + luaA_object_emit_signal(L, -1, "mouse::enter", 0); + lua_pop(L, 1); } - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } } @@ -574,20 +583,21 @@ event_handle_expose(xcb_expose_event_t *ev) static void event_handle_key(xcb_key_press_event_t *ev) { + lua_State *L = globalconf_get_lua_State(); globalconf.timestamp = ev->time; if(globalconf.keygrabber != LUA_REFNIL) { - lua_rawgeti(globalconf.L, LUA_REGISTRYINDEX, globalconf.keygrabber); - if(keygrabber_handlekpress(globalconf.L, ev)) + lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.keygrabber); + if(keygrabber_handlekpress(L, ev)) { - if(lua_pcall(globalconf.L, 3, 1, 0)) + if(lua_pcall(L, 3, 1, 0)) { - warn("error running function: %s", lua_tostring(globalconf.L, -1)); - luaA_keygrabber_stop(globalconf.L); + warn("error running function: %s", lua_tostring(L, -1)); + luaA_keygrabber_stop(L); } } - lua_pop(globalconf.L, 1); /* pop returned value or function if not called */ + lua_pop(L, 1); /* pop returned value or function if not called */ } else { @@ -596,11 +606,11 @@ event_handle_key(xcb_key_press_event_t *ev) client_t *c; if((c = client_getbyframewin(ev->event))) { - luaA_object_push(globalconf.L, c); - event_key_callback(ev, &c->keys, -1, 1, &keysym); + luaA_object_push(L, c); + event_key_callback(ev, &c->keys, L, -1, 1, &keysym); } else - event_key_callback(ev, &globalconf.keys, 0, 0, &keysym); + event_key_callback(ev, &globalconf.keys, L, 0, 0, &keysym); } } @@ -634,9 +644,10 @@ event_handle_maprequest(xcb_map_request_event_t *ev) /* Check that it may be visible, but not asked to be hidden */ if(client_maybevisible(c) && !c->hidden) { - luaA_object_push(globalconf.L, c); - client_set_minimized(globalconf.L, -1, false); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); + client_set_minimized(L, -1, false); + lua_pop(L, 1); /* it will be raised, so just update ourself */ client_raise(c); } @@ -714,12 +725,13 @@ event_handle_shape_notify(xcb_shape_notify_event_t *ev) client_t *c = client_getbywin(ev->affected_window); if (c) { - luaA_object_push(globalconf.L, c); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); if (ev->shape_kind == XCB_SHAPE_SK_BOUNDING) - luaA_object_emit_signal(globalconf.L, -1, "property::shape_client_bounding", 0); + luaA_object_emit_signal(L, -1, "property::shape_client_bounding", 0); if (ev->shape_kind == XCB_SHAPE_SK_CLIP) - luaA_object_emit_signal(globalconf.L, -1, "property::shape_client_clip", 0); - lua_pop(globalconf.L, 1); + luaA_object_emit_signal(L, -1, "property::shape_client_clip", 0); + lua_pop(L, 1); } } @@ -740,9 +752,10 @@ event_handle_clientmessage(xcb_client_message_event_t *ev) && ev->format == 32 && ev->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC) { - luaA_object_push(globalconf.L, c); - client_set_minimized(globalconf.L, -1, true); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); + client_set_minimized(L, -1, true); + lua_pop(L, 1); } } else if(ev->type == _XEMBED) diff --git a/ewmh.c b/ewmh.c index 2d8bf86fd..e2e1ccf37 100755 --- a/ewmh.c +++ b/ewmh.c @@ -107,6 +107,7 @@ ewmh_update_net_client_list(lua_State *L) void ewmh_init(void) { + lua_State *L = globalconf_get_lua_State(); xcb_window_t father; xcb_screen_t *xscreen = globalconf.screen; xcb_atom_t atom[] = @@ -185,20 +186,20 @@ ewmh_init(void) father, _NET_WM_PID, XCB_ATOM_CARDINAL, 32, 1, &i); - luaA_class_connect_signal(globalconf.L, &client_class, "focus", ewmh_update_net_active_window); - luaA_class_connect_signal(globalconf.L, &client_class, "unfocus", ewmh_update_net_active_window); - luaA_class_connect_signal(globalconf.L, &client_class, "manage", ewmh_update_net_client_list); - luaA_class_connect_signal(globalconf.L, &client_class, "unmanage", ewmh_update_net_client_list); - luaA_class_connect_signal(globalconf.L, &client_class, "property::modal" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::fullscreen" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::maximized_horizontal" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::maximized_vertical" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::sticky" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::skip_taskbar" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::above" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::below" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::minimized" , ewmh_client_update_hints); - luaA_class_connect_signal(globalconf.L, &client_class, "property::urgent" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "focus", ewmh_update_net_active_window); + luaA_class_connect_signal(L, &client_class, "unfocus", ewmh_update_net_active_window); + luaA_class_connect_signal(L, &client_class, "manage", ewmh_update_net_client_list); + luaA_class_connect_signal(L, &client_class, "unmanage", ewmh_update_net_client_list); + luaA_class_connect_signal(L, &client_class, "property::modal" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::fullscreen" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::maximized_horizontal" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::maximized_vertical" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::sticky" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::skip_taskbar" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::above" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::below" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::minimized" , ewmh_client_update_hints); + luaA_class_connect_signal(L, &client_class, "property::urgent" , ewmh_client_update_hints); } /** Set the client list in stacking order, bottom to top. @@ -259,121 +260,123 @@ ewmh_update_net_desktop_names(void) static void ewmh_process_state_atom(client_t *c, xcb_atom_t state, int set) { - luaA_object_push(globalconf.L, c); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); if(state == _NET_WM_STATE_STICKY) { if(set == _NET_WM_STATE_REMOVE) - client_set_sticky(globalconf.L, -1, false); + client_set_sticky(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_sticky(globalconf.L, -1, true); + client_set_sticky(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_sticky(globalconf.L, -1, !c->sticky); + client_set_sticky(L, -1, !c->sticky); } else if(state == _NET_WM_STATE_SKIP_TASKBAR) { if(set == _NET_WM_STATE_REMOVE) - client_set_skip_taskbar(globalconf.L, -1, false); + client_set_skip_taskbar(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_skip_taskbar(globalconf.L, -1, true); + client_set_skip_taskbar(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_skip_taskbar(globalconf.L, -1, !c->skip_taskbar); + client_set_skip_taskbar(L, -1, !c->skip_taskbar); } else if(state == _NET_WM_STATE_FULLSCREEN) { if(set == _NET_WM_STATE_REMOVE) - client_set_fullscreen(globalconf.L, -1, false); + client_set_fullscreen(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_fullscreen(globalconf.L, -1, true); + client_set_fullscreen(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_fullscreen(globalconf.L, -1, !c->fullscreen); + client_set_fullscreen(L, -1, !c->fullscreen); } else if(state == _NET_WM_STATE_MAXIMIZED_HORZ) { if(set == _NET_WM_STATE_REMOVE) - client_set_maximized_horizontal(globalconf.L, -1, false); + client_set_maximized_horizontal(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_maximized_horizontal(globalconf.L, -1, true); + client_set_maximized_horizontal(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_maximized_horizontal(globalconf.L, -1, !c->maximized_horizontal); + client_set_maximized_horizontal(L, -1, !c->maximized_horizontal); } else if(state == _NET_WM_STATE_MAXIMIZED_VERT) { if(set == _NET_WM_STATE_REMOVE) - client_set_maximized_vertical(globalconf.L, -1, false); + client_set_maximized_vertical(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_maximized_vertical(globalconf.L, -1, true); + client_set_maximized_vertical(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_maximized_vertical(globalconf.L, -1, !c->maximized_vertical); + client_set_maximized_vertical(L, -1, !c->maximized_vertical); } else if(state == _NET_WM_STATE_ABOVE) { if(set == _NET_WM_STATE_REMOVE) - client_set_above(globalconf.L, -1, false); + client_set_above(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_above(globalconf.L, -1, true); + client_set_above(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_above(globalconf.L, -1, !c->above); + client_set_above(L, -1, !c->above); } else if(state == _NET_WM_STATE_BELOW) { if(set == _NET_WM_STATE_REMOVE) - client_set_below(globalconf.L, -1, false); + client_set_below(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_below(globalconf.L, -1, true); + client_set_below(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_below(globalconf.L, -1, !c->below); + client_set_below(L, -1, !c->below); } else if(state == _NET_WM_STATE_MODAL) { if(set == _NET_WM_STATE_REMOVE) - client_set_modal(globalconf.L, -1, false); + client_set_modal(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_modal(globalconf.L, -1, true); + client_set_modal(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_modal(globalconf.L, -1, !c->modal); + client_set_modal(L, -1, !c->modal); } else if(state == _NET_WM_STATE_HIDDEN) { if(set == _NET_WM_STATE_REMOVE) - client_set_minimized(globalconf.L, -1, false); + client_set_minimized(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_minimized(globalconf.L, -1, true); + client_set_minimized(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_minimized(globalconf.L, -1, !c->minimized); + client_set_minimized(L, -1, !c->minimized); } else if(state == _NET_WM_STATE_DEMANDS_ATTENTION) { if(set == _NET_WM_STATE_REMOVE) - client_set_urgent(globalconf.L, -1, false); + client_set_urgent(L, -1, false); else if(set == _NET_WM_STATE_ADD) - client_set_urgent(globalconf.L, -1, true); + client_set_urgent(L, -1, true); else if(set == _NET_WM_STATE_TOGGLE) - client_set_urgent(globalconf.L, -1, !c->urgent); + client_set_urgent(L, -1, !c->urgent); } - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } static void ewmh_process_desktop(client_t *c, uint32_t desktop) { + lua_State *L = globalconf_get_lua_State(); int idx = desktop; if(desktop == 0xffffffff) { - luaA_object_push(globalconf.L, c); - lua_pushnil(globalconf.L); - luaA_object_emit_signal(globalconf.L, -2, "request::tag", 1); + luaA_object_push(L, c); + lua_pushnil(L); + luaA_object_emit_signal(L, -2, "request::tag", 1); /* Pop the client, arguments are already popped */ - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } else if (idx >= 0 && idx < globalconf.tags.len) { - luaA_object_push(globalconf.L, c); - luaA_object_push(globalconf.L, globalconf.tags.tab[idx]); - luaA_object_emit_signal(globalconf.L, -2, "request::tag", 1); + luaA_object_push(L, c); + luaA_object_push(L, globalconf.tags.tab[idx]); + luaA_object_emit_signal(L, -2, "request::tag", 1); /* Pop the client, arguments are already popped */ - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } } @@ -387,9 +390,10 @@ ewmh_process_client_message(xcb_client_message_event_t *ev) int idx = ev->data.data32[0]; if (idx >= 0 && idx < globalconf.tags.len) { - luaA_object_push(globalconf.L, globalconf.tags.tab[idx]); - luaA_object_emit_signal(globalconf.L, -1, "request::select", 0); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, globalconf.tags.tab[idx]); + luaA_object_emit_signal(L, -1, "request::select", 0); + lua_pop(L, 1); } } else if(ev->type == _NET_CLOSE_WINDOW) @@ -417,10 +421,11 @@ ewmh_process_client_message(xcb_client_message_event_t *ev) else if(ev->type == _NET_ACTIVE_WINDOW) { if((c = client_getbywin(ev->window))) { - luaA_object_push(globalconf.L, c); - lua_pushstring(globalconf.L,"ewmh"); - luaA_object_emit_signal(globalconf.L, -2, "request::activate", 1); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); + lua_pushstring(L, "ewmh"); + luaA_object_emit_signal(L, -2, "request::activate", 1); + lua_pop(L, 1); } } @@ -595,9 +600,10 @@ ewmh_process_client_strut(client_t *c) c->strut.bottom_start_x = strut[10]; c->strut.bottom_end_x = strut[11]; - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "property::struts", 0); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "property::struts", 0); + lua_pop(L, 1); } } diff --git a/globalconf.h b/globalconf.h index 945c008ab..c3d3325a6 100644 --- a/globalconf.h +++ b/globalconf.h @@ -91,8 +91,10 @@ typedef struct xembed_window_array_t embedded; /** Stack client history */ client_array_t stack; - /** Lua VM state */ - lua_State *L; + /** Lua VM state (opaque to avoid mis-use, see globalconf_get_lua_State()) */ + struct { + lua_State *real_L_dont_use_directly; + } L; /** All errors messages from loading config files */ buffer_t startup_errors; /** main loop that awesome is running on */ @@ -150,5 +152,12 @@ typedef struct extern awesome_t globalconf; +/** You should always use this as lua_State *L = globalconf_get_lua_State(). + * That way it becomes harder to introduce coroutine-related problems. + */ +static inline lua_State *globalconf_get_lua_State(void) { + return globalconf.L.real_L_dont_use_directly; +} + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/luaa.c b/luaa.c index 04b0a2626..c2f932836 100644 --- a/luaa.c +++ b/luaa.c @@ -363,7 +363,7 @@ luaA_init(xdgHandle* xdg) { NULL, NULL } }; - L = globalconf.L = luaL_newstate(); + L = globalconf.L.real_L_dont_use_directly = luaL_newstate(); /* Set panic function */ lua_atpanic(L, luaA_panic); @@ -492,7 +492,8 @@ luaA_startup_error(const char *err) static bool luaA_loadrc(const char *confpath, bool run) { - if(!luaL_loadfile(globalconf.L, confpath)) + lua_State *L = globalconf_get_lua_State(); + if(!luaL_loadfile(L, confpath)) { if(run) { @@ -500,11 +501,11 @@ luaA_loadrc(const char *confpath, bool run) * configuration file. */ conffile = a_strdup(confpath); /* Move error handling function before function */ - lua_pushcfunction(globalconf.L, luaA_dofunction_on_error); - lua_insert(globalconf.L, -2); - if(lua_pcall(globalconf.L, 0, LUA_MULTRET, -2)) + lua_pushcfunction(L, luaA_dofunction_on_error); + lua_insert(L, -2); + if(lua_pcall(L, 0, LUA_MULTRET, -2)) { - const char *err = lua_tostring(globalconf.L, -1); + const char *err = lua_tostring(L, -1); luaA_startup_error(err); fprintf(stderr, "%s\n", err); /* An error happened, so reset this. */ @@ -515,13 +516,13 @@ luaA_loadrc(const char *confpath, bool run) } else { - lua_pop(globalconf.L, 1); + lua_pop(L, 1); return true; } } else { - const char *err = lua_tostring(globalconf.L, -1); + const char *err = lua_tostring(L, -1); luaA_startup_error(err); fprintf(stderr, "%s\n", err); } @@ -593,7 +594,8 @@ luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj) void luaA_emit_refresh() { - signal_object_emit(globalconf.L, &global_signals, "refresh", 0); + lua_State *L = globalconf_get_lua_State(); + signal_object_emit(L, &global_signals, "refresh", 0); } int diff --git a/objects/client.c b/objects/client.c index b8d3c4153..138d8a46e 100644 --- a/objects/client.c +++ b/objects/client.c @@ -199,11 +199,12 @@ client_getbyframewin(xcb_window_t w) static void client_unfocus_internal(client_t *c) { + lua_State *L = globalconf_get_lua_State(); globalconf.focus.client = NULL; - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "unfocus", 0); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "unfocus", 0); + lua_pop(L, 1); } /** Unfocus a client. @@ -297,6 +298,8 @@ client_restore_enterleave_events(void) void client_focus_update(client_t *c) { + lua_State *L = globalconf_get_lua_State(); + if(!client_maybevisible(c)) return; @@ -317,11 +320,11 @@ client_focus_update(client_t *c) globalconf.focus.client = c; /* according to EWMH, we have to remove the urgent state from a client */ - luaA_object_push(globalconf.L, c); - client_set_urgent(globalconf.L, -1, false); + luaA_object_push(L, c); + client_set_urgent(L, -1, false); - luaA_object_emit_signal(globalconf.L, -1, "focus", 0); - lua_pop(globalconf.L, 1); + luaA_object_emit_signal(L, -1, "focus", 0); + lua_pop(L, 1); } /** Give focus to client, or to first client if client is NULL. @@ -426,6 +429,7 @@ client_update_properties(lua_State *L, int cidx, client_t *c) void client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, xcb_get_window_attributes_reply_t *wattr) { + lua_State *L = globalconf_get_lua_State(); const uint32_t select_input_val[] = { CLIENT_SELECT_INPUT_EVENT_MASK }; if(systray_iskdedockapp(w)) @@ -445,7 +449,7 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, xcb_get_window_at if (globalconf.have_shape) xcb_shape_select_input(globalconf.connection, w, 1); - client_t *c = client_new(globalconf.L); + client_t *c = client_new(L); xcb_screen_t *s = globalconf.screen; /* consider the window banned */ @@ -493,7 +497,7 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, xcb_get_window_at * (Else, reparent could cause an UnmapNotify) */ xcb_change_window_attributes(globalconf.connection, w, XCB_CW_EVENT_MASK, select_input_val); - luaA_object_emit_signal(globalconf.L, -1, "property::window", 0); + luaA_object_emit_signal(L, -1, "property::window", 0); /* The frame window gets the border, not the real client window */ xcb_configure_window(globalconf.connection, w, @@ -509,8 +513,8 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, xcb_get_window_at (uint32_t[]) { XCB_STACK_MODE_BELOW}); /* Duplicate client and push it in client list */ - lua_pushvalue(globalconf.L, -1); - client_array_push(&globalconf.clients, luaA_object_ref(globalconf.L, -1)); + lua_pushvalue(L, -1); + client_array_push(&globalconf.clients, luaA_object_ref(L, -1)); /* Set the right screen */ screen_client_moveto(c, screen_getbycoord(wgeom->x, wgeom->y), false); @@ -519,24 +523,24 @@ client_manage(xcb_window_t w, xcb_get_geometry_reply_t *wgeom, xcb_get_window_at * been set. */ #define HANDLE_GEOM(attr) \ c->geometry.attr = wgeom->attr; \ - luaA_object_emit_signal(globalconf.L, -1, "property::" #attr, 0); + luaA_object_emit_signal(L, -1, "property::" #attr, 0); HANDLE_GEOM(x) HANDLE_GEOM(y) HANDLE_GEOM(width) HANDLE_GEOM(height) #undef HANDLE_GEOM - luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0); + luaA_object_emit_signal(L, -1, "property::geometry", 0); /* Set border width */ - window_set_border_width(globalconf.L, -1, wgeom->border_width); + window_set_border_width(L, -1, wgeom->border_width); /* we honor size hints by default */ c->size_hints_honor = true; - luaA_object_emit_signal(globalconf.L, -1, "property::size_hints_honor", 0); + luaA_object_emit_signal(L, -1, "property::size_hints_honor", 0); /* update all properties */ - client_update_properties(globalconf.L, -1, c); + client_update_properties(L, -1, c); /* Then check clients hints */ ewmh_client_check_hints(c); @@ -571,12 +575,12 @@ HANDLE_GEOM(height) p_delete(&reply); spawn_start_notify(c, startup_id); - luaA_class_emit_signal(globalconf.L, &client_class, "list", 0); + luaA_class_emit_signal(L, &client_class, "list", 0); /* client is still on top of the stack; emit signal */ - luaA_object_emit_signal(globalconf.L, -1, "manage", 0); + luaA_object_emit_signal(L, -1, "manage", 0); /* pop client */ - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } static void @@ -721,6 +725,7 @@ client_apply_size_hints(client_t *c, area_t geometry) static void client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hints) { + lua_State *L = globalconf_get_lua_State(); bool send_notice = force_notice; bool hide_titlebars = c->fullscreen; screen_t *new_screen = screen_getbycoord(geometry.x, geometry.y); @@ -767,17 +772,17 @@ client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hin client_restore_enterleave_events(); - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "property::geometry", 0); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "property::geometry", 0); if (old_geometry.x != geometry.x) - luaA_object_emit_signal(globalconf.L, -1, "property::x", 0); + luaA_object_emit_signal(L, -1, "property::x", 0); if (old_geometry.y != geometry.y) - luaA_object_emit_signal(globalconf.L, -1, "property::y", 0); + luaA_object_emit_signal(L, -1, "property::y", 0); if (old_geometry.width != geometry.width) - luaA_object_emit_signal(globalconf.L, -1, "property::width", 0); + luaA_object_emit_signal(L, -1, "property::width", 0); if (old_geometry.height != geometry.height) - luaA_object_emit_signal(globalconf.L, -1, "property::height", 0); - lua_pop(globalconf.L, 1); + luaA_object_emit_signal(L, -1, "property::height", 0); + lua_pop(L, 1); screen_client_moveto(c, new_screen, false); @@ -786,9 +791,9 @@ client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hin if (c->titlebar[bar].drawable == NULL && c->titlebar[bar].size == 0) continue; - luaA_object_push(globalconf.L, c); - drawable_t *drawable = titlebar_get_drawable(globalconf.L, c, -1, bar); - luaA_object_push_item(globalconf.L, -1, drawable); + luaA_object_push(L, c); + drawable_t *drawable = titlebar_get_drawable(L, c, -1, bar); + luaA_object_push_item(L, -1, drawable); area_t area = titlebar_get_area(c, bar); @@ -797,10 +802,10 @@ client_resize_do(client_t *c, area_t geometry, bool force_notice, bool honor_hin area.y += geometry.y; if (hide_titlebars) area.width = area.height = 0; - drawable_set_geometry(globalconf.L, -1, area); + drawable_set_geometry(L, -1, area); /* Pop the client and the drawable */ - lua_pop(globalconf.L, 2); + lua_pop(L, 2); } } @@ -1102,6 +1107,7 @@ client_set_ontop(lua_State *L, int cidx, bool s) void client_unban(client_t *c) { + lua_State *L = globalconf_get_lua_State(); if(c->isbanned) { xcb_map_window(globalconf.connection, c->frame_window); @@ -1109,10 +1115,10 @@ client_unban(client_t *c) c->isbanned = false; /* An unbanned client shouldn't be minimized or hidden */ - luaA_object_push(globalconf.L, c); - client_set_minimized(globalconf.L, -1, false); - client_set_hidden(globalconf.L, -1, false); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + client_set_minimized(L, -1, false); + client_set_hidden(L, -1, false); + lua_pop(L, 1); } } @@ -1123,6 +1129,8 @@ client_unban(client_t *c) void client_unmanage(client_t *c, bool window_valid) { + lua_State *L = globalconf_get_lua_State(); + /* Reset transient_for attributes of windows that might be referring to us */ foreach(_tc, globalconf.clients) { @@ -1145,14 +1153,14 @@ client_unmanage(client_t *c, bool window_valid) for(int i = 0; i < globalconf.tags.len; i++) untag_client(c, globalconf.tags.tab[i]); - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "unmanage", 0); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "unmanage", 0); + lua_pop(L, 1); - luaA_class_emit_signal(globalconf.L, &client_class, "list", 0); + luaA_class_emit_signal(L, &client_class, "list", 0); if(strut_has_value(&c->strut)) - client_emit_property_workarea_on_screen(globalconf.L, c); + client_emit_property_workarea_on_screen(L, c); /* Get rid of all titlebars */ for (client_titlebar_t bar = CLIENT_TITLEBAR_TOP; bar < CLIENT_TITLEBAR_COUNT; bar++) { @@ -1160,10 +1168,10 @@ client_unmanage(client_t *c, bool window_valid) continue; /* Forget about the drawable */ - luaA_object_push(globalconf.L, c); - luaA_object_unref_item(globalconf.L, -1, c->titlebar[bar].drawable); + luaA_object_push(L, c); + luaA_object_unref_item(L, -1, c->titlebar[bar].drawable); c->titlebar[bar].drawable = NULL; - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } /* Clear our event mask so that we don't receive any events from now on, @@ -1206,7 +1214,7 @@ client_unmanage(client_t *c, bool window_valid) /* set client as invalid */ c->window = XCB_NONE; - luaA_object_unref(globalconf.L, c); + luaA_object_unref(L, c); } /** Kill a client via a WM_DELETE_WINDOW request or KillClient if not @@ -1287,15 +1295,17 @@ luaA_client_isvisible(lua_State *L) void client_set_icon(client_t *c, cairo_surface_t *s) { + lua_State *L = globalconf_get_lua_State(); + if (s) s = draw_dup_image_surface(s); if(c->icon) cairo_surface_destroy(c->icon); c->icon = s; - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "property::icon", 0); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "property::icon", 0); + lua_pop(L, 1); } /** Kill a client. @@ -1340,7 +1350,7 @@ luaA_client_swap(lua_State *L) *ref_c = swap; *ref_swap = c; - luaA_class_emit_signal(globalconf.L, &client_class, "list", 0); + luaA_class_emit_signal(L, &client_class, "list", 0); } return 0; @@ -2156,7 +2166,7 @@ luaA_client_module_index(lua_State *L) const char *buf = luaL_checkstring(L, 2); if (A_STREQ(buf, "focus")) - return luaA_object_push(globalconf.L, globalconf.focus.client); + return luaA_object_push(L, globalconf.focus.client); return 0; } diff --git a/objects/screen.c b/objects/screen.c index ea8d27581..36bd1407f 100644 --- a/objects/screen.c +++ b/objects/screen.c @@ -144,6 +144,8 @@ screen_scan_randr(void) for(int i = 0; i < screen_res_r->num_crtcs; i++) { + lua_State *L = globalconf_get_lua_State(); + /* Get info on the output crtc */ xcb_randr_get_crtc_info_cookie_t crtc_info_c = xcb_randr_get_crtc_info(globalconf.connection, randr_crtcs[i], XCB_CURRENT_TIME); xcb_randr_get_crtc_info_reply_t *crtc_info_r = xcb_randr_get_crtc_info_reply(globalconf.connection, crtc_info_c, NULL); @@ -153,7 +155,7 @@ screen_scan_randr(void) continue; /* Prepare the new screen */ - screen_t *new_screen = screen_new(globalconf.L); + screen_t *new_screen = screen_new(L); new_screen->geometry.x = crtc_info_r->x; new_screen->geometry.y = crtc_info_r->y; new_screen->geometry.width= crtc_info_r->width; @@ -179,7 +181,7 @@ screen_scan_randr(void) p_delete(&output_info_r); } - screen_add(globalconf.L, -1); + screen_add(L, -1); p_delete(&crtc_info_r); } @@ -223,9 +225,10 @@ screen_scan_xinerama(void) /* now check if screens overlaps (same x,y): if so, we take only the biggest one */ for(int screen = 0; screen < xinerama_screen_number; screen++) { - screen_t *s = screen_new(globalconf.L); + lua_State *L = globalconf_get_lua_State(); + screen_t *s = screen_new(L); s->geometry = screen_xsitoarea(xsi[screen]); - screen_add(globalconf.L, -1); + screen_add(L, -1); } p_delete(&xsq); @@ -239,13 +242,14 @@ screen_scan_xinerama(void) static void screen_scan_x11(void) { /* One screen only / Zaphod mode */ + lua_State *L = globalconf_get_lua_State(); xcb_screen_t *xcb_screen = globalconf.screen; - screen_t *s = screen_new(globalconf.L); + screen_t *s = screen_new(L); s->geometry.x = 0; s->geometry.y = 0; s->geometry.width = xcb_screen->width_in_pixels; s->geometry.height = xcb_screen->height_in_pixels; - screen_add(globalconf.L, -1); + screen_add(L, -1); } /** Get screens informations and fill global configuration. @@ -367,6 +371,7 @@ display_area_get(void) void screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize) { + lua_State *L = globalconf_get_lua_State(); screen_t *old_screen = c->screen; area_t from, to; bool had_focus = false; @@ -381,9 +386,9 @@ screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize) if(!doresize) { - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "property::screen", 0); + lua_pop(L, 1); if(had_focus) client_focus(c); return; @@ -411,9 +416,9 @@ screen_client_moveto(client_t *c, screen_t *new_screen, bool doresize) /* move / resize the client */ client_resize(c, new_geometry, false); - luaA_object_push(globalconf.L, c); - luaA_object_emit_signal(globalconf.L, -1, "property::screen", 0); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + luaA_object_emit_signal(L, -1, "property::screen", 0); + lua_pop(L, 1); if(had_focus) client_focus(c); } diff --git a/objects/tag.c b/objects/tag.c index 80bc00461..096a784d4 100644 --- a/objects/tag.c +++ b/objects/tag.c @@ -46,7 +46,8 @@ LUA_OBJECT_FUNCS(tag_class, tag_t, tag) void tag_unref_simplified(tag_t **tag) { - luaA_object_unref(globalconf.L, *tag); + lua_State *L = globalconf_get_lua_State(); + luaA_object_unref(L, *tag); } static void @@ -79,8 +80,9 @@ tag_view(lua_State *L, int udx, bool view) } static void -tag_client_emit_signal(lua_State *L, tag_t *t, client_t *c, const char *signame) +tag_client_emit_signal(tag_t *t, client_t *c, const char *signame) { + lua_State *L = globalconf_get_lua_State(); luaA_object_push(L, c); luaA_object_push(L, t); /* emit signal on client, with new tag as argument */ @@ -114,7 +116,7 @@ tag_client(lua_State *L, client_t *c) ewmh_client_update_desktop(c); banning_need_update(); - tag_client_emit_signal(L, t, c, "tagged"); + tag_client_emit_signal(t, c, "tagged"); } /** Untag a client with specified tag. @@ -127,11 +129,12 @@ untag_client(client_t *c, tag_t *t) for(int i = 0; i < t->clients.len; i++) if(t->clients.tab[i] == c) { + lua_State *L = globalconf_get_lua_State(); client_array_take(&t->clients, i); banning_need_update(); ewmh_client_update_desktop(c); - tag_client_emit_signal(globalconf.L, t, c, "untagged"); - luaA_object_unref(globalconf.L, t); + tag_client_emit_signal(t, c, "untagged"); + luaA_object_unref(L, t); return; } } diff --git a/objects/timer.c b/objects/timer.c index 0cf57f8a6..c7ea5e31b 100644 --- a/objects/timer.c +++ b/objects/timer.c @@ -38,9 +38,10 @@ LUA_OBJECT_FUNCS(timer_class, atimer_t, timer) static gboolean timer_emit_signal(gpointer data) { - luaA_object_push(globalconf.L, data); - luaA_object_emit_signal(globalconf.L, -1, "timeout", 0); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, data); + luaA_object_emit_signal(L, -1, "timeout", 0); + lua_pop(L, 1); return TRUE; } diff --git a/property.c b/property.c index 8b77ded68..177cc92e0 100644 --- a/property.c +++ b/property.c @@ -44,11 +44,12 @@ void \ property_update_##funcname(client_t *c, xcb_get_property_cookie_t cookie) \ { \ + lua_State *L = globalconf_get_lua_State(); \ xcb_get_property_reply_t * reply = \ xcb_get_property_reply(globalconf.connection, cookie, NULL); \ - luaA_object_push(globalconf.L, c); \ - setfunc(globalconf.L, -1, xutil_get_text_property_from_reply(reply)); \ - lua_pop(globalconf.L, 1); \ + luaA_object_push(L, c); \ + setfunc(L, -1, xutil_get_text_property_from_reply(reply)); \ + lua_pop(L, 1); \ p_delete(&reply); \ } \ static int \ @@ -102,6 +103,7 @@ property_get_wm_transient_for(client_t *c) void property_update_wm_transient_for(client_t *c, xcb_get_property_cookie_t cookie) { + lua_State *L = globalconf_get_lua_State(); xcb_window_t trans; int counter; client_t *tc, *tmp; @@ -113,9 +115,9 @@ property_update_wm_transient_for(client_t *c, xcb_get_property_cookie_t cookie) tmp = tc = client_getbywin(trans); - luaA_object_push(globalconf.L, c); - client_set_type(globalconf.L, -1, WINDOW_TYPE_DIALOG); - client_set_above(globalconf.L, -1, false); + luaA_object_push(L, c); + client_set_type(L, -1, WINDOW_TYPE_DIALOG); + client_set_above(L, -1, false); /* Verify that there are no loops in the transient_for relation after we are done */ for(counter = 0; tmp != NULL && counter <= globalconf.stack.len; counter++) @@ -126,9 +128,9 @@ property_update_wm_transient_for(client_t *c, xcb_get_property_cookie_t cookie) tmp = tmp->transient_for; } if (counter <= globalconf.stack.len) - client_set_transient_for(globalconf.L, -1, tc); + client_set_transient_for(L, -1, tc); - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } xcb_get_property_cookie_t @@ -187,6 +189,7 @@ property_get_wm_hints(client_t *c) void property_update_wm_hints(client_t *c, xcb_get_property_cookie_t cookie) { + lua_State *L = globalconf_get_lua_State(); xcb_icccm_wm_hints_t wmh; if(!xcb_icccm_get_wm_hints_reply(globalconf.connection, @@ -194,16 +197,16 @@ property_update_wm_hints(client_t *c, xcb_get_property_cookie_t cookie) &wmh, NULL)) return; - luaA_object_push(globalconf.L, c); - client_set_urgent(globalconf.L, -1, xcb_icccm_wm_hints_get_urgency(&wmh)); + luaA_object_push(L, c); + client_set_urgent(L, -1, xcb_icccm_wm_hints_get_urgency(&wmh)); if(wmh.flags & XCB_ICCCM_WM_HINT_INPUT) c->nofocus = !wmh.input; if(wmh.flags & XCB_ICCCM_WM_HINT_WINDOW_GROUP) - client_set_group_window(globalconf.L, -1, wmh.window_group); + client_set_group_window(L, -1, wmh.window_group); - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } xcb_get_property_cookie_t @@ -219,6 +222,7 @@ property_get_wm_class(client_t *c) void property_update_wm_class(client_t *c, xcb_get_property_cookie_t cookie) { + lua_State *L = globalconf_get_lua_State(); xcb_icccm_get_wm_class_reply_t hint; if(!xcb_icccm_get_wm_class_reply(globalconf.connection, @@ -226,9 +230,9 @@ property_update_wm_class(client_t *c, xcb_get_property_cookie_t cookie) &hint, NULL)) return; - luaA_object_push(globalconf.L, c); - client_set_class_instance(globalconf.L, -1, hint.class_name, hint.instance_name); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + client_set_class_instance(L, -1, hint.class_name, hint.instance_name); + lua_pop(L, 1); xcb_icccm_get_wm_class_reply_wipe(&hint); } @@ -281,9 +285,10 @@ property_update_net_wm_pid(client_t *c, xcb_get_property_cookie_t cookie) uint32_t *rdata = xcb_get_property_value(reply); if(rdata) { - luaA_object_push(globalconf.L, c); - client_set_pid(globalconf.L, -1, *rdata); - lua_pop(globalconf.L, 1); + lua_State *L = globalconf_get_lua_State(); + luaA_object_push(L, c); + client_set_pid(L, -1, *rdata); + lua_pop(L, 1); } } @@ -346,22 +351,23 @@ static int property_handle_net_wm_opacity(uint8_t state, xcb_window_t window) { + lua_State *L = globalconf_get_lua_State(); drawin_t *drawin = drawin_getbywin(window); if(drawin) { - luaA_object_push(globalconf.L, drawin); - window_set_opacity(globalconf.L, -1, xwindow_get_opacity(drawin->window)); - lua_pop(globalconf.L, -1); + luaA_object_push(L, drawin); + window_set_opacity(L, -1, xwindow_get_opacity(drawin->window)); + lua_pop(L, -1); } else { client_t *c = client_getbywin(window); if(c) { - luaA_object_push(globalconf.L, c); - window_set_opacity(globalconf.L, -1, xwindow_get_opacity(c->window)); - lua_pop(globalconf.L, 1); + luaA_object_push(L, c); + window_set_opacity(L, -1, xwindow_get_opacity(c->window)); + lua_pop(L, 1); } } @@ -372,7 +378,8 @@ static int property_handle_xrootpmap_id(uint8_t state, xcb_window_t window) { - signal_object_emit(globalconf.L, &global_signals, "wallpaper_changed", 0); + lua_State *L = globalconf_get_lua_State(); + signal_object_emit(L, &global_signals, "wallpaper_changed", 0); return 0; } @@ -382,6 +389,7 @@ property_handle_xrootpmap_id(uint8_t state, static void property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev) { + lua_State *L = globalconf_get_lua_State(); xproperty_t *prop; xproperty_t lookup = { .atom = ev->atom }; buffer_t buf; @@ -409,11 +417,11 @@ property_handle_propertynotify_xproperty(xcb_property_notify_event_t *ev) /* And emit the right signal */ if (obj) { - luaA_object_push(globalconf.L, obj); - luaA_object_emit_signal(globalconf.L, -1, buf.s, 0); - lua_pop(globalconf.L, 1); + luaA_object_push(L, obj); + luaA_object_emit_signal(L, -1, buf.s, 0); + lua_pop(L, 1); } else - signal_object_emit(globalconf.L, &global_signals, buf.s, 0); + signal_object_emit(L, &global_signals, buf.s, 0); buffer_wipe(&buf); } diff --git a/spawn.c b/spawn.c index a2577214a..1da89f5e3 100644 --- a/spawn.c +++ b/spawn.c @@ -67,16 +67,17 @@ spawn_monitor_timeout(gpointer sequence) if(sig) { /* send a timeout signal */ - lua_createtable(globalconf.L, 0, 2); - lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence)); - lua_setfield(globalconf.L, -2, "id"); + lua_State *L = globalconf_get_lua_State(); + lua_createtable(L, 0, 2); + lua_pushstring(L, sn_startup_sequence_get_id(sequence)); + lua_setfield(L, -2, "id"); foreach(func, sig->sigfuncs) { - lua_pushvalue(globalconf.L, -1); - luaA_object_push(globalconf.L, (void *) *func); - luaA_dofunction(globalconf.L, 1, 0); + lua_pushvalue(L, -1); + luaA_object_push(L, (void *) *func); + luaA_dofunction(L, 1, 0); } - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } else warn("spawn::timeout signal is missing"); @@ -88,12 +89,13 @@ spawn_monitor_timeout(gpointer sequence) static void spawn_monitor_event(SnMonitorEvent *event, void *data) { + lua_State *L = globalconf_get_lua_State(); SnStartupSequence *sequence = sn_monitor_event_get_startup_sequence(event); SnMonitorEventType event_type = sn_monitor_event_get_type(event); - lua_createtable(globalconf.L, 0, 2); - lua_pushstring(globalconf.L, sn_startup_sequence_get_id(sequence)); - lua_setfield(globalconf.L, -2, "id"); + lua_createtable(L, 0, 2); + lua_pushstring(L, sn_startup_sequence_get_id(sequence)); + lua_setfield(L, -2, "id"); const char *event_type_str = NULL; @@ -131,35 +133,35 @@ spawn_monitor_event(SnMonitorEvent *event, void *data) const char *s = sn_startup_sequence_get_name(sequence); if(s) { - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "name"); + lua_pushstring(L, s); + lua_setfield(L, -2, "name"); } if((s = sn_startup_sequence_get_description(sequence))) { - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "description"); + lua_pushstring(L, s); + lua_setfield(L, -2, "description"); } - lua_pushnumber(globalconf.L, sn_startup_sequence_get_workspace(sequence)); - lua_setfield(globalconf.L, -2, "workspace"); + lua_pushnumber(L, sn_startup_sequence_get_workspace(sequence)); + lua_setfield(L, -2, "workspace"); if((s = sn_startup_sequence_get_binary_name(sequence))) { - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "binary_name"); + lua_pushstring(L, s); + lua_setfield(L, -2, "binary_name"); } if((s = sn_startup_sequence_get_icon_name(sequence))) { - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "icon_name"); + lua_pushstring(L, s); + lua_setfield(L, -2, "icon_name"); } if((s = sn_startup_sequence_get_wmclass(sequence))) { - lua_pushstring(globalconf.L, s); - lua_setfield(globalconf.L, -2, "wmclass"); + lua_pushstring(L, s); + lua_setfield(L, -2, "wmclass"); } } break; @@ -177,11 +179,11 @@ spawn_monitor_event(SnMonitorEvent *event, void *data) { foreach(func, sig->sigfuncs) { - lua_pushvalue(globalconf.L, -1); - luaA_object_push(globalconf.L, (void *) *func); - luaA_dofunction(globalconf.L, 1, 0); + lua_pushvalue(L, -1); + luaA_object_push(L, (void *) *func); + luaA_dofunction(L, 1, 0); } - lua_pop(globalconf.L, 1); + lua_pop(L, 1); } else warn("%s signal is missing", event_type_str); diff --git a/systray.c b/systray.c index 86ffe9e65..d312251ed 100644 --- a/systray.c +++ b/systray.c @@ -248,7 +248,8 @@ xembed_process_client_message(xcb_client_message_event_t *ev) void luaA_systray_invalidate(void) { - signal_object_emit(globalconf.L, &global_signals, "systray::update", 0); + lua_State *L = globalconf_get_lua_State(); + signal_object_emit(L, &global_signals, "systray::update", 0); } static void