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