Jump to content

Creating an app to count mouse clicks


Recommended Posts

I'd like to create an app to count mouse clicks. I'd like to use THINK Pascal, if possible. 

 

The goal is to know how many mouse clicks were used to make a piece of art. 

 

See: Mouse Odometer exists (INIT & app) but only counts movement. 

 

Requirements

- count mouse clicks

- count total persists with app

- count reset

 

Nice to have

- count only when specific apps are frontmost (ie. only in my drawing app)

- count only when click happens in certain part of the screen (ie. only canvas clicks, not toolbar clicks)

- multiple totals (ie. one per piece of art)

 

Any thoughts, relevant example source code, or any other advice is greatly appreciated!

 

cheers,

matt

Link to post
Share on other sites
20 hours ago, gingerbeardman said:

Yes, I'll be using it on System 7. I should have said that.


I don’t have much background but I do remember in the days of 68k System 7 the multitasking was horrible. An app in the background couldn’t really have any cpu cycles while you were in a foreground app. 
 

Im thinking you might need to write this as a CDEV that runs in the OS background. 
 

I wish you luck with this project. 

Link to post
Share on other sites

Thanks!

 

The code above works well, and I adapted it to check the mouse button as well as location. But it results in a modal app rather than a multi-tasking app. 

 

So the next task is to make the correct type of multi-tasking app. I'll look into CDEV too.

 

Some reading to do!

 

 

Edited by gingerbeardman
Link to post
Share on other sites
  • 68kMLA Supporter

Probably the "right" and reasonably straightforward way to do this (though I would defer to @Crutch on this point, who has much more experience than me at this) would be to create an INIT that patches GetNextEvent and adds 1 to a global variable whenever it would return a mouse up to an application.  You could check which application is frontmost and only count when a certain app is.

 

There was someone in this forum a little while ago who wrote a simple similar INIT to enable cursor keys on keyboard that didn't have them (or something).  The basic skeleton of that INIT would be the same as what you'd need here; I'll see if I can find it, though I think it was in C rather than in Pascal.

 

How you'd present this to the user I'm less sure; I suspect the "right" answer is a cdev which just displays the contents of that variable and lets you clear it with a button or something.  Remember you don't have any memory protection under System 7 so so long as you have a way to pass the location from the INIT to the cdev, you don't have to do any weird tricks to do this.

Link to post
Share on other sites
  • 68kMLA Supporter

Hi, cool idea and I agree with @cheesestraws!  That said here are a few ways to do this:

 

1.  Write an INIT that patches GetNextEvent and WaitNextEvent (apps could call either to get an event).  Increment a counter if theEvent.what == mouseDown then pass it on.  Write a cdev to give it a user interface.  This is probably the “right” way to do this.

 

But you could also:

 

2.  Write an application to do this.  As @MrFahrenheit states above, the issue is you have to deal with is the application might not get enough background time to notice the click, and in particular it’s unlikely to get any foreground time at all between the user mousing down on the frontmost application and the front application getting that event (typically you only get background time when the front app would be getting null events).  Also you can’t do it this way by patching traps, since trap patches are local to each application under System 7 (and I think MultiFinder under System 6).  So what to do?  

 

You would need to write a VBL task.  VBL tasks are installed with VInstall() and get called 60 times/second at interrupt time.  If your app installs a VBL task, it will keep running even when you’re in the background!  Then inside your VBL task, you can check for a mouse click.  Except you can’t call GetNextEvent or WaitNextEvent or EventAvail or even Button at interrupt time (they are listed as possibly moving or purging memory).  What you CAN do, though, is see Inside Macintosh Volume III, p. 27 and note that you can find out if the mouse button is down by examining bit 3 of VIA data register B (memory mapped vBase+vBufB, where vBase is stored in lomem global variable “VIA”).  So if you wanted to be tricky, you could check this in a VBL task (this is how the OS gets mouseDown events, by the way).  Then if the mouse is down but was previously up, increment an application global (be sure to see the relevant docs on using globals from a VBL task) used to update the UI in your application.

 

Either way you may find the system global CurApName useful to know which application was in the foreground when the click happened.

 

 

 

Link to post
Share on other sites
  • 68kMLA Supporter
19 hours ago, Crutch said:

Either way you may find the system global CurApName useful to know which application was in the foreground when the click happened.

 

That was the variable I was trying to remember the name of!  The single 'p' always throws me off

Link to post
Share on other sites
  • 68kMLA Supporter

Maybe you could make it show the clicks in the menubar. Kind of like the system 6 clock extension someone made. Then you could see it from your drawing program. I, too, think it would benefit from a control panel. You're going to want to reset the number every once in a while I would think. 

 

When looking for hints, I would include MacTech in my google search. There are tons of great articles in there, and actually quite a few have pascal code examples. You might get lucky!

 

 

Link to post
Share on other sites
  • 68kMLA Supporter
19 hours ago, Frobozz said:

Maybe you could make it show the clicks in the menubar

 

That's a good idea.  Probably easier than a control panel; perhaps just clicking in that area of the menu bar could clear the count, then you wouldn't need a control panel at all.  I'm sure I've seen sample code to do this somewhere but I have no idea where :/

Link to post
Share on other sites

Found some interesting stuff...

 

  • MacTech articles about writing INITs in Pascal.
  • DBL Pascal Library with Pascal INIT source
  • some C stuff manipulating the menu bar

 

 

Quote


/mac/development/source/snippets/menudraw.hqx
   3     2/26/94    BinHex4.0
 
   C routines for drawing the time on the right side of the menu.

/mac/development/source/snippets/patchdrawmenu.hqx
   3     2/26/94    BinHex4.0
 
   C function to patch DrawMenuBar

