1 /************************************************************************** 2 * 3 * Copyright (C) 2013 DENSO CORPORATION 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ****************************************************************************/ 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <memory.h> 22 #include <unistd.h> 23 #include <pthread.h> 24 #include "ilm_common.h" 25 #include "ilm_common_platform.h" 26 #include "ilm_types.h" 27 #include "wayland-util.h" 28 #include "wayland-client.h" 29 30 static ilmErrorTypes wayland_init(t_ilm_nativedisplay nativedisplay); 31 static t_ilm_nativedisplay wayland_getNativedisplay(void); 32 static t_ilm_bool wayland_isInitialized(void); 33 static ilmErrorTypes wayland_destroy(void); 34 35 ILM_COMMON_PLATFORM_FUNC gIlmCommonPlatformFunc; 36 init_ilmCommonPlatformTable(void)37void init_ilmCommonPlatformTable(void) 38 { 39 gIlmCommonPlatformFunc.init = wayland_init; 40 gIlmCommonPlatformFunc.getNativedisplay = wayland_getNativedisplay; 41 gIlmCommonPlatformFunc.isInitialized = wayland_isInitialized; 42 gIlmCommonPlatformFunc.destroy = wayland_destroy; 43 } 44 45 /* 46 *============================================================================= 47 * global vars 48 *============================================================================= 49 */ 50 /* automatically gets assigned argv[0] */ 51 extern char *__progname; 52 53 struct ilm_common_context { 54 int32_t valid; 55 int32_t disconnect_display; 56 struct wl_display *display; 57 }; 58 59 static struct ilm_common_context ilm_context = {0}; 60 61 static ilmErrorTypes wayland_init(t_ilm_nativedisplay nativedisplay)62wayland_init(t_ilm_nativedisplay nativedisplay) 63 { 64 struct ilm_common_context *ctx = &ilm_context; 65 66 if (nativedisplay != 0) { 67 ctx->display = (struct wl_display*)nativedisplay; 68 ctx->disconnect_display = 0; 69 } else { 70 ctx->display = wl_display_connect(NULL); 71 ctx->disconnect_display = 1; 72 if (ctx->display == NULL) { 73 fprintf(stderr, "Failed to connect display in libilmCommon\n"); 74 return ILM_FAILED; 75 } 76 } 77 78 ctx->valid = 1; 79 80 return ILM_SUCCESS; 81 } 82 83 static t_ilm_nativedisplay wayland_getNativedisplay(void)84wayland_getNativedisplay(void) 85 { 86 struct ilm_common_context *ctx = &ilm_context; 87 88 return (t_ilm_nativedisplay)ctx->display; 89 } 90 91 static t_ilm_bool wayland_isInitialized(void)92wayland_isInitialized(void) 93 { 94 struct ilm_common_context *ctx = &ilm_context; 95 96 if (ctx->valid != 0) { 97 return ILM_TRUE; 98 } else { 99 return ILM_FALSE; 100 } 101 } 102 103 static ilmErrorTypes wayland_destroy(void)104wayland_destroy(void) 105 { 106 struct ilm_common_context *ctx = &ilm_context; 107 108 if (ctx->valid == 0) 109 { 110 fprintf(stderr, "[Warning] The ilm_common_context is already destroyed\n"); 111 return ILM_FAILED; 112 } 113 114 ctx->valid = 0; 115 116 // we own the display, act like it. 117 if (ctx->disconnect_display) 118 { 119 wl_display_roundtrip(ctx->display); 120 wl_display_disconnect(ctx->display); 121 ctx->display = NULL; 122 } 123 124 return ILM_SUCCESS; 125 } 126