Read and pass config to mruby class

pull/1/head
Takashi Kokubun 8 years ago
parent 56d8592bd6
commit 0c531e1583

@ -1,5 +1,6 @@
current_dir := $(shell pwd)
SRCS := $(wildcard tools/xkremap/*.[ch])
CSRCS := $(wildcard tools/xkremap/*.[ch])
RBSRCS := $(wildcard mrblib/xkremap/*.rb)
.PHONY: all
all: xkremap
@ -11,5 +12,5 @@ mruby:
curl -L --fail --retry 3 --retry-delay 1 https://github.com/mruby/mruby/archive/1.2.0.tar.gz -s -o - | tar zxf -
mv mruby-1.2.0 $@
mruby/build/host/bin/xkremap: mruby build_config.rb $(SRCS)
mruby/build/host/bin/xkremap: mruby build_config.rb $(CSRCS) $(RBSRCS)
cd mruby && MRUBY_CONFIG="$(current_dir)/build_config.rb" make

@ -1,6 +1,7 @@
MRuby::Build.new do |conf|
toolchain :gcc
conf.gembox 'default'
conf.gem File.expand_path(File.dirname(__FILE__))
conf.instance_eval do

@ -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…
Cancel
Save