Skip to content

Commit 52275b8

Browse files
Try to unify linux and apple code paths
1 parent 50bb065 commit 52275b8

File tree

7 files changed

+102
-93
lines changed

7 files changed

+102
-93
lines changed

CMakeLists.txt

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cmake_dependent_option(ENABLE_ZLIB "Enable zlib" ON "ENABLE_IMAGEMAGICK6 OR ENAB
4343
cmake_dependent_option(ENABLE_EGL "Enable egl" ON "LINUX" OFF)
4444
cmake_dependent_option(ENABLE_GLX "Enable glx" ON "LINUX" OFF)
4545
cmake_dependent_option(ENABLE_OSMESA "Enable osmesa" ON "LINUX" OFF)
46-
cmake_dependent_option(ENABLE_APPLECGL "Enable apple-cgl" ON "APPLE" OFF)
46+
cmake_dependent_option(ENABLE_CGL "Enable cgl" ON "APPLE" OFF)
4747
cmake_dependent_option(ENABLE_OPENCL "Enable opencl" ON "LINUX OR APPLE" OFF)
4848

4949
option(BUILD_TESTS "Build tests" OFF) # Also create test executables
@@ -276,7 +276,7 @@ if(HAVE_SYSINFO_H)
276276
target_compile_definitions(libfastfetch PUBLIC FF_HAVE_SYSINFO_H)
277277
endif()
278278

279-
function(ff_check_lib VARNAME)
279+
function(ff_lib_enable VARNAME)
280280
if(NOT ENABLE_${VARNAME})
281281
return()
282282
endif()
@@ -293,51 +293,28 @@ function(ff_check_lib VARNAME)
293293
message(WARNING "Package ${ARGV1} not found, building without support.")
294294
endfunction()
295295

296-
ff_check_lib(LIBPCI libpci)
297-
ff_check_lib(VULKAN vulkan)
298-
ff_check_lib(WAYLAND wayland-client)
299-
ff_check_lib(XCB_RANDR xcb-randr)
300-
ff_check_lib(XCB xcb)
301-
ff_check_lib(XRANDR xrandr)
302-
ff_check_lib(X11 x11)
303-
ff_check_lib(GIO gio-2.0)
304-
ff_check_lib(DCONF dconf)
305-
ff_check_lib(DBUS dbus-1)
306-
ff_check_lib(XFCONF libxfconf-0)
307-
ff_check_lib(SQLITE3 sqlite3)
308-
ff_check_lib(RPM rpm)
309-
ff_check_lib(IMAGEMAGICK7 MagickCore-7.Q16HDRI MagickCore-7.Q16 /usr/lib/imagemagick7/pkgconfig/MagickCore-7.Q16HDRI.pc /usr/lib/imagemagick7/pkgconfig/MagickCore-7.Q16.pc)
310-
ff_check_lib(IMAGEMAGICK6 MagickCore-6.Q16HDRI MagickCore-6.Q16 /usr/lib/imagemagick6/pkgconfig/MagickCore-6.Q16HDRI.pc /usr/lib/imagemagick6/pkgconfig/MagickCore-6.Q16.pc)
311-
ff_check_lib(ZLIB zlib)
312-
ff_check_lib(CHAFA chafa>=1.10)
313-
314-
if(APPLE)
315-
# Apple framework bundles can't be found by pkg-config
316-
if(ENABLE_APPLECGL)
317-
find_package(OpenGL)
318-
if(OpenGL_FOUND)
319-
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_APPLECGL=1)
320-
target_link_libraries(libfastfetch PRIVATE ${OPENGL_LIBRARIES})
321-
else()
322-
message(WARNING "Package Apple-CGL not found, building without support.")
323-
endif()
324-
endif()
325-
326-
if(ENABLE_OPENCL)
327-
find_package(OpenCL)
328-
if(OpenCL_FOUND)
329-
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_OPENCL=1)
330-
target_link_libraries(libfastfetch PRIVATE ${OpenCL_LIBRARY})
331-
else()
332-
message(WARNING "Package OpenCL not found, building without support.")
333-
endif()
334-
endif()
335-
else()
336-
ff_check_lib(EGL egl)
337-
ff_check_lib(GLX glx)
338-
ff_check_lib(OSMESA osmesa)
339-
ff_check_lib(OPENCL OpenCL)
340-
endif()
296+
ff_lib_enable(LIBPCI libpci)
297+
ff_lib_enable(VULKAN vulkan)
298+
ff_lib_enable(WAYLAND wayland-client)
299+
ff_lib_enable(XCB_RANDR xcb-randr)
300+
ff_lib_enable(XCB xcb)
301+
ff_lib_enable(XRANDR xrandr)
302+
ff_lib_enable(X11 x11)
303+
ff_lib_enable(GIO gio-2.0)
304+
ff_lib_enable(DCONF dconf)
305+
ff_lib_enable(DBUS dbus-1)
306+
ff_lib_enable(XFCONF libxfconf-0)
307+
ff_lib_enable(SQLITE3 sqlite3)
308+
ff_lib_enable(RPM rpm)
309+
ff_lib_enable(IMAGEMAGICK7 MagickCore-7.Q16HDRI MagickCore-7.Q16 /usr/lib/imagemagick7/pkgconfig/MagickCore-7.Q16HDRI.pc /usr/lib/imagemagick7/pkgconfig/MagickCore-7.Q16.pc)
310+
ff_lib_enable(IMAGEMAGICK6 MagickCore-6.Q16HDRI MagickCore-6.Q16 /usr/lib/imagemagick6/pkgconfig/MagickCore-6.Q16HDRI.pc /usr/lib/imagemagick6/pkgconfig/MagickCore-6.Q16.pc)
311+
ff_lib_enable(ZLIB zlib)
312+
ff_lib_enable(CHAFA chafa>=1.10)
313+
ff_lib_enable(EGL egl)
314+
ff_lib_enable(GLX glx)
315+
ff_lib_enable(OSMESA osmesa)
316+
ff_lib_enable(CGL, OpenGL)
317+
ff_lib_enable(OPENCL OpenCL)
341318

342319
target_include_directories(libfastfetch
343320
PUBLIC ${PROJECT_BINARY_DIR}

src/common/init.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ static void defaultConfig(FFinstance* instance)
211211
ffStrbufInitA(&instance->config.libEGL, 0);
212212
ffStrbufInitA(&instance->config.libGLX, 0);
213213
ffStrbufInitA(&instance->config.libOSMesa, 0);
214+
ffStrbufInitA(&instance->config.libCGL, 0);
214215
ffStrbufInitA(&instance->config.libOpenCL, 0);
215216

216217
instance->config.titleFQDN = false;
@@ -430,8 +431,8 @@ void ffListFeatures()
430431
#ifdef FF_HAVE_OSMESA
431432
"osmesa\n"
432433
#endif
433-
#ifdef FF_HAVE_APPLECGL
434-
"apple-cgl\n"
434+
#ifdef FF_HAVE_CGL
435+
"cgl\n"
435436
#endif
436437
#ifdef FF_HAVE_OPENCL
437438
"opencl\n"

src/common/library.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
static void* libraryLoad(const char* path, int maxVersion)
77
{
88
void* result = dlopen(path, RTLD_LAZY);
9-
if(result != NULL)
9+
if(result != NULL || maxVersion < 0)
1010
return result;
1111

1212
FFstrbuf pathbuf;

src/fastfetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
12581258
"egl", FF_GL_TYPE_EGL,
12591259
"glx", FF_GL_TYPE_GLX,
12601260
"osmesa", FF_GL_TYPE_OSMESA,
1261-
"apple-cgl", FF_GL_TYPE_APPLECGL,
1261+
"cgl", FF_GL_TYPE_CGL,
12621262
NULL
12631263
);
12641264
}

src/fastfetch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ typedef enum FFGLType
5151
FF_GL_TYPE_EGL,
5252
FF_GL_TYPE_GLX,
5353
FF_GL_TYPE_OSMESA,
54-
FF_GL_TYPE_APPLECGL
54+
FF_GL_TYPE_CGL
5555
} FFGLType;
5656

5757
typedef struct FFModuleArgs
@@ -148,6 +148,7 @@ typedef struct FFconfig
148148
FFstrbuf libEGL;
149149
FFstrbuf libGLX;
150150
FFstrbuf libOSMesa;
151+
FFstrbuf libCGL;
151152
FFstrbuf libOpenCL;
152153

153154
bool titleFQDN;

src/modules/opencl.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,17 @@ static const char* printOpenCL(FFinstance* instance)
8080
OpenCLData data;
8181

8282
#ifdef __APPLE__
83-
data.ffclGetPlatformIDs = clGetPlatformIDs;
84-
data.ffclGetDeviceIDs = clGetDeviceIDs;
85-
data.ffclGetDeviceInfo = clGetDeviceInfo;
83+
FF_LIBRARY_LOAD(opencl, instance->config.libOpenCL, "dlopen OpenCL failed", "/System/Library/Frameworks/OpenCL.framework/OpenCL", -1);
8684
#else
87-
FF_LIBRARY_LOAD(opencl, instance->config.libOpenCL, "dlopen libOpenCL.so failed", "libOpenCL.so", 1);
85+
FF_LIBRARY_LOAD(opencl, instance->config.libOpenCL, "dlopen libOpenCL.so failed", "libOpenCL.so", 1);
86+
#endif
87+
8888
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetPlatformIDs);
8989
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceIDs);
9090
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(opencl, data, clGetDeviceInfo);
91-
#endif
9291

9392
const char* error = openCLHandelData(instance, &data);
94-
#ifndef __APPLE__
9593
dlclose(opencl);
96-
#endif
9794
return error;
9895
}
9996
#endif

src/modules/opengl.c

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
#define FF_OPENGL_MODULE_NAME "OpenGL"
88
#define FF_OPENGL_NUM_FORMAT_ARGS 4
99

10-
#if defined(FF_HAVE_EGL) || defined(FF_HAVE_GLX) || defined(FF_HAVE_OSMESA) || defined(FF_HAVE_APPLECGL)
10+
#if defined(FF_HAVE_EGL) || defined(FF_HAVE_GLX) || defined(FF_HAVE_OSMESA) || defined(FF_HAVE_CGL)
1111
#define FF_HAVE_GL 1
12+
1213
#include "common/library.h"
14+
1315
#ifdef __APPLE__
14-
#define GL_SILENCE_DEPRECATION
15-
#include <OpenGL/gl.h>
16+
#define GL_SILENCE_DEPRECATION
17+
#include <OpenGL/gl.h>
1618
#else
17-
#include <GL/gl.h>
19+
#include <GL/gl.h>
1820
#endif
1921

2022
#define FF_OPENGL_BUFFER_WIDTH 1
@@ -337,44 +339,75 @@ static const char* osMesaPrint(FFinstance* instance)
337339

338340
#endif //FF_HAVE_OSMESA
339341

340-
#ifdef FF_HAVE_APPLECGL
342+
#ifdef FF_HAVE_CGL
343+
344+
typedef struct CGLData
345+
{
346+
GLData glData;
347+
348+
FF_LIBRARY_SYMBOL(CGLChoosePixelFormat);
349+
FF_LIBRARY_SYMBOL(CGLCreateContext);
350+
FF_LIBRARY_SYMBOL(CGLSetCurrentContext);
351+
FF_LIBRARY_SYMBOL(CGLDestroyContext);
352+
FF_LIBRARY_SYMBOL(CGLDestroyPixelFormat);
353+
354+
CGLPixelFormatObj pixelFormat;
355+
CGLContextObj context;
356+
} CGLData;
357+
358+
static const char* cglHandleContext(FFinstance* instance, CGLData* data)
359+
{
360+
if(data->ffCGLSetCurrentContext(data->context) != kCGLNoError)
361+
return "CGLSetCurrentContext() failed";
341362

342-
#include <OpenGL/OpenGL.h>
363+
return glHandlePrint(instance, &data->glData);
364+
}
343365

