/core/main.d

http://github.com/wilkie/djehuty · D · 138 lines · 91 code · 29 blank · 18 comment · 15 complexity · a3ba45225b4357ada2eee7bcf48719fa MD5 · raw file

  1. /*
  2. * main.d
  3. *
  4. * This file contains the main logic and the main class for handling
  5. * an Application.
  6. *
  7. * Author: Dave Wilkinson
  8. *
  9. */
  10. module core.main;
  11. import core.string;
  12. import core.arguments;
  13. import core.application;
  14. import core.locale;
  15. import core.system;
  16. import core.color;
  17. import io.console;
  18. import synch.semaphore;
  19. import synch.thread;
  20. import gui.apploop;
  21. // Section: Core
  22. // Description: This class is the main class for the framework. It provides base functionality.
  23. class Djehuty {
  24. static:
  25. public:
  26. void application(Application application) {
  27. if (app !is null) {
  28. throw new Exception("Application Already Spawned");
  29. }
  30. app = application;
  31. }
  32. Application application() {
  33. return app;
  34. }
  35. package:
  36. void start() {
  37. // Get default locale
  38. Locale.id = System.Locale.id;
  39. // Allow console output
  40. Console.unlock();
  41. // Can only start the framework once
  42. if (!_hasStarted) {
  43. _hasStarted = true;
  44. }
  45. else {
  46. throw new Exception("Framework Already Started");
  47. }
  48. // Constitute the main thread class
  49. ThreadModuleInit();
  50. // Check to make sure the app provided a suitable class to use
  51. if (app is null) {
  52. throw new Exception("No Application Class");
  53. }
  54. else {
  55. app.onPreApplicationStart();
  56. app.onApplicationStart();
  57. }
  58. }
  59. void end(uint code = 0) {
  60. // Tell all running threads that they should end to allow shutdown to commense
  61. if (_threads !is null) {
  62. _threadRegisterSemaphore.down();
  63. foreach(th; _threads) {
  64. th.pleaseStop();
  65. }
  66. _threadRegisterSemaphore.up();
  67. }
  68. // Reset colors to something sane
  69. Console.forecolor = Color.White;
  70. Console.backcolor = Color.Black;
  71. if (app !is null) {
  72. app.onApplicationEnd();
  73. app.onPostApplicationEnd(code);
  74. }
  75. }
  76. bool _hasStarted = false;
  77. Thread[] _threads;
  78. Semaphore _threadRegisterSemaphore;
  79. Application app;
  80. }
  81. void RegisterThread(ref Thread thread) {
  82. if (Djehuty._threadRegisterSemaphore is null) {
  83. Djehuty._threadRegisterSemaphore = new Semaphore(1);
  84. }
  85. Djehuty._threadRegisterSemaphore.down();
  86. Djehuty._threads ~= thread;
  87. Djehuty._threadRegisterSemaphore.up();
  88. }
  89. void UnregisterThread(ref Thread thread)
  90. {
  91. Djehuty._threadRegisterSemaphore.down();
  92. if (Djehuty._threads !is null) {
  93. foreach(i, th; Djehuty._threads) {
  94. if (th is thread) {
  95. if (Djehuty._threads.length == 1) {
  96. Djehuty._threads = null;
  97. }
  98. else if (i >= Djehuty._threads.length - 1) {
  99. Djehuty._threads = Djehuty._threads[0..i];
  100. }
  101. else {
  102. Djehuty._threads = Djehuty._threads[0..i] ~ Djehuty._threads[i+1..$];
  103. }
  104. break;
  105. }
  106. }
  107. }
  108. Djehuty._threadRegisterSemaphore.up();
  109. }