Skip to content

Commit

Permalink
feat: populate session list with real data
Browse files Browse the repository at this point in the history
- This is the first UI component that is driven by real data from Brewers friend
- There is still some stuff to be worked out like dragging the window but it's a good milestone
  • Loading branch information
NoxHarmonium committed Apr 2, 2021
1 parent 0a4bf2d commit 8480be6
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 24 deletions.
14 changes: 13 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
"mbconstants.h": "c",
"mbsessionlist.h": "c",
"typeinfo": "cpp",
"mbwsplash.h": "c"
"mbwsplash.h": "c",
"optional": "c",
"istream": "c",
"ostream": "c",
"system_error": "c",
"array": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"mbdatamanager.h": "c",
"mbtypes.h": "c",
"string": "cpp",
"cstring": "c"
}
}
Binary file modified macbrew-ui/MacBrewUI.bin
Binary file not shown.
Binary file modified macbrew-ui/MacBrewUI.rsrc.bin
Binary file not shown.
35 changes: 30 additions & 5 deletions macbrew-ui/macbrew.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

extern WindowPtr splashWindow;
// extern Rect dragRect;
extern WindowPtr sessionListWindow;

void InitMacintosh(void);
void HandleMouseDown(EventRecord *theEvent);
Expand Down Expand Up @@ -40,15 +41,30 @@ void HandleMouseDown(EventRecord *theEvent)
break;

case inDrag:
// if (theWindow == mbWindow)
// DragWindow(mbWindow, theEvent->where, &dragRect);
// TODO: Why isn't drag working?
if (theWindow == sessionListWindow)
{
DragWindow(sessionListWindow, theEvent->where, &(*GetGrayRgn())->rgnBBox);
}
break;

case inContent:
if (theWindow == splashWindow)
{
DestroySplashWindow();
}
if (theWindow == sessionListWindow)
{
if (sessionListWindow != FrontWindow())
{
SelectWindow(sessionListWindow);
}
else
{
InvalRect(&sessionListWindow->portRect);
}
SessionListMouseDown(*theEvent);
}
break;

case inGoAway:
Expand Down Expand Up @@ -84,16 +100,25 @@ void HandleEvent(void)
break;

case updateEvt:
// BeginUpdate(splashWindow);
//Draw stuff here
// EndUpdate(splashWindow);
if (sessionListWindow != NULL)
{
BeginUpdate(sessionListWindow);
SessionListUpdate();
EndUpdate(sessionListWindow);
}

break;

case activateEvt:
if (splashWindow != NULL)
{
InvalRect(&splashWindow->portRect);
}
if (sessionListWindow != NULL)
{
SessionListActivate((theEvent.modifiers & activeFlag) != 0);
InvalRect(&sessionListWindow->portRect);
}
break;
}
}
Expand Down
119 changes: 117 additions & 2 deletions macbrew-ui/mbDataManager.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,102 @@
#include "mbTypes.h"
#include "mbDataManager.h"
#include "mbSerial.h"
#include "mbUtil.h"
#include <string.h>

// Response Structure
// -----------------
// Note: Response is in big endian to match m68k processor of the Mac
// 2 bytes message length (discarded by serial routine and stored in response struct)
// Response ID (2 bytes length (not including terminator), then n characters)
// One byte to indicate success (0 or 1)
// <PAYLOAD>
// Four bytes checksum (does not include initial 2 bytes for message length)

// Payload Structure
// -----------------
// Array: 2 bytes length, then each element packed together
// String: 2 bytes length, then n characters (no terminator)

void InitReader(ResponseReader *reader, SerialResponse *responseData)
{
reader->cursor = 0;
reader->response = responseData;
}

void ReadBool(ResponseReader *reader, Boolean *outBoolean)
{
char value = GetCharFromBuffer(*reader->response->data, reader->cursor);
reader->cursor += 1; //sizeof(Boolean);
*outBoolean = value;
}

void ReadUnsignedShort(ResponseReader *reader, unsigned short *outShort)
{
unsigned short value = GetShortFromBuffer(*reader->response->data, reader->cursor);
reader->cursor += sizeof(unsigned short);
*outShort = value;
}

void ReadString(ResponseReader *reader, unsigned char *outString)
{
unsigned short length = 0;
Ptr buffer = *(reader->response->data);

ReadUnsignedShort(reader, &length);

if (length > 254)
{
Panic("\pCannot read strings larger than 254 characters at this time");
}
outString[0] = (char)length;

memcpy(outString + 1, (char *)buffer + reader->cursor, length);

reader->cursor += length;
}

void ReadSequence(ResponseReader *reader, Sequence *outSequence)
{
unsigned short length;
ReadUnsignedShort(reader, &length);
outSequence->size = length;
// A handle of handles
outSequence->elements = (Handle *)NewPtr(length * sizeof(Handle));
}

void ReadBrewSessionReference(ResponseReader *reader, Handle *outHandle)
{
Str255 x;
int s = sizeof(BrewSessionReference);
Handle handle = NewHandle(s);
BrewSessionReference *brewSessionReference = (BrewSessionReference *)*handle;
char *tmp = NewPtr(255);

sprintf(tmp, "size: %i", s);
//CShowAlert(tmp);

ReadString(reader, brewSessionReference->id);
ReadString(reader, brewSessionReference->batch_code);
ReadString(reader, brewSessionReference->name);

*outHandle = handle;
}

void ValidateResponse(ResponseReader *reader)
{
Boolean success;
Str255 responseId;
ReadString(reader, responseId);
ReadBool(reader, &success);

// TODO: Check that response ID matches request ID
if (!success)
{
// TODO: Read error message
Panic("\pResponse from MacBrew proxy indicates an error occurred");
}
}

void Ping()
{
Expand All @@ -13,14 +110,32 @@ void Ping()
DisposeResponse(&responseData);
}

void FetchBrewSessionReferences(Handle *sessionReferences)
void FetchBrewSessionReferences(Sequence **outSessionReferences)
{

SerialResponse *responseData;
ResponseReader reader;
Sequence *sessionReference = (Sequence *)NewPtr(sizeof(Sequence));
short i;

SetUpSerial();
SendCommand("1 LIST SESSION\r");
ReadResponse(&responseData);
TearDownSerial();

DisposeResponse(&responseData);
InitReader(&reader, responseData);

ValidateResponse(&reader);

ReadSequence(&reader, sessionReference);
for (i = 0; i < sessionReference->size; i++)
{
ReadBrewSessionReference(&reader, &sessionReference->elements[i]);
}

*outSessionReferences = sessionReference;

// TODO: Disposing!

//DisposeResponse(&responseData);
}
9 changes: 2 additions & 7 deletions macbrew-ui/mbDataManager.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
typedef struct BrewSessionReference
{
Str255 id;
Str255 batch_code;
Str255 name;
} BrewSessionReference;
struct Sequence;

void Ping();
void FetchBrewSessionReferences(Handle *sessionReferences);
void FetchBrewSessionReferences(Sequence **outSessionReferences);
7 changes: 4 additions & 3 deletions macbrew-ui/mbMenus.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "mbMenus.h"
#include "mbTypes.h"
#include "mbDataManager.h"
#include "mbWSessionList.h"

Expand Down Expand Up @@ -41,7 +42,7 @@ void HandleMenu(long mSelect)
GrafPtr savePort;
WindowPeek frontWindow;
char *command;
Handle sessionReferences;
Sequence *sessionReferences;

switch (menuID)
{
Expand All @@ -60,9 +61,9 @@ void HandleMenu(long mSelect)
break;
case listSessionsItem:
SetUpSessionListWindow();

FetchBrewSessionReferences(&sessionReferences);

UpdateSessionListWindow(sessionReferences);

break;
case quitItem:
ExitToShell();
Expand Down
10 changes: 7 additions & 3 deletions macbrew-ui/mbSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
*
****/

struct SerialResponse
typedef struct SerialResponse
{
Handle data;
unsigned int length;
};
} SerialResponse;

typedef struct SerialResponse SerialResponse;
typedef struct ResponseReader
{
SerialResponse *response;
int cursor;
} ResponseReader;

OSErr SendCommand(char *command);
OSErr ReadResponse(SerialResponse **outResponse);
Expand Down
12 changes: 12 additions & 0 deletions macbrew-ui/mbTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
typedef struct BrewSessionReference
{
Str255 id;
Str255 batch_code;
Str255 name;
} BrewSessionReference;

typedef struct Sequence
{
unsigned short size;
Handle *elements;
} Sequence;
7 changes: 7 additions & 0 deletions macbrew-ui/mbUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ unsigned short GetShortFromBuffer(char *buffer, int offset)
return (b0 << 8) | b1;
}

unsigned char GetCharFromBuffer(char *buffer, int offset)
{
unsigned char b0;
b0 = buffer[offset];
return b0;
}

void Panic(Str255 message)
{
ShowAlert(message);
Expand Down
3 changes: 2 additions & 1 deletion macbrew-ui/mbUtil.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
unsigned long GetLongFromBuffer(char *buffer, int offset);
unsigned short GetShortFromBuffer(char *buffer, int offset);
unsigned char GetCharFromBuffer(char *buffer, int offset);
void Panic(Str255 message);
void ShowAlert(Str255 message);
void CShowAlert(char *message);
void CShowAlert(char *message);
Loading

0 comments on commit 8480be6

Please sign in to comment.