diff --git a/pupil_src/launchables/eye.py b/pupil_src/launchables/eye.py index 6ec1c9b451..f8eca4d569 100644 --- a/pupil_src/launchables/eye.py +++ b/pupil_src/launchables/eye.py @@ -126,8 +126,9 @@ def eye( # display import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() from pyglui import ui, graph, cygl from pyglui.cygl.utils import draw_points, RGBA, draw_polyline @@ -432,7 +433,8 @@ def toggle_general_settings(collapsed): general_settings.collapsed = collapsed # Initialize glfw - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window diff --git a/pupil_src/launchables/player.py b/pupil_src/launchables/player.py index 8a93ae8cc2..42d0fece01 100644 --- a/pupil_src/launchables/player.py +++ b/pupil_src/launchables/player.py @@ -73,8 +73,9 @@ def player( # display import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() # check versions for our own depedencies as they are fast-changing from pyglui import __version__ as pyglui_version @@ -371,10 +372,10 @@ def get_dt(): window_name = f"Pupil Player: {meta_info.recording_name} - {rec_dir}" - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) main_window = glfw.create_window(width, height, window_name, None, None) - window_position_manager = gl_utils.WindowPositionManager() window_pos = window_position_manager.new_window_position( window=main_window, @@ -808,8 +809,9 @@ def player_drop( try: import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() import gl_utils from OpenGL.GL import glClearColor @@ -857,7 +859,8 @@ def on_drop(window, paths): session_settings.clear() w, h = session_settings.get("window_size", (1280, 720)) - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) glfw.window_hint(glfw.RESIZABLE, 0) window = glfw.create_window(w, h, "Pupil Player", None, None) diff --git a/pupil_src/launchables/world.py b/pupil_src/launchables/world.py index 3008f1afc5..b664d8fadb 100644 --- a/pupil_src/launchables/world.py +++ b/pupil_src/launchables/world.py @@ -121,8 +121,9 @@ def detection_enabled_setter(is_on: bool): # display import glfw + from gl_utils import GLFWErrorReporting - glfw.ERROR_REPORTING = "raise" + GLFWErrorReporting.set_default() from version_utils import parse_version from pyglui import ui, cygl, __version__ as pyglui_version @@ -525,7 +526,8 @@ def handle_notifications(noti): ) # window and gl setup - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window diff --git a/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py b/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py index 0e2e3b25d8..a89741a8ec 100644 --- a/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py +++ b/pupil_src/shared_modules/calibration_choreography/controller/gui_monitor.py @@ -2,8 +2,9 @@ import typing as T import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() try: from typing import OrderedDict as T_OrderedDict # Python 3.7.2 diff --git a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py index 159038da83..0b3bab1a16 100644 --- a/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py +++ b/pupil_src/shared_modules/calibration_choreography/controller/gui_window.py @@ -5,10 +5,10 @@ import OpenGL.GL as gl import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from gl_utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() from pyglui.cygl.utils import draw_polyline diff --git a/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py b/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py index 09c5acec27..6957bc3087 100644 --- a/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py +++ b/pupil_src/shared_modules/calibration_choreography/natural_feature_plugin.py @@ -16,8 +16,9 @@ import numpy as np import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from methods import normalize from pyglui import ui diff --git a/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py b/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py index 19e70aa91b..c7170fa319 100644 --- a/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py +++ b/pupil_src/shared_modules/calibration_choreography/screen_marker_plugin.py @@ -17,8 +17,9 @@ import OpenGL.GL as gl import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from circle_detector import CircleTracker from platform import system diff --git a/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py b/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py index 3876a0019f..1e534edd1f 100644 --- a/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py +++ b/pupil_src/shared_modules/calibration_choreography/single_marker_plugin.py @@ -27,8 +27,9 @@ import OpenGL.GL as gl import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from .mixin import MonitorSelectionMixin from .controller import ( diff --git a/pupil_src/shared_modules/camera_intrinsics_estimation.py b/pupil_src/shared_modules/camera_intrinsics_estimation.py index 01ecbf8ea6..d0fd3730bd 100644 --- a/pupil_src/shared_modules/camera_intrinsics_estimation.py +++ b/pupil_src/shared_modules/camera_intrinsics_estimation.py @@ -27,10 +27,10 @@ from pyglui.ui import get_opensans_font_path import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from gl_utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() from plugin import Plugin diff --git a/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py b/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py index 14c10d5f41..5315066b02 100644 --- a/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py +++ b/pupil_src/shared_modules/gaze_producer/controller/reference_location_controllers.py @@ -14,8 +14,9 @@ import numpy as np import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() import tasklib from gaze_producer import model, worker diff --git a/pupil_src/shared_modules/gl_utils/__init__.py b/pupil_src/shared_modules/gl_utils/__init__.py index 3dd65f571f..30587d0008 100644 --- a/pupil_src/shared_modules/gl_utils/__init__.py +++ b/pupil_src/shared_modules/gl_utils/__init__.py @@ -28,5 +28,6 @@ make_coord_system_norm_based, make_coord_system_pixel_based, window_coordinate_to_framebuffer_coordinate, + GLFWErrorReporting, ) from .window_position_manager import WindowPositionManager diff --git a/pupil_src/shared_modules/gl_utils/utils.py b/pupil_src/shared_modules/gl_utils/utils.py index 83c9f5cfa5..e42ad359aa 100644 --- a/pupil_src/shared_modules/gl_utils/utils.py +++ b/pupil_src/shared_modules/gl_utils/utils.py @@ -8,6 +8,7 @@ See COPYING and COPYING.LESSER for license details. ---------------------------------------------------------------------------~(*) """ +import contextlib import logging import math import typing as T @@ -41,8 +42,6 @@ ) from OpenGL.GLU import gluErrorString, gluPerspective -glfw.ERROR_REPORTING = "raise" - # OpenGL.FULL_LOGGING = True OpenGL.ERROR_LOGGING = False @@ -330,3 +329,61 @@ def get_window_title_bar_rect(window) -> _Rectangle: return _Rectangle( x=frame_rect.x, y=frame_rect.y, width=frame_rect.width, height=frame_edges.top ) + + +_GLFWErrorReportingDict = T.Dict[T.Union[None, int], str] + + +class GLFWErrorReporting: + @classmethod + @contextlib.contextmanager + def glfw_init(cls): + ignore = [ + # GLFWError: (65544) b'Cocoa: Failed to find service port for display' + # This happens on macOS Big Sur running on Apple Silicone hardware + 65544, + ] + with cls.error_code_handling(ignore=tuple(ignore)): + yield + + @classmethod + @contextlib.contextmanager + def error_code_handling( + cls, + *_, + ignore: T.Optional[T.Tuple[int]] = None, + debug: T.Optional[T.Tuple[int]] = None, + warn: T.Optional[T.Tuple[int]] = None, + raise_: T.Optional[T.Tuple[int]] = None, + ): + old_reporting = glfw.ERROR_REPORTING + + if isinstance(old_reporting, dict): + new_reporting: _GLFWErrorReportingDict = dict(old_reporting) + else: + new_reporting = cls.__default_error_reporting() + + new_reporting.update({err_code: "ignore" for err_code in ignore or ()}) + new_reporting.update({err_code: "log" for err_code in debug or ()}) + new_reporting.update({err_code: "warn" for err_code in warn or ()}) + new_reporting.update({err_code: "raise" for err_code in raise_ or ()}) + + glfw.ERROR_REPORTING = new_reporting + + try: + yield + finally: + glfw.ERROR_REPORTING = old_reporting + + @classmethod + def set_default(cls): + glfw.ERROR_REPORTING = cls.__default_error_reporting() + + ### Private + + @staticmethod + def __default_error_reporting() -> _GLFWErrorReportingDict: + return {None: "raise"} + + +GLFWErrorReporting.set_default() diff --git a/pupil_src/shared_modules/gl_utils/window_position_manager.py b/pupil_src/shared_modules/gl_utils/window_position_manager.py index 3c5fbfaf90..ee93b57390 100644 --- a/pupil_src/shared_modules/gl_utils/window_position_manager.py +++ b/pupil_src/shared_modules/gl_utils/window_position_manager.py @@ -13,10 +13,10 @@ import typing as T import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from .utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() class WindowPositionManager: diff --git a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py index 3625da2be5..42aff44a7d 100644 --- a/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py +++ b/pupil_src/shared_modules/head_pose_tracker/ui/gl_window.py @@ -16,8 +16,9 @@ import gl_utils import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from observable import Observable @@ -64,7 +65,8 @@ def _init_trackball(): return trackball def _glfw_init(self): - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) window = glfw.create_window( 640, diff --git a/pupil_src/shared_modules/log_display.py b/pupil_src/shared_modules/log_display.py index 6526623e14..fe48023312 100644 --- a/pupil_src/shared_modules/log_display.py +++ b/pupil_src/shared_modules/log_display.py @@ -16,10 +16,10 @@ from pyglui.pyfontstash import fontstash from pyglui.ui import get_opensans_font_path import glfw - -glfw.ERROR_REPORTING = "raise" - import gl_utils +from gl_utils import GLFWErrorReporting + +GLFWErrorReporting.set_default() def color_from_level(lvl): diff --git a/pupil_src/shared_modules/marker_auto_trim_marks.py b/pupil_src/shared_modules/marker_auto_trim_marks.py index 192a902e8f..0096557f44 100644 --- a/pupil_src/shared_modules/marker_auto_trim_marks.py +++ b/pupil_src/shared_modules/marker_auto_trim_marks.py @@ -32,8 +32,9 @@ ) import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() import numpy as np from itertools import groupby diff --git a/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py b/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py index 04687c475d..89b092c3c5 100644 --- a/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py +++ b/pupil_src/shared_modules/pupil_detector_plugins/detector_2d_plugin.py @@ -16,15 +16,17 @@ import glfw -glfw.ERROR_REPORTING = "raise" - from gl_utils import ( adjust_gl_view, basic_gl_setup, clear_gl_screen, make_coord_system_norm_based, make_coord_system_pixel_based, + GLFWErrorReporting, ) + +GLFWErrorReporting.set_default() + from methods import normalize from plugin import Plugin diff --git a/pupil_src/shared_modules/roi.py b/pupil_src/shared_modules/roi.py index 03b5900726..10696e95cf 100644 --- a/pupil_src/shared_modules/roi.py +++ b/pupil_src/shared_modules/roi.py @@ -19,8 +19,9 @@ from pyglui.cygl.utils import draw_polyline as cygl_draw_polyline import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from methods import denormalize, normalize from observable import Observable diff --git a/pupil_src/shared_modules/service_ui.py b/pupil_src/shared_modules/service_ui.py index 48b862b83c..fc52b42023 100644 --- a/pupil_src/shared_modules/service_ui.py +++ b/pupil_src/shared_modules/service_ui.py @@ -18,10 +18,11 @@ from OpenGL.GL import GL_COLOR_BUFFER_BIT import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from pyglui import ui, cygl from plugin import System_Plugin_Base @@ -53,10 +54,12 @@ def __init__( self.texture = np.zeros((1, 1, 3), dtype=np.uint8) + 128 - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) if g_pool.hide_ui: glfw.window_hint(glfw.VISIBLE, 0) # hide window + main_window = glfw.create_window(*window_size, "Pupil Service", None, None) window_position_manager = gl_utils.WindowPositionManager() diff --git a/pupil_src/shared_modules/surface_tracker/gui.py b/pupil_src/shared_modules/surface_tracker/gui.py index 3094d046e6..1189ab898d 100644 --- a/pupil_src/shared_modules/surface_tracker/gui.py +++ b/pupil_src/shared_modules/surface_tracker/gui.py @@ -21,8 +21,9 @@ import gl_utils import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from .surface_marker import Surface_Marker_Type diff --git a/pupil_src/shared_modules/system_graphs.py b/pupil_src/shared_modules/system_graphs.py index 4a2a4af074..53b015c30c 100644 --- a/pupil_src/shared_modules/system_graphs.py +++ b/pupil_src/shared_modules/system_graphs.py @@ -12,10 +12,11 @@ import os import psutil import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from pyglui import ui, graph from pyglui.cygl.utils import RGBA, mix_smooth from plugin import System_Plugin_Base diff --git a/pupil_src/shared_modules/video_overlay/ui/interactions.py b/pupil_src/shared_modules/video_overlay/ui/interactions.py index 259cf96b22..2b88d2ad78 100644 --- a/pupil_src/shared_modules/video_overlay/ui/interactions.py +++ b/pupil_src/shared_modules/video_overlay/ui/interactions.py @@ -9,10 +9,11 @@ ---------------------------------------------------------------------------~(*) """ import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from methods import normalize, denormalize diff --git a/pupil_src/shared_modules/vis_watermark.py b/pupil_src/shared_modules/vis_watermark.py index e2e0fcbc5e..eb04b8eb1c 100644 --- a/pupil_src/shared_modules/vis_watermark.py +++ b/pupil_src/shared_modules/vis_watermark.py @@ -18,8 +18,9 @@ from pyglui import ui import glfw +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() from methods import normalize, denormalize import logging diff --git a/pupil_src/shared_modules/visualizer.py b/pupil_src/shared_modules/visualizer.py index 612052a881..076ee40e26 100644 --- a/pupil_src/shared_modules/visualizer.py +++ b/pupil_src/shared_modules/visualizer.py @@ -9,10 +9,11 @@ ---------------------------------------------------------------------------~(*) """ import glfw +import gl_utils +from gl_utils import GLFWErrorReporting -glfw.ERROR_REPORTING = "raise" +GLFWErrorReporting.set_default() -import gl_utils from OpenGL.GL import ( GL_BLEND, GL_COLOR_BUFFER_BIT, @@ -203,7 +204,8 @@ def open_window(self): # get glfw started if self.run_independently: - glfw.init() + with GLFWErrorReporting.glfw_init(): + glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) self.window = glfw.create_window( self.window_size[0], self.window_size[1], self.name, None, None