<commit_before>//
//  UIApplication.swift
//  Cacao
//
//  Created by <NAME> on 6/7/17.
//

import CSDL2
import SDL
import Silica
import Cairo

// MARK: - UIApplicationMain

@_silgen_name("_UIApplicationMain")
public func UIApplicationMain(delegate: UIApplicationDelegate, options: CacaoOptions = CacaoOptions()) {
    
    UIApplication.shared.delegate = delegate
    
    SDL.initialize(subSystems: [.video]).sdlAssert()
    
    defer { SDL.quit() }
    
    var windowOptions: Set<Window.Option> = [.allowRetina, .opengl]
    
    if options.canResizeWindow {
        
        windowOptions.insert(.resizable)
    }
    
    let preferredSize = options.windowSize
    
    let initialWindowSize = preferredSize // can we query for screen resolution?
    
    let window = Window(title: options.windowName, frame: (x: .centered, y: .centered, width: Int(initialWindowSize.width), height:  Int(initialWindowSize.height)), options: windowOptions).sdlAssert()
    
    // create UIScreen
    let screen = UIScreen(window: window, size: initialWindowSize)
    UIScreen.main = screen
    
    let launchOptions = [UIApplicationLaunchOptionsKey: Any]()
    
    guard delegate.application(UIApplication.shared, willFinishLaunchingWithOptions: launchOptions),
        delegate.application(UIApplication.shared, didFinishLaunchingWithOptions: launchOptions)
        else { options.log("Application delegate could not launch app"); return }
    
    // enter main loop
    
    let framesPerSecond = screen.maximumFramesPerSecond
    
    var frame = 0
    
    var done = false
    
    var sdlEvent = SDL_Event()
    
    var lastEvent: UIEvent?
    
    while !done {
        
        frame += 1
        
        let startTime = SDL_GetTicks()
        
        // poll event queue
        
        var pollEventStatus: Int32 = 0
        
        repeat {
            
            pollEventStatus = SDL_PollEvent(&sdlEvent)
            
            let eventType = SDL_EventType(rawValue: sdlEvent.type)
            
            let screenLocation = CGPoint(x: Double(sdlEvent.button.x), y: Double(sdlEvent.button.y))
                        
            switch eventType {
                
            case SDL_QUIT, SDL_APP_TERMINATING:
                
                done = true
                
            case SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP:
                
                guard sdlEvent.button.which != -1
                    else { return }
                
                let timestamp = Double(sdlEvent.button.timestamp) / 1000
                
                let event = UIEvent(timestamp: timestamp)
                
                /// Only the key window can recieve touch input
                guard let window = UIApplication.shared.keyWindow,
                    let view = window.hitTest(screenLocation, with: event)
                    else { continue }
                
                let touchPhase: UITouchPhase
                
                // mouse released
                if eventType == SDL_MOUSEBUTTONUP {
                    
                    touchPhase = .ended
                    
                } else if let previousTouch = lastEvent?.allTouches?.first {
                    
                    touchPhase = previousTouch.location == screenLocation ? .stationary : .moved
                    
                    if view != previousTouch.view {
                        
                        // TODO
                    }
                    
                } else {
                    
                    touchPhase = .began
                }
                
                if let previousTouch = lastEvent?.allTouches?.first {
                    
                    // guard againt multiple touchesEnded events
                    guard (previousTouch.phase == .ended && touchPhase == .ended) == false
                        else { continue }
                }
                
                let touch = UITouch(timestamp: timestamp, location: screenLocation, phase: touchPhase, view: view, window: window)
                
                event.allTouches?.insert(touch)
                
                // inform responder chain
                window.sendEvent(event)
                
                lastEvent = event
                
            case SDL_WINDOWEVENT:
                
                switch sdlEvent.window.event {
                    
                case UInt8(SDL_WINDOWEVENT_SIZE_CHANGED.rawValue):
                    
                    screen.updateSize()
                    
                default: break
                }
                
            default: break
            }
            
        } while pollEventStatus != 0<commit_msg>fixed window redrawing on Linux<commit_after>//
//  UIApplication.swift
//  Cacao
//
//  Created by <NAME> on 6/7/17.
//

import CSDL2
import SDL
import Silica
import Cairo

// MARK: - UIApplicationMain

@_silgen_name("_UIApplicationMain")
public func UIApplicationMain(delegate: UIApplicationDelegate, options: CacaoOptions = CacaoOptions()) {
    
    UIApplication.shared.delegate = delegate
    
    SDL.initialize(subSystems: [.video]).sdlAssert()
    
    defer { SDL.quit() }
    
    var windowOptions: Set<Window.Option> = [.allowRetina, .opengl]
    
    if options.canResizeWindow {
        
        windowOptions.insert(.resizable)
    }
    
    let preferredSize = options.windowSize
    
    let initialWindowSize = preferredSize // can we query for screen resolution?
    
    let window = Window(title: options.windowName, frame: (x: .centered, y: .centered, width: Int(initialWindowSize.width), height:  Int(initialWindowSize.height)), options: windowOptions).sdlAssert()
    
    // create UIScreen
    let screen = UIScreen(window: window, size: initialWindowSize)
    UIScreen.main = screen
    
    let launchOptions = [UIApplicationLaunchOptionsKey: Any]()
    
    guard delegate.application(UIApplication.shared, willFinishLaunchingWithOptions: launchOptions),
        delegate.application(UIApplication.shared, didFinishLaunchingWithOptions: launchOptions)
        else { options.log("Application delegate could not launch app"); return }
    
    // enter main loop
    
    let framesPerSecond = screen.maximumFramesPerSecond
    
    var frame = 0
    
    var done = false
    
    var sdlEvent = SDL_Event()
    
    var lastEvent: UIEvent?
    
    while !done {
        
        frame += 1
        
        let startTime = SDL_GetTicks()
        
        // poll event queue
        
        var pollEventStatus: Int32 = 0
        
        repeat {
            
            pollEventStatus = SDL_PollEvent(&sdlEvent)
            
            let eventType = SDL_EventType(rawValue: sdlEvent.type)
            
            let screenLocation = CGPoint(x: Double(sdlEvent.button.x), y: Double(sdlEvent.button.y))
                        
            switch eventType {
                
            case SDL_QUIT, SDL_APP_TERMINATING:
                
                done = true
                
            case SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP:
                
                guard sdlEvent.button.which != -1
                    else { return }
                
                let timestamp = Double(sdlEvent.button.timestamp) / 1000
                
                let event = UIEvent(timestamp: timestamp)
                
                /// Only the key window can recieve touch input
                guard let window = UIApplication.shared.keyWindow,
                    let view = window.hitTest(screenLocation, with: event)
                    else { continue }
                
                let touchPhase: UITouchPhase
                
                // mouse released
                if eventType == SDL_MOUSEBUTTONUP {
                    
                    touchPhase = .ended
                    
                } else if let previousTouch = lastEvent?.allTouches?.first {
                    
                    touchPhase = previousTouch.location == screenLocation ? .stationary : .moved
                    
                    if view != previousTouch.view {
                        
                        // TODO
                    }
                    
                } else {
                    
                    touchPhase = .began
                }
                
                if let previousTouch = lastEvent?.allTouches?.first {
                    
                    // guard againt multiple touchesEnded events
                    guard (previousTouch.phase == .ended && touchPhase == .ended) == false
                        else { continue }
                }
                
                let touch = UITouch(timestamp: timestamp, location: screenLocation, phase: touchPhase, view: view, window: window)
                
                event.allTouches?.insert(touch)
                
                // inform responder chain
                window.sendEvent(event)
                
                lastEvent = event
                
            case SDL_WINDOWEVENT:
                
                let windowEvent = SDL_WindowEventID(rawValue: SDL_WindowEventID.RawValue(sdlEvent.window.event))
                
                switch windowEvent {
                    
                case SDL_WINDOWEVENT_SIZE_CHANGED:
                    
                    screen.updateSize()
                    
                case SDL_WINDOWEVENT_FOCUS_GAINED, SDL_WINDOWEVENT_FOCUS_LOST:
                    
                    screen.needsDisplay = true
                    
                default: break
                }
                
            default: break
            }
            
        } while pollEventStatus != 0
{'fraction_non_alphanumeric': 0.06834458768160426, 'fraction_numerical': 0.003376304481276857, 'mean_word_length': 1.2606382978723405, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2210090', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2281, 'n_words': 532}