Login
main.cr at [46e5d48b28]
Login

File src/main.cr artifact 3c143d37b8 part of check-in 46e5d48b28


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   274
   275
   276
   277
   278
   279
   280
   281
   282
   283
   284
   285
   286
   287
   288
   289
   290
   291
   292
   293
   294
   295
   296
   297
   298
   299
   300
   301
   302
   303
   304
   305
   306
   307
   308
   309
   310
   311
#### Benben
#### Copyright (C) 2023-2024 Remilia Scarlet <remilia@posteo.jp>
####
#### This program is free software: you can redistribute it and/or
#### modify it under the terms of the GNU Affero General Public
#### License as published by the Free Software Foundation, either
#### version 3 of the License, or (at your option) any later version.
####
#### This program is distributed in the hope that it will be useful,
#### but WITHOUT ANY WARRANTY; without even the implied warranty of
#### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#### Affero General Public License for more details.
####
#### You should have received a copy of the GNU Affero General Public License
#### along with this program.  If not, see <https://www.gnu.org/licenses/>.
require "libremiliacr"
require "remiaudio"
require "yunosynth"
#require "remislang"
require "./common"
require "./vgm-extensions"
require "./command-line"
require "./playbacksettings"
require "./config"
require "./file-handler"

# Load all UIs
require "./printer"
require "./uis/orig/*"

require "./audio-driver"
require "./player"
require "./rendering/renderer"
require "./playback-manager"

####
#### Main Toplevel Program
####

module Benben
  RemiLib.classSetonce! ui : Benben::UI?
  RemiLib.classSetonce! fileHandler : Benben::FileHandler?
  RemiLib.classSetonce! driver : Benben::AudioDriver?
  RemiLib.classSetonce! player : Benben::Player?
  RemiLib.classSetonce! manager : PlaybackManager?
  RemiLib.classSetonce! logFile : File?

  # Used to find the configuration file and data files.
  @@configResolver = RemiConf::Resolver.xdg(SHORT_NAME)
  class_getter configResolver

  # The loaded configuration file.
  @@config : Config? = nil
  class_property! config

  # Holds the original screen attributes.
  @@originalAttrs : LibC::Termios?
  class_property originalAttrs

  # Restore the original screen attributes on exit.  S-Lang sometimes doesn't do
  # this correctly.
  at_exit do
    Benben.originalAttrs.try do |attrs|
      LibC.tcsetattr(STDOUT.fd, LibC::TCSANOW, pointerof(attrs))
    end
  end

  RemiLib.classSetonce! settings : PlaybackSettings?

  class Program
    def initialize
      LibC.tcgetattr(STDOUT.fd, out origAttrs)
      Benben.originalAttrs = origAttrs

      # This is meant for debugging, and is currently only useful if Benben is
      # built with -Dbenben_debug
      if filename = ENV["BENBEN_DEBUG_LOG_FILE"]?
        mode = "w"
        if filename.starts_with?("append:")
          mode = "a"
          filename = filename[7..].strip
        else
          filename = filename.strip
        end

        unless filename.empty?
          begin
            Benben.logFile = File.open(filename, mode)
            RemiLib.log.defaultStream = Benben.logFile
            RemiLib.log.debugStream = Benben.logFile
            Benben.logFile.sync = true
            RemiLib.log.dlog!("=== Benben Debug Log File, opened #{Time.local} ===")
          rescue err : Exception
            RemiLib.log.fatal("Couldn't open the requested log file: #{err}")
          end
        else
          RemiLib.log.fatal("Couldn't open the requested log file: no file specified")
        end
      end

      # Ensure we have a minimum number of worker threads.
      Benben.dlog!("CRYSTAL_WORKERS: #{ENV["CRYSTAL_WORKERS"]?}")
      if crWorkers = ENV["CRYSTAL_WORKERS"]?
        if crWorkers.to_i32 < 4
          RemiLib.log.fatal("Could not request at least four CRYSTAL_WORKERS from the Crystal runtime")
        end
      end

      # Initialize and parse the command line
      initCLI
      Benben.args.parse

      # Load the configuration files
      Benben.config = Config.load
      Benben.songConfigs = SongConfig.load

      # Check for arguments that may exit the program early.  This will also
      # check their values.
      checkEarlyOutArguments

      # We require at least one VGM file
      if Benben.args.positionalArgs.empty?
        RemiLib.log.error("No VGM files specified")
        exit 1
      end

      # If we're rendering, make sure the bit depth is good
      checkRenderingArgs

      Benben.settings = PlaybackSettings.new
      Benben.settings.apply(Benben.config)
      Benben.settings.apply(Benben.args)
    end

    # Initializes the file handler.
    private def initFileHandler : Nil
      if (Benben.args["render"].called? && !Benben.args["quiet"].called?) || !Benben.args["render"].called?
        puts "Checking files..."
      end
      STDOUT.flush
      Benben.fileHandler = FileHandler.new
      if (Benben.args["render"].called? && !Benben.args["quiet"].called?) || !Benben.args["render"].called?
        format("~:d file~:p queued for playback~%", Benben.fileHandler.size)
      end

      # Do we have files to play?
      if Benben.fileHandler.size == 0
        RemiLib.log.error("No files to play")
        exit 1
      end
    end

    # Starts the rendering process.
    private def runRendering
      # Start the renderer
      Benben::Rendering::Renderer.new.render
    end

    # Starts normal (non-rendering) operation.
    private def runNormal
      # Initialize audio driver.  We do this before starting up the printer
      # because some drivers may print extra info that isn't related directly to
      # this program.
      drv = if Benben.args["driver"].called?
              Driver.parse(Benben.args["driver"].str)
            else
              Benben.config.audioDriver
            end

      begin
        # Initialize audio driver (i.e., the backend used for output)
        Benben.driver = AudioDriver.new(drv)

        # Check a few args that should be used with --render (not considered a
        # fatal error).
        if Benben.args["outdir"].called?
          RemiLib.log.warn("--outdir used, but --render was not specified")
        end
        if Benben.args["quiet"].called?
          RemiLib.log.warn("--quiet used, but --render was not specified")
        end

        # Create a PlaybackManager
        Benben.manager = PlaybackManager.new

        # TODO move this earlier once the global PlaybackSettings is also used for rendering.
        @@config = nil # No longer needed since everything is in the global PlaybackSettings.

        # Start the PlaybackManager
        Benben.manager.run
      ensure
        Benben.dlog!("Within main ensure, deinitializing stuff")

        # Clean up driver resources
        Benben.driver?.try &.deinit

        # Flush
        STDOUT.flush
        STDOUT << '\n'
      end
    end

    def run
      # Start the file handler.  This handles its own printing.
      initFileHandler

      if Benben.args["render"].called?
        runRendering
      else
        runNormal
      end
    end
  end
end

###
### Entry point
###

begin
  RemiLib.log = RemiLib::ConcurrentLogger.new
  RemiLib.log.as(RemiLib::ConcurrentLogger).start
  sleep 15.milliseconds # Because fibers don't immediately start *sigh*
  Benben::Program.new.run
rescue err : RemiLib::Args::ArgumentError
  RemiLib.log.fatal("#{err}")
rescue err : YAML::Error
  RemiLib.log.fatal("Config error: #{err}", 2)
rescue err : Yuno::YunoError
  {% if flag?(:benben_debug) %}
    RemiLib.log.fatal(err)
  {% else %}
    RemiLib.log.fatal("Internal error: #{err}", 128)
  {% end %}
rescue err : Benben::BenbenError
  Benben.ui.try do |ui|
    begin
      ui.deinit
    rescue Exception
    end
  end
  RemiSlang.deinit
  RemiLib.log.fatal("#{err}", 1)
ensure
  RemiLib.log.as(RemiLib::ConcurrentLogger).stop
  RemiLib.log.as(RemiLib::ConcurrentLogger).drain
  Benben.logFile?.try do |file|
    file.flush
    file.close
  end
end

###
### KLUDGE: Totally janky main() override since we can't spawn first-class
### threads in Crystal yet.  We don't want to spawn too many worker threads
### because that just wastes system resources.  We can't spawn too few or
### performance degrades (or the program gets glitchy).  So we want to spawn a
### good amount.
###
### Ideally we could just have the option to spawn Threads ourselves instead of
### _only_ Fibers.  Then we wouldn't need to pre-process the command line,
### override an environment variable, override main(), bind atoll...
###
### https://portal.mozz.us/gemini/nanako.mooo.com/gemlog/2023-11-01-a.gmi
###

lib LibC
  fun atoll(str : UInt8*) : Int64
end

fun main(argc : Int32, argv : UInt8**) : Int32
  rendering : Bool = false
  jobsNext : Bool = false
  jobs : Int64 = -1
  minWorkers : Int32 = 5

  # Attempt to detect --render/-n and --jobs.
  argc.times do |idx|
    str = String.new(argv[idx])
    if str == "--render" || str == "-n"
      rendering = true
    elsif str == "--jobs"
      jobsNext = true
    elsif jobsNext
      jobs = LibC.atoll(str) # Will return 0 if it can't be converted
      jobsNext = false
    end
  end

  if rendering
    # We don't really care if jobs is 0 or negative or whatever.  We just want
    # to spawn a positive number of worker threads.  The actual command line
    # checking will handle invalid values later on.
    if jobs > 0
      # +2 so we don't starve ourselves of worker threads.  We always want at
      # least minWorkers, though.
      LibC.setenv("CRYSTAL_WORKERS", Math.max((jobs + 2), minWorkers).to_s, 1)
    else
      # This will need to be raised in the future if Benben gets more heavily
      # parallel.  We should ideally always have a few spare workers for the
      # extra fibers we spawn.
      LibC.setenv("CRYSTAL_WORKERS", Math.max(System.cpu_count + 2, minWorkers).to_s, 1)
    end
  else
    # This will need to be raised in the future if Benben gets more heavily
    # parallel.  We should ideally always have one spare worker.
    LibC.setenv("CRYSTAL_WORKERS", minWorkers.to_s, 1)
  end

  Crystal.main(argc, argv)
end