Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * Copyright © 2012 Intel Corporation |
| 4 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 11 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 25 | #include "config.h" |
| 26 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <string.h> |
| 32 | #include <math.h> |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 33 | #include <errno.h> |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 34 | #include <cairo.h> |
| 35 | |
| 36 | #include <linux/input.h> |
| 37 | #include <wayland-client.h> |
| 38 | #include "window.h" |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 39 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
Pekka Paalanen | ecbdcfd | 2019-04-04 14:46:00 +0300 | [diff] [blame] | 40 | #include <libweston/zalloc.h> |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 41 | |
| 42 | struct fs_output { |
| 43 | struct wl_list link; |
| 44 | struct output *output; |
| 45 | }; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 46 | |
| 47 | struct fullscreen { |
| 48 | struct display *display; |
| 49 | struct window *window; |
| 50 | struct widget *widget; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 51 | struct zwp_fullscreen_shell_v1 *fshell; |
| 52 | enum zwp_fullscreen_shell_v1_present_method present_method; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 53 | int width, height; |
| 54 | int fullscreen; |
| 55 | float pointer_x, pointer_y; |
Jasper St. Pierre | af314bb | 2014-05-12 11:23:44 -0400 | [diff] [blame] | 56 | int draw_cursor; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 57 | |
| 58 | struct wl_list output_list; |
| 59 | struct fs_output *current_output; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static void |
| 63 | fullscreen_handler(struct window *window, void *data) |
| 64 | { |
| 65 | struct fullscreen *fullscreen = data; |
| 66 | |
| 67 | fullscreen->fullscreen ^= 1; |
| 68 | window_set_fullscreen(window, fullscreen->fullscreen); |
| 69 | } |
| 70 | |
| 71 | static void |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 72 | draw_string(cairo_t *cr, |
| 73 | const char *fmt, ...) |
| 74 | { |
| 75 | char buffer[4096]; |
| 76 | char *p, *end; |
| 77 | va_list argp; |
| 78 | cairo_text_extents_t text_extents; |
| 79 | cairo_font_extents_t font_extents; |
| 80 | |
| 81 | cairo_save(cr); |
| 82 | |
Alyssa Ross | 7c12182 | 2021-11-17 17:25:17 +0000 | [diff] [blame] | 83 | cairo_select_font_face(cr, "sans-serif", |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 84 | CAIRO_FONT_SLANT_NORMAL, |
| 85 | CAIRO_FONT_WEIGHT_NORMAL); |
| 86 | cairo_set_font_size(cr, 14); |
| 87 | |
| 88 | cairo_font_extents (cr, &font_extents); |
| 89 | |
| 90 | va_start(argp, fmt); |
| 91 | |
| 92 | vsnprintf(buffer, sizeof(buffer), fmt, argp); |
| 93 | |
| 94 | p = buffer; |
| 95 | while (*p) { |
| 96 | end = strchr(p, '\n'); |
| 97 | if (end) |
| 98 | *end = 0; |
| 99 | |
| 100 | cairo_show_text(cr, p); |
| 101 | cairo_text_extents (cr, p, &text_extents); |
| 102 | cairo_rel_move_to (cr, -text_extents.x_advance, font_extents.height); |
| 103 | |
| 104 | if (end) |
| 105 | p = end + 1; |
| 106 | else |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | va_end(argp); |
| 111 | |
| 112 | cairo_restore(cr); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | redraw_handler(struct widget *widget, void *data) |
| 118 | { |
| 119 | struct fullscreen *fullscreen = data; |
| 120 | struct rectangle allocation; |
| 121 | cairo_surface_t *surface; |
| 122 | cairo_t *cr; |
| 123 | int i; |
| 124 | double x, y, border; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 125 | const char *method_name[] = { "default", "center", "zoom", "zoom_crop", "stretch"}; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 126 | |
| 127 | surface = window_get_surface(fullscreen->window); |
| 128 | if (surface == NULL || |
| 129 | cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { |
| 130 | fprintf(stderr, "failed to create cairo egl surface\n"); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | widget_get_allocation(fullscreen->widget, &allocation); |
| 135 | |
| 136 | cr = widget_cairo_create(widget); |
| 137 | |
| 138 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 139 | cairo_paint (cr); |
| 140 | |
| 141 | cairo_set_source_rgb(cr, 0, 0, 1); |
| 142 | cairo_set_line_width (cr, 10); |
| 143 | cairo_rectangle(cr, 5, 5, allocation.width - 10, allocation.height - 10); |
| 144 | cairo_stroke (cr); |
| 145 | |
| 146 | cairo_move_to(cr, |
| 147 | allocation.x + 15, |
| 148 | allocation.y + 25); |
| 149 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 150 | |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 151 | if (fullscreen->fshell) { |
| 152 | draw_string(cr, |
| 153 | "Surface size: %d, %d\n" |
| 154 | "Scale: %d, transform: %d\n" |
| 155 | "Pointer: %f,%f\n" |
| 156 | "Output: %s, present method: %s\n" |
| 157 | "Keys: (s)cale, (t)ransform, si(z)e, (m)ethod,\n" |
| 158 | " (o)utput, modes(w)itch, (q)uit\n", |
| 159 | fullscreen->width, fullscreen->height, |
| 160 | window_get_buffer_scale (fullscreen->window), |
| 161 | window_get_buffer_transform (fullscreen->window), |
| 162 | fullscreen->pointer_x, fullscreen->pointer_y, |
| 163 | method_name[fullscreen->present_method], |
| 164 | fullscreen->current_output ? output_get_model(fullscreen->current_output->output): "null"); |
| 165 | } else { |
| 166 | draw_string(cr, |
| 167 | "Surface size: %d, %d\n" |
| 168 | "Scale: %d, transform: %d\n" |
| 169 | "Pointer: %f,%f\n" |
| 170 | "Fullscreen: %d\n" |
| 171 | "Keys: (s)cale, (t)ransform, si(z)e, (f)ullscreen, (q)uit\n", |
| 172 | fullscreen->width, fullscreen->height, |
| 173 | window_get_buffer_scale (fullscreen->window), |
| 174 | window_get_buffer_transform (fullscreen->window), |
| 175 | fullscreen->pointer_x, fullscreen->pointer_y, |
| 176 | fullscreen->fullscreen); |
| 177 | } |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 178 | |
| 179 | y = 100; |
| 180 | i = 0; |
| 181 | while (y + 60 < fullscreen->height) { |
| 182 | border = (i++ % 2 == 0) ? 1 : 0.5; |
| 183 | |
| 184 | x = 50; |
| 185 | cairo_set_line_width (cr, border); |
| 186 | while (x + 70 < fullscreen->width) { |
Jasper St. Pierre | af314bb | 2014-05-12 11:23:44 -0400 | [diff] [blame] | 187 | if (window_has_focus(fullscreen->window) && |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 188 | fullscreen->pointer_x >= x && fullscreen->pointer_x < x + 50 && |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 189 | fullscreen->pointer_y >= y && fullscreen->pointer_y < y + 40) { |
| 190 | cairo_set_source_rgb(cr, 1, 0, 0); |
| 191 | cairo_rectangle(cr, |
| 192 | x, y, |
| 193 | 50, 40); |
| 194 | cairo_fill(cr); |
| 195 | } |
| 196 | cairo_set_source_rgb(cr, 0, 1, 0); |
| 197 | cairo_rectangle(cr, |
| 198 | x + border/2.0, y + border/2.0, |
| 199 | 50, 40); |
| 200 | cairo_stroke(cr); |
| 201 | x += 60; |
| 202 | } |
| 203 | |
| 204 | y += 50; |
| 205 | } |
| 206 | |
Jasper St. Pierre | af314bb | 2014-05-12 11:23:44 -0400 | [diff] [blame] | 207 | if (window_has_focus(fullscreen->window) && fullscreen->draw_cursor) { |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 208 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 209 | cairo_set_line_width (cr, 8); |
| 210 | cairo_move_to(cr, |
| 211 | fullscreen->pointer_x - 12, |
| 212 | fullscreen->pointer_y - 12); |
| 213 | cairo_line_to(cr, |
| 214 | fullscreen->pointer_x + 12, |
| 215 | fullscreen->pointer_y + 12); |
| 216 | cairo_stroke(cr); |
| 217 | |
| 218 | cairo_move_to(cr, |
| 219 | fullscreen->pointer_x + 12, |
| 220 | fullscreen->pointer_y - 12); |
| 221 | cairo_line_to(cr, |
| 222 | fullscreen->pointer_x - 12, |
| 223 | fullscreen->pointer_y + 12); |
| 224 | cairo_stroke(cr); |
| 225 | |
| 226 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 227 | cairo_set_line_width (cr, 4); |
| 228 | cairo_move_to(cr, |
| 229 | fullscreen->pointer_x - 10, |
| 230 | fullscreen->pointer_y - 10); |
| 231 | cairo_line_to(cr, |
| 232 | fullscreen->pointer_x + 10, |
| 233 | fullscreen->pointer_y + 10); |
| 234 | cairo_stroke(cr); |
| 235 | |
| 236 | cairo_move_to(cr, |
| 237 | fullscreen->pointer_x + 10, |
| 238 | fullscreen->pointer_y - 10); |
| 239 | cairo_line_to(cr, |
| 240 | fullscreen->pointer_x - 10, |
| 241 | fullscreen->pointer_y + 10); |
| 242 | cairo_stroke(cr); |
| 243 | } |
| 244 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 245 | cairo_destroy(cr); |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | key_handler(struct window *window, struct input *input, uint32_t time, |
| 250 | uint32_t key, uint32_t sym, enum wl_keyboard_key_state state, |
| 251 | void *data) |
| 252 | { |
| 253 | struct fullscreen *fullscreen = data; |
| 254 | int transform, scale; |
| 255 | static int current_size = 0; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 256 | struct fs_output *fsout; |
| 257 | struct wl_output *wl_output; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 258 | int widths[] = { 640, 320, 800, 400 }; |
| 259 | int heights[] = { 480, 240, 600, 300 }; |
| 260 | |
| 261 | if (state == WL_KEYBOARD_KEY_STATE_RELEASED) |
| 262 | return; |
| 263 | |
| 264 | switch (sym) { |
| 265 | case XKB_KEY_t: |
| 266 | transform = window_get_buffer_transform (window); |
| 267 | transform = (transform + 1) % 8; |
| 268 | window_set_buffer_transform(window, transform); |
| 269 | window_schedule_redraw(window); |
| 270 | break; |
| 271 | |
| 272 | case XKB_KEY_s: |
| 273 | scale = window_get_buffer_scale (window); |
| 274 | if (scale == 1) |
| 275 | scale = 2; |
| 276 | else |
| 277 | scale = 1; |
| 278 | window_set_buffer_scale(window, scale); |
| 279 | window_schedule_redraw(window); |
| 280 | break; |
| 281 | |
| 282 | case XKB_KEY_z: |
Marius Vlad | 7e0f203 | 2019-08-25 22:33:41 +0300 | [diff] [blame] | 283 | if (fullscreen->fullscreen) |
| 284 | break; |
| 285 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 286 | current_size = (current_size + 1) % 4; |
| 287 | fullscreen->width = widths[current_size]; |
| 288 | fullscreen->height = heights[current_size]; |
| 289 | window_schedule_resize(fullscreen->window, |
| 290 | fullscreen->width, fullscreen->height); |
| 291 | break; |
| 292 | |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 293 | case XKB_KEY_m: |
| 294 | if (!fullscreen->fshell) |
| 295 | break; |
| 296 | |
| 297 | wl_output = NULL; |
| 298 | if (fullscreen->current_output) |
| 299 | wl_output = output_get_wl_output(fullscreen->current_output->output); |
| 300 | fullscreen->present_method = (fullscreen->present_method + 1) % 5; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 301 | zwp_fullscreen_shell_v1_present_surface(fullscreen->fshell, |
| 302 | window_get_wl_surface(fullscreen->window), |
| 303 | fullscreen->present_method, |
| 304 | wl_output); |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 305 | window_schedule_redraw(window); |
| 306 | break; |
| 307 | |
| 308 | case XKB_KEY_o: |
| 309 | if (!fullscreen->fshell) |
| 310 | break; |
| 311 | |
| 312 | fsout = fullscreen->current_output; |
| 313 | wl_output = fsout ? output_get_wl_output(fsout->output) : NULL; |
| 314 | |
| 315 | /* Clear the current presentation */ |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 316 | zwp_fullscreen_shell_v1_present_surface(fullscreen->fshell, NULL, |
| 317 | 0, wl_output); |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 318 | |
| 319 | if (fullscreen->current_output) { |
| 320 | if (fullscreen->current_output->link.next == &fullscreen->output_list) |
| 321 | fsout = NULL; |
| 322 | else |
| 323 | fsout = wl_container_of(fullscreen->current_output->link.next, |
| 324 | fsout, link); |
| 325 | } else { |
| 326 | fsout = wl_container_of(fullscreen->output_list.next, |
| 327 | fsout, link); |
| 328 | } |
| 329 | |
| 330 | fullscreen->current_output = fsout; |
| 331 | wl_output = fsout ? output_get_wl_output(fsout->output) : NULL; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 332 | zwp_fullscreen_shell_v1_present_surface(fullscreen->fshell, |
| 333 | window_get_wl_surface(fullscreen->window), |
| 334 | fullscreen->present_method, |
| 335 | wl_output); |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 336 | window_schedule_redraw(window); |
| 337 | break; |
| 338 | |
| 339 | case XKB_KEY_w: |
| 340 | if (!fullscreen->fshell || !fullscreen->current_output) |
| 341 | break; |
| 342 | |
| 343 | wl_output = NULL; |
| 344 | if (fullscreen->current_output) |
| 345 | wl_output = output_get_wl_output(fullscreen->current_output->output); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 346 | zwp_fullscreen_shell_mode_feedback_v1_destroy( |
| 347 | zwp_fullscreen_shell_v1_present_surface_for_mode(fullscreen->fshell, |
| 348 | window_get_wl_surface(fullscreen->window), |
| 349 | wl_output, 0)); |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 350 | window_schedule_redraw(window); |
| 351 | break; |
| 352 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 353 | case XKB_KEY_f: |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 354 | if (fullscreen->fshell) |
| 355 | break; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 356 | fullscreen->fullscreen ^= 1; |
| 357 | window_set_fullscreen(window, fullscreen->fullscreen); |
| 358 | break; |
| 359 | |
| 360 | case XKB_KEY_q: |
| 361 | exit (0); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | static int |
| 367 | motion_handler(struct widget *widget, |
| 368 | struct input *input, |
| 369 | uint32_t time, |
| 370 | float x, |
| 371 | float y, void *data) |
| 372 | { |
| 373 | struct fullscreen *fullscreen = data; |
| 374 | |
| 375 | fullscreen->pointer_x = x; |
| 376 | fullscreen->pointer_y = y; |
| 377 | |
| 378 | widget_schedule_redraw(widget); |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 379 | |
| 380 | return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 383 | static int |
| 384 | enter_handler(struct widget *widget, |
| 385 | struct input *input, |
| 386 | float x, float y, void *data) |
| 387 | { |
| 388 | struct fullscreen *fullscreen = data; |
| 389 | |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 390 | fullscreen->pointer_x = x; |
| 391 | fullscreen->pointer_y = y; |
| 392 | |
| 393 | widget_schedule_redraw(widget); |
| 394 | |
| 395 | return fullscreen->draw_cursor ? CURSOR_BLANK : CURSOR_LEFT_PTR; |
| 396 | } |
| 397 | |
| 398 | static void |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 399 | button_handler(struct widget *widget, |
| 400 | struct input *input, uint32_t time, |
| 401 | uint32_t button, enum wl_pointer_button_state state, void *data) |
| 402 | { |
| 403 | struct fullscreen *fullscreen = data; |
| 404 | |
| 405 | switch (button) { |
| 406 | case BTN_LEFT: |
| 407 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 408 | window_move(fullscreen->window, input, |
| 409 | display_get_serial(fullscreen->display)); |
| 410 | break; |
| 411 | case BTN_RIGHT: |
| 412 | if (state == WL_POINTER_BUTTON_STATE_PRESSED) |
| 413 | window_show_frame_menu(fullscreen->window, input, time); |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | static void |
Michael Vetter | 2a18a52 | 2015-05-15 17:17:47 +0200 | [diff] [blame] | 419 | touch_handler(struct widget *widget, struct input *input, |
| 420 | uint32_t serial, uint32_t time, int32_t id, |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 421 | float x, float y, void *data) |
| 422 | { |
| 423 | struct fullscreen *fullscreen = data; |
Jasper St. Pierre | 01eaaac | 2013-11-12 20:19:57 -0500 | [diff] [blame] | 424 | window_move(fullscreen->window, input, display_get_serial(fullscreen->display)); |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 428 | fshell_capability_handler(void *data, struct zwp_fullscreen_shell_v1 *fshell, |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 429 | uint32_t capability) |
| 430 | { |
| 431 | struct fullscreen *fullscreen = data; |
| 432 | |
| 433 | switch (capability) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 434 | case ZWP_FULLSCREEN_SHELL_V1_CAPABILITY_CURSOR_PLANE: |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 435 | fullscreen->draw_cursor = 0; |
| 436 | break; |
| 437 | default: |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 442 | struct zwp_fullscreen_shell_v1_listener fullscreen_shell_listener = { |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 443 | fshell_capability_handler |
| 444 | }; |
| 445 | |
| 446 | static void |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 447 | usage(int error_code) |
| 448 | { |
| 449 | fprintf(stderr, "Usage: fullscreen [OPTIONS]\n\n" |
| 450 | " -w <width>\tSet window width to <width>\n" |
| 451 | " -h <height>\tSet window height to <height>\n" |
| 452 | " --help\tShow this help text\n\n"); |
| 453 | |
| 454 | exit(error_code); |
| 455 | } |
| 456 | |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 457 | static void |
| 458 | output_handler(struct output *output, void *data) |
| 459 | { |
| 460 | struct fullscreen *fullscreen = data; |
| 461 | struct fs_output *fsout; |
| 462 | |
| 463 | /* If we've already seen this one, don't add it to the list */ |
| 464 | wl_list_for_each(fsout, &fullscreen->output_list, link) |
| 465 | if (fsout->output == output) |
| 466 | return; |
| 467 | |
Bryce Harrington | 0d1a622 | 2016-02-11 16:42:49 -0800 | [diff] [blame] | 468 | fsout = zalloc(sizeof *fsout); |
| 469 | if (fsout == NULL) { |
| 470 | fprintf(stderr, "out of memory in output_handler\n"); |
| 471 | return; |
| 472 | } |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 473 | fsout->output = output; |
| 474 | wl_list_insert(&fullscreen->output_list, &fsout->link); |
| 475 | } |
| 476 | |
| 477 | static void |
| 478 | global_handler(struct display *display, uint32_t id, const char *interface, |
| 479 | uint32_t version, void *data) |
| 480 | { |
| 481 | struct fullscreen *fullscreen = data; |
| 482 | |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 483 | if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 484 | fullscreen->fshell = display_bind(display, id, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 485 | &zwp_fullscreen_shell_v1_interface, |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 486 | 1); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 487 | zwp_fullscreen_shell_v1_add_listener(fullscreen->fshell, |
| 488 | &fullscreen_shell_listener, |
| 489 | fullscreen); |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 493 | int main(int argc, char *argv[]) |
| 494 | { |
| 495 | struct fullscreen fullscreen; |
| 496 | struct display *d; |
| 497 | int i; |
| 498 | |
| 499 | fullscreen.width = 640; |
| 500 | fullscreen.height = 480; |
| 501 | fullscreen.fullscreen = 0; |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 502 | fullscreen.present_method = ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 503 | wl_list_init(&fullscreen.output_list); |
| 504 | fullscreen.current_output = NULL; |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 505 | |
| 506 | for (i = 1; i < argc; i++) { |
| 507 | if (strcmp(argv[i], "-w") == 0) { |
| 508 | if (++i >= argc) |
| 509 | usage(EXIT_FAILURE); |
| 510 | |
| 511 | fullscreen.width = atol(argv[i]); |
| 512 | } else if (strcmp(argv[i], "-h") == 0) { |
| 513 | if (++i >= argc) |
| 514 | usage(EXIT_FAILURE); |
| 515 | |
| 516 | fullscreen.height = atol(argv[i]); |
| 517 | } else if (strcmp(argv[i], "--help") == 0) |
| 518 | usage(EXIT_SUCCESS); |
| 519 | else |
| 520 | usage(EXIT_FAILURE); |
| 521 | } |
| 522 | |
| 523 | d = display_create(&argc, argv); |
| 524 | if (d == NULL) { |
Antonio Borneo | 3957863 | 2019-04-26 23:57:31 +0200 | [diff] [blame] | 525 | fprintf(stderr, "failed to create display: %s\n", |
| 526 | strerror(errno)); |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | fullscreen.display = d; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 531 | fullscreen.fshell = NULL; |
| 532 | display_set_user_data(fullscreen.display, &fullscreen); |
| 533 | display_set_global_handler(fullscreen.display, global_handler); |
| 534 | display_set_output_configure_handler(fullscreen.display, output_handler); |
| 535 | |
| 536 | if (fullscreen.fshell) { |
| 537 | fullscreen.window = window_create_custom(d); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 538 | zwp_fullscreen_shell_v1_present_surface(fullscreen.fshell, |
| 539 | window_get_wl_surface(fullscreen.window), |
| 540 | fullscreen.present_method, |
| 541 | NULL); |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 542 | /* If we get the CURSOR_PLANE capability, we'll change this */ |
| 543 | fullscreen.draw_cursor = 1; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 544 | } else { |
| 545 | fullscreen.window = window_create(d); |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 546 | fullscreen.draw_cursor = 0; |
Jason Ekstrand | 2bb72fe | 2014-04-02 19:53:52 -0500 | [diff] [blame] | 547 | } |
| 548 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 549 | fullscreen.widget = |
| 550 | window_add_widget(fullscreen.window, &fullscreen); |
| 551 | |
| 552 | window_set_title(fullscreen.window, "Fullscreen"); |
Marius Vlad | b3544c2 | 2021-09-09 13:52:18 +0300 | [diff] [blame] | 553 | window_set_appid(fullscreen.window, "org.freedesktop.weston.fullscreen"); |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 554 | |
| 555 | widget_set_transparent(fullscreen.widget, 0); |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 556 | |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 557 | widget_set_default_cursor(fullscreen.widget, CURSOR_LEFT_PTR); |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 558 | widget_set_redraw_handler(fullscreen.widget, redraw_handler); |
| 559 | widget_set_button_handler(fullscreen.widget, button_handler); |
| 560 | widget_set_motion_handler(fullscreen.widget, motion_handler); |
Jason Ekstrand | 7a17d42 | 2014-04-02 19:53:53 -0500 | [diff] [blame] | 561 | widget_set_enter_handler(fullscreen.widget, enter_handler); |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 562 | |
Rusty Lynch | 1084da5 | 2013-08-15 09:10:08 -0700 | [diff] [blame] | 563 | widget_set_touch_down_handler(fullscreen.widget, touch_handler); |
| 564 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 565 | window_set_key_handler(fullscreen.window, key_handler); |
| 566 | window_set_fullscreen_handler(fullscreen.window, fullscreen_handler); |
| 567 | |
| 568 | window_set_user_data(fullscreen.window, &fullscreen); |
| 569 | /* Hack to set minimum allocation so we can shrink later */ |
| 570 | window_schedule_resize(fullscreen.window, |
| 571 | 1, 1); |
| 572 | window_schedule_resize(fullscreen.window, |
| 573 | fullscreen.width, fullscreen.height); |
| 574 | |
| 575 | display_run(d); |
| 576 | |
vivek | 31732f7 | 2014-05-15 18:58:16 +0530 | [diff] [blame] | 577 | widget_destroy(fullscreen.widget); |
| 578 | window_destroy(fullscreen.window); |
| 579 | display_destroy(d); |
| 580 | |
Alexander Larsson | 73469ed | 2013-05-28 16:23:34 +0200 | [diff] [blame] | 581 | return 0; |
| 582 | } |