@@ -472,6 +472,10 @@ bool apply_output_config(struct output_config *oc, struct sway_output *output) {
472472 // this output came online, and some config items (like map_to_output) are
473473 // dependent on an output being present.
474474 input_manager_configure_all_inputs ();
475+
476+ // If we've changed the config of an output restart the backgrounds as well
477+ spawn_swaybg ();
478+
475479 return true;
476480}
477481
@@ -507,13 +511,13 @@ static void default_output_config(struct output_config *oc,
507511
508512struct output_config * find_output_config (struct sway_output * sway_output ) {
509513 // Start with a default config for this output
510- struct output_config * result = new_output_config ("merge" );
514+ char * name = sway_output -> wlr_output -> name ;
515+ struct output_config * result = new_output_config (name );
511516 default_output_config (result , sway_output -> wlr_output );
512517
513518 // Apply all matches in order
514519 char id [128 ];
515520 output_get_identifier (id , sizeof (id ), sway_output );
516- char * name = sway_output -> wlr_output -> name ;
517521 for (int i = 0 ; i < config -> output_configs -> length ; ++ i ) {
518522 struct output_config * oc = config -> output_configs -> items [i ];
519523 if (!fnmatch (oc -> name , name , 0 ) || !fnmatch (oc -> name , id , 0 )) {
@@ -640,21 +644,8 @@ bool spawn_swaybg(void) {
640644 return true;
641645 }
642646
643- size_t length = 2 ;
644- for (int i = 0 ; i < config -> output_configs -> length ; i ++ ) {
645- struct output_config * oc = config -> output_configs -> items [i ];
646- if (!oc -> background ) {
647- continue ;
648- }
649- if (strcmp (oc -> background_option , "solid_color" ) == 0 ) {
650- length += 4 ;
651- } else if (oc -> background_fallback ) {
652- length += 8 ;
653- } else {
654- length += 6 ;
655- }
656- }
657-
647+ // At most the program, 8 arguments per output and the terminating NULL
648+ size_t length = 2 + wl_list_length (& root -> all_outputs ) * 8 ;
658649 char * * cmd = calloc (length , sizeof (char * ));
659650 if (!cmd ) {
660651 sway_log (SWAY_ERROR , "Failed to allocate spawn_swaybg command" );
@@ -663,8 +654,13 @@ bool spawn_swaybg(void) {
663654
664655 size_t i = 0 ;
665656 cmd [i ++ ] = config -> swaybg_command ;
666- for (int j = 0 ; j < config -> output_configs -> length ; j ++ ) {
667- struct output_config * oc = config -> output_configs -> items [j ];
657+
658+ // Iterate all the outputs and write the command
659+ list_t * output_configs = create_list ();
660+ struct sway_output * sway_output , * tmp ;
661+ wl_list_for_each_safe (sway_output , tmp , & root -> all_outputs , link ) {
662+ struct output_config * oc = find_output_config (sway_output );
663+ list_add (output_configs , oc );
668664 if (!oc -> background ) {
669665 continue ;
670666 }
@@ -685,14 +681,20 @@ bool spawn_swaybg(void) {
685681 cmd [i ++ ] = oc -> background_fallback ;
686682 }
687683 }
688- assert (i <= length );
684+ assert (i < length );
689685 }
690686
691687 for (size_t k = 0 ; k < i ; k ++ ) {
692688 sway_log (SWAY_DEBUG , "spawn_swaybg cmd[%zd] = %s" , k , cmd [k ]);
693689 }
694690
695691 bool result = _spawn_swaybg (cmd );
692+
696693 free (cmd );
694+ for (int k = 0 ; k < output_configs -> length ; k ++ ) {
695+ free_output_config (output_configs -> items [k ]);
696+ }
697+ list_free (output_configs );
698+
697699 return result ;
698700}
0 commit comments