You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.8 KiB

10 years ago
  1. /* vim: set tabstop=2:softtabstop=2:shiftwidth=2:noexpandtab */
  2. // modules: X
  3. using X;
  4. namespace NeoLayoutViewer{
  5. public ConfigManager configm;
  6. public NeoLayoutViewerApp app;
  7. public NeoWindow neo_win = null;
  8. #if tray
  9. public AppStatusIcon neo_tray = null; //for gnome2.x, kde(?)
  10. #endif
  11. #if indicator
  12. public NeoIndicator neo_indicator = null; //for gnome3.x
  13. #endif
  14. #if _NO_WIN
  15. public KeybindingManager manager = null;
  16. #endif
  17. private void bind_shortcuts() {
  18. manager = new KeybindingManager(neo_win);
  19. var show_shortcut = configm.getConfig().get("show_shortcut").strip();
  20. var move_shortcut = configm.getConfig().get("move_shortcut").strip();
  21. var monitor_shortcut = configm.getConfig().get("monitor_shortcut").strip();
  22. if (move_shortcut.length > 0) {
  23. manager.bind(move_shortcut, ()=>{neo_win.numkeypad_move(0);});
  24. }
  25. if (show_shortcut == monitor_shortcut) {
  26. // combination of show + monitor move
  27. debug("Use combined shortcut for window showing and monitor switching.");
  28. manager.bind(monitor_shortcut, ()=>{neo_win.monitor_move(-1, true);});
  29. } else {
  30. if (monitor_shortcut.length > 0) {
  31. manager.bind(monitor_shortcut, ()=>{neo_win.monitor_move();});
  32. }
  33. if (show_shortcut.length > 0) {
  34. manager.bind(show_shortcut, ()=>{neo_win.toggle();});
  35. }
  36. }
  37. }
  38. public static int main(string[] args) {
  39. // Minimal initialization. Check if primary instance and quit otherwise
  40. app = new NeoLayoutViewerApp ();
  41. try {
  42. app.register(); // returns false if and only if throws Error
  43. } catch (Error e) {
  44. debug(@"Gtk.Application.register() failed.\n");
  45. return -1;
  46. }
  47. if (app.is_remote) {
  48. print(@"Application is already running.\n");
  49. // Run without further initalization. It triggers transfer of arguments
  50. // to command_line()-call of primary instance.
  51. app.run(args);
  52. /* Gives D-Bus some time.
  53. * Only needed if app.run() isn't called.
  54. */
  55. //GLib.Thread.usleep(100000);
  56. return 0;
  57. }
  58. string[] paths = {
  59. GLib.Environment.get_user_config_dir(),
  60. GLib.Environment.get_home_dir(),
  61. GLib.Environment.get_current_dir(),
  62. };
  63. configm = new ConfigManager(paths, "neo_layout_viewer.conf");
  64. // Try to find asset folder (images)
  65. string asset_folder = search_asset_folder( configm.getConfig().get("asset_folder") );
  66. if (asset_folder == null) {
  67. stdout.printf(@"Application start failed because asset folder was not found.\nTry to set path manually in the config file '$(configm.used_config_path)'\n");
  68. stdout.flush();
  69. return 0;
  70. }
  71. // Update asset folder in config
  72. configm.getConfig().set("asset_folder", asset_folder);
  73. debug(@"Asset folder: $(asset_folder)\n");
  74. // Create the window of this application and show it
  75. neo_win = new NeoWindow (configm, app);
  76. #if tray
  77. neo_tray = new AppStatusIcon(neo_win);
  78. #endif
  79. #if indicator
  80. neo_indicator = new NeoIndicator(neo_win);
  81. #endif
  82. #if _NO_WIN
  83. bind_shortcuts();
  84. #endif
  85. app.neo_win = neo_win; // Required for action handlers.
  86. app.add_window(neo_win);
  87. return app.run(args);
  88. }
  89. private static void quit() {
  90. app.quit(); // stops app.run()
  91. }
  92. private static void about_dialog() {
  93. /* This function create the about dialog in
  94. tray menu or indicator menu */
  95. var show_shortcut = configm.getConfig().get("show_shortcut").strip();
  96. var move_shortcut = configm.getConfig().get("move_shortcut").strip();
  97. var monitor_shortcut = configm.getConfig().get("monitor_shortcut").strip();
  98. string tmp = "";
  99. if (show_shortcut == monitor_shortcut) {
  100. tmp = "Monitor wechseln/Ausblenden - %s".printf(show_shortcut);
  101. } else {
  102. tmp = """Ein-/Ausblenden - %s
  103. Monitor wechseln - %s""".printf(show_shortcut,
  104. monitor_shortcut);
  105. }
  106. var about = new Gtk.AboutDialog();
  107. about.set_logo(app.neo_win.getIcon());
  108. about.set_destroy_with_parent (true);
  109. about.set_transient_for (app.neo_win);
  110. about.set_version(@"$(RELEASE_VERSION) (git $(GIT_COMMIT_VERSION))");
  111. about.set_program_name("Neo2.0 Ebenenanzeige");
  112. about.set_comments("""Erleichtert das Nachschlagen von Tastenkombinationen im Neo 2.0-Layout.
  113. Olaf Schulz
  114. olaf_schulz+neo@posteo.de
  115. Tastenkombinationen:
  116. %s
  117. Bewegen - %s
  118. Beenden (sofern Fenster selektiert) - q
  119. Verwendete Konfigurationsdatei:
  120. %s""".printf(
  121. tmp,
  122. move_shortcut,
  123. configm.used_config_path)
  124. );
  125. about.set_copyright("LGPLv3");
  126. center_window(about);
  127. about.run();
  128. about.hide();
  129. about.destroy();
  130. }
  131. private void center_window(Gtk.Window win) {
  132. int screen_width = app.neo_win.get_screen_width();
  133. int screen_height = app.neo_win.get_screen_height();
  134. int x, y, w, h;
  135. win.get_size(out w, out h);
  136. x = (screen_width - w) / 2;
  137. y = (screen_height - h) / 2;
  138. win.move(x, y);
  139. }
  140. /* Check given path and shared files folders for the asset folder.
  141. The folder will be assumed as right one if one required file was found.
  142. @return: assed folder or null.
  143. */
  144. private static string? search_asset_folder(string path) {
  145. string filename = "/icons/Neo-Icon.png"; // Name of file we search for
  146. string[] paths = {
  147. path, // path from config file
  148. "./assets",
  149. "../assets",
  150. SHARED_ASSETS_PATH, // path given by Makefile
  151. };
  152. foreach (var p in paths) {
  153. debug(@"Search assets in $(p)\n");
  154. var file = File.new_for_path (p+filename);
  155. if (file.query_exists(null)) return p;
  156. }
  157. foreach (var s in GLib.Environment.get_system_data_dirs()) {
  158. var env_path = @"$(s)/NeoLayoutViewer/assets";
  159. debug(@"Search assets in $(env_path)\n");
  160. var file2 = File.new_for_path (env_path+filename);
  161. if (file2.query_exists(null)) return env_path;
  162. }
  163. return null;
  164. }
  165. }
  166. #if _NO_WIN
  167. /* Extern C routines */
  168. extern int keysend(uint keysym, int modifiers);
  169. extern int keysend2(uint keysym, uint modsym1, uint modsym2);
  170. extern bool checkCapsLock(X.Display* d);
  171. extern void checkModifier(X.Display* d, int* keycodes, int nkeycodes, int* pressed );
  172. #endif