ecore_cocoa: add code from old branch

This commit is contained in:
Raoul Hecky 2014-06-08 21:50:42 +02:00 committed by Cedric BAIL
parent fc4d108b5f
commit 6f2d060c80
4 changed files with 370 additions and 337 deletions

View File

@ -30,6 +30,7 @@ extern "C" {
#endif
typedef struct _Ecore_Cocoa_Window Ecore_Cocoa_Window;
typedef struct _Ecore_Cocoa_Screen Ecore_Cocoa_Screen;
EAPI extern int ECORE_COCOA_EVENT_GOT_FOCUS;
EAPI extern int ECORE_COCOA_EVENT_LOST_FOCUS;
@ -43,6 +44,10 @@ struct _Ecore_Cocoa_Event_Video_Resize
int h;
};
struct _Ecore_Cocoa_Screen
{
int dummy;
};
/* Core */
@ -50,6 +55,10 @@ EAPI int ecore_cocoa_init(void);
EAPI int ecore_cocoa_shutdown(void);
EAPI void ecore_cocoa_feed_events(void);
/* Screen */
EAPI void ecore_cocoa_screen_size_get(Ecore_Cocoa_Screen *screen, int *w, int *h);
/* Window */
EAPI Ecore_Cocoa_Window *ecore_cocoa_window_new(int x,

View File

@ -14,8 +14,7 @@ static const struct _ecore_cocoa_keys_s keystable[] =
{ 0, "0x00", "" },
{ 0, "First", "" },
{ 3, "Return", "\015" },
// OS-X's backspace and Delete are reversed!
{ 8, "Delete", "\010" },
{ 8, "Delete", "\010" },
{ 9, "Tab", "\011" },
{ 12, "Clear", "" },
{ 13, "Return", "\015" },
@ -91,11 +90,10 @@ static const struct _ecore_cocoa_keys_s keystable[] =
{ 123, "braceleft", "" },
{ 124, "pipe", "" },
{ 125, "braceright", "" },
// OS-X's backspace and Delete are reversed!
{ 127, "BackSpace", "\177" },
{ 127, "BackSpace", "\177" },
{ 126, "asciitilde", "~" },
{ 160, "w0", "" },
{ 160, "w0", "" },
{ 161, "w1", "" },
{ 162, "w2", "" },
{ 163, "w3", "" },
@ -259,8 +257,8 @@ static const struct _ecore_cocoa_keys_s keystable[] =
{ NSClearLineFunctionKey, "Num_Lock", "" },
{ 301, "Caps_Lock", "" },
{ NSScrollLockFunctionKey, "Scroll_Lock", "" },
{ 303, "Shift_R", "" },
{ 304, "Shift_L", "" },
{ 303, "Shift_R", "Shift_R" },
{ 304, "Shift_L", "Shift_L" },
{ 305, "Control_R", "" },
{ 306, "Control_L", "" },
{ 307, "Alt_R", "" },

View File

@ -2,7 +2,8 @@
# include <config.h>
#endif
#include <Cocoa/Cocoa.h>
#import <Cocoa/Cocoa.h>
#import "ecore_cocoa_window.h"
#include <Eina.h>
@ -23,54 +24,6 @@ static int _ecore_cocoa_init_count = 0;
static int old_flags;
static unsigned int
_ecore_cocoa_event_modifiers(int mod)
{
unsigned int modifiers = 0;
if(mod & NSShiftKeyMask) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
if(mod & NSControlKeyMask) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
if(mod & NSAlternateKeyMask) modifiers |= ECORE_EVENT_MODIFIER_ALT;
if(mod & NSAlphaShiftKeyMask) modifiers |= ECORE_EVENT_LOCK_CAPS;
return modifiers;
}
static Ecore_Event_Key*
_ecore_cocoa_event_key(NSEvent *nsevent, double timestamp)
{
Ecore_Event_Key *ev;
unsigned int i;
int flags = [nsevent modifierFlags];
ev = malloc(sizeof(Ecore_Event_Key));
if (!ev) return NULL;
ev->timestamp = timestamp;
ev->window = 0;
ev->event_window = 0;
ev->modifiers = _ecore_cocoa_event_modifiers(flags);
ev->key = NULL;
ev->compose = NULL;
printf("key code : %d\n", [[nsevent charactersIgnoringModifiers] characterAtIndex:0]);
for (i = 0; i < EINA_C_ARRAY_LENGTH(keystable); ++i)
if (keystable[i].code == [[nsevent charactersIgnoringModifiers] characterAtIndex:0])
{
printf("keycode : %d key pressed : %s\n", keystable[i].code, keystable[i].name);
ev->keyname = keystable[i].name;
ev->key = ev->keyname;
ev->string = keystable[i].compose;
return ev;
}
free(ev);
return NULL;
}
EAPI int
ecore_cocoa_init(void)
{
@ -107,9 +60,25 @@ ecore_cocoa_shutdown(void)
return _ecore_cocoa_init_count;
}
static unsigned int
_ecore_cocoa_event_modifiers(unsigned int mod)
{
unsigned int modifiers = 0;
if(mod & NSShiftKeyMask) modifiers |= ECORE_EVENT_MODIFIER_SHIFT;
if(mod & NSControlKeyMask) modifiers |= ECORE_EVENT_MODIFIER_CTRL;
if(mod & NSAlternateKeyMask) modifiers |= ECORE_EVENT_MODIFIER_ALT;
if(mod & NSCommandKeyMask) modifiers |= ECORE_EVENT_MODIFIER_WIN;
if(mod & NSNumericPadKeyMask) modifiers |= ECORE_EVENT_LOCK_NUM;
printf("key modifiers: %d, %d\n", mod, modifiers);
return modifiers;
}
EAPI void
ecore_cocoa_feed_events(void)
{
Ecore_Event *ev;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.001];
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:date
@ -139,7 +108,8 @@ ecore_cocoa_feed_events(void)
ev->root.x = ev->x;
ev->root.y = ev->y;
ev->timestamp = time;
ev->window = [event window];
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
ev->modifiers = 0; /* FIXME: keep modifier around. */
ecore_event_add(ECORE_EVENT_MOUSE_MOVE, ev, NULL, NULL);
@ -163,15 +133,13 @@ ecore_cocoa_feed_events(void)
ev->root.x = ev->x;
ev->root.y = ev->y;
ev->timestamp = time;
if ([event buttonNumber] == 0)
ev->buttons = 1;
else if ([event buttonNumber] == 2)
ev->buttons = 2;
else
ev->buttons = 3;
printf("ev buttons : %d - %d\n", ev->buttons, [event buttonNumber]);
switch ([event buttonNumber])
{
case 0: ev->buttons = 1; break;
case 1: ev->buttons = 3; break;
case 2: ev->buttons = 2; break;
default: ev->buttons = 0; break;
}
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
@ -206,14 +174,13 @@ ecore_cocoa_feed_events(void)
ev->root.x = ev->x;
ev->root.y = ev->y;
ev->timestamp = time;
if ([event buttonNumber] == 0)
ev->buttons = 1;
else if ([event buttonNumber] == 2)
ev->buttons = 2;
else
ev->buttons = 3;
switch ([event buttonNumber])
{
case 0: ev->buttons = 1; break;
case 1: ev->buttons = 3; break;
case 2: ev->buttons = 2; break;
default: ev->buttons = 0; break;
}
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
@ -235,60 +202,117 @@ ecore_cocoa_feed_events(void)
case NSKeyDown:
{
Ecore_Event_Key *ev;
unsigned int i;
EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window];
ev = _ecore_cocoa_event_key(event, time);
ev = calloc(1, sizeof (Ecore_Event_Key));
if (!ev) return;
ev->timestamp = time;
ev->modifiers = _ecore_cocoa_event_modifiers([event modifierFlags]);
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, NULL, NULL);
for (i = 0; i < sizeof (keystable) / sizeof (struct _ecore_cocoa_keys_s); ++i)
{
if (keystable[i].code == tolower([[event charactersIgnoringModifiers] characterAtIndex:0]))
{
printf("Key pressed : %s\n", keystable[i].name);
ev->keyname = keystable[i].name;
ev->key = keystable[i].name;
ev->string = keystable[i].compose;
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
ecore_event_add(ECORE_EVENT_KEY_DOWN, ev, NULL, NULL);
return;
}
}
break;
}
case NSKeyUp:
{
Ecore_Event_Key *ev;
unsigned int i;
EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window];
ev = _ecore_cocoa_event_key(event, time);
if (!ev) return;
printf("Key Up\n");
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
ev = calloc(1, sizeof (Ecore_Event_Key));
if (!ev) return;
ev->timestamp = time;
ev->modifiers = _ecore_cocoa_event_modifiers([event modifierFlags]);
for (i = 0; i < sizeof (keystable) / sizeof (struct _ecore_cocoa_keys_s); ++i)
{
if (keystable[i].code == tolower([[event charactersIgnoringModifiers] characterAtIndex:0]))
{
ev->keyname = keystable[i].name;
ev->key = keystable[i].name;
ev->string = keystable[i].compose;
ev->window = window.ecore_window_data;
ev->event_window = ev->window;
ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
return;
}
}
break;
}
case NSFlagsChanged:
{
int flags = [event modifierFlags];
EcoreCocoaWindow *window = (EcoreCocoaWindow *)[event window];
Ecore_Event_Key *ev;
ev = calloc(1, sizeof(Ecore_Event_Key));
if (!ev) return;
Ecore_Event_Key *evDown = NULL;
Ecore_Event_Key *evUp = NULL;
evDown = calloc(1, sizeof (Ecore_Event_Key));
if (!evDown) return;
evUp = calloc(1, sizeof (Ecore_Event_Key));
if (!evUp)
{
free(evDown);
return;
}
// Turn special key flags on
if (flags & NSShiftKeyMask)
ev->keyname = "Shift_L";
evDown->keyname = "Shift_L";
else if (flags & NSControlKeyMask)
ev->keyname = "Control_L";
evDown->keyname = "Control_L";
else if (flags & NSAlternateKeyMask)
ev->keyname = "Alt_L";
evDown->keyname = "Alt_L";
else if (flags & NSCommandKeyMask)
ev->keyname = "Super_L";
evDown->keyname = "Super_L";
else if (flags & NSAlphaShiftKeyMask)
ev->keyname = "Caps_Lock";
evDown->keyname = "Caps_Lock";
if (ev->keyname)
if (evDown->keyname)
{
ev->key = ev->keyname;
ev->timestamp = time;
ev->string = "";
ev->modifiers = _ecore_cocoa_event_modifiers(flags);
printf("Flags changed keyup\n");
ecore_event_add(ECORE_EVENT_KEY_UP, ev, NULL, NULL);
evDown->timestamp = time;
evDown->string = "";
ecore_event_add(ECORE_EVENT_KEY_DOWN, evDown, NULL, NULL);
old_flags = flags;
break;
}
int changed_flags = flags ^ old_flags;
// Turn special key flags off
if (changed_flags & NSShiftKeyMask)
evUp->keyname = "Shift_L";
else if (changed_flags & NSControlKeyMask)
evUp->keyname = "Control_L";
else if (changed_flags & NSAlternateKeyMask)
evUp->keyname = "Alt_L";
else if (changed_flags & NSCommandKeyMask)
evUp->keyname = "Super_L";
else if (changed_flags & NSAlphaShiftKeyMask)
evUp->keyname = "Caps_Lock";
if (evUp->keyname)
{
evUp->timestamp = time;
evUp->string = "";
ecore_event_add(ECORE_EVENT_KEY_UP, evUp, NULL, NULL);
old_flags = flags;
break;
}
@ -306,6 +330,7 @@ ecore_cocoa_feed_events(void)
}
case NSScrollWheel:
{
printf("Scroll Wheel\n");
break;
}
default:
@ -317,3 +342,12 @@ ecore_cocoa_feed_events(void)
[event release];
}
EAPI void
ecore_cocoa_screen_size_get(Ecore_Cocoa_Screen *screen, int *w, int *h)
{
NSSize pt = [[[NSScreen screens] objectAtIndex:0] frame].size;
if (w) *w = (int)pt.width;
if (h) *h = (int)pt.height;
}

View File

@ -1,237 +1,229 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#import "ecore_cocoa_window.h"
#include "Ecore_Cocoa.h"
#include "ecore_cocoa_private.h"
@implementation EcoreCocoaWindow
@synthesize ecore_window_data;
- (id) initWithContentRect: (NSRect) contentRect
styleMask: (unsigned int) aStyle
backing: (NSBackingStoreType) bufferingType
defer: (BOOL) flag
{
if (![super initWithContentRect: contentRect
styleMask: aStyle
backing: bufferingType
defer: flag]) return nil;
[self setBackgroundColor: [NSColor whiteColor]];
[self makeKeyWindow];
[self setDelegate:self];
[self setAcceptsMouseMovedEvents:YES];
return self;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (void)windowWillClose:(NSNotification *)notification
{
NSLog(@"window is going to be closed");
}
- (void)windowDidResize:(NSNotification *)notification {
NSRect content_rect = [self contentRectForFrameRect:[self frame]];
Ecore_Cocoa_Window *window = ecore_window_data;
ecore_cocoa_window_resize(window, content_rect.size.width, content_rect.size.height);
printf("THIS IS A RESIZE......................%gx%g\n", content_rect.size.width, content_rect.size.height);
}
@end
static float _title_bar_height(void)
{
NSRect frame = NSMakeRect (0, 0, 100, 100);
NSRect contentRect;
contentRect = [NSWindow contentRectForFrameRect: frame
styleMask: NSTitledWindowMask];
return (frame.size.height - contentRect.size.height);
}
Ecore_Cocoa_Window *
ecore_cocoa_window_new(int x,
int y,
int width,
int height)
{
Ecore_Cocoa_Window *w;
EcoreCocoaWindow *window = [[EcoreCocoaWindow alloc] initWithContentRect:NSMakeRect(x, y, width, height)
styleMask:(NSTitledWindowMask |
NSClosableWindowMask |
NSResizableWindowMask |
NSMiniaturizableWindowMask)
backing:NSBackingStoreBuffered
defer:NO];
if (!window)
return NULL;
//Set the process to be a foreground process,
//without that it prevents the window to become the key window and
//receive all mouse mouve events.
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
w = calloc(1, sizeof(Ecore_Cocoa_Window));
w->window = window;
w->borderless = 0;
window.ecore_window_data = w;
return w;
}
void
ecore_cocoa_window_free(Ecore_Cocoa_Window *window)
{
if (!window)
return;
[window->window release];
free(window);
}
void
ecore_cocoa_window_move(Ecore_Cocoa_Window *window,
int x,
int y)
{
NSRect win_frame;
if (!window)
return;
win_frame = [window->window frame];
win_frame.origin.x = x;
win_frame.origin.y = y;
[window->window setFrame:win_frame display:YES];
}
void
ecore_cocoa_window_resize(Ecore_Cocoa_Window *window,
int width,
int height)
{
if (!window)
return;
NSRect win_frame;
if (!window)
return;
win_frame = [window->window frame];
win_frame.size.height = height + _title_bar_height();
win_frame.size.width = width;
printf("Resize ..............\n");
[window->window setFrame:win_frame display:YES];
//ecore_event_add(ECORE_COCOA_EVENT_RESIZE, NULL, NULL, NULL);
}
void
ecore_cocoa_window_move_resize(Ecore_Cocoa_Window *window,
int x,
int y,
int width,
int height)
{
if (!window)
return;
NSRect win_frame;
if (!window)
return;
win_frame = [window->window frame];
win_frame.size.height = height + _title_bar_height();
win_frame.size.width = width;
win_frame.origin.x = x;
win_frame.origin.y = y;
[window->window setFrame:win_frame display:YES];
}
void
ecore_cocoa_window_title_set(Ecore_Cocoa_Window *window, const char *title)
{
if (!window || !title)
return;
[window->window setTitle:[NSString stringWithUTF8String:title]];
}
void
ecore_cocoa_window_show(Ecore_Cocoa_Window *window)
{
if (!window || [window->window isVisible])
{
printf("Window(%p) is not visible\n", window->window);
return;
}
[window->window makeKeyAndOrderFront:NSApp];
}
void
ecore_cocoa_window_hide(Ecore_Cocoa_Window *window)
{
if (!window || ![window->window isVisible])
return;
[window->window orderOut:NSApp];
}
void
ecore_cocoa_window_borderless_set(Ecore_Cocoa_Window *window,
int on)
{
if (!window)
return;
if (on)
[window->window setContentBorderThickness:0.0
forEdje:NSMinXEdge | NSMinYEdge | NSMaxXEdge | NSMaxYEdge];
}
void
ecore_cocoa_window_view_set(Ecore_Cocoa_Window *window,
void *view)
{
if (!window || !view)
return;
//[[window->window contentView] addSubview:view];
[window->window setContentView:view];
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[view frame]
options:NSTrackingMouseMoved |
NSTrackingActiveInActiveApp |
NSTrackingInVisibleRect
owner:view
userInfo:nil];
[view addTrackingArea:area];
[area release];
}
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#import "ecore_cocoa_window.h"
@implementation EcoreCocoaWindow
@synthesize ecore_window_data;
- (id) initWithContentRect: (NSRect) contentRect
styleMask: (unsigned int) aStyle
backing: (NSBackingStoreType) bufferingType
defer: (BOOL) flag
{
if (![super initWithContentRect: contentRect
styleMask: aStyle
backing: bufferingType
defer: flag]) return nil;
[self setBackgroundColor: [NSColor whiteColor]];
[self makeKeyWindow];
[self setDelegate:self];
[self setAcceptsMouseMovedEvents:YES];
return self;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (void)windowWillClose:(NSNotification *)notification
{
NSLog(@"window is going to be closed");
}
@end
#include "Ecore_Cocoa.h"
#include "ecore_cocoa_private.h"
static float _title_bar_height(void)
{
NSRect frame = NSMakeRect (0, 0, 100, 100);
NSRect contentRect;
contentRect = [NSWindow contentRectForFrameRect: frame
styleMask: NSTitledWindowMask];
return (frame.size.height - contentRect.size.height);
}
Ecore_Cocoa_Window *
ecore_cocoa_window_new(int x,
int y,
int width,
int height)
{
Ecore_Cocoa_Window *w;
EcoreCocoaWindow *window = [[EcoreCocoaWindow alloc]
initWithContentRect:NSMakeRect(x, y, width, height)
styleMask:(NSTitledWindowMask |
NSClosableWindowMask |
NSResizableWindowMask |
NSMiniaturizableWindowMask)
backing:NSBackingStoreBuffered
defer:NO];
if (!window)
return NULL;
//Set the process to be a foreground process,
//without that it prevents the window to become the key window and
//receive all mouse mouve events.
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
w = calloc(1, sizeof(Ecore_Cocoa_Window));
w->window = window;
w->borderless = 0;
window.ecore_window_data = w;
return w;
}
void
ecore_cocoa_window_free(Ecore_Cocoa_Window *window)
{
if (!window)
return;
[window->window release];
free(window);
}
void
ecore_cocoa_window_move(Ecore_Cocoa_Window *window,
int x,
int y)
{
NSRect win_frame;
if (!window)
return;
win_frame = [window->window frame];
win_frame.origin.x = x;
win_frame.origin.y = y;
[window->window setFrame:win_frame display:YES];
}
void
ecore_cocoa_window_resize(Ecore_Cocoa_Window *window,
int width,
int height)
{
if (!window)
return;
NSRect win_frame;
if (!window)
return;
win_frame = [window->window frame];
win_frame.size.height = height + _title_bar_height();
win_frame.size.width = width;
[window->window setFrame:win_frame display:YES];
}
void
ecore_cocoa_window_move_resize(Ecore_Cocoa_Window *window,
int x,
int y,
int width,
int height)
{
if (!window)
return;
NSRect win_frame;
if (!window)
return;
win_frame = [window->window frame];
win_frame.size.height = height + _title_bar_height();
win_frame.size.width = width;
win_frame.origin.x = x;
win_frame.origin.y = y;
[window->window setFrame:win_frame display:YES];
}
void
ecore_cocoa_window_title_set(Ecore_Cocoa_Window *window, const char *title)
{
if (!window || !title)
return;
[window->window setTitle:[NSString stringWithUTF8String:title]];
}
void
ecore_cocoa_window_show(Ecore_Cocoa_Window *window)
{
if (!window || [window->window isVisible])
{
printf("Window(%p) is not visible\n", window->window);
return;
}
[window->window makeKeyAndOrderFront:NSApp];
}
void
ecore_cocoa_window_hide(Ecore_Cocoa_Window *window)
{
if (!window || ![window->window isVisible])
return;
[window->window orderOut:NSApp];
}
void
ecore_cocoa_window_borderless_set(Ecore_Cocoa_Window *window,
int on)
{
if (!window)
return;
if (on)
[window->window setContentBorderThickness:0.0
forEdje:NSMinXEdge | NSMinYEdge | NSMaxXEdge | NSMaxYEdge];
}
void
ecore_cocoa_window_view_set(Ecore_Cocoa_Window *window,
void *view)
{
if (!window || !view)
return;
//[[window->window contentView] addSubview:view];
[window->window setContentView:view];
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[view frame]
options:NSTrackingMouseMoved |
NSTrackingActiveInActiveApp |
NSTrackingInVisibleRect
owner:view
userInfo:nil];
[view addTrackingArea:area];
[area release];
}