344-
static const char* appleCglPrint(FFinstance* instance)
366+
static const char* cglHandlePixelFormat(FFinstance* instance, CGLData* data)
367+
{
368+
if(data->ffCGLCreateContext(data->pixelFormat, NULL, &data->context) != kCGLNoError)
369+
return "CGLCreateContext() failed";
370+
371+
const char* error = cglHandleContext(instance, data);
372+
data->ffCGLDestroyContext(data->context);
373+
return error;
374+
}
375+
376+
static const char* cglHandleData(FFinstance* instance, CGLData* data)
345377
{
346378
CGLPixelFormatAttribute attrs[] = {
347-
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
379+
kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
348380
kCGLPFAAccelerated,
349381
0
350382
};
351-
CGLPixelFormatObj pix;
383+
352384
GLint num;
353-
if (CGLChoosePixelFormat(attrs, &pix, &num) != kCGLNoError) {
385+
if (data->ffCGLChoosePixelFormat(attrs, &data->pixelFormat, &num) != kCGLNoError)
354386
return "CGLChoosePixelFormat() failed";
355-
}
356387

357-
CGLContextObj ctx;
358-
if (CGLCreateContext(pix, NULL, &ctx) != kCGLNoError) {
359-
return "CGLCreateContext() failed";
360-
}
361-
if (CGLSetCurrentContext(ctx) != kCGLNoError) {
362-
return "CGLSetCurrentContext() failed";
363-
}
364-
365-
GLData glData = {
366-
.ffglGetString = glGetString
367-
};
368-
369-
const char* error = glHandlePrint(instance, &glData);
370-
371-
CGLDestroyContext(ctx);
372-
CGLDestroyPixelFormat(pix);
388+
const char* error = cglHandlePixelFormat(instance, data);
389+
data->ffCGLDestroyPixelFormat(data->pixelFormat);
390+
return error;
391+
}
373392

393+
static const char* cglPrint(FFinstance* instance)
394+
{
395+
CGLData data;
396+
397+
FF_LIBRARY_LOAD(cgl, instance->config.libCGL, "dlopen cgl failed", "/System/Library/Frameworks/OpenGL.framework/OpenGL", -1);
398+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(cgl, data, CGLChoosePixelFormat);
399+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(cgl, data, CGLCreateContext);
400+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(cgl, data, CGLSetCurrentContext);
401+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(cgl, data, CGLDestroyContext);
402+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(cgl, data, CGLDestroyPixelFormat);
403+
FF_LIBRARY_LOAD_SYMBOL_VAR_MESSAGE(cgl, data.glData, glGetString);
404+
405+
const char* error = cglHandleData(instance, &data);
406+
dlclose(cgl);
374407
return error;
375408
}
376409

377-
#endif //FF_HAVE_APPLECGL
410+
#endif //FF_HAVE_CGL
378411

379412
static const char* glPrint(FFinstance* instance)
380413
{
@@ -405,12 +438,12 @@ static const char* glPrint(FFinstance* instance)
405438
#endif
406439
}
407440

408-
if(instance->config.glType == FF_GL_TYPE_APPLECGL)
441+
if(instance->config.glType == FF_GL_TYPE_CGL)
409442
{
410-
#ifdef FF_HAVE_APPLECGL
411-
return appleCglPrint(instance);
443+
#ifdef FF_HAVE_CGL
444+
return cglPrint(instance);
412445
#else
413-
return "fastfetch was compiled without apple-cgl support";
446+
return "fastfetch was compiled without cgl support";
414447
#endif
415448
}
416449

@@ -425,9 +458,9 @@ static const char* glPrint(FFinstance* instance)
425458
error = glxPrint(instance);
426459
#endif
427460

428-
#ifdef FF_HAVE_APPLECGL
461+
#ifdef FF_HAVE_CGL
429462
if(error != NULL)
430-
error = appleCglPrint(instance);
463+
error = cglPrint(instance);
431464
#endif
432465

433466
//We don't use osmesa in auto mode here, because it is a software implementation,

0 commit comments

Comments
 (0)