/mac/development/source/colormdef.sit.hqx
  27     12/8/95     BinHex4.0,StuffIt3.50

   A CodeWarrior project that shows how to implement  
   a color picker mdef (menu) object.  Includes a test app.

/mac/development/source/mbarclock1.0source.sit.hqx
  15    12/26/94    BinHex4.0,StuffIt3.50
 
   THINK C source for /mac/util/multifinder/mbarclockdigital, a
   menubar clock that appears to the left of the Apple Menu.

/mac/development/libraries/dblpascallib.sit.hqx
 376      3/10/94    BinHex4.0,StuffIt3.50
 
   Some dude's collection of Pascal routines, in source form.
   Includes:  Agendas, Animated Cursor, Async Sound, Call Chain,
   Checksum, CRC, Debugger Presence, ICON control CDEF, sicn 
   control CDEF, Pop up menu CDEF, Error Signals, Fast Bit Vectors,
   Hash Tables, INIT Shell, Layer Manager IF, Levenshtein Matching,
   Short Fractions, Submenu Delay, Preferences, Queues, RSRCRuntime
   Bug Fix, SFGetFolder 6, String Lists, Transactions Manager, 
   Dialog Utils, Pop up Utils, Block Zero, VM Traps IF

 

 

Edited by gingerbeardman
Link to post
Share on other sites
  • 68kMLA Supporter

There's also an example here (it's in assembler, but it's short) about drawing (in this case, sarcastic messages) in the menu bar:

 

http://preserve.mactech.com/articles/mactech/Vol.05/05.04/ScrollingMenubar/index.html

 

Look at the drawAMessage routine: this one is a bit simpler / worse-behaved than the menu bar clock example you found above.  It uses the window manager's graphics port and sets up a clip region every time, which sounds like a fun way to cause trouble, but might be easier if your code is well-behaved.

 

So, here's how I think this might work:

  • Tail patch GetNextEvent/WaitNextEvent.  If it's a mouseDown (or a mouseUp, depending on whether you want to count when the button goes down or up!) increment your variable.  If the mouse is, say, in the top-right corner of the screen, reset the count to zero.  Redraw the display.
  • Tail patch DrawMenuBar so that when the menu bar is redrawn, you redraw your display.

 

This seems eminently doable in Pascal to me (personally, I haven't written anything but an application in THINK Pascal but I know that you can write INITs in it).  I'd suggest attacking the menu-bar drawing code first, as it will make debugging the counter easier :-).  I'm happy to join in via this thread and be a second opinion if you want one.

Link to post
Share on other sites
  • 68kMLA Supporter
On 2/26/2021 at 10:43 PM, gingerbeardman said:

Thanks all, have downloaded MacTech Archive and some books and will be reading up.

 

does that mean there's a way to get PDF downloads of the MacTech old issues? I looked around for that before, and didn't find it. Could you share a link? The versions online don't have images, and sometimes the code gets cut off in mid-stream. I wish I had a few physical copies, I love looking at the ads. 

 

Link to post
Share on other sites

v interested in getting the pdf version as well.

 

I have an ISO of a CD full of mactech articles running in my Basilisk II setup, but I'm not sure how complete it is (probably not)

 

the only thing I've done with the menu bar is under System 6 and below, to hide/reveal it, here's my source code for it. Gotta love incompatible exact memory locations:

https://mu0n.github.io/ForayInto68k/code/NoMenuBar.c.txt

 

I'll be interested to see the result when you get there.

 

 

Link to post
Share on other sites
  • 68kMLA Supporter

That approach to hiding the menu bar (set MBarHeight to 0 and increase the size of Gray Rgn) should work on any version of the classic Mac OS, in general Apple was pretty good about not messing with the documented lomem globals.  That code exists more or less exactly in one of Knaster’s books also, probably Macintosh Programming Secrets.

 

If all you want to do is draw on the menu bar, you can just call _OpenPort and do it (the default portRect includes the whole universe, -32768 to 32767 in each direction).  So, your INIT could probably work without patching _DrawMenubar just by re-drawing your counter every so often.  Of course you can’t call DrawString from a VBL task so you’d have to spin up a driver to do that, probably easier to just patch the trap but you don’t really have to.

 

I would also like to know how to “download the MacTech archive” since I am always worried their website will disappear one day.  I can say that the old phonebook-style anthologies (“The Essential MacTutor”, etc.) do appear on eBay or AbeBooks from time to time, and are well-indexed and wonderful.

 

Link to post
Share on other sites
  • 68kMLA Supporter

Yeah, I suggested patching DrawMenuBar because that seemed to be the lowest-faff way of doing it and doesn't require coordinating the activity of multiple code resources.  I mean, if you're going to have a driver you may as well go the whole hog and have a CDEV for it to show the counts etc :-)

Link to post
Share on other sites
On 2/27/2021 at 5:59 PM, Frobozz said:

 

does that mean there's a way to get PDF downloads of the MacTech old issues? I looked around for that before, and didn't find it. Could you share a link? The versions online don't have images, and sometimes the code gets cut off in mid-stream. I wish I had a few physical copies, I love looking at the ads. 

 

I means I downloaded the MacTech ISO from garden. I've not heard of MacTech before. 

 

A quick search shows 152 issues available and searchable and downloadable on internet archive. 

 

https://archive.org/search.php?query=MacTech OCR

 

Enjoy!

 

 

Edited by gingerbeardman
Link to post
Share on other sites
  • 68kMLA Supporter

Thanks.  Those look like mostly ‘00s issues so maybe not as directly relevant for your project but still cool to see.  (The older MacTech issues were called MacTutor and are archived on the MacTech website in plain text form.)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...