busUtils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* busUtils.js
  2. *
  3. * Copyright (C) 2022 kosmospredanie, efosmark
  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 GLib from 'gi://GLib';
  19. import Gio from 'gi://Gio';
  20. export const Methods = Object.freeze({
  21. 'verify': 0,
  22. 'temporary': 1,
  23. 'persistent': 2
  24. });
  25. export const Monitor = class Monitor {
  26. constructor(variant) {
  27. let unpacked = variant.unpack();
  28. this.connector = unpacked[0].unpack()[0].unpack();
  29. let modes = unpacked[1].unpack();
  30. for (let i = 0; i < modes.length; i++) {
  31. let mode = modes[i].unpack();
  32. let id = mode[0].unpack();
  33. let mode_props = mode[6].unpack();
  34. if ('is-current' in mode_props) {
  35. let is_current = mode_props['is-current'].unpack().get_boolean();
  36. if (is_current) {
  37. this.current_mode_id = id;
  38. break;
  39. }
  40. }
  41. }
  42. let props = unpacked[2].unpack();
  43. if ('is-underscanning' in props) {
  44. this.is_underscanning = props['is-underscanning'].unpack().get_boolean();
  45. } else {
  46. this.is_underscanning = false;
  47. }
  48. if ('is-builtin' in props) {
  49. this.is_builtin = props['is-builtin'].unpack().get_boolean();
  50. } else {
  51. this.is_builtin = false;
  52. }
  53. }
  54. }
  55. export const LogicalMonitor = class LogicalMonitor {
  56. constructor(variant) {
  57. let unpacked = variant.unpack();
  58. this.x = unpacked[0].unpack();
  59. this.y = unpacked[1].unpack();
  60. this.scale = unpacked[2].unpack();
  61. this.transform = unpacked[3].unpack();
  62. this.primary = unpacked[4].unpack();
  63. // [ [connector, vendor, product, serial]* ]
  64. this.monitors = unpacked[5].deep_unpack();
  65. this.properties = unpacked[6].unpack();
  66. for (let key in this.properties) {
  67. this.properties[key] = this.properties[key].unpack().unpack();
  68. }
  69. }
  70. }
  71. export const DisplayConfigState = class DisplayConfigState {
  72. constructor(result) {
  73. let unpacked = result.unpack();
  74. this.serial = unpacked[0].unpack();
  75. this.monitors = [];
  76. let monitors = unpacked[1].unpack();
  77. monitors.forEach(monitor_packed => {
  78. let monitor = new Monitor(monitor_packed);
  79. this.monitors.push(monitor);
  80. });
  81. this.logical_monitors = [];
  82. let logical_monitors = unpacked[2].unpack();
  83. logical_monitors.forEach(lmonitor_packed => {
  84. let lmonitor = new LogicalMonitor(lmonitor_packed);
  85. this.logical_monitors.push(lmonitor);
  86. });
  87. this.properties = unpacked[3].unpack();
  88. for (let key in this.properties) {
  89. this.properties[key] = this.properties[key].unpack().unpack();
  90. }
  91. }
  92. get builtin_monitor() {
  93. for (let i = 0; i < this.monitors.length; i++) {
  94. let monitor = this.monitors[i];
  95. if (monitor.is_builtin) {
  96. return monitor;
  97. }
  98. }
  99. return null;
  100. }
  101. get_monitor(connector) {
  102. for (let i = 0; i < this.monitors.length; i++) {
  103. let monitor = this.monitors[i];
  104. if (monitor.connector === connector) {
  105. return monitor;
  106. }
  107. }
  108. return null;
  109. }
  110. get_logical_monitor_for(connector) {
  111. for (let i = 0; i < this.logical_monitors.length; i++) {
  112. let lmonitor = this.logical_monitors[i];
  113. for (let j = 0; j < lmonitor.monitors.length; j++) {
  114. let lm_connector = lmonitor.monitors[j][0];
  115. if (connector === lm_connector) {
  116. return lmonitor;
  117. }
  118. }
  119. }
  120. return null;
  121. }
  122. pack_to_apply(method) {
  123. let packing = [this.serial, method, [], {}];
  124. let logical_monitors = packing[2];
  125. let properties = packing[3];
  126. this.logical_monitors.forEach(lmonitor => {
  127. let lmonitor_pack = [
  128. lmonitor.x,
  129. lmonitor.y,
  130. lmonitor.scale,
  131. lmonitor.transform,
  132. lmonitor.primary,
  133. []
  134. ];
  135. let monitors = lmonitor_pack[5];
  136. for (let i = 0; i < lmonitor.monitors.length; i++) {
  137. let connector = lmonitor.monitors[i][0];
  138. let monitor = this.get_monitor(connector);
  139. monitors.push([
  140. connector,
  141. monitor.current_mode_id,
  142. {
  143. 'enable_underscanning': new GLib.Variant('b', monitor.is_underscanning)
  144. }
  145. ]);
  146. }
  147. logical_monitors.push(lmonitor_pack);
  148. });
  149. if ('layout-mode' in this.properties) {
  150. properties['layout-mode'] = new GLib.Variant('b', this.properties['layout-mode']);
  151. }
  152. return new GLib.Variant('(uua(iiduba(ssa{sv}))a{sv})', packing);
  153. }
  154. }