23 January 2024, 14:12 | #1 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Passing Icon ToolTypes values to the window
Hello,
I made the code to display some hardware variables in a small window. When the window is called, the global int variables: scrLeft, scrTop have the values I gave to the icon ToolTypes for the location of the window, but despite, it keeps openning in the location 0,0 why? Any help is welcomed Code:
#include <proto/all.h> #include <exec/types.h> #include <intuition/intuition.h> #include <libraries/dos.h> #include <stdlib.h> #include <stdio.h> #define scrWidth 180 #define scrHeight 90 int scrLeft; int scrTop; int refresh; // Recebe os files com as variaveis, abre-os, extrai e envia as variaveis char *readFile(const char *envName) { struct FILE *file; char fileName[100]; char *buffer = NULL; size_t length = 0; size_t bytesRead; //Abre SensorFile para ler variavel sprintf(fileName,"ENV:%s",envName); file=fopen(fileName,"r"); if(file==NULL){ perror("Nao consegui abrir a variavel"); return -1; } // Determina o tamanho do arquivo fseek(file, 0, SEEK_END); length = ftell(file); fseek(file, 0, SEEK_SET); // Aloca memória para o buffer buffer = (char *)malloc(length + 1); // +1 para o caractere nulo de terminação // Passa o conteúdo do arquivo para o buffer bytesRead = fread(buffer, 1, length, file); // Adiciona caracter nulo de terminação no final do buffer buffer[bytesRead] = '\0'; fclose(file); return buffer; } static STRPTR conwinname = "CON:30/50/500/400/Tooltype parsing/AUTO/CLOSE/WAIT"; static BPTR conwin; static BPTR olddir = (BPTR)-1; static void read_icon(struct WBArg *wbarg); int main(int argc, char** argv) { struct Window *myWindow; int hasEnded = FALSE; struct IntuiMessage *msg; ULONG msgClass; char *str; int i; char *varName[]= { "AK_5V_supply_V", "AK_Internal_temp_C", "AK_RTC_battery_percent" }; // open window on the public workbench screen struct NewWindow winlayout = { scrLeft, scrTop, scrWidth, scrHeight, 0,1, // IDCMP_CLOSEWINDOW, // WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_ACTIVATE, NULL, NULL, NULL, NULL, // "Volts / Temp", NULL, NULL,NULL, 0,0, 600,400, WBENCHSCREEN }; if (argc) { clean_exit("Application must be started from Workbench."); /* See dos_readargs.c for start from Shell. */ } else { struct WBArg *wbarg; struct WBStartup *wbmsg = (struct WBStartup *)argv; LONG idx; /* An application started from Workbench doesn't have a console window for output. We have to open our own con: window or all output will go to Nirwana. */ conwin = fopen(conwinname, "w"); if (!conwin) { clean_exit("Can't open console window"); } /* Loop trough all icons. The first one is the application itself. */ for(idx = 0, wbarg = wbmsg->sm_ArgList; idx < wbmsg->sm_NumArgs; idx++, wbarg++) { if ((wbarg->wa_Lock) && (*wbarg->wa_Name)) { fprintf(conwin, "\n\nName %s\n", wbarg->wa_Name); /* We must enter the directory of the icon */ olddir = CurrentDir(wbarg->wa_Lock); read_icon(wbarg); /* Switch back to old directory. It's important that the directory which was active at program start is set when the application is quit. */ if (olddir != (BPTR)-1) { CurrentDir(olddir); olddir = (BPTR)-1; } } } fprintf(conwin, "\nLOCATE_X:: %i\n", scrTop); fprintf(conwin, "LOCATE_Y:: %i\n", scrLeft); fprintf(conwin, "REFRESH:: %i\n", refresh); /* Tratamento dos valores das variaveis */ myWindow = OpenWindow(&winlayout); i=0; // Defina a cor do texto SetAPen(myWindow->RPort, i + 1); while(i<3){ str=readFile(varName[i]); if(i==0){ Move(myWindow->RPort, 20, 30); Text(myWindow->RPort,"Volts: ",7); Text(myWindow->RPort, str, strlen(str)); Text(myWindow->RPort," V",2); } if(i==1){ Move(myWindow->RPort, 20, 50); Text(myWindow->RPort,"Temp: ",7); Text(myWindow->RPort, str, strlen(str)-1); Text(myWindow->RPort," oC",3); } if(i==2){ Move(myWindow->RPort, 20, 70); Text(myWindow->RPort,"Bat: ",7); Text(myWindow->RPort, str, strlen(str)-3); Text(myWindow->RPort," %",2); } i++; } while (hasEnded == FALSE) { // wait for and event Wait(1L << myWindow->UserPort->mp_SigBit); // get message msg = GT_GetIMsg(myWindow->UserPort); // get class of message; msgClass = msg->Class; // handle message (close it); GT_ReplyIMsg(msg); if (msgClass == IDCMP_CLOSEWINDOW) { CloseWindow(myWindow); free(str); hasEnded = TRUE; } } } clean_exit(NULL); return 0; } static void read_icon(struct WBArg *wbarg) { struct DiskObject *dobj; STRPTR *toolarray; STRPTR result; /* le os ToolTypes do icon */ dobj = GetDiskObject(wbarg->wa_Name); if (dobj) { toolarray = dobj->do_ToolTypes; result = FindToolType(toolarray, "LOCATE_X"); if (result) { scrTop=atol(result); //converte string to long fprintf(conwin, "LOCATE_X: %i\n", scrTop); } else { fputs("Tooltype 'LOCATE_X' doesn't exist\n", conwin); } result = FindToolType(toolarray, "LOCATE_Y"); if(result) { scrLeft=atol(result); fprintf(conwin, "LOCATE_Y: %i\n", scrLeft); } else { fputs("Tooltype 'LOCATE_Y' doesn't exist\n", conwin); } result = FindToolType(toolarray, "REFRESH"); if (result) { refresh=atol(result); //converte string to long fprintf(conwin, "REFRESH: %i\n", refresh); } else { fputs("Tooltype 'REFRESH' doesn't exist\n", conwin); } FreeDiskObject(dobj); } else { fputs("Can't open DiskObjekt\n", conwin); } return; } static void clean_exit(CONST_STRPTR s) { if (s) { if (conwin) { fprintf(conwin, "%s\n", s); } else { printf("%s\n", s); } } /* Give back allocated resourses */ if (conwin) fclose(conwin); exit(0); } // so could run trhough wb icon in case of StormC or StormGCC #if defined __STORM__ || defined __STORMGCC__ void wbmain(struct WBStartup* wbmsg) { main(0, (char **) wbmsg); } #endif |
23 January 2024, 15:00 | #2 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,646
|
your winlayout structure is initialized *before* you call read_icon(), which means that it is initialized with the default value of 0 from the scr* globals.
|
23 January 2024, 15:00 | #3 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,365
|
You initialize scrLeft, scrTop after winlayout. So uninitialized value (0 by accident) is used.
To fix this, you can leave zero or whatever in the first two fields of your NewWindow, and change them after values are known. |
23 January 2024, 22:59 | #4 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Thanks for the reply but, the compiler makes me to declare and fill the NewWindow structure at the begining of main() function, otherwise it gives me the error "parse error before..." at the line of NewWindow structure fill.
Anyway the window is called after the read_icon() function with " myWindow = OpenWindow(&winlayout); ", it shouldn´t assume the values that the global variables scrTop & scrLeft have at that time? |
23 January 2024, 23:55 | #5 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,068
|
Do winlayout.XYZ = scrXYZ; (for each of the 4 variables) after calling read_icon() to update the values in the NewWindow structure.
|
24 January 2024, 07:39 | #6 | |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,365
|
Quote:
It's not C++ either, which would have allowed declaration and initialization of a structure right in the middle of the function. So do like a/b said and it should solve your issue. |
|
24 January 2024, 11:21 | #7 |
Registered User
Join Date: Sep 2022
Location: Switzerland
Posts: 120
|
Obviously you are programming for OS 2 and higher? => Why don't you use OpenWindowTagList ?
GT_GetIMsg ? Are you planning to add a GadTools GUI ? |
24 January 2024, 11:34 | #8 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
Thanks for the help guys
I will try OpenWindowTagLIst but I just would like to understand why it couldn't do with the NewWindow structure. @No.3: Im not intend to add a GadTools GUI, this is some old code snippet I´m just not cleaned yet |
24 January 2024, 12:10 | #9 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
@a/b: It works with your tip..
winlayout.TopEdge=scrTop; winlayout.LeftEdge=scrLeft; thanks |
24 January 2024, 12:41 | #10 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,646
|
There's nothing wrong with using a NewWindow structure, but as already said by everyone in the thread, the values of the globals are read where you initialize your struct. The struct definition specifies numbers here, so there's no way it could reference your globals (to be read later) - this will probably get clearer to you as you gain more experience with programming
|
24 January 2024, 22:34 | #11 |
A1200 040 SAM440EP 667
Join Date: Jan 2008
Location: Lisbon / Portugal
Posts: 873
|
@meynaf & @hooverphonique: Thank you for your explanations, I understand now
I had the wrong idea that, when initialized, the NewWindow structure acquired the initial values of the global variables but, at the time the window was called the NewWindow structure would update the values through variables name Now I know how the process acts, instead of the variables name, for the struct is just values |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Changing Icon Tool Type Values on Mass | manic23 | support.Other | 15 | 27 April 2019 15:39 |
Icon for script on main Workbench window? | Foebane | support.Apps | 31 | 10 May 2018 22:05 |
Tray icon stays after closing log window | BlankVector | support.WinUAE | 4 | 11 December 2016 23:04 |
Icon tooltypes and stuff | oRBIT | Coders. General | 22 | 23 May 2010 21:27 |
Passing arguments to scripts | Leffmann | Coders. General | 4 | 23 January 2009 20:43 |
|
|