From a4580f2126c7c6e7e6d7548dd9d45dbc24195b2d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Sep 2017 12:15:47 +0530 Subject: [PATCH] DRYer glfw --- kitty/cocoa_window.m | 14 +++++++++++++ kitty/data-types.c | 5 +++-- kitty/glfw.c | 15 +++++++++++++- kitty/glfw.h | 47 -------------------------------------------- 4 files changed, 31 insertions(+), 50 deletions(-) delete mode 100644 kitty/glfw.h diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index e4404203d..f3e3d321c 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -173,3 +173,17 @@ cocoa_get_lang(PyObject UNUSED *self) { if (!locale) { Py_RETURN_NONE; } return Py_BuildValue("s", [locale UTF8String]); } + +static PyMethodDef module_methods[] = { + {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, \ + {"cocoa_make_window_resizable", (PyCFunction)cocoa_make_window_resizable, METH_O, ""}, \ + {"cocoa_create_global_menu", (PyCFunction)cocoa_create_global_menu, METH_NOARGS, ""}, \ + {"cocoa_update_title", (PyCFunction)cocoa_update_title, METH_O, ""}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +bool +init_cocoa(PyObject *module) { + if (PyModule_AddFunctions(module, module_methods) != 0) return false; + return true; +} diff --git a/kitty/data-types.c b/kitty/data-types.c index dd9afec7e..247881ae0 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -6,7 +6,6 @@ */ #include "data-types.h" -#include "glfw.h" #include "modes.h" #include "sprites.h" #include @@ -68,7 +67,6 @@ static PyMethodDef module_methods[] = { {"redirect_std_streams", (PyCFunction)redirect_std_streams, METH_VARARGS, ""}, {"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""}, {"change_wcwidth", (PyCFunction)change_wcwidth_wrap, METH_O, ""}, - GLFW_FUNC_WRAPPERS SPRITE_FUNC_WRAPPERS #ifdef WITH_PROFILER {"start_profiler", (PyCFunction)start_profiler, METH_VARARGS, ""}, @@ -101,8 +99,10 @@ extern int init_Face(PyObject *); extern int init_Window(PyObject *); extern bool init_freetype_library(PyObject*); extern bool init_fontconfig_library(PyObject*); +extern bool init_glfw(PyObject *m); #ifdef __APPLE__ extern int init_CoreText(PyObject *); +extern bool init_cocoa(PyObject *module); #endif @@ -127,6 +127,7 @@ PyInit_fast_data_types(void) { if (!init_Window(m)) return NULL; #ifdef __APPLE__ if (!init_CoreText(m)) return NULL; + if (!init_cocoa(m)) return NULL; #else if (!init_Face(m)) return NULL; if (!init_freetype_library(m)) return NULL; diff --git a/kitty/glfw.c b/kitty/glfw.c index b1f3bfa8c..d8d1c9908 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -439,11 +439,24 @@ PyTypeObject Window_Type = { INIT_TYPE(Window) +static PyMethodDef module_methods[] = { + {"glfw_set_error_callback", (PyCFunction)glfw_set_error_callback, METH_O, ""}, \ + {"glfw_init", (PyCFunction)glfw_init, METH_NOARGS, ""}, \ + {"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""}, \ + {"glfw_window_hint", (PyCFunction)glfw_window_hint, METH_VARARGS, ""}, \ + {"glfw_swap_interval", (PyCFunction)glfw_swap_interval, METH_VARARGS, ""}, \ + {"glfw_wait_events", (PyCFunction)glfw_wait_events, METH_VARARGS, ""}, \ + {"glfw_post_empty_event", (PyCFunction)glfw_post_empty_event, METH_NOARGS, ""}, \ + {"glfw_get_physical_dpi", (PyCFunction)glfw_get_physical_dpi, METH_NOARGS, ""}, \ + {"glfw_get_key_name", (PyCFunction)glfw_get_key_name, METH_VARARGS, ""}, \ + {"glfw_init_hint_string", (PyCFunction)glfw_init_hint_string, METH_VARARGS, ""}, \ + {NULL, NULL, 0, NULL} /* Sentinel */ +}; // constants {{{ bool init_glfw(PyObject *m) { - PyEval_InitThreads(); + if (PyModule_AddFunctions(m, module_methods) != 0) return false; glfwSetErrorCallback(cb_error_callback); #define ADDC(n) if(PyModule_AddIntConstant(m, #n, n) != 0) return false; #ifdef GLFW_X11_WM_CLASS_NAME diff --git a/kitty/glfw.h b/kitty/glfw.h deleted file mode 100644 index d7e7019de..000000000 --- a/kitty/glfw.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2016 Kovid Goyal - * - * Distributed under terms of the GPL3 license. - */ - -#pragma once - -bool init_glfw(PyObject *m); - -PyObject* glfw_set_error_callback(PyObject UNUSED *self, PyObject *callback); -PyObject* glfw_init(PyObject UNUSED *self); -PyObject* glfw_terminate(PyObject UNUSED *self); -PyObject* glfw_window_hint(PyObject UNUSED *self, PyObject *args); -PyObject* glfw_swap_interval(PyObject UNUSED *self, PyObject *args); -PyObject* glfw_wait_events(PyObject UNUSED *self, PyObject*); -PyObject* glfw_post_empty_event(PyObject UNUSED *self); -PyObject* glfw_get_physical_dpi(PyObject UNUSED *self); -PyObject* glfw_get_key_name(PyObject UNUSED *self, PyObject *args); -PyObject* glfw_init_hint_string(PyObject UNUSED *self, PyObject *args); - -#ifdef __APPLE__ -PyObject* cocoa_get_lang(PyObject UNUSED *self); -PyObject* cocoa_make_window_resizable(PyObject UNUSED *self, PyObject *window_id); -PyObject* cocoa_create_global_menu(PyObject UNUSED *self); -PyObject* cocoa_update_title(PyObject UNUSED *self, PyObject *title); -#define COCOA_FUNCS \ - {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, \ - {"cocoa_make_window_resizable", (PyCFunction)cocoa_make_window_resizable, METH_O, ""}, \ - {"cocoa_create_global_menu", (PyCFunction)cocoa_create_global_menu, METH_NOARGS, ""}, \ - {"cocoa_update_title", (PyCFunction)cocoa_update_title, METH_O, ""}, -#else -#define COCOA_FUNCS -#endif - -#define GLFW_FUNC_WRAPPERS \ - {"glfw_set_error_callback", (PyCFunction)glfw_set_error_callback, METH_O, ""}, \ - {"glfw_init", (PyCFunction)glfw_init, METH_NOARGS, ""}, \ - {"glfw_terminate", (PyCFunction)glfw_terminate, METH_NOARGS, ""}, \ - {"glfw_window_hint", (PyCFunction)glfw_window_hint, METH_VARARGS, ""}, \ - {"glfw_swap_interval", (PyCFunction)glfw_swap_interval, METH_VARARGS, ""}, \ - {"glfw_wait_events", (PyCFunction)glfw_wait_events, METH_VARARGS, ""}, \ - {"glfw_post_empty_event", (PyCFunction)glfw_post_empty_event, METH_NOARGS, ""}, \ - {"glfw_get_physical_dpi", (PyCFunction)glfw_get_physical_dpi, METH_NOARGS, ""}, \ - {"glfw_get_key_name", (PyCFunction)glfw_get_key_name, METH_VARARGS, ""}, \ - {"glfw_init_hint_string", (PyCFunction)glfw_init_hint_string, METH_VARARGS, ""}, \ - COCOA_FUNCS