rotator.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* rotator.js
  2. *
  3. * Copyright (C) 2022 kosmospredanie, shyzus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. import Gio from 'gi://Gio';
  19. import { DisplayConfigState } from './displayConfigState.js'
  20. const connection = Gio.DBus.session;
  21. export const Methods = Object.freeze({
  22. 'verify': 0,
  23. 'temporary': 1,
  24. 'persistent': 2
  25. });
  26. export function call_dbus_method(method, handler, params = null) {
  27. if (handler !== undefined || handler !== null) {
  28. connection.call(
  29. 'org.gnome.Mutter.DisplayConfig',
  30. '/org/gnome/Mutter/DisplayConfig',
  31. 'org.gnome.Mutter.DisplayConfig',
  32. method,
  33. params,
  34. null,
  35. Gio.DBusCallFlags.NONE,
  36. -1,
  37. null, handler);
  38. } else {
  39. connection.call(
  40. 'org.gnome.Mutter.DisplayConfig',
  41. '/org/gnome/Mutter/DisplayConfig',
  42. 'org.gnome.Mutter.DisplayConfig',
  43. method,
  44. params,
  45. null,
  46. Gio.DBusCallFlags.NONE,
  47. -1,
  48. null);
  49. }
  50. }
  51. export function get_state() {
  52. return new Promise((resolve, reject) => {
  53. call_dbus_method('GetCurrentState', (conn, res) => {
  54. try {
  55. let reply = conn.call_finish(res);
  56. let configState = new DisplayConfigState(reply)
  57. resolve(configState);
  58. } catch (err) {
  59. reject(err);
  60. }
  61. });
  62. })
  63. }
  64. export function rotate_to(transform) {
  65. this.get_state().then(state => {
  66. let target_monitor = state.builtin_monitor;
  67. if (target_monitor === undefined) {
  68. target_monitor = state.monitors[0]
  69. }
  70. let logical_monitor = state.get_logical_monitor_for(target_monitor.connector);
  71. logical_monitor.transform = transform;
  72. let variant = state.pack_to_apply(this.Methods['temporary']);
  73. call_dbus_method('ApplyMonitorsConfig', null, variant);
  74. }).catch(err => {
  75. console.error(err);
  76. })
  77. }