mirror of https://github.com/k0kubun/xremap
Read and pass config to mruby class
parent
56d8592bd6
commit
0c531e1583
@ -0,0 +1,16 @@
|
||||
module Xkremap
|
||||
class Config
|
||||
# @param [String] dsl
|
||||
def self.load(dsl)
|
||||
config = self.new
|
||||
ConfigContext.new(config).instance_eval(dsl)
|
||||
config
|
||||
end
|
||||
|
||||
def initialize
|
||||
@remaps = []
|
||||
end
|
||||
|
||||
attr_reader :remaps
|
||||
end
|
||||
end
|
@ -0,0 +1,8 @@
|
||||
module Xkremap
|
||||
class ConfigContext
|
||||
# @param [Xkremap::Config] config
|
||||
def initialize(config)
|
||||
@config = config
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
module Xkremap
|
||||
class EventHandler
|
||||
# @param [Xkremap::Config] config
|
||||
# @param [Xkremap::Display] display
|
||||
def initialize(config, display)
|
||||
@config = config
|
||||
@display = display
|
||||
end
|
||||
|
||||
def handle_key_press(serial, keycode, state)
|
||||
p serial
|
||||
end
|
||||
|
||||
def handle_property_notify(serial)
|
||||
end
|
||||
|
||||
def handle_mapping_notify(serial)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,9 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <mruby.h>
|
||||
|
||||
FILE*
|
||||
open_file(char *filename)
|
||||
{
|
||||
if (!strcmp(filename, "-"))
|
||||
return stdin;
|
||||
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (!file) {
|
||||
fprintf(stderr, "Failed to open: '%s'\n", filename);
|
||||
exit(1);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
char*
|
||||
read_file(char *filename)
|
||||
{
|
||||
FILE *fp = open_file(filename);
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
long size = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
char *ret = malloc(size * sizeof(char));
|
||||
fread(ret, sizeof(char), size, fp);
|
||||
fclose(fp);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
mrb_value
|
||||
load_config(mrb_state *mrb, char *filename)
|
||||
{
|
||||
printf("load: %s\n", filename);
|
||||
return mrb_nil_value();
|
||||
char *dsl = read_file(filename);
|
||||
|
||||
struct RClass *mXkremap = mrb_module_get(mrb, "Xkremap");
|
||||
struct RClass *cConfig = mrb_class_get_under(mrb, mXkremap, "Config");
|
||||
mrb_value config = mrb_funcall(mrb, mrb_obj_value(cConfig), "load", 1, mrb_str_new_cstr(mrb, dsl));
|
||||
|
||||
free(dsl);
|
||||
return config;
|
||||
}
|
||||
|
Loading…
Reference in New Issue