[96bc8fa] | 1 | /* CrO2 datassette emulator
|
---|
| 2 | * Copyright 2012, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
---|
| 3 | *
|
---|
| 4 | * Distributed under the terms of the MIT licence.
|
---|
[0da6f01] | 5 | */
|
---|
| 6 |
|
---|
| 7 | #include <stdio.h>
|
---|
| 8 | #include <stdlib.h>
|
---|
| 9 | #include <string.h>
|
---|
[d46151a] | 10 | #include <stdint.h>
|
---|
[88642a4] | 11 | #include <unistd.h>
|
---|
| 12 |
|
---|
[5e633b6] | 13 | #include <iup.h>
|
---|
| 14 | #include <iupcontrols.h>
|
---|
| 15 |
|
---|
[96bc8fa] | 16 | #include "device.h"
|
---|
[5e633b6] | 17 |
|
---|
[96bc8fa] | 18 | /* UI */
|
---|
[5e633b6] | 19 | int menu_open(Ihandle* that)
|
---|
| 20 | {
|
---|
| 21 | IupPopup(IupFileDlg(), IUP_CENTER, IUP_CENTER);
|
---|
| 22 | return IUP_DEFAULT;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[96bc8fa] | 25 | int menu_exit(Ihandle* that)
|
---|
| 26 | {
|
---|
| 27 | return IUP_CLOSE;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[5e633b6] | 30 |
|
---|
| 31 | void GUI_open(int* argc, char*** argv)
|
---|
| 32 | {
|
---|
| 33 | IupOpen(argc, argv);
|
---|
| 34 | // IupControlsOpen();
|
---|
| 35 |
|
---|
| 36 | IupSetFunction("OPEN", menu_open);
|
---|
[96bc8fa] | 37 | IupSetFunction("EXIT", menu_exit);
|
---|
[5e633b6] | 38 |
|
---|
| 39 | Ihandle* menu = IupMenu(
|
---|
| 40 | IupSubmenu("File",
|
---|
| 41 | IupMenu(
|
---|
| 42 | IupItem("Open", "OPEN"),
|
---|
[96bc8fa] | 43 | IupItem("Exit", "EXIT"),
|
---|
[5e633b6] | 44 | NULL
|
---|
| 45 | )
|
---|
| 46 | ),
|
---|
| 47 | NULL
|
---|
| 48 | );
|
---|
| 49 |
|
---|
| 50 | Ihandle* platformlist = IupList(NULL);
|
---|
| 51 | IupSetAttribute(platformlist, "EXPAND", "HORIZONTAL");
|
---|
| 52 | IupSetAttribute(platformlist, "DROPDOWN", "YES");
|
---|
| 53 | IupSetAttribute(platformlist, "1", "MO5");
|
---|
| 54 | IupSetAttribute(platformlist, "VALUE", "1");
|
---|
| 55 |
|
---|
| 56 | Ihandle* blocklist = IupTree();
|
---|
| 57 | IupSetAttribute(blocklist, "EXPAND", "VERTICAL");
|
---|
| 58 |
|
---|
| 59 | Ihandle* tabs = IupTabs(
|
---|
| 60 | IupVbox(
|
---|
| 61 | IupLabel("Hello World"),
|
---|
| 62 | NULL
|
---|
| 63 | ),
|
---|
| 64 | IupVbox(
|
---|
| 65 | IupHbox(
|
---|
| 66 | IupLabel("Format:"),
|
---|
| 67 | platformlist,
|
---|
| 68 | NULL
|
---|
| 69 | ),
|
---|
| 70 | IupHbox(
|
---|
| 71 | blocklist,
|
---|
| 72 | IupVbox(
|
---|
| 73 | // IupMatrix(NULL),
|
---|
| 74 | IupLabel("Checksum:"),
|
---|
| 75 | NULL
|
---|
| 76 | ),
|
---|
| 77 | NULL
|
---|
| 78 | )
|
---|
| 79 | ),
|
---|
| 80 | NULL
|
---|
| 81 | );
|
---|
| 82 |
|
---|
| 83 | IupSetAttribute(tabs,"TABTITLE0", "Control");
|
---|
| 84 | IupSetAttribute(tabs,"TABTITLE1", "Explore");
|
---|
| 85 |
|
---|
| 86 | Ihandle* dialog = IupDialog(tabs);
|
---|
| 87 | IupSetAttribute(dialog, "TITLE", "CrO2 tape emulator");
|
---|
| 88 | IupSetAttributeHandle(dialog, "MENU", menu);
|
---|
| 89 | IupShow(dialog);
|
---|
| 90 |
|
---|
| 91 | IupMainLoop();
|
---|
| 92 |
|
---|
| 93 | IupClose();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
[0da6f01] | 97 | int main(int argc, char **argv)
|
---|
| 98 | {
|
---|
[96bc8fa] | 99 | //usb_dev_handle *handle = NULL;
|
---|
[5e633b6] | 100 | unsigned char buffer[275];
|
---|
| 101 | int nBytes = 0;
|
---|
[0da6f01] | 102 |
|
---|
| 103 | if(argc < 2){
|
---|
[5e633b6] | 104 | GUI_open(&argc, &argv);
|
---|
| 105 | exit(0);
|
---|
[0da6f01] | 106 | }
|
---|
[96bc8fa] | 107 |
|
---|
| 108 | Device dev; // Constructor inits communication.
|
---|
| 109 | // TODO handle thrown exceptions.
|
---|
| 110 |
|
---|
[0da6f01] | 111 | if(strcmp(argv[1], "get") == 0){
|
---|
| 112 | memset(buffer, 0, 275);
|
---|
[96bc8fa] | 113 | nBytes = dev.read(buffer, sizeof(buffer));
|
---|
[0da6f01] | 114 | }else if(strcmp(argv[1], "put") == 0){
|
---|
[d46151a] | 115 |
|
---|
[96bc8fa] | 116 | while (dev.getStatus() & 8)
|
---|
[88642a4] | 117 | usleep(1000000);
|
---|
| 118 |
|
---|
[d46151a] | 119 | FILE* fptr = fopen(argv[2], "rb");
|
---|
| 120 | int blockid;
|
---|
| 121 | uint8_t blktype, blksize;
|
---|
| 122 | sscanf(argv[3], "%d", &blockid);
|
---|
| 123 |
|
---|
| 124 | do
|
---|
| 125 | {
|
---|
[88642a4] | 126 | do
|
---|
| 127 | {
|
---|
| 128 | fread(&blktype, 1, 1, fptr);
|
---|
| 129 | if (feof(fptr))
|
---|
| 130 | {
|
---|
| 131 | fprintf(stderr, "end of file.\n");
|
---|
| 132 | fclose(fptr);
|
---|
| 133 | exit(0);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | while(blktype != 0x5A); // skip sync header
|
---|
| 137 |
|
---|
[d46151a] | 138 | fread(&blktype, 1, 1, fptr);
|
---|
| 139 | fread(&blksize, 1, 1, fptr);
|
---|
| 140 | blksize -= 2;
|
---|
| 141 | fread(buffer, 1, blksize + 1, fptr);
|
---|
[88642a4] | 142 | if (blktype == 0)
|
---|
| 143 | {
|
---|
| 144 | // new file
|
---|
| 145 | printf("%.11s\n",buffer);
|
---|
| 146 | }
|
---|
[d46151a] | 147 | }
|
---|
| 148 | while (blockid --);
|
---|
| 149 |
|
---|
| 150 | fclose(fptr);
|
---|
| 151 |
|
---|
[96bc8fa] | 152 | nBytes = dev.write(buffer, blksize, blktype);
|
---|
[0da6f01] | 153 | }else{
|
---|
[5e633b6] | 154 | GUI_open(&argc, &argv);
|
---|
[0da6f01] | 155 | }
|
---|
| 156 |
|
---|
| 157 | if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
|
---|
| 158 | return 0;
|
---|
| 159 | }
|
---|
| 160 |
|
---|