§ 13.11 - akplugobjc.c

// akplugobjc.c
// ------------
// tab stops every 4 spaces

#include <math.h>
#include <string.h>
#include "developer.h"
#include <Cocoa/Cocoa.h>

@interface Plugger : NSObject
{
    BOOL placeholderizer;   // gcc complains if this is empty... hence a useless BOOL. Warnings are...
}                           // ...BAD because even harmless ones can hide real problems in the noise
- (void) bailOut:       (id)sender;
- (void)fauxAction:     (id)sender;
- (void)i_r_a_method:   (struct plug *)pl;
@end
                                                
@implementation Plugger

- (void)bailOut: (id)sender
{
NSEvent* event;

    [NSApp stop: nil];                                          // tell NSApp to stop the runloop

    event = [NSEvent otherEventWithType: NSApplicationDefined   // create empty event
                               location: NSMakePoint(0,0)
                          modifierFlags: 0
                              timestamp: 0.0
                           windowNumber: 0
                                context: nil
                                subtype: 0
                                  data1: 0
                                  data2: 0];

    [NSApp postEvent: event atStart: true];                     // and nudge NSApp so it notices the Stop
}

- (void)fauxAction: (id)sender
{
    [self bailOut: self];                   // this will stop the modal runloop
}

#define WWIDE 200   // Window dimensions
#define WHIGH 200   // ...
#define BWIDE 100   // Button dimensions
#define BHIGH 30    // ...

- (void)i_r_a_method: (struct plug *)pl
{
id window;
NSView *superview;
NSButton *button;
NSRect buttonFrame,labelFrame;
NSTextField *label;
NSAutoreleasePool *pool;

    pool = [[NSAutoreleasePool alloc] init];    // for memory autorelease

    // window
    // ------
    window = [[[NSWindow alloc] initWithContentRect: NSMakeRect(0, 0, WWIDE, WHIGH)
                                          styleMask: NSTitledWindowMask
                                            backing: NSBackingStoreBuffered
                                              defer: NO] autorelease];
    [window cascadeTopLeftFromPoint: NSMakePoint(20,20)];
    [window setTitle: @"Cocoa / obj-c Interface"];
    [window makeKeyAndOrderFront: self];
    superview = [window contentView];

    // label 90% of width, 20% of height, -25% from top
    // ------------------------------------------------
    labelFrame = NSMakeRect(WWIDE*.05,WHIGH - (WHIGH * .35),WWIDE*.9,WHIGH*.3);
    label = [[[NSTextField alloc] initWithFrame: labelFrame] autorelease];
    [label setAlignment: NSCenterTextAlignment];
    [label setFont: [NSFont boldSystemFontOfSize: 16]];
    [label setStringValue: [[[NSString alloc] initWithFormat:@"Brightness\n%6.2f",pl->f[0]] autorelease]];
    [superview addSubview: label];

    // button 50% of width, centered horizontally, BHIGH, starts at 5% of height
    // -------------------------------------------------------------------------
    buttonFrame = NSMakeRect(WWIDE * .5 - BWIDE * .5, WHIGH * .05, BWIDE, BHIGH);
    button = [[[NSButton alloc] initWithFrame: buttonFrame] autorelease]; 
    [button setTitle: @"Click me!"]; 
    [button setBezelStyle: NSRoundedBezelStyle];
    [button setTarget: self];
    [button setAction: @selector(fauxAction:)]; // this is what calls our action method on press
    [superview addSubview: button]; 

    // run
    // ---
    [NSApp runModalForWindow: window];  // loop that only services our window, blocks main app
    [pool release];                     // free autorelease objects on exit
}

@end

// ----- above this line is implementation of Plugger ----------
// -------------------------------------------------------------
// ----- below this line is c interface to objc / Cocoa --------

void lowercfunction(id refToPlugger,struct plug *pl)    // incoming reference to object
{
    [refToPlugger i_r_a_method: pl];        // using ref, invoke i_r_a_method
}

void objcwindow(struct plug *pl)
{
NSAutoreleasePool *pool;
Plugger *t;

    pool = [[NSAutoreleasePool alloc] init];    // yay, a p00l! Let's swim!
    t = [[[Plugger alloc] init] autorelease];   // get ref to Plugger object
    lowercfunction(t,pl);                       // pass that along (so you can see how to do it in c)
    [pool release];                             // what, out of the p00l already? Was it cold?
}

// ----- above this line is c interface to objc / Cocoa --------
// -------------------------------------------------------------
// ----- below this line is the actual plug-in code ------------

void pengine(struct plug *p,long start,long finish,struct pkg *(*service)(struct req *p))   // sets 64K remap
{
long i,v,b;

    switch(p->phase)
    {
        case 0: // setup phase
        {
            objcwindow(p);          // open Cocoa interface
            break;
        }
        case 1:
        {
            b = (p->f[0] - 100.0f) * 655.35f;   // b is now [-65535...65535]
            for (i = start; i < finish; i++)    // we set this range to [0...65536]
            {
                v = i + b;                  // temp brightness adjust;
                if (v < 0) v = 0;           // now limit remap range...
                if (v > 65535) v = 65535;   // ...to acceptable bounds
                p->lr[i] = v;               // install in remap channel
            }
            break;
        }
    }
    
}

void dengine(struct plug *p,long start,long finish,struct pkg *(*service)(struct req *p))   // generates output
{
long i;

    for (i=start; i<finish; i++) // 1-dimensional pixel processing
    {
        p->sr[i] = p->lr[p->mr[i]]; // remap pixel brightness
        p->sg[i] = p->lr[p->mg[i]]; // ...using 64k table in layer
        p->sb[i] = p->lr[p->mb[i]]; // ...
    }
}

void setup(struct plugin *p)            // NOTE: most struct elements are preset to 0,
{                                       // if that is the setting you need, it's there.
#include "ak_pu_date.txt"               // this inserts the current date in the plugin

    p->layer_pri    = 9502;             // layer priority (posit in fx stack)
    strcpy(p->panel.n,"Brightness");    // name appearing on panel
    p->pphase       = 2;                // phases 0 and 1 desired
    p->pthread[0]   = 0;                // phase 0 is interface - no threading
    p->pthread[1]   = 1;                // plugin desires pengine() threading in phase 1
    p->prange_methods[1]= 2;            // custom range for pengine() in 2nd phase
    p->pfinish[1]   = 65536;            // end+1 range for pengine() to process
    p->dthread[1]   = 1;                // plugin desires dengine() threading
    p->promotable   = 1;                // BUT user can add alpha, paint
    p->req_lr       = 65536;            // we'd like 1 layer of this size in lr
    p->slider_req   = 1;                // we'd like 1 slider (#0)
    strcpy(p->sliders[0].n,"Intensity");// legend on the slider
    p->svalues[0]   = 100.0f;           // initial value
    p->zvalues[0]   = 200.0f;           // initial hi value (user settable)
    p->ticks[0]     = 3;                // total tick marks
    p->forder[0]    = 18;               // h position
}                                       // (low values default to 0.0f)

void init(struct plug *pl,struct pkg *(*service)(struct req *p)) // called before pengine() and dengine()
{
struct req r;
struct pkg *p;
char *msg = "objc demo plugin init";

    // the following just demonstrates using the service function
    r.svc = REQ_STATUS;     // request status line message service
    r.p1 = msg;             // set up request with proper parameter
    p = service(&r);        // send msg to status line, get package back
}

void cleanup(struct plug *pl,struct pkg *(*service)(struct req *p)) // called after all pengine() and dengine threads complete
{
struct req r;
struct pkg *p;
char *msg = "objc demo plugin cleanup";
    // the following just demonstrates using the service function
    r.svc = REQ_STATUS;     // request status line message service
    r.p1 = msg;             // set up request with proper parameter
    p = service(&r);        // send msg to status line, get package back
}

Keyboard Navigation
, Previous Page . Next Page t TOC i Index o Operators g Glossary

Valid HTML 4.01 Loose
 

This manual was generated with wtfm
wtfm uses aa_macro and SqLite
aa_macro uses python 2.7
Please consider supporting my dTank (β) development efforts via a small PayPal donation.