DRYer glfw

This commit is contained in:
Kovid Goyal 2017-09-09 12:15:47 +05:30
parent 4a27793651
commit a4580f2126
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 31 additions and 50 deletions

View File

@ -173,3 +173,17 @@ cocoa_get_lang(PyObject UNUSED *self) {
if (!locale) { Py_RETURN_NONE; } if (!locale) { Py_RETURN_NONE; }
return Py_BuildValue("s", [locale UTF8String]); 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;
}

View File

@ -6,7 +6,6 @@
*/ */
#include "data-types.h" #include "data-types.h"
#include "glfw.h"
#include "modes.h" #include "modes.h"
#include "sprites.h" #include "sprites.h"
#include <stddef.h> #include <stddef.h>
@ -68,7 +67,6 @@ static PyMethodDef module_methods[] = {
{"redirect_std_streams", (PyCFunction)redirect_std_streams, METH_VARARGS, ""}, {"redirect_std_streams", (PyCFunction)redirect_std_streams, METH_VARARGS, ""},
{"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""}, {"wcwidth", (PyCFunction)wcwidth_wrap, METH_O, ""},
{"change_wcwidth", (PyCFunction)change_wcwidth_wrap, METH_O, ""}, {"change_wcwidth", (PyCFunction)change_wcwidth_wrap, METH_O, ""},
GLFW_FUNC_WRAPPERS
SPRITE_FUNC_WRAPPERS SPRITE_FUNC_WRAPPERS
#ifdef WITH_PROFILER #ifdef WITH_PROFILER
{"start_profiler", (PyCFunction)start_profiler, METH_VARARGS, ""}, {"start_profiler", (PyCFunction)start_profiler, METH_VARARGS, ""},
@ -101,8 +99,10 @@ extern int init_Face(PyObject *);
extern int init_Window(PyObject *); extern int init_Window(PyObject *);
extern bool init_freetype_library(PyObject*); extern bool init_freetype_library(PyObject*);
extern bool init_fontconfig_library(PyObject*); extern bool init_fontconfig_library(PyObject*);
extern bool init_glfw(PyObject *m);
#ifdef __APPLE__ #ifdef __APPLE__
extern int init_CoreText(PyObject *); extern int init_CoreText(PyObject *);
extern bool init_cocoa(PyObject *module);
#endif #endif
@ -127,6 +127,7 @@ PyInit_fast_data_types(void) {
if (!init_Window(m)) return NULL; if (!init_Window(m)) return NULL;
#ifdef __APPLE__ #ifdef __APPLE__
if (!init_CoreText(m)) return NULL; if (!init_CoreText(m)) return NULL;
if (!init_cocoa(m)) return NULL;
#else #else
if (!init_Face(m)) return NULL; if (!init_Face(m)) return NULL;
if (!init_freetype_library(m)) return NULL; if (!init_freetype_library(m)) return NULL;

View File

@ -439,11 +439,24 @@ PyTypeObject Window_Type = {
INIT_TYPE(Window) 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 {{{ // constants {{{
bool bool
init_glfw(PyObject *m) { init_glfw(PyObject *m) {
PyEval_InitThreads(); if (PyModule_AddFunctions(m, module_methods) != 0) return false;
glfwSetErrorCallback(cb_error_callback); glfwSetErrorCallback(cb_error_callback);
#define ADDC(n) if(PyModule_AddIntConstant(m, #n, n) != 0) return false; #define ADDC(n) if(PyModule_AddIntConstant(m, #n, n) != 0) return false;
#ifdef GLFW_X11_WM_CLASS_NAME #ifdef GLFW_X11_WM_CLASS_NAME

View File

@ -1,47 +0,0 @@
/*
* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
*
* 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