Commit ae41b12f authored by Cameron Armstrong (Nightfox)'s avatar Cameron Armstrong (Nightfox) Committed by Gargaj
Browse files

Added TouchBar support for MacBook Pro (#67)

* Added TouchBar support for MacBook Pro

* [macOS] Optionalize use of touchbar

There is a new CMake flag named BONZOMATIC_TOUCHBAR (default ON) to activate or deactivate TouchBar support.

* Add a runtime check to see if the TouchBar API is available. If it's not, do not attempt to create a TouchBar.

* Updated Travis CI to use Xcode 9.3 for its build
parent 259e8f5e
......@@ -10,6 +10,7 @@ matrix:
env: BONZOMATIC_X64=ON
- os: osx
env: BONZOMATIC_X64=OFF
osx_image: xcode9.3
exclude:
- compiler: gcc # travis bug: https://github.com/travis-ci/travis-ci/issues/4681
......
......@@ -24,6 +24,8 @@ endif ()
option(BONZOMATIC_NDI "Enable NDI?" OFF)
option(BONZOMATIC_TOUCHBAR "Compile with macOS TouchBar support (Xcode 9 or newer required)?" ON)
set(BONZOMATIC_WINDOWS_FLAVOR "DX11" CACHE STRING "Windows renderer flavor selected at CMake configure time (DX11, DX9 or GLFW)")
set_property(CACHE BONZOMATIC_WINDOWS_FLAVOR PROPERTY STRINGS DX11 DX9 GLFW)
......@@ -325,6 +327,7 @@ if (APPLE)
${CMAKE_SOURCE_DIR}/src/platform_x11/Timer.cpp
${CMAKE_SOURCE_DIR}/src/platform_osx/SetupDialog.cpp
${CMAKE_SOURCE_DIR}/src/platform_osx/Clipboard.cpp
${CMAKE_SOURCE_DIR}/src/platform_osx/TouchBar.mm
)
source_group("Bonzomatic\\Platform" FILES ${BZC_PLATFORM_SRCS})
set(BZC_RESOURCES_DATA
......@@ -447,7 +450,7 @@ set(BZC_PROJECT_LIBS ${BZC_PROJECT_LIBS} ${PLATFORM_LIBS})
# create the executable
link_directories(${BZC_LINK_DIRS})
if (UNIX AND (NOT APPLE))
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
endif ()
add_executable(${BZC_EXE_NAME} ${BZC_PROJECT_SRCS})
if (APPLE AND GLFW_USE_RETINA)
......@@ -456,6 +459,9 @@ if (APPLE AND GLFW_USE_RETINA)
endif ()
# Set compiler specific flags
if (APPLE)
if (BONZOMATIC_TOUCHBAR)
target_compile_definitions(${BZC_EXE_NAME} PUBLIC -DBONZOMATIC_ENABLE_TOUCHBAR)
endif ()
#target_compile_options(${BZC_EXE_NAME} PUBLIC -Werror -pedantic-errors -Wall -Wno-format-security)
elseif (UNIX)
if (NOT BONZOMATIC_64BIT)
......
//
// TouchBar.h
// Bonzomatic
//
// Created by Nightfox on 13/5/18.
//
#ifdef BONZOMATIC_ENABLE_TOUCHBAR
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#include <GLFW/glfw3native.h>
void ShowTouchBar(GLFWwindow *window);
#endif
......@@ -23,6 +23,10 @@
#include "stb_image.h"
#include "Scintilla.h"
#ifdef __APPLE__
#include "../TouchBar.h"
#endif
const char * shaderKeyword =
"discard struct if else switch case default break goto return for while do continue";
......@@ -244,6 +248,12 @@ namespace Renderer
return false;
}
#ifdef __APPLE__
#ifdef BONZOMATIC_ENABLE_TOUCHBAR
ShowTouchBar(mWindow);
#endif
#endif
glfwMakeContextCurrent(mWindow);
// TODO: here add text callbacks
......
//
// TouchBar.mm
// Bonzomatic
//
// Created by Nightfox on 13/5/18.
//
#ifdef BONZOMATIC_ENABLE_TOUCHBAR
#include <Carbon/Carbon.h>
#import <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#import "../TouchBar.h"
static NSString *touchBarCustomizationId = @"com.something.customization_id";
static NSString *touchBarItemIdCompile = @"bonzomatic.compile";
static NSString *touchBarItemIdToggleGUI = @"bonzomatic.toggleGUI";
static NSString *touchBarItemIdTextures = @"bonzomatic.textures";
static NSString *touchBarItemIdSeparator = @"bonzomatic.separator";
static NSString *touchBarItemIdQuit = @"bonzomatic.quit";
@interface TouchBar : NSObject <NSTouchBarDelegate>
- (NSTouchBar *)makeTouchBar;
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier;
- (void)glfwButtonAction:(id)sender;
typedef enum {
toggleTextures,
toggleGUI,
compile
} BonzomaticCommand;
@end
@implementation TouchBar
- (NSTouchBar *)makeTouchBar
{
// Create TouchBar object
NSTouchBar *touchBar = [[NSTouchBar alloc] init];
touchBar.delegate = self;
touchBar.customizationIdentifier = touchBarCustomizationId;
// Set the default ordering of items.
touchBar.defaultItemIdentifiers = @[touchBarItemIdCompile, touchBarItemIdToggleGUI, touchBarItemIdTextures, touchBarItemIdSeparator, touchBarItemIdQuit, NSTouchBarItemIdentifierOtherItemsProxy];
return touchBar;
}
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
if ([identifier isEqualToString:touchBarItemIdCompile]) {
NSButton *button = [NSButton buttonWithTitle:NSLocalizedString(@"Compile", @"") target:self action:@selector(compile)];
button.bezelColor = [NSColor colorWithRed:0.0 green:0.5 blue:0.3 alpha:1.0];
NSCustomTouchBarItem* g_TouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:touchBarItemIdCompile];
g_TouchBarItem.view = button;
g_TouchBarItem.customizationLabel = NSLocalizedString(@"Truth Button", @"");
return g_TouchBarItem;
}
if ([identifier isEqualToString:touchBarItemIdToggleGUI]) {
NSButton *button = [NSButton buttonWithTitle:NSLocalizedString(@"Toggle GUI", @"") target:self action:@selector(toggleGui)];
NSCustomTouchBarItem* g_TouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:touchBarItemIdToggleGUI];
g_TouchBarItem.view = button;
g_TouchBarItem.customizationLabel = NSLocalizedString(@"Truth Button", @"");
return g_TouchBarItem;
}
if ([identifier isEqualToString:touchBarItemIdTextures]) {
NSButton *button = [NSButton buttonWithTitle:NSLocalizedString(@"Textures", @"") target:self action:@selector(toggleTextures)];
NSCustomTouchBarItem* g_TouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:touchBarItemIdTextures];
g_TouchBarItem.view = button;
g_TouchBarItem.customizationLabel = NSLocalizedString(@"Truth Button", @"");
return g_TouchBarItem;
}
if ([identifier isEqualToString:touchBarItemIdSeparator]) {
NSCustomTouchBarItem* g_TouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:touchBarItemIdSeparator];
g_TouchBarItem.view = [[NSView alloc] init];
g_TouchBarItem.customizationLabel = NSLocalizedString(@"Truth Button", @"");
return g_TouchBarItem;
}
if ([identifier isEqualToString:touchBarItemIdQuit]) {
NSButton *button = [NSButton buttonWithTitle:NSLocalizedString(@"Quit", @"") target:self action:@selector(quit)];
button.bezelColor = [NSColor redColor];
NSCustomTouchBarItem* g_TouchBarItem = [[NSCustomTouchBarItem alloc] initWithIdentifier:touchBarItemIdQuit];
g_TouchBarItem.view = button;
g_TouchBarItem.customizationLabel = NSLocalizedString(@"Truth Button", @"");
return g_TouchBarItem;
}
return nil;
}
- (void)quit {
exit(0);
}
- (void)toggleGui {
[self sendKeyboardCommandForCommand:toggleGUI];
}
- (void)toggleTextures {
[self sendKeyboardCommandForCommand:toggleTextures];
}
- (void)compile {
[self sendKeyboardCommandForCommand:compile];
}
- (void)sendKeyboardCommandForCommand:(BonzomaticCommand)command {
pid_t pid = [NSRunningApplication currentApplication].processIdentifier;
CGEventRef event1, event2;
switch (command) {
case toggleGUI:
event1 = CGEventCreateKeyboardEvent (NULL, kVK_F11, true);
event2 = CGEventCreateKeyboardEvent (NULL, kVK_F11, false);
break;
case toggleTextures:
event1 = CGEventCreateKeyboardEvent (NULL, kVK_F2, true);
event2 = CGEventCreateKeyboardEvent (NULL, kVK_F2, false);
break;
case compile:
event1 = CGEventCreateKeyboardEvent (NULL, kVK_F5, true);
event2 = CGEventCreateKeyboardEvent (NULL, kVK_F5, false);
break;
}
CGEventPostToPid(pid, event1);
CGEventPostToPid(pid, event2);
CFRelease(event1);
CFRelease(event2);
}
@end
void ShowTouchBar(GLFWwindow *window) {
if (@available(macOS 10.12.2, *)) {
TouchBar* g_TouchBarDelegate = [[TouchBar alloc] init];
[NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES;
NSTouchBar *touchBar = [g_TouchBarDelegate makeTouchBar];
NSWindow *nswin = glfwGetCocoaWindow(window);
nswin.touchBar = touchBar;
}
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment