-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathoutput-layout.cpp
More file actions
1688 lines (1428 loc) · 49.6 KB
/
Copy pathoutput-layout.cpp
File metadata and controls
1688 lines (1428 loc) · 49.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "wayfire/output.hpp"
#include "wayfire/core.hpp"
#include "wayfire/output-layout.hpp"
#include "wayfire/workspace-manager.hpp"
#include "wayfire/render-manager.hpp"
#include "wayfire/signal-definitions.hpp"
#include "wayfire/util.hpp"
#include "../output/output-impl.hpp"
#include "seat/cursor.hpp"
#include "core-impl.hpp"
#include <xf86drmMode.h>
#include <sstream>
#include <cstring>
#include <unordered_set>
#include <wayfire/debug.hpp>
#include <wayfire/util/log.hpp>
#include <wayfire/nonstd/wlroots-full.hpp>
static void*const WF_NOOP_OUTPUT_MAGIC = (void*)0x1234;
static wl_output_transform get_transform_from_string(std::string transform)
{
if (transform == "normal")
{
return WL_OUTPUT_TRANSFORM_NORMAL;
} else if (transform == "90")
{
return WL_OUTPUT_TRANSFORM_90;
} else if (transform == "180")
{
return WL_OUTPUT_TRANSFORM_180;
} else if (transform == "270")
{
return WL_OUTPUT_TRANSFORM_270;
} else if (transform == "flipped")
{
return WL_OUTPUT_TRANSFORM_FLIPPED;
} else if (transform == "180_flipped")
{
return WL_OUTPUT_TRANSFORM_FLIPPED_180;
} else if (transform == "90_flipped")
{
return WL_OUTPUT_TRANSFORM_FLIPPED_90;
} else if (transform == "270_flipped")
{
return WL_OUTPUT_TRANSFORM_FLIPPED_270;
}
LOGE("Bad output transform in config: ", transform);
return WL_OUTPUT_TRANSFORM_NORMAL;
}
wlr_output_mode *find_matching_mode(wlr_output *output,
const wlr_output_mode& reference)
{
wlr_output_mode *mode;
wlr_output_mode *best = NULL;
wl_list_for_each(mode, &output->modes, link)
{
if ((mode->width == reference.width) && (mode->height == reference.height))
{
if (mode->refresh == reference.refresh)
{
return mode;
}
if (!best || (best->refresh < mode->refresh))
{
best = mode;
}
}
}
return best;
}
// from rootston
static bool parse_modeline(const char *modeline, drmModeModeInfo & mode)
{
char hsync[16];
char vsync[16];
char interlace[16];
interlace[0] = '\0';
float fclock;
std::memset(&mode, 0, sizeof(mode));
mode.type = DRM_MODE_TYPE_USERDEF;
if (sscanf(modeline, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s %15s",
&fclock, &mode.hdisplay, &mode.hsync_start, &mode.hsync_end,
&mode.htotal, &mode.vdisplay, &mode.vsync_start, &mode.vsync_end,
&mode.vtotal, hsync, vsync, interlace) < 11)
{
return false;
}
mode.clock = fclock * 1000;
mode.vrefresh = mode.clock * 1000.0 * 1000.0 / mode.htotal / mode.vtotal;
if (strcasecmp(hsync, "+hsync") == 0)
{
mode.flags |= DRM_MODE_FLAG_PHSYNC;
} else if (strcasecmp(hsync, "-hsync") == 0)
{
mode.flags |= DRM_MODE_FLAG_NHSYNC;
} else
{
return false;
}
if (strcasecmp(vsync, "+vsync") == 0)
{
mode.flags |= DRM_MODE_FLAG_PVSYNC;
} else if (strcasecmp(vsync, "-vsync") == 0)
{
mode.flags |= DRM_MODE_FLAG_NVSYNC;
} else
{
return false;
}
if (strcasecmp(interlace, "interlace") == 0)
{
mode.flags |= DRM_MODE_FLAG_INTERLACE;
}
snprintf(mode.name, sizeof(mode.name), "%dx%d@%d",
mode.hdisplay, mode.vdisplay, mode.vrefresh / 1000);
return true;
}
namespace wf
{
void transfer_views(wf::output_t *from, wf::output_t *to)
{
assert(from);
LOGI("transfer views from ", from->handle->name, " -> ",
to ? to->handle->name : "null");
/* first move each desktop view(e.g windows) to another output */
std::vector<wayfire_view> views;
std::vector<wayfire_view> unmapped_views;
if (to)
{
/* If we aren't moving to another output, then there is no need to
* enumerate views either */
views = from->workspace->get_views_in_layer(wf::WM_LAYERS);
// Also get a list of views which are on that output, but do not have
// a layer. These are usually unmapped Xwayland views, which are not to
// be killed, as they are needed for the "real" views.
for (auto& view : wf::get_core().get_all_views())
{
if ((view->get_output() == from) &&
(from->workspace->get_view_layer(view) == 0) &&
(view->role != VIEW_ROLE_DESKTOP_ENVIRONMENT))
{
unmapped_views.push_back(view);
}
}
std::reverse(views.begin(), views.end());
}
for (auto& view : views)
{
from->workspace->remove_view(view);
}
/* views would be empty if !to, but clang-analyzer detects null deref */
if (to)
{
for (auto& view : unmapped_views)
{
// Most operations for transferring an unmapped view to another
// output don't make any sense, so we handle them separately.
view->set_output(to);
}
for (auto& view : views)
{
wf::get_core().move_view_to_output(view, to, true);
}
}
// Find all leftover views
std::vector<wayfire_view> reffed;
for (auto& view : wf::get_core().get_all_views())
{
if (view->get_output() != from)
{
continue;
}
// Ensure that no view is destroyed before we're finished with it!
// This is necessary in case we have for ex. a popup on a layer-shell
// surface. We don't know in which order they will be closed/destroyed.
reffed.push_back(view);
view->take_ref();
}
// Close the leftover views, typically layer-shell ones
for (auto& view : reffed)
{
view->close();
view->set_output(nullptr);
}
// Drop refs we have taken
for (auto& view : reffed)
{
view->unref();
}
}
bool output_state_t::operator ==(const output_state_t& other) const
{
if (source == OUTPUT_IMAGE_SOURCE_NONE)
{
return other.source == OUTPUT_IMAGE_SOURCE_NONE;
}
if (source == OUTPUT_IMAGE_SOURCE_MIRROR)
{
return other.source == OUTPUT_IMAGE_SOURCE_MIRROR &&
mirror_from == other.mirror_from;
}
bool eq = true;
eq &= source == other.source;
eq &= position == other.position;
eq &= (mode.width == other.mode.width);
eq &= (mode.height == other.mode.height);
eq &= (mode.refresh == other.mode.refresh);
eq &= (transform == other.transform);
eq &= (scale == other.scale);
return eq;
}
inline bool is_shutting_down()
{
return wf::get_core().get_current_state() == compositor_state_t::SHUTDOWN;
}
/** Represents a single output in the output layout */
struct output_layout_output_t
{
wlr_output *handle;
output_state_t current_state;
bool is_externally_managed = false;
std::unique_ptr<wf::output_impl_t> output;
wl_listener_wrapper on_destroy, on_mode;
std::shared_ptr<wf::config::section_t> config_section;
wf::option_wrapper_t<wf::output_config::mode_t> mode_opt;
wf::option_wrapper_t<wf::output_config::position_t> position_opt;
wf::option_wrapper_t<double> scale_opt;
wf::option_wrapper_t<std::string> transform_opt;
wf::option_wrapper_t<bool> use_ext_config{
"workarounds/use_external_output_configuration"};
void initialize_config_options()
{
config_section = wf::get_core().config_backend->get_output_section(handle);
auto name = config_section->get_name();
mode_opt.load_option(name + "/mode");
position_opt.load_option(name + "/position");
scale_opt.load_option(name + "/scale");
transform_opt.load_option(name + "/transform");
}
output_layout_output_t(wlr_output *handle)
{
this->handle = handle;
on_destroy.connect(&handle->events.destroy);
initialize_config_options();
bool is_nested_compositor = wlr_output_is_wl(handle);
#if WLR_HAS_X11_BACKEND
is_nested_compositor |= wlr_output_is_x11(handle);
#endif
if (is_nested_compositor)
{
/* Nested backends can be resized by the user. We need to handle
* these cases */
on_mode.set_callback([=] (void *data)
{
handle_mode_changed();
});
on_mode.connect(&handle->events.mode);
}
}
/**
* Update the current configuration based on the mode set by the
* backend.
*/
void handle_mode_changed()
{
auto& lmanager = wf::get_core().output_layout;
auto config = lmanager->get_current_configuration();
if (config.count(handle) &&
(config[handle].source == OUTPUT_IMAGE_SOURCE_SELF))
{
if (output && (output->get_screen_size() != get_effective_size()))
{
/* mode changed. Apply new configuration. */
current_state.mode.width = handle->width;
current_state.mode.height = handle->height;
current_state.mode.refresh = handle->refresh;
this->output->set_effective_size(get_effective_size());
this->output->render->damage_whole();
emit_configuration_changed(wf::OUTPUT_MODE_CHANGE);
}
}
}
wlr_output_mode select_default_mode()
{
wlr_output_mode *mode;
wl_list_for_each(mode, &handle->modes, link)
{
if (mode->preferred)
{
return *mode;
}
}
/* Couldn't find a preferred mode. Just return the last, which is
* usually also the "largest" */
wl_list_for_each_reverse(mode, &handle->modes, link)
return *mode;
/* Finally, if there isn't any mode (for ex. wayland backend),
* try the wlr_output resolution, falling back to 1200x720
* if width or height is <= 0 */
wlr_output_mode default_mode;
auto width = handle->width > 0 ? handle->width : 1200;
auto height = handle->height > 0 ? handle->height : 720;
auto refresh = handle->refresh > 0 ? handle->refresh : 60000;
default_mode.width = width;
default_mode.height = height;
default_mode.refresh = refresh;
return default_mode;
}
/* Returns true if mode setting for the given output can succeed */
bool is_mode_supported(const wlr_output_mode& query)
{
/* DRM doesn't support setting a custom mode, so any supported mode
* must be found in the mode list */
if (wlr_output_is_drm(handle))
{
wlr_output_mode *mode;
wl_list_for_each(mode, &handle->modes, link)
{
if ((mode->width == query.width) && (mode->height == query.height))
{
return true;
}
}
return false;
}
/* X11 and Wayland backends support setting custom modes */
return true;
}
/**
* Determine whether the state in the config file should be ignored.
*/
bool should_ignore_config_state()
{
if (is_externally_managed && use_ext_config)
{
wf::output_config::mode_t mode = mode_opt;
if (mode.get_type() == output_config::MODE_MIRROR)
{
// Special case: output mirroring
// It is not supported directly supported by wlr-output-management
// Thus, if the config file says to mirror an output, we do use that
// information.
return false;
}
return true;
}
return false;
}
/**
* Load the state the output is configured with.
* This is typically the config file, but in case of daemons like kanshi this
* might be the external configuration.
*/
output_state_t load_configured_state()
{
// Ensure custom modes from the config are enabled
// Also make sure to refresh them even if the output is externally
// managed.
refresh_custom_modes();
if (should_ignore_config_state())
{
// Current state is what was requested by the client.
return this->current_state;
}
output_state_t state;
state.position = position_opt;
wf::output_config::mode_t mode = mode_opt;
wlr_output_mode tmp;
LOGI("loaded mode ",
((wf::option_sptr_t<wf::output_config::mode_t>)mode_opt)->get_value_str());
switch (mode.get_type())
{
case output_config::MODE_AUTO:
state.mode = select_default_mode();
state.source = OUTPUT_IMAGE_SOURCE_SELF;
break;
// fallthrough
case output_config::MODE_RESOLUTION:
tmp.width = mode.get_width();
tmp.height = mode.get_height();
tmp.refresh = mode.get_refresh();
state.mode = (is_mode_supported(tmp) ? tmp : select_default_mode());
state.source = OUTPUT_IMAGE_SOURCE_SELF;
break;
case output_config::MODE_OFF:
state.source = OUTPUT_IMAGE_SOURCE_NONE;
return state;
case output_config::MODE_MIRROR:
state.source = OUTPUT_IMAGE_SOURCE_MIRROR;
tmp.width = mode.get_width();
tmp.height = mode.get_height();
tmp.refresh = mode.get_refresh();
state.mode = (tmp.width > 0 && is_mode_supported(tmp) ? tmp : select_default_mode());
state.mirror_from = mode.get_mirror_from();
break;
}
state.scale = scale_opt;
state.transform = get_transform_from_string(transform_opt);
return state;
}
void ensure_wayfire_output(const wf::dimensions_t& effective_size)
{
if (this->output)
{
this->output->set_effective_size(effective_size);
return;
}
this->output =
std::make_unique<wf::output_impl_t>(handle, effective_size);
auto wo = output.get();
/* Focus the first output, but do not change the focus on subsequently
* added outputs. We also change the focus if the noop output was
* focused */
wlr_output *focused = get_core().get_active_output() ?
get_core().get_active_output()->handle : nullptr;
if (!focused || (focused->data == WF_NOOP_OUTPUT_MAGIC))
{
get_core().focus_output(wo);
}
output_added_signal data;
data.output = wo;
get_core().output_layout->emit(&data);
}
void destroy_wayfire_output()
{
if (!this->output)
{
return;
}
LOGE("disabling output: ", output->handle->name);
auto wo = output.get();
output_pre_remove_signal data;
data.output = wo;
wo->emit(&data);
get_core().output_layout->emit(&data);
wo->cancel_active_plugins();
bool shutdown = is_shutting_down();
if ((get_core().get_active_output() == wo) && !shutdown)
{
get_core().focus_output(
get_core().output_layout->get_next_output(wo));
} else if (shutdown)
{
get_core().focus_output(nullptr);
}
/* It doesn't make sense to transfer to another output if we're
* going to shut down the compositor */
transfer_views(wo, shutdown ? nullptr : get_core().get_active_output());
wf::output_removed_signal data2;
data2.output = wo;
get_core().output_layout->emit(&data2);
this->output = nullptr;
}
std::unordered_set<std::string> added_custom_modes;
void add_custom_mode(std::string modeline)
{
if (added_custom_modes.count(modeline))
{
return;
}
added_custom_modes.insert(modeline);
drmModeModeInfo *mode = new drmModeModeInfo;
if (!parse_modeline(modeline.c_str(), *mode))
{
LOGE("invalid modeline ", modeline, " in config file");
return;
}
LOGD("output ", handle->name, ": adding custom mode ", mode->name);
if (wlr_output_is_drm(handle))
{
wlr_drm_connector_add_mode(handle, mode);
}
}
void refresh_custom_modes()
{
auto section =
wf::get_core().config_backend->get_output_section(handle);
static const std::string custom_mode_prefix = "custom_mode";
for (auto& opt : section->get_registered_options())
{
if (custom_mode_prefix ==
opt->get_name().substr(0, custom_mode_prefix.length()))
{
add_custom_mode(opt->get_value_str());
}
}
}
/** Check whether the given state can be applied */
bool test_state(const output_state_t& state)
{
if (state.source == OUTPUT_IMAGE_SOURCE_NONE)
{
return true;
}
if (state.source == OUTPUT_IMAGE_SOURCE_MIRROR)
{
return true;
}
/* XXX: are there more things to check? */
refresh_custom_modes();
return is_mode_supported(state.mode);
}
/** Change the output mode */
void apply_mode(const wlr_output_mode& mode)
{
if (handle->current_mode)
{
/* Do not modeset if nothing changed */
if ((handle->current_mode->width == mode.width) &&
(handle->current_mode->height == mode.height) &&
(handle->current_mode->refresh == mode.refresh))
{
/* Commit the enabling of the output */
wlr_output_commit(handle);
return;
}
}
refresh_custom_modes();
auto built_in = find_matching_mode(handle, mode);
if (built_in)
{
wlr_output_set_mode(handle, built_in);
} else
{
LOGI("Couldn't find matching mode ",
mode.width, "x", mode.height, "@", mode.refresh / 1000.0,
" for output ", handle->name, ". Trying to use custom mode",
"(might not work)");
wlr_output_set_custom_mode(handle, mode.width, mode.height,
mode.refresh);
}
wlr_output_commit(handle);
}
/* Mirroring implementation */
wl_listener_wrapper on_mirrored_frame;
wl_listener_wrapper on_frame;
wlr_output *locked_cursors_on = NULL;
/** Render the output using texture as source */
void render_output(wlr_texture *texture)
{
auto renderer = get_core().renderer;
wlr_output_attach_render(handle, NULL);
wlr_renderer_begin(renderer, handle->width, handle->height);
wf::texture_t tex{texture};
OpenGL::render_transformed_texture(tex, {-1, -1, 2, 2});
wlr_renderer_end(renderer);
wlr_output_commit(handle);
}
/* Load output contents and render them */
wlr_buffer *source_back_buffer = NULL;
void handle_frame()
{
auto wo = get_core().output_layout->find_output(
current_state.mirror_from);
if (!wo)
{
LOGE("Cannot find mirrored output ", current_state.mirror_from,
" for output ", handle->name);
return;
}
wlr_dmabuf_attributes attributes;
if (source_back_buffer == NULL)
{
LOGE("Got empty buffer on ", wo->handle->name);
return;
}
if (!wlr_buffer_get_dmabuf(source_back_buffer, &attributes))
{
LOGE("Failed reading mirrored output contents from ", wo->handle->name);
return;
}
/* We export the output to mirror from to a dmabuf, then create
* a texture from this and use it to render "our" output */
auto texture = wlr_texture_from_dmabuf(
get_core().renderer, &attributes);
render_output(texture);
wlr_texture_destroy(texture);
wlr_dmabuf_attributes_finish(&attributes);
}
void set_enabled(bool enabled)
{
wlr_output_enable(handle, enabled);
if (!enabled)
{
wlr_output_commit(handle);
}
}
void setup_mirror()
{
/* Check if we can mirror */
auto wo = get_core().output_layout->find_output(
current_state.mirror_from);
bool mirror_active = (wo != nullptr);
if (wo)
{
auto config =
get_core().output_layout->get_current_configuration();
auto& wo_state = config[wo->handle];
if (wo_state.source & OUTPUT_IMAGE_SOURCE_NONE)
{
mirror_active = false;
}
}
if (!mirror_active)
{
/* If we mirror from a DPMS or an OFF output, we should turn
* off this output as well */
set_enabled(false);
LOGI(handle->name, ": Cannot mirror from output ",
current_state.mirror_from, ". Disabling output.");
return;
}
/* Force software cursors on the mirrored from output.
* This ensures that they will be copied when reading pixels
* from the main plane */
wlr_output_lock_software_cursors(wo->handle, true);
locked_cursors_on = wo->handle;
wlr_output_schedule_frame(handle);
on_mirrored_frame.set_callback([=] (void *data)
{
auto ev = (wlr_output_event_commit*)data;
if (ev->buffer)
{
if (source_back_buffer)
{
wlr_buffer_unlock(source_back_buffer);
}
source_back_buffer = ev->buffer;
wlr_buffer_lock(ev->buffer);
}
/* The mirrored output was repainted, schedule repaint
* for us as well */
wlr_output_damage_whole(handle);
wlr_output_schedule_frame(handle);
});
on_mirrored_frame.connect(&wo->handle->events.commit);
on_frame.set_callback([=] (void*) { handle_frame(); });
on_frame.connect(&handle->events.frame);
}
void teardown_mirror()
{
if (locked_cursors_on)
{
wlr_output_lock_software_cursors(locked_cursors_on, false);
locked_cursors_on = NULL;
}
if (source_back_buffer)
{
wlr_buffer_unlock(source_back_buffer);
source_back_buffer = NULL;
}
on_mirrored_frame.disconnect();
on_frame.disconnect();
}
wf::dimensions_t get_effective_size()
{
wf::dimensions_t effective_size;
wlr_output_effective_resolution(handle,
&effective_size.width, &effective_size.height);
return effective_size;
}
/**
* Send the output-configuration-changed signal.
*/
void emit_configuration_changed(uint32_t changed_fields)
{
if ((handle->data != WF_NOOP_OUTPUT_MAGIC) && changed_fields)
{
wf::output_configuration_changed_signal data{current_state};
data.output = output.get();
data.changed_fields = changed_fields;
output->emit(&data);
}
}
/** Apply the given state to the output, ignoring position.
*
* This won't have any effect if the output state can't be applied,
* i.e if test_state(state) == false */
void apply_state(const output_state_t& state)
{
if (!test_state(state))
{
return;
}
uint32_t changed_fields = 0;
if (this->current_state.source != state.source)
{
changed_fields |= wf::OUTPUT_SOURCE_CHANGE;
}
if ((this->current_state.mode.width != state.mode.width) ||
(this->current_state.mode.height != state.mode.height) ||
(this->current_state.mode.refresh != state.mode.refresh))
{
changed_fields |= wf::OUTPUT_MODE_CHANGE;
}
if (this->current_state.scale != state.scale)
{
changed_fields |= wf::OUTPUT_SCALE_CHANGE;
}
if (this->current_state.transform != state.transform)
{
changed_fields |= wf::OUTPUT_TRANSFORM_CHANGE;
}
if (!(this->current_state.position == state.position))
{
changed_fields |= wf::OUTPUT_POSITION_CHANGE;
}
this->current_state = state;
/* Even if output will remain mirrored, we can tear it down and set
* up again, in case the output to mirror from changed */
teardown_mirror();
if (state.source == OUTPUT_IMAGE_SOURCE_NONE)
{
/* output is OFF */
destroy_wayfire_output();
set_enabled(false);
return;
}
set_enabled(!(state.source & OUTPUT_IMAGE_SOURCE_NONE));
apply_mode(state.mode);
if (state.source & OUTPUT_IMAGE_SOURCE_SELF)
{
if (handle->transform != state.transform)
{
wlr_output_set_transform(handle, state.transform);
}
if (handle->scale != state.scale)
{
wlr_output_set_scale(handle, state.scale);
wf::get_core_impl().seat->priv->cursor->load_xcursor_scale(
state.scale);
}
wlr_output_commit(handle);
ensure_wayfire_output(get_effective_size());
output->render->damage_whole();
emit_configuration_changed(changed_fields);
} else /* state.source == OUTPUT_IMAGE_SOURCE_MIRROR */
{
destroy_wayfire_output();
setup_mirror();
}
}
};
class output_layout_t::impl
{
std::map<wlr_output*, std::unique_ptr<output_layout_output_t>> outputs;
wlr_output_layout *output_layout;
wlr_output_manager_v1 *output_manager;
wlr_output_power_manager_v1 *output_pw_manager;
wl_listener_wrapper on_new_output;
wl_listener_wrapper on_output_manager_test;
wl_listener_wrapper on_output_manager_apply;
wl_listener_wrapper on_output_power_mode_set;
wl_listener_wrapper on_backend_destroy;
wl_idle_call idle_update_configuration;
wl_timer timer_remove_noop;
wlr_backend *noop_backend;
/* Wayfire generally assumes that an enabled output is always available.
* However, when switching connectors or something it might happen that
* temporarily no output is available. For those cases, we create a
* virtual output with the noop backend. */
std::unique_ptr<output_layout_output_t> noop_output;
wf::signal::connection_t<wf::reload_config_signal> on_config_reload = [=] (wf::reload_config_signal *ev)
{
reconfigure_from_config();
};
wf::signal::connection_t<core_backend_started_signal> on_backend_started =
[=] (core_backend_started_signal *ev)
{
// We need to ensure that at any given time we have at least one
// output while core is running.
//
// Thus we need to make sure the noop output is available if nothing
// else is at startup.
if (get_outputs().empty())
{
ensure_noop_output();
}
};
void deinit_noop()
{
/* Disconnect timer, since otherwise it will be destroyed
* after the wayland display is. */
this->timer_remove_noop.disconnect();
if (noop_output)
{
noop_output->destroy_wayfire_output();
noop_output.reset();
}
}
public:
impl(wlr_backend *backend)
{
on_new_output.set_callback([=] (void *data)
{
add_output((wlr_output*)data);
});
on_new_output.connect(&backend->events.new_output);
// We destroy the noop output when the renderer is destroyed.
// This is needed because the noop output uses the same wlr_egl
// as the real outputs.
on_backend_destroy.set_callback([=] (auto) { deinit_noop(); });
on_backend_destroy.connect(&wf::get_core().renderer->events.destroy);
output_layout = wlr_output_layout_create();
get_core().connect(&on_config_reload);
noop_backend = wlr_headless_backend_create(get_core().display);
wlr_backend_start(noop_backend);
get_core().connect(&on_backend_started);
output_manager = wlr_output_manager_v1_create(get_core().display);
on_output_manager_test.set_callback([=] (void *data)
{
apply_wlr_configuration((wlr_output_configuration_v1*)data, true);
});
on_output_manager_apply.set_callback([=] (void *data)
{
apply_wlr_configuration((wlr_output_configuration_v1*)data, false);
});
on_output_manager_test.connect(&output_manager->events.test);
on_output_manager_apply.connect(&output_manager->events.apply);
output_pw_manager = wlr_output_power_manager_v1_create(get_core().display);
on_output_power_mode_set.set_callback([=] (void *data)
{
set_power_mode((wlr_output_power_v1_set_mode_event*)data);
});
on_output_power_mode_set.connect(&output_pw_manager->events.set_mode);
}
~impl() = default;
impl(const impl &) = delete;
impl(impl &&) = delete;
impl& operator =(const impl&) = delete;
impl& operator =(impl&&) = delete;
output_configuration_t output_configuration_from_wlr_configuration(
wlr_output_configuration_v1 *configuration)
{
output_configuration_t result;
wlr_output_configuration_head_v1 *head;
wl_list_for_each(head, &configuration->heads, link)
{
if (!this->outputs.count(head->state.output))
{
LOGE("Output configuration request contains unknown",
" output, probably a compositor bug!");
continue;
}