summaryrefslogtreecommitdiff
path: root/src/main.c
blob: d655273d03f4cccca24a59ffaea7f0aca6ded322 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdbool.h>
#include <signal.h>

#include <wayland-client.h>
#include <wayland-egl.h>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"

#include <glad/glad.h>
#include <glad/glad_egl.h>

#include <mpv/client.h>
#include <mpv/render_gl.h>

#include <cflogprinter.h>

typedef unsigned int uint;

struct wl_state {
    struct wl_display *display;
    struct wl_compositor *compositor;
    struct zwlr_layer_shell_v1 *layer_shell;
    struct zxdg_output_manager_v1 *xdg_output_manager;
    struct wl_list outputs;  // struct display_output::link
    char* monitor; // User selected output
    int surface_layer;
    bool run_display;
};

struct display_output {
    uint32_t wl_name;
    struct wl_output *wl_output;
    struct zxdg_output_v1 *xdg_output;
    char *name;
    char *identifier;

    struct wl_state *state;
    struct wl_surface *surface;
    struct zwlr_layer_surface_v1 *layer_surface;

    uint32_t width, height;

    struct wl_list link;
};

static struct wl_egl_window* egl_window;
static EGLDisplay *egl_display;
static EGLContext *egl_context;
static EGLSurface *egl_surface;

static mpv_handle *mpv;
static mpv_render_context *mpv_glcontext;
static char *video_path;
static char *opt_config_path;

static struct {
    char **pauselist;
    char **stoplist;

    char **argv_copy;
    char *save_info;

    bool auto_pause;
    bool auto_stop;

    int is_paused;
    bool frame_ready;
    bool kill_render_loop;

} halt_info = {NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0};

static pthread_t threads[5];

static uint SLIDESHOW_TIME = 0;
static bool VERBOSE = 0;

static void nop() {}

static void exit_cleanup() {
    // Cancel all threads
    for(uint i=0; threads[i] != 0; i++) {
        if (pthread_self() != threads[i])
            pthread_cancel(threads[i]);
    }

    // Give mpv a chance to finish
    halt_info.kill_render_loop = 1;
    for (int trys=10; halt_info.kill_render_loop && trys > 0; trys--) {
        usleep(10000);
    }

    if (mpv_glcontext)
       mpv_render_context_free(mpv_glcontext);
    if (mpv)
        mpv_terminate_destroy(mpv);

    if (egl_surface)
        eglDestroySurface(egl_display, egl_surface);
    if (egl_context)
        eglDestroyContext(egl_display, egl_context);
    if (egl_window)
        wl_egl_window_destroy(egl_window);
}

static void exit_mpvpaper(int reason) {
    if (VERBOSE)
        cflp_info("Exiting mpvpaper");
    exit_cleanup();
    exit(reason);
}

static void *exit_by_pthread() { exit_mpvpaper(1); pthread_exit(NULL);}

static void handle_signal(int signum) {
    (void) signum;
    // Separate thread to avoid crash
    pthread_t thread;
    pthread_create(&thread, NULL, exit_by_pthread, NULL);
}

const static struct wl_callback_listener wl_surface_frame_listener;

static void render(struct display_output *output) {
    mpv_render_param render_params[] = {
        {MPV_RENDER_PARAM_OPENGL_FBO, &(mpv_opengl_fbo){
            .fbo = 0,
            .w = output->width,
            .h = output->height,
        }},
        // Flip rendering (needed due to flipped GL coordinate system).
        {MPV_RENDER_PARAM_FLIP_Y, &(int){1}},
    };
    // Render frame
    mpv_render_context_render(mpv_glcontext, render_params);

    // Callback new frame
    struct wl_callback *callback = wl_surface_frame(output->surface);
    wl_callback_add_listener(callback, &wl_surface_frame_listener, output);

    // Display frame
    if (!eglSwapBuffers(egl_display, egl_surface))
        cflp_error("Failed to swap egl buffers 0x%X", eglGetError());
}

static void frame_handle_done(void *data, struct wl_callback *callback, uint32_t frame_time) {
    (void) frame_time;
    wl_callback_destroy(callback);

    // Reset deadman switch timer
    halt_info.frame_ready = 1;

    // Sleep more while paused
    if (halt_info.is_paused) {
        int start_time = time(NULL);
        while (halt_info.is_paused) {
            if (time(NULL) - start_time >= 1)
                break;
            usleep(1000);
        }
    }

    // Render next frame
    if (!halt_info.kill_render_loop)
        render(data);
    else
        halt_info.kill_render_loop = 0;
}

const static struct wl_callback_listener wl_surface_frame_listener = {
    .done = frame_handle_done,
};

static void stop_mpvpaper() {

    // Save video position to arg -Z
    const char* time_pos = mpv_get_property_string(mpv, "time-pos");
    const char* playlist_pos = mpv_get_property_string(mpv, "playlist-pos");

    char save_info[30];
    sprintf(save_info, "%s %s", time_pos, playlist_pos);

    int argv_alloc_size = strlen("-Z")+1 + strlen(save_info)+1;
    for(uint i=0;  halt_info.argv_copy[i] != NULL; i++) {
        argv_alloc_size += strlen(halt_info.argv_copy[i])+1;
    }
    char **argv = calloc(argv_alloc_size+1, sizeof(char));

    uint i = 0;
    for(i=0; halt_info.argv_copy[i] != NULL; i++) {
        argv[i] = strdup(halt_info.argv_copy[i]);
    }
    argv[i] = "-Z";
    argv[i+1] = save_info;
    argv[i+2] = NULL;

    // Get the "real" cwd
    char exe_dir[1024];
    int cut_point = readlink("/proc/self/exe", exe_dir, sizeof(exe_dir));
    for(uint i=cut_point; i > 1; i--) {
        if (exe_dir[i] == '/') {
            exe_dir[i+1] = '\0';
            break;
        }
    }

    exit_cleanup();
    // Start holder script
    execv(strcat(exe_dir, "mpvpaper-holder"), argv);

    cflp_error("Failed to stop mpvpaper");
    exit(EXIT_FAILURE);
}

// Allow pthread_cancel while sleeping
static void pthread_sleep(uint time) {
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    sleep(time);
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
}
static void pthread_usleep(uint time) {
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    usleep(time);
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
}

static char *check_watch_list(char **list) {

    char pid_name[512] = {0};

    for (uint i=0; list[i] != NULL; i++) {
        strcpy(pid_name, "pidof ");
        strcat(pid_name, list[i]);
        strcat(pid_name, " > /dev/null");

        // Stop if program is open
        if (!system(pid_name)) {
            return list[i];
        }
    }
    return NULL;
}

static void *monitor_pauselist() {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    bool is_paused = 0;

    while (halt_info.pauselist) {
        if (!halt_info.is_paused) {
            char *app;
            while ((app = check_watch_list(halt_info.pauselist))) {
                if (app && !is_paused) {
                    if (VERBOSE)
                        cflp_info("Pausing for %s", app);
                    mpv_command_async(mpv, 0, (const char*[]) {"set", "pause", "yes", NULL});
                    is_paused = 1;
                    halt_info.is_paused += 1;
                }
                pthread_sleep(1);
            }
            if (is_paused) {
                is_paused = 0;
                halt_info.is_paused -= 1;
            }
        }
        pthread_sleep(1);
    }
    pthread_exit(NULL);
}

static void *monitor_stoplist() {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

    while (halt_info.stoplist) {
        char *app = check_watch_list(halt_info.stoplist);
        if (app) {
            if (VERBOSE)
                cflp_info("Stopping for %s", app);
            stop_mpvpaper();
        }
        pthread_sleep(1);
    }
    pthread_exit(NULL);
}

static void *handle_auto_pause() {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

    while (halt_info.auto_pause) {
        if (!halt_info.is_paused) {
            time_t start_time = time(NULL);
            bool is_paused = 0;

            // Set deadman switch timer
            halt_info.frame_ready = 0;
            while(!halt_info.frame_ready) {
                if ((time(NULL) - start_time) >= 2 && !is_paused) {
                    if (VERBOSE)
                        cflp_info("Pausing because mpvpaper is hidden");
                    mpv_command_async(mpv, 0, (const char*[]) {"set", "pause", "yes", NULL});
                    is_paused = 1;
                    halt_info.is_paused += 1;
                }
                pthread_usleep(10000);
            }
            if (is_paused) {
                is_paused = 0;
                halt_info.is_paused -= 1;
            }
        }
        pthread_sleep(1);
    }
    pthread_exit(NULL);
}

static void *handle_auto_stop() {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

    while (halt_info.auto_stop) {
        time_t start_time = time(NULL);

        // Set deadman switch timer
        halt_info.frame_ready = 0;
        while(!halt_info.frame_ready) {
            if ((time(NULL) - start_time) >= 2) {
                if (VERBOSE)
                    cflp_info("Stopping because mpvpaper is hidden");
                stop_mpvpaper();
                break;
            }
            pthread_usleep(10000);
        }
        pthread_sleep(1);
    }
    pthread_exit(NULL);
}

static void *handle_mpv_events() {
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    bool mpv_paused = 0;
    time_t start_time = time(NULL);

    while (!halt_info.kill_render_loop) {
        if (SLIDESHOW_TIME) {
            if ((time(NULL) - start_time) >= SLIDESHOW_TIME) {
                mpv_command_async(mpv, 0, (const char*[]) {"playlist-next", NULL});
                start_time = time(NULL);
            }
        }

        mpv_event* event = mpv_wait_event(mpv, 0);
        if (event->event_id == MPV_EVENT_SHUTDOWN || event->event_id == MPV_EVENT_IDLE)
            exit_mpvpaper(0);
        else if (event->event_id == MPV_EVENT_PAUSE) {
            mpv_paused = 1;
            // User paused
            if (!halt_info.is_paused)
                halt_info.is_paused += 1;
        }
        else if (event->event_id == MPV_EVENT_UNPAUSE) {
            mpv_paused = 0;
            halt_info.is_paused = 0;
        }

        if (!halt_info.is_paused && mpv_paused) {
            mpv_command_async(mpv, 0, (const char*[]) {"set", "pause", "no", NULL});
        }

        pthread_usleep(10000);
    }
    pthread_exit(NULL);
}

static void init_threads() {
    uint id = 0;

    pthread_create(&threads[id], NULL, handle_mpv_events, NULL);
    id++;

    // Thread for monitoring if mpvpaper is hidden
    if (halt_info.auto_pause) {
        pthread_create(&threads[id], NULL, handle_auto_pause, NULL);
        id++;
    }
    else if (halt_info.auto_stop) {
        pthread_create(&threads[id], NULL, handle_auto_stop, NULL);
        id++;
    }

    // Threads for monitoring watch lists
    if (halt_info.pauselist) {
        pthread_create(&threads[id], NULL, monitor_pauselist, NULL);
        id++;
    }
    if (halt_info.stoplist) {
        pthread_create(&threads[id], NULL, monitor_stoplist, NULL);
        id++;
    }
}

static void set_init_mpv_options() {
    // Enable user control through terminal by default
    mpv_set_option_string(mpv, "input-default-bindings", "yes");
    mpv_set_option_string(mpv, "input-terminal", "yes");
    mpv_set_option_string(mpv, "terminal", "yes");

    // Load user configs
    const char *home_dir = getenv("HOME");
    char *config_path = calloc(strlen(home_dir)+1 + 30, sizeof(char));
    strcpy(config_path, home_dir);

    char loaded_configs[50] = "";

    strcpy(config_path+strlen(home_dir), "/.config/mpv/mpv.conf");
    if (mpv_load_config_file(mpv, config_path) == 0)
        strcat(loaded_configs, "mpv.conf ");
    strcpy(config_path+strlen(home_dir), "/.config/mpv/input.conf");
    if (mpv_load_config_file(mpv, config_path) == 0)
        strcat(loaded_configs, "input.conf ");
    strcpy(config_path+strlen(home_dir), "/.config/mpv/fonts.conf");
    if(mpv_load_config_file(mpv, config_path) == 0)
        strcat(loaded_configs, "fonts.conf ");
    free(config_path);

    if (VERBOSE && strcmp(loaded_configs, ""))
        cflp_info("Loaded [ %s] user configs from \"~/.config/mpv/\"", loaded_configs);

    // Convenience options passed for slideshow mode
    if (SLIDESHOW_TIME != 0) {
        mpv_set_option_string(mpv, "loop", "yes");
        mpv_set_option_string(mpv, "loop-playlist", "yes");
    }

    // Set mpv_options passed
    mpv_load_config_file(mpv, opt_config_path);
    remove(opt_config_path);

}

static void *get_proc_address_mpv(void *ctx, const char *name){
    (void) ctx;
    return eglGetProcAddress(name);
}

static void init_mpv(struct display_output *output) {

    mpv = mpv_create();
    if (!mpv) {
        cflp_error("Failed creating mpv context");
        exit_mpvpaper(1);
    }

    set_init_mpv_options();

    if (mpv_initialize(mpv) < 0) {
        cflp_error("mpv init failed");
        exit_mpvpaper(1);
    }

    // Force libmpv vo as nothing else will work
    char *vo_option = mpv_get_property_string(mpv, "options/vo");
    if (strcmp(vo_option, "libmpv") != 0 && strcmp(vo_option, "") != 0) {
        cflp_warning("mpvpaper does not support any other vo than \"libmpv\"");
        mpv_set_option_string(mpv, "vo", "libmpv");
    }

    // Have mpv render onto egl context
    mpv_render_param params[] = {
        {MPV_RENDER_PARAM_WL_DISPLAY, output->state->display},
        {MPV_RENDER_PARAM_API_TYPE, MPV_RENDER_API_TYPE_OPENGL},
        {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &(mpv_opengl_init_params){
            .get_proc_address = get_proc_address_mpv,
        }},
    };
    if (mpv_render_context_create(&mpv_glcontext, mpv, params) < 0)
        cflp_error("Failed to initialize mpv GL context");

    // Restore video position after auto stop event
    char* default_start = NULL;
    if (halt_info.save_info) {
        char time_pos[10];
        char playlist_pos[10];
        sscanf(halt_info.save_info, "%s %s", time_pos, playlist_pos);

        // Save default start pos
        default_start = mpv_get_property_string(mpv, "start");
        // Restore video position
        mpv_command_async(mpv, 0, (const char*[]) {"set", "start", time_pos, NULL});
        // Recover playlist pos, that is if it's not shuffled...
        mpv_command_async(mpv, 0, (const char*[]) {"set", "playlist-start", playlist_pos, NULL});
    }

    mpv_command_async(mpv, 0, (const char*[]) {"loadfile", video_path, NULL});

    mpv_event* event = mpv_wait_event(mpv, 1);
    while (event->event_id != MPV_EVENT_FILE_LOADED){
        event = mpv_wait_event(mpv, 1);
    }
    if (VERBOSE)
        cflp_info("Loaded %s", video_path);

    // Return start pos to default
    if (default_start)
        mpv_command_async(mpv, 0, (const char*[]) {"set", "start", default_start, NULL});
}

static void init_egl(struct display_output *output) {

    egl_window = wl_egl_window_create(output->surface, output->width, output->height);
    if (!egl_display) {
        egl_display = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, output->state->display, NULL);
        eglInitialize(egl_display, NULL, NULL);
    }
    eglBindAPI(EGL_OPENGL_API);
    const EGLint win_attrib[] = {
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
        EGL_RED_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE, 8,
        EGL_NONE
    };

    EGLConfig config;
    EGLint config_len;
    eglChooseConfig(egl_display, win_attrib, &config, 1, &config_len);

    if (!egl_context) {
        // Check for OpenGL compatibility for creating egl context
        static const struct { int major, minor; } gl_versions[] = {
            {4, 6}, {4, 5}, {4, 4}, {4, 3}, {4, 2}, {4, 1}, {4, 0},
            {3, 3}, {3, 2}, {3, 1}, {3, 0},
            {0, 0}
        };
        egl_context = NULL;
        for (uint i = 0; gl_versions[i].major > 0; i++) {
            const EGLint ctx_attrib[] = {
                EGL_CONTEXT_MAJOR_VERSION, gl_versions[i].major,
                EGL_CONTEXT_MINOR_VERSION, gl_versions[i].major,
                EGL_NONE
            };
            egl_context = eglCreateContext(egl_display, config, EGL_NO_CONTEXT, ctx_attrib);
            if (egl_context) {
                if (VERBOSE) {
                    cflp_info("OpenGL %i.%i EGL context loaded", gl_versions[i].major, gl_versions[i].minor);
                }
                break;
            }
        }
        if (!egl_context) {
            cflp_error("Failed to create EGL context");
            exit_mpvpaper(1);
        }
    }

    egl_surface = eglCreatePlatformWindowSurface(egl_display, config, egl_window, NULL);
    eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
    eglSwapInterval(egl_display, 0);

    gladLoadGLLoader((GLADloadproc) eglGetProcAddress);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glViewport(0, 0, output->width, output->height);
}

static void destroy_display_output(struct display_output *output) {
    if (!output) {
        return;
    }
    wl_list_remove(&output->link);
    if (output->layer_surface != NULL) {
        zwlr_layer_surface_v1_destroy(output->layer_surface);
    }
    if (output->surface != NULL) {
        wl_surface_destroy(output->surface);
    }
    if (egl_display && strcmp(output->name,output->state->monitor) == 0) {
        eglDestroySurface(egl_display, egl_surface);
        wl_egl_window_destroy(egl_window);
    }
    zxdg_output_v1_destroy(output->xdg_output);
    wl_output_destroy(output->wl_output);

    free(output->name);
    free(output->identifier);
    free(output);
}

static void layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
        uint32_t serial, uint32_t width, uint32_t height) {
    struct display_output *output = data;
    output->width = width;
    output->height = height;
    zwlr_layer_surface_v1_ack_configure(surface, serial);

    // Setup render loop
    init_egl(output);
    if (!mpv) {
        init_mpv(output);
        init_threads();
    }

    if (egl_display && mpv_glcontext) {
        // Start render loop
        render(output);
    }
}

static void layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) {
    (void) surface;

    struct display_output *output = data;
    if (VERBOSE)
        cflp_info("Destroying output %s (%s)", output->name, output->identifier);
    destroy_display_output(output);
}

static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
    .configure = layer_surface_configure,
    .closed = layer_surface_closed,
};

static void create_layer_surface(struct display_output *output) {
    output->surface = wl_compositor_create_surface(output->state->compositor);

    // Empty input region
    struct wl_region *input_region = wl_compositor_create_region(output->state->compositor);
    wl_surface_set_input_region(output->surface, input_region);
    wl_region_destroy(input_region);

    output->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
        output->state->layer_shell, output->surface, output->wl_output,
        output->state->surface_layer, "mpvpaper");

    zwlr_layer_surface_v1_set_size(output->layer_surface, 0, 0);
    zwlr_layer_surface_v1_set_anchor(output->layer_surface,
        ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
        ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
        ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
        ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
    zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, -1);
    zwlr_layer_surface_v1_add_listener(output->layer_surface, &layer_surface_listener, output);
    wl_surface_commit(output->surface);
}

static void xdg_output_handle_name(void *data,
        struct zxdg_output_v1 *xdg_output, const char *name) {
    (void) xdg_output;

    struct display_output *output = data;
    output->name = strdup(name);
}

static void xdg_output_handle_description(void *data,
        struct zxdg_output_v1 *xdg_output, const char *description) {
    (void) xdg_output;

    struct display_output *output = data;

    // wlroots currently sets the description to `make model serial (name)`
    // If this changes in the future, this will need to be modified.
    char *paren = strrchr(description, '(');
    if (paren) {
        size_t length = paren - description;
        output->identifier = calloc(length, sizeof(char));
        if (!output->identifier) {
            cflp_warning("Failed to allocate output identifier");
            return;
        }
        strncpy(output->identifier, description, length);
        output->identifier[length - 1] = '\0';
    }
}

static void xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output) {
    (void) xdg_output;

    struct display_output *output = data;

    if (strcmp(output->name, output->state->monitor) == 0 && !output->layer_surface) {
        if (VERBOSE)
            cflp_info("Output %s (%s) selected", output->name, output->identifier);
        create_layer_surface(output);
    }
    else {
        if (VERBOSE)
            cflp_warning("Output %s (%s) not selected", output->name, output->identifier);
        destroy_display_output(output);
    }
}

static const struct zxdg_output_v1_listener xdg_output_listener = {
    .logical_position = nop,
    .logical_size = nop,
    .name = xdg_output_handle_name,
    .description = xdg_output_handle_description,
    .done = xdg_output_handle_done,
};

static void handle_global(void *data, struct wl_registry *registry,
        uint32_t name, const char *interface, uint32_t version) {
    (void) version;

    struct wl_state *state = data;
    if (strcmp(interface, wl_compositor_interface.name) == 0) {
        state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4);
    } else if (strcmp(interface, wl_output_interface.name) == 0) {
        struct display_output *output = calloc(1, sizeof(struct display_output));
        output->state = state;
        output->wl_name = name;
        output->wl_output = wl_registry_bind(registry, name, &wl_output_interface, 3);

        wl_list_insert(&state->outputs, &output->link);

        if (state->run_display) {
            output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
                state->xdg_output_manager, output->wl_output);
            zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener, output);
        }
    } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
        state->layer_shell = wl_registry_bind(registry, name,
            &zwlr_layer_shell_v1_interface, 1);
    } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
        state->xdg_output_manager = wl_registry_bind(registry, name,
            &zxdg_output_manager_v1_interface, 2);
    }
}

static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
    (void) registry;

    struct wl_state *state = data;
    struct display_output *output, *tmp;
    wl_list_for_each_safe(output, tmp, &state->outputs, link) {
        if (output->wl_name == name) {
            cflp_info("Destroying output %s (%s)", output->name, output->identifier);
            destroy_display_output(output);
            break;
        }
    }
}

static const struct wl_registry_listener registry_listener = {
    .global = handle_global,
    .global_remove = handle_global_remove,
};

static char **get_watch_list(char *path_name) {

    FILE *file = fopen(path_name, "r");
    if (file) {
        // Get alloc size
        fseek(file, 0L, SEEK_END);
        char **list = calloc(ftell(file) + 1, sizeof(char));
        rewind(file);

        // Read lines
        char app[512];
        for (uint i=0; fscanf(file, "%s", app) == 1; i++) {
            list[i] = strdup(app);
        }

        fclose(file);
        return list;
    }
    return NULL;
}

static void set_watch_lists() {
    const char *home_dir = getenv("HOME");

    char *pause_path = calloc(strlen(home_dir)+1 + strlen("/.config/mpvpaper/pauselist")+1, sizeof(char));
    strcpy(pause_path, home_dir);
    strcat(pause_path, "/.config/mpvpaper/pauselist");
    halt_info.pauselist = get_watch_list(pause_path);
    free(pause_path);

    char *stop_path = calloc(strlen(home_dir)+1 + strlen("/.config/mpvpaper/stoplist")+1, sizeof(char));
    strcpy(stop_path, home_dir);
    strcat(stop_path, "/.config/mpvpaper/stoplist");
    halt_info.stoplist = get_watch_list(stop_path);
    free(stop_path);
}

static void parse_command_line(int argc, char **argv, struct wl_state *state) {

    static struct option long_options[] = {
        {"help", no_argument, NULL, 'h'},
        {"verbose", no_argument, NULL, 'v'},
        {"fork", no_argument, NULL, 'f'},
        {"auto-pause", no_argument, NULL, 'p'},
        {"auto-stop", no_argument, NULL, 's'},
        {"slideshow", required_argument, NULL, 'n'},
        {"layer", required_argument, NULL, 'l'},
        {"mpv-options", required_argument, NULL, 'o'},
        {0, 0, 0, 0}
    };

    const char *usage =
        "Usage: mpvpaper [options] <output> <url|path filename>\n"
        "\n"
        "Example: mpvpaper -vs -o \"no-audio loop\" DP-2 /path/to/video\n"
        "\n"
        "Options:\n"
        "--help         -h              Displays this help message\n"
        "--verbose      -v              Be more verbose\n"
        "--fork         -f              Forks mpvpaper so you can close the terminal\n"
        "--auto-pause   -p              Automagically pause mpv when the wallpaper is hidden\n"
        "                               This saves CPU usage, more or less, seamlessly\n"
        "--auto-stop    -s              Automagically stop mpv when the wallpaper is hidden\n"
        "                               This saves CPU/RAM usage, although more abruptly\n"
        "--slideshow    -n SECS         Slideshow mode plays the next video in a playlist every ? seconds\n"
        "                               And passes mpv options \"loop loop-playlist\" for convenience\n"
        "--layer        -l LAYER        Specifies shell surface layer to run on (background by default)\n"
        "--mpv-options  -o \"OPTIONS\"    Forwards mpv options (Must be within quotes\"\")\n"
        ;

    if(argc > 2) {
        char *layer_name;
        char *mpv_options = NULL;

        char opt;
        while((opt = getopt_long(argc, argv, "hvfpsn:l:o:Z:", long_options, NULL)) != -1) {

            switch (opt) {
            case 'h':
                fprintf(stdout, "%s", usage);
                exit(EXIT_SUCCESS);
            case 'v':
                VERBOSE = 1;
                break;
            case 'f':
                if(fork() > 0) {
                    exit(EXIT_SUCCESS);
                }
                fclose(stdout);
                fclose(stderr);
                fclose(stdin);
                break;
            case 'p':
                halt_info.auto_pause = 1;
                halt_info.auto_stop = 0;
                break;
            case 's':
                halt_info.auto_stop = 1;
                halt_info.auto_pause = 0;
                break;
            case 'n':
                SLIDESHOW_TIME = atoi(optarg);
                if (SLIDESHOW_TIME == 0) 
                    cflp_warning("0 or invalid time set for slideshow\n"
                                 "Please use a positive integer");
                break;
            case 'l':
                layer_name = strdup(optarg);
                if(layer_name == NULL) {
                    state->surface_layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
                } else if(strcasecmp(layer_name, "top") == 0) {
                    state->surface_layer = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
                } else if(strcasecmp(layer_name, "bottom") == 0) {
                    state->surface_layer = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
                } else if(strcasecmp(layer_name, "background") == 0) {
                    state->surface_layer = ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND;
                } else if(strcasecmp(layer_name, "overlay") == 0) {
                    state->surface_layer = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
                } else {
                    cflp_error("%s is not a shell surface layer\n"
                               "Your options are: top, bottom, background and overlay", layer_name);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'o':
                mpv_options = strdup(optarg);
                break;

            case 'Z': // Hidden option to recover video pos after stopping
                halt_info.save_info = strdup(optarg);
                break;
            }
        }
        if(optind + 1 >= argc) {
            fprintf(stderr, "%s", usage);
            exit(EXIT_FAILURE);
        }
        state->monitor = strdup(argv[optind]);
        video_path = strdup(argv[optind+1]);

        // Forward options to a tmp file so mpv can parse options
        if(mpv_options) {
            for (int i = 0; i < (int)strlen(mpv_options); i++) {
                if (mpv_options[i] == ' ')
                    mpv_options[i] = '\n';
            }
            // Create config file name
            opt_config_path = calloc(strlen("/tmp/mpvpaper.config")+1 + strlen(state->monitor)+1, sizeof(char));
            strcpy(opt_config_path, "/tmp/mpvpaper");
            strcat(opt_config_path, state->monitor);
            strcat(opt_config_path, ".config");
            // Put options into config file
            FILE* file = fopen(opt_config_path, "w");
            fputs(mpv_options, file);
            fclose(file);
        }

        if(!system("pidof swaybg > /dev/null")) {
            cflp_warning("swaybg is running. This may block mpvpaper from being seen.");
        }
    }
    else {
        cflp_error("Not enough args passed");
        fprintf(stderr, "%s", usage);
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char **argv) {
    signal(SIGINT, handle_signal);

    struct wl_state state = {0};
    wl_list_init(&state.outputs);

    parse_command_line(argc, argv, &state);
    set_watch_lists();

    // Copy argv
    int argv_alloc_size = 0;
    for(int i=0; argv[i] != NULL; i++) {
        argv_alloc_size += strlen(argv[i])+1;
    }
    halt_info.argv_copy = calloc(argv_alloc_size, sizeof(char));

    int j = 0;
    for(int i=0; i < argc; i++) {
        if (strcmp(argv[i], "-Z") == 0) { // Remove hidden opt
            i++; // Skip optind
        }
        else {
            halt_info.argv_copy[j] = strdup(argv[i]);
            j++;
        }
    }

    state.display = wl_display_connect(NULL);
    if (!state.display) {
        cflp_error("Unable to connect to the compositor. "
                "If your compositor is running, check or set the "
                "WAYLAND_DISPLAY environment variable.");
        return EXIT_FAILURE;
    }

    struct wl_registry *registry = wl_display_get_registry(state.display);
    wl_registry_add_listener(registry, &registry_listener, &state);
    wl_display_roundtrip(state.display);
    if (state.compositor == NULL || state.layer_shell == NULL ||
            state.xdg_output_manager == NULL) {
        cflp_error("Missing a required Wayland interface");
        return EXIT_FAILURE;
    }

    struct display_output *output;
    wl_list_for_each(output, &state.outputs, link) {
        output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
            state.xdg_output_manager, output->wl_output);
        zxdg_output_v1_add_listener(output->xdg_output,
            &xdg_output_listener, output);
    }

    // Check outputs
    wl_display_roundtrip(state.display);
    if (wl_list_empty(&state.outputs)) {
        cflp_error(":/ sorry about this but we can't seem to find any output.");
        return EXIT_FAILURE;
    }

    state.run_display = 1;
    while (wl_display_dispatch(state.display) != -1) {
        // NOP
    }

    struct display_output *tmp_output;
    wl_list_for_each_safe(output, tmp_output, &state.outputs, link) {
        destroy_display_output(output);
    }

    return EXIT_SUCCESS;
}