src/video/mir/SDL_mirwindow.c
changeset 10089 25fda20d0173
parent 9998 f67cf37e9cd4
child 10095 0897c3accd52
equal deleted inserted replaced
10088:33bee97dbcbb 10089:25fda20d0173
    44     }
    44     }
    45 
    45 
    46     return 0;
    46     return 0;
    47 }
    47 }
    48 
    48 
    49 MirPixelFormat
       
    50 FindValidPixelFormat(MIR_Data* mir_data)
       
    51 {
       
    52     unsigned int pf_size = 32;
       
    53     unsigned int valid_formats;
       
    54     unsigned int f;
       
    55 
       
    56     MirPixelFormat formats[pf_size];
       
    57     MIR_mir_connection_get_available_surface_formats(mir_data->connection, formats,
       
    58                                                  pf_size, &valid_formats);
       
    59 
       
    60     for (f = 0; f < valid_formats; f++) {
       
    61         MirPixelFormat cur_pf = formats[f];
       
    62 
       
    63         if (cur_pf == mir_pixel_format_abgr_8888 ||
       
    64             cur_pf == mir_pixel_format_xbgr_8888 ||
       
    65             cur_pf == mir_pixel_format_argb_8888 ||
       
    66             cur_pf == mir_pixel_format_xrgb_8888) {
       
    67 
       
    68             return cur_pf;
       
    69         }
       
    70     }
       
    71 
       
    72     return mir_pixel_format_invalid;
       
    73 }
       
    74 
       
    75 int
    49 int
    76 MIR_CreateWindow(_THIS, SDL_Window* window)
    50 MIR_CreateWindow(_THIS, SDL_Window* window)
    77 {
    51 {
    78     MIR_Window* mir_window;
    52     MIR_Window* mir_window;
    79     MIR_Data* mir_data;
    53     MIR_Data* mir_data;
    80 
    54     MirPixelFormat pixel_format;
    81     MirSurfaceParameters surfaceparm =
    55     MirBufferUsage buffer_usage;
    82     {
    56 
    83         .name = "MirSurface",
    57     MirSurfaceSpec* spec;
    84         .width = window->w,
       
    85         .height = window->h,
       
    86         .pixel_format = mir_pixel_format_invalid,
       
    87         .buffer_usage = mir_buffer_usage_hardware,
       
    88         .output_id = mir_display_output_id_invalid
       
    89     };
       
    90 
       
    91     MirEventDelegate delegate = {
       
    92         MIR_HandleInput,
       
    93         window
       
    94     };
       
    95 
    58 
    96     mir_window = SDL_calloc(1, sizeof(MIR_Window));
    59     mir_window = SDL_calloc(1, sizeof(MIR_Window));
    97     if (!mir_window)
    60     if (!mir_window)
    98         return SDL_OutOfMemory();
    61         return SDL_OutOfMemory();
    99 
    62 
   100     mir_data = _this->driverdata;
    63     mir_data = _this->driverdata;
   101     window->driverdata = mir_window;
    64     window->driverdata = mir_window;
   102 
    65 
   103     if (mir_data->software)
       
   104         surfaceparm.buffer_usage = mir_buffer_usage_software;
       
   105 
       
   106     if (window->x == SDL_WINDOWPOS_UNDEFINED)
    66     if (window->x == SDL_WINDOWPOS_UNDEFINED)
   107         window->x = 0;
    67         window->x = 0;
   108 
    68 
   109     if (window->y == SDL_WINDOWPOS_UNDEFINED)
    69     if (window->y == SDL_WINDOWPOS_UNDEFINED)
   110         window->y = 0;
    70         window->y = 0;
   111 
    71 
   112     mir_window->mir_data = mir_data;
    72     mir_window->mir_data = mir_data;
   113     mir_window->sdl_window = window;
    73     mir_window->sdl_window = window;
   114 
    74 
   115     surfaceparm.pixel_format = FindValidPixelFormat(mir_data);
    75     pixel_format = MIR_mir_connection_get_egl_pixel_format(mir_data->connection,
   116     if (surfaceparm.pixel_format == mir_pixel_format_invalid) {
    76                                                            _this->egl_data->egl_display,
       
    77                                                            _this->egl_data->egl_config);
       
    78 
       
    79     mir_data->pixel_format = pixel_format;
       
    80     if (pixel_format == mir_pixel_format_invalid) {
   117         return SDL_SetError("Failed to find a valid pixel format.");
    81         return SDL_SetError("Failed to find a valid pixel format.");
   118     }
    82     }
   119 
    83 
   120     mir_window->surface = MIR_mir_connection_create_surface_sync(mir_data->connection, &surfaceparm);
    84     buffer_usage = mir_buffer_usage_hardware;
       
    85     if (mir_data->software)
       
    86         buffer_usage = mir_buffer_usage_software;
       
    87 
       
    88     spec = MIR_mir_connection_create_spec_for_normal_surface(mir_data->connection,
       
    89                                                              window->w,
       
    90                                                              window->h,
       
    91                                                              pixel_format);
       
    92 
       
    93     MIR_mir_surface_spec_set_buffer_usage(spec, buffer_usage);
       
    94     MIR_mir_surface_spec_set_name(spec, "Mir surface");
       
    95 
       
    96     mir_window->surface = MIR_mir_surface_create_sync(spec);
       
    97     MIR_mir_surface_set_event_handler(mir_window->surface, MIR_HandleEvent, window);
       
    98 
       
    99     MIR_mir_surface_spec_release(spec);
       
   100 
   121     if (!MIR_mir_surface_is_valid(mir_window->surface)) {
   101     if (!MIR_mir_surface_is_valid(mir_window->surface)) {
   122         const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
   102         const char* error = MIR_mir_surface_get_error_message(mir_window->surface);
   123         return SDL_SetError("Failed to created a mir surface: %s", error);
   103         return SDL_SetError("Failed to created a mir surface: %s", error);
   124     }
   104     }
   125 
   105 
   126     if (window->flags & SDL_WINDOW_OPENGL) {
   106     if (window->flags & SDL_WINDOW_OPENGL) {
   127         EGLNativeWindowType egl_native_window =
   107         EGLNativeWindowType egl_native_window =
   128                         (EGLNativeWindowType)MIR_mir_surface_get_egl_native_window(mir_window->surface);
   108                         (EGLNativeWindowType)MIR_mir_buffer_stream_get_egl_native_window(
       
   109                                                        MIR_mir_surface_get_buffer_stream(mir_window->surface));
   129 
   110 
   130         mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
   111         mir_window->egl_surface = SDL_EGL_CreateSurface(_this, egl_native_window);
   131 
   112 
   132         if (mir_window->egl_surface == EGL_NO_SURFACE) {
   113         if (mir_window->egl_surface == EGL_NO_SURFACE) {
   133             return SDL_SetError("Failed to created a window surface %p",
   114             return SDL_SetError("Failed to created a window surface %p",
   136     }
   117     }
   137     else {
   118     else {
   138         mir_window->egl_surface = EGL_NO_SURFACE;
   119         mir_window->egl_surface = EGL_NO_SURFACE;
   139     }
   120     }
   140 
   121 
   141     MIR_mir_surface_set_event_handler(mir_window->surface, &delegate);
   122     mir_data->current_window = mir_window;
   142 
   123 
   143     return 0;
   124     return 0;
   144 }
   125 }
   145 
   126 
   146 void
   127 void
   147 MIR_DestroyWindow(_THIS, SDL_Window* window)
   128 MIR_DestroyWindow(_THIS, SDL_Window* window)
   148 {
   129 {
   149     MIR_Data* mir_data = _this->driverdata;
   130     MIR_Data* mir_data     = _this->driverdata;
   150     MIR_Window* mir_window = window->driverdata;
   131     MIR_Window* mir_window = window->driverdata;
   151 
   132 
   152     if (mir_data) {
   133     if (mir_data) {
   153         SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
   134         SDL_EGL_DestroySurface(_this, mir_window->egl_surface);
   154         MIR_mir_surface_release_sync(mir_window->surface);
   135         MIR_mir_surface_release_sync(mir_window->surface);
   155 
   136 
       
   137         mir_data->current_window = NULL;
       
   138 
   156         SDL_free(mir_window);
   139         SDL_free(mir_window);
   157     }
   140     }
   158     window->driverdata = NULL;
   141     window->driverdata = NULL;
   159 }
   142 }
   160 
   143 
   178 void
   161 void
   179 MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
   162 MIR_SetWindowFullscreen(_THIS, SDL_Window* window,
   180                         SDL_VideoDisplay* display,
   163                         SDL_VideoDisplay* display,
   181                         SDL_bool fullscreen)
   164                         SDL_bool fullscreen)
   182 {
   165 {
   183     MIR_Window* mir_window = window->driverdata;
   166     MIR_Data*   mir_data   = _this->driverdata;
       
   167     MIR_Window* mir_window = window->driverdata;
       
   168     MirSurfaceSpec* spec;
       
   169     MirSurfaceState state;
   184 
   170 
   185     if (IsSurfaceValid(mir_window) < 0)
   171     if (IsSurfaceValid(mir_window) < 0)
   186         return;
   172         return;
   187 
   173 
   188     if (fullscreen) {
   174     if (fullscreen) {
   189         MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_fullscreen);
   175         state = mir_surface_state_fullscreen;
   190     } else {
   176     } else {
   191         MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_restored);
   177         state = mir_surface_state_restored;
   192     }
   178     }
       
   179 
       
   180     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   181     MIR_mir_surface_spec_set_state(spec, state);
       
   182 
       
   183     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   184     MIR_mir_surface_spec_release(spec);
   193 }
   185 }
   194 
   186 
   195 void
   187 void
   196 MIR_MaximizeWindow(_THIS, SDL_Window* window)
   188 MIR_MaximizeWindow(_THIS, SDL_Window* window)
   197 {
   189 {
   198     MIR_Window* mir_window = window->driverdata;
   190     MIR_Data*   mir_data   = _this->driverdata;
   199 
   191     MIR_Window* mir_window = window->driverdata;
   200     if (IsSurfaceValid(mir_window) < 0)
   192     MirSurfaceSpec* spec;
   201         return;
   193 
   202 
   194     if (IsSurfaceValid(mir_window) < 0)
   203     MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_maximized);
   195         return;
       
   196 
       
   197     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   198     MIR_mir_surface_spec_set_state(spec, mir_surface_state_maximized);
       
   199 
       
   200     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   201     MIR_mir_surface_spec_release(spec);
   204 }
   202 }
   205 
   203 
   206 void
   204 void
   207 MIR_MinimizeWindow(_THIS, SDL_Window* window)
   205 MIR_MinimizeWindow(_THIS, SDL_Window* window)
   208 {
   206 {
   209     MIR_Window* mir_window = window->driverdata;
   207     MIR_Data*   mir_data   = _this->driverdata;
   210 
   208     MIR_Window* mir_window = window->driverdata;
   211     if (IsSurfaceValid(mir_window) < 0)
   209     MirSurfaceSpec* spec;
   212         return;
   210 
   213 
   211     if (IsSurfaceValid(mir_window) < 0)
   214     MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_minimized);
   212         return;
       
   213 
       
   214     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   215     MIR_mir_surface_spec_set_state(spec, mir_surface_state_minimized);
       
   216 
       
   217     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   218     MIR_mir_surface_spec_release(spec);
   215 }
   219 }
   216 
   220 
   217 void
   221 void
   218 MIR_RestoreWindow(_THIS, SDL_Window * window)
   222 MIR_RestoreWindow(_THIS, SDL_Window * window)
   219 {
   223 {
   220     MIR_Window* mir_window = window->driverdata;
   224     MIR_Data*   mir_data   = _this->driverdata;
   221 
   225     MIR_Window* mir_window = window->driverdata;
   222     if (IsSurfaceValid(mir_window) < 0)
   226     MirSurfaceSpec* spec;
   223         return;
   227 
   224 
   228     if (IsSurfaceValid(mir_window) < 0)
   225     MIR_mir_surface_set_state(mir_window->surface, mir_surface_state_restored);
   229         return;
       
   230 
       
   231     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   232     MIR_mir_surface_spec_set_state(spec, mir_surface_state_restored);
       
   233 
       
   234     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   235     MIR_mir_surface_spec_release(spec);
       
   236 }
       
   237 
       
   238 void
       
   239 MIR_HideWindow(_THIS, SDL_Window* window)
       
   240 {
       
   241     MIR_Data*   mir_data   = _this->driverdata;
       
   242     MIR_Window* mir_window = window->driverdata;
       
   243     MirSurfaceSpec* spec;
       
   244 
       
   245     if (IsSurfaceValid(mir_window) < 0)
       
   246         return;
       
   247 
       
   248     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   249     MIR_mir_surface_spec_set_state(spec, mir_surface_state_hidden);
       
   250 
       
   251     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   252     MIR_mir_surface_spec_release(spec);
       
   253 }
       
   254 
       
   255 void
       
   256 MIR_SetWindowSize(_THIS, SDL_Window* window)
       
   257 {
       
   258     MIR_Data*   mir_data   = _this->driverdata;
       
   259     MIR_Window* mir_window = window->driverdata;
       
   260     MirSurfaceSpec* spec;
       
   261 
       
   262     if (IsSurfaceValid(mir_window) < 0)
       
   263         return;
       
   264 
       
   265     /* You cannot set the x/y of a mir window! So only update w/h */
       
   266     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   267     MIR_mir_surface_spec_set_width (spec, window->w);
       
   268     MIR_mir_surface_spec_set_height(spec, window->h);
       
   269 
       
   270     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   271     MIR_mir_surface_spec_release(spec);
       
   272 }
       
   273 
       
   274 void
       
   275 MIR_SetWindowMinimumSize(_THIS, SDL_Window* window)
       
   276 {
       
   277     MIR_Data*   mir_data   = _this->driverdata;
       
   278     MIR_Window* mir_window = window->driverdata;
       
   279     MirSurfaceSpec* spec;
       
   280 
       
   281     if (IsSurfaceValid(mir_window) < 0)
       
   282         return;
       
   283 
       
   284     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   285     MIR_mir_surface_spec_set_min_width (spec, window->min_w);
       
   286     MIR_mir_surface_spec_set_min_height(spec, window->min_h);
       
   287 
       
   288     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   289     MIR_mir_surface_spec_release(spec);
       
   290 }
       
   291 
       
   292 void
       
   293 MIR_SetWindowMaximumSize(_THIS, SDL_Window* window)
       
   294 {
       
   295     MIR_Data*   mir_data   = _this->driverdata;
       
   296     MIR_Window* mir_window = window->driverdata;
       
   297     MirSurfaceSpec* spec;
       
   298 
       
   299     if (IsSurfaceValid(mir_window) < 0)
       
   300         return;
       
   301 
       
   302     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   303     MIR_mir_surface_spec_set_max_width (spec, window->max_w);
       
   304     MIR_mir_surface_spec_set_max_height(spec, window->max_h);
       
   305 
       
   306     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   307     MIR_mir_surface_spec_release(spec);
       
   308 }
       
   309 
       
   310 void
       
   311 MIR_SetWindowTitle(_THIS, SDL_Window* window)
       
   312 {
       
   313     MIR_Data*   mir_data   = _this->driverdata;
       
   314     MIR_Window* mir_window = window->driverdata;
       
   315     char const* title = window->title ? window->title : "";
       
   316     MirSurfaceSpec* spec;
       
   317 
       
   318     if (IsSurfaceValid(mir_window) < 0)
       
   319         return;
       
   320 
       
   321     spec = MIR_mir_connection_create_spec_for_changes(mir_data->connection);
       
   322     MIR_mir_surface_spec_set_name(spec, title);
       
   323 
       
   324     MIR_mir_surface_apply_spec(mir_window->surface, spec);
       
   325     MIR_mir_surface_spec_release(spec);
   226 }
   326 }
   227 
   327 
   228 #endif /* SDL_VIDEO_DRIVER_MIR */
   328 #endif /* SDL_VIDEO_DRIVER_MIR */
   229 
   329 
   230 /* vi: set ts=4 sw=4 expandtab: */
   330 /* vi: set ts=4 sw=4 expandtab: */