aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/wm/main.c
blob: 2607801faecb76a58b388a53c2498fc89ab5b046 (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
#include "wm.h"

Server server = {
   .event = {
       .make_input            = { .notify = make_input },
       .make_monitor          = { .notify = make_monitor },
       .make_xdg_surface      = { .notify = make_xdg_surface },
       .make_layer_surface    = { .notify = make_layer_surface },

       .monitor_change        = { .notify = monitor_change },
       .monitor_test          = { .notify = monitor_test  },
       .monitor_apply         = { .notify = monitor_apply },

       .cursor_move           = { .notify = cursor_move },
       .cursor_move_abs       = { .notify = cursor_move_abs },
       .cursor_button         = { .notify = cursor_button },
       .cursor_axis           = { .notify = cursor_axis },
       .cursor_frame          = { .notify = cursor_frame },

       .request_activate      = { .notify = request_activate },
       .request_cursor        = { .notify = request_cursor },
       .request_set_selection = { .notify = request_set_selection },
   },
};

// -----------------------------------------------------------------------
// helper functions

static inline
void
init(void)
{
    /* compositor initialization */
    server.display  = wl_display_create();
    server.backend  = wlr_backend_autocreate(server.display);
    server.renderer = wlr_backend_get_renderer(server.backend);
    server.present  = wlr_presentation_create(server.display, server.backend);

    wlr_renderer_init_wl_display(server.renderer, server.display);

    wlr_compositor_create(server.display, server.renderer);
    wlr_export_dmabuf_manager_v1_create(server.display);
    wlr_screencopy_manager_v1_create(server.display);
    wlr_data_control_manager_v1_create(server.display);
    wlr_data_device_manager_create(server.display);
    wlr_gamma_control_manager_v1_create(server.display);
    wlr_primary_selection_v1_device_manager_create(server.display);
    wlr_viewporter_create(server.display);

    server.activate = wlr_xdg_activation_v1_create(server.display);
    wl_signal_add(&server.activate->events.request_activate, &server.event.request_activate);

    wlr_data_device_manager_create(server.display);

    server.monitor.layout = wlr_output_layout_create();
    wl_signal_add(&server.monitor.layout->events.change, &server.event.monitor_change);
    wlr_xdg_output_manager_v1_create(server.display, server.monitor.layout);

    wl_list_init(&server.monitor.list);
    wl_signal_add(&server.backend->events.new_output, &server.event.make_monitor);

    server.monitor.manager = wlr_output_manager_v1_create(server.display);
    wl_signal_add(&server.monitor.manager->events.test,  &server.event.monitor_test);
    wl_signal_add(&server.monitor.manager->events.apply, &server.event.monitor_apply);

    /* shell initialization */
    wl_list_init(&server.client.list);
    wl_list_init(&server.client.stack);
    wl_list_init(&server.client.focus);

    server.shell.xdg = wlr_xdg_shell_create(server.display);
    wl_signal_add(&server.shell.xdg->events.new_surface, &server.event.make_xdg_surface);

    server.shell.layer = wlr_layer_shell_v1_create(server.display);
    wl_signal_add(&server.shell.layer->events.new_surface, &server.event.make_layer_surface);

    wlr_server_decoration_manager_set_default_mode(
        wlr_server_decoration_manager_create(server.display),
        WLR_SERVER_DECORATION_MANAGER_MODE_SERVER
    );
    wlr_xdg_decoration_manager_v1_create(server.display);

    /* input initialization */
    server.cursor.dot = wlr_cursor_create();
    wlr_cursor_attach_output_layout(server.cursor.dot, server.monitor.layout);

    server.cursor.manager = wlr_xcursor_manager_create(nil, 24);
    wlr_xcursor_manager_load(server.cursor.manager, 1);

    wl_signal_add(&server.cursor.dot->events.motion, &server.event.cursor_move);
    wl_signal_add(&server.cursor.dot->events.motion_absolute, &server.event.cursor_move_abs);
    wl_signal_add(&server.cursor.dot->events.button, &server.event.cursor_button);
    wl_signal_add(&server.cursor.dot->events.axis, &server.event.cursor_axis);
    wl_signal_add(&server.cursor.dot->events.frame, &server.event.cursor_frame);

    wl_list_init(&server.input.keyboards);
    wl_signal_add(&server.backend->events.new_input, &server.event.make_input);

    server.input.idle = wlr_idle_create(server.display);
    server.input.seat = wlr_seat_create(server.display, "seat0");

    wl_signal_add(&server.input.seat->events.request_set_cursor, &server.event.request_cursor);
    wl_signal_add(&server.input.seat->events.request_set_selection, &server.event.request_set_selection);
}

static inline
void
fini(void)
{
    wl_display_destroy_clients(server.display);

    wlr_backend_destroy(server.backend);
    wlr_xcursor_manager_destroy(server.cursor.manager);
    wlr_output_layout_destroy(server.monitor.layout);
    wlr_seat_destroy(server.input.seat);

    wl_display_destroy(server.display);
}

// -----------------------------------------------------------------------
// main point of entry

int
usage(void)
{
    fprintf(stderr, "usage: %s [-s startup command]\n", argv0);
    return 1;
}


int
main(int argc, char *argv[])
{
    char *socket, *cmd=nil;

    ARGBEGIN{
    case 's':
        cmd = ARGF();
        break;
    default:
        return usage();
    } ARGEND

    if(argc != 0)
        return usage();

    wlr_log_init(WLR_DEBUG, nil);

    init();

    if(!(socket=(char*)wl_display_add_socket_auto(server.display))) {
        wlr_backend_destroy(server.backend);
        return 1;
    }

    if(!(wlr_backend_start(server.backend))) {
        wlr_backend_destroy(server.backend);
        wl_display_destroy(server.display);
        return 1;
    }

    setenv("WAYLAND_DISPLAY", socket, true);
    if(cmd) {
        if(fork()==0)
            execl("/bin/sh", "/bin/sh", "-c", cmd, nil);
    }
    wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s", socket);

    server.monitor.selected = monitor_at(server.cursor.dot->x, server.cursor.dot->y);
    wlr_cursor_warp_closest(server.cursor.dot, nil, server.cursor.dot->x, server.cursor.dot->y);
    wlr_xcursor_manager_set_cursor_image(server.cursor.manager, "left_ptr", server.cursor.dot);

    wl_display_run(server.display); /* event loop */

    fini();
    return 0;
}