<commit_before>/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

typedef struct {
	int ld;
	int brightness;
	int isOn;
} LightInfo;

static int openLightDevice(lua_State *L) {
	LightInfo *light = (LightInfo*) lua_newuserdata(L, sizeof(LightInfo));
	luaL_getmetatable(L, "kobolight");
	lua_setmetatable(L, -2);
	light->ld = open("/dev/ntx_io", O_RDWR);
	if (light->ld == -1) {
		return luaL_error(L, "cannot open light device");
	}
	return 1;
}

static int closeLightDevice(lua_State *L) {
	LightInfo *light = (LightInfo*) lua_newuserdata(L, sizeof(LightInfo));
	close(light->ld);
	return 0;
}

static int setBrightness(lua_State *L) {
	LightInfo *light = (LightInfo*) luaL_checkudata(L, 1, "kobolight");
	int brightness = luaL_optint(L, 2, 0);

	if (brightness < 0 || brightness > 100) {
		return luaL_error(L, "Wrong brightness value %d given!", brightness);
	}

	if (ioctl(light->ld, 241, brightness)) {
		return luaL_error(L, "cannot change brightess value");
	}
	light->brightness = brightness;
	light->isOn = 1;
	return 0;
}

static int toggleLight(lua_State *L) {
	LightInfo *light = (LightInfo*) luaL_checkudata(L, 1, "kobolight");
	
	if (light->isOn) {
		if (ioctl(light->ld, 241, 0)) {
			return luaL_error(L, "cannot turn off the light");
		}
		light->isOn = 0;
		return 0;
	}
	else {
		if (ioctl(light->ld, 241, light->brightness)) {
			return luaL_error(L, "cannot turn on the light");
		}
		light->isOn = 1;
		return 1;
	}
}

static const struct luaL_Reg kobolight_func[] = {
	{"open", openLightDevice},
	{NULL, NULL}
};

static const struct luaL_Reg kobolight_meth[] = {
	{"close", closeLightDevice},
	{"__gc", closeLightDevice},
	{"toggle", toggleLight},
	{"setBrightness", setBrightness},
	{NULL, NULL}
};

int luaopen_kobolight(lua_State *L) {
	luaL_newmetatable(L, "kobolight");
	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);
	lua_settable(L, -3);
	luaL_register(L, NULL, kobolight_meth);
	luaL_register(L, "kobolight", kobolight_func);

	return 1;
}
<commit_msg>Add some methods useful for standby support.<commit_after>/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

typedef struct {
	int ld;
	int brightness;
	int isOn;
} LightInfo;

static int openLightDevice(lua_State *L) {
	LightInfo *light = (LightInfo*) lua_newuserdata(L, sizeof(LightInfo));
	luaL_getmetatable(L, "kobolight");
	lua_setmetatable(L, -2);
	light->ld = open("/dev/ntx_io", O_RDWR);
	if (light->ld == -1) {
		return luaL_error(L, "cannot open light device");
	}
	return 1;
}

static int closeLightDevice(lua_State *L) {
	LightInfo *light = (LightInfo*) lua_newuserdata(L, sizeof(LightInfo));
	close(light->ld);
	return 0;
}

static int setBrightness(lua_State *L) {
	LightInfo *light = (LightInfo*) luaL_checkudata(L, 1, "kobolight");
	int brightness = luaL_optint(L, 2, 0);

	if (brightness < 0 || brightness > 100) {
		return luaL_error(L, "Wrong brightness value %d given!", brightness);
	}

	if (ioctl(light->ld, 241, brightness)) {
		return luaL_error(L, "cannot change brightess value");
	}
	light->brightness = brightness;
	light->isOn = 1;
	return 0;
}

static int toggleLight(lua_State *L) {
	LightInfo *light = (LightInfo*) luaL_checkudata(L, 1, "kobolight");
	
	if (light->isOn) {
		if (ioctl(light->ld, 241, 0)) {
			return luaL_error(L, "cannot turn off the light");
		}
		light->isOn = 0;
		return 0;
	}
	else {
		if (ioctl(light->ld, 241, light->brightness)) {
			return luaL_error(L, "cannot turn on the light");
		}
		light->isOn = 1;
		return 1;
	}
}

static int sleepLight(lua_State *L) {
	LightInfo *light = (LightInfo*) luaL_checkudata(L, 1, "kobolight");

	if (light->isOn) {
		if (ioctl(light->ld, 241, 0)) {
			return luaL_error(L, "cannot turn off the light");
		}
		// leave light->isOn == 1
	}
	return 0;
}

static int restoreLight(lua_State *L) {
	LightInfo *light = (LightInfo*) luaL_checkudata(L, 1, "kobolight");

	if (light->isOn) {
		if (ioctl(light->ld, 241, light->brightness)) {
			return luaL_error(L, "cannot turn on the light");
		}
	}
	return 1;
}

static const struct luaL_Reg kobolight_func[] = {
	{"open", openLightDevice},
	{NULL, NULL}
};

static const struct luaL_Reg kobolight_meth[] = {
	{"close", closeLightDevice},
	{"__gc", closeLightDevice},
	{"toggle", toggleLight},
	{"sleep", sleepLight},
	{"restore", restoreLight},
	{"setBrightness", setBrightness},
	{NULL, NULL}
};

int luaopen_kobolight(lua_State *L) {
	luaL_newmetatable(L, "kobolight");
	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);
	lua_settable(L, -3);
	luaL_register(L, NULL, kobolight_meth);
	luaL_register(L, "kobolight", kobolight_func);

	return 1;
}

{'fraction_non_alphanumeric': 0.13568588469184892, 'fraction_numerical': 0.012591119946984758, 'mean_word_length': 4.3662222222222224, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2517285', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2178, 'n_words': 653}