From 09c723380c4ccd31439ce9946c556e24ccdf67bc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 21 Oct 2017 12:10:42 +0530 Subject: [PATCH] Refactoring to allow using kitty in other projects --- kitty/constants.py | 8 ++++++++ kitty/glfw.c | 23 +++++++++++++++++++++++ kitty/main.py | 31 ++++++++++++++++--------------- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/kitty/constants.py b/kitty/constants.py index b046f1c88..b07283f7d 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -34,6 +34,7 @@ def _get_config_dir(): config_dir = _get_config_dir() del _get_config_dir +defconf = os.path.join(config_dir, 'kitty.conf') class ViewportSize: @@ -116,4 +117,11 @@ def selection_clipboard_funcs(): return ans +def x11_window_id(window): + lib = glfw_lib() + lib.glfwGetX11Window.restype = ctypes.c_int32 + lib.glfwGetX11Window.argtypes = [ctypes.c_void_p] + return lib.glfwGetX11Window(window.window_id()) + + iswayland = not isosx and hasattr(glfw_lib(), 'glfwGetWaylandDisplay') diff --git a/kitty/glfw.c b/kitty/glfw.c index a83264ddd..3bfa63023 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -286,6 +286,18 @@ window_id(WindowWrapper *self) { return PyLong_FromVoidPtr(self->window); } +static PyObject* +show(WindowWrapper *self) { + glfwShowWindow(self->window); + Py_RETURN_NONE; +} + +static PyObject* +hide(WindowWrapper *self) { + glfwHideWindow(self->window); + Py_RETURN_NONE; +} + static PyObject* should_close(WindowWrapper *self) { PyObject *ans = glfwWindowShouldClose(self->window) ? Py_True : Py_False; @@ -313,6 +325,14 @@ set_should_close(WindowWrapper *self, PyObject *args) { Py_RETURN_NONE; } +static PyObject* +set_pos(WindowWrapper *self, PyObject *args) { + int x, y; + if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; + glfwSetWindowPos(self->window, x, y); + Py_RETURN_NONE; +} + static PyObject* is_key_pressed(WindowWrapper *self, PyObject *args) { int c; @@ -436,9 +456,12 @@ static PyMethodDef methods[] = { #endif MND(set_should_close, METH_VARARGS), MND(is_key_pressed, METH_VARARGS), + MND(set_pos, METH_VARARGS), MND(set_clipboard_string, METH_VARARGS), MND(make_context_current, METH_NOARGS), MND(window_id, METH_NOARGS), + MND(show, METH_NOARGS), + MND(hide, METH_NOARGS), {"set_icon", (PyCFunction)set_window_icon, METH_VARARGS, ""}, {NULL} /* Sentinel */ }; diff --git a/kitty/main.py b/kitty/main.py index ae7147659..264b4559b 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -14,7 +14,7 @@ from .config import ( cached_values, load_cached_values, load_config, save_cached_values ) from .constants import ( - appname, config_dir, isosx, iswayland, logo_data_file, str_version, + appname, defconf, isosx, iswayland, logo_data_file, str_version, viewport_size ) from .fast_data_types import ( @@ -35,9 +35,6 @@ except ImportError: GLFW_X11_WM_CLASS_NAME = GLFW_X11_WM_CLASS_CLASS = None -defconf = os.path.join(config_dir, 'kitty.conf') - - def option_parser(): parser = argparse.ArgumentParser( prog=appname, @@ -154,6 +151,20 @@ def setup_opengl(opts): glfw_window_hint(GLFW_STENCIL_BITS, 8) +def initialize_window(window, opts): + set_logical_dpi(*get_logical_dpi()) + viewport_size.width, viewport_size.height = window.get_framebuffer_size() + w, h = window.get_window_size() + viewport_size.x_ratio = viewport_size.width / float(w) + viewport_size.y_ratio = viewport_size.height / float(h) + glewInit() + glfw_swap_interval(0) + clear_buffers(window.swap_buffers, color_as_int(opts.background)) + # We dont turn this on as it causes rendering performance to be much worse, + # for example, dragging the mouse to select is laggy + # glfw_swap_interval(1) + + def run_app(opts, args): set_options(opts) setup_opengl(opts) @@ -187,19 +198,9 @@ def run_app(opts, args): elif not iswayland: # no window icons on wayland with open(logo_data_file, 'rb') as f: window.set_icon(f.read(), 256, 256) - set_logical_dpi(*get_logical_dpi()) - viewport_size.width, viewport_size.height = window.get_framebuffer_size() - w, h = window.get_window_size() - viewport_size.x_ratio = viewport_size.width / float(w) - viewport_size.y_ratio = viewport_size.height / float(h) - glewInit() + initialize_window(window, opts) boss = Boss(window, opts, args) boss.start() - glfw_swap_interval(0) - clear_buffers(window.swap_buffers, color_as_int(opts.background)) - # We dont turn this on as it causes rendering performance to be much worse, - # for example, dragging the mouse to select is laggy - # glfw_swap_interval(1) try: boss.child_monitor.main_loop() finally: