kitty/kitty/main.py
2016-11-26 17:40:28 +05:30

111 lines
3.9 KiB
Python

#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import argparse
import tempfile
import os
import sys
from gettext import gettext as _
from .config import load_config
from .constants import appname, str_version, config_dir, viewport_size
from .tabs import TabManager
from .shaders import GL_VERSION
from .fast_data_types import glewInit, enable_automatic_opengl_error_checking
import glfw
import glfw_constants
def option_parser():
parser = argparse.ArgumentParser(prog=appname, description=_('The {} terminal emulator').format(appname))
a = parser.add_argument
a('--class', default=appname, dest='cls', help=_('Set the WM_CLASS property'))
a('--config', default=os.path.join(config_dir, 'kitty.conf'), help=_('Specify a path to the config file to use'))
a('--cmd', '-c', default=None, help=_('Run python code in the kitty context'))
a('-d', '--directory', default='.', help=_('Change to the specified directory when launching'))
a('--version', action='version', version='{} {} by Kovid Goyal'.format(appname, '.'.join(str_version)))
a('--profile', action='store_true', default=False, help=_('Show profiling data after exit'))
a('--dump-commands', action='store_true', default=False, help=_('Output commands received from child process to stdout'))
a('args', nargs=argparse.REMAINDER, help=_(
'The remaining arguments are used to launch a program other than the default shell. Any further options are passed'
' directly to the program being invoked.'
))
return parser
def setup_opengl():
glfw.glfwWindowHint(glfw_constants.GLFW_CONTEXT_VERSION_MAJOR, GL_VERSION[0])
glfw.glfwWindowHint(glfw_constants.GLFW_CONTEXT_VERSION_MINOR, GL_VERSION[1])
glfw.glfwWindowHint(glfw_constants.GLFW_OPENGL_PROFILE, glfw_constants.GLFW_OPENGL_CORE_PROFILE)
glfw.glfwWindowHint(glfw_constants.GLFW_OPENGL_FORWARD_COMPAT, True)
glfw.glfwWindowHint(glfw_constants.GLFW_SAMPLES, 0)
def run_app(opts, args):
setup_opengl()
window = glfw.glfwCreateWindow(
viewport_size.width, viewport_size.height, args.cls.encode('utf-8'), None, None)
if not window:
raise SystemExit("glfwCreateWindow failed")
glfw.glfwSetWindowTitle(window, appname.encode('utf-8'))
try:
glfw.glfwMakeContextCurrent(window)
glewInit()
glfw.glfwSwapInterval(1)
tabs = TabManager(window, opts, args)
tabs.start()
try:
while not glfw.glfwWindowShouldClose(window):
tabs.render()
glfw.glfwSwapBuffers(window)
glfw.glfwWaitEvents()
finally:
tabs.destroy()
finally:
glfw.glfwDestroyWindow(window)
def on_glfw_error(code, msg):
if isinstance(msg, bytes):
try:
msg = msg.decode('utf-8')
except Exception:
msg = repr(msg)
print('[glfw error]:', msg, file=sys.stderr)
def main():
args = option_parser().parse_args()
if args.cmd:
exec(args.cmd)
return
opts = load_config(args.config)
glfw.glfwSetErrorCallback(on_glfw_error)
enable_automatic_opengl_error_checking(False)
if not glfw.glfwInit():
raise SystemExit('GLFW initialization failed')
try:
if args.profile:
tf = tempfile.NamedTemporaryFile(prefix='kitty-profiling-stats-')
args.profile = tf.name
import cProfile
import pstats
pr = cProfile.Profile()
pr.enable()
run_app(opts, args)
pr.disable()
pr.create_stats()
s = pstats.Stats(pr)
s.add(args.profile)
tf.close()
s.strip_dirs()
s.sort_stats('time', 'name')
s.print_stats(30)
else:
run_app(opts, args)
finally:
glfw.glfwTerminate()
os.closerange(3, 100)