Permalink
Browse files

Got the sources working for text and numbers

  • Loading branch information...
dpp committed Apr 18, 2012
1 parent 88c9b4a commit c8219286adb248f798a969a29e300c48af2e1eb8
@@ -7,6 +7,7 @@
//
#import <Cocoa/Cocoa.h>
+#import <objc/runtime.h>
#import "include/VisiBridge.h"
@@ -30,8 +31,10 @@
- (IBAction)runCode:(id) sender;
- (IBAction)grabBool:(id)sender;
- (IBAction)grabText:(id)sender;
+- (IBAction)grabNumber:(id)sender;
- (void) windowWillClose: (NSNotification *) notification;
- (void)runEvent:(visi_event *)event;
+-(void) controlTextDidChange:(NSNotification *)aNotification;
// - (void) findCopyOrRemove: (SourceSinkInfo *) ss;
- (void) layoutControls;
@@ -6,6 +6,7 @@
// Copyright (c) 2012 David Pollak. All rights reserved.
//
+#import <Cocoa/Cocoa.h>
#import "VisiDocument.h"
#import "RunCommandOnMainThread.h"
@@ -17,6 +18,9 @@ void setText(NSTextView *tv, NSString *txt) {
withString:txt];
}
+static char fieldNameKey;
+static char isNumericField;
+
void freeEvent(visi_event *evt) {
switch (evt -> cmd) {
case reportErrorEvent:
@@ -69,47 +73,82 @@ - (IBAction)runCode:(id) sender {
callIntoVisi(&cmd);
}
-/*
-- (IBAction)grabBool:(id)sender {
- NamedSwitch *sw = sender;
- BOOL b = sw.on;
- NSString *ns = sw.name;
- setSourceString([ns cStringUsingEncoding:[NSString defaultCStringEncoding]], BoolSourceType, b ? "t" : "0");
+-(void) controlTextDidChange:(NSNotification *)aNotification {
+ id obj = [aNotification object];
+ if (objc_getAssociatedObject(obj,
+ &isNumericField)) {
+ NSString *sv = [obj stringValue];
+ NSError *error = NULL;
+ NSRegularExpression *regex = [NSRegularExpression
+ regularExpressionWithPattern:@"[-+]?[0-9]*\\.?[0-9]*"
+ options:NSRegularExpressionCaseInsensitive
+ error:&error];
+ NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:sv options:0 range:NSMakeRange(0, [sv length])];
+
+ if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
+
+ NSString *substringForFirstMatch = [sv substringWithRange:rangeOfFirstMatch];
+ [obj setStringValue:substringForFirstMatch];
+ } else {
+ [obj setStringValue:@"0"];
+ }
+
+ [self grabNumber:obj];
+ } else {
+ [self grabText:obj];
+ }
}
+
- (IBAction)grabText:(id)sender {
- NamedText *sw = sender;
- NSString *b = sw.text;
- NSString *ns = sw.name;
- setSourceString([ns cStringUsingEncoding:[NSString defaultCStringEncoding]], [sw delegate] == nil ? StringSourceType : NumberSourceType,
- [b cStringUsingEncoding:[NSString defaultCStringEncoding]]);
+ NSInteger tag = [sender tag];
+ visi_command cmd;
+ cmd.cmd = setSourceCmd;
+ cmd.targetHash = tag;
+ cmd.cmdType = stringVisiType;
+ cmd.cmdInfo.text = [[sender stringValue] UTF8String];
+ id fieldName = objc_getAssociatedObject(sender, &fieldNameKey);
+ if (fieldName) {
+ cmd.cmdTarget = [fieldName UTF8String];
+ callIntoVisi(&cmd);
+ }
}
-*/
-- (IBAction)grabBool:(id)sender {
- /*
- NamedSwitch *sw = sender;
- BOOL b = sw.on;
- NSString *ns = sw.name;
- setSourceString([ns cStringUsingEncoding:[NSString defaultCStringEncoding]], BoolSourceType, b ? "t" : "0");
- */
+- (IBAction)grabNumber:(id)sender {
+ NSInteger tag = [sender tag];
+ visi_command cmd;
+ cmd.cmd = setSourceCmd;
+ cmd.targetHash = tag;
+ cmd.cmdType = doubleVisiType;
+ cmd.cmdInfo.number = [sender doubleValue];
+ id fieldName = objc_getAssociatedObject(sender, &fieldNameKey);
+ cmd.cmdTarget = [fieldName UTF8String];
+ callIntoVisi(&cmd);
}
-- (IBAction)grabText:(id)sender {
-
+- (IBAction)grabBool:(id)sender {
+ NSInteger tag = [sender tag];
+ visi_command cmd;
+ cmd.cmd = setSourceCmd;
+ cmd.targetHash = tag;
+ cmd.cmdType = boolVisiType;
+ cmd.cmdInfo.boolValue = [sender boolValue];
+ id fieldName = objc_getAssociatedObject(sender, &fieldNameKey);
+ cmd.cmdTarget = [fieldName UTF8String];
+ callIntoVisi(&cmd);
}
+
+
- (id)findSink:(NSInteger) hash {
id first = [sourceControls viewWithTag:hash];
if (first) return first;
return [sinkControls viewWithTag:hash];
}
- (void) layoutControls {
- //[self layoutControls: [sourceControls contentView]];
- // [self layoutControls: [sinkControls contentView]];
[self layoutControls: sourceControls];
- [self layoutControls: sinkControls];
+ [self layoutControls: sinkControls];
}
- (void) layoutControls:(id)view {
@@ -162,9 +201,57 @@ - (void)runEvent:(visi_event *)event {
case addSourceEvent:
{
+ int sourceCnt = 0; // sinkControls...
+ NSView *fr = [[NSView alloc] initWithFrame:CGRectMake(0, 30 * sourceCnt, 300, 30)];
+ [sourceControls addSubview:fr];
+ NSTextField *label = [[NSTextField alloc] initWithFrame: CGRectMake(0, 0, 100, 30)];
+ [label setEditable:NO];
+ [label setSelectable:NO];
+ [label setStringValue: [NSString stringWithUTF8String:
+ event -> evtInfo.sourceSinkName]];
+ [fr addSubview:label];
+ id out;
+ switch (event -> eventType) {
+ case doubleVisiType:
+ {
+ out = [[NSTextField alloc] initWithFrame:CGRectMake(100, 0, 200, 30)];
+ [out setDoubleValue:0];
+ [out setTarget:self];
+ [out setAction:@selector(grabNumber:)];
+ [out setDelegate:self];
+ objc_setAssociatedObject (out,
+ &isNumericField,
+ @"true",
+ OBJC_ASSOCIATION_RETAIN);
+ }
+ break;
+
+ case stringVisiType:
+ {
+ out = [[NSTextField alloc] initWithFrame:CGRectMake(100, 0, 200, 30)];
+ [out setTarget:self];
+ [out setAction:@selector(grabText:)];
+ [out setDelegate:self];
+
+ }
+ break;
+
+ case boolVisiType:
+ {
+
+ }
+ break;
+ }
+ objc_setAssociatedObject (out,
+ &fieldNameKey,
+ [NSString stringWithUTF8String:
+ event -> evtInfo.sourceSinkName],
+ OBJC_ASSOCIATION_RETAIN);
+ [out setTag:event -> targetHash];
+ [fr addSubview:out];
+ [self layoutControls];
}
- // FIXME add source
break;
case addSinkEvent: {
@@ -10,6 +10,7 @@ import Visi.Runtime
import Visi.Expression
import Visi.Model
import Visi.Util
+import Data.Bits
#include "VisiBridge.h"
@@ -45,11 +46,17 @@ getUnpacked what =
return ret
-getTarget :: VoidPtr -> IO Int
+getTarget :: VoidPtr -> IO T.Text
getTarget what = do
- cint <- {#get visi_command->targetHash#} what
- return $ fromIntegral cint
+ targetPtr <- {#get visi_command->cmdTarget#} what
+ peekText targetPtr
+mask = 0x7ffffffffff
+
+findHash model val =
+ let testV = val .&. mask in
+ let test a = vtrace ("Testing " ++ (show a) ++ " masked " ++ (show (a .&. mask)) ++ " against " ++ (show testV)) $ (a .&. mask) == testV in
+ stringFromHash model test
doError objcId err =
do
@@ -62,16 +69,15 @@ doError objcId err =
{#call sendEvent #} objcId ptr
-
-
doSourceSink :: VoidPtr -> [SourceSinkAction] -> IO ()
doSourceSink objcId sourceSinkInfo =
do
let addIt name cmd tpe = do
ptr <- mallocBytes {#sizeof visi_event #}
name' <- newString name
{#set visi_event->evtInfo.sourceSinkName #} ptr name'
- {#set visi_event->targetHash #} ptr $ fromIntegral $ intHash name
+ putStrLn $ "Setting source " ++ (show name) ++ " hash " ++ (show $ (intHash name) .&. mask)
+ {#set visi_event->targetHash #} ptr $ fromIntegral $ ((intHash name) .&. mask)
{#set visi_event->cmd #} ptr $ enumMe cmd
{#set visi_event->eventType #} ptr $ enumMe tpe
{#call sendEvent #} objcId ptr
@@ -83,12 +89,12 @@ doSourceSink objcId sourceSinkInfo =
let doAction (RemoveSourceAction name) = do
ptr <- mallocBytes {#sizeof visi_event #}
{#set visi_event->cmd #} ptr $ enumMe RemoveSourceEvent
- {#set visi_event->targetHash #} ptr $ fromIntegral $ intHash name
+ {#set visi_event->targetHash #} ptr $ fromIntegral $ ((intHash name) .&. mask)
{#call sendEvent #} objcId ptr
doAction (RemoveSinkAction name) = do
ptr <- mallocBytes {#sizeof visi_event #}
{#set visi_event->cmd #} ptr $ enumMe RemoveSinkEvent
- {#set visi_event->targetHash #} ptr $ fromIntegral $ intHash name
+ {#set visi_event->targetHash #} ptr $ fromIntegral $ ((intHash name) .&. mask)
{#call sendEvent #} objcId ptr
doAction (AddSourceAction src theType) = figureType src AddSourceEvent theType
doAction (AddSinkAction src theType) = figureType src AddSinkEvent theType
@@ -101,7 +107,7 @@ doSetSinks objcId nvp = do
(Just (TPrim prim)) -> do
ptr <- mallocBytes {#sizeof visi_event #}
{#set visi_event->cmd#} ptr $ enumMe SetSinkEvent
- {#set visi_event->targetHash#} ptr $ fromIntegral $ intHash name
+ {#set visi_event->targetHash#} ptr $ fromIntegral $ ((intHash name) .&. mask)
case (prim, value) of
(PrimDouble, DoubleValue dv) -> do
{#set visi_event->evtValue.number#} ptr $ realToFrac dv
@@ -117,8 +123,8 @@ doSetSinks objcId nvp = do
_ -> return ()
mapM_ sendIt nvp
-convertFromC :: Model a -> VoidPtr -> IO (Maybe VisiCommand)
-convertFromC model what =
+convertFromC :: VoidPtr -> IO (Maybe VisiCommand)
+convertFromC what =
do
cmd <- getCommandCmd what
cmdType' <- {#get visi_command->cmdType#} what
@@ -129,16 +135,16 @@ convertFromC model what =
return $ Just $ SetProgramText theStr
SetSourceCmd | cmdType == StringVisiType -> do
theStr <- getUnpacked $ {#get visi_command->cmdInfo.text #} what
- targetHash <- getTarget what
- return $ fmap (\target -> SetValueSource target $ StrValue theStr) $ stringFromHash model targetHash
+ target <- getTarget what
+ return $ Just $ SetValueSource target $ StrValue theStr
SetSourceCmd | cmdType == DoubleVisiType -> do
cNum <- {#get visi_command->cmdInfo.number #} what
- targetHash <- getTarget what
- return $ fmap (\target -> SetValueSource target $ DoubleValue $ realToFrac cNum) $ stringFromHash model targetHash
+ target <- getTarget what
+ return $ Just $ SetValueSource target $ DoubleValue $ realToFrac cNum
SetSourceCmd | cmdType == BoolVisiType -> do
bool <- ({#get visi_command->cmdInfo.boolValue #} what)
- targetHash <- getTarget what
- return $ fmap (\target -> SetValueSource target $ BoolValue (bool /= 0)) $ stringFromHash model targetHash
+ target <- getTarget what
+ return $ Just $ SetValueSource target $ BoolValue (bool /= 0)
StopRunningCmd -> return $ Just StopRunning
_ -> return $ Nothing
@@ -64,11 +64,13 @@ foreign export ccall startProcess :: VoidPtr -> IO (FunPtr (VoidPtr -> IO ()))
startProcess objcId =
do
(model, handler) <- runApp $ AppCallback (doError objcId) (doSourceSink objcId) (doSetSinks objcId)
- wrapIt $ handleMsg model handler
+ wrapIt $ handleMsg handler
-handleMsg model handler msg =
+
+
+handleMsg handler msg =
do
- myMsg <- convertFromC model msg
+ myMsg <- convertFromC msg
case myMsg of
Just myMsg' -> handler myMsg'
_ -> return ()
@@ -22,6 +22,7 @@
*
* ***** END LICENSE BLOCK *****/
+#define HashType long
enum cmds {
setProgramTextCmd,
@@ -36,7 +37,8 @@ typedef struct {
int boolValue;
double number;
} cmdInfo;
- int targetHash;
+ HashType targetHash;
+ const char *cmdTarget;
int cmdType;
} visi_command;
@@ -61,7 +63,7 @@ typedef struct
char *errorText;
char *sourceSinkName;
} evtInfo;
- int targetHash;
+ HashType targetHash;
union {
char *text;
int boolValue;
View
@@ -192,12 +192,13 @@ runSinkActions names model =
let actions = collectSinkActions names model
mapM_ passthru actions
-stringFromHash :: Model a -> Int -> Maybe T.Text
-stringFromHash model num =
+stringFromHash :: Model a -> (Int -> Bool) -> Maybe T.Text
+stringFromHash model func =
let sourceNames = Map.keys $ sources model in
let sinkNames = Map.keys $ sinks model in
- let found = filter (\a -> num == intHash a) (sourceNames ++ sinkNames) in
- case found of
+ let names = sourceNames ++ sinkNames in
+ let found = filter (\a -> func $ intHash a) names in
+ case vtrace ("names " ++ show names ++ " Found " ++ show found) found of
a:_ -> Just a
_ -> Nothing
Oops, something went wrong.

0 comments on commit c821928

Please sign in to comment.