Advertisement
Guest User

crashing window code for Amiga

a guest
Sep 20th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.14 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdlib.h>
  3. #include <proto/dos.h>
  4. #include <proto/exec.h>
  5. #include <stdio.h>
  6.  
  7. #include <string.h>
  8. #include <exec/memory.h>
  9. #include <machine/limits.h>
  10. #include <libraries/filehandler.h>
  11.  
  12. /*added for colours*/
  13. #include <proto/intuition.h>
  14. #include <proto/graphics.h>
  15. #include <proto/gadtools.h>
  16. #include <proto/layers.h>
  17.  
  18. #define MYTEXT_LEFT (10)
  19. #define MYTEXT_TOP (10)
  20.  
  21. /*window*/
  22.  
  23. struct Window *mywindow;
  24.  
  25. /*text for window*/
  26.  
  27. struct DrawInfo *drawinfo;
  28. struct IntuiText myIText;
  29. struct TextAttr myTextAttr;
  30. ULONG myTEXTPEN;
  31. ULONG myBACKGROUNDPEN;
  32.  
  33. struct Region *win_reg;
  34. struct Region *win_oldreg;
  35. struct Rectangle win_rect;
  36.  
  37. unsigned long oldwin_width, oldwin_height;
  38.  
  39. int numcolours=0, white=1, black=2;
  40.  
  41. int colour_red=1,colour_green=1,colour_yellow=1; /*in real code calculated to be reddest, greenest etc colour*/
  42. int loop_again=0;
  43. struct IntuiText Ftext=
  44. {
  45. 1,0, /*front pen, backpen*/
  46. JAM2, /*draw mode*/
  47. 15,0, /*left edge, top edge*/
  48. NULL, /*font standard*/
  49. (UBYTE*) "System programming on the Amiga",
  50. NULL /*Next text*/
  51. };
  52.  
  53. struct Screen *screen;
  54. unsigned char screenName[] = "Workbench";
  55. unsigned long colorTable[256 *3 ];
  56. struct ViewPort *viewPort;
  57. struct ColorMap *colorMap;
  58. unsigned long red,green,blue;
  59.  
  60. struct RastPort *mywindowrastport;
  61. unsigned char *win_buffer; /*used for lines*/
  62. unsigned char *win_buffer_part; /*used for colours*/
  63. static unsigned int win_buffer_len;
  64.  
  65.  
  66. struct window_keeper
  67. {
  68. struct window_keeper *next;
  69. char* line;
  70. int full;
  71. int linetype;
  72. unsigned int lineno;
  73. };
  74.  
  75. struct window_keeper *winhead, *wintail, *wintemp;
  76.  
  77. static unsigned int winline=0;
  78. static unsigned int maxlinelen=0;
  79.  
  80. /* example output which is generated in real code*/
  81.  
  82. static char *lines[]={"Unit DOS Size Used Free Full Errors Status Name",\
  83. "SMBFS0: DOS0 488Gb 223Gb 264Gb 45% 0 Read & Write PCbackup",\
  84. "PC0: MSD0 720Kb 11Kb 709Kb 1% 0 Read only EMPTY",\
  85. "CF0: No disk in drive",\
  86. "RAM: DOS0 702Kb 702Kb 121Mb 0% 0 Read & Write Ram Disk",\
  87. "DH0: PFS3 94Mb 93Mb 652Kb 99% 0 Read & Write WB",\
  88. "DF0: PC Disk",\
  89. "CC0: No disk in drive",\
  90. "DF1: DOS0 837Kb 789Kb 48Kb 94% 0 Read & Write EdWordPro_2",\
  91. "DH1: PFS3 3471Mb 3354Mb 117Mb 96% 0 Read & Write Games",\
  92. "DH2: PFS3 3567Mb 3487Mb 80Mb 97% 0 Read & Write Aps",\
  93. "Available Volumes:",\
  94. "PCbackup Mounted",\
  95. "EMPTY Mounted",\
  96. "Ram Disk Mounted",\
  97. "Games1 Mounted",\
  98. "Aps Mounted",\
  99. "EdWordPro_2 Mounted",\
  100. "WB Mounted"};
  101.  
  102.  
  103. int findchar(char string[], char letter)
  104. {
  105. int result=-1;
  106. int i=0;
  107. while (string[i]!='\0')
  108. {
  109. if (string[i]==letter)
  110. result=i;
  111. i++;
  112. }
  113. return result;
  114. }
  115.  
  116. int midstr(char str1[], char str2[],int start,int last)
  117. {
  118. int count,count2;
  119. count=start;count2=0;
  120. if (last!=NULL)
  121. {
  122. while ((str2[count]!='\0') && (count<=last))
  123. {
  124. str1[count2]=str2[count];
  125. count++;
  126. count2++;
  127. }
  128. }
  129. else
  130. {
  131. while ((str2[count]!='\0'))
  132. {
  133. str1[count2]=str2[count];
  134. count++;
  135. count2++;
  136. }
  137.  
  138. }
  139. str1[count2]='\0';
  140. return count2;
  141. }
  142.  
  143. int leftstr(char str1[],char str2[],int last)
  144. {
  145. return midstr(str1,str2,0,last);
  146. }
  147.  
  148. void clearstr(char str1[],unsigned int last)
  149. {
  150. unsigned int i=0;
  151.  
  152. for (i=0;i<=last;i++)
  153. str1[i]='\0';
  154.  
  155. }
  156.  
  157.  
  158. long finddoscol(char str[])
  159. {
  160. int i=0;
  161.  
  162. while((str[i]!='\0') && (str[i]!=' '))
  163. i++;
  164.  
  165. if (str[i]=='\0')
  166. return i;
  167.  
  168. while ((str[i]==' ') && (str[i]!='\0'))
  169. i++;
  170.  
  171. return i;
  172. }
  173.  
  174. int min(int a,int b)
  175. {
  176. if (a<b)
  177. return a;
  178. else if (b<a)
  179. return b;
  180.  
  181. return a;
  182. }
  183.  
  184. int max(int a,int b)
  185. {
  186. if (a>b)
  187. return a;
  188. else if (b>a)
  189. return b;
  190.  
  191. return a;
  192. }
  193.  
  194. int open_window(int scrnwidth,int scrnheight)
  195. {
  196. int topleftx=0,toplefty=0;
  197. int width=200,height=200;
  198. int tempw=0,temph=0;
  199.  
  200. unsigned long maxchars=0;
  201.  
  202. struct TextFont *font;
  203. UWORD font_width=0;
  204.  
  205. /*calculate 1 quarter of screen width*/
  206.  
  207. if (mywindow)
  208. return 0; /*opened already*/
  209.  
  210. tempw=scrnwidth/4;
  211.  
  212. /*calculate 1 5th screen height*/
  213.  
  214. temph=scrnheight/5;
  215.  
  216. topleftx=tempw;
  217. toplefty=temph;
  218.  
  219. width=scrnwidth-(tempw*2);
  220. height=scrnheight-(temph*2);
  221.  
  222. mywindow = OpenWindowTags(NULL, WA_Left, topleftx, WA_Top, toplefty,\
  223. WA_InnerWidth, width, WA_InnerHeight, height,\
  224. WA_MinWidth,90,WA_MinHeight, 40, WA_MaxWidth, scrnwidth-20, WA_MaxHeight, scrnheight-20,\
  225. WA_IDCMP,IDCMP_CLOSEWINDOW | IDCMP_DISKINSERTED | IDCMP_DISKREMOVED | IDCMP_NEWSIZE | IDCMP_REFRESHWINDOW,\
  226. WA_Flags, WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_ACTIVATE,\
  227. WA_Title, (ULONG)"Info64", WA_PubScreenName, (ULONG)"WorkBench", TAG_DONE);
  228.  
  229. if (mywindow)
  230. {
  231. myIText.FrontPen = myTEXTPEN;
  232. myIText.BackPen = myBACKGROUNDPEN;
  233. myIText.DrawMode = JAM2;
  234. myIText.LeftEdge = MYTEXT_LEFT;
  235. myIText.TopEdge = MYTEXT_TOP;
  236. myIText.ITextFont = &myTextAttr;
  237. myIText.IText = "Hello, World. ;-)";
  238. myIText.NextText = NULL;
  239. mywindowrastport=mywindow->RPort;
  240.  
  241. oldwin_width=mywindow->Width;
  242. oldwin_height=mywindow->Height;
  243.  
  244. /*open line buffers*/
  245.  
  246. font=mywindow->RPort->Font;
  247. font_width=font->tf_XSize;
  248.  
  249. maxchars=(scrnwidth/font_width)+1;
  250.  
  251. win_buffer=malloc((maxchars+2)*sizeof(char)); /*make memory buffers for line plus null char*/
  252. win_buffer_part=malloc((maxchars+2)*sizeof(char));
  253. win_buffer_len=maxchars;
  254.  
  255. win_rect.MinX = mywindow->BorderLeft;
  256. win_rect.MinY = mywindow->BorderTop;
  257. win_rect.MaxX = (mywindow->Width-mywindow->BorderRight)-3;
  258. win_rect.MaxY = (mywindow->Height-mywindow->BorderBottom)-3;
  259. if (win_reg = NewRegion())
  260. {
  261. OrRectRegion(win_reg, &win_rect);
  262. win_oldreg = InstallClipRegion(mywindow->WLayer, win_reg);
  263. }
  264. return 0;
  265. }
  266. else
  267. return 1;
  268. }
  269.  
  270. int write_win()
  271. {
  272. /* struct textattr TextAttr;*/
  273. struct TextFont *font;
  274. UWORD font_width=0;
  275. long win_width=0,win_height=0;
  276. long line_len=0,doscol=0,currentdoscol;
  277. long numpix=0, avpix=0, fitchar=0,tempfitchar=0;
  278. struct window_keeper *currentline=NULL;
  279. unsigned int charpos=0, percentpos=0;
  280.  
  281.  
  282.  
  283. int zz=0;
  284. Forbid();
  285.  
  286. win_width=mywindow->Width-mywindow->BorderRight;
  287. win_height=mywindow->Height-mywindow->BorderBottom;
  288.  
  289. /*reset buffers*/
  290. clearstr((char *)win_buffer,win_buffer_len-1);
  291. clearstr((char *)win_buffer_part,win_buffer_len-1);
  292.  
  293. font=mywindow->RPort->Font;
  294. font_width=font->tf_XSize;
  295.  
  296.  
  297. /*find how many chars will fit into window*/
  298. numpix=font_width;
  299.  
  300. fitchar=(win_width/numpix) -4; /* -4 for safety*/
  301.  
  302. printf("number of pix %d width %d max=%d\n",numpix,win_width,fitchar);
  303.  
  304.  
  305. if (fitchar==0) /*cant get any so give up*/
  306. {
  307. Permit();
  308. return 0;
  309. }
  310.  
  311. if (winhead) /*if we have something*/
  312. {
  313.  
  314. if (currentline==NULL)
  315. currentline=winhead;
  316. }
  317. else
  318. return 0; /*nothing saved!*/
  319.  
  320. while ((currentline) && ((10*currentline->lineno)<win_height)) /*now dump saved text rough attempt stopping at end of data or window*/
  321. {
  322. printf("line no%d type %d\n",currentline->lineno,currentline->linetype);
  323. switch (currentline->linetype)
  324. {
  325. case 1:
  326. myIText.FrontPen = white; /*<<<< calculated to be whitest in real code*/
  327. doscol=finddoscol(currentline->line);
  328. break;
  329.  
  330. case 2:
  331. charpos=findchar(currentline->line,':');
  332. printf("charpos=%d",charpos);
  333. currentdoscol=finddoscol(currentline->line);
  334. if (charpos!=-1)
  335. {
  336.  
  337. myIText.FrontPen = white;
  338. leftstr((char*)win_buffer_part,(char*)currentline->line,charpos);
  339.  
  340. printf(" win_buffer_part -%s-\n",(char*)win_buffer_part);
  341.  
  342. myIText.IText=win_buffer_part;
  343. PrintIText(mywindow->RPort,&myIText,10,10*currentline->lineno);
  344.  
  345. printf("OK - %s\n",win_buffer_part);
  346. /*now the rest*/
  347.  
  348. myIText.FrontPen = black;
  349. midstr(win_buffer,currentline->line,currentdoscol,strlen(currentline->line)); /*tempfitchar-(currentdoscol));*/
  350.  
  351. /*now look for %*/
  352.  
  353. percentpos=findchar(currentline->line,'%');
  354. printf("percentpos=%d ",percentpos);
  355. if (percentpos!=-1)
  356. {
  357. percentpos=percentpos-3;
  358. /*dump up to percent*/
  359.  
  360. midstr(win_buffer_part,currentline->line,currentdoscol,percentpos);
  361. myIText.IText=win_buffer_part;
  362.  
  363. printf(" before percent win_buffer_part -%s-\n",win_buffer_part);
  364.  
  365. PrintIText(mywindow->RPort,&myIText,10+(font_width*doscol),10*currentline->lineno);
  366. printf("OK - %s\n",win_buffer_part);
  367. /*colour for percent*/
  368.  
  369. myIText.FrontPen = white;
  370. midstr(win_buffer_part,currentline->line,percentpos,percentpos+3);
  371. myIText.IText=win_buffer_part;
  372.  
  373.  
  374. printf("percent win_buffer_part -%s-\n",win_buffer_part);
  375.  
  376. if (currentline->full>90)
  377. myIText.FrontPen = max(1,colour_red);
  378. else if (currentline->full <40)
  379. {
  380. printf("GREEN=%d\n",colour_green);
  381. myIText.FrontPen = max(1,colour_green);
  382. }
  383. else
  384. {
  385. printf("YELLOW=%d\n",colour_yellow);
  386. myIText.FrontPen = max(1,colour_yellow);
  387. }
  388.  
  389. PrintIText(mywindow->RPort,&myIText,10+(font_width*percentpos),10*currentline->lineno);
  390. printf("OK - %s\n",win_buffer_part);
  391.  
  392. myIText.FrontPen = black;
  393. /*set up to print remainder - do we need to check size?*/
  394. printf("OK set colour to black\n");
  395. midstr(win_buffer,currentline->line,percentpos+4,strlen(currentline->line));
  396. printf("OK midstr(win_buffer,currentline->line,percentpos+4\n");
  397. myIText.IText=win_buffer;
  398. printf("OK set myIText to win_buffer\n");
  399. printf(" after percent win_buffer -%s-\n",win_buffer);
  400.  
  401.  
  402. charpos=percentpos+3;
  403. PrintIText(mywindow->RPort,&myIText,10+(font_width*(charpos+1)),10*currentline->lineno);
  404. printf("OK - %s\n",win_buffer);
  405.  
  406.  
  407. }
  408. else
  409. {
  410. /*now print out*/
  411. printf("no percent-win_buffer -%s%\n",win_buffer);
  412. myIText.IText=win_buffer;
  413. PrintIText(mywindow->RPort,&myIText,10+(font_width*doscol),10*currentline->lineno);
  414. printf("OK\n");
  415. }
  416.  
  417. }
  418. else /* cant find so dump*/
  419. {
  420. myIText.FrontPen = black;
  421. myIText.IText=currentline->line;
  422.  
  423. PrintIText(mywindow->RPort,&myIText,10,10*currentline->lineno);
  424. }
  425. break;
  426.  
  427. case 3:
  428. myIText.FrontPen = white;
  429. break;
  430.  
  431. default:
  432. myIText.FrontPen = black;
  433. break;
  434. }
  435.  
  436. if (currentline->linetype!=2)
  437. {
  438. myIText.IText=currentline->line;
  439.  
  440. PrintIText(mywindow->RPort,&myIText,10,10*currentline->lineno);
  441. }
  442.  
  443. currentline=currentline->next;
  444.  
  445. }
  446.  
  447. Permit();
  448. return 0;
  449. }
  450.  
  451. /*only refresh the bits of the window we need to*/
  452.  
  453. int refresh_window()
  454. {
  455. printf("START REFRESH\n");
  456. if ((oldwin_width!=mywindow->Width) || (oldwin_height!=mywindow->Height))
  457. {
  458. BeginRefresh(mywindow);
  459.  
  460. write_win();
  461.  
  462. EndRefresh(mywindow,TRUE);
  463. }
  464. printf("END REFRESH\n");
  465. return 0;
  466. }
  467.  
  468. /*handle window messages*/
  469.  
  470. BOOL handleIDCMP(struct Window *win, BOOL done)
  471. {
  472. struct IntuiMessage *message;
  473. USHORT code;
  474. SHORT mousex, mousey;
  475. ULONG class;
  476.  
  477. printf("inside message handler\n");
  478.  
  479. while (NULL != (message = (struct IntuiMessage *)GetMsg(win->UserPort)))
  480. {
  481.  
  482. printf("Message handler!!!\n");
  483.  
  484.  
  485. class = message->Class;
  486. code = message->Code;
  487. mousex = message->MouseX;
  488. mousey = message->MouseY;
  489.  
  490. ReplyMsg((struct Message *)message);
  491.  
  492. /* The class contains the IDCMP type of the message. */
  493.  
  494. switch (class)
  495. {
  496. case IDCMP_CLOSEWINDOW:
  497. done = TRUE;
  498. break;
  499.  
  500. case IDCMP_VANILLAKEY:
  501. printf("IDCMP_VANILLAKEY (%lc)\n",code);
  502. break;
  503.  
  504. case IDCMP_RAWKEY:
  505. printf("IDCMP_RAWKEY\n");
  506. break;
  507.  
  508. case IDCMP_DISKINSERTED:
  509. loop_again=TRUE;
  510. break;
  511.  
  512. case IDCMP_DISKREMOVED:
  513. loop_again=TRUE;
  514. break;
  515.  
  516.  
  517. case IDCMP_NEWSIZE:
  518. if (win_reg)
  519. {
  520. InstallClipRegion(mywindow->WLayer, win_oldreg);
  521. DisposeRegion(win_reg);
  522.  
  523. win_rect.MinX = mywindow->BorderLeft;
  524. win_rect.MinY = mywindow->BorderTop;
  525. win_rect.MaxX = mywindow->Width-mywindow->BorderRight-1;
  526. win_rect.MaxY = mywindow->Height-mywindow->BorderBottom-1;
  527. if (win_reg = NewRegion())
  528. {
  529. OrRectRegion(win_reg, &win_rect);
  530. win_oldreg = InstallClipRegion(mywindow->WLayer, win_reg);
  531. }
  532. }
  533.  
  534. /*fall through to refresh*/
  535.  
  536. case IDCMP_REFRESHWINDOW:
  537. done=refresh_window();
  538. break;
  539.  
  540. default:
  541. printf("Unknown IDCMP message\n");
  542. done=TRUE;
  543. break;
  544. }
  545. }
  546.  
  547. printf("LEAVING message handler\n");
  548. return(done);
  549. }
  550.  
  551.  
  552.  
  553.  
  554. int main()
  555. {
  556. int i,return_code,ctrlc=0,finish=0,zz=0;
  557. int error=0;
  558. int scrnwidth=0,scrnheight=0;
  559.  
  560. unsigned long signals;
  561. screen = LockPubScreen(screenName);
  562. if (screen) /*locked the screen so we can now read info */
  563. {
  564. if (drawinfo=GetScreenDrawInfo(screen))
  565. {
  566.  
  567. myTEXTPEN = drawinfo->dri_Pens[TEXTPEN];
  568. myBACKGROUNDPEN = drawinfo->dri_Pens[BACKGROUNDPEN];
  569.  
  570. /* create a TextAttr that matches the specified font. */
  571.  
  572. myTextAttr.ta_Name = drawinfo->dri_Font->tf_Message.mn_Node.ln_Name;
  573. myTextAttr.ta_YSize = drawinfo->dri_Font->tf_YSize;
  574. myTextAttr.ta_Style = drawinfo->dri_Font->tf_Style;
  575. myTextAttr.ta_Flags = drawinfo->dri_Font->tf_Flags;
  576. FreeScreenDrawInfo(screen,drawinfo);
  577. }
  578.  
  579. viewPort = &screen->ViewPort;
  580. colorMap = viewPort->ColorMap;
  581. GetRGB32(colorMap,0,colorMap->Count, colorTable);
  582.  
  583. UnlockPubScreen(NULL,screen);
  584. numcolours=colorMap->Count;
  585. printf("width= %d height=%d\n",screen->Width,screen->Height);
  586. scrnwidth=screen->Width;
  587. scrnheight=screen->Height;
  588. }
  589.  
  590. error=open_window(scrnwidth,scrnheight);
  591.  
  592. if (error==0)
  593. {
  594. for(i=0;i<19;i++)
  595. {
  596. if ((wintemp = AllocVec(sizeof(struct window_keeper), MEMF_CLEAR|MEMF_ANY))==NULL)
  597. {
  598. Printf("Cant allocate memory\n"); /*MSG_ERROR_MEM*/
  599. return 1;
  600. }
  601. /*allocate memory and save string*/
  602. wintemp->line = (char*)malloc((strlen(lines[i])+1)*sizeof(char));
  603.  
  604. sprintf(wintemp->line,"%s",lines[i]);
  605. if (winhead==NULL)
  606. {
  607. winhead = wintail = wintemp;
  608. /*printf("llhead mem= %d\n",&llhead);*/
  609. /*printf("-----llhead->next = %d\n", llhead->next);*/
  610. }
  611. else
  612. {
  613. wintail->next = wintemp;
  614. wintail = wintemp;
  615. /*printf("lltail mem =%d\n",&lltail);*/
  616. }
  617.  
  618. winline++;
  619. wintemp->lineno=winline;
  620. wintemp->linetype=1; /*<<< in real code this varies from 1 to 4 depending on type of line*/
  621. wintemp->full=100; /*<<< in real code this is calculated*/
  622.  
  623. }
  624.  
  625. do
  626. {
  627. error=write_win();
  628. ctrlc=FALSE;
  629. finish=FALSE;
  630.  
  631. while ((error==0) && (ctrlc==FALSE) && (finish==FALSE))
  632. {
  633. printf("waiting/n");
  634. signals = Wait (1L << mywindow->UserPort->mp_SigBit | SIGBREAKF_CTRL_C);
  635.  
  636. printf("have signal %d\n",signals);
  637.  
  638. if (CheckSignal(SIGBREAKF_CTRL_C))
  639. ctrlc=TRUE;
  640.  
  641.  
  642. printf("about to check signal\n");
  643.  
  644. if (signals & (1L << mywindow->UserPort->mp_SigBit))
  645. finish=handleIDCMP(mywindow,finish);
  646. }
  647.  
  648. printf("BEFORE CLEAR WINDOW STORE space or a abort\n");
  649. zz=0;
  650. while ((zz!=32) && (zz!='a'))
  651. {
  652. zz=getchar();
  653. if (zz=='a')
  654. return 0;
  655. putchar(zz);
  656. }
  657.  
  658. while(winhead)
  659. {
  660. wintemp=winhead->next;
  661. free (winhead->line);
  662. printf("FREED winhead->line\n");
  663. FreeVec (winhead);
  664. printf("FREED WINHEAD\n");
  665. winhead=wintemp;
  666. }
  667. /* clear window*/
  668. SetRast(mywindow->RPort,0); /*0 is pen colour to fill window with*/
  669.  
  670. } while (loop_again);
  671. } /*end if error*/
  672.  
  673.  
  674. /*clean up*/
  675.  
  676. free(win_buffer); /*clear the line buffers*/
  677. free(win_buffer_part);
  678.  
  679. if (win_reg)
  680. {
  681. InstallClipRegion(mywindow->WLayer, win_oldreg);
  682. DisposeRegion(win_reg);
  683. }
  684.  
  685. CloseWindow(mywindow); /*close window if open */
  686.  
  687.  
  688.  
  689.  
  690. return 0;
  691. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement