Refactoring to allow using kitty in other projects

This commit is contained in:
Kovid Goyal 2017-10-21 12:10:42 +05:30
parent ff35bd61a8
commit 09c723380c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 47 additions and 15 deletions

View File

@ -34,6 +34,7 @@ def _get_config_dir():
config_dir = _get_config_dir() config_dir = _get_config_dir()
del _get_config_dir del _get_config_dir
defconf = os.path.join(config_dir, 'kitty.conf')
class ViewportSize: class ViewportSize:
@ -116,4 +117,11 @@ def selection_clipboard_funcs():
return ans 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') iswayland = not isosx and hasattr(glfw_lib(), 'glfwGetWaylandDisplay')

View File

@ -286,6 +286,18 @@ window_id(WindowWrapper *self) {
return PyLong_FromVoidPtr(self->window); 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* static PyObject*
should_close(WindowWrapper *self) { should_close(WindowWrapper *self) {
PyObject *ans = glfwWindowShouldClose(self->window) ? Py_True : Py_False; PyObject *ans = glfwWindowShouldClose(self->window) ? Py_True : Py_False;
@ -313,6 +325,14 @@ set_should_close(WindowWrapper *self, PyObject *args) {
Py_RETURN_NONE; 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* static PyObject*
is_key_pressed(WindowWrapper *self, PyObject *args) { is_key_pressed(WindowWrapper *self, PyObject *args) {
int c; int c;
@ -436,9 +456,12 @@ static PyMethodDef methods[] = {
#endif #endif
MND(set_should_close, METH_VARARGS), MND(set_should_close, METH_VARARGS),
MND(is_key_pressed, METH_VARARGS), MND(is_key_pressed, METH_VARARGS),
MND(set_pos, METH_VARARGS),
MND(set_clipboard_string, METH_VARARGS), MND(set_clipboard_string, METH_VARARGS),
MND(make_context_current, METH_NOARGS), MND(make_context_current, METH_NOARGS),
MND(window_id, METH_NOARGS), MND(window_id, METH_NOARGS),
MND(show, METH_NOARGS),
MND(hide, METH_NOARGS),
{"set_icon", (PyCFunction)set_window_icon, METH_VARARGS, ""}, {"set_icon", (PyCFunction)set_window_icon, METH_VARARGS, ""},
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };

View File

@ -14,7 +14,7 @@ from .config import (
cached_values, load_cached_values, load_config, save_cached_values cached_values, load_cached_values, load_config, save_cached_values
) )
from .constants import ( from .constants import (
appname, config_dir, isosx, iswayland, logo_data_file, str_version, appname, defconf, isosx, iswayland, logo_data_file, str_version,
viewport_size viewport_size
) )
from .fast_data_types import ( from .fast_data_types import (
@ -35,9 +35,6 @@ except ImportError:
GLFW_X11_WM_CLASS_NAME = GLFW_X11_WM_CLASS_CLASS = None GLFW_X11_WM_CLASS_NAME = GLFW_X11_WM_CLASS_CLASS = None
defconf = os.path.join(config_dir, 'kitty.conf')
def option_parser(): def option_parser():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog=appname, prog=appname,
@ -154,6 +151,20 @@ def setup_opengl(opts):
glfw_window_hint(GLFW_STENCIL_BITS, 8) 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): def run_app(opts, args):
set_options(opts) set_options(opts)
setup_opengl(opts) setup_opengl(opts)
@ -187,19 +198,9 @@ def run_app(opts, args):
elif not iswayland: # no window icons on wayland elif not iswayland: # no window icons on wayland
with open(logo_data_file, 'rb') as f: with open(logo_data_file, 'rb') as f:
window.set_icon(f.read(), 256, 256) window.set_icon(f.read(), 256, 256)
set_logical_dpi(*get_logical_dpi()) initialize_window(window, opts)
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()
boss = Boss(window, opts, args) boss = Boss(window, opts, args)
boss.start() 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: try:
boss.child_monitor.main_loop() boss.child_monitor.main_loop()
finally: finally: